]> git.mdlowis.com Git - proto/labwc.git/commitdiff
action: do not expand env vars in Exec action
authorJohan Malm <jgm323@gmail.com>
Thu, 21 Sep 2023 22:09:05 +0000 (23:09 +0100)
committerJohan Malm <johanmalm@users.noreply.github.com>
Sat, 23 Sep 2023 13:52:59 +0000 (14:52 +0100)
...<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

docs/labwc-actions.5.scd
docs/labwc-config.5.scd
include/common/buf.h
src/action.c
src/common/buf.c
src/config/session.c

index b5daf093231bf6b9faaaaf7fa17809ff1eb6bcd0..287eb28e8be21dac768a7d00c342d0d5af8a58ff 100644 (file)
@@ -19,6 +19,7 @@ Actions are used in menus and keyboard/mouse bindings.
        Execute command.  Note that in the interest of backward compatibility,
        labwc supports <execute> as an alternative to <command> even though
        openbox documentation states that it is deprecated.
+       Note: Tilde (~) is expanded in the command before passing to execvp()
 
 *<action name="Exit" />*
        Exit labwc.
index 023f057829cf62ae90ce3f2195679ab2b9a54afd..79448374910034cd347b50625772640fd8b93d3b 100644 (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
 that the environment file is treated differently by openbox where it is simply
 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
 labwc-menu(5).
index 9fcb52062e35f89a8c2da8b535e9f60d77183d8b..64310c9a7594c42c91cc4f74f3982702e72939cc 100644 (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
  * Note: $$ is not handled
  */
index c04e1c54eb45817baa44c5528d169b65bdc2788f..e114d193c59dd4dae088eb989223335f3c63a8b8 100644 (file)
@@ -564,7 +564,7 @@ actions_run(struct view *activator, struct server *server,
                                struct buf cmd;
                                buf_init(&cmd);
                                buf_add(&cmd, action_str_from_arg(arg));
-                               buf_expand_shell_variables(&cmd);
+                               buf_expand_tilde(&cmd);
                                spawn_async_no_shell(cmd.buf);
                                free(cmd.buf);
                        }
index 90a180dc30e66004855b48f0576abb8a29043801..6311def954ff135f69d18156c465d1705ec3692e 100644 (file)
@@ -4,6 +4,35 @@
 #include "common/buf.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
 strip_curly_braces(char *s)
 {
@@ -46,22 +75,15 @@ buf_expand_shell_variables(struct buf *s)
                        if (p) {
                                buf_add(&new, p);
                        }
-               } else if (s->buf[i] == '~') {
-                       /* expand tilde */
-                       buf_add(&new, getenv("HOME"));
                } else {
-                       /* just add one character at a time */
-                       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';
+                       buf_add_one_char(&new, s->buf[i]);
                }
        }
        free(environment_variable.buf);
        free(s->buf);
        s->buf = new.buf;
+       s->len = new.len;
+       s->alloc = new.alloc;
 }
 
 void
index 1a528d18eb6861216be31bf77f4cec579782abbd..d7c37b6be4623ca866834569377dd0edf4a66fd4 100644 (file)
@@ -38,6 +38,7 @@ process_line(char *line)
        buf_init(&value);
        buf_add(&value, string_strip(++p));
        buf_expand_shell_variables(&value);
+       buf_expand_tilde(&value);
        if (string_empty(key) || !value.len) {
                goto error;
        }