Cleanup argc/argv lugging around.

This commit is contained in:
Dave Davenport 2015-03-11 18:32:37 +01:00
parent c89a272d4d
commit 4fad02225e
8 changed files with 70 additions and 86 deletions

View File

@ -1,6 +1,8 @@
0.15.3: (unreleased) 0.15.3: (unreleased)
New feature: New feature:
- Number mode for dmenu. allows user to get index back instead of content. - Number mode for dmenu. allows user to get index back instead of content.
Cleanup:
- Do not lug argc,argv around everywhere.
0.15.2: 0.15.2:
Removed features: Removed features:

View File

@ -41,8 +41,6 @@ char *token_collate_key ( const char *token, int case_sensitive );
char **tokenize ( const char *input, 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 key The key to search for
* @param val Pointer to the string to set to the key value (if found) * @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. * @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 key The key to search for
* @param val Pointer to the string to set to the key value (if found) * @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. * @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 key The key to search for
* @param val Pointer to the string to set to the key value (if found) * @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. * @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 key The key to search for
* @param val Pointer to the string to set to the key value (if found) * @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. * @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 * @param key The key to search for
* *
* Check if key is passed as argument. * Check if key is passed as argument.
* *
* @returns return position of string or -1 if not found. * @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 * @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. * 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 ); char helper_parse_char ( const char *arg );
void cmd_set_arguments ( int argc, char **argv );
#endif // __HELPER_H__ #endif // __HELPER_H__

View File

