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

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