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

Annotation of src/usr.bin/tmux/options.c, Revision 1.63

1.63    ! nicm        1: /* $OpenBSD: options.c,v 1.62 2021/03/11 06:31:05 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.18      nicm        4:  * Copyright (c) 2008 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:
1.26      nicm       21: #include <ctype.h>
1.57      nicm       22: #include <fnmatch.h>
1.1       nicm       23: #include <stdarg.h>
1.8       nicm       24: #include <stdlib.h>
1.1       nicm       25: #include <string.h>
                     26:
                     27: #include "tmux.h"
                     28:
                     29: /*
                     30:  * Option handling; each option has a name, type and value and is stored in
1.9       nicm       31:  * a red-black tree.
1.1       nicm       32:  */
                     33:
1.39      nicm       34: struct options_array_item {
                     35:        u_int                            index;
1.41      nicm       36:        union options_value              value;
1.39      nicm       37:        RB_ENTRY(options_array_item)     entry;
                     38: };
                     39: static int
                     40: options_array_cmp(struct options_array_item *a1, struct options_array_item *a2)
                     41: {
                     42:        if (a1->index < a2->index)
                     43:                return (-1);
                     44:        if (a1->index > a2->index)
                     45:                return (1);
                     46:        return (0);
                     47: }
                     48: RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp);
                     49:
1.27      nicm       50: struct options_entry {
1.41      nicm       51:        struct options                          *owner;
1.26      nicm       52:
1.41      nicm       53:        const char                              *name;
                     54:        const struct options_table_entry        *tableentry;
                     55:        union options_value                      value;
1.26      nicm       56:
1.56      nicm       57:        int                                      cached;
                     58:        struct style                             style;
                     59:
1.41      nicm       60:        RB_ENTRY(options_entry)                  entry;
1.26      nicm       61: };
                     62:
1.13      nicm       63: struct options {
1.27      nicm       64:        RB_HEAD(options_tree, options_entry)     tree;
1.26      nicm       65:        struct options                          *parent;
1.13      nicm       66: };
                     67:
