Add a load of miscellaneous pane formats, from George Nachman.
[tmux-openbsd.git] / cmd-string.c
blob5a27c5bdc3428f0d61fd979c9ec0c0f13b367c9d
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 void cmd_string_copy(char **, char *, size_t *);
37 char *cmd_string_string(const char *, size_t *, char, int);
38 char *cmd_string_variable(const char *, size_t *);
39 char *cmd_string_expand_tilde(const char *, size_t *);
41 int
42 cmd_string_getc(const char *s, size_t *p)
44 const u_char *ucs = s;
46 if (ucs[*p] == '\0')
47 return (EOF);
48 return (ucs[(*p)++]);
51 void
52 cmd_string_ungetc(size_t *p)
54 (*p)--;
58 * Parse command string. Returns -1 on error. If returning -1, cause is error
59 * string, or NULL for empty command.
61 int
62 cmd_string_parse(const char *s, struct cmd_list **cmdlist, char **cause)
64 size_t p;
65 int ch, i, argc, rval;
66 char **argv, *buf, *t;
67 const char *whitespace, *equals;
68 size_t len;
70 argv = NULL;
71 argc = 0;
73 buf = NULL;
74 len = 0;
76 *cause = NULL;
78 *cmdlist = NULL;
79 rval = -1;
81 p = 0;
82 for (;;) {
83 ch = cmd_string_getc(s, &p);
84 switch (ch) {
85 case '\'':
86 if ((t = cmd_string_string(s, &p, '\'', 0)) == NULL)
87 goto error;
88 cmd_string_copy(&buf, t, &len);
89 break;
90 case '"':
91 if ((t = cmd_string_string(s, &p, '"', 1)) == NULL)
92 goto error;
93 cmd_string_copy(&buf, t, &len);
94 break;
95 case '$':
96 if ((t = cmd_string_variable(s, &p)) == NULL)
97 goto error;
98 cmd_string_copy(&buf, t, &len);
99 break;
100 case '#':
101 /* Comment: discard rest of line. */
102 while ((ch = cmd_string_getc(s, &p)) != EOF)
104 /* FALLTHROUGH */
105 case EOF:
106 case ' ':
107 case '\t':
108 if (buf != NULL) {
109 buf = xrealloc(buf, 1, len + 1);
110 buf[len] = '\0';
112 argv = xrealloc(argv, argc + 1, sizeof *argv);
113 argv[argc++] = buf;
115 buf = NULL;
116 len = 0;
119 if (ch != EOF)
120 break;
122 while (argc != 0) {
123 equals = strchr(argv[0], '=');
124 whitespace = argv[0] + strcspn(argv[0], " \t");
125 if (equals == NULL || equals > whitespace)
126 break;
127 environ_put(&global_environ, argv[0]);
128 argc--;
129 memmove(argv, argv + 1, argc * (sizeof *argv));
131 if (argc == 0)
132 goto out;
134 *cmdlist = cmd_list_parse(argc, argv, cause);
135 if (*cmdlist == NULL)
136 goto out;
138 rval = 0;
139 goto out;
140 case '~':
141 if (buf == NULL) {
142 t = cmd_string_expand_tilde(s, &p);
143 if (t == NULL)
144 goto error;
145 cmd_string_copy(&buf, t, &len);
146 break;
148 /* FALLTHROUGH */
149 default:
150 if (len >= SIZE_MAX - 2)
151 goto error;
153 buf = xrealloc(buf, 1, len + 1);
154 buf[len++] = ch;
155 break;
159 error:
160 xasprintf(cause, "invalid or unknown command: %s", s);
162 out:
163 free(buf);
165 if (argv != NULL) {
166 for (i = 0; i < argc; i++)
167 free(argv[i]);
168 free(argv);
171 return (rval);
174 void
175 cmd_string_copy(char **dst, char *src, size_t *len)
177 size_t srclen;
179 srclen = strlen(src);
181 *dst = xrealloc(*dst, 1, *len + srclen + 1);
182 strlcpy(*dst + *len, src, srclen + 1);
184 *len += srclen;
185 free(src);
188 char *
189 cmd_string_string(const char *s, size_t *p, char endch, int esc)
191 int ch;
192 char *buf, *t;
193 size_t len;
195 buf = NULL;
196 len = 0;
198 while ((ch = cmd_string_getc(s, p)) != endch) {
199 switch (ch) {
200 case EOF:
201 goto error;
202 case '\\':
203 if (!esc)
204 break;
205 switch (ch = cmd_string_getc(s, p)) {
206 case EOF:
207 goto error;
208 case 'e':
209 ch = '\033';
210 break;
211 case 'r':
212 ch = '\r';
213 break;
214 case 'n':
215 ch = '\n';
216 break;
217 case 't':
218 ch = '\t';
219 break;
221 break;
222 case '$':
223 if (!esc)
224 break;
225 if ((t = cmd_string_variable(s, p)) == NULL)
226 goto error;
227 cmd_string_copy(&buf, t, &len);
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 free(buf);
243 return (NULL);
246 char *
247 cmd_string_variable(const char *s, size_t *p)
249 int ch, fch;
250 char *buf, *t;
251 size_t len;
252 struct environ_entry *envent;
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 envent = environ_find(&global_environ, buf);
305 free(buf);
306 if (envent == NULL)
307 return (xstrdup(""));
308 return (xstrdup(envent->value));
310 error:
311 free(buf);
312 return (NULL);
315 char *
316 cmd_string_expand_tilde(const char *s, size_t *p)
318 struct passwd *pw;
319 struct environ_entry *envent;
320 char *home, *path, *username;
322 home = NULL;
323 if (cmd_string_getc(s, p) == '/') {
324 envent = environ_find(&global_environ, "HOME");
325 if (envent != NULL && *envent->value != '\0')
326 home = envent->value;
327 else if ((pw = getpwuid(getuid())) != NULL)
328 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 free(username);
337 if (home == NULL)
338 return (NULL);
340 xasprintf(&path, "%s/", home);
341 return (path);