Coverage Report

Created: 2025-08-24 07:01

/src/tmux/cfg.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2008 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 <ctype.h>
22
#include <errno.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
#include "tmux.h"
28
29
struct client    *cfg_client;
30
int       cfg_finished;
31
static char   **cfg_causes;
32
static u_int      cfg_ncauses;
33
static struct cmdq_item  *cfg_item;
34
35
int                       cfg_quiet = 1;
36
char                    **cfg_files;
37
u_int                     cfg_nfiles;
38
39
static enum cmd_retval
40
cfg_client_done(__unused struct cmdq_item *item, __unused void *data)
41
0
{
42
0
  if (!cfg_finished)
43
0
    return (CMD_RETURN_WAIT);
44
0
  return (CMD_RETURN_NORMAL);
45
0
}
46
47
static enum cmd_retval
48
cfg_done(__unused struct cmdq_item *item, __unused void *data)
49
0
{
50
0
  if (cfg_finished)
51
0
    return (CMD_RETURN_NORMAL);
52
0
  cfg_finished = 1;
53
54
0
  cfg_show_causes(NULL);
55
56
0
  if (cfg_item != NULL)
57
0
    cmdq_continue(cfg_item);
58
59
0
  status_prompt_load_history();
60
61
0
  return (CMD_RETURN_NORMAL);
62
0
}
63
64
void
65
start_cfg(void)
66
0
{
67
0
  struct client  *c;
68
0
  u_int     i;
69
0
  int     flags = 0;
70
71
  /*
72
   * Configuration files are loaded without a client, so commands are run
73
   * in the global queue with item->client NULL.
74
   *
75
   * However, we must block the initial client (but just the initial
76
   * client) so that its command runs after the configuration is loaded.
77
   * Because start_cfg() is called so early, we can be sure the client's
78
   * command queue is currently empty and our callback will be at the
79
   * front - we need to get in before MSG_COMMAND.
80
   */
81
0
  cfg_client = c = TAILQ_FIRST(&clients);
82
0
  if (c != NULL) {
83
0
    cfg_item = cmdq_get_callback(cfg_client_done, NULL);
84
0
    cmdq_append(c, cfg_item);
85
0
  }
86
87
0
  if (cfg_quiet)
88
0
    flags = CMD_PARSE_QUIET;
89
0
  for (i = 0; i < cfg_nfiles; i++)
90
0
    load_cfg(cfg_files[i], c, NULL, NULL, flags, NULL);
91
92
0
  cmdq_append(NULL, cmdq_get_callback(cfg_done, NULL));
93
0
}
94
95
int
96
load_cfg(const char *path, struct client *c, struct cmdq_item *item,
97
    struct cmd_find_state *current, int flags, struct cmdq_item **new_item)
98
0
{
99
0
  FILE      *f;
100
0
  struct cmd_parse_input   pi;
101
0
  struct cmd_parse_result *pr;
102
0
  struct cmdq_item  *new_item0;
103
0
  struct cmdq_state *state;
104
105
0
  if (new_item != NULL)
106
0
    *new_item = NULL;
107
108
0
  log_debug("loading %s", path);
109
0
  if ((f = fopen(path, "rb")) == NULL) {
110
0
    if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
111
0
      return (0);
112
0
    cfg_add_cause("%s: %s", path, strerror(errno));
113
0
    return (-1);
114
0
  }
115
116
0
  memset(&pi, 0, sizeof pi);
117
0
  pi.flags = flags;
118
0
  pi.file = path;
119
0
  pi.line = 1;
120
0
  pi.item = item;
121
0
  pi.c = c;
122
123
0
  pr = cmd_parse_from_file(f, &pi);
124
0
  fclose(f);
125
0
  if (pr->status == CMD_PARSE_ERROR) {
126
0
    cfg_add_cause("%s", pr->error);
127
0
    free(pr->error);
128
0
    return (-1);
129
0
  }
130
0
  if (flags & CMD_PARSE_PARSEONLY) {
131
0
    cmd_list_free(pr->cmdlist);
132
0
    return (0);
133
0
  }
134
135
0
  if (item != NULL)
136
0
    state = cmdq_copy_state(cmdq_get_state(item), current);
137
0
  else
138
0
    state = cmdq_new_state(NULL, NULL, 0);
139
0
  cmdq_add_format(state, "current_file", "%s", pi.file);
140
141
0
  new_item0 = cmdq_get_command(pr->cmdlist, state);
142
0
  if (item != NULL)
143
0
    new_item0 = cmdq_insert_after(item, new_item0);
144
0
  else
145
0
    new_item0 = cmdq_append(NULL, new_item0);
146
0
  cmd_list_free(pr->cmdlist);
147
0
  cmdq_free_state(state);
148
149
0
  if (new_item != NULL)
150
0
    *new_item = new_item0;
151
0
  return (0);
152
0
}
153
154
int
155
load_cfg_from_buffer(const void *buf, size_t len, const char *path,
156
    struct client *c, struct cmdq_item *item, struct cmd_find_state *current,
157
    int flags, struct cmdq_item **new_item)
