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

1.136   ! nicm        1: /* $OpenBSD: cmd-set-option.c,v 1.135 2020/05/16 16:02:24 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.1       nicm       32: const struct cmd_entry cmd_set_option_entry = {
1.91      nicm       33:        .name = "set-option",
                     34:        .alias = "set",
                     35:
1.127     nicm       36:        .args = { "aFgopqst:uw", 1, 2 },
                     37:        .usage = "[-aFgopqsuw] " CMD_TARGET_PANE_USAGE " option [value]",
1.91      nicm       38:
1.127     nicm       39:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.92      nicm       40:
1.100     nicm       41:        .flags = CMD_AFTERHOOK,
1.91      nicm       42:        .exec = cmd_set_option_exec
1.1       nicm       43: };
                     44:
1.46      nicm       45: const struct cmd_entry cmd_set_window_option_entry = {
1.91      nicm       46:        .name = "set-window-option",
                     47:        .alias = "setw",
                     48:
1.115     nicm       49:        .args = { "aFgoqt:u", 1, 2 },
                     50:        .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
1.91      nicm       51:
1.114     nicm       52:        .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.92      nicm       53:
1.100     nicm       54:        .flags = CMD_AFTERHOOK,
1.91      nicm       55:        .exec = cmd_set_option_exec
1.46      nicm       56: };
                     57:
1.125     nicm       58: const struct cmd_entry cmd_set_hook_entry = {
                     59:        .name = "set-hook",
                     60:        .alias = NULL,
                     61:
1.129     nicm       62:        .args = { "agpRt:uw", 1, 2 },
                     63:        .usage = "[-agpRuw] " CMD_TARGET_PANE_USAGE " hook [command]",
1.125     nicm       64:
1.129     nicm       65:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.125     nicm       66:
                     67:        .flags = CMD_AFTERHOOK,
                     68:        .exec = cmd_set_option_exec
                     69: };
                     70:
1.99      nicm       71: static enum cmd_retval
1.101     nicm       72: cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       73: {
1.130     nicm       74:        struct args                     *args = cmd_get_args(self);
1.109     nicm       75:        int                              append = args_has(args, 'a');
1.131     nicm       76:        struct cmd_find_state           *target = cmdq_get_target(item);
1.105     nicm       77:        struct options                  *oo;
1.107     nicm       78:        struct options_entry            *parent, *o;
1.115     nicm       79:        char                            *name, *argument, *value = NULL, *cause;
1.105     nicm       80:        int                              window, idx, already, error, ambiguous;
1.127     nicm       81:        int                              scope;
1.115     nicm       82:
1.130     nicm       83:        window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
1.126     nicm       84:
1.115     nicm       85:        /* Expand argument. */
1.132     nicm       86:        argument = format_single_from_target(item, args->argv[0]);
1.105     nicm       87:
1.126     nicm       88:        /* If set-hook -R, fire the hook straight away. */
1.130     nicm       89:        if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
1.125     nicm       90:                notify_hook(item, argument);
1.126     nicm       91:                free(argument);
1.125     nicm       92:                return (CMD_RETURN_NORMAL);
                     93:        }
                     94:
1.105     nicm       95:        /* Parse option name and index. */
1.115     nicm       96:        name = options_match(argument, &idx, &ambiguous);
1.105     nicm       97:        if (name == NULL) {
1.106     nicm       98:                if (args_has(args, 'q'))
1.115     nicm       99:                        goto out;
1.105     nicm      100:                if (ambiguous)
1.115     nicm      101:                        cmdq_error(item, "ambiguous option: %s", argument);
1.105     nicm      102:                else
1.115     nicm      103:                        cmdq_error(item, "invalid option: %s", argument);
                    104:                goto fail;
1.50      nicm      105:        }
                    106:        if (args->argc < 2)
1.105     nicm      107:                value = NULL;
1.115     nicm      108:        else if (args_has(args, 'F'))
1.132     nicm      109:                value = format_single_from_target(item, args->argv[1]);
1.50      nicm      110:        else
1.115     nicm      111:                value = xstrdup(args->argv[1]);
1.50      nicm      112:
1.126     nicm      113:        /* Get the scope and table for the option .*/
1.131     nicm      114:        scope = options_scope_from_name(args, window, name, target, &oo,
                    115:            &cause);
