Commit | Line | Data |
---|---|---|
f640bcb3 RK |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2008 Richard Kettlewell | |
4 | * | |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
f640bcb3 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
f640bcb3 | 8 | * (at your option) any later version. |
e7eb3a27 RK |
9 | * |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
f640bcb3 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
f640bcb3 RK |
17 | */ |
18 | ||
19 | /** @file lib/macros-builtin.c | |
20 | * @brief Built-in expansions | |
21 | * | |
b36be3a1 RK |
22 | * This is a grab-bag of non-domain-specific expansions. Documentation will be |
23 | * generated from the comments at the head of each function. | |
f640bcb3 RK |
24 | */ |
25 | ||
05b75f8d | 26 | #include "common.h" |
f640bcb3 | 27 | |
f640bcb3 | 28 | #include <errno.h> |
f640bcb3 | 29 | #include <unistd.h> |
b36be3a1 RK |
30 | #include <fcntl.h> |
31 | #include <sys/stat.h> | |
f640bcb3 | 32 | |
9faa7a88 | 33 | #include "hash.h" |
b36be3a1 | 34 | #include "mem.h" |
f640bcb3 RK |
35 | #include "macros.h" |
36 | #include "sink.h" | |
37 | #include "syscalls.h" | |
38 | #include "log.h" | |
39 | #include "wstat.h" | |
40 | #include "kvp.h" | |
b36be3a1 RK |
41 | #include "split.h" |
42 | #include "printf.h" | |
43 | #include "vector.h" | |
2257512d | 44 | #include "filepart.h" |
b36be3a1 RK |
45 | |
46 | static struct vector include_path; | |
f640bcb3 RK |
47 | |
48 | /** @brief Return 1 if @p s is 'true' else 0 */ | |
49 | int mx_str2bool(const char *s) { | |
50 | return !strcmp(s, "true"); | |
51 | } | |
52 | ||
53 | /** @brief Return "true" if @p n is nonzero else "false" */ | |
54 | const char *mx_bool2str(int n) { | |
55 | return n ? "true" : "false"; | |
56 | } | |
57 | ||
b36be3a1 | 58 | /** @brief Write a boolean result */ |
71634563 | 59 | int mx_bool_result(struct sink *output, int result) { |
b36be3a1 RK |
60 | if(sink_writes(output, mx_bool2str(result)) < 0) |
61 | return -1; | |
62 | else | |
63 | return 0; | |
64 | } | |
65 | ||
9faa7a88 | 66 | /** @brief Search the include path */ |
f2d306b4 | 67 | char *mx_find(const char *name, int report) { |
9faa7a88 RK |
68 | char *path; |
69 | int n; | |
70 | ||
71 | if(name[0] == '/') { | |
72 | if(access(name, O_RDONLY) < 0) { | |
f2d306b4 RK |
73 | if(report) |
74 | error(errno, "cannot read %s", name); | |
9faa7a88 RK |
75 | return 0; |
76 | } | |
77 | path = xstrdup(name); | |
78 | } else { | |
79 | /* Search the include path */ | |
80 | for(n = 0; n < include_path.nvec; ++n) { | |
81 | byte_xasprintf(&path, "%s/%s", include_path.vec[n], name); | |
82 | if(access(path, O_RDONLY) == 0) | |
83 | break; | |
84 | } | |
85 | if(n >= include_path.nvec) { | |
f2d306b4 RK |
86 | if(report) |
87 | error(0, "cannot find '%s' in search path", name); | |
9faa7a88 RK |
88 | return 0; |
89 | } | |
90 | } | |
91 | return path; | |
92 | } | |
93 | ||
5c1ae3bc | 94 | /*! @include{TEMPLATE}@ |
b36be3a1 RK |
95 | * |
96 | * Includes TEMPLATE. | |
97 | * | |
98 | * TEMPLATE can be an absolute filename starting with a '/'; only the file with | |
99 | * exactly this name will be included. | |
100 | * | |
101 | * Alternatively it can be a relative filename, not starting with a '/'. In | |
102 | * this case the file will be searched for in the include path. When searching | |
103 | * paths, unreadable files are treated as if they do not exist (rather than | |
104 | * matching then producing an error). | |
f640bcb3 | 105 | * |
b36be3a1 RK |
106 | * If the name chosen ends ".tmpl" then the file will be expanded as a |
107 | * template. Anything else is included byte-for-byte without further | |
108 | * modification. | |
109 | * | |
110 | * Only regular files are allowed (no devices, sockets or name pipes). | |
f640bcb3 RK |
111 | */ |
112 | static int exp_include(int attribute((unused)) nargs, | |
b36be3a1 RK |
113 | char **args, |
114 | struct sink *output, | |
115 | void *u) { | |
9faa7a88 | 116 | const char *path; |
b36be3a1 RK |
117 | int fd, n; |
118 | char buffer[4096]; | |
119 | struct stat sb; | |
b36be3a1 | 120 | |
f2d306b4 | 121 | if(!(path = mx_find(args[0], 1/*report*/))) { |
f5fdc06f | 122 | if(sink_printf(output, "[[cannot find '%s']]", args[0]) < 0) |
dd0f422a | 123 | return 0; |
9faa7a88 | 124 | return 0; |
b36be3a1 RK |
125 | } |
126 | /* If it's a template expand it */ | |
127 | if(strlen(path) >= 5 && !strncmp(path + strlen(path) - 5, ".tmpl", 5)) | |
128 | return mx_expand_file(path, output, u); | |
129 | /* Read the raw file. As with mx_expand_file() we insist that the file is a | |
130 | * regular file. */ | |
131 | if((fd = open(path, O_RDONLY)) < 0) | |
132 | fatal(errno, "error opening %s", path); | |
133 | if(fstat(fd, &sb) < 0) | |
134 | fatal(errno, "error statting %s", path); | |
135 | if(!S_ISREG(sb.st_mode)) | |
136 | fatal(0, "%s: not a regular file", path); | |
137 | while((n = read(fd, buffer, sizeof buffer)) > 0) { | |
138 | if(sink_write(output, buffer, n) < 0) { | |
139 | xclose(fd); | |
140 | return -1; | |
141 | } | |
142 | } | |
143 | if(n < 0) | |
144 | fatal(errno, "error reading %s", path); | |
145 | xclose(fd); | |
146 | return 0; | |
f640bcb3 RK |
147 | } |
148 | ||
5c1ae3bc | 149 | /*! @include{COMMAND}@ |
f640bcb3 RK |
150 | * |
151 | * Executes COMMAND via the shell (using "sh -c") and copies its | |
152 | * standard output to the template output. The shell command output | |
153 | * is not expanded or modified in any other way. | |
154 | * | |
155 | * The shell command's standard error is copied to the error log. | |
156 | * | |
157 | * If the shell exits nonzero then this is reported to the error log | |
158 | * but otherwise no special action is taken. | |
159 | */ | |
160 | static int exp_shell(int attribute((unused)) nargs, | |
161 | char **args, | |
162 | struct sink *output, | |
163 | void attribute((unused)) *u) { | |
164 | int w, p[2], n; | |
165 | char buffer[4096]; | |
166 | pid_t pid; | |
167 | ||
168 | xpipe(p); | |
169 | if(!(pid = xfork())) { | |
170 | exitfn = _exit; | |
171 | xclose(p[0]); | |
172 | xdup2(p[1], 1); | |
173 | xclose(p[1]); | |
174 | execlp("sh", "sh", "-c", args[0], (char *)0); | |
175 | fatal(errno, "error executing sh"); | |
176 | } | |
177 | xclose(p[1]); | |
178 | while((n = read(p[0], buffer, sizeof buffer))) { | |
179 | if(n < 0) { | |
180 | if(errno == EINTR) | |
181 | continue; | |
182 | else | |
183 | fatal(errno, "error reading from pipe"); | |
184 | } | |
185 | if(output->write(output, buffer, n) < 0) | |
186 | return -1; | |
187 | } | |
188 | xclose(p[0]); | |
189 | while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR) | |
190 | ; | |
191 | if(n < 0) | |
192 | fatal(errno, "error calling waitpid"); | |
193 | if(w) | |
194 | error(0, "shell command '%s' %s", args[0], wstat(w)); | |
195 | return 0; | |
196 | } | |
197 | ||
5c1ae3bc | 198 | /*! @if{CONDITION}{IF-TRUE}{IF-FALSE}@ |
f640bcb3 RK |
199 | * |
200 | * If CONDITION is "true" then evaluates to IF-TRUE. Otherwise | |
201 | * evaluates to IF-FALSE. The IF-FALSE part is optional. | |
202 | */ | |
203 | static int exp_if(int nargs, | |
204 | const struct mx_node **args, | |
205 | struct sink *output, | |
206 | void *u) { | |
207 | char *s; | |
208 | int rc; | |
209 | ||
b36be3a1 | 210 | if((rc = mx_expandstr(args[0], &s, u, "argument #0 (CONDITION)"))) |
f640bcb3 RK |
211 | return rc; |
212 | if(mx_str2bool(s)) | |
213 | return mx_expand(args[1], output, u); | |
214 | else if(nargs > 2) | |
215 | return mx_expand(args[2], output, u); | |
216 | else | |
217 | return 0; | |
218 | } | |
219 | ||
5c1ae3bc | 220 | /*! @and{BRANCH}{BRANCH}...@ |
f640bcb3 RK |
221 | * |
222 | * Expands to "true" if all the branches are "true" otherwise to "false". If | |
223 | * there are no brances then the result is "true". Only as many branches as | |
224 | * necessary to compute the answer are evaluated (starting from the first one), | |
225 | * so if later branches have side effects they may not take place. | |
226 | */ | |
227 | static int exp_and(int nargs, | |
228 | const struct mx_node **args, | |
229 | struct sink *output, | |
230 | void *u) { | |
231 | int n, result = 1, rc; | |
b36be3a1 | 232 | char *s, *argname; |
f640bcb3 RK |
233 | |
234 | for(n = 0; n < nargs; ++n) { | |
b36be3a1 RK |
235 | byte_xasprintf(&argname, "argument #%d", n); |
236 | if((rc = mx_expandstr(args[n], &s, u, argname))) | |
f640bcb3 RK |
237 | return rc; |
238 | if(!mx_str2bool(s)) { | |
239 | result = 0; | |
240 | break; | |
241 | } | |
242 | } | |
b36be3a1 | 243 | return mx_bool_result(output, result); |
f640bcb3 RK |
244 | } |
245 | ||
5c1ae3bc | 246 | /*! @or{BRANCH}{BRANCH}...@ |
f640bcb3 RK |
247 | * |
248 | * Expands to "true" if any of the branches are "true" otherwise to "false". | |
249 | * If there are no brances then the result is "false". Only as many branches | |
250 | * as necessary to compute the answer are evaluated (starting from the first | |
251 | * one), so if later branches have side effects they may not take place. | |
252 | */ | |
253 | static int exp_or(int nargs, | |
254 | const struct mx_node **args, | |
255 | struct sink *output, | |
256 | void *u) { | |
257 | int n, result = 0, rc; | |
b36be3a1 | 258 | char *s, *argname; |
f640bcb3 RK |
259 | |
260 | for(n = 0; n < nargs; ++n) { | |
b36be3a1 RK |
261 | byte_xasprintf(&argname, "argument #%d", n); |
262 | if((rc = mx_expandstr(args[n], &s, u, argname))) | |
f640bcb3 RK |
263 | return rc; |
264 | if(mx_str2bool(s)) { | |
265 | result = 1; | |
266 | break; | |
267 | } | |
268 | } | |
b36be3a1 | 269 | return mx_bool_result(output, result); |
f640bcb3 RK |
270 | } |
271 | ||
5c1ae3bc | 272 | /*! @not{CONDITION}@ |
f640bcb3 RK |
273 | * |
274 | * Expands to "true" unless CONDITION is "true" in which case "false". | |
275 | */ | |
276 | static int exp_not(int attribute((unused)) nargs, | |
277 | char **args, | |
278 | struct sink *output, | |
279 | void attribute((unused)) *u) { | |
b36be3a1 | 280 | return mx_bool_result(output, !mx_str2bool(args[0])); |
f640bcb3 RK |
281 | } |
282 | ||
5c1ae3bc | 283 | /*! @#{...}@ |
f640bcb3 RK |
284 | * |
285 | * Expands to nothing. The argument(s) are not fully evaluated, and no side | |
286 | * effects occur. | |
287 | */ | |
288 | static int exp_comment(int attribute((unused)) nargs, | |
289 | const struct mx_node attribute((unused)) **args, | |
290 | struct sink attribute((unused)) *output, | |
291 | void attribute((unused)) *u) { | |
292 | return 0; | |
293 | } | |
294 | ||
5c1ae3bc | 295 | /*! @urlquote{STRING}@ |
f640bcb3 RK |
296 | * |
297 | * URL-quotes a string, i.e. replaces any characters not safe to use unquoted | |
298 | * in a URL with %-encoded form. | |
299 | */ | |
300 | static int exp_urlquote(int attribute((unused)) nargs, | |
301 | char **args, | |
302 | struct sink *output, | |
303 | void attribute((unused)) *u) { | |
304 | if(sink_writes(output, urlencodestring(args[0])) < 0) | |
305 | return -1; | |
306 | else | |
307 | return 0; | |
308 | } | |
309 | ||
5c1ae3bc | 310 | /*! @eq{S1}{S2}...@ |
b36be3a1 RK |
311 | * |
312 | * Expands to "true" if all the arguments are identical, otherwise to "false" | |
313 | * (i.e. if any pair of arguments differs). | |
314 | * | |
315 | * If there are no arguments then expands to "true". Evaluates all arguments | |
316 | * (with their side effects) even if that's not strictly necessary to discover | |
317 | * the result. | |
318 | */ | |
319 | static int exp_eq(int nargs, | |
320 | char **args, | |
321 | struct sink *output, | |
322 | void attribute((unused)) *u) { | |
323 | int n, result = 1; | |
324 | ||
325 | for(n = 1; n < nargs; ++n) { | |
326 | if(strcmp(args[n], args[0])) { | |
327 | result = 0; | |
328 | break; | |
329 | } | |
330 | } | |
331 | return mx_bool_result(output, result); | |
332 | } | |
333 | ||
5c1ae3bc | 334 | /*! @ne{S1}{S2}...@ |
b36be3a1 RK |
335 | * |
336 | * Expands to "true" if all of the arguments differ from one another, otherwise | |
337 | * to "false" (i.e. if any value appears more than once). | |
338 | * | |
339 | * If there are no arguments then expands to "true". Evaluates all arguments | |
340 | * (with their side effects) even if that's not strictly necessary to discover | |
341 | * the result. | |
342 | */ | |
343 | static int exp_ne(int nargs, | |
344 | char **args, | |
345 | struct sink *output, | |
346 | void attribute((unused))*u) { | |
347 | hash *h = hash_new(sizeof (char *)); | |
348 | int n, result = 1; | |
349 | ||
350 | for(n = 0; n < nargs; ++n) | |
351 | if(hash_add(h, args[n], "", HASH_INSERT)) { | |
352 | result = 0; | |
353 | break; | |
354 | } | |
355 | return mx_bool_result(output, result); | |
356 | } | |
357 | ||
5c1ae3bc | 358 | /*! @discard{...}@ |
b36be3a1 RK |
359 | * |
360 | * Expands to nothing. Unlike the comment expansion @#{...}, side effects of | |
361 | * arguments are not suppressed. So this can be used to surround a collection | |
362 | * of macro definitions with whitespace, free text commentary, etc. | |
363 | */ | |
364 | static int exp_discard(int attribute((unused)) nargs, | |
365 | char attribute((unused)) **args, | |
366 | struct sink attribute((unused)) *output, | |
367 | void attribute((unused)) *u) { | |
368 | return 0; | |
369 | } | |
370 | ||
5c1ae3bc | 371 | /*! @define{NAME}{ARG1 ARG2...}{DEFINITION}@ |
b36be3a1 RK |
372 | * |
373 | * Define a macro. The macro will be called NAME and will act like an | |
374 | * expansion. When it is expanded, the expansion is replaced by DEFINITION, | |
375 | * with each occurence of @ARG1@ etc replaced by the parameters to the | |
376 | * expansion. | |
377 | */ | |
378 | static int exp_define(int attribute((unused)) nargs, | |
379 | const struct mx_node **args, | |
380 | struct sink attribute((unused)) *output, | |
381 | void attribute((unused)) *u) { | |
382 | char **as, *name, *argnames; | |
383 | int rc, nas; | |
384 | ||
385 | if((rc = mx_expandstr(args[0], &name, u, "argument #0 (NAME)"))) | |
386 | return rc; | |
387 | if((rc = mx_expandstr(args[1], &argnames, u, "argument #1 (ARGS)"))) | |
388 | return rc; | |
389 | as = split(argnames, &nas, 0, 0, 0); | |
390 | mx_register_macro(name, nas, as, args[2]); | |
391 | return 0; | |
392 | } | |
393 | ||
5c1ae3bc | 394 | /*! @basename{PATH} |
2257512d RK |
395 | * |
396 | * Expands to the UNQUOTED basename of PATH. | |
397 | */ | |
398 | static int exp_basename(int attribute((unused)) nargs, | |
399 | char **args, | |
400 | struct sink attribute((unused)) *output, | |
401 | void attribute((unused)) *u) { | |
402 | return sink_writes(output, d_basename(args[0])) < 0 ? -1 : 0; | |
403 | } | |
404 | ||
5c1ae3bc | 405 | /*! @dirname{PATH} |
2257512d RK |
406 | * |
407 | * Expands to the UNQUOTED directory name of PATH. | |
408 | */ | |
409 | static int exp_dirname(int attribute((unused)) nargs, | |
410 | char **args, | |
411 | struct sink attribute((unused)) *output, | |
412 | void attribute((unused)) *u) { | |
413 | return sink_writes(output, d_dirname(args[0])) < 0 ? -1 : 0; | |
414 | } | |
415 | ||
5c1ae3bc | 416 | /*! @q{STRING} |
721d8bf4 RK |
417 | * |
418 | * Expands to STRING. | |
419 | */ | |
420 | static int exp_q(int attribute((unused)) nargs, | |
421 | char **args, | |
422 | struct sink attribute((unused)) *output, | |
423 | void attribute((unused)) *u) { | |
424 | return sink_writes(output, args[0]) < 0 ? -1 : 0; | |
425 | } | |
426 | ||
dd0f422a | 427 | /** @brief Register built-in expansions */ |
f640bcb3 | 428 | void mx_register_builtin(void) { |
2257512d RK |
429 | mx_register("basename", 1, 1, exp_basename); |
430 | mx_register("dirname", 1, 1, exp_dirname); | |
b36be3a1 RK |
431 | mx_register("discard", 0, INT_MAX, exp_discard); |
432 | mx_register("eq", 0, INT_MAX, exp_eq); | |
f640bcb3 | 433 | mx_register("include", 1, 1, exp_include); |
b36be3a1 | 434 | mx_register("ne", 0, INT_MAX, exp_ne); |
f640bcb3 | 435 | mx_register("not", 1, 1, exp_not); |
b36be3a1 | 436 | mx_register("shell", 1, 1, exp_shell); |
f640bcb3 | 437 | mx_register("urlquote", 1, 1, exp_urlquote); |
721d8bf4 | 438 | mx_register("q", 1, 1, exp_q); |
2257512d RK |
439 | mx_register_magic("#", 0, INT_MAX, exp_comment); |
440 | mx_register_magic("and", 0, INT_MAX, exp_and); | |
441 | mx_register_magic("define", 3, 3, exp_define); | |
442 | mx_register_magic("if", 2, 3, exp_if); | |
443 | mx_register_magic("or", 0, INT_MAX, exp_or); | |
f640bcb3 RK |
444 | } |
445 | ||
dd0f422a RK |
446 | /** @brief Add a directory to the search path |
447 | * @param s Directory to add | |
448 | */ | |
449 | void mx_search_path(const char *s) { | |
450 | vector_append(&include_path, xstrdup(s)); | |
451 | } | |
452 | ||
f640bcb3 RK |
453 | /* |
454 | Local Variables: | |
455 | c-basic-offset:2 | |
456 | comment-column:40 | |
457 | fill-column:79 | |
458 | indent-tabs-mode:nil | |
459 | End: | |
460 | */ |