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_value { 32 char *value; 33 TAILQ_ENTRY(args_value) entry; 34 }; 35 TAILQ_HEAD(args_values, args_value); 36 37 struct args_entry { 38 u_char flag; 39 struct args_values values; 40 u_int count; 41 RB_ENTRY(args_entry) entry; 42 }; 43 44 static struct args_entry *args_find(struct args *, u_char); 45 46 static int args_cmp(struct args_entry *, struct args_entry *); 47 RB_GENERATE_STATIC(args_tree, args_entry, entry, args_cmp); 48 49 /* Arguments tree comparison function. */ 50 static int 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. */ 57 static struct args_entry * 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 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) { 79 if (opt < 0) 80 continue; 81 if (opt == '?' || strchr(template, opt) == NULL) { 82 args_free(args); 83 return (NULL); 84 } 85 args_set(args, opt, optarg); 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 { 100 struct args_entry *entry; 101 struct args_entry *entry1; 102 struct args_value *value; 103 struct args_value *value1; 104 105 cmd_free_argv(args->argc, args->argv); 106 107 RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) { 108 RB_REMOVE(args_tree, &args->tree, entry); 109 TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) { 110 TAILQ_REMOVE(&entry->values, value, entry); 111 free(value->value); 112 free(value); 113 } 114 free(entry); 115 } 116 117 free(args); 118 } 119 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 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 char *escaped; 145 146 if (**buf != '\0') 147 args_print_add(buf, len, " -%c ", entry->flag); 148 else 149 args_print_add(buf, len, "-%c ", entry->flag); 150 151 escaped = args_escape(value->value); 152 args_print_add(buf, len, "%s", escaped); 153 free(escaped); 154 } 155 156 /* Add argument to string. */ 157 static void 158 args_print_add_argument(char **buf, size_t *len, const char *argument) 159 { 160 char *escaped; 161 162 if (**buf != '\0') 163 args_print_add(buf, len, " "); 164 165 escaped = args_escape(argument); 166 args_print_add(buf, len, "%s", escaped); 167 free(escaped); 168 } 169 170 /* Print a set of arguments. */ 171 char * 172 args_print(struct args *args) 173 { 174 size_t len; 175 char *buf; 176 int i; 177 u_int j; 178 struct args_entry *entry; 179 struct args_value *value; 180 181 len = 1; 182 buf = xcalloc(1, len); 183 184 /* Process the flags first. */ 185 RB_FOREACH(entry, args_tree, &args->tree) { 186 if (!TAILQ_EMPTY(&entry->values)) 187 continue; 188 189 if (*buf == '\0') 190 args_print_add(&buf, &len, "-"); 191 for (j = 0; j < entry->count; j++) 192 args_print_add(&buf, &len, "%c", entry->flag); 193 } 194 195 /* Then the flags with arguments. */ 196 RB_FOREACH(entry, args_tree, &args->tree) { 197 TAILQ_FOREACH(value, &entry->values, entry) 198 args_print_add_value(&buf, &len, entry, value); 199 } 200 201 /* And finally the argument vector. */ 202 for (i = 0; i < args->argc; i++) 203 args_print_add_argument(&buf, &len, args->argv[i]); 204 205 return (buf); 206 } 207 208 /* Escape an argument. */ 209 char * 210 args_escape(const char *s) 211 { 212 static const char quoted[] = " #\"';${}"; 213 char *escaped, *result; 214 int flags; 215 216 if (*s == '\0') 217 return (xstrdup(s)); 218 if (s[0] != ' ' && 219 (strchr(quoted, s[0]) != NULL || s[0] == '~') && 220 s[1] == '\0') { 221 xasprintf(&escaped, "\\%c", s[0]); 222 return (escaped); 223 } 224 225 flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL; 226 if (s[strcspn(s, quoted)] != '\0') 227 flags |= VIS_DQ; 228 utf8_stravis(&escaped, s, flags); 229 230 if (flags & VIS_DQ) { 231 if (*escaped == '~') 232 xasprintf(&result, "\"\\%s\"", escaped); 233 else 234 xasprintf(&result, "\"%s\"", escaped); 235 } else { 236 if (*escaped == '~') 237 xasprintf(&result, "\\%s", escaped); 238 else 239 result = xstrdup(escaped); 240 } 241 free(escaped); 242 return (result); 243 } 244 245 /* Return if an argument is present. */ 246 int 247 args_has(struct args *args, u_char ch) 248 { 249 struct args_entry *entry; 250 251 entry = args_find(args, ch); 252 if (entry == NULL) 253 return (0); 254 return (entry->count); 255 } 256 257 /* Set argument value in the arguments tree. */ 258 void 259 args_set(struct args *args, u_char ch, const char *s) 260 { 261 struct args_entry *entry; 262 struct args_value *value; 263 264 entry = args_find(args, ch); 265 if (entry == NULL) { 266 entry = xcalloc(1, sizeof *entry); 267 entry->flag = ch; 268 entry->count = 1; 269 TAILQ_INIT(&entry->values); 270 RB_INSERT(args_tree, &args->tree, entry); 271 } else 272 entry->count++; 273 274 if (s != NULL) { 275 value = xcalloc(1, sizeof *value); 276 value->value = xstrdup(s); 277 TAILQ_INSERT_TAIL(&entry->values, value, entry); 278 } 279 } 280 281 /* Get argument value. Will be NULL if it isn't present. */ 282 const char * 283 args_get(struct args *args, u_char ch) 284 { 285 struct args_entry *entry; 286 287 if ((entry = args_find(args, ch)) == NULL) 288 return (NULL); 289 return (TAILQ_LAST(&entry->values, args_values)->value); 290 } 291 292 /* Get first value in argument. */ 293 const char * 294 args_first_value(struct args *args, u_char ch, struct args_value **value) 295 { 296 struct args_entry *entry; 297 298 if ((entry = args_find(args, ch)) == NULL) 299 return (NULL); 300 301 *value = TAILQ_FIRST(&entry->values); 302 if (*value == NULL) 303 return (NULL); 304 return ((*value)->value); 305 } 306 307 /* Get next value in argument. */ 308 const char * 309 args_next_value(struct args_value **value) 310 { 311 if (*value == NULL) 312 return (NULL); 313 *value = TAILQ_NEXT(*value, entry); 314 if (*value == NULL) 315 return (NULL); 316 return ((*value)->value); 317 } 318 319 /* Convert an argument value to a number. */ 320 long long 321 args_strtonum(struct args *args, u_char ch, long long minval, long long maxval, 322 char **cause) 323 { 324 const char *errstr; 325 long long ll; 326 struct args_entry *entry; 327 struct args_value *value; 328 329 if ((entry = args_find(args, ch)) == NULL) { 330 *cause = xstrdup("missing"); 331 return (0); 332 } 333 value = TAILQ_LAST(&entry->values, args_values); 334 335 ll = strtonum(value->value, minval, maxval, &errstr); 336 if (errstr != NULL) { 337 *cause = xstrdup(errstr); 338 return (0); 339 } 340 341 *cause = NULL; 342 return (ll); 343 } 344