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. Note that
25 * documentation will be generated from the comments at the head of
45 /** @brief Return 1 if @p s is 'true' else 0 */
46 int mx_str2bool(const char *s) {
47 return !strcmp(s, "true");
50 /** @brief Return "true" if @p n is nonzero else "false" */
51 const char *mx_bool2str(int n) {
52 return n ? "true" : "false";
55 /* @include{TEMPLATE}@
57 * Includes TEMPLATE as if its text were substituted for the @include
60 static int exp_include(int attribute((unused)) nargs,
61 char attribute((unused)) **args,
62 struct sink attribute((unused)) *output,
63 void attribute((unused)) *u) {
64 assert(!"TODO implement search path");
69 * Executes COMMAND via the shell (using "sh -c") and copies its
70 * standard output to the template output. The shell command output
71 * is not expanded or modified in any other way.
73 * The shell command's standard error is copied to the error log.
75 * If the shell exits nonzero then this is reported to the error log
76 * but otherwise no special action is taken.
78 static int exp_shell(int attribute((unused)) nargs,
81 void attribute((unused)) *u) {
87 if(!(pid = xfork())) {
92 execlp("sh", "sh", "-c", args[0], (char *)0);
93 fatal(errno, "error executing sh");
96 while((n = read(p[0], buffer, sizeof buffer))) {
101 fatal(errno, "error reading from pipe");
103 if(output->write(output, buffer, n) < 0)
107 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
110 fatal(errno, "error calling waitpid");
112 error(0, "shell command '%s' %s", args[0], wstat(w));
116 /* @if{CONDITION}{IF-TRUE}{IF-FALSE}
118 * If CONDITION is "true" then evaluates to IF-TRUE. Otherwise
119 * evaluates to IF-FALSE. The IF-FALSE part is optional.
121 static int exp_if(int nargs,
122 const struct mx_node **args,
128 if((rc = mx_expandstr(args[0], &s, u)))
131 return mx_expand(args[1], output, u);
133 return mx_expand(args[2], output, u);
138 /* @and{BRANCH}{BRANCH}...
140 * Expands to "true" if all the branches are "true" otherwise to "false". If
141 * there are no brances then the result is "true". Only as many branches as
142 * necessary to compute the answer are evaluated (starting from the first one),
143 * so if later branches have side effects they may not take place.
145 static int exp_and(int nargs,
146 const struct mx_node **args,
149 int n, result = 1, rc;
152 for(n = 0; n < nargs; ++n) {
153 if((rc = mx_expandstr(args[n], &s, u)))
155 if(!mx_str2bool(s)) {
160 if(sink_writes(output, mx_bool2str(result)) < 0)
166 /* @or{BRANCH}{BRANCH}...
168 * Expands to "true" if any of the branches are "true" otherwise to "false".
169 * If there are no brances then the result is "false". Only as many branches
170 * as necessary to compute the answer are evaluated (starting from the first
171 * one), so if later branches have side effects they may not take place.
173 static int exp_or(int nargs,
174 const struct mx_node **args,
177 int n, result = 0, rc;
180 for(n = 0; n < nargs; ++n) {
181 if((rc = mx_expandstr(args[n], &s, u)))
188 if(sink_writes(output, mx_bool2str(result)) < 0)
196 * Expands to "true" unless CONDITION is "true" in which case "false".
198 static int exp_not(int attribute((unused)) nargs,
201 void attribute((unused)) *u) {
202 if(sink_writes(output, mx_bool2str(!mx_str2bool(args[0]))) < 0)
210 * Expands to nothing. The argument(s) are not fully evaluated, and no side
213 static int exp_comment(int attribute((unused)) nargs,
214 const struct mx_node attribute((unused)) **args,
215 struct sink attribute((unused)) *output,
216 void attribute((unused)) *u) {
222 * URL-quotes a string, i.e. replaces any characters not safe to use unquoted
223 * in a URL with %-encoded form.
225 static int exp_urlquote(int attribute((unused)) nargs,
228 void attribute((unused)) *u) {
229 if(sink_writes(output, urlencodestring(args[0])) < 0)
235 void mx_register_builtin(void) {
236 mx_register("include", 1, 1, exp_include);
237 mx_register("shell", 1, 1, exp_shell);
238 mx_magic_register("if", 2, 3, exp_if);
239 mx_magic_register("and", 0, INT_MAX, exp_and);
240 mx_magic_register("or", 0, INT_MAX, exp_or);
241 mx_register("not", 1, 1, exp_not);
242 mx_magic_register("#", 0, INT_MAX, exp_comment);
243 mx_register("urlquote", 1, 1, exp_urlquote);