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