tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

cmd-show-options.c
Go to the documentation of this file.
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 void cmd_show_options_print(struct cmd *, struct cmdq_item *,
33  struct options_entry *, int, int);
34 static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *,
35  int, struct options *);
36 
37 const struct cmd_entry cmd_show_options_entry = {
38  .name = "show-options",
39  .alias = "show",
40 
41  .args = { "AgHpqst:vw", 0, 1 },
42  .usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]",
43 
44  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
45 
46  .flags = CMD_AFTERHOOK,
47  .exec = cmd_show_options_exec
48 };
49 
51  .name = "show-window-options",
52  .alias = "showw",
53 
54  .args = { "gvt:", 0, 1 },
55  .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]",
56 
57  .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
58 
59  .flags = CMD_AFTERHOOK,
60  .exec = cmd_show_options_exec
61 };
62 
63 const struct cmd_entry cmd_show_hooks_entry = {
64  .name = "show-hooks",
65  .alias = NULL,
66 
67  .args = { "gpt:w", 0, 1 },
68  .usage = "[-gpw] " CMD_TARGET_PANE_USAGE,
69 
70  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
71 
72  .flags = CMD_AFTERHOOK,
73  .exec = cmd_show_options_exec
74 };
75 
76 static enum cmd_retval
77 cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
78 {
79  struct args *args = cmd_get_args(self);
80  struct cmd_find_state *target = cmdq_get_target(item);
81  struct options *oo;
82  char *argument, *name = NULL, *cause;
83  int window, idx, ambiguous, parent, scope;
84  struct options_entry *o;
85 
87 
88  if (args->argc == 0) {
89  scope = options_scope_from_flags(args, window, target, &oo,
90  &cause);
91  if (scope == OPTIONS_TABLE_NONE) {
92  if (args_has(args, 'q'))
93  return (CMD_RETURN_NORMAL);
94  cmdq_error(item, "%s", cause);
95  free(cause);
96  return (CMD_RETURN_ERROR);
97  }
98  return (cmd_show_options_all(self, item, scope, oo));
99  }
100  argument = format_single_from_target(item, args->argv[0]);
101 
102  name = options_match(argument, &idx, &ambiguous);
103  if (name == NULL) {
104  if (args_has(args, 'q'))
105  goto fail;
106  if (ambiguous)
107  cmdq_error(item, "ambiguous option: %s", argument);
108  else
109  cmdq_error(item, "invalid option: %s", argument);
110  goto fail;
111  }
112  scope = options_scope_from_name(args, window, name, target, &oo,
113  &cause);
114  if (scope == OPTIONS_TABLE_NONE) {
115  if (args_has(args, 'q'))
116  goto fail;
117  cmdq_error(item, "%s", cause);
118  free(cause);
119  goto fail;
120  }
121  o = options_get_only(oo, name);
122  if (args_has(args, 'A') && o == NULL) {
123  o = options_get(oo, name);
124  parent = 1;
125  } else
126  parent = 0;
127  if (o != NULL)
128  cmd_show_options_print(self, item, o, idx, parent);
129 
130  free(name);
131  free(argument);
132  return (CMD_RETURN_NORMAL);
133 
134 fail:
135  free(name);
136  free(argument);
137  return (CMD_RETURN_ERROR);
138 }
139 
140 static void
141 cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
142  struct options_entry *o, int idx, int parent)
143 {
144  struct args *args = cmd_get_args(self);
145  struct options_array_item *a;
146  const char *name = options_name(o);
147  char *value, *tmp = NULL, *escaped;
148 
149  if (idx != -1) {
150  xasprintf(&tmp, "%s[%d]", name, idx);
151  name = tmp;
152  } else {
153  if (options_is_array(o)) {
154  a = options_array_first(o);
155  if (a == NULL) {
156  if (!args_has(args, 'v'))
157  cmdq_print(item, "%s", name);
158  return;
159  }
160  while (a != NULL) {
161  idx = options_array_item_index(a);
162  cmd_show_options_print(self, item, o, idx,
163  parent);
164  a = options_array_next(a);
165  }
166  return;
167  }
168  }
169 
170  value = options_to_string(o, idx, 0);
171  if (args_has(args, 'v'))
172  cmdq_print(item, "%s", value);
173  else if (options_is_string(o)) {
174  escaped = args_escape(value);
175  if (parent)
176  cmdq_print(item, "%s* %s", name, escaped);
177  else
178  cmdq_print(item, "%s %s", name, escaped);
179  free(escaped);
180  } else {
181  if (parent)
182  cmdq_print(item, "%s* %s", name, value);
183  else
184  cmdq_print(item, "%s %s", name, value);
185  }
186  free(value);
187 
188  free(tmp);
189 }
190 
191 static enum cmd_retval
192 cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
193  struct options *oo)
194 {
195  struct args *args = cmd_get_args(self);
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  if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
204  o = options_first(oo);
205  while (o != NULL) {
206  if (options_table_entry(o) == NULL)
207  cmd_show_options_print(self, item, o, -1, 0);
208  o = options_next(o);
209  }
210  }
211  for (oe = options_table; oe->name != NULL; oe++) {
212  if (~oe->scope & scope)
213  continue;
214 
215  if ((cmd_get_entry(self) != &cmd_show_hooks_entry &&
216  !args_has(args, 'H') &&
217  (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
218  (cmd_get_entry(self) == &cmd_show_hooks_entry &&
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(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_is_array(o))
234  cmd_show_options_print(self, item, o, -1, parent);
235  else if ((a = options_array_first(o)) == NULL) {
236  if (!args_has(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 }
int args_has(struct args *args, u_char flag)
Definition: arguments.c:259
char * args_escape(const char *s)
Definition: arguments.c:212
void cmdq_print(struct cmdq_item *item, const char *fmt,...)
Definition: cmd-queue.c:792
struct cmd_find_state * cmdq_get_target(struct cmdq_item *item)
Definition: cmd-queue.c:171
void cmdq_error(struct cmdq_item *item, const char *fmt,...)
Definition: cmd-queue.c:833
static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *, int, struct options *)
const struct cmd_entry cmd_show_hooks_entry
static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *)
const struct cmd_entry cmd_show_window_options_entry
const struct cmd_entry cmd_show_options_entry
static void cmd_show_options_print(struct cmd *, struct cmdq_item *, struct options_entry *, int, int)
struct args * cmd_get_args(struct cmd *cmd)
Definition: cmd.c:393
const struct cmd_entry * cmd_get_entry(struct cmd *cmd)
Definition: cmd.c:386
char * format_single_from_target(struct cmdq_item *item, const char *fmt)
Definition: format.c:4643
const char * name
Definition: layout-set.c:38
const struct options_table_entry options_table[]
const struct options_table_entry * options_table_entry(struct options_entry *o)
Definition: options.c:363
int options_is_string(struct options_entry *o)
Definition: options.c:561
struct options_array_item * options_array_first(struct options_entry *o)
Definition: options.c:529
char * options_to_string(struct options_entry *o, int idx, int numeric)
Definition: options.c:567
int options_scope_from_name(struct args *args, int window, const char *name, struct cmd_find_state *fs, struct options **oo, char **cause)
Definition: options.c:773
u_int options_array_item_index(struct options_array_item *a)
Definition: options.c:543
char * options_match(const char *s, int *idx, int *ambiguous)
Definition: options.c:625
int options_is_array(struct options_entry *o)
Definition: options.c:555
int options_scope_from_flags(struct args *args, int window, struct cmd_find_state *fs, struct options **oo, char **cause)
Definition: options.c:844
const char * options_name(struct options_entry *o)
Definition: options.c:351
struct options_array_item * options_array_next(struct options_array_item *a)
Definition: options.c:537
struct options_entry * options_get(struct options *oo, const char *name)
Definition: options.c:229
struct options_entry * options_next(struct options_entry *o)
Definition: options.c:210
struct options_entry * options_first(struct options *oo)
Definition: options.c:204
struct options_entry * options_get_only(struct options *oo, const char *name)
Definition: options.c:216
Definition: tmux.h:1435
int argc
Definition: tmux.h:1437
char ** argv
Definition: tmux.h:1438
Definition: tmux.h:1525
const char * name
Definition: tmux.h:1526
Definition: cmd.c:212
union options_value value
Definition: options.c:36
Definition: options.c:50
Definition: tmux.h:1830
const char * name
Definition: tmux.h:1831
int flags
Definition: tmux.h:1835
int scope
Definition: tmux.h:1834
Definition: tmux.h:1041
#define CMD_FIND_CANFAIL
Definition: tmux.h:1465
#define OPTIONS_TABLE_NONE
Definition: tmux.h:1820
cmd_retval
Definition: tmux.h:1475
@ CMD_RETURN_NORMAL
Definition: tmux.h:1477
@ CMD_RETURN_ERROR
Definition: tmux.h:1476
#define CMD_AFTERHOOK
Definition: tmux.h:1541
#define CMD_TARGET_WINDOW_USAGE
Definition: tmux.h:1859
#define CMD_TARGET_PANE_USAGE
Definition: tmux.h:1858
#define OPTIONS_TABLE_IS_HOOK
Definition: tmux.h:1827
@ CMD_FIND_WINDOW
Definition: tmux.h:1444
@ CMD_FIND_PANE
Definition: tmux.h:1443
int xasprintf(char **ret, const char *fmt,...)
Definition: xmalloc.c:109