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