Coverage Report

Created: 2026-08-31 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-show-options.c
Line
Count
Source
1
/* $OpenBSD: cmd-show-options.c,v 1.76 2026/07/27 19:15:58 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
24
#include "tmux.h"
25
26
/*
27
 * Show options.
28
 */
29
30
#define SHOW_OPTIONS_TEMPLATE           \
31
0
  "#{?option_value_only,"           \
32
0
  "#{option_value},"            \
33
0
  "#{option_name}#{?option_has_array_key,"      \
34
0
  "[#{option_array_key}],}"         \
35
0
  "#{?option_is_parent,*,}"         \
36
0
  "#{?option_has_value, "           \
37
0
  "#{?option_is_string,#{q/a:option_value},#{option_value}},}}"
38
#define SHOW_HOOKS_MONITOR_TEMPLATE         \
39
0
  "#{option_name}:#{hook_monitor_target}:#{hook_monitor_format}"
40
41
static enum cmd_retval  cmd_show_options_exec(struct cmd *, struct cmdq_item *);
42
43
static void   cmd_show_options_print(struct cmd *, struct cmdq_item *,
44
          struct options_entry *, const char *, int);
45
static void   cmd_show_hooks_print_monitor(struct cmd *,
46
          struct cmdq_item *, struct options_entry *);
47
static enum cmd_retval  cmd_show_options_all(struct cmd *, struct cmdq_item *,
48
          int, struct options *);
