xref: /titanic_50/usr/src/cmd/sgs/lex/common/header.c (revision 22337b4b3c8a2b9db615e524e0e9e1fbd3dc71bf)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1988 AT&T	*/
27 /*	All Rights Reserved	*/
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include "ldefs.h"
32 
33 static void rhd1(void);
34 static void chd1(void);
35 static void chd2(void);
36 static void ctail(void);
37 static void rtail(void);
38 
39 void
40 phead1(void)
41 {
42 	ratfor ? rhd1() : chd1();
43 }
44 
45 static void
46 chd1(void)
47 {
48 	if (*v_stmp == 'y')
49 		(void)  fprintf(fout, "#ident\t\"lex: %s %s\"\n",
50 		    (const char *)SGU_PKG, (const char *)SGU_REL);
51 	if (handleeuc) {
52 		fprintf(fout, "#ifndef EUC\n");
53 		fprintf(fout, "#define EUC\n");
54 		fprintf(fout, "#endif\n");
55 		fprintf(fout, "#include <stdio.h>\n");
56 		fprintf(fout, "#include <stdlib.h>\n");
57 		fprintf(fout, "#include <inttypes.h>\n");
58 		fprintf(fout, "#include <widec.h>\n");
59 		if (widecio) { /* -w option */
60 			fprintf(fout, "#define YYTEXT yytext\n");
61 			fprintf(fout, "#define YYLENG yyleng\n");
62 			fprintf(fout, "#ifndef __cplusplus\n");
63 			fprintf(fout, "#define YYINPUT input\n");
64 			fprintf(fout, "#define YYOUTPUT output\n");
65 			fprintf(fout, "#else\n");
66 			fprintf(fout, "#define YYINPUT lex_input\n");
67 			fprintf(fout, "#define YYOUTPUT lex_output\n");
68 			fprintf(fout, "#endif\n");
69 			fprintf(fout, "#define YYUNPUT unput\n");
70 		} else { /* -e option */
71 			fprintf(fout, "#include <limits.h>\n");
72 			fprintf(fout, "#include <sys/euc.h>\n");
73 			fprintf(fout, "#define YYLEX_E 1\n");
74 			fprintf(fout, "#define YYTEXT yywtext\n");
75 			fprintf(fout, "#define YYLENG yywleng\n");
76 			fprintf(fout, "#define YYINPUT yywinput\n");
77 			fprintf(fout, "#define YYOUTPUT yywoutput\n");
78 			fprintf(fout, "#define YYUNPUT yywunput\n");
79 		}
80 	} else { /* ASCII compatibility mode. */
81 		fprintf(fout, "#include <stdio.h>\n");
82 		fprintf(fout, "#include <stdlib.h>\n");
83 		fprintf(fout, "#include <inttypes.h>\n");
84 	}
85 	if (ZCH > NCH)
86 		(void) fprintf(fout, "# define U(x) ((x)&0377)\n");
87 	else
88 	(void) fprintf(fout, "# define U(x) x\n");
89 	(void) fprintf(fout, "# define NLSTATE yyprevious=YYNEWLINE\n");
90 	(void) fprintf(fout, "# define BEGIN yybgin = yysvec + 1 +\n");
91 	(void) fprintf(fout, "# define INITIAL 0\n");
92 	(void) fprintf(fout, "# define YYLERR yysvec\n");
93 	(void) fprintf(fout, "# define YYSTATE (yyestate-yysvec-1)\n");
94 	if (optim)
95 		(void) fprintf(fout, "# define YYOPTIM 1\n");
96 #ifdef DEBUG
97 	(void) fprintf(fout, "# define LEXDEBUG 1\n");
98 #endif
99 	(void) fprintf(fout, "# ifndef YYLMAX \n");
100 	(void) fprintf(fout, "# define YYLMAX BUFSIZ\n");
101 	(void) fprintf(fout, "# endif \n");
102 	(void) fprintf(fout, "#ifndef __cplusplus\n");
103 	if (widecio)
104 		(void) fprintf(fout,
105 		"# define output(c) (void)putwc(c,yyout)\n");
106 	else
107 		(void) fprintf(fout,
108 		"# define output(c) (void)putc(c,yyout)\n");
109 	(void) fprintf(fout, "#else\n");
110 	if (widecio)
111 		(void) fprintf(fout,
112 		"# define lex_output(c) (void)putwc(c,yyout)\n");
113 	else
114 		(void) fprintf(fout,
115 		"# define lex_output(c) (void)putc(c,yyout)\n");
116 	(void) fprintf(fout, "#endif\n");
117 	(void) fprintf(fout,
118 	"\n#if defined(__cplusplus) || defined(__STDC__)\n");
119 	(void) fprintf(fout,
120 	"\n#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
121 	(void) fprintf(fout, "extern \"C\" {\n");
122 	(void) fprintf(fout, "#endif\n");
123 	(void) fprintf(fout, "\tint yyback(int *, int);\n"); /* ? */
124 	(void) fprintf(fout, "\tint yyinput(void);\n"); /* ? */
125 	(void) fprintf(fout, "\tint yylook(void);\n"); /* ? */
126 	(void) fprintf(fout, "\tvoid yyoutput(int);\n"); /* ? */
127 	(void) fprintf(fout, "\tint yyracc(int);\n"); /* ? */
128 	(void) fprintf(fout, "\tint yyreject(void);\n"); /* ? */
129 	(void) fprintf(fout, "\tvoid yyunput(int);\n"); /* ? */
130 	(void) fprintf(fout, "\tint yylex(void);\n");
131 	(void) fprintf(fout, "#ifdef YYLEX_E\n");
132 	(void) fprintf(fout, "\tvoid yywoutput(wchar_t);\n");
133 	(void) fprintf(fout, "\twchar_t yywinput(void);\n");
134 	(void) fprintf(fout, "\tvoid yywunput(wchar_t);\n");
135 	(void) fprintf(fout, "#endif\n");
136 
137 	/* XCU4: type of yyless is int */
138 	(void) fprintf(fout, "#ifndef yyless\n");
139 	(void) fprintf(fout, "\tint yyless(int);\n");
140 	(void) fprintf(fout, "#endif\n");
141 	(void) fprintf(fout, "#ifndef yywrap\n");
142 	(void) fprintf(fout, "\tint yywrap(void);\n");
143 	(void) fprintf(fout, "#endif\n");
144 	(void) fprintf(fout, "#ifdef LEXDEBUG\n");
145 	(void) fprintf(fout, "\tvoid allprint(char);\n");
146 	(void) fprintf(fout, "\tvoid sprint(char *);\n");
147 	(void) fprintf(fout, "#endif\n");
148 	(void) fprintf(fout,
149 	"#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
150 	(void) fprintf(fout, "}\n");
151 	(void) fprintf(fout, "#endif\n\n");
152 	(void) fprintf(fout, "#ifdef __cplusplus\n");
153 	(void) fprintf(fout, "extern \"C\" {\n");
154 	(void) fprintf(fout, "#endif\n");
155 	(void) fprintf(fout, "\tvoid exit(int);\n");
156 	(void) fprintf(fout, "#ifdef __cplusplus\n");
157 	(void) fprintf(fout, "}\n");
158 	(void) fprintf(fout, "#endif\n\n");
159 	(void) fprintf(fout, "#endif\n");
160 	(void) fprintf(fout,
161 	"# define unput(c)"
162 	" {yytchar= (c);if(yytchar=='\\n')yylineno--;*yysptr++=yytchar;}\n");
163 	(void) fprintf(fout, "# define yymore() (yymorfg=1)\n");
164 	if (widecio) {
165 		(void) fprintf(fout, "#ifndef __cplusplus\n");
166 		(void) fprintf(fout, "%s%d%s\n",
167 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getwc(yyin))==",
168 		    ctable['\n'],
169 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
170 		(void) fprintf(fout, "#else\n");
171 		(void) fprintf(fout, "%s%d%s\n",
172 "# define lex_input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getwc(yyin))==",
173 		    ctable['\n'],
174 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
175 		(void) fprintf(fout, "#endif\n");
176 		(void) fprintf(fout,
177 		"# define ECHO (void)fprintf(yyout, \"%%ws\",yytext)\n");
178 		(void) fprintf(fout,
179 		"# define REJECT { nstr = yyreject_w(); goto yyfussy;}\n");
180 		(void) fprintf(fout, "#define yyless yyless_w\n");
181 		(void) fprintf(fout, "int yyleng;\n");
182 
183 		/*
184 		 * XCU4:
185 		 * If %array, yytext[] contains the token.
186 		 * If %pointer, yytext is a pointer to yy_tbuf[].
187 		 */
188 
189 		if (isArray) {
190 			(void) fprintf(fout, "#define YYISARRAY\n");
191 			(void) fprintf(fout, "wchar_t yytext[YYLMAX];\n");
192 		} else {
193 			(void) fprintf(fout, "wchar_t yy_tbuf[YYLMAX];\n");
194 			(void) fprintf(fout, "wchar_t * yytext = yy_tbuf;\n");
195 			(void) fprintf(fout, "int yytextsz = YYLMAX;\n");
196 			(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
197 			(void) fprintf(fout, "#define YYTEXTSZINC 100\n");
198 			(void) fprintf(fout, "#endif\n");
199 		}
200 	} else {
201 		(void) fprintf(fout, "#ifndef __cplusplus\n");
202 		(void) fprintf(fout, "%s%d%s\n",
203 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
204 		    ctable['\n'],
205 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
206 		(void) fprintf(fout, "#else\n");
207 		(void) fprintf(fout, "%s%d%s\n",
208 "# define lex_input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
209 		    ctable['\n'],
210 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
211 		(void) fprintf(fout, "#endif\n");
212 		fprintf(fout, "#define ECHO fprintf(yyout, \"%%s\",yytext)\n");
213 		if (handleeuc) {
214 			(void) fprintf(fout,
215 "# define REJECT { nstr = yyreject_e(); goto yyfussy;}\n");
216 			(void) fprintf(fout, "int yyleng;\n");
217 			(void) fprintf(fout, "size_t yywleng;\n");
218 			/*
219 			 * XCU4:
220 			 * If %array, yytext[] contains the token.
221 			 * If %pointer, yytext is a pointer to yy_tbuf[].
222 			 */
223 			if (isArray) {
224 				(void) fprintf(fout, "#define YYISARRAY\n");
225 				(void) fprintf(fout,
226 				"unsigned char yytext[YYLMAX*MB_LEN_MAX];\n");
227 				(void) fprintf(fout,
228 				"wchar_t yywtext[YYLMAX];\n");
229 			} else {
230 				(void) fprintf(fout,
231 				"wchar_t yy_twbuf[YYLMAX];\n");
232 				(void) fprintf(fout,
233 				"wchar_t yy_tbuf[YYLMAX*MB_LEN_MAX];\n");
234 				(void) fprintf(fout,
235 				"unsigned char * yytext ="
236 				"(unsigned char *)yy_tbuf;\n");
237 				(void) fprintf(fout,
238 				"wchar_t * yywtext = yy_twbuf;\n");
239 				(void) fprintf(fout,
240 				    "int yytextsz = YYLMAX;\n");
241 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
242 				(void) fprintf(fout,
243 				    "#define YYTEXTSZINC 100\n");
244 				(void) fprintf(fout, "#endif\n");
245 			}
246 		} else {
247 			(void) fprintf(fout,
248 "# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
249 			(void) fprintf(fout, "int yyleng;\n");
250 
251 			/*
252 			 * XCU4:
253 			 * If %array, yytext[] contains the token.
254 			 * If %pointer, yytext is a pointer to yy_tbuf[].
255 			 */
256 			if (isArray) {
257 				(void) fprintf(fout, "#define YYISARRAY\n");
258 				(void) fprintf(fout, "char yytext[YYLMAX];\n");
259 			} else {
260 				(void) fprintf(fout, "char yy_tbuf[YYLMAX];\n");
261 				(void) fprintf(fout,
262 				"char * yytext = yy_tbuf;\n");
263 				(void) fprintf(fout,
264 				    "int yytextsz = YYLMAX;\n");
265 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
266 				(void) fprintf(fout,
267 				    "#define YYTEXTSZINC 100\n");
268 				(void) fprintf(fout, "#endif\n");
269 			}
270 		}
271 	}
272 	(void) fprintf(fout, "int yymorfg;\n");
273 	if (handleeuc)
274 		(void) fprintf(fout, "extern wchar_t *yysptr, yysbuf[];\n");
275 	else
276 		(void) fprintf(fout, "extern char *yysptr, yysbuf[];\n");
277 	(void) fprintf(fout, "int yytchar;\n");
278 	(void) fprintf(fout, "FILE *yyin = {stdin}, *yyout = {stdout};\n");
279 	(void) fprintf(fout, "extern int yylineno;\n");
280 	(void) fprintf(fout, "struct yysvf { \n");
281 	(void) fprintf(fout, "\tstruct yywork *yystoff;\n");
282 	(void) fprintf(fout, "\tstruct yysvf *yyother;\n");
283 	(void) fprintf(fout, "\tint *yystops;};\n");
284 	(void) fprintf(fout, "struct yysvf *yyestate;\n");
285 	(void) fprintf(fout, "extern struct yysvf yysvec[], *yybgin;\n");
286 }
287 
288 static void
289 rhd1(void)
290 {
291 	(void) fprintf(fout, "integer function yylex(dummy)\n");
292 	(void) fprintf(fout, "define YYLMAX 200\n");
293 	(void) fprintf(fout, "define ECHO call yyecho(yytext,yyleng)\n");
294 	(void) fprintf(fout,
295 	"define REJECT nstr = yyrjct(yytext,yyleng);goto 30998\n");
296 	(void) fprintf(fout, "integer nstr,yylook,yywrap\n");
297 	(void) fprintf(fout, "integer yyleng, yytext(YYLMAX)\n");
298 	(void) fprintf(fout, "common /yyxel/ yyleng, yytext\n");
299 	(void) fprintf(fout,
300 	"common /yyldat/ yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta\n");
301 	(void) fprintf(fout,
302 	"integer yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta(YYLMAX)\n");
303 	(void) fprintf(fout, "for(;;){\n");
304 	(void) fprintf(fout, "\t30999 nstr = yylook(dummy)\n");
305 	(void) fprintf(fout, "\tgoto 30998\n");
306 	(void) fprintf(fout, "\t30000 k = yywrap(dummy)\n");
307 	(void) fprintf(fout, "\tif(k .ne. 0){\n");
308 	(void) fprintf(fout, "\tyylex=0; return; }\n");
309 	(void) fprintf(fout, "\t\telse goto 30998\n");
310 }
311 
312 void
313 phead2(void)
314 {
315 	if (!ratfor)
316 		chd2();
317 }
318 
319 static void
320 chd2(void)
321 {
322 	(void) fprintf(fout, "#ifdef __cplusplus\n");
323 	(void) fprintf(fout,
324 	"/* to avoid CC and lint complaining yyfussy not being used ...*/\n");
325 	(void) fprintf(fout, "static int __lex_hack = 0;\n");
326 	(void) fprintf(fout, "if (__lex_hack) goto yyfussy;\n");
327 	(void) fprintf(fout, "#endif\n");
328 	(void) fprintf(fout, "while((nstr = yylook()) >= 0)\n");
329 	(void) fprintf(fout, "yyfussy: switch(nstr){\n");
330 	(void) fprintf(fout, "case 0:\n");
331 	(void) fprintf(fout, "if(yywrap()) return(0); break;\n");
332 }
333 
334 void
335 ptail(void)
336 {
337 	if (!pflag)
338 		ratfor ? rtail() : ctail();
339 	pflag = 1;
340 }
341 
342 static void
343 ctail(void)
344 {
345 	(void) fprintf(fout, "case -1:\nbreak;\n");		/* for reject */
346 	(void) fprintf(fout, "default:\n");
347 	(void) fprintf(fout,
348 	"(void)fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
349 	(void) fprintf(fout, "} return(0); }\n");
350 	(void) fprintf(fout, "/* end of yylex */\n");
351 }
352 
353 static void
354 rtail(void)
355 {
356 	int i;
357 	(void) fprintf(fout,
358 	"\n30998 if(nstr .lt. 0 .or. nstr .gt. %d)goto 30999\n", casecount);
359 	(void) fprintf(fout, "nstr = nstr + 1\n");
360 	(void) fprintf(fout, "goto(\n");
361 	for (i = 0; i < casecount; i++)
362 		(void) fprintf(fout, "%d,\n", 30000+i);
363 	(void) fprintf(fout, "30999),nstr\n");
364 	(void) fprintf(fout, "30997 continue\n");
365 	(void) fprintf(fout, "}\nend\n");
366 }
367 
368 void
369 statistics(void)
370 {
371 	(void) fprintf(errorf,
372 "%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions,\n",
373 	    tptr, treesize, nxtpos-positions, maxpos, stnum + 1, nstates,
374 	    rcount);
375 	(void) fprintf(errorf,
376 	"%d/%d packed char classes(%%k), ", pcptr-pchar, pchlen);
377 	if (optim)
378 		(void) fprintf(errorf,
379 		" %d/%d packed transitions(%%a), ", nptr, ntrans);
380 	(void) fprintf(errorf, " %d/%d output slots(%%o)", yytop, outsize);
381 	(void) putc('\n', errorf);
382 }
383