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

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