Coverage Report

Created: 2025-08-29 06:28

/src/tmux/cmd-set-option.c
Line
Count
Source (jump to first uncovered line)
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
 * 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
35
const struct cmd_entry cmd_set_option_entry = {
36
  .name = "set-option",
37
  .alias = "set",
38
39
  .args = { "aFgopqst:uUw", 1, 2, cmd_set_option_args_parse },
40
  .usage = "[-aFgopqsuUw] " CMD_TARGET_PANE_USAGE " option [value]",
41
42
  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
43
44
  .flags = CMD_AFTERHOOK,
45
  .exec = cmd_set_option_exec
46
};
47
48
const struct cmd_entry cmd_set_window_option_entry = {
49
  .name = "set-window-option",
50
  .alias = "setw",
51
52
  .args = { "aFgoqt:u", 1, 2, cmd_set_option_args_parse },
53
  .usage = "[-aFgoqu] " CMD_TARGET_WINDOW_USAGE " option [value]",
54
55
  .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
56
57
  .flags = CMD_AFTERHOOK,
58
  .exec = cmd_set_option_exec
59
};
60
61
const struct cmd_entry cmd_set_hook_entry = {
62
  .name = "set-hook",
63
  .alias = NULL,
64
65
  .args = { "agpRt:uw", 1, 2, cmd_set_option_args_parse },
66
  .usage = "[-agpRuw] " CMD_TARGET_PANE_USAGE " hook [command]",
67
68
  .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
69
70
  .flags = CMD_AFTERHOOK,
71
  .exec = cmd_set_option_exec
72
};
73
74
static enum args_parse_type
75
cmd_set_option_args_parse(__unused struct args *args, u_int idx,
76
    __unused char **cause)
