Mercurial > projects > pwm
comparison pwmrc.c @ 29:00d782cb45fa
Read configuration file on startup
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Thu, 28 Sep 2017 10:06:59 +0200 |
parents | |
children | 2552eec9b913 |
comparison
equal
deleted
inserted
replaced
28:e3db02d7f1f4 | 29:00d782cb45fa |
---|---|
1 /* | |
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining | |
5 * a copy of this software and associated documentation files (the | |
6 * "Software"), to deal in the Software without restriction, including | |
7 * without limitation the rights to use, copy, modify, merge, publish, | |
8 * distribute, sublicense, and/or sell copies of the Software, and to | |
9 * permit persons to whom the Software is furnished to do so, subject to | |
10 * the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included | |
13 * in all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #include "compat.h" | |
25 | |
26 #include <errno.h> | |
27 #include <fcntl.h> | |
28 #ifdef HAVE_ERR_H | |
29 #include <err.h> | |
30 #endif /* HAVE_ERR_H */ | |
31 #include <stdio.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 #include <sys/stat.h> | |
35 #include <unistd.h> | |
36 | |
37 #include "pwm.h" | |
38 #include "cmd.h" | |
39 #include "io.h" | |
40 #include "macro.h" | |
41 #include "tok.h" | |
42 #include "util.h" | |
43 | |
44 static const char *config_cmds[] = { | |
45 "set", | |
46 "define" | |
47 }; | |
48 | |
49 int | |
50 pwmrc_read(struct pwm_ctx *ctx) | |
51 { | |
52 int retval = -1; | |
53 char *filename = NULL; | |
54 FILE *fp = NULL; | |
55 int lineno; | |
56 char buf[PWM_LINE_MAX] = { '\0' }; | |
57 size_t tokenc = 0; | |
58 union tok **tokenv = NULL; | |
59 int argc = 0; | |
60 char **argv = NULL; | |
61 struct cmd *cmd; | |
62 size_t i; | |
63 int is_config_cmd; | |
64 int j; | |
65 | |
66 xasprintf(&filename, "%s/pwmrc", ctx->dirname); | |
67 | |
68 fp = fopen(filename, "r"); | |
69 if (fp == NULL) { | |
70 if (errno == ENOENT) { | |
71 retval = 0; | |
72 } else { | |
73 warn("failed to open configuration file \"%s\"", | |
74 filename); | |
75 } | |
76 goto out; | |
77 } | |
78 | |
79 for (lineno = 1;; lineno++) { | |
80 /* read next line */ | |
81 is_config_cmd = 0; | |
82 if (fgets(buf, sizeof (buf), fp) == NULL) { | |
83 if (feof(fp)) { | |
84 goto done; | |
85 } | |
86 warn("failed to read from configuration file \"%s\"", | |
87 filename); | |
88 goto out; | |
89 } | |
90 if (strchr(buf, '\n') == NULL) { | |
91 fprintf(stderr, "line %d too long\n", lineno); | |
92 goto out; | |
93 } | |
94 | |
95 /* tokenize line */ | |
96 switch (tok_tokenize(buf, &tokenc, &tokenv)) { | |
97 case TOK_ERR_UNTERMINATED_QUOTE: | |
98 fprintf(stderr, "unterminated quote in line %d\n", | |
99 lineno); | |
100 if (!ctx->is_interactive) { | |
101 goto out; | |
102 } | |
103 goto next; | |
104 case TOK_ERR_TRAILING_BACKSLASH: | |
105 fprintf(stderr, "trailing backslash in line %d\n", | |
106 lineno); | |
107 if (!ctx->is_interactive) { | |
108 goto out; | |
109 } | |
110 goto next; | |
111 case TOK_ERR_INVALID_MACRO_NAME: | |
112 fprintf(stderr, "invalid macro name in line %d\n", | |
113 lineno); | |
114 if (!ctx->is_interactive) { | |
115 goto out; | |
116 } | |
117 goto next; | |
118 } | |
119 | |
120 /* expand macros */ | |
121 if (macro_expand_macros(ctx->macro_head, tokenc, tokenv, &argc, | |
122 &argv) != 0) { | |
123 fprintf(stderr, "undefined macro in line %d\n", lineno); | |
124 if (!ctx->is_interactive) { | |
125 goto out; | |
126 } | |
127 goto next; | |
128 } | |
129 | |
130 if (argc == 0) { | |
131 /* empty line */ | |
132 goto next; | |
133 } | |
134 | |
135 /* find and execute the command */ | |
136 cmd = cmd_match(argv[0]); | |
137 for (i = 0; (cmd != NULL) && (i < COUNTOF(config_cmds)); i++) { | |
138 if (strcmp(cmd->full_cmd, config_cmds[i]) == 0) { | |
139 is_config_cmd = 1; | |
140 break; | |
141 } | |
142 } | |
143 if (!is_config_cmd) { | |
144 fprintf(stderr, "unknown configuration command \"%s\"" | |
145 " in line %d\n", argv[0], lineno); | |
146 goto out; | |
147 } | |
148 if (cmd->cmd_func(ctx, argc, argv) != CMD_OK) { | |
149 fprintf(stderr, "syntax error in line %d\n", lineno); | |
150 goto out; | |
151 } | |
152 | |
153 next: | |
154 for (j = 0; j < argc; j++) { | |
155 free(argv[j]); | |
156 } | |
157 free(argv); | |
158 argc = 0; | |
159 argv = NULL; | |
160 tok_free(tokenv); | |
161 tokenc = 0; | |
162 tokenv = NULL; | |
163 } | |
164 | |
165 done: | |
166 retval = 0; | |
167 | |
168 out: | |
169 for (j = 0; j < argc; j++) { | |
170 free(argv[j]); | |
171 } | |
172 free(argv); | |
173 tok_free(tokenv); | |
174 if (fp != NULL) { | |
175 fclose(fp); | |
176 } | |
177 free(filename); | |
178 | |
179 return (retval); | |
180 } |