1 /* $OpenBSD: cmd-show-options.c,v 1.57 2019/06/20 13:39:17 nicm Exp $ */ 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 #include <vis.h> 24 25 #include "tmux.h" 26 27 /* 28 * Show options. 29 */ 30 31 static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *); 32 33 static void cmd_show_options_print(struct cmd *, struct cmdq_item *, 34 struct options_entry *, int, int); 35 static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *, 36 int, struct options *); 37 38 const struct cmd_entry cmd_show_options_entry = { 39 .name = "show-options", 40 .alias = "show", 41 42 .args = { "AgHpqst:vw", 0, 1 }, 43 .usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]", 44 45 .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, 46 47 .flags = CMD_AFTERHOOK, 48 .exec = cmd_show_options_exec 49 }; 50 51 const struct cmd_entry cmd_show_window_options_entry = { 52 .name = "show-window-options", 53 .alias = "showw", 54 55 .args = { "gvt:", 0, 1 }, 56 .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]", 57 58 .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, 59 60 .flags = CMD_AFTERHOOK, 61 .exec = cmd_show_options_exec 62 }; 63 64 const struct cmd_entry cmd_show_hooks_entry = { 65 .name = "show-hooks", 66 .alias = NULL, 67 68 .args = { "gt:", 0, 1 }, 69 .usage = "[-g] " CMD_TARGET_SESSION_USAGE, 70 71 .target = { 't', CMD_FIND_SESSION, 0 }, 72 73 .flags = CMD_AFTERHOOK, 74 .exec = cmd_show_options_exec 75 }; 76 77 static enum cmd_retval 78 cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) 79 { 80 struct args *args = self->args; 81 struct cmd_find_state *fs = &item->target; 82 struct client *c = cmd_find_client(item, NULL, 1); 83 struct session *s = item->target.s; 84 struct winlink *wl = item->target.wl; 85 struct options *oo; 86 char *argument, *name = NULL, *cause; 87 int window, idx, ambiguous, parent, scope; 88 struct options_entry *o; 89 90 window = (self->entry == &cmd_show_window_options_entry); 91 92 if (args->argc == 0) { 93 scope = options_scope_from_flags(args, window, fs, &oo, &cause); 94 if (scope == OPTIONS_TABLE_NONE) { 95 if (args_has(args, 'q')) 96 return (CMD_RETURN_NORMAL); 97 cmdq_error(item, "%s", cause); 98 free(cause); 99 return (CMD_RETURN_ERROR); 100 } 101 return (cmd_show_options_all(self, item, scope, oo)); 102 } 103 argument = format_single(item, args->argv[0], c, s, wl, NULL); 104 105 name = options_match(argument, &idx, &ambiguous); 106 if (name == NULL) { 107 if (args_has(args, 'q')) 108 goto fail; 109 if (ambiguous) 110 cmdq_error(item, "ambiguous option: %s", argument); 111 else 112 cmdq_error(item, "invalid option: %s", argument); 113 goto fail; 114 } 115 scope = options_scope_from_name(args, window, name, fs, &oo, &cause); 116 if (scope == OPTIONS_TABLE_NONE) { 117 if (args_has(args, 'q')) 118 goto fail; 119 cmdq_error(item, "%s", cause); 120 free(cause); 121 goto fail; 122 } 123 o = options_get_only(oo, name); 124 if (args_has(args, 'A') && o == NULL) { 125 o = options_get(oo, name); 126 parent = 1; 127 } else 128 parent = 0; 129 if (o != NULL) 130 cmd_show_options_print(self, item, o, idx, parent); 131 132 free(name); 133 free(argument); 134 return (CMD_RETURN_NORMAL); 135 136 fail: 137 free(name); 138 free(argument); 139 return (CMD_RETURN_ERROR); 140 } 141 142 static void 143 cmd_show_options_print(struct cmd *self, struct cmdq_item *item, 144 struct options_entry *o, int idx, int parent) 145 { 146 struct options_array_item *a; 147 const char *name = options_name(o); 148 char *value, *tmp = NULL, *escaped; 149 150 if (idx != -1) { 151 xasprintf(&tmp, "%s[%d]", name, idx); 152 name = tmp; 153 } else { 154 if (options_isarray(o)) { 155 a = options_array_first(o); 156 if (a == NULL) { 157 if (!args_has(self->args, 'v')) 158 cmdq_print(item, "%s", name); 159 return; 160 } 161 while (a != NULL) { 162 idx = options_array_item_index(a); 163 cmd_show_options_print(self, item, o, idx, 164 parent); 165 a = options_array_next(a); 166 } 167 return; 168 } 169 } 170 171 value = options_tostring(o, idx, 0); 172 if (args_has(self->args, 'v')) 173 cmdq_print(item, "%s", value); 174 else if (options_isstring(o)) { 175 escaped = args_escape(value); 176 if (parent) 177 cmdq_print(item, "%s* %s", name, escaped); 178 else 179 cmdq_print(item, "%s %s", name, escaped); 180 free(escaped); 181 } else { 182 if (parent) 183 cmdq_print(item, "%s* %s", name, value); 184 else 185 cmdq_print(item, "%s %s", name, value); 186 } 187 free(value); 188 189 free(tmp); 190 } 191 192 static enum cmd_retval 193 cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope, 194 struct options *oo) 195 { 196 const struct options_table_entry *oe; 197 struct options_entry *o; 198 struct options_array_item *a; 199 const char *name; 200 u_int idx; 201 int parent; 202 203 o = options_first(oo); 204 while (o != NULL) { 205 if (options_table_entry(o) == NULL) 206 cmd_show_options_print(self, item, o, -1, 0); 207 o = options_next(o); 208 } 209 for (oe = options_table; oe->name != NULL; oe++) { 210 if (~oe->scope & scope) 211 continue; 212 213 if ((self->entry != &cmd_show_hooks_entry && 214 !args_has(self->args, 'H') && 215 oe != NULL && 216 (oe->flags & OPTIONS_TABLE_IS_HOOK)) || 217 (self->entry == &cmd_show_hooks_entry && 218 (oe == NULL || 219 (~oe->flags & OPTIONS_TABLE_IS_HOOK)))) 220 continue; 221 222 o = options_get_only(oo, oe->name); 223 if (o == NULL) { 224 if (!args_has(self->args, 'A')) 225 continue; 226 o = options_get(oo, oe->name); 227 if (o == NULL) 228 continue; 229 parent = 1; 230 } else 231 parent = 0; 232 233 if (!options_isarray(o)) 234 cmd_show_options_print(self, item, o, -1, parent); 235 else if ((a = options_array_first(o)) == NULL) { 236 if (!args_has(self->args, 'v')) { 237 name = options_name(o); 238 if (parent) 239 cmdq_print(item, "%s*", name); 240 else 241 cmdq_print(item, "%s", name); 242 } 243 } else { 244 while (a != NULL) { 245 idx = options_array_item_index(a); 246 cmd_show_options_print(self, item, o, idx, 247 parent); 248 a = options_array_next(a); 249 } 250 } 251 } 252 return (CMD_RETURN_NORMAL); 253 } 254