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

1.40    ! nicm        1: /* $OpenBSD: options.c,v 1.39 2019/03/18 11:58:40 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;
                     35:        char                            *value;
                     36:        RB_ENTRY(options_array_item)     entry;
                     37: };
                     38: RB_HEAD(options_array, options_array_item);
                     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.26      nicm       51:        struct options                           *owner;
                     52:
                     53:        const char                               *name;
                     54:        const struct options_table_entry         *tableentry;
                     55:
                     56:        union {
                     57:                char                             *string;
                     58:                long long                         number;
1.37      nicm       59:                struct style                      style;
1.39      nicm       60:                struct options_array              array;
1.26      nicm       61:        };
                     62:
1.27      nicm       63:        RB_ENTRY(options_entry)                   entry;
1.26      nicm       64: };
                     65:
1.13      nicm       66: struct options {
1.27      nicm       67:        RB_HEAD(options_tree, options_entry)     tree;
1.26      nicm       68:        struct options                          *parent;
1.13      nicm       69: };
                     70:
1.27      nicm       71: static struct options_entry    *options_add(struct options *, const char *);
1.26      nicm       72:
                     73: #define OPTIONS_IS_STRING(o)                                           \
                     74:        ((o)->tableentry == NULL ||                                     \
                     75:            (o)->tableentry->type == OPTIONS_TABLE_STRING)
                     76: #define OPTIONS_IS_NUMBER(o) \
                     77:        ((o)->tableentry != NULL &&                                     \
                     78:            ((o)->tableentry->type == OPTIONS_TABLE_NUMBER ||           \
                     79:            (o)->tableentry->type == OPTIONS_TABLE_KEY ||               \
                     80:            (o)->tableentry->type == OPTIONS_TABLE_COLOUR ||            \
                     81:            (o)->tableentry->type == OPTIONS_TABLE_FLAG ||              \
                     82:            (o)->tableentry->type == OPTIONS_TABLE_CHOICE))
                     83: #define OPTIONS_IS_STYLE(o) \
                     84:        ((o)->tableentry != NULL &&                                     \
                     85:            (o)->tableentry->type == OPTIONS_TABLE_STYLE)
                     86: #define OPTIONS_IS_ARRAY(o) \
                     87:        ((o)->tableentry != NULL &&                                     \
                     88:            (o)->tableentry->type == OPTIONS_TABLE_ARRAY)
                     89:
1.27      nicm       90: static int     options_cmp(struct options_entry *, struct options_entry *);
                     91: RB_GENERATE_STATIC(options_tree, options_entry, entry, options_cmp);
1.1       nicm       92:
1.17      nicm       93: static int
1.27      nicm       94: options_cmp(struct options_entry *lhs, struct options_entry *rhs)
1.26      nicm       95: {
                     96:        return (strcmp(lhs->name, rhs->name));
                     97: }
                     98:
                     99: static const struct options_table_entry *
                    100: options_parent_table_entry(struct options *oo, const char *s)
1.1       nicm      101: {
1.27      nicm      102:        struct options_entry    *o;
1.26      nicm      103:
                    104:        if (oo->parent == NULL)
                    105:                fatalx("no parent options for %s", s);
                    106:        o = options_get_only(oo->parent, s);
                    107:        if (o == NULL)
                    108:                fatalx("%s not in parent options", s);
                    109:        return (o->tableentry);
1.1       nicm      110: }
                    111:
1.13      nicm      112: struct options *
                    113: options_create(struct options *parent)
1.1       nicm      114: {
1.16      nicm      115:        struct options  *oo;
1.13      nicm      116:
                    117:        oo = xcalloc(1, sizeof *oo);
1.7       nicm      118:        RB_INIT(&oo->tree);
1.1       nicm      119:        oo->parent = parent;
1.13      nicm      120:        return (oo);
1.1       nicm      121: }
                    122:
                    123: void
                    124: options_free(struct options *oo)
                    125: {
1.27      nicm      126:        struct options_entry    *o, *tmp;
1.1       nicm      127:
1.35      nicm      128:        RB_FOREACH_SAFE(o, options_tree, &oo->tree, tmp)
1.26      nicm      129:                options_remove(o);
1.13      nicm      130:        free(oo);
                    131: }
                    132:
1.27      nicm      133: struct options_entry *
1.13      nicm      134: options_first(struct options *oo)
                    135: {
                    136:        return (RB_MIN(options_tree, &oo->tree));
                    137: }
                    138:
1.27      nicm      139: struct options_entry *
                    140: options_next(struct options_entry *o)
1.13      nicm      141: {
                    142:        return (RB_NEXT(options_tree, &oo->tree, o));
1.1       nicm      143: }
                    144:
1.27      nicm      145: struct options_entry *
1.26      nicm      146: options_get_only(struct options *oo, const char *name)
1.1       nicm      147: {
1.27      nicm      148:        struct options_entry    o;
1.1       nicm      149:
1.26      nicm      150:        o.name = name;
                    151:        return (RB_FIND(options_tree, &oo->tree, &o));
1.1       nicm      152: }
                    153:
1.27      nicm      154: struct options_entry *
1.26      nicm      155: options_get(struct options *oo, const char *name)
1.1       nicm      156: {
1.27      nicm      157:        struct options_entry    *o;
1.1       nicm      158:
1.26      nicm      159:        o = options_get_only(oo, name);
1.1       nicm      160:        while (o == NULL) {
                    161:                oo = oo->parent;
                    162:                if (oo == NULL)
                    163:                        break;
1.26      nicm      164:                o = options_get_only(oo, name);
                    165:        }
                    166:        return (o);
                    167: }
                    168:
1.27      nicm      169: struct options_entry *
1.26      nicm      170: options_empty(struct options *oo, const struct options_table_entry *oe)
                    171: {
1.27      nicm      172:        struct options_entry    *o;
1.26      nicm      173:
                    174:        o = options_add(oo, oe->name);
                    175:        o->tableentry = oe;
                    176:
1.39      nicm      177:        if (oe->type == OPTIONS_TABLE_ARRAY)
                    178:                RB_INIT(&o->array);
                    179:
1.26      nicm      180:        return (o);
                    181: }
                    182:
1.27      nicm      183: struct options_entry *
1.26      nicm      184: options_default(struct options *oo, const struct options_table_entry *oe)
                    185: {
1.38      nicm      186:        struct options_entry     *o;
                    187:        u_int                     i;
1.26      nicm      188:
                    189:        o = options_empty(oo, oe);
1.38      nicm      190:        if (oe->type == OPTIONS_TABLE_ARRAY) {
                    191:                if (oe->default_arr != NULL) {
                    192:                        for (i = 0; oe->default_arr[i] != NULL; i++)
                    193:                                options_array_set(o, i, oe->default_arr[i], 0);
                    194:                } else
                    195:                        options_array_assign(o, oe->default_str);
                    196:        } else if (oe->type == OPTIONS_TABLE_STRING)
1.26      nicm      197:                o->string = xstrdup(oe->default_str);
                    198:        else if (oe->type == OPTIONS_TABLE_STYLE) {
1.37      nicm      199:                style_set(&o->style, &grid_default_cell);
                    200:                style_parse(&o->style, &grid_default_cell, oe->default_str);
1.26      nicm      201:        } else
                    202:                o->number = oe->default_num;
                    203:        return (o);
                    204: }
                    205:
1.27      nicm      206: static struct options_entry *
1.26      nicm      207: options_add(struct options *oo, const char *name)
                    208: {
1.27      nicm      209:        struct options_entry    *o;
1.26      nicm      210:
                    211:        o = options_get_only(oo, name);
                    212:        if (o != NULL)
                    213:                options_remove(o);
                    214:
                    215:        o = xcalloc(1, sizeof *o);
                    216:        o->owner = oo;
                    217:        o->name = xstrdup(name);
                    218:
                    219:        RB_INSERT(options_tree, &oo->tree, o);
1.1       nicm      220:        return (o);
                    221: }
                    222:
1.2       nicm      223: void
1.27      nicm      224: options_remove(struct options_entry *o)
1.26      nicm      225: {
                    226:        struct options  *oo = o->owner;
                    227:
                    228:        if (OPTIONS_IS_STRING(o))
1.39      nicm      229:                free(o->string);
                    230:        else if (OPTIONS_IS_ARRAY(o))
                    231:                options_array_clear(o);
1.26      nicm      232:
                    233:        RB_REMOVE(options_tree, &oo->tree, o);
                    234:        free(o);
                    235: }
                    236:
                    237: const char *
1.27      nicm      238: options_name(struct options_entry *o)
1.26      nicm      239: {
                    240:        return (o->name);
                    241: }
                    242:
                    243: const struct options_table_entry *
1.27      nicm      244: options_table_entry(struct options_entry *o)
1.26      nicm      245: {
                    246:        return (o->tableentry);
                    247: }
                    248:
1.39      nicm      249: static struct options_array_item *
                    250: options_array_item(struct options_entry *o, u_int idx)
                    251: {
                    252:        struct options_array_item       a;
                    253:
                    254:        a.index = idx;
                    255:        return (RB_FIND(options_array, &o->array, &a));
                    256: }
                    257:
                    258: static void
                    259: options_array_free(struct options_entry *o, struct options_array_item *a)
                    260: {
                    261:        free(a->value);
                    262:        RB_REMOVE(options_array, &o->array, a);
                    263:        free(a);
                    264: }
                    265:
1.31      nicm      266: void
                    267: options_array_clear(struct options_entry *o)
                    268: {
1.39      nicm      269:        struct options_array_item       *a, *a1;
                    270:
                    271:        if (!OPTIONS_IS_ARRAY(o))
                    272:                return;
                    273:
                    274:        RB_FOREACH_SAFE(a, options_array, &o->array, a1)
                    275:            options_array_free(o, a);
1.31      nicm      276: }
                    277:
1.26      nicm      278: const char *
1.27      nicm      279: options_array_get(struct options_entry *o, u_int idx)
1.26      nicm      280: {
1.39      nicm      281:        struct options_array_item       *a;
                    282:
1.26      nicm      283:        if (!OPTIONS_IS_ARRAY(o))
                    284:                return (NULL);
1.39      nicm      285:        a = options_array_item(o, idx);
                    286:        if (a == NULL)
1.26      nicm      287:                return (NULL);
1.39      nicm      288:        return (a->value);
1.26      nicm      289: }
                    290:
                    291: int
1.31      nicm      292: options_array_set(struct options_entry *o, u_int idx, const char *value,
                    293:     int append)
1.26      nicm      294: {
1.39      nicm      295:        struct options_array_item       *a;
                    296:        char                            *new;
1.26      nicm      297:
                    298:        if (!OPTIONS_IS_ARRAY(o))
                    299:                return (-1);
                    300:
1.39      nicm      301:        a = options_array_item(o, idx);
                    302:        if (value == NULL) {
                    303:                if (a != NULL)
                    304:                        options_array_free(o, a);
                    305:                return (0);
1.26      nicm      306:        }
1.31      nicm      307:
1.39      nicm      308:        if (a == NULL) {
                    309:                a = xcalloc(1, sizeof *a);
                    310:                a->index = idx;
                    311:                a->value = xstrdup(value);
                    312:                RB_INSERT(options_array, &o->array, a);
                    313:        } else {
                    314:                free(a->value);
                    315:                if (a != NULL && append)
                    316:                        xasprintf(&new, "%s%s", a->value, value);
1.31      nicm      317:                else
                    318:                        new = xstrdup(value);
1.39      nicm      319:                a->value = new;
1.31      nicm      320:        }
                    321:
1.26      nicm      322:        return (0);
                    323: }
                    324:
1.31      nicm      325: void
                    326: options_array_assign(struct options_entry *o, const char *s)
                    327: {
                    328:        const char      *separator;
                    329:        char            *copy, *next, *string;
                    330:        u_int            i;
                    331:
                    332:        separator = o->tableentry->separator;
                    333:        if (separator == NULL)
                    334:                separator = " ,";
                    335:
                    336:        copy = string = xstrdup(s);
                    337:        while ((next = strsep(&string, separator)) != NULL) {
                    338:                if (*next == '\0')
                    339:                        continue;
1.39      nicm      340:                for (i = 0; i < UINT_MAX; i++) {
                    341:                        if (options_array_item(o, i) == NULL)
1.31      nicm      342:                                break;
                    343:                }
1.39      nicm      344:                if (i == UINT_MAX)
1.31      nicm      345:                        break;
                    346:                options_array_set(o, i, next, 0);
                    347:        }
                    348:        free(copy);
                    349: }
                    350:
1.39      nicm      351: struct options_array_item *
                    352: options_array_first(struct options_entry *o)
                    353: {
                    354:        if (!OPTIONS_IS_ARRAY(o))
                    355:                return (NULL);
                    356:        return (RB_MIN(options_array, &o->array));
                    357: }
                    358:
                    359: struct options_array_item *
                    360: options_array_next(struct options_array_item *a)
                    361: {
                    362:        return (RB_NEXT(options_array, &o->array, a));
                    363: }
                    364:
                    365: u_int
                    366: options_array_item_index(struct options_array_item *a)
                    367: {
                    368:        return (a->index);
                    369: }
                    370:
                    371: const char *
                    372: options_array_item_value(struct options_array_item *a)
                    373: {
                    374:        return (a->value);
                    375: }
                    376:
                    377: int
                    378: options_isarray(struct options_entry *o)
                    379: {
                    380:        return (OPTIONS_IS_ARRAY(o));
                    381: }
                    382:
1.26      nicm      383: int
1.27      nicm      384: options_isstring(struct options_entry *o)
1.26      nicm      385: {
                    386:        return (OPTIONS_IS_STRING(o) || OPTIONS_IS_ARRAY(o));
                    387: }
                    388:
                    389: const char *
1.32      nicm      390: options_tostring(struct options_entry *o, int idx, int numeric)
1.26      nicm      391: {
1.39      nicm      392:        static char                      s[1024];
                    393:        const char                      *tmp;
                    394:        struct options_array_item       *a;
1.26      nicm      395:
                    396:        if (OPTIONS_IS_ARRAY(o)) {
                    397:                if (idx == -1)
                    398:                        return (NULL);
1.39      nicm      399:                a = options_array_item(o, idx);
                    400:                if (a == NULL)
1.26      nicm      401:                        return ("");
1.39      nicm      402:                return (a->value);
1.26      nicm      403:        }
                    404:        if (OPTIONS_IS_STYLE(o))
                    405:                return (style_tostring(&o->style));
                    406:        if (OPTIONS_IS_NUMBER(o)) {
                    407:                tmp = NULL;
                    408:                switch (o->tableentry->type) {
                    409:                case OPTIONS_TABLE_NUMBER:
                    410:                        xsnprintf(s, sizeof s, "%lld", o->number);
                    411:                        break;
                    412:                case OPTIONS_TABLE_KEY:
                    413:                        tmp = key_string_lookup_key(o->number);
                    414:                        break;
                    415:                case OPTIONS_TABLE_COLOUR:
                    416:                        tmp = colour_tostring(o->number);
                    417:                        break;
                    418:                case OPTIONS_TABLE_FLAG:
1.32      nicm      419:                        if (numeric)
                    420:                                xsnprintf(s, sizeof s, "%lld", o->number);
                    421:                        else
                    422:                                tmp = (o->number ? "on" : "off");
1.26      nicm      423:                        break;
                    424:                case OPTIONS_TABLE_CHOICE:
                    425:                        tmp = o->tableentry->choices[o->number];
                    426:                        break;
                    427:                case OPTIONS_TABLE_STRING:
                    428:                case OPTIONS_TABLE_STYLE:
                    429:                case OPTIONS_TABLE_ARRAY:
                    430:                        break;
                    431:                }
                    432:                if (tmp != NULL)
                    433:                        xsnprintf(s, sizeof s, "%s", tmp);
                    434:                return (s);
                    435:        }
                    436:        if (OPTIONS_IS_STRING(o))
                    437:                return (o->string);
                    438:        return (NULL);
                    439: }
                    440:
                    441: char *
                    442: options_parse(const char *name, int *idx)
1.1       nicm      443: {
1.26      nicm      444:        char    *copy, *cp, *end;
1.1       nicm      445:
1.26      nicm      446:        if (*name == '\0')
                    447:                return (NULL);
                    448:        copy = xstrdup(name);
                    449:        if ((cp = strchr(copy, '[')) == NULL) {
                    450:                *idx = -1;
                    451:                return (copy);
                    452:        }
                    453:        end = strchr(cp + 1, ']');
                    454:        if (end == NULL || end[1] != '\0' || !isdigit((u_char)end[-1])) {
                    455:                free(copy);
                    456:                return (NULL);
                    457:        }
                    458:        if (sscanf(cp, "[%d]", idx) != 1 || *idx < 0) {
                    459:                free(copy);
                    460:                return (NULL);
                    461:        }
                    462:        *cp = '\0';
                    463:        return (copy);
1.1       nicm      464: }
                    465:
1.27      nicm      466: struct options_entry *
1.26      nicm      467: options_parse_get(struct options *oo, const char *s, int *idx, int only)
1.1       nicm      468: {
1.27      nicm      469:        struct options_entry    *o;
                    470:        char                    *name;
1.1       nicm      471:
1.26      nicm      472:        name = options_parse(s, idx);
                    473:        if (name == NULL)
                    474:                return (NULL);
                    475:        if (only)
                    476:                o = options_get_only(oo, name);
                    477:        else
                    478:                o = options_get(oo, name);
                    479:        free(name);
                    480:        return (o);
                    481: }
1.1       nicm      482:
1.26      nicm      483: char *
                    484: options_match(const char *s, int *idx, int* ambiguous)
                    485: {
                    486:        const struct options_table_entry        *oe, *found;
                    487:        char                                    *name;
                    488:        size_t                                   namelen;
                    489:
                    490:        name = options_parse(s, idx);
1.33      nicm      491:        if (name == NULL)
                    492:                return (NULL);
1.26      nicm      493:        namelen = strlen(name);
1.29      nicm      494:
                    495:        if (*name == '@') {
                    496:                *ambiguous = 0;
1.34      nicm      497:                return (name);
1.29      nicm      498:        }
1.26      nicm      499:
                    500:        found = NULL;
                    501:        for (oe = options_table; oe->name != NULL; oe++) {
                    502:                if (strcmp(oe->name, name) == 0) {
                    503:                        found = oe;
                    504:                        break;
                    505:                }
                    506:                if (strncmp(oe->name, name, namelen) == 0) {
                    507:                        if (found != NULL) {
                    508:                                *ambiguous = 1;
                    509:                                free(name);
                    510:                                return (NULL);
                    511:                        }
                    512:                        found = oe;
                    513:                }
                    514:        }
                    515:        free(name);
                    516:        if (found == NULL) {
                    517:                *ambiguous = 0;
                    518:                return (NULL);
1.22      nicm      519:        }
1.26      nicm      520:        return (xstrdup(found->name));
                    521: }
1.22      nicm      522:
1.27      nicm      523: struct options_entry *
1.26      nicm      524: options_match_get(struct options *oo, const char *s, int *idx, int only,
                    525:     int* ambiguous)
                    526: {
1.27      nicm      527:        char                    *name;
                    528:        struct options_entry    *o;
1.21      nicm      529:
1.26      nicm      530:        name = options_match(s, idx, ambiguous);
                    531:        if (name == NULL)
                    532:                return (NULL);
                    533:        *ambiguous = 0;
                    534:        if (only)
                    535:                o = options_get_only(oo, name);
                    536:        else
                    537:                o = options_get(oo, name);
                    538:        free(name);
1.4       nicm      539:        return (o);
1.1       nicm      540: }
1.26      nicm      541:
1.23      nicm      542: const char *
1.1       nicm      543: options_get_string(struct options *oo, const char *name)
                    544: {
1.27      nicm      545:        struct options_entry    *o;
1.26      nicm      546:
                    547:        o = options_get(oo, name);
                    548:        if (o == NULL)
                    549:                fatalx("missing option %s", name);
                    550:        if (!OPTIONS_IS_STRING(o))
                    551:                fatalx("option %s is not a string", name);
                    552:        return (o->string);
                    553: }
                    554:
                    555: long long
                    556: options_get_number(struct options *oo, const char *name)
                    557: {
1.27      nicm      558:        struct options_entry    *o;
1.1       nicm      559:
1.26      nicm      560:        o = options_get(oo, name);
                    561:        if (o == NULL)
1.15      nicm      562:                fatalx("missing option %s", name);
1.26      nicm      563:        if (!OPTIONS_IS_NUMBER(o))
                    564:            fatalx("option %s is not a number", name);
                    565:        return (o->number);
1.1       nicm      566: }
                    567:
1.37      nicm      568: struct style *
1.26      nicm      569: options_get_style(struct options *oo, const char *name)
                    570: {
1.27      nicm      571:        struct options_entry    *o;
1.26      nicm      572:
                    573:        o = options_get(oo, name);
                    574:        if (o == NULL)
                    575:                fatalx("missing option %s", name);
                    576:        if (!OPTIONS_IS_STYLE(o))
                    577:                fatalx("option %s is not a style", name);
                    578:        return (&o->style);
                    579: }
                    580:
1.27      nicm      581: struct options_entry *
1.26      nicm      582: options_set_string(struct options *oo, const char *name, int append,
                    583:     const char *fmt, ...)
1.1       nicm      584: {
1.27      nicm      585:        struct options_entry    *o;
                    586:        va_list                  ap;
                    587:        char                    *s, *value;
1.1       nicm      588:
1.26      nicm      589:        va_start(ap, fmt);
                    590:        xvasprintf(&s, fmt, ap);
                    591:        va_end(ap);
                    592:
                    593:        o = options_get_only(oo, name);
                    594:        if (o != NULL && append && OPTIONS_IS_STRING(o)) {
                    595:                xasprintf(&value, "%s%s", o->string, s);
                    596:                free(s);
                    597:        } else
                    598:                value = s;
                    599:        if (o == NULL && *name == '@')
                    600:                o = options_add(oo, name);
                    601:        else if (o == NULL) {
                    602:                o = options_default(oo, options_parent_table_entry(oo, name));
                    603:                if (o == NULL)
                    604:                        return (NULL);
                    605:        }
1.21      nicm      606:
1.26      nicm      607:        if (!OPTIONS_IS_STRING(o))
                    608:                fatalx("option %s is not a string", name);
                    609:        free(o->string);
                    610:        o->string = value;
1.4       nicm      611:        return (o);
1.1       nicm      612: }
                    613:
1.27      nicm      614: struct options_entry *
1.26      nicm      615: options_set_number(struct options *oo, const char *name, long long value)
1.1       nicm      616: {
1.27      nicm      617:        struct options_entry    *o;
1.26      nicm      618:
                    619:        if (*name == '@')
                    620:                fatalx("user option %s must be a string", name);
1.1       nicm      621:
1.26      nicm      622:        o = options_get_only(oo, name);
                    623:        if (o == NULL) {
                    624:                o = options_default(oo, options_parent_table_entry(oo, name));
                    625:                if (o == NULL)
                    626:                        return (NULL);
                    627:        }
                    628:
                    629:        if (!OPTIONS_IS_NUMBER(o))
                    630:                fatalx("option %s is not a number", name);
                    631:        o->number = value;
                    632:        return (o);
1.10      nicm      633: }
                    634:
1.27      nicm      635: struct options_entry *
1.22      nicm      636: options_set_style(struct options *oo, const char *name, int append,
                    637:     const char *value)
1.10      nicm      638: {
1.27      nicm      639:        struct options_entry    *o;
1.37      nicm      640:        struct style             sy;
1.26      nicm      641:
                    642:        if (*name == '@')
                    643:                fatalx("user option %s must be a string", name);
1.10      nicm      644:
1.26      nicm      645:        o = options_get_only(oo, name);
                    646:        if (o != NULL && append && OPTIONS_IS_STYLE(o))
1.37      nicm      647:                style_copy(&sy, &o->style);
1.12      nicm      648:        else
1.37      nicm      649:                style_set(&sy, &grid_default_cell);
                    650:        if (style_parse(&sy, &grid_default_cell, value) == -1)
1.26      nicm      651:                return (NULL);
                    652:        if (o == NULL) {
                    653:                o = options_default(oo, options_parent_table_entry(oo, name));
                    654:                if (o == NULL)
                    655:                        return (NULL);
                    656:        }
                    657:
                    658:        if (!OPTIONS_IS_STYLE(o))
                    659:                fatalx("option %s is not a style", name);
1.37      nicm      660:        style_copy(&o->style, &sy);
1.26      nicm      661:        return (o);
                    662: }
1.12      nicm      663:
1.26      nicm      664: enum options_table_scope
                    665: options_scope_from_flags(struct args *args, int window,
                    666:     struct cmd_find_state *fs, struct options **oo, char **cause)
                    667: {
                    668:        struct session  *s = fs->s;
                    669:        struct winlink  *wl = fs->wl;
                    670:        const char      *target= args_get(args, 't');
                    671:
                    672:        if (args_has(args, 's')) {
                    673:                *oo = global_options;
                    674:                return (OPTIONS_TABLE_SERVER);
                    675:        }
1.12      nicm      676:
1.26      nicm      677:        if (window || args_has(args, 'w')) {
                    678:                if (args_has(args, 'g')) {
                    679:                        *oo = global_w_options;
                    680:                        return (OPTIONS_TABLE_WINDOW);
                    681:                }
                    682:                if (wl == NULL) {
                    683:                        if (target != NULL)
                    684:                                xasprintf(cause, "no such window: %s", target);
                    685:                        else
                    686:                                xasprintf(cause, "no current window");
                    687:                        return (OPTIONS_TABLE_NONE);
                    688:                }
                    689:                *oo = wl->window->options;
                    690:                return (OPTIONS_TABLE_WINDOW);
                    691:        } else {
                    692:                if (args_has(args, 'g')) {
                    693:                        *oo = global_s_options;
                    694:                        return (OPTIONS_TABLE_SESSION);
                    695:                }
                    696:                if (s == NULL) {
                    697:                        if (target != NULL)
                    698:                                xasprintf(cause, "no such session: %s", target);
                    699:                        else
                    700:                                xasprintf(cause, "no current session");
                    701:                        return (OPTIONS_TABLE_NONE);
                    702:                }
                    703:                *oo = s->options;
                    704:                return (OPTIONS_TABLE_SESSION);
                    705:        }
1.1       nicm      706: }