From b9c192c4fe8952cf1faceb46bb49c024439ca554 Mon Sep 17 00:00:00 2001 From: QC Date: Mon, 3 Feb 2014 22:49:07 +0100 Subject: [PATCH] Removing duplicate code, small fixes. --- Makefile | 6 +++--- include/simpleswitcher.h | 5 +++++ source/dmenu-dialog.c | 29 +++++------------------------ source/profile-dialog.c | 37 +++---------------------------------- source/run-dialog.c | 22 +++------------------- source/simpleswitcher.c | 17 +++++++++++++++++ source/ssh-dialog.c | 16 +--------------- 7 files changed, 37 insertions(+), 95 deletions(-) diff --git a/Makefile b/Makefile index 4666ffd4..36d5ae86 100644 --- a/Makefile +++ b/Makefile @@ -78,9 +78,9 @@ $(BUILD_DIR): $(QUIET)mkdir -p $@/$(CONFIG_DIR) # Objects depend on header files and makefile too. -$(BUILD_DIR)/%.o: %.c | Makefile $(HEADERS) $(BUILD_DIR) - $(info Compiling $^ -> $@) - $(QUIET) $(CC) $(CFLAGS) -c -o $@ $^ +$(BUILD_DIR)/%.o: %.c Makefile $(HEADERS) | $(BUILD_DIR) + $(info Compiling $< -> $@) + $(QUIET) $(CC) $(CFLAGS) -c -o $@ $< $(BUILD_DIR)/$(PROGRAM): $(OBJECTS) $(info Linking $@) diff --git a/include/simpleswitcher.h b/include/simpleswitcher.h index fc9ec124..d091c49e 100644 --- a/include/simpleswitcher.h +++ b/include/simpleswitcher.h @@ -104,4 +104,9 @@ typedef struct _Settings { } Settings; extern Settings config; + + +int token_match ( char **tokens, const char *input, + __attribute__( ( unused ) )int index, + __attribute__( ( unused ) )void *data ); #endif diff --git a/source/dmenu-dialog.c b/source/dmenu-dialog.c index 8a5c8548..fdf4d35d 100644 --- a/source/dmenu-dialog.c +++ b/source/dmenu-dialog.c @@ -27,16 +27,12 @@ #define _GNU_SOURCE -#include #include -#include - +#include #include -#include #include #include #include -#include #include "simpleswitcher.h" @@ -51,41 +47,27 @@ static char **get_dmenu ( ) while ( fgets( buffer, 1024, stdin ) != NULL ) { retv = realloc( retv, ( index+2 )*sizeof( char* ) ); retv[index] = strdup( buffer ); + retv[index+1] = NULL; + // Filter out line-end. if ( retv[index][strlen( buffer )-1] == '\n' ) retv[index][strlen( buffer )-1] = '\0'; - retv[index+1] = NULL; index++; } return retv; } -static int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data ) -{ - int match = 1; - - // Do a tokenized match. - if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); - } - - return match; -} - SwitcherMode dmenu_switcher_dialog ( char **input ) { - int shift=0; int selected_line = 0; SwitcherMode retv = MODE_EXIT; // act as a launcher char **list = get_dmenu( ); - int mretv = menu( list, input, dmenu_prompt,NULL, &shift, - token_match, NULL, &selected_line ); + int mretv = menu( list, input, dmenu_prompt,NULL, NULL, + token_match, NULL, &selected_line ); if ( mretv == MENU_NEXT ) { retv = DMENU_DIALOG; @@ -100,7 +82,6 @@ SwitcherMode dmenu_switcher_dialog ( char **input ) } if ( list != NULL ) free( list ); - return retv; } diff --git a/source/profile-dialog.c b/source/profile-dialog.c index f10c6896..8c4e2273 100644 --- a/source/profile-dialog.c +++ b/source/profile-dialog.c @@ -42,9 +42,6 @@ #include "simpleswitcher.h" #include "profile-dialog.h" -#ifdef TIMING -#include -#endif static inline int execshprofile( const char *profile ) { @@ -66,7 +63,7 @@ static pid_t exec_profile( const char *cmd ) return pid; } -static char ** add_elements( char **retv, char *element, unsigned int *retv_index ) +static inline char ** add_elements( char **retv, char *element, unsigned int *retv_index ) { retv = realloc( retv, ( ( *retv_index )+2 )*sizeof( char* ) ); retv[( *retv_index )] = element; @@ -80,10 +77,6 @@ static char ** get_profile ( ) unsigned int retv_index = 0; char *path; char **retv = NULL; -#ifdef TIMING - struct timespec start, stop; - clock_gettime( CLOCK_REALTIME, &start ); -#endif if ( getenv( "HOME" ) == NULL ) return NULL; @@ -138,32 +131,9 @@ static char ** get_profile ( ) } free( path ); -#ifdef TIMING - clock_gettime( CLOCK_REALTIME, &stop ); - - if ( stop.tv_sec != start.tv_sec ) { - stop.tv_nsec += ( stop.tv_sec-start.tv_sec )*1e9; - } - - long diff = stop.tv_nsec-start.tv_nsec; - printf( "Time elapsed: %ld us\n", diff/1000 ); -#endif return retv; } -static int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data ) -{ - int match = 1; - - // Do a tokenized match. - if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); - } - - return match; -} SwitcherMode profile_switcher_dialog ( char **input ) { @@ -172,9 +142,8 @@ SwitcherMode profile_switcher_dialog ( char **input ) char **cmd_list = get_profile( ); if ( cmd_list == NULL ) { - cmd_list = allocate( 2*sizeof( char * ) ); - cmd_list[0] = strdup( "No profiles found" ); - cmd_list[1] = NULL; + unsigned int retv_index = 0; + cmd_list = add_elements( cmd_list, "No profiles found", &retv_index ); } int shift=0; diff --git a/source/run-dialog.c b/source/run-dialog.c index 41b37b6b..8ffbe0e2 100644 --- a/source/run-dialog.c +++ b/source/run-dialog.c @@ -135,8 +135,6 @@ static void delete_entry( const char *cmd ) unsigned int index = 0; char **retv = NULL; - printf( "Delete entry: %s\n", cmd ); - /** * This happens in non-critical time (After launching app) * It is allowed to be a bit slower. @@ -280,22 +278,10 @@ static char ** get_apps ( ) return retv; } -static int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data ) -{ - int match = 1; - - // Do a tokenized match. - if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); - } - - return match; -} - SwitcherMode run_switcher_dialog ( char **input ) { + int shift=0; + int selected_line = 0; SwitcherMode retv = MODE_EXIT; // act as a launcher char **cmd_list = get_apps( ); @@ -306,8 +292,6 @@ SwitcherMode run_switcher_dialog ( char **input ) cmd_list[1] = NULL; } - int shift=0; - int selected_line = 0; int mretv = menu( cmd_list, input, "$", NULL, &shift,token_match, NULL, &selected_line ); if ( mretv == MENU_NEXT ) { @@ -325,7 +309,7 @@ SwitcherMode run_switcher_dialog ( char **input ) free( cmd_list[i] ); } - free( cmd_list ); + if (cmd_list != NULL) free( cmd_list ); return retv; } diff --git a/source/simpleswitcher.c b/source/simpleswitcher.c index b0d2c0aa..98d9f388 100644 --- a/source/simpleswitcher.c +++ b/source/simpleswitcher.c @@ -78,7 +78,23 @@ xdgHandle xdg_handle; const char *cache_dir = NULL; +/** + * Shared 'token_match' function. + * Matches tokenized. + */ +int token_match ( char **tokens, const char *input, + __attribute__( ( unused ) )int index, + __attribute__( ( unused ) )void *data ) +{ + int match = 1; + // Do a tokenized match. + if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { + match = ( strcasestr( input, tokens[j] ) != NULL ); + } + + return match; +} void* allocate( unsigned long bytes ) { @@ -1239,6 +1255,7 @@ void run_switcher( int fmode, SwitcherMode mode ) } else if ( mode == SSH_DIALOG ) { retv = ssh_switcher_dialog( &input ); } + #ifdef __QC_MODE__ else if ( mode == PROFILE_DIALOG ) { retv = profile_switcher_dialog ( &input ); diff --git a/source/ssh-dialog.c b/source/ssh-dialog.c index 148c220a..8ba87d95 100644 --- a/source/ssh-dialog.c +++ b/source/ssh-dialog.c @@ -278,20 +278,6 @@ static char ** get_ssh ( ) return retv; } -static int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data ) -{ - int match = 1; - - // Do a tokenized match. - if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); - } - - return match; -} - SwitcherMode ssh_switcher_dialog ( char **input ) { SwitcherMode retv = MODE_EXIT; @@ -324,7 +310,7 @@ SwitcherMode ssh_switcher_dialog ( char **input ) free( cmd_list[i] ); } - free( cmd_list ); + if(cmd_list != NULL) free( cmd_list ); return retv; }