xref: /netbsd-src/external/bsd/tmux/dist/cmd-source-file.c (revision c23f9150cad51fdd442fa1806fac769ae26a1fdd)
15494e770Schristos /* $OpenBSD$ */
2698d5317Sjmmv 
3698d5317Sjmmv /*
4698d5317Sjmmv  * Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
5698d5317Sjmmv  *
6698d5317Sjmmv  * Permission to use, copy, modify, and distribute this software for any
7698d5317Sjmmv  * purpose with or without fee is hereby granted, provided that the above
8698d5317Sjmmv  * copyright notice and this permission notice appear in all copies.
9698d5317Sjmmv  *
10698d5317Sjmmv  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11698d5317Sjmmv  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12698d5317Sjmmv  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13698d5317Sjmmv  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14698d5317Sjmmv  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15698d5317Sjmmv  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16698d5317Sjmmv  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17698d5317Sjmmv  */
18698d5317Sjmmv 
19698d5317Sjmmv #include <sys/types.h>
20698d5317Sjmmv 
214e179ddaSchristos #include <errno.h>
224e179ddaSchristos #include <glob.h>
23928fc495Schristos #include <stdlib.h>
244e179ddaSchristos #include <string.h>
25928fc495Schristos 
26698d5317Sjmmv #include "tmux.h"
27698d5317Sjmmv 
28698d5317Sjmmv /*
29698d5317Sjmmv  * Sources a configuration file.
30698d5317Sjmmv  */
31698d5317Sjmmv 
324e179ddaSchristos static enum cmd_retval	cmd_source_file_exec(struct cmd *, struct cmdq_item *);
33928fc495Schristos 
34698d5317Sjmmv const struct cmd_entry cmd_source_file_entry = {
35ed4e6cd4Schristos 	.name = "source-file",
36ed4e6cd4Schristos 	.alias = "source",
37ed4e6cd4Schristos 
38*c23f9150Swiz 	.args = { "t:Fnqv", 1, -1, NULL },
39*c23f9150Swiz 	.usage = "[-Fnqv] " CMD_TARGET_PANE_USAGE " path ...",
40*c23f9150Swiz 
41*c23f9150Swiz 	.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL },
42ed4e6cd4Schristos 
43ed4e6cd4Schristos 	.flags = 0,
44ed4e6cd4Schristos 	.exec = cmd_source_file_exec
45698d5317Sjmmv };
46698d5317Sjmmv 
47aa83ff61Schristos struct cmd_source_file_data {
48aa83ff61Schristos 	struct cmdq_item	 *item;
49aa83ff61Schristos 	int			  flags;
50aa83ff61Schristos 
51aa83ff61Schristos 	struct cmdq_item	 *after;
52aa83ff61Schristos 	enum cmd_retval		  retval;
53aa83ff61Schristos 
54aa83ff61Schristos 	u_int			  current;
55aa83ff61Schristos 	char			**files;
56aa83ff61Schristos 	u_int			  nfiles;
57aa83ff61Schristos };
58aa83ff61Schristos 
59aa83ff61Schristos static enum cmd_retval
cmd_source_file_complete_cb(struct cmdq_item * item,__unused void * data)60aa83ff61Schristos cmd_source_file_complete_cb(struct cmdq_item *item, __unused void *data)
61aa83ff61Schristos {
62aa83ff61Schristos 	cfg_print_causes(item);
63aa83ff61Schristos 	return (CMD_RETURN_NORMAL);
64aa83ff61Schristos }
65aa83ff61Schristos 
66aa83ff61Schristos static void
cmd_source_file_complete(struct client * c,struct cmd_source_file_data * cdata)67aa83ff61Schristos cmd_source_file_complete(struct client *c, struct cmd_source_file_data *cdata)
68aa83ff61Schristos {
69aa83ff61Schristos 	struct cmdq_item	*new_item;
706db26757Swiz 	u_int			 i;
71aa83ff61Schristos 
72aa83ff61Schristos 	if (cfg_finished) {
73de7701e2Schristos 		if (cdata->retval == CMD_RETURN_ERROR &&
74de7701e2Schristos 		    c != NULL &&
75de7701e2Schristos 		    c->session == NULL)
76aa83ff61Schristos 			c->retval = 1;
77aa83ff61Schristos 		new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL);
78aa83ff61Schristos 		cmdq_insert_after(cdata->after, new_item);
79aa83ff61Schristos 	}
80aa83ff61Schristos 
816db26757Swiz 	for (i = 0; i < cdata->nfiles; i++)
826db26757Swiz 		free(cdata->files[i]);
83aa83ff61Schristos 	free(cdata->files);
84aa83ff61Schristos 	free(cdata);
85aa83ff61Schristos }
86aa83ff61Schristos 
87aa83ff61Schristos static void
cmd_source_file_done(struct client * c,const char * path,int error,int closed,struct evbuffer * buffer,void * data)88aa83ff61Schristos cmd_source_file_done(struct client *c, const char *path, int error,
89aa83ff61Schristos     int closed, struct evbuffer *buffer, void *data)
90aa83ff61Schristos {
91aa83ff61Schristos 	struct cmd_source_file_data	*cdata = data;
92aa83ff61Schristos 	struct cmdq_item		*item = cdata->item;
93aa83ff61Schristos 	void				*bdata = EVBUFFER_DATA(buffer);
94aa83ff61Schristos 	size_t				 bsize = EVBUFFER_LENGTH(buffer);
95aa83ff61Schristos 	u_int				 n;
96aa83ff61Schristos 	struct cmdq_item		*new_item;
97*c23f9150Swiz 	struct cmd_find_state		*target = cmdq_get_target(item);
98aa83ff61Schristos 
99aa83ff61Schristos 	if (!closed)
100aa83ff61Schristos 		return;
101aa83ff61Schristos 
102aa83ff61Schristos 	if (error != 0)
103aa83ff61Schristos 		cmdq_error(item, "%s: %s", path, strerror(error));
104aa83ff61Schristos 	else if (bsize != 0) {
105aa83ff61Schristos 		if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after,
106*c23f9150Swiz 		    target, cdata->flags, &new_item) < 0)
107aa83ff61Schristos 			cdata->retval = CMD_RETURN_ERROR;
108aa83ff61Schristos 		else if (new_item != NULL)
109aa83ff61Schristos 			cdata->after = new_item;
110aa83ff61Schristos 	}
111aa83ff61Schristos 
112aa83ff61Schristos 	n = ++cdata->current;
113aa83ff61Schristos 	if (n < cdata->nfiles)
114aa83ff61Schristos 		file_read(c, cdata->files[n], cmd_source_file_done, cdata);
115aa83ff61Schristos 	else {
116aa83ff61Schristos 		cmd_source_file_complete(c, cdata);
117aa83ff61Schristos 		cmdq_continue(item);
118aa83ff61Schristos 	}
119aa83ff61Schristos }
120aa83ff61Schristos 
121aa83ff61Schristos static void
cmd_source_file_add(struct cmd_source_file_data * cdata,const char * path)122aa83ff61Schristos cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
123aa83ff61Schristos {
124aa83ff61Schristos 	log_debug("%s: %s", __func__, path);
125aa83ff61Schristos 	cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1,
126aa83ff61Schristos 	    sizeof *cdata->files);
127aa83ff61Schristos 	cdata->files[cdata->nfiles++] = xstrdup(path);
128aa83ff61Schristos }
129aa83ff61Schristos 
1304e179ddaSchristos static enum cmd_retval
cmd_source_file_exec(struct cmd * self,struct cmdq_item * item)1314e179ddaSchristos cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
132698d5317Sjmmv {
1339fb66d81Schristos 	struct args			*args = cmd_get_args(self);
134aa83ff61Schristos 	struct cmd_source_file_data	*cdata;
1359fb66d81Schristos 	struct client			*c = cmdq_get_client(item);
136aa83ff61Schristos 	enum cmd_retval			 retval = CMD_RETURN_NORMAL;
1376db26757Swiz 	char				*pattern, *cwd, *expanded = NULL;
1386483eba0Schristos 	const char			*path, *error;
1394e179ddaSchristos 	glob_t				 g;
1406db26757Swiz 	int				 result;
1416db26757Swiz 	u_int				 i, j;
142928fc495Schristos 
143aa83ff61Schristos 	cdata = xcalloc(1, sizeof *cdata);
144aa83ff61Schristos 	cdata->item = item;
145aa83ff61Schristos 
1466483eba0Schristos 	if (args_has(args, 'q'))
147aa83ff61Schristos 		cdata->flags |= CMD_PARSE_QUIET;
1486483eba0Schristos 	if (args_has(args, 'n'))
149aa83ff61Schristos 		cdata->flags |= CMD_PARSE_PARSEONLY;
1506483eba0Schristos 	if (args_has(args, 'v'))
151aa83ff61Schristos 		cdata->flags |= CMD_PARSE_VERBOSE;
152aa83ff61Schristos 
1536483eba0Schristos 	utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
1544e179ddaSchristos 
1556db26757Swiz 	for (i = 0; i < args_count(args); i++) {
1566db26757Swiz 		path = args_string(args, i);
1579fb66d81Schristos 		if (args_has(args, 'F')) {
1586db26757Swiz 			free(expanded);
1596db26757Swiz 			expanded = format_single_from_target(item, path);
1606db26757Swiz 			path = expanded;
1616db26757Swiz 		}
162aa83ff61Schristos 		if (strcmp(path, "-") == 0) {
163aa83ff61Schristos 			cmd_source_file_add(cdata, "-");
164aa83ff61Schristos 			continue;
165aa83ff61Schristos 		}
166aa83ff61Schristos 
1676483eba0Schristos 		if (*path == '/')
1686483eba0Schristos 			pattern = xstrdup(path);
1696483eba0Schristos 		else
1706483eba0Schristos 			xasprintf(&pattern, "%s/%s", cwd, path);
1716483eba0Schristos 		log_debug("%s: %s", __func__, pattern);
1726483eba0Schristos 
173aa83ff61Schristos 		if ((result = glob(pattern, 0, NULL, &g)) != 0) {
174aa83ff61Schristos 			if (result != GLOB_NOMATCH ||
175aa83ff61Schristos 			    (~cdata->flags & CMD_PARSE_QUIET)) {
176aa83ff61Schristos 				if (result == GLOB_NOMATCH)
177aa83ff61Schristos 					error = strerror(ENOENT);
178aa83ff61Schristos 				else if (result == GLOB_NOSPACE)
179aa83ff61Schristos 					error = strerror(ENOMEM);
180aa83ff61Schristos 				else
181aa83ff61Schristos 					error = strerror(EINVAL);
1826483eba0Schristos 				cmdq_error(item, "%s: %s", path, error);
1834e179ddaSchristos 				retval = CMD_RETURN_ERROR;
1844e179ddaSchristos 			}
1856db26757Swiz 			globfree(&g);
1864e179ddaSchristos 			free(pattern);
1876483eba0Schristos 			continue;
1884e179ddaSchristos 		}
1894e179ddaSchristos 		free(pattern);
1904e179ddaSchristos 
191aa83ff61Schristos 		for (j = 0; j < g.gl_pathc; j++)
192aa83ff61Schristos 			cmd_source_file_add(cdata, g.gl_pathv[j]);
1936db26757Swiz 		globfree(&g);
1946483eba0Schristos 	}
1956db26757Swiz 	free(expanded);
196aa83ff61Schristos 
197aa83ff61Schristos 	cdata->after = item;
198aa83ff61Schristos 	cdata->retval = retval;
199aa83ff61Schristos 
200aa83ff61Schristos 	if (cdata->nfiles != 0) {
201aa83ff61Schristos 		file_read(c, cdata->files[0], cmd_source_file_done, cdata);
202aa83ff61Schristos 		retval = CMD_RETURN_WAIT;
203aa83ff61Schristos 	} else
204aa83ff61Schristos 		cmd_source_file_complete(c, cdata);
205928fc495Schristos 
2066483eba0Schristos 	free(cwd);
2074e179ddaSchristos 	return (retval);
208928fc495Schristos }
209