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