1 /* Id */
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
30 enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmd_q *);
31
32 enum cmd_retval cmd_show_options_one(struct cmd *, struct cmd_q *,
33 struct options *, int);
34 enum cmd_retval cmd_show_options_all(struct cmd *, struct cmd_q *,
35 const struct options_table_entry *, struct options *);
36
37 const struct cmd_entry cmd_show_options_entry = {
38 "show-options", "show",
39 "gqst:vw", 0, 1,
40 "[-gqsvw] [-t target-session|target-window] [option]",
41 0,
42 NULL,
43 cmd_show_options_exec
44 };
45
46 const struct cmd_entry cmd_show_window_options_entry = {
47 "show-window-options", "showw",
48 "gvt:", 0, 1,
49 "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
50 0,
51 NULL,
52 cmd_show_options_exec
53 };
54
55 enum cmd_retval
cmd_show_options_exec(struct cmd * self,struct cmd_q * cmdq)56 cmd_show_options_exec(struct cmd *self, struct cmd_q *cmdq)
57 {
58 struct args *args = self->args;
59 struct session *s;
60 struct winlink *wl;
61 const struct options_table_entry *table;
62 struct options *oo;
63 int quiet;
64
65 if (args_has(self->args, 's')) {
66 oo = &global_options;
67 table = server_options_table;
68 } else if (args_has(self->args, 'w') ||
69 self->entry == &cmd_show_window_options_entry) {
70 table = window_options_table;
71 if (args_has(self->args, 'g'))
72 oo = &global_w_options;
73 else {
74 wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
75 if (wl == NULL)
76 return (CMD_RETURN_ERROR);
77 oo = &wl->window->options;
78 }
79 } else {
80 table = session_options_table;
81 if (args_has(self->args, 'g'))
82 oo = &global_s_options;
83 else {
84 s = cmd_find_session(cmdq, args_get(args, 't'), 0);
85 if (s == NULL)
86 return (CMD_RETURN_ERROR);
87 oo = &s->options;
88 }
89 }
90
91 quiet = args_has(self->args, 'q');
92 if (args->argc == 0)
93 return (cmd_show_options_all(self, cmdq, table, oo));
94 else
95 return (cmd_show_options_one(self, cmdq, oo, quiet));
96 }
97
98 enum cmd_retval
cmd_show_options_one(struct cmd * self,struct cmd_q * cmdq,struct options * oo,int quiet)99 cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
100 struct options *oo, int quiet)
101 {
102 struct args *args = self->args;
103 const struct options_table_entry *table, *oe;
104 struct options_entry *o;
105 const char *optval;
106
107 if (*args->argv[0] == '@') {
108 if ((o = options_find1(oo, args->argv[0])) == NULL) {
109 if (quiet)
110 return (CMD_RETURN_NORMAL);
111 cmdq_error(cmdq, "unknown option: %s", args->argv[0]);
112 return (CMD_RETURN_ERROR);
113 }
114 if (args_has(self->args, 'v'))
115 cmdq_print(cmdq, "%s", o->str);
116 else
117 cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
118 return (CMD_RETURN_NORMAL);
119 }
120
121 table = oe = NULL;
122 if (options_table_find(args->argv[0], &table, &oe) != 0) {
123 cmdq_error(cmdq, "ambiguous option: %s", args->argv[0]);
124 return (CMD_RETURN_ERROR);
125 }
126 if (oe == NULL) {
127 if (quiet)
128 return (CMD_RETURN_NORMAL);
129 cmdq_error(cmdq, "unknown option: %s", args->argv[0]);
130 return (CMD_RETURN_ERROR);
131 }
132 if ((o = options_find1(oo, oe->name)) == NULL)
133 return (CMD_RETURN_NORMAL);
134 optval = options_table_print_entry(oe, o, args_has(self->args, 'v'));
135 if (args_has(self->args, 'v'))
136 cmdq_print(cmdq, "%s", optval);
137 else
138 cmdq_print(cmdq, "%s %s", oe->name, optval);
139 return (CMD_RETURN_NORMAL);
140 }
141
142 enum cmd_retval
cmd_show_options_all(struct cmd * self,struct cmd_q * cmdq,const struct options_table_entry * table,struct options * oo)143 cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq,
144 const struct options_table_entry *table, struct options *oo)
145 {
146 const struct options_table_entry *oe;
147 struct options_entry *o;
148 const char *optval;
149
150 RB_FOREACH(o, options_tree, &oo->tree) {
151 if (*o->name == '@') {
152 if (args_has(self->args, 'v'))
153 cmdq_print(cmdq, "%s", o->str);
154 else
155 cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
156 }
157 }
158
159 for (oe = table; oe->name != NULL; oe++) {
160 if ((o = options_find1(oo, oe->name)) == NULL)
161 continue;
162 optval = options_table_print_entry(oe, o,
163 args_has(self->args, 'v'));
164 if (args_has(self->args, 'v'))
165 cmdq_print(cmdq, "%s", optval);
166 else
167 cmdq_print(cmdq, "%s %s", oe->name, optval);
168 }
169
170 return (CMD_RETURN_NORMAL);
171 }
172