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

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