Annotation of embedaddon/tmux/cmd-show-options.c, revision 1.1

1.1     ! misho       1: /* $OpenBSD$ */
        !             2: 
        !             3: /*
        !             4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
        !             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:  * Show options.
        !            28:  */
        !            29: 
        !            30: static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *);
        !            31: 
        !            32: static enum cmd_retval cmd_show_options_one(struct cmd *, struct cmdq_item *,
        !            33:                            struct options *);
        !            34: static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *,
        !            35:                            struct options *);
        !            36: 
        !            37: const struct cmd_entry cmd_show_options_entry = {
        !            38:        .name = "show-options",
        !            39:        .alias = "show",
        !            40: 
        !            41:        .args = { "gqst:vw", 0, 1 },
        !            42:        .usage = "[-gqsvw] [-t target-session|target-window] [option]",
        !            43: 
        !            44:        .tflag = CMD_WINDOW_CANFAIL,
        !            45: 
        !            46:        .flags = CMD_AFTERHOOK,
        !            47:        .exec = cmd_show_options_exec
        !            48: };
        !            49: 
        !            50: const struct cmd_entry cmd_show_window_options_entry = {
        !            51:        .name = "show-window-options",
        !            52:        .alias = "showw",
        !            53: 
        !            54:        .args = { "gvt:", 0, 1 },
        !            55:        .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
        !            56: 
        !            57:        .tflag = CMD_WINDOW_CANFAIL,
        !            58: 
        !            59:        .flags = CMD_AFTERHOOK,
        !            60:        .exec = cmd_show_options_exec
        !            61: };
        !            62: 
        !            63: static enum cmd_retval
        !            64: cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
        !            65: {
        !            66:        struct args                     *args = self->args;
        !            67:        struct cmd_find_state           *fs = &item->state.tflag;
        !            68:        struct options                  *oo;
        !            69:        enum options_table_scope         scope;
        !            70:        char                            *cause;
        !            71:        int                              window;
        !            72: 
        !            73:        window = (self->entry == &cmd_show_window_options_entry);
        !            74:        scope = options_scope_from_flags(args, window, fs, &oo, &cause);
        !            75:        if (scope == OPTIONS_TABLE_NONE) {
        !            76:                cmdq_error(item, "%s", cause);
        !            77:                free(cause);
        !            78:                return (CMD_RETURN_ERROR);
        !            79:        }
        !            80: 
        !            81:        if (args->argc == 0)
        !            82:                return (cmd_show_options_all(self, item, oo));
        !            83:        else
        !            84:                return (cmd_show_options_one(self, item, oo));
        !            85: }
        !            86: 
        !            87: static void
        !            88: cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
        !            89:     struct options_entry *o, int idx)
        !            90: {
        !            91:        const char      *name;
        !            92:        const char      *value;
        !            93:        char            *tmp, *escaped;
        !            94:        u_int            size, i;
        !            95: 
        !            96:        if (idx != -1) {
        !            97:                xasprintf(&tmp, "%s[%d]", options_name(o), idx);
        !            98:                name = tmp;
        !            99:        } else {
        !           100:                if (options_array_size(o, &size) != -1) {
        !           101:                        for (i = 0; i < size; i++) {
        !           102:                                if (options_array_get(o, i) == NULL)
        !           103:                                        continue;
        !           104:                                cmd_show_options_print(self, item, o, i);
        !           105:                        }
        !           106:                        return;
        !           107:                }
        !           108:                tmp = NULL;
        !           109:                name = options_name(o);
        !           110:        }
        !           111: 
        !           112:        value = options_tostring(o, idx, 0);
        !           113:        if (args_has(self->args, 'v'))
        !           114:                cmdq_print(item, "%s", value);
        !           115:        else if (options_isstring(o)) {
        !           116:                utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ);
        !           117:                cmdq_print(item, "%s \"%s\"", name, escaped);
        !           118:                free(escaped);
        !           119:        } else
        !           120:                cmdq_print(item, "%s %s", name, value);
        !           121: 
        !           122:        free(tmp);
        !           123: }
        !           124: 
        !           125: static enum cmd_retval
        !           126: cmd_show_options_one(struct cmd *self, struct cmdq_item *item,
        !           127:     struct options *oo)
        !           128: {
        !           129:        struct args             *args = self->args;
        !           130:        struct options_entry    *o;
        !           131:        int                      idx, ambiguous;
        !           132:        const char              *name = args->argv[0];
        !           133: 
        !           134:        o = options_match_get(oo, name, &idx, 1, &ambiguous);
        !           135:        if (o == NULL) {
        !           136:                if (args_has(args, 'q'))
        !           137:                        return (CMD_RETURN_NORMAL);
        !           138:                if (ambiguous) {
        !           139:                        cmdq_error(item, "ambiguous option: %s", name);
        !           140:                        return (CMD_RETURN_ERROR);
        !           141:                }
        !           142:                if (*name != '@' &&
        !           143:                    options_match_get(oo, name, &idx, 0, &ambiguous) != NULL)
        !           144:                        return (CMD_RETURN_NORMAL);
        !           145:                cmdq_error(item, "unknown option: %s", name);
        !           146:                return (CMD_RETURN_ERROR);
        !           147:        }
        !           148:        cmd_show_options_print(self, item, o, idx);
        !           149:        return (CMD_RETURN_NORMAL);
        !           150: }
        !           151: 
        !           152: static enum cmd_retval
        !           153: cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
        !           154:     struct options *oo)
        !           155: {
        !           156:        struct options_entry                     *o;
        !           157:        const struct options_table_entry        *oe;
        !           158:        u_int                                    size, idx;
        !           159: 
        !           160:        o = options_first(oo);
        !           161:        while (o != NULL) {
        !           162:                oe = options_table_entry(o);
        !           163:                if (oe != NULL && oe->style != NULL) {
        !           164:                        o = options_next(o);
        !           165:                        continue;
        !           166:                }
        !           167:                if (options_array_size(o, &size) == -1)
        !           168:                        cmd_show_options_print(self, item, o, -1);
        !           169:                else {
        !           170:                        for (idx = 0; idx < size; idx++) {
        !           171:                                if (options_array_get(o, idx) == NULL)
        !           172:                                        continue;
        !           173:                                cmd_show_options_print(self, item, o, idx);
        !           174:                        }
        !           175:                }
        !           176:                o = options_next(o);
        !           177:        }
        !           178:        return (CMD_RETURN_NORMAL);
        !           179: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>