1.105     nicm      116:        if (scope == OPTIONS_TABLE_NONE) {
1.106     nicm      117:                if (args_has(args, 'q'))
1.115     nicm      118:                        goto out;
1.105     nicm      119:                cmdq_error(item, "%s", cause);
                    120:                free(cause);
1.113     nicm      121:                goto fail;
1.105     nicm      122:        }
                    123:        o = options_get_only(oo, name);
                    124:        parent = options_get(oo, name);
                    125:
                    126:        /* Check that array options and indexes match up. */
1.135     nicm      127:        if (idx != -1 && (*name == '@' || !options_is_array(parent))) {
1.122     nicm      128:                cmdq_error(item, "not an array: %s", argument);
                    129:                goto fail;
1.105     nicm      130:        }
                    131:
                    132:        /* With -o, check this option is not already set. */
                    133:        if (!args_has(args, 'u') && args_has(args, 'o')) {
                    134:                if (idx == -1)
                    135:                        already = (o != NULL);
                    136:                else {
                    137:                        if (o == NULL)
                    138:                                already = 0;
                    139:                        else
                    140:                                already = (options_array_get(o, idx) != NULL);
                    141:                }
                    142:                if (already) {
                    143:                        if (args_has(args, 'q'))
1.115     nicm      144:                                goto out;
                    145:                        cmdq_error(item, "already set: %s", argument);
1.113     nicm      146:                        goto fail;
1.105     nicm      147:                }
1.1       nicm      148:        }
                    149:
1.105     nicm      150:        /* Change the option. */
1.44      nicm      151:        if (args_has(args, 'u')) {
1.105     nicm      152:                if (o == NULL)
1.118     nicm      153:                        goto out;
1.105     nicm      154:                if (idx == -1) {
1.119     nicm      155:                        if (*name == '@')
                    156:                                options_remove(o);
                    157:                        else if (oo == global_options ||
1.105     nicm      158:                            oo == global_s_options ||
                    159:                            oo == global_w_options)
                    160:                                options_default(oo, options_table_entry(o));
                    161:                        else
                    162:                                options_remove(o);
1.125     nicm      163:                } else if (options_array_set(o, idx, NULL, 0, &cause) != 0) {
                    164:                        cmdq_error(item, "%s", cause);
                    165:                        free(cause);
                    166:                        goto fail;
                    167:                }
1.109     nicm      168:        } else if (*name == '@') {
                    169:                if (value == NULL) {
                    170:                        cmdq_error(item, "empty value");
1.113     nicm      171:                        goto fail;
1.109     nicm      172:                }
                    173:                options_set_string(oo, name, append, "%s", value);
1.135     nicm      174:        } else if (idx == -1 && !options_is_array(parent)) {
                    175:                error = options_from_string(oo, options_table_entry(parent),
                    176:                    options_table_entry(parent)->name, value,
                    177:                    args_has(args, 'a'), &cause);
                    178:                if (error != 0) {
                    179:                        cmdq_error(item, "%s", cause);
                    180:                        free(cause);
1.113     nicm      181:                        goto fail;
1.135     nicm      182:                }
1.43      nicm      183:        } else {
1.109     nicm      184:                if (value == NULL) {
                    185:                        cmdq_error(item, "empty value");
1.113     nicm      186:                        goto fail;
1.109     nicm      187:                }
1.105     nicm      188:                if (o == NULL)
                    189:                        o = options_empty(oo, options_table_entry(parent));
1.110     nicm      190:                if (idx == -1) {
                    191:                        if (!append)
                    192:                                options_array_clear(o);
1.125     nicm      193:                        if (options_array_assign(o, value, &cause) != 0) {
                    194:                                cmdq_error(item, "%s", cause);
                    195:                                free(cause);
                    196:                                goto fail;
                    197:                        }
                    198:                } else if (options_array_set(o, idx, value, append,
                    199:                    &cause) != 0) {
                    200:                        cmdq_error(item, "%s", cause);
                    201:                        free(cause);
1.113     nicm      202:                        goto fail;
1.61      nicm      203:                }
1.55      nicm      204:        }
                    205:
1.135     nicm      206:        options_push_changes(name);
1.1       nicm      207:
1.115     nicm      208: out:
                    209:        free(argument);
                    210:        free(value);
1.113     nicm      211:        free(name);
1.57      nicm      212:        return (CMD_RETURN_NORMAL);
1.113     nicm      213:
                    214: fail:
1.115     nicm      215:        free(argument);
                    216:        free(value);
1.113     nicm      217:        free(name);
                    218:        return (CMD_RETURN_ERROR);
1.1       nicm      219: }