xref: /netbsd-src/external/bsd/tmux/dist/cmd-show-options.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 static enum cmd_retval	cmd_show_options_exec(struct cmd *, struct cmdq_item *);
31 
32 static enum cmd_retval	cmd_show_options_one(struct cmd *, struct cmdq_item *,
33 			    struct options *);
34 static enum cmd_retval	cmd_show_options_all(struct cmd *, struct cmdq_item *,
35 		    	    struct options *);
36 
37 const struct cmd_entry cmd_show_options_entry = {
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 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
45 
46 	.flags = CMD_AFTERHOOK,
47 	.exec = cmd_show_options_exec
48 };
49 
50 const struct cmd_entry cmd_show_window_options_entry = {
51 	.name = "show-window-options",
52 	.alias = "showw",
53 
54 	.args = { "gvt:", 0, 1 },
55 	.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
56 
57 	.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
58 
59 	.flags = CMD_AFTERHOOK,
60 	.exec = cmd_show_options_exec
61 };
62 
63 static enum cmd_retval
64 cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
65 {
66 	struct args			*args = self->args;
67 	struct cmd_find_state		*fs = &item->target;
68 	struct options			*oo;
69 	enum options_table_scope	 scope;
70 	char				*cause;
71 	int				 window;
72 
73 	window = (self->entry == &cmd_show_window_options_entry);
74 	scope = options_scope_from_flags(args, window, fs, &oo, &cause);
75 	if (scope == OPTIONS_TABLE_NONE) {
76 		cmdq_error(item, "%s", cause);
77 		free(cause);
78 		return (CMD_RETURN_ERROR);
79 	}
80 
81 	if (args->argc == 0)
82 		return (cmd_show_options_all(self, item, oo));
83 	else
84 		return (cmd_show_options_one(self, item, oo));
85 }
86 
87 static void
88 cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
89     struct options_entry *o, int idx)
90 {
91 	const char	*name;
92 	const char	*value;
93 	char		*tmp, *escaped;
94 	u_int		 size, i;
95 
96 	if (idx != -1) {
97 		xasprintf(&tmp, "%s[%d]", options_name(o), idx);
98 		name = tmp;
99 	} else {
100 		if (options_array_size(o, &size) != -1) {
101 			for (i = 0; i < size; i++) {
102 				if (options_array_get(o, i) == NULL)
103 					continue;
104 				cmd_show_options_print(self, item, o, i);
105 			}
106 			return;
107 		}
108 		tmp = NULL;
109 		name = options_name(o);
110 	}
111 
112 	value = options_tostring(o, idx, 0);
113 	if (args_has(self->args, 'v'))
114 		cmdq_print(item, "%s", value);
115 	else if (options_isstring(o)) {
116 		utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ);
117 		cmdq_print(item, "%s \"%s\"", name, escaped);
118 		free(escaped);
119 	} else
120 		cmdq_print(item, "%s %s", name, value);
121 
122 	free(tmp);
123 }
124 
125 static enum cmd_retval
126 cmd_show_options_one(struct cmd *self, struct cmdq_item *item,
127     struct options *oo)
128 {
129 	struct args		*args = self->args;
130 	struct client		*c = cmd_find_client(item, NULL, 1);
131 	struct session		*s = item->target.s;
132 	struct winlink		*wl = item->target.wl;
133 	struct options_entry	*o;
134 	int			 idx, ambiguous;
135 	char			*name;
136 
137 	name = format_single(item, args->argv[0], c, s, wl, NULL);
138 	o = options_match_get(oo, name, &idx, 1, &ambiguous);
139 	if (o == NULL) {
140 		if (args_has(args, 'q')) {
141 			free(name);
142 			return (CMD_RETURN_NORMAL);
143 		}
144 		if (ambiguous) {
145 			cmdq_error(item, "ambiguous option: %s", name);
146 			free(name);
147 			return (CMD_RETURN_ERROR);
148 		}
149 		if (*name != '@' &&
150 		    options_match_get(oo, name, &idx, 0, &ambiguous) != NULL) {
151 			free(name);
152 			return (CMD_RETURN_NORMAL);
153 		}
154 		cmdq_error(item, "unknown option: %s", name);
155 		free(name);
156 		return (CMD_RETURN_ERROR);
157 	}
158 	cmd_show_options_print(self, item, o, idx);
159 	free(name);
160 	return (CMD_RETURN_NORMAL);
161 }
162 
163 static enum cmd_retval
164 cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
165     struct options *oo)
166 {
167 	struct options_entry			 *o;
168 	const struct options_table_entry	*oe;
169 	u_int					 size, idx;
170 
171 	o = options_first(oo);
172 	while (o != NULL) {
173 		oe = options_table_entry(o);
174 		if (oe != NULL && oe->style != NULL) {
175 			o = options_next(o);
176 			continue;
177 		}
178 		if (options_array_size(o, &size) == -1)
179 			cmd_show_options_print(self, item, o, -1);
180 		else {
181 			for (idx = 0; idx < size; idx++) {
182 				if (options_array_get(o, idx) == NULL)
183 					continue;
184 				cmd_show_options_print(self, item, o, idx);
185 			}
186 		}
187 		o = options_next(o);
188 	}
189 	return (CMD_RETURN_NORMAL);
190 }
191