From 0735d1e62ad0f1a474b7bd758fe03f2e434f5c40 Mon Sep 17 00:00:00 2001 From: werdl Date: Wed, 28 May 2025 08:42:25 +0100 Subject: Add OpenBSD installation instructions --- src/parser.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/parser.c b/src/parser.c index 7cc59d1..0dcc03e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -166,6 +166,7 @@ int parser(Config *cfg) } found: + if (0) {} // label followed by declaration is a C23 extension FILE *f = fopen(path, "r"); if (!f) { fprintf(stderr, "sxwmrc: cannot open %s\n", path); -- cgit v1.2.3 From 3f92a7181d251d7429c195f856557675cd3a10f5 Mon Sep 17 00:00:00 2001 From: werdl Date: Wed, 28 May 2025 10:22:24 +0100 Subject: improve should_float parsing (make comments and trailing spaces work), ensure segfaults are no longer a problem --- src/parser.c | 83 ++++++++++++++++++++++++++++++------------------------------ src/sxwm.c | 51 +++++++++++++++++++------------------ 2 files changed, 68 insertions(+), 66 deletions(-) (limited to 'src') diff --git a/src/parser.c b/src/parser.c index 0dcc03e..85b6d20 100644 --- a/src/parser.c +++ b/src/parser.c @@ -143,22 +143,22 @@ int parser(Config *cfg) // check $XDG_CONFIG_HOME/sxwmrc, then $XDG_CONFIG_HOME/sxwm/sxwmrc, then $HOME/.config/sxwmrc const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); - if (xdg_config_home) { - snprintf(path, sizeof path, "%s/sxwmrc", xdg_config_home); - if (access(path, R_OK) == 0) { - goto found; - } - - snprintf(path, sizeof path, "%s/sxwm/sxwmrc", xdg_config_home); - if (access(path, R_OK) == 0) { - goto found; - } - } - - snprintf(path, sizeof path, "%s/.config/sxwmrc", home); - if (access(path, R_OK) == 0) { - goto found; - } + if (xdg_config_home) { + snprintf(path, sizeof path, "%s/sxwmrc", xdg_config_home); + if (access(path, R_OK) == 0) { + goto found; + } + + snprintf(path, sizeof path, "%s/sxwm/sxwmrc", xdg_config_home); + if (access(path, R_OK) == 0) { + goto found; + } + } + + snprintf(path, sizeof path, "%s/.config/sxwmrc", home); + if (access(path, R_OK) == 0) { + goto found; + } snprintf(path, sizeof path, "/usr/local/share/sxwmrc"); if (access(path, R_OK) == 0) { @@ -166,19 +166,20 @@ int parser(Config *cfg) } found: - if (0) {} // label followed by declaration is a C23 extension - FILE *f = fopen(path, "r"); - if (!f) { - fprintf(stderr, "sxwmrc: cannot open %s\n", path); - return -1; - } + if (0) { + } // label followed by declaration is a C23 extension + FILE *f = fopen(path, "r"); + if (!f) { + fprintf(stderr, "sxwmrc: cannot open %s\n", path); + return -1; + } char line[512]; int lineno = 0; int should_floatn = 0; - for (int i = 0; i < 256; i++) { - cfg->should_float[i] = NULL; + for (int j = 0; j < 256; j++) { + cfg->should_float[j] = calloc(256, sizeof(char *)); // allocate array of 256 strings } while (fgets(line, sizeof line, f)) { @@ -235,18 +236,28 @@ found: } else if (!strcmp(key, "should_float")) { // should_float: binary --arg,binary2 parameter --arg,binary3 - + if (should_floatn >= 256) { fprintf(stderr, "sxwmrc:%d: too many should_float entries\n", lineno); continue; } char *win = strip(rest); - - cfg->should_float[should_floatn] = malloc(256 * sizeof(char *)); + // remove comments + char *nocom = malloc(strlen(win) + 1); + char *comment = strchr(win, '#'); + if (comment) { + strncpy(nocom, win, comment - win); + nocom[comment - win] = '\0'; + } else { + strcpy(nocom, win); + } + + char *final = strip(nocom); + char *comma_ptr, *space_ptr; - char *comma = strtok_r(win, ",", &comma_ptr); + char *comma = strtok_r(final, ",", &comma_ptr); while (comma) { if (should_floatn < 256) { @@ -263,7 +274,6 @@ found: char *argv = strtok_r(comma, " ", &space_ptr); int i = 0; - while (argv) { printf("argv: %s\n", argv); cfg->should_float[should_floatn][i] = strdup(argv); @@ -272,16 +282,14 @@ found: } should_floatn++; - cfg->should_float[should_floatn] = malloc(256 * sizeof(char *)); - - } else { + } + else { fprintf(stderr, "sxwmrc:%d: too many should_float entries\n", lineno); break; } comma = strtok_r(NULL, ",", &comma_ptr); } - should_floatn++; } else if (!strcmp(key, "call") || !strcmp(key, "bind")) { @@ -365,15 +373,6 @@ found: } } - // print should_float - for (int i = 0; i < should_floatn; i++) { - fprintf(stderr, "sxwmrc: should_float[%d]: ", i); - for (int j = 0; cfg->should_float[i][j]; j++) { - fprintf(stderr, "%s ", cfg->should_float[i][j]); - } - fprintf(stderr, "\n"); - } - fclose(f); remap_and_dedupe_binds(cfg); return 0; diff --git a/src/sxwm.c b/src/sxwm.c index d3917c7..eb90d8a 100644 --- a/src/sxwm.c +++ b/src/sxwm.c @@ -591,19 +591,31 @@ void hdl_keypress(XEvent *xev) switch (b->type) { case TYPE_CMD: spawn(b->action.cmd); - for (int j = 0; j < 256; j++) { - Bool valid = False; - for (int k = 0; user_config.should_float[j] && user_config.should_float[j][k] && b->action.cmd[k]; k++) { - if (!strcmp(b->action.cmd[k], user_config.should_float[j][k])) { - valid = True; - break; - } - } - if (valid) { + next_should_float = False; + for (int j = 0; j < 256; j++) { + Bool all_matching = True; + for (int k = 0; k < 256; k++) { + if (!user_config.should_float[j] || !b->action.cmd) + continue; + + if (!user_config.should_float[j][k] || !b->action.cmd[k]) { + all_matching = (!user_config.should_float[j][k] && !b->action.cmd[k]); + break; + } + // confirm these two entries match + if (strcmp(user_config.should_float[j][k], b->action.cmd[k]) != 0) { + all_matching = False; + printf("%s != %s\n", user_config.should_float[j][k], b->action.cmd[k]); + break; + } + } + if (all_matching) { next_should_float = True; break; } } + + end: break; case TYPE_FUNC: if (b->action.fn) @@ -739,7 +751,7 @@ void hdl_map_req(XEvent *xev) should_float = True; c->fixed = True; } - + if (should_float || global_floating || next_should_float) { c->floating = True; @@ -752,9 +764,8 @@ void hdl_map_req(XEvent *xev) c->w = wa.width; c->h = wa.height; - XConfigureWindow( - dpy, c->win, CWX | CWY | CWWidth | CWHeight, - &(XWindowChanges){.x = c->x, .y = c->y, .width = c->w, .height = c->h}); + XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight, + &(XWindowChanges){.x = c->x, .y = c->y, .width = c->w, .height = c->h}); } tile(); @@ -766,8 +777,7 @@ void hdl_map_req(XEvent *xev) } next_should_float = False; } - } - + } /* center floating windows & set border */ if (c->floating && !c->fullscreen) { @@ -1551,15 +1561,8 @@ void toggle_floating(void) focused->h = wa.height; XConfigureWindow( - dpy, focused->win, - CWX | CWY | CWWidth | CWHeight, - &(XWindowChanges){ - .x = focused->x, - .y = focused->y, - .width = focused->w, - .height = focused->h - } - ); + dpy, focused->win, CWX | CWY | CWWidth | CWHeight, + &(XWindowChanges){.x = focused->x, .y = focused->y, .width = focused->w, .height = focused->h}); } } -- cgit v1.2.3