[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.62

1.62    ! nicm        1: /* $OpenBSD: options.c,v 1.61 2021/01/18 11:14:23 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.39      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.26      nicm      428:
1.43      nicm      429:        if (!OPTIONS_IS_ARRAY(o)) {
1.44      nicm      430:                if (cause != NULL)
                    431:                        *cause = xstrdup("not an array");
1.26      nicm      432:                return (-1);
1.43      nicm      433:        }
                    434:
1.54      nicm      435:        if (value == NULL) {
                    436:                a = options_array_item(o, idx);
                    437:                if (a != NULL)
                    438:                        options_array_free(o, a);
                    439:                return (0);
                    440:        }
                    441:
                    442:        if (OPTIONS_IS_COMMAND(o)) {
1.45      nicm      443:                pr = cmd_parse_from_string(value, NULL);
                    444:                switch (pr->status) {
                    445:                case CMD_PARSE_EMPTY:
1.47      nicm      446:                        if (cause != NULL)
                    447:                                *cause = xstrdup("empty command");
1.45      nicm      448:                        return (-1);
                    449:                case CMD_PARSE_ERROR:
1.44      nicm      450:                        if (cause != NULL)
1.45      nicm      451:                                *cause = pr->error;
1.44      nicm      452:                        else
1.45      nicm      453:                                free(pr->error);
1.43      nicm      454:                        return (-1);
1.45      nicm      455:                case CMD_PARSE_SUCCESS:
                    456:                        break;
1.44      nicm      457:                }
1.26      nicm      458:
1.54      nicm      459:                a = options_array_item(o, idx);
                    460:                if (a == NULL)
                    461:                        a = options_array_new(o, idx);
                    462:                else
                    463:                        options_value_free(o, &a->value);
                    464:                a->value.cmdlist = pr->cmdlist;
1.39      nicm      465:                return (0);
1.26      nicm      466:        }
1.31      nicm      467:
1.43      nicm      468:        if (OPTIONS_IS_STRING(o)) {
1.54      nicm      469:                a = options_array_item(o, idx);
1.43      nicm      470:                if (a != NULL && append)
                    471:                        xasprintf(&new, "%s%s", a->value.string, value);
                    472:                else
                    473:                        new = xstrdup(value);
1.54      nicm      474:                if (a == NULL)
                    475:                        a = options_array_new(o, idx);
                    476:                else
                    477:                        options_value_free(o, &a->value);
                    478:                a->value.string = new;
                    479:                return (0);
1.43      nicm      480:        }
                    481:
1.54      nicm      482:        if (cause != NULL)
                    483:                *cause = xstrdup("wrong array type");
                    484:        return (-1);
1.26      nicm      485: }
                    486:
1.43      nicm      487: int
                    488: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31      nicm      489: {
                    490:        const char      *separator;
                    491:        char            *copy, *next, *string;
                    492:        u_int            i;
                    493:
                    494:        separator = o->tableentry->separator;
                    495:        if (separator == NULL)
                    496:                separator = " ,";
1.43      nicm      497:        if (*separator == '\0') {
                    498:                if (*s == '\0')
                    499:                        return (0);
                    500:                for (i = 0; i < UINT_MAX; i++) {
                    501:                        if (options_array_item(o, i) == NULL)
                    502:                                break;
                    503:                }
                    504:                return (options_array_set(o, i, s, 0, cause));
                    505:        }
1.31      nicm      506:
1.43      nicm      507:        if (*s == '\0')
                    508:                return (0);
1.31      nicm      509:        copy = string = xstrdup(s);
                    510:        while ((next = strsep(&string, separator)) != NULL) {
                    511:                if (*next == '\0')
                    512:                        continue;
1.39      nicm      513:                for (i = 0; i < UINT_MAX; i++) {
                    514:                        if (options_array_item(o, i) == NULL)
1.31      nicm      515:                                break;
                    516:                }
1.39      nicm      517:                if (i == UINT_MAX)
1.31      nicm      518:                        break;
1.43      nicm      519:                if (options_array_set(o, i, next, 0, cause) != 0) {
                    520:                        free(copy);
                    521:                        return (-1);
                    522:                }
1.31      nicm      523:        }
                    524:        free(copy);
1.43      nicm      525:        return (0);
1.31      nicm      526: }
                    527:
1.39      nicm      528: struct options_array_item *
                    529: options_array_first(struct options_entry *o)
                    530: {
                    531:        if (!OPTIONS_IS_ARRAY(o))
                    532:                return (NULL);
1.41      nicm      533:        return (RB_MIN(options_array, &o->value.array));
1.39      nicm      534: }
                    535:
                    536: struct options_array_item *
                    537: options_array_next(struct options_array_item *a)
                    538: {
1.41      nicm      539:        return (RB_NEXT(options_array, &o->value.array, a));
1.39      nicm      540: }
                    541:
                    542: u_int
                    543: options_array_item_index(struct options_array_item *a)
                    544: {
                    545:        return (a->index);
                    546: }
                    547:
1.41      nicm      548: union options_value *
1.39      nicm      549: options_array_item_value(struct options_array_item *a)
                    550: {
1.41      nicm      551:        return (&a->value);
1.39      nicm      552: }
                    553:
                    554: int
1.57      nicm      555: options_is_array(struct options_entry *o)
1.39      nicm      556: {
                    557:        return (OPTIONS_IS_ARRAY(o));
                    558: }
                    559:
1.26      nicm      560: int
1.57      nicm      561: options_is_string(struct options_entry *o)
1.26      nicm      562: {
1.41      nicm      563:        return (OPTIONS_IS_STRING(o));
1.26      nicm      564: }
                    565:
1.42      nicm      566: char *
1.57      nicm      567: options_to_string(struct options_entry *o, int idx, int numeric)
1.26      nicm      568: {
1.39      nicm      569:        struct options_array_item       *a;
1.26      nicm      570:
                    571:        if (OPTIONS_IS_ARRAY(o)) {
                    572:                if (idx == -1)
1.42      nicm      573:                        return (xstrdup(""));
1.39      nicm      574:                a = options_array_item(o, idx);
                    575:                if (a == NULL)
1.42      nicm      576:                        return (xstrdup(""));
1.57      nicm      577:                return (options_value_to_string(o, &a->value, numeric));
1.26      nicm      578:        }
1.57      nicm      579:        return (options_value_to_string(o, &o->value, numeric));
1.26      nicm      580: }
                    581:
                    582: char *
                    583: options_parse(const char *name, int *idx)
1.1       nicm      584: {
1.26      nicm      585:        char    *copy, *cp, *end;
1.1       nicm      586:
1.26      nicm      587:        if (*name == '\0')
                    588:                return (NULL);
                    589:        copy = xstrdup(name);
                    590:        if ((cp = strchr(copy, '[')) == NULL) {
                    591:                *idx = -1;
                    592:                return (copy);
                    593:        }
                    594:        end = strchr(cp + 1, ']');
                    595:        if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
                    596:                free(copy);
                    597:                return (NULL);
                    598:        }
                    599:        if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
                    600:                free(copy);
                    601:                return (NULL);
                    602:        }
                    603:        *cp = '\0';
                    604:        return (copy);
1.1       nicm      605: }
                    606:
1.27      nicm      607: struct options_entry *
1.26      nicm      608: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1       nicm      609: {
1.27      nicm      610:        struct options_entry    *o;
                    611:        char                    *name;
1.1       nicm      612:
1.26      nicm      613:        name = options_parse(s, idx);
                    614:        if (name == NULL)
                    615:                return (NULL);
                    616:        if (only)
                    617:                o = options_get_only(oo, name);
                    618:        else
                    619:                o = options_get(oo, name);
                    620:        free(name);
                    621:        return (o);
                    622: }
1.1       nicm      623:
1.26      nicm      624: char *
1.50      nicm      625: options_match(const char *s, int *idx, int *ambiguous)
1.26      nicm      626: {
                    627:        const struct options_table_entry        *oe, *found;
1.60      nicm      628:        char                                    *parsed;
                    629:        const char                              *name;
1.26      nicm      630:        size_t                                   namelen;
                    631:
1.60      nicm      632:        parsed = options_parse(s, idx);
                    633:        if (parsed == NULL)
1.33      nicm      634:                return (NULL);
1.60      nicm      635:        if (*parsed == '@') {
1.29      nicm      636:                *ambiguous = 0;
1.60      nicm      637:                return (parsed);
1.29      nicm      638:        }
1.26      nicm      639:
1.60      nicm      640:        name = options_map_name(parsed);
                    641:        namelen = strlen(name);
                    642:
1.26      nicm      643:        found = NULL;
                    644:        for (oe = options_table; oe->name != NULL; oe++) {
                    645:                if (strcmp(oe->name, name) == 0) {
                    646:                        found = oe;
                    647:                        break;
                    648:                }
                    649:                if (strncmp(oe->name, name, namelen) == 0) {
                    650:                        if (found != NULL) {
                    651:                                *ambiguous = 1;
1.60      nicm      652:                                free(parsed);
1.26      nicm      653:                                return (NULL);
                    654:                        }
                    655:                        found = oe;
                    656:                }
                    657:        }
1.60      nicm      658:        free(parsed);
1.26      nicm      659:        if (found == NULL) {
                    660:                *ambiguous = 0;
                    661:                return (NULL);
1.22      nicm      662:        }
1.26      nicm      663:        return (xstrdup(found->name));
                    664: }
1.22      nicm      665:
1.27      nicm      666: struct options_entry *
1.26      nicm      667: options_match_get(struct options *oo, const char *s, int *idx, int only,
1.55      nicm      668:     int *ambiguous)
1.26      nicm      669: {
1.27      nicm      670:        char                    *name;
                    671:        struct options_entry    *o;
1.21      nicm      672:
1.26      nicm      673:        name = options_match(s, idx, ambiguous);
                    674:        if (name == NULL)
                    675:                return (NULL);
                    676:        *ambiguous = 0;
                    677:        if (only)
                    678:                o = options_get_only(oo, name);
                    679:        else
                    680:                o = options_get(oo, name);
                    681:        free(name);
1.4       nicm      682:        return (o);
1.1       nicm      683: }
1.26      nicm      684:
1.23      nicm      685: const char *
1.1       nicm      686: options_get_string(struct options *oo, const char *name)
                    687: {
1.27      nicm      688:        struct options_entry    *o;
1.26      nicm      689:
                    690:        o = options_get(oo, name);
                    691:        if (o == NULL)
                    692:                fatalx("missing option %s", name);
                    693:        if (!OPTIONS_IS_STRING(o))
                    694:                fatalx("option %s is not a string", name);
1.41      nicm      695:        return (o->value.string);
1.26      nicm      696: }
                    697:
                    698: long long
                    699: options_get_number(struct options *oo, const char *name)
                    700: {
1.27      nicm      701:        struct options_entry    *o;
1.1       nicm      702:
1.26      nicm      703:        o = options_get(oo, name);
                    704:        if (o == NULL)
1.15      nicm      705:                fatalx("missing option %s", name);
1.26      nicm      706:        if (!OPTIONS_IS_NUMBER(o))
1.61      nicm      707:                fatalx("option %s is not a number", name);
1.41      nicm      708:        return (o->value.number);
1.1       nicm      709: }
                    710:
1.27      nicm      711: struct options_entry *
1.26      nicm      712: options_set_string(struct options *oo, const char *name, int append,
                    713:     const char *fmt, ...)
1.1       nicm      714: {
1.27      nicm      715:        struct options_entry    *o;
                    716:        va_list                  ap;
1.56      nicm      717:        const char              *separator = "";
1.27      nicm      718:        char                    *s, *value;
1.1       nicm      719:
1.26      nicm      720:        va_start(ap, fmt);
                    721:        xvasprintf(&s, fmt, ap);
                    722:        va_end(ap);
                    723:
                    724:        o = options_get_only(oo, name);
                    725:        if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.56      nicm      726:                if (*name != '@') {
                    727:                        separator = o->tableentry->separator;
                    728:                        if (separator == NULL)
                    729:                                separator = "";
                    730:                }
                    731:                xasprintf(&value, "%s%s%s", o->value.string, separator, s);
1.26      nicm      732:                free(s);
                    733:        } else
                    734:                value = s;
                    735:        if (o == NULL && *name == '@')
                    736:                o = options_add(oo, name);
                    737:        else if (o == NULL) {
                    738:                o = options_default(oo, options_parent_table_entry(oo, name));
                    739:                if (o == NULL)
                    740:                        return (NULL);
                    741:        }
1.21      nicm      742:
1.26      nicm      743:        if (!OPTIONS_IS_STRING(o))
                    744:                fatalx("option %s is not a string", name);
1.41      nicm      745:        free(o->value.string);
                    746:        o->value.string = value;
1.56      nicm      747:        o->cached = 0;
1.4       nicm      748:        return (o);
1.1       nicm      749: }
                    750:
1.27      nicm      751: struct options_entry *
1.26      nicm      752: options_set_number(struct options *oo, const char *name, long long value)
1.1       nicm      753: {
1.27      nicm      754:        struct options_entry    *o;
1.26      nicm      755:
                    756:        if (*name == '@')
                    757:                fatalx("user option %s must be a string", name);
1.1       nicm      758:
1.26      nicm      759:        o = options_get_only(oo, name);
                    760:        if (o == NULL) {
                    761:                o = options_default(oo, options_parent_table_entry(oo, name));
                    762:                if (o == NULL)
                    763:                        return (NULL);
                    764:        }
                    765:
                    766:        if (!OPTIONS_IS_NUMBER(o))
                    767:                fatalx("option %s is not a number", name);
1.41      nicm      768:        o->value.number = value;
1.26      nicm      769:        return (o);
1.10      nicm      770: }
                    771:
1.50      nicm      772: int
1.49      nicm      773: options_scope_from_name(struct args *args, int window,
                    774:     const char *name, struct cmd_find_state *fs, struct options **oo,
                    775:     char **cause)
                    776: {
1.50      nicm      777:        struct session                          *s = fs->s;
                    778:        struct winlink                          *wl = fs->wl;
                    779:        struct window_pane                      *wp = fs->wp;
                    780:        const char                              *target = args_get(args, 't');
                    781:        const struct options_table_entry        *oe;
1.51      nicm      782:        int                                      scope = OPTIONS_TABLE_NONE;
1.49      nicm      783:
                    784:        if (*name == '@')
                    785:                return (options_scope_from_flags(args, window, fs, oo, cause));
                    786:
1.50      nicm      787:        for (oe = options_table; oe->name != NULL; oe++) {
                    788:                if (strcmp(oe->name, name) == 0)
                    789:                        break;
                    790:        }
                    791:        if (oe->name == NULL) {
1.49      nicm      792:                xasprintf(cause, "unknown option: %s", name);
                    793:                return (OPTIONS_TABLE_NONE);
                    794:        }
1.51      nicm      795:        switch (oe->scope) {
1.50      nicm      796:        case OPTIONS_TABLE_SERVER:
1.49      nicm      797:                *oo = global_options;
1.51      nicm      798:                scope = OPTIONS_TABLE_SERVER;
1.50      nicm      799:                break;
                    800:        case OPTIONS_TABLE_SESSION:
1.51      nicm      801:                if (args_has(args, 'g')) {
1.49      nicm      802:                        *oo = global_s_options;
1.51      nicm      803:                        scope = OPTIONS_TABLE_SESSION;
                    804:                } else if (s == NULL && target != NULL)
1.50      nicm      805:                        xasprintf(cause, "no such session: %s", target);
                    806:                else if (s == NULL)
                    807:                        xasprintf(cause, "no current session");
1.51      nicm      808:                else {
1.50      nicm      809:                        *oo = s->options;
1.51      nicm      810:                        scope = OPTIONS_TABLE_SESSION;
                    811:                }
1.50      nicm      812:                break;
                    813:        case OPTIONS_TABLE_WINDOW|OPTIONS_TABLE_PANE:
                    814:                if (args_has(args, 'p')) {
                    815:                        if (wp == NULL && target != NULL)
                    816:                                xasprintf(cause, "no such pane: %s", target);
                    817:                        else if (wp == NULL)
                    818:                                xasprintf(cause, "no current pane");
1.51      nicm      819:                        else {
1.50      nicm      820:                                *oo = wp->options;
1.51      nicm      821:                                scope = OPTIONS_TABLE_PANE;
                    822:                        }
1.50      nicm      823:                        break;
                    824:                }
                    825:                /* FALLTHROUGH */
                    826:        case OPTIONS_TABLE_WINDOW:
1.51      nicm      827:                if (args_has(args, 'g')) {
1.49      nicm      828:                        *oo = global_w_options;
1.51      nicm      829:                        scope = OPTIONS_TABLE_WINDOW;
                    830:                } else if (wl == NULL && target != NULL)
1.50      nicm      831:                        xasprintf(cause, "no such window: %s", target);
                    832:                else if (wl == NULL)
                    833:                        xasprintf(cause, "no current window");
1.51      nicm      834:                else {
1.49      nicm      835:                        *oo = wl->window->options;
1.51      nicm      836:                        scope = OPTIONS_TABLE_WINDOW;
                    837:                }
1.50      nicm      838:                break;
1.49      nicm      839:        }
                    840:        return (scope);
                    841: }
                    842:
1.50      nicm      843: int
1.26      nicm      844: options_scope_from_flags(struct args *args, int window,
                    845:     struct cmd_find_state *fs, struct options **oo, char **cause)
                    846: {
1.50      nicm      847:        struct session          *s = fs->s;
                    848:        struct winlink          *wl = fs->wl;
                    849:        struct window_pane      *wp = fs->wp;
                    850:        const char              *target = args_get(args, 't');
1.26      nicm      851:
                    852:        if (args_has(args, 's')) {
                    853:                *oo = global_options;
                    854:                return (OPTIONS_TABLE_SERVER);
                    855:        }
1.12      nicm      856:
1.50      nicm      857:        if (args_has(args, 'p')) {
                    858:                if (wp == NULL) {
                    859:                        if (target != NULL)
                    860:                                xasprintf(cause, "no such pane: %s", target);
                    861:                        else
                    862:                                xasprintf(cause, "no current pane");
                    863:                        return (OPTIONS_TABLE_NONE);
                    864:                }
                    865:                *oo = wp->options;
                    866:                return (OPTIONS_TABLE_PANE);
                    867:        } else if (window || args_has(args, 'w')) {
1.26      nicm      868:                if (args_has(args, 'g')) {
                    869:                        *oo = global_w_options;
                    870:                        return (OPTIONS_TABLE_WINDOW);
                    871:                }
                    872:                if (wl == NULL) {
                    873:                        if (target != NULL)
                    874:                                xasprintf(cause, "no such window: %s", target);
                    875:                        else
                    876:                                xasprintf(cause, "no current window");
                    877:                        return (OPTIONS_TABLE_NONE);
                    878:                }
                    879:                *oo = wl->window->options;
                    880:                return (OPTIONS_TABLE_WINDOW);
                    881:        } else {
                    882:                if (args_has(args, 'g')) {
                    883:                        *oo = global_s_options;
                    884:                        return (OPTIONS_TABLE_SESSION);
                    885:                }
                    886:                if (s == NULL) {
                    887:                        if (target != NULL)
                    888:                                xasprintf(cause, "no such session: %s", target);
                    889:                        else
                    890:                                xasprintf(cause, "no current session");
                    891:                        return (OPTIONS_TABLE_NONE);
                    892:                }
                    893:                *oo = s->options;
                    894:                return (OPTIONS_TABLE_SESSION);
                    895:        }
1.56      nicm      896: }
                    897:
                    898: struct style *
                    899: options_string_to_style(struct options *oo, const char *name,
                    900:     struct format_tree *ft)
                    901: {
                    902:        struct options_entry    *o;
                    903:        const char              *s;
                    904:        char                    *expanded;
                    905:
                    906:        o = options_get(oo, name);
                    907:        if (o == NULL || !OPTIONS_IS_STRING(o))
                    908:                return (NULL);
                    909:
                    910:        if (o->cached)
                    911:                return (&o->style);
                    912:        s = o->value.string;
                    913:        log_debug("%s: %s is '%s'", __func__, name, s);
                    914:
                    915:        style_set(&o->style, &grid_default_cell);
                    916:        o->cached = (strstr(s, "#{") == NULL);
                    917:
                    918:        if (ft != NULL && !o->cached) {
                    919:                expanded = format_expand(ft, s);
                    920:                if (style_parse(&o->style, &grid_default_cell, expanded) != 0) {
                    921:                        free(expanded);
                    922:                        return (NULL);
                    923:                }
                    924:                free(expanded);
                    925:        } else {
                    926:                if (style_parse(&o->style, &grid_default_cell, s) != 0)
                    927:                        return (NULL);
                    928:        }
                    929:        return (&o->style);
1.57      nicm      930: }
                    931:
                    932: static int
                    933: options_from_string_check(const struct options_table_entry *oe,
                    934:     const char *value, char **cause)
                    935: {
                    936:        struct style    sy;
                    937:
                    938:        if (oe == NULL)
                    939:                return (0);
                    940:        if (strcmp(oe->name, "default-shell") == 0 && !checkshell(value)) {
                    941:                xasprintf(cause, "not a suitable shell: %s", value);
                    942:                return (-1);
                    943:        }
                    944:        if (oe->pattern != NULL && fnmatch(oe->pattern, value, 0) != 0) {
                    945:                xasprintf(cause, "value is invalid: %s", value);
                    946:                return (-1);
                    947:        }
                    948:        if ((oe->flags & OPTIONS_TABLE_IS_STYLE) &&
                    949:            strstr(value, "#{") == NULL &&
                    950:            style_parse(&sy, &grid_default_cell, value) != 0) {
                    951:                xasprintf(cause, "invalid style: %s", value);
                    952:                return (-1);
                    953:        }
                    954:        return (0);
                    955: }
                    956:
                    957: static int
                    958: options_from_string_flag(struct options *oo, const char *name,
                    959:     const char *value, char **cause)
                    960: {
                    961:        int     flag;
                    962:
                    963:        if (value == NULL || *value == '\0')
                    964:                flag = !options_get_number(oo, name);
                    965:        else if (strcmp(value, "1") == 0 ||
                    966:            strcasecmp(value, "on") == 0 ||
                    967:            strcasecmp(value, "yes") == 0)
                    968:                flag = 1;
                    969:        else if (strcmp(value, "0") == 0 ||
                    970:            strcasecmp(value, "off") == 0 ||
                    971:            strcasecmp(value, "no") == 0)
                    972:                flag = 0;
                    973:        else {
                    974:                xasprintf(cause, "bad value: %s", value);
                    975:                return (-1);
                    976:        }
                    977:        options_set_number(oo, name, flag);
                    978:        return (0);
                    979: }
                    980:
                    981: static int
                    982: options_from_string_choice(const struct options_table_entry *oe,
                    983:     struct options *oo, const char *name, const char *value, char **cause)
                    984: {
                    985:        const char      **cp;
                    986:        int               n, choice = -1;
                    987:
                    988:        if (value == NULL) {
                    989:                choice = options_get_number(oo, name);
                    990:                if (choice < 2)
                    991:                        choice = !choice;
                    992:        } else {
                    993:                n = 0;
                    994:                for (cp = oe->choices; *cp != NULL; cp++) {
                    995:                        if (strcmp(*cp, value) == 0)
                    996:                                choice = n;
                    997:                        n++;
                    998:                }
                    999:                if (choice == -1) {
                   1000:                        xasprintf(cause, "unknown value: %s", value);
                   1001:                        return (-1);
                   1002:                }
                   1003:        }
                   1004:        options_set_number(oo, name, choice);
                   1005:        return (0);
                   1006: }
                   1007:
                   1008: int
                   1009: options_from_string(struct options *oo, const struct options_table_entry *oe,
                   1010:     const char *name, const char *value, int append, char **cause)
                   1011: {
                   1012:        enum options_table_type  type;
                   1013:        long long                number;
                   1014:        const char              *errstr, *new;
                   1015:        char                    *old;
                   1016:        key_code                 key;
                   1017:
                   1018:        if (oe != NULL) {
                   1019:                if (value == NULL &&
                   1020:                    oe->type != OPTIONS_TABLE_FLAG &&
                   1021:                    oe->type != OPTIONS_TABLE_CHOICE) {
                   1022:                        xasprintf(cause, "empty value");
                   1023:                        return (-1);
                   1024:                }
                   1025:                type = oe->type;
                   1026:        } else {
                   1027:                if (*name != '@') {
                   1028:                        xasprintf(cause, "bad option name");
                   1029:                        return (-1);
                   1030:                }
                   1031:                type = OPTIONS_TABLE_STRING;
                   1032:        }
                   1033:
                   1034:        switch (type) {
                   1035:        case OPTIONS_TABLE_STRING:
                   1036:                old = xstrdup(options_get_string(oo, name));
                   1037:                options_set_string(oo, name, append, "%s", value);
                   1038:
                   1039:                new = options_get_string(oo, name);
                   1040:                if (options_from_string_check(oe, new, cause) != 0) {
                   1041:                        options_set_string(oo, name, 0, "%s", old);
                   1042:                        free(old);
                   1043:                        return (-1);
                   1044:                }
                   1045:                free(old);
                   1046:                return (0);
                   1047:        case OPTIONS_TABLE_NUMBER:
                   1048:                number = strtonum(value, oe->minimum, oe->maximum, &errstr);
                   1049:                if (errstr != NULL) {
                   1050:                        xasprintf(cause, "value is %s: %s", errstr, value);
                   1051:                        return (-1);
                   1052:                }
                   1053:                options_set_number(oo, name, number);
                   1054:                return (0);
                   1055:        case OPTIONS_TABLE_KEY:
                   1056:                key = key_string_lookup_string(value);
                   1057:                if (key == KEYC_UNKNOWN) {
                   1058:                        xasprintf(cause, "bad key: %s", value);
                   1059:                        return (-1);
                   1060:                }
                   1061:                options_set_number(oo, name, key);
                   1062:                return (0);
                   1063:        case OPTIONS_TABLE_COLOUR:
                   1064:                if ((number = colour_fromstring(value)) == -1) {
                   1065:                        xasprintf(cause, "bad colour: %s", value);
                   1066:                        return (-1);
                   1067:                }
                   1068:                options_set_number(oo, name, number);
                   1069:                return (0);
                   1070:        case OPTIONS_TABLE_FLAG:
                   1071:                return (options_from_string_flag(oo, name, value, cause));
                   1072:        case OPTIONS_TABLE_CHOICE:
                   1073:                return (options_from_string_choice(oe, oo, name, value, cause));
                   1074:        case OPTIONS_TABLE_COMMAND:
                   1075:                break;
                   1076:        }
                   1077:        return (-1);
                   1078: }
                   1079:
                   1080: void
                   1081: options_push_changes(const char *name)
                   1082: {
                   1083:        struct client           *loop;
                   1084:        struct session          *s;
                   1085:        struct window           *w;
                   1086:        struct window_pane      *wp;
                   1087:
                   1088:        if (strcmp(name, "automatic-rename") == 0) {
                   1089:                RB_FOREACH(w, windows, &windows) {
                   1090:                        if (w->active == NULL)
                   1091:                                continue;
                   1092:                        if (options_get_number(w->options, "automatic-rename"))
                   1093:                                w->active->flags |= PANE_CHANGED;
                   1094:                }
                   1095:        }
                   1096:        if (strcmp(name, "key-table") == 0) {
                   1097:                TAILQ_FOREACH(loop, &clients, entry)
                   1098:                        server_client_set_key_table(loop, NULL);
                   1099:        }
                   1100:        if (strcmp(name, "user-keys") == 0) {
                   1101:                TAILQ_FOREACH(loop, &clients, entry) {
                   1102:                        if (loop->tty.flags & TTY_OPENED)
                   1103:                                tty_keys_build(&loop->tty);
                   1104:                }
                   1105:        }
                   1106:        if (strcmp(name, "status") == 0 ||
                   1107:            strcmp(name, "status-interval") == 0)
                   1108:                status_timer_start_all();
                   1109:        if (strcmp(name, "monitor-silence") == 0)
                   1110:                alerts_reset_all();
                   1111:        if (strcmp(name, "window-style") == 0 ||
                   1112:            strcmp(name, "window-active-style") == 0) {
                   1113:                RB_FOREACH(wp, window_pane_tree, &all_window_panes)
                   1114:                        wp->flags |= PANE_STYLECHANGED;
                   1115:        }
                   1116:        if (strcmp(name, "pane-border-status") == 0) {
                   1117:                RB_FOREACH(w, windows, &windows)
1.62    ! nicm     1118:                        layout_fix_panes(w, NULL);
1.57      nicm     1119:        }
                   1120:        RB_FOREACH(s, sessions, &sessions)
                   1121:                status_update_cache(s);
                   1122:
                   1123:        recalculate_sizes();
                   1124:        TAILQ_FOREACH(loop, &clients, entry) {
                   1125:                if (loop->session != NULL)
                   1126:                        server_redraw_client(loop);
                   1127:        }
1.59      nicm     1128: }
                   1129:
                   1130: int
                   1131: options_remove_or_default(struct options_entry *o, int idx, char **cause)
                   1132: {
                   1133:        struct options  *oo = o->owner;
                   1134:
                   1135:        if (idx == -1) {
                   1136:                if (o->tableentry != NULL &&
                   1137:                    (oo == global_options ||
                   1138:                    oo == global_s_options ||
                   1139:                    oo == global_w_options))
                   1140:                        options_default(oo, o->tableentry);
                   1141:                else
                   1142:                        options_remove(o);
                   1143:        } else if (options_array_set(o, idx, NULL, 0, cause) != 0)
                   1144:                return (-1);
                   1145:        return (0);
1.1       nicm     1146: }