Annotation of src/usr.bin/tmux/cmd-show-options.c, Revision 1.23
1.23 ! nicm 1: /* $OpenBSD: cmd-show-options.c,v 1.22 2014/04/17 07:43:20 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.18 nicm 35: const struct options_table_entry *, struct options *);
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.12 nicm 41: 0,
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.13 nicm 49: 0,
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.12 nicm 56: struct args *args = self->args;
1.11 nicm 57: struct session *s;
58: struct winlink *wl;
1.18 nicm 59: const struct options_table_entry *table;
1.11 nicm 60: struct options *oo;
1.20 nicm 61: int quiet;
1.1 nicm 62:
1.12 nicm 63: if (args_has(self->args, 's')) {
1.10 nicm 64: oo = &global_options;
1.11 nicm 65: table = server_options_table;
1.13 nicm 66: } else if (args_has(self->args, 'w') ||
67: self->entry == &cmd_show_window_options_entry) {
1.11 nicm 68: table = window_options_table;
1.12 nicm 69: if (args_has(self->args, 'g'))
1.8 nicm 70: oo = &global_w_options;
71: else {
1.19 nicm 72: wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
1.8 nicm 73: if (wl == NULL)
1.16 nicm 74: return (CMD_RETURN_ERROR);
1.8 nicm 75: oo = &wl->window->options;
76: }
77: } else {
1.11 nicm 78: table = session_options_table;
1.12 nicm 79: if (args_has(self->args, 'g'))
1.8 nicm 80: oo = &global_s_options;
81: else {
1.19 nicm 82: s = cmd_find_session(cmdq, args_get(args, 't'), 0);
1.8 nicm 83: if (s == NULL)
1.16 nicm 84: return (CMD_RETURN_ERROR);
1.8 nicm 85: oo = &s->options;
86: }
1.1 nicm 87: }
88:
1.20 nicm 89: quiet = args_has(self->args, 'q');
90: if (args->argc == 0)
91: return (cmd_show_options_all(self, cmdq, table, oo));
1.18 nicm 92: else
1.20 nicm 93: return (cmd_show_options_one(self, cmdq, oo, quiet));
1.18 nicm 94: }
95:
96: enum cmd_retval
1.19 nicm 97: cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
1.20 nicm 98: struct options *oo, int quiet)
1.18 nicm 99: {
100: struct args *args = self->args;
1.22 nicm 101: const char *name = args->argv[0];
1.18 nicm 102: const struct options_table_entry *table, *oe;
103: struct options_entry *o;
104: const char *optval;
105:
1.22 nicm 106: retry:
107: if (*name == '@') {
108: if ((o = options_find1(oo, name)) == NULL) {
1.20 nicm 109: if (quiet)
110: return (CMD_RETURN_NORMAL);
1.22 nicm 111: cmdq_error(cmdq, "unknown option: %s", name);
1.16 nicm 112: return (CMD_RETURN_ERROR);
1.15 nicm 113: }
1.18 nicm 114: if (args_has(self->args, 'v'))
1.19 nicm 115: cmdq_print(cmdq, "%s", o->str);
1.18 nicm 116: else
1.19 nicm 117: cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
1.18 nicm 118: return (CMD_RETURN_NORMAL);
119: }
120:
121: table = oe = NULL;
1.22 nicm 122: if (options_table_find(name, &table, &oe) != 0) {
123: cmdq_error(cmdq, "ambiguous option: %s", name);
1.18 nicm 124: return (CMD_RETURN_ERROR);
125: }
126: if (oe == NULL) {
1.20 nicm 127: if (quiet)
128: return (CMD_RETURN_NORMAL);
1.22 nicm 129: cmdq_error(cmdq, "unknown option: %s", name);
1.18 nicm 130: return (CMD_RETURN_ERROR);
131: }
1.22 nicm 132: if (oe->style != NULL) {
133: name = oe->style;
134: goto retry;
135: }
1.18 nicm 136: if ((o = options_find1(oo, oe->name)) == NULL)
137: return (CMD_RETURN_NORMAL);
138: optval = options_table_print_entry(oe, o, args_has(self->args, 'v'));
139: if (args_has(self->args, 'v'))
1.19 nicm 140: cmdq_print(cmdq, "%s", optval);
1.18 nicm 141: else
1.19 nicm 142: cmdq_print(cmdq, "%s %s", oe->name, optval);
1.18 nicm 143: return (CMD_RETURN_NORMAL);
144: }
145:
146: enum cmd_retval
1.19 nicm 147: cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq,
1.18 nicm 148: const struct options_table_entry *table, struct options *oo)
149: {
150: const struct options_table_entry *oe;
151: struct options_entry *o;
152: const char *optval;
153:
154: RB_FOREACH(o, options_tree, &oo->tree) {
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.18 nicm 161: }
162:
163: for (oe = table; oe->name != NULL; oe++) {
1.22 nicm 164: if (oe->style != NULL)
165: continue;
1.11 nicm 166: if ((o = options_find1(oo, oe->name)) == NULL)
1.18 nicm 167: continue;
1.17 nicm 168: optval = options_table_print_entry(oe, o,
169: args_has(self->args, 'v'));
170: if (args_has(self->args, 'v'))
1.19 nicm 171: cmdq_print(cmdq, "%s", optval);
1.17 nicm 172: else
1.19 nicm 173: cmdq_print(cmdq, "%s %s", oe->name, optval);
1.1 nicm 174: }
175:
1.16 nicm 176: return (CMD_RETURN_NORMAL);
1.1 nicm 177: }