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

Annotation of src/usr.bin/tmux/cmd-show-options.c, Revision 1.68

1.68    ! nicm        1: /* $OpenBSD: cmd-show-options.c,v 1.67 2021/08/21 10:22:39 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.30      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>
1.35      nicm       23: #include <vis.h>
1.1       nicm       24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Show options.
                     29:  */
                     30:
1.34      nicm       31: static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       32:
1.46      nicm       33: static void            cmd_show_options_print(struct cmd *, struct cmdq_item *,
1.54      nicm       34:                            struct options_entry *, int, int);
1.34      nicm       35: static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *,
1.56      nicm       36:                            int, struct options *);
1.18      nicm       37:
1.1       nicm       38: const struct cmd_entry cmd_show_options_entry = {
1.28      nicm       39:        .name = "show-options",
                     40:        .alias = "show",
                     41:
1.67      nicm       42:        .args = { "AgHpqst:vw", 0, 1, NULL },
1.56      nicm       43:        .usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]",
1.28      nicm       44:
1.56      nicm       45:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.29      nicm       46:
1.33      nicm       47:        .flags = CMD_AFTERHOOK,
1.28      nicm       48:        .exec = cmd_show_options_exec
1.1       nicm       49: };
                     50:
1.13      nicm       51: const struct cmd_entry cmd_show_window_options_entry = {
1.28      nicm       52:        .name = "show-window-options",
                     53:        .alias = "showw",
                     54:
1.67      nicm       55:        .args = { "gvt:", 0, 1, NULL },
1.28      nicm       56:        .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
                     57:
1.41      nicm       58:        .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.29      nicm       59:
1.33      nicm       60:        .flags = CMD_AFTERHOOK,
1.28      nicm       61:        .exec = cmd_show_options_exec
1.13      nicm       62: };
                     63:
1.48      nicm       64: const struct cmd_entry cmd_show_hooks_entry = {
                     65:        .name = "show-hooks",
                     66:        .alias = NULL,
                     67:
1.67      nicm       68:        .args = { "gpt:w", 0, 1, NULL },
1.59      nicm       69:        .usage = "[-gpw] " CMD_TARGET_PANE_USAGE,
1.48      nicm       70:
1.59      nicm       71:        .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
1.48      nicm       72:
                     73:        .flags = CMD_AFTERHOOK,
                     74:        .exec = cmd_show_options_exec
                     75: };
                     76:
1.32      nicm       77: static enum cmd_retval
1.34      nicm       78: cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       79: {
1.60      nicm       80:        struct args                     *args = cmd_get_args(self);
1.61      nicm       81:        struct cmd_find_state           *target = cmdq_get_target(item);
1.31      nicm       82:        struct options                  *oo;
1.46      nicm       83:        char                            *argument, *name = NULL, *cause;
1.56      nicm       84:        int                              window, idx, ambiguous, parent, scope;
1.46      nicm       85:        struct options_entry            *o;
1.1       nicm       86:
1.60      nicm       87:        window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
1.55      nicm       88:
1.66      nicm       89:        if (args_count(args) == 0) {
1.61      nicm       90:                scope = options_scope_from_flags(args, window, target, &oo,
                     91:                    &cause);
1.52      nicm       92:                if (scope == OPTIONS_TABLE_NONE) {
                     93:                        if (args_has(args, 'q'))
                     94:                                return (CMD_RETURN_NORMAL);
                     95:                        cmdq_error(item, "%s", cause);
                     96:                        free(cause);
                     97:                        return (CMD_RETURN_ERROR);
                     98:                }
1.54      nicm       99:                return (cmd_show_options_all(self, item, scope, oo));
1.51      nicm      100:        }
1.66      nicm      101:        argument = format_single_from_target(item, args_string(args, 0));
1.46      nicm      102:
                    103:        name = options_match(argument, &idx, &ambiguous);
                    104:        if (name == NULL) {
                    105:                if (args_has(args, 'q'))
                    106:                        goto fail;
                    107:                if (ambiguous)
                    108:                        cmdq_error(item, "ambiguous option: %s", argument);
                    109:                else
                    110:                        cmdq_error(item, "invalid option: %s", argument);
                    111:                goto fail;
                    112:        }
1.61      nicm      113:        scope = options_scope_from_name(args, window, name, target, &oo,
                    114:            &cause);
1.35      nicm      115:        if (scope == OPTIONS_TABLE_NONE) {
1.46      nicm      116:                if (args_has(args, 'q'))
                    117:                        goto fail;
1.35      nicm      118:                cmdq_error(item, "%s", cause);
                    119:                free(cause);
1.46      nicm      120:                goto fail;
1.1       nicm      121:        }
1.46      nicm      122:        o = options_get_only(oo, name);
1.54      nicm      123:        if (args_has(args, 'A') && o == NULL) {
                    124:                o = options_get(oo, name);
                    125:                parent = 1;
                    126:        } else
                    127:                parent = 0;
1.46      nicm      128:        if (o != NULL)
1.54      nicm      129:                cmd_show_options_print(self, item, o, idx, parent);
1.68    ! nicm      130:        else if (*name == '@') {
        !           131:                if (args_has(args, 'q'))
        !           132:                        goto fail;
        !           133:                cmdq_error(item, "invalid option: %s", argument);
        !           134:                goto fail;
        !           135:        }
1.1       nicm      136:
1.46      nicm      137:        free(name);
                    138:        free(argument);
                    139:        return (CMD_RETURN_NORMAL);
                    140:
                    141: fail:
                    142:        free(name);
                    143:        free(argument);
                    144:        return (CMD_RETURN_ERROR);
1.35      nicm      145: }
                    146:
                    147: static void
                    148: cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
