From 8264e92b3797cda37cc6610ddc26138ba46e2832 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Tue, 27 Nov 2012 22:59:34 +0000 Subject: [PATCH 1/5] Fix return value of load_cfg, from Thomas Adam. --- cfg.c | 4 ++-- tmux.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cfg.c b/cfg.c index 3da6ea4d..b64a4d6a 100644 --- a/cfg.c +++ b/cfg.c @@ -73,7 +73,7 @@ cfg_add_cause(struct causelist *causes, const char *fmt, ...) * Load configuration file. Returns -1 for an error with a list of messages in * causes. Note that causes must be initialised by the caller! */ -int +enum cmd_retval load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) { FILE *f; @@ -86,7 +86,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) if ((f = fopen(path, "rb")) == NULL) { cfg_add_cause(causes, "%s: %s", path, strerror(errno)); - return (-1); + return (CMD_RETURN_ERROR); } n = 0; diff --git a/tmux.h b/tmux.h index 981d4f21..429f2d7f 100644 --- a/tmux.h +++ b/tmux.h @@ -1520,7 +1520,7 @@ extern int cfg_finished; extern int cfg_references; extern struct causelist cfg_causes; void printflike2 cfg_add_cause(struct causelist *, const char *, ...); -int load_cfg(const char *, struct cmd_ctx *, struct causelist *); +enum cmd_retval load_cfg(const char *, struct cmd_ctx *, struct causelist *); void show_cfg_causes(struct session *); /* format.c */ From 51a1dbfe09f1a8ad543f818c836057c6a56e0938 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 6 Dec 2012 12:47:48 +0000 Subject: [PATCH 2/5] Simplify command string parsing with a helper function from Tiago Cunha. --- cmd-string.c | 53 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/cmd-string.c b/cmd-string.c index 93beb3ca..5a27c5bd 100644 --- a/cmd-string.c +++ b/cmd-string.c @@ -31,11 +31,12 @@ * Parse a command from a string. */ -int cmd_string_getc(const char *, size_t *); -void cmd_string_ungetc(size_t *); -char *cmd_string_string(const char *, size_t *, char, int); -char *cmd_string_variable(const char *, size_t *); -char *cmd_string_expand_tilde(const char *, size_t *); +int cmd_string_getc(const char *, size_t *); +void cmd_string_ungetc(size_t *); +void cmd_string_copy(char **, char *, size_t *); +char *cmd_string_string(const char *, size_t *, char, int); +char *cmd_string_variable(const char *, size_t *); +char *cmd_string_expand_tilde(const char *, size_t *); int cmd_string_getc(const char *s, size_t *p) @@ -84,26 +85,17 @@ cmd_string_parse(const char *s, struct cmd_list **cmdlist, char **cause) case '\'': if ((t = cmd_string_string(s, &p, '\'', 0)) == NULL) goto error; - buf = xrealloc(buf, 1, len + strlen(t) + 1); - strlcpy(buf + len, t, strlen(t) + 1); - len += strlen(t); - free(t); + cmd_string_copy(&buf, t, &len); break; case '"': if ((t = cmd_string_string(s, &p, '"', 1)) == NULL) goto error; - buf = xrealloc(buf, 1, len + strlen(t) + 1); - strlcpy(buf + len, t, strlen(t) + 1); - len += strlen(t); - free(t); + cmd_string_copy(&buf, t, &len); break; case '$': if ((t = cmd_string_variable(s, &p)) == NULL) goto error; - buf = xrealloc(buf, 1, len + strlen(t) + 1); - strlcpy(buf + len, t, strlen(t) + 1); - len += strlen(t); - free(t); + cmd_string_copy(&buf, t, &len); break; case '#': /* Comment: discard rest of line. */ @@ -147,12 +139,10 @@ cmd_string_parse(const char *s, struct cmd_list **cmdlist, char **cause) goto out; case '~': if (buf == NULL) { - if ((t = cmd_string_expand_tilde(s, &p)) == NULL) + t = cmd_string_expand_tilde(s, &p); + if (t == NULL) goto error; - buf = xrealloc(buf, 1, len + strlen(t) + 1); - strlcpy(buf + len, t, strlen(t) + 1); - len += strlen(t); - free(t); + cmd_string_copy(&buf, t, &len); break; } /* FALLTHROUGH */ @@ -181,6 +171,20 @@ out: return (rval); } +void +cmd_string_copy(char **dst, char *src, size_t *len) +{ + size_t srclen; + + srclen = strlen(src); + + *dst = xrealloc(*dst, 1, *len + srclen + 1); + strlcpy(*dst + *len, src, srclen + 1); + + *len += srclen; + free(src); +} + char * cmd_string_string(const char *s, size_t *p, char endch, int esc) { @@ -220,10 +224,7 @@ cmd_string_string(const char *s, size_t *p, char endch, int esc) break; if ((t = cmd_string_variable(s, p)) == NULL) goto error; - buf = xrealloc(buf, 1, len + strlen(t) + 1); - strlcpy(buf + len, t, strlen(t) + 1); - len += strlen(t); - free(t); + cmd_string_copy(&buf, t, &len); continue; } From 8378be03d1dd260fda325c15fb047da8ddd12c1c Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 6 Dec 2012 12:49:13 +0000 Subject: [PATCH 3/5] Fix argument order in a log statement. --- server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.c b/server.c index bad22270..a3632ad7 100644 --- a/server.c +++ b/server.c @@ -166,7 +166,7 @@ server_start(int lockfd, char *lockfile) load_cfg(SYSTEM_CFG, NULL, &cfg_causes); else if (errno != ENOENT) { cfg_add_cause( - &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG); + &cfg_causes, "%s: %s", SYSTEM_CFG, strerror(errno)); } if (cfg_file != NULL) load_cfg(cfg_file, NULL, &cfg_causes); From 8600fe054bd5db89f8e5758d657018f111be0610 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 6 Dec 2012 13:06:05 +0000 Subject: [PATCH 4/5] Use strlcat not strncat in load_cfg and some other trivial tidying from Tiago Cunha. --- cfg.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/cfg.c b/cfg.c index b64a4d6a..5b9ef763 100644 --- a/cfg.c +++ b/cfg.c @@ -79,7 +79,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) FILE *f; u_int n; char *buf, *line, *cause; - size_t len; + size_t len, newlen; struct cmd_list *cmdlist; struct cmd_ctx ctx; enum cmd_retval retval; @@ -88,31 +88,35 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) cfg_add_cause(causes, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } - n = 0; cfg_references++; + n = 0; line = NULL; retval = CMD_RETURN_NORMAL; while ((buf = fgetln(f, &len))) { if (buf[len - 1] == '\n') len--; - if (line != NULL) - line = xrealloc(line, 1, strlen(line) + len + 1); - else { - line = xmalloc(len + 1); + /* Current line is the continuation of the previous one. */ + if (line != NULL) { + newlen = strlen(line) + len + 1; + line = xrealloc(line, 1, newlen); + } else { + newlen = len + 1; + line = xmalloc(newlen); *line = '\0'; } - /* Append buffer to line. strncat will terminate. */ - strncat(line, buf, len); + /* Append current line to the previous. */ + strlcat(line, buf, newlen); n++; /* Continuation: get next line? */ len = strlen(line); if (len > 0 && line[len - 1] == '\\') { line[len - 1] = '\0'; + /* Ignore escaped backslash at EOL. */ if (len > 1 && line[len - 2] != '\\') continue; @@ -127,11 +131,10 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) cfg_add_cause(causes, "%s: %u: %s", path, n, cause); free(cause); continue; - } else - free(buf); + } + free(buf); if (cmdlist == NULL) continue; - cfg_cause = NULL; if (ctxin == NULL) { ctx.msgdata = NULL; @@ -162,8 +165,7 @@ load_cfg(const char *path, struct cmd_ctx *ctxin, struct causelist *causes) } cmd_list_free(cmdlist); if (cfg_cause != NULL) { - cfg_add_cause( - causes, "%s: %d: %s", path, n, cfg_cause); + cfg_add_cause(causes, "%s: %d: %s", path, n, cfg_cause); free(cfg_cause); } } From 3fa4f691e32482b2d07d16be84b6e22657f9c7dd Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sat, 8 Dec 2012 17:05:57 +0000 Subject: [PATCH 5/5] Handle resetting 256-colours properly when parsing #[default], #[fg=default] and #[bg=default] styles. --- screen-write.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/screen-write.c b/screen-write.c index ce9411b6..ec7d741e 100644 --- a/screen-write.c +++ b/screen-write.c @@ -324,6 +324,9 @@ screen_write_parsestyle( fg = defgc->fg; bg = defgc->bg; attr = defgc->attr; + flags &= ~(GRID_FLAG_FG256|GRID_FLAG_BG256); + flags |= + defgc->flags & (GRID_FLAG_FG256|GRID_FLAG_BG256); } else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) { if ((val = colour_fromstring(tmp + 3)) == -1) return; @@ -335,8 +338,11 @@ screen_write_parsestyle( } else flags &= ~GRID_FLAG_FG256; fg = val; - } else + } else { fg = defgc->fg; + flags &= ~GRID_FLAG_FG256; + flags |= defgc->flags & GRID_FLAG_FG256; + } } else if (*in == 'b' || *in == 'B') { if (val != 8) { if (val & 0x100) { @@ -345,8 +351,11 @@ screen_write_parsestyle( } else flags &= ~GRID_FLAG_BG256; bg = val; - } else + } else { bg = defgc->bg; + flags &= ~GRID_FLAG_BG256; + flags |= defgc->flags & GRID_FLAG_BG256; + } } else return; } else if (end > 2 && strncasecmp(tmp, "no", 2) == 0) {