11f5207bJohn Levon/*
21f5207bJohn Levon * Do C preprocessing, based on a token list gathered by
31f5207bJohn Levon * the tokenizer.
41f5207bJohn Levon *
51f5207bJohn Levon * This may not be the smartest preprocessor on the planet.
61f5207bJohn Levon *
71f5207bJohn Levon * Copyright (C) 2003 Transmeta Corp.
81f5207bJohn Levon *               2003-2004 Linus Torvalds
91f5207bJohn Levon *
101f5207bJohn Levon * Permission is hereby granted, free of charge, to any person obtaining a copy
111f5207bJohn Levon * of this software and associated documentation files (the "Software"), to deal
121f5207bJohn Levon * in the Software without restriction, including without limitation the rights
131f5207bJohn Levon * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
141f5207bJohn Levon * copies of the Software, and to permit persons to whom the Software is
151f5207bJohn Levon * furnished to do so, subject to the following conditions:
161f5207bJohn Levon *
171f5207bJohn Levon * The above copyright notice and this permission notice shall be included in
181f5207bJohn Levon * all copies or substantial portions of the Software.
191f5207bJohn Levon *
201f5207bJohn Levon * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
211f5207bJohn Levon * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
221f5207bJohn Levon * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
231f5207bJohn Levon * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
241f5207bJohn Levon * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
251f5207bJohn Levon * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
261f5207bJohn Levon * THE SOFTWARE.
271f5207bJohn Levon */
281f5207bJohn Levon#include <stdio.h>
291f5207bJohn Levon#include <stdlib.h>
301f5207bJohn Levon#include <stdarg.h>
311f5207bJohn Levon#include <stddef.h>
321f5207bJohn Levon#include <string.h>
331f5207bJohn Levon#include <ctype.h>
341f5207bJohn Levon#include <unistd.h>
351f5207bJohn Levon#include <fcntl.h>
361f5207bJohn Levon#include <limits.h>
371f5207bJohn Levon#include <time.h>
381f5207bJohn Levon#include <dirent.h>
391f5207bJohn Levon#include <sys/stat.h>
401f5207bJohn Levon
411f5207bJohn Levon#include "lib.h"
421f5207bJohn Levon#include "allocate.h"
431f5207bJohn Levon#include "parse.h"
441f5207bJohn Levon#include "token.h"
451f5207bJohn Levon#include "symbol.h"
461f5207bJohn Levon#include "expression.h"
471f5207bJohn Levon#include "scope.h"
481f5207bJohn Levon
491f5207bJohn Levonstatic struct ident_list *macros;	// only needed for -dD
501f5207bJohn Levonstatic int false_nesting = 0;
511f5207bJohn Levonstatic int counter_macro = 0;		// __COUNTER__ expansion
52c85f09cJohn Levonstatic int include_level = 0;
531f5207bJohn Levon
541f5207bJohn Levon#define INCLUDEPATHS 300
551f5207bJohn Levonconst char *includepath[INCLUDEPATHS+1] = {
561f5207bJohn Levon	"",
571f5207bJohn Levon	"/usr/include",
581f5207bJohn Levon	"/usr/local/include",
591f5207bJohn Levon	NULL
601f5207bJohn Levon};
611f5207bJohn Levon
621f5207bJohn Levonstatic const char **quote_includepath = includepath;
631f5207bJohn Levonstatic const char **angle_includepath = includepath + 1;
641f5207bJohn Levonstatic const char **isys_includepath   = includepath + 1;
651f5207bJohn Levonstatic const char **sys_includepath   = includepath + 1;
661f5207bJohn Levonstatic const char **dirafter_includepath = includepath + 3;
671f5207bJohn Levon
681f5207bJohn Levon#define dirty_stream(stream)				\
691f5207bJohn Levon	do {						\
701f5207bJohn Levon		if (!stream->dirty) {			\
711f5207bJohn Levon			stream->dirty = 1;		\
721f5207bJohn Levon			if (!stream->ifndef)		\
731f5207bJohn Levon				stream->protect = NULL;	\
741f5207bJohn Levon		}					\
751f5207bJohn Levon	} while(0)
761f5207bJohn Levon
771f5207bJohn Levon#define end_group(stream)					\
781f5207bJohn Levon	do {							\
791f5207bJohn Levon		if (stream->ifndef == stream->top_if) {		\
801f5207bJohn Levon			stream->ifndef = NULL;			\
811f5207bJohn Levon			if (!stream->dirty)			\
821f5207bJohn Levon				stream->protect = NULL;		\
831f5207bJohn Levon			else if (stream->protect)		\
841f5207bJohn Levon				stream->dirty = 0;		\
851f5207bJohn Levon		}						\
861f5207bJohn Levon	} while(0)
871f5207bJohn Levon
881f5207bJohn Levon#define nesting_error(stream)		\
891f5207bJohn Levon	do {				\
901f5207bJohn Levon		stream->dirty = 1;	\
911f5207bJohn Levon		stream->ifndef = NULL;	\
921f5207bJohn Levon		stream->protect = NULL;	\
931f5207bJohn Levon	} while(0)
941f5207bJohn Levon
951f5207bJohn Levonstatic struct token *alloc_token(struct position *pos)
961f5207bJohn Levon{
971f5207bJohn Levon	struct token *token = __alloc_token(0);
981f5207bJohn Levon
991f5207bJohn Levon	token->pos.stream = pos->stream;
1001f5207bJohn Levon	token->pos.line = pos->line;
1011f5207bJohn Levon	token->pos.pos = pos->pos;
1021f5207bJohn Levon	token->pos.whitespace = 1;
1031f5207bJohn Levon	return token;
1041f5207bJohn Levon}
1051f5207bJohn Levon
1061f5207bJohn Levon/* Expand symbol 'sym' at '*list' */
1071f5207bJohn Levonstatic int expand(struct token **, struct symbol *);
1081f5207bJohn Levon
1091f5207bJohn Levonstatic void replace_with_string(struct token *token, const char *str)
1101f5207bJohn Levon{
1111f5207bJohn Levon	int size = strlen(str) + 1;
1121f5207bJohn Levon	struct string *s = __alloc_string(size);
1131f5207bJohn Levon
1141f5207bJohn Levon	s->length = size;
1151f5207bJohn Levon	memcpy(s->data, str, size);
1161f5207bJohn Levon	token_type(token) = TOKEN_STRING;
1171f5207bJohn Levon	token->string = s;
1181f5207bJohn Levon}
1191f5207bJohn Levon
1201f5207bJohn Levonstatic void replace_with_integer(struct token *token, unsigned int val)
1211f5207bJohn Levon{
1221f5207bJohn Levon	char *buf = __alloc_bytes(11);
1231f5207bJohn Levon	sprintf(buf, "%u", val);
1241f5207bJohn Levon	token_type(token) = TOKEN_NUMBER;
1251f5207bJohn Levon	token->number = buf;
1261f5207bJohn Levon}
1271f5207bJohn Levon
1281f5207bJohn Levonstatic struct symbol *lookup_macro(struct ident *ident)
1291f5207bJohn Levon{
1301f5207bJohn Levon	struct symbol *sym = lookup_symbol(ident, NS_MACRO | NS_UNDEF);
1311f5207bJohn Levon	if (sym && sym->namespace != NS_MACRO)
1321f5207bJohn Levon		sym = NULL;
1331f5207bJohn Levon	return sym;
1341f5207bJohn Levon}
1351f5207bJohn Levon
1361f5207bJohn Levonstatic int token_defined(struct token *token)
1371f5207bJohn Levon{
1381f5207bJohn Levon	if (token_type(token) == TOKEN_IDENT) {
1391f5207bJohn Levon		struct symbol *sym = lookup_macro(token->ident);
1401f5207bJohn Levon		if (sym) {
1411f5207bJohn Levon			sym->used_in = file_scope;
1421f5207bJohn Levon			return 1;
1431f5207bJohn Levon		}
1441f5207bJohn Levon		return 0;
1451f5207bJohn Levon	}
1461f5207bJohn Levon
1471f5207bJohn Levon	sparse_error(token->pos, "expected preprocessor identifier");
1481f5207bJohn Levon	return 0;
1491f5207bJohn Levon}
1501f5207bJohn Levon
151c85f09cJohn Levonstatic void replace_with_bool(struct token *token, bool val)
1521f5207bJohn Levon{
1531f5207bJohn Levon	static const char *string[] = { "0", "1" };
1541f5207bJohn Levon
1551f5207bJohn Levon	token_type(token) = TOKEN_NUMBER;
156c85f09cJohn Levon	token->number = string[val];
157c85f09cJohn Levon}
158c85f09cJohn Levon
159c85f09cJohn Levonstatic void replace_with_defined(struct token *token)
160c85f09cJohn Levon{
161c85f09cJohn Levon	replace_with_bool(token, token_defined(token));
162c85f09cJohn Levon}
163c85f09cJohn Levon
164c85f09cJohn Levonstatic void replace_with_has_builtin(struct token *token)
165c85f09cJohn Levon{
166c85f09cJohn Levon	struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL);
167c85f09cJohn Levon	replace_with_bool(token, sym && sym->builtin);
168c85f09cJohn Levon}
169c85f09cJohn Levon
170c85f09cJohn Levonstatic void replace_with_has_attribute(struct token *token)
171c85f09cJohn Levon{
172c85f09cJohn Levon	struct symbol *sym = lookup_symbol(token->ident, NS_KEYWORD);
173c85f09cJohn Levon	replace_with_bool(token, sym && sym->op && sym->op->attribute);
174c85f09cJohn Levon}
175c85f09cJohn Levon
176c85f09cJohn Levonstatic void expand_line(struct token *token)
177c85f09cJohn Levon{
178c85f09cJohn Levon	replace_with_integer(token, token->pos.line);
179c85f09cJohn Levon}
180c85f09cJohn Levon
181c85f09cJohn Levonstatic void expand_file(struct token *token)
182c85f09cJohn Levon{
183c85f09cJohn Levon	replace_with_string(token, stream_name(token->pos.stream));
184c85f09cJohn Levon}
185c85f09cJohn Levon
186c85f09cJohn Levonstatic void expand_basefile(struct token *token)
187c85f09cJohn Levon{
188c85f09cJohn Levon	replace_with_string(token, base_filename);
189c85f09cJohn Levon}
190c85f09cJohn Levon
191c85f09cJohn Levonstatic time_t t = 0;
192c85f09cJohn Levonstatic void expand_date(struct token *token)
193c85f09cJohn Levon{
194c85f09cJohn Levon	static char buffer[12]; /* __DATE__: 3 + ' ' + 2 + ' ' + 4 + '\0' */
195c85f09cJohn Levon
196c85f09cJohn Levon	if (!t)
197c85f09cJohn Levon		time(&t);
198c85f09cJohn Levon	strftime(buffer, 12, "%b %e %Y", localtime(&t));
199c85f09cJohn Levon	replace_with_string(token, buffer);
200c85f09cJohn Levon}
201c85f09cJohn Levon
202c85f09cJohn Levonstatic void expand_time(struct token *token)
203c85f09cJohn Levon{
204c85f09cJohn Levon	static char buffer[9]; /* __TIME__: 2 + ':' + 2 + ':' + 2 + '\0' */
205c85f09cJohn Levon
206c85f09cJohn Levon	if (!t)
207c85f09cJohn Levon		time(&t);
208c85f09cJohn Levon	strftime(buffer, 9, "%T", localtime(&t));
209c85f09cJohn Levon	replace_with_string(token, buffer);
210c85f09cJohn Levon}
211c85f09cJohn Levon
212c85f09cJohn Levonstatic void expand_counter(struct token *token)
213c85f09cJohn Levon{
214c85f09cJohn Levon	replace_with_integer(token, counter_macro++);
215c85f09cJohn Levon}
216c85f09cJohn Levon
217c85f09cJohn Levonstatic void expand_include_level(struct token *token)
218c85f09cJohn Levon{
219c85f09cJohn Levon	replace_with_integer(token, include_level - 1);
2201f5207bJohn Levon}
2211f5207bJohn Levon
2221f5207bJohn Levonstatic int expand_one_symbol(struct token **list)
2231f5207bJohn Levon{
2241f5207bJohn Levon	struct token *token = *list;
2251f5207bJohn Levon	struct symbol *sym;
2261f5207bJohn Levon
2271f5207bJohn Levon	if (token->pos.noexpand)
2281f5207bJohn Levon		return 1;
2291f5207bJohn Levon
2301f5207bJohn Levon	sym = lookup_macro(token->ident);
231c85f09cJohn Levon	if (!sym)
232c85f09cJohn Levon		return 1;
233c85f09cJohn Levon	store_macro_pos(token);
234c85f09cJohn Levon	if (sym->expander) {
235c85f09cJohn Levon		sym->expander(token);
236c85f09cJohn Levon		return 1;
237c85f09cJohn Levon	} else {
2381f5207bJohn Levon		sym->used_in = file_scope;
2391f5207bJohn Levon		return expand(list, sym);
2401f5207bJohn Levon	}
2411f5207bJohn Levon}
2421f5207bJohn Levon
2431f5207bJohn Levonstatic inline struct token *scan_next(struct token **where)
2441f5207bJohn Levon{
2451f5207bJohn Levon	struct token *token = *where;
2461f5207bJohn Levon	if (token_type(token) != TOKEN_UNTAINT)
2471f5207bJohn Levon		return token;
2481f5207bJohn Levon	do {
2491f5207bJohn Levon		token->ident->tainted = 0;
2501f5207bJohn Levon		token = token->next;
2511f5207bJohn Levon	} while (token_type(token) == TOKEN_UNTAINT);
2521f5207bJohn Levon	*where = token;
2531f5207bJohn Levon	return token;
2541f5207bJohn Levon}
2551f5207bJohn Levon
2561f5207bJohn Levonstatic void expand_list(struct token **list)
2571f5207bJohn Levon{
2581f5207bJohn Levon	struct token *next;
2591f5207bJohn Levon	while (!eof_token(next = scan_next(list))) {
2601f5207bJohn Levon		if (token_type(next) != TOKEN_IDENT || expand_one_symbol(list))
2611f5207bJohn Levon			list = &next->next;
2621f5207bJohn Levon	}
2631f5207bJohn Levon}
2641f5207bJohn Levon
2651f5207bJohn Levonstatic void preprocessor_line(struct stream *stream, struct token **line);
2661f5207bJohn Levon
2671f5207bJohn Levonstatic struct token *collect_arg(struct token *prev, int vararg, struct position *pos, int count)
2681f5207bJohn Levon{
2691f5207bJohn Levon	struct stream *stream = input_streams + prev->pos.stream;
2701f5207bJohn Levon	struct token **p = &prev->next;
2711f5207bJohn Levon	struct token *next;
2721f5207bJohn Levon	int nesting = 0;
2731f5207bJohn Levon
2741f5207bJohn Levon	while (!eof_token(next = scan_next(p))) {
2751f5207bJohn Levon		if (next->pos.newline && match_op(next, '#')) {
2761f5207bJohn Levon			if (!next->pos.noexpand) {
2771f5207bJohn Levon				sparse_error(next->pos,
2781f5207bJohn Levon					     "directive in argument list");
2791f5207bJohn Levon				preprocessor_line(stream, p);
2801f5207bJohn Levon				__free_token(next);	/* Free the '#' token */
2811f5207bJohn Levon				continue;
2821f5207bJohn Levon			}
2831f5207bJohn Levon		}
2841f5207bJohn Levon		switch (token_type(next)) {
2851f5207bJohn Levon		case TOKEN_STREAMEND:
2861f5207bJohn Levon		case TOKEN_STREAMBEGIN:
2871f5207bJohn Levon			*p = &eof_token_entry;
2881f5207bJohn Levon			return next;
2891f5207bJohn Levon		case TOKEN_STRING:
2901f5207bJohn Levon		case TOKEN_WIDE_STRING:
2911f5207bJohn Levon			if (count > 1)
2921f5207bJohn Levon				next->string->immutable = 1;
2931f5207bJohn Levon			break;
2941f5207bJohn Levon		}
2951f5207bJohn Levon		if (false_nesting) {
2961f5207bJohn Levon			*p = next->next;
2971f5207bJohn Levon			__free_token(next);
2981f5207bJohn Levon			continue;
2991f5207bJohn Levon		}
3001f5207bJohn Levon		if (match_op(next, '(')) {
3011f5207bJohn Levon			nesting++;
3021f5207bJohn Levon		} else if (match_op(next, ')')) {
3031f5207bJohn Levon			if (!nesting--)
3041f5207bJohn Levon				break;
3051f5207bJohn Levon		} else if (match_op(next, ',') && !nesting && !vararg) {
3061f5207bJohn Levon			break;
3071f5207bJohn Levon		}
3081f5207bJohn Levon		next->pos.stream = pos->stream;
3091f5207bJohn Levon		next->pos.line = pos->line;
3101f5207bJohn Levon		next->pos.pos = pos->pos;
3111f5207bJohn Levon		p = &next->next;
3121f5207bJohn Levon	}
3131f5207bJohn Levon	*p = &eof_token_entry;
3141f5207bJohn Levon	return next;
3151f5207bJohn Levon}
3161f5207bJohn Levon
3171f5207bJohn Levon/*
3181f5207bJohn Levon * We store arglist as <counter> [arg1] <number of uses for arg1> ... eof
3191f5207bJohn Levon */
3201f5207bJohn Levon
3211f5207bJohn Levonstruct arg {
3221f5207bJohn Levon	struct token *arg;
3231f5207bJohn Levon	struct token *expanded;
3241f5207bJohn Levon	struct token *str;
3251f5207bJohn Levon	int n_normal;
3261f5207bJohn Levon	int n_quoted;
3271f5207bJohn Levon	int n_str;
3281f5207bJohn Levon};
3291f5207bJohn Levon
3301f5207bJohn Levonstatic int collect_arguments(struct token *start, struct token *arglist, struct arg *args, struct token *what)
3311f5207bJohn Levon{
3321f5207bJohn Levon	int wanted = arglist->count.normal;
3331f5207bJohn Levon	struct token *next = NULL;
3341f5207bJohn Levon	int count = 0;
3351f5207bJohn Levon
3361f5207bJohn Levon	arglist = arglist->next;	/* skip counter */
3371f5207bJohn Levon
3381f5207bJohn Levon	if (!wanted) {
3391f5207bJohn Levon		next = collect_arg(start, 0, &what->pos, 0);
3401f5207bJohn Levon		if (eof_token(next))
3411f5207bJohn Levon			goto Eclosing;
3421f5207bJohn Levon		if (!eof_token(start->next) || !match_op(next, ')')) {
3431f5207bJohn Levon			count++;
3441f5207bJohn Levon			goto Emany;
3451f5207bJohn Levon		}
3461f5207bJohn Levon	} else {
3471f5207bJohn Levon		for (count = 0; count < wanted; count++) {
3481f5207bJohn Levon			struct argcount *p = &arglist->next->count;
3491f5207bJohn Levon			next = collect_arg(start, p->vararg, &what->pos, p->normal);
3501f5207bJohn Levon			if (eof_token(next))
3511f5207bJohn Levon				goto Eclosing;
3521f5207bJohn Levon			if (p->vararg && wanted == 1 && eof_token(start->next))
3531f5207bJohn Levon				break;
3541f5207bJohn Levon			arglist = arglist->next->next;
3551f5207bJohn Levon			args[count].arg = start->next;
3561f5207bJohn Levon			args[count].n_normal = p->normal;
3571f5207bJohn Levon			args[count].n_quoted = p->quoted;
3581f5207bJohn Levon			args[count].n_str = p->str;
3591f5207bJohn Levon			if (match_op(next, ')')) {
3601f5207bJohn Levon				count++;
3611f5207bJohn Levon				break;
3621f5207bJohn Levon			}
3631f5207bJohn Levon			start = next;
3641f5207bJohn Levon		}
3651f5207bJohn Levon		if (count == wanted && !match_op(next, ')'))
3661f5207bJohn Levon			goto Emany;
3671f5207bJohn Levon		if (count == wanted - 1) {
3681f5207bJohn Levon			struct argcount *p = &arglist->next->count;
3691f5207bJohn Levon			if (!p->vararg)
3701f5207bJohn Levon				goto Efew;
3711f5207bJohn Levon			args[count].arg = NULL;
3721f5207bJohn Levon			args[count].n_normal = p->normal;
3731f5207bJohn Levon			args[count].n_quoted = p->quoted;
3741f5207bJohn Levon			args[count].n_str = p->str;
3751f5207bJohn Levon		}
3761f5207bJohn Levon		if (count < wanted - 1)
3771f5207bJohn Levon			goto Efew;
3781f5207bJohn Levon	}
3791f5207bJohn Levon	what->next = next->next;
3801f5207bJohn Levon	return 1;
3811f5207bJohn Levon
3821f5207bJohn LevonEfew:
3831f5207bJohn Levon	sparse_error(what->pos, "macro \"%s\" requires %d arguments, but only %d given",
3841f5207bJohn Levon		show_token(what), wanted, count);
3851f5207bJohn Levon	goto out;
3861f5207bJohn LevonEmany:
3871f5207bJohn Levon	while (match_op(next, ',')) {
3881f5207bJohn Levon		next = collect_arg(next, 0, &what->pos, 0);
3891f5207bJohn Levon		count++;
3901f5207bJohn Levon	}
3911f5207bJohn Levon	if (eof_token(next))
3921f5207bJohn Levon		goto Eclosing;
3931f5207bJohn Levon	sparse_error(what->pos, "macro \"%s\" passed %d arguments, but takes just %d",
3941f5207bJohn Levon		show_token(what), count, wanted);
3951f5207bJohn Levon	goto out;
3961f5207bJohn LevonEclosing:
3971f5207bJohn Levon	sparse_error(what->pos, "unterminated argument list invoking macro \"%s\"",
3981f5207bJohn Levon		show_token(what));
3991f5207bJohn Levonout:
4001f5207bJohn Levon	what->next = next->next;
4011f5207bJohn Levon	return 0;
4021f5207bJohn Levon}
4031f5207bJohn Levon
4041f5207bJohn Levonstatic struct token *dup_list(struct token *list)
4051f5207bJohn Levon{
4061f5207bJohn Levon	struct token *res = NULL;
4071f5207bJohn Levon	struct token **p = &res;
4081f5207bJohn Levon
4091f5207bJohn Levon	while (!eof_token(list)) {
4101f5207bJohn Levon		struct token *newtok = __alloc_token(0);
4111f5207bJohn Levon		*newtok = *list;
4121f5207bJohn Levon		*p = newtok;
4131f5207bJohn Levon		p = &newtok->next;
4141f5207bJohn Levon		list = list->next;
4151f5207bJohn Levon	}
4161f5207bJohn Levon	return res;
4171f5207bJohn Levon}
4181f5207bJohn Levon
4191f5207bJohn Levonstatic const char *show_token_sequence(struct token *token, int quote)
4201f5207bJohn Levon{
4211f5207bJohn Levon	static char buffer[MAX_STRING];
4221f5207bJohn Levon	char *ptr = buffer;
4231f5207bJohn Levon	int whitespace = 0;
4241f5207bJohn Levon
4251f5207bJohn Levon	if (!token && !quote)
4261f5207bJohn Levon		return "<none>";
4271f5207bJohn Levon	while (!eof_token(token)) {
4281f5207bJohn Levon		const char *val = quote ? quote_token(token) : show_token(token);
4291f5207bJohn Levon		int len = strlen(val);
4301f5207bJohn Levon
4311f5207bJohn Levon		if (ptr + whitespace + len >= buffer + sizeof(buffer)) {
4321f5207bJohn Levon			sparse_error(token->pos, "too long token expansion");
4331f5207bJohn Levon			break;
4341f5207bJohn Levon		}
4351f5207bJohn Levon
4361f5207bJohn Levon		if (whitespace)
4371f5207bJohn Levon			*ptr++ = ' ';
4381f5207bJohn Levon		memcpy(ptr, val, len);
4391f5207bJohn Levon		ptr += len;
4401f5207bJohn Levon		token = token->next;
4411f5207bJohn Levon		whitespace = token->pos.whitespace;
4421f5207bJohn Levon	}
4431f5207bJohn Levon	*ptr = 0;
4441f5207bJohn Levon	return buffer;
4451f5207bJohn Levon}
4461f5207bJohn Levon
4471f5207bJohn Levonstatic struct token *stringify(struct token *arg)
4481f5207bJohn Levon{
4491f5207bJohn Levon	const char *s = show_token_sequence(arg, 1);
4501f5207bJohn Levon	int size = strlen(s)+1;
4511f5207bJohn Levon	struct token *token = __alloc_token(0);
4521f5207bJohn Levon	struct string *string = __alloc_string(size);
4531f5207bJohn Levon
4541f5207bJohn Levon	memcpy(string->data, s, size);
4551f5207bJohn Levon	string->length = size;
4561f5207bJohn Levon	token->pos = arg->pos;
4571f5207bJohn Levon	token_type(token) = TOKEN_STRING;
4581f5207bJohn Levon	token->string = string;
4591f5207bJohn Levon	token->next = &eof_token_entry;
4601f5207bJohn Levon	return token;
4611f5207bJohn Levon}
4621f5207bJohn Levon
4631f5207bJohn Levonstatic void expand_arguments(int count, struct arg *args)
4641f5207bJohn Levon{
4651f5207bJohn Levon	int i;
4661f5207bJohn Levon	for (i = 0; i < count; i++) {
4671f5207bJohn Levon		struct token *arg = args[i].arg;
4681f5207bJohn Levon		if (!arg)
4691f5207bJohn Levon			arg = &eof_token_entry;
4701f5207bJohn Levon		if (args[i].n_str)
4711f5207bJohn Levon			args[i].str = stringify(arg);
4721f5207bJohn Levon		if (args[i].n_normal) {
4731f5207bJohn Levon			if (!args[i].n_quoted) {
4741f5207bJohn Levon				args[i].expanded = arg;
4751f5207bJohn Levon				args[i].arg = NULL;
4761f5207bJohn Levon			} else if (eof_token(arg)) {
4771f5207bJohn Levon				args[i].expanded = arg;
4781f5207bJohn Levon			} else {
4791f5207bJohn Levon				args[i].expanded = dup_list(arg);
4801f5207bJohn Levon			}
4811f5207bJohn Levon			expand_list(&args[i].expanded);
4821f5207bJohn Levon		}
4831f5207bJohn Levon	}
4841f5207bJohn Levon}
4851f5207bJohn Levon
4861f5207bJohn Levon/*
4871f5207bJohn Levon * Possibly valid combinations:
4881f5207bJohn Levon *  - ident + ident -> ident
4891f5207bJohn Levon *  - ident + number -> ident unless number contains '.', '+' or '-'.
4901f5207bJohn Levon *  - 'L' + char constant -> wide char constant
4911f5207bJohn Levon *  - 'L' + string literal -> wide string literal
4921f5207bJohn Levon *  - number + number -> number
4931f5207bJohn Levon *  - number + ident -> number
4941f5207bJohn Levon *  - number + '.' -> number
4951f5207bJohn Levon *  - number + '+' or '-' -> number, if number used to end on [eEpP].
4961f5207bJohn Levon *  - '.' + number -> number, if number used to start with a digit.
4971f5207bJohn Levon *  - special + special -> either special or an error.
4981f5207bJohn Levon */
4991f5207bJohn Levonstatic enum token_type combine(struct token *left, struct token *right, char *p)
5001f5207bJohn Levon{
5011f5207bJohn Levon	int len;
5021f5207bJohn Levon	enum token_type t1 = token_type(left), t2 = token_type(right);
5031f5207bJohn Levon
5041f5207bJohn Levon	if (t1 != TOKEN_IDENT && t1 != TOKEN_NUMBER && t1 != TOKEN_SPECIAL)
5051f5207bJohn Levon		return TOKEN_ERROR;
5061f5207bJohn Levon
5071f5207bJohn Levon	if (t1 == TOKEN_IDENT && left->ident == &L_ident) {
5081f5207bJohn Levon		if (t2 >= TOKEN_CHAR && t2 < TOKEN_WIDE_CHAR)
5091f5207bJohn Levon			return t2 + TOKEN_WIDE_CHAR - TOKEN_CHAR;
5101f5207bJohn Levon		if (t2 == TOKEN_STRING)
5111f5207bJohn Levon			return TOKEN_WIDE_STRING;
5121f5207bJohn Levon	}
5131f5207bJohn Levon
5141f5207bJohn Levon	if (t2 != TOKEN_IDENT && t2 != TOKEN_NUMBER && t2 != TOKEN_SPECIAL)
5151f5207bJohn Levon		return TOKEN_ERROR;
5161f5207bJohn Levon
5171f5207bJohn Levon	strcpy(p, show_token(left));
5181f5207bJohn Levon	strcat(p, show_token(right));
5191f5207bJohn Levon	len = strlen(p);
5201f5207bJohn Levon
5211f5207bJohn Levon	if (len >= 256)
5221f5207bJohn Levon		return TOKEN_ERROR;
5231f5207bJohn Levon
5241f5207bJohn Levon	if (t1 == TOKEN_IDENT) {
5251f5207bJohn Levon		if (t2 == TOKEN_SPECIAL)
5261f5207bJohn Levon			return TOKEN_ERROR;
5271f5207bJohn Levon		if (t2 == TOKEN_NUMBER && strpbrk(p, "+-."))
5281f5207bJohn Levon			return TOKEN_ERROR;
5291f5207bJohn Levon		return TOKEN_IDENT;
5301f5207bJohn Levon	}
5311f5207bJohn Levon
5321f5207bJohn Levon	if (t1 == TOKEN_NUMBER) {
5331f5207bJohn Levon		if (t2 == TOKEN_SPECIAL) {
5341f5207bJohn Levon			switch (right->special) {
5351f5207bJohn Levon			case '.':
5361f5207bJohn Levon				break;
5371f5207bJohn Levon			case '+': case '-':
5381f5207bJohn Levon				if (strchr("eEpP", p[len - 2]))
5391f5207bJohn Levon					break;
5401f5207bJohn Levon			default:
5411f5207bJohn Levon				return TOKEN_ERROR;
5421f5207bJohn Levon			}
5431f5207bJohn Levon		}
5441f5207bJohn Levon		return TOKEN_NUMBER;
5451f5207bJohn Levon	}
5461f5207bJohn Levon
5471f5207bJohn Levon	if (p[0] == '.' && isdigit((unsigned char)p[1]))
5481f5207bJohn Levon		return TOKEN_NUMBER;
5491f5207bJohn Levon
5501f5207bJohn Levon	return TOKEN_SPECIAL;
5511f5207bJohn Levon}
5521f5207bJohn Levon
5531f5207bJohn Levonstatic int merge(struct token *left, struct token *right)
5541f5207bJohn Levon{
5551f5207bJohn Levon	static char buffer[512];
5561f5207bJohn Levon	enum token_type res = combine(left, right, buffer);
5571f5207bJohn Levon	int n;
5581f5207bJohn Levon
5591f5207bJohn Levon	switch (res) {
5601f5207bJohn Levon	case TOKEN_IDENT:
5611f5207bJohn Levon		left->ident = built_in_ident(buffer);
5621f5207bJohn Levon		left->pos.noexpand = 0;
5631f5207bJohn Levon		return 1;
5641f5207bJohn Levon
565c85f09cJohn Levon	case TOKEN_NUMBER:
5661f5207bJohn Levon		token_type(left) = TOKEN_NUMBER;	/* could be . + num */
567c85f09cJohn Levon		left->number = xstrdup(buffer);
5681f5207bJohn Levon		return 1;
5691f5207bJohn Levon
5701f5207bJohn Levon	case TOKEN_SPECIAL:
5711f5207bJohn Levon		if (buffer[2] && buffer[3])
5721f5207bJohn Levon			break;
5731f5207bJohn Levon		for (n = SPECIAL_BASE; n < SPECIAL_ARG_SEPARATOR; n++) {
5741f5207bJohn Levon			if (!memcmp(buffer, combinations[n-SPECIAL_BASE], 3)) {
5751f5207bJohn Levon				left->special = n;
5761f5207bJohn Levon				return 1;
5771f5207bJohn Levon			}
5781f5207bJohn Levon		}
5791f5207bJohn Levon		break;
5801f5207bJohn Levon
5811f5207bJohn Levon	case TOKEN_WIDE_CHAR:
5821f5207bJohn Levon	case TOKEN_WIDE_STRING:
5831f5207bJohn Levon		token_type(left) = res;
5841f5207bJohn Levon		left->pos.noexpand = 0;
5851f5207bJohn Levon		left->string = right->string;
5861f5207bJohn Levon		return 1;
5871f5207bJohn Levon
5881f5207bJohn Levon	case TOKEN_WIDE_CHAR_EMBEDDED_0 ... TOKEN_WIDE_CHAR_EMBEDDED_3:
5891f5207bJohn Levon		token_type(left) = res;
5901f5207bJohn Levon		left->pos.noexpand = 0;
5911f5207bJohn Levon		memcpy(left->embedded, right->embedded, 4);
5921f5207bJohn Levon		return 1;
5931f5207bJohn Levon
5941f5207bJohn Levon	default:
5951f5207bJohn Levon		;
5961f5207bJohn Levon	}
5971f5207bJohn Levon	sparse_error(left->pos, "'##' failed: concatenation is not a valid token");
5981f5207bJohn Levon	return 0;
5991f5207bJohn Levon}
6001f5207bJohn Levon
6011f5207bJohn Levonstatic struct token *dup_token(struct token *token, struct position *streampos)
6021f5207bJohn Levon{
6031f5207bJohn Levon	struct token *alloc = alloc_token(streampos);
6041f5207bJohn Levon	token_type(alloc) = token_type(token);
6051f5207bJohn Levon	alloc->pos.newline = token->pos.newline;
6061f5207bJohn Levon	alloc->pos.whitespace = token->pos.whitespace;
6071f5207bJohn Levon	alloc->number = token->number;
6081f5207bJohn Levon	alloc->pos.noexpand = token->pos.noexpand;
6091f5207bJohn Levon	return alloc;
6101f5207bJohn Levon}
6111f5207bJohn Levon
6121f5207bJohn Levonstatic struct token **copy(struct token **where, struct token *list, int *count)
6131f5207bJohn Levon{
6141f5207bJohn Levon	int need_copy = --*count;
6151f5207bJohn Levon	while (!eof_token(list)) {
6161f5207bJohn Levon		struct token *token;
6171f5207bJohn Levon		if (need_copy)
6181f5207bJohn Levon			token = dup_token(list, &list->pos);
6191f5207bJohn Levon		else
6201f5207bJohn Levon			token = list;
6211f5207bJohn Levon		if (token_type(token) == TOKEN_IDENT && token->ident->tainted)
6221f5207bJohn Levon			token->pos.noexpand = 1;
6231f5207bJohn Levon		*where = token;
6241f5207bJohn Levon		where = &token->next;
6251f5207bJohn Levon		list = list->next;
6261f5207bJohn Levon	}
6271f5207bJohn Levon	*where = &eof_token_entry;
6281f5207bJohn Levon	return where;
6291f5207bJohn Levon}
6301f5207bJohn Levon
6311f5207bJohn Levonstatic int handle_kludge(struct token **p, struct arg *args)
6321f5207bJohn Levon{
6331f5207bJohn Levon	struct token *t = (*p)->next->next;
6341f5207bJohn Levon	while (1) {
6351f5207bJohn Levon		struct arg *v = &args[t->argnum];
6361f5207bJohn Levon		if (token_type(t->next) != TOKEN_CONCAT) {
6371f5207bJohn Levon			if (v->arg) {
6381f5207bJohn Levon				/* ignore the first ## */
6391f5207bJohn Levon				*p = (*p)->next;
6401f5207bJohn Levon				return 0;
6411f5207bJohn Levon			}
6421f5207bJohn Levon			/* skip the entire thing */
6431f5207bJohn Levon			*p = t;
6441f5207bJohn Levon			return 1;
6451f5207bJohn Levon		}
6461f5207bJohn Levon		if (v->arg && !eof_token(v->arg))
6471f5207bJohn Levon			return 0; /* no magic */
6481f5207bJohn Levon		t = t->next->next;
6491f5207bJohn Levon	}
6501f5207bJohn Levon}
6511f5207bJohn Levon
6521f5207bJohn Levonstatic struct token **substitute(struct token **list, struct token *body, struct arg *args)
6531f5207bJohn Levon{
6541f5207bJohn Levon	struct position *base_pos = &(*list)->pos;
6551f5207bJohn Levon	int *count;
6561f5207bJohn Levon	enum {Normal, Placeholder, Concat} state = Normal;
6571f5207bJohn Levon
6581f5207bJohn Levon	for (; !eof_token(body); body = body->next) {
6591f5207bJohn Levon		struct token *added, *arg;
6601f5207bJohn Levon		struct token **tail;
6611f5207bJohn Levon		struct token *t;
6621f5207bJohn Levon
6631f5207bJohn Levon		switch (token_type(body)) {
6641f5207bJohn Levon		case TOKEN_GNU_KLUDGE:
6651f5207bJohn Levon			/*
6661f5207bJohn Levon			 * GNU kludge: if we had <comma>##<vararg>, behaviour
6671f5207bJohn Levon			 * depends on whether we had enough arguments to have
6681f5207bJohn Levon			 * a vararg.  If we did, ## is just ignored.  Otherwise
6691f5207bJohn Levon			 * both , and ## are ignored.  Worse, there can be
6701f5207bJohn Levon			 * an arbitrary number of ##<arg> in between; if all of
6711f5207bJohn Levon			 * those are empty, we act as if they hadn't been there,
6721f5207bJohn Levon			 * otherwise we act as if the kludge didn't exist.
6731f5207bJohn Levon			 */
6741f5207bJohn Levon			t = body;
6751f5207bJohn Levon			if (handle_kludge(&body, args)) {
6761f5207bJohn Levon				if (state == Concat)
6771f5207bJohn Levon					state = Normal;
6781f5207bJohn Levon				else
6791f5207bJohn Levon					state = Placeholder;
6801f5207bJohn Levon				continue;
6811f5207bJohn Levon			}
6821f5207bJohn Levon			added = dup_token(t, base_pos);
6831f5207bJohn Levon			token_type(added) = TOKEN_SPECIAL;
6841f5207bJohn Levon			tail = &added->next;
6851f5207bJohn Levon			break;
6861f5207bJohn Levon
6871f5207bJohn Levon		case TOKEN_STR_ARGUMENT:
6881f5207bJohn Levon			arg = args[body->argnum].str;
6891f5207bJohn Levon			count = &args[body->argnum].n_str;
6901f5207bJohn Levon			goto copy_arg;
6911f5207bJohn Levon
6921f5207bJohn Levon		case TOKEN_QUOTED_ARGUMENT:
6931f5207bJohn Levon			arg = args[body->argnum].arg;
6941f5207bJohn Levon			count = &args[body->argnum].n_quoted;
6951f5207bJohn Levon			if (!arg || eof_token(arg)) {
6961f5207bJohn Levon				if (state == Concat)
6971f5207bJohn Levon					state = Normal;
6981f5207bJohn Levon				else
6991f5207bJohn Levon					state = Placeholder;
7001f5207bJohn Levon				continue;
7011f5207bJohn Levon			}
7021f5207bJohn Levon			goto copy_arg;
7031f5207bJohn Levon
7041f5207bJohn Levon		case TOKEN_MACRO_ARGUMENT:
7051f5207bJohn Levon			arg = args[body->argnum].expanded;
7061f5207bJohn Levon			count = &args[body->argnum].n_normal;
7071f5207bJohn Levon			if (eof_token(arg)) {
7081f5207bJohn Levon				state = Normal;
7091f5207bJohn Levon				continue;
7101f5207bJohn Levon			}
7111f5207bJohn Levon		copy_arg:
7121f5207bJohn Levon			tail = copy(&added, arg, count);
7131f5207bJohn Levon			added->pos.newline = body->pos.newline;
7141f5207bJohn Levon			added->pos.whitespace = body->pos.whitespace;
7151f5207bJohn Levon			break;
7161f5207bJohn Levon
7171f5207bJohn Levon		case TOKEN_CONCAT:
7181f5207bJohn Levon			if (state == Placeholder)
7191f5207bJohn Levon				state = Normal;
7201f5207bJohn Levon			else
7211f5207bJohn Levon				state = Concat;
7221f5207bJohn Levon			continue;
7231f5207bJohn Levon
7241f5207bJohn Levon		case TOKEN_IDENT:
7251f5207bJohn Levon			added = dup_token(body, base_pos);
7261f5207bJohn Levon			if (added->ident->tainted)
7271f5207bJohn Levon				added->pos.noexpand = 1;
7281f5207bJohn Levon			tail = &added->next;
7291f5207bJohn Levon			break;
7301f5207bJohn Levon
7311f5207bJohn Levon		default:
7321f5207bJohn Levon			added = dup_token(body, base_pos);
7331f5207bJohn Levon			tail = &added->next;
7341f5207bJohn Levon			break;
7351f5207bJohn Levon		}
7361f5207bJohn Levon
7371f5207bJohn Levon		/*
7381f5207bJohn Levon		 * if we got to doing real concatenation, we already have
7391f5207bJohn Levon		 * added something into the list, so containing_token() is OK.
7401f5207bJohn Levon		 */
7411f5207bJohn Levon		if (state == Concat && merge(containing_token(list), added)) {
7421f5207bJohn Levon			*list = added->next;
7431f5207bJohn Levon			if (tail != &added->next)
7441f5207bJohn Levon				list = tail;
7451f5207bJohn Levon		} else {
7461f5207bJohn Levon			*list = added;
7471f5207bJohn Levon			list = tail;
7481f5207bJohn Levon		}
7491f5207bJohn Levon		state = Normal;
7501f5207bJohn Levon	}
7511f5207bJohn Levon	*list = &eof_token_entry;
7521f5207bJohn Levon	return list;
7531f5207bJohn Levon}
7541f5207bJohn Levon
7551f5207bJohn Levonstatic int expand(struct token **list, struct symbol *sym)
7561f5207bJohn Levon{
7571f5207bJohn Levon	struct token *last;
7581f5207bJohn Levon	struct token *token = *list;
7591f5207bJohn Levon	struct ident *expanding = token->ident;
7601f5207bJohn Levon	struct token **tail;
7611f5207bJohn Levon	int nargs = sym->arglist ? sym->arglist->count.normal : 0;
7621f5207bJohn Levon	struct arg args[nargs];
7631f5207bJohn Levon
7641f5207bJohn Levon	if (expanding->tainted) {
7651f5207bJohn Levon		token->pos.noexpand = 1;
7661f5207bJohn Levon		return 1;
7671f5207bJohn Levon	}
7681f5207bJohn Levon
7691f5207bJohn Levon	if (sym->arglist) {
7701f5207bJohn Levon		if (!match_op(scan_next(&token->next), '('))
7711f5207bJohn Levon			return 1;
7721f5207bJohn Levon		if (!collect_arguments(token->next, sym->arglist, args, token))
7731f5207bJohn Levon			return 1;
7741f5207bJohn Levon		expand_arguments(nargs, args);
7751f5207bJohn Levon	}
7761f5207bJohn Levon
7771f5207bJohn Levon	expanding->tainted = 1;
7781f5207bJohn Levon
7791f5207bJohn Levon	last = token->next;
7801f5207bJohn Levon	tail = substitute(list, sym->expansion, args);
7811f5207bJohn Levon	/*
7821f5207bJohn Levon	 * Note that it won't be eof - at least TOKEN_UNTAINT will be there.
7831f5207bJohn Levon	 * We still can lose the newline flag if the sucker expands to nothing,
7841f5207bJohn Levon	 * but the price of dealing with that is probably too high (we'd need
7851f5207bJohn Levon	 * to collect the flags during scan_next())
7861f5207bJohn Levon	 */
7871f5207bJohn Levon	(*list)->pos.newline = token->pos.newline;
7881f5207bJohn Levon	(*list)->pos.whitespace = token->pos.whitespace;
7891f5207bJohn Levon	*tail = last;
7901f5207bJohn Levon
7911f5207bJohn Levon	return 0;
7921f5207bJohn Levon}
7931f5207bJohn Levon
7941f5207bJohn Levonstatic const char *token_name_sequence(struct token *token, int endop, struct token *start)
7951f5207bJohn Levon{
7961f5207bJohn Levon	static char buffer[256];
7971f5207bJohn Levon	char *ptr = buffer;
7981f5207bJohn Levon
7991f5207bJohn Levon	while (!eof_token(token) && !match_op(token, endop)) {
8001f5207bJohn Levon		int len;
8011f5207bJohn Levon		const char *val = token->string->data;
8021f5207bJohn Levon		if (token_type(token) != TOKEN_STRING)
8031f5207bJohn Levon			val = show_token(token);
8041f5207bJohn Levon		len = strlen(val);
8051f5207bJohn Levon		memcpy(ptr, val, len);
8061f5207bJohn Levon		ptr += len;
8071f5207bJohn Levon		token = token->next;
8081f5207bJohn Levon	}
8091f5207bJohn Levon	*ptr = 0;
8101f5207bJohn Levon	if (endop && !match_op(token, endop))
8111f5207bJohn Levon		sparse_error(start->pos, "expected '>' at end of filename");
8121f5207bJohn Levon	return buffer;
8131f5207bJohn Levon}
8141f5207bJohn Levon
8151f5207bJohn Levonstatic int already_tokenized(const char *path)
8161f5207bJohn Levon{
8171f5207bJohn Levon	int stream, next;
8181f5207bJohn Levon
8191f5207bJohn Levon	for (stream = *hash_stream(path); stream >= 0 ; stream = next) {
8201f5207bJohn Levon		struct stream *s = input_streams + stream;
8211f5207bJohn Levon
8221f5207bJohn Levon		next = s->next_stream;
8231f5207bJohn Levon		if (s->once) {
8241f5207bJohn Levon			if (strcmp(path, s->name))
8251f5207bJohn Levon				continue;
8261f5207bJohn Levon			return 1;
8271f5207bJohn Levon		}
8281f5207bJohn Levon		if (s->constant != CONSTANT_FILE_YES)
8291f5207bJohn Levon			continue;
8301f5207bJohn Levon		if (strcmp(path, s->name))
8311f5207bJohn Levon			continue;
8321f5207bJohn Levon		if (s->protect && !lookup_macro(s->protect))
8331f5207bJohn Levon			continue;
8341f5207bJohn Levon		return 1;
8351f5207bJohn Levon	}
8361f5207bJohn Levon	return 0;
8371f5207bJohn Levon}
8381f5207bJohn Levon
8391f5207bJohn Levon/* Handle include of header files.
8401f5207bJohn Levon * The relevant options are made compatible with gcc. The only options that
8411f5207bJohn Levon * are not supported is -withprefix and friends.
8421f5207bJohn Levon *
8431f5207bJohn Levon * Three set of include paths are known:
8441f5207bJohn Levon * quote_includepath:	Path to search when using #include "file.h"
8451f5207bJohn Levon * angle_includepath:	Paths to search when using #include <file.h>
8461f5207bJohn Levon * isys_includepath:	Paths specified with -isystem, come before the
8471f5207bJohn Levon *			built-in system include paths. Gcc would suppress
8481f5207bJohn Levon *			warnings from system headers. Here we separate
8491f5207bJohn Levon *			them from the angle_ ones to keep search ordering.
8501f5207bJohn Levon *
8511f5207bJohn Levon * sys_includepath:	Built-in include paths.
8521f5207bJohn Levon * dirafter_includepath Paths added with -dirafter.
8531f5207bJohn Levon *
8541f5207bJohn Levon * The above is implemented as one array with pointers
8551f5207bJohn Levon *                         +--------------+
8561f5207bJohn Levon * quote_includepath --->  |              |
8571f5207bJohn Levon *                         +--------------+
8581f5207bJohn Levon *                         |              |
8591f5207bJohn Levon *                         +--------------+
8601f5207bJohn Levon * angle_includepath --->  |              |
8611f5207bJohn Levon *                         +--------------+
8621f5207bJohn Levon * isys_includepath  --->  |              |
8631f5207bJohn Levon *                         +--------------+
8641f5207bJohn Levon * sys_includepath   --->  |              |
8651f5207bJohn Levon *                         +--------------+
8661f5207bJohn Levon * dirafter_includepath -> |              |
8671f5207bJohn Levon *                         +--------------+
8681f5207bJohn Levon *
8691f5207bJohn Levon * -I dir insert dir just before isys_includepath and move the rest
8701f5207bJohn Levon * -I- makes all dirs specified with -I before to quote dirs only and
8711f5207bJohn Levon *   angle_includepath is set equal to isys_includepath.
8721f5207bJohn Levon * -nostdinc removes all sys dirs by storing NULL in entry pointed
8731f5207bJohn Levon *   to by * sys_includepath. Note that this will reset all dirs built-in
8741f5207bJohn Levon *   and added before -nostdinc by -isystem and -idirafter.
8751f5207bJohn Levon * -isystem dir adds dir where isys_includepath points adding this dir as
8761f5207bJohn Levon *   first systemdir
8771f5207bJohn Levon * -idirafter dir adds dir to the end of the list
8781f5207bJohn Levon */
8791f5207bJohn Levon
8801f5207bJohn Levonstatic void set_stream_include_path(struct stream *stream)
8811f5207bJohn Levon{
8821f5207bJohn Levon	const char *path = stream->path;
8831f5207bJohn Levon	if (!path) {
8841f5207bJohn Levon		const char *p = strrchr(stream->name, '/');
8851f5207bJohn Levon		path = "";
8861f5207bJohn Levon		if (p) {
8871f5207bJohn Levon			int len = p - stream->name + 1;
8881f5207bJohn Levon			char *m = malloc(len+1);
8891f5207bJohn Levon			/* This includes the final "/" */
8901f5207bJohn Levon			memcpy(m, stream->name, len);
8911f5207bJohn Levon			m[len] = 0;
8921f5207bJohn Levon			path = m;
8931f5207bJohn Levon		}
8941f5207bJohn Levon		stream->path = path;
8951f5207bJohn Levon	}
8961f5207bJohn Levon	includepath[0] = path;
8971f5207bJohn Levon}
8981f5207bJohn Levon
899c85f09cJohn Levon#ifndef PATH_MAX
900c85f09cJohn Levon#define PATH_MAX 4096	// for Hurd where it's not defined
901c85f09cJohn Levon#endif
902c85f09cJohn Levon
9031f5207bJohn Levonstatic int try_include(const char *path, const char *filename, int flen, struct token **where, const char **next_path)
9041f5207bJohn Levon{
9051f5207bJohn Levon	int fd;
9061f5207bJohn Levon	int plen = strlen(path);
9071f5207bJohn Levon	static char fullname[PATH_MAX];
9081f5207bJohn Levon
9091f5207bJohn Levon	memcpy(fullname, path, plen);
9101f5207bJohn Levon	if (plen && path[plen-1] != '/') {
9111f5207bJohn Levon		fullname[plen] = '/';
9121f5207bJohn Levon		plen++;
9131f5207bJohn Levon	}
9141f5207bJohn Levon	memcpy(fullname+plen, filename, flen);
9151f5207bJohn Levon	if (already_tokenized(fullname))
9161f5207bJohn Levon		return 1;
9171f5207bJohn Levon	fd = open(fullname, O_RDONLY);
9181f5207bJohn Levon	if (fd >= 0) {
919c85f09cJohn Levon		char *streamname = xmemdup(fullname, plen + flen);
9201f5207bJohn Levon		*where = tokenize(streamname, fd, *where, next_path);
9211f5207bJohn Levon		close(fd);
9221f5207bJohn Levon		return 1;
9231f5207bJohn Levon	}
9241f5207bJohn Levon	return 0;
9251f5207bJohn Levon}
9261f5207bJohn Levon
9271f5207bJohn Levonstatic int do_include_path(const char **pptr, struct token **list, struct token *token, const char *filename, int flen)
9281f5207bJohn Levon{
9291f5207bJohn Levon	const char *path;
9301f5207bJohn Levon
9311f5207bJohn Levon	while ((path = *pptr++) != NULL) {
9321f5207bJohn Levon		if (!try_include(path, filename, flen, list, pptr))
9331f5207bJohn Levon			continue;
9341f5207bJohn Levon		return 1;
9351f5207bJohn Levon	}
9361f5207bJohn Levon	return 0;
9371f5207bJohn Levon}
9381f5207bJohn Levon
9391f5207bJohn Levonstatic int free_preprocessor_line(struct token *token)
9401f5207bJohn Levon{
9411f5207bJohn Levon	while (token_type(token) != TOKEN_EOF) {
9421f5207bJohn Levon		struct token *free = token;
9431f5207bJohn Levon		token = token->next;
9441f5207bJohn Levon		__free_token(free);
9451f5207bJohn Levon	};
9461f5207bJohn Levon	return 1;
9471f5207bJohn Levon}
9481f5207bJohn Levon
9491f5207bJohn Levonconst char *find_include(const char *skip, const char *look_for)
9501f5207bJohn Levon{
9511f5207bJohn Levon	DIR *dp;
9521f5207bJohn Levon	struct dirent *entry;
9531f5207bJohn Levon	struct stat statbuf;
9541f5207bJohn Levon	const char *ret;
9551f5207bJohn Levon	char cwd[PATH_MAX];
9561f5207bJohn Levon	static char buf[PATH_MAX + 1];
9571f5207bJohn Levon
9581f5207bJohn Levon	dp = opendir(".");
9591f5207bJohn Levon	if (!dp)
9601f5207bJohn Levon		return NULL;
9611f5207bJohn Levon
9621f5207bJohn Levon	if (!getcwd(cwd, sizeof(cwd)))
963c85f09cJohn Levon		goto close;
9641f5207bJohn Levon
9651f5207bJohn Levon	while ((entry = readdir(dp))) {
9661f5207bJohn Levon		lstat(entry->d_name, &statbuf);
9671f5207bJohn Levon
9681f5207bJohn Levon		if (strcmp(entry->d_name, look_for) == 0) {
9691f5207bJohn Levon			snprintf(buf, sizeof(buf), "%s/%s", cwd, entry->d_name);
970c85f09cJohn Levon			closedir(dp);
9711f5207bJohn Levon			return buf;
9721f5207bJohn Levon		}
9731f5207bJohn Levon
9741f5207bJohn Levon		if (S_ISDIR(statbuf.st_mode)) {
9751f5207bJohn Levon			/* Found a directory, but ignore . and .. */
9761f5207bJohn Levon			if (strcmp(".", entry->d_name) == 0 ||
9771f5207bJohn Levon			    strcmp("..", entry->d_name) == 0 ||
9781f5207bJohn Levon			    strcmp(skip, entry->d_name) == 0)
9791f5207bJohn Levon				continue;
9801f5207bJohn Levon
9811f5207bJohn Levon			chdir(entry->d_name);
9821f5207bJohn Levon			ret = find_include("", look_for);
9831f5207bJohn Levon			chdir("..");
984c85f09cJohn Levon			if (ret) {
985c85f09cJohn Levon				closedir(dp);
9861f5207bJohn Levon				return ret;
987c85f09cJohn Levon			}
9881f5207bJohn Levon		}
9891f5207bJohn Levon	}
990c85f09cJohn Levonclose:
9911f5207bJohn Levon	closedir(dp);
9921f5207bJohn Levon
9931f5207bJohn Levon	return NULL;
9941f5207bJohn Levon}
9951f5207bJohn Levon
9961f5207bJohn Levonconst char *search_dir(const char *stop, const char *look_for)
9971f5207bJohn Levon{
9981f5207bJohn Levon	char cwd[PATH_MAX];
9991f5207bJohn Levon	int len;
10001f5207bJohn Levon	const char *ret;
10011f5207bJohn Levon	int cnt = 0;
10021f5207bJohn Levon
10031f5207bJohn Levon	if (!getcwd(cwd, sizeof(cwd)))
10041f5207bJohn Levon		return NULL;
10051f5207bJohn Levon
10061f5207bJohn Levon	len = strlen(cwd);
10071f5207bJohn Levon	while (len >= 0) {
10081f5207bJohn Levon		ret = find_include(cnt++ ? cwd + len + 1 : "", look_for);
10091f5207bJohn Levon		if (ret)
10101f5207bJohn Levon			return ret;
10111f5207bJohn Levon
10121f5207bJohn Levon		if (strcmp(cwd, stop) == 0 ||
10131f5207bJohn Levon		    strcmp(cwd, "/usr/include") == 0 ||
10141f5207bJohn Levon		    strcmp(cwd, "/usr/local/include") == 0 ||
10151f5207bJohn Levon		    strlen(cwd) <= 10 ||  /* heck...  don't search /usr/lib/ */
10161f5207bJohn Levon		    strcmp(cwd, "/") == 0)
10171f5207bJohn Levon			return NULL;
10181f5207bJohn Levon
10191f5207bJohn Levon		while (--len >= 0) {
10201f5207bJohn Levon			if (cwd[len] == '/') {
10211f5207bJohn Levon				cwd[len] = '\0';
10221f5207bJohn Levon				break;
10231f5207bJohn Levon			}
10241f5207bJohn Levon		}
10251f5207bJohn Levon
10261f5207bJohn Levon		chdir("..");
10271f5207bJohn Levon	}
10281f5207bJohn Levon	return NULL;
10291f5207bJohn Levon}
10301f5207bJohn Levon
10311f5207bJohn Levonstatic void use_best_guess_header_file(struct token *token, const char *filename, struct token **list)
10321f5207bJohn Levon{
10331f5207bJohn Levon	char cwd[PATH_MAX];
10341f5207bJohn Levon	char dir_part[PATH_MAX];
10351f5207bJohn Levon	const char *file_part;
10361f5207bJohn Levon	const char *include_name;
1037c85f09cJohn Levon	static int cnt;
10381f5207bJohn Levon	int len;
10391f5207bJohn Levon
1040c85f09cJohn Levon	/* Avoid guessing includes recursively. */
1041c85f09cJohn Levon	if (cnt++ > 1000)
1042c85f09cJohn Levon		return;
1043c85f09cJohn Levon
10441f5207bJohn Levon	if (!filename || filename[0] == '\0')
10451f5207bJohn Levon		return;
10461f5207bJohn Levon
10471f5207bJohn Levon	file_part = filename;
10481f5207bJohn Levon	while ((filename = strchr(filename, '/'))) {
10491f5207bJohn Levon		++filename;
10501f5207bJohn Levon		if (filename[0])
10511f5207b