2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-01-09 19:27:00 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <glob.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2017-01-09 19:27:00 +00:00
|
|
|
#include <string.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sources a configuration file.
|
|
|
|
*/
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_source_file_exec(struct cmd *, struct cmdq_item *);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct cmd_entry cmd_source_file_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "source-file",
|
|
|
|
.alias = "source",
|
|
|
|
|
2021-08-21 10:22:38 +00:00
|
|
|
.args = { "Fnqv", 1, -1, NULL },
|
2020-09-01 09:19:01 +00:00
|
|
|
.usage = "[-Fnqv] path ...",
|
2015-12-13 21:53:57 +00:00
|
|
|
|
|
|
|
.flags = 0,
|
|
|
|
.exec = cmd_source_file_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2019-12-12 12:49:36 +00:00
|
|
|
struct cmd_source_file_data {
|
|
|
|
struct cmdq_item *item;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
struct cmdq_item *after;
|
|
|
|
enum cmd_retval retval;
|
|
|
|
|
|
|
|
u_int current;
|
|
|
|
char **files;
|
|
|
|
u_int nfiles;
|
|
|
|
};
|
|
|
|
|
|
|
|
static enum cmd_retval
|
|
|
|
cmd_source_file_complete_cb(struct cmdq_item *item, __unused void *data)
|
|
|
|
{
|
|
|
|
cfg_print_causes(item);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_source_file_complete(struct client *c, struct cmd_source_file_data *cdata)
|
|
|
|
{
|
|
|
|
struct cmdq_item *new_item;
|
2021-08-23 11:04:21 +00:00
|
|
|
u_int i;
|
2019-12-12 12:49:36 +00:00
|
|
|
|
|
|
|
if (cfg_finished) {
|
2021-05-12 06:08:58 +00:00
|
|
|
if (cdata->retval == CMD_RETURN_ERROR &&
|
|
|
|
c != NULL &&
|
|
|
|
c->session == NULL)
|
2019-12-12 12:49:36 +00:00
|
|
|
c->retval = 1;
|
|
|
|
new_item = cmdq_get_callback(cmd_source_file_complete_cb, NULL);
|
|
|
|
cmdq_insert_after(cdata->after, new_item);
|
|
|
|
}
|
|
|
|
|
2021-08-23 11:04:21 +00:00
|
|
|
for (i = 0; i < cdata->nfiles; i++)
|
|
|
|
free(cdata->files[i]);
|
2019-12-12 12:49:36 +00:00
|
|
|
free(cdata->files);
|
|
|
|
free(cdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_source_file_done(struct client *c, const char *path, int error,
|
|
|
|
int closed, struct evbuffer *buffer, void *data)
|
|
|
|
{
|
|
|
|
struct cmd_source_file_data *cdata = data;
|
|
|
|
struct cmdq_item *item = cdata->item;
|
|
|
|
void *bdata = EVBUFFER_DATA(buffer);
|
|
|
|
size_t bsize = EVBUFFER_LENGTH(buffer);
|
|
|
|
u_int n;
|
|
|
|
struct cmdq_item *new_item;
|
|
|
|
|
|
|
|
if (!closed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
cmdq_error(item, "%s: %s", path, strerror(error));
|
|
|
|
else if (bsize != 0) {
|
|
|
|
if (load_cfg_from_buffer(bdata, bsize, path, c, cdata->after,
|
|
|
|
cdata->flags, &new_item) < 0)
|
|
|
|
cdata->retval = CMD_RETURN_ERROR;
|
|
|
|
else if (new_item != NULL)
|
|
|
|
cdata->after = new_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = ++cdata->current;
|
|
|
|
if (n < cdata->nfiles)
|
|
|
|
file_read(c, cdata->files[n], cmd_source_file_done, cdata);
|
|
|
|
else {
|
|
|
|
cmd_source_file_complete(c, cdata);
|
|
|
|
cmdq_continue(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_source_file_add(struct cmd_source_file_data *cdata, const char *path)
|
|
|
|
{
|
2019-12-19 09:22:33 +00:00
|
|
|
log_debug("%s: %s", __func__, path);
|
2019-12-12 12:49:36 +00:00
|
|
|
cdata->files = xreallocarray(cdata->files, cdata->nfiles + 1,
|
|
|
|
sizeof *cdata->files);
|
|
|
|
cdata->files[cdata->nfiles++] = xstrdup(path);
|
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_source_file_exec(struct cmd *self, struct cmdq_item *item)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
2020-04-13 08:26:27 +00:00
|
|
|
struct args *args = cmd_get_args(self);
|
2019-12-12 12:49:36 +00:00
|
|
|
struct cmd_source_file_data *cdata;
|
2020-04-13 10:59:58 +00:00
|
|
|
struct client *c = cmdq_get_client(item);
|
2019-12-12 12:49:36 +00:00
|
|
|
enum cmd_retval retval = CMD_RETURN_NORMAL;
|
2021-08-20 19:50:16 +00:00
|
|
|
char *pattern, *cwd, *expanded = NULL;
|
2019-12-12 12:49:36 +00:00
|
|
|
const char *path, *error;
|
|
|
|
glob_t g;
|
2021-08-20 19:50:16 +00:00
|
|
|
int result;
|
|
|
|
u_int i, j;
|
2019-12-12 12:49:36 +00:00
|
|
|
|
|
|
|
cdata = xcalloc(1, sizeof *cdata);
|
|
|
|
cdata->item = item;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2019-05-20 13:23:32 +00:00
|
|
|
if (args_has(args, 'q'))
|
2019-12-12 12:49:36 +00:00
|
|
|
cdata->flags |= CMD_PARSE_QUIET;
|
2019-05-23 11:13:30 +00:00
|
|
|
if (args_has(args, 'n'))
|
2019-12-12 12:49:36 +00:00
|
|
|
cdata->flags |= CMD_PARSE_PARSEONLY;
|
2019-06-05 20:00:53 +00:00
|
|
|
if (args_has(args, 'v'))
|
2019-12-12 12:49:36 +00:00
|
|
|
cdata->flags |= CMD_PARSE_VERBOSE;
|
|
|
|
|
2019-05-28 11:46:30 +00:00
|
|
|
utf8_stravis(&cwd, server_client_get_cwd(c, NULL), VIS_GLOB);
|
2017-01-09 19:27:00 +00:00
|
|
|
|
2021-08-20 19:50:16 +00:00
|
|
|
for (i = 0; i < args_count(args); i++) {
|
|
|
|
path = args_string(args, i);
|
2020-09-01 09:19:01 +00:00
|
|
|
if (args_has(args, 'F')) {
|
2021-08-20 19:50:16 +00:00
|
|
|
free(expanded);
|
|
|
|
expanded = format_single_from_target(item, path);
|
|
|
|
path = expanded;
|
|
|
|
}
|
2019-12-12 12:49:36 +00:00
|
|
|
if (strcmp(path, "-") == 0) {
|
|
|
|
cmd_source_file_add(cdata, "-");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:46:30 +00:00
|
|
|
if (*path == '/')
|
|
|
|
pattern = xstrdup(path);
|
|
|
|
else
|
|
|
|
xasprintf(&pattern, "%s/%s", cwd, path);
|
|
|
|
log_debug("%s: %s", __func__, pattern);
|
|
|
|
|
2019-12-18 07:48:56 +00:00
|
|
|
if ((result = glob(pattern, 0, NULL, &g)) != 0) {
|
|
|
|
if (result != GLOB_NOMATCH ||
|
2019-12-21 17:30:48 +00:00
|
|
|
(~cdata->flags & CMD_PARSE_QUIET)) {
|
2019-12-18 07:48:56 +00:00
|
|
|
if (result == GLOB_NOMATCH)
|
|
|
|
error = strerror(ENOENT);
|
|
|
|
else if (result == GLOB_NOSPACE)
|
|
|
|
error = strerror(ENOMEM);
|
|
|
|
else
|
|
|
|
error = strerror(EINVAL);
|
2019-05-28 11:46:30 +00:00
|
|
|
cmdq_error(item, "%s: %s", path, error);
|
|
|
|
retval = CMD_RETURN_ERROR;
|
|
|
|
}
|
2021-08-23 11:04:21 +00:00
|
|
|
globfree(&g);
|
2019-05-28 11:46:30 +00:00
|
|
|
free(pattern);
|
|
|
|
continue;
|
2017-02-14 18:13:05 +00:00
|
|
|
}
|
|
|
|
free(pattern);
|
2019-05-28 11:46:30 +00:00
|
|
|
|
2019-12-12 12:49:36 +00:00
|
|
|
for (j = 0; j < g.gl_pathc; j++)
|
|
|
|
cmd_source_file_add(cdata, g.gl_pathv[j]);
|
2021-08-23 11:04:21 +00:00
|
|
|
globfree(&g);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
2021-08-22 13:48:29 +00:00
|
|
|
free(expanded);
|
2017-01-09 19:27:00 +00:00
|
|
|
|
2019-12-12 12:49:36 +00:00
|
|
|
cdata->after = item;
|
|
|
|
cdata->retval = retval;
|
|
|
|
|
|
|
|
if (cdata->nfiles != 0) {
|
|
|
|
file_read(c, cdata->files[0], cmd_source_file_done, cdata);
|
|
|
|
retval = CMD_RETURN_WAIT;
|
|
|
|
} else
|
|
|
|
cmd_source_file_complete(c, cdata);
|
|
|
|
|
2019-05-28 11:46:30 +00:00
|
|
|
free(cwd);
|
2017-01-09 19:27:00 +00:00
|
|
|
return (retval);
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|