1.27      nicm       68: static struct options_entry    *options_add(struct options *, const char *);
1.59      nicm       69: static void                     options_remove(struct options_entry *);
1.26      nicm       70:
                     71: #define OPTIONS_IS_STRING(o)                                           \
                     72:        ((o)->tableentry == NULL ||                                     \
                     73:            (o)->tableentry->type == OPTIONS_TABLE_STRING)
                     74: #define OPTIONS_IS_NUMBER(o) \
                     75:        ((o)->tableentry != NULL &&                                     \
                     76:            ((o)->tableentry->type == OPTIONS_TABLE_NUMBER ||           \
                     77:            (o)->tableentry->type == OPTIONS_TABLE_KEY ||               \
                     78:            (o)->tableentry->type == OPTIONS_TABLE_COLOUR ||            \
                     79:            (o)->tableentry->type == OPTIONS_TABLE_FLAG ||              \
                     80:            (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
1.43      nicm       81: #define OPTIONS_IS_COMMAND(o) \
                     82:        ((o)->tableentry != NULL &&                                     \
                     83:            (o)->tableentry->type == OPTIONS_TABLE_COMMAND)
1.41      nicm       84:
                     85: #define OPTIONS_IS_ARRAY(o)                                            \
1.26      nicm       86:        ((o)->tableentry != NULL &&                                     \
1.41      nicm       87:            ((o)->tableentry->flags & OPTIONS_TABLE_IS_ARRAY))
1.26      nicm       88:
1.27      nicm       89: static int     options_cmp(struct options_entry *, struct options_entry *);
                     90: RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
1.1       nicm       91:
1.17      nicm       92: static int
1.27      nicm       93: options_cmp(struct options_entry *lhs, struct options_entry *rhs)
1.26      nicm       94: {
                     95:        return (strcmp(lhs->name, rhs->name));
                     96: }
                     97:
1.60      nicm       98: static const char *
                     99: options_map_name(const char *name)
                    100: {
                    101:        const struct options_name_map   *map;
                    102:
                    103:        for (map = options_other_names; map->from != NULL; map++) {
                    104:                if (strcmp(map->from, name) == 0)
                    105:                        return (map->to);
                    106:        }
                    107:        return (name);
                    108: }
                    109:
1.26      nicm      110: static const struct options_table_entry *
                    111: options_parent_table_entry(struct options *oo, const char *s)
1.1       nicm      112: {
1.27      nicm      113:        struct options_entry    *o;
1.26      nicm      114:
                    115:        if (oo->parent == NULL)
                    116:                fatalx("no parent options for %s", s);
1.50      nicm      117:        o = options_get(oo->parent, s);
1.26      nicm      118:        if (o == NULL)
                    119:                fatalx("%s not in parent options", s);
                    120:        return (o->tableentry);
1.1       nicm      121: }
                    122:
1.41      nicm      123: static void
                    124: options_value_free(struct options_entry *o, union options_value *ov)
                    125: {
                    126:        if (OPTIONS_IS_STRING(o))
                    127:                free(ov->string);
1.43      nicm      128:        if (OPTIONS_IS_COMMAND(o) && ov->cmdlist != NULL)
                    129:                cmd_list_free(ov->cmdlist);
1.41      nicm      130: }
                    131:
1.42      nicm      132: static char *
1.57      nicm      133: options_value_to_string(struct options_entry *o, union options_value *ov,
1.41      nicm      134:     int numeric)
                    135: {
1.42      nicm      136:        char    *s;
1.41      nicm      137:
1.43      nicm      138:        if (OPTIONS_IS_COMMAND(o))
1.46      nicm      139:                return (cmd_list_print(ov->cmdlist, 0));
1.41      nicm      140:        if (OPTIONS_IS_NUMBER(o)) {
                    141:                switch (o->tableentry->type) {
                    142:                case OPTIONS_TABLE_NUMBER:
1.42      nicm      143:                        xasprintf(&s, "%lld", ov->number);
1.41      nicm      144:                        break;
                    145:                case OPTIONS_TABLE_KEY:
1.58      nicm      146:                        s = xstrdup(key_string_lookup_key(ov->number, 0));
1.41      nicm      147:                        break;
                    148:                case OPTIONS_TABLE_COLOUR:
1.42      nicm      149:                        s = xstrdup(colour_tostring(ov->number));
1.41      nicm      150:                        break;
                    151:                case OPTIONS_TABLE_FLAG:
                    152:                        if (numeric)
1.42      nicm      153:                                xasprintf(&s, "%lld", ov->number);
1.41      nicm      154:                        else
1.42      nicm      155:                                s = xstrdup(ov->number ? "on" : "off");
1.41      nicm      156:                        break;
                    157:                case OPTIONS_TABLE_CHOICE:
1.42      nicm      158:                        s = xstrdup(o->tableentry->choices[ov->number]);
1.41      nicm      159:                        break;
1.61      nicm      160:                default:
1.42      nicm      161:                        fatalx("not a number option type");
1.41      nicm      162:                }
                    163:                return (s);
                    164:        }
                    165:        if (OPTIONS_IS_STRING(o))
1.42      nicm      166:                return (xstrdup(ov->string));
                    167:        return (xstrdup(""));
1.41      nicm      168: }
                    169:
1.13      nicm      170: struct options *
                    171: options_create(struct options *parent)
1.1       nicm      172: {
1.16      nicm      173:        struct options  *oo;
1.13      nicm      174:
                    175:        oo = xcalloc(1, sizeof *oo);
1.7       nicm      176:        RB_INIT(&oo->tree);
1.1       nicm      177:        oo->parent = parent;
1.13      nicm      178:        return (oo);
1.1       nicm      179: }
                    180:
                    181: void
                    182: options_free(struct options *oo)
                    183: {
1.27      nicm      184:        struct options_entry    *o, *tmp;
1.1       nicm      185:
1.35      nicm      186:        RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
1.26      nicm      187:                options_remove(o);
1.13      nicm      188:        free(oo);
                    189: }
                    190:
1.57      nicm      191: struct options *
                    192: options_get_parent(struct options *oo)
                    193: {
                    194:        return (oo->parent);
                    195: }
                    196:
1.50      nicm      197: void
                    198: options_set_parent(struct options *oo, struct options *parent)
                    199: {
                    200:        oo->parent = parent;
                    201: }
                    202:
1.27      nicm      203: struct options_entry *
1.13      nicm      204: options_first(struct options *oo)
                    205: {
                    206:        return (RB_MIN(options_tree, &oo->tree));
                    207: }
                    208:
1.27      nicm      209: struct options_entry *
                    210: options_next(struct options_entry *o)
1.13      nicm      211: {
                    212:        return (RB_NEXT(options_tree, &oo->tree, o));
1.1       nicm      213: }
                    214:
1.27      nicm      215: struct options_entry *
1.26      nicm      216: options_get_only(struct options *oo, const char *name)
1.1       nicm      217: {
1.60      nicm      218:        struct options_entry    o = { .name = name }, *found;
1.1       nicm      219:
1.60      nicm      220:        found = RB_FIND(options_tree, &oo->tree, &o);
                    221:        if (found == NULL) {
                    222:                o.name = options_map_name(name);
                    223:                return (RB_FIND(options_tree, &oo->tree, &o));
                    224:        }
                    225:        return (found);
1.1       nicm      226: }
                    227:
1.27      nicm      228: struct options_entry *
1.26      nicm      229: options_get(struct options *oo, const char *name)
1.1       nicm      230: {
1.27      nicm      231:        struct options_entry    *o;
1.1       nicm      232:
1.26      nicm      233:        o = options_get_only(oo, name);
1.1       nicm      234:        while (o == NULL) {
                    235:                oo = oo->parent;
                    236:                if (oo == NULL)
                    237:                        break;
1.26      nicm      238:                o = options_get_only(oo, name);
                    239:        }
                    240:        return (o);
                    241: }
                    242:
1.27      nicm      243: struct options_entry *
1.26      nicm      244: options_empty(struct options *oo, const struct options_table_entry *oe)
                    245: {
1.27      nicm      246:        struct options_entry    *o;
1.26      nicm      247:
                    248:        o = options_add(oo, oe->name);
                    249:        o->tableentry = oe;
                    250:
1.42      nicm      251:        if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
1.41      nicm      252:                RB_INIT(&o->value.array);
1.39      nicm      253:
1.26      nicm      254:        return (o);
                    255: }
                    256:
1.27      nicm      257: struct options_entry *
1.26      nicm      258: options_default(struct options *oo, const struct options_table_entry *oe)
                    259: {
1.41      nicm      260:        struct options_entry    *o;
                    261:        union options_value     *ov;
                    262:        u_int                    i;
1.26      nicm      263:
                    264:        o = options_empty(oo, oe);
1.41      nicm      265:        ov = &o->value;
                    266:
                    267:        if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
1.43      nicm      268:                if (oe->default_arr == NULL) {
                    269:                        options_array_assign(o, oe->default_str, NULL);
                    270:                        return (o);
                    271:                }
                    272:                for (i = 0; oe->default_arr[i] != NULL; i++)
                    273:                        options_array_set(o, i, oe->default_arr[i], 0, NULL);
1.41      nicm      274:                return (o);
                    275:        }
                    276:
                    277:        switch (oe->type) {
                    278:        case OPTIONS_TABLE_STRING:
                    279:                ov->string = xstrdup(oe->default_str);
                    280:                break;
                    281:        default:
                    282:                ov->number = oe->default_num;
                    283:                break;
                    284:        }
1.26      nicm      285:        return (o);
                    286: }
                    287:
1.57      nicm      288: char *
                    289: options_default_to_string(const struct options_table_entry *oe)
                    290: {
                    291:        char    *s;
                    292:
                    293:        switch (oe->type) {
                    294:        case OPTIONS_TABLE_STRING:
                    295:        case OPTIONS_TABLE_COMMAND:
                    296:                s = xstrdup(oe->default_str);
                    297:                break;
                    298:        case OPTIONS_TABLE_NUMBER:
                    299:                xasprintf(&s, "%lld", oe->default_num);
                    300:                break;
                    301:        case OPTIONS_TABLE_KEY:
1.58      nicm      302:                s = xstrdup(key_string_lookup_key(oe->default_num, 0));
1.57      nicm      303:                break;
                    304:        case OPTIONS_TABLE_COLOUR:
                    305:                s = xstrdup(colour_tostring(oe->default_num));
                    306:                break;
                    307:        case OPTIONS_TABLE_FLAG:
                    308:                s = xstrdup(oe->default_num ? "on" : "off");
                    309:                break;
                    310:        case OPTIONS_TABLE_CHOICE:
                    311:                s = xstrdup(oe->choices[oe->default_num]);
                    312:                break;
1.61      nicm      313:        default:
                    314:                fatalx("unknown option type");
1.57      nicm      315:        }
                    316:        return (s);
                    317: }
                    318:
1.27      nicm      319: static struct options_entry *
1.26      nicm      320: options_add(struct options *oo, const char *name)
                    321: {
1.27      nicm      322:        struct options_entry    *o;
1.26      nicm      323:
                    324:        o = options_get_only(oo, name);
                    325:        if (o != NULL)
                    326:                options_remove(o);
                    327:
                    328:        o = xcalloc(1, sizeof *o);
                    329:        o->owner = oo;
                    330:        o->name = xstrdup(name);
                    331:
                    332:        RB_INSERT(options_tree, &oo->tree, o);
1.1       nicm      333:        return (o);
                    334: }
                    335:
1.59      nicm      336: static void
1.27      nicm      337: options_remove(struct options_entry *o)
1.26      nicm      338: {
                    339:        struct options  *oo = o->owner;
                    340:
1.41      nicm      341:        if (OPTIONS_IS_ARRAY(o))
1.39      nicm      342:                options_array_clear(o);
1.41      nicm      343:        else
                    344:                options_value_free(o, &o->value);
1.26      nicm      345:        RB_REMOVE(options_tree, &oo->tree, o);
1.53      nicm      346:        free((void *)o->name);
1.26      nicm      347:        free(o);
                    348: }
                    349:
                    350: const char *
1.27      nicm      351: options_name(struct options_entry *o)
1.26      nicm      352: {
                    353:        return (o->name);
                    354: }
                    355:
1.57      nicm      356: struct options *
                    357: options_owner(struct options_entry *o)
                    358: {
                    359:        return (o->owner);
                    360: }
                    361:
1.26      nicm      362: const struct options_table_entry *
1.27      nicm      363: options_table_entry(struct options_entry *o)
1.26      nicm      364: {
                    365:        return (o->tableentry);
                    366: }
                    367:
1.39      nicm      368: static struct options_array_item *
                    369: options_array_item(struct options_entry *o, u_int idx)
                    370: {
                    371:        struct options_array_item       a;
                    372:
                    373:        a.index = idx;
1.41      nicm      374:        return (RB_FIND(options_array, &o->value.array, &a));
1.39      nicm      375: }
                    376:
1.54      nicm      377: static struct options_array_item *
                    378: options_array_new(struct options_entry *o, u_int idx)
                    379: {
                    380:        struct options_array_item       *a;
                    381:
                    382:        a = xcalloc(1, sizeof *a);
                    383:        a->index = idx;
                    384:        RB_INSERT(options_array, &o->value.array, a);
                    385:        return (a);
                    386: }
                    387:
1.39      nicm      388: static void
                    389: options_array_free(struct options_entry *o, struct options_array_item *a)
                    390: {
1.41      nicm      391:        options_value_free(o, &a->value);
                    392:        RB_REMOVE(options_array, &o->value.array, a);
1.39      nicm      393:        free(a);
                    394: }
                    395:
1.31      nicm      396: void
                    397: options_array_clear(struct options_entry *o)
                    398: {
1.39      nicm      399:        struct options_array_item       *a, *a1;
                    400:
                    401:        if (!OPTIONS_IS_ARRAY(o))
                    402:                return;
                    403:
1.41      nicm      404:        RB_FOREACH_SAFE(a, options_array, &o->value.array, a1)
1.63    ! nicm      405:                options_array_free(o, a);
1.31      nicm      406: }
                    407:
1.41      nicm      408: union options_value *
1.27      nicm      409: options_array_get(struct options_entry *o, u_int idx)
1.26      nicm      410: {
1.39      nicm      411:        struct options_array_item       *a;
                    412:
1.26      nicm      413:        if (!OPTIONS_IS_ARRAY(o))
                    414:                return (NULL);
1.39      nicm      415:        a = options_array_item(o, idx);
                    416:        if (a == NULL)
1.26      nicm      417:                return (NULL);
1.41      nicm      418:        return (&a->value);
1.26      nicm      419: }
                    420:
                    421: int
1.31      nicm      422: options_array_set(struct options_entry *o, u_int idx, const char *value,
1.43      nicm      423:     int append, char **cause)
1.26      nicm      424: {
1.39      nicm      425:        struct options_array_item       *a;
                    426:        char                            *new;
1.45      nicm      427:        struct cmd_parse_result         *pr;
1.63    ! nicm      428:        long long                        number;
1.26      nicm      429:
1.43      nicm      430:        if (!OPTIONS_IS_ARRAY(o)) {
1.44      nicm      431:                if (cause != NULL)
                    432:                        *cause = xstrdup("not an array");
1.26      nicm      433:                return (-1);
1.43      nicm      434:        }
                    435:
1.54      nicm      436:        if (value == NULL) {
                    437:                a = options_array_item(o, idx);
                    438:                if (a != NULL)
                    439:                        options_array_free(o, a);
                    440:                return (0);
                    441:        }
                    442:
                    443:        if (OPTIONS_IS_COMMAND(o)) {
1.45      nicm      444:                pr = cmd_parse_from_string(value, NULL);
                    445:                switch (pr->status) {
                    446:                case CMD_PARSE_EMPTY:
1.47      nicm      447:                        if (cause != NULL)
                    448:                                *cause = xstrdup("empty command");
1.45      nicm      449:                        return (-1);
                    450:                case CMD_PARSE_ERROR:
1.44      nicm      451:                        if (cause != NULL)
1.45      nicm      452:                                *cause = pr->error;
1.44      nicm      453:                        else
1.45      nicm      454:                                free(pr->error);
1.43      nicm      455:                        return (-1);
1.45      nicm      456:                case CMD_PARSE_SUCCESS:
                    457:                        break;
1.44      nicm      458:                }
1.26      nicm      459:
1.54      nicm      460:                a = options_array_item(o, idx);
                    461:                if (a == NULL)
                    462:                        a = options_array_new(o, idx);
                    463:                else
                    464:                        options_value_free(o, &a->value);
                    465:                a->value.cmdlist = pr->cmdlist;
1.39      nicm      466:                return (0);
1.26      nicm      467:        }
1.31      nicm      468:
1.43      nicm      469:        if (OPTIONS_IS_STRING(o)) {
1.54      nicm      470:                a = options_array_item(o, idx);
1.43      nicm      471:                if (a != NULL && append)
                    472:                        xasprintf(&new, "%s%s", a->value.string, value);
                    473:                else
                    474:                        new = xstrdup(value);
1.54      nicm      475:                if (a == NULL)
                    476:                        a = options_array_new(o, idx);
                    477:                else
                    478:                        options_value_free(o, &a->value);
                    479:                a->value.string = new;
                    480:                return (0);
1.43      nicm      481:        }
                    482:
1.63    ! nicm      483:        if (o->tableentry->type == OPTIONS_TABLE_COLOUR) {
        !           484:                if ((number = colour_fromstring(value)) == -1) {
        !           485:                        xasprintf(cause, "bad colour: %s", value);
        !           486:                        return (-1);
        !           487:                }
        !           488:                a = options_array_item(o, idx);
        !           489:                if (a == NULL)
        !           490:                        a = options_array_new(o, idx);
        !           491:                else
        !           492:                        options_value_free(o, &a->value);
        !           493:                a->value.number = number;
        !           494:                return (0);
        !           495:        }
        !           496:
1.54      nicm      497:        if (cause != NULL)
                    498:                *cause = xstrdup("wrong array type");
                    499:        return (-1);
1.26      nicm      500: }
                    501:
1.43      nicm      502: int
                    503: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31      nicm      504: {
                    505:        const char      *separator;
                    506:        char            *copy, *next, *string;
                    507:        u_int            i;
                    508:
                    509:        separator = o->tableentry->separator;
                    510:        if (separator == NULL)
                    511:                separator = " ,";
1.43      nicm      512:        if (*separator == '\0') {
                    513:                if (*s == '\0')
                    514:                        return (0);
                    515:                for (i = 0; i < UINT_MAX; i++) {
                    516:                        if (options_array_item(o, i) == NULL)
                    517:                                break;
                    518:                }
                    519:                return (options_array_set(o, i, s, 0, cause));
                    520:        }
1.31      nicm      521:
1.43      nicm      522:        if (*s == '\0')
                    523:                return (0);
1.31      nicm      524:        copy = string = xstrdup(s);
                    525:        while ((next = strsep(&string, separator)) != NULL) {
                    526:                if (*next == '\0')
                    527:                        continue;
1.39      nicm      528:                for (i = 0; i < UINT_MAX; i++) {
                    529:                        if (options_array_item(o, i) == NULL)
1.31      nicm      530:                                break;
                    531:                }
1.39      nicm      532:                if (i == UINT_MAX)
1.31      nicm      533:                        break;
1.43      nicm      534:                if (options_array_set(o, i, next, 0, cause) != 0) {
                    535:                        free(copy);
                    536:                        return (-1);
                    537:                }
1.31      nicm      538:        }
                    539:        free(copy);
1.43      nicm      540:        return (0);
1.31      nicm      541: }
                    542:
1.39      nicm      543: struct options_array_item *
                    544: options_array_first(struct options_entry *o)
                    545: {
                    546:        if (!OPTIONS_IS_ARRAY(o))
                    547:                return (NULL);
1.41      nicm      548:        return (RB_MIN(options_array, &o->value.array));
1.39      nicm      549: }
                    550:
                    551: struct options_array_item *
                    552: options_array_next(struct options_array_item *a)
                    553: {
1.41      nicm      554:        return (RB_NEXT(options_array, &o->value.array, a));
1.39      nicm      555: }
                    556:
                    557: u_int
                    558: options_array_item_index(struct options_array_item *a)
                    559: {
                    560:        return (a->index);
                    561: }
                    562:
1.41      nicm      563: union options_value *
1.39      nicm      564: options_array_item_value(struct options_array_item *a)
                    565: {
1.41      nicm      566:        return (&a->value);
1.39      nicm      567: }
                    568:
                    569: int
1.57      nicm      570: options_is_array(struct options_entry *o)
1.39      nicm      571: {
                    572:        return (OPTIONS_IS_ARRAY(o));
                    573: }
                    574:
1.26      nicm      575: int
1.57      nicm      576: options_is_string(struct options_entry *o)
1.26      nicm      577: {
1.41      nicm      578:        return (OPTIONS_IS_STRING(o));
1.26      nicm      579: }
                    580:
1.42      nicm      581: char *
1.57      nicm      582: options_to_string(struct options_entry *o, int idx, int numeric)
1.26      nicm      583: {
1.39      nicm      584:        struct options_array_item       *a;
1.26      nicm      585:
                    586:        if (OPTIONS_IS_ARRAY(o)) {
                    587:                if (idx == -1)
1.42      nicm      588:                        return (xstrdup(""));
1.39      nicm      589:                a = options_array_item(o, idx);
                    590:                if (a == NULL)
1.42      nicm      591:                        return (xstrdup(""));
1.57      nicm      592:                return (options_value_to_string(o, &a->value, numeric));
1.26      nicm      593:        }
1.57      nicm      594:        return (options_value_to_string(o, &o->value, numeric));
1.26      nicm      595: }
                    596:
                    597: char *
                    598: options_parse(const char *name, int *idx)
1.1       nicm      599: {
1.26      nicm      600:        char    *copy, *cp, *end;
1.1       nicm      601:
1.26      nicm      602:        if (*name == '\0')
                    603:                return (NULL);
                    604:        copy = xstrdup(name);
                    605:        if ((cp = strchr(copy, '[')) == NULL) {
                    606:                *idx = -1;
                    607:                return (copy);
                    608:        }
                    609:        end = strchr(cp + 1, ']');
                    610:        if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
                    611:                free(copy);
                    612:                return (NULL);
                    613:        }
                    614:        if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
                    615:                free(copy);
                    616:                return (NULL);
                    617:        }
                    618:        *cp = '\0';
                    619:        return (copy);
1.1       nicm      620: }
                    621:
1.27      nicm      622: struct options_entry *
1.26      nicm      623: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1       nicm      624: {
1.27      nicm      625:        struct options_entry    *o;
                    626:        char                    *name;
1.1       nicm      627:
1.26      nicm      628:        name = options_parse(s, idx);
                    629:        if (name == NULL)
                    630:                return (NULL);
                    631:        if (only)
                    632:                o = options_get_only(oo, name);
                    633:        else
                    634:                o = options_get(oo, name);
                    635:        free(name);
                    636:        return (o);
                    637: }
1.1       nicm      638:
1.26      nicm      639: char *
1.50      nicm      640: options_match(const char *s, int *idx, int *ambiguous)
1.26      nicm      641: {
                    642:        const struct options_table_entry        *oe, *found;
1.60      nicm      643:        char                                    *parsed;
                    644:        const char                              *name;
1.26      nicm      645:        size_t                                   namelen;
                    646:
1.60      nicm      647:        parsed = options_parse(s, idx);
                    648:        if (parsed == NULL)
1.33      nicm      649:                return (NULL);
1.60      nicm      650:        if (*parsed == '@') {
1.29      nicm      651:                *ambiguous = 0;
1.60      nicm      652:                return (parsed);
1.29      nicm      653:        }
1.26      nicm      654:
1.60      nicm      655:        name = options_map_name(parsed);
                    656:        namelen = strlen(name);
                    657:
1.26      nicm      658:        found = NULL;
                    659:        for (oe = options_table; oe->name != NULL; oe++) {
                    660:                if (strcmp(oe->name, name) == 0) {
                    661:                        found = oe;
                    662:                        break;
                    663:                }
                    664:                if (strncmp(oe->name, name, namelen) == 0) {
                    665:                        if (found != NULL) {
                    666:                                *ambiguous = 1;
1.60      nicm      667:                                free(parsed);
1.26      nicm      668:                                return (NULL);
                    669:                        }
                    670:                        found = oe;
                    671:                }
                    672:        }
1.60      nicm      673:        free(parsed);
1.26      nicm      674:        if (found == NULL) {
                    675:                *ambiguous = 0;
                    676:                return (NULL);
1.22      nicm      677:        }
1.26      nicm      678:        return (xstrdup(found->name));
                    679: }
