From a425d3477b69fa1bf9a0f05eaeb5fd723ea26c2c Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 10 May 2023 14:33:53 +0200 Subject: [PATCH] remove global variables `errbuf` and `fixbuf` --- src/debugger.c | 2 +- src/errors.c | 22 +++++----------------- src/errors.h | 5 +---- src/main.c | 2 +- src/resources.c | 7 +++++-- src/x48.c | 47 +++++++++++++++++++---------------------------- 6 files changed, 32 insertions(+), 53 deletions(-) diff --git a/src/debugger.c b/src/debugger.c index f9461dd..9dc0e3e 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -15,7 +15,7 @@ #include "resources.h" #include "romio.h" #include "timer.h" -#include "x48.h" /* call exit_x48() */ +#include "x48.h" /* call exit_x48() */ #define MAX_ARGS 16 diff --git a/src/errors.c b/src/errors.c index 4d4951a..0313d4e 100644 --- a/src/errors.c +++ b/src/errors.c @@ -1,28 +1,16 @@ #include #include -#include "resources.h" - -char errbuf[ 1024 ] = { - 0, -}; -char fixbuf[ 1024 ] = { - 0, -}; - -void fatal_exit( void ) { - if ( quiet ) - exit( 1 ); - - if ( errbuf[ 0 ] == '\0' ) { +void fatal_exit( char* error, char* advice ) { + if ( error[ 0 ] == '\0' ) { fprintf( stderr, "FATAL ERROR, exit.\n" ); exit( 1 ); } - fprintf( stderr, "FATAL ERROR, exit.\n - %s\n", errbuf ); + fprintf( stderr, "FATAL ERROR, exit.\n - %s\n", error ); - if ( fixbuf[ 0 ] != '\0' ) - fprintf( stderr, " - %s\n", fixbuf ); + if ( advice[ 0 ] != '\0' ) + fprintf( stderr, " - %s\n", advice ); exit( 1 ); } diff --git a/src/errors.h b/src/errors.h index 1b6f903..bef9ae2 100644 --- a/src/errors.h +++ b/src/errors.h @@ -1,9 +1,6 @@ #ifndef _ERRORS_H #define _ERRORS_H -extern char errbuf[ 1024 ]; -extern char fixbuf[ 1024 ]; - -extern void fatal_exit( void ); +extern void fatal_exit( char* error, char* advice ); #endif /* !_ERRORS_H */ diff --git a/src/main.c b/src/main.c index e462499..8d462f7 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,7 @@ saturn_t saturn; void signal_handler( int sig ) { switch ( sig ) { - case SIGINT: /* Ctrl-C */ + case SIGINT: /* Ctrl-C */ enter_debugger |= USER_INTERRUPT; break; case SIGALRM: diff --git a/src/resources.c b/src/resources.c index a2c3804..31123ad 100644 --- a/src/resources.c +++ b/src/resources.c @@ -344,13 +344,16 @@ XFontStruct* get_font_resource( Display* dpy, char* name, char* class ) { if ( s ) f = XLoadQueryFont( dpy, s ); else { + char errbuf[ 1024 ]; sprintf( errbuf, "can\'t get resource \'%s\'", name ); - fatal_exit(); + fatal_exit( errbuf, "" ); } if ( f == ( XFontStruct* )0 ) { + char errbuf[ 1024 ]; + char fixbuf[ 1024 ]; sprintf( errbuf, "can\'t load font \'%s\'", s ); sprintf( fixbuf, "Please change resource \'%s\'", name ); - fatal_exit(); + fatal_exit( errbuf, fixbuf ); } return f; } diff --git a/src/x48.c b/src/x48.c index dbe3549..9efbfe7 100644 --- a/src/x48.c +++ b/src/x48.c @@ -1278,10 +1278,9 @@ int AllocColors( void ) { * Create my own Colormap */ cmap = XCreateColormap( dpy, mainW, visual, AllocNone ); - if ( cmap == ( Colormap )0 ) { - sprintf( errbuf, "can\'t alloc Colormap.\n" ); - fatal_exit(); - } + if ( cmap == ( Colormap )0 ) + fatal_exit( "can\'t alloc Colormap.\n", "" ); + xswa.colormap = cmap; XChangeWindowAttributes( dpy, mainW, CWColormap, &xswa ); if ( iconW ) @@ -1313,24 +1312,20 @@ int AllocColors( void ) { if ( XAllocColorCells( dpy, cmap, True, ( unsigned long* )0, 0, &colors[ c ].xcolor.pixel, 1 ) == 0 ) { dyn = 0; - if ( XAllocColor( dpy, cmap, &colors[ c ].xcolor ) == 0 ) { - sprintf( errbuf, "can\'t alloc Color.\n" ); - fatal_exit(); - } + if ( XAllocColor( dpy, cmap, &colors[ c ].xcolor ) == 0 ) + fatal_exit( "can\'t alloc Color.\n", "" ); + } else if ( colors[ c ].xcolor.pixel >= visual->map_entries ) { dyn = 0; - if ( XAllocColor( dpy, cmap, &colors[ c ].xcolor ) == 0 ) { - sprintf( errbuf, "can\'t alloc Color.\n" ); - fatal_exit(); - } + if ( XAllocColor( dpy, cmap, &colors[ c ].xcolor ) == 0 ) + fatal_exit( "can\'t alloc Color.\n", "" ); + } else { XStoreColor( dpy, cmap, &colors[ c ].xcolor ); } } else { - if ( XAllocColor( dpy, cmap, &colors[ c ].xcolor ) == 0 ) { - sprintf( errbuf, "can\'t alloc Color.\n" ); - fatal_exit(); - } + if ( XAllocColor( dpy, cmap, &colors[ c ].xcolor ) == 0 ) + fatal_exit( "can\'t alloc Color.\n", "" ); } } } @@ -1453,10 +1448,8 @@ int InitDisplay( int argc, char** argv ) { */ res = get_string_resource_from_db( cmd, "name", "Name" ); if ( res ) { - if ( !( res_name = strdup( res ) ) ) { - sprintf( errbuf, "out of memory in InitDisplay()\n" ); - fatal_exit(); - } + if ( !( res_name = strdup( res ) ) ) + fatal_exit( "out of memory in InitDisplay()\n", "" ); for ( s = res_name; *s; s++ ) *s = isupper( *s ) ? tolower( *s ) : *s; @@ -1467,10 +1460,9 @@ int InitDisplay( int argc, char** argv ) { argc = saved_argc; argv = ( char** )malloc( ( argc + 1 ) * sizeof( char* ) ); - if ( argv == ( char** )0 ) { - sprintf( errbuf, "out of memory in InitDisplay()\n" ); - fatal_exit(); - } + if ( argv == ( char** )0 ) + fatal_exit( "out of memory in InitDisplay()\n", "" ); + argv[ argc ] = ( char* )0; for ( i = 0; i < argc; i++ ) argv[ i ] = saved_argv[ i ]; @@ -3271,10 +3263,9 @@ int CreateWindows( int argc, char** argv ) { if ( ( name = get_string_resource( "title", "Title" ) ) == ( char* )0 ) { name = ( char* )malloc( 128 ); - if ( name == ( char* )0 ) { - sprintf( errbuf, "malloc failed.\n" ); - fatal_exit(); - } + if ( name == ( char* )0 ) + fatal_exit( "malloc failed.\n", "" ); + sprintf( name, "%s-%d.%d.%d", progname, saturn.version[ 0 ], saturn.version[ 1 ], saturn.version[ 2 ] ); }