diff --git a/Changelog b/Changelog index 90fdc667..5962aff0 100644 --- a/Changelog +++ b/Changelog @@ -1,6 +1,8 @@ 0.15.3: (unreleased) New feature: - Number mode for dmenu. allows user to get index back instead of content. + Cleanup: + - Do not lug argc,argv around everywhere. 0.15.2: Removed features: diff --git a/include/helper.h b/include/helper.h index 2e1dbe23..76b69b85 100644 --- a/include/helper.h +++ b/include/helper.h @@ -41,8 +41,6 @@ char *token_collate_key ( const char *token, int case_sensitive ); char **tokenize ( const char *input, int case_sensitive ); /** - * @param argc Number of arguments. - * @param argv 2 dimensional array of arguments. * @param key The key to search for * @param val Pointer to the string to set to the key value (if found) * @@ -51,11 +49,9 @@ char **tokenize ( const char *input, int case_sensitive ); * * @returns TRUE if key was found and val was set. */ -int find_arg_char ( const int argc, char * const argv[], const char * const key, char *val ); +int find_arg_char ( const char * const key, char *val ); /** - * @param argc Number of arguments. - * @param argv 2 dimensional array of arguments. * @param key The key to search for * @param val Pointer to the string to set to the key value (if found) * @@ -63,11 +59,9 @@ int find_arg_char ( const int argc, char * const argv[], const char * const key, * * @returns TRUE if key was found and val was set. */ -int find_arg_uint ( const int argc, char * const argv[], const char * const key, unsigned int *val ); +int find_arg_uint ( const char * const key, unsigned int *val ); /** - * @param argc Number of arguments. - * @param argv 2 dimensional array of arguments. * @param key The key to search for * @param val Pointer to the string to set to the key value (if found) * @@ -75,12 +69,10 @@ int find_arg_uint ( const int argc, char * const argv[], const char * const key, * * @returns TRUE if key was found and val was set. */ -int find_arg_int ( const int argc, char * const argv[], const char * const key, int *val ); +int find_arg_int ( const char * const key, int *val ); /** - * @param argc Number of arguments. - * @param argv 2 dimensional array of arguments. * @param key The key to search for * @param val Pointer to the string to set to the key value (if found) * @@ -88,18 +80,16 @@ int find_arg_int ( const int argc, char * const argv[], const char * const key, * * @returns TRUE if key was found and val was set. */ -int find_arg_str ( const int argc, char * const argv[], const char * const key, char** val ); +int find_arg_str ( const char * const key, char** val ); /** - * @param argc Number of arguments. - * @param argv 2 dimensional array of arguments. * @param key The key to search for * * Check if key is passed as argument. * * @returns return position of string or -1 if not found. */ -int find_arg ( const int argc, char * const argv[], const char * const key ); +int find_arg ( const char * const key ); /** * @params tokens @@ -140,6 +130,7 @@ void create_pid_file ( const char *pidfile ); * * This functions exits the program with 1 when it finds an invalid configuration. */ -void config_sanity_check ( int argc, char **argv ); +void config_sanity_check ( void ); char helper_parse_char ( const char *arg ); +void cmd_set_arguments ( int argc, char **argv ); #endif // __HELPER_H__ diff --git a/include/xrmoptions.h b/include/xrmoptions.h index 35428692..be9496c1 100644 --- a/include/xrmoptions.h +++ b/include/xrmoptions.h @@ -22,19 +22,13 @@ void config_parse_xresource_options ( Display *display ); /** - * @param argc Number of arguments. - * @param argv Array of arguments. - * * Parse commandline options. */ -void config_parse_cmd_options ( int argc, char ** argv ); +void config_parse_cmd_options ( void ); /** - * @param argc Number of arguments. - * @param argv Array of arguments. - * * Parse dynamic commandline options. */ -void config_parse_cmd_options_dynamic ( int argc, char ** argv ); +void config_parse_cmd_options_dynamic ( void ); /** * @param display Handler of the display to fetch the settings from. * diff --git a/source/dialogs/dmenu-dialog.c b/source/dialogs/dmenu-dialog.c index 93501ea5..b3841a8b 100644 --- a/source/dialogs/dmenu-dialog.c +++ b/source/dialogs/dmenu-dialog.c @@ -64,10 +64,6 @@ static char **get_dmenu ( int *length ) return retv; } -// Remote pointer to input arguments. -extern int stored_argc; -extern char **stored_argv; - int dmenu_switcher_dialog ( char **input ) { char *dmenu_prompt = "dmenu "; @@ -79,12 +75,12 @@ int dmenu_switcher_dialog ( char **input ) int number_mode = FALSE; // Check if the user requested number mode. - if ( find_arg ( stored_argc, stored_argv, "-i" ) >= 0 ) { + if ( find_arg ( "-i" ) >= 0 ) { number_mode = TRUE; } // Check prompt - find_arg_str ( stored_argc, stored_argv, "-p", &dmenu_prompt ); - find_arg_int ( stored_argc, stored_argv, "-l", &selected_line ); + find_arg_str ( "-p", &dmenu_prompt ); + find_arg_int ( "-l", &selected_line ); do { int shift = 0; diff --git a/source/helper.c b/source/helper.c index a08a317b..bd55f24e 100644 --- a/source/helper.c +++ b/source/helper.c @@ -38,6 +38,15 @@ #include "helper.h" #include "rofi.h" +int stored_argc = 0; +char **stored_argv = NULL; + +void cmd_set_arguments ( int argc, char **argv ) +{ + stored_argc = argc; + stored_argv = argv; +} + /** * `fgets` implementation with custom separator. */ @@ -197,43 +206,43 @@ char **tokenize ( const char *input, int case_sensitive ) } // cli arg handling -int find_arg ( const int argc, char * const argv[], const char * const key ) +int find_arg ( const char * const key ) { int i; - for ( i = 0; i < argc && strcasecmp ( argv[i], key ); i++ ) { + for ( i = 0; i < stored_argc && strcasecmp ( stored_argv[i], key ); i++ ) { ; } - return i < argc ? i : -1; + return i < stored_argc ? i : -1; } -int find_arg_str ( const int argc, char * const argv[], const char * const key, char** val ) +int find_arg_str ( const char * const key, char** val ) { - int i = find_arg ( argc, argv, key ); + int i = find_arg ( key ); - if ( val != NULL && i > 0 && i < argc - 1 ) { - *val = argv[i + 1]; + if ( val != NULL && i > 0 && i < stored_argc - 1 ) { + *val = stored_argv[i + 1]; return TRUE; } return FALSE; } -int find_arg_int ( const int argc, char * const argv[], const char * const key, int *val ) +int find_arg_int ( const char * const key, int *val ) { - int i = find_arg ( argc, argv, key ); + int i = find_arg ( key ); - if ( val != NULL && i > 0 && i < ( argc - 1 ) ) { - *val = strtol ( argv[i + 1], NULL, 10 ); + if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) { + *val = strtol ( stored_argv[i + 1], NULL, 10 ); return TRUE; } return FALSE; } -int find_arg_uint ( const int argc, char * const argv[], const char * const key, unsigned int *val ) +int find_arg_uint ( const char * const key, unsigned int *val ) { - int i = find_arg ( argc, argv, key ); + int i = find_arg ( key ); - if ( val != NULL && i > 0 && i < ( argc - 1 ) ) { - *val = strtoul ( argv[i + 1], NULL, 10 ); + if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) { + *val = strtoul ( stored_argv[i + 1], NULL, 10 ); return TRUE; } return FALSE; @@ -292,12 +301,12 @@ char helper_parse_char ( const char *arg ) return retv; } -int find_arg_char ( const int argc, char * const argv[], const char * const key, char *val ) +int find_arg_char ( const char * const key, char *val ) { - int i = find_arg ( argc, argv, key ); + int i = find_arg ( key ); - if ( val != NULL && i > 0 && i < ( argc - 1 ) ) { - *val = helper_parse_char ( argv[i + 1] ); + if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) { + *val = helper_parse_char ( stored_argv[i + 1] ); return TRUE; } return FALSE; @@ -403,14 +412,14 @@ void create_pid_file ( const char *pidfile ) * * This functions exits the program with 1 when it finds an invalid configuration. */ -void config_sanity_check ( int argc, char **argv ) +void config_sanity_check ( ) { - if ( find_arg ( argc, argv, "-rnow" ) >= 0 || find_arg ( argc, argv, "-snow" ) >= 0 || - find_arg ( argc, argv, "-now" ) >= 0 || find_arg ( argc, argv, "-key" ) >= 0 || - find_arg ( argc, argv, "-skey" ) >= 0 || find_arg ( argc, argv, "-rkey" ) >= 0 ) { + if ( find_arg ( "-rnow" ) >= 0 || find_arg ( "-snow" ) >= 0 || + find_arg ( "-now" ) >= 0 || find_arg ( "-key" ) >= 0 || + find_arg ( "-skey" ) >= 0 || find_arg ( "-rkey" ) >= 0 ) { fprintf ( stderr, "The -snow, -now, -rnow, -key, -rkey, -skey are deprecated " "and have been removed.\n" - "Please see the manpage: %s -help for the correct syntax.", argv[0] ); + "Please see the manpage: %s -help for the correct syntax.", stored_argv[0] ); exit ( EXIT_FAILURE ); } if ( config.element_height < 1 ) { diff --git a/source/rofi.c b/source/rofi.c index 5b59512e..0c7b1e32 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -1579,12 +1579,6 @@ static void setup_switchers ( void ) } } -/** - * Keep a copy of arc, argv around, so we can use the same parsing method - */ -int stored_argc; -char **stored_argv; - /** * @param display Pointer to the X connection to use. * Load configuration. @@ -1596,16 +1590,16 @@ static inline void load_configuration ( Display *display ) config_parse_xresource_options ( display ); // Parse command line for settings. - config_parse_cmd_options ( stored_argc, stored_argv ); + config_parse_cmd_options ( ); } static inline void load_configuration_dynamic ( Display *display ) { // Load in config from X resources. config_parse_xresource_options_dynamic ( display ); - config_parse_cmd_options_dynamic ( stored_argc, stored_argv ); + config_parse_cmd_options_dynamic ( ); // Sanity check - config_sanity_check ( stored_argc, stored_argv ); + config_sanity_check ( ); } @@ -1651,16 +1645,14 @@ static void show_error_message ( const char *msg ) int main ( int argc, char *argv[] ) { int dmenu_mode = FALSE; - stored_argc = argc; - stored_argv = argv; - + cmd_set_arguments ( argc, argv ); // catch help request - if ( find_arg ( argc, argv, "-h" ) >= 0 || find_arg ( argc, argv, "-help" ) >= 0 ) { + if ( find_arg ( "-h" ) >= 0 || find_arg ( "-help" ) >= 0 ) { help (); exit ( EXIT_SUCCESS ); } // Version - if ( find_arg ( argc, argv, "-v" ) >= 0 || find_arg ( argc, argv, "-version" ) >= 0 ) { + if ( find_arg ( "-v" ) >= 0 || find_arg ( "-version" ) >= 0 ) { fprintf ( stdout, "Version: "VERSION "\n" ); exit ( EXIT_SUCCESS ); } @@ -1668,7 +1660,7 @@ int main ( int argc, char *argv[] ) // Detect if we are in dmenu mode. // This has two possible causes. // 1 the user specifies it on the command-line. - if ( find_arg ( argc, argv, "-dmenu" ) >= 0 ) { + if ( find_arg ( "-dmenu" ) >= 0 ) { dmenu_mode = TRUE; } // 2 the binary that executed is called dmenu (e.g. symlink to rofi) @@ -1695,7 +1687,7 @@ int main ( int argc, char *argv[] ) // Get DISPLAY, first env, then argument. display_str = getenv ( "DISPLAY" ); - find_arg_str ( argc, argv, "-display", &display_str ); + find_arg_str ( "-display", &display_str ); if ( !( display = XOpenDisplay ( display_str ) ) ) { fprintf ( stderr, "cannot open display!\n" ); @@ -1715,7 +1707,7 @@ int main ( int argc, char *argv[] ) load_configuration_dynamic ( display ); // Dump. - if ( find_arg ( argc, argv, "-dump-xresources" ) >= 0 ) { + if ( find_arg ( "-dump-xresources" ) >= 0 ) { xresource_dump (); exit ( EXIT_SUCCESS ); } @@ -1729,7 +1721,7 @@ int main ( int argc, char *argv[] ) x11_setup ( display ); char *msg = NULL; - if ( find_arg_str ( argc, argv, "-e", &( msg ) ) ) { + if ( find_arg_str ( "-e", &( msg ) ) ) { show_error_message ( msg ); exit ( EXIT_SUCCESS ); } @@ -1750,7 +1742,7 @@ int main ( int argc, char *argv[] ) // flags to run immediately and exit char *sname = NULL; - if ( find_arg_str ( argc, argv, "-show", &sname ) == TRUE ) { + if ( find_arg_str ( "-show", &sname ) == TRUE ) { int index = switcher_get ( sname ); if ( index >= 0 ) { run_switcher ( FALSE, index ); diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 7ca9d768..b5133ae2 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -195,20 +195,20 @@ void config_parse_xresource_options ( Display *display ) /** * Parse an option from the commandline vector. */ -static void config_parse_cmd_option ( XrmOption *option, int argc, char **argv ) +static void config_parse_cmd_option ( XrmOption *option ) { // Prepend a - to the option name. char *key = g_strdup_printf ( "-%s", option->name ); switch ( option->type ) { case xrm_Number: - find_arg_uint ( argc, argv, key, option->value.num ); + find_arg_uint ( key, option->value.num ); break; case xrm_SNumber: - find_arg_int ( argc, argv, key, option->value.snum ); + find_arg_int ( key, option->value.snum ); break; case xrm_String: - if ( find_arg_str ( argc, argv, key, option->value.str ) == TRUE ) { + if ( find_arg_str ( key, option->value.str ) == TRUE ) { if ( option->mem != NULL ) { g_free ( option->mem ); option->mem = NULL; @@ -216,12 +216,12 @@ static void config_parse_cmd_option ( XrmOption *option, int argc, char **argv ) } break; case xrm_Boolean: - if ( find_arg ( argc, argv, key ) >= 0 ) { + if ( find_arg ( key ) >= 0 ) { *( option->value.num ) = TRUE; } break; case xrm_Char: - find_arg_char ( argc, argv, key, option->value.charc ); + find_arg_char ( key, option->value.charc ); break; default: break; @@ -229,19 +229,19 @@ static void config_parse_cmd_option ( XrmOption *option, int argc, char **argv ) g_free ( key ); } -void config_parse_cmd_options ( int argc, char ** argv ) +void config_parse_cmd_options ( void ) { for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( XrmOption ); ++i ) { XrmOption *op = &( xrmOptions[i] ); - config_parse_cmd_option ( op, argc, argv ); + config_parse_cmd_option ( op ); } } -void config_parse_cmd_options_dynamic ( int argc, char ** argv ) +void config_parse_cmd_options_dynamic ( void ) { for ( unsigned int i = 0; i < num_extra_options; ++i ) { XrmOption *op = &( extra_options[i] ); - config_parse_cmd_option ( op, argc, argv ); + config_parse_cmd_option ( op ); } } diff --git a/test/helper-test.c b/test/helper-test.c index b8a1061c..1ea9dd4a 100644 --- a/test/helper-test.c +++ b/test/helper-test.c @@ -10,9 +10,9 @@ static int test = 0; assert ( a );\ printf("Test %i passed (%s)\n", ++test, #a);\ } - -int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char ** argv ) +int main ( int argc, char ** argv ) { + cmd_set_arguments(argc, argv); char **list = NULL; int llength = 0; char * test_str = "{host} {terminal} -e bash -c \"{ssh-client} {host}; echo '{terminal} {host}'\"";