diff options
| author | werdl <werdl_@outlook.com> | 2025-05-31 20:20:33 +0100 |
|---|---|---|
| committer | werdl <werdl_@outlook.com> | 2025-05-31 20:20:33 +0100 |
| commit | f869ac4b6038c151f5c89726e17e16db3b90e9b0 (patch) | |
| tree | 392d07d9180c3f455f96dc2f9bdec655b349c1f2 /src | |
| parent | 7189b13ef67c3a66930d94cad41a5becd2df9609 (diff) | |
add exec config file keyword
Diffstat (limited to 'src')
| -rw-r--r-- | src/parser.c | 56 |
1 files changed, 55 insertions, 1 deletions
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); } |
