1
0
Fork 0
forked from Miroirs/x49gp

Compare commits

...

2 commits

Author SHA1 Message Date
Gwenhael Le Moine
6089396a7a
load (limited) configuration from ~/.config/x49gpng/config.lua is present 2024-11-10 18:09:15 +01:00
Gwenhael Le Moine
94b8e7b04d
So-called config file is really about state. Name it so. 2024-11-10 16:37:36 +01:00
6 changed files with 264 additions and 85 deletions

View file

@ -7,12 +7,19 @@ PATCHLEVEL = 0
# #
DEBUG_CFLAGS = -g # -pg DEBUG_CFLAGS = -g # -pg
OPTIM = 2 OPTIM ?= 2
LUA_VERSION ?= lua
PKG_CONFIG ?= pkg-config
### lua
LUACFLAGS = $(shell "$(PKG_CONFIG)" --cflags $(LUA_VERSION))
LUALIBS = $(shell "$(PKG_CONFIG)" --libs $(LUA_VERSION))
# GTK # GTK
GTK_VERSION = "+-3.0" GTK_VERSION ?= "+-3.0"
GTK_CFLAGS = $(shell pkg-config --cflags gtk$(GTK_VERSION)) -DGTK_DISABLE_SINGLE_INCLUDES -DGSEAL_ENABLE GTK_CFLAGS = $(shell "$(PKG_CONFIG)" --cflags gtk$(GTK_VERSION)) -DGTK_DISABLE_SINGLE_INCLUDES -DGSEAL_ENABLE
GTK_LDLIBS = $(shell pkg-config --libs gtk$(GTK_VERSION)) -lz -lm GTK_LDLIBS = $(shell "$(PKG_CONFIG)" --libs gtk$(GTK_VERSION)) -lz -lm
# Embedded qemu # Embedded qemu
QEMU_DIR = src/qemu-git QEMU_DIR = src/qemu-git
@ -87,6 +94,7 @@ X49GP_CFLAGS = $(CFLAGS) \
$(X49GP_INCLUDES) \ $(X49GP_INCLUDES) \
$(QEMU_DEFINES) \ $(QEMU_DEFINES) \
$(GTK_CFLAGS) \ $(GTK_CFLAGS) \
$(LUACFLAGS) \
-D_GNU_SOURCE=1 \ -D_GNU_SOURCE=1 \
-DVERSION_MAJOR=$(VERSION_MAJOR) \ -DVERSION_MAJOR=$(VERSION_MAJOR) \
-DVERSION_MINOR=$(VERSION_MINOR) \ -DVERSION_MINOR=$(VERSION_MINOR) \
@ -99,7 +107,7 @@ ifeq ($(DEBUG), yes)
endif endif
X49GP_LDFLAGS = $(DEBUG_CFLAGS) $(LDFLAGS) X49GP_LDFLAGS = $(DEBUG_CFLAGS) $(LDFLAGS)
X49GP_LDLIBS = $(QEMU_OBJS) $(GDB_LIBS) $(COCOA_LIBS) $(GTK_LDLIBS) X49GP_LDLIBS = $(QEMU_OBJS) $(GDB_LIBS) $(COCOA_LIBS) $(GTK_LDLIBS) $(LUALIBS)
SRCS = ./src/x49gpng/main.c \ SRCS = ./src/x49gpng/main.c \
./src/x49gpng/module.c \ ./src/x49gpng/module.c \

View file

@ -278,12 +278,12 @@ int main( int argc, char** argv )
if ( x49gp_modules_init( x49gp ) ) if ( x49gp_modules_init( x49gp ) )
exit( EXIT_FAILURE ); exit( EXIT_FAILURE );
x49gp->basename = g_path_get_dirname( opt.config ); x49gp->basename = g_path_get_dirname( opt.state_filename );
x49gp->debug_port = opt.debug_port; x49gp->debug_port = opt.debug_port;
x49gp->startup_reinit = opt.reinit; x49gp->startup_reinit = opt.reinit;
x49gp->firmware = opt.firmware; x49gp->firmware = opt.firmware;
int error = x49gp_modules_load( x49gp, opt.config ); int error = x49gp_modules_load( x49gp, opt.state_filename );
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 );
@ -312,7 +312,7 @@ int main( int argc, char** argv )
x49gp_main_loop( x49gp ); x49gp_main_loop( x49gp );
x49gp_modules_save( x49gp, opt.config ); x49gp_modules_save( x49gp, opt.state_filename );
x49gp_modules_exit( x49gp ); x49gp_modules_exit( x49gp );
#if false #if false

