2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 /** @file lib/macros-builtin.c
22 * @brief Built-in expansions
24 * This is a grab-bag of non-domain-specific expansions. Documentation will be
25 * generated from the comments at the head of each function.
53 static struct vector include_path;
55 /** @brief Return 1 if @p s is 'true' else 0 */
56 int mx_str2bool(const char *s) {
57 return !strcmp(s, "true");
60 /** @brief Return "true" if @p n is nonzero else "false" */
61 const char *mx_bool2str(int n) {
62 return n ? "true" : "false";
65 /** @brief Write a boolean result */
66 int mx_bool_result(struct sink *output, int result) {
67 if(sink_writes(output, mx_bool2str(result)) < 0)
73 /** @brief Search the include path */
74 char *mx_find(const char *name) {
79 if(access(name, O_RDONLY) < 0) {
80 error(errno, "cannot read %s", name);
85 /* Search the include path */
86 for(n = 0; n < include_path.nvec; ++n) {
87 byte_xasprintf(&path, "%s/%s", include_path.vec[n], name);
88 if(access(path, O_RDONLY) == 0)
91 if(n >= include_path.nvec) {
92 error(0, "cannot find '%s' in search path", name);
99 /* @include{TEMPLATE}@
103 * TEMPLATE can be an absolute filename starting with a '/'; only the file with
104 * exactly this name will be included.
106 * Alternatively it can be a relative filename, not starting with a '/'. In
107 * this case the file will be searched for in the include path. When searching
108 * paths, unreadable files are treated as if they do not exist (rather than
109 * matching then producing an error).
111 * If the name chosen ends ".tmpl" then the file will be expanded as a
112 * template. Anything else is included byte-for-byte without further
115 * Only regular files are allowed (no devices, sockets or name pipes).
117 static int exp_include(int attribute((unused)) nargs,
126 if(!(path = mx_find(args[0]))) {
127 if(sink_printf(output, "[[cannot find '%s']]", args[0]) < 0)
131 /* If it's a template expand it */
132 if(strlen(path) >= 5 && !strncmp(path + strlen(path) - 5, ".tmpl", 5))
133 return mx_expand_file(path, output, u);
134 /* Read the raw file. As with mx_expand_file() we insist that the file is a
136 if((fd = open(path, O_RDONLY)) < 0)
137 fatal(errno, "error opening %s", path);
138 if(fstat(fd, &sb) < 0)
139 fatal(errno, "error statting %s", path);
140 if(!S_ISREG(sb.st_mode))
141 fatal(0, "%s: not a regular file", path);
142 while((n = read(fd, buffer, sizeof buffer)) > 0) {
143 if(sink_write(output, buffer, n) < 0) {
149 fatal(errno, "error reading %s", path);
154 /* @include{COMMAND}@
156 * Executes COMMAND via the shell (using "sh -c") and copies its
157 * standard output to the template output. The shell command output
158 * is not expanded or modified in any other way.
160 * The shell command's standard error is copied to the error log.
162 * If the shell exits nonzero then this is reported to the error log
163 * but otherwise no special action is taken.
165 static int exp_shell(int attribute((unused)) nargs,
168 void attribute((unused)) *u) {
174 if(!(pid = xfork())) {
179 execlp("sh", "sh", "-c", args[0], (char *)0);
180 fatal(errno, "error executing sh");
183 while((n = read(p[0], buffer, sizeof buffer))) {
188 fatal(errno, "error reading from pipe");
190 if(output->write(output, buffer, n) < 0)
194 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
197 fatal(errno, "error calling waitpid");
199 error(0, "shell command '%s' %s", args[0], wstat(w));
203 /* @if{CONDITION}{IF-TRUE}{IF-FALSE}@
205 * If CONDITION is "true" then evaluates to IF-TRUE. Otherwise
206 * evaluates to IF-FALSE. The IF-FALSE part is optional.
208 static int exp_if(int nargs,
209 const struct mx_node **args,
215 if((rc = mx_expandstr(args[0], &s, u, "argument #0 (CONDITION)")))
218 return mx_expand(args[1], output, u);
220 return mx_expand(args[2], output, u);
225 /* @and{BRANCH}{BRANCH}...@
227 * Expands to "true" if all the branches are "true" otherwise to "false". If
228 * there are no brances then the result is "true". Only as many branches as
229 * necessary to compute the answer are evaluated (starting from the first one),
230 * so if later branches have side effects they may not take place.
232 static int exp_and(int nargs,
233 const struct mx_node **args,
236 int n, result = 1, rc;
239 for(n = 0; n < nargs; ++n) {
240 byte_xasprintf(&argname, "argument #%d", n);
241 if((rc = mx_expandstr(args[n], &s, u, argname)))
243 if(!mx_str2bool(s)) {
248 return mx_bool_result(output, result);
251 /* @or{BRANCH}{BRANCH}...@
253 * Expands to "true" if any of the branches are "true" otherwise to "false".
254 * If there are no brances then the result is "false". Only as many branches
255 * as necessary to compute the answer are evaluated (starting from the first
256 * one), so if later branches have side effects they may not take place.
258 static int exp_or(int nargs,
259 const struct mx_node **args,
262 int n, result = 0, rc;
265 for(n = 0; n < nargs; ++n) {
266 byte_xasprintf(&argname, "argument #%d", n);
267 if((rc = mx_expandstr(args[n], &s, u, argname)))
274 return mx_bool_result(output, result);
279 * Expands to "true" unless CONDITION is "true" in which case "false".
281 static int exp_not(int attribute((unused)) nargs,
284 void attribute((unused)) *u) {
285 return mx_bool_result(output, !mx_str2bool(args[0]));
290 * Expands to nothing. The argument(s) are not fully evaluated, and no side
293 static int exp_comment(int attribute((unused)) nargs,
294 const struct mx_node attribute((unused)) **args,
295 struct sink attribute((unused)) *output,
296 void attribute((unused)) *u) {
300 /* @urlquote{STRING}@
302 * URL-quotes a string, i.e. replaces any characters not safe to use unquoted
303 * in a URL with %-encoded form.
305 static int exp_urlquote(int attribute((unused)) nargs,
308 void attribute((unused)) *u) {
309 if(sink_writes(output, urlencodestring(args[0])) < 0)
317 * Expands to "true" if all the arguments are identical, otherwise to "false"
318 * (i.e. if any pair of arguments differs).
320 * If there are no arguments then expands to "true". Evaluates all arguments
321 * (with their side effects) even if that's not strictly necessary to discover
324 static int exp_eq(int nargs,
327 void attribute((unused)) *u) {
330 for(n = 1; n < nargs; ++n) {
331 if(strcmp(args[n], args[0])) {
336 return mx_bool_result(output, result);
341 * Expands to "true" if all of the arguments differ from one another, otherwise
342 * to "false" (i.e. if any value appears more than once).
344 * If there are no arguments then expands to "true". Evaluates all arguments
345 * (with their side effects) even if that's not strictly necessary to discover
348 static int exp_ne(int nargs,
351 void attribute((unused))*u) {
352 hash *h = hash_new(sizeof (char *));
355 for(n = 0; n < nargs; ++n)
356 if(hash_add(h, args[n], "", HASH_INSERT)) {
360 return mx_bool_result(output, result);
365 * Expands to nothing. Unlike the comment expansion @#{...}, side effects of
366 * arguments are not suppressed. So this can be used to surround a collection
367 * of macro definitions with whitespace, free text commentary, etc.
369 static int exp_discard(int attribute((unused)) nargs,
370 char attribute((unused)) **args,
371 struct sink attribute((unused)) *output,
372 void attribute((unused)) *u) {
376 /* @define{NAME}{ARG1 ARG2...}{DEFINITION}@
378 * Define a macro. The macro will be called NAME and will act like an
379 * expansion. When it is expanded, the expansion is replaced by DEFINITION,
380 * with each occurence of @ARG1@ etc replaced by the parameters to the
383 static int exp_define(int attribute((unused)) nargs,
384 const struct mx_node **args,
385 struct sink attribute((unused)) *output,
386 void attribute((unused)) *u) {
387 char **as, *name, *argnames;
390 if((rc = mx_expandstr(args[0], &name, u, "argument #0 (NAME)")))
392 if((rc = mx_expandstr(args[1], &argnames, u, "argument #1 (ARGS)")))
394 as = split(argnames, &nas, 0, 0, 0);
395 mx_register_macro(name, nas, as, args[2]);
401 * Expands to the UNQUOTED basename of PATH.
403 static int exp_basename(int attribute((unused)) nargs,
405 struct sink attribute((unused)) *output,
406 void attribute((unused)) *u) {
407 return sink_writes(output, d_basename(args[0])) < 0 ? -1 : 0;
412 * Expands to the UNQUOTED directory name of PATH.
414 static int exp_dirname(int attribute((unused)) nargs,
416 struct sink attribute((unused)) *output,
417 void attribute((unused)) *u) {
418 return sink_writes(output, d_dirname(args[0])) < 0 ? -1 : 0;
425 static int exp_q(int attribute((unused)) nargs,
427 struct sink attribute((unused)) *output,
428 void attribute((unused)) *u) {
429 return sink_writes(output, args[0]) < 0 ? -1 : 0;
432 /** @brief Register built-in expansions */
433 void mx_register_builtin(void) {
434 mx_register("basename", 1, 1, exp_basename);
435 mx_register("dirname", 1, 1, exp_dirname);
436 mx_register("discard", 0, INT_MAX, exp_discard);
437 mx_register("eq", 0, INT_MAX, exp_eq);
438 mx_register("include", 1, 1, exp_include);
439 mx_register("ne", 0, INT_MAX, exp_ne);
440 mx_register("not", 1, 1, exp_not);
441 mx_register("shell", 1, 1, exp_shell);
442 mx_register("urlquote", 1, 1, exp_urlquote);
443 mx_register("q", 1, 1, exp_q);
444 mx_register_magic("#", 0, INT_MAX, exp_comment);
445 mx_register_magic("and", 0, INT_MAX, exp_and);
446 mx_register_magic("define", 3, 3, exp_define);
447 mx_register_magic("if", 2, 3, exp_if);
448 mx_register_magic("or", 0, INT_MAX, exp_or);
451 /** @brief Add a directory to the search path
452 * @param s Directory to add
454 void mx_search_path(const char *s) {
455 vector_append(&include_path, xstrdup(s));