xref: /openbsd/usr.bin/tmux/cmd-source-file.c (revision 926caf46)
1 /* $OpenBSD: cmd-source-file.c,v 1.56 2024/12/16 09:13:09 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 
33 #define CMD_SOURCE_FILE_DEPTH_LIMIT 50
34 static u_int cmd_source_file_depth;
35 
36 static enum cmd_retval	cmd_source_file_exec(struct cmd *, struct cmdq_item *);
37 
38 const struct cmd_entry cmd_source_file_entry = {
39 	.name = "source-file",
40 	.alias = "source",
41 
42 	.args = { "t:Fnqv", 1, -1, NULL },
43 	.usage = "[-Fnqv] " CMD_TARGET_PANE_USAGE " path ...",
44 
45 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
46 
47 	.flags = 0,
48 	.exec = cmd_source_file_exec
49 };
50 
51 struct cmd_source_file_data {
52 	struct cmdq_item	 *item;
53 	int			  flags;
54 
55 	struct cmdq_item	 *after;
56 	enum cmd_retval		  retval;
57 
58 	u_int			  current;
59 	char			**files;
60 	u_int			  nfiles;
61 };
62 
63 static enum cmd_retval
cmd_source_file_complete_cb(struct cmdq_item * item,__unused void * data)64 cmd_source_file_complete_cb(struct cmdq_item *item, __unused void *data)
65 {
66 	struct client	*c = cmdq_get_client(item);
67 
68 	if (c == NULL) {
69 		cmd_source_file_depth--;
70 		log_debug("%s: depth now %u", __func__, cmd_source_file_depth);
71 	} else {
72 		c->source_file_depth--;
73 		log_debug("%s: depth now %u", __func__, c->source_file_depth);
74 	}
75 
76 	cfg_print_causes(item);
77 	return (CMD_RETURN_NORMAL);
78 }
79 
80 static void
cmd_source_file_complete(struct client * c,struct cmd_source_file_data * cdata)81 cmd_source_file_complete(struct client *c, struct cmd_source_file_data *cdata)
82 {
83 	struct cmdq_item	*new_item;
84 	u_int			 i;
85 
86 	if (cfg_finished) {
87 		if (cdata->retval == CMD_RETURN_ERROR &&
88 		    c != NULL &&
89 		    c->session == NULL)
90 			c->retval = 1;
91 		new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL);
92 		cmdq_insert_after(cdata->after, new_item);
93 	}
94 
95 	for (i = 0; i < cdata->nfiles; i++)
96 		free(cdata->files[i]);
97 	free(cdata->files);
98 	free(cdata);
99 }
100 
101 static void
cmd_source_file_done(struct client * c,const char * path,int error,int closed,struct evbuffer * buffer,void * data)102 cmd_source_file_done(struct client *c, const char *path, int error,
103     int closed, struct evbuffer *buffer, void *data)
104 {
105 	struct cmd_source_file_data	*cdata = data;
106 	struct cmdq_item		*item = cdata->item;
107 	void				*bdata = EVBUFFER_DATA(buffer);
108 	size_t				 bsize = EVBUFFER_LENGTH(buffer);
109 	u_int				 n;
110 	struct cmdq_item		*new_item;
111 	struct cmd_find_state		*target = cmdq_get_target(item);
112 
113 	if (!closed)
114 		return;
115 
116 	if (error != 0)
117 		cmdq_error(item, "%s: %s", path, strerror(error));
118 	else if (bsize != 0) {
119 		if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after,
120 		    target, cdata->flags, &new_item) < 0)
121 			cdata->retval = CMD_RETURN_ERROR;
122 		else if (new_item != NULL)
123 			cdata->after = new_item;
124 	}
125 
126 	n = ++cdata->current;
127 	if (n < cdata->nfiles)
128 		file_read(c, cdata->files[n], cmd_source_file_done, cdata);
129 	else {
130 		cmd_source_file_complete(c, cdata);
131 		cmdq_continue(item);
132 	}
133 }
134 
135 static void
cmd_source_file_add(struct cmd_source_file_data * cdata,const char * path)136 cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
137 {
138 	char	resolved[PATH_MAX];
139 
140 	if (realpath(path, resolved) == NULL) {
141 		log_debug("%s: realpath(\"%s\") failed: %s", __func__,
142 			path, strerror(errno));
143 	} else
144 		path = resolved;
145 
146 	log_debug("%s: %s", __func__, path);
147 
148 	cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1,
149 	    sizeof *cdata->files);
150 	cdata->files[cdata->nfiles++] = xstrdup(path);
151 }
152 
153 static enum cmd_retval
cmd_source_file_exec(struct cmd * self,struct cmdq_item * item)154 cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
155 {
156 	struct args			*args = cmd_get_args(self);
157 	struct cmd_source_file_data	*cdata;
158 	struct client			*c = cmdq_get_client(item);
159 	enum cmd_retval			 retval = CMD_RETURN_NORMAL;
160 	char				*pattern, *cwd, *expanded = NULL;
161 	const char			*path, *error;
162 	glob_t				 g;
163 	int				 result;
164 	u_int				 i, j;
165 
166 	if (c == NULL) {
167 		if (cmd_source_file_depth >= CMD_SOURCE_FILE_DEPTH_LIMIT) {
168 			cmdq_error(item, "too many nested files");
169 			return (CMD_RETURN_ERROR);
170 		}
171 		cmd_source_file_depth++;
172 		log_debug("%s: depth now %u", __func__, cmd_source_file_depth);
173 	} else {
174 		if (c->source_file_depth >= CMD_SOURCE_FILE_DEPTH_LIMIT) {
175 			cmdq_error(item, "too many nested files");
176 			return (CMD_RETURN_ERROR);
177 		}
178 		c->source_file_depth++;
179 		log_debug("%s: depth now %u", __func__, c->source_file_depth);
180 	}
181 
182 	cdata = xcalloc(1, sizeof *cdata);
183 	cdata->item = item;
184 
185 	if (args_has(args, 'q'))
186 		cdata->flags |= CMD_PARSE_QUIET;
187 	if (args_has(args, 'n'))
188 		cdata->flags |= CMD_PARSE_PARSEONLY;
189 	if (args_has(args, 'v'))
190 		cdata->flags |= CMD_PARSE_VERBOSE;
191 
192 	utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
193 
194 	for (i = 0; i < args_count(args); i++) {
195 		path = args_string(args, i);
196 		if (args_has(args, 'F')) {
197 			free(expanded);
198 			expanded = format_single_from_target(item, path);
199 			path = expanded;
200 		}
201 		if (strcmp(path, "-") == 0) {
202 			cmd_source_file_add(cdata, "-");
203 			continue;
204 		}
205 
206 		if (*path == '/')
207 			pattern = xstrdup(path);
208 		else
209 			xasprintf(&pattern, "%s/%s", cwd, path);
210 		log_debug("%s: %s", __func__, pattern);
211 
212 		if ((result = glob(pattern, 0, NULL, &g)) != 0) {
213 			if (result != GLOB_NOMATCH ||
214 			    (~cdata->flags & CMD_PARSE_QUIET)) {
215 				if (result == GLOB_NOMATCH)
216 					error = strerror(ENOENT);
217 				else if (result == GLOB_NOSPACE)
218 					error = strerror(ENOMEM);
219 				else
220 					error = strerror(EINVAL);
221 				cmdq_error(item, "%s: %s", path, error);
222 				retval = CMD_RETURN_ERROR;
223 			}
224 			globfree(&g);
225 			free(pattern);
226 			continue;
227 		}
228 		free(pattern);
229 
230 		for (j = 0; j < g.gl_pathc; j++)
231 			cmd_source_file_add(cdata, g.gl_pathv[j]);
232 		globfree(&g);
233 	}
234 	free(expanded);
235 
236 	cdata->after = item;
237 	cdata->retval = retval;
238 
239 	if (cdata->nfiles != 0) {
240 		file_read(c, cdata->files[0], cmd_source_file_done, cdata);
241 		retval = CMD_RETURN_WAIT;
242 	} else
243 		cmd_source_file_complete(c, cdata);
244 
245 	free(cwd);
246 	return (retval);
247 }
248