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

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