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

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