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
580ab886dSwesolows  * Common Development and Distribution License (the "License").
680ab886dSwesolows  * 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  */
2180ab886dSwesolows 
227c478bd9Sstevel@tonic-gate /*
2380ab886dSwesolows  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * escmain.c -- main routine for esc, the eversholt compiler
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * argument processing and the general flow through all the other
297c478bd9Sstevel@tonic-gate  * modules is driven by this file.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #ifdef sun
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #else
377c478bd9Sstevel@tonic-gate #include <getopt.h>
387c478bd9Sstevel@tonic-gate #endif /* sun */
397c478bd9Sstevel@tonic-gate #include "out.h"
407c478bd9Sstevel@tonic-gate #include "stats.h"
417c478bd9Sstevel@tonic-gate #include "alloc.h"
427c478bd9Sstevel@tonic-gate #include "stable.h"
437c478bd9Sstevel@tonic-gate #include "literals.h"
447c478bd9Sstevel@tonic-gate #include "lut.h"
457c478bd9Sstevel@tonic-gate #include "esclex.h"
467c478bd9Sstevel@tonic-gate #include "eftwrite.h"
477c478bd9Sstevel@tonic-gate #include "ptree.h"
487c478bd9Sstevel@tonic-gate #include "tree.h"
497c478bd9Sstevel@tonic-gate #include "check.h"
507c478bd9Sstevel@tonic-gate #include "version.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* stuff exported by yacc-generated parsers */
537c478bd9Sstevel@tonic-gate extern void yyparse(void);
547c478bd9Sstevel@tonic-gate extern int yydebug;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * This external definition has to be here.  If we put it in literals.h
587c478bd9Sstevel@tonic-gate  * lint complains about the declaration not being used within the block
597c478bd9Sstevel@tonic-gate  * when compiling literals.c.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate extern void literals_init(void);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate static const char *Usage =
647c478bd9Sstevel@tonic-gate "[-SYdghpqvy] [-Dname[=def]] [-I dir] [-Uname] [-o outfile] esc-files...";
657c478bd9Sstevel@tonic-gate static const char *Help =
667c478bd9Sstevel@tonic-gate "\tinput files are run through cpp and concatenated.\n"
677c478bd9Sstevel@tonic-gate "\t-D name[=def] Pass to cpp\n"
687c478bd9Sstevel@tonic-gate "\t-I dir        Pass to cpp\n"
697c478bd9Sstevel@tonic-gate "\t-S            Print stats for compiler memory usage, etc.\n"
707c478bd9Sstevel@tonic-gate "\t-U name       Pass to cpp\n"
717c478bd9Sstevel@tonic-gate "\t-Y            Enable parser debug output\n"
727c478bd9Sstevel@tonic-gate "\t-d            Enable general debug output\n"
737c478bd9Sstevel@tonic-gate "\t-g            Print generated iterators (use with -p)\n"
747c478bd9Sstevel@tonic-gate "\t-h            Print this help message\n"
757c478bd9Sstevel@tonic-gate "\t-o outfile    Emit compiled EFT to \"outfile\"\n"
767c478bd9Sstevel@tonic-gate "\t-p            Print complete parse tree\n"
777c478bd9Sstevel@tonic-gate "\t-q            Quiet mode: suppress warnings\n"
787c478bd9Sstevel@tonic-gate "\t-v            Enable verbose output\n"
797c478bd9Sstevel@tonic-gate "\t-y            Enable lexer debug output";
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate int Debug;
827c478bd9Sstevel@tonic-gate int Verbose;
837c478bd9Sstevel@tonic-gate int Warn = 1;	/* the esc compiler should issue language warnings */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate extern int Pchildgen;	/* flag to ptree for printing generated iterators */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	MAXARGS 8192
887c478bd9Sstevel@tonic-gate char Args[MAXARGS];
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #define	MAXCPPARGS 4000
917c478bd9Sstevel@tonic-gate static char Cppargs[MAXCPPARGS];
927c478bd9Sstevel@tonic-gate 
9380ab886dSwesolows int
main(int argc,char * argv[])947c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	char flagbuf[] = " -D";
977c478bd9Sstevel@tonic-gate 	char **av;
987c478bd9Sstevel@tonic-gate 	int c;
997c478bd9Sstevel@tonic-gate 	int stats = 0;
1007c478bd9Sstevel@tonic-gate 	int lexecho = 0;
1017c478bd9Sstevel@tonic-gate 	const char *outfile = NULL;
1027c478bd9Sstevel@tonic-gate 	int count;
1037c478bd9Sstevel@tonic-gate 	int i;
1047c478bd9Sstevel@tonic-gate 	int pflag = 0;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	alloc_init();
1077c478bd9Sstevel@tonic-gate 	out_init(argv[0]);
1087c478bd9Sstevel@tonic-gate 	stats_init(1);		/* extended stats always enabled for esc */
1097c478bd9Sstevel@tonic-gate 	stable_init(0);
1107c478bd9Sstevel@tonic-gate 	literals_init();
1117c478bd9Sstevel@tonic-gate 	lut_init();
1127c478bd9Sstevel@tonic-gate 	tree_init();
1137c478bd9Sstevel@tonic-gate 	eftwrite_init();
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/* built a best effort summary of args for eftwrite() */
1167c478bd9Sstevel@tonic-gate 	count = 0;
1177c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
1187c478bd9Sstevel@tonic-gate 		char *ptr = argv[i];
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		if (count < MAXARGS - 1)
1217c478bd9Sstevel@tonic-gate 			Args[count++] = ' ';
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		while (count < MAXARGS - 1 && *ptr)
1247c478bd9Sstevel@tonic-gate 			Args[count++] = *ptr++;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 	Args[count] = '\0';
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "D:I:SU:Ydgho:pqvy")) != EOF) {
1327c478bd9Sstevel@tonic-gate 		switch (c) {
1337c478bd9Sstevel@tonic-gate 		case 'D':
1347c478bd9Sstevel@tonic-gate 		case 'I':
1357c478bd9Sstevel@tonic-gate 		case 'U':
1367c478bd9Sstevel@tonic-gate 			if (strlen(optarg) + strlen(Cppargs) + 4 >= MAXCPPARGS)
1377c478bd9Sstevel@tonic-gate 				out(O_DIE, "cpp args too long (max %d bytes)",
1387c478bd9Sstevel@tonic-gate 				    MAXCPPARGS);
1397c478bd9Sstevel@tonic-gate 			flagbuf[2] = c;
1407c478bd9Sstevel@tonic-gate 			(void) strcat(Cppargs, flagbuf);
1417c478bd9Sstevel@tonic-gate 			(void) strcat(Cppargs, optarg);
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		case 'S':
1457c478bd9Sstevel@tonic-gate 			stats++;
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		case 'Y':
1497c478bd9Sstevel@tonic-gate 			yydebug++;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		case 'd':
1537c478bd9Sstevel@tonic-gate 			Debug++;
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		case 'g':
1577c478bd9Sstevel@tonic-gate 			Pchildgen++;
1587c478bd9Sstevel@tonic-gate 			break;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		case 'h':
1617c478bd9Sstevel@tonic-gate 		case '?':
1627c478bd9Sstevel@tonic-gate 			out(O_PROG, "eversholt compiler version %d.%d",
1637c478bd9Sstevel@tonic-gate 			    VERSION_MAJOR, VERSION_MINOR);
1647c478bd9Sstevel@tonic-gate 			out(O_DIE|O_USAGE, "%s\n%s", Usage, Help);
1657c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
166*586c519fSToomas Soome 			break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		case 'o':
1697c478bd9Sstevel@tonic-gate 			outfile = optarg;
1707c478bd9Sstevel@tonic-gate 			break;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		case 'p':
1737c478bd9Sstevel@tonic-gate 			pflag++;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		case 'q':
1777c478bd9Sstevel@tonic-gate 			Warn = 0;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		case 'v':
1817c478bd9Sstevel@tonic-gate 			Verbose++;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		case 'y':
1857c478bd9Sstevel@tonic-gate 			lexecho++;
1867c478bd9Sstevel@tonic-gate 			break;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		default:
1897c478bd9Sstevel@tonic-gate 			out(O_DIE|O_USAGE, Usage);
1907c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	out(O_PROG|O_VERB, "eversholt compiler version %d.%d",
1957c478bd9Sstevel@tonic-gate 	    VERSION_MAJOR, VERSION_MINOR);
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	argc -= optind;
1987c478bd9Sstevel@tonic-gate 	av = &argv[optind];
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (argc < 1) {
2017c478bd9Sstevel@tonic-gate 		out(O_ERR, "no esc source files given");
2027c478bd9Sstevel@tonic-gate 		out(O_DIE|O_USAGE, Usage);
2037c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	lex_init(av, Cppargs, lexecho);
2077c478bd9Sstevel@tonic-gate 	check_init();
2087c478bd9Sstevel@tonic-gate 	yyparse();
2097c478bd9Sstevel@tonic-gate 	(void) lex_fini();
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	tree_report();
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if (count = out_errcount())
2147c478bd9Sstevel@tonic-gate 		out(O_DIE, "%d language error%s encountered, exiting.",
2157c478bd9Sstevel@tonic-gate 		    OUTS(count));
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (outfile)
2187c478bd9Sstevel@tonic-gate 		eftwrite(outfile);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (pflag)
2217c478bd9Sstevel@tonic-gate 		ptree_name_iter(O_OK, tree_root(NULL));
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (stats) {
2247c478bd9Sstevel@tonic-gate 		out(O_OK, "Stats:");
2257c478bd9Sstevel@tonic-gate 		stats_publish();
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	out_exit(0);
2297c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
23080ab886dSwesolows 	return (0);
2317c478bd9Sstevel@tonic-gate }
232