xref: /illumos-gate/usr/src/cmd/sgs/lex/common/header.c (revision 16b76d3cb933ff92018a2a75594449010192eacb)
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
38 phead1(void)
39 {
40 	ratfor ? rhd1() : chd1();
41 }
42 
43 static 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,
118 	"\n#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
119 	(void) fprintf(fout, "extern \"C\" {\n");
120 	(void) fprintf(fout, "#endif\n");
121 	(void) fprintf(fout, "\tint yyback(int *, int);\n"); /* ? */
122 	(void) fprintf(fout, "\tint yyinput(void);\n"); /* ? */
123 	(void) fprintf(fout, "\tint yylook(void);\n"); /* ? */
124 	(void) fprintf(fout, "\tvoid yyoutput(int);\n"); /* ? */
125 	(void) fprintf(fout, "\tint yyracc(int);\n"); /* ? */
126 	(void) fprintf(fout, "\tint yyreject(void);\n"); /* ? */
127 	(void) fprintf(fout, "\tvoid yyunput(int);\n"); /* ? */
128 	(void) fprintf(fout, "\tint yylex(void);\n");
129 	(void) fprintf(fout, "#ifdef YYLEX_E\n");
130 	(void) fprintf(fout, "\tvoid yywoutput(wchar_t);\n");
131 	(void) fprintf(fout, "\twchar_t yywinput(void);\n");
132 	(void) fprintf(fout, "\tvoid yywunput(wchar_t);\n");
133 	(void) fprintf(fout, "#endif\n");
134 
135 	/* XCU4: type of yyless is int */
136 	(void) fprintf(fout, "#ifndef yyless\n");
137 	(void) fprintf(fout, "\tint yyless(int);\n");
138 	(void) fprintf(fout, "#endif\n");
139 	(void) fprintf(fout, "#ifndef yywrap\n");
140 	(void) fprintf(fout, "\tint yywrap(void);\n");
141 	(void) fprintf(fout, "#endif\n");
142 	(void) fprintf(fout, "#ifdef LEXDEBUG\n");
143 	(void) fprintf(fout, "\tvoid allprint(char);\n");
144 	(void) fprintf(fout, "\tvoid sprint(char *);\n");
145 	(void) fprintf(fout, "#endif\n");
146 	(void) fprintf(fout,
147 	"#if defined(__cplusplus) && defined(__EXTERN_C__)\n");
148 	(void) fprintf(fout, "}\n");
149 	(void) fprintf(fout, "#endif\n\n");
150 	(void) fprintf(fout, "#ifdef __cplusplus\n");
151 	(void) fprintf(fout, "extern \"C\" {\n");
152 	(void) fprintf(fout, "#endif\n");
153 	(void) fprintf(fout, "\tvoid exit(int);\n");
154 	(void) fprintf(fout, "#ifdef __cplusplus\n");
155 	(void) fprintf(fout, "}\n");
156 	(void) fprintf(fout, "#endif\n\n");
157 	(void) fprintf(fout, "#endif\n");
158 	(void) fprintf(fout,
159 	"# define unput(c)"
160 	" {yytchar= (c);if(yytchar=='\\n')yylineno--;*yysptr++=yytchar;}\n");
161 	(void) fprintf(fout, "# define yymore() (yymorfg=1)\n");
162 	if (widecio) {
163 		(void) fprintf(fout, "#ifndef __cplusplus\n");
164 		(void) fprintf(fout, "%s%d%s\n",
165 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getwc(yyin))==",
166 		    ctable['\n'],
167 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
168 		(void) fprintf(fout, "#else\n");
169 		(void) fprintf(fout, "%s%d%s\n",
170 "# define lex_input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getwc(yyin))==",
171 		    ctable['\n'],
172 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
173 		(void) fprintf(fout, "#endif\n");
174 		(void) fprintf(fout,
175 		"# define ECHO (void)fprintf(yyout, \"%%ws\",yytext)\n");
176 		(void) fprintf(fout,
177 		"# define REJECT { nstr = yyreject_w(); goto yyfussy;}\n");
178 		(void) fprintf(fout, "#define yyless yyless_w\n");
179 		(void) fprintf(fout, "int yyleng;\n");
180 
181 		/*
182 		 * XCU4:
183 		 * If %array, yytext[] contains the token.
184 		 * If %pointer, yytext is a pointer to yy_tbuf[].
185 		 */
186 
187 		if (isArray) {
188 			(void) fprintf(fout, "#define YYISARRAY\n");
189 			(void) fprintf(fout, "wchar_t yytext[YYLMAX];\n");
190 		} else {
191 			(void) fprintf(fout, "wchar_t yy_tbuf[YYLMAX];\n");
192 			(void) fprintf(fout, "wchar_t * yytext = yy_tbuf;\n");
193 			(void) fprintf(fout, "int yytextsz = YYLMAX;\n");
194 			(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
195 			(void) fprintf(fout, "#define YYTEXTSZINC 100\n");
196 			(void) fprintf(fout, "#endif\n");
197 		}
198 	} else {
199 		(void) fprintf(fout, "#ifndef __cplusplus\n");
200 		(void) fprintf(fout, "%s%d%s\n",
201 "# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
202 		    ctable['\n'],
203 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
204 		(void) fprintf(fout, "#else\n");
205 		(void) fprintf(fout, "%s%d%s\n",
206 "# define lex_input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==",
207 		    ctable['\n'],
208 "?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)");
209 		(void) fprintf(fout, "#endif\n");
210 		(void) fprintf(fout,
211 		    "#define ECHO fprintf(yyout, \"%%s\",yytext)\n");
212 		if (handleeuc) {
213 			(void) fprintf(fout,
214 "# define REJECT { nstr = yyreject_e(); goto yyfussy;}\n");
215 			(void) fprintf(fout, "int yyleng;\n");
216 			(void) fprintf(fout, "size_t yywleng;\n");
217 			/*
218 			 * XCU4:
219 			 * If %array, yytext[] contains the token.
220 			 * If %pointer, yytext is a pointer to yy_tbuf[].
221 			 */
222 			if (isArray) {
223 				(void) fprintf(fout, "#define YYISARRAY\n");
224 				(void) fprintf(fout,
225 				"unsigned char yytext[YYLMAX*MB_LEN_MAX];\n");
226 				(void) fprintf(fout,
227 				"wchar_t yywtext[YYLMAX];\n");
228 			} else {
229 				(void) fprintf(fout,
230 				"wchar_t yy_twbuf[YYLMAX];\n");
231 				(void) fprintf(fout,
232 				"wchar_t yy_tbuf[YYLMAX*MB_LEN_MAX];\n");
233 				(void) fprintf(fout,
234 				"unsigned char * yytext ="
235 				"(unsigned char *)yy_tbuf;\n");
236 				(void) fprintf(fout,
237 				"wchar_t * yywtext = yy_twbuf;\n");
238 				(void) fprintf(fout,
239 				    "int yytextsz = YYLMAX;\n");
240 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
241 				(void) fprintf(fout,
242 				    "#define YYTEXTSZINC 100\n");
243 				(void) fprintf(fout, "#endif\n");
244 			}
245 		} else {
246 			(void) fprintf(fout,
247 "# define REJECT { nstr = yyreject(); goto yyfussy;}\n");
248 			(void) fprintf(fout, "int yyleng;\n");
249 
250 			/*
251 			 * XCU4:
252 			 * If %array, yytext[] contains the token.
253 			 * If %pointer, yytext is a pointer to yy_tbuf[].
254 			 */
255 			if (isArray) {
256 				(void) fprintf(fout, "#define YYISARRAY\n");
257 				(void) fprintf(fout, "char yytext[YYLMAX];\n");
258 			} else {
259 				(void) fprintf(fout, "char yy_tbuf[YYLMAX];\n");
260 				(void) fprintf(fout,
261 				"char * yytext = yy_tbuf;\n");
262 				(void) fprintf(fout,
263 				    "int yytextsz = YYLMAX;\n");
264 				(void) fprintf(fout, "#ifndef YYTEXTSZINC\n");
265 				(void) fprintf(fout,
266 				    "#define YYTEXTSZINC 100\n");
267 				(void) fprintf(fout, "#endif\n");
268 			}
269 		}
270 	}
271 	(void) fprintf(fout, "int yymorfg;\n");
272 	if (handleeuc)
273 		(void) fprintf(fout, "extern wchar_t *yysptr, yysbuf[];\n");
274 	else
275 		(void) fprintf(fout, "extern char *yysptr, yysbuf[];\n");
276 	(void) fprintf(fout, "int yytchar;\n");
277 	(void) fprintf(fout, "FILE *yyin = {stdin}, *yyout = {stdout};\n");
278 	(void) fprintf(fout, "extern int yylineno;\n");
279 	(void) fprintf(fout, "struct yysvf { \n");
280 	(void) fprintf(fout, "\tstruct yywork *yystoff;\n");
281 	(void) fprintf(fout, "\tstruct yysvf *yyother;\n");
282 	(void) fprintf(fout, "\tint *yystops;};\n");
283 	(void) fprintf(fout, "struct yysvf *yyestate;\n");
284 	(void) fprintf(fout, "extern struct yysvf yysvec[], *yybgin;\n");
285 }
286 
287 static void
288 rhd1(void)
289 {
290 	(void) fprintf(fout, "integer function yylex(dummy)\n");
291 	(void) fprintf(fout, "define YYLMAX 200\n");
292 	(void) fprintf(fout, "define ECHO call yyecho(yytext,yyleng)\n");
293 	(void) fprintf(fout,
294 	"define REJECT nstr = yyrjct(yytext,yyleng);goto 30998\n");
295 	(void) fprintf(fout, "integer nstr,yylook,yywrap\n");
296 	(void) fprintf(fout, "integer yyleng, yytext(YYLMAX)\n");
297 	(void) fprintf(fout, "common /yyxel/ yyleng, yytext\n");
298 	(void) fprintf(fout,
299 	"common /yyldat/ yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta\n");
300 	(void) fprintf(fout,
301 	"integer yyfnd, yymorf, yyprev, yybgin, yylsp, yylsta(YYLMAX)\n");
302 	(void) fprintf(fout, "for(;;){\n");
303 	(void) fprintf(fout, "\t30999 nstr = yylook(dummy)\n");
304 	(void) fprintf(fout, "\tgoto 30998\n");
305 	(void) fprintf(fout, "\t30000 k = yywrap(dummy)\n");
306 	(void) fprintf(fout, "\tif(k .ne. 0){\n");
307 	(void) fprintf(fout, "\tyylex=0; return; }\n");
308 	(void) fprintf(fout, "\t\telse goto 30998\n");
309 }
310 
311 void
312 phead2(void)
313 {
314 	if (!ratfor)
315 		chd2();
316 }
317 
318 static void
319 chd2(void)
320 {
321 	(void) fprintf(fout, "#ifdef __cplusplus\n");
322 	(void) fprintf(fout,
323 	"/* to avoid CC and lint complaining yyfussy not being used ...*/\n");
324 	(void) fprintf(fout, "static int __lex_hack = 0;\n");
325 	(void) fprintf(fout, "if (__lex_hack) goto yyfussy;\n");
326 	(void) fprintf(fout, "#endif\n");
327 	(void) fprintf(fout, "while((nstr = yylook()) >= 0)\n");
328 	(void) fprintf(fout, "yyfussy: switch(nstr){\n");
329 	(void) fprintf(fout, "case 0:\n");
330 	(void) fprintf(fout, "if(yywrap()) return(0); break;\n");
331 }
332 
333 void
334 ptail(void)
335 {
336 	if (!pflag)
337 		ratfor ? rtail() : ctail();
338 	pflag = 1;
339 }
340 
341 static void
342 ctail(void)
343 {
344 	(void) fprintf(fout, "case -1:\nbreak;\n");		/* for reject */
345 	(void) fprintf(fout, "default:\n");
346 	(void) fprintf(fout,
347 	"(void)fprintf(yyout,\"bad switch yylook %%d\",nstr);\n");
348 	(void) fprintf(fout, "} return(0); }\n");
349 	(void) fprintf(fout, "/* end of yylex */\n");
350 }
351 
352 static void
353 rtail(void)
354 {
355 	int i;
356 	(void) fprintf(fout,
357 	"\n30998 if(nstr .lt. 0 .or. nstr .gt. %d)goto 30999\n", casecount);
358 	(void) fprintf(fout, "nstr = nstr + 1\n");
359 	(void) fprintf(fout, "goto(\n");
360 	for (i = 0; i < casecount; i++)
361 		(void) fprintf(fout, "%d,\n", 30000+i);
362 	(void) fprintf(fout, "30999),nstr\n");
363 	(void) fprintf(fout, "30997 continue\n");
364 	(void) fprintf(fout, "}\nend\n");
365 }
366 
367 void
368 statistics(void)
369 {
370 	(void) fprintf(errorf,
371 "%d/%d nodes(%%e), %d/%d positions(%%p), %d/%d (%%n), %ld transitions,\n",
372 	    tptr, treesize, (int)(nxtpos-positions), maxpos, stnum + 1,
373 	    nstates, rcount);
374 	(void) fprintf(errorf,
375 	"%d/%d packed char classes(%%k), ", (int)(pcptr-pchar), pchlen);
376 	if (optim)
377 		(void) fprintf(errorf,
378 		" %d/%d packed transitions(%%a), ", nptr, ntrans);
379 	(void) fprintf(errorf, " %d/%d output slots(%%o)", yytop, outsize);
380 	(void) putc('\n', errorf);
381 }
382