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

1.137   ! nicm        1: /* $OpenBSD: cmd-set-option.c,v 1.136 2020/05/16 16:50: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:
                     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.137   ! nicm      154:                if (options_remove_or_default(o, idx, &cause) != 0) {
1.125     nicm      155:                        cmdq_error(item, "%s", cause);
                    156:                        free(cause);
                    157:                        goto fail;
                    158:                }
1.109     nicm      159:        } else if (*name == '@') {
                    160:                if (value == NULL) {
                    161:                        cmdq_error(item, "empty value");
1.113     nicm      162:                        goto fail;
1.109     nicm      163:                }
                    164:                options_set_string(oo, name, append, "%s", value);
1.135     nicm      165:        } else if (idx == -1 && !options_is_array(parent)) {
                    166:                error = options_from_string(oo, options_table_entry(parent),
                    167:                    options_table_entry(parent)->name, value,
                    168:                    args_has(args, 'a'), &cause);
                    169:                if (error != 0) {
                    170:                        cmdq_error(item, "%s", cause);
                    171:                        free(cause);
1.113     nicm      172:                        goto fail;
1.135     nicm      173:                }
1.43      nicm      174:        } else {
1.109     nicm      175:                if (value == NULL) {
                    176:                        cmdq_error(item, "empty value");
1.113     nicm      177:                        goto fail;
1.109     nicm      178:                }
1.105     nicm      179:                if (o == NULL)
                    180:                        o = options_empty(oo, options_table_entry(parent));
1.110     nicm      181:                if (idx == -1) {
                    182:                        if (!append)
                    183:                                options_array_clear(o);
1.125     nicm      184:                        if (options_array_assign(o, value, &cause) != 0) {
                    185:                                cmdq_error(item, "%s", cause);
                    186:                                free(cause);
                    187:                                goto fail;
                    188:                        }
                    189:                } else if (options_array_set(o, idx, value, append,
                    190:                    &cause) != 0) {
                    191:                        cmdq_error(item, "%s", cause);
                    192:                        free(cause);
1.113     nicm      193:                        goto fail;
1.61      nicm      194:                }
1.55      nicm      195:        }
                    196:
1.135     nicm      197:        options_push_changes(name);
1.1       nicm      198:
1.115     nicm      199: out:
                    200:        free(argument);
                    201:        free(value);
1.113     nicm      202:        free(name);
1.57      nicm      203:        return (CMD_RETURN_NORMAL);
1.113     nicm      204:
                    205: fail:
1.115     nicm      206:        free(argument);
                    207:        free(value);
1.113     nicm      208:        free(name);
                    209:        return (CMD_RETURN_ERROR);
1.1       nicm      210: }