Coverage Report

Created: 2026-08-13 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-set-option.c
Line
Count
Source
1
/* $OpenBSD: cmd-set-option.c,v 1.146 2026/07/10 15:20:06 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
 * Set an option.
28
 */
29
30
static enum args_parse_type cmd_set_option_args_parse(struct args *,
31
            u_int, char **);
32
static enum cmd_retval    cmd_set_option_exec(struct cmd *,
33
            struct cmdq_item *);
34
static enum cmd_retval    cmd_set_hook_event_exec(struct cmd *,
35
            struct cmdq_item *);
36
static enum cmd_retval    cmd_set_hook_monitor_exec(struct cmdq_item *,
37
            struct args *, int);
38
39
const struct cmd_entry cmd_set_option_entry = {
40
  .name = "set-option",
41
  .alias = "set",
42
43
  .args = { "aFgopqst:uUw", 1, 2, cmd_set_option_args_parse },
44
  .usage = "[-aFgopqsuUw] " CMD_TARGET_PANE_USAGE " option [value]",
45
46
  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
47
48
  .flags = CMD_AFTERHOOK,
49
  .exec = cmd_set_option_exec
50
};
51
52
const struct cmd_entry cmd_set_window_option_entry = {
53
  .name = "set-window-option",
54
  .alias = "setw",
55
56
  .args = { "aFgoqt:u", 1, 2, cmd_set_option_args_parse },
57
  .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
58
59
  .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
60
61
  .flags = CMD_AFTERHOOK,
62
  .exec = cmd_set_option_exec
63
};
64
65
const struct cmd_entry cmd_set_hook_entry = {
66
  .name = "set-hook",
67
  .alias = NULL,
68
69
  .args = { "agpERTt:uB:w", 0, 2, cmd_set_option_args_parse },
70
  .usage = "[-agpERTuw] [-B name:what:format] " CMD_TARGET_PANE_USAGE " "
71
     "[hook] [command]",
72
73
  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
74
75
  .flags = CMD_AFTERHOOK,
76
  .exec = cmd_set_option_exec
77
};
78
79
static enum args_parse_type
80
cmd_set_option_args_parse(struct args *args, u_int idx,
81
    __unused char **cause)
