xref: /openbsd-src/usr.bin/tmux/cmd-show-options.c (revision d59bb9942320b767f2a19aaa7690c8c6e30b724c)
1 /* $OpenBSD: cmd-show-options.c,v 1.40 2017/01/30 21:41:17 nicm Exp $ */
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 #include <vis.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Show options.
29  */
30 
31 static enum cmd_retval	cmd_show_options_exec(struct cmd *, struct cmdq_item *);
32 
33 static enum cmd_retval	cmd_show_options_one(struct cmd *, struct cmdq_item *,
34 			    struct options *);
35 static enum cmd_retval	cmd_show_options_all(struct cmd *, struct cmdq_item *,
36 		    	    struct options *);
37 
38 const struct cmd_entry cmd_show_options_entry = {
39 	.name = "show-options",
40 	.alias = "show",
41 
42 	.args = { "gqst:vw", 0, 1 },
43 	.usage = "[-gqsvw] [-t target-session|target-window] [option]",
44 
45 	.tflag = CMD_WINDOW_CANFAIL,
46 
47 	.flags = CMD_AFTERHOOK,
48 	.exec = cmd_show_options_exec
49 };
50 
51 const struct cmd_entry cmd_show_window_options_entry = {
52 	.name = "show-window-options",
53 	.alias = "showw",
54 
55 	.args = { "gvt:", 0, 1 },
56 	.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
57 
58 	.tflag = CMD_WINDOW_CANFAIL,
59 
60 	.flags = CMD_AFTERHOOK,
61 	.exec = cmd_show_options_exec
62 };
63 
64 static enum cmd_retval
65 cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
66 {
67 	struct args			*args = self->args;
68 	struct cmd_find_state		*fs = &item->state.tflag;
69 	struct options			*oo;
70 	enum options_table_scope	 scope;
71 	char				*cause;
72 	int				 window;
73 
74 	window = (self->entry == &cmd_show_window_options_entry);
75 	scope = options_scope_from_flags(args, window, fs, &oo, &cause);
76 	if (scope == OPTIONS_TABLE_NONE) {
77 		cmdq_error(item, "%s", cause);
78 		free(cause);
79 		return (CMD_RETURN_ERROR);
80 	}
81 
82 	if (args->argc == 0)
83 		return (cmd_show_options_all(self, item, oo));
84 	else
85 		return (cmd_show_options_one(self, item, oo));
86 }
87 
88 static void
89 cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
90     struct options_entry *o, int idx)
91 {
92 	const char	*name;
93 	const char	*value;
94 	char		*tmp, *escaped;
95 	u_int		 size, i;
96 
97 	if (idx != -1) {
98 		xasprintf(&tmp, "%s[%d]", options_name(o), idx);
99 		name = tmp;
100 	} else {
101 		if (options_array_size(o, &size) != -1) {
102 			for (i = 0; i < size; i++) {
103 				if (options_array_get(o, i) == NULL)
104 					continue;
105 				cmd_show_options_print(self, item, o, i);
106 			}
107 			return;
108 		}
109 		tmp = NULL;
110 		name = options_name(o);
111 	}
112 
113 	value = options_tostring(o, idx, 0);
114 	if (args_has(self->args, 'v'))
115 		cmdq_print(item, "%s", value);
116 	else if (options_isstring(o)) {
117 		utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ);
118 		cmdq_print(item, "%s \"%s\"", name, escaped);
119 		free(escaped);
120 	} else
121 		cmdq_print(item, "%s %s", name, value);
122 
123 	free(tmp);
124 }
125 
126 static enum cmd_retval
127 cmd_show_options_one(struct cmd *self, struct cmdq_item *item,
128     struct options *oo)
129 {
130 	struct args		*args = self->args;
131 	struct options_entry	*o;
132 	int			 idx, ambiguous;
133 	const char		*name = args->argv[0];
134 
135 	o = options_match_get(oo, name, &idx, 1, &ambiguous);
136 	if (o == NULL) {
137 		if (args_has(args, 'q'))
138 			return (CMD_RETURN_NORMAL);
139 		if (ambiguous) {
140 			cmdq_error(item, "ambiguous option: %s", name);
141 			return (CMD_RETURN_ERROR);
142 		}
143 		if (*name != '@' &&
144 		    options_match_get(oo, name, &idx, 0, &ambiguous) != NULL)
145 			return (CMD_RETURN_NORMAL);
146 		cmdq_error(item, "unknown option: %s", name);
147 		return (CMD_RETURN_ERROR);
148 	}
149 	cmd_show_options_print(self, item, o, idx);
150 	return (CMD_RETURN_NORMAL);
151 }
152 
153 static enum cmd_retval
154 cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
155     struct options *oo)
156 {
157 	struct options_entry			 *o;
158 	const struct options_table_entry	*oe;
159 	u_int					 size, idx;
160 
161 	o = options_first(oo);
162 	while (o != NULL) {
163 		oe = options_table_entry(o);
164 		if (oe != NULL && oe->style != NULL) {
165 			o = options_next(o);
166 			continue;
167 		}
168 		if (options_array_size(o, &size) == -1)
169 			cmd_show_options_print(self, item, o, -1);
170 		else {
171 			for (idx = 0; idx < size; idx++) {
172 				if (options_array_get(o, idx) == NULL)
173 					continue;
174 				cmd_show_options_print(self, item, o, idx);
175 			}
176 		}
177 		o = options_next(o);
178 	}
179 	return (CMD_RETURN_NORMAL);
180 }
181