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

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