1.54      nicm      149:     struct options_entry *o, int idx, int parent)
1.35      nicm      150: {
1.60      nicm      151:        struct args                     *args = cmd_get_args(self);
1.43      nicm      152:        struct options_array_item       *a;
1.48      nicm      153:        const char                      *name = options_name(o);
                    154:        char                            *value, *tmp = NULL, *escaped;
1.35      nicm      155:
                    156:        if (idx != -1) {
1.48      nicm      157:                xasprintf(&tmp, "%s[%d]", name, idx);
1.35      nicm      158:                name = tmp;
                    159:        } else {
1.64      nicm      160:                if (options_is_array(o)) {
1.43      nicm      161:                        a = options_array_first(o);
1.48      nicm      162:                        if (a == NULL) {
1.60      nicm      163:                                if (!args_has(args, 'v'))
1.48      nicm      164:                                        cmdq_print(item, "%s", name);
                    165:                                return;
                    166:                        }
1.43      nicm      167:                        while (a != NULL) {
                    168:                                idx = options_array_item_index(a);
1.54      nicm      169:                                cmd_show_options_print(self, item, o, idx,
                    170:                                    parent);
1.43      nicm      171:                                a = options_array_next(a);
1.39      nicm      172:                        }
                    173:                        return;
                    174:                }
1.35      nicm      175:        }
                    176:
1.64      nicm      177:        value = options_to_string(o, idx, 0);
1.60      nicm      178:        if (args_has(args, 'v'))
1.35      nicm      179:                cmdq_print(item, "%s", value);
1.64      nicm      180:        else if (options_is_string(o)) {
1.53      nicm      181:                escaped = args_escape(value);
1.54      nicm      182:                if (parent)
                    183:                        cmdq_print(item, "%s* %s", name, escaped);
                    184:                else
                    185:                        cmdq_print(item, "%s %s", name, escaped);
1.35      nicm      186:                free(escaped);
1.54      nicm      187:        } else {
                    188:                if (parent)
                    189:                        cmdq_print(item, "%s* %s", name, value);
                    190:                else
                    191:                        cmdq_print(item, "%s %s", name, value);
                    192:        }
1.47      nicm      193:        free(value);
1.35      nicm      194:
                    195:        free(tmp);
1.18      nicm      196: }
                    197:
1.32      nicm      198: static enum cmd_retval
1.56      nicm      199: cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
                    200:     struct options *oo)
1.18      nicm      201: {
1.60      nicm      202:        struct args                             *args = cmd_get_args(self);
1.54      nicm      203:        const struct options_table_entry        *oe;
1.49      nicm      204:        struct options_entry                    *o;
                    205:        struct options_array_item               *a;
1.56      nicm      206:        const char                              *name;
1.49      nicm      207:        u_int                                    idx;
1.54      nicm      208:        int                                      parent;
                    209:
1.65      nicm      210:        if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
                    211:                o = options_first(oo);
                    212:                while (o != NULL) {
                    213:                        if (options_table_entry(o) == NULL)
                    214:                                cmd_show_options_print(self, item, o, -1, 0);
                    215:                        o = options_next(o);
                    216:                }
1.57      nicm      217:        }
1.54      nicm      218:        for (oe = options_table; oe->name != NULL; oe++) {
1.56      nicm      219:                if (~oe->scope & scope)
1.54      nicm      220:                        continue;
1.18      nicm      221:
1.60      nicm      222:                if ((cmd_get_entry(self) != &cmd_show_hooks_entry &&
                    223:                    !args_has(args, 'H') &&
1.49      nicm      224:                    (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
1.60      nicm      225:                    (cmd_get_entry(self) == &cmd_show_hooks_entry &&
1.58      nicm      226:                    (~oe->flags & OPTIONS_TABLE_IS_HOOK)))
1.48      nicm      227:                        continue;
1.54      nicm      228:
                    229:                o = options_get_only(oo, oe->name);
                    230:                if (o == NULL) {
1.60      nicm      231:                        if (!args_has(args, 'A'))
1.54      nicm      232:                                continue;
                    233:                        o = options_get(oo, oe->name);
                    234:                        if (o == NULL)
                    235:                                continue;
                    236:                        parent = 1;
                    237:                } else
                    238:                        parent = 0;
                    239:
1.64      nicm      240:                if (!options_is_array(o))
1.54      nicm      241:                        cmd_show_options_print(self, item, o, -1, parent);
1.48      nicm      242:                else if ((a = options_array_first(o)) == NULL) {
1.60      nicm      243:                        if (!args_has(args, 'v')) {
1.56      nicm      244:                                name = options_name(o);
1.54      nicm      245:                                if (parent)
1.56      nicm      246:                                        cmdq_print(item, "%s*", name);
1.54      nicm      247:                                else
1.56      nicm      248:                                        cmdq_print(item, "%s", name);
1.54      nicm      249:                        }
1.48      nicm      250:                } else {
1.43      nicm      251:                        while (a != NULL) {
                    252:                                idx = options_array_item_index(a);
1.56      nicm      253:                                cmd_show_options_print(self, item, o, idx,
                    254:                                    parent);
1.43      nicm      255:                                a = options_array_next(a);
1.35      nicm      256:                        }
1.15      nicm      257:                }
1.18      nicm      258:        }
1.16      nicm      259:        return (CMD_RETURN_NORMAL);
1.1       nicm      260: }