xref: /illumos-gate/usr/src/cmd/fm/eversholt/esc/common/escmain.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * escmain.c -- main routine for esc, the eversholt compiler
27  *
28  * argument processing and the general flow through all the other
29  * modules is driven by this file.
30  */
31 
32 #include <stdio.h>
33 #include <string.h>
34 #ifdef sun
35 #include <stdlib.h>
36 #else
37 #include <getopt.h>
38 #endif /* sun */
39 #include "out.h"
40 #include "stats.h"
41 #include "alloc.h"
42 #include "stable.h"
43 #include "literals.h"
44 #include "lut.h"
45 #include "esclex.h"
46 #include "eftwrite.h"
47 #include "ptree.h"
48 #include "tree.h"
49 #include "check.h"
50 #include "version.h"
51 
52 /* stuff exported by yacc-generated parsers */
53 extern void yyparse(void);
54 extern int yydebug;
55 
56 /*
57  * This external definition has to be here.  If we put it in literals.h
58  * lint complains about the declaration not being used within the block
59  * when compiling literals.c.
60  */
61 extern void literals_init(void);
62 
63 static const char *Usage =
64 "[-SYdghpqvy] [-Dname[=def]] [-I dir] [-Uname] [-o outfile] esc-files...";
65 static const char *Help =
66 "\tinput files are run through cpp and concatenated.\n"
67 "\t-D name[=def] Pass to cpp\n"
68 "\t-I dir        Pass to cpp\n"
69 "\t-S            Print stats for compiler memory usage, etc.\n"
70 "\t-U name       Pass to cpp\n"
71 "\t-Y            Enable parser debug output\n"
72 "\t-d            Enable general debug output\n"
73 "\t-g            Print generated iterators (use with -p)\n"
74 "\t-h            Print this help message\n"
75 "\t-o outfile    Emit compiled EFT to \"outfile\"\n"
76 "\t-p            Print complete parse tree\n"
77 "\t-q            Quiet mode: suppress warnings\n"
78 "\t-v            Enable verbose output\n"
79 "\t-y            Enable lexer debug output";
80 
81 int Debug;
82 int Verbose;
83 int Warn = 1;	/* the esc compiler should issue language warnings */
84 
85 extern int Pchildgen;	/* flag to ptree for printing generated iterators */
86 
87 #define	MAXARGS 8192
88 char Args[MAXARGS];
89 
90 #define	MAXCPPARGS 4000
91 static char Cppargs[MAXCPPARGS];
92 
93 int
94 main(int argc, char *argv[])
95 {
96 	char flagbuf[] = " -D";
97 	char **av;
98 	int c;
99 	int stats = 0;
100 	int lexecho = 0;
101 	const char *outfile = NULL;
102 	int count;
103 	int i;
104 	int pflag = 0;
105 
106 	alloc_init();
107 	out_init(argv[0]);
108 	stats_init(1);		/* extended stats always enabled for esc */
109 	stable_init(0);
110 	literals_init();
111 	lut_init();
112 	tree_init();
113 	eftwrite_init();
114 
115 	/* built a best effort summary of args for eftwrite() */
116 	count = 0;
117 	for (i = 1; i < argc; i++) {
118 		char *ptr = argv[i];
119 
120 		if (count < MAXARGS - 1)
121 			Args[count++] = ' ';
122 
123 		while (count < MAXARGS - 1 && *ptr)
124 			Args[count++] = *ptr++;
125 
126 	}
127 	Args[count] = '\0';
128 
129 
130 
131 	while ((c = getopt(argc, argv, "D:I:SU:Ydgho:pqvy")) != EOF) {
132 		switch (c) {
133 		case 'D':
134 		case 'I':
135 		case 'U':
136 			if (strlen(optarg) + strlen(Cppargs) + 4 >= MAXCPPARGS)
137 				out(O_DIE, "cpp args too long (max %d bytes)",
138 				    MAXCPPARGS);
139 			flagbuf[2] = c;
140 			(void) strcat(Cppargs, flagbuf);
141 			(void) strcat(Cppargs, optarg);
142 			break;
143 
144 		case 'S':
145 			stats++;
146 			break;
147 
148 		case 'Y':
149 			yydebug++;
150 			break;
151 
152 		case 'd':
153 			Debug++;
154 			break;
155 
156 		case 'g':
157 			Pchildgen++;
158 			break;
159 
160 		case 'h':
161 		case '?':
162 			out(O_PROG, "eversholt compiler version %d.%d",
163 			    VERSION_MAJOR, VERSION_MINOR);
164 			out(O_DIE|O_USAGE, "%s\n%s", Usage, Help);
165 			/*NOTREACHED*/
166 			break;
167 
168 		case 'o':
169 			outfile = optarg;
170 			break;
171 
172 		case 'p':
173 			pflag++;
174 			break;
175 
176 		case 'q':
177 			Warn = 0;
178 			break;
179 
180 		case 'v':
181 			Verbose++;
182 			break;
183 
184 		case 'y':
185 			lexecho++;
186 			break;
187 
188 		default:
189 			out(O_DIE|O_USAGE, Usage);
190 			/*NOTREACHED*/
191 		}
192 	}
193 
194 	out(O_PROG|O_VERB, "eversholt compiler version %d.%d",
195 	    VERSION_MAJOR, VERSION_MINOR);
196 
197 	argc -= optind;
198 	av = &argv[optind];
199 
200 	if (argc < 1) {
201 		out(O_ERR, "no esc source files given");
202 		out(O_DIE|O_USAGE, Usage);
203 		/*NOTREACHED*/
204 	}
205 
206 	lex_init(av, Cppargs, lexecho);
207 	check_init();
208 	yyparse();
209 	(void) lex_fini();
210 
211 	tree_report();
212 
213 	if (count = out_errcount())
214 		out(O_DIE, "%d language error%s encountered, exiting.",
215 		    OUTS(count));
216 
217 	if (outfile)
218 		eftwrite(outfile);
219 
220 	if (pflag)
221 		ptree_name_iter(O_OK, tree_root(NULL));
222 
223 	if (stats) {
224 		out(O_OK, "Stats:");
225 		stats_publish();
226 	}
227 
228 	out_exit(0);
229 	/*NOTREACHED*/
230 	return (0);
231 }
232