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

1.52    ! nicm        1: /* $OpenBSD: options.c,v 1.51 2019/06/20 18:13:04 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.1       nicm       22: #include <stdarg.h>
1.8       nicm       23: #include <stdlib.h>
1.1       nicm       24: #include <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: /*
                     29:  * Option handling; each option has a name, type and value and is stored in
1.9       nicm       30:  * a red-black tree.
1.1       nicm       31:  */
                     32:
1.39      nicm       33: struct options_array_item {
                     34:        u_int                            index;
1.41      nicm       35:        union options_value              value;
1.39      nicm       36:        RB_ENTRY(options_array_item)     entry;
                     37: };
                     38: static int
                     39: options_array_cmp(struct options_array_item *a1, struct options_array_item *a2)
                     40: {
                     41:        if (a1->index < a2->index)
                     42:                return (-1);
                     43:        if (a1->index > a2->index)
                     44:                return (1);
                     45:        return (0);
                     46: }
                     47: RB_GENERATE_STATIC(options_array, options_array_item, entry, options_array_cmp);
                     48:
1.27      nicm       49: struct options_entry {
1.41      nicm       50:        struct options                          *owner;
1.26      nicm       51:
1.41      nicm       52:        const char                              *name;
                     53:        const struct options_table_entry        *tableentry;
                     54:        union options_value                      value;
1.26      nicm       55:
1.41      nicm       56:        RB_ENTRY(options_entry)                  entry;
1.26      nicm       57: };
                     58:
1.13      nicm       59: struct options {
1.27      nicm       60:        RB_HEAD(options_tree, options_entry)     tree;
1.26      nicm       61:        struct options                          *parent;
1.13      nicm       62: };
                     63:
1.27      nicm       64: static struct options_entry    *options_add(struct options *, const char *);
1.26      nicm       65:
                     66: #define OPTIONS_IS_STRING(o)                                           \
                     67:        ((o)->tableentry == NULL ||                                     \
                     68:            (o)->tableentry->type == OPTIONS_TABLE_STRING)
                     69: #define OPTIONS_IS_NUMBER(o) \
                     70:        ((o)->tableentry != NULL &&                                     \
                     71:            ((o)->tableentry->type == OPTIONS_TABLE_NUMBER ||           \
                     72:            (o)->tableentry->type == OPTIONS_TABLE_KEY ||               \
                     73:            (o)->tableentry->type == OPTIONS_TABLE_COLOUR ||            \
                     74:            (o)->tableentry->type == OPTIONS_TABLE_FLAG ||              \
                     75:            (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
                     76: #define OPTIONS_IS_STYLE(o) \
                     77:        ((o)->tableentry != NULL &&                                     \
                     78:            (o)->tableentry->type == OPTIONS_TABLE_STYLE)
1.43      nicm       79: #define OPTIONS_IS_COMMAND(o) \
                     80:        ((o)->tableentry != NULL &&                                     \
                     81:            (o)->tableentry->type == OPTIONS_TABLE_COMMAND)
1.41      nicm       82:
                     83: #define OPTIONS_IS_ARRAY(o)                                            \
1.26      nicm       84:        ((o)->tableentry != NULL &&                                     \
1.41      nicm       85:            ((o)->tableentry->flags & OPTIONS_TABLE_IS_ARRAY))
1.26      nicm       86:
1.27      nicm       87: static int     options_cmp(struct options_entry *, struct options_entry *);
                     88: RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
1.1       nicm       89:
1.17      nicm       90: static int
1.27      nicm       91: options_cmp(struct options_entry *lhs, struct options_entry *rhs)
1.26      nicm       92: {
                     93:        return (strcmp(lhs->name, rhs->name));
                     94: }
                     95:
                     96: static const struct options_table_entry *
                     97: options_parent_table_entry(struct options *oo, const char *s)
1.1       nicm       98: {
1.27      nicm       99:        struct options_entry    *o;
1.26      nicm      100:
                    101:        if (oo->parent == NULL)
                    102:                fatalx("no parent options for %s", s);
1.50      nicm      103:        o = options_get(oo->parent, s);
1.26      nicm      104:        if (o == NULL)
                    105:                fatalx("%s not in parent options", s);
                    106:        return (o->tableentry);
1.1       nicm      107: }
                    108:
1.41      nicm      109: static void
                    110: options_value_free(struct options_entry *o, union options_value *ov)
                    111: {
                    112:        if (OPTIONS_IS_STRING(o))
                    113:                free(ov->string);
1.43      nicm      114:        if (OPTIONS_IS_COMMAND(o) && ov->cmdlist != NULL)
                    115:                cmd_list_free(ov->cmdlist);
1.41      nicm      116: }
                    117:
1.42      nicm      118: static char *
1.41      nicm      119: options_value_tostring(struct options_entry *o, union options_value *ov,
                    120:     int numeric)
                    121: {
1.42      nicm      122:        char    *s;
1.41      nicm      123:
1.43      nicm      124:        if (OPTIONS_IS_COMMAND(o))
1.46      nicm      125:                return (cmd_list_print(ov->cmdlist, 0));
1.41      nicm      126:        if (OPTIONS_IS_STYLE(o))
1.42      nicm      127:                return (xstrdup(style_tostring(&ov->style)));
1.41      nicm      128:        if (OPTIONS_IS_NUMBER(o)) {
                    129:                switch (o->tableentry->type) {
                    130:                case OPTIONS_TABLE_NUMBER:
1.42      nicm      131:                        xasprintf(&s, "%lld", ov->number);
1.41      nicm      132:                        break;
                    133:                case OPTIONS_TABLE_KEY:
1.42      nicm      134:                        s = xstrdup(key_string_lookup_key(ov->number));
1.41      nicm      135:                        break;
                    136:                case OPTIONS_TABLE_COLOUR:
1.42      nicm      137:                        s = xstrdup(colour_tostring(ov->number));
1.41      nicm      138:                        break;
                    139:                case OPTIONS_TABLE_FLAG:
                    140:                        if (numeric)
1.42      nicm      141:                                xasprintf(&s, "%lld", ov->number);
1.41      nicm      142:                        else
1.42      nicm      143:                                s = xstrdup(ov->number ? "on" : "off");
1.41      nicm      144:                        break;
                    145:                case OPTIONS_TABLE_CHOICE:
1.42      nicm      146:                        s = xstrdup(o->tableentry->choices[ov->number]);
1.41      nicm      147:                        break;
                    148:                case OPTIONS_TABLE_STRING:
                    149:                case OPTIONS_TABLE_STYLE:
1.43      nicm      150:                case OPTIONS_TABLE_COMMAND:
1.42      nicm      151:                        fatalx("not a number option type");
1.41      nicm      152:                }
                    153:                return (s);
                    154:        }
                    155:        if (OPTIONS_IS_STRING(o))
1.42      nicm      156:                return (xstrdup(ov->string));
                    157:        return (xstrdup(""));
1.41      nicm      158: }
                    159:
1.13      nicm      160: struct options *
                    161: options_create(struct options *parent)
1.1       nicm      162: {
1.16      nicm      163:        struct options  *oo;
1.13      nicm      164:
                    165:        oo = xcalloc(1, sizeof *oo);
1.7       nicm      166:        RB_INIT(&oo->tree);
1.1       nicm      167:        oo->parent = parent;
1.13      nicm      168:        return (oo);
1.1       nicm      169: }
                    170:
                    171: void
                    172: options_free(struct options *oo)
                    173: {
1.27      nicm      174:        struct options_entry    *o, *tmp;
1.1       nicm      175:
1.35      nicm      176:        RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
1.26      nicm      177:                options_remove(o);
1.13      nicm      178:        free(oo);
                    179: }
                    180:
1.50      nicm      181: void
                    182: options_set_parent(struct options *oo, struct options *parent)
                    183: {
                    184:        oo->parent = parent;
                    185: }
                    186:
1.27      nicm      187: struct options_entry *
1.13      nicm      188: options_first(struct options *oo)
                    189: {
                    190:        return (RB_MIN(options_tree, &oo->tree));
                    191: }
                    192:
1.27      nicm      193: struct options_entry *
                    194: options_next(struct options_entry *o)
1.13      nicm      195: {
                    196:        return (RB_NEXT(options_tree, &oo->tree, o));
1.1       nicm      197: }
                    198:
1.27      nicm      199: struct options_entry *
1.26      nicm      200: options_get_only(struct options *oo, const char *name)
1.1       nicm      201: {
1.27      nicm      202:        struct options_entry    o;
1.1       nicm      203:
1.26      nicm      204:        o.name = name;
                    205:        return (RB_FIND(options_tree, &oo->tree, &o));
1.1       nicm      206: }
                    207:
1.27      nicm      208: struct options_entry *
1.26      nicm      209: options_get(struct options *oo, const char *name)
1.1       nicm      210: {
1.27      nicm      211:        struct options_entry    *o;
1.1       nicm      212:
1.26      nicm      213:        o = options_get_only(oo, name);
1.1       nicm      214:        while (o == NULL) {
                    215:                oo = oo->parent;
                    216:                if (oo == NULL)
                    217:                        break;
1.26      nicm      218:                o = options_get_only(oo, name);
                    219:        }
                    220:        return (o);
                    221: }
                    222:
1.27      nicm      223: struct options_entry *
1.26      nicm      224: options_empty(struct options *oo, const struct options_table_entry *oe)
                    225: {
1.27      nicm      226:        struct options_entry    *o;
1.26      nicm      227:
                    228:        o = options_add(oo, oe->name);
                    229:        o->tableentry = oe;
                    230:
1.42      nicm      231:        if (oe->flags & OPTIONS_TABLE_IS_ARRAY)
1.41      nicm      232:                RB_INIT(&o->value.array);
1.39      nicm      233:
1.26      nicm      234:        return (o);
                    235: }
                    236:
1.27      nicm      237: struct options_entry *
1.26      nicm      238: options_default(struct options *oo, const struct options_table_entry *oe)
                    239: {
1.41      nicm      240:        struct options_entry    *o;
                    241:        union options_value     *ov;
                    242:        u_int                    i;
1.26      nicm      243:
                    244:        o = options_empty(oo, oe);
1.41      nicm      245:        ov = &o->value;
                    246:
                    247:        if (oe->flags & OPTIONS_TABLE_IS_ARRAY) {
1.43      nicm      248:                if (oe->default_arr == NULL) {
                    249:                        options_array_assign(o, oe->default_str, NULL);
                    250:                        return (o);
                    251:                }
                    252:                for (i = 0; oe->default_arr[i] != NULL; i++)
                    253:                        options_array_set(o, i, oe->default_arr[i], 0, NULL);
1.41      nicm      254:                return (o);
                    255:        }
                    256:
                    257:        switch (oe->type) {
                    258:        case OPTIONS_TABLE_STRING:
                    259:                ov->string = xstrdup(oe->default_str);
                    260:                break;
                    261:        case OPTIONS_TABLE_STYLE:
                    262:                style_set(&ov->style, &grid_default_cell);
                    263:                style_parse(&ov->style, &grid_default_cell, 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.27      nicm      272: static struct options_entry *
1.26      nicm      273: options_add(struct options *oo, const char *name)
                    274: {
1.27      nicm      275:        struct options_entry    *o;
1.26      nicm      276:
                    277:        o = options_get_only(oo, name);
                    278:        if (o != NULL)
                    279:                options_remove(o);
                    280:
                    281:        o = xcalloc(1, sizeof *o);
                    282:        o->owner = oo;
                    283:        o->name = xstrdup(name);
                    284:
                    285:        RB_INSERT(options_tree, &oo->tree, o);
1.1       nicm      286:        return (o);
                    287: }
                    288:
1.2       nicm      289: void
1.27      nicm      290: options_remove(struct options_entry *o)
1.26      nicm      291: {
                    292:        struct options  *oo = o->owner;
                    293:
1.41      nicm      294:        if (OPTIONS_IS_ARRAY(o))
1.39      nicm      295:                options_array_clear(o);
1.41      nicm      296:        else
                    297:                options_value_free(o, &o->value);
1.26      nicm      298:        RB_REMOVE(options_tree, &oo->tree, o);
                    299:        free(o);
                    300: }
                    301:
                    302: const char *
1.27      nicm      303: options_name(struct options_entry *o)
1.26      nicm      304: {
                    305:        return (o->name);
                    306: }
                    307:
                    308: const struct options_table_entry *
1.27      nicm      309: options_table_entry(struct options_entry *o)
1.26      nicm      310: {
                    311:        return (o->tableentry);
                    312: }
                    313:
1.39      nicm      314: static struct options_array_item *
                    315: options_array_item(struct options_entry *o, u_int idx)
                    316: {
                    317:        struct options_array_item       a;
                    318:
                    319:        a.index = idx;
1.41      nicm      320:        return (RB_FIND(options_array, &o->value.array, &a));
1.39      nicm      321: }
                    322:
                    323: static void
                    324: options_array_free(struct options_entry *o, struct options_array_item *a)
                    325: {
1.41      nicm      326:        options_value_free(o, &a->value);
                    327:        RB_REMOVE(options_array, &o->value.array, a);
1.39      nicm      328:        free(a);
                    329: }
                    330:
1.31      nicm      331: void
                    332: options_array_clear(struct options_entry *o)
                    333: {
1.39      nicm      334:        struct options_array_item       *a, *a1;
                    335:
                    336:        if (!OPTIONS_IS_ARRAY(o))
                    337:                return;
                    338:
1.41      nicm      339:        RB_FOREACH_SAFE(a, options_array, &o->value.array, a1)
1.39      nicm      340:            options_array_free(o, a);
1.31      nicm      341: }
                    342:
1.41      nicm      343: union options_value *
1.27      nicm      344: options_array_get(struct options_entry *o, u_int idx)
1.26      nicm      345: {
1.39      nicm      346:        struct options_array_item       *a;
                    347:
1.26      nicm      348:        if (!OPTIONS_IS_ARRAY(o))
                    349:                return (NULL);
1.39      nicm      350:        a = options_array_item(o, idx);
                    351:        if (a == NULL)
1.26      nicm      352:                return (NULL);
1.41      nicm      353:        return (&a->value);
1.26      nicm      354: }
                    355:
                    356: int
1.31      nicm      357: options_array_set(struct options_entry *o, u_int idx, const char *value,
1.43      nicm      358:     int append, char **cause)
1.26      nicm      359: {
1.39      nicm      360:        struct options_array_item       *a;
                    361:        char                            *new;
1.45      nicm      362:        struct cmd_parse_result         *pr;
1.26      nicm      363:
1.43      nicm      364:        if (!OPTIONS_IS_ARRAY(o)) {
1.44      nicm      365:                if (cause != NULL)
                    366:                        *cause = xstrdup("not an array");
1.26      nicm      367:                return (-1);
1.43      nicm      368:        }
                    369:
1.48      nicm      370:        if (OPTIONS_IS_COMMAND(o) && value != NULL) {
1.45      nicm      371:                pr = cmd_parse_from_string(value, NULL);
                    372:                switch (pr->status) {
                    373:                case CMD_PARSE_EMPTY:
1.47      nicm      374:                        if (cause != NULL)
                    375:                                *cause = xstrdup("empty command");
1.45      nicm      376:                        return (-1);
                    377:                case CMD_PARSE_ERROR:
1.44      nicm      378:                        if (cause != NULL)
1.45      nicm      379:                                *cause = pr->error;
1.44      nicm      380:                        else
1.45      nicm      381:                                free(pr->error);
1.43      nicm      382:                        return (-1);
1.45      nicm      383:                case CMD_PARSE_SUCCESS:
                    384:                        break;
1.44      nicm      385:                }
1.43      nicm      386:        }
1.26      nicm      387:
1.39      nicm      388:        a = options_array_item(o, idx);
                    389:        if (value == NULL) {
                    390:                if (a != NULL)
                    391:                        options_array_free(o, a);
                    392:                return (0);
1.26      nicm      393:        }
1.31      nicm      394:
1.43      nicm      395:        if (OPTIONS_IS_STRING(o)) {
                    396:                if (a != NULL && append)
                    397:                        xasprintf(&new, "%s%s", a->value.string, value);
                    398:                else
                    399:                        new = xstrdup(value);
                    400:        }
                    401:
1.39      nicm      402:        if (a == NULL) {
                    403:                a = xcalloc(1, sizeof *a);
                    404:                a->index = idx;
1.41      nicm      405:                RB_INSERT(options_array, &o->value.array, a);
1.43      nicm      406:        } else
1.41      nicm      407:                options_value_free(o, &a->value);
1.43      nicm      408:
                    409:        if (OPTIONS_IS_STRING(o))
1.41      nicm      410:                a->value.string = new;
1.43      nicm      411:        else if (OPTIONS_IS_COMMAND(o))
1.45      nicm      412:                a->value.cmdlist = pr->cmdlist;
1.26      nicm      413:        return (0);
                    414: }
                    415:
1.43      nicm      416: int
                    417: options_array_assign(struct options_entry *o, const char *s, char **cause)
1.31      nicm      418: {
                    419:        const char      *separator;
                    420:        char            *copy, *next, *string;
                    421:        u_int            i;
                    422:
                    423:        separator = o->tableentry->separator;
                    424:        if (separator == NULL)
                    425:                separator = " ,";
1.43      nicm      426:        if (*separator == '\0') {
                    427:                if (*s == '\0')
                    428:                        return (0);
                    429:                for (i = 0; i < UINT_MAX; i++) {
                    430:                        if (options_array_item(o, i) == NULL)
                    431:                                break;
                    432:                }
                    433:                return (options_array_set(o, i, s, 0, cause));
                    434:        }
1.31      nicm      435:
1.43      nicm      436:        if (*s == '\0')
                    437:                return (0);
1.31      nicm      438:        copy = string = xstrdup(s);
                    439:        while ((next = strsep(&string, separator)) != NULL) {
                    440:                if (*next == '\0')
                    441:                        continue;
1.39      nicm      442:                for (i = 0; i < UINT_MAX; i++) {
                    443:                        if (options_array_item(o, i) == NULL)
1.31      nicm      444:                                break;
                    445:                }
1.39      nicm      446:                if (i == UINT_MAX)
1.31      nicm      447:                        break;
1.43      nicm      448:                if (options_array_set(o, i, next, 0, cause) != 0) {
                    449:                        free(copy);
                    450:                        return (-1);
                    451:                }
1.31      nicm      452:        }
                    453:        free(copy);
1.43      nicm      454:        return (0);
1.31      nicm      455: }
                    456:
1.39      nicm      457: struct options_array_item *
                    458: options_array_first(struct options_entry *o)
                    459: {
                    460:        if (!OPTIONS_IS_ARRAY(o))
                    461:                return (NULL);
1.41      nicm      462:        return (RB_MIN(options_array, &o->value.array));
1.39      nicm      463: }
                    464:
                    465: struct options_array_item *
                    466: options_array_next(struct options_array_item *a)
                    467: {
1.41      nicm      468:        return (RB_NEXT(options_array, &o->value.array, a));
1.39      nicm      469: }
                    470:
                    471: u_int
                    472: options_array_item_index(struct options_array_item *a)
                    473: {
                    474:        return (a->index);
                    475: }
                    476:
1.41      nicm      477: union options_value *
1.39      nicm      478: options_array_item_value(struct options_array_item *a)
                    479: {
1.41      nicm      480:        return (&a->value);
1.39      nicm      481: }
                    482:
                    483: int
                    484: options_isarray(struct options_entry *o)
                    485: {
                    486:        return (OPTIONS_IS_ARRAY(o));
                    487: }
                    488:
1.26      nicm      489: int
1.27      nicm      490: options_isstring(struct options_entry *o)
1.26      nicm      491: {
1.41      nicm      492:        return (OPTIONS_IS_STRING(o));
1.26      nicm      493: }
                    494:
1.42      nicm      495: char *
1.32      nicm      496: options_tostring(struct options_entry *o, int idx, int numeric)
1.26      nicm      497: {
1.39      nicm      498:        struct options_array_item       *a;
1.26      nicm      499:
                    500:        if (OPTIONS_IS_ARRAY(o)) {
                    501:                if (idx == -1)
1.42      nicm      502:                        return (xstrdup(""));
1.39      nicm      503:                a = options_array_item(o, idx);
                    504:                if (a == NULL)
1.42      nicm      505:                        return (xstrdup(""));
1.41      nicm      506:                return (options_value_tostring(o, &a->value, numeric));
1.26      nicm      507:        }
1.41      nicm      508:        return (options_value_tostring(o, &o->value, numeric));
1.26      nicm      509: }
                    510:
                    511: char *
                    512: options_parse(const char *name, int *idx)
1.1       nicm      513: {
1.26      nicm      514:        char    *copy, *cp, *end;
1.1       nicm      515:
1.26      nicm      516:        if (*name == '\0')
                    517:                return (NULL);
                    518:        copy = xstrdup(name);
                    519:        if ((cp = strchr(copy, '[')) == NULL) {
                    520:                *idx = -1;
                    521:                return (copy);
                    522:        }
                    523:        end = strchr(cp + 1, ']');
                    524:        if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
                    525:                free(copy);
                    526:                return (NULL);
                    527:        }
                    528:        if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
                    529:                free(copy);
                    530:                return (NULL);
                    531:        }
                    532:        *cp = '\0';
                    533:        return (copy);
1.1       nicm      534: }
                    535:
1.27      nicm      536: struct options_entry *
1.26      nicm      537: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1       nicm      538: {
1.27      nicm      539:        struct options_entry    *o;
                    540:        char                    *name;
1.1       nicm      541:
1.26      nicm      542:        name = options_parse(s, idx);
                    543:        if (name == NULL)
                    544:                return (NULL);
                    545:        if (only)
                    546:                o = options_get_only(oo, name);
                    547:        else
                    548:                o = options_get(oo, name);
                    549:        free(name);
                    550:        return (o);
                    551: }
1.1       nicm      552:
1.26      nicm      553: char *
1.50      nicm      554: options_match(const char *s, int *idx, int *ambiguous)
1.26      nicm      555: {
                    556:        const struct options_table_entry        *oe, *found;
                    557:        char                                    *name;
                    558:        size_t                                   namelen;
                    559:
                    560:        name = options_parse(s, idx);
1.33      nicm      561:        if (name == NULL)
                    562:                return (NULL);
1.26      nicm      563:        namelen = strlen(name);
1.29      nicm      564:
                    565:        if (*name == '@') {
                    566:                *ambiguous = 0;
1.34      nicm      567:                return (name);
1.29      nicm      568:        }
1.26      nicm      569:
                    570:        found = NULL;
                    571:        for (oe = options_table; oe->name != NULL; oe++) {
                    572:                if (strcmp(oe->name, name) == 0) {
                    573:                        found = oe;
                    574:                        break;
                    575:                }
                    576:                if (strncmp(oe->name, name, namelen) == 0) {
                    577:                        if (found != NULL) {
                    578:                                *ambiguous = 1;
                    579:                                free(name);
                    580:                                return (NULL);
                    581:                        }
                    582:                        found = oe;
                    583:                }
                    584:        }
                    585:        free(name);
                    586:        if (found == NULL) {
                    587:                *ambiguous = 0;
                    588:                return (NULL);
1.22      nicm      589:        }
1.26      nicm      590:        return (xstrdup(found->name));
                    591: }
1.22      nicm      592:
1.27      nicm      593: struct options_entry *
1.26      nicm      594: options_match_get(struct options *oo, const char *s, int *idx, int only,
                    595:     int* ambiguous)
                    596: {
1.27      nicm      597:        char                    *name;
                    598:        struct options_entry    *o;
1.21      nicm      599:
1.26      nicm      600:        name = options_match(s, idx, ambiguous);
                    601:        if (name == NULL)
                    602:                return (NULL);
                    603:        *ambiguous = 0;
                    604:        if (only)
                    605:                o = options_get_only(oo, name);
                    606:        else
                    607:                o = options_get(oo, name);
                    608:        free(name);
1.4       nicm      609:        return (o);
1.1       nicm      610: }
1.26      nicm      611:
1.23      nicm      612: const char *
1.1       nicm      613: options_get_string(struct options *oo, const char *name)
                    614: {
1.27      nicm      615:        struct options_entry    *o;
1.26      nicm      616:
                    617:        o = options_get(oo, name);
                    618:        if (o == NULL)
                    619:                fatalx("missing option %s", name);
                    620:        if (!OPTIONS_IS_STRING(o))
                    621:                fatalx("option %s is not a string", name);
1.41      nicm      622:        return (o->value.string);
1.26      nicm      623: }
                    624:
                    625: long long
                    626: options_get_number(struct options *oo, const char *name)
                    627: {
1.27      nicm      628:        struct options_entry    *o;
1.1       nicm      629:
1.26      nicm      630:        o = options_get(oo, name);
                    631:        if (o == NULL)
1.15      nicm      632:                fatalx("missing option %s", name);
1.26      nicm      633:        if (!OPTIONS_IS_NUMBER(o))
                    634:            fatalx("option %s is not a number", name);
1.41      nicm      635:        return (o->value.number);
1.1       nicm      636: }
                    637:
1.37      nicm      638: struct style *
1.26      nicm      639: options_get_style(struct options *oo, const char *name)
                    640: {
1.27      nicm      641:        struct options_entry    *o;
1.26      nicm      642:
                    643:        o = options_get(oo, name);
                    644:        if (o == NULL)
                    645:                fatalx("missing option %s", name);
                    646:        if (!OPTIONS_IS_STYLE(o))
                    647:                fatalx("option %s is not a style", name);
1.41      nicm      648:        return (&o->value.style);
1.26      nicm      649: }
                    650:
1.27      nicm      651: struct options_entry *
1.26      nicm      652: options_set_string(struct options *oo, const char *name, int append,
                    653:     const char *fmt, ...)
1.1       nicm      654: {
1.27      nicm      655:        struct options_entry    *o;
                    656:        va_list                  ap;
                    657:        char                    *s, *value;
1.1       nicm      658:
1.26      nicm      659:        va_start(ap, fmt);
                    660:        xvasprintf(&s, fmt, ap);
                    661:        va_end(ap);
                    662:
                    663:        o = options_get_only(oo, name);
                    664:        if (o != NULL && append && OPTIONS_IS_STRING(o)) {
1.41      nicm      665:                xasprintf(&value, "%s%s", o->value.string, s);
1.26      nicm      666:                free(s);
                    667:        } else
                    668:                value = s;
                    669:        if (o == NULL && *name == '@')
                    670:                o = options_add(oo, name);
                    671:        else if (o == NULL) {
                    672:                o = options_default(oo, options_parent_table_entry(oo, name));
                    673:                if (o == NULL)
                    674:                        return (NULL);
                    675:        }
1.21      nicm      676:
1.26      nicm      677:        if (!OPTIONS_IS_STRING(o))
                    678:                fatalx("option %s is not a string", name);
1.41      nicm      679:        free(o->value.string);
                    680:        o->value.string = value;
1.4       nicm      681:        return (o);
1.1       nicm      682: }
                    683:
1.27      nicm      684: struct options_entry *
1.26      nicm      685: options_set_number(struct options *oo, const char *name, long long value)
1.1       nicm      686: {
1.27      nicm      687:        struct options_entry    *o;
1.26      nicm      688:
                    689:        if (*name == '@')
                    690:                fatalx("user option %s must be a string", name);
1.1       nicm      691:
1.26      nicm      692:        o = options_get_only(oo, name);
                    693:        if (o == NULL) {
                    694:                o = options_default(oo, options_parent_table_entry(oo, name));
                    695:                if (o == NULL)
                    696:                        return (NULL);
                    697:        }
                    698:
                    699:        if (!OPTIONS_IS_NUMBER(o))
                    700:                fatalx("option %s is not a number", name);
1.41      nicm      701:        o->value.number = value;
1.26      nicm      702:        return (o);
1.10      nicm      703: }
                    704:
1.27      nicm      705: struct options_entry *
1.22      nicm      706: options_set_style(struct options *oo, const char *name, int append,
                    707:     const char *value)
1.10      nicm      708: {
1.27      nicm      709:        struct options_entry    *o;
1.37      nicm      710:        struct style             sy;
1.26      nicm      711:
                    712:        if (*name == '@')
                    713:                fatalx("user option %s must be a string", name);
1.10      nicm      714:
1.26      nicm      715:        o = options_get_only(oo, name);
                    716:        if (o != NULL && append && OPTIONS_IS_STYLE(o))
1.41      nicm      717:                style_copy(&sy, &o->value.style);
1.12      nicm      718:        else
1.37      nicm      719:                style_set(&sy, &grid_default_cell);
                    720:        if (style_parse(&sy, &grid_default_cell, value) == -1)
1.26      nicm      721:                return (NULL);
                    722:        if (o == NULL) {
                    723:                o = options_default(oo, options_parent_table_entry(oo, name));
                    724:                if (o == NULL)
                    725:                        return (NULL);
                    726:        }
                    727:
                    728:        if (!OPTIONS_IS_STYLE(o))
                    729:                fatalx("option %s is not a style", name);
1.41      nicm      730:        style_copy(&o->value.style, &sy);
1.26      nicm      731:        return (o);
                    732: }
1.12      nicm      733:
1.50      nicm      734: int
1.49      nicm      735: options_scope_from_name(struct args *args, int window,
                    736:     const char *name, struct cmd_find_state *fs, struct options **oo,
                    737:     char **cause)
                    738: {
1.50      nicm      739:        struct session                          *s = fs->s;
                    740:        struct winlink                          *wl = fs->wl;
                    741:        struct window_pane                      *wp = fs->wp;
                    742:        const char                              *target = args_get(args, 't');
                    743:        const struct options_table_entry        *oe;
1.51      nicm      744:        int                                      scope = OPTIONS_TABLE_NONE;
1.49      nicm      745:
                    746:        if (*name == '@')
                    747:                return (options_scope_from_flags(args, window, fs, oo, cause));
                    748:
1.50      nicm      749:        for (oe = options_table; oe->name != NULL; oe++) {
                    750:                if (strcmp(oe->name, name) == 0)
                    751:                        break;
                    752:        }
                    753:        if (oe->name == NULL) {
1.49      nicm      754:                xasprintf(cause, "unknown option: %s", name);
                    755:                return (OPTIONS_TABLE_NONE);
                    756:        }
1.51      nicm      757:        switch (oe->scope) {
1.50      nicm      758:        case OPTIONS_TABLE_SERVER:
1.49      nicm      759:                *oo = global_options;
1.51      nicm      760:                scope = OPTIONS_TABLE_SERVER;
1.50      nicm      761:                break;
                    762:        case OPTIONS_TABLE_SESSION:
1.51      nicm      763:                if (args_has(args, 'g')) {
1.49      nicm      764:                        *oo = global_s_options;
1.51      nicm      765:                        scope = OPTIONS_TABLE_SESSION;
                    766:                } else if (s == NULL && target != NULL)
1.50      nicm      767:                        xasprintf(cause, "no such session: %s", target);
                    768:                else if (s == NULL)
                    769:                        xasprintf(cause, "no current session");
1.51      nicm      770:                else {
1.50      nicm      771:                        *oo = s->options;
1.51      nicm      772:                        scope = OPTIONS_TABLE_SESSION;
                    773:                }
1.50      nicm      774:                break;
                    775:        case OPTIONS_TABLE_WINDOW|OPTIONS_TABLE_PANE:
                    776:                if (args_has(args, 'p')) {
                    777:                        if (wp == NULL && target != NULL)
                    778:                                xasprintf(cause, "no such pane: %s", target);
                    779:                        else if (wp == NULL)
                    780:                                xasprintf(cause, "no current pane");
1.51      nicm      781:                        else {
1.50      nicm      782:                                *oo = wp->options;
1.51      nicm      783:                                scope = OPTIONS_TABLE_PANE;
                    784:                        }
1.50      nicm      785:                        break;
                    786:                }
                    787:                /* FALLTHROUGH */
                    788:        case OPTIONS_TABLE_WINDOW:
1.51      nicm      789:                if (args_has(args, 'g')) {
1.49      nicm      790:                        *oo = global_w_options;
1.51      nicm      791:                        scope = OPTIONS_TABLE_WINDOW;
                    792:                } else if (wl == NULL && target != NULL)
1.50      nicm      793:                        xasprintf(cause, "no such window: %s", target);
                    794:                else if (wl == NULL)
                    795:                        xasprintf(cause, "no current window");
1.51      nicm      796:                else {
1.49      nicm      797:                        *oo = wl->window->options;
1.51      nicm      798:                        scope = OPTIONS_TABLE_WINDOW;
                    799:                }
1.50      nicm      800:                break;
1.49      nicm      801:        }
                    802:        return (scope);
                    803: }
                    804:
1.50      nicm      805: int
1.26      nicm      806: options_scope_from_flags(struct args *args, int window,
                    807:     struct cmd_find_state *fs, struct options **oo, char **cause)
                    808: {
1.50      nicm      809:        struct session          *s = fs->s;
                    810:        struct winlink          *wl = fs->wl;
                    811:        struct window_pane      *wp = fs->wp;
                    812:        const char              *target = args_get(args, 't');
1.26      nicm      813:
                    814:        if (args_has(args, 's')) {
                    815:                *oo = global_options;
                    816:                return (OPTIONS_TABLE_SERVER);
                    817:        }
1.12      nicm      818:
1.50      nicm      819:        if (args_has(args, 'p')) {
                    820:                if (wp == NULL) {
                    821:                        if (target != NULL)
                    822:                                xasprintf(cause, "no such pane: %s", target);
                    823:                        else
                    824:                                xasprintf(cause, "no current pane");
                    825:                        return (OPTIONS_TABLE_NONE);
                    826:                }
                    827:                *oo = wp->options;
                    828:                return (OPTIONS_TABLE_PANE);
                    829:        } else if (window || args_has(args, 'w')) {
1.26      nicm      830:                if (args_has(args, 'g')) {
                    831:                        *oo = global_w_options;
                    832:                        return (OPTIONS_TABLE_WINDOW);
                    833:                }
                    834:                if (wl == NULL) {
                    835:                        if (target != NULL)
                    836:                                xasprintf(cause, "no such window: %s", target);
                    837:                        else
                    838:                                xasprintf(cause, "no current window");
                    839:                        return (OPTIONS_TABLE_NONE);
                    840:                }
                    841:                *oo = wl->window->options;
                    842:                return (OPTIONS_TABLE_WINDOW);
                    843:        } else {
                    844:                if (args_has(args, 'g')) {
                    845:                        *oo = global_s_options;
                    846:                        return (OPTIONS_TABLE_SESSION);
                    847:                }
                    848:                if (s == NULL) {
                    849:                        if (target != NULL)
                    850:                                xasprintf(cause, "no such session: %s", target);
                    851:                        else
                    852:                                xasprintf(cause, "no current session");
                    853:                        return (OPTIONS_TABLE_NONE);
                    854:                }
                    855:                *oo = s->options;
                    856:                return (OPTIONS_TABLE_SESSION);
                    857:        }
1.1       nicm      858: }