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

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