82
0
{
83
0
  if (args_has(args, 'B'))
84
0
    return (ARGS_PARSE_COMMANDS_OR_STRING);
85
0
  if (idx == 1)
86
0
    return (ARGS_PARSE_COMMANDS_OR_STRING);
87
0
  return (ARGS_PARSE_STRING);
88
0
}
89
90
static enum cmd_retval
91
cmd_set_hook_event_exec(struct cmd *self, struct cmdq_item *item)
92
0
{
93
0
  struct args   *args = cmd_get_args(self);
94
0
  struct cmd_find_state *target = cmdq_get_target(item);
95
0
  struct event_payload  *ep;
96
0
  struct client   *c;
97
0
  char      *argument;
98
99
0
  if (args_count(args) == 0) {
100
0
    cmdq_error(item, "missing argument");
101
0
    return (CMD_RETURN_ERROR);
102
0
  }
103
0
  if (args_count(args) != 1) {
104
0
    cmdq_error(item, "too many arguments");
105
0
    return (CMD_RETURN_ERROR);
106
0
  }
107
108
0
  argument = format_single_from_target(item, args_string(args, 0));
109
0
  if (*argument != '@') {
110
0
    cmdq_error(item, "event name must start with @");
111
0
    free(argument);
112
0
    return (CMD_RETURN_ERROR);
113
0
  }
114
115
0
  ep = event_payload_create();
116
0
  event_payload_set_target(ep, target);
117
0
  c = cmdq_get_client(item);
118
0
  if (c != NULL)
119
0
    event_payload_set_client(ep, "client", c);
120
0
  if (target->s != NULL)
121
0
    event_payload_set_session(ep, "session", target->s);
122
0
  if (target->w != NULL)
123
0
    event_payload_set_window(ep, "window", target->w);
124
0
  if (target->wl != NULL)
125
0
    event_payload_set_int(ep, "window_index", target->wl->idx);
126
0
  else if (target->idx != -1)
127
0
    event_payload_set_int(ep, "window_index", target->idx);
128
0
  if (target->wp != NULL)
129
0
    event_payload_set_pane(ep, "pane", target->wp);
130
0
  events_fire(argument, ep);
131
0
  free(argument);
132
0
  return (CMD_RETURN_NORMAL);
133
0
}
134
135
static enum cmd_retval
136
cmd_set_hook_monitor_exec(struct cmdq_item *item, struct args *args, int window)
137
0
{
138
0
  struct cmd_find_state *target = cmdq_get_target(item), fs;
139
0
  struct options    *oo;
140
0
  struct options_entry  *o;
141
0
  struct session    *s = NULL;
142
0
  char      *cause = NULL, *name = NULL, *format = NULL;
143
0
  char      *expanded = NULL, *newvalue = NULL;
144
0
  const char    *value, *old;
145
0
  enum monitor_type  type;
146
0
  int      id, scope, flags = 0;
147
148
0
  if (args_count(args) > 1) {
149
0
    cmdq_error(item, "too many arguments");
150
0
    return (CMD_RETURN_ERROR);
151
0
  }
152
153
0
  value = args_get(args, 'B');
154
0
  if (args_has(args, 'u')) {
155
0
    if (monitor_parse(value, &name, &type, &id, &format) != 0)
156
0
      name = xstrdup(value);
157
0
    free(format);
158
0
    format = NULL;
159
0
  } else {
160
0
    if (monitor_parse(value, &name, &type, &id, &format) != 0) {
161
0
      cmdq_error(item, "invalid subscription: %s", value);
162
0
      return (CMD_RETURN_ERROR);
163
0
    }
164
0
  }
165
166
0
  if (*name != '@') {
167
0
    cmdq_error(item, "monitor hook name must start with @");
168
0
    goto fail;
169
0
  }
170
171
0
  scope = options_scope_from_name(args, window, name, target, &oo,
172
0
      &cause);
173
0
  if (scope == OPTIONS_TABLE_NONE) {
174
0
    cmdq_error(item, "%s", cause);
175
0
    free(cause);
176
0
    goto fail;
177
0
  }
178
0
  cmd_find_copy_state(&fs, target);
179
180
0
  if (args_has(args, 'u')) {
181
0
    hooks_monitor_remove(oo, name);
182
0
    goto out;
183
0
  }
184
185
0
  if (args_count(args) != 0) {
186
0
    value = args_string(args, 0);
187
0
    if (args_has(args, 'F')) {
188
0
      expanded = format_single_from_target(item, value);
189
0
      value = expanded;
190
0
    }
191
0
    o = options_get_only(oo, name);
192
0
    if (!args_has(args, 'o') || o == NULL) {
193
0
      if (args_has(args, 'a') && o != NULL) {
194
0
        old = options_get_string(oo, name);
195
0
        xasprintf(&newvalue, "%s%s", old, value);
196
0
        value = newvalue;
197
0
      }
198
0
      options_set_string(oo, name, 0, "%s", value);
199
0
      options_push_changes(name);
200
0
    }
201
0
  }
202
203
0
  if (oo != global_options &&
204
0
      oo != global_s_options &&
205
0
      oo != global_w_options)
206
0
    s = target->s;
207
0
  if (args_has(args, 'T'))
208
0
    flags |= MONITOR_NOTIFY_TRUE;
209
0
  hooks_monitor_add(item, oo, name, type, id, format, flags, &fs, s);
210
211
0
out:
212
0
  free(newvalue);
213
0
  free(expanded);
214
0
  free(name);
215
0
  free(format);
216
0
  return (CMD_RETURN_NORMAL);
217
218
0
fail:
219
0
  free(newvalue);
220
0
  free(expanded);
221
0
  free(name);
222
0
  free(format);
223
0
  return (CMD_RETURN_ERROR);
224
0
}
225
226
static enum cmd_retval
227
cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
228
0
{
229
0
  struct args     *args = cmd_get_args(self);
230
0
  int        append = args_has(args, 'a');
231
0
  struct cmd_find_state   *target = cmdq_get_target(item);
232
0
  struct window_pane    *loop;
233
0
  struct options      *oo;
234
0
  struct options_entry    *parent, *o, *po;
235
0
  char        *name, *argument, *cause;
236
0
  char        *expanded = NULL, *array_key = NULL;
237
0
  const char      *value;
238
0
  int        window, already, error, ambiguous;
239
0
  int        scope;
240
241
0
  window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
242
0
  if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'E'))
243
0
    return (cmd_set_hook_event_exec(self, item));
244
0
  if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'B'))
245
0
    return (cmd_set_hook_monitor_exec(item, args, window));
246
0
  if (args_count(args) == 0) {
247
0
    cmdq_error(item, "missing argument");
248
0
    return (CMD_RETURN_ERROR);
249
0
  }
250
251
  /* Expand argument. */
252
0
  argument = format_single_from_target(item, args_string(args, 0));
253
254
  /* If set-hook -R, fire the hook straight away. */
255
0
  if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
256
0
    hooks_run(item, argument);
257
0
    free(argument);
258
0
    return (CMD_RETURN_NORMAL);
259
0
  }
260
261
  /* Parse option name and array key. */
262
0
  name = options_match(argument, &array_key, &ambiguous);
263
0
  if (name == NULL) {
264
0
    if (args_has(args, 'q'))
265
0
      goto out;
266
0
    if (ambiguous)
267
0
      cmdq_error(item, "ambiguous option: %s", argument);
268
0
    else
269
0
      cmdq_error(item, "invalid option: %s", argument);
270
0
    goto fail;
271
0
  }