@ -22,19 +22,13 @@ void config_parse_xresource_options ( Display *display );
/** /**
* @param argc Number of arguments.
* @param argv Array of arguments.
*
* Parse commandline options. * 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. * 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. * @param display Handler of the display to fetch the settings from.
* *

View File

@ -64,10 +64,6 @@ static char **get_dmenu ( int *length )
return retv; return retv;
} }
// Remote pointer to input arguments.
extern int stored_argc;
extern char **stored_argv;
int dmenu_switcher_dialog ( char **input ) int dmenu_switcher_dialog ( char **input )
{ {
char *dmenu_prompt = "dmenu "; char *dmenu_prompt = "dmenu ";
@ -79,12 +75,12 @@ int dmenu_switcher_dialog ( char **input )
int number_mode = FALSE; int number_mode = FALSE;
// Check if the user requested number mode. // Check if the user requested number mode.
if ( find_arg ( stored_argc, stored_argv, "-i" ) >= 0 ) { if ( find_arg ( "-i" ) >= 0 ) {
number_mode = TRUE; number_mode = TRUE;
} }
// Check prompt // Check prompt
find_arg_str ( stored_argc, stored_argv, "-p", &dmenu_prompt ); find_arg_str ( "-p", &dmenu_prompt );
find_arg_int ( stored_argc, stored_argv, "-l", &selected_line ); find_arg_int ( "-l", &selected_line );
do { do {
int shift = 0; int shift = 0;

View File

@ -38,6 +38,15 @@
#include "helper.h" #include "helper.h"
#include "rofi.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. * `fgets` implementation with custom separator.
*/ */
@ -197,43 +206,43 @@ char **tokenize ( const char *input, int case_sensitive )
} }
// cli arg handling // 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; 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 ) { if ( val != NULL && i > 0 && i < stored_argc - 1 ) {
*val = argv[i + 1]; *val = stored_argv[i + 1];
return TRUE; return TRUE;
} }
return FALSE; 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 ) ) { if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) {
*val = strtol ( argv[i + 1], NULL, 10 ); *val = strtol ( stored_argv[i + 1], NULL, 10 );
return TRUE; return TRUE;
} }
return FALSE; 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 ) ) { if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) {
*val = strtoul ( argv[i + 1], NULL, 10 ); *val = strtoul ( stored_argv[i + 1], NULL, 10 );
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
@ -292,12 +301,12 @@ char helper_parse_char ( const char *arg )
return retv; 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 ) ) { if ( val != NULL && i > 0 && i < ( stored_argc - 1 ) ) {
*val = helper_parse_char ( argv[i + 1] ); *val = helper_parse_char ( stored_argv[i + 1] );
return TRUE; return TRUE;
} }
return FALSE; 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. * 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 || if ( find_arg ( "-rnow" ) >= 0 || find_arg ( "-snow" ) >= 0 ||
find_arg ( argc, argv, "-now" ) >= 0 || find_arg ( argc, argv, "-key" ) >= 0 || find_arg ( "-now" ) >= 0 || find_arg ( "-key" ) >= 0 ||
find_arg ( argc, argv, "-skey" ) >= 0 || find_arg ( argc, argv, "-rkey" ) >= 0 ) { find_arg ( "-skey" ) >= 0 || find_arg ( "-rkey" ) >= 0 ) {
fprintf ( stderr, "The -snow, -now, -rnow, -key, -rkey, -skey are deprecated " fprintf ( stderr, "The -snow, -now, -rnow, -key, -rkey, -skey are deprecated "
"and have been removed.\n" "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 ); exit ( EXIT_FAILURE );
} }
if ( config.element_height < 1 ) { if ( config.element_height < 1 ) {

View File

@ -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. * @param display Pointer to the X connection to use.
* Load configuration. * Load configuration.
@ -1596,16 +1590,16 @@ static inline void load_configuration ( Display *display )
config_parse_xresource_options ( display ); config_parse_xresource_options ( display );
// Parse command line for settings. // 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 ) static inline void load_configuration_dynamic ( Display *display )
{ {
// Load in config from X resources. // Load in config from X resources.
config_parse_xresource_options_dynamic ( display ); config_parse_xresource_options_dynamic ( display );
config_parse_cmd_options_dynamic ( stored_argc, stored_argv ); config_parse_cmd_options_dynamic ( );
// Sanity check // 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 main ( int argc, char *argv[] )
{ {
int dmenu_mode = FALSE; int dmenu_mode = FALSE;
stored_argc = argc; cmd_set_arguments ( argc, argv );
stored_argv = argv;
// catch help request // 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 (); help ();
exit ( EXIT_SUCCESS ); exit ( EXIT_SUCCESS );
} }
// Version // 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" ); fprintf ( stdout, "Version: "VERSION "\n" );
exit ( EXIT_SUCCESS ); exit ( EXIT_SUCCESS );
} }
@ -1668,7 +1660,7 @@ int main ( int argc, char *argv[] )
// Detect if we are in dmenu mode. // Detect if we are in dmenu mode.
// This has two possible causes. // This has two possible causes.
// 1 the user specifies it on the command-line. // 1 the user specifies it on the command-line.
if ( find_arg ( argc, argv, "-dmenu" ) >= 0 ) { if ( find_arg ( "-dmenu" ) >= 0 ) {
dmenu_mode = TRUE; dmenu_mode = TRUE;
} }
// 2 the binary that executed is called dmenu (e.g. symlink to rofi) // 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. // Get DISPLAY, first env, then argument.
display_str = getenv ( "DISPLAY" ); display_str = getenv ( "DISPLAY" );
find_arg_str ( argc, argv, "-display", &display_str ); find_arg_str ( "-display", &display_str );
if ( !( display = XOpenDisplay ( display_str ) ) ) { if ( !( display = XOpenDisplay ( display_str ) ) ) {
fprintf ( stderr, "cannot open display!\n" ); fprintf ( stderr, "cannot open display!\n" );
@ -1715,7 +1707,7 @@ int main ( int argc, char *argv[] )
load_configuration_dynamic ( display ); load_configuration_dynamic ( display );
// Dump. // Dump.
if ( find_arg ( argc, argv, "-dump-xresources" ) >= 0 ) { if ( find_arg ( "-dump-xresources" ) >= 0 ) {
xresource_dump (); xresource_dump ();
exit ( EXIT_SUCCESS ); exit ( EXIT_SUCCESS );
} }
@ -1729,7 +1721,7 @@ int main ( int argc, char *argv[] )
x11_setup ( display ); x11_setup ( display );
char *msg = NULL; char *msg = NULL;
if ( find_arg_str ( argc, argv, "-e", &( msg ) ) ) { if ( find_arg_str ( "-e", &( msg ) ) ) {
show_error_message ( msg ); show_error_message ( msg );
exit ( EXIT_SUCCESS ); exit ( EXIT_SUCCESS );
} }
@ -1750,7 +1742,7 @@ int main ( int argc, char *argv[] )
// flags to run immediately and exit // flags to run immediately and exit
char *sname = NULL; char *sname = NULL;
if ( find_arg_str ( argc, argv, "-show", &sname ) == TRUE ) { if ( find_arg_str ( "-show", &sname ) == TRUE ) {
int index = switcher_get ( sname ); int index = switcher_get ( sname );
if ( index >= 0 ) { if ( index >= 0 ) {
run_switcher ( FALSE, index ); run_switcher ( FALSE, index );

View File

@ -195,20 +195,20 @@ void config_parse_xresource_options ( Display *display )
/** /**
* Parse an option from the commandline vector. * 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. // Prepend a - to the option name.
char *key = g_strdup_printf ( "-%s", option->name ); char *key = g_strdup_printf ( "-%s", option->name );
switch ( option->type ) switch ( option->type )
{ {
case xrm_Number: case xrm_Number:
find_arg_uint ( argc, argv, key, option->value.num ); find_arg_uint ( key, option->value.num );
break; break;
case xrm_SNumber: case xrm_SNumber:
find_arg_int ( argc, argv, key, option->value.snum ); find_arg_int ( key, option->value.snum );
break; break;
case xrm_String: 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 ) { if ( option->mem != NULL ) {
g_free ( option->mem ); g_free ( option->mem );
option->mem = NULL; option->mem = NULL;
@ -216,12 +216,12 @@ static void config_parse_cmd_option ( XrmOption *option, int argc, char **argv )
} }
break; break;
case xrm_Boolean: case xrm_Boolean:
if ( find_arg ( argc, argv, key ) >= 0 ) { if ( find_arg ( key ) >= 0 ) {
*( option->value.num ) = TRUE; *( option->value.num ) = TRUE;
} }
break; break;
case xrm_Char: case xrm_Char:
find_arg_char ( argc, argv, key, option->value.charc ); find_arg_char ( key, option->value.charc );
break; break;
default: default:
break; break;
@ -229,19 +229,19 @@ static void config_parse_cmd_option ( XrmOption *option, int argc, char **argv )
g_free ( key ); 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 ) { for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( XrmOption ); ++i ) {
XrmOption *op = &( xrmOptions[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 ) { for ( unsigned int i = 0; i < num_extra_options; ++i ) {
XrmOption *op = &( extra_options[i] ); XrmOption *op = &( extra_options[i] );
config_parse_cmd_option ( op, argc, argv ); config_parse_cmd_option ( op );
} }
} }

View File

@ -10,9 +10,9 @@ static int test = 0;
assert ( a );\ assert ( a );\
printf("Test %i passed (%s)\n", ++test, #a);\ printf("Test %i passed (%s)\n", ++test, #a);\
} }
int main ( int argc, char ** argv )
int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char ** argv )
{ {
cmd_set_arguments(argc, argv);
char **list = NULL; char **list = NULL;
int llength = 0; int llength = 0;
char * test_str = "{host} {terminal} -e bash -c \"{ssh-client} {host}; echo '{terminal} {host}'\""; char * test_str = "{host} {terminal} -e bash -c \"{ssh-client} {host}; echo '{terminal} {host}'\"";