Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.40
1.40 ! nicm 1: /* $OpenBSD: cmd-set-option.c,v 1.39 2010/09/26 20:43:30 nicm Exp $ */
1.1 nicm 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:
1.27 nicm 32: const char *cmd_set_option_print(
33: const struct set_option_entry *, struct options_entry *);
34: void cmd_set_option_string(struct cmd_ctx *,
35: struct options *, const struct set_option_entry *, char *, int);
36: void cmd_set_option_number(struct cmd_ctx *,
1.28 nicm 37: struct options *, const struct set_option_entry *, char *);
1.27 nicm 38: void cmd_set_option_keys(struct cmd_ctx *,
1.28 nicm 39: struct options *, const struct set_option_entry *, char *);
1.27 nicm 40: void cmd_set_option_colour(struct cmd_ctx *,
1.28 nicm 41: struct options *, const struct set_option_entry *, char *);
1.27 nicm 42: void cmd_set_option_attributes(struct cmd_ctx *,
1.28 nicm 43: struct options *, const struct set_option_entry *, char *);
1.27 nicm 44: void cmd_set_option_flag(struct cmd_ctx *,
1.28 nicm 45: struct options *, const struct set_option_entry *, char *);
1.27 nicm 46: void cmd_set_option_choice(struct cmd_ctx *,
1.28 nicm 47: struct options *, const struct set_option_entry *, char *);
1.27 nicm 48:
1.1 nicm 49: const struct cmd_entry cmd_set_option_entry = {
50: "set-option", "set",
1.29 nicm 51: "[-agsuw] [-t target-session|target-window] option [value]",
52: CMD_ARG12, "agsuw",
1.1 nicm 53: NULL,
1.14 nicm 54: cmd_target_parse,
1.1 nicm 55: cmd_set_option_exec,
1.14 nicm 56: cmd_target_free,
57: cmd_target_print
1.1 nicm 58: };
59:
1.27 nicm 60: const char *set_option_mode_keys_list[] = {
61: "emacs", "vi", NULL
62: };
63: const char *set_option_clock_mode_style_list[] = {
64: "12", "24", NULL
65: };
1.1 nicm 66: const char *set_option_status_keys_list[] = {
67: "emacs", "vi", NULL
68: };
1.8 nicm 69: const char *set_option_status_justify_list[] = {
70: "left", "centre", "right", NULL
71: };
1.1 nicm 72: const char *set_option_bell_action_list[] = {
73: "none", "any", "current", NULL
74: };
1.27 nicm 75:
1.29 nicm 76: const struct set_option_entry set_option_table[] = {
1.31 nicm 77: { "escape-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
1.39 nicm 78: { "exit-unattached", SET_OPTION_FLAG, 0, 0, NULL },
1.29 nicm 79: { "quiet", SET_OPTION_FLAG, 0, 0, NULL },
1.30 nicm 80: { NULL, 0, 0, 0, NULL }
1.29 nicm 81: };
82:
1.27 nicm 83: const struct set_option_entry set_session_option_table[] = {
1.15 nicm 84: { "base-index", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
1.1 nicm 85: { "bell-action", SET_OPTION_CHOICE, 0, 0, set_option_bell_action_list },
86: { "buffer-limit", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
87: { "default-command", SET_OPTION_STRING, 0, 0, NULL },
88: { "default-path", SET_OPTION_STRING, 0, 0, NULL },
1.17 nicm 89: { "default-shell", SET_OPTION_STRING, 0, 0, NULL },
1.4 nicm 90: { "default-terminal", SET_OPTION_STRING, 0, 0, NULL },
1.39 nicm 91: { "destroy-unattached", SET_OPTION_FLAG, 0, 0, NULL },
1.37 nicm 92: { "detach-on-destroy", SET_OPTION_FLAG, 0, 0, NULL },
1.16 nicm 93: { "display-panes-colour", SET_OPTION_COLOUR, 0, 0, NULL },
1.33 nicm 94: { "display-panes-active-colour", SET_OPTION_COLOUR, 0, 0, NULL },
1.16 nicm 95: { "display-panes-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
1.1 nicm 96: { "display-time", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
97: { "history-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
98: { "lock-after-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
1.20 nicm 99: { "lock-command", SET_OPTION_STRING, 0, 0, NULL },
1.21 nicm 100: { "lock-server", SET_OPTION_FLAG, 0, 0, NULL },
1.1 nicm 101: { "message-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
102: { "message-bg", SET_OPTION_COLOUR, 0, 0, NULL },
103: { "message-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.26 nicm 104: { "message-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
1.22 nicm 105: { "mouse-select-pane", SET_OPTION_FLAG, 0, 0, NULL },
1.32 nicm 106: { "pane-active-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
107: { "pane-active-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
108: { "pane-border-bg", SET_OPTION_COLOUR, 0, 0, NULL },
109: { "pane-border-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.19 nicm 110: { "prefix", SET_OPTION_KEYS, 0, 0, NULL },
1.1 nicm 111: { "repeat-time", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
112: { "set-remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
113: { "set-titles", SET_OPTION_FLAG, 0, 0, NULL },
1.18 nicm 114: { "set-titles-string", SET_OPTION_STRING, 0, 0, NULL },
1.1 nicm 115: { "status", SET_OPTION_FLAG, 0, 0, NULL },
116: { "status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
117: { "status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
118: { "status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
119: { "status-interval", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
1.28 nicm 120: { "status-justify",
1.8 nicm 121: SET_OPTION_CHOICE, 0, 0, set_option_status_justify_list },
1.1 nicm 122: { "status-keys", SET_OPTION_CHOICE, 0, 0, set_option_status_keys_list },
123: { "status-left", SET_OPTION_STRING, 0, 0, NULL },
1.12 nicm 124: { "status-left-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
125: { "status-left-bg", SET_OPTION_COLOUR, 0, 0, NULL },
126: { "status-left-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.1 nicm 127: { "status-left-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
128: { "status-right", SET_OPTION_STRING, 0, 0, NULL },
1.12 nicm 129: { "status-right-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
130: { "status-right-bg", SET_OPTION_COLOUR, 0, 0, NULL },
131: { "status-right-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.1 nicm 132: { "status-right-length", SET_OPTION_NUMBER, 0, SHRT_MAX, NULL },
1.28 nicm 133: { "status-utf8", SET_OPTION_FLAG, 0, 0, NULL },
1.10 nicm 134: { "terminal-overrides", SET_OPTION_STRING, 0, 0, NULL },
1.13 nicm 135: { "update-environment", SET_OPTION_STRING, 0, 0, NULL },
1.7 nicm 136: { "visual-activity", SET_OPTION_FLAG, 0, 0, NULL },
1.28 nicm 137: { "visual-bell", SET_OPTION_FLAG, 0, 0, NULL },
1.7 nicm 138: { "visual-content", SET_OPTION_FLAG, 0, 0, NULL },
1.40 ! nicm 139: { "visual-silence", SET_OPTION_FLAG, 0, 0, NULL },
1.6 nicm 140: { NULL, 0, 0, 0, NULL }
1.1 nicm 141: };
142:
1.27 nicm 143: const struct set_option_entry set_window_option_table[] = {
144: { "aggressive-resize", SET_OPTION_FLAG, 0, 0, NULL },
1.34 nicm 145: { "alternate-screen", SET_OPTION_FLAG, 0, 0, NULL },
1.27 nicm 146: { "automatic-rename", SET_OPTION_FLAG, 0, 0, NULL },
147: { "clock-mode-colour", SET_OPTION_COLOUR, 0, 0, NULL },
148: { "clock-mode-style",
149: SET_OPTION_CHOICE, 0, 0, set_option_clock_mode_style_list },
150: { "force-height", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
151: { "force-width", SET_OPTION_NUMBER, 0, INT_MAX, NULL },
152: { "main-pane-height", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
153: { "main-pane-width", SET_OPTION_NUMBER, 1, INT_MAX, NULL },
154: { "mode-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
155: { "mode-bg", SET_OPTION_COLOUR, 0, 0, NULL },
156: { "mode-fg", SET_OPTION_COLOUR, 0, 0, NULL },
157: { "mode-keys", SET_OPTION_CHOICE, 0, 0, set_option_mode_keys_list },
158: { "mode-mouse", SET_OPTION_FLAG, 0, 0, NULL },
159: { "monitor-activity", SET_OPTION_FLAG, 0, 0, NULL },
160: { "monitor-content", SET_OPTION_STRING, 0, 0, NULL },
1.40 ! nicm 161: { "monitor-silence",SET_OPTION_NUMBER, 0, INT_MAX, NULL},
1.27 nicm 162: { "remain-on-exit", SET_OPTION_FLAG, 0, 0, NULL },
163: { "synchronize-panes", SET_OPTION_FLAG, 0, 0, NULL },
164: { "utf8", SET_OPTION_FLAG, 0, 0, NULL },
1.36 nicm 165: { "window-status-alert-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
166: { "window-status-alert-bg", SET_OPTION_COLOUR, 0, 0, NULL },
167: { "window-status-alert-fg", SET_OPTION_COLOUR, 0, 0, NULL },
1.27 nicm 168: { "window-status-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
169: { "window-status-bg", SET_OPTION_COLOUR, 0, 0, NULL },
170: { "window-status-current-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL },
171: { "window-status-current-bg", SET_OPTION_COLOUR, 0, 0, NULL },
172: { "window-status-current-fg", SET_OPTION_COLOUR, 0, 0, NULL },
173: { "window-status-current-format", SET_OPTION_STRING, 0, 0, NULL },
174: { "window-status-fg", SET_OPTION_COLOUR, 0, 0, NULL },
175: { "window-status-format", SET_OPTION_STRING, 0, 0, NULL },
1.35 nicm 176: { "word-separators", SET_OPTION_STRING, 0, 0, NULL },
1.27 nicm 177: { "xterm-keys", SET_OPTION_FLAG, 0, 0, NULL },
178: { NULL, 0, 0, 0, NULL }
179: };
180:
1.1 nicm 181: int
182: cmd_set_option_exec(struct cmd *self, struct cmd_ctx *ctx)
183: {
1.14 nicm 184: struct cmd_target_data *data = self->data;
1.27 nicm 185: const struct set_option_entry *table;
1.1 nicm 186: struct session *s;
1.27 nicm 187: struct winlink *wl;
1.1 nicm 188: struct client *c;
189: struct options *oo;
1.6 nicm 190: const struct set_option_entry *entry, *opt;
1.24 nicm 191: struct jobs *jobs;
192: struct job *job, *nextjob;
1.1 nicm 193: u_int i;
1.24 nicm 194: int try_again;
1.1 nicm 195:
1.29 nicm 196: if (cmd_check_flag(data->chflags, 's')) {
197: oo = &global_options;
198: table = set_option_table;
199: } else if (cmd_check_flag(data->chflags, 'w')) {
1.27 nicm 200: table = set_window_option_table;
201: if (cmd_check_flag(data->chflags, 'g'))
202: oo = &global_w_options;
203: else {
204: wl = cmd_find_window(ctx, data->target, NULL);
205: if (wl == NULL)
206: return (-1);
207: oo = &wl->window->options;
208: }
209: } else {
210: table = set_session_option_table;
211: if (cmd_check_flag(data->chflags, 'g'))
212: oo = &global_s_options;
213: else {
214: s = cmd_find_session(ctx, data->target);
215: if (s == NULL)
216: return (-1);
217: oo = &s->options;
218: }
1.1 nicm 219: }
220:
1.14 nicm 221: if (*data->arg == '\0') {
1.1 nicm 222: ctx->error(ctx, "invalid option");
223: return (-1);
224: }
225:
226: entry = NULL;
1.27 nicm 227: for (opt = table; opt->name != NULL; opt++) {
1.14 nicm 228: if (strncmp(opt->name, data->arg, strlen(data->arg)) != 0)
1.1 nicm 229: continue;
230: if (entry != NULL) {
1.14 nicm 231: ctx->error(ctx, "ambiguous option: %s", data->arg);
1.1 nicm 232: return (-1);
233: }
1.6 nicm 234: entry = opt;
1.1 nicm 235:
236: /* Bail now if an exact match. */
1.14 nicm 237: if (strcmp(entry->name, data->arg) == 0)
1.1 nicm 238: break;
239: }
240: if (entry == NULL) {
1.14 nicm 241: ctx->error(ctx, "unknown option: %s", data->arg);
1.1 nicm 242: return (-1);
243: }
244:
1.25 nicm 245: if (cmd_check_flag(data->chflags, 'u')) {
246: if (cmd_check_flag(data->chflags, 'g')) {
1.1 nicm 247: ctx->error(ctx,
248: "can't unset global option: %s", entry->name);
249: return (-1);
250: }
1.14 nicm 251: if (data->arg2 != NULL) {
1.1 nicm 252: ctx->error(ctx,
253: "value passed to unset option: %s", entry->name);
254: return (-1);
255: }
256:
257: options_remove(oo, entry->name);
258: ctx->info(ctx, "unset option: %s", entry->name);
259: } else {
260: switch (entry->type) {
261: case SET_OPTION_STRING:
1.27 nicm 262: cmd_set_option_string(ctx, oo, entry,
1.25 nicm 263: data->arg2, cmd_check_flag(data->chflags, 'a'));
1.1 nicm 264: break;
265: case SET_OPTION_NUMBER:
1.27 nicm 266: cmd_set_option_number(ctx, oo, entry, data->arg2);
1.1 nicm 267: break;
1.19 nicm 268: case SET_OPTION_KEYS:
1.27 nicm 269: cmd_set_option_keys(ctx, oo, entry, data->arg2);
1.1 nicm 270: break;
271: case SET_OPTION_COLOUR:
1.27 nicm 272: cmd_set_option_colour(ctx, oo, entry, data->arg2);
1.1 nicm 273: break;
274: case SET_OPTION_ATTRIBUTES:
1.27 nicm 275: cmd_set_option_attributes(ctx, oo, entry, data->arg2);
1.1 nicm 276: break;
277: case SET_OPTION_FLAG:
1.27 nicm 278: cmd_set_option_flag(ctx, oo, entry, data->arg2);
1.1 nicm 279: break;
280: case SET_OPTION_CHOICE:
1.27 nicm 281: cmd_set_option_choice(ctx, oo, entry, data->arg2);
1.1 nicm 282: break;
283: }
284: }
285:
286: recalculate_sizes();
1.27 nicm 287: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
288: c = ARRAY_ITEM(&clients, i);
289: if (c != NULL && c->session != NULL)
290: server_redraw_client(c);
291: }
1.24 nicm 292:
1.28 nicm 293: /*
1.24 nicm 294: * Special-case: kill all persistent jobs if status-left, status-right
295: * or set-titles-string have changed. Persistent jobs are only used by
296: * the status line at the moment so this works XXX.
297: */
298: if (strcmp(entry->name, "status-left") == 0 ||
299: strcmp(entry->name, "status-right") == 0 ||
1.38 nicm 300: strcmp(entry->name, "status") == 0 ||
1.27 nicm 301: strcmp(entry->name, "set-titles-string") == 0 ||
302: strcmp(entry->name, "window-status-format") == 0) {
1.24 nicm 303: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
304: c = ARRAY_ITEM(&clients, i);
305: if (c == NULL || c->session == NULL)
306: continue;
307:
308: jobs = &c->status_jobs;
309: do {
1.28 nicm 310: try_again = 0;
1.24 nicm 311: job = RB_ROOT(jobs);
312: while (job != NULL) {
313: nextjob = RB_NEXT(jobs, jobs, job);
314: if (job->flags & JOB_PERSIST) {
315: job_remove(jobs, job);
316: try_again = 1;
317: break;
318: }
319: job = nextjob;
320: }
321: } while (try_again);
1.1 nicm 322: server_redraw_client(c);
1.23 nicm 323: }
1.1 nicm 324: }
325:
326: return (0);
1.27 nicm 327: }
328:
329: const char *
330: cmd_set_option_print(
331: const struct set_option_entry *entry, struct options_entry *o)
332: {
333: static char out[BUFSIZ];
334: const char *s;
335: struct keylist *keylist;
336: u_int i;
337:
338: *out = '\0';
339: switch (entry->type) {
340: case SET_OPTION_STRING:
341: xsnprintf(out, sizeof out, "\"%s\"", o->str);
342: break;
343: case SET_OPTION_NUMBER:
344: xsnprintf(out, sizeof out, "%lld", o->num);
345: break;
346: case SET_OPTION_KEYS:
347: keylist = o->data;
348: for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
349: strlcat(out, key_string_lookup_key(
350: ARRAY_ITEM(keylist, i)), sizeof out);
351: if (i != ARRAY_LENGTH(keylist) - 1)
352: strlcat(out, ",", sizeof out);
353: }
354: break;
355: case SET_OPTION_COLOUR:
356: s = colour_tostring(o->num);
357: xsnprintf(out, sizeof out, "%s", s);
358: break;
359: case SET_OPTION_ATTRIBUTES:
360: s = attributes_tostring(o->num);
361: xsnprintf(out, sizeof out, "%s", s);
362: break;
363: case SET_OPTION_FLAG:
364: if (o->num)
365: strlcpy(out, "on", sizeof out);
366: else
367: strlcpy(out, "off", sizeof out);
368: break;
369: case SET_OPTION_CHOICE:
370: s = entry->choices[o->num];
371: xsnprintf(out, sizeof out, "%s", s);
372: break;
373: }
374: return (out);
375: }
376:
377: void
378: cmd_set_option_string(struct cmd_ctx *ctx, struct options *oo,
379: const struct set_option_entry *entry, char *value, int append)
380: {
381: struct options_entry *o;
382: char *oldvalue, *newvalue;
383:
384: if (value == NULL) {
385: ctx->error(ctx, "empty value");
386: return;
387: }
388:
389: if (append) {
390: oldvalue = options_get_string(oo, entry->name);
391: xasprintf(&newvalue, "%s%s", oldvalue, value);
392: } else
393: newvalue = value;
1.28 nicm 394:
1.27 nicm 395: o = options_set_string(oo, entry->name, "%s", newvalue);
396: ctx->info(ctx,
397: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
398:
399: if (newvalue != value)
400: xfree(newvalue);
401: }
402:
403: void
404: cmd_set_option_number(struct cmd_ctx *ctx, struct options *oo,
405: const struct set_option_entry *entry, char *value)
406: {
407: struct options_entry *o;
408: long long number;
409: const char *errstr;
410:
411: if (value == NULL) {
412: ctx->error(ctx, "empty value");
413: return;
414: }
415:
416: number = strtonum(value, entry->minimum, entry->maximum, &errstr);
417: if (errstr != NULL) {
418: ctx->error(ctx, "value is %s: %s", errstr, value);
419: return;
420: }
421:
422: o = options_set_number(oo, entry->name, number);
423: ctx->info(ctx,
424: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
425: }
426:
427: void
428: cmd_set_option_keys(struct cmd_ctx *ctx, struct options *oo,
429: const struct set_option_entry *entry, char *value)
430: {
431: struct options_entry *o;
432: struct keylist *keylist;
433: char *copyvalue, *ptr, *str;
434: int key;
435:
436: if (value == NULL) {
437: ctx->error(ctx, "empty value");
438: return;
439: }
440:
441: keylist = xmalloc(sizeof *keylist);
442: ARRAY_INIT(keylist);
443:
444: ptr = copyvalue = xstrdup(value);
445: while ((str = strsep(&ptr, ",")) != NULL) {
446: if ((key = key_string_lookup_string(str)) == KEYC_NONE) {
447: xfree(keylist);
448: ctx->error(ctx, "unknown key: %s", str);
449: xfree(copyvalue);
450: return;
451: }
452: ARRAY_ADD(keylist, key);
453: }
454: xfree(copyvalue);
455:
456: o = options_set_data(oo, entry->name, keylist, xfree);
457: ctx->info(ctx,
458: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
459: }
460:
461: void
462: cmd_set_option_colour(struct cmd_ctx *ctx, struct options *oo,
463: const struct set_option_entry *entry, char *value)
464: {
465: struct options_entry *o;
466: int colour;
467:
468: if (value == NULL) {
469: ctx->error(ctx, "empty value");
470: return;
471: }
472:
473: if ((colour = colour_fromstring(value)) == -1) {
474: ctx->error(ctx, "bad colour: %s", value);
475: return;
476: }
477:
478: o = options_set_number(oo, entry->name, colour);
479: ctx->info(ctx,
480: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
481: }
482:
483: void
484: cmd_set_option_attributes(struct cmd_ctx *ctx, struct options *oo,
485: const struct set_option_entry *entry, char *value)
486: {
487: struct options_entry *o;
488: int attr;
489:
490: if (value == NULL) {
491: ctx->error(ctx, "empty value");
492: return;
493: }
494:
495: if ((attr = attributes_fromstring(value)) == -1) {
496: ctx->error(ctx, "bad attributes: %s", value);
497: return;
498: }
499:
500: o = options_set_number(oo, entry->name, attr);
501: ctx->info(ctx,
502: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
503: }
504:
505: void
506: cmd_set_option_flag(struct cmd_ctx *ctx, struct options *oo,
507: const struct set_option_entry *entry, char *value)
508: {
509: struct options_entry *o;
510: int flag;
511:
512: if (value == NULL || *value == '\0')
513: flag = !options_get_number(oo, entry->name);
514: else {
515: if ((value[0] == '1' && value[1] == '\0') ||
516: strcasecmp(value, "on") == 0 ||
517: strcasecmp(value, "yes") == 0)
518: flag = 1;
519: else if ((value[0] == '0' && value[1] == '\0') ||
520: strcasecmp(value, "off") == 0 ||
521: strcasecmp(value, "no") == 0)
522: flag = 0;
523: else {
524: ctx->error(ctx, "bad value: %s", value);
525: return;
526: }
527: }
528:
529: o = options_set_number(oo, entry->name, flag);
530: ctx->info(ctx,
531: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
532: }
533:
534: void
535: cmd_set_option_choice(struct cmd_ctx *ctx, struct options *oo,
536: const struct set_option_entry *entry, char *value)
537: {
538: struct options_entry *o;
539: const char **choicep;
540: int n, choice = -1;
541:
542: if (value == NULL) {
543: ctx->error(ctx, "empty value");
544: return;
545: }
546:
547: n = 0;
548: for (choicep = entry->choices; *choicep != NULL; choicep++) {
549: n++;
550: if (strncmp(*choicep, value, strlen(value)) != 0)
551: continue;
552:
553: if (choice != -1) {
554: ctx->error(ctx, "ambiguous option value: %s", value);
555: return;
556: }
557: choice = n - 1;
558: }
559: if (choice == -1) {
560: ctx->error(ctx, "unknown option value: %s", value);
561: return;
562: }
563:
564: o = options_set_number(oo, entry->name, choice);
565: ctx->info(ctx,
566: "set option: %s -> %s", o->name, cmd_set_option_print(entry, o));
1.1 nicm 567: }