4 * Copyright (c) 2008 Tiago Cunha <me@tiagocunha.org>
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.
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.
19 #include <sys/types.h>
24 * Sources a configuration file.
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);
33 struct cmd_source_file_data
{
37 const struct cmd_entry cmd_source_file_entry
= {
38 "source-file", "source",
42 cmd_source_file_parse
,
50 cmd_source_file_init(struct cmd
*self
, unused
int arg
)
52 struct cmd_source_file_data
*data
;
54 self
->data
= data
= xmalloc(sizeof *data
);
59 cmd_source_file_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
61 struct cmd_source_file_data
*data
;
64 self
->entry
->init(self
, KEYC_NONE
);
67 while ((opt
= getopt(argc
, argv
, "")) != -1) {
78 data
->path
= xstrdup(argv
[0]);
82 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
84 self
->entry
->free(self
);
89 cmd_source_file_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
91 struct cmd_source_file_data
*data
= self
->data
;
92 struct causelist causes
;
97 if (load_cfg(data
->path
, ctx
, &causes
) != 0) {
98 for (i
= 0; i
< ARRAY_LENGTH(&causes
); i
++) {
99 cause
= ARRAY_ITEM(&causes
, i
);
100 ctx
->print(ctx
, "%s", cause
);
110 cmd_source_file_free(struct cmd
*self
)
112 struct cmd_source_file_data
*data
= self
->data
;
114 if (data
->path
!= NULL
)
120 cmd_source_file_print(struct cmd
*self
, char *buf
, size_t len
)
122 struct cmd_source_file_data
*data
= self
->data
;
125 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
128 if (off
< len
&& data
->path
!= NULL
)
129 off
+= cmd_prarg(buf
+ off
, len
- off
, " ", data
->path
);