xref: /netbsd-src/external/bsd/tmux/dist/cmd-show-options.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* $OpenBSD$ */
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 	cmd_show_options_exec
43 };
44 
45 const struct cmd_entry cmd_show_window_options_entry = {
46 	"show-window-options", "showw",
47 	"gvt:", 0, 1,
48 	"[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
49 	0,
50 	cmd_show_options_exec
51 };
52 
53 enum cmd_retval
54 cmd_show_options_exec(struct cmd *self, struct cmd_q *cmdq)
55 {
56 	struct args				*args = self->args;
57 	struct session				*s;
58 	struct winlink				*wl;
59 	const struct options_table_entry	*table;
60 	struct options				*oo;
61 	int					 quiet;
62 
63 	if (args_has(self->args, 's')) {
64 		oo = &global_options;
65 		table = server_options_table;
66 	} else if (args_has(self->args, 'w') ||
67 	    self->entry == &cmd_show_window_options_entry) {
68 		table = window_options_table;
69 		if (args_has(self->args, 'g'))
70 			oo = &global_w_options;
71 		else {
72 			wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
73 			if (wl == NULL)
74 				return (CMD_RETURN_ERROR);
75 			oo = &wl->window->options;
76 		}
77 	} else {
78 		table = session_options_table;
79 		if (args_has(self->args, 'g'))
80 			oo = &global_s_options;
81 		else {
82 			s = cmd_find_session(cmdq, args_get(args, 't'), 0);
83 			if (s == NULL)
84 				return (CMD_RETURN_ERROR);
85 			oo = &s->options;
86 		}
87 	}
88 
89 	quiet = args_has(self->args, 'q');
90 	if (args->argc == 0)
91 		return (cmd_show_options_all(self, cmdq, table, oo));
92 	else
93 		return (cmd_show_options_one(self, cmdq, oo, quiet));
94 }
95 
96 enum cmd_retval
97 cmd_show_options_one(struct cmd *self, struct cmd_q *cmdq,
98     struct options *oo, int quiet)
99 {
100 	struct args				*args = self->args;
101 	const char				*name = args->argv[0];
102 	const struct options_table_entry	*table, *oe;
103 	struct options_entry			*o;
104 	const char				*optval;
105 
106 retry:
107 	if (*name == '@') {
108 		if ((o = options_find1(oo, name)) == NULL) {
109 			if (quiet)
110 				return (CMD_RETURN_NORMAL);
111 			cmdq_error(cmdq, "unknown option: %s", name);
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(name, &table, &oe) != 0) {
123 		cmdq_error(cmdq, "ambiguous option: %s", name);
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", name);
130 		return (CMD_RETURN_ERROR);
131 	}
132 	if (oe->style != NULL) {
133 		name = oe->style;
134 		goto retry;
135 	}
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'))
140 		cmdq_print(cmdq, "%s", optval);
141 	else
142 		cmdq_print(cmdq, "%s %s", oe->name, optval);
143 	return (CMD_RETURN_NORMAL);
144 }
145 
146 enum cmd_retval
147 cmd_show_options_all(struct cmd *self, struct cmd_q *cmdq,
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'))
157 				cmdq_print(cmdq, "%s", o->str);
158 			else
159 				cmdq_print(cmdq, "%s \"%s\"", o->name, o->str);
160 		}
161 	}
162 
163 	for (oe = table; oe->name != NULL; oe++) {
164 		if (oe->style != NULL)
165 			continue;
166 		if ((o = options_find1(oo, oe->name)) == NULL)
167 			continue;
168 		optval = options_table_print_entry(oe, o,
169 		    args_has(self->args, 'v'));
170 		if (args_has(self->args, 'v'))
171 			cmdq_print(cmdq, "%s", optval);
172 		else
173 			cmdq_print(cmdq, "%s %s", oe->name, optval);
174 	}
175 
176 	return (CMD_RETURN_NORMAL);
177 }
178