[BACK]Return to arguments.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/arguments.c, Revision 1.39

1.39    ! nicm        1: /* $OpenBSD: arguments.c,v 1.38 2021/08/20 19:50:16 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: TAILQ_HEAD(args_values, args_value);
                     33:
1.11      nicm       34: struct args_entry {
                     35:        u_char                   flag;
1.21      nicm       36:        struct args_values       values;
1.27      nicm       37:        u_int                    count;
1.11      nicm       38:        RB_ENTRY(args_entry)     entry;
                     39: };
1.5       nicm       40:
1.38      nicm       41: struct args {
                     42:        struct args_tree          tree;
                     43:        int                       argc;
                     44:        char                    **argv;
                     45: };
                     46:
1.14      nicm       47: static struct args_entry       *args_find(struct args *, u_char);
1.5       nicm       48:
1.14      nicm       49: static int     args_cmp(struct args_entry *, struct args_entry *);
                     50: RB_GENERATE_STATIC(args_tree, args_entry, entry, args_cmp);
1.5       nicm       51:
                     52: /* Arguments tree comparison function. */
1.14      nicm       53: static int
1.5       nicm       54: args_cmp(struct args_entry *a1, struct args_entry *a2)
                     55: {
                     56:        return (a1->flag - a2->flag);
                     57: }
                     58:
                     59: /* Find a flag in the arguments tree. */
1.14      nicm       60: static struct args_entry *
1.32      nicm       61: args_find(struct args *args, u_char flag)
1.5       nicm       62: {
                     63:        struct args_entry       entry;
                     64:
1.32      nicm       65:        entry.flag = flag;
1.5       nicm       66:        return (RB_FIND(args_tree, &args->tree, &entry));
                     67: }
                     68:
1.36      nicm       69: /* Create an empty arguments set. */
                     70: struct args *
                     71: args_create(void)
                     72: {
                     73:        struct args      *args;
                     74:
                     75:        args = xcalloc(1, sizeof *args);
                     76:        RB_INIT(&args->tree);
                     77:        return (args);
                     78: }
                     79:
1.1       nicm       80: /* Parse an argv and argc into a new argument set. */
                     81: struct args *
1.38      nicm       82: args_parse(const char *template, int argc, char **argv, int lower, int upper)
1.1       nicm       83: {
                     84:        struct args     *args;
                     85:        int              opt;
                     86:
                     87:        optreset = 1;
                     88:        optind = 1;
1.28      nicm       89:        optarg = NULL;
1.1       nicm       90:
1.36      nicm       91:        args = args_create();
1.1       nicm       92:        while ((opt = getopt(argc, argv, template)) != -1) {
1.5       nicm       93:                if (opt < 0)
1.1       nicm       94:                        continue;
1.8       nicm       95:                if (opt == '?' || strchr(template, opt) == NULL) {
1.5       nicm       96:                        args_free(args);
1.1       nicm       97:                        return (NULL);
                     98:                }
1.5       nicm       99:                args_set(args, opt, optarg);
1.28      nicm      100:                optarg = NULL;
1.1       nicm      101:        }
                    102:        argc -= optind;
                    103:        argv += optind;
                    104:
                    105:        args->argc = argc;
                    106:        args->argv = cmd_copy_argv(argc, argv);
                    107:
1.38      nicm      108:        if ((lower != -1 && argc < lower) || (upper != -1 && argc > upper)) {
                    109:                args_free(args);
                    110:                return (NULL);
                    111:        }
1.1       nicm      112:        return (args);
                    113: }
                    114:
                    115: /* Free an arguments set. */
                    116: void
                    117: args_free(struct args *args)
                    118: {
1.5       nicm      119:        struct args_entry       *entry;
                    120:        struct args_entry       *entry1;
1.21      nicm      121:        struct args_value       *value;
                    122:        struct args_value       *value1;
1.1       nicm      123:
                    124:        cmd_free_argv(args->argc, args->argv);
                    125:
1.5       nicm      126:        RB_FOREACH_SAFE(entry, args_tree, &args->tree, entry1) {
                    127:                RB_REMOVE(args_tree, &args->tree, entry);
1.21      nicm      128:                TAILQ_FOREACH_SAFE(value, &entry->values, entry, value1) {
                    129:                        TAILQ_REMOVE(&entry->values, value, entry);
                    130:                        free(value->value);
                    131:                        free(value);
                    132:                }
1.5       nicm      133:                free(entry);
                    134:        }
1.1       nicm      135:
1.4       nicm      136:        free(args);
1.1       nicm      137: }
                    138:
