Rename some imsg bits to make namespace collisions less likely buf to
[tmux-openbsd.git] / cmd-string.c
blob2a5e2b24b156af54116ad5a0deaa6c2a34d5a0de
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <errno.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
28 #include "tmux.h"
31 * Parse a command from a string.
34 int cmd_string_getc(const char *, size_t *);
35 void cmd_string_ungetc(size_t *);
36 char *cmd_string_string(const char *, size_t *, char, int);
37 char *cmd_string_variable(const char *, size_t *);
38 char *cmd_string_expand_tilde(const char *, size_t *);
40 int
41 cmd_string_getc(const char *s, size_t *p)
43 const u_char *ucs = s;
45 if (ucs[*p] == '\0')
46 return (EOF);
47 return (ucs[(*p)++]);
50 void
51 cmd_string_ungetc(size_t *p)
53 (*p)--;
57 * Parse command string. Returns -1 on error. If returning -1, cause is error
58 * string, or NULL for empty command.
60 int
61 cmd_string_parse(const char *s, struct cmd_list **cmdlist, char **cause)
63 size_t p;
64 int ch, i, argc, rval;
65 char **argv, *buf, *t;
66 const char *whitespace, *equals;
67 size_t len;
69 argv = NULL;
70 argc = 0;
72 buf = NULL;
73 len = 0;
75 *cause = NULL;
77 *cmdlist = NULL;
78 rval = -1;
80 p = 0;
81 for (;;) {
82 ch = cmd_string_getc(s, &p);
83 switch (ch) {
84 case '\'':
85 if ((t = cmd_string_string(s, &p, '\'', 0)) == NULL)
86 goto error;
87 buf = xrealloc(buf, 1, len + strlen(t) + 1);
88 strlcpy(buf + len, t, strlen(t) + 1);
89 len += strlen(t);
90 xfree(t);
91 break;
92 case '"':
93 if ((t = cmd_string_string(s, &p, '"', 1)) == NULL)
94 goto error;
95 buf = xrealloc(buf, 1, len + strlen(t) + 1);
96 strlcpy(buf + len, t, strlen(t) + 1);
97 len += strlen(t);
98 xfree(t);
99 break;
100 case '$':
101 if ((t = cmd_string_variable(s, &p)) == NULL)
102 goto error;
103 buf = xrealloc(buf, 1, len + strlen(t) + 1);
104 strlcpy(buf + len, t, strlen(t) + 1);
105 len += strlen(t);
106 xfree(t);
107 break;
108 case '#':
109 /* Comment: discard rest of line. */
110 while ((ch = cmd_string_getc(s, &p)) != EOF)
112 /* FALLTHROUGH */
113 case EOF:
114 case ' ':
115 case '\t':
116 if (buf != NULL) {
117 buf = xrealloc(buf, 1, len + 1);
118 buf[len] = '\0';
120 argv = xrealloc(argv, argc + 1, sizeof *argv);
121 argv[argc++] = buf;
123 buf = NULL;
124 len = 0;
127 if (ch != EOF)
128 break;
130 while (argc != 0) {
131 equals = strchr(argv[0], '=');
132 whitespace = argv[0] + strcspn(argv[0], " \t");
133 if (equals == NULL || equals > whitespace)
134 break;
135 environ_put(&global_environ, argv[0]);
136 argc--;
137 memmove(argv, argv + 1, argc * (sizeof *argv));
139 if (argc == 0)
140 goto out;
142 *cmdlist = cmd_list_parse(argc, argv, cause);
143 if (*cmdlist == NULL)
144 goto out;
146 rval = 0;
147 goto out;
148 case '~':
149 if (buf == NULL) {
150 if ((t = cmd_string_expand_tilde(s, &p)) == NULL)
151 goto error;
152 buf = xrealloc(buf, 1, len + strlen(t) + 1);
153 strlcpy(buf + len, t, strlen(t) + 1);
154 len += strlen(t);
155 xfree(t);
156 break;
158 /* FALLTHROUGH */
159 default:
160 if (len >= SIZE_MAX - 2)
161 goto error;
163 buf = xrealloc(buf, 1, len + 1);
164 buf[len++] = ch;
165 break;
169 error:
170 xasprintf(cause, "invalid or unknown command: %s", s);
172 out:
173 if (buf != NULL)
174 xfree(buf);
176 if (argv != NULL) {
177 for (i = 0; i < argc; i++)
178 xfree(argv[i]);
179 xfree(argv);
182 return (rval);
185 char *
186 cmd_string_string(const char *s, size_t *p, char endch, int esc)
188 int ch;
189 char *buf, *t;
190 size_t len;
192 buf = NULL;
193 len = 0;
195 while ((ch = cmd_string_getc(s, p)) != endch) {
196 switch (ch) {
197 case EOF:
198 goto error;
199 case '\\':
200 if (!esc)
201 break;
202 switch (ch = cmd_string_getc(s, p)) {
203 case EOF:
204 goto error;
205 case 'e':
206 ch = '\033';
207 break;
208 case 'r':
209 ch = '\r';
210 break;
211 case 'n':
212 ch = '\n';
213 break;
214 case 't':
215 ch = '\t';
216 break;
218 break;
219 case '$':
220 if (!esc)
221 break;
222 if ((t = cmd_string_variable(s, p)) == NULL)
223 goto error;
224 buf = xrealloc(buf, 1, len + strlen(t) + 1);
225 strlcpy(buf + len, t, strlen(t) + 1);
226 len += strlen(t);
227 xfree(t);
228 continue;
231 if (len >= SIZE_MAX - 2)
232 goto error;
233 buf = xrealloc(buf, 1, len + 1);
234 buf[len++] = ch;
237 buf = xrealloc(buf, 1, len + 1);
238 buf[len] = '\0';
239 return (buf);
241 error:
242 if (buf != NULL)
243 xfree(buf);
244 return (NULL);
247 char *
248 cmd_string_variable(const char *s, size_t *p)
250 int ch, fch;
251 char *buf, *t;
252 size_t len;
254 #define cmd_string_first(ch) ((ch) == '_' || \
255 ((ch) >= 'a' && (ch) <= 'z') || ((ch) >= 'A' && (ch) <= 'Z'))
256 #define cmd_string_other(ch) ((ch) == '_' || \
257 ((ch) >= 'a' && (ch) <= 'z') || ((ch) >= 'A' && (ch) <= 'Z') || \
258 ((ch) >= '0' && (ch) <= '9'))
260 buf = NULL;
261 len = 0;
263 fch = EOF;
264 switch (ch = cmd_string_getc(s, p)) {
265 case EOF:
266 goto error;
267 case '{':
268 fch = '{';
270 ch = cmd_string_getc(s, p);
271 if (!cmd_string_first(ch))
272 goto error;
273 /* FALLTHROUGH */
274 default:
275 if (!cmd_string_first(ch)) {
276 xasprintf(&t, "$%c", ch);
277 return (t);
280 buf = xrealloc(buf, 1, len + 1);
281 buf[len++] = ch;
283 for (;;) {
284 ch = cmd_string_getc(s, p);
285 if (ch == EOF || !cmd_string_other(ch))
286 break;
287 else {
288 if (len >= SIZE_MAX - 3)
289 goto error;
290 buf = xrealloc(buf, 1, len + 1);
291 buf[len++] = ch;
296 if (fch == '{' && ch != '}')
297 goto error;
298 if (ch != EOF && fch != '{')
299 cmd_string_ungetc(p); /* ch */
301 buf = xrealloc(buf, 1, len + 1);
302 buf[len] = '\0';
304 if ((t = getenv(buf)) == NULL) {
305 xfree(buf);
306 return (xstrdup(""));
308 xfree(buf);
309 return (xstrdup(t));
311 error:
312 if (buf != NULL)
313 xfree(buf);
314 return (NULL);
317 char *
318 cmd_string_expand_tilde(const char *s, size_t *p)
320 struct passwd *pw;
321 char *home, *path, *username;
323 home = NULL;
324 if (cmd_string_getc(s, p) == '/') {
325 if ((home = getenv("HOME")) == NULL || *home == '\0') {
326 if ((pw = getpwuid(getuid())) != NULL)
327 home = pw->pw_dir;
329 } else {
330 cmd_string_ungetc(p);
331 if ((username = cmd_string_string(s, p, '/', 0)) == NULL)
332 return (NULL);
333 if ((pw = getpwnam(username)) != NULL)
334 home = pw->pw_dir;
335 xfree(username);
337 if (home == NULL)
338 return (NULL);
340 xasprintf(&path, "%s/", home);
341 return (path);