xref: /openbsd-src/usr.bin/tmux/cmd-source-file.c (revision 311827fb38084af6afc5eb49c90c13c87a94815b)
1 /* $OpenBSD: cmd-source-file.c,v 1.1 2009/06/01 22:58:49 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_send(struct cmd *, struct buffer *);
30 void	cmd_source_file_recv(struct cmd *, struct buffer *);
31 void	cmd_source_file_free(struct cmd *);
32 void	cmd_source_file_init(struct cmd *, int);
33 size_t	cmd_source_file_print(struct cmd *, char *, size_t);
34 
35 struct cmd_source_file_data {
36 	char *path;
37 };
38 
39 const struct cmd_entry cmd_source_file_entry = {
40 	"source-file", "source",
41 	"path",
42 	0,
43 	cmd_source_file_init,
44 	cmd_source_file_parse,
45 	cmd_source_file_exec,
46 	cmd_source_file_send,
47 	cmd_source_file_recv,
48 	cmd_source_file_free,
49 	cmd_source_file_print
50 };
51 
52 void
53 cmd_source_file_init(struct cmd *self, unused int arg)
54 {
55 	struct cmd_source_file_data	*data;
56 
57 	self->data = data = xmalloc(sizeof *data);
58 	data->path = NULL;
59 }
60 
61 int
62 cmd_source_file_parse(struct cmd *self, int argc, char **argv, char **cause)
63 {
64 	struct cmd_source_file_data	*data;
65 	int				 opt;
66 
67 	self->entry->init(self, 0);
68 	data = self->data;
69 
70 	while ((opt = getopt(argc, argv, "")) != -1) {
71 		switch (opt) {
72 		default:
73 			goto usage;
74 		}
75 	}
76 	argc -= optind;
77 	argv += optind;
78 	if (argc != 1)
79 		goto usage;
80 
81 	data->path = xstrdup(argv[0]);
82 	return (0);
83 
84 usage:
85 	xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
86 
87 	self->entry->free(self);
88 	return (-1);
89 }
90 
91 int
92 cmd_source_file_exec(struct cmd *self, struct cmd_ctx *ctx)
93 {
94 	struct cmd_source_file_data	*data = self->data;
95 	char				*cause;
96 
97 	if (load_cfg(data->path, &cause) != 0) {
98 		ctx->error(ctx, "%s", cause);
99 		xfree(cause);
100 		return (-1);
101 	}
102 
103 	return (0);
104 }
105 
106 void
107 cmd_source_file_send(struct cmd *self, struct buffer *b)
108 {
109 	struct cmd_source_file_data	*data = self->data;
110 
111 	buffer_write(b, data, sizeof *data);
112 	cmd_send_string(b, data->path);
113 }
114 
115 void
116 cmd_source_file_recv(struct cmd *self, struct buffer *b)
117 {
118 	struct cmd_source_file_data	*data;
119 
120 	self->data = data = xmalloc(sizeof *data);
121 	buffer_read(b, data, sizeof *data);
122 	data->path = cmd_recv_string(b);
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