[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.103

1.103   ! nicm        1: /* $OpenBSD: cmd-set-option.c,v 1.102 2016/11/04 18:56:25 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.93      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.101     nicm       30: static enum cmd_retval cmd_set_option_exec(struct cmd *, struct cmdq_item *);
1.59      nicm       31:
1.101     nicm       32: static enum cmd_retval cmd_set_option_user(struct cmd *, struct cmdq_item *,
                     33:                            const char *, const char *);
1.1       nicm       34:
1.101     nicm       35: static int     cmd_set_option_unset(struct cmd *, struct cmdq_item *,
1.99      nicm       36:                    const struct options_table_entry *, struct options *,
                     37:                    const char *);
1.101     nicm       38: static int     cmd_set_option_set(struct cmd *, struct cmdq_item *,
1.99      nicm       39:                    const struct options_table_entry *, struct options *,
                     40:                    const char *);
1.43      nicm       41:
1.101     nicm       42: static struct options_entry *cmd_set_option_string(struct cmd *,
                     43:            struct cmdq_item *, const struct options_table_entry *,
                     44:            struct options *, const char *);
                     45: static struct options_entry *cmd_set_option_number(struct cmd *,
                     46:            struct cmdq_item *, const struct options_table_entry *,
                     47:            struct options *, const char *);
                     48: static struct options_entry *cmd_set_option_key(struct cmd *,
                     49:            struct cmdq_item *, const struct options_table_entry *,
                     50:            struct options *, const char *);
                     51: static struct options_entry *cmd_set_option_colour(struct cmd *,
                     52:            struct cmdq_item *, const struct options_table_entry *,
                     53:            struct options *, const char *);
1.99      nicm       54: static struct options_entry *cmd_set_option_attributes(struct cmd *,
1.101     nicm       55:            struct cmdq_item *, const struct options_table_entry *,
                     56:            struct options *, const char *);
                     57: static struct options_entry *cmd_set_option_flag(struct cmd *,
                     58:            struct cmdq_item *, const struct options_table_entry *,
                     59:            struct options *, const char *);
                     60: static struct options_entry *cmd_set_option_choice(struct cmd *,
                     61:            struct cmdq_item *, const struct options_table_entry *,
                     62:            struct options *, const char *);
                     63: static struct options_entry *cmd_set_option_style(struct cmd *,
                     64:            struct cmdq_item *, const struct options_table_entry *,
1.99      nicm       65:            struct options *, const char *);
1.27      nicm       66:
1.1       nicm       67: const struct cmd_entry cmd_set_option_entry = {
1.91      nicm       68:        .name = "set-option",
                     69:        .alias = "set",
                     70:
                     71:        .args = { "agoqst:uw", 1, 2 },
                     72:        .usage = "[-agosquw] [-t target-window] option [value]",
                     73:
1.92      nicm       74:        .tflag = CMD_WINDOW_CANFAIL,
                     75:
1.100     nicm       76:        .flags = CMD_AFTERHOOK,
1.91      nicm       77:        .exec = cmd_set_option_exec
1.1       nicm       78: };
                     79:
1.46      nicm       80: const struct cmd_entry cmd_set_window_option_entry = {
1.91      nicm       81:        .name = "set-window-option",
                     82:        .alias = "setw",
                     83:
                     84:        .args = { "agoqt:u", 1, 2 },
                     85:        .usage = "[-agoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
                     86:
1.92      nicm       87:        .tflag = CMD_WINDOW_CANFAIL,
                     88:
1.100     nicm       89:        .flags = CMD_AFTERHOOK,
1.91      nicm       90:        .exec = cmd_set_option_exec
1.46      nicm       91: };
                     92:
1.99      nicm       93: static enum cmd_retval
1.101     nicm       94: cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       95: {
1.44      nicm       96:        struct args                             *args = self->args;
1.101     nicm       97:        struct session                          *s = item->state.tflag.s;
                     98:        struct winlink                          *wl = item->state.tflag.wl;
1.90      nicm       99:        struct window                           *w;
                    100:        struct client                           *c;
1.87      nicm      101:        const struct options_table_entry        *oe;
1.43      nicm      102:        struct options                          *oo;
1.94      nicm      103:        const char                              *optstr, *valstr, *target;
1.1       nicm      104:
1.50      nicm      105:        /* Get the option name and value. */
                    106:        optstr = args->argv[0];
                    107:        if (*optstr == '\0') {
1.101     nicm      108:                cmdq_error(item, "invalid option");
1.57      nicm      109:                return (CMD_RETURN_ERROR);
1.50      nicm      110:        }
                    111:        if (args->argc < 2)
                    112:                valstr = NULL;
                    113:        else
                    114:                valstr = args->argv[1];
                    115:
1.59      nicm      116:        /* Is this a user option? */
                    117:        if (*optstr == '@')
1.101     nicm      118:                return (cmd_set_option_user(self, item, optstr, valstr));
1.59      nicm      119:
1.103   ! nicm      120:        /* Find the option entry. */
1.87      nicm      121:        oe = NULL;
                    122:        if (options_table_find(optstr, &oe) != 0) {
1.76      nicm      123:                if (!args_has(args, 'q')) {
1.101     nicm      124:                        cmdq_error(item, "ambiguous option: %s", optstr);
1.76      nicm      125:                        return (CMD_RETURN_ERROR);
                    126:                }
                    127:                return (CMD_RETURN_NORMAL);
1.50      nicm      128:        }
                    129:        if (oe == NULL) {
1.67      nicm      130:                if (!args_has(args, 'q')) {
1.101     nicm      131:                        cmdq_error(item, "unknown option: %s", optstr);
1.67      nicm      132:                        return (CMD_RETURN_ERROR);
                    133:                }
                    134:                return (CMD_RETURN_NORMAL);
1.50      nicm      135:        }
                    136:
1.87      nicm      137:        /* Work out the tree from the scope of the option. */
                    138:        if (oe->scope == OPTIONS_TABLE_SERVER)
1.84      nicm      139:                oo = global_options;
1.87      nicm      140:        else if (oe->scope == OPTIONS_TABLE_WINDOW) {
1.44      nicm      141:                if (args_has(self->args, 'g'))
1.84      nicm      142:                        oo = global_w_options;
1.94      nicm      143:                else if (wl == NULL) {
                    144:                        target = args_get(args, 't');
                    145:                        if (target != NULL) {
1.101     nicm      146:                                cmdq_error(item, "no such window: %s",
1.94      nicm      147:                                    target);
                    148:                        } else
1.101     nicm      149:                                cmdq_error(item, "no current window");
1.94      nicm      150:                        return (CMD_RETURN_ERROR);
                    151:                } else
1.84      nicm      152:                        oo = wl->window->options;
1.87      nicm      153:        } else if (oe->scope == OPTIONS_TABLE_SESSION) {
1.44      nicm      154:                if (args_has(self->args, 'g'))
1.84      nicm      155:                        oo = global_s_options;
1.94      nicm      156:                else if (s == NULL) {
                    157:                        target = args_get(args, 't');
                    158:                        if (target != NULL) {
1.101     nicm      159:                                cmdq_error(item, "no such session: %s",
1.94      nicm      160:                                    target);
                    161:                        } else
1.101     nicm      162:                                cmdq_error(item, "no current session");
1.94      nicm      163:                        return (CMD_RETURN_ERROR);
                    164:                } else
1.84      nicm      165:                        oo = s->options;
1.50      nicm      166:        } else {
1.101     nicm      167:                cmdq_error(item, "unknown table");
1.57      nicm      168:                return (CMD_RETURN_ERROR);
1.1       nicm      169:        }
                    170:
1.43      nicm      171:        /* Unset or set the option. */
1.44      nicm      172:        if (args_has(args, 'u')) {
1.101     nicm      173:                if (cmd_set_option_unset(self, item, oe, oo, valstr) != 0)
1.57      nicm      174:                        return (CMD_RETURN_ERROR);
1.43      nicm      175:        } else {
1.61      nicm      176:                if (args_has(args, 'o') && options_find1(oo, optstr) != NULL) {
1.67      nicm      177:                        if (!args_has(args, 'q')) {
1.101     nicm      178:                                cmdq_error(item, "already set: %s", optstr);
1.67      nicm      179:                                return (CMD_RETURN_ERROR);
                    180:                        }
1.61      nicm      181:                        return (CMD_RETURN_NORMAL);
                    182:                }
1.101     nicm      183:                if (cmd_set_option_set(self, item, oe, oo, valstr) != 0)
1.57      nicm      184:                        return (CMD_RETURN_ERROR);
1.55      nicm      185:        }
                    186:
1.103   ! nicm      187:        /* Update timers and so on for various options. */
1.65      nicm      188:        if (strcmp(oe->name, "automatic-rename") == 0) {
1.72      nicm      189:                RB_FOREACH(w, windows, &windows) {
1.102     nicm      190:                        if (w->active == NULL)
                    191:                                continue;
1.84      nicm      192:                        if (options_get_number(w->options, "automatic-rename"))
1.81      nicm      193:                                w->active->flags |= PANE_CHANGED;
1.55      nicm      194:                }
1.89      nicm      195:        }
                    196:        if (strcmp(oe->name, "key-table") == 0) {
                    197:                TAILQ_FOREACH(c, &clients, entry)
                    198:                        server_client_set_key_table(c, NULL);
1.1       nicm      199:        }
1.77      nicm      200:        if (strcmp(oe->name, "status") == 0 ||
                    201:            strcmp(oe->name, "status-interval") == 0)
                    202:                status_timer_start_all();
1.82      nicm      203:        if (strcmp(oe->name, "monitor-silence") == 0)
                    204:                alerts_reset_all();
1.96      nicm      205:        if (strcmp(oe->name, "window-style") == 0 ||
                    206:            strcmp(oe->name, "window-active-style") == 0) {
                    207:                RB_FOREACH(w, windows, &windows)
                    208:                        w->flags |= WINDOW_STYLECHANGED;
                    209:        }
1.95      nicm      210:        if (strcmp(oe->name, "pane-border-status") == 0) {
                    211:                RB_FOREACH(w, windows, &windows)
                    212:                        layout_fix_panes(w, w->sx, w->sy);
                    213:        }
1.1       nicm      214:
1.43      nicm      215:        /* Update sizes and redraw. May not need it but meh. */
1.1       nicm      216:        recalculate_sizes();
1.74      nicm      217:        TAILQ_FOREACH(c, &clients, entry) {
                    218:                if (c->session != NULL)
1.27      nicm      219:                        server_redraw_client(c);
1.1       nicm      220:        }
                    221:
1.57      nicm      222:        return (CMD_RETURN_NORMAL);
1.27      nicm      223: }
1.59      nicm      224:
                    225: /* Set user option. */
