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