4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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>
29 * Config file parser. Pretty quick and simple, each line is parsed into a
30 * argv array and executed as a command.
33 void printflike2
cfg_print(struct cmd_ctx
*, const char *, ...);
34 void printflike2
cfg_error(struct cmd_ctx
*, const char *, ...);
40 cfg_print(unused
struct cmd_ctx
*ctx
, unused
const char *fmt
, ...)
46 cfg_error(unused
struct cmd_ctx
*ctx
, const char *fmt
, ...)
51 xvasprintf(&cfg_cause
, fmt
, ap
);
56 load_cfg(const char *path
, struct cmd_ctx
*ctxin
, char **cause
)
60 char *buf
, *line
, *ptr
;
62 struct cmd_list
*cmdlist
;
65 if ((f
= fopen(path
, "rb")) == NULL
) {
66 xasprintf(cause
, "%s: %s", path
, strerror(errno
));
72 while ((buf
= fgetln(f
, &len
))) {
73 if (buf
[len
- 1] == '\n')
76 line
= xrealloc(line
, 1, len
+ 1);
77 memcpy(line
, buf
, len
);
83 if (cmd_string_parse(buf
, &cmdlist
, cause
) != 0) {
97 ctx
.msgdata
= ctxin
->msgdata
;
98 ctx
.curclient
= ctxin
->curclient
;
99 ctx
.cmdclient
= ctxin
->cmdclient
;
102 ctx
.error
= cfg_error
;
103 ctx
.print
= cfg_print
;
104 ctx
.info
= cfg_print
;
107 cmd_list_exec(cmdlist
, &ctx
);
108 cmd_list_free(cmdlist
);
109 if (cfg_cause
!= NULL
) {
125 xasprintf(&ptr
, "%s: %s at line %u", path
, *cause
, n
);