Pass URLs to browser as a single argument

Parsing the arguments with g_shell_parse_argv is more safe than just
splitting it by spaces. Also this allows to pass values with spaces to
the browser command.
This commit is contained in:
Benedikt Heine 2018-09-26 09:36:24 +02:00
parent 851953f5ef
commit 357c4309e6
3 changed files with 26 additions and 3 deletions

View File

@ -102,6 +102,11 @@ char *extract_urls(const char *to_match)
*/
void open_browser(const char *in)
{
if (!settings.browser_cmd) {
LOG_C("Unable to open browser: No browser command set.");
return;
}
char *url = NULL;
// If any, remove leading [ linktext ] from URL
@ -122,9 +127,16 @@ void open_browser(const char *in)
if (browser_pid2) {
exit(0);
} else {
char *browser_cmd = g_strconcat(settings.browser, " ", url, NULL);
char **cmd = g_strsplit(browser_cmd, " ", 0);
execvp(cmd[0], cmd);
int argc = 2+g_strv_length(settings.browser_cmd);
char **argv = g_malloc_n(argc, sizeof(char*));
memcpy(argv, settings.browser_cmd, argc * sizeof(char*));
argv[argc-2] = url;
argv[argc-1] = NULL;
execvp(argv[0], argv);
g_free(argv);
// execvp won't return if it's successful
// so, if we're here, it's definitely an error
fprintf(stderr, "Warning: failed to execute '%s': %s\n",

View File

@ -448,6 +448,16 @@ void load_settings(char *cmdline_config_path)
"path to browser"
);
{
GError *error = NULL;
if (!g_shell_parse_argv(settings.browser, NULL, &settings.browser_cmd, &error)) {
LOG_W("Unable to parse browser command: '%s'."
" URL functionality will be disabled.", error->message);
g_error_free(error);
settings.browser_cmd = NULL;
}
}
{
char *c = option_get_string(
"global",

View File

@ -75,6 +75,7 @@ struct settings {
char *dmenu;
char **dmenu_cmd;
char *browser;
char **browser_cmd;
enum icon_position icon_position;
int max_icon_size;
char *icon_path;