77
0
{
78
0
  if (idx == 1)
79
0
    return (ARGS_PARSE_COMMANDS_OR_STRING);
80
0
  return (ARGS_PARSE_STRING);
81
0
}
82
83
static enum cmd_retval
84
cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
85
0
{
86
0
  struct args     *args = cmd_get_args(self);
87
0
  int        append = args_has(args, 'a');
88
0
  struct cmd_find_state   *target = cmdq_get_target(item);
89
0
  struct window_pane    *loop;
90
0
  struct options      *oo;
91
0
  struct options_entry    *parent, *o, *po;
92
0
  char        *name, *argument, *expanded = NULL;
93
0
  char        *cause;
94
0
  const char      *value;
95
0
  int        window, idx, already, error, ambiguous;
96
0
  int        scope;
97
98
0
  window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
99
100
  /* Expand argument. */
101
0
  argument = format_single_from_target(item, args_string(args, 0));
102
103
  /* If set-hook -R, fire the hook straight away. */
104
0
  if (cmd_get_entry(self) == &cmd_set_hook_entry && args_has(args, 'R')) {
105
0
    notify_hook(item, argument);
106
0
    free(argument);
107
0
    return (CMD_RETURN_NORMAL);
108
0
  }
109
110
  /* Parse option name and index. */
111
0
  name = options_match(argument, &idx, &ambiguous);
112
0
  if (name == NULL) {
113
0
    if (args_has(args, 'q'))
114
0
      goto out;
115
0
    if (ambiguous)
116
0
      cmdq_error(item, "ambiguous option: %s", argument);
117
0
    else
118
0
      cmdq_error(item, "invalid option: %s", argument);
119
0
    goto fail;
120
0
  }
121
0
  if (args_count(args) < 2)
122
0
    value = NULL;
123
0
  else
124
0
    value = args_string(args, 1);
125
0
  if (value != NULL && args_has(args, 'F')) {
126
0
    expanded = format_single_from_target(item, value);
127
0
    value = expanded;
128
0
  }
129
130
  /* Get the scope and table for the option .*/
131
0
  scope = options_scope_from_name(args, window, name, target, &oo,
132
0
      &cause);
133
0
  if (scope == OPTIONS_TABLE_NONE) {
134
0
    if (args_has(args, 'q'))
135
0
      goto out;
136
0
    cmdq_error(item, "%s", cause);
137
0
    free(cause);
138
0
    goto fail;
139
0
  }
140
0
  o = options_get_only(oo, name);
141
0
  parent = options_get(oo, name);
142
143
  /* Check that array options and indexes match up. */
144
0
  if (idx != -1 && (*name == '@' || !options_is_array(parent))) {
145
0
    cmdq_error(item, "not an array: %s", argument);
146
0
    goto fail;
147
0
  }
148
149
  /* With -o, check this option is not already set. */
150
0
  if (!args_has(args, 'u') && args_has(args, 'o')) {
151
0
    if (idx == -1)
152
0
      already = (o != NULL);
153
0
    else {
154
0
      if (o == NULL)
155
0
        already = 0;
156
0
      else
157
0
        already = (options_array_get(o, idx) != NULL);
158
0
    }
159
0
    if (already) {
160
0
      if (args_has(args, 'q'))
161
0
        goto out;
162
0
      cmdq_error(item, "already set: %s", argument);
163
0
      goto fail;
164
0
    }
165
0
  }
166
167
  /* Change the option. */
168
0
  if (args_has(args, 'U') && scope == OPTIONS_TABLE_WINDOW) {
169
0
    TAILQ_FOREACH(loop, &target->w->panes, entry) {
170
0
      po = options_get_only(loop->options, name);
171
0
      if (po == NULL)
172
0
        continue;
173
0
      if (options_remove_or_default(po, idx, &cause) != 0) {
174
0
        cmdq_error(item, "%s", cause);
175
0
        free(cause);
176
0
        goto fail;
177
0
      }
178
0
    }
179
0
  }
180
0
  if (args_has(args, 'u') || args_has(args, 'U')) {
181
0
    if (o == NULL)
182
0
      goto out;
183
0
    if (options_remove_or_default(o, idx, &cause) != 0) {
184
0
      cmdq_error(item, "%s", cause);
185
0
      free(cause);
186
0
      goto fail;
187
0
    }
188
0
  } else if (*name == '@') {
189
0
    if (value == NULL) {
190
0
      cmdq_error(item, "empty value");
191
0
      goto fail;
192
0
    }
193
0
    options_set_string(oo, name, append, "%s", value);
194
0
  } else if (idx == -1 && !options_is_array(parent)) {
195
0
    error = options_from_string(oo, options_table_entry(parent),
196
0
        options_table_entry(parent)->name, value,
197
0
        args_has(args, 'a'), &cause);
198
0
    if (error != 0) {
199
0
      cmdq_error(item, "%s", cause);
200
0
      free(cause);
201
0
      goto fail;
202
0
    }
203
0
  } else {
204
0
    if (value == NULL) {
205
0
      cmdq_error(item, "empty value");
206
0
      goto fail;
207
0
    }
208
0
    if (o == NULL)
209
0
      o = options_empty(oo, options_table_entry(parent));
210
0
    if (idx == -1) {
211
0
      if (!append)
212
0
        options_array_clear(o);
213
0
      if (options_array_assign(o, value, &cause) != 0) {
214
0
        cmdq_error(item, "%s", cause);
215
0
        free(cause);
216
0
        goto fail;
217
0
      }
218
0
    } else if (options_array_set(o, idx, value, append,
219
0
        &cause) != 0) {
220
0
      cmdq_error(item, "%s", cause);
221
0
      free(cause);
222
0
      goto fail;
223
0
    }
224
0
  }
225
226
0
  options_push_changes(name);
227
228
0
out:
229
0
  free(argument);
230
0
  free(expanded);
231
0
  free(name);
232
0
  return (CMD_RETURN_NORMAL);
233
234
0
fail:
235
0
  free(argument);
236
0
  free(expanded);
237
0
  free(name);
238
0
  return (CMD_RETURN_ERROR);
239
0
}