272
0
  if (args_count(args) < 2)
273
0
    value = NULL;
274
0
  else
275
0
    value = args_string(args, 1);
276
0
  if (value != NULL && args_has(args, 'F')) {
277
0
    expanded = format_single_from_target(item, value);
278
0
    value = expanded;
279
0
  }
280
281
  /* Get the scope and table for the option .*/
282
0
  scope = options_scope_from_name(args, window, name, target, &oo,
283
0
      &cause);
284
0
  if (scope == OPTIONS_TABLE_NONE) {
285
0
    if (args_has(args, 'q'))
286
0
      goto out;
287
0
    cmdq_error(item, "%s", cause);
288
0
    free(cause);
289
0
    goto fail;
290
0
  }
291
0
  o = options_get_only(oo, name);
292
0
  parent = options_get(oo, name);
293
294
  /* Check that array options and keys match up. */
295
0
  if (array_key != NULL && (*name == '@' || !options_is_array(parent))) {
296
0
    cmdq_error(item, "not an array: %s", argument);
297
0
    goto fail;
298
0
  }
299
300
  /* With -o, check this option is not already set. */
301
0
  if (!args_has(args, 'u') && args_has(args, 'o')) {
302
0
    if (array_key == NULL)
303
0
      already = (o != NULL);
304
0
    else {
305
0
      if (o == NULL)
306
0
        already = 0;
307
0
      else if (options_array_get(o, array_key) != NULL)
308
0
        already = 1;
309
0
      else
310
0
        already = 0;
311
0
    }
312
0
    if (already) {
313
0
      if (args_has(args, 'q'))
314
0
        goto out;
315
0
      cmdq_error(item, "already set: %s", argument);
316
0
      goto fail;
317
0
    }
318
0
  }
319
320
  /* Change the option. */
321
0
  if (args_has(args, 'U') && scope == OPTIONS_TABLE_WINDOW) {
322
0
    TAILQ_FOREACH(loop, &target->w->panes, entry) {
323
0
      po = options_get_only(loop->options, name);
324
0
      if (po == NULL)
325
0
        continue;
326
0
      if (options_remove_or_default(po, array_key,
327
0
          &cause) != 0) {
328
0
        cmdq_error(item, "%s", cause);
329
0
        free(cause);
330
0
        goto fail;
331
0
      }
332
0
    }
333
0
  }
334
0
  if (args_has(args, 'u') || args_has(args, 'U')) {
335
0
    if (o == NULL)
336
0
      goto out;
337
0
    if (options_remove_or_default(o, array_key, &cause) != 0) {
338
0
      cmdq_error(item, "%s", cause);
339
0
      free(cause);
340
0
      goto fail;
341
0
    }
342
0
  } else if (*name == '@') {
343
0
    if (value == NULL) {
344
0
      cmdq_error(item, "empty value");
345
0
      goto fail;
346
0
    }
347
0
    options_set_string(oo, name, append, "%s", value);
348
0
    if (cmd_get_entry(self) == &cmd_set_hook_entry)
349
0
      hooks_add_event(name);
350
0
  } else if (array_key == NULL && !options_is_array(parent)) {
351
0
    error = options_from_string(oo, options_table_entry(parent),
352
0
        options_table_entry(parent)->name, value,
353
0
        args_has(args, 'a'), &cause);
354
0
    if (error != 0) {
355
0
      cmdq_error(item, "%s", cause);
356
0
      free(cause);
357
0
      goto fail;
358
0
    }
359
0
  } else {
360
0
    if (value == NULL) {
361
0
      cmdq_error(item, "empty value");
362
0
      goto fail;
363
0
    }
364
0
    if (o == NULL)
365
0
      o = options_empty(oo, options_table_entry(parent));
366
0
    if (array_key == NULL) {
367
0
      if (!append)
368
0
        options_array_clear(o);
369
0
      if (options_array_assign(o, value, &cause) != 0) {
370
0
        cmdq_error(item, "%s", cause);
371
0
        free(cause);
372
0
        goto fail;
373
0
      }
374
0
    } else if (options_array_set(o, array_key, value, append,
375
0
        &cause) != 0) {
376
0
      cmdq_error(item, "%s", cause);
377
0
      free(cause);
378
0
      goto fail;
379
0
    }
380
0
  }
381
382
0
  options_push_changes(name);
383
384
0
out:
385
0
  free(argument);
386
0
  free(expanded);
387
0
  free(name);
388
0
  free(array_key);
389
0
  return (CMD_RETURN_NORMAL);
390
391
0
fail:
392
0
  free(argument);
393
0
  free(expanded);
394
0
  free(name);
395
0
  free(array_key);
396
0
  return (CMD_RETURN_ERROR);
397
0
}