1.22      nicm      680:
1.27      nicm      681: struct options_entry *
1.26      nicm      682: options_match_get(struct options *oo, const char *s, int *idx, int only,
1.55      nicm      683:     int *ambiguous)
1.26      nicm      684: {
1.27      nicm      685:        char                    *name;
                    686:        struct options_entry    *o;
1.21      nicm      687:
1.26      nicm      688:        name = options_match(s, idx, ambiguous);
                    689:        if (name == NULL)
                    690:                return (NULL);
                    691:        *ambiguous = 0;
                    692:        if (only)
                    693:                o = options_get_only(oo, name);
                    694:        else
                    695:                o = options_get(oo, name);
                    696:        free(name);
1.4       nicm      697:        return (o);
1.1       nicm      698: }
1.26      nicm      699:
1.23      nicm      700: const char *
1.1       nicm      701: options_get_string(struct options *oo, const char *name)
                    702: {
1.27      nicm      703:        struct options_entry    *o;
1.26      nicm      704:
                    705:        o = options_get(oo, name);
                    706:        if (o == NULL)
                    707:                fatalx("missing option %s", name);
                    708:        if (!OPTIONS_IS_STRING(o))
                    709:                fatalx("option %s is not a string", name);
1.41      nicm      710:        return (o->value.string);
1.26      nicm      711: }
                    712:
                    713: long long
                    714: options_get_number(struct options *oo, const char *name)
                    715: {
1.27      nicm      716:        struct options_entry    *o;
1.1       nicm      717:
1.26      nicm      718:        o = options_get(oo, name);
                    719:        if (o == NULL)
1.15      nicm      720:                fatalx("missing option %s", name);
1.26      nicm      721:        if (!OPTIONS_IS_NUMBER(o))
1.61      nicm      722:                fatalx("option %s is not a number", name);
1.41      nicm      723:        return (o->value.number);
1.1       nicm      724: }
                    725:
1.27      nicm      726: struct options_entry *
1.26      nicm      727: options_set_string(struct options *oo, const char *name, int append,
                    728:     const char *fmt, ...)
1.1       nicm      729: {
1.27      nicm      730:        struct options_entry    *o;
                    731:        va_list                  ap;
1.56      nicm      732:        const char              *separator = "";
1.27      nicm      733:        char                    *s, *value;
1.1       nicm      734:
1.26      nicm      735:        va_start(ap, fmt);
                    736:        xvasprintf(&s, fmt, ap);
                    737:        va_end(ap);
                    738:
                    739:        o = options_get_only(oo, name);
                    740:        if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.56      nicm      741:                if (*name != '@') {
                    742:                        separator = o->tableentry->separator;
                    743:                        if (separator == NULL)
                    744:                                separator = "";
                    745:                }
                    746:                xasprintf(&value, "%s%s%s", o->value.string, separator, s);
1.26      nicm      747:                free(s);
                    748:        } else
                    749:                value = s;
                    750:        if (o == NULL && *name == '@')
                    751:                o = options_add(oo, name);
                    752:        else if (o == NULL) {
                    753:                o = options_default(oo, options_parent_table_entry(oo, name));
                    754:                if (o == NULL)
                    755:                        return (NULL);
                    756:        }
