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

1.60    ! nicm        1: /* $OpenBSD: cmd-set-option.c,v 1.59 2013/03/21 16:17:01 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:
1.60    ! nicm       30: enum cmd_retval        cmd_set_option_exec(struct cmd *, struct cmd_q *);
1.59      nicm       31:
1.60    ! nicm       32: enum cmd_retval        cmd_set_option_user(struct cmd *, struct cmd_q *,
1.59      nicm       33:                    const char *, const char *);
1.1       nicm       34:
1.60    ! nicm       35: int    cmd_set_option_unset(struct cmd *, struct cmd_q *,
1.44      nicm       36:            const struct options_table_entry *, struct options *,
                     37:            const char *);
1.60    ! nicm       38: int    cmd_set_option_set(struct cmd *, struct cmd_q *,
1.44      nicm       39:            const struct options_table_entry *, struct options *,
                     40:            const char *);
1.43      nicm       41:
1.60    ! nicm       42: struct options_entry *cmd_set_option_string(struct cmd *, struct cmd_q *,
1.44      nicm       43:            const struct options_table_entry *, struct options *,
                     44:            const char *);
1.60    ! nicm       45: struct options_entry *cmd_set_option_number(struct cmd *, struct cmd_q *,
1.44      nicm       46:            const struct options_table_entry *, struct options *,
                     47:            const char *);
1.60    ! nicm       48: struct options_entry *cmd_set_option_key(struct cmd *, struct cmd_q *,
1.44      nicm       49:            const struct options_table_entry *, struct options *,
                     50:            const char *);
1.60    ! nicm       51: struct options_entry *cmd_set_option_colour(struct cmd *, struct cmd_q *,
1.44      nicm       52:            const struct options_table_entry *, struct options *,
                     53:            const char *);
1.60    ! nicm       54: struct options_entry *cmd_set_option_attributes(struct cmd *, struct cmd_q *,
1.44      nicm       55:            const struct options_table_entry *, struct options *,
                     56:            const char *);
1.60    ! nicm       57: struct options_entry *cmd_set_option_flag(struct cmd *, struct cmd_q *,
1.44      nicm       58:            const struct options_table_entry *, struct options *,
                     59:            const char *);
1.60    ! nicm       60: struct options_entry *cmd_set_option_choice(struct cmd *, struct cmd_q *,
1.44      nicm       61:            const struct options_table_entry *, struct options *,
                     62:            const char *);
1.27      nicm       63:
1.1       nicm       64: const struct cmd_entry cmd_set_option_entry = {
                     65:        "set-option", "set",
1.54      nicm       66:        "agqst:uw", 1, 2,
                     67:        "[-agsquw] [-t target-session|target-window] option [value]",
1.44      nicm       68:        0,
1.1       nicm       69:        NULL,
1.44      nicm       70:        NULL,
                     71:        cmd_set_option_exec
1.1       nicm       72: };
                     73:
1.46      nicm       74: const struct cmd_entry cmd_set_window_option_entry = {
                     75:        "set-window-option", "setw",
1.54      nicm       76:        "agqt:u", 1, 2,
                     77:        "[-agqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
1.46      nicm       78:        0,
                     79:        NULL,
                     80:        NULL,
                     81:        cmd_set_option_exec
                     82: };
                     83:
1.57      nicm       84: enum cmd_retval
1.60    ! nicm       85: cmd_set_option_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       86: {
1.44      nicm       87:        struct args                             *args = self->args;
1.50      nicm       88:        const struct options_table_entry        *table, *oe;
1.43      nicm       89:        struct session                          *s;
                     90:        struct winlink                          *wl;
                     91:        struct client                           *c;
                     92:        struct options                          *oo;
1.55      nicm       93:        struct window                           *w;
1.44      nicm       94:        const char                              *optstr, *valstr;
1.43      nicm       95:        u_int                                    i;
1.1       nicm       96:
1.50      nicm       97:        /* Get the option name and value. */
                     98:        optstr = args->argv[0];
                     99:        if (*optstr == '\0') {
1.60    ! nicm      100:                cmdq_error(cmdq, "invalid option");
1.57      nicm      101:                return (CMD_RETURN_ERROR);
1.50      nicm      102:        }
                    103:        if (args->argc < 2)
                    104:                valstr = NULL;
                    105:        else
                    106:                valstr = args->argv[1];
                    107:
1.59      nicm      108:        /* Is this a user option? */
                    109:        if (*optstr == '@')
1.60    ! nicm      110:                return (cmd_set_option_user(self, cmdq, optstr, valstr));
1.59      nicm      111:
1.50      nicm      112:        /* Find the option entry, try each table. */
                    113:        table = oe = NULL;
1.53      nicm      114:        if (options_table_find(optstr, &table, &oe) != 0) {
1.60    ! nicm      115:                cmdq_error(cmdq, "ambiguous option: %s", optstr);
1.57      nicm      116:                return (CMD_RETURN_ERROR);
1.50      nicm      117:        }
                    118:        if (oe == NULL) {
1.60    ! nicm      119:                cmdq_error(cmdq, "unknown option: %s", optstr);
1.57      nicm      120:                return (CMD_RETURN_ERROR);
1.50      nicm      121:        }
                    122:
                    123:        /* Work out the tree from the table. */
                    124:        if (table == server_options_table)
1.29      nicm      125:                oo = &global_options;
1.50      nicm      126:        else if (table == window_options_table) {
1.44      nicm      127:                if (args_has(self->args, 'g'))
1.27      nicm      128:                        oo = &global_w_options;
                    129:                else {
1.60    ! nicm      130:                        wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
1.27      nicm      131:                        if (wl == NULL)
1.57      nicm      132:                                return (CMD_RETURN_ERROR);
1.27      nicm      133:                        oo = &wl->window->options;
                    134:                }
1.50      nicm      135:        } else if (table == session_options_table) {
1.44      nicm      136:                if (args_has(self->args, 'g'))
1.27      nicm      137:                        oo = &global_s_options;
                    138:                else {
1.60    ! nicm      139:                        s = cmd_find_session(cmdq, args_get(args, 't'), 0);
1.27      nicm      140:                        if (s == NULL)
1.57      nicm      141:                                return (CMD_RETURN_ERROR);
1.27      nicm      142:                        oo = &s->options;
                    143:                }
1.50      nicm      144:        } else {
1.60    ! nicm      145:                cmdq_error(cmdq, "unknown table");
1.57      nicm      146:                return (CMD_RETURN_ERROR);
1.1       nicm      147:        }
                    148:
1.43      nicm      149:        /* Unset or set the option. */
1.44      nicm      150:        if (args_has(args, 'u')) {
1.60    ! nicm      151:                if (cmd_set_option_unset(self, cmdq, oe, oo, valstr) != 0)
1.57      nicm      152:                        return (CMD_RETURN_ERROR);
1.43      nicm      153:        } else {
1.60    ! nicm      154:                if (cmd_set_option_set(self, cmdq, oe, oo, valstr) != 0)
1.57      nicm      155:                        return (CMD_RETURN_ERROR);
1.55      nicm      156:        }
                    157:
                    158:        /* Start or stop timers when automatic-rename changed. */
                    159:        if (strcmp (oe->name, "automatic-rename") == 0) {
                    160:                for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    161:                        if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                    162:                                continue;
                    163:                        if (options_get_number(&w->options, "automatic-rename"))
                    164:                                queue_window_name(w);
                    165:                        else if (event_initialized(&w->name_timer))
                    166:                                evtimer_del(&w->name_timer);
                    167:                }
1.1       nicm      168:        }
                    169:
1.43      nicm      170:        /* Update sizes and redraw. May not need it but meh. */
1.1       nicm      171:        recalculate_sizes();
1.27      nicm      172:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    173:                c = ARRAY_ITEM(&clients, i);
                    174:                if (c != NULL && c->session != NULL)
                    175:                        server_redraw_client(c);
1.1       nicm      176:        }
                    177:
1.57      nicm      178:        return (CMD_RETURN_NORMAL);
1.27      nicm      179: }
1.59      nicm      180:
                    181: /* Set user option. */
                    182: enum cmd_retval