1.99      nicm      226: static enum cmd_retval
1.101     nicm      227: cmd_set_option_user(struct cmd *self, struct cmdq_item *item,
                    228:     const char *optstr, const char *valstr)
1.59      nicm      229: {
1.97      nicm      230:        struct args             *args = self->args;
1.101     nicm      231:        struct session          *s = item->state.tflag.s;
                    232:        struct winlink          *wl = item->state.tflag.wl;
1.97      nicm      233:        struct options          *oo;
                    234:        struct options_entry    *o;
1.98      nicm      235:        const char              *target;
1.59      nicm      236:
                    237:        if (args_has(args, 's'))
1.84      nicm      238:                oo = global_options;
1.59      nicm      239:        else if (args_has(self->args, 'w') ||
                    240:            self->entry == &cmd_set_window_option_entry) {
                    241:                if (args_has(self->args, 'g'))
1.84      nicm      242:                        oo = global_w_options;
1.98      nicm      243:                else if (wl == NULL) {
                    244:                        target = args_get(args, 't');
                    245:                        if (target != NULL) {
1.101     nicm      246:                                cmdq_error(item, "no such window: %s",
1.98      nicm      247:                                    target);
                    248:                        } else
1.101     nicm      249:                                cmdq_error(item, "no current window");
1.98      nicm      250:                        return (CMD_RETURN_ERROR);
                    251:                } else
1.84      nicm      252:                        oo = wl->window->options;
1.59      nicm      253:        } else {
                    254:                if (args_has(self->args, 'g'))
1.84      nicm      255:                        oo = global_s_options;
1.98      nicm      256:                else if (s == NULL) {
                    257:                        target = args_get(args, 't');
                    258:                        if (target != NULL) {
1.101     nicm      259:                                cmdq_error(item, "no such session: %s",
1.98      nicm      260:                                    target);
                    261:                        } else
1.101     nicm      262:                                cmdq_error(item, "no current session");
1.98      nicm      263:                        return (CMD_RETURN_ERROR);
                    264:                } else
1.84      nicm      265:                        oo = s->options;
1.59      nicm      266:        }
                    267:
                    268:        if (args_has(args, 'u')) {
                    269:                if (options_find1(oo, optstr) == NULL) {
1.67      nicm      270:                        if (!args_has(args, 'q')) {
1.101     nicm      271:                                cmdq_error(item, "unknown option: %s", optstr);
1.67      nicm      272:                                return (CMD_RETURN_ERROR);
                    273:                        }
                    274:                        return (CMD_RETURN_NORMAL);
1.59      nicm      275:                }
                    276:                if (valstr != NULL) {
1.101     nicm      277:                        cmdq_error(item, "value passed to unset option: %s",
1.59      nicm      278:                            optstr);
                    279:                        return (CMD_RETURN_ERROR);
                    280:                }
                    281:                options_remove(oo, optstr);
                    282:        } else {
1.97      nicm      283:                o = options_find1(oo, optstr);
                    284:                if (args_has(args, 'o') && o != NULL) {
1.67      nicm      285:                        if (!args_has(args, 'q')) {
1.101     nicm      286:                                cmdq_error(item, "already set: %s", optstr);
1.70      nicm      287:                                return (CMD_RETURN_ERROR);
1.67      nicm      288:                        }
1.61      nicm      289:                        return (CMD_RETURN_NORMAL);
1.59      nicm      290:                }
1.97      nicm      291:                if (valstr == NULL) {
1.101     nicm      292:                        cmdq_error(item, "empty value");
1.97      nicm      293:                        return (CMD_RETURN_ERROR);
                    294:                }
                    295:                if (o != NULL && args_has(args, 'a'))
                    296:                        options_set_string(oo, optstr, "%s%s", o->str, valstr);
                    297:                else
                    298:                        options_set_string(oo, optstr, "%s", valstr);
1.59      nicm      299:        }
                    300:        return (CMD_RETURN_NORMAL);
                    301: }
