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.
49 static struct vector include_path
;
51 /** @brief Return 1 if @p s is 'true' else 0 */
52 int mx_str2bool(const char *s
) {
53 return !strcmp(s
, "true");
56 /** @brief Return "true" if @p n is nonzero else "false" */
57 const char *mx_bool2str(int n
) {
58 return n ?
"true" : "false";
61 /** @brief Write a boolean result */
62 int mx_bool_result(struct sink
*output
, int result
) {
63 if(sink_writes(output
, mx_bool2str(result
)) < 0)
69 /** @brief Search the include path */
70 char *mx_find(const char *name
, int report
) {
75 if(access(name
, O_RDONLY
) < 0) {
77 error(errno
, "cannot read %s", name
);
82 /* Search the include path */
83 for(n
= 0; n
< include_path
.nvec
; ++n
) {
84 byte_xasprintf(&path
, "%s/%s", include_path
.vec
[n
], name
);
85 if(access(path
, O_RDONLY
) == 0)
88 if(n
>= include_path
.nvec
) {
90 error(0, "cannot find '%s' in search path", name
);
97 /*! @include{TEMPLATE}@
101 * TEMPLATE can be an absolute filename starting with a '/'; only the file with
102 * exactly this name will be included.
104 * Alternatively it can be a relative filename, not starting with a '/'. In
105 * this case the file will be searched for in the include path. When searching
106 * paths, unreadable files are treated as if they do not exist (rather than
107 * matching then producing an error).
109 * If the name chosen ends ".tmpl" then the file will be expanded as a
110 * template. Anything else is included byte-for-byte without further
113 * Only regular files are allowed (no devices, sockets or name pipes).
115 static int exp_include(int attribute((unused
)) nargs
,
124 if(!(path
= mx_find(args
[0], 1/*report*/))) {
125 if(sink_printf(output
, "[[cannot find '%s']]", args
[0]) < 0)
129 /* If it's a template expand it */
130 if(strlen(path
) >= 5 && !strncmp(path
+ strlen(path
) - 5, ".tmpl", 5))
131 return mx_expand_file(path
, output
, u
);
132 /* Read the raw file. As with mx_expand_file() we insist that the file is a
134 if((fd
= open(path
, O_RDONLY
)) < 0)
135 fatal(errno
, "error opening %s", path
);
136 if(fstat(fd
, &sb
) < 0)
137 fatal(errno
, "error statting %s", path
);
138 if(!S_ISREG(sb
.st_mode
))
139 fatal(0, "%s: not a regular file", path
);
140 while((n
= read(fd
, buffer
, sizeof buffer
)) > 0) {
141 if(sink_write(output
, buffer
, n
) < 0) {
147 fatal(errno
, "error reading %s", path
);
152 /*! @include{COMMAND}@
154 * Executes COMMAND via the shell (using "sh -c") and copies its
155 * standard output to the template output. The shell command output
156 * is not expanded or modified in any other way.
158 * The shell command's standard error is copied to the error log.
160 * If the shell exits nonzero then this is reported to the error log
161 * but otherwise no special action is taken.
163 static int exp_shell(int attribute((unused
)) nargs
,
166 void attribute((unused
)) *u
) {
172 if(!(pid
= xfork())) {
177 execlp("sh", "sh", "-c", args
[0], (char *)0);
178 fatal(errno
, "error executing sh");
181 while((n
= read(p
[0], buffer
, sizeof buffer
))) {
186 fatal(errno
, "error reading from pipe");
188 if(output
->write(output
, buffer
, n
) < 0)
192 while((n
= waitpid(pid
, &w
, 0)) < 0 && errno
== EINTR
)
195 fatal(errno
, "error calling waitpid");
197 error(0, "shell command '%s' %s", args
[0], wstat(w
));
201 /*! @if{CONDITION}{IF-TRUE}{IF-FALSE}@
203 * If CONDITION is "true" then evaluates to IF-TRUE. Otherwise
204 * evaluates to IF-FALSE. The IF-FALSE part is optional.
206 static int exp_if(int nargs
,
207 const struct mx_node
**args
,
213 if((rc
= mx_expandstr(args
[0], &s
, u
, "argument #0 (CONDITION)")))
216 return mx_expand(args
[1], output
, u
);
218 return mx_expand(args
[2], output
, u
);
223 /*! @and{BRANCH}{BRANCH}...@
225 * Expands to "true" if all the branches are "true" otherwise to "false". If
226 * there are no brances then the result is "true". Only as many branches as
227 * necessary to compute the answer are evaluated (starting from the first one),
228 * so if later branches have side effects they may not take place.
230 static int exp_and(int nargs
,
231 const struct mx_node
**args
,
234 int n
, result
= 1, rc
;
237 for(n
= 0; n
< nargs
; ++n
) {
238 byte_xasprintf(&argname
, "argument #%d", n
);
239 if((rc
= mx_expandstr(args
[n
], &s
, u
, argname
)))
241 if(!mx_str2bool(s
)) {
246 return mx_bool_result(output
, result
);
249 /*! @or{BRANCH}{BRANCH}...@
251 * Expands to "true" if any of the branches are "true" otherwise to "false".
252 * If there are no brances then the result is "false". Only as many branches
253 * as necessary to compute the answer are evaluated (starting from the first
254 * one), so if later branches have side effects they may not take place.
256 static int exp_or(int nargs
,
257 const struct mx_node
**args
,
260 int n
, result
= 0, rc
;
263 for(n
= 0; n
< nargs
; ++n
) {
264 byte_xasprintf(&argname
, "argument #%d", n
);
265 if((rc
= mx_expandstr(args
[n
], &s
, u
, argname
)))
272 return mx_bool_result(output
, result
);
277 * Expands to "true" unless CONDITION is "true" in which case "false".
279 static int exp_not(int attribute((unused
)) nargs
,
282 void attribute((unused
)) *u
) {
283 return mx_bool_result(output
, !mx_str2bool(args
[0]));
288 * Expands to nothing. The argument(s) are not fully evaluated, and no side
291 static int exp_comment(int attribute((unused
)) nargs
,
292 const struct mx_node
attribute((unused
)) **args
,
293 struct sink
attribute((unused
)) *output
,
294 void attribute((unused
)) *u
) {
298 /*! @urlquote{STRING}@
300 * URL-quotes a string, i.e. replaces any characters not safe to use unquoted
301 * in a URL with %-encoded form.
303 static int exp_urlquote(int attribute((unused
)) nargs
,
306 void attribute((unused
)) *u
) {
307 if(sink_writes(output
, urlencodestring(args
[0])) < 0)
315 * Expands to "true" if all the arguments are identical, otherwise to "false"
316 * (i.e. if any pair of arguments differs).
318 * If there are no arguments then expands to "true". Evaluates all arguments
319 * (with their side effects) even if that's not strictly necessary to discover
322 static int exp_eq(int nargs
,
325 void attribute((unused
)) *u
) {
328 for(n
= 1; n
< nargs
; ++n
) {
329 if(strcmp(args
[n
], args
[0])) {
334 return mx_bool_result(output
, result
);
339 * Expands to "true" if all of the arguments differ from one another, otherwise
340 * to "false" (i.e. if any value appears more than once).
342 * If there are no arguments then expands to "true". Evaluates all arguments
343 * (with their side effects) even if that's not strictly necessary to discover
346 static int exp_ne(int nargs
,
349 void attribute((unused
))*u
) {
350 hash
*h
= hash_new(sizeof (char *));
353 for(n
= 0; n
< nargs
; ++n
)
354 if(hash_add(h
, args
[n
], "", HASH_INSERT
)) {
358 return mx_bool_result(output
, result
);
363 * Expands to nothing. Unlike the comment expansion @#{...}, side effects of
364 * arguments are not suppressed. So this can be used to surround a collection
365 * of macro definitions with whitespace, free text commentary, etc.
367 static int exp_discard(int attribute((unused
)) nargs
,
368 char attribute((unused
)) **args
,
369 struct sink
attribute((unused
)) *output
,
370 void attribute((unused
)) *u
) {
374 /*! @define{NAME}{ARG1 ARG2...}{DEFINITION}@
376 * Define a macro. The macro will be called NAME and will act like an
377 * expansion. When it is expanded, the expansion is replaced by DEFINITION,
378 * with each occurence of @ARG1@ etc replaced by the parameters to the
381 static int exp_define(int attribute((unused
)) nargs
,
382 const struct mx_node
**args
,
383 struct sink
attribute((unused
)) *output
,
384 void attribute((unused
)) *u
) {
385 char **as
, *name
, *argnames
;
388 if((rc
= mx_expandstr(args
[0], &name
, u
, "argument #0 (NAME)")))
390 if((rc
= mx_expandstr(args
[1], &argnames
, u
, "argument #1 (ARGS)")))
392 as
= split(argnames
, &nas
, 0, 0, 0);
393 mx_register_macro(name
, nas
, as
, args
[2]);
399 * Expands to the UNQUOTED basename of PATH.
401 static int exp_basename(int attribute((unused
)) nargs
,
403 struct sink
attribute((unused
)) *output
,
404 void attribute((unused
)) *u
) {
405 return sink_writes(output
, d_basename(args
[0])) < 0 ?
-1 : 0;
410 * Expands to the UNQUOTED directory name of PATH.
412 static int exp_dirname(int attribute((unused
)) nargs
,
414 struct sink
attribute((unused
)) *output
,
415 void attribute((unused
)) *u
) {
416 return sink_writes(output
, d_dirname(args
[0])) < 0 ?
-1 : 0;
423 static int exp_q(int attribute((unused
)) nargs
,
425 struct sink
attribute((unused
)) *output
,
426 void attribute((unused
)) *u
) {
427 return sink_writes(output
, args
[0]) < 0 ?
-1 : 0;
430 /** @brief Register built-in expansions */
431 void mx_register_builtin(void) {
432 mx_register("basename", 1, 1, exp_basename
);
433 mx_register("dirname", 1, 1, exp_dirname
);
434 mx_register("discard", 0, INT_MAX
, exp_discard
);
435 mx_register("eq", 0, INT_MAX
, exp_eq
);
436 mx_register("include", 1, 1, exp_include
);
437 mx_register("ne", 0, INT_MAX
, exp_ne
);
438 mx_register("not", 1, 1, exp_not
);
439 mx_register("shell", 1, 1, exp_shell
);
440 mx_register("urlquote", 1, 1, exp_urlquote
);
441 mx_register("q", 1, 1, exp_q
);
442 mx_register_magic("#", 0, INT_MAX
, exp_comment
);
443 mx_register_magic("and", 0, INT_MAX
, exp_and
);
444 mx_register_magic("define", 3, 3, exp_define
);
445 mx_register_magic("if", 2, 3, exp_if
);
446 mx_register_magic("or", 0, INT_MAX
, exp_or
);
449 /** @brief Add a directory to the search path
450 * @param s Directory to add
452 void mx_search_path(const char *s
) {
453 vector_append(&include_path
, xstrdup(s
));