xref: /openbsd-src/usr.bin/tmux/cmd-source-file.c (revision 6396a31b28c13abcc71f05292f11b42abbafd7d3)
1 /* $OpenBSD: cmd-source-file.c,v 1.41 2019/06/05 20:00:53 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 static enum cmd_retval	cmd_source_file_exec(struct cmd *, struct cmdq_item *);
34 
35 static enum cmd_retval	cmd_source_file_done(struct cmdq_item *, void *);
36 
37 const struct cmd_entry cmd_source_file_entry = {
38 	.name = "source-file",
39 	.alias = "source",
40 
41 	.args = { "nqv", 1, -1 },
42 	.usage = "[-nqv] path ...",
43 
44 	.flags = 0,
45 	.exec = cmd_source_file_exec
46 };
47 
48 static enum cmd_retval
49 cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
50 {
51 	struct args		*args = self->args;
52 	int			 flags = 0;
53 	struct client		*c = item->client;
54 	struct cmdq_item	*new_item, *after;
55 	enum cmd_retval		 retval;
56 	char			*pattern, *cwd;
57 	const char		*path, *error;
58 	glob_t			 g;
59 	int			 i;
60 	u_int			 j;
61 
62 	if (args_has(args, 'q'))
63 		flags |= CMD_PARSE_QUIET;
64 	if (args_has(args, 'n'))
65 		flags |= CMD_PARSE_PARSEONLY;
66 	if (args_has(args, 'v'))
67 		flags |= CMD_PARSE_VERBOSE;
68 	utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
69 
70 	retval = CMD_RETURN_NORMAL;
71 	for (i = 0; i < args->argc; i++) {
72 		path = args->argv[i];
73 		if (*path == '/')
74 			pattern = xstrdup(path);
75 		else
76 			xasprintf(&pattern, "%s/%s", cwd, path);
77 		log_debug("%s: %s", __func__, pattern);
78 
79 		if (glob(pattern, 0, NULL, &g) != 0) {
80 			error = strerror(errno);
81 			if (errno != ENOENT || (~flags & CMD_PARSE_QUIET)) {
82 				cmdq_error(item, "%s: %s", path, error);
83 				retval = CMD_RETURN_ERROR;
84 			}
85 			free(pattern);
86 			continue;
87 		}
88 		free(pattern);
89 
90 		after = item;
91 		for (j = 0; j < g.gl_pathc; j++) {
92 			path = g.gl_pathv[j];
93 			if (load_cfg(path, c, after, flags, &new_item) < 0)
94 				retval = CMD_RETURN_ERROR;
95 			else if (new_item != NULL)
96 				after = new_item;
97 		}
98 		globfree(&g);
99 	}
100 	if (cfg_finished) {
101 		if (retval == CMD_RETURN_ERROR && c->session == NULL)
102 			c->retval = 1;
103 		new_item = cmdq_get_callback(cmd_source_file_done, NULL);
104 		cmdq_insert_after(item, new_item);
105 	}
106 
107 	free(cwd);
108 	return (retval);
109 }
110 
111 static enum cmd_retval
112 cmd_source_file_done(struct cmdq_item *item, __unused void *data)
113 {
114 	cfg_print_causes(item);
115 	return (CMD_RETURN_NORMAL);
116 }
117