From 8e3a7586d1ff8da5e1802e2ff39da6a7b20cd092 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Fri, 4 Aug 2017 13:14:45 +0200 Subject: [PATCH] replace ~/ in script paths --- docs/dunst.pod | 8 +++++--- src/option_parser.c | 34 ++++++++++++++++++++++++++++++++++ src/option_parser.h | 4 ++++ src/settings.c | 6 +++--- src/utils.c | 13 +++++++++++++ src/utils.h | 3 +++ 6 files changed, 62 insertions(+), 6 deletions(-) diff --git a/docs/dunst.pod b/docs/dunst.pod index 122a600..ee53aeb 100644 --- a/docs/dunst.pod +++ b/docs/dunst.pod @@ -588,9 +588,7 @@ suppressed. =head2 SCRIPTING Within rules you can specify a script to be run every time the rule is matched -by assigning the 'script' option to the name of the script to be run. (If the -value is not an absolute path, the directories in the PATH variable will be -searched for an executable of the same name). +by assigning the 'script' option to the name of the script to be run. When the script is called details of the notification that triggered it will be passed via command line parameters in the following order: appname, summary, @@ -602,6 +600,10 @@ one of "LOW", "NORMAL" or "CRITICAL". If the notification is suppressed, the script will not be run unless B is set to true. +If '~/' occurs at the beginning of the script parameter, it will get replaced by the +users' home directory. If the value is not an absolute path, the directories in the +PATH variable will be searched for an executable of the same name. + =head1 COLORS Colors are interpreted as X11 color values. This includes both verbatim diff --git a/src/option_parser.c b/src/option_parser.c index 2e857f6..0d9c1f6 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -109,6 +109,11 @@ char *get_value(char *section, char *key) return NULL; } +char *ini_get_path(char *section, char *key, const char *def) +{ + return string_to_path(ini_get_string(section, key, def)); +} + char *ini_get_string(char *section, char *key, const char *def) { char *value = get_value(section, key); @@ -355,6 +360,17 @@ char *cmdline_get_string(char *key, const char *def, char *description) return g_strdup(def); } +char *cmdline_get_path(char *key, const char *def, char *description) +{ + cmdline_usage_append(key, "string", description); + char *str = cmdline_get_value(key); + + if (str) + return string_to_path(g_strdup(str)); + else + return string_to_path(g_strdup(def)); +} + int cmdline_get_int(char *key, int def, char *description) { cmdline_usage_append(key, "double", description); @@ -391,6 +407,24 @@ bool cmdline_is_set(char *key) return cmdline_get_value(key) != NULL; } +char *option_get_path(char *ini_section, char *ini_key, char *cmdline_key, + const char *def, char *description) +{ + char *val = NULL; + + if (cmdline_key) { + val = cmdline_get_path(cmdline_key, NULL, description); + } + + + if (val) { + return val; + } else { + return ini_get_path(ini_section, ini_key, def); + } + +} + char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, const char *def, char *description) { diff --git a/src/option_parser.h b/src/option_parser.h index 1f9728e..27c25b3 100644 --- a/src/option_parser.h +++ b/src/option_parser.h @@ -6,6 +6,7 @@ #include int load_ini_file(FILE *); +char *ini_get_path(char *section, char *key, const char *def); char *ini_get_string(char *section, char *key, const char *def); int ini_get_int(char *section, char *key, int def); double ini_get_double(char *section, char *key, double def); @@ -16,6 +17,7 @@ void free_ini(void); void cmdline_load(int argc, char *argv[]); /* for all cmdline_get_* key can be either "-key" or "-key/-longkey" */ char *cmdline_get_string(char *key, const char *def, char *description); +char *cmdline_get_path(char *key, const char *def, char *description); int cmdline_get_int(char *key, int def, char *description); double cmdline_get_double(char *key, double def, char *description); int cmdline_get_bool(char *key, int def, char *description); @@ -24,6 +26,8 @@ char *cmdline_create_usage(void); char *option_get_string(char *ini_section, char *ini_key, char *cmdline_key, const char *def, char *description); +char *option_get_path(char *ini_section, char *ini_key, char *cmdline_key, + const char *def, char *description); int option_get_int(char *ini_section, char *ini_key, char *cmdline_key, int def, char *description); double option_get_double(char *ini_section, char *ini_key, char *cmdline_key, diff --git a/src/settings.c b/src/settings.c index e998363..b53e93a 100644 --- a/src/settings.c +++ b/src/settings.c @@ -345,7 +345,7 @@ void load_settings(char *cmdline_config_path) "print notification on startup" ); - settings.dmenu = option_get_string( + settings.dmenu = option_get_path( "global", "dmenu", "-dmenu", dmenu, "path to dmenu" @@ -362,7 +362,7 @@ void load_settings(char *cmdline_config_path) } - settings.browser = option_get_string( + settings.browser = option_get_path( "global", "browser", "-browser", browser, "path to browser" @@ -636,7 +636,7 @@ void load_settings(char *cmdline_config_path) r->format = ini_get_string(cur_section, "format", r->format); r->new_icon = ini_get_string(cur_section, "new_icon", r->new_icon); r->history_ignore = ini_get_bool(cur_section, "history_ignore", r->history_ignore); - r->script = ini_get_string(cur_section, "script", NULL); + r->script = ini_get_path(cur_section, "script", NULL); } #ifndef STATIC_CONFIG diff --git a/src/utils.c b/src/utils.c index 78ff3c9..dc8e3c1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -105,6 +105,19 @@ void string_strip_delimited(char *str, char a, char b) str[iwrite] = 0; } +char* string_to_path(char* string) { + + if (string && 0 == strncmp(string, "~/", 2)) { + char* home = g_strconcat(getenv("HOME"), "/", NULL); + + string = string_replace("~/", home, string); + + g_free(home); + } + + return string; +} + void die(char *text, int exit_value) { fputs(text, stderr); diff --git a/src/utils.h b/src/utils.h index be98d46..5c0abf5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -21,5 +21,8 @@ void string_strip_delimited(char *str, char a, char b); /* exit with an error message */ void die(char *msg, int exit_value); +/* replace tilde and path-specific values with its equivalents */ +char* string_to_path(char* string); + #endif /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */