Skip to content
Myr

Merge branch 'tokenize-cmdline-rework'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Daniel Eklöf · committed by Daniel Eklöf · parents f6e71f0, 0a83e0f · Browse files · 2 files+47 added−43 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+47 added−43 removed

  • other:+47 added−43 removed

2 files+47 added−43 removed

modified:application.c+45 added-43 removed
42 unmodified lines
43{44 *argv = NULL;45 size_t argv_size = 0;4647 bool first_token_is_quoted = cmdline[0] == '"' || cmdline[0] == '\'';48 char delim = first_token_is_quoted ? cmdline[0] : ' ';47 char *p = cmdline;48 char *search_start = p; /* points to start of current arg */49 char open_quote = 0; /* the current opening quote character, 0 means none open */5050 char *p = first_token_is_quoted ? &cmdline[1] : &cmdline[0];51 char *search_start = p;5251 size_t idx = 0;52 while (*p != '\0') {55 char *end = strchr(search_start, delim);56 if (end == NULL) {57 if (delim != ' ') {58 LOG_ERR("unterminated %s quote\n", delim == '"' ? "double" : "single");59 free(*argv);60 return false;53 if (*p == '\\') {54 /* lookahead to handle escaped chars55 * only escape if not within single quotes */56 if (open_quote != '\'') {57 /* within double quotes, only \$, \`, \", \\ should be escaped, others should be literal */58 if ((open_quote != '"' && (*(p + 1) == '\'' || *(p + 1) == ' '))59 || *(p + 1) == '$' || *(p + 1) == '"' || *(p + 1) == '`' || *(p + 1) == '\\') {60 /* essentially delete the (first) backslash */61 memmove(p, p + 1, strlen(p + 1) + 1);62 }63 }6263 if (!push_argv(argv, &argv_size, p, &idx) ||64 !push_argv(argv, &argv_size, NULL, &idx))65 {66 goto err;67 } else68 return true;64 /* ignore the other cases */65 } else {66 if (open_quote == 0 && (*p == '\'' || *p == '"')) {67 /* open a quote, delete the opening character but remember that we have one open */68 open_quote = *p;69 memmove(p, p + 1, strlen(p + 1) + 1);70 continue; /* don't increment p */71 } else if (*p == open_quote) {72 /* close the quote, delete the closing character */73 open_quote = 0;74 memmove(p, p + 1, strlen(p + 1) + 1);75 continue; /* don't increment p */76 } else if (*p == ' ' && open_quote == 0) {77 /* we must not be in an argument rn78 * check if we can close the arg at p (exclusive)79 * note: passing empty quotes doesn't count as an argument */80 if (p > search_start) {81 *p = '\0';82 if (!push_argv(argv, &argv_size, search_start, &idx))83 goto err;84 }85 search_start = p + 1;86 }87 }88 p++;89 }9071 if (end > p && *(end - 1) == '\\') {72 /* Escaped quote, remove one level of escaping and73 * continue searching for "our" closing quote */74 memmove(end - 1, end, strlen(end));75 end[strlen(end) - 1] = '\0';76 search_start = end;77 continue;78 }91 if (open_quote != 0) {92 LOG_ERR("unterminated %s quote\n", open_quote == '"' ? "double" : "single");93 goto err;94 }9580 *end = '\0';8182 if (!push_argv(argv, &argv_size, p, &idx))96 /* edge case: argument terminated by \0 */97 if (p > search_start) {98 if (!push_argv(argv, &argv_size, search_start, &idx))99 goto err;8485 p = end + 1;86 while (*p == delim)87 p++;8889 while (*p == ' ')90 p++;9192 if (*p == '"' || *p == '\'') {93 delim = *p;94 p++;95 } else96 delim = ' ';97 search_start = p;100 }101102 if (!push_argv(argv, &argv_size, NULL, &idx))103 goto err;
230 unmodified lines
modified:CHANGELOG.md+2 added
29 unmodified lines
30* `Ctrl+y` binding to execute selected entry.31* `Ctrl+j`/`Ctrl+k` binding to move to the next/previous item32 ([#120][120]).33* Escape sequences in `Exec` arguments are now supported.34* Quoted environment variables in `Exec` arguments are now supported ([#143][143]).35* Multiple space-separated search words can now be entered at the prompt.36* `-M,--selection-match-color`, that lets you configure the color of37 matched substrings of the currently selected item38* New config option `image-size-ratio`, allowing you to control the
3 unmodified lines
42[3]: https://codeberg.org/dnkl/fuzzel/issues/343[107]: https://codeberg.org/dnkl/fuzzel/issues/10744[108]: https://codeberg.org/dnkl/fuzzel/issues/10845[120]: https://codeberg.org/dnkl/fuzzel/issues/12046[143]: https://codeberg.org/dnkl/fuzzel/issues/143474849### Changed50
338 unmodified lines