Annotation of src/usr.bin/tmux/cmd-show-options.c, Revision 1.43
1.43 ! nicm 1: /* $OpenBSD: cmd-show-options.c,v 1.42 2017/05/10 13:05:41 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.41 nicm 45: .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.29 nicm 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.41 nicm 58: .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
1.29 nicm 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.41 nicm 68: struct cmd_find_state *fs = &item->target;
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: {
1.43 ! nicm 92: struct options_array_item *a;
! 93: const char *name, *value;
! 94: char *tmp, *escaped;
1.35 nicm 95:
96: if (idx != -1) {
97: xasprintf(&tmp, "%s[%d]", options_name(o), idx);
98: name = tmp;
99: } else {
1.43 ! nicm 100: if (options_isarray(o)) {
! 101: a = options_array_first(o);
! 102: while (a != NULL) {
! 103: idx = options_array_item_index(a);
! 104: cmd_show_options_print(self, item, o, idx);
! 105: a = options_array_next(a);
1.39 nicm 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;
1.42 nicm 131: struct client *c = cmd_find_client(item, NULL, 1);
132: struct session *s = item->target.s;
133: struct winlink *wl = item->target.wl;
1.36 nicm 134: struct options_entry *o;
135: int idx, ambiguous;
1.42 nicm 136: char *name;
1.35 nicm 137:
1.42 nicm 138: name = format_single(item, args->argv[0], c, s, wl, NULL);
1.35 nicm 139: o = options_match_get(oo, name, &idx, 1, &ambiguous);
140: if (o == NULL) {
1.42 nicm 141: if (args_has(args, 'q')) {
142: free(name);
1.35 nicm 143: return (CMD_RETURN_NORMAL);
1.42 nicm 144: }
1.35 nicm 145: if (ambiguous) {
146: cmdq_error(item, "ambiguous option: %s", name);
1.42 nicm 147: free(name);
1.16 nicm 148: return (CMD_RETURN_ERROR);
1.15 nicm 149: }
1.37 nicm 150: if (*name != '@' &&
1.42 nicm 151: options_match_get(oo, name, &idx, 0, &ambiguous) != NULL) {
152: free(name);
1.25 nicm 153: return (CMD_RETURN_NORMAL);
1.42 nicm 154: }
1.34 nicm 155: cmdq_error(item, "unknown option: %s", name);
1.42 nicm 156: free(name);
1.18 nicm 157: return (CMD_RETURN_ERROR);
158: }
1.35 nicm 159: cmd_show_options_print(self, item, o, idx);
1.42 nicm 160: free(name);
1.18 nicm 161: return (CMD_RETURN_NORMAL);
162: }
163:
1.32 nicm 164: static enum cmd_retval
1.34 nicm 165: cmd_show_options_all(struct cmd *self, struct cmdq_item *item,
1.35 nicm 166: struct options *oo)
1.18 nicm 167: {
1.43 ! nicm 168: struct options_entry *o;
1.36 nicm 169: const struct options_table_entry *oe;
1.43 ! nicm 170: struct options_array_item *a;
! 171: u_int idx;
1.18 nicm 172:
1.24 nicm 173: o = options_first(oo);
174: while (o != NULL) {
1.35 nicm 175: oe = options_table_entry(o);
176: if (oe != NULL && oe->style != NULL) {
177: o = options_next(o);
178: continue;
179: }
1.43 ! nicm 180: if (!options_isarray(o))
1.35 nicm 181: cmd_show_options_print(self, item, o, -1);
182: else {
1.43 ! nicm 183: a = options_array_first(o);
! 184: while (a != NULL) {
! 185: idx = options_array_item_index(a);
1.35 nicm 186: cmd_show_options_print(self, item, o, idx);
1.43 ! nicm 187: a = options_array_next(a);
1.35 nicm 188: }
1.15 nicm 189: }
1.24 nicm 190: o = options_next(o);
1.18 nicm 191: }
1.16 nicm 192: return (CMD_RETURN_NORMAL);
1.1 nicm 193: }