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