View file

@ -100,16 +100,16 @@ int x49gp_modules_load( x49gp_t* x49gp, const char* filename )
return error; return error;
} }
x49gp->config = g_key_file_new(); x49gp->state_filename = g_key_file_new();
if ( NULL == x49gp->config ) { if ( NULL == x49gp->state_filename ) {
fprintf( stderr, "%s:%u: g_key_file_new: Out of memory\n", __FUNCTION__, __LINE__ ); fprintf( stderr, "%s:%u: g_key_file_new: Out of memory\n", __FUNCTION__, __LINE__ );
return -ENOMEM; return -ENOMEM;
} }
if ( !g_key_file_load_from_file( x49gp->config, filename, G_KEY_FILE_KEEP_COMMENTS, &gerror ) && if ( !g_key_file_load_from_file( x49gp->state_filename, filename, G_KEY_FILE_KEEP_COMMENTS, &gerror ) &&
!g_error_matches( gerror, G_FILE_ERROR, G_FILE_ERROR_NOENT ) ) { !g_error_matches( gerror, G_FILE_ERROR, G_FILE_ERROR_NOENT ) ) {
fprintf( stderr, "%s:%u: g_key_file_load_from_file: %s\n", __FUNCTION__, __LINE__, gerror->message ); fprintf( stderr, "%s:%u: g_key_file_load_from_file: %s\n", __FUNCTION__, __LINE__, gerror->message );
g_key_file_free( x49gp->config ); g_key_file_free( x49gp->state_filename );
return -EIO; return -EIO;
} }
@ -117,7 +117,7 @@ int x49gp_modules_load( x49gp_t* x49gp, const char* filename )
list_for_each_entry( module, &x49gp->modules, list ) list_for_each_entry( module, &x49gp->modules, list )
{ {
error = module->load( module, x49gp->config ); error = module->load( module, x49gp->state_filename );
if ( error ) { if ( error ) {
if ( error == -EAGAIN ) if ( error == -EAGAIN )
result = -EAGAIN; result = -EAGAIN;
@ -126,15 +126,15 @@ int x49gp_modules_load( x49gp_t* x49gp, const char* filename )
} }
} }
#ifdef DEBUG_X49GP_MODULES
{ {
extern unsigned char* phys_ram_base; extern unsigned char* phys_ram_base;
#ifdef DEBUG_X49GP_MODULES
printf( "%s: phys_ram_base: %p\n", __FUNCTION__, phys_ram_base ); printf( "%s: phys_ram_base: %p\n", __FUNCTION__, phys_ram_base );
printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n", phys_ram_base[ 0 ], phys_ram_base[ 1 ], phys_ram_base[ 2 ], printf( "\t%02x %02x %02x %02x %02x %02x %02x %02x\n", phys_ram_base[ 0 ], phys_ram_base[ 1 ], phys_ram_base[ 2 ],
phys_ram_base[ 3 ], phys_ram_base[ 4 ], phys_ram_base[ 5 ], phys_ram_base[ 6 ], phys_ram_base[ 7 ] ); phys_ram_base[ 3 ], phys_ram_base[ 4 ], phys_ram_base[ 5 ], phys_ram_base[ 6 ], phys_ram_base[ 7 ] );
#endif
} }
#endif
return result; return result;
} }
@ -154,12 +154,12 @@ int x49gp_modules_save( x49gp_t* x49gp, const char* filename )
list_for_each_entry( module, &x49gp->modules, list ) list_for_each_entry( module, &x49gp->modules, list )
{ {
error = module->save( module, x49gp->config ); error = module->save( module, x49gp->state_filename );
if ( error ) if ( error )
return error; return error;
} }
data = g_key_file_to_data( x49gp->config, &length, &gerror ); data = g_key_file_to_data( x49gp->state_filename, &length, &gerror );
if ( NULL == data ) { if ( NULL == data ) {
fprintf( stderr, "%s:%u: g_key_file_to_data: %s\n", __FUNCTION__, __LINE__, gerror->message ); fprintf( stderr, "%s:%u: g_key_file_to_data: %s\n", __FUNCTION__, __LINE__, gerror->message );
return -ENOMEM; return -ENOMEM;

View file

@ -5,61 +5,166 @@
#include <getopt.h> #include <getopt.h>
#include <glib.h> #include <glib.h>
#include <lua.h>
#include <lauxlib.h>
#include "options.h" #include "options.h"
#include "gdbstub.h" #include "gdbstub.h"
struct options opt; struct options opt = {
.config_lua_filename = NULL,
.state_filename = NULL,
.debug_port = 0,
.start_debugger = false,
.reinit = X49GP_REINIT_NONE,
.firmware = NULL,
.model = MODEL_50G,
.name = NULL,
.text_scale = 1,
.display_scale = 2,
#if defined( __linux__ )
.font = "urw gothic l",
#else
.font = "Century Gothic",
#endif
};
lua_State* config_lua_values;
static inline bool config_read( const char* filename )
{
int rc;
assert( filename != NULL );
/*---------------------------------------------------
; Create the Lua state, which includes NO predefined
; functions or values. This is literally an empty
; slate.
;----------------------------------------------------*/
config_lua_values = luaL_newstate();
if ( config_lua_values == NULL ) {
fprintf( stderr, "cannot create Lua state\n" );
return false;
}
/*-----------------------------------------------------
; For the truly paranoid about sandboxing, enable the
; following code, which removes the string library,
; which some people find problematic to leave un-sand-
; boxed. But in my opinion, if you are worried about
; such attacks in a configuration file, you have bigger
; security issues to worry about than this.
;------------------------------------------------------*/
#ifdef PARANOID
lua_pushliteral( config_lua_values, "x" );
lua_pushnil( config_lua_values );
lua_setmetatable( config_lua_values, -2 );
lua_pop( config_lua_values, 1 );
#endif
/*-----------------------------------------------------
; Lua 5.2+ can restrict scripts to being text only,
; to avoid a potential problem with loading pre-compiled
; Lua scripts that may have malformed Lua VM code that
; could possibly lead to an exploit, but again, if you
; have to worry about that, you have bigger security
; issues to worry about. But in any case, here I'm
; restricting the file to "text" only.
;------------------------------------------------------*/
rc = luaL_loadfile( config_lua_values, filename );
if ( rc != LUA_OK ) {
/* fprintf( stderr, "Lua error: (%d) %s\n", rc, lua_tostring( config_lua_values, -1 ) ); */
return false;
}
rc = lua_pcall( config_lua_values, 0, 0, 0 );
if ( rc != LUA_OK ) {
/* fprintf( stderr, "Lua error: (%d) %s\n", rc, lua_tostring( config_lua_values, -1 ) ); */
return false;
}
return true;
}
static void print_config( void )
{
fprintf( stdout, "--------------------------------------------------------------------------------\n" );
fprintf( stdout, "-- Configuration file for x49gpng\n" );
fprintf( stdout, "-- This is a comment\n" );
fprintf( stdout, "name = \"%s\"\n", opt.name );
fprintf( stdout, "model = \"" );
switch ( opt.model ) {
case MODEL_49GP:
fprintf( stdout, "49gp" );
break;
case MODEL_49GP_NEWRPL:
fprintf( stdout, "49gp-newrpl" );
break;
case MODEL_50G:
fprintf( stdout, "50g" );
break;
case MODEL_50G_NEWRPL:
fprintf( stdout, "50g-newrpl" );
break;
}
fprintf( stdout, "\" -- possible values: \"49gp\", \"50g\", \"49gp-newrpl\", \"50g-newrpl\"\n" );
fprintf( stdout, "font = \"%s\"\n", opt.font );
fprintf( stdout, "text_scale = %i\n", opt.text_scale );
fprintf( stdout, "display_scale = %i\n", opt.display_scale );
fprintf( stdout, "--- End of saturnng configuration ----------------------------------------------\n" );
}
void config_init( char* progname, int argc, char* argv[] ) void config_init( char* progname, int argc, char* argv[] )
{ {
int option_index; int option_index;
int c = '?'; int c = '?';
char* config_lua_filename = ( char* )"config.lua";
bool do_enable_debugger = false; bool do_enable_debugger = false;
bool do_start_debugger = false; bool do_start_debugger = false;
bool do_reflash = false; bool do_reflash = false;
bool do_reflash_full = false; bool do_reflash_full = false;
opt.config = NULL; char* clopt_name = NULL;
opt.debug_port = 0; char* clopt_font = NULL;
opt.start_debugger = false; int clopt_model = -1;
opt.reinit = X49GP_REINIT_NONE; int clopt_text_scale = -1;
opt.firmware = NULL; int clopt_display_scale = -1;
opt.model = MODEL_50G;
opt.name = NULL;
opt.text_scale = 1;
opt.display_scale = 2;
#if defined( __linux__ ) int print_config_and_exit = false;
opt.font = "urw gothic l";
#else
opt.font = "Century Gothic";
#endif
const char* optstring = "hrc:D:df:Fn:t:"; const char* optstring = "hrc:D:df:Fn:t:";
struct option long_options[] = { struct option long_options[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h' },
{"print-config", no_argument, &print_config_and_exit, true},
{"config", required_argument, NULL, 'c'}, {"config", required_argument, NULL, 'c' },
{"enable-debug", required_argument, NULL, 'D'}, {"state", required_argument, NULL, 1 },
{"debug", no_argument, NULL, 'd'},
{"reflash", required_argument, NULL, 'f'},
{"reflash-full", no_argument, NULL, 'F'},
{"reboot", no_argument, NULL, 'r'},
{"50g", no_argument, NULL, 506}, {"enable-debug", required_argument, NULL, 'D' },
{"50g-newrpl", no_argument, NULL, 507}, {"debug", no_argument, NULL, 'd' },
{"49gp", no_argument, NULL, 496}, {"reflash", required_argument, NULL, 'f' },
{"49gp-newrpl", no_argument, NULL, 497}, {"reflash-full", no_argument, NULL, 'F' },
{"name", required_argument, NULL, 'n'}, {"reboot", no_argument, NULL, 'r' },
{"text-scale", required_argument, NULL, 's'},
{"display-scale", required_argument, NULL, 'S'},
{"font", required_argument, NULL, 't'}, {"50g", no_argument, NULL, 506 },
{"50g-newrpl", no_argument, NULL, 507 },
{"49gp", no_argument, NULL, 496 },
{"49gp-newrpl", no_argument, NULL, 497 },
{"name", required_argument, NULL, 'n' },
{"text-scale", required_argument, NULL, 's' },
{"display-scale", required_argument, NULL, 'S' },
{0, 0, 0, 0 } {"font", required_argument, NULL, 't' },
{0, 0, 0, 0 }
}; };
while ( c != EOF ) { while ( c != EOF ) {
@ -72,7 +177,7 @@ void config_init( char* progname, int argc, char* argv[] )
"Usage: %s [<options>]\n" "Usage: %s [<options>]\n"
"Valid options:\n" "Valid options:\n"
" -h --help print this message and exit\n" " -h --help print this message and exit\n"
" -c --config[=<filename>] alternate config file\n" " --state[=<filename>] alternate config file\n"
" --50g show HP 50g faceplate (default)\n" " --50g show HP 50g faceplate (default)\n"
" --50g-newrpl show HP 50g faceplate with newRPL labels\n" " --50g-newrpl show HP 50g faceplate with newRPL labels\n"
" --49gp show HP 49g+ faceplate\n" " --49gp show HP 49g+ faceplate\n"
@ -102,11 +207,12 @@ void config_init( char* progname, int argc, char* argv[] )
if ( opt.reinit < X49GP_REINIT_REBOOT_ONLY ) if ( opt.reinit < X49GP_REINIT_REBOOT_ONLY )
opt.reinit = X49GP_REINIT_REBOOT_ONLY; opt.reinit = X49GP_REINIT_REBOOT_ONLY;
break; break;
case 'c': case 1:
opt.config = strdup( optarg ); opt.state_filename = strdup( optarg );
break; break;
case 'D': case 'D':
do_enable_debugger = true; do_enable_debugger = true;
opt.debug_port = atoi( optarg );
break; break;
case 'd': case 'd':
do_start_debugger = true; do_start_debugger = true;
@ -118,51 +224,123 @@ void config_init( char* progname, int argc, char* argv[] )
do_reflash_full = true; do_reflash_full = true;
break; break;
case 496: case 496:
opt.model = MODEL_49GP; clopt_model = MODEL_49GP;
if ( clopt_name == NULL )
clopt_name = "HP 49g+";
break; break;
case 497: case 497:
opt.model = MODEL_49GP_NEWRPL; clopt_model = MODEL_49GP_NEWRPL;
if ( clopt_name == NULL )
clopt_name = "HP 49g+ / newRPL";
break; break;
case 506: case 506:
opt.model = MODEL_50G; clopt_model = MODEL_50G;
if ( clopt_name == NULL )
clopt_name = "HP 50g";
break; break;
case 507: case 507:
opt.model = MODEL_50G_NEWRPL; clopt_model = MODEL_50G_NEWRPL;
if ( clopt_name == NULL )
clopt_name = "HP 50g / newRPL";
break; break;
case 'n': case 'n':
opt.name = strdup( optarg ); clopt_name = strdup( optarg );
break; break;
case 's': case 's':
opt.text_scale = atoi( optarg ); clopt_text_scale = atoi( optarg );
break; break;
case 'S': case 'S':
opt.display_scale = atoi( optarg ); clopt_display_scale = atoi( optarg );
break; break;
case 't': case 't':
opt.font = strdup( optarg ); clopt_font = strdup( optarg );
break; break;
default: default:
break; break;
} }
} }
if ( do_enable_debugger ) { char config_dir[ strlen( progname ) + 9 ];
char* end; const char* home = g_get_home_dir();
int port; sprintf( config_dir, ".config/%s", progname );
if ( optarg == NULL && opt.debug_port == 0 ) opt.config_lua_filename = g_build_filename( home, config_dir, config_lua_filename, NULL );
opt.debug_port = DEFAULT_GDBSTUB_PORT;
port = strtoul( optarg, &end, 0 ); /**********************/
if ( ( end == optarg ) || ( *end != '\0' ) ) { /* 1. read config.lua */
fprintf( stderr, "Invalid port \"%s\", using default\n", optarg ); /**********************/
if ( opt.debug_port == 0 ) bool haz_config_file = config_read( opt.config_lua_filename );
opt.debug_port = DEFAULT_GDBSTUB_PORT; if ( haz_config_file ) {
lua_getglobal( config_lua_values, "model" );
const char* svalue_model = luaL_optstring( config_lua_values, -1, "50g" );
if ( svalue_model != NULL ) {
if ( strcmp( svalue_model, "50g" ) == 0 )
opt.model = MODEL_50G;
if ( strcmp( svalue_model, "50g-newrpl" ) == 0 )
opt.model = MODEL_50G_NEWRPL;
if ( strcmp( svalue_model, "49gp" ) == 0 )
opt.model = MODEL_49GP;
if ( strcmp( svalue_model, "49gp-newrpl" ) == 0 )
opt.model = MODEL_49GP_NEWRPL;
} }
if ( opt.debug_port != 0 && opt.debug_port != DEFAULT_GDBSTUB_PORT ) lua_getglobal( config_lua_values, "name" );
fprintf( stderr, "Additional debug port \"%s\" specified, overriding\n", optarg ); opt.name = strdup( luaL_optstring( config_lua_values, -1, NULL ) );
opt.debug_port = port;
lua_getglobal( config_lua_values, "font" );
opt.font = strdup( luaL_optstring( config_lua_values, -1, NULL ) );
lua_getglobal( config_lua_values, "text_scale" );
opt.text_scale = luaL_optinteger( config_lua_values, -1, 1.0 );
lua_getglobal( config_lua_values, "display_scale" );
opt.display_scale = luaL_optinteger( config_lua_values, -1, 1.0 );
}
/****************************************************/
/* 2. treat command-line params which have priority */
/****************************************************/
if ( clopt_name != NULL )
opt.name = strdup( clopt_name );
else
switch ( opt.model ) {
case MODEL_50G_NEWRPL:
opt.name = "HP 50g / newRPL";
break;
case MODEL_49GP:
opt.name = "HP 49g+";
break;
case MODEL_49GP_NEWRPL:
opt.name = "HP 49g+ / newRPL";
break;
case MODEL_50G:
default:
opt.name = "HP 50g";
break;
}
if ( clopt_font != NULL )
opt.font = strdup( clopt_font );
if ( clopt_model != -1 )
opt.model = clopt_model;
if ( clopt_text_scale != -1 )
opt.text_scale = clopt_text_scale;
if ( clopt_display_scale != -1 )
opt.display_scale = clopt_display_scale;
if ( print_config_and_exit ) {
print_config();
exit( EXIT_SUCCESS );
}
if ( !haz_config_file ) {
fprintf( stderr, "\nConfiguration file %s doesn't seem to exist or is invalid!\n", opt.config_lua_filename );
fprintf( stderr, "You can solve this by running `mkdir -p %s/%s && %s --print-config >> %s`\n\n", home, config_dir, progname,
opt.config_lua_filename );
}
if ( do_enable_debugger ) {
if ( opt.debug_port == 0 )
opt.debug_port = DEFAULT_GDBSTUB_PORT;
opt.start_debugger = do_start_debugger; opt.start_debugger = do_start_debugger;
} }
@ -171,21 +349,13 @@ void config_init( char* progname, int argc, char* argv[] )
opt.reinit = X49GP_REINIT_FLASH; opt.reinit = X49GP_REINIT_FLASH;
if ( opt.firmware != NULL ) if ( opt.firmware != NULL )
fprintf( stderr, fprintf( stderr, "Additional firmware file \"%s\" specified, overriding\n", optarg );
"Additional firmware file \"%s\" specified,"
" overriding\n",
optarg );
opt.firmware = optarg; opt.firmware = optarg;
if ( do_reflash_full ) if ( do_reflash_full )
opt.reinit = X49GP_REINIT_FLASH_FULL; opt.reinit = X49GP_REINIT_FLASH_FULL;
} }
if ( opt.config == NULL ) { if ( opt.state_filename == NULL )
char config_dir[ strlen( progname ) + 9 ]; opt.state_filename = g_build_filename( home, config_dir, "state", NULL );
const char* home = g_get_home_dir();
sprintf( config_dir, ".config/%s", progname );
opt.config = g_build_filename( home, config_dir, "config", NULL );
}
} }

View file

@ -16,7 +16,7 @@
typedef enum { MODEL_49GP = 0, MODEL_49GP_NEWRPL, MODEL_50G, MODEL_50G_NEWRPL } x49gpng_model_t; typedef enum { MODEL_49GP = 0, MODEL_49GP_NEWRPL, MODEL_50G, MODEL_50G_NEWRPL } x49gpng_model_t;
struct options { struct options {
char* config; char* state_filename;
int debug_port; int debug_port;
int start_debugger; int start_debugger;
char* firmware; char* firmware;
@ -28,6 +28,7 @@ struct options {
char* font; char* font;
int display_scale; int display_scale;
int text_scale; int text_scale;
char* config_lua_filename;
}; };
extern struct options opt; extern struct options opt;

View file

@ -83,7 +83,7 @@ struct __x49gp_s__ {
x49gp_ui_t* ui; x49gp_ui_t* ui;
GKeyFile* config; GKeyFile* state_filename;
const char* progname; const char* progname;
const char* progpath; const char* progpath;
const char* basename; const char* basename;