17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58a40a695Sgavinm  * Common Development and Distribution License (the "License").
68a40a695Sgavinm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22837416c3Scy  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * esclex.c -- lexer for esc
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * this module provides lexical analysis and error handling routine
287c478bd9Sstevel@tonic-gate  * expected by the yacc-generated parser (i.e. yylex() and yyerror()).
297c478bd9Sstevel@tonic-gate  * it also does lots of tracking of things like filenames, line numbers,
307c478bd9Sstevel@tonic-gate  * and what tokens are seen on a line up to the point where a syntax error
317c478bd9Sstevel@tonic-gate  * was found.  this module also arranges for the input source files to
327c478bd9Sstevel@tonic-gate  * be run through cpp.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <ctype.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <time.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include "out.h"
437c478bd9Sstevel@tonic-gate #include "alloc.h"
447c478bd9Sstevel@tonic-gate #include "stats.h"
457c478bd9Sstevel@tonic-gate #include "stable.h"
467c478bd9Sstevel@tonic-gate #include "lut.h"
477c478bd9Sstevel@tonic-gate #include "literals.h"
487c478bd9Sstevel@tonic-gate #include "tree.h"
497c478bd9Sstevel@tonic-gate #include "esclex.h"
507c478bd9Sstevel@tonic-gate #include "eftread.h"
517c478bd9Sstevel@tonic-gate #include "check.h"
527c478bd9Sstevel@tonic-gate #include "y.tab.h"
537c478bd9Sstevel@tonic-gate 
54*cfc9ef1dSToomas Soome struct lut *Dicts;
55*cfc9ef1dSToomas Soome struct lut *Ident;
56*cfc9ef1dSToomas Soome struct lut *Timesuffixlut;
57*cfc9ef1dSToomas Soome int Pragma_trust_ereports;
58*cfc9ef1dSToomas Soome int Pragma_new_errors_only;
59*cfc9ef1dSToomas Soome 
607c478bd9Sstevel@tonic-gate /* ridiculously long token buffer -- disallow any token longer than this */
617c478bd9Sstevel@tonic-gate #define	MAXTOK	8192
627c478bd9Sstevel@tonic-gate static char Tok[MAXTOK];
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* some misc stats we keep on the lexer & parser */
657c478bd9Sstevel@tonic-gate static struct stats *Tokcount;
667c478bd9Sstevel@tonic-gate static struct stats *Lexelapse;
677c478bd9Sstevel@tonic-gate struct stats *Filecount;
687c478bd9Sstevel@tonic-gate struct filestats {
697c478bd9Sstevel@tonic-gate 	struct filestats *next;
707c478bd9Sstevel@tonic-gate 	struct stats *stats;
718a40a695Sgavinm 	struct stats *idstats;
727c478bd9Sstevel@tonic-gate } *Fstats;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate static int Errcount;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* input file state */
777c478bd9Sstevel@tonic-gate static char **Files;
787c478bd9Sstevel@tonic-gate static const char *Fileopened;
797c478bd9Sstevel@tonic-gate static FILE *Fp;
807c478bd9Sstevel@tonic-gate static int Line;
817c478bd9Sstevel@tonic-gate static const char *File;
8296fc3fdfSRobert Mustacchi static const char *Cpp = "/usr/lib/cpp";
837c478bd9Sstevel@tonic-gate #ifdef	ESC
847c478bd9Sstevel@tonic-gate static const char *Cppargs;
857c478bd9Sstevel@tonic-gate static const char *Cppstdargs = "-undef -Y.";
867c478bd9Sstevel@tonic-gate #endif	/* ESC */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* for debugging */
897c478bd9Sstevel@tonic-gate static int Lexecho;	/* echo tokens as we read them */
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /* forward declarations of our internal routines */
927c478bd9Sstevel@tonic-gate static int record(int tok, const char *s);
937c478bd9Sstevel@tonic-gate static void dumpline(int flags);
947c478bd9Sstevel@tonic-gate static void doident();
957c478bd9Sstevel@tonic-gate static void dopragma(const char *tok);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * table of reserved words.  this table is only used by lex_init()
997c478bd9Sstevel@tonic-gate  * to intialize the Rwords lookup table.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate static const struct {
1027c478bd9Sstevel@tonic-gate 	const char *word;
1037c478bd9Sstevel@tonic-gate 	const int val;
1047c478bd9Sstevel@tonic-gate } Rwords[] = {
1057c478bd9Sstevel@tonic-gate 	{ "asru", ASRU },
1067aec1d6eScindi 	{ "count", COUNT },
1077c478bd9Sstevel@tonic-gate 	{ "div", DIV },
1087c478bd9Sstevel@tonic-gate 	{ "engine", ENGINE },
1097c478bd9Sstevel@tonic-gate 	{ "event", EVENT },
1107c478bd9Sstevel@tonic-gate 	{ "fru", FRU },
1117c478bd9Sstevel@tonic-gate 	{ "if", IF },
1127c478bd9Sstevel@tonic-gate 	{ "mask", MASK },
1137c478bd9Sstevel@tonic-gate 	{ "prop", PROP },
1147c478bd9Sstevel@tonic-gate 	{ "config", CONFIG },
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * PATHFUNC indicates functions that operate only on paths
1177c478bd9Sstevel@tonic-gate 	 * and quotes
1187c478bd9Sstevel@tonic-gate 	 */
1197c478bd9Sstevel@tonic-gate 	{ "is_connected", PATHFUNC },
1207c478bd9Sstevel@tonic-gate 	{ "is_under", PATHFUNC },
121b7d3956bSstephh 	{ "is_on", PATHFUNC },
122b7d3956bSstephh 	{ "is_present", PATHFUNC },
123b7d3956bSstephh 	{ "is_type", PATHFUNC },
1248e7248e5SStephen Hanson 	{ "has_fault", PATHFUNC },
125b7d3956bSstephh 	{ "confprop", PATHFUNC },
126b7d3956bSstephh 	{ "confprop_defined", PATHFUNC },
1277c478bd9Sstevel@tonic-gate };
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Rwordslut is a lookup table of reserved words.  lhs is the word
1317c478bd9Sstevel@tonic-gate  * (in the string table) and the rhs is the token value returned
1327c478bd9Sstevel@tonic-gate  * by the yylex() for that word.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate static struct lut *Rwordslut;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static const struct {
1377c478bd9Sstevel@tonic-gate 	const char *suffix;
1387c478bd9Sstevel@tonic-gate 	const unsigned long long nsec;
1397c478bd9Sstevel@tonic-gate } Timesuffix[] = {
1407c478bd9Sstevel@tonic-gate 	{ "nanosecond",		1ULL },
1417c478bd9Sstevel@tonic-gate 	{ "nanoseconds",	1ULL },
1427c478bd9Sstevel@tonic-gate 	{ "nsec",		1ULL },
1437c478bd9Sstevel@tonic-gate 	{ "nsecs",		1ULL },
1447c478bd9Sstevel@tonic-gate 	{ "ns",			1ULL },
1457c478bd9Sstevel@tonic-gate 	{ "microsecond",	1000ULL },
1467c478bd9Sstevel@tonic-gate 	{ "microseconds",	1000ULL },
1477c478bd9Sstevel@tonic-gate 	{ "usec",		1000ULL },
1487c478bd9Sstevel@tonic-gate 	{ "usecs",		1000ULL },
1497c478bd9Sstevel@tonic-gate 	{ "us",			1000ULL },
1507c478bd9Sstevel@tonic-gate 	{ "millisecond",	1000000ULL },
1517c478bd9Sstevel@tonic-gate 	{ "milliseconds",	1000000ULL },
1527c478bd9Sstevel@tonic-gate 	{ "msec",		1000000ULL },
1537c478bd9Sstevel@tonic-gate 	{ "msecs",		1000000ULL },
1547c478bd9Sstevel@tonic-gate 	{ "ms",			1000000ULL },
1557c478bd9Sstevel@tonic-gate 	{ "second",		1000000000ULL },
1567c478bd9Sstevel@tonic-gate 	{ "seconds",		1000000000ULL },
1577c478bd9Sstevel@tonic-gate 	{ "s",			1000000000ULL },
1587c478bd9Sstevel@tonic-gate 	{ "minute",		1000000000ULL * 60 },
1597c478bd9Sstevel@tonic-gate 	{ "minutes",		1000000000ULL * 60 },
1607c478bd9Sstevel@tonic-gate 	{ "min",		1000000000ULL * 60 },
1617c478bd9Sstevel@tonic-gate 	{ "mins",		1000000000ULL * 60 },
1627c478bd9Sstevel@tonic-gate 	{ "m",			1000000000ULL * 60 },
1637c478bd9Sstevel@tonic-gate 	{ "hour",		1000000000ULL * 60 * 60 },
1647c478bd9Sstevel@tonic-gate 	{ "hours",		1000000000ULL * 60 * 60 },
1657c478bd9Sstevel@tonic-gate 	{ "hr",			1000000000ULL * 60 * 60 },
1667c478bd9Sstevel@tonic-gate 	{ "hrs",		1000000000ULL * 60 * 60 },
1677c478bd9Sstevel@tonic-gate 	{ "h",			1000000000ULL * 60 * 60 },
1687c478bd9Sstevel@tonic-gate 	{ "day",		1000000000ULL * 60 * 60 * 24 },
1697c478bd9Sstevel@tonic-gate 	{ "days",		1000000000ULL * 60 * 60 * 24 },
1707c478bd9Sstevel@tonic-gate 	{ "d",			1000000000ULL * 60 * 60 * 24 },
1717c478bd9Sstevel@tonic-gate 	{ "week",		1000000000ULL * 60 * 60 * 24 * 7 },
1727c478bd9Sstevel@tonic-gate 	{ "weeks",		1000000000ULL * 60 * 60 * 24 * 7 },
1737c478bd9Sstevel@tonic-gate 	{ "wk",			1000000000ULL * 60 * 60 * 24 * 7 },
1747c478bd9Sstevel@tonic-gate 	{ "wks",		1000000000ULL * 60 * 60 * 24 * 7 },
1757c478bd9Sstevel@tonic-gate 	{ "month",		1000000000ULL * 60 * 60 * 24 * 30 },
1767c478bd9Sstevel@tonic-gate 	{ "months",		1000000000ULL * 60 * 60 * 24 * 30 },
1777c478bd9Sstevel@tonic-gate 	{ "year",		1000000000ULL * 60 * 60 * 24 * 365 },
1787c478bd9Sstevel@tonic-gate 	{ "years",		1000000000ULL * 60 * 60 * 24 * 365 },
1797c478bd9Sstevel@tonic-gate 	{ "yr",			1000000000ULL * 60 * 60 * 24 * 365 },
1807c478bd9Sstevel@tonic-gate 	{ "yrs",		1000000000ULL * 60 * 60 * 24 * 365 },
1817c478bd9Sstevel@tonic-gate };
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * some wrappers around the general lut functions to provide type checking...
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static struct lut *
lex_s2i_lut_add(struct lut * root,const char * s,intptr_t i)188837416c3Scy lex_s2i_lut_add(struct lut *root, const char *s, intptr_t i)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate 	return (lut_add(root, (void *)s, (void *)i, NULL));
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate static int
lex_s2i_lut_lookup(struct lut * root,const char * s)1947c478bd9Sstevel@tonic-gate lex_s2i_lut_lookup(struct lut *root, const char *s)
1957c478bd9Sstevel@tonic-gate {
196837416c3Scy 	return ((intptr_t)lut_lookup(root, (void *)s, NULL));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static struct lut *
lex_s2ullp_lut_add(struct lut * root,const char * s,const unsigned long long * ullp)2007c478bd9Sstevel@tonic-gate lex_s2ullp_lut_add(struct lut *root, const char *s,
2017c478bd9Sstevel@tonic-gate     const unsigned long long *ullp)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	return (lut_add(root, (void *)s, (void *)ullp, NULL));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate const unsigned long long *
lex_s2ullp_lut_lookup(struct lut * root,const char * s)2077c478bd9Sstevel@tonic-gate lex_s2ullp_lut_lookup(struct lut *root, const char *s)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	return ((unsigned long long *)lut_lookup(root, (void *)s, NULL));
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * lex_init -- initialize the lexer with appropriate filenames & debug flags
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2177c478bd9Sstevel@tonic-gate void
lex_init(char ** av,const char * cppargs,int lexecho)2187c478bd9Sstevel@tonic-gate lex_init(char **av, const char *cppargs, int lexecho)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	int i;
2217c478bd9Sstevel@tonic-gate #ifdef	ESC
2227c478bd9Sstevel@tonic-gate 	const char *ptr;
2237c478bd9Sstevel@tonic-gate #endif	/* ESC */
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	Lexecho = lexecho;
2267c478bd9Sstevel@tonic-gate 	Tokcount = stats_new_counter("lex.tokens", "total tokens in", 1);
2277c478bd9Sstevel@tonic-gate 	Filecount = stats_new_counter("lex.files", "total files read", 0);
2287c478bd9Sstevel@tonic-gate 	Lexelapse = stats_new_elapse("lex.time", "elapsed lex/parse time", 1);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate #ifdef	ESC
2317c478bd9Sstevel@tonic-gate 	Cppargs = cppargs;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/* allow user to tell us where cpp is if it is some weird place */
2347c478bd9Sstevel@tonic-gate 	if (ptr = getenv("_ESC_CPP"))
2357c478bd9Sstevel@tonic-gate 		Cpp = ptr;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* and in case it takes some special stdargs */
2387c478bd9Sstevel@tonic-gate 	if (ptr = getenv("_ESC_CPP_STDARGS"))
2397c478bd9Sstevel@tonic-gate 		Cppstdargs = ptr;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* verify we can find cpp */
2427c478bd9Sstevel@tonic-gate 	if (access(Cpp, X_OK) < 0) {
2437c478bd9Sstevel@tonic-gate 		Cpp = "/usr/lib/cpp";
2447c478bd9Sstevel@tonic-gate 		if (access(Cpp, X_OK) < 0)
2457c478bd9Sstevel@tonic-gate 			out(O_DIE, "can't locate cpp");
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate #endif	/* ESC */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	Files = av;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/* verify we can find all the input files */
2527c478bd9Sstevel@tonic-gate 	while (*av) {
2537c478bd9Sstevel@tonic-gate 		if (strlen(*av) >= MAXTOK - strlen(Cpp) - 3)
2547c478bd9Sstevel@tonic-gate 			out(O_DIE, "filename too long: %.100s...", *av);
2557c478bd9Sstevel@tonic-gate 		if (access(*av, R_OK) < 0)
2567c478bd9Sstevel@tonic-gate 			out(O_DIE|O_SYS, "%s", *av);
2577c478bd9Sstevel@tonic-gate 		av++;
2587c478bd9Sstevel@tonic-gate 		stats_counter_bump(Filecount);
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/* put reserved words into the string table & a lookup table */
2627c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (Rwords) / sizeof (*Rwords); i++)
2637c478bd9Sstevel@tonic-gate 		Rwordslut = lex_s2i_lut_add(Rwordslut,
2647c478bd9Sstevel@tonic-gate 		    stable(Rwords[i].word), Rwords[i].val);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/* initialize table of timeval suffixes */
2677c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (Timesuffix) / sizeof (*Timesuffix); i++) {
2687c478bd9Sstevel@tonic-gate 		Timesuffixlut = lex_s2ullp_lut_add(Timesuffixlut,
2697c478bd9Sstevel@tonic-gate 		    stable(Timesuffix[i].suffix), &Timesuffix[i].nsec);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/* record start time */
2737c478bd9Sstevel@tonic-gate 	stats_elapse_start(Lexelapse);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate void
closefile(void)2777c478bd9Sstevel@tonic-gate closefile(void)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	if (Fp != NULL) {
2807c478bd9Sstevel@tonic-gate #ifdef	ESC
2817c478bd9Sstevel@tonic-gate 		if (pclose(Fp) > 0)
2827c478bd9Sstevel@tonic-gate 			out(O_DIE, "cpp errors while reading \"%s\", "
2837c478bd9Sstevel@tonic-gate 			    "bailing out.", Fileopened);
2847c478bd9Sstevel@tonic-gate #else
2857c478bd9Sstevel@tonic-gate 		(void) fclose(Fp);
2867c478bd9Sstevel@tonic-gate #endif	/* ESC */
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	Fp = NULL;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * yylex -- the lexer, called yylex() because that's what yacc wants
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate int
yylex()2967c478bd9Sstevel@tonic-gate yylex()
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int c;
2997c478bd9Sstevel@tonic-gate 	int nextc;
3007c478bd9Sstevel@tonic-gate 	char *ptr = Tok;
3017c478bd9Sstevel@tonic-gate 	char *eptr = &Tok[MAXTOK];
3027c478bd9Sstevel@tonic-gate 	const char *cptr;
3037c478bd9Sstevel@tonic-gate 	int startline;
3047c478bd9Sstevel@tonic-gate 	int val;
3057c478bd9Sstevel@tonic-gate 	static int bol = 1;	/* true if we're at beginning of line */
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	for (;;) {
3087c478bd9Sstevel@tonic-gate 		while (Fp == NULL) {
3098a40a695Sgavinm 			char ibuf[80];
3108a40a695Sgavinm 
3117c478bd9Sstevel@tonic-gate 			if (*Files == NULL)
3127c478bd9Sstevel@tonic-gate 				return (record(EOF, NULL));
3137c478bd9Sstevel@tonic-gate 			Fileopened = stable(*Files++);
3147c478bd9Sstevel@tonic-gate #ifdef	ESC
3157c478bd9Sstevel@tonic-gate 			sprintf(Tok, "%s %s %s %s",
3167c478bd9Sstevel@tonic-gate 			    Cpp, Cppstdargs, Cppargs, Fileopened);
3177c478bd9Sstevel@tonic-gate 			if ((Fp = popen(Tok, "r")) == NULL)
3187c478bd9Sstevel@tonic-gate 				out(O_DIE|O_SYS, "%s", Tok);
3197c478bd9Sstevel@tonic-gate #else
3208a40a695Sgavinm 			Fp = eftread_fopen(Fileopened, ibuf, sizeof (ibuf));
3217c478bd9Sstevel@tonic-gate #endif	/* ESC */
3227c478bd9Sstevel@tonic-gate 			Line = 1;
3237c478bd9Sstevel@tonic-gate 			bol = 1;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 			/* add name to stats for visibility */
3267c478bd9Sstevel@tonic-gate 			if (Fp != NULL) {
3277c478bd9Sstevel@tonic-gate 				static int fnum;
3287c478bd9Sstevel@tonic-gate 				char nbuf[100];
3297c478bd9Sstevel@tonic-gate 				struct filestats *nfs = MALLOC(sizeof (*nfs));
3308a40a695Sgavinm 
3318a40a695Sgavinm 				(void) sprintf(nbuf, "lex.file%d", fnum);
3327c478bd9Sstevel@tonic-gate 				nfs->stats = stats_new_string(nbuf, "", 0);
3337c478bd9Sstevel@tonic-gate 				stats_string_set(nfs->stats, Fileopened);
3348a40a695Sgavinm 
3358a40a695Sgavinm 				if (ibuf[0] != '\0') {
3368a40a695Sgavinm 					(void) sprintf(nbuf, "lex.file%d-ident",
3378a40a695Sgavinm 					    fnum);
3388a40a695Sgavinm 					nfs->idstats =
3398a40a695Sgavinm 					    stats_new_string(nbuf, "", 0);
3408a40a695Sgavinm 					stats_string_set(nfs->idstats, ibuf);
3418a40a695Sgavinm 				} else {
3428a40a695Sgavinm 					nfs->idstats = NULL;
3438a40a695Sgavinm 				}
3448a40a695Sgavinm 
3457c478bd9Sstevel@tonic-gate 				nfs->next = Fstats;
3467c478bd9Sstevel@tonic-gate 				Fstats = nfs;
3478a40a695Sgavinm 				fnum++;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		switch (c = getc(Fp)) {
3527c478bd9Sstevel@tonic-gate 		case '#':
3537c478bd9Sstevel@tonic-gate 			/* enforce that we're at beginning of line */
3547c478bd9Sstevel@tonic-gate 			if (!bol)
3557c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 			while ((c = getc(Fp)) != EOF &&
3587c478bd9Sstevel@tonic-gate 			    (c == ' ' || c == '\t'))
3597c478bd9Sstevel@tonic-gate 				;
3607c478bd9Sstevel@tonic-gate 			if (!isdigit(c)) {
3617c478bd9Sstevel@tonic-gate 				/*
3627c478bd9Sstevel@tonic-gate 				 * three cases here:
3637c478bd9Sstevel@tonic-gate 				 *	#pragma
3647c478bd9Sstevel@tonic-gate 				 *	#ident
3657c478bd9Sstevel@tonic-gate 				 *	#something-we-don't-understand
3667c478bd9Sstevel@tonic-gate 				 * anything we don't expect we just ignore.
3677c478bd9Sstevel@tonic-gate 				 */
3687c478bd9Sstevel@tonic-gate 				*ptr++ = c;
3697c478bd9Sstevel@tonic-gate 				while ((c = getc(Fp)) != EOF && isalnum(c))
3707c478bd9Sstevel@tonic-gate 					if (ptr < eptr - 1)
3717c478bd9Sstevel@tonic-gate 						*ptr++ = c;
3727c478bd9Sstevel@tonic-gate 				*ptr++ = '\0';
3737c478bd9Sstevel@tonic-gate 				if (strcmp(Tok, "pragma") == 0) {
3747c478bd9Sstevel@tonic-gate 					/* skip white space */
3757c478bd9Sstevel@tonic-gate 					while ((c = getc(Fp)) != EOF &&
3767c478bd9Sstevel@tonic-gate 					    (c == ' ' || c == '\t'))
3777c478bd9Sstevel@tonic-gate 						;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 					if (c == EOF || c == '\n')
3807c478bd9Sstevel@tonic-gate 						outfl(O_DIE, File, Line,
3817c478bd9Sstevel@tonic-gate 						    "bad #pragma");
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 					/* pull in next token */
3847c478bd9Sstevel@tonic-gate 					ptr = Tok;
3857c478bd9Sstevel@tonic-gate 					*ptr++ = c;
3867c478bd9Sstevel@tonic-gate 					while ((c = getc(Fp)) != EOF &&
3877c478bd9Sstevel@tonic-gate 					    !isspace(c))
3887c478bd9Sstevel@tonic-gate 						if (ptr < eptr - 1)
3897c478bd9Sstevel@tonic-gate 							*ptr++ = c;
3907c478bd9Sstevel@tonic-gate 					*ptr++ = '\0';
3917c478bd9Sstevel@tonic-gate 					(void) ungetc(c, Fp);
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 					dopragma(Tok);
3947c478bd9Sstevel@tonic-gate 				} else if (strcmp(Tok, "ident") == 0)
3957c478bd9Sstevel@tonic-gate 					doident();
3967c478bd9Sstevel@tonic-gate 			} else {
3977c478bd9Sstevel@tonic-gate 				/* handle file & line info from cpp */
3987c478bd9Sstevel@tonic-gate 				Line = 0;
3997c478bd9Sstevel@tonic-gate 				do {
4007c478bd9Sstevel@tonic-gate 					if (!isdigit(c))
4017c478bd9Sstevel@tonic-gate 						break;
4027c478bd9Sstevel@tonic-gate 					Line = Line * 10 + c - '0';
4037c478bd9Sstevel@tonic-gate 				} while ((c = getc(Fp)) != EOF);
4047c478bd9Sstevel@tonic-gate 				Line--;		/* newline will increment it */
4057c478bd9Sstevel@tonic-gate 				while (c != EOF && isspace(c))
4067c478bd9Sstevel@tonic-gate 					c = getc(Fp);
4077c478bd9Sstevel@tonic-gate 				if (c != '"')
4087c478bd9Sstevel@tonic-gate 					outfl(O_DIE, File, Line,
4097c478bd9Sstevel@tonic-gate 					    "bad # statement (file name)");
4107c478bd9Sstevel@tonic-gate 				while ((c = getc(Fp)) != EOF && c != '"')
4117c478bd9Sstevel@tonic-gate 					if (ptr < eptr - 1)
4127c478bd9Sstevel@tonic-gate 						*ptr++ = c;
4137c478bd9Sstevel@tonic-gate 				*ptr++ = '\0';
4147c478bd9Sstevel@tonic-gate 				if (c != '"')
4157c478bd9Sstevel@tonic-gate 					outfl(O_DIE, File, Line,
4167c478bd9Sstevel@tonic-gate 					    "bad # statement (quotes)");
4177c478bd9Sstevel@tonic-gate 				File = stable(Tok);
4187c478bd9Sstevel@tonic-gate 			}
4197c478bd9Sstevel@tonic-gate 			/* skip the rest of the cpp line */
4207c478bd9Sstevel@tonic-gate 			while ((c = getc(Fp)) != EOF && c != '\n' && c != '\r')
4217c478bd9Sstevel@tonic-gate 				;
4227c478bd9Sstevel@tonic-gate 			if (c == EOF)
4237c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
4247c478bd9Sstevel@tonic-gate 			else
4257c478bd9Sstevel@tonic-gate 				(void) ungetc(c, Fp);
4267c478bd9Sstevel@tonic-gate 			ptr = Tok;
4277c478bd9Sstevel@tonic-gate 			break;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		case EOF:
4307c478bd9Sstevel@tonic-gate 			closefile();
4317c478bd9Sstevel@tonic-gate 			continue;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		case '\n':
4347c478bd9Sstevel@tonic-gate 			Line++;
4357c478bd9Sstevel@tonic-gate 			bol = 1;
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		case '\r':
4397c478bd9Sstevel@tonic-gate 		case ' ':
4407c478bd9Sstevel@tonic-gate 		case '\t':
4417c478bd9Sstevel@tonic-gate 			bol = 0;
4427c478bd9Sstevel@tonic-gate 			break;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		case '/':
4457c478bd9Sstevel@tonic-gate 			bol = 0;
4467c478bd9Sstevel@tonic-gate 			/* comment handling */
4477c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == EOF)
4487c478bd9Sstevel@tonic-gate 				outfl(O_DIE, File, Line, "unexpected EOF");
4497c478bd9Sstevel@tonic-gate 			else if (nextc == '*') {
4507c478bd9Sstevel@tonic-gate 				startline = Line;
4517c478bd9Sstevel@tonic-gate 				while ((c = getc(Fp)) != EOF) {
4527c478bd9Sstevel@tonic-gate 					if (c == '\n')
4537c478bd9Sstevel@tonic-gate 						Line++;
4547c478bd9Sstevel@tonic-gate 					else if (c == '*' &&
455837416c3Scy 					    (((c = getc(Fp)) == EOF) ||
456837416c3Scy 					    (c == '/')))
4577c478bd9Sstevel@tonic-gate 						break;
4587c478bd9Sstevel@tonic-gate 				}
4597c478bd9Sstevel@tonic-gate 				if (c == EOF) {
4607c478bd9Sstevel@tonic-gate 					outfl(O_DIE, File, Line,
4617c478bd9Sstevel@tonic-gate 					    "end of comment not seen "
4627c478bd9Sstevel@tonic-gate 					    "(started on line %d)",
4637c478bd9Sstevel@tonic-gate 					    startline);
4647c478bd9Sstevel@tonic-gate 				}
4657c478bd9Sstevel@tonic-gate 			} else {
4667c478bd9Sstevel@tonic-gate 				/* wasn't a comment, return the '/' token */
4677c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
4687c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
4697c478bd9Sstevel@tonic-gate 			}
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		case '"': {
4737c478bd9Sstevel@tonic-gate 			int prevc;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			bol = 0;
4767c478bd9Sstevel@tonic-gate 			prevc = '\0';
4777c478bd9Sstevel@tonic-gate 			/* quoted string handling */
4787c478bd9Sstevel@tonic-gate 			startline = Line;
4797c478bd9Sstevel@tonic-gate 			for (;;) {
4807c478bd9Sstevel@tonic-gate 				c = getc(Fp);
4817c478bd9Sstevel@tonic-gate 				if (c == EOF)
4827c478bd9Sstevel@tonic-gate 					outfl(O_DIE, File, Line,
4837c478bd9Sstevel@tonic-gate 					    "end of string not seen "
4847c478bd9Sstevel@tonic-gate 					    "(started on line %d)",
4857c478bd9Sstevel@tonic-gate 					    startline);
4867c478bd9Sstevel@tonic-gate 				else if (c == '\n')
4877c478bd9Sstevel@tonic-gate 					Line++;
4887c478bd9Sstevel@tonic-gate 				else if (c == '"' && prevc != '\\')
4897c478bd9Sstevel@tonic-gate 					break;
4907c478bd9Sstevel@tonic-gate 				else if (ptr < eptr)
4917c478bd9Sstevel@tonic-gate 					*ptr++ = c;
4927c478bd9Sstevel@tonic-gate 				prevc = c;
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 			if (ptr >= eptr)
4957c478bd9Sstevel@tonic-gate 				out(O_DIE, File, Line, "string too long");
4967c478bd9Sstevel@tonic-gate 			*ptr++ = '\0';
4977c478bd9Sstevel@tonic-gate 			return (record(QUOTE, stable(Tok)));
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		case '&':
5007c478bd9Sstevel@tonic-gate 			bol = 0;
5017c478bd9Sstevel@tonic-gate 			/* && */
5027c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '&')
5037c478bd9Sstevel@tonic-gate 				return (record(AND, NULL));
5047c478bd9Sstevel@tonic-gate 			else {
5057c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5067c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5077c478bd9Sstevel@tonic-gate 			}
5087c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5097c478bd9Sstevel@tonic-gate 			break;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		case '|':
5127c478bd9Sstevel@tonic-gate 			bol = 0;
5137c478bd9Sstevel@tonic-gate 			/* || */
5147c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '|')
5157c478bd9Sstevel@tonic-gate 				return (record(OR, NULL));
5167c478bd9Sstevel@tonic-gate 			else {
5177c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5187c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5217c478bd9Sstevel@tonic-gate 			break;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		case '!':
5247c478bd9Sstevel@tonic-gate 			bol = 0;
5257c478bd9Sstevel@tonic-gate 			/* ! or != */
5267c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '=')
5277c478bd9Sstevel@tonic-gate 				return (record(NE, NULL));
5287c478bd9Sstevel@tonic-gate 			else {
5297c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5307c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5317c478bd9Sstevel@tonic-gate 			}
5327c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5337c478bd9Sstevel@tonic-gate 			break;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		case '=':
5367c478bd9Sstevel@tonic-gate 			bol = 0;
5377c478bd9Sstevel@tonic-gate 			/* == */
5387c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '=')
5397c478bd9Sstevel@tonic-gate 				return (record(EQ, NULL));
5407c478bd9Sstevel@tonic-gate 			else {
5417c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5427c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5457c478bd9Sstevel@tonic-gate 			break;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		case '-':
5487c478bd9Sstevel@tonic-gate 			bol = 0;
5497c478bd9Sstevel@tonic-gate 			/* -> */
5507c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '>')
5517c478bd9Sstevel@tonic-gate 				return (record(ARROW, stable(Tok)));
5527c478bd9Sstevel@tonic-gate 			else {
5537c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5547c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5557c478bd9Sstevel@tonic-gate 			}
5567c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5577c478bd9Sstevel@tonic-gate 			break;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		case '<':
5607c478bd9Sstevel@tonic-gate 			bol = 0;
5617c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '=')
5627c478bd9Sstevel@tonic-gate 				/* <= */
5637c478bd9Sstevel@tonic-gate 				return (record(LE, NULL));
5647c478bd9Sstevel@tonic-gate 			else if (nextc == '<')
5657c478bd9Sstevel@tonic-gate 				/* << */
5667c478bd9Sstevel@tonic-gate 				return (record(LSHIFT, NULL));
5677c478bd9Sstevel@tonic-gate 			else {
5687c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5697c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5727c478bd9Sstevel@tonic-gate 			break;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		case '>':
5757c478bd9Sstevel@tonic-gate 			bol = 0;
5767c478bd9Sstevel@tonic-gate 			if ((nextc = getc(Fp)) == '=')
5777c478bd9Sstevel@tonic-gate 				/* >= */
5787c478bd9Sstevel@tonic-gate 				return (record(GE, NULL));
5797c478bd9Sstevel@tonic-gate 			else if (nextc == '>')
5807c478bd9Sstevel@tonic-gate 				/* >> */
5817c478bd9Sstevel@tonic-gate 				return (record(RSHIFT, NULL));
5827c478bd9Sstevel@tonic-gate 			else {
5837c478bd9Sstevel@tonic-gate 				(void) ungetc(nextc, Fp);
5847c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
5877c478bd9Sstevel@tonic-gate 			break;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		default:
5907c478bd9Sstevel@tonic-gate 			bol = 0;
5917c478bd9Sstevel@tonic-gate 			if (isdigit(c)) {
5927c478bd9Sstevel@tonic-gate 				int base;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 				/* collect rest of number */
5957c478bd9Sstevel@tonic-gate 				if (c == '0') {
5967c478bd9Sstevel@tonic-gate 					*ptr++ = c;
5977c478bd9Sstevel@tonic-gate 					if ((c = getc(Fp)) == EOF) {
5987c478bd9Sstevel@tonic-gate 						*ptr++ = '\0';
5997c478bd9Sstevel@tonic-gate 						return (record(NUMBER,
6007c478bd9Sstevel@tonic-gate 						    stable(Tok)));
6017c478bd9Sstevel@tonic-gate 					} else if (c == 'x' || c == 'X') {
6027c478bd9Sstevel@tonic-gate 						*ptr++ = c;
6037c478bd9Sstevel@tonic-gate 						base = 16;
6047c478bd9Sstevel@tonic-gate 					} else {
6057c478bd9Sstevel@tonic-gate 						(void) ungetc(c, Fp);
6067c478bd9Sstevel@tonic-gate 						base = 8;
6077c478bd9Sstevel@tonic-gate 					}
6087c478bd9Sstevel@tonic-gate 				} else {
6097c478bd9Sstevel@tonic-gate 					*ptr++ = c;
6107c478bd9Sstevel@tonic-gate 					base = 10;
6117c478bd9Sstevel@tonic-gate 				}
6127c478bd9Sstevel@tonic-gate 				while ((c = getc(Fp)) != EOF) {
6137c478bd9Sstevel@tonic-gate 					if (ptr >= eptr)
6147c478bd9Sstevel@tonic-gate 						out(O_DIE, File, Line,
6157c478bd9Sstevel@tonic-gate 						    "number too long");
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 					switch (base) {
6187c478bd9Sstevel@tonic-gate 					case 16:
6197c478bd9Sstevel@tonic-gate 						if (c >= 'a' && c <= 'f' ||
6207c478bd9Sstevel@tonic-gate 						    c >= 'A' && c <= 'F') {
6217c478bd9Sstevel@tonic-gate 							*ptr++ = c;
6227c478bd9Sstevel@tonic-gate 							continue;
6237c478bd9Sstevel@tonic-gate 						}
6247c478bd9Sstevel@tonic-gate 						/*FALLTHRU*/
6257c478bd9Sstevel@tonic-gate 					case 10:
6267c478bd9Sstevel@tonic-gate 						if (c >= '8' && c <= '9') {
6277c478bd9Sstevel@tonic-gate 							*ptr++ = c;
6287c478bd9Sstevel@tonic-gate 							continue;
6297c478bd9Sstevel@tonic-gate 						}
6307c478bd9Sstevel@tonic-gate 						/*FALLTHRU*/
6317c478bd9Sstevel@tonic-gate 					case 8:
6327c478bd9Sstevel@tonic-gate 						if (c >= '0' && c <= '7') {
6337c478bd9Sstevel@tonic-gate 							*ptr++ = c;
6347c478bd9Sstevel@tonic-gate 							continue;
6357c478bd9Sstevel@tonic-gate 						}
6367c478bd9Sstevel@tonic-gate 						/* not valid for this base */
6377c478bd9Sstevel@tonic-gate 						*ptr++ = '\0';
6387c478bd9Sstevel@tonic-gate 						(void) ungetc(c, Fp);
6397c478bd9Sstevel@tonic-gate 						return (record(NUMBER,
6407c478bd9Sstevel@tonic-gate 						    stable(Tok)));
6417c478bd9Sstevel@tonic-gate 					}
6427c478bd9Sstevel@tonic-gate 				}
6437c478bd9Sstevel@tonic-gate 				*ptr++ = '\0';
6447c478bd9Sstevel@tonic-gate 				return (record(NUMBER, stable(Tok)));
6457c478bd9Sstevel@tonic-gate 			} else if (isalpha(c)) {
6467c478bd9Sstevel@tonic-gate 				/* collect identifier */
6477c478bd9Sstevel@tonic-gate 				*ptr++ = c;
6487c478bd9Sstevel@tonic-gate 				for (;;) {
6497c478bd9Sstevel@tonic-gate 					c = getc(Fp);
6507c478bd9Sstevel@tonic-gate 					if ((isalnum(c) || c == '_') &&
651837416c3Scy 					    ptr < eptr)
6527c478bd9Sstevel@tonic-gate 						*ptr++ = c;
6537c478bd9Sstevel@tonic-gate 					else {
6547c478bd9Sstevel@tonic-gate 						(void) ungetc(c, Fp);
6557c478bd9Sstevel@tonic-gate 						break;
6567c478bd9Sstevel@tonic-gate 					}
6577c478bd9Sstevel@tonic-gate 				}
6587c478bd9Sstevel@tonic-gate 				if (ptr >= eptr)
6597c478bd9Sstevel@tonic-gate 					out(O_DIE, File, Line,
6607c478bd9Sstevel@tonic-gate 					    "identifier too long");
6617c478bd9Sstevel@tonic-gate 				*ptr++ = '\0';
6627c478bd9Sstevel@tonic-gate 				cptr = stable(Tok);
6637c478bd9Sstevel@tonic-gate 				if (val = lex_s2i_lut_lookup(Rwordslut, cptr)) {
6647c478bd9Sstevel@tonic-gate 					return (record(val, cptr));
6657c478bd9Sstevel@tonic-gate 				}
6667c478bd9Sstevel@tonic-gate 				return (record(ID, cptr));
6677c478bd9Sstevel@tonic-gate 			} else
6687c478bd9Sstevel@tonic-gate 				return (record(c, NULL));
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate /*
6757c478bd9Sstevel@tonic-gate  * the record()/dumpline() routines are used to track & report
6767c478bd9Sstevel@tonic-gate  * the list of tokens seen on a given line.  this is used in two ways.
6777c478bd9Sstevel@tonic-gate  * first, syntax errors found by the parser are reported by us (via
6787c478bd9Sstevel@tonic-gate  * yyerror()) and we tack on the tokens processed so far on the current
6797c478bd9Sstevel@tonic-gate  * line to help indicate exactly where the error is.  second, if "lexecho"
6807c478bd9Sstevel@tonic-gate  * debugging is turned on, these routines provide it.
6817c478bd9Sstevel@tonic-gate  */
6827c478bd9Sstevel@tonic-gate #define	MAXRECORD 1000
6837c478bd9Sstevel@tonic-gate static int Recordedline;
6847c478bd9Sstevel@tonic-gate static struct {
6857c478bd9Sstevel@tonic-gate 	int tok;
6867c478bd9Sstevel@tonic-gate 	const char *s;
6877c478bd9Sstevel@tonic-gate } Recorded[MAXRECORD];
6887c478bd9Sstevel@tonic-gate static int Recordnext;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate static int
record(int tok,const char * s)6917c478bd9Sstevel@tonic-gate record(int tok, const char *s)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	stats_counter_bump(Tokcount);
6947c478bd9Sstevel@tonic-gate 	if (Line != Recordedline) {
6957c478bd9Sstevel@tonic-gate 		/* starting new line, dump out the previous line */
6967c478bd9Sstevel@tonic-gate 		if (Lexecho && Recordedline) {
6977c478bd9Sstevel@tonic-gate 			outfl(O_NONL, File, Recordedline, "lex: ");
6987c478bd9Sstevel@tonic-gate 			dumpline(O_OK);
6997c478bd9Sstevel@tonic-gate 		}
7007c478bd9Sstevel@tonic-gate 		Recordedline = Line;
7017c478bd9Sstevel@tonic-gate 		Recordnext = 0;
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	if (Recordnext >= MAXRECORD)
7047c478bd9Sstevel@tonic-gate 		outfl(O_DIE, File, Line, "line too long, bailing out");
7057c478bd9Sstevel@tonic-gate 	Recorded[Recordnext].tok = tok;
7067c478bd9Sstevel@tonic-gate 	Recorded[Recordnext++].s = s;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	yylval.tok.s = s;
7097c478bd9Sstevel@tonic-gate 	yylval.tok.file = File;
7107c478bd9Sstevel@tonic-gate 	yylval.tok.line = Line;
7117c478bd9Sstevel@tonic-gate 	return (tok);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7157c478bd9Sstevel@tonic-gate static void
dumpline(int flags)7167c478bd9Sstevel@tonic-gate dumpline(int flags)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	int i;
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate 	for (i = 0; i < Recordnext; i++)
7217c478bd9Sstevel@tonic-gate 		if (Recorded[i].s && Recorded[i].tok != ARROW)
7227c478bd9Sstevel@tonic-gate 			switch (Recorded[i].tok) {
7237c478bd9Sstevel@tonic-gate 			case T_QUOTE:
7247c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " \"%s\"",
725837416c3Scy 				    Recorded[i].s);
7267c478bd9Sstevel@tonic-gate 				break;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 			default:
7297c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " %s",
730837416c3Scy 				    Recorded[i].s);
7317c478bd9Sstevel@tonic-gate 				break;
7327c478bd9Sstevel@tonic-gate 			}
7337c478bd9Sstevel@tonic-gate 		else
7347c478bd9Sstevel@tonic-gate 			switch (Recorded[i].tok) {
7357c478bd9Sstevel@tonic-gate 			case EOF:
7367c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " EOF");
7377c478bd9Sstevel@tonic-gate 				break;
7387c478bd9Sstevel@tonic-gate 			case ARROW:
7397c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " ->%s",
740837416c3Scy 				    Recorded[i].s);
7417c478bd9Sstevel@tonic-gate 				break;
7427c478bd9Sstevel@tonic-gate 			case EQ:
7437c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " ==");
7447c478bd9Sstevel@tonic-gate 				break;
7457c478bd9Sstevel@tonic-gate 			case NE:
7467c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " !=");
7477c478bd9Sstevel@tonic-gate 				break;
7487c478bd9Sstevel@tonic-gate 			case OR:
7497c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " ||");
7507c478bd9Sstevel@tonic-gate 				break;
7517c478bd9Sstevel@tonic-gate 			case AND:
7527c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " &&");
7537c478bd9Sstevel@tonic-gate 				break;
7547c478bd9Sstevel@tonic-gate 			case LE:
7557c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " <=");
7567c478bd9Sstevel@tonic-gate 				break;
7577c478bd9Sstevel@tonic-gate 			case GE:
7587c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " >=");
7597c478bd9Sstevel@tonic-gate 				break;
7607c478bd9Sstevel@tonic-gate 			case LSHIFT:
7617c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " <<");
7627c478bd9Sstevel@tonic-gate 				break;
7637c478bd9Sstevel@tonic-gate 			case RSHIFT:
7647c478bd9Sstevel@tonic-gate 				out(flags|O_NONL, " >>");
7657c478bd9Sstevel@tonic-gate 				break;
7667c478bd9Sstevel@tonic-gate 			default:
7677c478bd9Sstevel@tonic-gate 				if (isprint(Recorded[i].tok))
7687c478bd9Sstevel@tonic-gate 					out(flags|O_NONL, " %c",
769837416c3Scy 					    Recorded[i].tok);
7707c478bd9Sstevel@tonic-gate 				else
7717c478bd9Sstevel@tonic-gate 					out(flags|O_NONL, " '\\%03o'",
772837416c3Scy 					    Recorded[i].tok);
7737c478bd9Sstevel@tonic-gate 				break;
7747c478bd9Sstevel@tonic-gate 			}
7757c478bd9Sstevel@tonic-gate 	out(flags, NULL);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate  * yyerror -- report a pareser error, called yyerror because yacc wants it
7807c478bd9Sstevel@tonic-gate  */
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate void
yyerror(const char * s)7837c478bd9Sstevel@tonic-gate yyerror(const char *s)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	Errcount++;
7867c478bd9Sstevel@tonic-gate 	outfl(O_ERR|O_NONL, File, Line, "%s, tokens: ", s);
7877c478bd9Sstevel@tonic-gate 	dumpline(O_ERR);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * doident -- handle "#pragma ident" directives
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate static void
doident()7947c478bd9Sstevel@tonic-gate doident()
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	int c;
7977c478bd9Sstevel@tonic-gate 	char *ptr = Tok;
7987c478bd9Sstevel@tonic-gate 	char *eptr = &Tok[MAXTOK];
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/* skip white space and quotes */
8017c478bd9Sstevel@tonic-gate 	while ((c = getc(Fp)) != EOF &&
8027c478bd9Sstevel@tonic-gate 	    (c == ' ' || c == '\t' || c == '"'))
8037c478bd9Sstevel@tonic-gate 		;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	if (c == EOF || c == '\n')
8067c478bd9Sstevel@tonic-gate 		outfl(O_DIE, File, Line, "bad ident");
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/* pull in next token */
8097c478bd9Sstevel@tonic-gate 	ptr = Tok;
8107c478bd9Sstevel@tonic-gate 	*ptr++ = c;
8117c478bd9Sstevel@tonic-gate 	while ((c = getc(Fp)) != EOF && c != '"' && c != '\n')
8127c478bd9Sstevel@tonic-gate 		if (ptr < eptr - 1)
8137c478bd9Sstevel@tonic-gate 			*ptr++ = c;
8147c478bd9Sstevel@tonic-gate 	*ptr++ = '\0';
8157c478bd9Sstevel@tonic-gate 	if (c != '\n') {
8167c478bd9Sstevel@tonic-gate 		/* skip to end of line (including close quote, if any) */
8177c478bd9Sstevel@tonic-gate 		while ((c = getc(Fp)) != EOF && c != '\n')
8187c478bd9Sstevel@tonic-gate 			;
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 	(void) ungetc(c, Fp);
8217c478bd9Sstevel@tonic-gate 	Ident = lut_add(Ident, (void *)stable(Tok), (void *)0, NULL);
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	outfl(O_VERB, File, Line, "pragma set: ident \"%s\"", Tok);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * dodictionary -- handle "#pragma dictionary" directives
8287c478bd9Sstevel@tonic-gate  */
8297c478bd9Sstevel@tonic-gate static void
dodictionary()8307c478bd9Sstevel@tonic-gate dodictionary()
8317c478bd9Sstevel@tonic-gate {
8327c478bd9Sstevel@tonic-gate 	int c;
8337c478bd9Sstevel@tonic-gate 	char *ptr = Tok;
8347c478bd9Sstevel@tonic-gate 	char *eptr = &Tok[MAXTOK];
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	/* skip white space and quotes */
8377c478bd9Sstevel@tonic-gate 	while ((c = getc(Fp)) != EOF &&
8387c478bd9Sstevel@tonic-gate 	    (c == ' ' || c == '\t' || c == '"'))
8397c478bd9Sstevel@tonic-gate 		;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	if (c == EOF || c == '\n')
8427c478bd9Sstevel@tonic-gate 		outfl(O_DIE, File, Line, "bad dictionary");
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/* pull in next token */
8457c478bd9Sstevel@tonic-gate 	ptr = Tok;
8467c478bd9Sstevel@tonic-gate 	*ptr++ = c;
8477c478bd9Sstevel@tonic-gate 	while ((c = getc(Fp)) != EOF && c != '"' && c != '\n')
8487c478bd9Sstevel@tonic-gate 		if (ptr < eptr - 1)
8497c478bd9Sstevel@tonic-gate 			*ptr++ = c;
8507c478bd9Sstevel@tonic-gate 	*ptr++ = '\0';
8517c478bd9Sstevel@tonic-gate 	if (c != '\n') {
8527c478bd9Sstevel@tonic-gate 		/* skip to end of line (including close quote, if any) */
8537c478bd9Sstevel@tonic-gate 		while ((c = getc(Fp)) != EOF && c != '\n')
8547c478bd9Sstevel@tonic-gate 			;
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 	(void) ungetc(c, Fp);
8577c478bd9Sstevel@tonic-gate 	Dicts = lut_add(Dicts, (void *)stable(Tok), (void *)0, NULL);
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	outfl(O_VERB, File, Line, "pragma set: dictionary \"%s\"", Tok);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * doallow_cycles -- handle "#pragma allow_cycles" directives
8647c478bd9Sstevel@tonic-gate  */
8657c478bd9Sstevel@tonic-gate static void
doallow_cycles()8667c478bd9Sstevel@tonic-gate doallow_cycles()
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	int c;
8697c478bd9Sstevel@tonic-gate 	char *ptr = Tok;
8707c478bd9Sstevel@tonic-gate 	char *eptr = &Tok[MAXTOK];
8717c478bd9Sstevel@tonic-gate 	unsigned long long newlevel;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * by default the compiler does not allow cycles or loops
8757c478bd9Sstevel@tonic-gate 	 * in propagations.  when cycles are encountered, the
8767c478bd9Sstevel@tonic-gate 	 * compiler prints out an error message.
8777c478bd9Sstevel@tonic-gate 	 *
8787c478bd9Sstevel@tonic-gate 	 *   "#pragma allow_cycles" and
8797c478bd9Sstevel@tonic-gate 	 *   "#pragma allow_cycles 0"
8807c478bd9Sstevel@tonic-gate 	 * allow cycles, but any such cycle will produce a warning
8817c478bd9Sstevel@tonic-gate 	 * message.
8827c478bd9Sstevel@tonic-gate 	 *
8837c478bd9Sstevel@tonic-gate 	 *   "#pragma allow_cycles N"
8847c478bd9Sstevel@tonic-gate 	 * with N > 0 will allow cycles and not produce any
8857c478bd9Sstevel@tonic-gate 	 * warning messages.
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/* skip white space and quotes */
8897c478bd9Sstevel@tonic-gate 	while ((c = getc(Fp)) != EOF &&
8907c478bd9Sstevel@tonic-gate 	    (c == ' ' || c == '\t' || c == '"'))
8917c478bd9Sstevel@tonic-gate 		;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	if (c == EOF || c == '\n')
8947c478bd9Sstevel@tonic-gate 		newlevel = 0ULL;
8957c478bd9Sstevel@tonic-gate 	else {
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		/* pull in next token */
8987c478bd9Sstevel@tonic-gate 		ptr = Tok;
8997c478bd9Sstevel@tonic-gate 		*ptr++ = c;
9007c478bd9Sstevel@tonic-gate 		while ((c = getc(Fp)) != EOF && c != '"' && c != '\n')
9017c478bd9Sstevel@tonic-gate 			if (ptr < eptr - 1)
9027c478bd9Sstevel@tonic-gate 				*ptr++ = c;
9037c478bd9Sstevel@tonic-gate 		*ptr++ = '\0';
9047c478bd9Sstevel@tonic-gate 		if (c != '\n') {
9057c478bd9Sstevel@tonic-gate 			/* skip to end of line */
9067c478bd9Sstevel@tonic-gate 			while ((c = getc(Fp)) != EOF && c != '\n')
9077c478bd9Sstevel@tonic-gate 				;
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 		newlevel = strtoll(Tok, NULL, 0);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	(void) ungetc(c, Fp);
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	(void) check_cycle_level(newlevel);
9147c478bd9Sstevel@tonic-gate 	outfl(O_VERB, File, Line,
9157c478bd9Sstevel@tonic-gate 	    "pragma set: allow_cycles (%s)",
916837416c3Scy 	    newlevel ? "no warnings" : "with warnings");
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate /*
9207c478bd9Sstevel@tonic-gate  * dopragma -- handle #pragma directives
9217c478bd9Sstevel@tonic-gate  */
9227c478bd9Sstevel@tonic-gate static void
dopragma(const char * tok)9237c478bd9Sstevel@tonic-gate dopragma(const char *tok)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	if (strcmp(tok, "ident") == 0)
9267c478bd9Sstevel@tonic-gate 		doident();
9277c478bd9Sstevel@tonic-gate 	else if (strcmp(tok, "dictionary") == 0)
9287c478bd9Sstevel@tonic-gate 		dodictionary();
9297c478bd9Sstevel@tonic-gate 	else if (strcmp(tok, "new_errors_only") == 0) {
9307c478bd9Sstevel@tonic-gate 		if (Pragma_new_errors_only++ == 0)
9317c478bd9Sstevel@tonic-gate 			outfl(O_VERB, File, Line,
9327c478bd9Sstevel@tonic-gate 			    "pragma set: new_errors_only");
9337c478bd9Sstevel@tonic-gate 	} else if (strcmp(tok, "trust_ereports") == 0) {
9347c478bd9Sstevel@tonic-gate 		if (Pragma_trust_ereports++ == 0)
9357c478bd9Sstevel@tonic-gate 			outfl(O_VERB, File, Line,
9367c478bd9Sstevel@tonic-gate 			    "pragma set: trust_ereports");
9377c478bd9Sstevel@tonic-gate 	} else if (strcmp(tok, "allow_cycles") == 0)
9387c478bd9Sstevel@tonic-gate 		doallow_cycles();
9397c478bd9Sstevel@tonic-gate 	else
9407c478bd9Sstevel@tonic-gate 		outfl(O_VERB, File, Line,
9417c478bd9Sstevel@tonic-gate 		    "unknown pragma ignored: \"%s\"", tok);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  * lex_fini -- finalize the lexer
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate int
lex_fini(void)9497c478bd9Sstevel@tonic-gate lex_fini(void)
9507c478bd9Sstevel@tonic-gate {
9517c478bd9Sstevel@tonic-gate 	stats_elapse_stop(Lexelapse);
9527c478bd9Sstevel@tonic-gate 	closefile();
9537c478bd9Sstevel@tonic-gate 	if (Lexecho) {
9547c478bd9Sstevel@tonic-gate 		outfl(O_OK, File, Line, "lex: ");
9557c478bd9Sstevel@tonic-gate 		dumpline(O_OK);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 	return (Errcount);
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate void
lex_free(void)9617c478bd9Sstevel@tonic-gate lex_free(void)
9627c478bd9Sstevel@tonic-gate {
9637c478bd9Sstevel@tonic-gate 	struct filestats *nfstats = Fstats;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
9667c478bd9Sstevel@tonic-gate 	 * Free up memory consumed by the lexer
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	stats_delete(Tokcount);
9697c478bd9Sstevel@tonic-gate 	stats_delete(Filecount);
9707c478bd9Sstevel@tonic-gate 	stats_delete(Lexelapse);
9717c478bd9Sstevel@tonic-gate 	while (nfstats != NULL) {
9727c478bd9Sstevel@tonic-gate 		Fstats = nfstats->next;
9737c478bd9Sstevel@tonic-gate 		stats_delete(nfstats->stats);
9748a40a695Sgavinm 		if (nfstats->idstats != NULL)
9758a40a695Sgavinm 			stats_delete(nfstats->idstats);
9767c478bd9Sstevel@tonic-gate 		FREE(nfstats);
9777c478bd9Sstevel@tonic-gate 		nfstats = Fstats;
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 	lut_free(Timesuffixlut, NULL, NULL);
9807c478bd9Sstevel@tonic-gate 	lut_free(Rwordslut, NULL, NULL);
9817c478bd9Sstevel@tonic-gate 	lut_free(Ident, NULL, NULL);
9827c478bd9Sstevel@tonic-gate 	lut_free(Dicts, NULL, NULL);
9837c478bd9Sstevel@tonic-gate }
984