10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54538Sdamico * Common Development and Distribution License (the "License").
64538Sdamico * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*6951Sab196087 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /* Copyright 1976, Bell Telephone Laboratories, Inc. */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include "once.h"
350Sstevel@tonic-gate #include "sgs.h"
360Sstevel@tonic-gate #include <locale.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate static wchar_t L_INITIAL[] = {'I', 'N', 'I', 'T', 'I', 'A', 'L', 0};
400Sstevel@tonic-gate static void get1core(void);
410Sstevel@tonic-gate static void free1core(void);
420Sstevel@tonic-gate static void get2core(void);
430Sstevel@tonic-gate static void free2core(void);
440Sstevel@tonic-gate static void get3core(void);
450Sstevel@tonic-gate #ifdef DEBUG
460Sstevel@tonic-gate static void free3core(void);
470Sstevel@tonic-gate #endif
480Sstevel@tonic-gate
490Sstevel@tonic-gate int
main(int argc,char ** argv)500Sstevel@tonic-gate main(int argc, char **argv)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate int i;
530Sstevel@tonic-gate int c;
540Sstevel@tonic-gate char *path = NULL;
550Sstevel@tonic-gate Boolean eoption = 0, woption = 0;
560Sstevel@tonic-gate
570Sstevel@tonic-gate sargv = argv;
580Sstevel@tonic-gate sargc = argc;
59*6951Sab196087 (void) setlocale(LC_ALL, "");
600Sstevel@tonic-gate #ifdef DEBUG
610Sstevel@tonic-gate while ((c = getopt(argc, argv, "dyctvnewVQ:Y:")) != EOF) {
620Sstevel@tonic-gate #else
630Sstevel@tonic-gate while ((c = getopt(argc, argv, "ctvnewVQ:Y:")) != EOF) {
640Sstevel@tonic-gate #endif
650Sstevel@tonic-gate switch (c) {
660Sstevel@tonic-gate #ifdef DEBUG
670Sstevel@tonic-gate case 'd':
680Sstevel@tonic-gate debug++;
690Sstevel@tonic-gate break;
700Sstevel@tonic-gate case 'y':
710Sstevel@tonic-gate yydebug = TRUE;
720Sstevel@tonic-gate break;
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate case 'V':
750Sstevel@tonic-gate (void) fprintf(stderr, "lex: %s %s\n",
760Sstevel@tonic-gate (const char *)SGU_PKG,
770Sstevel@tonic-gate (const char *)SGU_REL);
780Sstevel@tonic-gate break;
790Sstevel@tonic-gate case 'Q':
800Sstevel@tonic-gate v_stmp = optarg;
810Sstevel@tonic-gate if (*v_stmp != 'y' && *v_stmp != 'n')
820Sstevel@tonic-gate error(
830Sstevel@tonic-gate "lex: -Q should be followed by [y/n]");
840Sstevel@tonic-gate break;
850Sstevel@tonic-gate case 'Y':
860Sstevel@tonic-gate path = (char *)malloc(strlen(optarg) +
870Sstevel@tonic-gate sizeof ("/nceucform") + 1);
880Sstevel@tonic-gate path = strcpy(path, optarg);
890Sstevel@tonic-gate break;
900Sstevel@tonic-gate case 'c':
910Sstevel@tonic-gate ratfor = FALSE;
920Sstevel@tonic-gate break;
930Sstevel@tonic-gate case 't':
940Sstevel@tonic-gate fout = stdout;
950Sstevel@tonic-gate break;
960Sstevel@tonic-gate case 'v':
970Sstevel@tonic-gate report = 1;
980Sstevel@tonic-gate break;
990Sstevel@tonic-gate case 'n':
1000Sstevel@tonic-gate report = 0;
1010Sstevel@tonic-gate break;
1020Sstevel@tonic-gate case 'w':
1030Sstevel@tonic-gate case 'W':
1040Sstevel@tonic-gate woption = 1;
1050Sstevel@tonic-gate handleeuc = 1;
1060Sstevel@tonic-gate widecio = 1;
1070Sstevel@tonic-gate break;
1080Sstevel@tonic-gate case 'e':
1090Sstevel@tonic-gate case 'E':
1100Sstevel@tonic-gate eoption = 1;
1110Sstevel@tonic-gate handleeuc = 1;
1120Sstevel@tonic-gate widecio = 0;
1130Sstevel@tonic-gate break;
1140Sstevel@tonic-gate default:
1150Sstevel@tonic-gate (void) fprintf(stderr,
1160Sstevel@tonic-gate "Usage: lex [-ewctvnVY] [-Q(y/n)] [file]\n");
1170Sstevel@tonic-gate exit(1);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate if (woption && eoption) {
1210Sstevel@tonic-gate error(
1220Sstevel@tonic-gate "You may not specify both -w and -e simultaneously.");
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate no_input = argc - optind;
1250Sstevel@tonic-gate if (no_input) {
1260Sstevel@tonic-gate /* XCU4: recognize "-" file operand for stdin */
1270Sstevel@tonic-gate if (strcmp(argv[optind], "-") == 0)
1280Sstevel@tonic-gate fin = stdin;
1290Sstevel@tonic-gate else {
1300Sstevel@tonic-gate fin = fopen(argv[optind], "r");
1310Sstevel@tonic-gate if (fin == NULL)
1320Sstevel@tonic-gate error(
1330Sstevel@tonic-gate "Can't open input file -- %s", argv[optind]);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate } else
1360Sstevel@tonic-gate fin = stdin;
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /* may be gotten: def, subs, sname, schar, ccl, dchar */
1390Sstevel@tonic-gate (void) gch();
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /* may be gotten: name, left, right, nullstr, parent */
1420Sstevel@tonic-gate get1core();
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate scopy(L_INITIAL, sp);
1450Sstevel@tonic-gate sname[0] = sp;
1460Sstevel@tonic-gate sp += slength(L_INITIAL) + 1;
1470Sstevel@tonic-gate sname[1] = 0;
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /* XCU4: %x exclusive start */
1500Sstevel@tonic-gate exclusive[0] = 0;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (!handleeuc) {
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Set ZCH and ncg to their default values
1550Sstevel@tonic-gate * as they may be needed to handle %t directive.
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate ZCH = ncg = NCH; /* ncg behaves as constant in this mode. */
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate /* may be disposed of: def, subs, dchar */
1610Sstevel@tonic-gate if (yyparse())
1620Sstevel@tonic-gate exit(1); /* error return code */
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (handleeuc) {
1650Sstevel@tonic-gate ncg = ncgidtbl * 2;
1660Sstevel@tonic-gate ZCH = ncg;
1670Sstevel@tonic-gate if (ncg >= MAXNCG)
1680Sstevel@tonic-gate error(
1690Sstevel@tonic-gate "Too complex rules -- requires too many char groups.");
1700Sstevel@tonic-gate sortcgidtbl();
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate repbycgid(); /* Call this even in ASCII compat. mode. */
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate * maybe get:
1760Sstevel@tonic-gate * tmpstat, foll, positions, gotof, nexts,
1770Sstevel@tonic-gate * nchar, state, atable, sfall, cpackflg
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate free1core();
1800Sstevel@tonic-gate get2core();
1810Sstevel@tonic-gate ptail();
1820Sstevel@tonic-gate mkmatch();
1830Sstevel@tonic-gate #ifdef DEBUG
1840Sstevel@tonic-gate if (debug)
1850Sstevel@tonic-gate pccl();
1860Sstevel@tonic-gate #endif
1870Sstevel@tonic-gate sect = ENDSECTION;
1880Sstevel@tonic-gate if (tptr > 0)
1890Sstevel@tonic-gate cfoll(tptr-1);
1900Sstevel@tonic-gate #ifdef DEBUG
1910Sstevel@tonic-gate if (debug)
1920Sstevel@tonic-gate pfoll();
1930Sstevel@tonic-gate #endif
1940Sstevel@tonic-gate cgoto();
1950Sstevel@tonic-gate #ifdef DEBUG
1960Sstevel@tonic-gate if (debug) {
1970Sstevel@tonic-gate (void) printf("Print %d states:\n", stnum + 1);
1980Sstevel@tonic-gate for (i = 0; i <= stnum; i++)
1990Sstevel@tonic-gate stprt(i);
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate #endif
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate * may be disposed of:
2040Sstevel@tonic-gate * positions, tmpstat, foll, state, name,
2050Sstevel@tonic-gate * left, right, parent, ccl, schar, sname
2060Sstevel@tonic-gate * maybe get: verify, advance, stoff
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate free2core();
2090Sstevel@tonic-gate get3core();
2100Sstevel@tonic-gate layout();
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * may be disposed of:
2130Sstevel@tonic-gate * verify, advance, stoff, nexts, nchar,
2140Sstevel@tonic-gate * gotof, atable, ccpackflg, sfall
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate #ifdef DEBUG
2180Sstevel@tonic-gate free3core();
2190Sstevel@tonic-gate #endif
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate if (handleeuc) {
2220Sstevel@tonic-gate if (ratfor)
2230Sstevel@tonic-gate error("Ratfor is not supported by -w or -e option.");
2244538Sdamico path = EUCNAME;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate else
2274538Sdamico path = ratfor ? RATNAME : CNAME;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate fother = fopen(path, "r");
2300Sstevel@tonic-gate if (fother == NULL)
2310Sstevel@tonic-gate error("Lex driver missing, file %s", path);
2320Sstevel@tonic-gate while ((i = getc(fother)) != EOF)
2330Sstevel@tonic-gate (void) putc((char)i, fout);
2340Sstevel@tonic-gate (void) fclose(fother);
2350Sstevel@tonic-gate (void) fclose(fout);
2360Sstevel@tonic-gate if (report == 1)
2370Sstevel@tonic-gate statistics();
2380Sstevel@tonic-gate (void) fclose(stdout);
2390Sstevel@tonic-gate (void) fclose(stderr);
2400Sstevel@tonic-gate return (0); /* success return code */
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate static void
2440Sstevel@tonic-gate get1core(void)
2450Sstevel@tonic-gate {
246*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2470Sstevel@tonic-gate ccptr = ccl = (CHR *)myalloc(CCLSIZE, sizeof (*ccl));
248*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2490Sstevel@tonic-gate pcptr = pchar = (CHR *)myalloc(pchlen, sizeof (*pchar));
250*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2510Sstevel@tonic-gate def = (CHR **)myalloc(DEFSIZE, sizeof (*def));
252*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2530Sstevel@tonic-gate subs = (CHR **)myalloc(DEFSIZE, sizeof (*subs));
254*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2550Sstevel@tonic-gate dp = dchar = (CHR *)myalloc(DEFCHAR, sizeof (*dchar));
256*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2570Sstevel@tonic-gate sname = (CHR **)myalloc(STARTSIZE, sizeof (*sname));
2580Sstevel@tonic-gate /* XCU4: exclusive start array */
259*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2600Sstevel@tonic-gate exclusive = (int *)myalloc(STARTSIZE, sizeof (*exclusive));
261*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2620Sstevel@tonic-gate sp = schar = (CHR *)myalloc(STARTCHAR, sizeof (*schar));
2630Sstevel@tonic-gate if (ccl == 0 || def == 0 ||
2640Sstevel@tonic-gate pchar == 0 || subs == 0 || dchar == 0 ||
2650Sstevel@tonic-gate sname == 0 || exclusive == 0 || schar == 0)
2660Sstevel@tonic-gate error("Too little core to begin");
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate static void
2700Sstevel@tonic-gate free1core(void)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate free(def);
2730Sstevel@tonic-gate free(subs);
2740Sstevel@tonic-gate free(dchar);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate static void
2780Sstevel@tonic-gate get2core(void)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate int i;
281*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2820Sstevel@tonic-gate gotof = (int *)myalloc(nstates, sizeof (*gotof));
283*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2840Sstevel@tonic-gate nexts = (int *)myalloc(ntrans, sizeof (*nexts));
285*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2860Sstevel@tonic-gate nchar = (CHR *)myalloc(ntrans, sizeof (*nchar));
287*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2880Sstevel@tonic-gate state = (int **)myalloc(nstates, sizeof (*state));
289*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2900Sstevel@tonic-gate atable = (int *)myalloc(nstates, sizeof (*atable));
291*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2920Sstevel@tonic-gate sfall = (int *)myalloc(nstates, sizeof (*sfall));
2930Sstevel@tonic-gate cpackflg = (Boolean *)myalloc(nstates, sizeof (*cpackflg));
294*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2950Sstevel@tonic-gate tmpstat = (CHR *)myalloc(tptr+1, sizeof (*tmpstat));
296*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2970Sstevel@tonic-gate foll = (int **)myalloc(tptr+1, sizeof (*foll));
298*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
2990Sstevel@tonic-gate nxtpos = positions = (int *)myalloc(maxpos, sizeof (*positions));
3000Sstevel@tonic-gate if (tmpstat == 0 || foll == 0 || positions == 0 ||
3010Sstevel@tonic-gate gotof == 0 || nexts == 0 || nchar == 0 ||
3020Sstevel@tonic-gate state == 0 || atable == 0 || sfall == 0 || cpackflg == 0)
3030Sstevel@tonic-gate error("Too little core for state generation");
3040Sstevel@tonic-gate for (i = 0; i <= tptr; i++)
3050Sstevel@tonic-gate foll[i] = 0;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate static void
3090Sstevel@tonic-gate free2core(void)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate free(positions);
3120Sstevel@tonic-gate free(tmpstat);
3130Sstevel@tonic-gate free(foll);
3140Sstevel@tonic-gate free(name);
3150Sstevel@tonic-gate free(left);
3160Sstevel@tonic-gate free(right);
3170Sstevel@tonic-gate free(parent);
3180Sstevel@tonic-gate free(nullstr);
3190Sstevel@tonic-gate free(state);
3200Sstevel@tonic-gate free(sname);
3210Sstevel@tonic-gate /* XCU4: exclusive start array */
3220Sstevel@tonic-gate free(exclusive);
3230Sstevel@tonic-gate free(schar);
3240Sstevel@tonic-gate free(ccl);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate
3270Sstevel@tonic-gate static void
3280Sstevel@tonic-gate get3core(void)
3290Sstevel@tonic-gate {
330*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3310Sstevel@tonic-gate verify = (int *)myalloc(outsize, sizeof (*verify));
332*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3330Sstevel@tonic-gate advance = (int *)myalloc(outsize, sizeof (*advance));
334*6951Sab196087 /*LINTED: E_BAD_PTR_CAST_ALIGN*/
3350Sstevel@tonic-gate stoff = (int *)myalloc(stnum+2, sizeof (*stoff));
3360Sstevel@tonic-gate if (verify == 0 || advance == 0 || stoff == 0)
3370Sstevel@tonic-gate error("Too little core for final packing");
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate #ifdef DEBUG
3410Sstevel@tonic-gate static void
3420Sstevel@tonic-gate free3core(void)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate free(advance);
3450Sstevel@tonic-gate free(verify);
3460Sstevel@tonic-gate free(stoff);
3470Sstevel@tonic-gate free(gotof);
3480Sstevel@tonic-gate free(nexts);
3490Sstevel@tonic-gate free(nchar);
3500Sstevel@tonic-gate free(atable);
3510Sstevel@tonic-gate free(sfall);
3520Sstevel@tonic-gate free(cpackflg);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate #endif
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate BYTE *
3570Sstevel@tonic-gate myalloc(int a, int b)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate BYTE *i;
3600Sstevel@tonic-gate i = calloc(a, b);
3610Sstevel@tonic-gate if (i == 0)
3620Sstevel@tonic-gate warning("calloc returns a 0");
3630Sstevel@tonic-gate return (i);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate void
3670Sstevel@tonic-gate yyerror(char *s)
3680Sstevel@tonic-gate {
3690Sstevel@tonic-gate (void) fprintf(stderr,
3704538Sdamico "\"%s\":line %d: Error: %s\n", sargv[optind], yyline, s);
3710Sstevel@tonic-gate }
372