From 0a95ba47fa3c7c228d479478b8be9751b251122b Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Mon, 19 May 2014 21:02:05 +0200 Subject: [PATCH] Extra checks, first part cleanup --- config/config.c | 36 +++++++++++++++++++++++-- include/rofi.h | 2 ++ source/rofi.c | 65 ++++++++++++++++++++++++++++----------------- source/xrmoptions.c | 1 + 4 files changed, 78 insertions(+), 26 deletions(-) diff --git a/config/config.c b/config/config.c index 6c77ef84..97659b75 100644 --- a/config/config.c +++ b/config/config.c @@ -24,6 +24,8 @@ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ +#include +#include #include "rofi.h" Settings config = { @@ -33,14 +35,14 @@ Settings config = { .window_opacity = 100, // Border width around the window. .menu_bw = 1, - // The width of the switcher. (0-100 in % > 100 in pixels) + // The width of the switcher. (0100 in % > 100 in pixels) .menu_width = 50, // Maximum number of options to show. .menu_lines = 15, // Number of columns .menu_columns = 1, // Font - .menu_font = "mono-12", + .menu_font = "mono12", // Foreground color .menu_fg = "#222222", // Background color @@ -74,3 +76,33 @@ Settings config = { .x_offset = 0, .fixed_num_lines = 0 }; + +/** + * Do some input validation, especially the first few could break things. + * It is good to catch them beforehand. + * + * This functions exits the program with 1 when it finds an invalid configuration. + */ +void config_sanity_check( void ) +{ + if ( config.menu_lines == 0 ) { + fprintf(stderr, "config.menu_lines is invalid. You need at least one visible line.\n"); + exit(1); + } + if ( config.menu_columns == 0 ) { + fprintf(stderr, "config.menu_columns is invalid. You need at least one visible column.\n"); + exit(1); + } + + if ( config.menu_width == 0 ) { + fprintf(stderr, "config.menu_width is invalid. You cannot have a window with no width.\n"); + exit(1); + } + + if ( !( config.location >= WL_CENTER && config.location <= WL_WEST ) ) + { + fprintf(stderr, "config.location is invalid. ( %d >= %d >= %d) does not hold.\n", + WL_WEST, config.location, WL_CENTER); + exit(1); + } +} diff --git a/include/rofi.h b/include/rofi.h index 26c274b9..2eecc3b4 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -107,4 +107,6 @@ extern Settings config; int token_match ( char **tokens, const char *input, __attribute__( ( unused ) ) int index, __attribute__( ( unused ) ) void *data ); + +void config_sanity_check ( void ); #endif diff --git a/source/rofi.c b/source/rofi.c index b92ec7e4..cb981f0a 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -320,8 +320,8 @@ typedef struct int len; } winlist; -winlist *cache_client; -winlist *cache_xattr; +winlist *cache_client = NULL; +winlist *cache_xattr = NULL; #define winlist_ascend( l, i, w ) for ( ( i ) = 0; ( i ) < ( l )->len && ( ( ( w ) = ( l )->array[i] ) || 1 ); ( i )++ ) #define winlist_descend( l, i, w ) for ( ( i ) = ( l )->len - 1; ( i ) >= 0 && ( ( ( w ) = ( l )->array[i] ) || 1 ); ( i )-- ) @@ -1893,6 +1893,38 @@ static void parse_cmd_options( int argc, char ** argv ) } } +static void cleanup() +{ +printf("cleanup\n"); + // Cleanup + if ( display != NULL ) + { + if ( main_window != None ) + { + XFreeGC ( display, gc ); + XDestroyWindow ( display, main_window ); + XCloseDisplay ( display ); + } + } + if(cache_xattr != NULL) + { + winlist_free ( cache_xattr ); + } + if(cache_client != NULL) + { + winlist_free ( cache_client ); + } +#ifdef HAVE_I3_IPC_H + + if ( i3_socket_path != NULL ) + { + free ( i3_socket_path ); + } + +#endif + xdgWipeHandle ( &xdg_handle ); + +} int main ( int argc, char *argv[] ) { int i, j; @@ -1913,6 +1945,7 @@ int main ( int argc, char *argv[] ) return EXIT_SUCCESS; } + // Get DISPLAY char *display_str = getenv ( "DISPLAY" ); find_arg_str ( argc, argv, "-display", &display_str ); @@ -1930,6 +1963,9 @@ int main ( int argc, char *argv[] ) return EXIT_FAILURE; } + // Register cleanup function. + atexit(cleanup); + cache_dir = xdgCacheHome ( &xdg_handle ); @@ -1976,6 +2012,9 @@ int main ( int argc, char *argv[] ) // Parse command line for settings. parse_cmd_options ( argc, argv ); + // Sanity check + config_sanity_check (); + // flags to run immediately and exit if ( find_arg ( argc, argv, "-now" ) >= 0 ) { @@ -2033,27 +2072,5 @@ int main ( int argc, char *argv[] ) } } - // Cleanup - if ( display != NULL ) - { - if ( main_window != None ) - { - XFreeGC ( display, gc ); - XDestroyWindow ( display, main_window ); - XCloseDisplay ( display ); - } - } - - winlist_free ( cache_xattr ); - winlist_free ( cache_client ); -#ifdef HAVE_I3_IPC_H - - if ( i3_socket_path != NULL ) - { - free ( i3_socket_path ); - } - -#endif - xdgWipeHandle ( &xdg_handle ); return EXIT_SUCCESS; } diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 49f61855..763bbde3 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -105,6 +105,7 @@ void parse_xresource_options ( Display *display ) { if ( xrmOptions[i].type == xrm_String ) { + //TODO this leaks memory. *xrmOptions[i].str = ( char * ) malloc ( xrmValue.size * sizeof ( char ) ); strncpy ( *xrmOptions[i].str, xrmValue.addr, xrmValue.size ); }