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

1.54    ! nicm        1: /* $OpenBSD: cmd-show-options.c,v 1.53 2019/05/23 18:33:53 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.54    ! nicm       36:                            enum options_table_scope, 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.54    ! nicm       42:        .args = { "AgHqst:vw", 0, 1 },
        !            43:        .usage = "[-AgHqsvw] [-t target-session|target-window] [option]",
1.28      nicm       44:
1.41      nicm       45:        .target = { 't', CMD_FIND_WINDOW, 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:
                     55:        .args = { "gvt:", 0, 1 },
                     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:
                     68:        .args = { "gt:", 0, 1 },
                     69:        .usage = "[-g] " CMD_TARGET_SESSION_USAGE,
                     70:
                     71:        .target = { 't', CMD_FIND_SESSION, 0 },
                     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.31      nicm       80:        struct args                     *args = self->args;
1.41      nicm       81:        struct cmd_find_state           *fs = &item->target;
1.46      nicm       82:        struct client                   *c = cmd_find_client(item, NULL, 1);
                     83:        struct session                  *s = item->target.s;
                     84:        struct winlink                  *wl = item->target.wl;
1.31      nicm       85:        struct options                  *oo;
                     86:        enum options_table_scope         scope;
1.46      nicm       87:        char                            *argument, *name = NULL, *cause;
                     88:        const char                      *target;
1.54    ! nicm       89:        int                              window, idx, ambiguous, parent;
1.46      nicm       90:        struct options_entry            *o;
1.1       nicm       91:
1.35      nicm       92:        window = (self->entry == &cmd_show_window_options_entry);
1.51      nicm       93:        if (args->argc == 0) {
1.52      nicm       94:                scope = options_scope_from_flags(args, window, fs, &oo, &cause);
                     95:                if (scope == OPTIONS_TABLE_NONE) {
                     96:                        if (args_has(args, 'q'))
                     97:                                return (CMD_RETURN_NORMAL);
                     98:                        cmdq_error(item, "%s", cause);
                     99:                        free(cause);
                    100:                        return (CMD_RETURN_ERROR);
                    101:                }
1.54    ! nicm      102:                return (cmd_show_options_all(self, item, scope, oo));
1.51      nicm      103:        }
1.46      nicm      104:        argument = format_single(item, args->argv[0], c, s, wl, NULL);
                    105:
                    106:        name = options_match(argument, &idx, &ambiguous);
                    107:        if (name == NULL) {
                    108:                if (args_has(args, 'q'))
                    109:                        goto fail;
                    110:                if (ambiguous)
                    111:                        cmdq_error(item, "ambiguous option: %s", argument);
                    112:                else
                    113:                        cmdq_error(item, "invalid option: %s", argument);
                    114:                goto fail;
                    115:        }
                    116:        if (*name == '@')
                    117:                scope = options_scope_from_flags(args, window, fs, &oo, &cause);
                    118:        else {
                    119:                if (options_get_only(global_options, name) != NULL)
                    120:                        scope = OPTIONS_TABLE_SERVER;
                    121:                else if (options_get_only(global_s_options, name) != NULL)
                    122:                        scope = OPTIONS_TABLE_SESSION;
                    123:                else if (options_get_only(global_w_options, name) != NULL)
                    124:                        scope = OPTIONS_TABLE_WINDOW;
                    125:                else {
                    126:                        scope = OPTIONS_TABLE_NONE;
                    127:                        xasprintf(&cause, "unknown option: %s", argument);
                    128:                }
                    129:                if (scope == OPTIONS_TABLE_SERVER)
                    130:                        oo = global_options;
                    131:                else if (scope == OPTIONS_TABLE_SESSION) {
                    132:                        if (args_has(self->args, 'g'))
                    133:                                oo = global_s_options;
                    134:                        else if (s == NULL) {
                    135:                                target = args_get(args, 't');
                    136:                                if (target != NULL) {
                    137:                                        cmdq_error(item, "no such session: %s",
                    138:                                            target);
                    139:                                } else
                    140:                                        cmdq_error(item, "no current session");
                    141:                                goto fail;
                    142:                        } else
                    143:                                oo = s->options;
                    144:                } else if (scope == OPTIONS_TABLE_WINDOW) {
                    145:                        if (args_has(self->args, 'g'))
                    146:                                oo = global_w_options;
                    147:                        else if (wl == NULL) {
                    148:                                target = args_get(args, 't');
                    149:                                if (target != NULL) {
                    150:                                        cmdq_error(item, "no such window: %s",
                    151:                                            target);
                    152:                                } else
                    153:                                        cmdq_error(item, "no current window");
                    154:                                goto fail;
                    155:                        } else
                    156:                                oo = wl->window->options;
                    157:                }
                    158:        }
1.35      nicm      159:        if (scope == OPTIONS_TABLE_NONE) {
1.46      nicm      160:                if (args_has(args, 'q'))
                    161:                        goto fail;
1.35      nicm      162:                cmdq_error(item, "%s", cause);
                    163:                free(cause);
1.46      nicm      164:                goto fail;
1.1       nicm      165:        }
1.46      nicm      166:        o = options_get_only(oo, name);
1.54    ! nicm      167:        if (args_has(args, 'A') && o == NULL) {
        !           168:                o = options_get(oo, name);
        !           169:                parent = 1;
        !           170:        } else
        !           171:                parent = 0;
1.46      nicm      172:        if (o != NULL)
1.54    ! nicm      173:                cmd_show_options_print(self, item, o, idx, parent);
1.1       nicm      174:
1.46      nicm      175:        free(name);
                    176:        free(argument);
                    177:        return (CMD_RETURN_NORMAL);
                    178:
                    179: fail:
                    180:        free(name);
                    181:        free(argument);
                    182:        return (CMD_RETURN_ERROR);
1.35      nicm      183: }
                    184:
                    185: static void
                    186: cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
1.54    ! nicm      187:     struct options_entry *o, int idx, int parent)
1.35      nicm      188: {
1.43      nicm      189:        struct options_array_item       *a;
1.48      nicm      190:        const char                      *name = options_name(o);
                    191:        char                            *value, *tmp = NULL, *escaped;
1.35      nicm      192:
                    193:        if (idx != -1) {
1.48      nicm      194:                xasprintf(&tmp, "%s[%d]", name, idx);
1.35      nicm      195:                name = tmp;
                    196:        } else {
1.43      nicm      197:                if (options_isarray(o)) {
                    198:                        a = options_array_first(o);
1.48      nicm      199:                        if (a == NULL) {
                    200:                                if (!args_has(self->args, 'v'))
                    201:                                        cmdq_print(item, "%s", name);
                    202:                                return;
                    203:                        }
1.43      nicm      204:                        while (a != NULL) {
                    205:                                idx = options_array_item_index(a);
1.54    ! nicm      206:                                cmd_show_options_print(self, item, o, idx,
        !           207:                                    parent);
1.43      nicm      208:                                a = options_array_next(a);
1.39      nicm      209:                        }
                    210:                        return;
                    211:                }
1.35      nicm      212:        }
                    213:
1.40      nicm      214:        value = options_tostring(o, idx, 0);
1.35      nicm      215:        if (args_has(self->args, 'v'))
                    216:                cmdq_print(item, "%s", value);
                    217:        else if (options_isstring(o)) {
1.53      nicm      218:                escaped = args_escape(value);
1.54    ! nicm      219:                if (parent)
        !           220:                        cmdq_print(item, "%s* %s", name, escaped);
        !           221:                else
        !           222:                        cmdq_print(item, "%s %s", name, escaped);
1.35      nicm      223:                free(escaped);
1.54    ! nicm      224:        } else {
        !           225:                if (parent)
        !           226:                        cmdq_print(item, "%s* %s", name, value);
        !           227:                else
        !           228:                        cmdq_print(item, "%s %s", name, value);
        !           229:        }
1.47      nicm      230:        free(value);
1.35      nicm      231:
                    232:        free(tmp);
1.18      nicm      233: }
                    234:
1.32      nicm      235: static enum cmd_retval
1.34      nicm      236: cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
1.54    ! nicm      237:     enum options_table_scope scope, struct options *oo)
1.18      nicm      238: {
1.54    ! nicm      239:        const struct options_table_entry        *oe;
1.49      nicm      240:        struct options_entry                    *o;
                    241:        struct options_array_item               *a;
                    242:        u_int                                    idx;
1.54    ! nicm      243:        int                                      parent;
        !           244:
        !           245:        for (oe = options_table; oe->name != NULL; oe++) {
        !           246:                if (oe->scope != scope)
        !           247:                        continue;
1.18      nicm      248:
1.48      nicm      249:                if ((self->entry != &cmd_show_hooks_entry &&
                    250:                    !args_has(self->args, 'H') &&
1.49      nicm      251:                    oe != NULL &&
                    252:                    (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
1.48      nicm      253:                    (self->entry == &cmd_show_hooks_entry &&
1.49      nicm      254:                    (oe == NULL ||
1.54    ! nicm      255:                    (~oe->flags & OPTIONS_TABLE_IS_HOOK))))
1.48      nicm      256:                        continue;
1.54    ! nicm      257:
        !           258:                o = options_get_only(oo, oe->name);
        !           259:                if (o == NULL) {
        !           260:                        if (!args_has(self->args, 'A'))
        !           261:                                continue;
        !           262:                        o = options_get(oo, oe->name);
        !           263:                        if (o == NULL)
        !           264:                                continue;
        !           265:                        parent = 1;
        !           266:                } else
        !           267:                        parent = 0;
        !           268:
1.43      nicm      269:                if (!options_isarray(o))
1.54    ! nicm      270:                        cmd_show_options_print(self, item, o, -1, parent);
1.48      nicm      271:                else if ((a = options_array_first(o)) == NULL) {
1.54    ! nicm      272:                        if (!args_has(self->args, 'v')) {
        !           273:                                if (parent)
        !           274:                                        cmdq_print(item, "%s*", options_name(o));
        !           275:                                else
        !           276:                                        cmdq_print(item, "%s", options_name(o));
        !           277:                        }
1.48      nicm      278:                } else {
1.43      nicm      279:                        while (a != NULL) {
                    280:                                idx = options_array_item_index(a);
1.54    ! nicm      281:                                cmd_show_options_print(self, item, o, idx, parent);
1.43      nicm      282:                                a = options_array_next(a);
1.35      nicm      283:                        }
1.15      nicm      284:                }
1.18      nicm      285:        }
1.16      nicm      286:        return (CMD_RETURN_NORMAL);
1.1       nicm      287: }