xref: /openbsd-src/usr.bin/tmux/cmd-source-file.c (revision b719fcb083acabe2bd57c4a90291bffb9d1471d0)
1 /* $OpenBSD: cmd-source-file.c,v 1.10 2010/12/29 21:28:32 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 "tmux.h"
22 
23 /*
24  * Sources a configuration file.
25  */
26 
27 int	cmd_source_file_parse(struct cmd *, int, char **, char **);
28 int	cmd_source_file_exec(struct cmd *, struct cmd_ctx *);
29 void	cmd_source_file_free(struct cmd *);
30 void	cmd_source_file_init(struct cmd *, int);
31 size_t	cmd_source_file_print(struct cmd *, char *, size_t);
32 
33 struct cmd_source_file_data {
34 	char *path;
35 };
36 
37 const struct cmd_entry cmd_source_file_entry = {
38 	"source-file", "source",
39 	"path",
40 	0, "",
41 	cmd_source_file_init,
42 	cmd_source_file_parse,
43 	cmd_source_file_exec,
44 	cmd_source_file_free,
45 	cmd_source_file_print
46 };
47 
48 /* ARGSUSED */
49 void
50 cmd_source_file_init(struct cmd *self, unused int arg)
51 {
52 	struct cmd_source_file_data	*data;
53 
54 	self->data = data = xmalloc(sizeof *data);
55 	data->path = NULL;
56 }
57 
58 int
59 cmd_source_file_parse(struct cmd *self, int argc, char **argv, char **cause)
60 {
61 	struct cmd_source_file_data	*data;
62 	int				 opt;
63 
64 	self->entry->init(self, KEYC_NONE);
65 	data = self->data;
66 
67 	while ((opt = getopt(argc, argv, "")) != -1) {
68 		switch (opt) {
69 		default:
70 			goto usage;
71 		}
72 	}
73 	argc -= optind;
74 	argv += optind;
75 	if (argc != 1)
76 		goto usage;
77 
78 	data->path = xstrdup(argv[0]);
79 	return (0);
80 
81 usage:
82 	xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
83 
84 	self->entry->free(self);
85 	return (-1);
86 }
87 
88 int
89 cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx)
90 {
91 	struct cmd_source_file_data	*data = self->data;
92 	struct causelist		 causes;
93 	char				*cause;
94 	struct window_pane		*wp;
95 	int				 retval;
96 	u_int				 i;
97 
98 	ARRAY_INIT(&causes);
99 
100 	retval = load_cfg(data->path, ctx, &causes);
101 	if (ARRAY_EMPTY(&causes))
102 		return (retval);
103 
104 	if (retval == 1 && !RB_EMPTY(&sessions) && ctx->cmdclient != NULL) {
105 		wp = RB_MIN(sessions, &sessions)->curw->window->active;
106 		window_pane_set_mode(wp, &window_copy_mode);
107 		window_copy_init_for_output(wp);
108 		for (i = 0; i < ARRAY_LENGTH(&causes); i++) {
109 			cause = ARRAY_ITEM(&causes, i);
110 			window_copy_add(wp, "%s", cause);
111 			xfree(cause);
112 		}
113 	} else {
114 		for (i = 0; i < ARRAY_LENGTH(&causes); i++) {
115 			cause = ARRAY_ITEM(&causes, i);
116 			ctx->print(ctx, "%s", cause);
117 			xfree(cause);
118 		}
119 	}
120 	ARRAY_FREE(&causes);
121 
122 	return (retval);
123 }
124 
125 void
126 cmd_source_file_free(struct cmd *self)
127 {
128 	struct cmd_source_file_data	*data = self->data;
129 
130 	if (data->path != NULL)
131 		xfree(data->path);
132 	xfree(data);
133 }
134 
135 size_t
136 cmd_source_file_print(struct cmd *self, char *buf, size_t len)
137 {
138 	struct cmd_source_file_data	*data = self->data;
139 	size_t				off = 0;
140 
141 	off += xsnprintf(buf, len, "%s", self->entry->name);
142 	if (data == NULL)
143 		return (off);
144 	if (off < len && data->path != NULL)
145 		off += cmd_prarg(buf + off, len - off, " ", data->path);
146 	return (off);
147 }
148