xref: /netbsd-src/external/bsd/tmux/dist/arguments.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Manipulate command arguments.
29  */
30 
31 struct args_entry {
32 	u_char			 flag;
33 	char			*value;
34 	RB_ENTRY(args_entry)	 entry;
35 };
36 
37 static struct args_entry	*args_find(struct args *, u_char);
38 
39 static int	args_cmp(struct args_entry *, struct args_entry *);
40 RB_GENERATE_STATIC(args_tree, args_entry, entry, args_cmp);
41 
42 /* Arguments tree comparison function. */
43 static int
44 args_cmp(struct args_entry *a1, struct args_entry *a2)
45 {
46 	return (a1->flag - a2->flag);
47 }
48 
49 /* Find a flag in the arguments tree. */
50 static struct args_entry *
51 args_find(struct args *args, u_char ch)
52 {
53 	struct args_entry	entry;
54 
55 	entry.flag = ch;
56 	return (RB_FIND(args_tree, &args->tree, &entry));
57 }
58 
59 /* Parse an argv and argc into a new argument set. */
60 struct args *
61 args_parse(const char *template, int argc, char **argv)
62 {
63 	struct args	*args;
64 	int		 opt;
65 
66 	args = xcalloc(1, sizeof *args);
67 
68 	optreset = 1;
69 	optind = 1;
70 
71 	while ((opt = getopt(argc, argv, template)) != -1) {
72 		if (opt < 0)
73 			continue;
74 		if (opt == '?' || strchr(template, opt) == NULL) {
75 			args_free(args);
76 			return (NULL);
77 		}
78 		args_set(args, opt, optarg);
79 	}
80 	argc -= optind;
81 	argv += optind;
82 
83 	args->argc = argc;
84 	args->argv = cmd_copy_argv(argc, argv);
85 
86 	return (args);
87 }
88 
89 /* Free an arguments set. */
90 void
91 args_free(struct args *args)
92 {
93 	struct args_entry	*entry;
94 	struct args_entry	*entry1;
95 
96 	cmd_free_argv(args->argc, args->argv);
97 
98 	RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
99 		RB_REMOVE(args_tree, &args->tree, entry);
100 		free(entry->value);
101 		free(entry);
102 	}
103 
104 	free(args);
105 }
106 
107 /* Add to string. */
108 static void printflike(3, 4)
109 args_print_add(char **buf, size_t *len, const char *fmt, ...)
110 {
111 	va_list  ap;
112 	char	*s;
113 	size_t	 slen;
114 
115 	va_start(ap, fmt);
116 	slen = xvasprintf(&s, fmt, ap);
117 	va_end(ap);
118 
119 	*len += slen;
120 	*buf = xrealloc(*buf, *len);
121 
122 	strlcat(*buf, s, *len);
123 	free(s);
124 }
125 
126 /* Print a set of arguments. */
127 char *
128 args_print(struct args *args)
129 {
130 	size_t		 	 len;
131 	char			*buf, *escaped;
132 	int			 i, flags;
133 	struct args_entry	*entry;
134 	static const char	 quoted[] = " #\"';$";
135 
136 	len = 1;
137 	buf = xcalloc(1, len);
138 
139 	/* Process the flags first. */
140 	RB_FOREACH(entry, args_tree, &args->tree) {
141 		if (entry->value != NULL)
142 			continue;
143 
144 		if (*buf == '\0')
145 			args_print_add(&buf, &len, "-");
146 		args_print_add(&buf, &len, "%c", entry->flag);
147 	}
148 
149 	/* Then the flags with arguments. */
150 	RB_FOREACH(entry, args_tree, &args->tree) {
151 		if (entry->value == NULL)
152 			continue;
153 
154 		if (*buf != '\0')
155 			args_print_add(&buf, &len, " -%c ", entry->flag);
156 		else
157 			args_print_add(&buf, &len, "-%c ", entry->flag);
158 
159 		flags = VIS_OCTAL|VIS_TAB|VIS_NL;
160 		if (entry->value[strcspn(entry->value, quoted)] != '\0')
161 			flags |= VIS_DQ;
162 		utf8_stravis(&escaped, entry->value, flags);
163 		if (flags & VIS_DQ)
164 			args_print_add(&buf, &len, "\"%s\"", escaped);
165 		else
166 			args_print_add(&buf, &len, "%s", escaped);
167 		free(escaped);
168 	}
169 
170 	/* And finally the argument vector. */
171 	for (i = 0; i < args->argc; i++) {
172 		if (*buf != '\0')
173 			args_print_add(&buf, &len, " ");
174 
175 		flags = VIS_OCTAL|VIS_TAB|VIS_NL;
176 		if (args->argv[i][strcspn(args->argv[i], quoted)] != '\0')
177 			flags |= VIS_DQ;
178 		utf8_stravis(&escaped, args->argv[i], flags);
179 		if (flags & VIS_DQ)
180 			args_print_add(&buf, &len, "\"%s\"", escaped);
181 		else
182 			args_print_add(&buf, &len, "%s", escaped);
183 		free(escaped);
184 	}
185 
186 	return (buf);
187 }
188 
189 /* Return if an argument is present. */
190 int
191 args_has(struct args *args, u_char ch)
192 {
193 	return (args_find(args, ch) != NULL);
194 }
195 
196 /* Set argument value in the arguments tree. */
197 void
198 args_set(struct args *args, u_char ch, const char *value)
199 {
200 	struct args_entry	*entry;
201 
202 	/* Replace existing argument. */
203 	if ((entry = args_find(args, ch)) != NULL) {
204 		free(entry->value);
205 		entry->value = NULL;
206 	} else {
207 		entry = xcalloc(1, sizeof *entry);
208 		entry->flag = ch;
209 		RB_INSERT(args_tree, &args->tree, entry);
210 	}
211 
212 	if (value != NULL)
213 		entry->value = xstrdup(value);
214 }
215 
216 /* Get argument value. Will be NULL if it isn't present. */
217 const char *
218 args_get(struct args *args, u_char ch)
219 {
220 	struct args_entry	*entry;
221 
222 	if ((entry = args_find(args, ch)) == NULL)
223 		return (NULL);
224 	return (entry->value);
225 }
226 
227 /* Convert an argument value to a number. */
228 long long
229 args_strtonum(struct args *args, u_char ch, long long minval, long long maxval,
230     char **cause)
231 {
232 	const char		*errstr;
233 	long long 	 	 ll;
234 	struct args_entry	*entry;
235 
236 	if ((entry = args_find(args, ch)) == NULL) {
237 		*cause = xstrdup("missing");
238 		return (0);
239 	}
240 
241 	ll = strtonum(entry->value, minval, maxval, &errstr);
242 	if (errstr != NULL) {
243 		*cause = xstrdup(errstr);
244 		return (0);
245 	}
246 
247 	*cause = NULL;
248 	return (ll);
249 }
250