[BACK]Return to cmd-set-option.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.125

1.125   ! nicm        1: /* $OpenBSD: cmd-set-option.c,v 1.124 2019/04/23 20:36:55 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.93      nicm        4:  * Copyright (c) 2007 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.120     nicm       21: #include <fnmatch.h>
1.1       nicm       22: #include <stdlib.h>
                     23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Set an option.
                     29:  */
                     30:
1.101     nicm       31: static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *);
1.59      nicm       32:
1.105     nicm       33: static int     cmd_set_option_set(struct cmd *, struct cmdq_item *,
1.107     nicm       34:                    struct options *, struct options_entry *, const char *);
1.105     nicm       35: static int     cmd_set_option_flag(struct cmdq_item *,
1.99      nicm       36:                    const struct options_table_entry *, struct options *,
                     37:                    const char *);
1.105     nicm       38: static int     cmd_set_option_choice(struct cmdq_item *,
1.99      nicm       39:                    const struct options_table_entry *, struct options *,
                     40:                    const char *);
1.43      nicm       41:
1.1       nicm       42: const struct cmd_entry cmd_set_option_entry = {
1.91      nicm       43:        .name = "set-option",
                     44:        .alias = "set",
                     45:
1.115     nicm       46:        .args = { "aFgoqst:uw", 1, 2 },
                     47:        .usage = "[-aFgosquw] [-t target-window] option [value]",
1.91      nicm       48:
1.114     nicm       49:        .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92      nicm       50:
1.100     nicm       51:        .flags = CMD_AFTERHOOK,
1.91      nicm       52:        .exec = cmd_set_option_exec
1.1       nicm       53: };
                     54:
1.46      nicm       55: const struct cmd_entry cmd_set_window_option_entry = {
1.91      nicm       56:        .name = "set-window-option",
                     57:        .alias = "setw",
                     58:
1.115     nicm       59:        .args = { "aFgoqt:u", 1, 2 },
                     60:        .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
1.91      nicm       61:
1.114     nicm       62:        .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92      nicm       63:
1.100     nicm       64:        .flags = CMD_AFTERHOOK,
1.91      nicm       65:        .exec = cmd_set_option_exec
1.46      nicm       66: };
                     67:
1.125   ! nicm       68: const struct cmd_entry cmd_set_hook_entry = {
        !            69:        .name = "set-hook",
        !            70:        .alias = NULL,
        !            71:
        !            72:        .args = { "agRt:u", 1, 2 },
        !            73:        .usage = "[-agRu] " CMD_TARGET_SESSION_USAGE " hook [command]",
        !            74:
        !            75:        .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
        !            76:
        !            77:        .flags = CMD_AFTERHOOK,
        !            78:        .exec = cmd_set_option_exec
        !            79: };
        !            80:
1.99      nicm       81: static enum cmd_retval
1.101     nicm       82: cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       83: {
1.105     nicm       84:        struct args                     *args = self->args;
1.109     nicm       85:        int                              append = args_has(args, 'a');
1.114     nicm       86:        struct cmd_find_state           *fs = &item->target;
1.115     nicm       87:        struct client                   *c, *loop;
1.105     nicm       88:        struct session                  *s = fs->s;
                     89:        struct winlink                  *wl = fs->wl;
1.112     nicm       90:        struct window                   *w;
1.105     nicm       91:        enum options_table_scope         scope;
                     92:        struct options                  *oo;
1.107     nicm       93:        struct options_entry            *parent, *o;
1.115     nicm       94:        char                            *name, *argument, *value = NULL, *cause;
                     95:        const char                      *target;
1.105     nicm       96:        int                              window, idx, already, error, ambiguous;
1.123     nicm       97:        struct style                    *sy;
1.115     nicm       98:
                     99:        /* Expand argument. */
                    100:        c = cmd_find_client(item, NULL, 1);
                    101:        argument = format_single(item, args->argv[0], c, s, wl, NULL);
1.105     nicm      102:
1.125   ! nicm      103:        if (self->entry == &cmd_set_hook_entry && args_has(args, 'R')) {
        !           104:                notify_hook(item, argument);
        !           105:                return (CMD_RETURN_NORMAL);
        !           106:        }
        !           107:
1.105     nicm      108:        /* Parse option name and index. */
1.115     nicm      109:        name = options_match(argument, &idx, &ambiguous);
1.105     nicm      110:        if (name == NULL) {
1.106     nicm      111:                if (args_has(args, 'q'))
1.115     nicm      112:                        goto out;
1.105     nicm      113:                if (ambiguous)
1.115     nicm      114:                        cmdq_error(item, "ambiguous option: %s", argument);
1.105     nicm      115:                else
1.115     nicm      116:                        cmdq_error(item, "invalid option: %s", argument);
                    117:                goto fail;
1.50      nicm      118:        }
                    119:        if (args->argc < 2)
1.105     nicm      120:                value = NULL;
1.115     nicm      121:        else if (args_has(args, 'F'))
                    122:                value = format_single(item, args->argv[1], c, s, wl, NULL);
1.50      nicm      123:        else
1.115     nicm      124:                value = xstrdup(args->argv[1]);
1.50      nicm      125:
1.105     nicm      126:        /*
                    127:         * Figure out the scope: for user options it comes from the arguments,
                    128:         * otherwise from the option name.
                    129:         */
                    130:        if (*name == '@') {
                    131:                window = (self->entry == &cmd_set_window_option_entry);
                    132:                scope = options_scope_from_flags(args, window, fs, &oo, &cause);
                    133:        } else {
                    134:                if (options_get_only(global_options, name) != NULL)
                    135:                        scope = OPTIONS_TABLE_SERVER;
                    136:                else if (options_get_only(global_s_options, name) != NULL)
                    137:                        scope = OPTIONS_TABLE_SESSION;
                    138:                else if (options_get_only(global_w_options, name) != NULL)
                    139:                        scope = OPTIONS_TABLE_WINDOW;
                    140:                else {
                    141:                        scope = OPTIONS_TABLE_NONE;
1.115     nicm      142:                        xasprintf(&cause, "unknown option: %s", argument);
1.76      nicm      143:                }
1.50      nicm      144:        }
1.105     nicm      145:        if (scope == OPTIONS_TABLE_NONE) {
1.106     nicm      146:                if (args_has(args, 'q'))
1.115     nicm      147:                        goto out;
1.105     nicm      148:                cmdq_error(item, "%s", cause);
                    149:                free(cause);
1.113     nicm      150:                goto fail;
1.50      nicm      151:        }
                    152:
1.105     nicm      153:        /* Which table should this option go into? */
                    154:        if (scope == OPTIONS_TABLE_SERVER)
1.84      nicm      155:                oo = global_options;
1.105     nicm      156:        else if (scope == OPTIONS_TABLE_SESSION) {
                    157:                if (args_has(self->args, 'g'))
                    158:                        oo = global_s_options;
                    159:                else if (s == NULL) {
                    160:                        target = args_get(args, 't');
                    161:                        if (target != NULL)
                    162:                                cmdq_error(item, "no such session: %s", target);
                    163:                        else
                    164:                                cmdq_error(item, "no current session");
1.113     nicm      165:                        goto fail;
1.105     nicm      166:                } else
                    167:                        oo = s->options;
                    168:        } else if (scope == OPTIONS_TABLE_WINDOW) {
1.44      nicm      169:                if (args_has(self->args, 'g'))
1.84      nicm      170:                        oo = global_w_options;
1.94      nicm      171:                else if (wl == NULL) {
                    172:                        target = args_get(args, 't');
1.105     nicm      173:                        if (target != NULL)
                    174:                                cmdq_error(item, "no such window: %s", target);
                    175:                        else
1.101     nicm      176:                                cmdq_error(item, "no current window");
1.113     nicm      177:                        goto fail;
1.94      nicm      178:                } else
1.84      nicm      179:                        oo = wl->window->options;
1.105     nicm      180:        }
                    181:        o = options_get_only(oo, name);
                    182:        parent = options_get(oo, name);
                    183:
                    184:        /* Check that array options and indexes match up. */
1.122     nicm      185:        if (idx != -1 && (*name == '@' || !options_isarray(parent))) {
                    186:                cmdq_error(item, "not an array: %s", argument);
                    187:                goto fail;
1.105     nicm      188:        }
                    189:
                    190:        /* With -o, check this option is not already set. */
                    191:        if (!args_has(args, 'u') && args_has(args, 'o')) {
                    192:                if (idx == -1)
                    193:                        already = (o != NULL);
                    194:                else {
                    195:                        if (o == NULL)
                    196:                                already = 0;
                    197:                        else
                    198:                                already = (options_array_get(o, idx) != NULL);
                    199:                }
                    200:                if (already) {
                    201:                        if (args_has(args, 'q'))
1.115     nicm      202:                                goto out;
                    203:                        cmdq_error(item, "already set: %s", argument);
1.113     nicm      204:                        goto fail;
1.105     nicm      205:                }
1.1       nicm      206:        }
                    207:
1.105     nicm      208:        /* Change the option. */
1.44      nicm      209:        if (args_has(args, 'u')) {
1.105     nicm      210:                if (o == NULL)
1.118     nicm      211:                        goto out;
1.105     nicm      212:                if (idx == -1) {
1.119     nicm      213:                        if (*name == '@')
                    214:                                options_remove(o);
                    215:                        else if (oo == global_options ||
1.105     nicm      216:                            oo == global_s_options ||
                    217:                            oo == global_w_options)
                    218:                                options_default(oo, options_table_entry(o));
                    219:                        else
                    220:                                options_remove(o);
1.125   ! nicm      221:                } else if (options_array_set(o, idx, NULL, 0, &cause) != 0) {
        !           222:                        cmdq_error(item, "%s", cause);
        !           223:                        free(cause);
        !           224:                        goto fail;
        !           225:                }
1.109     nicm      226:        } else if (*name == '@') {
                    227:                if (value == NULL) {
                    228:                        cmdq_error(item, "empty value");
1.113     nicm      229:                        goto fail;
1.109     nicm      230:                }
                    231:                options_set_string(oo, name, append, "%s", value);
1.122     nicm      232:        } else if (idx == -1 && !options_isarray(parent)) {
1.105     nicm      233:                error = cmd_set_option_set(self, item, oo, parent, value);
                    234:                if (error != 0)
1.113     nicm      235:                        goto fail;
1.43      nicm      236:        } else {
1.109     nicm      237:                if (value == NULL) {
                    238:                        cmdq_error(item, "empty value");
1.113     nicm      239:                        goto fail;
1.109     nicm      240:                }
1.105     nicm      241:                if (o == NULL)
                    242:                        o = options_empty(oo, options_table_entry(parent));
1.110     nicm      243:                if (idx == -1) {
                    244:                        if (!append)
                    245:                                options_array_clear(o);
1.125   ! nicm      246:                        if (options_array_assign(o, value, &cause) != 0) {
        !           247:                                cmdq_error(item, "%s", cause);
        !           248:                                free(cause);
        !           249:                                goto fail;
        !           250:                        }
        !           251:                } else if (options_array_set(o, idx, value, append,
        !           252:                    &cause) != 0) {
        !           253:                        cmdq_error(item, "%s", cause);
        !           254:                        free(cause);
1.113     nicm      255:                        goto fail;
1.61      nicm      256:                }
1.55      nicm      257:        }
                    258:
1.103     nicm      259:        /* Update timers and so on for various options. */
1.105     nicm      260:        if (strcmp(name, "automatic-rename") == 0) {
1.72      nicm      261:                RB_FOREACH(w, windows, &windows) {
1.102     nicm      262:                        if (w->active == NULL)
                    263:                                continue;
1.84      nicm      264:                        if (options_get_number(w->options, "automatic-rename"))
1.81      nicm      265:                                w->active->flags |= PANE_CHANGED;
1.55      nicm      266:                }
1.89      nicm      267:        }
1.105     nicm      268:        if (strcmp(name, "key-table") == 0) {
1.115     nicm      269:                TAILQ_FOREACH(loop, &clients, entry)
                    270:                        server_client_set_key_table(loop, NULL);
1.117     nicm      271:        }
                    272:        if (strcmp(name, "user-keys") == 0) {
                    273:                TAILQ_FOREACH(loop, &clients, entry) {
                    274:                        if (loop->tty.flags & TTY_OPENED)
                    275:                                tty_keys_build(&loop->tty);
                    276:                }
1.1       nicm      277:        }
1.123     nicm      278:        if (strcmp(name, "status-fg") == 0 || strcmp(name, "status-bg") == 0) {
                    279:                sy = options_get_style(oo, "status-style");
                    280:                sy->gc.fg = options_get_number(oo, "status-fg");
                    281:                sy->gc.bg = options_get_number(oo, "status-bg");
                    282:        }
                    283:        if (strcmp(name, "status-style") == 0) {
                    284:                sy = options_get_style(oo, "status-style");
                    285:                options_set_number(oo, "status-fg", sy->gc.fg);
                    286:                options_set_number(oo, "status-bg", sy->gc.bg);
                    287:        }
1.105     nicm      288:        if (strcmp(name, "status") == 0 ||
                    289:            strcmp(name, "status-interval") == 0)
1.77      nicm      290:                status_timer_start_all();
1.105     nicm      291:        if (strcmp(name, "monitor-silence") == 0)
1.82      nicm      292:                alerts_reset_all();
1.105     nicm      293:        if (strcmp(name, "window-style") == 0 ||
                    294:            strcmp(name, "window-active-style") == 0) {
1.96      nicm      295:                RB_FOREACH(w, windows, &windows)
                    296:                        w->flags |= WINDOW_STYLECHANGED;
                    297:        }
1.105     nicm      298:        if (strcmp(name, "pane-border-status") == 0) {
1.95      nicm      299:                RB_FOREACH(w, windows, &windows)
1.120     nicm      300:                        layout_fix_panes(w);
1.95      nicm      301:        }
1.116     nicm      302:        RB_FOREACH(s, sessions, &sessions)
1.121     nicm      303:                status_update_cache(s);
1.1       nicm      304:
1.105     nicm      305:        /*
                    306:         * Update sizes and redraw. May not always be necessary but do it
                    307:         * anyway.
                    308:         */
1.1       nicm      309:        recalculate_sizes();
1.115     nicm      310:        TAILQ_FOREACH(loop, &clients, entry) {
                    311:                if (loop->session != NULL)
                    312:                        server_redraw_client(loop);
1.1       nicm      313:        }
                    314:
1.115     nicm      315: out:
                    316:        free(argument);
                    317:        free(value);
1.113     nicm      318:        free(name);
1.57      nicm      319:        return (CMD_RETURN_NORMAL);
1.113     nicm      320:
                    321: fail:
1.115     nicm      322:        free(argument);
                    323:        free(value);
1.113     nicm      324:        free(name);
                    325:        return (CMD_RETURN_ERROR);
1.27      nicm      326: }
1.59      nicm      327:
1.99      nicm      328: static int
1.105     nicm      329: cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo,
1.107     nicm      330:     struct options_entry *parent, const char *value)
1.27      nicm      331: {
1.105     nicm      332:        const struct options_table_entry        *oe;
                    333:        struct args                             *args = self->args;
                    334:        int                                      append = args_has(args, 'a');
1.107     nicm      335:        struct options_entry                    *o;
1.105     nicm      336:        long long                                number;
1.120     nicm      337:        const char                              *errstr, *new;
                    338:        char                                    *old;
1.105     nicm      339:        key_code                                 key;
                    340:
                    341:        oe = options_table_entry(parent);
                    342:        if (value == NULL &&
                    343:            oe->type != OPTIONS_TABLE_FLAG &&
                    344:            oe->type != OPTIONS_TABLE_CHOICE) {
                    345:                cmdq_error(item, "empty value");
1.43      nicm      346:                return (-1);
1.27      nicm      347:        }
1.43      nicm      348:
                    349:        switch (oe->type) {
                    350:        case OPTIONS_TABLE_STRING:
1.120     nicm      351:                old = xstrdup(options_get_string(oo, oe->name));
1.105     nicm      352:                options_set_string(oo, oe->name, append, "%s", value);
1.120     nicm      353:                new = options_get_string(oo, oe->name);
                    354:                if (oe->pattern != NULL && fnmatch(oe->pattern, new, 0) != 0) {
                    355:                        options_set_string(oo, oe->name, 0, "%s", old);
                    356:                        free(old);
                    357:                        cmdq_error(item, "value is invalid: %s", value);
                    358:                        return (-1);
                    359:                }
                    360:                free(old);
1.105     nicm      361:                return (0);
1.43      nicm      362:        case OPTIONS_TABLE_NUMBER:
1.105     nicm      363:                number = strtonum(value, oe->minimum, oe->maximum, &errstr);
                    364:                if (errstr != NULL) {
                    365:                        cmdq_error(item, "value is %s: %s", errstr, value);
                    366:                        return (-1);
                    367:                }
                    368:                options_set_number(oo, oe->name, number);
                    369:                return (0);
1.52      nicm      370:        case OPTIONS_TABLE_KEY:
1.105     nicm      371:                key = key_string_lookup_string(value);
                    372:                if (key == KEYC_UNKNOWN) {
                    373:                        cmdq_error(item, "bad key: %s", value);
                    374:                        return (-1);
                    375:                }
                    376:                options_set_number(oo, oe->name, key);
                    377:                return (0);
1.43      nicm      378:        case OPTIONS_TABLE_COLOUR:
1.105     nicm      379:                if ((number = colour_fromstring(value)) == -1) {
                    380:                        cmdq_error(item, "bad colour: %s", value);
                    381:                        return (-1);
                    382:                }
1.123     nicm      383:                options_set_number(oo, oe->name, number);
1.105     nicm      384:                return (0);
1.43      nicm      385:        case OPTIONS_TABLE_FLAG:
1.105     nicm      386:                return (cmd_set_option_flag(item, oe, oo, value));
1.43      nicm      387:        case OPTIONS_TABLE_CHOICE:
1.105     nicm      388:                return (cmd_set_option_choice(item, oe, oo, value));
1.64      nicm      389:        case OPTIONS_TABLE_STYLE:
1.105     nicm      390:                o = options_set_style(oo, oe->name, append, value);
                    391:                if (o == NULL) {
                    392:                        cmdq_error(item, "bad style: %s", value);
                    393:                        return (-1);
                    394:                }
                    395:                return (0);
1.125   ! nicm      396:        case OPTIONS_TABLE_COMMAND:
        !           397:                break;
1.43      nicm      398:        }
1.105     nicm      399:        return (-1);
1.43      nicm      400: }
                    401:
1.105     nicm      402: static int
                    403: cmd_set_option_flag(struct cmdq_item *item,
1.69      nicm      404:     const struct options_table_entry *oe, struct options *oo,
                    405:     const char *value)
1.27      nicm      406: {
1.44      nicm      407:        int     flag;
1.27      nicm      408:
1.44      nicm      409:        if (value == NULL || *value == '\0')
1.43      nicm      410:                flag = !options_get_number(oo, oe->name);
1.105     nicm      411:        else if (strcmp(value, "1") == 0 ||
                    412:            strcasecmp(value, "on") == 0 ||
                    413:            strcasecmp(value, "yes") == 0)
                    414:                flag = 1;
                    415:        else if (strcmp(value, "0") == 0 ||
                    416:            strcasecmp(value, "off") == 0 ||
                    417:            strcasecmp(value, "no") == 0)
                    418:                flag = 0;
1.27      nicm      419:        else {
1.105     nicm      420:                cmdq_error(item, "bad value: %s", value);
                    421:                return (-1);
1.27      nicm      422:        }
1.105     nicm      423:        options_set_number(oo, oe->name, flag);
                    424:        return (0);
1.27      nicm      425: }
                    426:
1.105     nicm      427: static int
                    428: cmd_set_option_choice(struct cmdq_item *item,
1.60      nicm      429:     const struct options_table_entry *oe, struct options *oo,
                    430:     const char *value)
1.27      nicm      431: {
1.105     nicm      432:        const char      **cp;
1.44      nicm      433:        int               n, choice = -1;
1.27      nicm      434:
1.73      nicm      435:        if (value == NULL) {
                    436:                choice = options_get_number(oo, oe->name);
                    437:                if (choice < 2)
                    438:                        choice = !choice;
                    439:        } else {
                    440:                n = 0;
1.105     nicm      441:                for (cp = oe->choices; *cp != NULL; cp++) {
                    442:                        if (strcmp(*cp, value) == 0)
                    443:                                choice = n;
1.73      nicm      444:                        n++;
                    445:                }
                    446:                if (choice == -1) {
1.101     nicm      447:                        cmdq_error(item, "unknown value: %s", value);
1.105     nicm      448:                        return (-1);
1.27      nicm      449:                }
                    450:        }
1.105     nicm      451:        options_set_number(oo, oe->name, choice);
                    452:        return (0);
1.1       nicm      453: }