1.21      nicm      757:
1.26      nicm      758:        if (!OPTIONS_IS_STRING(o))
                    759:                fatalx("option %s is not a string", name);
1.41      nicm      760:        free(o->value.string);
                    761:        o->value.string = value;
1.56      nicm      762:        o->cached = 0;
1.4       nicm      763:        return (o);
1.1       nicm      764: }
                    765:
1.27      nicm      766: struct options_entry *
1.26      nicm      767: options_set_number(struct options *oo, const char *name, long long value)
1.1       nicm      768: {
1.27      nicm      769:        struct options_entry    *o;
1.26      nicm      770:
                    771:        if (*name == '@')
                    772:                fatalx("user option %s must be a string", name);
1.1       nicm      773:
1.26      nicm      774:        o = options_get_only(oo, name);
                    775:        if (o == NULL) {
                    776:                o = options_default(oo, options_parent_table_entry(oo, name));
                    777:                if (o == NULL)
                    778:                        return (NULL);
                    779:        }
                    780:
                    781:        if (!OPTIONS_IS_NUMBER(o))
                    782:                fatalx("option %s is not a number", name);
1.41      nicm      783:        o->value.number = value;
1.26      nicm      784:        return (o);
1.10      nicm      785: }
                    786:
1.50      nicm      787: int
1.49      nicm      788: options_scope_from_name(struct args *args, int window,
                    789:     const char *name, struct cmd_find_state *fs, struct options **oo,
                    790:     char **cause)
                    791: {
1.50      nicm      792:        struct session                          *s = fs->s;
                    793:        struct winlink                          *wl = fs->wl;
                    794:        struct window_pane                      *wp = fs->wp;
                    795:        const char                              *target = args_get(args, 't');
                    796:        const struct options_table_entry        *oe;
1.51      nicm      797:        int                                      scope = OPTIONS_TABLE_NONE;
1.49      nicm      798:
                    799:        if (*name == '@')
                    800:                return (options_scope_from_flags(args, window, fs, oo, cause));
                    801:
1.50      nicm      802:        for (oe = options_table; oe->name != NULL; oe++) {
                    803:                if (strcmp(oe->name, name) == 0)
                    804:                        break;
                    805:        }
                    806:        if (oe->name == NULL) {
1.49      nicm      807:                xasprintf(cause, "unknown option: %s", name);
                    808:                return (OPTIONS_TABLE_NONE);
                    809:        }
1.51      nicm      810:        switch (oe->scope) {
1.50      nicm      811:        case OPTIONS_TABLE_SERVER:
1.49      nicm      812:                *oo = global_options;
1.51      nicm      813:                scope = OPTIONS_TABLE_SERVER;
1.50      nicm      814:                break;
                    815:        case OPTIONS_TABLE_SESSION:
1.51      nicm      816:                if (args_has(args, 'g')) {
1.49      nicm      817:                        *oo = global_s_options;
1.51      nicm      818:                        scope = OPTIONS_TABLE_SESSION;
                    819:                } else if (s == NULL && target != NULL)
1.50      nicm      820:                        xasprintf(cause, "no such session: %s", target);
                    821:                else if (s == NULL)
                    822:                        xasprintf(cause, "no current session");
1.51      nicm      823:                else {
1.50      nicm      824:                        *oo = s->options;
1.51      nicm      825:                        scope = OPTIONS_TABLE_SESSION;
                    826:                }
1.50      nicm      827:                break;
                    828:        case OPTIONS_TABLE_WINDOW|OPTIONS_TABLE_PANE:
                    829:                if (args_has(args, 'p')) {
                    830:                        if (wp == NULL && target != NULL)
                    831:                                xasprintf(cause, "no such pane: %s", target);
                    832:                        else if (wp == NULL)
                    833:                                xasprintf(cause, "no current pane");
1.51      nicm      834:                        else {
1.50      nicm      835:                                *oo = wp->options;
1.51      nicm      836:                                scope = OPTIONS_TABLE_PANE;
                    837:                        }
1.50      nicm      838:                        break;
                    839:                }
                    840:                /* FALLTHROUGH */
                    841:        case OPTIONS_TABLE_WINDOW:
1.51      nicm      842:                if (args_has(args, 'g')) {
1.49      nicm      843:                        *oo = global_w_options;
1.51      nicm      844:                        scope = OPTIONS_TABLE_WINDOW;
                    845:                } else if (wl == NULL && target != NULL)
1.50      nicm      846:                        xasprintf(cause, "no such window: %s", target);
                    847:                else if (wl == NULL)
                    848:                        xasprintf(cause, "no current window");
1.51      nicm      849:                else {
1.49      nicm      850:                        *oo = wl->window->options;
1.51      nicm      851:                        scope = OPTIONS_TABLE_WINDOW;
                    852:                }
1.50      nicm      853:                break;
1.49      nicm      854:        }
                    855:        return (scope);
                    856: }
                    857:
