mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-25 05:15:41 +02:00
particle: refactor command line tokenization
This commit is contained in:
parent
67e12e8d1e
commit
2123956b14
1 changed files with 86 additions and 19 deletions
105
particle.c
105
particle.c
|
@ -56,6 +56,85 @@ exposable_render_deco(const struct exposable *exposable,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
push_argv(char ***argv, size_t *size, char *arg, size_t *argc)
|
||||||
|
{
|
||||||
|
if (arg != NULL && arg[0] == '%')
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (*argc >= *size) {
|
||||||
|
size_t new_size = *size > 0 ? 2 * *size : 10;
|
||||||
|
char **new_argv = realloc(*argv, new_size * sizeof(new_argv[0]));
|
||||||
|
|
||||||
|
if (new_argv == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*argv = new_argv;
|
||||||
|
*size = new_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
(*argv)[(*argc)++] = arg;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
tokenize_cmdline(char *cmdline, char ***argv)
|
||||||
|
{
|
||||||
|
*argv = NULL;
|
||||||
|
size_t argv_size = 0;
|
||||||
|
|
||||||
|
bool first_token_is_quoted = cmdline[0] == '"' || cmdline[0] == '\'';
|
||||||
|
char delim = first_token_is_quoted ? cmdline[0] : ' ';
|
||||||
|
|
||||||
|
char *p = first_token_is_quoted ? &cmdline[1] : &cmdline[0];
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
while (*p != '\0') {
|
||||||
|
char *end = strchr(p, delim);
|
||||||
|
if (end == NULL) {
|
||||||
|
if (delim != ' ') {
|
||||||
|
LOG_ERR("unterminated %s quote\n", delim == '"' ? "double" : "single");
|
||||||
|
free(*argv);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!push_argv(argv, &argv_size, p, &idx) ||
|
||||||
|
!push_argv(argv, &argv_size, NULL, &idx))
|
||||||
|
{
|
||||||
|
goto err;
|
||||||
|
} else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
*end = '\0';
|
||||||
|
|
||||||
|
if (!push_argv(argv, &argv_size, p, &idx))
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
p = end + 1;
|
||||||
|
while (*p == delim)
|
||||||
|
p++;
|
||||||
|
|
||||||
|
while (*p == ' ')
|
||||||
|
p++;
|
||||||
|
|
||||||
|
if (*p == '"' || *p == '\'') {
|
||||||
|
delim = *p;
|
||||||
|
p++;
|
||||||
|
} else
|
||||||
|
delim = ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!push_argv(argv, &argv_size, NULL, &idx))
|
||||||
|
goto err;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
|
err:
|
||||||
|
free(*argv);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
exposable_default_on_mouse(struct exposable *exposable, struct bar *bar,
|
exposable_default_on_mouse(struct exposable *exposable, struct bar *bar,
|
||||||
enum mouse_event event, int x, int y)
|
enum mouse_event event, int x, int y)
|
||||||
|
@ -70,40 +149,28 @@ exposable_default_on_mouse(struct exposable *exposable, struct bar *bar,
|
||||||
if (exposable->on_click != NULL && event == ON_MOUSE_CLICK) {
|
if (exposable->on_click != NULL && event == ON_MOUSE_CLICK) {
|
||||||
/* Need a writeable copy, whose scope *we* control */
|
/* Need a writeable copy, whose scope *we* control */
|
||||||
char *cmd = strdup(exposable->on_click);
|
char *cmd = strdup(exposable->on_click);
|
||||||
const char *end = cmd + strlen(cmd);
|
LOG_DBG("cmd = \"%s\"", cmd);
|
||||||
|
|
||||||
char *argv[1024];
|
char **argv;
|
||||||
size_t tokens = 0;
|
if (!tokenize_cmdline(cmd, &argv)) {
|
||||||
|
free(cmd);
|
||||||
/* Tokenize the command string */
|
return;
|
||||||
for (char *ctx, *tok = strtok_r(cmd, " ", &ctx);
|
|
||||||
tok != NULL;
|
|
||||||
/*tok = strtok_r(NULL, " ", &ctx)*/)
|
|
||||||
{
|
|
||||||
argv[tokens++] = tok;
|
|
||||||
|
|
||||||
/* Is the beginning of the next token a quote? */
|
|
||||||
bool next_is_quoted = &tok[strlen(tok) + 1] < end &&
|
|
||||||
tok[strlen(tok) + 1] == '"';
|
|
||||||
tok = strtok_r(NULL, next_is_quoted ? "\"" : " ", &ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* NULL-terminate list (for execvp) */
|
|
||||||
argv[tokens] = NULL;
|
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid == -1)
|
if (pid == -1)
|
||||||
LOG_ERRNO("failed to run on_click handler (fork)");
|
LOG_ERRNO("failed to run on_click handler (fork)");
|
||||||
else if (pid > 0) {
|
else if (pid > 0) {
|
||||||
/* Parent */
|
/* Parent */
|
||||||
free(cmd);
|
free(cmd);
|
||||||
|
free(argv);
|
||||||
|
|
||||||
if (waitpid(pid, NULL, 0) == -1)
|
if (waitpid(pid, NULL, 0) == -1)
|
||||||
LOG_ERRNO("failed to wait for on_click handler");
|
LOG_ERRNO("failed to wait for on_click handler");
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
LOG_DBG("ARGV:");
|
LOG_DBG("ARGV:");
|
||||||
for (size_t i = 0; i < tokens; i++)
|
for (size_t i = 0; argv[i] != NULL; i++)
|
||||||
LOG_DBG(" #%zu: \"%s\" ", i, argv[i]);
|
LOG_DBG(" #%zu: \"%s\" ", i, argv[i]);
|
||||||
|
|
||||||
LOG_DBG("daemonizing on-click handler");
|
LOG_DBG("daemonizing on-click handler");
|
||||||
|
|
Loading…
Add table
Reference in a new issue