From 8b2c96f9c959cdc7f008c1944f6a871c3d06ab7a Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Thu, 6 Dec 2012 12:47:49 +0000 Subject: Simplify command string parsing with a helper function from Tiago Cunha. --- usr.bin/tmux/cmd-string.c | 55 ++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/usr.bin/tmux/cmd-string.c b/usr.bin/tmux/cmd-string.c index 97500bd28ca..29cf9301660 100644 --- a/usr.bin/tmux/cmd-string.c +++ b/usr.bin/tmux/cmd-string.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-string.c,v 1.16 2012/07/10 11:53:01 nicm Exp $ */ +/* $OpenBSD: cmd-string.c,v 1.17 2012/12/06 12:47:48 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -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; } -- cgit v1.2.3