1.50      nicm      858: int
1.26      nicm      859: options_scope_from_flags(struct args *args, int window,
                    860:     struct cmd_find_state *fs, struct options **oo, char **cause)
                    861: {
1.50      nicm      862:        struct session          *s = fs->s;
                    863:        struct winlink          *wl = fs->wl;
                    864:        struct window_pane      *wp = fs->wp;
                    865:        const char              *target = args_get(args, 't');
1.26      nicm      866:
                    867:        if (args_has(args, 's')) {
                    868:                *oo = global_options;
                    869:                return (OPTIONS_TABLE_SERVER);
                    870:        }
1.12      nicm      871:
1.50      nicm      872:        if (args_has(args, 'p')) {
                    873:                if (wp == NULL) {
                    874:                        if (target != NULL)
                    875:                                xasprintf(cause, "no such pane: %s", target);
                    876:                        else
                    877:                                xasprintf(cause, "no current pane");
                    878:                        return (OPTIONS_TABLE_NONE);
                    879:                }
                    880:                *oo = wp->options;
                    881:                return (OPTIONS_TABLE_PANE);
                    882:        } else if (window || args_has(args, 'w')) {
1.26      nicm      883:                if (args_has(args, 'g')) {
                    884:                        *oo = global_w_options;
                    885:                        return (OPTIONS_TABLE_WINDOW);
                    886:                }
                    887:                if (wl == NULL) {
                    888:                        if (target != NULL)
                    889:                                xasprintf(cause, "no such window: %s", target);
                    890:                        else
                    891:                                xasprintf(cause, "no current window");
                    892:                        return (OPTIONS_TABLE_NONE);
                    893:                }
                    894:                *oo = wl->window->options;
                    895:                return (OPTIONS_TABLE_WINDOW);
                    896:        } else {
                    897:                if (args_has(args, 'g')) {
                    898:                        *oo = global_s_options;
                    899:                        return (OPTIONS_TABLE_SESSION);
                    900:                }
                    901:                if (s == NULL) {
                    902:                        if (target != NULL)
                    903:                                xasprintf(cause, "no such session: %s", target);
                    904:                        else
                    905:                                xasprintf(cause, "no current session");
                    906:                        return (OPTIONS_TABLE_NONE);
                    907:                }
                    908:                *oo = s->options;
                    909:                return (OPTIONS_TABLE_SESSION);
                    910:        }
1.56      nicm      911: }
                    912:
                    913: struct style *
                    914: options_string_to_style(struct options *oo, const char *name,
                    915:     struct format_tree *ft)
                    916: {
                    917:        struct options_entry    *o;
                    918:        const char              *s;
                    919:        char                    *expanded;
                    920:
                    921:        o = options_get(oo, name);
                    922:        if (o == NULL || !OPTIONS_IS_STRING(o))
                    923:                return (NULL);
                    924:
                    925:        if (o->cached)
                    926:                return (&o->style);
                    927:        s = o->value.string;
                    928:        log_debug("%s: %s is '%s'", __func__, name, s);
                    929:
                    930:        style_set(&o->style, &grid_default_cell);
                    931:        o->cached = (strstr(s, "#{") == NULL);
                    932:
                    933:        if (ft != NULL && !o->cached) {
                    934:                expanded = format_expand(ft, s);
                    935:                if (style_parse(&o->style, &grid_default_cell, expanded) != 0) {
                    936:                        free(expanded);
                    937:                        return (NULL);
                    938:                }
                    939:                free(expanded);
                    940:        } else {
                    941:                if (style_parse(&o->style, &grid_default_cell, s) != 0)
                    942:                        return (NULL);
                    943:        }
                    944:        return (&o->style);
1.57      nicm      945: }
                    946:
                    947: static int
                    948: options_from_string_check(const struct options_table_entry *oe,
                    949:     const char *value, char **cause)
                    950: {
                    951:        struct style    sy;
                    952:
                    953:        if (oe == NULL)
                    954:                return (0);
                    955:        if (strcmp(oe->name, "default-shell") == 0 && !checkshell(value)) {
                    956:                xasprintf(cause, "not a suitable shell: %s", value);
                    957:                return (-1);
                    958:        }
                    959:        if (oe->pattern != NULL && fnmatch(oe->pattern, value, 0) != 0) {
                    960:                xasprintf(cause, "value is invalid: %s", value);
                    961:                return (-1);
                    962:        }
                    963:        if ((oe->flags & OPTIONS_TABLE_IS_STYLE) &&
                    964:            strstr(value, "#{") == NULL &&
                    965:            style_parse(&sy, &grid_default_cell, value) != 0) {
                    966:                xasprintf(cause, "invalid style: %s", value);
                    967:                return (-1);
                    968:        }
                    969:        return (0);
                    970: }
                    971:
                    972: static int
                    973: options_from_string_flag(struct options *oo, const char *name,
                    974:     const char *value, char **cause)
                    975: {
                    976:        int     flag;
                    977:
                    978:        if (value == NULL || *value == '\0')
                    979:                flag = !options_get_number(oo, name);
                    980:        else if (strcmp(value, "1") == 0 ||
                    981:            strcasecmp(value, "on") == 0 ||
                    982:            strcasecmp(value, "yes") == 0)
                    983:                flag = 1;
                    984:        else if (strcmp(value, "0") == 0 ||
                    985:            strcasecmp(value, "off") == 0 ||
                    986:            strcasecmp(value, "no") == 0)
                    987:                flag = 0;
                    988:        else {
                    989:                xasprintf(cause, "bad value: %s", value);
                    990:                return (-1);
                    991:        }
                    992:        options_set_number(oo, name, flag);
                    993:        return (0);
                    994: }
                    995:
                    996: static int
                    997: options_from_string_choice(const struct options_table_entry *oe,
                    998:     struct options *oo, const char *name, const char *value, char **cause)
                    999: {
                   1000:        const char      **cp;
                   1001:        int               n, choice = -1;
                   1002:
                   1003:        if (value == NULL) {
                   1004:                choice = options_get_number(oo, name);
                   1005:                if (choice < 2)
                   1006:                        choice = !choice;
                   1007:        } else {
                   1008:                n = 0;
                   1009:                for (cp = oe->choices; *cp != NULL; cp++) {
                   1010:                        if (strcmp(*cp, value) == 0)
                   1011:                                choice = n;
                   1012:                        n++;
                   1013:                }
                   1014:                if (choice == -1) {
                   1015:                        xasprintf(cause, "unknown value: %s", value);
                   1016:                        return (-1);
                   1017:                }
                   1018:        }
                   1019:        options_set_number(oo, name, choice);
                   1020:        return (0);
                   1021: }
                   1022:
                   1023: int
                   1024: options_from_string(struct options *oo, const struct options_table_entry *oe,
                   1025:     const char *name, const char *value, int append, char **cause)
                   1026: {
                   1027:        enum options_table_type  type;
                   1028:        long long                number;
                   1029:        const char              *errstr, *new;
                   1030:        char                    *old;
                   1031:        key_code                 key;
                   1032:
                   1033:        if (oe != NULL) {
                   1034:                if (value == NULL &&
                   1035:                    oe->type != OPTIONS_TABLE_FLAG &&
                   1036:                    oe->type != OPTIONS_TABLE_CHOICE) {
                   1037:                        xasprintf(cause, "empty value");
                   1038:                        return (-1);
                   1039:                }
                   1040:                type = oe->type;
                   1041:        } else {
                   1042:                if (*name != '@') {
                   1043:                        xasprintf(cause, "bad option name");
                   1044:                        return (-1);
                   1045:                }
                   1046:                type = OPTIONS_TABLE_STRING;
                   1047:        }
                   1048:
                   1049:        switch (type) {
                   1050:        case OPTIONS_TABLE_STRING:
                   1051:                old = xstrdup(options_get_string(oo, name));
                   1052:                options_set_string(oo, name, append, "%s", value);
                   1053:
                   1054:                new = options_get_string(oo, name);
                   1055:                if (options_from_string_check(oe, new, cause) != 0) {
                   1056:                        options_set_string(oo, name, 0, "%s", old);
                   1057:                        free(old);
                   1058:                        return (-1);
                   1059:                }
                   1060:                free(old);
                   1061:                return (0);
                   1062:        case OPTIONS_TABLE_NUMBER:
                   1063:                number = strtonum(value, oe->minimum, oe->maximum, &errstr);
                   1064:                if (errstr != NULL) {
                   1065:                        xasprintf(cause, "value is %s: %s", errstr, value);
                   1066:                        return (-1);
                   1067:                }
                   1068:                options_set_number(oo, name, number);
                   1069:                return (0);
                   1070:        case OPTIONS_TABLE_KEY:
                   1071:                key = key_string_lookup_string(value);
                   1072:                if (key == KEYC_UNKNOWN) {
                   1073:                        xasprintf(cause, "bad key: %s", value);
                   1074:                        return (-1);
                   1075:                }
                   1076:                options_set_number(oo, name, key);
                   1077:                return (0);
                   1078:        case OPTIONS_TABLE_COLOUR:
                   1079:                if ((number = colour_fromstring(value)) == -1) {
                   1080:                        xasprintf(cause, "bad colour: %s", value);
                   1081:                        return (-1);
                   1082:                }
                   1083:                options_set_number(oo, name, number);
                   1084:                return (0);
                   1085:        case OPTIONS_TABLE_FLAG:
                   1086:                return (options_from_string_flag(oo, name, value, cause));
                   1087:        case OPTIONS_TABLE_CHOICE:
                   1088:                return (options_from_string_choice(oe, oo, name, value, cause));
                   1089:        case OPTIONS_TABLE_COMMAND:
                   1090:                break;
                   1091:        }
                   1092:        return (-1);
                   1093: }
                   1094:
                   1095: void
                   1096: options_push_changes(const char *name)
                   1097: {
                   1098:        struct client           *loop;
                   1099:        struct session          *s;
                   1100:        struct window           *w;
                   1101:        struct window_pane      *wp;
                   1102:
                   1103:        if (strcmp(name, "automatic-rename") == 0) {
                   1104:                RB_FOREACH(w, windows, &windows) {
                   1105:                        if (w->active == NULL)
                   1106:                                continue;
                   1107:                        if (options_get_number(w->options, "automatic-rename"))
                   1108:                                w->active->flags |= PANE_CHANGED;
                   1109:                }
                   1110:        }
                   1111:        if (strcmp(name, "key-table") == 0) {
                   1112:                TAILQ_FOREACH(loop, &clients, entry)
                   1113:                        server_client_set_key_table(loop, NULL);
                   1114:        }
                   1115:        if (strcmp(name, "user-keys") == 0) {
                   1116:                TAILQ_FOREACH(loop, &clients, entry) {
                   1117:                        if (loop->tty.flags & TTY_OPENED)
                   1118:                                tty_keys_build(&loop->tty);
                   1119:                }
                   1120:        }
                   1121:        if (strcmp(name, "status") == 0 ||
                   1122:            strcmp(name, "status-interval") == 0)
                   1123:                status_timer_start_all();
                   1124:        if (strcmp(name, "monitor-silence") == 0)
                   1125:                alerts_reset_all();
                   1126:        if (strcmp(name, "window-style") == 0 ||
                   1127:            strcmp(name, "window-active-style") == 0) {
                   1128:                RB_FOREACH(wp, window_pane_tree, &all_window_panes)
                   1129:                        wp->flags |= PANE_STYLECHANGED;
1.63    ! nicm     1130:        }
        !          1131:        if (strcmp(name, "pane-colours") == 0) {
        !          1132:                RB_FOREACH(wp, window_pane_tree, &all_window_panes)
        !          1133:                        colour_palette_from_option(&wp->palette, wp->options);
1.57      nicm     1134:        }
                   1135:        if (strcmp(name, "pane-border-status") == 0) {
                   1136:                RB_FOREACH(w, windows, &windows)
1.62      nicm     1137:                        layout_fix_panes(w, NULL);
1.57      nicm     1138:        }
                   1139:        RB_FOREACH(s, sessions, &sessions)
                   1140:                status_update_cache(s);
                   1141:
                   1142:        recalculate_sizes();
                   1143:        TAILQ_FOREACH(loop, &clients, entry) {
                   1144:                if (loop->session != NULL)
                   1145:                        server_redraw_client(loop);
                   1146:        }
1.59      nicm     1147: }
                   1148:
                   1149: int
                   1150: options_remove_or_default(struct options_entry *o, int idx, char **cause)
                   1151: {
                   1152:        struct options  *oo = o->owner;
                   1153:
                   1154:        if (idx == -1) {
                   1155:                if (o->tableentry != NULL &&
                   1156:                    (oo == global_options ||
                   1157:                    oo == global_s_options ||
                   1158:                    oo == global_w_options))
                   1159:                        options_default(oo, o->tableentry);
                   1160:                else
                   1161:                        options_remove(o);
                   1162:        } else if (options_array_set(o, idx, NULL, 0, cause) != 0)
                   1163:                return (-1);
                   1164:        return (0);
1.1       nicm     1165: }