From 63e5f4e9405ed2be83f18b10965b6d66eb5b8a10 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Thu, 21 Aug 2014 19:03:21 +0200 Subject: [PATCH] Small cleanup and fixes. * Use g_strfreev * Use g_ascii*strto(u)ll --- source/history.c | 2 +- source/rofi.c | 121 ++++++++++++++++++-------------------------- source/xrmoptions.c | 81 ++++++++++++++++------------- 3 files changed, 96 insertions(+), 108 deletions(-) diff --git a/source/history.c b/source/history.c index c8ea5ff0..49c6c393 100644 --- a/source/history.c +++ b/source/history.c @@ -144,7 +144,7 @@ void history_set ( const char *filename, const char *entry ) list[length] = g_malloc ( sizeof ( _element ) ); // Copy name if ( list[length] != NULL ) { - strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH ); + g_strlcpy ( list[length]->name, entry, HISTORY_NAME_LENGTH ); list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0'; // set # hits list[length]->index = 1; diff --git a/source/rofi.c b/source/rofi.c index 93d9f23a..9a9513cd 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -67,10 +67,9 @@ #include "xrmoptions.h" -#define LINE_MARGIN 3 +#define LINE_MARGIN 3 #ifdef HAVE_I3_IPC_H -#define I3_SOCKET_PATH_PROP "I3_SOCKET_PATH" // This setting is no longer user configurable, but partial to this file: int config_i3_mode = 0; // Path to HAVE_I3_IPC_H socket. @@ -164,19 +163,6 @@ static char **tokenize ( const char *input ) return retv; } -static inline void tokenize_free ( char **ip ) -{ - if ( ip == NULL ) { - return; - } - - // Free with g_free. - for ( int i = 0; ip[i] != NULL; i++ ) { - g_free ( ip[i] ); - } - g_free ( ip ); -} - #ifdef HAVE_I3_IPC_H // Focus window on HAVE_I3_IPC_H window manager. static void focus_window_i3 ( const char *socket_path, int id ) @@ -813,35 +799,35 @@ int window_match ( char **tokens, __attribute__( ( unused ) ) const char *input, if ( tokens ) { for ( int j = 0; match && tokens[j]; j++ ) { - int test = 0; + int test = 0; - char *sml = g_utf8_casefold ( c->title, -1 ); - char *key = g_utf8_collate_key ( sml, -1 ); if ( !test && c->title[0] != '\0' ) { + char *sml = g_utf8_casefold ( c->title, -1 ); + char *key = g_utf8_collate_key ( sml, -1 ); test = ( strstr ( key, tokens[j] ) != NULL ); + g_free ( sml ); g_free ( key ); } - g_free ( sml ); g_free ( key ); - sml = g_utf8_casefold ( c->class, -1 ); - key = g_utf8_collate_key ( sml, -1 ); if ( !test && c->class[0] != '\0' ) { + char *sml = g_utf8_casefold ( c->class, -1 ); + char *key = g_utf8_collate_key ( sml, -1 ); test = ( strstr ( key, tokens[j] ) != NULL ); + g_free ( sml ); g_free ( key ); } - g_free ( sml ); g_free ( key ); - sml = g_utf8_casefold ( c->role, -1 ); - key = g_utf8_collate_key ( sml, -1 ); if ( !test && c->role[0] != '\0' ) { + char *sml = g_utf8_casefold ( c->role, -1 ); + char *key = g_utf8_collate_key ( sml, -1 ); test = ( strstr ( key, tokens[j] ) != NULL ); + g_free ( sml ); g_free ( key ); } - g_free ( sml ); g_free ( key ); - sml = g_utf8_casefold ( c->name, -1 ); - key = g_utf8_collate_key ( sml, -1 ); if ( !test && c->name[0] != '\0' ) { + char *sml = g_utf8_casefold ( c->name, -1 ); + char *key = g_utf8_collate_key ( sml, -1 ); test = ( strstr ( key, tokens[j] ) != NULL ); + g_free ( sml ); g_free ( key ); } - g_free ( sml ); g_free ( key ); if ( test == 0 ) { match = 0; @@ -861,48 +847,46 @@ static int lev_sort ( const void *p1, const void *p2, void *arg ) return distances[*a] - distances[*b]; } +static int dist ( const char *s, const char *t, int *d, int ls, int lt, int i, int j ) +{ + if ( d[i * ( lt + 1 ) + j] >= 0 ) { + return d[i * ( lt + 1 ) + j]; + } + + int x; + if ( i == ls ) { + x = lt - j; + } + else if ( j == lt ) { + x = ls - i; + } + else if ( s[i] == t[j] ) { + x = dist ( s, t, d, ls, lt, i + 1, j + 1 ); + } + else { + x = dist ( s, t, d, ls, lt, i + 1, j + 1 ); + + int y; + if ( ( y = dist ( s, t, d, ls, lt, i, j + 1 ) ) < x ) { + x = y; + } + if ( ( y = dist ( s, t, d, ls, lt, i + 1, j ) ) < x ) { + x = y; + } + x++; + } + return d[i * ( lt + 1 ) + j] = x; +} static int levenshtein ( const char *s, const char *t ) { int ls = strlen ( s ), lt = strlen ( t ); - int d[ls + 1][lt + 1]; + int d[( ls + 1 ) * ( lt + 1 )]; - for ( int i = 0; i <= ls; i++ ) { - for ( int j = 0; j <= lt; j++ ) { - d[i][j] = -1; - } + for ( int i = 0; i < ( ( ls + 1 ) * ( lt + 1 ) ); i++ ) { + d[i] = -1; } - int dist ( int i, int j ) - { - if ( d[i][j] >= 0 ) { - return d[i][j]; - } - - int x; - if ( i == ls ) { - x = lt - j; - } - else if ( j == lt ) { - x = ls - i; - } - else if ( s[i] == t[j] ) { - x = dist ( i + 1, j + 1 ); - } - else { - x = dist ( i + 1, j + 1 ); - - int y; - if ( ( y = dist ( i, j + 1 ) ) < x ) { - x = y; - } - if ( ( y = dist ( i + 1, j ) ) < x ) { - x = y; - } - x++; - } - return d[i][j] = x; - } - return dist ( 0, 0 ); + return dist ( s, t, d, ls, lt, 0, 0 ); } void window_set_opacity ( Display *display, Window box, unsigned int opacity ) @@ -1201,7 +1185,7 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom // Cleanup + bookkeeping. filtered_lines = j; - tokenize_free ( tokens ); + g_strfreev ( tokens ); } else{ for ( i = 0; i < num_lines; i++ ) { @@ -1638,12 +1622,7 @@ SwitcherMode run_switcher_window ( char **input, void *data ) } } - - for ( i = 0; i < lines; i++ ) { - g_free ( list[i] ); - } - - g_free ( list ); + g_strfreev ( list ); winlist_free ( ids ); } diff --git a/source/xrmoptions.c b/source/xrmoptions.c index 176c31f5..cad69158 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -39,7 +39,8 @@ typedef enum { xrm_String = 0, xrm_Number = 1, - xrm_Boolean = 2 + xrm_SNumber = 2, + xrm_Boolean = 3 } XrmOptionType; typedef struct @@ -49,6 +50,7 @@ typedef struct union { unsigned int * num; + int * snum; char ** str; }; char *mem; @@ -58,52 +60,52 @@ typedef struct * Currently supports string and number. */ static XrmOption xrmOptions[] = { - { xrm_String, "switchers", { .str = &config.switchers }, NULL }, - { xrm_Number, "opacity", { .num = &config.window_opacity }, NULL }, + { xrm_String, "switchers", { .str = &config.switchers }, NULL }, + { xrm_Number, "opacity", { .num = &config.window_opacity }, NULL }, - { xrm_Number, "width", { .num = &config.menu_width }, NULL }, + { xrm_SNumber, "width", { .snum = &config.menu_width }, NULL }, - { xrm_Number, "lines", { .num = &config.menu_lines }, NULL }, - { xrm_Number, "columns", { .num = &config.menu_columns }, NULL }, + { xrm_Number, "lines", { .num = &config.menu_lines }, NULL }, + { xrm_Number, "columns", { .num = &config.menu_columns }, NULL }, - { xrm_String, "font", { .str = &config.menu_font }, NULL }, + { xrm_String, "font", { .str = &config.menu_font }, NULL }, /* Foreground color */ - { xrm_String, "foreground", { .str = &config.menu_fg }, NULL }, - { xrm_String, "fg", { .str = &config.menu_fg }, NULL }, + { xrm_String, "foreground", { .str = &config.menu_fg }, NULL }, + { xrm_String, "fg", { .str = &config.menu_fg }, NULL }, - { xrm_String, "background", { .str = &config.menu_bg }, NULL }, - { xrm_String, "bg", { .str = &config.menu_bg }, NULL }, + { xrm_String, "background", { .str = &config.menu_bg }, NULL }, + { xrm_String, "bg", { .str = &config.menu_bg }, NULL }, - { xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL }, - { xrm_String, "hlfg", { .str = &config.menu_hlfg }, NULL }, + { xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL }, + { xrm_String, "hlfg", { .str = &config.menu_hlfg }, NULL }, - { xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL }, - { xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL }, + { xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL }, + { xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL }, - { xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL }, - { xrm_String, "bc", { .str = &config.menu_bc }, NULL }, + { xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL }, + { xrm_String, "bc", { .str = &config.menu_bc }, NULL }, - { xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL }, - { xrm_Number, "bw", { .num = &config.menu_bw }, NULL }, + { xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL }, + { xrm_Number, "bw", { .num = &config.menu_bw }, NULL }, - { xrm_Number, "location", { .num = &config.location }, NULL }, + { xrm_Number, "location", { .num = &config.location }, NULL }, - { xrm_Number, "padding", { .num = &config.padding }, NULL }, - { xrm_Number, "yoffset", { .num = &config.y_offset }, NULL }, - { xrm_Number, "xoffset", { .num = &config.x_offset }, NULL }, - { xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL }, - { xrm_Boolean, "hmode", { .num = &config.hmode }, NULL }, + { xrm_Number, "padding", { .num = &config.padding }, NULL }, + { xrm_SNumber, "yoffset", { .snum = &config.y_offset }, NULL }, + { xrm_SNumber, "xoffset", { .snum = &config.x_offset }, NULL }, + { xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL }, + { xrm_Boolean, "hmode", { .num = &config.hmode }, NULL }, - { xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL }, + { xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL }, - { xrm_Boolean, "ssh-set-title", { .num = &config.ssh_set_title }, NULL }, - { xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL }, - { xrm_Boolean, "levenshtein-sort", { .num = &config.levenshtein_sort }, NULL }, + { xrm_Boolean, "ssh-set-title", { .num = &config.ssh_set_title }, NULL }, + { xrm_Boolean, "disable-history", { .num = &config.disable_history }, NULL }, + { xrm_Boolean, "levenshtein-sort", { .num = &config.levenshtein_sort }, NULL }, /* Key bindings */ - { xrm_String, "key", { .str = &config.window_key }, NULL }, - { xrm_String, "rkey", { .str = &config.run_key }, NULL }, - { xrm_String, "skey", { .str = &config.ssh_key }, NULL }, + { xrm_String, "key", { .str = &config.window_key }, NULL }, + { xrm_String, "rkey", { .str = &config.run_key }, NULL }, + { xrm_String, "skey", { .str = &config.ssh_key }, NULL }, }; @@ -142,10 +144,13 @@ void parse_xresource_options ( Display *display ) xrmOptions[i].mem = ( *xrmOptions[i].str ); } else if ( xrmOptions[i].type == xrm_Number ) { - *xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 ); + *xrmOptions[i].num = (unsigned int) g_ascii_strtoll ( xrmValue.addr, NULL, 10 ); + } + else if ( xrmOptions[i].type == xrm_SNumber ) { + *xrmOptions[i].num = (int) g_ascii_strtoull ( xrmValue.addr, NULL, 10 ); } else if ( xrmOptions[i].type == xrm_Boolean ) { - if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) { + if ( xrmValue.size > 0 && g_ascii_strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) { *xrmOptions[i].num = TRUE; } else{ @@ -182,11 +187,15 @@ void xresource_dump ( void ) } } - printf ( "%s.%s: ", namePrefix, xrmOptions[i].name ); + printf ( "%s.%s: %*s", namePrefix, xrmOptions[i].name, (int) ( 16 - strlen ( xrmOptions[i].name ) ), + "" ); switch ( xrmOptions[i].type ) { case xrm_Number: - printf ( "%i", *xrmOptions[i].num ); + printf ( "%u", *xrmOptions[i].num ); + break; + case xrm_SNumber: + printf ( "%i", *xrmOptions[i].snum ); break; case xrm_String: printf ( "%s", *xrmOptions[i].str );