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.
52 static struct vector include_path;
54 /** @brief Return 1 if @p s is 'true' else 0 */
55 int mx_str2bool(const char *s) {
56 return !strcmp(s, "true");
59 /** @brief Return "true" if @p n is nonzero else "false" */
60 const char *mx_bool2str(int n) {
61 return n ? "true" : "false";
64 /** @brief Write a boolean result */
65 int mx_bool_result(struct sink *output, int result) {
66 if(sink_writes(output, mx_bool2str(result)) < 0)
72 /** @brief Search the include path */
73 char *mx_find(const char *name) {
78 if(access(name, O_RDONLY) < 0) {
79 error(errno, "cannot read %s", name);
84 /* Search the include path */
85 for(n = 0; n < include_path.nvec; ++n) {
86 byte_xasprintf(&path, "%s/%s", include_path.vec[n], name);
87 if(access(path, O_RDONLY) == 0)
90 if(n >= include_path.nvec) {
91 error(0, "cannot find '%s' in search path", name);
98 /* @include{TEMPLATE}@
102 * TEMPLATE can be an absolute filename starting with a '/'; only the file with
103 * exactly this name will be included.
105 * Alternatively it can be a relative filename, not starting with a '/'. In
106 * this case the file will be searched for in the include path. When searching
107 * paths, unreadable files are treated as if they do not exist (rather than
108 * matching then producing an error).
110 * If the name chosen ends ".tmpl" then the file will be expanded as a
111 * template. Anything else is included byte-for-byte without further
114 * Only regular files are allowed (no devices, sockets or name pipes).
116 static int exp_include(int attribute((unused)) nargs,
125 if(!(path = mx_find(args[0]))) {
126 if(sink_printf(output, "[[cannot find '%s']]", args[0]) < 0)
130 /* If it's a template expand it */
131 if(strlen(path) >= 5 && !strncmp(path + strlen(path) - 5, ".tmpl", 5))
132 return mx_expand_file(path, output, u);
133 /* Read the raw file. As with mx_expand_file() we insist that the file is a
135 if((fd = open(path, O_RDONLY)) < 0)
136 fatal(errno, "error opening %s", path);
137 if(fstat(fd, &sb) < 0)
138 fatal(errno, "error statting %s", path);
139 if(!S_ISREG(sb.st_mode))
140 fatal(0, "%s: not a regular file", path);
141 while((n = read(fd, buffer, sizeof buffer)) > 0) {
142 if(sink_write(output, buffer, n) < 0) {
148 fatal(errno, "error reading %s", path);
153 /* @include{COMMAND}@
155 * Executes COMMAND via the shell (using "sh -c") and copies its
156 * standard output to the template output. The shell command output
157 * is not expanded or modified in any other way.
159 * The shell command's standard error is copied to the error log.
161 * If the shell exits nonzero then this is reported to the error log
162 * but otherwise no special action is taken.
164 static int exp_shell(int attribute((unused)) nargs,
167 void attribute((unused)) *u) {
173 if(!(pid = xfork())) {
178 execlp("sh", "sh", "-c", args[0], (char *)0);
179 fatal(errno, "error executing sh");
182 while((n = read(p[0], buffer, sizeof buffer))) {
187 fatal(errno, "error reading from pipe");
189 if(output->write(output, buffer, n) < 0)
193 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
196 fatal(errno, "error calling waitpid");
198 error(0, "shell command '%s' %s", args[0], wstat(w));
202 /* @if{CONDITION}{IF-TRUE}{IF-FALSE}@
204 * If CONDITION is "true" then evaluates to IF-TRUE. Otherwise
205 * evaluates to IF-FALSE. The IF-FALSE part is optional.
207 static int exp_if(int nargs,
208 const struct mx_node **args,
214 if((rc = mx_expandstr(args[0], &s, u, "argument #0 (CONDITION)")))
217 return mx_expand(args[1], output, u);
219 return mx_expand(args[2], output, u);
224 /* @and{BRANCH}{BRANCH}...@
226 * Expands to "true" if all the branches are "true" otherwise to "false". If
227 * there are no brances then the result is "true". Only as many branches as
228 * necessary to compute the answer are evaluated (starting from the first one),
229 * so if later branches have side effects they may not take place.
231 static int exp_and(int nargs,
232 const struct mx_node **args,
235 int n, result = 1, rc;
238 for(n = 0; n < nargs; ++n) {
239 byte_xasprintf(&argname, "argument #%d", n);
240 if((rc = mx_expandstr(args[n], &s, u, argname)))
242 if(!mx_str2bool(s)) {
247 return mx_bool_result(output, result);
250 /* @or{BRANCH}{BRANCH}...@
252 * Expands to "true" if any of the branches are "true" otherwise to "false".
253 * If there are no brances then the result is "false". Only as many branches
254 * as necessary to compute the answer are evaluated (starting from the first
255 * one), so if later branches have side effects they may not take place.
257 static int exp_or(int nargs,
258 const struct mx_node **args,
261 int n, result = 0, rc;
264 for(n = 0; n < nargs; ++n) {
265 byte_xasprintf(&argname, "argument #%d", n);
266 if((rc = mx_expandstr(args[n], &s, u, argname)))
273 return mx_bool_result(output, result);
278 * Expands to "true" unless CONDITION is "true" in which case "false".
280 static int exp_not(int attribute((unused)) nargs,
283 void attribute((unused)) *u) {
284 return mx_bool_result(output, !mx_str2bool(args[0]));
289 * Expands to nothing. The argument(s) are not fully evaluated, and no side
292 static int exp_comment(int attribute((unused)) nargs,
293 const struct mx_node attribute((unused)) **args,
294 struct sink attribute((unused)) *output,
295 void attribute((unused)) *u) {
299 /* @urlquote{STRING}@
301 * URL-quotes a string, i.e. replaces any characters not safe to use unquoted
302 * in a URL with %-encoded form.
304 static int exp_urlquote(int attribute((unused)) nargs,
307 void attribute((unused)) *u) {
308 if(sink_writes(output, urlencodestring(args[0])) < 0)
316 * Expands to "true" if all the arguments are identical, otherwise to "false"
317 * (i.e. if any pair of arguments differs).
319 * If there are no arguments then expands to "true". Evaluates all arguments
320 * (with their side effects) even if that's not strictly necessary to discover
323 static int exp_eq(int nargs,
326 void attribute((unused)) *u) {
329 for(n = 1; n < nargs; ++n) {
330 if(strcmp(args[n], args[0])) {
335 return mx_bool_result(output, result);
340 * Expands to "true" if all of the arguments differ from one another, otherwise
341 * to "false" (i.e. if any value appears more than once).
343 * If there are no arguments then expands to "true". Evaluates all arguments
344 * (with their side effects) even if that's not strictly necessary to discover
347 static int exp_ne(int nargs,
350 void attribute((unused))*u) {
351 hash *h = hash_new(sizeof (char *));
354 for(n = 0; n < nargs; ++n)
355 if(hash_add(h, args[n], "", HASH_INSERT)) {
359 return mx_bool_result(output, result);
364 * Expands to nothing. Unlike the comment expansion @#{...}, side effects of
365 * arguments are not suppressed. So this can be used to surround a collection
366 * of macro definitions with whitespace, free text commentary, etc.
368 static int exp_discard(int attribute((unused)) nargs,
369 char attribute((unused)) **args,
370 struct sink attribute((unused)) *output,
371 void attribute((unused)) *u) {
375 /* @define{NAME}{ARG1 ARG2...}{DEFINITION}@
377 * Define a macro. The macro will be called NAME and will act like an
378 * expansion. When it is expanded, the expansion is replaced by DEFINITION,
379 * with each occurence of @ARG1@ etc replaced by the parameters to the
382 static int exp_define(int attribute((unused)) nargs,
383 const struct mx_node **args,
384 struct sink attribute((unused)) *output,
385 void attribute((unused)) *u) {
386 char **as, *name, *argnames;
389 if((rc = mx_expandstr(args[0], &name, u, "argument #0 (NAME)")))
391 if((rc = mx_expandstr(args[1], &argnames, u, "argument #1 (ARGS)")))
393 as = split(argnames, &nas, 0, 0, 0);
394 mx_register_macro(name, nas, as, args[2]);
398 /** @brief Register built-in expansions */
399 void mx_register_builtin(void) {
400 mx_register_magic("#", 0, INT_MAX, exp_comment);
401 mx_register_magic("and", 0, INT_MAX, exp_and);
402 mx_register_magic("define", 3, 3, exp_define);
403 mx_register_magic("if", 2, 3, exp_if);
404 mx_register_magic("or", 0, INT_MAX, exp_or);
405 mx_register("discard", 0, INT_MAX, exp_discard);
406 mx_register("eq", 0, INT_MAX, exp_eq);
407 mx_register("include", 1, 1, exp_include);
408 mx_register("ne", 0, INT_MAX, exp_ne);
409 mx_register("not", 1, 1, exp_not);
410 mx_register("shell", 1, 1, exp_shell);
411 mx_register("urlquote", 1, 1, exp_urlquote);
414 /** @brief Add a directory to the search path
415 * @param s Directory to add
417 void mx_search_path(const char *s) {
418 vector_append(&include_path, xstrdup(s));