1.38      nicm      139: /* Convert arguments to vector. */
                    140: void
                    141: args_vector(struct args *args, int *argc, char ***argv)
                    142: {
                    143:        *argc = args->argc;
                    144:        *argv = cmd_copy_argv(args->argc, args->argv);
                    145: }
                    146:
1.12      nicm      147: /* Add to string. */
                    148: static void printflike(3, 4)
                    149: args_print_add(char **buf, size_t *len, const char *fmt, ...)
                    150: {
1.39    ! nicm      151:        va_list  ap;
1.12      nicm      152:        char    *s;
                    153:        size_t   slen;
                    154:
                    155:        va_start(ap, fmt);
                    156:        slen = xvasprintf(&s, fmt, ap);
                    157:        va_end(ap);
                    158:
                    159:        *len += slen;
                    160:        *buf = xrealloc(*buf, *len);
                    161:
                    162:        strlcat(*buf, s, *len);
                    163:        free(s);
                    164: }
                    165:
1.21      nicm      166: /* Add argument to string. */
                    167: static void
                    168: args_print_add_argument(char **buf, size_t *len, const char *argument)
                    169: {
1.22      nicm      170:        char    *escaped;
1.21      nicm      171:
                    172:        if (**buf != '\0')
                    173:                args_print_add(buf, len, " ");
                    174:
1.22      nicm      175:        escaped = args_escape(argument);
                    176:        args_print_add(buf, len, "%s", escaped);
1.21      nicm      177:        free(escaped);
                    178: }
                    179:
1.1       nicm      180: /* Print a set of arguments. */
1.12      nicm      181: char *
                    182: args_print(struct args *args)
1.1       nicm      183: {
1.39    ! nicm      184:        size_t                   len;
1.21      nicm      185:        char                    *buf;
                    186:        int                      i;
1.27      nicm      187:        u_int                    j;
1.5       nicm      188:        struct args_entry       *entry;
1.21      nicm      189:        struct args_value       *value;
1.1       nicm      190:
1.12      nicm      191:        len = 1;
                    192:        buf = xcalloc(1, len);
1.1       nicm      193:
                    194:        /* Process the flags first. */
1.5       nicm      195:        RB_FOREACH(entry, args_tree, &args->tree) {
1.21      nicm      196:                if (!TAILQ_EMPTY(&entry->values))
1.1       nicm      197:                        continue;
                    198:
1.12      nicm      199:                if (*buf == '\0')
                    200:                        args_print_add(&buf, &len, "-");
1.27      nicm      201:                for (j = 0; j < entry->count; j++)
                    202:                        args_print_add(&buf, &len, "%c", entry->flag);
1.1       nicm      203:        }
                    204:
                    205:        /* Then the flags with arguments. */
1.5       nicm      206:        RB_FOREACH(entry, args_tree, &args->tree) {
1.38      nicm      207:                TAILQ_FOREACH(value, &entry->values, entry) {
                    208:                        if (*buf != '\0')
                    209:                                args_print_add(&buf, &len, " -%c", entry->flag);
                    210:                        else
                    211:                                args_print_add(&buf, &len, "-%c", entry->flag);
                    212:                        args_print_add_argument(&buf, &len, value->value);
                    213:                }
1.1       nicm      214:        }
                    215:
                    216:        /* And finally the argument vector. */
1.21      nicm      217:        for (i = 0; i < args->argc; i++)
                    218:                args_print_add_argument(&buf, &len, args->argv[i]);
1.1       nicm      219:
1.12      nicm      220:        return (buf);
1.22      nicm      221: }
                    222:
                    223: /* Escape an argument. */
                    224: char *
                    225: args_escape(const char *s)
                    226: {
1.35      nicm      227:        static const char        dquoted[] = " #';${}";
                    228:        static const char        squoted[] = " \"";
1.22      nicm      229:        char                    *escaped, *result;
1.35      nicm      230:        int                      flags, quotes = 0;
1.22      nicm      231:
1.30      nicm      232:        if (*s == '\0') {
                    233:                xasprintf(&result, "''");
                    234:                return (result);
                    235:        }
1.35      nicm      236:        if (s[strcspn(s, dquoted)] != '\0')
                    237:                quotes = '"';
                    238:        else if (s[strcspn(s, squoted)] != '\0')
                    239:                quotes = '\'';
                    240:
1.26      nicm      241:        if (s[0] != ' ' &&
1.35      nicm      242:            s[1] == '\0' &&
                    243:            (quotes != 0 || s[0] == '~')) {
1.22      nicm      244:                xasprintf(&escaped, "\\%c", s[0]);
1.34      nicm      245:                return (escaped);
                    246:        }
                    247:
1.25      nicm      248:        flags = VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL;
1.35      nicm      249:        if (quotes == '"')
1.22      nicm      250:                flags |= VIS_DQ;
                    251:        utf8_stravis(&escaped, s, flags);
                    252:
1.35      nicm      253:        if (quotes == '\'')
                    254:                xasprintf(&result, "'%s'", escaped);
                    255:        else if (quotes == '"') {
1.22      nicm      256:                if (*escaped == '~')
                    257:                        xasprintf(&result, "\"\\%s\"", escaped);
                    258:                else
                    259:                        xasprintf(&result, "\"%s\"", escaped);
                    260:        } else {
                    261:                if (*escaped == '~')
                    262:                        xasprintf(&result, "\\%s", escaped);
                    263:                else
                    264:                        result = xstrdup(escaped);
                    265:        }
                    266:        free(escaped);
                    267:        return (result);
1.1       nicm      268: }
                    269:
                    270: /* Return if an argument is present. */
                    271: int