158
0
{
159
0
  struct cmd_parse_input   pi;
160
0
  struct cmd_parse_result *pr;
161
0
  struct cmdq_item  *new_item0;
162
0
  struct cmdq_state *state;
163
164
0
  if (new_item != NULL)
165
0
    *new_item = NULL;
166
167
0
  log_debug("loading %s", path);
168
169
0
  memset(&pi, 0, sizeof pi);
170
0
  pi.flags = flags;
171
0
  pi.file = path;
172
0
  pi.line = 1;
173
0
  pi.item = item;
174
0
  pi.c = c;
175
176
0
  pr = cmd_parse_from_buffer(buf, len, &pi);
177
0
  if (pr->status == CMD_PARSE_ERROR) {
178
0
    cfg_add_cause("%s", pr->error);
179
0
    free(pr->error);
180
0
    return (-1);
181
0
  }
182
0
  if (flags & CMD_PARSE_PARSEONLY) {
183
0
    cmd_list_free(pr->cmdlist);
184
0
    return (0);
185
0
  }
186
187
0
  if (item != NULL)
188
0
    state = cmdq_copy_state(cmdq_get_state(item), current);
189
0
  else
190
0
    state = cmdq_new_state(NULL, NULL, 0);
191
0
  cmdq_add_format(state, "current_file", "%s", pi.file);
192
193
0
  new_item0 = cmdq_get_command(pr->cmdlist, state);
194
0
  if (item != NULL)
195
0
    new_item0 = cmdq_insert_after(item, new_item0);
196
0
  else
197
0
    new_item0 = cmdq_append(NULL, new_item0);
198
0
  cmd_list_free(pr->cmdlist);
199
0
  cmdq_free_state(state);
200
201
0
  if (new_item != NULL)
202
0
    *new_item = new_item0;
203
0
  return (0);
204
0
}
205
206
void
207
cfg_add_cause(const char *fmt, ...)
208
0
{
209
0
  va_list  ap;
210
0
  char  *msg;
211
212
0
  va_start(ap, fmt);
213
0
  xvasprintf(&msg, fmt, ap);
214
0
  va_end(ap);
215
216
0
  cfg_ncauses++;
217
0
  cfg_causes = xreallocarray(cfg_causes, cfg_ncauses, sizeof *cfg_causes);
218
0
  cfg_causes[cfg_ncauses - 1] = msg;
219
0
}
220
221
void
222
cfg_print_causes(struct cmdq_item *item)
223
0
{
224
0
  u_int  i;
225
226
0
  for (i = 0; i < cfg_ncauses; i++) {
227
0
    cmdq_print(item, "%s", cfg_causes[i]);
228
0
    free(cfg_causes[i]);
229
0
  }
230
231
0
  free(cfg_causes);
232
0
  cfg_causes = NULL;
233
0
  cfg_ncauses = 0;
234
0
}
235
236
void
237
cfg_show_causes(struct session *s)
238
0
{
239
0
  struct client     *c = TAILQ_FIRST(&clients);
240
0
  struct window_pane    *wp;
241
0
  struct window_mode_entry  *wme;
242
0
  u_int        i;
243
244
0
  if (cfg_ncauses == 0)
245
0
    return;
246
247
0
  if (c != NULL && (c->flags & CLIENT_CONTROL)) {
248
0
    for (i = 0; i < cfg_ncauses; i++) {
249
0
      control_write(c, "%%config-error %s", cfg_causes[i]);
250
0
      free(cfg_causes[i]);
251
0
    }
252
0
    goto out;
253
0
  }
254
255
0
  if (s == NULL) {
256
0
    if (c != NULL && c->session != NULL)
257
0
      s = c->session;
258
0
    else
259
0
      s = RB_MIN(sessions, &sessions);
260
0
  }
261
0
  if (s == NULL || s->attached == 0) /* wait for an attached session */
262
0
    return;
263
0
  wp = s->curw->window->active;
264
265
0
  wme = TAILQ_FIRST(&wp->modes);
266
0
  if (wme == NULL || wme->mode != &window_view_mode)
267
0
    window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
268
0
  for (i = 0; i < cfg_ncauses; i++) {
269
0
    window_copy_add(wp, 0, "%s", cfg_causes[i]);
270
0
    free(cfg_causes[i]);
271
0
  }
272
273
0
out:
274
0
  free(cfg_causes);
275
0
  cfg_causes = NULL;
276
0
  cfg_ncauses = 0;
277
0
}