cmd-source-file.c revision 1.47
1/* $OpenBSD: cmd-source-file.c,v 1.47 2020/04/13 10:59:58 nicm Exp $ */
2
3/*
4 * Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
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 <errno.h>
22#include <glob.h>
23#include <stdlib.h>
24#include <string.h>
25#include <vis.h>
26
27#include "tmux.h"
28
29/*
30 * Sources a configuration file.
31 */
32
33static enum cmd_retval	cmd_source_file_exec(struct cmd *, struct cmdq_item *);
34
35const struct cmd_entry cmd_source_file_entry = {
36	.name = "source-file",
37	.alias = "source",
38
39	.args = { "nqv", 1, -1 },
40	.usage = "[-nqv] path ...",
41
42	.flags = 0,
43	.exec = cmd_source_file_exec
44};
45
46struct cmd_source_file_data {
47	struct cmdq_item	 *item;
48	int			  flags;
49
50	struct cmdq_item	 *after;
51	enum cmd_retval		  retval;
52
53	u_int			  current;
54	char			**files;
55	u_int			  nfiles;
56};
57
58static enum cmd_retval
59cmd_source_file_complete_cb(struct cmdq_item *item, __unused void *data)
60{
61	cfg_print_causes(item);
62	return (CMD_RETURN_NORMAL);
63}
64
65static void
66cmd_source_file_complete(struct client *c, struct cmd_source_file_data *cdata)
67{
68	struct cmdq_item	*new_item;
69
70	if (cfg_finished) {
71		if (cdata->retval == CMD_RETURN_ERROR && c->session == NULL)
72			c->retval = 1;
73		new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL);
74		cmdq_insert_after(cdata->after, new_item);
75	}
76
77	free(cdata->files);
78	free(cdata);
79}
80
81static void
82cmd_source_file_done(struct client *c, const char *path, int error,
83    int closed, struct evbuffer *buffer, void *data)
84{
85	struct cmd_source_file_data	*cdata = data;
86	struct cmdq_item		*item = cdata->item;
87	void				*bdata = EVBUFFER_DATA(buffer);
88	size_t				 bsize = EVBUFFER_LENGTH(buffer);
89	u_int				 n;
90	struct cmdq_item		*new_item;
91
92	if (!closed)
93		return;
94
95	if (error != 0)
96		cmdq_error(item, "%s: %s", path, strerror(error));
97	else if (bsize != 0) {
98		if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after,
99		    cdata->flags, &new_item) < 0)
100			cdata->retval = CMD_RETURN_ERROR;
101		else if (new_item != NULL)
102			cdata->after = new_item;
103	}
104
105	n = ++cdata->current;
106	if (n < cdata->nfiles)
107		file_read(c, cdata->files[n], cmd_source_file_done, cdata);
108	else {
109		cmd_source_file_complete(c, cdata);
110		cmdq_continue(item);
111	}
112}
113
114static void
115cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
116{
117	log_debug("%s: %s", __func__, path);
118	cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1,
119	    sizeof *cdata->files);
120	cdata->files[cdata->nfiles++] = xstrdup(path);
121}
122
123static enum cmd_retval
124cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
125{
126	struct args			*args = cmd_get_args(self);
127	struct cmd_source_file_data	*cdata;
128	struct client			*c = cmdq_get_client(item);
129	enum cmd_retval			 retval = CMD_RETURN_NORMAL;
130	char				*pattern, *cwd;
131	const char			*path, *error;
132	glob_t				 g;
133	int				 i, result;
134	u_int				 j;
135
136	cdata = xcalloc(1, sizeof *cdata);
137	cdata->item = item;
138
139	if (args_has(args, 'q'))
140		cdata->flags |= CMD_PARSE_QUIET;
141	if (args_has(args, 'n'))
142		cdata->flags |= CMD_PARSE_PARSEONLY;
143	if (args_has(args, 'v'))
144		cdata->flags |= CMD_PARSE_VERBOSE;
145
146	utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
147
148	for (i = 0; i < args->argc; i++) {
149		path = args->argv[i];
150		if (strcmp(path, "-") == 0) {
151			cmd_source_file_add(cdata, "-");
152			continue;
153		}
154
155		if (*path == '/')
156			pattern = xstrdup(path);
157		else
158			xasprintf(&pattern, "%s/%s", cwd, path);
159		log_debug("%s: %s", __func__, pattern);
160
161		if ((result = glob(pattern, 0, NULL, &g)) != 0) {
162			if (result != GLOB_NOMATCH ||
163			    (~cdata->flags & CMD_PARSE_QUIET)) {
164				if (result == GLOB_NOMATCH)
165					error = strerror(ENOENT);
166				else if (result == GLOB_NOSPACE)
167					error = strerror(ENOMEM);
168				else
169					error = strerror(EINVAL);
170				cmdq_error(item, "%s: %s", path, error);
171				retval = CMD_RETURN_ERROR;
172			}
173			free(pattern);
174			continue;
175		}
176		free(pattern);
177
178		for (j = 0; j < g.gl_pathc; j++)
179			cmd_source_file_add(cdata, g.gl_pathv[j]);
180	}
181
182	cdata->after = item;
183	cdata->retval = retval;
184
185	if (cdata->nfiles != 0) {
186		file_read(c, cdata->files[0], cmd_source_file_done, cdata);
187		retval = CMD_RETURN_WAIT;
188	} else
189		cmd_source_file_complete(c, cdata);
190
191	free(cwd);
192	return (retval);
193}
194