action: do not expand env vars in Exec action

...<command> argument (but still resolve tilde).

This makes it easier to write sh -c '' constructs without turning labwc
into a shell parser in order to expand environment variables, whilst
respecting single quotes and escaped characters as well as ignoring
subshells syntax like $(foo) and backticks.

Also, fix bug where buffer length+alloc get out-of-sync
This commit is contained in:
Johan Malm 2023-09-21 23:09:05 +01:00 committed by Johan Malm
parent c6c1f8e04b
commit 881d788bee
6 changed files with 44 additions and 12 deletions

View file

@ -19,6 +19,7 @@ Actions are used in menus and keyboard/mouse bindings.
Execute command. Note that in the interest of backward compatibility, Execute command. Note that in the interest of backward compatibility,
labwc supports <execute> as an alternative to <command> even though labwc supports <execute> as an alternative to <command> even though
openbox documentation states that it is deprecated. openbox documentation states that it is deprecated.
Note: Tilde (~) is expanded in the command before passing to execvp()
*<action name="Exit" />* *<action name="Exit" />*
Exit labwc. Exit labwc.

View file

@ -32,6 +32,8 @@ variables accordingly. It is recommended to specify keyboard layout settings and
cursor size/theme here; see environment variable section below for details. Note cursor size/theme here; see environment variable section below for details. Note
that the environment file is treated differently by openbox where it is simply that the environment file is treated differently by openbox where it is simply
sourced prior to running openbox. sourced prior to running openbox.
Note: Tilde (~) and environment variables in the value are expanded, but
subshell syntax and apostrophes are ignored.
The *menu.xml* file defines the context/root-menus and is described in The *menu.xml* file defines the context/root-menus and is described in
labwc-menu(5). labwc-menu(5).

View file

@ -19,7 +19,13 @@ struct buf {
}; };
/** /**
* buf_expand_shell_variables - expand $foo, ${foo} and ~ in buffer * buf_expand_tilde - expand ~ in buffer
* @s: buffer
*/
void buf_expand_tilde(struct buf *s);
/**
* buf_expand_shell_variables - expand $foo and ${foo} in buffer
* @s: buffer * @s: buffer
* Note: $$ is not handled * Note: $$ is not handled
*/ */

View file

@ -564,7 +564,7 @@ actions_run(struct view *activator, struct server *server,
struct buf cmd; struct buf cmd;
buf_init(&cmd); buf_init(&cmd);
buf_add(&cmd, action_str_from_arg(arg)); buf_add(&cmd, action_str_from_arg(arg));
buf_expand_shell_variables(&cmd); buf_expand_tilde(&cmd);
spawn_async_no_shell(cmd.buf); spawn_async_no_shell(cmd.buf);
free(cmd.buf); free(cmd.buf);
} }

View file

@ -4,6 +4,35 @@
#include "common/buf.h" #include "common/buf.h"
#include "common/mem.h" #include "common/mem.h"
static void
buf_add_one_char(struct buf *s, char ch)
{
if (s->alloc <= s->len + 1) {
s->alloc = s->alloc * 3 / 2 + 16;
s->buf = xrealloc(s->buf, s->alloc);
}
s->buf[s->len++] = ch;
s->buf[s->len] = '\0';
}
void
buf_expand_tilde(struct buf *s)
{
struct buf new;
buf_init(&new);
for (int i = 0 ; i < s->len ; i++) {
if (s->buf[i] == '~') {
buf_add(&new, getenv("HOME"));
} else {
buf_add_one_char(&new, s->buf[i]);
}
}
free(s->buf);
s->buf = new.buf;
s->len = new.len;
s->alloc = new.alloc;
}
static void static void
strip_curly_braces(char *s) strip_curly_braces(char *s)
{ {
@ -46,22 +75,15 @@ buf_expand_shell_variables(struct buf *s)
if (p) { if (p) {
buf_add(&new, p); buf_add(&new, p);
} }
} else if (s->buf[i] == '~') {
/* expand tilde */
buf_add(&new, getenv("HOME"));
} else { } else {
/* just add one character at a time */ buf_add_one_char(&new, s->buf[i]);
if (new.alloc <= new.len + 1) {
new.alloc = new.alloc * 3 / 2 + 16;
new.buf = xrealloc(new.buf, new.alloc);
}
new.buf[new.len++] = s->buf[i];
new.buf[new.len] = '\0';
} }
} }
free(environment_variable.buf); free(environment_variable.buf);
free(s->buf); free(s->buf);
s->buf = new.buf; s->buf = new.buf;
s->len = new.len;
s->alloc = new.alloc;
} }
void void

View file

@ -38,6 +38,7 @@ process_line(char *line)
buf_init(&value); buf_init(&value);
buf_add(&value, string_strip(++p)); buf_add(&value, string_strip(++p));
buf_expand_shell_variables(&value); buf_expand_shell_variables(&value);
buf_expand_tilde(&value);
if (string_empty(key) || !value.len) { if (string_empty(key) || !value.len) {
goto error; goto error;
} }