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