1 /* $OpenBSD: cmd-show-options.c,v 1.38 2017/01/18 10:08:05 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 enum cmd_retval cmd_show_options_one(struct cmd *, struct cmdq_item *, 34 struct options *); 35 static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *, 36 struct options *); 37 38 const struct cmd_entry cmd_show_options_entry = { 39 .name = "show-options", 40 .alias = "show", 41 42 .args = { "gqst:vw", 0, 1 }, 43 .usage = "[-gqsvw] [-t target-session|target-window] [option]", 44 45 .tflag = CMD_WINDOW_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 .tflag = CMD_WINDOW_CANFAIL, 59 60 .flags = CMD_AFTERHOOK, 61 .exec = cmd_show_options_exec 62 }; 63 64 static enum cmd_retval 65 cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) 66 { 67 struct args *args = self->args; 68 struct cmd_find_state *fs = &item->state.tflag; 69 struct options *oo; 70 enum options_table_scope scope; 71 char *cause; 72 int window; 73 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); 80 } 81 82 if (args->argc == 0) 83 return (cmd_show_options_all(self, item, oo)); 84 else 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, 90 struct options_entry *o, int idx) 91 { 92 const char *name; 93 const char *value; 94 char *tmp, *escaped; 95 96 if (idx != -1) { 97 xasprintf(&tmp, "%s[%d]", options_name(o), idx); 98 name = tmp; 99 } else { 100 tmp = NULL; 101 name = options_name(o); 102 } 103 104 value = options_tostring(o, idx); 105 if (args_has(self->args, 'v')) 106 cmdq_print(item, "%s", value); 107 else if (options_isstring(o)) { 108 utf8_stravis(&escaped, value, VIS_OCTAL|VIS_TAB|VIS_NL|VIS_DQ); 109 cmdq_print(item, "%s \"%s\"", name, escaped); 110 free(escaped); 111 } else 112 cmdq_print(item, "%s %s", name, value); 113 114 free(tmp); 115 } 116 117 static enum cmd_retval 118 cmd_show_options_one(struct cmd *self, struct cmdq_item *item, 119 struct options *oo) 120 { 121 struct args *args = self->args; 122 struct options_entry *o; 123 int idx, ambiguous; 124 const char *name = args->argv[0]; 125 126 o = options_match_get(oo, name, &idx, 1, &ambiguous); 127 if (o == NULL) { 128 if (args_has(args, 'q')) 129 return (CMD_RETURN_NORMAL); 130 if (ambiguous) { 131 cmdq_error(item, "ambiguous option: %s", name); 132 return (CMD_RETURN_ERROR); 133 } 134 if (*name != '@' && 135 options_match_get(oo, name, &idx, 0, &ambiguous) != NULL) 136 return (CMD_RETURN_NORMAL); 137 cmdq_error(item, "unknown option: %s", name); 138 return (CMD_RETURN_ERROR); 139 } 140 cmd_show_options_print(self, item, o, idx); 141 return (CMD_RETURN_NORMAL); 142 } 143 144 static enum cmd_retval 145 cmd_show_options_all(struct cmd *self, struct cmdq_item *item, 146 struct options *oo) 147 { 148 struct options_entry *o; 149 const struct options_table_entry *oe; 150 u_int size, idx; 151 152 o = options_first(oo); 153 while (o != NULL) { 154 oe = options_table_entry(o); 155 if (oe != NULL && oe->style != NULL) { 156 o = options_next(o); 157 continue; 158 } 159 if (options_array_size(o, &size) == -1) 160 cmd_show_options_print(self, item, o, -1); 161 else { 162 for (idx = 0; idx < size; idx++) { 163 if (options_array_get(o, idx) == NULL) 164 continue; 165 cmd_show_options_print(self, item, o, idx); 166 } 167 } 168 o = options_next(o); 169 } 170 return (CMD_RETURN_NORMAL); 171 } 172