hbase

heirloom base
git clone git://git.2f30.org/hbase
Log | Files | Refs | README

main.c (7760B)


      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, Version 1.0 only
      6  * (the "License").  You may not use this file except in compliance
      7  * with the License.
      8  *
      9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     10  * or http://www.opensolaris.org/os/licensing.
     11  * See the License for the specific language governing permissions
     12  * and limitations under the License.
     13  *
     14  * When distributing Covered Code, include this CDDL HEADER in each
     15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     16  * If applicable, add the following below this CDDL HEADER, with the
     17  * fields enclosed by brackets "[]" replaced with your own identifying
     18  * information: Portions Copyright [yyyy] [name of copyright owner]
     19  *
     20  * CDDL HEADER END
     21  */
     22 /*
     23  * Copyright 2005 Sun Microsystems, Inc.
     24  * All rights reserved.
     25  * Use is subject to license terms.
     26  */
     27 
     28 /* Copyright (c) 1988 AT&T */
     29 /* All Rights Reserved */
     30 
     31 /* Copyright 1976, Bell Telephone Laboratories, Inc. */
     32 
     33 /*	from OpenSolaris "main.c	6.16	05/06/08 SMI"	*/
     34 
     35 /*
     36  * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
     37  *
     38  * Sccsid @(#)main.c	1.9 (gritter) 11/26/05
     39  */
     40 
     41 #include <string.h>
     42 #include "once.h"
     43 #include "sgs.h"
     44 #include <locale.h>
     45 #include <limits.h>
     46 #include <unistd.h>
     47 
     48 static wchar_t  L_INITIAL[] = {'I', 'N', 'I', 'T', 'I', 'A', 'L', 0};
     49 
     50 static void get1core(void);
     51 static void free1core(void);
     52 static void get2core(void);
     53 static void free2core(void);
     54 static void get3core(void);
     55 #ifdef DEBUG
     56 static void free3core(void);
     57 #endif
     58 
     59 int
     60 main(int argc, char **argv)
     61 {
     62 	int i;
     63 	int c;
     64 	char *path = NULL;
     65 	Boolean eoption = 0, woption = 0;
     66 
     67 	sargv = argv;
     68 	sargc = argc;
     69 	errorf = stderr;
     70 	setlocale(LC_CTYPE, "");
     71 #ifdef DEBUG
     72 	while ((c = getopt(argc, argv, "dyctvnewVQ:Y:")) != EOF) {
     73 #else
     74 	while ((c = getopt(argc, argv, "ctvnewVQ:Y:")) != EOF) {
     75 #endif
     76 		switch (c) {
     77 #ifdef DEBUG
     78 			case 'd':
     79 				debug++;
     80 				break;
     81 			case 'y':
     82 				yydebug = TRUE;
     83 				break;
     84 #endif
     85 			case 'V':
     86 				fprintf(stderr, "lex:%s , %s\n", pkg, rel);
     87 				break;
     88 			case 'Q':
     89 				v_stmp = optarg;
     90 				if (*v_stmp != 'y' && *v_stmp != 'n')
     91 					error(
     92 					"lex: -Q should be followed by [y/n]");
     93 				break;
     94 			case 'Y':
     95 				path = malloc(strlen(optarg) +
     96 				    sizeof ("/nceucform") + 1);
     97 				path = strcpy(path, optarg);
     98 				break;
     99 			case 'c':
    100 				ratfor = FALSE;
    101 				break;
    102 			case 't':
    103 				fout = stdout;
    104 				break;
    105 			case 'v':
    106 				report = 1;
    107 				break;
    108 			case 'n':
    109 				report = 0;
    110 				break;
    111 			case 'w':
    112 			case 'W':
    113 				woption = 1;
    114 				handleeuc = 1;
    115 				widecio = 1;
    116 				break;
    117 			case 'e':
    118 			case 'E':
    119 				eoption = 1;
    120 				handleeuc = 1;
    121 				widecio = 0;
    122 				break;
    123 			default:
    124 				fprintf(stderr,
    125 				"Usage: lex [-ctvnV] [-Q(y/n)] [files...]\n");
    126 				exit(1);
    127 		}
    128 	}
    129 	if (woption && eoption) {
    130 		error(
    131 		"You may not specify both -w and -e simultaneously.");
    132 	}
    133 	no_input = argc - optind;
    134 	if (no_input) {
    135 		/* XCU4: recognize "-" file operand for stdin */
    136 		if (strcmp(argv[optind], "-") == 0)
    137 			fin = stdin;
    138 		else {
    139 			fin = fopen(argv[optind], "r");
    140 			if (fin == NULL)
    141 				error(
    142 				"Can't open input file -- %s", argv[optind]);
    143 		}
    144 	} else
    145 		fin = stdin;
    146 
    147 	/* may be gotten: def, subs, sname, schar, ccl, dchar */
    148 	gch();
    149 
    150 	/* may be gotten: name, left, right, nullstr, parent */
    151 	get1core();
    152 
    153 	scopy(L_INITIAL, sp);
    154 	sname[0] = sp;
    155 	sp += slength(L_INITIAL) + 1;
    156 	sname[1] = 0;
    157 
    158 	/* XCU4: %x exclusive start */
    159 	exclusive[0] = 0;
    160 
    161 	if (!handleeuc) {
    162 		/*
    163 		 * Set ZCH and ncg to their default values
    164 		 * as they may be needed to handle %t directive.
    165 		 */
    166 		ZCH = ncg = NCH; /* ncg behaves as constant in this mode. */
    167 	}
    168 
    169 	/* may be disposed of: def, subs, dchar */
    170 	if (yyparse())
    171 		exit(1);	/* error return code */
    172 
    173 	if (handleeuc) {
    174 		ncg = ncgidtbl * 2;
    175 		ZCH = ncg;
    176 		if (ncg >= MAXNCG)
    177 			error(
    178 			"Too complex rules -- requires too many char groups.");
    179 		sortcgidtbl();
    180 	}
    181 	repbycgid(); /* Call this even in ASCII compat. mode. */
    182 
    183 	/*
    184 	 * maybe get:
    185 	 *		tmpstat, foll, positions, gotof, nexts,
    186 	 *		nchar, state, atable, sfall, cpackflg
    187 	 */
    188 	free1core();
    189 	get2core();
    190 	ptail();
    191 	mkmatch();
    192 #ifdef DEBUG
    193 	if (debug)
    194 		pccl();
    195 #endif
    196 	sect  = ENDSECTION;
    197 	if (tptr > 0)
    198 		cfoll(tptr-1);
    199 #ifdef DEBUG
    200 	if (debug)
    201 		pfoll();
    202 #endif
    203 	cgoto();
    204 #ifdef DEBUG
    205 	if (debug) {
    206 		printf("Print %d states:\n", stnum + 1);
    207 		for (i = 0; i <= stnum; i++)
    208 			stprt(i);
    209 	}
    210 #endif
    211 	/*
    212 	 * may be disposed of:
    213 	 *		positions, tmpstat, foll, state, name,
    214 	 *		left, right, parent, ccl, schar, sname
    215 	 * maybe get:	 verify, advance, stoff
    216 	 */
    217 	free2core();
    218 	get3core();
    219 	layout();
    220 	/*
    221 	 * may be disposed of:
    222 	 *		verify, advance, stoff, nexts, nchar,
    223 	 *		gotof, atable, ccpackflg, sfall
    224 	 */
    225 
    226 #ifdef DEBUG
    227 	free3core();
    228 #endif
    229 	if (path == NULL) {
    230 		static char formpath[sizeof FORMPATH + 20] = FORMPATH;
    231 		path = formpath;
    232 	}
    233 
    234 	if (handleeuc) {
    235 		if (ratfor)
    236 			error("Ratfor is not supported by -w or -e option.");
    237 		strcat(path, "/nceucform");
    238 	}
    239 	else
    240 		strcat(path, ratfor ? "/nrform" : "/ncform");
    241 
    242 	fother = fopen(path, "r");
    243 	if (fother == NULL)
    244 		error("Lex driver missing, file %s", path);
    245 	while ((i = getc(fother)) != EOF)
    246 		putc(i, fout);
    247 	fclose(fother);
    248 	fclose(fout);
    249 	if (report == 1)
    250 		statistics();
    251 	fclose(stdout);
    252 	fclose(stderr);
    253 	return (0);	/* success return code */
    254 }
    255 
    256 static void
    257 get1core(void)
    258 {
    259 	ccptr =	ccl = myalloc(CCLSIZE, sizeof (*ccl));
    260 	pcptr = pchar = myalloc(pchlen, sizeof (*pchar));
    261 	def = myalloc(DEFSIZE, sizeof (*def));
    262 	subs = myalloc(DEFSIZE, sizeof (*subs));
    263 	dp = dchar = myalloc(DEFCHAR, sizeof (*dchar));
    264 	sname = myalloc(STARTSIZE, sizeof (*sname));
    265 	/* XCU4: exclusive start array */
    266 	exclusive = myalloc(STARTSIZE, sizeof (*exclusive));
    267 	sp = schar = myalloc(STARTCHAR, sizeof (*schar));
    268 	if (ccl == 0 || def == 0 ||
    269 	    pchar == 0 || subs == 0 || dchar == 0 ||
    270 	    sname == 0 || exclusive == 0 || schar == 0)
    271 		error("Too little core to begin");
    272 }
    273 
    274 static void
    275 free1core(void)
    276 {
    277 	free(def);
    278 	free(subs);
    279 	free(dchar);
    280 }
    281 
    282 static void
    283 get2core(void)
    284 {
    285 	int i;
    286 	gotof = myalloc(nstates, sizeof (*gotof));
    287 	nexts = myalloc(ntrans, sizeof (*nexts));
    288 	nchar = myalloc(ntrans, sizeof (*nchar));
    289 	state = myalloc(nstates, sizeof (*state));
    290 	atable =myalloc(nstates, sizeof (*atable));
    291 	sfall = myalloc(nstates, sizeof (*sfall));
    292 	cpackflg = myalloc(nstates, sizeof (*cpackflg));
    293 	tmpstat = myalloc(tptr+1, sizeof (*tmpstat));
    294 	foll = myalloc(tptr+1, sizeof (*foll));
    295 	nxtpos = positions = myalloc(maxpos, sizeof (*positions));
    296 	if (tmpstat == 0 || foll == 0 || positions == 0 ||
    297 	    gotof == 0 || nexts == 0 || nchar == 0 ||
    298 	    state == 0 || atable == 0 || sfall == 0 || cpackflg == 0)
    299 		error("Too little core for state generation");
    300 	for (i = 0; i <= tptr; i++)
    301 		foll[i] = 0;
    302 }
    303 
    304 static void
    305 free2core(void)
    306 {
    307 	free(positions);
    308 	free(tmpstat);
    309 	free(foll);
    310 	free(name);
    311 	free(left);
    312 	free(right);
    313 	free(parent);
    314 	free(nullstr);
    315 	free(state);
    316 	free(sname);
    317 	/* XCU4: exclusive start array */
    318 	free(exclusive);
    319 	free(schar);
    320 	free(ccl);
    321 }
    322 
    323 static void
    324 get3core(void)
    325 {
    326 	verify = myalloc(outsize, sizeof (*verify));
    327 	advance = myalloc(outsize, sizeof (*advance));
    328 	stoff = myalloc(stnum+2, sizeof (*stoff));
    329 	if (verify == 0 || advance == 0 || stoff == 0)
    330 		error("Too little core for final packing");
    331 }
    332 
    333 #ifdef DEBUG
    334 static void
    335 free3core(void)
    336 {
    337 	free(advance);
    338 	free(verify);
    339 	free(stoff);
    340 	free(gotof);
    341 	free(nexts);
    342 	free(nchar);
    343 	free(atable);
    344 	free(sfall);
    345 	free(cpackflg);
    346 }
    347 #endif
    348 
    349 void *
    350 myalloc(int a, int b)
    351 {
    352 	void *i;
    353 	i = calloc(a,  b);
    354 	if (i == NULL)
    355 		warning("calloc returns a 0");
    356 	return (i);
    357 }
    358 
    359 void
    360 yyerror(char *s)
    361 {
    362 	fprintf(stderr,
    363 		"\"%s\":line %d: Error: %s\n", sargv[optind], yyline, s);
    364 }