xref: /titanic_41/usr/src/cmd/sgs/lex/common/header.c (revision 6a1af1a67532df169a657cce07140be64bdea084)
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 2008 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 		(void) fprintf(fout, "#ifndef EUC\n");
53 		(void) fprintf(fout, "#define EUC\n");
54 		(void) fprintf(fout, "#endif\n");
55 		(void) fprintf(fout, "#include <stdio.h>\n");
56 		(void) fprintf(fout, "#include <stdlib.h>\n");
57 		(void) fprintf(fout, "#include <inttypes.h>\n");
58 		(void) fprintf(fout, "#include <widec.h>\n");
59 		if (widecio) { /* -w option */
60 			(void) fprintf(fout, "#define YYTEXT yytext\n");
61 			(void) fprintf(fout, "#define YYLENG yyleng\n");
62 			(void) fprintf(fout, "#ifndef __cplusplus\n");
63 			(void) fprintf(fout, "#define YYINPUT input\n");
64 			(void) fprintf(fout, "#define YYOUTPUT output\n");
65 			(void) fprintf(fout, "#else\n");
66 			(void) fprintf(fout, "#define YYINPUT lex_input\n");
67 			(void) fprintf(fout, "#define YYOUTPUT lex_output\n");
68 			(void) fprintf(fout, "#endif\n");
69 			(void) fprintf(fout, "#define YYUNPUT unput\n");
70 		} else { /* -e option */
71 			(void) fprintf(fout, "#include <limits.h>\n");
72 			(void) fprintf(fout, "#include <sys/euc.h>\n");
73 			(void) fprintf(fout, "#define YYLEX_E 1\n");
74 			(void) fprintf(fout, "#define YYTEXT yywtext\n");
75 			(void) fprintf(fout, "#define YYLENG yywleng\n");
76 			(void) fprintf(fout, "#define YYINPUT yywinput\n");
77 			(void) fprintf(fout, "#define YYOUTPUT yywoutput\n");
78 			(void) fprintf(fout, "#define YYUNPUT yywunput\n");
79 		}
80 	} else { /* ASCII compatibility mode. */
81 		(void) fprintf(fout, "#include <stdio.h>\n");
82 		(void) fprintf(fout, "#include <stdlib.h>\n");
83 		(void) 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 		(void) fprintf(fout,
213 		    "#define ECHO fprintf(yyout, \"%%s\",yytext)\n");
214 		if (handleeuc) {
215 			(void) fprintf(fout,
216 "# define REJECT { nstr = yyreject_e(); goto yyfussy;}\n");
217 			(void) fprintf(fout, "int yyleng;\n");
218 			(void) fprintf(fout, "size_t yywleng;\n");
219 			/*
220 			 * XCU4:
221 			 * If %array, yytext[] contains the token.
222 			 * If %pointer, yytext is a pointer to yy_tbuf[].
223 			 */
224 			if (isArray) {
225 				(void) fprintf(fout, "#define YYISARRAY\n");
226 				(void) fprintf(fout,
227 				"unsigned char yytext[YYLMAX*MB_LEN_MAX];\n");
228 				(void) fprintf(fout,
229 				"wchar_t yywtext[YYLMAX];\n");
230 			} else {
231 				(void) fprintf(fout,
232 				"wchar_t yy_twbuf[YYLMAX];\n");
233 				(void) fprintf(fout,
234 				"wchar_t yy_tbuf[YYLMAX*MB_LEN_MAX];\n");
235 				(void) fprintf(fout,
236 				"unsigned char * yytext ="
237 				"(unsigned char *)yy_tbuf;\n");
238 				(void) fprintf(fout,
239 				"wchar_t * yywtext = yy_twbuf;\n");
240 				(void) fprintf(fout,
241 				    "int yytextsz = YYLMAX;\n");
242 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
243 				(void) fprintf(fout,
244 				    "#define YYTEXTSZINC 100\n");
245 				(void) fprintf(fout, "#endif\n");
246 			}
247 		} else {
248 			(void) fprintf(fout,
249 "# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
250 			(void) fprintf(fout, "int yyleng;\n");
251 
252 			/*
253 			 * XCU4:
254 			 * If %array, yytext[] contains the token.
255 			 * If %pointer, yytext is a pointer to yy_tbuf[].
256 			 */
257 			if (isArray) {
258 				(void) fprintf(fout, "#define YYISARRAY\n");
259 				(void) fprintf(fout, "char yytext[YYLMAX];\n");
260 			} else {
261 				(void) fprintf(fout, "char yy_tbuf[YYLMAX];\n");
262 				(void) fprintf(fout,
263 				"char * yytext = yy_tbuf;\n");
264 				(void) fprintf(fout,
265 				    "int yytextsz = YYLMAX;\n");
266 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
267 				(void) fprintf(fout,
268 				    "#define YYTEXTSZINC 100\n");
269 				(void) fprintf(fout, "#endif\n");
270 			}
271 		}
272 	}
273 	(void) fprintf(fout, "int yymorfg;\n");
274 	if (handleeuc)
275 		(void) fprintf(fout, "extern wchar_t *yysptr, yysbuf[];\n");
276 	else
277 		(void) fprintf(fout, "extern char *yysptr, yysbuf[];\n");
278 	(void) fprintf(fout, "int yytchar;\n");
279 	(void) fprintf(fout, "FILE *yyin = {stdin}, *yyout = {stdout};\n");
280 	(void) fprintf(fout, "extern int yylineno;\n");
281 	(void) fprintf(fout, "struct yysvf { \n");
282 	(void) fprintf(fout, "\tstruct yywork *yystoff;\n");
283 	(void) fprintf(fout, "\tstruct yysvf *yyother;\n");
284 	(void) fprintf(fout, "\tint *yystops;};\n");
285 	(void) fprintf(fout, "struct yysvf *yyestate;\n");
286 	(void) fprintf(fout, "extern struct yysvf yysvec[], *yybgin;\n");
287 }
288 
289 static void
290 rhd1(void)
291 {
292 	(void) fprintf(fout, "integer function yylex(dummy)\n");
293 	(void) fprintf(fout, "define YYLMAX 200\n");
294 	(void) fprintf(fout, "define ECHO call yyecho(yytext,yyleng)\n");
295 	(void) fprintf(fout,
296 	"define REJECT nstr = yyrjct(yytext,yyleng);goto 30998\n");
297 	(void) fprintf(fout, "integer nstr,yylook,yywrap\n");
298 	(void) fprintf(fout, "integer yyleng, yytext(YYLMAX)\n");
299 	(void) fprintf(fout, "common /yyxel/ yyleng, yytext\n");
300 	(void) fprintf(fout,
301 	"common /yyldat/ yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta\n");
302 	(void) fprintf(fout,
303 	"integer yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta(YYLMAX)\n");
304 	(void) fprintf(fout, "for(;;){\n");
305 	(void) fprintf(fout, "\t30999 nstr = yylook(dummy)\n");
306 	(void) fprintf(fout, "\tgoto 30998\n");
307 	(void) fprintf(fout, "\t30000 k = yywrap(dummy)\n");
308 	(void) fprintf(fout, "\tif(k .ne. 0){\n");
309 	(void) fprintf(fout, "\tyylex=0; return; }\n");
310 	(void) fprintf(fout, "\t\telse goto 30998\n");
311 }
312 
313 void
314 phead2(void)
315 {
316 	if (!ratfor)
317 		chd2();
318 }
319 
320 static void
321 chd2(void)
322 {
323 	(void) fprintf(fout, "#ifdef __cplusplus\n");
324 	(void) fprintf(fout,
325 	"/* to avoid CC and lint complaining yyfussy not being used ...*/\n");
326 	(void) fprintf(fout, "static int __lex_hack = 0;\n");
327 	(void) fprintf(fout, "if (__lex_hack) goto yyfussy;\n");
328 	(void) fprintf(fout, "#endif\n");
329 	(void) fprintf(fout, "while((nstr = yylook()) >= 0)\n");
330 	(void) fprintf(fout, "yyfussy: switch(nstr){\n");
331 	(void) fprintf(fout, "case 0:\n");
332 	(void) fprintf(fout, "if(yywrap()) return(0); break;\n");
333 }
334 
335 void
336 ptail(void)
337 {
338 	if (!pflag)
339 		ratfor ? rtail() : ctail();
340 	pflag = 1;
341 }
342 
343 static void
344 ctail(void)
345 {
346 	(void) fprintf(fout, "case -1:\nbreak;\n");		/* for reject */
347 	(void) fprintf(fout, "default:\n");
348 	(void) fprintf(fout,
349 	"(void)fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
350 	(void) fprintf(fout, "} return(0); }\n");
351 	(void) fprintf(fout, "/* end of yylex */\n");
352 }
353 
354 static void
355 rtail(void)
356 {
357 	int i;
358 	(void) fprintf(fout,
359 	"\n30998 if(nstr .lt. 0 .or. nstr .gt. %d)goto 30999\n", casecount);
360 	(void) fprintf(fout, "nstr = nstr + 1\n");
361 	(void) fprintf(fout, "goto(\n");
362 	for (i = 0; i < casecount; i++)
363 		(void) fprintf(fout, "%d,\n", 30000+i);
364 	(void) fprintf(fout, "30999),nstr\n");
365 	(void) fprintf(fout, "30997 continue\n");
366 	(void) fprintf(fout, "}\nend\n");
367 }
368 
369 void
370 statistics(void)
371 {
372 	(void) fprintf(errorf,
373 "%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions,\n",
374 	    tptr, treesize, (int)(nxtpos-positions), maxpos, stnum + 1,
375 	    nstates, rcount);
376 	(void) fprintf(errorf,
377 	"%d/%d packed char classes(%%k), ", (int)(pcptr-pchar), pchlen);
378 	if (optim)
379 		(void) fprintf(errorf,
380 		" %d/%d packed transitions(%%a), ", nptr, ntrans);
381 	(void) fprintf(errorf, " %d/%d output slots(%%o)", yytop, outsize);
382 	(void) putc('\n', errorf);
383 }
384