Added support for \ escaping and quote wrapping arguments for spaces

This commit is contained in:
Scoopta 2020-06-30 00:26:04 -07:00
parent 56e971a636
commit 5a2fa0e7ca

View File

@ -150,6 +150,32 @@ struct widget* wofi_run_get_widget(void) {
return NULL; return NULL;
} }
static char* parse_args(const char* cmd, size_t* space_count) {
size_t cmd_l = strlen(cmd);
char* ret = calloc(1, cmd_l + 1);
bool in_quote = false;
size_t ret_count = 0;
for(size_t count = 0; count < cmd_l; ++count) {
if(cmd[count] == ' ' && !in_quote) {
++*space_count;
ret[ret_count++] = '\n';
continue;
}
if(cmd[count] == '"') {
in_quote = !in_quote;
continue;
}
if(cmd[count] == '\\') {
++count;
}
ret[ret_count++] = cmd[count];
}
return ret;
}
void wofi_run_exec(const char* cmd) { void wofi_run_exec(const char* cmd) {
bool arg_run = wofi_mod_control() || always_parse_args; bool arg_run = wofi_mod_control() || always_parse_args;
@ -163,13 +189,9 @@ void wofi_run_exec(const char* cmd) {
wofi_term_run(cmd); wofi_term_run(cmd);
} }
if(arg_run) { if(arg_run) {
char* tmp = strdup(cmd);
size_t space_count = 2; size_t space_count = 2;
char* space; char* tmp = parse_args(cmd, &space_count);
while((space = strchr(tmp, ' ')) != NULL) {
++space_count;
*space = '\n';
}
char** args = malloc(space_count * sizeof(char*)); char** args = malloc(space_count * sizeof(char*));
char* save_ptr; char* save_ptr;
char* str = strtok_r(tmp, "\n", &save_ptr); char* str = strtok_r(tmp, "\n", &save_ptr);