Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 399 lines (347 sloc) 10.715 kb
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
1 /* $Id: cmd-set-option.c 2669 2012-01-21 19:36:40Z tcunha $ */
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
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 * Set an option.
28 */
29
30 int cmd_set_option_exec(struct cmd *, struct cmd_ctx *);
31
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
32 int cmd_set_option_find(const char *, const struct options_table_entry **,
33 const struct options_table_entry **);
34
35 int cmd_set_option_unset(struct cmd *, struct cmd_ctx *,
36 const struct options_table_entry *, struct options *,
37 const char *);
38 int cmd_set_option_set(struct cmd *, struct cmd_ctx *,
39 const struct options_table_entry *, struct options *,
40 const char *);
41
42 struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_ctx *,
43 const struct options_table_entry *, struct options *,
44 const char *);
45 struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_ctx *,
46 const struct options_table_entry *, struct options *,
47 const char *);
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
48 struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_ctx *,
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
49 const struct options_table_entry *, struct options *,
50 const char *);
51 struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_ctx *,
52 const struct options_table_entry *, struct options *,
53 const char *);
54 struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_ctx *,
55 const struct options_table_entry *, struct options *,
56 const char *);
57 struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_ctx *,
58 const struct options_table_entry *, struct options *,
59 const char *);
60 struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_ctx *,
61 const struct options_table_entry *, struct options *,
62 const char *);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
63
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
64 const struct cmd_entry cmd_set_option_entry = {
65 "set-option", "set",
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
66 "agst:uw", 1, 2,
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
67 "[-agsuw] [-t target-session|target-window] option [value]",
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
68 0,
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
69 NULL,
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
70 NULL,
71 cmd_set_option_exec
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
72 };
73
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
74 const struct cmd_entry cmd_set_window_option_entry = {
75 "set-window-option", "setw",
76 "agt:u", 1, 2,
77 "[-agu] " CMD_TARGET_WINDOW_USAGE " option [value]",
78 0,
79 NULL,
80 NULL,
81 cmd_set_option_exec
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
82 };
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
83
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
84 /* Look for an option in all three tables. */
85 int
86 cmd_set_option_find(
87 const char *optstr, const struct options_table_entry **table,
88 const struct options_table_entry **oe)
89 {
90 static const struct options_table_entry *tables[] = {
91 server_options_table,
92 window_options_table,
93 session_options_table
94 };
95 const struct options_table_entry *oe_loop;
96 u_int i;
97
98 for (i = 0; i < nitems(tables); i++) {
99 for (oe_loop = tables[i]; oe_loop->name != NULL; oe_loop++) {
100 if (strncmp(oe_loop->name, optstr, strlen(optstr)) != 0)
101 continue;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
102
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
103 /* If already found, ambiguous. */
104 if (*oe != NULL)
105 return (-1);
106 *oe = oe_loop;
107 *table = tables[i];
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
108
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
109 /* Bail now if an exact match. */
110 if (strcmp((*oe)->name, optstr) == 0)
111 break;
112 }
113 }
114 return (0);
115 }
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
116
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
117 int
118 cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
119 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
120 struct args *args = self->args;
121 const struct options_table_entry *table, *oe;
122 struct session *s;
123 struct winlink *wl;
124 struct client *c;
125 struct options *oo;
126 const char *optstr, *valstr;
127 u_int i;
128
129 /* Get the option name and value. */
130 optstr = args->argv[0];
131 if (*optstr == '\0') {
132 ctx->error(ctx, "invalid option");
133 return (-1);
134 }
135 if (args->argc < 2)
136 valstr = NULL;
137 else
138 valstr = args->argv[1];
139
140 /* Find the option entry, try each table. */
141 table = oe = NULL;
142 if (cmd_set_option_find(optstr, &table, &oe) != 0) {
143 ctx->error(ctx, "ambiguous option: %s", optstr);
144 return (-1);
145 }
146 if (oe == NULL) {
147 ctx->error(ctx, "unknown option: %s", optstr);
148 return (-1);
149 }
150
151 /* Work out the tree from the table. */
152 if (table == server_options_table)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
153 oo = &global_options;
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
154 else if (table == window_options_table) {
155 if (args_has(self->args, 'g'))
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
156 oo = &global_w_options;
157 else {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
158 wl = cmd_find_window(ctx, args_get(args, 't'), NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
159 if (wl == NULL)
160 return (-1);
161 oo = &wl->window->options;
162 }
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
163 } else if (table == session_options_table) {
164 if (args_has(self->args, 'g'))
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
165 oo = &global_s_options;
166 else {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
167 s = cmd_find_session(ctx, args_get(args, 't'), 0);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
168 if (s == NULL)
169 return (-1);
170 oo = &s->options;
171 }
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
172 } else {
173 ctx->error(ctx, "unknown table");
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
174 return (-1);
175 }
176
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
177 /* Unset or set the option. */
178 if (args_has(args, 'u')) {
179 if (cmd_set_option_unset(self, ctx, oe, oo, valstr) != 0)
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
180 return (-1);
181 } else {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
182 if (cmd_set_option_set(self, ctx, oe, oo, valstr) != 0)
183 return (-1);
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
184 }
185
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
186 /* Update sizes and redraw. May not need it but meh. */
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
187 recalculate_sizes();
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
188 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
189 c = ARRAY_ITEM(&clients, i);
190 if (c != NULL && c->session != NULL)
191 server_redraw_client(c);
192 }
8fade26b » Karl Ferdinand Ebert
2009-11-22 Imported Upstream version 1.1
193
194 return (0);
195 }
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
196
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
197 /* Unset an option. */
198 int
199 cmd_set_option_unset(struct cmd *self, struct cmd_ctx *ctx,
200 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
201 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
202 struct args *args = self->args;
203
204 if (args_has(args, 'g')) {
205 ctx->error(ctx, "can't unset global option: %s", oe->name);
206 return (-1);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
207 }
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
208 if (value != NULL) {
209 ctx->error(ctx, "value passed to unset option: %s", oe->name);
210 return (-1);
211 }
212
213 options_remove(oo, oe->name);
214 ctx->info(ctx, "unset option: %s", oe->name);
215 return (0);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
216 }
217
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
218 /* Set an option. */
219 int
220 cmd_set_option_set(struct cmd *self, struct cmd_ctx *ctx,
221 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
222 {
223 struct options_entry *o;
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
224 const char *s;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
225
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
226 if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) {
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
227 ctx->error(ctx, "empty value");
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
228 return (-1);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
229 }
230
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
231 o = NULL;
232 switch (oe->type) {
233 case OPTIONS_TABLE_STRING:
234 o = cmd_set_option_string(self, ctx, oe, oo, value);
235 break;
236 case OPTIONS_TABLE_NUMBER:
237 o = cmd_set_option_number(self, ctx, oe, oo, value);
238 break;
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
239 case OPTIONS_TABLE_KEY:
240 o = cmd_set_option_key(self, ctx, oe, oo, value);
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
241 break;
242 case OPTIONS_TABLE_COLOUR:
243 o = cmd_set_option_colour(self, ctx, oe, oo, value);
244 break;
245 case OPTIONS_TABLE_ATTRIBUTES:
246 o = cmd_set_option_attributes(self, ctx, oe, oo, value);
247 break;
248 case OPTIONS_TABLE_FLAG:
249 o = cmd_set_option_flag(self, ctx, oe, oo, value);
250 break;
251 case OPTIONS_TABLE_CHOICE:
252 o = cmd_set_option_choice(self, ctx, oe, oo, value);
253 break;
254 }
255 if (o == NULL)
256 return (-1);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
257
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
258 s = options_table_print_entry(oe, o);
259 ctx->info(ctx, "set option: %s -> %s", oe->name, s);
260 return (0);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
261 }
262
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
263 /* Set a string option. */
264 struct options_entry *
265 cmd_set_option_string(struct cmd *self, unused struct cmd_ctx *ctx,
266 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
267 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
268 struct args *args = self->args;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
269 struct options_entry *o;
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
270 char *oldval, *newval;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
271
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
272 if (args_has(args, 'a')) {
273 oldval = options_get_string(oo, oe->name);
274 xasprintf(&newval, "%s%s", oldval, value);
275 } else
276 newval = xstrdup(value);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
277
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
278 o = options_set_string(oo, oe->name, "%s", newval);
279
280 xfree(newval);
281 return (o);
282 }
283
284 /* Set a number option. */
285 struct options_entry *
286 cmd_set_option_number(unused struct cmd *self, struct cmd_ctx *ctx,
287 const struct options_table_entry *oe, struct options *oo, const char *value)
288 {
289 long long ll;
290 const char *errstr;
291
292 ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
293 if (errstr != NULL) {
294 ctx->error(ctx, "value is %s: %s", errstr, value);
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
295 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
296 }
297
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
298 return (options_set_number(oo, oe->name, ll));
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
299 }
300
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
301 /* Set a key option. */
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
302 struct options_entry *
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
303 cmd_set_option_key(unused struct cmd *self, struct cmd_ctx *ctx,
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
304 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
305 {
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
306 int key;
307
308 if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
309 ctx->error(ctx, "bad key: %s", value);
310 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
311 }
312
0f2e3364 » orebokech
2012-01-21 Import upstream snapshot from SVN r2670
313 return (options_set_number(oo, oe->name, key));
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
314 }
315
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
316 /* Set a colour option. */
317 struct options_entry *
318 cmd_set_option_colour(unused struct cmd *self, struct cmd_ctx *ctx,
319 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
320 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
321 int colour;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
322
323 if ((colour = colour_fromstring(value)) == -1) {
324 ctx->error(ctx, "bad colour: %s", value);
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
325 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
326 }
327
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
328 return (options_set_number(oo, oe->name, colour));
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
329 }
330
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
331 /* Set an attributes option. */
332 struct options_entry *
333 cmd_set_option_attributes(unused struct cmd *self, struct cmd_ctx *ctx,
334 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
335 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
336 int attr;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
337
338 if ((attr = attributes_fromstring(value)) == -1) {
339 ctx->error(ctx, "bad attributes: %s", value);
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
340 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
341 }
342
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
343 return (options_set_number(oo, oe->name, attr));
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
344 }
345
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
346 /* Set a flag option. */
347 struct options_entry *
348 cmd_set_option_flag(unused struct cmd *self, struct cmd_ctx *ctx,
349 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
350 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
351 int flag;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
352
353 if (value == NULL || *value == '\0')
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
354 flag = !options_get_number(oo, oe->name);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
355 else {
356 if ((value[0] == '1' && value[1] == '\0') ||
357 strcasecmp(value, "on") == 0 ||
358 strcasecmp(value, "yes") == 0)
359 flag = 1;
360 else if ((value[0] == '0' && value[1] == '\0') ||
361 strcasecmp(value, "off") == 0 ||
362 strcasecmp(value, "no") == 0)
363 flag = 0;
364 else {
365 ctx->error(ctx, "bad value: %s", value);
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
366 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
367 }
368 }
369
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
370 return (options_set_number(oo, oe->name, flag));
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
371 }
372
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
373 /* Set a choice option. */
374 struct options_entry *
375 cmd_set_option_choice(unused struct cmd *self, struct cmd_ctx *ctx,
376 const struct options_table_entry *oe, struct options *oo, const char *value)
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
377 {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
378 const char **choicep;
379 int n, choice = -1;
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
380
381 n = 0;
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
382 for (choicep = oe->choices; *choicep != NULL; choicep++) {
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
383 n++;
384 if (strncmp(*choicep, value, strlen(value)) != 0)
385 continue;
386
387 if (choice != -1) {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
388 ctx->error(ctx, "ambiguous value: %s", value);
389 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
390 }
391 choice = n - 1;
392 }
393 if (choice == -1) {
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
394 ctx->error(ctx, "unknown value: %s", value);
395 return (NULL);
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
396 }
397
7216c39c » kfebert
2011-07-09 Imported Upstream version 1.5
398 return (options_set_number(oo, oe->name, choice));
dda8e8e5 » kfebert
2010-03-13 Imported Upstream version 1.2
399 }
Something went wrong with that request. Please try again.