49
50
const struct cmd_entry cmd_show_options_entry = {
51
  .name = "show-options",
52
  .alias = "show",
53
54
  .args = { "AgF:Hpqst:vw", 0, 1, NULL },
55
  .usage = "[-AgHpqsvw] [-F format] " CMD_TARGET_PANE_USAGE " [option]",
56
57
  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
58
59
  .flags = CMD_AFTERHOOK,
60
  .exec = cmd_show_options_exec
61
};
62
63
const struct cmd_entry cmd_show_window_options_entry = {
64
  .name = "show-window-options",
65
  .alias = "showw",
66
67
  .args = { "F:gvt:", 0, 1, NULL },
68
  .usage = "[-gv] [-F format] " CMD_TARGET_WINDOW_USAGE " [option]",
69
70
  .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
71
72
  .flags = CMD_AFTERHOOK,
73
  .exec = cmd_show_options_exec
74
};
75
76
const struct cmd_entry cmd_show_hooks_entry = {
77
  .name = "show-hooks",
78
  .alias = NULL,
79
80
  .args = { "BF:gpt:w", 0, 1, NULL },
81
  .usage = "[-Bgpw] [-F format] " CMD_TARGET_PANE_USAGE " [hook]",
82
83
  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
84
85
  .flags = CMD_AFTERHOOK,
86
  .exec = cmd_show_options_exec
87
};
88
89
static enum cmd_retval
90
cmd_show_options_exec(struct cmd *self, struct cmdq_item *item)
91
0
{
92
0
  struct args     *args = cmd_get_args(self);
93
0
  struct cmd_find_state   *target = cmdq_get_target(item);
94
0
  struct options      *oo;
95
0
  char        *argument, *name = NULL, *cause;
96
0
  char        *array_key = NULL;
97
0
  int        window, ambiguous, parent, print_parent, scope;
98
0
  struct options_entry    *o;
99
100
0
  window = (cmd_get_entry(self) == &cmd_show_window_options_entry);
101
102
0
  if (args_count(args) == 0) {
103
0
    scope = options_scope_from_flags(args, window, target, &oo,
104
0
        &cause);
105
0
    if (scope == OPTIONS_TABLE_NONE) {
106
0
      if (args_has(args, 'q'))
107
0
        return (CMD_RETURN_NORMAL);
108
0
      cmdq_error(item, "%s", cause);
109
0
      free(cause);
110
0
      return (CMD_RETURN_ERROR);
111
0
    }
112
0
    if (cmd_get_entry(self) == &cmd_show_hooks_entry &&
113
0
        args_has(args, 'B')) {
114
0
      o = options_first(oo);
115
0
      while (o != NULL) {
116
0
        cmd_show_hooks_print_monitor(self, item, o);
117
0
        o = options_next(o);
118
0
      }
119
0
      return (CMD_RETURN_NORMAL);
120
0
    }
121
0
    return (cmd_show_options_all(self, item, scope, oo));
122
0
  }
123
0
  argument = format_single_from_target(item, args_string(args, 0));
124
125
0
  name = options_match(argument, &array_key, &ambiguous);
126
0
  if (name == NULL) {
127
0
    if (args_has(args, 'q'))
128
0
      goto out;
129
0
    if (ambiguous)
130
0
      cmdq_error(item, "ambiguous option: %s", argument);
131
0
    else
132
0
      cmdq_error(item, "invalid option: %s", argument);
133
0
    goto fail;
134
0
  }
135
0
  scope = options_scope_from_name(args, window, name, target, &oo,
136
0
      &cause);
137
0
  if (scope == OPTIONS_TABLE_NONE) {
138
0
    if (args_has(args, 'q'))
139
0
      goto out;
140
0
    cmdq_error(item, "%s", cause);
141
0
    free(cause);
142
0
    goto fail;
143
0
  }
144
0
  o = options_get_only(oo, name);
145
0
  if (args_has(args, 'A') && o == NULL) {
146
0
    o = options_get(oo, name);
147
0
    parent = 1;
148
0
  } else
149
0
    parent = 0;
150
0
  if (o != NULL) {
151
0
    if (cmd_get_entry(self) == &cmd_show_hooks_entry &&
152
0
        args_has(args, 'B'))
153
0
      cmd_show_hooks_print_monitor(self, item, o);
154
0
    else {
155
0
      print_parent = parent;
156
0
      if (array_key == NULL && options_is_array(o) &&
157
0
          options_array_first(o) == NULL) {
158
0
        print_parent = 0;
159
0
      }
160
0
      cmd_show_options_print(self, item, o, array_key,
161
0
          print_parent);
162
0
    }
163
0
  } else if (*name == '@') {
164
0
    if (args_has(args, 'q'))
165
0
      goto out;
166
0
    cmdq_error(item, "invalid option: %s", argument);
167
0
    goto fail;
168
0
  }
169
170
0
out:
171
0
  free(name);
172
0
  free(array_key);
173
0
  free(argument);
174
0
  return (CMD_RETURN_NORMAL);
175
176
0
fail:
177
0
  free(name);
178
0
  free(array_key);
179
0
  free(argument);
180
0
  return (CMD_RETURN_ERROR);
181
0
}
182
183
static void
184
cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
185
    struct options_entry *o, const char *array_key, int parent)
186
0
{
187
0
  struct args      *args = cmd_get_args(self);
188
0
  struct options_array_item  *a;
189
0
  struct format_tree     *ft;
190
0
  const char       *name = options_name(o);
191
0
  const char       *template = args_get(args, 'F');
192
0
  char         *value, *line;
193
0
  struct timeval        tv = { 0 };
194
0
  u_int         fire_count;
195
0
  time_t          fire_time;
196
0
  int         is_hook = 0, is_user = 0;
197
0
  int         has_value = 1;
198
0
  const struct options_table_entry *oe = options_table_entry(o);
199
200
0
  if (array_key != NULL)
201
0
    value = options_to_string(o, array_key, 0);
202
0
  else if (options_is_array(o)) {
203
0
    a = options_array_first(o);
204
0
    if (a != NULL) {
205
0
      while (a != NULL) {
206
0
        array_key = options_array_item_key(a);
207
0
        cmd_show_options_print(self, item, o, array_key,
208
0
            parent);
209
0
        a = options_array_next(a);
210
0
      }
211
0
      return;
212
0
    }
213
0
    if (template == NULL && args_has(args, 'v'))
214
0
      return;
215
0
    value = xstrdup("");
216
0
    has_value = 0;
217
0
  } else
218
0
    value = options_to_string(o, NULL, 0);
219
220
0
  if (template == NULL)
221
0
    template = SHOW_OPTIONS_TEMPLATE;
222
223
0
  if (oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK))