1.60    ! nicm      183: cmd_set_option_user(struct cmd *self, struct cmd_q *cmdq, const char* optstr,
1.59      nicm      184:     const char *valstr)
                    185: {
                    186:        struct args     *args = self->args;
                    187:        struct session  *s;
                    188:        struct winlink  *wl;
                    189:        struct options  *oo;
                    190:
                    191:        if (args_has(args, 's'))
                    192:                oo = &global_options;
                    193:        else if (args_has(self->args, 'w') ||
                    194:            self->entry == &cmd_set_window_option_entry) {
                    195:                if (args_has(self->args, 'g'))
                    196:                        oo = &global_w_options;
                    197:                else {
1.60    ! nicm      198:                        wl = cmd_find_window(cmdq, args_get(args, 't'), NULL);
1.59      nicm      199:                        if (wl == NULL)
                    200:                                return (CMD_RETURN_ERROR);
                    201:                        oo = &wl->window->options;
                    202:                }
                    203:        } else {
                    204:                if (args_has(self->args, 'g'))
                    205:                        oo = &global_s_options;
                    206:                else {
1.60    ! nicm      207:                        s = cmd_find_session(cmdq, args_get(args, 't'), 0);
1.59      nicm      208:                        if (s == NULL)
                    209:                                return (CMD_RETURN_ERROR);
                    210:                        oo = &s->options;
                    211:                }
                    212:        }
                    213:
                    214:        if (args_has(args, 'u')) {
                    215:                if (options_find1(oo, optstr) == NULL) {
1.60    ! nicm      216:                        cmdq_error(cmdq, "unknown option: %s", optstr);
1.59      nicm      217:                        return (CMD_RETURN_ERROR);
                    218:                }
                    219:                if (valstr != NULL) {
1.60    ! nicm      220:                        cmdq_error(cmdq, "value passed to unset option: %s",
1.59      nicm      221:                            optstr);
                    222:                        return (CMD_RETURN_ERROR);
                    223:                }
                    224:                options_remove(oo, optstr);
                    225:        } else {
                    226:                if (valstr == NULL) {
1.60    ! nicm      227:                        cmdq_error(cmdq, "empty value");
1.59      nicm      228:                        return (CMD_RETURN_ERROR);
                    229:                }
                    230:                options_set_string(oo, optstr, "%s", valstr);
1.60    ! nicm      231:                if (!args_has(args, 'q')) {
        !           232:                        cmdq_info(cmdq, "set option: %s -> %s", optstr,
        !           233:                            valstr);
        !           234:                }
1.59      nicm      235:        }
                    236:        return (CMD_RETURN_NORMAL);
                    237: }
                    238:
1.27      nicm      239:
1.43      nicm      240: /* Unset an option. */
                    241: int