1.27      nicm      302:
1.43      nicm      303: /* Unset an option. */
1.99      nicm      304: static int
1.101     nicm      305: cmd_set_option_unset(struct cmd *self, struct cmdq_item *item,
1.69      nicm      306:     const struct options_table_entry *oe, struct options *oo,
                    307:     const char *value)
1.27      nicm      308: {
1.44      nicm      309:        struct args     *args = self->args;
1.43      nicm      310:
1.44      nicm      311:        if (value != NULL) {
1.101     nicm      312:                cmdq_error(item, "value passed to unset option: %s", oe->name);
1.43      nicm      313:                return (-1);
1.27      nicm      314:        }
1.43      nicm      315:
1.84      nicm      316:        if (args_has(args, 'g') || oo == global_options) {
1.75      nicm      317:                switch (oe->type) {
                    318:                case OPTIONS_TABLE_STRING:
                    319:                        options_set_string(oo, oe->name, "%s", oe->default_str);
                    320:                        break;
                    321:                case OPTIONS_TABLE_STYLE:
                    322:                        options_set_style(oo, oe->name, oe->default_str, 0);
                    323:                        break;
                    324:                default:
                    325:                        options_set_number(oo, oe->name, oe->default_num);
                    326:                        break;
                    327:                }
                    328:        } else
                    329:                options_remove(oo, oe->name);
1.43      nicm      330:        return (0);
1.27      nicm      331: }
                    332:
