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

1.26    ! nicm        1: /* $OpenBSD: cmd-show-options.c,v 1.25 2015/11/20 12:01:19 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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:  * Show options.
                     28:  */
                     29:
1.19      nicm       30: enum cmd_retval         cmd_show_options_exec(struct cmd *, struct cmd_q *);
1.1       nicm       31:
1.19      nicm       32: enum cmd_retval        cmd_show_options_one(struct cmd *, struct cmd_q *,
1.20      nicm       33:                    struct options *, int);
1.19      nicm       34: enum cmd_retval cmd_show_options_all(struct cmd *, struct cmd_q *,
1.25      nicm       35:                    struct options *, enum options_table_scope);
1.18      nicm       36:
1.1       nicm       37: const struct cmd_entry cmd_show_options_entry = {
                     38:        "show-options", "show",
1.20      nicm       39:        "gqst:vw", 0, 1,
                     40:        "[-gqsvw] [-t target-session|target-window] [option]",
1.26    ! nicm       41:        CMD_WINDOW_T,
1.12      nicm       42:        cmd_show_options_exec
1.1       nicm       43: };
                     44:
1.13      nicm       45: const struct cmd_entry cmd_show_window_options_entry = {
                     46:        "show-window-options", "showw",
1.17      nicm       47:        "gvt:", 0, 1,
                     48:        "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
1.26    ! nicm       49:        CMD_WINDOW_T,
1.13      nicm       50:        cmd_show_options_exec
                     51: };
                     52:
1.16      nicm       53: enum cmd_retval
1.19      nicm       54: cmd_show_options_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       55: {
1.26    ! nicm       56:        struct args                             *args = self->args;
        !            57:        struct session                          *s = cmdq->state.tflag.s;
        !            58:        struct winlink                          *wl = cmdq->state.tflag.wl;
        !            59:        struct options                          *oo;
        !            60:        enum options_table_scope                 scope;
        !            61:        int                                      quiet;
1.1       nicm       62:
1.12      nicm       63:        if (args_has(self->args, 's')) {
1.24      nicm       64:                oo = global_options;
1.25      nicm       65:                scope = OPTIONS_TABLE_SERVER;
1.13      nicm       66:        } else if (args_has(self->args, 'w') ||
                     67:            self->entry == &cmd_show_window_options_entry) {
1.25      nicm       68:                scope = OPTIONS_TABLE_WINDOW;
1.12      nicm       69:                if (args_has(self->args, 'g'))
1.24      nicm       70:                        oo = global_w_options;
1.26    ! nicm       71:                else
1.24      nicm       72:                        oo = wl->window->options;
1.8       nicm       73:        } else {
1.25      nicm       74:                scope = OPTIONS_TABLE_SESSION;
1.12      nicm       75:                if (args_has(self->args, 'g'))
1.24      nicm       76:                        oo = global_s_options;
1.26    ! nicm       77:                else
1.24      nicm       78:                        oo = s->options;
1.1       nicm       79:        }
                     80:
1.20      nicm       81:        quiet = args_has(self->args, 'q');
                     82:        if (args->argc == 0)
1.25      nicm       83:                return (cmd_show_options_all(self, cmdq, oo, scope));
1.18      nicm       84:        else
1.20      nicm       85:                return (cmd_show_options_one(self, cmdq, oo, quiet));
1.18      nicm       86: }
                     87:
                     88: enum cmd_retval
1.19      nicm       89: cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
1.20      nicm       90:     struct options *oo, int quiet)
1.18      nicm       91: {
                     92:        struct args                             *args = self->args;
1.22      nicm       93:        const char                              *name = args->argv[0];
1.25      nicm       94:        const struct options_table_entry        *oe;
1.18      nicm       95:        struct options_entry                    *o;
                     96:        const char                              *optval;
                     97:
1.22      nicm       98: retry:
                     99:        if (*name == '@') {
                    100:                if ((o = options_find1(oo, name)) == NULL) {
1.20      nicm      101:                        if (quiet)
                    102:                                return (CMD_RETURN_NORMAL);
1.22      nicm      103:                        cmdq_error(cmdq, "unknown option: %s", name);
1.16      nicm      104:                        return (CMD_RETURN_ERROR);
1.15      nicm      105:                }
1.18      nicm      106:                if (args_has(self->args, 'v'))
1.19      nicm      107:                        cmdq_print(cmdq, "%s", o->str);
1.18      nicm      108:                else
1.19      nicm      109:                        cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
1.18      nicm      110:                return (CMD_RETURN_NORMAL);
                    111:        }
                    112:
1.25      nicm      113:        oe = NULL;
                    114:        if (options_table_find(name, &oe) != 0) {
1.22      nicm      115:                cmdq_error(cmdq, "ambiguous option: %s", name);
1.18      nicm      116:                return (CMD_RETURN_ERROR);
                    117:        }
                    118:        if (oe == NULL) {
1.20      nicm      119:                if (quiet)
1.25      nicm      120:                        return (CMD_RETURN_NORMAL);
1.22      nicm      121:                cmdq_error(cmdq, "unknown option: %s", name);
1.18      nicm      122:                return (CMD_RETURN_ERROR);
                    123:        }
1.22      nicm      124:        if (oe->style != NULL) {
                    125:                name = oe->style;
                    126:                goto retry;
                    127:        }
1.18      nicm      128:        if ((o = options_find1(oo, oe->name)) == NULL)
                    129:                return (CMD_RETURN_NORMAL);
                    130:        optval = options_table_print_entry(oe, o, args_has(self->args, 'v'));
                    131:        if (args_has(self->args, 'v'))
1.19      nicm      132:                cmdq_print(cmdq, "%s", optval);
1.18      nicm      133:        else
1.19      nicm      134:                cmdq_print(cmdq, "%s %s", oe->name, optval);
1.18      nicm      135:        return (CMD_RETURN_NORMAL);
                    136: }
                    137:
                    138: enum cmd_retval
1.25      nicm      139: cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq, struct options *oo,
                    140:     enum options_table_scope scope)
1.18      nicm      141: {
                    142:        const struct options_table_entry        *oe;
                    143:        struct options_entry                    *o;
                    144:        const char                              *optval;
1.25      nicm      145:        int                                      vflag;
1.18      nicm      146:
1.24      nicm      147:        o = options_first(oo);
                    148:        while (o != NULL) {
1.18      nicm      149:                if (*o->name == '@') {
                    150:                        if (args_has(self->args, 'v'))
1.19      nicm      151:                                cmdq_print(cmdq, "%s", o->str);
1.18      nicm      152:                        else
1.19      nicm      153:                                cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
1.15      nicm      154:                }
1.24      nicm      155:                o = options_next(o);
1.18      nicm      156:        }
                    157:
1.25      nicm      158:        vflag = args_has(self->args, 'v');
                    159:        for (oe = options_table; oe->name != NULL; oe++) {
                    160:                if (oe->style != NULL || oe->scope != scope)
1.22      nicm      161:                        continue;
1.11      nicm      162:                if ((o = options_find1(oo, oe->name)) == NULL)
1.18      nicm      163:                        continue;
1.25      nicm      164:                optval = options_table_print_entry(oe, o, vflag);
                    165:                if (vflag)
1.19      nicm      166:                        cmdq_print(cmdq, "%s", optval);
1.17      nicm      167:                else
1.19      nicm      168:                        cmdq_print(cmdq, "%s %s", oe->name, optval);
1.1       nicm      169:        }
                    170:
1.16      nicm      171:        return (CMD_RETURN_NORMAL);
1.1       nicm      172: }