224
0
    is_hook = 1;
225
0
  else if (oe == NULL)
226
0
    is_user = 1;
227
228
0
  ft = format_create_from_target(item);
229
0
  format_add(ft, "option_name", "%s", name);
230
0
  format_add(ft, "option_value", "%s", value);
231
0
  format_add(ft, "option_value_only", "%d", args_has(args, 'v'));
232
0
  format_add(ft, "option_is_parent", "%d", parent);
233
0
  format_add(ft, "option_is_array", "%d", options_is_array(o));
234
0
  format_add(ft, "option_is_string", "%d", options_is_string(o));
235
0
  format_add(ft, "option_is_hook", "%d", is_hook);
236
0
  format_add(ft, "option_is_user", "%d", is_user);
237
0
  format_add(ft, "option_has_value", "%d", has_value);
238
0
  if (cmd_get_entry(self) == &cmd_show_hooks_entry) {
239
0
    fire_count = options_get_fire_count(o);
240
0
    format_add(ft, "hook_fire_count", "%u", fire_count);
241
242
0
    fire_time = options_get_fire_time(o);
243
0
    if (fire_time != 0) {
244
0
      tv.tv_sec = fire_time;
245
0
      format_add_tv(ft, "hook_fire_time", &tv);
246
0
    }
247
0
  }
248
0
  if (array_key != NULL) {
249
0
    format_add(ft, "option_array_key", "%s", array_key);
250
0
    format_add(ft, "option_has_array_key", "1");
251
0
  } else {
252
0
    format_add(ft, "option_array_key", "%s", "");
253
0
    format_add(ft, "option_has_array_key", "0");
254
0
  }
255
0
  line = format_expand(ft, template);
256
0
  format_free(ft);
257
258
0
  cmdq_print(item, "%s", line);
259
0
  free(line);
260
0
  free(value);
261
0
}
262
263
static void
264
cmd_show_hooks_print_monitor(struct cmd *self, struct cmdq_item *item,
265
    struct options_entry *o)
