1
0
Fork 0
forked from Miroirs/x49gp

load (limited) configuration from ~/.config/x49gpng/config.lua is present

This commit is contained in:
Gwenhael Le Moine 2024-11-10 18:09:15 +01:00
parent 94b8e7b04d
commit 6089396a7a
No known key found for this signature in database
GPG key ID: FDFE3669426707A7
3 changed files with 248 additions and 69 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

@ -5,44 +5,149 @@
#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.state_filename = 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' },
{"state", required_argument, NULL, 1 },
{"enable-debug", required_argument, NULL, 'D' }, {"enable-debug", required_argument, NULL, 'D' },
{"debug", no_argument, NULL, 'd' }, {"debug", no_argument, NULL, 'd' },
{"reflash", required_argument, NULL, 'f' }, {"reflash", required_argument, NULL, 'f' },
@ -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.state_filename = 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.state_filename == NULL ) { if ( opt.state_filename == NULL )
char config_dir[ strlen( progname ) + 9 ];
const char* home = g_get_home_dir();
sprintf( config_dir, ".config/%s", progname );
opt.state_filename = g_build_filename( home, config_dir, "state", NULL ); opt.state_filename = g_build_filename( home, config_dir, "state", NULL );
} }
}

View file

@ -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;