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.c
22 * @brief Macro expansion
46 VECTOR_TYPE(mx_node_vector
, const struct mx_node
*, xrealloc
);
48 /** @brief Definition of an expansion */
50 /** @brief Minimum permitted arguments */
53 /** @brief Maximum permitted arguments */
62 * - @ref EXP_TYPE_MASK
66 /** @brief Macro argument names */
69 /** @brief Callback (cast to appropriate type)
71 * Cast to @ref mx_simple_callback or @ref mx_magic_callback as required. */
74 /** @brief Macro definition
76 * Only for @ref EXP_MACRO expansions. */
77 const struct mx_node
*definition
;
80 /** @brief Expansion takes pre-expanded strings
82 * @p callback is cast to @ref mx_simple_callback. */
83 #define EXP_SIMPLE 0x0000
85 /** @brief Expansion takes parsed templates, not strings
87 * @p callback is cast to @ref mx_magic_callback. The callback must do its own
88 * expansion e.g. via mx_expandstr() where necessary. */
89 #define EXP_MAGIC 0x0001
91 /** @brief Expansion is a macro */
92 #define EXP_MACRO 0x0002
94 /** @brief Mask of types */
95 #define EXP_TYPE_MASK 0x0003
97 /** @brief Hash of all expansions
99 * Created by mx_register(), mx_register_macro() or mx_register_magic().
101 static hash
*expansions
;
103 static int mx__expand_macro(const struct expansion
*e
,
104 const struct mx_node
*m
,
108 /* Parsing ------------------------------------------------------------------ */
110 static int next_non_whitespace(const char *input
,
112 while(input
< end
&& isspace((unsigned char)*input
))
114 return input
< end ?
*input
: -1;
117 /** @brief Parse a template
118 * @param filename Input filename (for diagnostics)
119 * @param line Line number (use 1 on initial call)
120 * @param input Start of text to parse
121 * @param end End of text to parse or NULL
122 * @return Pointer to parse tree root node
124 * Parses the text in [start, end) and returns an (immutable) parse
125 * tree representing it.
127 * If @p end is NULL then the whole string is parsed.
129 * Note that the @p filename value stored in the parse tree is @p filename,
130 * i.e. it is not copied.
132 const struct mx_node
*mx_parse(const char *filename
,
136 int braces
, argument_start_line
, obracket
, cbracket
;
137 const char *argument_start
, *argument_end
;
138 struct mx_node_vector v
[1];
140 struct mx_node
*head
= 0, **tailp
= &head
, *e
;
143 end
= input
+ strlen(input
);
146 e
= xmalloc(sizeof *e
);
148 e
->filename
= filename
;
152 /* Gather up text without any expansions in. */
153 while(input
< end
&& *input
!= '@') {
156 dynstr_append(d
, *input
++);
167 /* '@@' expands to '@' */
168 e
= xmalloc(sizeof *e
);
170 e
->filename
= filename
;
179 /* '@#' starts a (newline-eating comment), like dnl */
181 while(input
< end
&& *input
!= '\n')
189 /* '@_' expands to nothing. It's there to allow dump to terminate
190 * expansions without having to know what follows. */
194 /* It's a full expansion */
196 e
= xmalloc(sizeof *e
);
198 e
->filename
= filename
;
200 e
->type
= MX_EXPANSION
;
201 /* Collect the expansion name. Expansion names start with an alnum and
202 * consist of alnums and '-'. We don't permit whitespace between the '@'
206 fatal(0, "%s:%d: invalid expansion syntax (truncated)",
208 if(!isalnum((unsigned char)*input
))
209 fatal(0, "%s:%d: invalid expansion syntax (unexpected %#x)",
210 filename
, e
->line
, (unsigned char)*input
);
211 while(input
< end
&& (isalnum((unsigned char)*input
) || *input
== '-'))
212 dynstr_append(d
, *input
++);
215 /* See what the bracket character is */
216 obracket
= next_non_whitespace(input
, end
);
218 case '(': cbracket
= ')'; break;
219 case '[': cbracket
= ']'; break;
220 case '{': cbracket
= '}'; break;
221 default: cbracket
= obracket
= -1; break; /* no arguments */
223 mx_node_vector_init(v
);
225 /* Gather up arguments */
226 while(next_non_whitespace(input
, end
) == obracket
) {
227 while(isspace((unsigned char)*input
)) {
232 ++input
; /* the bracket */
234 /* Find the end of the argument */
235 argument_start
= input
;
236 argument_start_line
= line
;
237 while(input
< end
&& (*input
!= cbracket
|| braces
> 0)) {
238 const int c
= *input
++;
242 else if(c
== cbracket
)
248 /* We ran out of input without encountering a balanced cbracket */
249 fatal(0, "%s:%d: unterminated expansion argument '%.*s'",
250 filename
, argument_start_line
,
251 (int)(input
- argument_start
), argument_start
);
253 /* Consistency check */
254 assert(*input
== cbracket
);
255 /* Record the end of the argument */
256 argument_end
= input
;
257 /* Step over the cbracket */
259 /* Now we have an argument in [argument_start, argument_end), and we
260 * know its filename and initial line number. This is sufficient to
262 mx_node_vector_append(v
, mx_parse(filename
, argument_start_line
,
263 argument_start
, argument_end
));
266 /* Guarantee a NULL terminator (for the case where there's more than one
268 mx_node_vector_terminate(v
);
269 /* Fill in the remains of the node */
278 static void mx__dump(struct dynstr
*d
, const struct mx_node
*m
) {
280 const struct mx_node
*mm
;
286 if(m
->text
[0] == '@')
287 dynstr_append(d
, '@');
288 dynstr_append_string(d
, m
->text
);
291 dynstr_append(d
, '@');
292 dynstr_append_string(d
, m
->name
);
293 for(n
= 0; n
< m
->nargs
; ++n
) {
294 dynstr_append(d
, '{');
295 mx__dump(d
, m
->args
[n
]);
296 dynstr_append(d
, '}');
298 /* If the next non-whitespace is '{', add @_ to stop it being
301 while(mm
&& mm
->type
== MX_TEXT
) {
302 switch(next_non_whitespace(mm
->text
, mm
->text
+ strlen(mm
->text
))) {
307 dynstr_append_string(d
, "@_");
316 assert(!"invalid m->type");
318 mx__dump(d
, m
->next
);
321 /** @brief Dump a parse macro expansion to a string
323 * Not of production quality! Only intended for testing!
325 char *mx_dump(const struct mx_node
*m
) {
334 /* Expansion registration --------------------------------------------------- */
336 static int mx__register(unsigned flags
,
342 const struct mx_node
*definition
) {
343 struct expansion e
[1];
346 expansions
= hash_new(sizeof(struct expansion
));
351 e
->callback
= callback
;
352 e
->definition
= definition
;
353 return hash_add(expansions
, name
, &e
,
354 ((flags
& EXP_TYPE_MASK
) == EXP_MACRO
)
355 ? HASH_INSERT
: HASH_INSERT_OR_REPLACE
);
358 /** @brief Register a simple expansion rule
360 * @param min Minimum number of arguments
361 * @param max Maximum number of arguments
362 * @param callback Callback to write output
364 void mx_register(const char *name
,
367 mx_simple_callback
*callback
) {
368 mx__register(EXP_SIMPLE
, name
, min
, max
, 0, (void (*)())callback
, 0);
371 /** @brief Register a magic expansion rule
373 * @param min Minimum number of arguments
374 * @param max Maximum number of arguments
375 * @param callback Callback to write output
377 void mx_register_magic(const char *name
,
380 mx_magic_callback
*callback
) {
381 mx__register(EXP_MAGIC
, name
, min
, max
, 0, (void (*)())callback
, 0);
384 /** @brief Register a macro
386 * @param nargs Number of arguments
387 * @param args Argument names
388 * @param definition Macro definition
389 * @return 0 on success, negative on error
391 int mx_register_macro(const char *name
,
394 const struct mx_node
*definition
) {
395 if(mx__register(EXP_MACRO
, name
, nargs
, nargs
, args
, 0/*callback*/,
397 /* This locates the error to the definition, which may be a line or two
398 * beyond the @define command itself. The backtrace generated by
399 * mx_expand() may help more. */
400 error(0, "%s:%d: duplicate definition of '%s'",
401 definition
->filename
, definition
->line
, name
);
407 /* Expansion ---------------------------------------------------------------- */
409 /** @brief Expand a template
410 * @param m Where to start
411 * @param output Where to send output
413 * @return 0 on success, non-0 on error
415 * Interpretation of return values:
417 * - -1 means an error writing to the sink.
418 * - other negative values mean errors generated from with the macro
420 * - positive values are reserved for the application
422 * If any callback returns non-zero then that value is returned, abandoning
425 int mx_expand(const struct mx_node
*m
,
428 const struct expansion
*e
;
435 if(sink_writes(output
, m
->text
) < 0)
440 if(!(e
= hash_find(expansions
, m
->name
))) {
441 error(0, "%s:%d: unknown expansion name '%s'",
442 m
->filename
, m
->line
, m
->name
);
443 if(sink_printf(output
, "[['%s' unknown]]", m
->name
) < 0)
445 } else if(m
->nargs
< e
->min
) {
446 error(0, "%s:%d: expansion '%s' requires %d args, only %d given",
447 m
->filename
, m
->line
, m
->name
, e
->min
, m
->nargs
);
448 if(sink_printf(output
, "[['%s' too few args]]", m
->name
) < 0)
450 } else if(m
->nargs
> e
->max
) {
451 error(0, "%s:%d: expansion '%s' takes at most %d args, but %d given",
452 m
->filename
, m
->line
, m
->name
, e
->max
, m
->nargs
);
453 if(sink_printf(output
, "[['%s' too many args]]", m
->name
) < 0)
455 } else switch(e
->flags
& EXP_TYPE_MASK
) {
457 /* Magic callbacks we can call directly */
458 rc
= ((mx_magic_callback
*)e
->callback
)(m
->nargs
,
465 /* For simple callbacks we expand their arguments for them. */
466 char **args
= xcalloc(1 + m
->nargs
, sizeof (char *)), *argname
;
469 for(n
= 0; n
< m
->nargs
; ++n
) {
470 /* Argument numbers are at least clear from looking at the text;
471 * adding names as well would be nice. TODO */
472 byte_xasprintf(&argname
, "argument #%d", n
);
473 if((rc
= mx_expandstr(m
->args
[n
], &args
[n
], u
, argname
)))
478 rc
= ((mx_simple_callback
*)e
->callback
)(m
->nargs
,
486 /* Macros we expand by rewriting their definition with argument values
487 * substituted and then expanding that. */
488 rc
= mx__expand_macro(e
, m
, output
, u
);
492 assert(!"impossible EXP_TYPE_MASK value");
495 /* For non-IO errors we generate some backtrace */
497 error(0, " ...in @%s at %s:%d",
498 m
->name
, m
->filename
, m
->line
);
503 assert(!"invalid m->type");
505 return mx_expand(m
->next
, output
, u
);
508 /** @brief Expand a template storing the result in a string
509 * @param m Where to start
510 * @param sp Where to store string
512 * @param what Token for backtrace, or NULL
513 * @return 0 on success, non-0 on error
515 * Same return conventions as mx_expand(). This wrapper is slightly more
516 * convenient to use from 'magic' expansions.
518 int mx_expandstr(const struct mx_node
*m
,
526 if(!(rc
= mx_expand(m
, sink_dynstr(d
), u
))) {
531 if(rc
&& rc
!= -1 && what
)
532 error(0, " ...in %s at %s:%d", what
, m
->filename
, m
->line
);
536 /** @brief Expand a template file
537 * @param path Filename
538 * @param output Where to send output
540 * @return 0 on success, non-0 on error
542 * Same return conventions as mx_expand().
544 int mx_expand_file(const char *path
,
551 const struct mx_node
*m
;
553 if((fd
= open(path
, O_RDONLY
)) < 0)
554 fatal(errno
, "error opening %s", path
);
555 if(fstat(fd
, &sb
) < 0)
556 fatal(errno
, "error statting %s", path
);
557 if(!S_ISREG(sb
.st_mode
))
558 fatal(0, "%s: not a regular file", path
);
560 b
= xmalloc_noptr(sb
.st_size
);
561 while(sofar
< sb
.st_size
) {
562 n
= read(fd
, b
+ sofar
, sb
.st_size
- sofar
);
566 fatal(0, "unexpected EOF reading %s", path
);
567 else if(errno
!= EINTR
)
568 fatal(errno
, "error reading %s", path
);
571 m
= mx_parse(path
, 1, b
, b
+ sb
.st_size
);
572 rc
= mx_expand(m
, output
, u
);
574 /* Mention inclusion in backtrace */
575 error(0, " ...in inclusion of file '%s'", path
);
579 /* Macros ------------------------------------------------------------------- */
581 /** @brief Rewrite a parse tree substituting sub-expansions
582 * @param m Parse tree to rewrite (from macro definition)
583 * @param ... Name/value pairs to rewrite
584 * @return Rewritten parse tree
586 * The name/value pair list consists of pairs of strings and is terminated by
587 * (char *)0. Names and values are both copied so need not survive the call.
589 const struct mx_node
*mx_rewritel(const struct mx_node
*m
,
592 hash
*h
= hash_new(sizeof (struct mx_node
*));
597 while((n
= va_arg(ap
, const char *))) {
598 v
= va_arg(ap
, const char *);
599 e
= xmalloc(sizeof *e
);
601 e
->filename
= m
->filename
;
604 e
->text
= xstrdup(v
);
605 hash_add(h
, n
, &e
, HASH_INSERT
);
606 /* hash_add() copies n */
608 return mx_rewrite(m
, h
);
611 /** @brief Rewrite a parse tree substituting in macro arguments
612 * @param definition Parse tree to rewrite (from macro definition)
613 * @param h Hash mapping argument names to argument values
614 * @return Rewritten parse tree
616 const struct mx_node
*mx_rewrite(const struct mx_node
*definition
,
618 const struct mx_node
*head
= 0, **tailp
= &head
, *argvalue
, *m
, *mm
, **ap
;
622 for(m
= definition
; m
; m
= m
->next
) {
625 nm
= xmalloc(sizeof *nm
);
626 *nm
= *m
; /* Dumb copy of text node fields */
627 nm
->next
= 0; /* Maintain list structure */
629 tailp
= (const struct mx_node
**)&nm
->next
;
633 && (ap
= hash_find(h
, m
->name
))) {
634 /* This expansion has no arguments and its name matches one of the
635 * macro arguments. (Even if it's a valid expansion name we override
636 * it.) We insert its value at this point. We do NOT recursively
637 * rewrite the argument's value - it is outside the lexical scope of
640 * We need to recreate the list structure but a shallow copy will
644 for(mm
= argvalue
; mm
; mm
= mm
->next
) {
645 nm
= xmalloc(sizeof *nm
);
649 tailp
= (const struct mx_node
**)&nm
->next
;
652 /* This is some other expansion. We recursively rewrite its argument
653 * values according to h. */
654 nm
= xmalloc(sizeof *nm
);
656 nm
->args
= xcalloc(nm
->nargs
, sizeof (struct mx_node
*));
657 for(n
= 0; n
< nm
->nargs
; ++n
)
658 nm
->args
[n
] = mx_rewrite(m
->args
[n
], h
);
661 tailp
= (const struct mx_node
**)&nm
->next
;
665 assert(!"invalid m->type");
668 *tailp
= 0; /* Mark end of list */
672 /** @brief Expand a macro
673 * @param e Macro definition
674 * @param m Macro expansion
675 * @param output Where to send output
677 * @return 0 on success, non-0 on error
679 static int mx__expand_macro(const struct expansion
*e
,
680 const struct mx_node
*m
,
683 hash
*h
= hash_new(sizeof (struct mx_node
*));
686 /* We store the macro arguments in a hash. Currently there is no check for
687 * duplicate argument names (and this would be the wrong place for it
688 * anyway); if you do that you just lose in some undefined way. */
689 for(n
= 0; n
< m
->nargs
; ++n
)
690 hash_add(h
, e
->args
[n
], &m
->args
[n
], HASH_INSERT
);
691 /* Generate a rewritten parse tree */
692 m
= mx_rewrite(e
->definition
, h
);
693 /* Expand the result */
694 return mx_expand(m
, output
, u
);
695 /* mx_expand() will update the backtrace */