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