e900a5e7d1a542f0ec1faedaa813b0bd9ac883dc
[pspp] / src / language / lexer / macro.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2021 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "language/lexer/macro.h"
20
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdlib.h>
24
25 #include "data/settings.h"
26 #include "language/lexer/segment.h"
27 #include "language/lexer/scan.h"
28 #include "libpspp/assertion.h"
29 #include "libpspp/i18n.h"
30 #include "libpspp/message.h"
31 #include "libpspp/str.h"
32 #include "libpspp/string-array.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 void
38 macro_token_copy (struct macro_token *dst, const struct macro_token *src)
39 {
40   token_copy (&dst->token, &src->token);
41   ss_alloc_substring (&dst->representation, src->representation);
42 }
43
44 void
45 macro_token_uninit (struct macro_token *mt)
46 {
47   token_uninit (&mt->token);
48   ss_dealloc (&mt->representation);
49 }
50
51 void
52 macro_token_to_representation (struct macro_token *mt, struct string *s)
53 {
54   ds_put_substring (s, mt->representation);
55 }
56
57 void
58 macro_tokens_copy (struct macro_tokens *dst, const struct macro_tokens *src)
59 {
60   *dst = (struct macro_tokens) {
61     .mts = xmalloc (src->n * sizeof *dst->mts),
62     .n = src->n,
63     .allocated = src->n,
64   };
65   for (size_t i = 0; i < src->n; i++)
66     macro_token_copy (&dst->mts[i], &src->mts[i]);
67 }
68
69 void
70 macro_tokens_uninit (struct macro_tokens *mts)
71 {
72   for (size_t i = 0; i < mts->n; i++)
73     macro_token_uninit (&mts->mts[i]);
74   free (mts->mts);
75 }
76
77 struct macro_token *
78 macro_tokens_add_uninit (struct macro_tokens *mts)
79 {
80   if (mts->n >= mts->allocated)
81     mts->mts = x2nrealloc (mts->mts, &mts->allocated, sizeof *mts->mts);
82   return &mts->mts[mts->n++];
83 }
84
85 void
86 macro_tokens_add (struct macro_tokens *mts, const struct macro_token *mt)
87 {
88   macro_token_copy (macro_tokens_add_uninit (mts), mt);
89 }
90
91 void
92 macro_tokens_from_string (struct macro_tokens *mts, const struct substring src,
93                           enum segmenter_mode mode)
94 {
95   struct state
96     {
97       struct segmenter segmenter;
98       struct substring body;
99     };
100
101   struct state state = {
102     .segmenter = SEGMENTER_INIT (mode),
103     .body = src,
104   };
105   struct state saved = state;
106
107   while (state.body.length > 0)
108     {
109       struct macro_token mt = {
110         .token = { .type = T_STOP },
111         .representation = { .string = state.body.string },
112       };
113       struct token *token = &mt.token;
114
115       struct scanner scanner;
116       scanner_init (&scanner, token);
117
118       for (;;)
119         {
120           enum segment_type type;
121           int seg_len = segmenter_push (&state.segmenter, state.body.string,
122                                         state.body.length, true, &type);
123           assert (seg_len >= 0);
124
125           struct substring segment = ss_head (state.body, seg_len);
126           ss_advance (&state.body, seg_len);
127
128           enum scan_result result = scanner_push (&scanner, type, segment, token);
129           if (result == SCAN_SAVE)
130             saved = state;
131           else if (result == SCAN_BACK)
132             {
133               state = saved;
134               break;
135             }
136           else if (result == SCAN_DONE)
137             break;
138         }
139
140       /* We have a token in 'token'. */
141       if (is_scan_type (token->type))
142         {
143           if (token->type != SCAN_SKIP)
144             {
145               printf ("error\n");
146               /* XXX report error */
147             }
148         }
149       else
150         {
151           mt.representation.length = state.body.string - mt.representation.string;
152           macro_tokens_add (mts, &mt);
153         }
154       token_uninit (token);
155     }
156 }
157
158 void
159 macro_tokens_print (const struct macro_tokens *mts, FILE *stream)
160 {
161   for (size_t i = 0; i < mts->n; i++)
162     token_print (&mts->mts[i].token, stream);
163 }
164
165 enum token_class
166   {
167     TC_ENDCMD,                  /* No space before or after (new-line after). */
168     TC_BINOP,                   /* Space on both sides. */
169     TC_COMMA,                   /* Space afterward. */
170     TC_ID,                      /* Don't need spaces except sequentially. */
171     TC_PUNCT,                   /* Don't need spaces except sequentially. */
172   };
173
174 static bool
175 needs_space (enum token_class prev, enum token_class next)
176 {
177   /* Don't need a space before or after the end of a command.
178      (A new-line is needed afterward as a special case.) */
179   if (prev == TC_ENDCMD || next == TC_ENDCMD)
180     return false;
181
182   /* Binary operators always have a space on both sides. */
183   if (prev == TC_BINOP || next == TC_BINOP)
184     return true;
185
186   /* A comma always has a space afterward. */
187   if (prev == TC_COMMA)
188     return true;
189
190   /* Otherwise, PREV is TC_ID or TC_PUNCT, which only need a space if there are
191      two or them in a row. */
192   return prev == next;
193 }
194
195 static enum token_class
196 classify_token (enum token_type type)
197 {
198   switch (type)
199     {
200     case T_ID:
201     case T_MACRO_ID:
202     case T_POS_NUM:
203     case T_NEG_NUM:
204     case T_STRING:
205       return TC_ID;
206
207     case T_STOP:
208       return TC_PUNCT;
209
210     case T_ENDCMD:
211       return TC_ENDCMD;
212
213     case T_LPAREN:
214     case T_RPAREN:
215     case T_LBRACK:
216     case T_RBRACK:
217       return TC_PUNCT;
218
219     case T_PLUS:
220     case T_DASH:
221     case T_ASTERISK:
222     case T_SLASH:
223     case T_EQUALS:
224     case T_AND:
225     case T_OR:
226     case T_NOT:
227     case T_EQ:
228     case T_GE:
229     case T_GT:
230     case T_LE:
231     case T_LT:
232     case T_NE:
233     case T_ALL:
234     case T_BY:
235     case T_TO:
236     case T_WITH:
237     case T_EXP:
238     case T_MACRO_PUNCT:
239       return TC_BINOP;
240
241     case T_COMMA:
242       return TC_COMMA;
243     }
244
245   NOT_REACHED ();
246 }
247
248 void
249 macro_tokens_to_representation (struct macro_tokens *mts, struct string *s)
250 {
251   if (!mts->n)
252     return;
253
254   macro_token_to_representation (&mts->mts[0], s);
255   for (size_t i = 1; i < mts->n; i++)
256     {
257       enum token_type prev = mts->mts[i - 1].token.type;
258       enum token_type next = mts->mts[i].token.type;
259
260       if (prev == T_ENDCMD)
261         ds_put_byte (s, '\n');
262       else
263         {
264           enum token_class pc = classify_token (prev);
265           enum token_class nc = classify_token (next);
266           if (needs_space (pc, nc))
267             ds_put_byte (s, ' ');
268         }
269
270       macro_token_to_representation (&mts->mts[i], s);
271     }
272 }
273
274 void
275 macro_destroy (struct macro *m)
276 {
277   if (!m)
278     return;
279
280   free (m->name);
281   for (size_t i = 0; i < m->n_params; i++)
282     {
283       struct macro_param *p = &m->params[i];
284       free (p->name);
285
286       macro_tokens_uninit (&p->def);
287
288       switch (p->arg_type)
289         {
290         case ARG_N_TOKENS:
291           break;
292
293         case ARG_CHAREND:
294           token_uninit (&p->charend);
295           break;
296
297         case ARG_ENCLOSE:
298           token_uninit (&p->enclose[0]);
299           token_uninit (&p->enclose[1]);
300           break;
301
302         case ARG_CMDEND:
303           break;
304         }
305     }
306   free (m->params);
307   macro_tokens_uninit (&m->body);
308   free (m);
309 }
310 \f
311 struct macro_set *
312 macro_set_create (void)
313 {
314   struct macro_set *set = xmalloc (sizeof *set);
315   *set = (struct macro_set) {
316     .macros = HMAP_INITIALIZER (set->macros),
317   };
318   return set;
319 }
320
321 void
322 macro_set_destroy (struct macro_set *set)
323 {
324   if (!set)
325     return;
326
327   struct macro *macro, *next;
328   HMAP_FOR_EACH_SAFE (macro, next, struct macro, hmap_node, &set->macros)
329     {
330       hmap_delete (&set->macros, &macro->hmap_node);
331       macro_destroy (macro);
332     }
333   hmap_destroy (&set->macros);
334   free (set);
335 }
336
337 static unsigned int
338 hash_macro_name (const char *name)
339 {
340   return utf8_hash_case_string (name, 0);
341 }
342
343 static struct macro *
344 macro_set_find__ (struct macro_set *set, const char *name)
345 {
346   struct macro *macro;
347   HMAP_FOR_EACH_WITH_HASH (macro, struct macro, hmap_node,
348                            hash_macro_name (name), &set->macros)
349     if (!utf8_strcasecmp (macro->name, name))
350       return macro;
351
352   return NULL;
353 }
354
355 const struct macro *
356 macro_set_find (const struct macro_set *set, const char *name)
357 {
358   return macro_set_find__ (CONST_CAST (struct macro_set *, set), name);
359 }
360
361 /* Adds M to SET.  M replaces any existing macro with the same name.  Takes
362    ownership of M. */
363 void
364 macro_set_add (struct macro_set *set, struct macro *m)
365 {
366   struct macro *victim = macro_set_find__ (set, m->name);
367   if (victim)
368     {
369       hmap_delete (&set->macros, &victim->hmap_node);
370       macro_destroy (victim);
371     }
372
373   hmap_insert (&set->macros, &m->hmap_node, hash_macro_name (m->name));
374 }
375 \f
376 enum me_state
377   {
378     /* Error state. */
379     ME_ERROR,
380
381     /* Accumulating tokens in me->params toward the end of any type of
382        argument. */
383     ME_ARG,
384
385     /* Expecting the opening delimiter of an ARG_ENCLOSE argument. */
386     ME_ENCLOSE,
387
388     /* Expecting a keyword for a keyword argument. */
389     ME_KEYWORD,
390
391     /* Expecting an equal sign for a keyword argument. */
392     ME_EQUALS,
393   };
394
395
396 struct macro_expander
397   {
398     const struct macro_set *macros;
399
400     enum me_state state;
401     size_t n_tokens;
402
403     const struct macro *macro;
404     struct macro_tokens **args;
405     const struct macro_param *param;
406   };
407
408 static int
409 me_finished (struct macro_expander *me)
410 {
411   for (size_t i = 0; i < me->macro->n_params; i++)
412     if (!me->args[i])
413       {
414         me->args[i] = xmalloc (sizeof *me->args[i]);
415         macro_tokens_copy (me->args[i], &me->macro->params[i].def);
416       }
417   return me->n_tokens;
418 }
419
420 static int
421 me_next_arg (struct macro_expander *me)
422 {
423   if (!me->param)
424     {
425       assert (!me->macro->n_params);
426       return me_finished (me);
427     }
428   else if (me->param->positional)
429     {
430       me->param++;
431       if (me->param >= &me->macro->params[me->macro->n_params])
432         return me_finished (me);
433       else
434         {
435           me->state = (!me->param->positional ? ME_KEYWORD
436                        : me->param->arg_type == ARG_ENCLOSE ? ME_ENCLOSE
437                        : ME_ARG);
438           return 0;
439         }
440     }
441   else
442     {
443       for (size_t i = 0; i < me->macro->n_params; i++)
444         if (!me->args[i])
445           {
446             me->state = ME_KEYWORD;
447             return 0;
448           }
449       return me_finished (me);
450     }
451 }
452
453 static int
454 me_error (struct macro_expander *me)
455 {
456   me->state = ME_ERROR;
457   return -1;
458 }
459
460 static int
461 me_add_arg (struct macro_expander *me, const struct macro_token *mt)
462 {
463   const struct macro_param *p = me->param;
464
465   const struct token *token = &mt->token;
466   if ((token->type == T_ENDCMD || token->type == T_STOP)
467       && p->arg_type != ARG_CMDEND)
468     {
469       msg (SE, _("Unexpected end of command reading argument %s "
470                  "to macro %s."), me->param->name, me->macro->name);
471
472       return me_error (me);
473     }
474
475   me->n_tokens++;
476
477   struct macro_tokens **argp = &me->args[p - me->macro->params];
478   if (!*argp)
479     *argp = xzalloc (sizeof **argp);
480   struct macro_tokens *arg = *argp;
481   if (p->arg_type == ARG_N_TOKENS)
482     {
483       macro_tokens_add (arg, mt);
484       if (arg->n >= p->n_tokens)
485         return me_next_arg (me);
486       return 0;
487     }
488   else if (p->arg_type == ARG_CMDEND)
489     {
490       if (token->type == T_ENDCMD || token->type == T_STOP)
491         return me_next_arg (me);
492       macro_tokens_add (arg, mt);
493       return 0;
494     }
495   else
496     {
497       const struct token *end
498         = p->arg_type == ARG_CHAREND ? &p->charend : &p->enclose[1];
499       if (token_equal (token, end))
500         return me_next_arg (me);
501       macro_tokens_add (arg, mt);
502       return 0;
503     }
504 }
505
506 static int
507 me_expected (struct macro_expander *me, const struct macro_token *actual,
508              const struct token *expected)
509 {
510   const struct substring actual_s
511     = (actual->representation.length ? actual->representation
512        : ss_cstr (_("<end of input>")));
513   char *expected_s = token_to_string (expected);
514   msg (SE, _("Found `%.*s' while expecting `%s' reading argument %s "
515              "to macro %s."),
516        (int) actual_s.length, actual_s.string, expected_s,
517        me->param->name, me->macro->name);
518   free (expected_s);
519
520   return me_error (me);
521 }
522
523 static int
524 me_enclose (struct macro_expander *me, const struct macro_token *mt)
525 {
526   const struct token *token = &mt->token;
527   me->n_tokens++;
528
529   if (token_equal (&me->param->enclose[0], token))
530     {
531       me->state = ME_ARG;
532       return 0;
533     }
534
535   return me_expected (me, mt, &me->param->enclose[0]);
536 }
537
538 static const struct macro_param *
539 macro_find_parameter_by_name (const struct macro *m, struct substring name)
540 {
541   if (ss_first (name) == '!')
542     ss_advance (&name, 1);
543
544   for (size_t i = 0; i < m->n_params; i++)
545     {
546       const struct macro_param *p = &m->params[i];
547       struct substring p_name = ss_cstr (p->name + 1);
548       if (!utf8_strncasecmp (p_name.string, p_name.length,
549                              name.string, name.length))
550         return p;
551     }
552   return NULL;
553 }
554
555 static int
556 me_keyword (struct macro_expander *me, const struct macro_token *mt)
557 {
558   const struct token *token = &mt->token;
559   if (token->type != T_ID)
560     return me_finished (me);
561
562   const struct macro_param *p = macro_find_parameter_by_name (me->macro,
563                                                               token->string);
564   if (p)
565     {
566       size_t arg_index = p - me->macro->params;
567       me->param = p;
568       if (me->args[arg_index])
569         {
570           msg (SE,
571                _("Argument %s multiply specified in call to macro %s."),
572                p->name, me->macro->name);
573           return me_error (me);
574         }
575
576       me->n_tokens++;
577       me->state = ME_EQUALS;
578       return 0;
579     }
580
581   return me_finished (me);
582 }
583
584 static int
585 me_equals (struct macro_expander *me, const struct macro_token *mt)
586 {
587   const struct token *token = &mt->token;
588   me->n_tokens++;
589
590   if (token->type == T_EQUALS)
591     {
592       me->state = ME_ARG;
593       return 0;
594     }
595
596   return me_expected (me, mt, &(struct token) { .type = T_EQUALS });
597 }
598
599 int
600 macro_expander_create (const struct macro_set *macros,
601                        const struct token *token,
602                        struct macro_expander **mep)
603 {
604   *mep = NULL;
605   if (macro_set_is_empty (macros))
606     return -1;
607   if (token->type != T_ID && token->type != T_MACRO_ID)
608     return -1;
609
610   const struct macro *macro = macro_set_find (macros, token->string.string);
611   if (!macro)
612     return -1;
613
614   struct macro_expander *me = xmalloc (sizeof *me);
615   *me = (struct macro_expander) {
616     .macros = macros,
617     .n_tokens = 1,
618     .macro = macro,
619   };
620   *mep = me;
621
622   if (!macro->n_params)
623     return 1;
624   else
625     {
626       me->state = (!macro->params[0].positional ? ME_KEYWORD
627                    : macro->params[0].arg_type == ARG_ENCLOSE ? ME_ENCLOSE
628                    : ME_ARG);
629       me->args = xcalloc (macro->n_params, sizeof *me->args);
630       me->param = macro->params;
631       return 0;
632     }
633 }
634
635 void
636 macro_expander_destroy (struct macro_expander *me)
637 {
638   if (!me)
639     return;
640
641   for (size_t i = 0; i < me->macro->n_params; i++)
642     if (me->args[i])
643       {
644         macro_tokens_uninit (me->args[i]);
645         free (me->args[i]);
646       }
647   free (me->args);
648   free (me);
649 }
650
651 /* Adds TOKEN to the collection of tokens in ME that potentially need to be
652    macro expanded.
653
654    Returns -1 if the tokens added do not actually invoke a macro.  The caller
655    should consume the first token without expanding it.
656
657    Returns 0 if the macro expander needs more tokens, for macro arguments or to
658    decide whether this is actually a macro invocation.  The caller should call
659    macro_expander_add() again with the next token.
660
661    Returns a positive number to indicate that the returned number of tokens
662    invoke a macro.  The number returned might be less than the number of tokens
663    added because it can take a few tokens of lookahead to determine whether the
664    macro invocation is finished.  The caller should call
665    macro_expander_get_expansion() to obtain the expansion. */
666 int
667 macro_expander_add (struct macro_expander *me, const struct macro_token *mt)
668 {
669   switch (me->state)
670     {
671     case ME_ERROR:
672       return -1;
673
674     case ME_ARG:
675       return me_add_arg (me, mt);
676
677     case ME_ENCLOSE:
678       return me_enclose (me, mt);
679
680     case ME_KEYWORD:
681       return me_keyword (me, mt);
682
683     case ME_EQUALS:
684       return me_equals (me, mt);
685
686     default:
687       NOT_REACHED ();
688     }
689 }
690
691 /* Each argument to a macro function is one of:
692
693        - A quoted string or other single literal token.
694
695        - An argument to the macro being expanded, e.g. !1 or a named argument.
696
697        - !*.
698
699        - A function invocation.
700
701    Each function invocation yields a character sequence to be turned into a
702    sequence of tokens.  The case where that character sequence is a single
703    quoted string is an important special case.
704 */
705 struct parse_macro_function_ctx
706   {
707     struct macro_token *input;
708     size_t n_input;
709     int nesting_countdown;
710     const struct macro_set *macros;
711     const struct macro_expander *me;
712     bool *expand;
713   };
714
715 static void
716 macro_expand (const struct macro_tokens *,
717               int nesting_countdown, const struct macro_set *,
718               const struct macro_expander *, bool *expand, struct macro_tokens *exp);
719
720 static bool
721 expand_macro_function (struct parse_macro_function_ctx *ctx,
722                        struct string *output, size_t *input_consumed);
723
724 static size_t
725 parse_function_arg (struct parse_macro_function_ctx *ctx,
726                     size_t i, struct string *farg)
727 {
728   struct macro_token *tokens = ctx->input;
729   const struct token *token = &tokens[i].token;
730   if (token->type == T_MACRO_ID)
731     {
732       const struct macro_param *param = macro_find_parameter_by_name (
733         ctx->me->macro, token->string);
734       if (param)
735         {
736           size_t param_idx = param - ctx->me->macro->params;
737           const struct macro_tokens *marg = ctx->me->args[param_idx];
738           for (size_t i = 0; i < marg->n; i++)
739             {
740               if (i)
741                 ds_put_byte (farg, ' ');
742               ds_put_substring (farg, marg->mts[i].representation);
743             }
744           return 1;
745         }
746
747       struct parse_macro_function_ctx subctx = {
748         .input = &ctx->input[i],
749         .n_input = ctx->n_input - i,
750         .nesting_countdown = ctx->nesting_countdown,
751         .macros = ctx->macros,
752         .me = ctx->me,
753         .expand = ctx->expand,
754       };
755       size_t subinput_consumed;
756       if (expand_macro_function (&subctx, farg, &subinput_consumed))
757         return subinput_consumed;
758     }
759
760   ds_put_substring (farg, tokens[i].representation);
761   return 1;
762 }
763
764 static bool
765 parse_macro_function (struct parse_macro_function_ctx *ctx,
766                       struct string_array *args,
767                       struct substring function,
768                       int min_args, int max_args,
769                       size_t *input_consumed)
770 {
771   struct macro_token *tokens = ctx->input;
772   size_t n_tokens = ctx->n_input;
773
774   if (!n_tokens
775       || tokens[0].token.type != T_MACRO_ID
776       || !ss_equals_case (tokens[0].token.string, function))
777     return false;
778
779   if (n_tokens < 2 || tokens[1].token.type != T_LPAREN)
780     {
781       printf ("`(' expected following %s'\n", function.string);
782       return false;
783     }
784
785   string_array_init (args);
786
787   for (size_t i = 2;; )
788     {
789       if (i >= n_tokens)
790         goto unexpected_end;
791       if (tokens[i].token.type == T_RPAREN)
792         {
793           *input_consumed = i + 1;
794           if (args->n < min_args || args->n > max_args)
795             {
796               printf ("Wrong number of arguments to %s.\n", function.string);
797               goto error;
798             }
799           return true;
800         }
801
802       struct string s = DS_EMPTY_INITIALIZER;
803       i += parse_function_arg (ctx, i, &s);
804       if (i >= n_tokens)
805         {
806           ds_destroy (&s);
807           goto unexpected_end;
808         }
809       string_array_append_nocopy (args, ds_steal_cstr (&s));
810
811       if (tokens[i].token.type == T_COMMA)
812         i++;
813       else if (tokens[i].token.type != T_RPAREN)
814         {
815           printf ("Expecting `,' or `)' in %s invocation.", function.string);
816           goto error;
817         }
818     }
819
820 unexpected_end:
821   printf ("Missing closing parenthesis in arguments to %s.\n",
822           function.string);
823   /* Fall through. */
824 error:
825   string_array_destroy (args);
826   return false;
827 }
828
829 static bool
830 unquote_string (const char *s, struct string *content)
831 {
832   struct string_lexer slex;
833   string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE /* XXX */);
834
835   struct token token1;
836   if (!string_lexer_next (&slex, &token1))
837     return false;
838
839   if (token1.type != T_STRING)
840     {
841       token_uninit (&token1);
842       return false;
843     }
844
845   struct token token2;
846   if (string_lexer_next (&slex, &token2))
847     {
848       token_uninit (&token1);
849       token_uninit (&token2);
850       return false;
851     }
852
853   ds_put_substring (content, token1.string);
854   token_uninit (&token1);
855   return true;
856 }
857
858 static const char *
859 unquote_string_in_place (const char *s, struct string *tmp)
860 {
861   ds_init_empty (tmp);
862   return unquote_string (s, tmp) ? ds_cstr (tmp) : s;
863 }
864
865 static bool
866 parse_integer (const char *s, int *np)
867 {
868   errno = 0;
869
870   char *tail;
871   long int n = strtol (s, &tail, 10);
872   *np = n < INT_MIN ? INT_MIN : n > INT_MAX ? INT_MAX : n;
873   tail += strspn (tail, CC_SPACES);
874   return *tail == '\0' && errno != ERANGE && n == *np;
875 }
876
877 static bool
878 expand_macro_function (struct parse_macro_function_ctx *ctx,
879                        struct string *output,
880                        size_t *input_consumed)
881 {
882   struct string_array args;
883
884   if (parse_macro_function (ctx, &args, ss_cstr ("!length"), 1, 1,
885                             input_consumed))
886     ds_put_format (output, "%zu", strlen (args.strings[0]));
887   else if (parse_macro_function (ctx, &args, ss_cstr ("!blanks"), 1, 1,
888                                  input_consumed))
889     {
890       int n;
891       if (!parse_integer (args.strings[0], &n))
892         {
893           printf ("argument to !BLANKS must be non-negative integer (not \"%s\")\n", args.strings[0]);
894           string_array_destroy (&args);
895           return false;
896         }
897
898       ds_put_byte_multiple (output, ' ', n);
899     }
900   else if (parse_macro_function (ctx, &args, ss_cstr ("!concat"), 1, INT_MAX,
901                                  input_consumed))
902     {
903       for (size_t i = 0; i < args.n; i++)
904         if (!unquote_string (args.strings[i], output))
905           ds_put_cstr (output, args.strings[i]);
906     }
907   else if (parse_macro_function (ctx, &args, ss_cstr ("!head"), 1, 1,
908                                  input_consumed))
909     {
910       struct string tmp;
911       const char *s = unquote_string_in_place (args.strings[0], &tmp);
912
913       struct macro_tokens mts = { .n = 0 };
914       macro_tokens_from_string (&mts, ss_cstr (s), SEG_MODE_INTERACTIVE /* XXX */);
915       if (mts.n > 0)
916         ds_put_substring (output, mts.mts[0].representation);
917       macro_tokens_uninit (&mts);
918       ds_destroy (&tmp);
919     }
920   else if (parse_macro_function (ctx, &args, ss_cstr ("!index"), 2, 2,
921                                  input_consumed))
922     {
923       const char *haystack = args.strings[0];
924       const char *needle = strstr (haystack, args.strings[1]);
925       ds_put_format (output, "%zu", needle ? needle - haystack + 1 : 0);
926     }
927   else if (parse_macro_function (ctx, &args, ss_cstr ("!quote"), 1, 1,
928                                  input_consumed))
929     {
930       if (unquote_string (args.strings[0], NULL))
931         ds_put_cstr (output, args.strings[0]);
932       else
933         {
934           ds_extend (output, strlen (args.strings[0]) + 2);
935           ds_put_byte (output, '\'');
936           for (const char *p = args.strings[0]; *p; p++)
937             {
938               if (*p == '\'')
939                 ds_put_byte (output, '\'');
940               ds_put_byte (output, *p);
941             }
942           ds_put_byte (output, '\'');
943         }
944     }
945   else if (parse_macro_function (ctx, &args, ss_cstr ("!substr"), 2, 3,
946                                  input_consumed))
947     {
948       int start;
949       if (!parse_integer (args.strings[1], &start) || start < 1)
950         {
951           printf ("second argument to !SUBSTR must be positive integer (not \"%s\")\n", args.strings[1]);
952           string_array_destroy (&args);
953           return false;
954         }
955
956       int count = INT_MAX;
957       if (args.n > 2 && (!parse_integer (args.strings[2], &count) || count < 0))
958         {
959           printf ("third argument to !SUBSTR must be non-negative integer (not \"%s\")\n", args.strings[1]);
960           string_array_destroy (&args);
961           return false;
962         }
963
964       struct substring s = ss_cstr (args.strings[0]);
965       ds_put_substring (output, ss_substr (s, start - 1, count));
966     }
967   else if (parse_macro_function (ctx, &args, ss_cstr ("!tail"), 1, 1,
968                                  input_consumed))
969     {
970       struct string tmp;
971       const char *s = unquote_string_in_place (args.strings[0], &tmp);
972
973       struct macro_tokens mts = { .n = 0 };
974       macro_tokens_from_string (&mts, ss_cstr (s), SEG_MODE_INTERACTIVE /* XXX */);
975       if (mts.n > 1)
976         {
977           struct macro_tokens tail = { .mts = mts.mts + 1, .n = mts.n - 1 };
978           macro_tokens_to_representation (&tail, output);
979         }
980       macro_tokens_uninit (&mts);
981       ds_destroy (&tmp);
982     }
983   else if (parse_macro_function (ctx, &args, ss_cstr ("!unquote"), 1, 1,
984                                  input_consumed))
985     {
986       if (!unquote_string (args.strings[0], output))
987         ds_put_cstr (output, args.strings[0]);
988     }
989   else if (parse_macro_function (ctx, &args, ss_cstr ("!upcase"), 1, 1,
990                                  input_consumed))
991     {
992       struct string tmp;
993       const char *s = unquote_string_in_place (args.strings[0], &tmp);
994       char *upper = utf8_to_upper (s);
995       ds_put_cstr (output, upper);
996       free (upper);
997       ds_destroy (&tmp);
998     }
999   else if (parse_macro_function (ctx, &args, ss_cstr ("!eval"), 1, 1,
1000                                  input_consumed))
1001     {
1002       struct macro_tokens mts = { .n = 0 };
1003       macro_tokens_from_string (&mts, ss_cstr (args.strings[0]),
1004                                 SEG_MODE_INTERACTIVE /* XXX */);
1005       struct macro_tokens exp = { .n = 0 };
1006       macro_expand (&mts, ctx->nesting_countdown - 1, ctx->macros,
1007                     ctx->me, ctx->expand, &exp);
1008       macro_tokens_to_representation (&exp, output);
1009       macro_tokens_uninit (&exp);
1010       macro_tokens_uninit (&mts);
1011     }
1012   else if (ctx->n_input > 0
1013            && ctx->input[0].token.type == T_MACRO_ID
1014            && ss_equals_case (ctx->input[0].token.string, ss_cstr ("!null")))
1015     {
1016       *input_consumed = 1;
1017       return true;
1018     }
1019   else
1020     return false;
1021
1022   string_array_destroy (&args);
1023   return true;
1024 }
1025
1026 static void
1027 macro_expand (const struct macro_tokens *mts,
1028               int nesting_countdown, const struct macro_set *macros,
1029               const struct macro_expander *me, bool *expand,
1030               struct macro_tokens *exp)
1031 {
1032   if (nesting_countdown <= 0)
1033     {
1034       printf ("maximum nesting level exceeded\n");
1035       for (size_t i = 0; i < mts->n; i++)
1036         macro_tokens_add (exp, &mts->mts[i]);
1037       return;
1038     }
1039
1040   for (size_t i = 0; i < mts->n; i++)
1041     {
1042       const struct macro_token *mt = &mts->mts[i];
1043       const struct token *token = &mt->token;
1044       if (token->type == T_MACRO_ID && me)
1045         {
1046           const struct macro_param *param = macro_find_parameter_by_name (
1047             me->macro, token->string);
1048           if (param)
1049             {
1050               const struct macro_tokens *arg = me->args[param - me->macro->params];
1051               //macro_tokens_print (arg, stdout);
1052               if (*expand && param->expand_arg)
1053                 macro_expand (arg, nesting_countdown, macros, NULL, expand, exp);
1054               else
1055                 for (size_t i = 0; i < arg->n; i++)
1056                   macro_tokens_add (exp, &arg->mts[i]);
1057               continue;
1058             }
1059         }
1060
1061       if (*expand)
1062         {
1063           struct macro_expander *subme;
1064           int retval = macro_expander_create (macros, token, &subme);
1065           for (size_t j = 1; !retval; j++)
1066             {
1067               const struct macro_token endcmd = { .token = { .type = T_ENDCMD } };
1068               retval = macro_expander_add (
1069                 subme, i + j < mts->n ? &mts->mts[i + j] : &endcmd);
1070             }
1071           if (retval > 0)
1072             {
1073               i += retval - 1;
1074               macro_expand (&subme->macro->body, nesting_countdown - 1, macros,
1075                             subme, expand, exp);
1076               macro_expander_destroy (subme);
1077               continue;
1078             }
1079
1080           macro_expander_destroy (subme);
1081         }
1082
1083       if (token->type != T_MACRO_ID)
1084         {
1085           macro_tokens_add (exp, mt);
1086           continue;
1087         }
1088
1089       /* Maybe each arg should just be a string, either a quoted string or a
1090          non-quoted string containing tokens. */
1091       struct parse_macro_function_ctx ctx = {
1092         .input = &mts->mts[i],
1093         .n_input = mts->n - i,
1094         .nesting_countdown = nesting_countdown,
1095         .macros = macros,
1096         .me = me,
1097         .expand = expand,
1098       };
1099       struct string function_output = DS_EMPTY_INITIALIZER;
1100       size_t function_consumed;
1101       if (expand_macro_function (&ctx, &function_output, &function_consumed))
1102         {
1103           i += function_consumed - 1;
1104
1105           macro_tokens_from_string (exp, function_output.ss,
1106                                     SEG_MODE_INTERACTIVE /* XXX */);
1107           ds_destroy (&function_output);
1108
1109           continue;
1110         }
1111
1112       if (ss_equals_case (token->string, ss_cstr ("!onexpand")))
1113         *expand = true;
1114       else if (ss_equals_case (token->string, ss_cstr ("!offexpand")))
1115         *expand = false;
1116       else
1117         macro_tokens_add (exp, mt);
1118     }
1119 }
1120
1121 void
1122 macro_expander_get_expansion (struct macro_expander *me, struct macro_tokens *exp)
1123 {
1124 #if 0
1125   for (size_t i = 0; i < me->macro->n_params; i++)
1126     {
1127       printf ("%s:\n", me->macro->params[i].name);
1128       macro_tokens_print (me->args[i], stdout);
1129     }
1130 #endif
1131
1132   bool expand = true;
1133   macro_expand (&me->macro->body, settings_get_mnest (),
1134                 me->macros, me, &expand, exp);
1135
1136 #if 0
1137   printf ("expansion:\n");
1138   macro_tokens_print (exp, stdout);
1139 #endif
1140 }
1141