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

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