1.32      nicm      272: args_has(struct args *args, u_char flag)
1.1       nicm      273: {
1.27      nicm      274:        struct args_entry       *entry;
                    275:
1.32      nicm      276:        entry = args_find(args, flag);
1.27      nicm      277:        if (entry == NULL)
                    278:                return (0);
                    279:        return (entry->count);
1.1       nicm      280: }
                    281:
1.5       nicm      282: /* Set argument value in the arguments tree. */
1.19      nicm      283: void
1.32      nicm      284: args_set(struct args *args, u_char flag, const char *s)
1.1       nicm      285: {
1.5       nicm      286:        struct args_entry       *entry;
1.21      nicm      287:        struct args_value       *value;
1.5       nicm      288:
1.32      nicm      289:        entry = args_find(args, flag);
1.21      nicm      290:        if (entry == NULL) {
1.7       nicm      291:                entry = xcalloc(1, sizeof *entry);
1.32      nicm      292:                entry->flag = flag;
1.27      nicm      293:                entry->count = 1;
1.21      nicm      294:                TAILQ_INIT(&entry->values);
1.7       nicm      295:                RB_INSERT(args_tree, &args->tree, entry);
1.27      nicm      296:        } else
                    297:                entry->count++;
1.5       nicm      298:
1.21      nicm      299:        if (s != NULL) {
                    300:                value = xcalloc(1, sizeof *value);
                    301:                value->value = xstrdup(s);
                    302:                TAILQ_INSERT_TAIL(&entry->values, value, entry);
                    303:        }
1.1       nicm      304: }
                    305:
                    306: /* Get argument value. Will be NULL if it isn't present. */
                    307: const char *
1.32      nicm      308: args_get(struct args *args, u_char flag)
1.1       nicm      309: {
1.5       nicm      310:        struct args_entry       *entry;
                    311:
1.32      nicm      312:        if ((entry = args_find(args, flag)) == NULL)
                    313:                return (NULL);
                    314:        if (TAILQ_EMPTY(&entry->values))
1.5       nicm      315:                return (NULL);
1.21      nicm      316:        return (TAILQ_LAST(&entry->values, args_values)->value);
                    317: }
                    318:
1.32      nicm      319: /* Get first argument. */
                    320: u_char
                    321: args_first(struct args *args, struct args_entry **entry)
                    322: {
                    323:        *entry = RB_MIN(args_tree, &args->tree);
                    324:        if (*entry == NULL)
                    325:                return (0);
                    326:        return ((*entry)->flag);
                    327: }
                    328:
                    329: /* Get next argument. */
                    330: u_char
                    331: args_next(struct args_entry **entry)
                    332: {
                    333:        *entry = RB_NEXT(args_tree, &args->tree, *entry);
                    334:        if (*entry == NULL)
                    335:                return (0);
                    336:        return ((*entry)->flag);
1.38      nicm      337: }
                    338:
                    339: /* Get argument count. */
                    340: u_int
                    341: args_count(struct args *args)
                    342: {
                    343:        return (args->argc);
                    344: }
                    345:
                    346: /* Return argument as string. */
                    347: const char *
                    348: args_string(struct args *args, u_int idx)
                    349: {
                    350:        if (idx >= (u_int)args->argc)
                    351:                return (NULL);
                    352:        return (args->argv[idx]);
1.32      nicm      353: }
                    354:
1.21      nicm      355: /* Get first value in argument. */
1.37      nicm      356: struct args_value *
                    357: args_first_value(struct args *args, u_char flag)
1.21      nicm      358: {
                    359:        struct args_entry       *entry;
                    360:
1.32      nicm      361:        if ((entry = args_find(args, flag)) == NULL)
1.21      nicm      362:                return (NULL);
1.37      nicm      363:        return (TAILQ_FIRST(&entry->values));
1.21      nicm      364: }
                    365:
                    366: /* Get next value in argument. */
1.37      nicm      367: struct args_value *
                    368: args_next_value(struct args_value *value)
1.21      nicm      369: {
1.37      nicm      370:        return (TAILQ_NEXT(value, entry));
1.1       nicm      371: }
                    372:
                    373: /* Convert an argument value to a number. */
                    374: long long
1.32      nicm      375: args_strtonum(struct args *args, u_char flag, long long minval,
                    376:     long long maxval, char **cause)
1.1       nicm      377: {
1.5       nicm      378:        const char              *errstr;
1.39    ! nicm      379:        long long                ll;
1.5       nicm      380:        struct args_entry       *entry;
1.21      nicm      381:        struct args_value       *value;
1.1       nicm      382:
1.32      nicm      383:        if ((entry = args_find(args, flag)) == NULL) {
1.1       nicm      384:                *cause = xstrdup("missing");
                    385:                return (0);
                    386:        }
1.21      nicm      387:        value = TAILQ_LAST(&entry->values, args_values);
1.1       nicm      388:
1.21      nicm      389:        ll = strtonum(value->value, minval, maxval, &errstr);
1.1       nicm      390:        if (errstr != NULL) {
                    391:                *cause = xstrdup(errstr);
                    392:                return (0);
1.29      nicm      393:        }
                    394:
                    395:        *cause = NULL;
                    396:        return (ll);
                    397: }
                    398:
                    399: /* Convert an argument to a number which may be a percentage. */
                    400: long long
1.32      nicm      401: args_percentage(struct args *args, u_char flag, long long minval,
1.29      nicm      402:     long long maxval, long long curval, char **cause)
                    403: {
1.31      nicm      404:        const char              *value;
1.29      nicm      405:        struct args_entry       *entry;
                    406:
1.32      nicm      407:        if ((entry = args_find(args, flag)) == NULL) {
1.29      nicm      408:                *cause = xstrdup("missing");
                    409:                return (0);
                    410:        }
1.31      nicm      411:        value = TAILQ_LAST(&entry->values, args_values)->value;
                    412:        return (args_string_percentage(value, minval, maxval, curval, cause));
                    413: }
                    414:
                    415: /* Convert a string to a number which may be a percentage. */
                    416: long long
                    417: args_string_percentage(const char *value, long long minval, long long maxval,
                    418:     long long curval, char **cause)
                    419: {
                    420:        const char      *errstr;
1.39    ! nicm      421:        long long        ll;
1.31      nicm      422:        size_t           valuelen = strlen(value);
                    423:        char            *copy;
1.29      nicm      424:
1.31      nicm      425:        if (value[valuelen - 1] == '%') {
                    426:                copy = xstrdup(value);
1.29      nicm      427:                copy[valuelen - 1] = '\0';
                    428:
                    429:                ll = strtonum(copy, 0, 100, &errstr);
                    430:                free(copy);
                    431:                if (errstr != NULL) {
                    432:                        *cause = xstrdup(errstr);
                    433:                        return (0);
                    434:                }
                    435:                ll = (curval * ll) / 100;
                    436:                if (ll < minval) {
1.33      nicm      437:                        *cause = xstrdup("too small");
1.29      nicm      438:                        return (0);
                    439:                }
                    440:                if (ll > maxval) {
1.33      nicm      441:                        *cause = xstrdup("too large");
1.29      nicm      442:                        return (0);
                    443:                }
                    444:        } else {
1.31      nicm      445:                ll = strtonum(value, minval, maxval, &errstr);
1.29      nicm      446:                if (errstr != NULL) {
                    447:                        *cause = xstrdup(errstr);
                    448:                        return (0);
                    449:                }
1.1       nicm      450:        }
                    451:
                    452:        *cause = NULL;
                    453:        return (ll);
                    454: }