xref: /netbsd-src/external/bsd/tmux/dist/cmd-source-file.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
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 = { "nqv", 1, -1 },
39 	.usage = "[-nqv] 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 
69 	if (cfg_finished) {
70 		if (cdata->retval == CMD_RETURN_ERROR && c->session == NULL)
71 			c->retval = 1;
72 		new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL);
73 		cmdq_insert_after(cdata->after, new_item);
74 	}
75 
76 	free(cdata->files);
77 	free(cdata);
78 }
79 
80 static void
81 cmd_source_file_done(struct client *c, const char *path, int error,
82     int closed, struct evbuffer *buffer, void *data)
83 {
84 	struct cmd_source_file_data	*cdata = data;
85 	struct cmdq_item		*item = cdata->item;
86 	void				*bdata = EVBUFFER_DATA(buffer);
87 	size_t				 bsize = EVBUFFER_LENGTH(buffer);
88 	u_int				 n;
89 	struct cmdq_item		*new_item;
90 
91 	if (!closed)
92 		return;
93 
94 	if (error != 0)
95 		cmdq_error(item, "%s: %s", path, strerror(error));
96 	else if (bsize != 0) {
97 		if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after,
98 		    cdata->flags, &new_item) < 0)
99 			cdata->retval = CMD_RETURN_ERROR;
100 		else if (new_item != NULL)
101 			cdata->after = new_item;
102 	}
103 
104 	n = ++cdata->current;
105 	if (n < cdata->nfiles)
106 		file_read(c, cdata->files[n], cmd_source_file_done, cdata);
107 	else {
108 		cmd_source_file_complete(c, cdata);
109 		cmdq_continue(item);
110 	}
111 }
112 
113 static void
114 cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
115 {
116 	log_debug("%s: %s", __func__, path);
117 	cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1,
118 	    sizeof *cdata->files);
119 	cdata->files[cdata->nfiles++] = xstrdup(path);
120 }
121 
122 static enum cmd_retval
123 cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
124 {
125 	struct args			*args = self->args;
126 	struct cmd_source_file_data	*cdata;
127 	struct client			*c = item->client;
128 	enum cmd_retval			 retval = CMD_RETURN_NORMAL;
129 	char				*pattern, *cwd;
130 	const char			*path, *error;
131 	glob_t				 g;
132 	int				 i, result;
133 	u_int				 j;
134 
135 	cdata = xcalloc(1, sizeof *cdata);
136 	cdata->item = item;
137 
138 	if (args_has(args, 'q'))
139 		cdata->flags |= CMD_PARSE_QUIET;
140 	if (args_has(args, 'n'))
141 		cdata->flags |= CMD_PARSE_PARSEONLY;
142 	if (args_has(args, 'v'))
143 		cdata->flags |= CMD_PARSE_VERBOSE;
144 
145 	utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
146 
147 	for (i = 0; i < args->argc; i++) {
148 		path = args->argv[i];
149 		if (strcmp(path, "-") == 0) {
150 			cmd_source_file_add(cdata, "-");
151 			continue;
152 		}
153 
154 		if (*path == '/')
155 			pattern = xstrdup(path);
156 		else
157 			xasprintf(&pattern, "%s/%s", cwd, path);
158 		log_debug("%s: %s", __func__, pattern);
159 
160 		if ((result = glob(pattern, 0, NULL, &g)) != 0) {
161 			if (result != GLOB_NOMATCH ||
162 			    (~cdata->flags & CMD_PARSE_QUIET)) {
163 				if (result == GLOB_NOMATCH)
164 					error = strerror(ENOENT);
165 				else if (result == GLOB_NOSPACE)
166 					error = strerror(ENOMEM);
167 				else
168 					error = strerror(EINVAL);
169 				cmdq_error(item, "%s: %s", path, error);
170 				retval = CMD_RETURN_ERROR;
171 			}
172 			free(pattern);
173 			continue;
174 		}
175 		free(pattern);
176 
177 		for (j = 0; j < g.gl_pathc; j++)
178 			cmd_source_file_add(cdata, g.gl_pathv[j]);
179 	}
180 
181 	cdata->after = item;
182 	cdata->retval = retval;
183 
184 	if (cdata->nfiles != 0) {
185 		file_read(c, cdata->files[0], cmd_source_file_done, cdata);
186 		retval = CMD_RETURN_WAIT;
187 	} else
188 		cmd_source_file_complete(c, cdata);
189 
190 	free(cwd);
191 	return (retval);
192 }
193