chiark / gitweb /
Source code reorganization:
[disorder] / lib / macros-builtin.c
CommitLineData
f640bcb3
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
21/** @file lib/macros-builtin.c
22 * @brief Built-in expansions
23 *
b36be3a1
RK
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.
f640bcb3
RK
26 */
27
05b75f8d 28#include "common.h"
f640bcb3 29
f640bcb3 30#include <errno.h>
f640bcb3 31#include <unistd.h>
b36be3a1
RK
32#include <fcntl.h>
33#include <sys/stat.h>
c240ff9a 34#include <sys/wait.h>
f640bcb3 35
9faa7a88 36#include "hash.h"
b36be3a1 37#include "mem.h"
f640bcb3
RK
38#include "macros.h"
39#include "sink.h"
40#include "syscalls.h"
41#include "log.h"
42#include "wstat.h"
43#include "kvp.h"
b36be3a1
RK
44#include "split.h"
45#include "printf.h"
46#include "vector.h"
2257512d 47#include "filepart.h"
b36be3a1
RK
48
49static struct vector include_path;
f640bcb3
RK
50
51/** @brief Return 1 if @p s is 'true' else 0 */
52int mx_str2bool(const char *s) {
53 return !strcmp(s, "true");
54}
55
56/** @brief Return "true" if @p n is nonzero else "false" */
57const char *mx_bool2str(int n) {
58 return n ? "true" : "false";
59}
60
b36be3a1 61/** @brief Write a boolean result */
71634563 62int mx_bool_result(struct sink *output, int result) {
b36be3a1
RK
63 if(sink_writes(output, mx_bool2str(result)) < 0)
64 return -1;
65 else
66 return 0;
67}
68
9faa7a88 69/** @brief Search the include path */
f2d306b4 70char *mx_find(const char *name, int report) {
9faa7a88
RK
71 char *path;
72 int n;
73
74 if(name[0] == '/') {
75 if(access(name, O_RDONLY) < 0) {
f2d306b4
RK
76 if(report)
77 error(errno, "cannot read %s", name);
9faa7a88
RK
78 return 0;
79 }
80 path = xstrdup(name);
81 } else {
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)
86 break;
87 }
88 if(n >= include_path.nvec) {
f2d306b4
RK
89 if(report)
90 error(0, "cannot find '%s' in search path", name);
9faa7a88
RK
91 return 0;
92 }
93 }
94 return path;
95}
96
5c1ae3bc 97/*! @include{TEMPLATE}@
b36be3a1
RK
98 *
99 * Includes TEMPLATE.
100 *
101 * TEMPLATE can be an absolute filename starting with a '/'; only the file with
102 * exactly this name will be included.
103 *
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).
f640bcb3 108 *
b36be3a1
RK
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
111 * modification.
112 *
113 * Only regular files are allowed (no devices, sockets or name pipes).
f640bcb3
RK
114 */
115static int exp_include(int attribute((unused)) nargs,
b36be3a1
RK
116 char **args,
117 struct sink *output,
118 void *u) {
9faa7a88 119 const char *path;
b36be3a1
RK
120 int fd, n;
121 char buffer[4096];
122 struct stat sb;
b36be3a1 123
f2d306b4 124 if(!(path = mx_find(args[0], 1/*report*/))) {
f5fdc06f 125 if(sink_printf(output, "[[cannot find '%s']]", args[0]) < 0)
dd0f422a 126 return 0;
9faa7a88 127 return 0;
b36be3a1
RK
128 }
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
133 * regular file. */
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) {
142 xclose(fd);
143 return -1;
144 }
145 }
146 if(n < 0)
147 fatal(errno, "error reading %s", path);
148 xclose(fd);
149 return 0;
f640bcb3
RK
150}
151
5c1ae3bc 152/*! @include{COMMAND}@
f640bcb3
RK
153 *
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.
157 *
158 * The shell command's standard error is copied to the error log.
159 *
160 * If the shell exits nonzero then this is reported to the error log
161 * but otherwise no special action is taken.
162 */
163static int exp_shell(int attribute((unused)) nargs,
164 char **args,
165 struct sink *output,
166 void attribute((unused)) *u) {
167 int w, p[2], n;
168 char buffer[4096];
169 pid_t pid;
170
171 xpipe(p);
172 if(!(pid = xfork())) {
173 exitfn = _exit;
174 xclose(p[0]);
175 xdup2(p[1], 1);
176 xclose(p[1]);
177 execlp("sh", "sh", "-c", args[0], (char *)0);
178 fatal(errno, "error executing sh");
179 }
180 xclose(p[1]);
181 while((n = read(p[0], buffer, sizeof buffer))) {
182 if(n < 0) {
183 if(errno == EINTR)
184 continue;
185 else
186 fatal(errno, "error reading from pipe");
187 }
188 if(output->write(output, buffer, n) < 0)
189 return -1;
190 }
191 xclose(p[0]);
192 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
193 ;
194 if(n < 0)
195 fatal(errno, "error calling waitpid");
196 if(w)
197 error(0, "shell command '%s' %s", args[0], wstat(w));
198 return 0;
199}
200
5c1ae3bc 201/*! @if{CONDITION}{IF-TRUE}{IF-FALSE}@
f640bcb3
RK
202 *
203 * If CONDITION is "true" then evaluates to IF-TRUE. Otherwise
204 * evaluates to IF-FALSE. The IF-FALSE part is optional.
205 */
206static int exp_if(int nargs,
207 const struct mx_node **args,
208 struct sink *output,
209 void *u) {
210 char *s;
211 int rc;
212
b36be3a1 213 if((rc = mx_expandstr(args[0], &s, u, "argument #0 (CONDITION)")))
f640bcb3
RK
214 return rc;
215 if(mx_str2bool(s))
216 return mx_expand(args[1], output, u);
217 else if(nargs > 2)
218 return mx_expand(args[2], output, u);
219 else
220 return 0;
221}
222
5c1ae3bc 223/*! @and{BRANCH}{BRANCH}...@
f640bcb3
RK
224 *
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.
229 */
230static int exp_and(int nargs,
231 const struct mx_node **args,
232 struct sink *output,
233 void *u) {
234 int n, result = 1, rc;
b36be3a1 235 char *s, *argname;
f640bcb3
RK
236
237 for(n = 0; n < nargs; ++n) {
b36be3a1
RK
238 byte_xasprintf(&argname, "argument #%d", n);
239 if((rc = mx_expandstr(args[n], &s, u, argname)))
f640bcb3
RK
240 return rc;
241 if(!mx_str2bool(s)) {
242 result = 0;
243 break;
244 }
245 }
b36be3a1 246 return mx_bool_result(output, result);
f640bcb3
RK
247}
248
5c1ae3bc 249/*! @or{BRANCH}{BRANCH}...@
f640bcb3
RK
250 *
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.
255 */
256static int exp_or(int nargs,
257 const struct mx_node **args,
258 struct sink *output,
259 void *u) {
260 int n, result = 0, rc;
b36be3a1 261 char *s, *argname;
f640bcb3
RK
262
263 for(n = 0; n < nargs; ++n) {
b36be3a1
RK
264 byte_xasprintf(&argname, "argument #%d", n);
265 if((rc = mx_expandstr(args[n], &s, u, argname)))
f640bcb3
RK
266 return rc;
267 if(mx_str2bool(s)) {
268 result = 1;
269 break;
270 }
271 }
b36be3a1 272 return mx_bool_result(output, result);
f640bcb3
RK
273}
274
5c1ae3bc 275/*! @not{CONDITION}@
f640bcb3
RK
276 *
277 * Expands to "true" unless CONDITION is "true" in which case "false".
278 */
279static int exp_not(int attribute((unused)) nargs,
280 char **args,
281 struct sink *output,
282 void attribute((unused)) *u) {
b36be3a1 283 return mx_bool_result(output, !mx_str2bool(args[0]));
f640bcb3
RK
284}
285
5c1ae3bc 286/*! @#{...}@
f640bcb3
RK
287 *
288 * Expands to nothing. The argument(s) are not fully evaluated, and no side
289 * effects occur.
290 */
291static 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) {
295 return 0;
296}
297
5c1ae3bc 298/*! @urlquote{STRING}@
f640bcb3
RK
299 *
300 * URL-quotes a string, i.e. replaces any characters not safe to use unquoted
301 * in a URL with %-encoded form.
302 */
303static int exp_urlquote(int attribute((unused)) nargs,
304 char **args,
305 struct sink *output,
306 void attribute((unused)) *u) {
307 if(sink_writes(output, urlencodestring(args[0])) < 0)
308 return -1;
309 else
310 return 0;
311}
312
5c1ae3bc 313/*! @eq{S1}{S2}...@
b36be3a1
RK
314 *
315 * Expands to "true" if all the arguments are identical, otherwise to "false"
316 * (i.e. if any pair of arguments differs).
317 *
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
320 * the result.
321 */
322static int exp_eq(int nargs,
323 char **args,
324 struct sink *output,
325 void attribute((unused)) *u) {
326 int n, result = 1;
327
328 for(n = 1; n < nargs; ++n) {
329 if(strcmp(args[n], args[0])) {
330 result = 0;
331 break;
332 }
333 }
334 return mx_bool_result(output, result);
335}
336
5c1ae3bc 337/*! @ne{S1}{S2}...@
b36be3a1
RK
338 *
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).
341 *
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
344 * the result.
345 */
346static int exp_ne(int nargs,
347 char **args,
348 struct sink *output,
349 void attribute((unused))*u) {
350 hash *h = hash_new(sizeof (char *));
351 int n, result = 1;
352
353 for(n = 0; n < nargs; ++n)
354 if(hash_add(h, args[n], "", HASH_INSERT)) {
355 result = 0;
356 break;
357 }
358 return mx_bool_result(output, result);
359}
360
5c1ae3bc 361/*! @discard{...}@
b36be3a1
RK
362 *
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.
366 */
367static int exp_discard(int attribute((unused)) nargs,
368 char attribute((unused)) **args,
369 struct sink attribute((unused)) *output,
370 void attribute((unused)) *u) {
371 return 0;
372}
373
5c1ae3bc 374/*! @define{NAME}{ARG1 ARG2...}{DEFINITION}@
b36be3a1
RK
375 *
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
379 * expansion.
380 */
381static 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;
386 int rc, nas;
387
388 if((rc = mx_expandstr(args[0], &name, u, "argument #0 (NAME)")))
389 return rc;
390 if((rc = mx_expandstr(args[1], &argnames, u, "argument #1 (ARGS)")))
391 return rc;
392 as = split(argnames, &nas, 0, 0, 0);
393 mx_register_macro(name, nas, as, args[2]);
394 return 0;
395}
396
5c1ae3bc 397/*! @basename{PATH}
2257512d
RK
398 *
399 * Expands to the UNQUOTED basename of PATH.
400 */
401static int exp_basename(int attribute((unused)) nargs,
402 char **args,
403 struct sink attribute((unused)) *output,
404 void attribute((unused)) *u) {
405 return sink_writes(output, d_basename(args[0])) < 0 ? -1 : 0;
406}
407
5c1ae3bc 408/*! @dirname{PATH}
2257512d
RK
409 *
410 * Expands to the UNQUOTED directory name of PATH.
411 */
412static int exp_dirname(int attribute((unused)) nargs,
413 char **args,
414 struct sink attribute((unused)) *output,
415 void attribute((unused)) *u) {
416 return sink_writes(output, d_dirname(args[0])) < 0 ? -1 : 0;
417}
418
5c1ae3bc 419/*! @q{STRING}
721d8bf4
RK
420 *
421 * Expands to STRING.
422 */
423static int exp_q(int attribute((unused)) nargs,
424 char **args,
425 struct sink attribute((unused)) *output,
426 void attribute((unused)) *u) {
427 return sink_writes(output, args[0]) < 0 ? -1 : 0;
428}
429
dd0f422a 430/** @brief Register built-in expansions */
f640bcb3 431void mx_register_builtin(void) {
2257512d
RK
432 mx_register("basename", 1, 1, exp_basename);
433 mx_register("dirname", 1, 1, exp_dirname);
b36be3a1
RK
434 mx_register("discard", 0, INT_MAX, exp_discard);
435 mx_register("eq", 0, INT_MAX, exp_eq);
f640bcb3 436 mx_register("include", 1, 1, exp_include);
b36be3a1 437 mx_register("ne", 0, INT_MAX, exp_ne);
f640bcb3 438 mx_register("not", 1, 1, exp_not);
b36be3a1 439 mx_register("shell", 1, 1, exp_shell);
f640bcb3 440 mx_register("urlquote", 1, 1, exp_urlquote);
721d8bf4 441 mx_register("q", 1, 1, exp_q);
2257512d
RK
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);
f640bcb3
RK
447}
448
dd0f422a
RK
449/** @brief Add a directory to the search path
450 * @param s Directory to add
451 */
452void mx_search_path(const char *s) {
453 vector_append(&include_path, xstrdup(s));
454}
455
f640bcb3
RK
456/*
457Local Variables:
458c-basic-offset:2
459comment-column:40
460fill-column:79
461indent-tabs-mode:nil
462End:
463*/