1.43      nicm      333: /* Set an option. */
1.99      nicm      334: static int
1.101     nicm      335: cmd_set_option_set(struct cmd *self, struct cmdq_item *item,
1.69      nicm      336:     const struct options_table_entry *oe, struct options *oo,
                    337:     const char *value)
1.27      nicm      338: {
                    339:        struct options_entry    *o;
                    340:
1.73      nicm      341:        switch (oe->type) {
                    342:        case OPTIONS_TABLE_FLAG:
                    343:        case OPTIONS_TABLE_CHOICE:
                    344:                break;
                    345:        default:
                    346:                if (value == NULL) {
1.101     nicm      347:                        cmdq_error(item, "empty value");
1.73      nicm      348:                        return (-1);
                    349:                }
1.27      nicm      350:        }
                    351:
1.43      nicm      352:        o = NULL;
                    353:        switch (oe->type) {
                    354:        case OPTIONS_TABLE_STRING:
1.101     nicm      355:                o = cmd_set_option_string(self, item, oe, oo, value);
1.43      nicm      356:                break;
                    357:        case OPTIONS_TABLE_NUMBER:
1.101     nicm      358:                o = cmd_set_option_number(self, item, oe, oo, value);
1.43      nicm      359:                break;
1.52      nicm      360:        case OPTIONS_TABLE_KEY:
1.101     nicm      361:                o = cmd_set_option_key(self, item, oe, oo, value);
1.43      nicm      362:                break;
                    363:        case OPTIONS_TABLE_COLOUR:
1.101     nicm      364:                o = cmd_set_option_colour(self, item, oe, oo, value);
1.66      nicm      365:                if (o != NULL)
                    366:                        style_update_new(oo, o->name, oe->style);
1.43      nicm      367:                break;
                    368:        case OPTIONS_TABLE_ATTRIBUTES:
1.101     nicm      369:                o = cmd_set_option_attributes(self, item, oe, oo, value);
1.66      nicm      370:                if (o != NULL)
                    371:                        style_update_new(oo, o->name, oe->style);
1.43      nicm      372:                break;
                    373:        case OPTIONS_TABLE_FLAG:
1.101     nicm      374:                o = cmd_set_option_flag(self, item, oe, oo, value);
1.43      nicm      375:                break;
                    376:        case OPTIONS_TABLE_CHOICE:
1.101     nicm      377:                o = cmd_set_option_choice(self, item, oe, oo, value);
1.43      nicm      378:                break;
1.64      nicm      379:        case OPTIONS_TABLE_STYLE:
1.101     nicm      380:                o = cmd_set_option_style(self, item, oe, oo, value);
1.64      nicm      381:                break;
1.43      nicm      382:        }
                    383:        if (o == NULL)
                    384:                return (-1);
                    385:        return (0);
                    386: }
                    387:
                    388: /* Set a string option. */
