Annotation of src/usr.bin/tmux/arguments.c, Revision 1.21
1.21 ! nicm 1: /* $OpenBSD: arguments.c,v 1.20 2017/08/23 09:14:21 nicm Exp $ */
1.1 nicm 2:
3: /*
1.13 nicm 4: * Copyright (c) 2010 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.6 okan 23: #include <unistd.h>
1.16 nicm 24: #include <vis.h>
1.1 nicm 25:
26: #include "tmux.h"
27:
1.5 nicm 28: /*
29: * Manipulate command arguments.
30: */
1.11 nicm 31:
1.21 ! nicm 32: struct args_value {
! 33: char *value;
! 34: TAILQ_ENTRY(args_value) entry;
! 35: };
! 36: TAILQ_HEAD(args_values, args_value);
! 37:
1.11 nicm 38: struct args_entry {
39: u_char flag;
1.21 ! nicm 40: struct args_values values;
1.11 nicm 41: RB_ENTRY(args_entry) entry;
42: };
1.5 nicm 43:
1.14 nicm 44: static struct args_entry *args_find(struct args *, u_char);
1.5 nicm 45:
1.14 nicm 46: static int args_cmp(struct args_entry *, struct args_entry *);
47: RB_GENERATE_STATIC(args_tree, args_entry, entry, args_cmp);
1.5 nicm 48:
49: /* Arguments tree comparison function. */
1.14 nicm 50: static int
1.5 nicm 51: args_cmp(struct args_entry *a1, struct args_entry *a2)
52: {
53: return (a1->flag - a2->flag);
54: }
55:
56: /* Find a flag in the arguments tree. */
1.14 nicm 57: static struct args_entry *
1.5 nicm 58: args_find(struct args *args, u_char ch)
59: {
60: struct args_entry entry;
61:
62: entry.flag = ch;
63: return (RB_FIND(args_tree, &args->tree, &entry));
64: }
65:
1.1 nicm 66: /* Parse an argv and argc into a new argument set. */
67: struct args *
68: args_parse(const char *template, int argc, char **argv)
69: {
70: struct args *args;
71: int opt;
72:
73: args = xcalloc(1, sizeof *args);
74:
75: optreset = 1;
76: optind = 1;
77:
78: while ((opt = getopt(argc, argv, template)) != -1) {
1.5 nicm 79: if (opt < 0)
1.1 nicm 80: continue;
1.8 nicm 81: if (opt == '?' || strchr(template, opt) == NULL) {
1.5 nicm 82: args_free(args);
1.1 nicm 83: return (NULL);
84: }
1.5 nicm 85: args_set(args, opt, optarg);
1.1 nicm 86: }
87: argc -= optind;
88: argv += optind;
89:
90: args->argc = argc;
91: args->argv = cmd_copy_argv(argc, argv);
92:
93: return (args);
94: }
95:
96: /* Free an arguments set. */
97: void
98: args_free(struct args *args)
99: {
1.5 nicm 100: struct args_entry *entry;
101: struct args_entry *entry1;
1.21 ! nicm 102: struct args_value *value;
! 103: struct args_value *value1;
1.1 nicm 104:
105: cmd_free_argv(args->argc, args->argv);
106:
1.5 nicm 107: RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
108: RB_REMOVE(args_tree, &args->tree, entry);
1.21 ! nicm 109: TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) {
! 110: TAILQ_REMOVE(&entry->values, value, entry);
! 111: free(value->value);
! 112: free(value);
! 113: }
1.5 nicm 114: free(entry);
115: }
1.1 nicm 116:
1.4 nicm 117: free(args);
1.1 nicm 118: }
119:
1.12 nicm 120: /* Add to string. */
121: static void printflike(3, 4)
122: args_print_add(char **buf, size_t *len, const char *fmt, ...)
123: {
124: va_list ap;
125: char *s;
126: size_t slen;
127:
128: va_start(ap, fmt);
129: slen = xvasprintf(&s, fmt, ap);
130: va_end(ap);
131:
132: *len += slen;
133: *buf = xrealloc(*buf, *len);
134:
135: strlcat(*buf, s, *len);
136: free(s);
137: }
138:
1.21 ! nicm 139: /* Add value to string. */
! 140: static void
! 141: args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
! 142: struct args_value *value)
! 143: {
! 144: static const char quoted[] = " #\"';$";
! 145: char *escaped;
! 146: int flags;
! 147:
! 148: if (**buf != '\0')
! 149: args_print_add(buf, len, " -%c ", entry->flag);
! 150: else
! 151: args_print_add(buf, len, "-%c ", entry->flag);
! 152:
! 153: flags = VIS_OCTAL|VIS_TAB|VIS_NL;
! 154: if (value->value[strcspn(value->value, quoted)] != '\0')
! 155: flags |= VIS_DQ;
! 156: utf8_stravis(&escaped, value->value, flags);
! 157: if (flags & VIS_DQ)
! 158: args_print_add(buf, len, "\"%s\"", escaped);
! 159: else
! 160: args_print_add(buf, len, "%s", escaped);
! 161: free(escaped);
! 162: }
! 163:
! 164: /* Add argument to string. */
! 165: static void
! 166: args_print_add_argument(char **buf, size_t *len, const char *argument)
! 167: {
! 168: static const char quoted[] = " #\"';$";
! 169: char *escaped;
! 170: int flags;
! 171:
! 172: if (**buf != '\0')
! 173: args_print_add(buf, len, " ");
! 174:
! 175: flags = VIS_OCTAL|VIS_TAB|VIS_NL;
! 176: if (argument[strcspn(argument, quoted)] != '\0')
! 177: flags |= VIS_DQ;
! 178: utf8_stravis(&escaped, argument, 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:
1.1 nicm 186: /* Print a set of arguments. */
1.12 nicm 187: char *
188: args_print(struct args *args)
1.1 nicm 189: {
1.12 nicm 190: size_t len;
1.21 ! nicm 191: char *buf;
! 192: int i;
1.5 nicm 193: struct args_entry *entry;
1.21 ! nicm 194: struct args_value *value;
1.1 nicm 195:
1.12 nicm 196: len = 1;
197: buf = xcalloc(1, len);
1.1 nicm 198:
199: /* Process the flags first. */
1.5 nicm 200: RB_FOREACH(entry, args_tree, &args->tree) {
1.21 ! nicm 201: if (!TAILQ_EMPTY(&entry->values))
1.1 nicm 202: continue;
203:
1.12 nicm 204: if (*buf == '\0')
205: args_print_add(&buf, &len, "-");
206: args_print_add(&buf, &len, "%c", entry->flag);
1.1 nicm 207: }
208:
209: /* Then the flags with arguments. */
1.5 nicm 210: RB_FOREACH(entry, args_tree, &args->tree) {
1.21 ! nicm 211: TAILQ_FOREACH(value, &entry->values, entry)
! 212: args_print_add_value(&buf, &len, entry, value);
1.1 nicm 213: }
214:
215: /* And finally the argument vector. */
1.21 ! nicm 216: for (i = 0; i < args->argc; i++)
! 217: args_print_add_argument(&buf, &len, args->argv[i]);
1.1 nicm 218:
1.12 nicm 219: return (buf);
1.1 nicm 220: }
221:
222: /* Return if an argument is present. */
223: int
224: args_has(struct args *args, u_char ch)
225: {
1.20 nicm 226: return (args_find(args, ch) != NULL);
1.1 nicm 227: }
228:
1.5 nicm 229: /* Set argument value in the arguments tree. */
1.19 nicm 230: void
1.21 ! nicm 231: args_set(struct args *args, u_char ch, const char *s)
1.1 nicm 232: {
1.5 nicm 233: struct args_entry *entry;
1.21 ! nicm 234: struct args_value *value;
1.5 nicm 235:
1.21 ! nicm 236: entry = args_find(args, ch);
! 237: if (entry == NULL) {
1.7 nicm 238: entry = xcalloc(1, sizeof *entry);
239: entry->flag = ch;
1.21 ! nicm 240: TAILQ_INIT(&entry->values);
1.7 nicm 241: RB_INSERT(args_tree, &args->tree, entry);
1.5 nicm 242: }
243:
1.21 ! nicm 244: if (s != NULL) {
! 245: value = xcalloc(1, sizeof *value);
! 246: value->value = xstrdup(s);
! 247: TAILQ_INSERT_TAIL(&entry->values, value, entry);
! 248: }
1.1 nicm 249: }
250:
251: /* Get argument value. Will be NULL if it isn't present. */
252: const char *
253: args_get(struct args *args, u_char ch)
254: {
1.5 nicm 255: struct args_entry *entry;
256:
257: if ((entry = args_find(args, ch)) == NULL)
258: return (NULL);
1.21 ! nicm 259: return (TAILQ_LAST(&entry->values, args_values)->value);
! 260: }
! 261:
! 262: /* Get first value in argument. */
! 263: const char *
! 264: args_first_value(struct args *args, u_char ch, struct args_value **value)
! 265: {
! 266: struct args_entry *entry;
! 267:
! 268: if ((entry = args_find(args, ch)) == NULL)
! 269: return (NULL);
! 270:
! 271: *value = TAILQ_FIRST(&entry->values);
! 272: if (*value == NULL)
! 273: return (NULL);
! 274: return ((*value)->value);
! 275: }
! 276:
! 277: /* Get next value in argument. */
! 278: const char *
! 279: args_next_value(struct args_value **value)
! 280: {
! 281: if (*value == NULL)
! 282: return (NULL);
! 283: *value = TAILQ_NEXT(*value, entry);
! 284: if (*value == NULL)
! 285: return (NULL);
! 286: return ((*value)->value);
1.1 nicm 287: }
288:
289: /* Convert an argument value to a number. */
290: long long
1.5 nicm 291: args_strtonum(struct args *args, u_char ch, long long minval, long long maxval,
292: char **cause)
1.1 nicm 293: {
1.5 nicm 294: const char *errstr;
295: long long ll;
296: struct args_entry *entry;
1.21 ! nicm 297: struct args_value *value;
1.1 nicm 298:
1.5 nicm 299: if ((entry = args_find(args, ch)) == NULL) {
1.1 nicm 300: *cause = xstrdup("missing");
301: return (0);
302: }
1.21 ! nicm 303: value = TAILQ_LAST(&entry->values, args_values);
1.1 nicm 304:
1.21 ! nicm 305: ll = strtonum(value->value, minval, maxval, &errstr);
1.1 nicm 306: if (errstr != NULL) {
307: *cause = xstrdup(errstr);
308: return (0);
309: }
310:
311: *cause = NULL;
312: return (ll);
313: }