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)  

cfg.c
Go to the documentation of this file.
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 
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 {
42  if (!cfg_finished)
43  return (CMD_RETURN_WAIT);
44  return (CMD_RETURN_NORMAL);
45 }
46 
47 static enum cmd_retval
48 cfg_done(__unused struct cmdq_item *item, __unused void *data)
49 {
50  if (cfg_finished)
51  return (CMD_RETURN_NORMAL);
52  cfg_finished = 1;
53 
54  if (!RB_EMPTY(&sessions))
56 
57  if (cfg_item != NULL)
59 
61 
62  return (CMD_RETURN_NORMAL);
63 }
64 
65 void
66 start_cfg(void)
67 {
68  struct client *c;
69  u_int i;
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  cfg_client = c = TAILQ_FIRST(&clients);
82  if (c != NULL) {
85  }
86 
87  for (i = 0; i < cfg_nfiles; i++) {
88  if (cfg_quiet)
89  load_cfg(cfg_files[i], c, NULL, CMD_PARSE_QUIET, NULL);
90  else
91  load_cfg(cfg_files[i], c, NULL, 0, NULL);
92  }
93 
95 }
96 
97 int
98 load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags,
99  struct cmdq_item **new_item)
100 {
101  FILE *f;
102  struct cmd_parse_input pi;
103  struct cmd_parse_result *pr;
104  struct cmdq_item *new_item0;
105  struct cmdq_state *state;
106 
107  if (new_item != NULL)
108  *new_item = NULL;
109 
110  log_debug("loading %s", path);
111  if ((f = fopen(path, "rb")) == NULL) {
112  if (errno == ENOENT && (flags & CMD_PARSE_QUIET))
113  return (0);
114  cfg_add_cause("%s: %s", path, strerror(errno));
115  return (-1);
116  }
117 
118  memset(&pi, 0, sizeof pi);
119  pi.flags = flags;
120  pi.file = path;
121  pi.line = 1;
122  pi.item = item;
123  pi.c = c;
124 
125  pr = cmd_parse_from_file(f, &pi);
126  fclose(f);
127  if (pr->status == CMD_PARSE_EMPTY)
128  return (0);
129  if (pr->status == CMD_PARSE_ERROR) {
130  cfg_add_cause("%s", pr->error);
131  free(pr->error);
132  return (-1);
133  }
134  if (flags & CMD_PARSE_PARSEONLY) {
135  cmd_list_free(pr->cmdlist);
136  return (0);
137  }
138 
139  if (item != NULL)
140  state = cmdq_copy_state(cmdq_get_state(item));
141  else
142  state = cmdq_new_state(NULL, NULL, 0);
143  cmdq_add_format(state, "current_file", "%s", pi.file);
144 
145  new_item0 = cmdq_get_command(pr->cmdlist, state);
146  if (item != NULL)
147  new_item0 = cmdq_insert_after(item, new_item0);
148  else
149  new_item0 = cmdq_append(NULL, new_item0);
150  cmd_list_free(pr->cmdlist);
151  cmdq_free_state(state);
152 
153  if (new_item != NULL)
154  *new_item = new_item0;
155  return (0);
156 }
157 
158 int
159 load_cfg_from_buffer(const void *buf, size_t len, const char *path,
160  struct client *c, struct cmdq_item *item, int flags,
161  struct cmdq_item **new_item)
162 {
163  struct cmd_parse_input pi;
164  struct cmd_parse_result *pr;
165  struct cmdq_item *new_item0;
166  struct cmdq_state *state;
167 
168  if (new_item != NULL)
169  *new_item = NULL;
170 
171  log_debug("loading %s", path);
172 
173  memset(&pi, 0, sizeof pi);
174  pi.flags = flags;
175  pi.file = path;
176  pi.line = 1;
177  pi.item = item;
178  pi.c = c;
179 
180  pr = cmd_parse_from_buffer(buf, len, &pi);
181  if (pr->status == CMD_PARSE_EMPTY)
182  return (0);
183  if (pr->status == CMD_PARSE_ERROR) {
184  cfg_add_cause("%s", pr->error);
185  free(pr->error);
186  return (-1);
187  }
188  if (flags & CMD_PARSE_PARSEONLY) {
189  cmd_list_free(pr->cmdlist);
190  return (0);
191  }
192 
193  if (item != NULL)
194  state = cmdq_copy_state(cmdq_get_state(item));
195  else
196  state = cmdq_new_state(NULL, NULL, 0);
197  cmdq_add_format(state, "current_file", "%s", pi.file);
198 
199  new_item0 = cmdq_get_command(pr->cmdlist, state);
200  if (item != NULL)
201  new_item0 = cmdq_insert_after(item, new_item0);
202  else
203  new_item0 = cmdq_append(NULL, new_item0);
204  cmd_list_free(pr->cmdlist);
205  cmdq_free_state(state);
206 
207  if (new_item != NULL)
208  *new_item = new_item0;
209  return (0);
210 }
211 
212 void
213 cfg_add_cause(const char *fmt, ...)
214 {
215  va_list ap;
216  char *msg;
217 
218  va_start(ap, fmt);
219  xvasprintf(&msg, fmt, ap);
220  va_end(ap);
221 
222  cfg_ncauses++;
224  cfg_causes[cfg_ncauses - 1] = msg;
225 }
226 
227 void
229 {
230  u_int i;
231 
232  for (i = 0; i < cfg_ncauses; i++) {
233  cmdq_print(item, "%s", cfg_causes[i]);
234  free(cfg_causes[i]);
235  }
236 
237  free(cfg_causes);
238  cfg_causes = NULL;
239  cfg_ncauses = 0;
240 }
241 
242 void
244 {
245  struct window_pane *wp;
246  struct window_mode_entry *wme;
247  u_int i;
248 
249  if (s == NULL || cfg_ncauses == 0)
250  return;
251  wp = s->curw->window->active;
252 
253  wme = TAILQ_FIRST(&wp->modes);
254  if (wme == NULL || wme->mode != &window_view_mode)
255  window_pane_set_mode(wp, NULL, &window_view_mode, NULL, NULL);
256  for (i = 0; i < cfg_ncauses; i++) {
257  window_copy_add(wp, "%s", cfg_causes[i]);
258  free(cfg_causes[i]);
259  }
260 
261  free(cfg_causes);
262  cfg_causes = NULL;
263  cfg_ncauses = 0;
264 }
static struct cmdq_item * cfg_item
Definition: cfg.c:33
static char ** cfg_causes
Definition: cfg.c:31
static enum cmd_retval cfg_client_done(struct cmdq_item *item, void *data)
Definition: cfg.c:40
int load_cfg_from_buffer(const void *buf, size_t len, const char *path, struct client *c, struct cmdq_item *item, int flags, struct cmdq_item **new_item)
Definition: cfg.c:159
int cfg_quiet
Definition: cfg.c:35
static u_int cfg_ncauses
Definition: cfg.c:32
void cfg_add_cause(const char *fmt,...)
Definition: cfg.c:213
void cfg_print_causes(struct cmdq_item *item)
Definition: cfg.c:228
u_int cfg_nfiles
Definition: cfg.c:37
struct client * cfg_client
Definition: cfg.c:29
void start_cfg(void)
Definition: cfg.c:66
static enum cmd_retval cfg_done(struct cmdq_item *item, void *data)
Definition: cfg.c:48
void cfg_show_causes(struct session *s)
Definition: cfg.c:243
int cfg_finished
Definition: cfg.c:30
char ** cfg_files
Definition: cfg.c:36
int load_cfg(const char *path, struct client *c, struct cmdq_item *item, int flags, struct cmdq_item **new_item)
Definition: cfg.c:98
struct cmd_parse_result * cmd_parse_from_buffer(const void *buf, size_t len, struct cmd_parse_input *pi)
Definition: cmd-parse.c:667
struct cmd_parse_result * cmd_parse_from_file(FILE *f, struct cmd_parse_input *pi)
Definition: cmd-parse.c:573
void cmdq_print(struct cmdq_item *item, const char *fmt,...)
Definition: cmd-queue.c:792
struct cmdq_state * cmdq_get_state(struct cmdq_item *item)
Definition: cmd-queue.c:164
void cmdq_free_state(struct cmdq_state *state)
Definition: cmd-queue.c:244
struct cmdq_item * cmdq_get_command(struct cmd_list *cmdlist, struct cmdq_state *state)
Definition: cmd-queue.c:474
struct cmdq_item * cmdq_append(struct client *c, struct cmdq_item *item)
Definition: cmd-queue.c:288
void cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt,...)
Definition: cmd-queue.c:256
struct cmdq_state * cmdq_new_state(struct cmd_find_state *current, struct key_event *event, int flags)
Definition: cmd-queue.c:206
struct cmdq_item * cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
Definition: cmd-queue.c:312
struct cmdq_state * cmdq_copy_state(struct cmdq_state *state)
Definition: cmd-queue.c:237
void cmdq_continue(struct cmdq_item *item)
Definition: cmd-queue.c:434
void cmd_list_free(struct cmd_list *cmdlist)
Definition: cmd.c:606
#define __unused
Definition: compat.h:60
void log_debug(const char *msg,...)
Definition: log.c:130
struct clients clients
Definition: server.c:42
struct sessions sessions
Definition: session.c:29
void status_prompt_load_history(void)
Definition: status.c:80
Definition: tmux.h:1608
uint64_t flags
Definition: tmux.h:1703
struct cmdq_item * item
Definition: tmux.h:1504
u_int line
Definition: tmux.h:1502
struct client * c
Definition: tmux.h:1505
const char * file
Definition: tmux.h:1501
enum cmd_parse_status status
Definition: tmux.h:1489
char * error
Definition: tmux.h:1491
struct cmd_list * cmdlist
Definition: tmux.h:1490
void * data
Definition: cmd-queue.c:63
int flags
Definition: cmd-queue.c:78
Definition: tmux.h:1179
struct winlink * curw
Definition: tmux.h:1192
Definition: tmux.h:927
const struct window_mode * mode
Definition: tmux.h:931
struct window_pane * wp
Definition: tmux.h:928
struct window_pane * active
Definition: tmux.h:1054
#define cmdq_get_callback(cb, data)
Definition: tmux.h:2364
#define CMD_PARSE_QUIET
Definition: tmux.h:1495
void window_copy_add(struct window_pane *, const char *,...)
Definition: window-copy.c:503
cmd_retval
Definition: tmux.h:1475
@ CMD_RETURN_NORMAL
Definition: tmux.h:1477
@ CMD_RETURN_WAIT
Definition: tmux.h:1478
#define CMD_PARSE_PARSEONLY
Definition: tmux.h:1496
@ CMD_PARSE_ERROR
Definition: tmux.h:1485
@ CMD_PARSE_EMPTY
Definition: tmux.h:1484
int window_pane_set_mode(struct window_pane *, struct window_pane *, const struct window_mode *, struct cmd_find_state *, struct args *)
Definition: window.c:1068
const struct window_mode window_view_mode
Definition: window-copy.c:157
enum window_copy_cmd_action(* f)(struct window_copy_cmd_state *)
Definition: window-copy.c:2248
void * xreallocarray(void *ptr, size_t nmemb, size_t size)
Definition: xmalloc.c:61
int xvasprintf(char **ret, const char *fmt, va_list ap)
Definition: xmalloc.c:122