1.99      nicm      389: static struct options_entry *
1.101     nicm      390: cmd_set_option_string(struct cmd *self, __unused struct cmdq_item *item,
1.69      nicm      391:     const struct options_table_entry *oe, struct options *oo,
                    392:     const char *value)
1.43      nicm      393: {
1.54      nicm      394:        struct args             *args = self->args;
1.43      nicm      395:        struct options_entry    *o;
                    396:        char                    *oldval, *newval;
                    397:
1.44      nicm      398:        if (args_has(args, 'a')) {
1.43      nicm      399:                oldval = options_get_string(oo, oe->name);
1.44      nicm      400:                xasprintf(&newval, "%s%s", oldval, value);
1.27      nicm      401:        } else
1.44      nicm      402:                newval = xstrdup(value);
1.28      nicm      403:
1.43      nicm      404:        o = options_set_string(oo, oe->name, "%s", newval);
1.27      nicm      405:
1.56      nicm      406:        free(newval);
1.43      nicm      407:        return (o);
1.27      nicm      408: }
                    409:
1.43      nicm      410: /* Set a number option. */
1.99      nicm      411: static struct options_entry *
1.101     nicm      412: cmd_set_option_number(__unused struct cmd *self, struct cmdq_item *item,
1.69      nicm      413:     const struct options_table_entry *oe, struct options *oo,
                    414:     const char *value)
