From f869ac4b6038c151f5c89726e17e16db3b90e9b0 Mon Sep 17 00:00:00 2001 From: werdl Date: Sat, 31 May 2025 20:20:33 +0100 Subject: add exec config file keyword --- src/parser.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index 7bca735..baa15a0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -160,7 +160,8 @@ int parser(Config *cfg) fprintf(stderr, "sxwmrc: no configuration file found\n"); return -1; -found:; +found: + printf("sxwmrc: using configuration file %s\n", path); FILE *f = fopen(path, "r"); if (!f) { fprintf(stderr, "sxwmrc: cannot open %s\n", path); @@ -355,6 +356,59 @@ found:; fprintf(stderr, "sxwmrc:%d: invalid workspace action '%s'\n", lineno, act); } } + else if (!strcmp(key, "exec")) { + char *comment = strchr(rest, '#'); + size_t len = comment ? (size_t)(comment - rest) : strlen(rest); + char win[len + 1]; + strncpy(win, rest, len); + win[len] = '\0'; + + char *final = strip(win); + char *comma_ptr; + printf("DEBUG: exec command: '%s'\n", final); + + char *comma = strtok_r(final, ",", &comma_ptr); + while (comma) { + comma = strip(comma); + if (*comma == '"') { + comma++; + } + char *end = comma + strlen(comma) - 1; + if (*end == '"') { + *end = '\0'; + } + + char *cmd = strdup(comma); + + // we have to fork and exec, because system() is blocking + pid_t pid = fork(); + if (pid == 0) { + char *argv[64]; + char *argv_ptr; + int i = 0; + char *arg = strtok_r(cmd, " ", &argv_ptr); + while (arg && i < 63) { + argv[i++] = arg; + arg = strtok_r(NULL, " ", &argv_ptr); + } + argv[i] = NULL; + + execvp(argv[0], argv); + perror("execvp"); + _exit(127); + } + else if (pid > 0) { + // parent: don’t wait, just continue (background) + } + else { + perror("fork"); + } + + printf("DEBUG: exec command: '%s'\n", comma); + + comma = strtok_r(NULL, ",", &comma_ptr); + } + } else { fprintf(stderr, "sxwmrc:%d: unknown option '%s'\n", lineno, key); } -- cgit v1.2.3 From e80d59d3ae22bef8aea5282fca64204c6cad1492 Mon Sep 17 00:00:00 2001 From: werdl Date: Sun, 1 Jun 2025 17:31:29 +0100 Subject: adjust syntax of exec config keyword --- src/parser.c | 66 ++++++++++++++++++++++-------------------------------------- 1 file changed, 24 insertions(+), 42 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index baa15a0..cfd7c00 100644 --- a/src/parser.c +++ b/src/parser.c @@ -171,6 +171,7 @@ found: char line[512]; int lineno = 0; int should_floatn = 0; + int torun = 0; // Initialize should_float matrix for (int j = 0; j < 256; j++) { @@ -365,55 +366,36 @@ found: char *final = strip(win); char *comma_ptr; - printf("DEBUG: exec command: '%s'\n", final); - char *comma = strtok_r(final, ",", &comma_ptr); - while (comma) { - comma = strip(comma); - if (*comma == '"') { - comma++; - } - char *end = comma + strlen(comma) - 1; - if (*end == '"') { - *end = '\0'; - } + char *cmd = strip(final); + if (*cmd == '"') { + cmd++; + } else { + fprintf(stderr, "sxwmrc:%d: exec not enclosed in quotes", lineno); + } - char *cmd = strdup(comma); - - // we have to fork and exec, because system() is blocking - pid_t pid = fork(); - if (pid == 0) { - char *argv[64]; - char *argv_ptr; - int i = 0; - char *arg = strtok_r(cmd, " ", &argv_ptr); - while (arg && i < 63) { - argv[i++] = arg; - arg = strtok_r(NULL, " ", &argv_ptr); - } - argv[i] = NULL; - execvp(argv[0], argv); - perror("execvp"); - _exit(127); - } - else if (pid > 0) { - // parent: don’t wait, just continue (background) - } - else { - perror("fork"); - } - - printf("DEBUG: exec command: '%s'\n", comma); - - comma = strtok_r(NULL, ",", &comma_ptr); - } - } + char *end = cmd + strlen(cmd) - 1; + if (*end == '"') { + *end = '\0'; + } else { + fprintf(stderr, "sxwmrc:%d: exec not enclosed in quotes", lineno); + } + + printf("DEBUG: exec command '%s'\n", cmd); + cfg->torun[torun] = strdup(cmd); + + if (torun > 254) { + fprintf(stderr, "sxwmrc:%d: too many execs", lineno); + } else { + torun++; + } + } else { fprintf(stderr, "sxwmrc:%d: unknown option '%s'\n", lineno, key); } } - + fclose(f); remap_and_dedupe_binds(cfg); return 0; -- cgit v1.2.3