xref: /openbsd-src/usr.bin/tmux/cmd-set-option.c (revision 2f0d274ade81ba63b6c504544237a0e37ce69820)
1 /* $OpenBSD: cmd-set-option.c,v 1.108 2017/01/24 19:11:46 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 
24 #include "tmux.h"
25 
26 /*
27  * Set an option.
28  */
29 
30 static enum cmd_retval	cmd_set_option_exec(struct cmd *, struct cmdq_item *);
31 
32 static int	cmd_set_option_set(struct cmd *, struct cmdq_item *,
33 		    struct options *, struct options_entry *, const char *);
34 static int	cmd_set_option_flag(struct cmdq_item *,
35 		    const struct options_table_entry *, struct options *,
36 		    const char *);
37 static int	cmd_set_option_choice(struct cmdq_item *,
38 		    const struct options_table_entry *, struct options *,
39 		    const char *);
40 
41 const struct cmd_entry cmd_set_option_entry = {
42 	.name = "set-option",
43 	.alias = "set",
44 
45 	.args = { "agoqst:uw", 1, 2 },
46 	.usage = "[-agosquw] [-t target-window] option [value]",
47 
48 	.tflag = CMD_WINDOW_CANFAIL,
49 
50 	.flags = CMD_AFTERHOOK,
51 	.exec = cmd_set_option_exec
52 };
53 
54 const struct cmd_entry cmd_set_window_option_entry = {
55 	.name = "set-window-option",
56 	.alias = "setw",
57 
58 	.args = { "agoqt:u", 1, 2 },
59 	.usage = "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
60 
61 	.tflag = CMD_WINDOW_CANFAIL,
62 
63 	.flags = CMD_AFTERHOOK,
64 	.exec = cmd_set_option_exec
65 };
66 
67 static enum cmd_retval
68 cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
69 {
70 	struct args			*args = self->args;
71 	struct cmd_find_state		*fs = &item->state.tflag;
72 	struct session			*s = fs->s;
73 	struct winlink			*wl = fs->wl;
74 	struct window			*w = wl->window;
75 	struct client			*c;
76 	enum options_table_scope	 scope;
77 	struct options			*oo;
78 	struct options_entry		*parent, *o;
79 	const char			*name, *value, *target;
80 	int				 window, idx, already, error, ambiguous;
81 	char				*cause;
82 
83 	/* Parse option name and index. */
84 	name = options_match(args->argv[0], &idx, &ambiguous);
85 	if (name == NULL) {
86 		if (args_has(args, 'q'))
87 			return (CMD_RETURN_NORMAL);
88 		if (ambiguous)
89 			cmdq_error(item, "ambiguous option: %s", args->argv[0]);
90 		else
91 			cmdq_error(item, "invalid option: %s", args->argv[0]);
92 		return (CMD_RETURN_ERROR);
93 	}
94 	if (args->argc < 2)
95 		value = NULL;
96 	else
97 		value = args->argv[1];
98 
99 	/*
100 	 * Figure out the scope: for user options it comes from the arguments,
101 	 * otherwise from the option name.
102 	 */
103 	if (*name == '@') {
104 		window = (self->entry == &cmd_set_window_option_entry);
105 		scope = options_scope_from_flags(args, window, fs, &oo, &cause);
106 	} else {
107 		if (options_get_only(global_options, name) != NULL)
108 			scope = OPTIONS_TABLE_SERVER;
109 		else if (options_get_only(global_s_options, name) != NULL)
110 			scope = OPTIONS_TABLE_SESSION;
111 		else if (options_get_only(global_w_options, name) != NULL)
112 			scope = OPTIONS_TABLE_WINDOW;
113 		else {
114 			scope = OPTIONS_TABLE_NONE;
115 			xasprintf(&cause, "unknown option: %s", args->argv[0]);
116 		}
117 	}
118 	if (scope == OPTIONS_TABLE_NONE) {
119 		if (args_has(args, 'q'))
120 			return (CMD_RETURN_NORMAL);
121 		cmdq_error(item, "%s", cause);
122 		free(cause);
123 		return (CMD_RETURN_ERROR);
124 	}
125 
126 	/* Which table should this option go into? */
127 	if (scope == OPTIONS_TABLE_SERVER)
128 		oo = global_options;
129 	else if (scope == OPTIONS_TABLE_SESSION) {
130 		if (args_has(self->args, 'g'))
131 			oo = global_s_options;
132 		else if (s == NULL) {
133 			target = args_get(args, 't');
134 			if (target != NULL)
135 				cmdq_error(item, "no such session: %s", target);
136 			else
137 				cmdq_error(item, "no current session");
138 			return (CMD_RETURN_ERROR);
139 		} else
140 			oo = s->options;
141 	} else if (scope == OPTIONS_TABLE_WINDOW) {
142 		if (args_has(self->args, 'g'))
143 			oo = global_w_options;
144 		else if (wl == NULL) {
145 			target = args_get(args, 't');
146 			if (target != NULL)
147 				cmdq_error(item, "no such window: %s", target);
148 			else
149 				cmdq_error(item, "no current window");
150 			return (CMD_RETURN_ERROR);
151 		} else
152 			oo = wl->window->options;
153 	}
154 	o = options_get_only(oo, name);
155 	parent = options_get(oo, name);
156 
157 	/* Check that array options and indexes match up. */
158 	if (idx != -1) {
159 		if (*name == '@' || options_array_size(parent, NULL) == -1) {
160 			cmdq_error(item, "not an array: %s", args->argv[0]);
161 			return (CMD_RETURN_ERROR);
162 		}
163 	} else if (*name != '@' && options_array_size(parent, NULL) != -1) {
164 		if (value == NULL) {
165 			cmdq_error(item, "empty value");
166 			return (-1);
167 		}
168 		if (o == NULL)
169 			o = options_empty(oo, options_table_entry(parent));
170 		if (!args_has(args, 'a'))
171 			options_array_clear(o);
172 		options_array_assign(o, value);
173 		return (CMD_RETURN_NORMAL);
174 	}
175 
176 	/* With -o, check this option is not already set. */
177 	if (!args_has(args, 'u') && args_has(args, 'o')) {
178 		if (idx == -1)
179 			already = (o != NULL);
180 		else {
181 			if (o == NULL)
182 				already = 0;
183 			else
184 				already = (options_array_get(o, idx) != NULL);
185 		}
186 		if (already) {
187 			if (args_has(args, 'q'))
188 				return (CMD_RETURN_NORMAL);
189 			cmdq_error(item, "already set: %s", args->argv[0]);
190 			return (CMD_RETURN_ERROR);
191 		}
192 	}
193 
194 	/* Change the option. */
195 	if (args_has(args, 'u')) {
196 		if (o == NULL)
197 			return (CMD_RETURN_NORMAL);
198 		if (idx == -1) {
199 			if (oo == global_options ||
200 			    oo == global_s_options ||
201 			    oo == global_w_options)
202 				options_default(oo, options_table_entry(o));
203 			else
204 				options_remove(o);
205 		} else
206 			options_array_set(o, idx, NULL, 0);
207 	} else if (*name == '@')
208 		options_set_string(oo, name, args_has(args, 'a'), "%s", value);
209 	else if (idx == -1) {
210 		error = cmd_set_option_set(self, item, oo, parent, value);
211 		if (error != 0)
212 			return (CMD_RETURN_ERROR);
213 	} else {
214 		if (o == NULL)
215 			o = options_empty(oo, options_table_entry(parent));
216 		if (options_array_set(o, idx, value, 1) != 0) {
217 			cmdq_error(item, "invalid index: %s", args->argv[0]);
218 			return (CMD_RETURN_ERROR);
219 		}
220 	}
221 
222 	/* Update timers and so on for various options. */
223 	if (strcmp(name, "automatic-rename") == 0) {
224 		RB_FOREACH(w, windows, &windows) {
225 			if (w->active == NULL)
226 				continue;
227 			if (options_get_number(w->options, "automatic-rename"))
228 				w->active->flags |= PANE_CHANGED;
229 		}
230 	}
231 	if (strcmp(name, "key-table") == 0) {
232 		TAILQ_FOREACH(c, &clients, entry)
233 			server_client_set_key_table(c, NULL);
234 	}
235 	if (strcmp(name, "status") == 0 ||
236 	    strcmp(name, "status-interval") == 0)
237 		status_timer_start_all();
238 	if (strcmp(name, "monitor-silence") == 0)
239 		alerts_reset_all();
240 	if (strcmp(name, "window-style") == 0 ||
241 	    strcmp(name, "window-active-style") == 0) {
242 		RB_FOREACH(w, windows, &windows)
243 			w->flags |= WINDOW_STYLECHANGED;
244 	}
245 	if (strcmp(name, "pane-border-status") == 0) {
246 		RB_FOREACH(w, windows, &windows)
247 			layout_fix_panes(w, w->sx, w->sy);
248 	}
249 
250 	/*
251 	 * Update sizes and redraw. May not always be necessary but do it
252 	 * anyway.
253 	 */
254 	recalculate_sizes();
255 	TAILQ_FOREACH(c, &clients, entry) {
256 		if (c->session != NULL)
257 			server_redraw_client(c);
258 	}
259 
260 	return (CMD_RETURN_NORMAL);
261 }
262 
263 static int
264 cmd_set_option_set(struct cmd *self, struct cmdq_item *item, struct options *oo,
265     struct options_entry *parent, const char *value)
266 {
267 	const struct options_table_entry	*oe;
268 	struct args				*args = self->args;
269 	int					 append = args_has(args, 'a');
270 	struct options_entry			*o;
271 	long long				 number;
272 	const char				*errstr;
273 	key_code				 key;
274 
275 	oe = options_table_entry(parent);
276 	if (value == NULL &&
277 	    oe->type != OPTIONS_TABLE_FLAG &&
278 	    oe->type != OPTIONS_TABLE_CHOICE) {
279 		cmdq_error(item, "empty value");
280 		return (-1);
281 	}
282 
283 	switch (oe->type) {
284 	case OPTIONS_TABLE_STRING:
285 		options_set_string(oo, oe->name, append, "%s", value);
286 		return (0);
287 	case OPTIONS_TABLE_NUMBER:
288 		number = strtonum(value, oe->minimum, oe->maximum, &errstr);
289 		if (errstr != NULL) {
290 			cmdq_error(item, "value is %s: %s", errstr, value);
291 			return (-1);
292 		}
293 		options_set_number(oo, oe->name, number);
294 		return (0);
295 	case OPTIONS_TABLE_KEY:
296 		key = key_string_lookup_string(value);
297 		if (key == KEYC_UNKNOWN) {
298 			cmdq_error(item, "bad key: %s", value);
299 			return (-1);
300 		}
301 		options_set_number(oo, oe->name, key);
302 		return (0);
303 	case OPTIONS_TABLE_COLOUR:
304 		if ((number = colour_fromstring(value)) == -1) {
305 			cmdq_error(item, "bad colour: %s", value);
306 			return (-1);
307 		}
308 		o = options_set_number(oo, oe->name, number);
309 		options_style_update_new(oo, o);
310 		return (0);
311 	case OPTIONS_TABLE_ATTRIBUTES:
312 		if ((number = attributes_fromstring(value)) == -1) {
313 			cmdq_error(item, "bad attributes: %s", value);
314 			return (-1);
315 		}
316 		o = options_set_number(oo, oe->name, number);
317 		options_style_update_new(oo, o);
318 		return (0);
319 	case OPTIONS_TABLE_FLAG:
320 		return (cmd_set_option_flag(item, oe, oo, value));
321 	case OPTIONS_TABLE_CHOICE:
322 		return (cmd_set_option_choice(item, oe, oo, value));
323 	case OPTIONS_TABLE_STYLE:
324 		o = options_set_style(oo, oe->name, append, value);
325 		if (o == NULL) {
326 			cmdq_error(item, "bad style: %s", value);
327 			return (-1);
328 		}
329 		options_style_update_old(oo, o);
330 		return (0);
331 	case OPTIONS_TABLE_ARRAY:
332 		break;
333 	}
334 	return (-1);
335 }
336 
337 static int
338 cmd_set_option_flag(struct cmdq_item *item,
339     const struct options_table_entry *oe, struct options *oo,
340     const char *value)
341 {
342 	int	flag;
343 
344 	if (value == NULL || *value == '\0')
345 		flag = !options_get_number(oo, oe->name);
346 	else if (strcmp(value, "1") == 0 ||
347 	    strcasecmp(value, "on") == 0 ||
348 	    strcasecmp(value, "yes") == 0)
349 		flag = 1;
350 	else if (strcmp(value, "0") == 0 ||
351 	    strcasecmp(value, "off") == 0 ||
352 	    strcasecmp(value, "no") == 0)
353 		flag = 0;
354 	else {
355 		cmdq_error(item, "bad value: %s", value);
356 		return (-1);
357 	}
358 	options_set_number(oo, oe->name, flag);
359 	return (0);
360 }
361 
362 static int
363 cmd_set_option_choice(struct cmdq_item *item,
364     const struct options_table_entry *oe, struct options *oo,
365     const char *value)
366 {
367 	const char	**cp;
368 	int		  n, choice = -1;
369 
370 	if (value == NULL) {
371 		choice = options_get_number(oo, oe->name);
372 		if (choice < 2)
373 			choice = !choice;
374 	} else {
375 		n = 0;
376 		for (cp = oe->choices; *cp != NULL; cp++) {
377 			if (strcmp(*cp, value) == 0)
378 				choice = n;
379 			n++;
380 		}
381 		if (choice == -1) {
382 			cmdq_error(item, "unknown value: %s", value);
383 			return (-1);
384 		}
385 	}
386 	options_set_number(oo, oe->name, choice);
387 	return (0);
388 }
389