1.27      nicm      415: {
1.44      nicm      416:        long long        ll;
                    417:        const char      *errstr;
1.27      nicm      418:
1.44      nicm      419:        ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
1.27      nicm      420:        if (errstr != NULL) {
1.101     nicm      421:                cmdq_error(item, "value is %s: %s", errstr, value);
1.43      nicm      422:                return (NULL);
1.27      nicm      423:        }
                    424:
1.43      nicm      425:        return (options_set_number(oo, oe->name, ll));
1.27      nicm      426: }
                    427:
1.52      nicm      428: /* Set a key option. */
1.99      nicm      429: static struct options_entry *
1.101     nicm      430: cmd_set_option_key(__unused struct cmd *self, struct cmdq_item *item,
1.69      nicm      431:     const struct options_table_entry *oe, struct options *oo,
                    432:     const char *value)
1.27      nicm      433: {
1.85      nicm      434:        key_code        key;
1.52      nicm      435:
1.88      nicm      436:        key = key_string_lookup_string(value);
                    437:        if (key == KEYC_UNKNOWN) {
1.101     nicm      438:                cmdq_error(item, "bad key: %s", value);
1.52      nicm      439:                return (NULL);
1.27      nicm      440:        }
                    441:
1.52      nicm      442:        return (options_set_number(oo, oe->name, key));
1.27      nicm      443: }
                    444:
1.43      nicm      445: /* Set a colour option. */
1.99      nicm      446: static struct options_entry *
1.101     nicm      447: cmd_set_option_colour(__unused struct cmd *self, struct cmdq_item *item,
1.69      nicm      448:     const struct options_table_entry *oe, struct options *oo,
                    449:     const char *value)
1.27      nicm      450: {
1.44      nicm      451:        int     colour;
1.27      nicm      452:
1.44      nicm      453:        if ((colour = colour_fromstring(value)) == -1) {
1.101     nicm      454:                cmdq_error(item, "bad colour: %s", value);
1.43      nicm      455:                return (NULL);
1.27      nicm      456:        }
                    457:
1.43      nicm      458:        return (options_set_number(oo, oe->name, colour));
1.27      nicm      459: }
                    460:
1.43      nicm      461: /* Set an attributes option. */
1.99      nicm      462: static struct options_entry *
1.101     nicm      463: cmd_set_option_attributes(__unused struct cmd *self, struct cmdq_item *item,
1.69      nicm      464:     const struct options_table_entry *oe, struct options *oo,
                    465:     const char *value)
1.27      nicm      466: {
1.44      nicm      467:        int     attr;
1.27      nicm      468:
1.44      nicm      469:        if ((attr = attributes_fromstring(value)) == -1) {
1.101     nicm      470:                cmdq_error(item, "bad attributes: %s", value);
1.43      nicm      471:                return (NULL);
1.27      nicm      472:        }
                    473:
1.43      nicm      474:        return (options_set_number(oo, oe->name, attr));
1.27      nicm      475: }
                    476:
1.43      nicm      477: /* Set a flag option. */
1.99      nicm      478: static struct options_entry *
1.101     nicm      479: cmd_set_option_flag(__unused struct cmd *self, struct cmdq_item *item,
1.69      nicm      480:     const struct options_table_entry *oe, struct options *oo,
                    481:     const char *value)
1.27      nicm      482: {
1.44      nicm      483:        int     flag;
1.27      nicm      484:
1.44      nicm      485:        if (value == NULL || *value == '\0')
1.43      nicm      486:                flag = !options_get_number(oo, oe->name);
1.27      nicm      487:        else {
1.44      nicm      488:                if ((value[0] == '1' && value[1] == '\0') ||
                    489:                    strcasecmp(value, "on") == 0 ||
                    490:                    strcasecmp(value, "yes") == 0)
1.27      nicm      491:                        flag = 1;
1.44      nicm      492:                else if ((value[0] == '0' && value[1] == '\0') ||
                    493:                    strcasecmp(value, "off") == 0 ||
                    494:                    strcasecmp(value, "no") == 0)
1.27      nicm      495:                        flag = 0;
                    496:                else {
1.101     nicm      497:                        cmdq_error(item, "bad value: %s", value);
1.43      nicm      498:                        return (NULL);
1.27      nicm      499:                }
                    500:        }
                    501:
1.43      nicm      502:        return (options_set_number(oo, oe->name, flag));
1.27      nicm      503: }
                    504:
1.43      nicm      505: /* Set a choice option. */
1.99      nicm      506: static struct options_entry *
1.101     nicm      507: cmd_set_option_choice(__unused struct cmd *self, struct cmdq_item *item,
1.60      nicm      508:     const struct options_table_entry *oe, struct options *oo,
                    509:     const char *value)
1.27      nicm      510: {
1.44      nicm      511:        const char      **choicep;
                    512:        int               n, choice = -1;
1.27      nicm      513:
1.73      nicm      514:        if (value == NULL) {
                    515:                choice = options_get_number(oo, oe->name);
                    516:                if (choice < 2)
                    517:                        choice = !choice;
                    518:        } else {
                    519:                n = 0;
                    520:                for (choicep = oe->choices; *choicep != NULL; choicep++) {
                    521:                        n++;
                    522:                        if (strncmp(*choicep, value, strlen(value)) != 0)
                    523:                                continue;
                    524:
                    525:                        if (choice != -1) {
1.101     nicm      526:                                cmdq_error(item, "ambiguous value: %s", value);
1.73      nicm      527:                                return (NULL);
                    528:                        }
                    529:                        choice = n - 1;
                    530:                }
                    531:                if (choice == -1) {
1.101     nicm      532:                        cmdq_error(item, "unknown value: %s", value);
1.43      nicm      533:                        return (NULL);
1.27      nicm      534:                }
                    535:        }
                    536:
1.43      nicm      537:        return (options_set_number(oo, oe->name, choice));
1.64      nicm      538: }
                    539:
                    540: /* Set a style option. */
1.99      nicm      541: static struct options_entry *
1.101     nicm      542: cmd_set_option_style(struct cmd *self, struct cmdq_item *item,
1.64      nicm      543:     const struct options_table_entry *oe, struct options *oo,
                    544:     const char *value)
                    545: {
                    546:        struct args             *args = self->args;
                    547:        struct options_entry    *o;
                    548:        int                      append;
                    549:
                    550:        append = args_has(args, 'a');
                    551:        if ((o = options_set_style(oo, oe->name, value, append)) == NULL) {
1.101     nicm      552:                cmdq_error(item, "bad style: %s", value);
1.64      nicm      553:                return (NULL);
                    554:        }
                    555:
                    556:        style_update_old(oo, oe->name, &o->style);
                    557:        return (o);
1.1       nicm      558: }