266
0
{
267
0
  struct args   *args = cmd_get_args(self);
268
0
  struct format_tree  *ft;
269
0
  enum monitor_type  type;
270
0
  const char    *template = args_get(args, 'F'), *format;
271
0
  char      *value, *target, *line;
272
0
  struct timeval     tv = { 0 };
273
0
  u_int      fire_count;
274
0
  time_t       fire_time;
275
0
  int      id;
276
277
0
  value = hooks_monitor_to_string(o);
278
0
  if (value == NULL)
279
0
    return;
280
0
  if (!hooks_monitor_get(o, &type, &id, &format)) {
281
0
    free(value);
282
0
    return;
283
0
  }
284
0
  if (template == NULL)
285
0
    template = SHOW_HOOKS_MONITOR_TEMPLATE;
286
287
0
  switch (type) {
288
0
  case MONITOR_SESSION:
289
0
    target = xstrdup("");
290
0
    break;
291
0
  case MONITOR_PANE:
292
0
    xasprintf(&target, "%%%d", id);
293
0
    break;
294
0
  case MONITOR_ALL_PANES:
295
0
    target = xstrdup("%*");
296
0
    break;
297
0
  case MONITOR_WINDOW:
298
0
    xasprintf(&target, "@%d", id);
299
0
    break;
300
0
  case MONITOR_ALL_WINDOWS:
301
0
    target = xstrdup("@*");
302
0
    break;
303
0
  }
304
305
0
  ft = format_create_from_target(item);
306
0
  format_add(ft, "option_name", "%s", options_name(o));
307
0
  format_add(ft, "option_value", "%s", value);
308
0
  format_add(ft, "option_value_only", "%d", 0);
309
0
  format_add(ft, "option_is_parent", "%d", 0);
310
0
  format_add(ft, "option_is_array", "%d", 0);
311
0
  format_add(ft, "option_is_string", "%d", 1);
312
0
  format_add(ft, "option_is_hook", "%d", 1);
313
0
  format_add(ft, "option_is_user", "%d", 1);
314
0
  format_add(ft, "option_has_value", "%d", 1);
315
0
  format_add(ft, "option_array_key", "%s", "");
316
0
  format_add(ft, "option_has_array_key", "0");
317
318
0
  format_add(ft, "hook_monitor_target", "%s", target);
319
0
  format_add(ft, "hook_monitor_format", "%s", format);
320
321
0
  fire_count = hooks_monitor_get_fire_count(o);
322
0
  format_add(ft, "hook_fire_count", "%u", fire_count);
323
324
0
  fire_time = hooks_monitor_get_fire_time(o);
325
0
  if (fire_time != 0) {
326
0
    tv.tv_sec = fire_time;
327
0
    format_add_tv(ft, "hook_fire_time", &tv);
328
0
  }
329
330
0
  line = format_expand(ft, template);
331
0
  format_free(ft);
332
333
0
  cmdq_print(item, "%s", line);
334
0
  free(line);
335
0
  free(target);
336
0
  free(value);
337
0
}
338
339
static enum cmd_retval
340
cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
341
    struct options *oo)
342
0
{
343
0
  struct args       *args = cmd_get_args(self);
344
0
  const struct options_table_entry  *oe;
345
0
  struct options_entry      *o;
346
0
  const char        *name;
347
0
  int          parent, is_user_hook;
348
349
0
  o = options_first(oo);
350
0
  while (o != NULL) {
351
0
    if (options_table_entry(o) == NULL) {
352
0
      name = options_name(o);
353
0
      is_user_hook = 0;
354
0
      if (*name == '@') {
355
0
        if (hooks_is_event(name) ||
356
0
            options_get_monitor_data(o) != NULL) {
357
0
          is_user_hook = 1;
358
0
        }
359
0
      }
360
0
      if (cmd_get_entry(self) != &cmd_show_hooks_entry) {
361
0
        if (!is_user_hook || args_has(args, 'H')) {
362
0
          cmd_show_options_print(self, item, o,
363
0
              NULL, 0);
364
0
        }
365
0
      } else if (is_user_hook)
366
0
        cmd_show_options_print(self, item, o, NULL, 0);
367
0
    }
368
0
    o = options_next(o);
369
0
  }
370
0
  for (oe = options_table; oe->name != NULL; oe++) {
371
0
    if (~oe->scope & scope)
372
0
      continue;
373
374
0
    if ((cmd_get_entry(self) != &cmd_show_hooks_entry &&
375
0
        !args_has(args, 'H') &&
376
0
        (oe->flags & OPTIONS_TABLE_IS_HOOK)) ||
377
0
        (cmd_get_entry(self) == &cmd_show_hooks_entry &&
378
0
        (~oe->flags & OPTIONS_TABLE_IS_HOOK)))
379
0
      continue;
380
381
0
    o = options_get_only(oo, oe->name);
382
0
    if (o == NULL) {
383
0
      if (!args_has(args, 'A'))
384
0
        continue;
385
0
      o = options_get(oo, oe->name);
386
0
      if (o == NULL)
387
0
        continue;
388
0
      parent = 1;
389
0
    } else
390
0
      parent = 0;
391
392
0
    cmd_show_options_print(self, item, o, NULL, parent);
393
0
  }
394
0
  return (CMD_RETURN_NORMAL);
395
0
}