X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=spectrwm.c;h=09c7ede0b67b5bfc1e90cabcc7ea03567845a935;hb=ae4071ff80081b461c539001c45d6401adc732ca;hp=7a0ccf2b17051dca0f1182a47b362ddfd55c746a;hpb=8ce185e7ff585c29649990f20cc6e69d918cab64;p=spectrwm.git diff --git a/spectrwm.c b/spectrwm.c index 7a0ccf2..09c7ede 100644 --- a/spectrwm.c +++ b/spectrwm.c @@ -1472,78 +1472,116 @@ bar_fmt(char *fmtexp, char *fmtnew, struct swm_region *r, size_t sz) strlcat(fmtnew, " ", sz); if (window_name_enabled) - strlcat(fmtnew, "+W", sz); + strlcat(fmtnew, "+64W", sz); /* finally add the action script output and the version */ strlcat(fmtnew, " +A +V", sz); } /* replaces the bar format character sequences (like in tmux(1)) */ -void -bar_replace(char *fmt, char *fmtrep, int nscreen, struct swm_region *r, +char * +bar_replace_seq(char *fmt, char *fmtrep, struct swm_region *r, size_t *offrep, size_t sz) { char *ptr; - char tmp[SWM_BAR_MAX]; + char num[8], tmp[SWM_BAR_MAX]; + int limit; + size_t len, numoff = 0; + + /* reset strlcat(3) buffer */ + *tmp = '\0'; + + /* get number, if any */ + fmt++; + while (*fmt != '\0' && isdigit((unsigned char) *fmt)) { + if (numoff >= sizeof num - 1) + break; + num[numoff++] = *fmt++; + } + num[numoff] = '\0'; + + if ((limit = strtonum(num, 1, sizeof tmp - 1, NULL)) == 0) + limit = sizeof tmp - 1; + + /* if number is too big, skip to the first non-digit */ + if (numoff >= sizeof num - 1) { + while (*fmt != '\0' && isdigit((unsigned char) *fmt)) + fmt++; + } + /* there is nothing to replace (ie EOL) */ + if (*fmt == '\0') + return (fmt); + + switch (*fmt) { + case 'A': + snprintf(tmp, sizeof tmp, "%s", bar_ext); + break; + case 'C': + bar_class_name(tmp, sizeof tmp, r); + break; + case 'D': + bar_workspace_name(tmp, sizeof tmp, r); + break; + case 'I': + snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1); + break; + case 'N': + snprintf(tmp, sizeof tmp, "%d", r->s->idx + 1); + break; + case 'S': + snprintf(tmp, sizeof tmp, "%s", r->ws->stacker); + break; + case 'T': + bar_title_name(tmp, sizeof tmp, r); + break; + case 'U': + bar_urgent(tmp, sizeof tmp); + break; + case 'V': + snprintf(tmp, sizeof tmp, "%s", bar_vertext); + break; + case 'W': + bar_window_name(tmp, sizeof tmp, r); + break; + default: + /* unknown character sequence; copy as-is */ + snprintf(tmp, sizeof tmp, "+%c", *fmt); + break; + } + + len = strlen(tmp); + ptr = tmp; + if (len < limit) + limit = len; + while (limit-- > 0) { + if (*offrep >= sz - 1) + break; + fmtrep[(*offrep)++] = *ptr++; + } + + fmt++; + return (fmt); +} + +void +bar_replace(char *fmt, char *fmtrep, struct swm_region *r, size_t sz) +{ size_t off; off = 0; - ptr = fmt; - while (*ptr != '\0') { - if (*ptr != '+') { + while (*fmt != '\0') { + if (*fmt != '+') { /* skip ordinary characters */ if (off >= sz - 1) break; - fmtrep[off++] = *ptr++; + fmtrep[off++] = *fmt++; continue; } /* character sequence found; replace it */ - *tmp = '\0'; - - switch (*++ptr) { - case 'A': - snprintf(tmp, sizeof tmp, "%s", bar_ext); - break; - case 'C': - bar_class_name(tmp, sizeof tmp, r); - break; - case 'D': - bar_workspace_name(tmp, sizeof tmp, r); - break; - case 'I': - snprintf(tmp, sizeof tmp, "%d", r->ws->idx + 1); - break; - case 'N': - snprintf(tmp, sizeof tmp, "%d", nscreen + 1); + fmt = bar_replace_seq(fmt, fmtrep, r, &off, sz); + if (off >= sz - 1) break; - case 'S': - snprintf(tmp, sizeof tmp, "%s", r->ws->stacker); - break; - case 'T': - bar_title_name(tmp, sizeof tmp, r); - break; - case 'U': - bar_urgent(tmp, sizeof tmp); - break; - case 'V': - snprintf(tmp, sizeof tmp, "%s", bar_vertext); - break; - case 'W': - bar_window_name(tmp, sizeof tmp, r); - break; - default: - /* unknown character sequence; copy as-is */ - snprintf(tmp, sizeof tmp, "+%c", *ptr); - break; - } - - off += snprintf(fmtrep + off, sz - off, "%s", tmp); - if (off >= sz - 1) { - off = sz; - break; - } - ptr++; } fmtrep[off] = '\0'; @@ -1576,7 +1614,8 @@ bar_fmt_expand(char *fmtexp, size_t sz) strlcpy(fmtexp, fmt, sz); /* finally pass the string through strftime(3) */ #ifndef SWM_DENY_CLOCK_FORMAT - len = strftime(fmtexp, sz, fmt, &tm); + if ((len = strftime(fmtexp, sz, fmt, &tm)) == 0) + warnx("format too long"); fmtexp[len] = '\0'; #endif } @@ -1595,7 +1634,7 @@ bar_fmt_print(void) for (i = 0; i < ScreenCount(display); i++) { TAILQ_FOREACH(r, &screens[i].rl, entry) { bar_fmt(fmtexp, fmtnew, r, sizeof fmtnew); - bar_replace(fmtnew, fmtrep, i, r, sizeof fmtrep); + bar_replace(fmtnew, fmtrep, r, sizeof fmtrep); bar_print(r, fmtrep); } }