diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2012-12-06 12:47:49 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2012-12-06 12:47:49 +0000 |
commit | 8b2c96f9c959cdc7f008c1944f6a871c3d06ab7a (patch) | |
tree | 98d7e463c6d2ee748c494a2b192a3fdaffd79e1e /usr.bin | |
parent | fbda61ad58e8dcb050be13e593b038adf3d78c3d (diff) |
Simplify command string parsing with a helper function from Tiago Cunha.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/cmd-string.c | 55 |
1 files 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 <nicm@users.sourceforge.net> @@ -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; } |