1
0
Fork 0
forked from Miroirs/x49gp

Actually create config.lua if needed or requested

This commit is contained in:
Gwenhael Le Moine 2024-11-20 14:50:10 +01:00
parent 6381ab9d29
commit e58daf23e2
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
4 changed files with 83 additions and 45 deletions

1
dist/x50ng.scd vendored
View file

@ -52,6 +52,7 @@ Please consult HP's manual (for their official firmware) or the documentation of
- *-h* *--help* print this message and exit - *-h* *--help* print this message and exit
- *--verbose* print out more information - *--verbose* print out more information
- *--datadir*[=_absolute path_] alternate datadir (default: $XDG_CONFIG_HOME/x49gpng/) - *--datadir*[=_absolute path_] alternate datadir (default: $XDG_CONFIG_HOME/x49gpng/)
- *--overwrite-config* force writing <datadir>/config.lua even if it exists
- *--50g* emulate an HP 50g (default) - *--50g* emulate an HP 50g (default)
- *--49gp* emulate an HP 49g+ - *--49gp* emulate an HP 49g+
- *--newrpl-keyboard* label keyboard for newRPL - *--newrpl-keyboard* label keyboard for newRPL

View file

@ -285,7 +285,7 @@ int main( int argc, char** argv )
if ( x49gp_modules_init( x49gp ) ) if ( x49gp_modules_init( x49gp ) )
exit( EXIT_FAILURE ); exit( EXIT_FAILURE );
int error = x49gp_modules_load( x49gp, opt.state_filename ); int error = x49gp_modules_load( x49gp );
if ( error || opt.reinit >= X49GP_REINIT_REBOOT_ONLY ) { if ( error || opt.reinit >= X49GP_REINIT_REBOOT_ONLY ) {
if ( error && error != -EAGAIN ) if ( error && error != -EAGAIN )
exit( EXIT_FAILURE ); exit( EXIT_FAILURE );
@ -314,7 +314,10 @@ int main( int argc, char** argv )
x49gp_main_loop( x49gp ); x49gp_main_loop( x49gp );
x49gp_modules_save( x49gp, opt.state_filename ); x49gp_modules_save( x49gp );
if ( !opt.haz_config_file )
save_config();
x49gp_modules_exit( x49gp ); x49gp_modules_exit( x49gp );
return 0; return 0;

View file

@ -12,6 +12,8 @@
#include "gdbstub.h" #include "gdbstub.h"
#define CONFIG_LUA_FILE_NAME "config.lua"
struct options opt = { struct options opt = {
.datadir = NULL, .datadir = NULL,
@ -90,25 +92,55 @@ static inline bool config_read( const char* filename )
return true; return true;
} }
static void print_config( void ) static char* config_to_string( void )
{ {
fprintf( stdout, "--------------------------------------------------------------------------------\n" ); char* config;
fprintf( stdout, "-- Configuration file for x50ng\n" ); asprintf( &config,
fprintf( stdout, "-- This is a comment\n" ); "--------------------------------------------------------------------------------\n"
"-- Configuration file for x50ng\n"
"-- This is a comment\n"
"name = \"%s\" -- this customize the title of the window\n"
"model = \"%s\" -- possible values: \"49gp\", \"50g\". Changes the colors and the bootloader looked for when (re-)flashing\n"
"newrpl_keyboard = %s -- when true this makes the keyboard labels more suited to newRPL use\n"
"font = \"%s\"\n"
"font_size = %i -- integer only\n"
"display_scale = %i -- integer only\n"
"verbose = %s\n"
"--- End of x50ng configuration -----------------------------------------------\n",
opt.name, opt.model == MODEL_50G ? "50g" : "49gp", opt.newrpl ? "true" : "false", opt.font, opt.font_size, opt.display_scale,
opt.verbose ? "true" : "false" );
fprintf( stdout, "name = \"%s\" -- this customize the title of the window\n", opt.name ); return config;
fprintf( stdout, }
"model = \"%s\" -- possible values: \"49gp\", \"50g\". Changes the colors and the bootloader looked for when (re-)flashing\n",
opt.model == MODEL_50G ? "50g" : "49gp" );
fprintf( stdout, "newrpl_keyboard = %s -- when true this makes the keyboard labels more suited to newRPL use\n",
opt.newrpl ? "true" : "false" );
fprintf( stdout, "font = \"%s\"\n", opt.font );
fprintf( stdout, "font_size = %i -- integer only\n", opt.font_size );
fprintf( stdout, "display_scale = %i -- integer only\n", opt.display_scale );
fprintf( stdout, "verbose = %s\n", opt.verbose ? "true" : "false" ); int save_config( void )
{
int error;
const char* config_lua_filename = g_build_filename( opt.datadir, CONFIG_LUA_FILE_NAME, NULL );
int fd = open( config_lua_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644 );
fprintf( stdout, "--- End of x50ng configuration -----------------------------------------------\n" ); if ( fd < 0 ) {
error = -errno;
fprintf( stderr, "%s:%u: open %s: %s\n", __FUNCTION__, __LINE__, config_lua_filename, strerror( errno ) );
return error;
}
char* data = config_to_string();
if ( write( fd, data, strlen( data ) ) != strlen( data ) ) {
error = -errno;
fprintf( stderr, "%s:%u: write %s: %s\n", __FUNCTION__, __LINE__, config_lua_filename, strerror( errno ) );
close( fd );
g_free( data );
return error;
}
close( fd );
g_free( data );
if ( opt.verbose )
fprintf( stderr, "Current configuration has been written to %s\n", config_lua_filename );
return EXIT_SUCCESS;
} }
void config_init( char* progname, int argc, char* argv[] ) void config_init( char* progname, int argc, char* argv[] )
@ -131,6 +163,7 @@ void config_init( char* progname, int argc, char* argv[] )
int clopt_display_scale = -1; int clopt_display_scale = -1;
int print_config_and_exit = false; int print_config_and_exit = false;
int overwrite_config = false;
const char* optstring = "hrf:n:s:S:"; const char* optstring = "hrf:n:s:S:";
struct option long_options[] = { struct option long_options[] = {
@ -138,6 +171,7 @@ void config_init( char* progname, int argc, char* argv[] )
{"print-config", no_argument, &print_config_and_exit, true}, {"print-config", no_argument, &print_config_and_exit, true},
{"verbose", no_argument, &clopt_verbose, true}, {"verbose", no_argument, &clopt_verbose, true},
{"overwrite-config", no_argument, &overwrite_config, true},
{"datadir", required_argument, NULL, 1 }, {"datadir", required_argument, NULL, 1 },
{"50g", no_argument, NULL, 506 }, {"50g", no_argument, NULL, 506 },
@ -171,6 +205,8 @@ void config_init( char* progname, int argc, char* argv[] )
"\n" "\n"
" --datadir[=<absolute path>] alternate datadir (default: $XDG_CONFIG_HOME/%s/)\n" " --datadir[=<absolute path>] alternate datadir (default: $XDG_CONFIG_HOME/%s/)\n"
"\n" "\n"
" --overwrite-config force writing <datadir>/config.lua even if it exists\n"
"\n"
" --50g emulate an HP 50g (default)\n" " --50g emulate an HP 50g (default)\n"
" --49gp emulate an HP 49g+\n" " --49gp emulate an HP 49g+\n"
" --newrpl-keyboard label keyboard for newRPL\n" " --newrpl-keyboard label keyboard for newRPL\n"
@ -247,13 +283,13 @@ void config_init( char* progname, int argc, char* argv[] )
if ( opt.datadir == NULL ) if ( opt.datadir == NULL )
opt.datadir = g_build_filename( g_get_user_config_dir(), progname, NULL ); opt.datadir = g_build_filename( g_get_user_config_dir(), progname, NULL );
const char* config_lua_filename = g_build_filename( opt.datadir, "config.lua", NULL ); const char* config_lua_filename = g_build_filename( opt.datadir, CONFIG_LUA_FILE_NAME, NULL );
/**********************/ /**********************/
/* 1. read config.lua */ /* 1. read config.lua */
/**********************/ /**********************/
bool haz_config_file = config_read( config_lua_filename ); opt.haz_config_file = config_read( config_lua_filename );
if ( haz_config_file ) { if ( opt.haz_config_file ) {
lua_getglobal( config_lua_values, "newrpl_keyboard" ); lua_getglobal( config_lua_values, "newrpl_keyboard" );
opt.newrpl = lua_toboolean( config_lua_values, -1 ); opt.newrpl = lua_toboolean( config_lua_values, -1 );
@ -288,6 +324,8 @@ void config_init( char* progname, int argc, char* argv[] )
lua_getglobal( config_lua_values, "display_scale" ); lua_getglobal( config_lua_values, "display_scale" );
opt.display_scale = luaL_optinteger( config_lua_values, -1, opt.display_scale ); opt.display_scale = luaL_optinteger( config_lua_values, -1, opt.display_scale );
} }
if ( opt.haz_config_file && overwrite_config )
opt.haz_config_file = false;
/****************************************************/ /****************************************************/
/* 2. treat command-line params which have priority */ /* 2. treat command-line params which have priority */
@ -308,17 +346,11 @@ void config_init( char* progname, int argc, char* argv[] )
opt.display_scale = clopt_display_scale; opt.display_scale = clopt_display_scale;
if ( print_config_and_exit ) { if ( print_config_and_exit ) {
print_config(); fprintf( stdout, config_to_string() );
exit( EXIT_SUCCESS ); exit( EXIT_SUCCESS );
} }
if ( opt.verbose ) if ( opt.verbose )
print_config(); fprintf( stdout, config_to_string() );
if ( !haz_config_file ) {
fprintf( stderr, "\nConfiguration file %s doesn't seem to exist or is invalid!\n", config_lua_filename );
fprintf( stderr, "You can solve this by running `mkdir -p %s/ && %s --print-config >> %s`\n\n", opt.datadir, progname,
config_lua_filename );
}
if ( do_enable_debugger ) { if ( do_enable_debugger ) {
if ( opt.debug_port == 0 ) if ( opt.debug_port == 0 )

View file

@ -31,6 +31,7 @@ struct options {
int display_scale; int display_scale;
int font_size; int font_size;
bool verbose; bool verbose;
bool haz_config_file;
char* datadir; char* datadir;
}; };
@ -41,5 +42,6 @@ extern struct options opt;
/* functions */ /* functions */
/*************/ /*************/
extern void config_init( char* progname, int argc, char* argv[] ); extern void config_init( char* progname, int argc, char* argv[] );
extern int save_config( void );
#endif /* !_CONFIG_H */ #endif /* !_CONFIG_H */