Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / cmd-string.c
blob9384f9e25d00eb9b94c7714db0468bfd8465d2e3
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;
253 struct environ_entry *envent;
255 #define cmd_string_first(ch) ((ch) == '_' || \
256 ((ch) >= 'a' && (ch) <= 'z') || ((ch) >= 'A' && (ch) <= 'Z'))
257 #define cmd_string_other(ch) ((ch) == '_' || \
258 ((ch) >= 'a' && (ch) <= 'z') || ((ch) >= 'A' && (ch) <= 'Z') || \
259 ((ch) >= '0' && (ch) <= '9'))
261 buf = NULL;
262 len = 0;
264 fch = EOF;
265 switch (ch = cmd_string_getc(s, p)) {
266 case EOF:
267 goto error;
268 case '{':
269 fch = '{';
271 ch = cmd_string_getc(s, p);
272 if (!cmd_string_first(ch))
273 goto error;
274 /* FALLTHROUGH */
275 default:
276 if (!cmd_string_first(ch)) {
277 xasprintf(&t, "$%c", ch);
278 return (t);
281 buf = xrealloc(buf, 1, len + 1);
282 buf[len++] = ch;
284 for (;;) {
285 ch = cmd_string_getc(s, p);
286 if (ch == EOF || !cmd_string_other(ch))
287 break;
288 else {
289 if (len >= SIZE_MAX - 3)
290 goto error;
291 buf = xrealloc(buf, 1, len + 1);
292 buf[len++] = ch;
297 if (fch == '{' && ch != '}')
298 goto error;
299 if (ch != EOF && fch != '{')
300 cmd_string_ungetc(p); /* ch */
302 buf = xrealloc(buf, 1, len + 1);
303 buf[len] = '\0';
305 envent = environ_find(&global_environ, buf);
306 xfree(buf);
307 if (envent == NULL)
308 return (xstrdup(""));
309 return (xstrdup(envent->value));
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 struct environ_entry *envent;
322 char *home, *path, *username;
324 home = NULL;
325 if (cmd_string_getc(s, p) == '/') {
326 envent = environ_find(&global_environ, "HOME");
327 if (envent != NULL && *envent->value != '\0')
328 home = envent->value;
329 else if ((pw = getpwuid(getuid())) != NULL)
330 home = pw->pw_dir;
331 } else {
332 cmd_string_ungetc(p);
333 if ((username = cmd_string_string(s, p, '/', 0)) == NULL)
334 return (NULL);
335 if ((pw = getpwnam(username)) != NULL)
336 home = pw->pw_dir;
337 xfree(username);
339 if (home == NULL)
340 return (NULL);
342 xasprintf(&path, "%s/", home);
343 return (path);