1.60    ! nicm      242: cmd_set_option_unset(struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      243:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      244: {
1.44      nicm      245:        struct args     *args = self->args;
1.43      nicm      246:
1.44      nicm      247:        if (args_has(args, 'g')) {
1.60    ! nicm      248:                cmdq_error(cmdq, "can't unset global option: %s", oe->name);
1.43      nicm      249:                return (-1);
                    250:        }
1.44      nicm      251:        if (value != NULL) {
1.60    ! nicm      252:                cmdq_error(cmdq, "value passed to unset option: %s", oe->name);
1.43      nicm      253:                return (-1);
1.27      nicm      254:        }
1.43      nicm      255:
                    256:        options_remove(oo, oe->name);
1.54      nicm      257:        if (!args_has(args, 'q'))
1.60    ! nicm      258:                cmdq_info(cmdq, "unset option: %s", oe->name);
1.43      nicm      259:        return (0);
1.27      nicm      260: }
                    261:
1.43      nicm      262: /* Set an option. */
                    263: int
1.60    ! nicm      264: cmd_set_option_set(struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      265:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      266: {
1.54      nicm      267:        struct args             *args = self->args;
1.27      nicm      268:        struct options_entry    *o;
1.43      nicm      269:        const char              *s;
1.27      nicm      270:
1.44      nicm      271:        if (oe->type != OPTIONS_TABLE_FLAG && value == NULL) {
1.60    ! nicm      272:                cmdq_error(cmdq, "empty value");
1.43      nicm      273:                return (-1);
1.27      nicm      274:        }
                    275:
1.43      nicm      276:        o = NULL;
                    277:        switch (oe->type) {
                    278:        case OPTIONS_TABLE_STRING:
1.60    ! nicm      279:                o = cmd_set_option_string(self, cmdq, oe, oo, value);
1.43      nicm      280:                break;
                    281:        case OPTIONS_TABLE_NUMBER:
1.60    ! nicm      282:                o = cmd_set_option_number(self, cmdq, oe, oo, value);
1.43      nicm      283:                break;
1.52      nicm      284:        case OPTIONS_TABLE_KEY:
1.60    ! nicm      285:                o = cmd_set_option_key(self, cmdq, oe, oo, value);
1.43      nicm      286:                break;
                    287:        case OPTIONS_TABLE_COLOUR:
1.60    ! nicm      288:                o = cmd_set_option_colour(self, cmdq, oe, oo, value);
1.43      nicm      289:                break;
                    290:        case OPTIONS_TABLE_ATTRIBUTES:
1.60    ! nicm      291:                o = cmd_set_option_attributes(self, cmdq, oe, oo, value);
1.43      nicm      292:                break;
                    293:        case OPTIONS_TABLE_FLAG:
1.60    ! nicm      294:                o = cmd_set_option_flag(self, cmdq, oe, oo, value);
1.43      nicm      295:                break;
                    296:        case OPTIONS_TABLE_CHOICE:
1.60    ! nicm      297:                o = cmd_set_option_choice(self, cmdq, oe, oo, value);
1.43      nicm      298:                break;
                    299:        }
                    300:        if (o == NULL)
                    301:                return (-1);
                    302:
1.58      nicm      303:        s = options_table_print_entry(oe, o, 0);
1.54      nicm      304:        if (!args_has(args, 'q'))
1.60    ! nicm      305:                cmdq_info(cmdq, "set option: %s -> %s", oe->name, s);
1.43      nicm      306:        return (0);
                    307: }
                    308:
                    309: /* Set a string option. */
                    310: struct options_entry *
1.60    ! nicm      311: cmd_set_option_string(struct cmd *self, unused struct cmd_q *cmdq,
1.44      nicm      312:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.43      nicm      313: {
1.54      nicm      314:        struct args             *args = self->args;
1.43      nicm      315:        struct options_entry    *o;
                    316:        char                    *oldval, *newval;
                    317:
1.44      nicm      318:        if (args_has(args, 'a')) {
1.43      nicm      319:                oldval = options_get_string(oo, oe->name);
1.44      nicm      320:                xasprintf(&newval, "%s%s", oldval, value);
1.27      nicm      321:        } else
1.44      nicm      322:                newval = xstrdup(value);
1.28      nicm      323:
1.43      nicm      324:        o = options_set_string(oo, oe->name, "%s", newval);
1.27      nicm      325:
1.56      nicm      326:        free(newval);
1.43      nicm      327:        return (o);
1.27      nicm      328: }
                    329:
1.43      nicm      330: /* Set a number option. */
                    331: struct options_entry *
1.60    ! nicm      332: cmd_set_option_number(unused struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      333:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      334: {
1.44      nicm      335:        long long        ll;
                    336:        const char      *errstr;
1.27      nicm      337:
1.44      nicm      338:        ll = strtonum(value, oe->minimum, oe->maximum, &errstr);
1.27      nicm      339:        if (errstr != NULL) {
1.60    ! nicm      340:                cmdq_error(cmdq, "value is %s: %s", errstr, value);
1.43      nicm      341:                return (NULL);
1.27      nicm      342:        }
                    343:
1.43      nicm      344:        return (options_set_number(oo, oe->name, ll));
1.27      nicm      345: }
                    346:
1.52      nicm      347: /* Set a key option. */
1.43      nicm      348: struct options_entry *
1.60    ! nicm      349: cmd_set_option_key(unused struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      350:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      351: {
1.52      nicm      352:        int     key;
                    353:
                    354:        if ((key = key_string_lookup_string(value)) == KEYC_NONE) {
1.60    ! nicm      355:                cmdq_error(cmdq, "bad key: %s", value);
1.52      nicm      356:                return (NULL);
1.27      nicm      357:        }
                    358:
1.52      nicm      359:        return (options_set_number(oo, oe->name, key));
1.27      nicm      360: }
                    361:
1.43      nicm      362: /* Set a colour option. */
                    363: struct options_entry *
1.60    ! nicm      364: cmd_set_option_colour(unused struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      365:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      366: {
1.44      nicm      367:        int     colour;
1.27      nicm      368:
1.44      nicm      369:        if ((colour = colour_fromstring(value)) == -1) {
1.60    ! nicm      370:                cmdq_error(cmdq, "bad colour: %s", value);
1.43      nicm      371:                return (NULL);
1.27      nicm      372:        }
                    373:
1.43      nicm      374:        return (options_set_number(oo, oe->name, colour));
1.27      nicm      375: }
                    376:
1.43      nicm      377: /* Set an attributes option. */
                    378: struct options_entry *
1.60    ! nicm      379: cmd_set_option_attributes(unused struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      380:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      381: {
1.44      nicm      382:        int     attr;
1.27      nicm      383:
1.44      nicm      384:        if ((attr = attributes_fromstring(value)) == -1) {
1.60    ! nicm      385:                cmdq_error(cmdq, "bad attributes: %s", value);
1.43      nicm      386:                return (NULL);
1.27      nicm      387:        }
                    388:
1.43      nicm      389:        return (options_set_number(oo, oe->name, attr));
1.27      nicm      390: }
                    391:
1.43      nicm      392: /* Set a flag option. */
                    393: struct options_entry *
1.60    ! nicm      394: cmd_set_option_flag(unused struct cmd *self, struct cmd_q *cmdq,
1.44      nicm      395:     const struct options_table_entry *oe, struct options *oo, const char *value)
1.27      nicm      396: {
1.44      nicm      397:        int     flag;
1.27      nicm      398:
1.44      nicm      399:        if (value == NULL || *value == '\0')
1.43      nicm      400:                flag = !options_get_number(oo, oe->name);
1.27      nicm      401:        else {
1.44      nicm      402:                if ((value[0] == '1' && value[1] == '\0') ||
                    403:                    strcasecmp(value, "on") == 0 ||
                    404:                    strcasecmp(value, "yes") == 0)
1.27      nicm      405:                        flag = 1;
1.44      nicm      406:                else if ((value[0] == '0' && value[1] == '\0') ||
                    407:                    strcasecmp(value, "off") == 0 ||
                    408:                    strcasecmp(value, "no") == 0)
1.27      nicm      409:                        flag = 0;
                    410:                else {
1.60    ! nicm      411:                        cmdq_error(cmdq, "bad value: %s", value);
1.43      nicm      412:                        return (NULL);
1.27      nicm      413:                }
                    414:        }
                    415:
1.43      nicm      416:        return (options_set_number(oo, oe->name, flag));
1.27      nicm      417: }
                    418:
1.43      nicm      419: /* Set a choice option. */
                    420: struct options_entry *
1.60    ! nicm      421: cmd_set_option_choice(unused struct cmd *self, struct cmd_q *cmdq,
        !           422:     const struct options_table_entry *oe, struct options *oo,
        !           423:     const char *value)
1.27      nicm      424: {
1.44      nicm      425:        const char      **choicep;
                    426:        int               n, choice = -1;
1.27      nicm      427:
                    428:        n = 0;
1.43      nicm      429:        for (choicep = oe->choices; *choicep != NULL; choicep++) {
1.27      nicm      430:                n++;
1.44      nicm      431:                if (strncmp(*choicep, value, strlen(value)) != 0)
1.27      nicm      432:                        continue;
                    433:
                    434:                if (choice != -1) {
1.60    ! nicm      435:                        cmdq_error(cmdq, "ambiguous value: %s", value);
1.43      nicm      436:                        return (NULL);
1.27      nicm      437:                }
                    438:                choice = n - 1;
                    439:        }
                    440:        if (choice == -1) {
1.60    ! nicm      441:                cmdq_error(cmdq, "unknown value: %s", value);
1.43      nicm      442:                return (NULL);
1.27      nicm      443:        }
                    444:
1.43      nicm      445:        return (options_set_number(oo, oe->name, choice));
1.1       nicm      446: }