[BACK]Return to cmd-set-option.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-set-option.c, Revision 1.37

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