11f5207b7SJohn Levon /*
21f5207b7SJohn Levon  * Do C preprocessing, based on a token list gathered by
31f5207b7SJohn Levon  * the tokenizer.
41f5207b7SJohn Levon  *
51f5207b7SJohn Levon  * This may not be the smartest preprocessor on the planet.
61f5207b7SJohn Levon  *
71f5207b7SJohn Levon  * Copyright (C) 2003 Transmeta Corp.
81f5207b7SJohn Levon  *               2003-2004 Linus Torvalds
91f5207b7SJohn Levon  *
101f5207b7SJohn Levon  * Permission is hereby granted, free of charge, to any person obtaining a copy
111f5207b7SJohn Levon  * of this software and associated documentation files (the "Software"), to deal
121f5207b7SJohn Levon  * in the Software without restriction, including without limitation the rights
131f5207b7SJohn Levon  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
141f5207b7SJohn Levon  * copies of the Software, and to permit persons to whom the Software is
151f5207b7SJohn Levon  * furnished to do so, subject to the following conditions:
161f5207b7SJohn Levon  *
171f5207b7SJohn Levon  * The above copyright notice and this permission notice shall be included in
181f5207b7SJohn Levon  * all copies or substantial portions of the Software.
191f5207b7SJohn Levon  *
201f5207b7SJohn Levon  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
211f5207b7SJohn Levon  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
221f5207b7SJohn Levon  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
231f5207b7SJohn Levon  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
241f5207b7SJohn Levon  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
251f5207b7SJohn Levon  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
261f5207b7SJohn Levon  * THE SOFTWARE.
271f5207b7SJohn Levon  */
281f5207b7SJohn Levon #include <stdio.h>
291f5207b7SJohn Levon #include <stdlib.h>
301f5207b7SJohn Levon #include <stdarg.h>
311f5207b7SJohn Levon #include <stddef.h>
321f5207b7SJohn Levon #include <string.h>
331f5207b7SJohn Levon #include <ctype.h>
341f5207b7SJohn Levon #include <unistd.h>
351f5207b7SJohn Levon #include <fcntl.h>
361f5207b7SJohn Levon #include <limits.h>
371f5207b7SJohn Levon #include <time.h>
381f5207b7SJohn Levon #include <dirent.h>
391f5207b7SJohn Levon #include <sys/stat.h>
401f5207b7SJohn Levon 
411f5207b7SJohn Levon #include "lib.h"
421f5207b7SJohn Levon #include "allocate.h"
431f5207b7SJohn Levon #include "parse.h"
441f5207b7SJohn Levon #include "token.h"
451f5207b7SJohn Levon #include "symbol.h"
461f5207b7SJohn Levon #include "expression.h"
471f5207b7SJohn Levon #include "scope.h"
481f5207b7SJohn Levon 
491f5207b7SJohn Levon static struct ident_list *macros;	// only needed for -dD
501f5207b7SJohn Levon static int false_nesting = 0;
511f5207b7SJohn Levon static int counter_macro = 0;		// __COUNTER__ expansion
52c85f09ccSJohn Levon static int include_level = 0;
531f5207b7SJohn Levon 
541f5207b7SJohn Levon #define INCLUDEPATHS 300
551f5207b7SJohn Levon const char *includepath[INCLUDEPATHS+1] = {
561f5207b7SJohn Levon 	"",
571f5207b7SJohn Levon 	"/usr/include",
581f5207b7SJohn Levon 	"/usr/local/include",
591f5207b7SJohn Levon 	NULL
601f5207b7SJohn Levon };
611f5207b7SJohn Levon 
621f5207b7SJohn Levon static const char **quote_includepath = includepath;
631f5207b7SJohn Levon static const char **angle_includepath = includepath + 1;
641f5207b7SJohn Levon static const char **isys_includepath   = includepath + 1;
651f5207b7SJohn Levon static const char **sys_includepath   = includepath + 1;
661f5207b7SJohn Levon static const char **dirafter_includepath = includepath + 3;
671f5207b7SJohn Levon 
681f5207b7SJohn Levon #define dirty_stream(stream)				\
691f5207b7SJohn Levon 	do {						\
701f5207b7SJohn Levon 		if (!stream->dirty) {			\
711f5207b7SJohn Levon 			stream->dirty = 1;		\
721f5207b7SJohn Levon 			if (!stream->ifndef)		\
731f5207b7SJohn Levon 				stream->protect = NULL;	\
741f5207b7SJohn Levon 		}					\
751f5207b7SJohn Levon 	} while(0)
761f5207b7SJohn Levon 
771f5207b7SJohn Levon #define end_group(stream)					\
781f5207b7SJohn Levon 	do {							\
791f5207b7SJohn Levon 		if (stream->ifndef == stream->top_if) {		\
801f5207b7SJohn Levon 			stream->ifndef = NULL;			\
811f5207b7SJohn Levon 			if (!stream->dirty)			\
821f5207b7SJohn Levon 				stream->protect = NULL;		\
831f5207b7SJohn Levon 			else if (stream->protect)		\
841f5207b7SJohn Levon 				stream->dirty = 0;		\
851f5207b7SJohn Levon 		}						\
861f5207b7SJohn Levon 	} while(0)
871f5207b7SJohn Levon 
881f5207b7SJohn Levon #define nesting_error(stream)		\
891f5207b7SJohn Levon 	do {				\
901f5207b7SJohn Levon 		stream->dirty = 1;	\
911f5207b7SJohn Levon 		stream->ifndef = NULL;	\
921f5207b7SJohn Levon 		stream->protect = NULL;	\
931f5207b7SJohn Levon 	} while(0)
941f5207b7SJohn Levon 
alloc_token(struct position * pos)951f5207b7SJohn Levon static struct token *alloc_token(struct position *pos)
961f5207b7SJohn Levon {
971f5207b7SJohn Levon 	struct token *token = __alloc_token(0);
981f5207b7SJohn Levon 
991f5207b7SJohn Levon 	token->pos.stream = pos->stream;
1001f5207b7SJohn Levon 	token->pos.line = pos->line;
1011f5207b7SJohn Levon 	token->pos.pos = pos->pos;
1021f5207b7SJohn Levon 	token->pos.whitespace = 1;
1031f5207b7SJohn Levon 	return token;
1041f5207b7SJohn Levon }
1051f5207b7SJohn Levon 
1061f5207b7SJohn Levon /* Expand symbol 'sym' at '*list' */
1071f5207b7SJohn Levon static int expand(struct token **, struct symbol *);
1081f5207b7SJohn Levon 
replace_with_string(struct token * token,const char * str)1091f5207b7SJohn Levon static void replace_with_string(struct token *token, const char *str)
1101f5207b7SJohn Levon {
1111f5207b7SJohn Levon 	int size = strlen(str) + 1;
1121f5207b7SJohn Levon 	struct string *s = __alloc_string(size);
1131f5207b7SJohn Levon 
1141f5207b7SJohn Levon 	s->length = size;
1151f5207b7SJohn Levon 	memcpy(s->data, str, size);
1161f5207b7SJohn Levon 	token_type(token) = TOKEN_STRING;
1171f5207b7SJohn Levon 	token->string = s;
1181f5207b7SJohn Levon }
1191f5207b7SJohn Levon 
replace_with_integer(struct token * token,unsigned int val)1201f5207b7SJohn Levon static void replace_with_integer(struct token *token, unsigned int val)
1211f5207b7SJohn Levon {
1221f5207b7SJohn Levon 	char *buf = __alloc_bytes(11);
1231f5207b7SJohn Levon 	sprintf(buf, "%u", val);
1241f5207b7SJohn Levon 	token_type(token) = TOKEN_NUMBER;
1251f5207b7SJohn Levon 	token->number = buf;
1261f5207b7SJohn Levon }
1271f5207b7SJohn Levon 
lookup_macro(struct ident * ident)1281f5207b7SJohn Levon static struct symbol *lookup_macro(struct ident *ident)
1291f5207b7SJohn Levon {
1301f5207b7SJohn Levon 	struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
1311f5207b7SJohn Levon 	if (sym && sym->namespace != NS_MACRO)
1321f5207b7SJohn Levon 		sym = NULL;
1331f5207b7SJohn Levon 	return sym;
1341f5207b7SJohn Levon }
1351f5207b7SJohn Levon 
token_defined(struct token * token)1361f5207b7SJohn Levon static int token_defined(struct token *token)
1371f5207b7SJohn Levon {
1381f5207b7SJohn Levon 	if (token_type(token) == TOKEN_IDENT) {
1391f5207b7SJohn Levon 		struct symbol *sym = lookup_macro(token->ident);
1401f5207b7SJohn Levon 		if (sym) {
1411f5207b7SJohn Levon 			sym->used_in = file_scope;
1421f5207b7SJohn Levon 			return 1;
1431f5207b7SJohn Levon 		}
1441f5207b7SJohn Levon 		return 0;
1451f5207b7SJohn Levon 	}
1461f5207b7SJohn Levon 
1471f5207b7SJohn Levon 	sparse_error(token->pos, "expected preprocessor identifier");
1481f5207b7SJohn Levon 	return 0;
1491f5207b7SJohn Levon }
1501f5207b7SJohn Levon 
replace_with_bool(struct token * token,bool val)151c85f09ccSJohn Levon static void replace_with_bool(struct token *token, bool val)
1521f5207b7SJohn Levon {
1531f5207b7SJohn Levon 	static const char *string[] = { "0", "1" };
1541f5207b7SJohn Levon 
1551f5207b7SJohn Levon 	token_type(token) = TOKEN_NUMBER;
156c85f09ccSJohn Levon 	token->number = string[val];
157c85f09ccSJohn Levon }
158c85f09ccSJohn Levon 
replace_with_defined(struct token * token)159c85f09ccSJohn Levon static void replace_with_defined(struct token *token)
160c85f09ccSJohn Levon {
161c85f09ccSJohn Levon 	replace_with_bool(token, token_defined(token));
162c85f09ccSJohn Levon }
163c85f09ccSJohn Levon 
replace_with_has_builtin(struct token * token)164c85f09ccSJohn Levon static void replace_with_has_builtin(struct token *token)
165c85f09ccSJohn Levon {
166c85f09ccSJohn Levon 	struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL);
167c85f09ccSJohn Levon 	replace_with_bool(token, sym && sym->builtin);
168c85f09ccSJohn Levon }
169c85f09ccSJohn Levon 
replace_with_has_attribute(struct token * token)170c85f09ccSJohn Levon static void replace_with_has_attribute(struct token *token)
171c85f09ccSJohn Levon {
172c85f09ccSJohn Levon 	struct symbol *sym = lookup_symbol(token->ident, NS_KEYWORD);
173c85f09ccSJohn Levon 	replace_with_bool(token, sym && sym->op && sym->op->attribute);
174c85f09ccSJohn Levon }
175c85f09ccSJohn Levon 
expand_line(struct token * token)176c85f09ccSJohn Levon static void expand_line(struct token *token)
177c85f09ccSJohn Levon {
178c85f09ccSJohn Levon 	replace_with_integer(token, token->pos.line);
179c85f09ccSJohn Levon }
180c85f09ccSJohn Levon 
expand_file(struct token * token)181c85f09ccSJohn Levon static void expand_file(struct token *token)
182c85f09ccSJohn Levon {
183c85f09ccSJohn Levon 	replace_with_string(token, stream_name(token->pos.stream));
184c85f09ccSJohn Levon }
185c85f09ccSJohn Levon 
expand_basefile(struct token * token)186c85f09ccSJohn Levon static void expand_basefile(struct token *token)
187c85f09ccSJohn Levon {
188c85f09ccSJohn Levon 	replace_with_string(token, base_filename);
189c85f09ccSJohn Levon }
190c85f09ccSJohn Levon 
191c85f09ccSJohn Levon static time_t t = 0;
expand_date(struct token * token)192c85f09ccSJohn Levon static void expand_date(struct token *token)
193c85f09ccSJohn Levon {
194c85f09ccSJohn Levon 	static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
195c85f09ccSJohn Levon 
196c85f09ccSJohn Levon 	if (!t)
197c85f09ccSJohn Levon 		time(&t);
198c85f09ccSJohn Levon 	strftime(buffer, 12, "%b %e %Y", localtime(&t));
199c85f09ccSJohn Levon 	replace_with_string(token, buffer);
200c85f09ccSJohn Levon }
201c85f09ccSJohn Levon 
expand_time(struct token * token)202c85f09ccSJohn Levon static void expand_time(struct token *token)
203c85f09ccSJohn Levon {
204c85f09ccSJohn Levon 	static char buffer[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
205c85f09ccSJohn Levon 
206c85f09ccSJohn Levon 	if (!t)
207c85f09ccSJohn Levon 		time(&t);
208c85f09ccSJohn Levon 	strftime(buffer, 9, "%T", localtime(&t));
209c85f09ccSJohn Levon 	replace_with_string(token, buffer);
210c85f09ccSJohn Levon }
211c85f09ccSJohn Levon 
expand_counter(struct token * token)212c85f09ccSJohn Levon static void expand_counter(struct token *token)
213c85f09ccSJohn Levon {
214c85f09ccSJohn Levon 	replace_with_integer(token, counter_macro++);
215c85f09ccSJohn Levon }
216c85f09ccSJohn Levon 
expand_include_level(struct token * token)217c85f09ccSJohn Levon static void expand_include_level(struct token *token)
218c85f09ccSJohn Levon {
219c85f09ccSJohn Levon 	replace_with_integer(token, include_level - 1);
2201f5207b7SJohn Levon }
2211f5207b7SJohn Levon 
expand_one_symbol(struct token ** list)2221f5207b7SJohn Levon static int expand_one_symbol(struct token **list)
2231f5207b7SJohn Levon {
2241f5207b7SJohn Levon 	struct token *token = *list;
2251f5207b7SJohn Levon 	struct symbol *sym;
2261f5207b7SJohn Levon 
2271f5207b7SJohn Levon 	if (token->pos.noexpand)
2281f5207b7SJohn Levon 		return 1;
2291f5207b7SJohn Levon 
2301f5207b7SJohn Levon 	sym = lookup_macro(token->ident);
231c85f09ccSJohn Levon 	if (!sym)
232c85f09ccSJohn Levon 		return 1;
233c85f09ccSJohn Levon 	store_macro_pos(token);
234c85f09ccSJohn Levon 	if (sym->expander) {
235c85f09ccSJohn Levon 		sym->expander(token);
236c85f09ccSJohn Levon 		return 1;
237c85f09ccSJohn Levon 	} else {
2381f5207b7SJohn Levon 		sym->used_in = file_scope;
2391f5207b7SJohn Levon 		return expand(list, sym);
2401f5207b7SJohn Levon 	}
2411f5207b7SJohn Levon }
2421f5207b7SJohn Levon 
scan_next(struct token ** where)2431f5207b7SJohn Levon static inline struct token *scan_next(struct token **where)
2441f5207b7SJohn Levon {
2451f5207b7SJohn Levon 	struct token *token = *where;
2461f5207b7SJohn Levon 	if (token_type(token) != TOKEN_UNTAINT)
2471f5207b7SJohn Levon 		return token;
2481f5207b7SJohn Levon 	do {
2491f5207b7SJohn Levon 		token->ident->tainted = 0;
2501f5207b7SJohn Levon 		token = token->next;
2511f5207b7SJohn Levon 	} while (token_type(token) == TOKEN_UNTAINT);
2521f5207b7SJohn Levon 	*where = token;
2531f5207b7SJohn Levon 	return token;
2541f5207b7SJohn Levon }
2551f5207b7SJohn Levon 
expand_list(struct token ** list)2561f5207b7SJohn Levon static void expand_list(struct token **list)
2571f5207b7SJohn Levon {
2581f5207b7SJohn Levon 	struct token *next;
2591f5207b7SJohn Levon 	while (!eof_token(next = scan_next(list))) {
2601f5207b7SJohn Levon 		if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
2611f5207b7SJohn Levon 			list = &next->next;
2621f5207b7SJohn Levon 	}
2631f5207b7SJohn Levon }
2641f5207b7SJohn Levon 
2651f5207b7SJohn Levon static void preprocessor_line(struct stream *stream, struct token **line);
2661f5207b7SJohn Levon 
collect_arg(struct token * prev,int vararg,struct position * pos,int count)2671f5207b7SJohn Levon static struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
2681f5207b7SJohn Levon {
2691f5207b7SJohn Levon 	struct stream *stream = input_streams + prev->pos.stream;
2701f5207b7SJohn Levon 	struct token **p = &prev->next;
2711f5207b7SJohn Levon 	struct token *next;
2721f5207b7SJohn Levon 	int nesting = 0;
2731f5207b7SJohn Levon 
2741f5207b7SJohn Levon 	while (!eof_token(next = scan_next(p))) {
2751f5207b7SJohn Levon 		if (next->pos.newline && match_op(next, '#')) {
2761f5207b7SJohn Levon 			if (!next->pos.noexpand) {
2771f5207b7SJohn Levon 				sparse_error(next->pos,
2781f5207b7SJohn Levon 					     "directive in argument list");
2791f5207b7SJohn Levon 				preprocessor_line(stream, p);
2801f5207b7SJohn Levon 				__free_token(next);	/* Free the '#' token */
2811f5207b7SJohn Levon 				continue;
2821f5207b7SJohn Levon 			}
2831f5207b7SJohn Levon 		}
2841f5207b7SJohn Levon 		switch (token_type(next)) {
2851f5207b7SJohn Levon 		case TOKEN_STREAMEND:
2861f5207b7SJohn Levon 		case TOKEN_STREAMBEGIN:
2871f5207b7SJohn Levon 			*p = &eof_token_entry;
2881f5207b7SJohn Levon 			return next;
2891f5207b7SJohn Levon 		case TOKEN_STRING:
2901f5207b7SJohn Levon 		case TOKEN_WIDE_STRING:
2911f5207b7SJohn Levon 			if (count > 1)
2921f5207b7SJohn Levon 				next->string->immutable = 1;
2931f5207b7SJohn Levon 			break;
2941f5207b7SJohn Levon 		}
2951f5207b7SJohn Levon 		if (false_nesting) {
2961f5207b7SJohn Levon 			*p = next->next;
2971f5207b7SJohn Levon 			__free_token(next);
2981f5207b7SJohn Levon 			continue;
2991f5207b7SJohn Levon 		}
3001f5207b7SJohn Levon 		if (match_op(next, '(')) {
3011f5207b7SJohn Levon 			nesting++;
3021f5207b7SJohn Levon 		} else if (match_op(next, ')')) {
3031f5207b7SJohn Levon 			if (!nesting--)
3041f5207b7SJohn Levon 				break;
3051f5207b7SJohn Levon 		} else if (match_op(next, ',') && !nesting && !vararg) {
3061f5207b7SJohn Levon 			break;
3071f5207b7SJohn Levon 		}
3081f5207b7SJohn Levon 		next->pos.stream = pos->stream;
3091f5207b7SJohn Levon 		next->pos.line = pos->line;
3101f5207b7SJohn Levon 		next->pos.pos = pos->pos;
3111f5207b7SJohn Levon 		p = &next->next;
3121f5207b7SJohn Levon 	}
3131f5207b7SJohn Levon 	*p = &eof_token_entry;
3141f5207b7SJohn Levon 	return next;
3151f5207b7SJohn Levon }
3161f5207b7SJohn Levon 
3171f5207b7SJohn Levon /*
3181f5207b7SJohn Levon  * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
3191f5207b7SJohn Levon  */
3201f5207b7SJohn Levon 
3211f5207b7SJohn Levon struct arg {
3221f5207b7SJohn Levon 	struct token *arg;
3231f5207b7SJohn Levon 	struct token *expanded;
3241f5207b7SJohn Levon 	struct token *str;
3251f5207b7SJohn Levon 	int n_normal;
3261f5207b7SJohn Levon 	int n_quoted;
3271f5207b7SJohn Levon 	int n_str;
3281f5207b7SJohn Levon };
3291f5207b7SJohn Levon 
collect_arguments(struct token * start,struct token * arglist,struct arg * args,struct token * what)3301f5207b7SJohn Levon static int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
3311f5207b7SJohn Levon {
3321f5207b7SJohn Levon 	int wanted = arglist->count.normal;
3331f5207b7SJohn Levon 	struct token *next = NULL;
3341f5207b7SJohn Levon 	int count = 0;
3351f5207b7SJohn Levon 
3361f5207b7SJohn Levon 	arglist = arglist->next;	/* skip counter */
3371f5207b7SJohn Levon 
3381f5207b7SJohn Levon 	if (!wanted) {
3391f5207b7SJohn Levon 		next = collect_arg(start, 0, &what->pos, 0);
3401f5207b7SJohn Levon 		if (eof_token(next))
3411f5207b7SJohn Levon 			goto Eclosing;
3421f5207b7SJohn Levon 		if (!eof_token(start->next) || !match_op(next, ')')) {
3431f5207b7SJohn Levon 			count++;
3441f5207b7SJohn Levon 			goto Emany;
3451f5207b7SJohn Levon 		}
3461f5207b7SJohn Levon 	} else {
3471f5207b7SJohn Levon 		for (count = 0; count < wanted; count++) {
3481f5207b7SJohn Levon 			struct argcount *p = &arglist->next->count;
3491f5207b7SJohn Levon 			next = collect_arg(start, p->vararg, &what->pos, p->normal);
3501f5207b7SJohn Levon 			if (eof_token(next))
3511f5207b7SJohn Levon 				goto Eclosing;
3521f5207b7SJohn Levon 			if (p->vararg && wanted == 1 && eof_token(start->next))
3531f5207b7SJohn Levon 				break;
3541f5207b7SJohn Levon 			arglist = arglist->next->next;
3551f5207b7SJohn Levon 			args[count].arg = start->next;
3561f5207b7SJohn Levon 			args[count].n_normal = p->normal;
3571f5207b7SJohn Levon 			args[count].n_quoted = p->quoted;
3581f5207b7SJohn Levon 			args[count].n_str = p->str;
3591f5207b7SJohn Levon 			if (match_op(next, ')')) {
3601f5207b7SJohn Levon 				count++;
3611f5207b7SJohn Levon 				break;
3621f5207b7SJohn Levon 			}
3631f5207b7SJohn Levon 			start = next;
3641f5207b7SJohn Levon 		}
3651f5207b7SJohn Levon 		if (count == wanted && !match_op(next, ')'))
3661f5207b7SJohn Levon 			goto Emany;
3671f5207b7SJohn Levon 		if (count == wanted - 1) {
3681f5207b7SJohn Levon 			struct argcount *p = &arglist->next->count;
3691f5207b7SJohn Levon 			if (!p->vararg)
3701f5207b7SJohn Levon 				goto Efew;
3711f5207b7SJohn Levon 			args[count].arg = NULL;
3721f5207b7SJohn Levon 			args[count].n_normal = p->normal;
3731f5207b7SJohn Levon 			args[count].n_quoted = p->quoted;
3741f5207b7SJohn Levon 			args[count].n_str = p->str;
3751f5207b7SJohn Levon 		}
3761f5207b7SJohn Levon 		if (count < wanted - 1)
3771f5207b7SJohn Levon 			goto Efew;
3781f5207b7SJohn Levon 	}
3791f5207b7SJohn Levon 	what->next = next->next;
3801f5207b7SJohn Levon 	return 1;
3811f5207b7SJohn Levon 
3821f5207b7SJohn Levon Efew:
3831f5207b7SJohn Levon 	sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
3841f5207b7SJohn Levon 		show_token(what), wanted, count);
3851f5207b7SJohn Levon 	goto out;
3861f5207b7SJohn Levon Emany:
3871f5207b7SJohn Levon 	while (match_op(next, ',')) {
3881f5207b7SJohn Levon 		next = collect_arg(next, 0, &what->pos, 0);
3891f5207b7SJohn Levon 		count++;
3901f5207b7SJohn Levon 	}
3911f5207b7SJohn Levon 	if (eof_token(next))
3921f5207b7SJohn Levon 		goto Eclosing;
3931f5207b7SJohn Levon 	sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
3941f5207b7SJohn Levon 		show_token(what), count, wanted);
3951f5207b7SJohn Levon 	goto out;
3961f5207b7SJohn Levon Eclosing:
3971f5207b7SJohn Levon 	sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
3981f5207b7SJohn Levon 		show_token(what));
3991f5207b7SJohn Levon out:
4001f5207b7SJohn Levon 	what->next = next->next;
4011f5207b7SJohn Levon 	return 0;
4021f5207b7SJohn Levon }
4031f5207b7SJohn Levon 
dup_list(struct token * list)4041f5207b7SJohn Levon static struct token *dup_list(struct token *list)
4051f5207b7SJohn Levon {
4061f5207b7SJohn Levon 	struct token *res = NULL;
4071f5207b7SJohn Levon 	struct token **p = &res;
4081f5207b7SJohn Levon 
4091f5207b7SJohn Levon 	while (!eof_token(list)) {
4101f5207b7SJohn Levon 		struct token *newtok = __alloc_token(0);
4111f5207b7SJohn Levon 		*newtok = *list;
4121f5207b7SJohn Levon 		*p = newtok;
4131f5207b7SJohn Levon 		p = &newtok->next;
4141f5207b7SJohn Levon 		list = list->next;
4151f5207b7SJohn Levon 	}
4161f5207b7SJohn Levon 	return res;
4171f5207b7SJohn Levon }
4181f5207b7SJohn Levon 
show_token_sequence(struct token * token,int quote)4191f5207b7SJohn Levon static const char *show_token_sequence(struct token *token, int quote)
4201f5207b7SJohn Levon {
4211f5207b7SJohn Levon 	static char buffer[MAX_STRING];
4221f5207b7SJohn Levon 	char *ptr = buffer;
4231f5207b7SJohn Levon 	int whitespace = 0;
4241f5207b7SJohn Levon 
4251f5207b7SJohn Levon 	if (!token && !quote)
4261f5207b7SJohn Levon 		return "<none>";
4271f5207b7SJohn Levon 	while (!eof_token(token)) {
4281f5207b7SJohn Levon 		const char *val = quote ? quote_token(token) : show_token(token);
4291f5207b7SJohn Levon 		int len = strlen(val);
4301f5207b7SJohn Levon 
4311f5207b7SJohn Levon 		if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
4321f5207b7SJohn Levon 			sparse_error(token->pos, "too long token expansion");
4331f5207b7SJohn Levon 			break;
4341f5207b7SJohn Levon 		}
4351f5207b7SJohn Levon 
4361f5207b7SJohn Levon 		if (whitespace)
4371f5207b7SJohn Levon 			*ptr++ = ' ';
4381f5207b7SJohn Levon 		memcpy(ptr, val, len);
4391f5207b7SJohn Levon 		ptr += len;
4401f5207b7SJohn Levon 		token = token->next;
4411f5207b7SJohn Levon 		whitespace = token->pos.whitespace;
4421f5207b7SJohn Levon 	}
4431f5207b7SJohn Levon 	*ptr = 0;
4441f5207b7SJohn Levon 	return buffer;
4451f5207b7SJohn Levon }
4461f5207b7SJohn Levon 
stringify(struct token * arg)4471f5207b7SJohn Levon static struct token *stringify(struct token *arg)
4481f5207b7SJohn Levon {
4491f5207b7SJohn Levon 	const char *s = show_token_sequence(arg, 1);
4501f5207b7SJohn Levon 	int size = strlen(s)+1;
4511f5207b7SJohn Levon 	struct token *token = __alloc_token(0);
4521f5207b7SJohn Levon 	struct string *string = __alloc_string(size);
4531f5207b7SJohn Levon 
4541f5207b7SJohn Levon 	memcpy(string->data, s, size);
4551f5207b7SJohn Levon 	string->length = size;
4561f5207b7SJohn Levon 	token->pos = arg->pos;
4571f5207b7SJohn Levon 	token_type(token) = TOKEN_STRING;
4581f5207b7SJohn Levon 	token->string = string;
4591f5207b7SJohn Levon 	token->next = &eof_token_entry;
4601f5207b7SJohn Levon 	return token;
4611f5207b7SJohn Levon }
4621f5207b7SJohn Levon 
expand_arguments(int count,struct arg * args)4631f5207b7SJohn Levon static void expand_arguments(int count, struct arg *args)
4641f5207b7SJohn Levon {
4651f5207b7SJohn Levon 	int i;
4661f5207b7SJohn Levon 	for (i = 0; i < count; i++) {
4671f5207b7SJohn Levon 		struct token *arg = args[i].arg;
4681f5207b7SJohn Levon 		if (!arg)
4691f5207b7SJohn Levon 			arg = &eof_token_entry;
4701f5207b7SJohn Levon 		if (args[i].n_str)
4711f5207b7SJohn Levon 			args[i].str = stringify(arg);
4721f5207b7SJohn Levon 		if (args[i].n_normal) {
4731f5207b7SJohn Levon 			if (!args[i].n_quoted) {
4741f5207b7SJohn Levon 				args[i].expanded = arg;
4751f5207b7SJohn Levon 				args[i].arg = NULL;
4761f5207b7SJohn Levon 			} else if (eof_token(arg)) {
4771f5207b7SJohn Levon 				args[i].expanded = arg;
4781f5207b7SJohn Levon 			} else {
4791f5207b7SJohn Levon 				args[i].expanded = dup_list(arg);
4801f5207b7SJohn Levon 			}
4811f5207b7SJohn Levon 			expand_list(&args[i].expanded);
4821f5207b7SJohn Levon 		}
4831f5207b7SJohn Levon 	}
4841f5207b7SJohn Levon }
4851f5207b7SJohn Levon 
4861f5207b7SJohn Levon /*
4871f5207b7SJohn Levon  * Possibly valid combinations:
4881f5207b7SJohn Levon  *  - ident + ident -> ident
4891f5207b7SJohn Levon  *  - ident + number -> ident unless number contains '.', '+' or '-'.
4901f5207b7SJohn Levon  *  - 'L' + char constant -> wide char constant
4911f5207b7SJohn Levon  *  - 'L' + string literal -> wide string literal
4921f5207b7SJohn Levon  *  - number + number -> number
4931f5207b7SJohn Levon  *  - number + ident -> number
4941f5207b7SJohn Levon  *  - number + '.' -> number
4951f5207b7SJohn Levon  *  - number + '+' or '-' -> number, if number used to end on [eEpP].
4961f5207b7SJohn Levon  *  - '.' + number -> number, if number used to start with a digit.
4971f5207b7SJohn Levon  *  - special + special -> either special or an error.
4981f5207b7SJohn Levon  */
combine(struct token * left,struct token * right,char * p)4991f5207b7SJohn Levon static enum token_type combine(struct token *left, struct token *right, char *p)
5001f5207b7SJohn Levon {
5011f5207b7SJohn Levon 	int len;
5021f5207b7SJohn Levon 	enum token_type t1 = token_type(left), t2 = token_type(right);
5031f5207b7SJohn Levon 
5041f5207b7SJohn Levon 	if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
5051f5207b7SJohn Levon 		return TOKEN_ERROR;
5061f5207b7SJohn Levon 
5071f5207b7SJohn Levon 	if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
5081f5207b7SJohn Levon 		if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
5091f5207b7SJohn Levon 			return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
5101f5207b7SJohn Levon 		if (t2 == TOKEN_STRING)
5111f5207b7SJohn Levon 			return TOKEN_WIDE_STRING;
5121f5207b7SJohn Levon 	}
5131f5207b7SJohn Levon 
5141f5207b7SJohn Levon 	if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
5151f5207b7SJohn Levon 		return TOKEN_ERROR;
5161f5207b7SJohn Levon 
5171f5207b7SJohn Levon 	strcpy(p, show_token(left));
5181f5207b7SJohn Levon 	strcat(p, show_token(right));
5191f5207b7SJohn Levon 	len = strlen(p);
5201f5207b7SJohn Levon 
5211f5207b7SJohn Levon 	if (len >= 256)
5221f5207b7SJohn Levon 		return TOKEN_ERROR;
5231f5207b7SJohn Levon 
5241f5207b7SJohn Levon 	if (t1 == TOKEN_IDENT) {
5251f5207b7SJohn Levon 		if (t2 == TOKEN_SPECIAL)
5261f5207b7SJohn Levon 			return TOKEN_ERROR;
5271f5207b7SJohn Levon 		if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
5281f5207b7SJohn Levon 			return TOKEN_ERROR;
5291f5207b7SJohn Levon 		return TOKEN_IDENT;
5301f5207b7SJohn Levon 	}
5311f5207b7SJohn Levon 
5321f5207b7SJohn Levon 	if (t1 == TOKEN_NUMBER) {
5331f5207b7SJohn Levon 		if (t2 == TOKEN_SPECIAL) {
5341f5207b7SJohn Levon 			switch (right->special) {
5351f5207b7SJohn Levon 			case '.':
5361f5207b7SJohn Levon 				break;
5371f5207b7SJohn Levon 			case '+': case '-':
5381f5207b7SJohn Levon 				if (strchr("eEpP", p[len - 2]))
5391f5207b7SJohn Levon 					break;
5401f5207b7SJohn Levon 			default:
5411f5207b7SJohn Levon 				return TOKEN_ERROR;
5421f5207b7SJohn Levon 			}
5431f5207b7SJohn Levon 		}
5441f5207b7SJohn Levon 		return TOKEN_NUMBER;
5451f5207b7SJohn Levon 	}
5461f5207b7SJohn Levon 
5471f5207b7SJohn Levon 	if (p[0] == '.' && isdigit((unsigned char)p[1]))
5481f5207b7SJohn Levon 		return TOKEN_NUMBER;
5491f5207b7SJohn Levon 
5501f5207b7SJohn Levon 	return TOKEN_SPECIAL;
5511f5207b7SJohn Levon }
5521f5207b7SJohn Levon 
merge(struct token * left,struct token * right)5531f5207b7SJohn Levon static int merge(struct token *left, struct token *right)
5541f5207b7SJohn Levon {
5551f5207b7SJohn Levon 	static char buffer[512];
5561f5207b7SJohn Levon 	enum token_type res = combine(left, right, buffer);
5571f5207b7SJohn Levon 	int n;
5581f5207b7SJohn Levon 
5591f5207b7SJohn Levon 	switch (res) {
5601f5207b7SJohn Levon 	case TOKEN_IDENT:
5611f5207b7SJohn Levon 		left->ident = built_in_ident(buffer);
5621f5207b7SJohn Levon 		left->pos.noexpand = 0;
5631f5207b7SJohn Levon 		return 1;
5641f5207b7SJohn Levon 
565c85f09ccSJohn Levon 	case TOKEN_NUMBER:
5661f5207b7SJohn Levon 		token_type(left) = TOKEN_NUMBER;	/* could be . + num */
567c85f09ccSJohn Levon 		left->number = xstrdup(buffer);
5681f5207b7SJohn Levon 		return 1;
5691f5207b7SJohn Levon 
5701f5207b7SJohn Levon 	case TOKEN_SPECIAL:
5711f5207b7SJohn Levon 		if (buffer[2] && buffer[3])
5721f5207b7SJohn Levon 			break;
5731f5207b7SJohn Levon 		for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
5741f5207b7SJohn Levon 			if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
5751f5207b7SJohn Levon 				left->special = n;
5761f5207b7SJohn Levon 				return 1;
5771f5207b7SJohn Levon 			}
5781f5207b7SJohn Levon 		}
5791f5207b7SJohn Levon 		break;
5801f5207b7SJohn Levon 
5811f5207b7SJohn Levon 	case TOKEN_WIDE_CHAR:
5821f5207b7SJohn Levon 	case TOKEN_WIDE_STRING:
5831f5207b7SJohn Levon 		token_type(left) = res;
5841f5207b7SJohn Levon 		left->pos.noexpand = 0;
5851f5207b7SJohn Levon 		left->string = right->string;
5861f5207b7SJohn Levon 		return 1;
5871f5207b7SJohn Levon 
5881f5207b7SJohn Levon 	case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
5891f5207b7SJohn Levon 		token_type(left) = res;
5901f5207b7SJohn Levon 		left->pos.noexpand = 0;
5911f5207b7SJohn Levon 		memcpy(left->embedded, right->embedded, 4);
5921f5207b7SJohn Levon 		return 1;
5931f5207b7SJohn Levon 
5941f5207b7SJohn Levon 	default:
5951f5207b7SJohn Levon 		;
5961f5207b7SJohn Levon 	}
5971f5207b7SJohn Levon 	sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
5981f5207b7SJohn Levon 	return 0;
5991f5207b7SJohn Levon }
6001f5207b7SJohn Levon 
dup_token(struct token * token,struct position * streampos)6011f5207b7SJohn Levon static struct token *dup_token(struct token *token, struct position *streampos)
6021f5207b7SJohn Levon {
6031f5207b7SJohn Levon 	struct token *alloc = alloc_token(streampos);
6041f5207b7SJohn Levon 	token_type(alloc) = token_type(token);
6051f5207b7SJohn Levon 	alloc->pos.newline = token->pos.newline;
6061f5207b7SJohn Levon 	alloc->pos.whitespace = token->pos.whitespace;
6071f5207b7SJohn Levon 	alloc->number = token->number;
6081f5207b7SJohn Levon 	alloc->pos.noexpand = token->pos.noexpand;
6091f5207b7SJohn Levon 	return alloc;
6101f5207b7SJohn Levon }
6111f5207b7SJohn Levon 
copy(struct token ** where,struct token * list,int * count)6121f5207b7SJohn Levon static struct token **copy(struct token **where, struct token *list, int *count)
6131f5207b7SJohn Levon {
6141f5207b7SJohn Levon 	int need_copy = --*count;
6151f5207b7SJohn Levon 	while (!eof_token(list)) {
6161f5207b7SJohn Levon 		struct token *token;
6171f5207b7SJohn Levon 		if (need_copy)
6181f5207b7SJohn Levon 			token = dup_token(list, &list->pos);
6191f5207b7SJohn Levon 		else
6201f5207b7SJohn Levon 			token = list;
6211f5207b7SJohn Levon 		if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
6221f5207b7SJohn Levon 			token->pos.noexpand = 1;
6231f5207b7SJohn Levon 		*where = token;
6241f5207b7SJohn Levon 		where = &token->next;
6251f5207b7SJohn Levon 		list = list->next;
6261f5207b7SJohn Levon 	}
6271f5207b7SJohn Levon 	*where = &eof_token_entry;
6281f5207b7SJohn Levon 	return where;
6291f5207b7SJohn Levon }
6301f5207b7SJohn Levon 
handle_kludge(struct token ** p,struct arg * args)6311f5207b7SJohn Levon static int handle_kludge(struct token **p, struct arg *args)
6321f5207b7SJohn Levon {
6331f5207b7SJohn Levon 	struct token *t = (*p)->next->next;
6341f5207b7SJohn Levon 	while (1) {
6351f5207b7SJohn Levon 		struct arg *v = &args[t->argnum];
6361f5207b7SJohn Levon 		if (token_type(t->next) != TOKEN_CONCAT) {
6371f5207b7SJohn Levon 			if (v->arg) {
6381f5207b7SJohn Levon 				/* ignore the first ## */
6391f5207b7SJohn Levon 				*p = (*p)->next;
6401f5207b7SJohn Levon 				return 0;
6411f5207b7SJohn Levon 			}
6421f5207b7SJohn Levon 			/* skip the entire thing */
6431f5207b7SJohn Levon 			*p = t;
6441f5207b7SJohn Levon 			return 1;
6451f5207b7SJohn Levon 		}
6461f5207b7SJohn Levon 		if (v->arg && !eof_token(v->arg))
6471f5207b7SJohn Levon 			return 0; /* no magic */
6481f5207b7SJohn Levon 		t = t->next->next;
6491f5207b7SJohn Levon 	}
6501f5207b7SJohn Levon }
6511f5207b7SJohn Levon 
substitute(struct token ** list,struct token * body,struct arg * args)6521f5207b7SJohn Levon static struct token **substitute(struct token **list, struct token *body, struct arg *args)
6531f5207b7SJohn Levon {
6541f5207b7SJohn Levon 	struct position *base_pos = &(*list)->pos;
6551f5207b7SJohn Levon 	int *count;
6561f5207b7SJohn Levon 	enum {Normal, Placeholder, Concat} state = Normal;
6571f5207b7SJohn Levon 
6581f5207b7SJohn Levon 	for (; !eof_token(body); body = body->next) {
6591f5207b7SJohn Levon 		struct token *added, *arg;
6601f5207b7SJohn Levon 		struct token **tail;
6611f5207b7SJohn Levon 		struct token *t;
6621f5207b7SJohn Levon 
6631f5207b7SJohn Levon 		switch (token_type(body)) {
6641f5207b7SJohn Levon 		case TOKEN_GNU_KLUDGE:
6651f5207b7SJohn Levon 			/*
6661f5207b7SJohn Levon 			 * GNU kludge: if we had <comma>##<vararg>, behaviour
6671f5207b7SJohn Levon 			 * depends on whether we had enough arguments to have
6681f5207b7SJohn Levon 			 * a vararg.  If we did, ## is just ignored.  Otherwise
6691f5207b7SJohn Levon 			 * both , and ## are ignored.  Worse, there can be
6701f5207b7SJohn Levon 			 * an arbitrary number of ##<arg> in between; if all of
6711f5207b7SJohn Levon 			 * those are empty, we act as if they hadn't been there,
6721f5207b7SJohn Levon 			 * otherwise we act as if the kludge didn't exist.
6731f5207b7SJohn Levon 			 */
6741f5207b7SJohn Levon 			t = body;
6751f5207b7SJohn Levon 			if (handle_kludge(&body, args)) {
6761f5207b7SJohn Levon 				if (state == Concat)
6771f5207b7SJohn Levon 					state = Normal;
6781f5207b7SJohn Levon 				else
6791f5207b7SJohn Levon 					state = Placeholder;
6801f5207b7SJohn Levon 				continue;
6811f5207b7SJohn Levon 			}
6821f5207b7SJohn Levon 			added = dup_token(t, base_pos);
6831f5207b7SJohn Levon 			token_type(added) = TOKEN_SPECIAL;
6841f5207b7SJohn Levon 			tail = &added->next;
6851f5207b7SJohn Levon 			break;
6861f5207b7SJohn Levon 
6871f5207b7SJohn Levon 		case TOKEN_STR_ARGUMENT:
6881f5207b7SJohn Levon 			arg = args[body->argnum].str;
6891f5207b7SJohn Levon 			count = &args[body->argnum].n_str;
6901f5207b7SJohn Levon 			goto copy_arg;
6911f5207b7SJohn Levon 
6921f5207b7SJohn Levon 		case TOKEN_QUOTED_ARGUMENT:
6931f5207b7SJohn Levon 			arg = args[body->argnum].arg;
6941f5207b7SJohn Levon 			count = &args[body->argnum].n_quoted;
6951f5207b7SJohn Levon 			if (!arg || eof_token(arg)) {
6961f5207b7SJohn Levon 				if (state == Concat)
6971f5207b7SJohn Levon 					state = Normal;
6981f5207b7SJohn Levon 				else
6991f5207b7SJohn Levon 					state = Placeholder;
7001f5207b7SJohn Levon 				continue;
7011f5207b7SJohn Levon 			}
7021f5207b7SJohn Levon 			goto copy_arg;
7031f5207b7SJohn Levon 
7041f5207b7SJohn Levon 		case TOKEN_MACRO_ARGUMENT:
7051f5207b7SJohn Levon 			arg = args[body->argnum].expanded;
7061f5207b7SJohn Levon 			count = &args[body->argnum].n_normal;
7071f5207b7SJohn Levon 			if (eof_token(arg)) {
7081f5207b7SJohn Levon 				state = Normal;
7091f5207b7SJohn Levon 				continue;
7101f5207b7SJohn Levon 			}
7111f5207b7SJohn Levon 		copy_arg:
7121f5207b7SJohn Levon 			tail = copy(&added, arg, count);
7131f5207b7SJohn Levon 			added->pos.newline = body->pos.newline;
7141f5207b7SJohn Levon 			added->pos.whitespace = body->pos.whitespace;
7151f5207b7SJohn Levon 			break;
7161f5207b7SJohn Levon 
7171f5207b7SJohn Levon 		case TOKEN_CONCAT:
7181f5207b7SJohn Levon 			if (state == Placeholder)
7191f5207b7SJohn Levon 				state = Normal;
7201f5207b7SJohn Levon 			else
7211f5207b7SJohn Levon 				state = Concat;
7221f5207b7SJohn Levon 			continue;
7231f5207b7SJohn Levon 
7241f5207b7SJohn Levon 		case TOKEN_IDENT:
7251f5207b7SJohn Levon 			added = dup_token(body, base_pos);
7261f5207b7SJohn Levon 			if (added->ident->tainted)
7271f5207b7SJohn Levon 				added->pos.noexpand = 1;
7281f5207b7SJohn Levon 			tail = &added->next;
7291f5207b7SJohn Levon 			break;
7301f5207b7SJohn Levon 
7311f5207b7SJohn Levon 		default:
7321f5207b7SJohn Levon 			added = dup_token(body, base_pos);
7331f5207b7SJohn Levon 			tail = &added->next;
7341f5207b7SJohn Levon 			break;
7351f5207b7SJohn Levon 		}
7361f5207b7SJohn Levon 
7371f5207b7SJohn Levon 		/*
7381f5207b7SJohn Levon 		 * if we got to doing real concatenation, we already have
7391f5207b7SJohn Levon 		 * added something into the list, so containing_token() is OK.
7401f5207b7SJohn Levon 		 */
7411f5207b7SJohn Levon 		if (state == Concat && merge(containing_token(list), added)) {
7421f5207b7SJohn Levon 			*list = added->next;
7431f5207b7SJohn Levon 			if (tail != &added->next)
7441f5207b7SJohn Levon 				list = tail;
7451f5207b7SJohn Levon 		} else {
7461f5207b7SJohn Levon 			*list = added;
7471f5207b7SJohn Levon 			list = tail;
7481f5207b7SJohn Levon 		}
7491f5207b7SJohn Levon 		state = Normal;
7501f5207b7SJohn Levon 	}
7511f5207b7SJohn Levon 	*list = &eof_token_entry;
7521f5207b7SJohn Levon 	return list;
7531f5207b7SJohn Levon }
7541f5207b7SJohn Levon 
expand(struct token ** list,struct symbol * sym)7551f5207b7SJohn Levon static int expand(struct token **list, struct symbol *sym)
7561f5207b7SJohn Levon {
7571f5207b7SJohn Levon 	struct token *last;
7581f5207b7SJohn Levon 	struct token *token = *list;
7591f5207b7SJohn Levon 	struct ident *expanding = token->ident;
7601f5207b7SJohn Levon 	struct token **tail;
7611f5207b7SJohn Levon 	int nargs = sym->arglist ? sym->arglist->count.normal : 0;
7621f5207b7SJohn Levon 	struct arg args[nargs];
7631f5207b7SJohn Levon 
7641f5207b7SJohn Levon 	if (expanding->tainted) {
7651f5207b7SJohn Levon 		token->pos.noexpand = 1;
7661f5207b7SJohn Levon 		return 1;
7671f5207b7SJohn Levon 	}
7681f5207b7SJohn Levon 
7691f5207b7SJohn Levon 	if (sym->arglist) {
7701f5207b7SJohn Levon 		if (!match_op(scan_next(&token->next), '('))
7711f5207b7SJohn Levon 			return 1;
7721f5207b7SJohn Levon 		if (!collect_arguments(token->next, sym->arglist, args, token))
7731f5207b7SJohn Levon 			return 1;
7741f5207b7SJohn Levon 		expand_arguments(nargs, args);
7751f5207b7SJohn Levon 	}
7761f5207b7SJohn Levon 
7771f5207b7SJohn Levon 	expanding->tainted = 1;
7781f5207b7SJohn Levon 
7791f5207b7SJohn Levon 	last = token->next;
7801f5207b7SJohn Levon 	tail = substitute(list, sym->expansion, args);
7811f5207b7SJohn Levon 	/*
7821f5207b7SJohn Levon 	 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
7831f5207b7SJohn Levon 	 * We still can lose the newline flag if the sucker expands to nothing,
7841f5207b7SJohn Levon 	 * but the price of dealing with that is probably too high (we'd need
7851f5207b7SJohn Levon 	 * to collect the flags during scan_next())
7861f5207b7SJohn Levon 	 */
7871f5207b7SJohn Levon 	(*list)->pos.newline = token->pos.newline;
7881f5207b7SJohn Levon 	(*list)->pos.whitespace = token->pos.whitespace;
7891f5207b7SJohn Levon 	*tail = last;
7901f5207b7SJohn Levon 
7911f5207b7SJohn Levon 	return 0;
7921f5207b7SJohn Levon }
7931f5207b7SJohn Levon 
token_name_sequence(struct token * token,int endop,struct token * start)7941f5207b7SJohn Levon static const char *token_name_sequence(struct token *token, int endop, struct token *start)
7951f5207b7SJohn Levon {
7961f5207b7SJohn Levon 	static char buffer[256];
7971f5207b7SJohn Levon 	char *ptr = buffer;
7981f5207b7SJohn Levon 
7991f5207b7SJohn Levon 	while (!eof_token(token) && !match_op(token, endop)) {
8001f5207b7SJohn Levon 		int len;
8011f5207b7SJohn Levon 		const char *val = token->string->data;
8021f5207b7SJohn Levon 		if (token_type(token) != TOKEN_STRING)
8031f5207b7SJohn Levon 			val = show_token(token);
8041f5207b7SJohn Levon 		len = strlen(val);
8051f5207b7SJohn Levon 		memcpy(ptr, val, len);
8061f5207b7SJohn Levon 		ptr += len;
8071f5207b7SJohn Levon 		token = token->next;
8081f5207b7SJohn Levon 	}
8091f5207b7SJohn Levon 	*ptr = 0;
8101f5207b7S