xref: /onnv-gate/usr/src/cmd/oawk/b.c (revision 732:aa54f2ad3011)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
230Sstevel@tonic-gate /*
240Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
28731Srobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29731Srobbin /*	  All Rights Reserved  	*/
30731Srobbin 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "awk.def"
340Sstevel@tonic-gate #include "stdio.h"
350Sstevel@tonic-gate #include "awk.h"
36731Srobbin #include <stdlib.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate 
390Sstevel@tonic-gate extern NODE *op2();
400Sstevel@tonic-gate extern struct fa *cgotofn();
410Sstevel@tonic-gate #define	MAXLIN 256
420Sstevel@tonic-gate #define	NCHARS 128
430Sstevel@tonic-gate #define	NSTATES 256
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #define	type(v)	v->nobj
470Sstevel@tonic-gate #define	left(v)	v->narg[0]
480Sstevel@tonic-gate #define	right(v)	v->narg[1]
490Sstevel@tonic-gate #define	parent(v)	v->nnext
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	LEAF	case CCL: case NCCL: case CHAR: case DOT:
530Sstevel@tonic-gate #define	UNARY	case FINAL: case STAR: case PLUS: case QUEST:
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * encoding in tree NODEs:
580Sstevel@tonic-gate  * leaf (CCL, NCCL, CHAR, DOT): left is index,
590Sstevel@tonic-gate  * right contains value or pointer to value
600Sstevel@tonic-gate  * unary (FINAL, STAR, PLUS, QUEST): left is child, right is null
610Sstevel@tonic-gate  * binary (CAT, OR): left and right are children
620Sstevel@tonic-gate  * parent contains pointer to parent
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 
660Sstevel@tonic-gate struct fa {
670Sstevel@tonic-gate union {
680Sstevel@tonic-gate 		ccl_chars_t s;
690Sstevel@tonic-gate 		int h;
700Sstevel@tonic-gate 	} cc;
710Sstevel@tonic-gate #define	MLCMPLT(m1, l1, m2, l2) ((m1 != m2 &&\
720Sstevel@tonic-gate 				(int)m1 < (int)m2) ||\
730Sstevel@tonic-gate 				(m1 == m2 && (int)l1 < (int)l2))
740Sstevel@tonic-gate #define	MLCMPLE(m1, l1, m2, l2) ((m1 != m2 &&\
750Sstevel@tonic-gate 				(int)m1 <= (int)m2) ||\
760Sstevel@tonic-gate 				(m1 == m2 && (int)l1 <= (int)l2))
770Sstevel@tonic-gate #define	MLCMPGT(m1, l1, m2, l2) ((m1 != m2 &&\
780Sstevel@tonic-gate 				(int)m1 > (int)m2) ||\
790Sstevel@tonic-gate 				(m1 == m2 && (int)l1 > (int)l2))
800Sstevel@tonic-gate #define	MAX_CODESET	3
810Sstevel@tonic-gate 	struct fa *st;
820Sstevel@tonic-gate };
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 
850Sstevel@tonic-gate int	*state[NSTATES];
860Sstevel@tonic-gate int	*foll[MAXLIN];
870Sstevel@tonic-gate int	setvec[MAXLIN];
880Sstevel@tonic-gate NODE	*point[MAXLIN];
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 
910Sstevel@tonic-gate int	setcnt;
920Sstevel@tonic-gate int	line;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static int	ccln_member();
960Sstevel@tonic-gate static int	insert_table();
970Sstevel@tonic-gate static int	delete_table();
98731Srobbin static void	penter(NODE *p);
99731Srobbin static void	follow(NODE *v);
100731Srobbin static void	overflo(void);
101731Srobbin static void	cfoll(NODE *v);
102731Srobbin static void	freetr(NODE *p);
1030Sstevel@tonic-gate #ifdef DEBUG
1040Sstevel@tonic-gate #define	ddump_table(t, s)	dump_table(t, s)
1050Sstevel@tonic-gate #else
1060Sstevel@tonic-gate #define	ddump_table(t, s)
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate struct fa *
makedfa(p)1100Sstevel@tonic-gate makedfa(p)	/* returns dfa for tree pointed to by p */
1110Sstevel@tonic-gate NODE *p;
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate 	NODE *p1;
1140Sstevel@tonic-gate 	struct fa *fap;
1150Sstevel@tonic-gate 	p1 = op2(CAT, op2(STAR, op2(DOT, (NODE *) 0,
1160Sstevel@tonic-gate 		(NODE *) 0), (NODE *) 0), p);
1170Sstevel@tonic-gate 		/* put DOT STAR in front of reg. exp. */
1180Sstevel@tonic-gate 	p1 = op2(FINAL, p1, (NODE *) 0);	/* install FINAL NODE */
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	line = 0;
1220Sstevel@tonic-gate 	penter(p1);	/* enter parent pointers and leaf indices */
1230Sstevel@tonic-gate 	point[line] = p1;	/* FINAL NODE */
1240Sstevel@tonic-gate 	setvec[0] = 1;		/* for initial DOT STAR */
1250Sstevel@tonic-gate 	cfoll(p1);	/* set up follow sets */
1260Sstevel@tonic-gate 	fap = cgotofn();
1270Sstevel@tonic-gate 	freetr(p1);	/* add this when alloc works */
1280Sstevel@tonic-gate 	return (fap);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate 
131731Srobbin static void
penter(NODE * p)132731Srobbin penter(NODE *p)	/* set up parent pointers and leaf indices */
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	switch (type(p)) {
1350Sstevel@tonic-gate 		LEAF
1360Sstevel@tonic-gate 			left(p) = (NODE *)line;
1370Sstevel@tonic-gate 			point[line++] = p;
1380Sstevel@tonic-gate 			break;
1390Sstevel@tonic-gate 		UNARY
1400Sstevel@tonic-gate 			penter(left(p));
1410Sstevel@tonic-gate 			parent(left(p)) = p;
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		case CAT:
1440Sstevel@tonic-gate 		case OR:
1450Sstevel@tonic-gate 			penter(left(p));
1460Sstevel@tonic-gate 			penter(right(p));
1470Sstevel@tonic-gate 			parent(left(p)) = p;
1480Sstevel@tonic-gate 			parent(right(p)) = p;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 		default:
1510Sstevel@tonic-gate 			error(FATAL, "unknown type %d in penter\n", type(p));
1520Sstevel@tonic-gate 			break;
1530Sstevel@tonic-gate 	}
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
156731Srobbin static void
freetr(NODE * p)157731Srobbin freetr(NODE *p)	/* free parse tree and follow sets */
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	switch (type(p)) {
1600Sstevel@tonic-gate 		LEAF
1610Sstevel@tonic-gate 			xfree(foll[(int)left(p)]);
1620Sstevel@tonic-gate 			xfree(p);
1630Sstevel@tonic-gate 			break;
1640Sstevel@tonic-gate 		UNARY
1650Sstevel@tonic-gate 			freetr(left(p));
1660Sstevel@tonic-gate 			xfree(p);
1670Sstevel@tonic-gate 			break;
1680Sstevel@tonic-gate 		case CAT:
1690Sstevel@tonic-gate 		case OR:
1700Sstevel@tonic-gate 			freetr(left(p));
1710Sstevel@tonic-gate 			freetr(right(p));
1720Sstevel@tonic-gate 			xfree(p);
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 		default:
1750Sstevel@tonic-gate 			error(FATAL, "unknown type %d in freetr", type(p));
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate ccl_chars_t *
cclenter(wchar_t * p)180731Srobbin cclenter(wchar_t *p)
1810Sstevel@tonic-gate {
182731Srobbin 	int 		i, cn;
183731Srobbin 	wchar_t		c, pc;
184731Srobbin 	wchar_t		*op;
185731Srobbin 	ccl_chars_t	*new;
186731Srobbin 	ccl_chars_t	chars[MAXLIN];
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	op = p;
1890Sstevel@tonic-gate 	i = 0;
1900Sstevel@tonic-gate 	while ((c = *p++) != 0) {
1910Sstevel@tonic-gate 		if (c == '-' && i > 0)  {
1920Sstevel@tonic-gate 			if (*p != 0) {
1930Sstevel@tonic-gate 				/*
1940Sstevel@tonic-gate 				 * If there are not in same code set,  the
1950Sstevel@tonic-gate 				 * class should be ignore (make two independent
1960Sstevel@tonic-gate 				 * characters)!
1970Sstevel@tonic-gate 				 */
1980Sstevel@tonic-gate 				c = *p++;
1990Sstevel@tonic-gate 				cn = wcsetno(pc);
2000Sstevel@tonic-gate 				if (cn != wcsetno(c) || pc > c)
2010Sstevel@tonic-gate 					goto char_array;
2020Sstevel@tonic-gate 				i = insert_table(chars, i, cn, pc, cn, c);
2030Sstevel@tonic-gate 				continue;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate char_array:
2070Sstevel@tonic-gate 		if (i >= MAXLIN)
2080Sstevel@tonic-gate 			overflo();
2090Sstevel@tonic-gate 		cn = wcsetno(c);
2100Sstevel@tonic-gate 		i = insert_table(chars, i, cn, c, cn, c);
2110Sstevel@tonic-gate 		pc = c;
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	dprintf("cclenter: in = |%ws|, ", op, NULL, NULL);
2140Sstevel@tonic-gate 	xfree(op);
2150Sstevel@tonic-gate 	i = (i + 1) * sizeof (ccl_chars_t);
2160Sstevel@tonic-gate 	if ((new = (ccl_chars_t *)malloc(i)) == NULL)
2170Sstevel@tonic-gate 		error(FATAL, "out of space in cclenter on %s", op);
2180Sstevel@tonic-gate 	(void) memcpy((char *)new, (char *)chars, i);
2190Sstevel@tonic-gate 	ddump_table(chars, i / 4);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	return (new);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate 
225731Srobbin static void
overflo(void)226731Srobbin overflo(void)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate 	error(FATAL, "regular expression too long\n");
2290Sstevel@tonic-gate }
2300Sstevel@tonic-gate 
231731Srobbin static void
cfoll(NODE * v)232731Srobbin cfoll(NODE *v)	/* enter follow set of each leaf of vertex v into foll[leaf] */
2330Sstevel@tonic-gate {
234731Srobbin 	int i;
2350Sstevel@tonic-gate 	int prev;
2360Sstevel@tonic-gate 	int *add();
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	switch (type(v)) {
2400Sstevel@tonic-gate 		LEAF
2410Sstevel@tonic-gate 			setcnt = 0;
2420Sstevel@tonic-gate 			for (i = 1; i <= line; i++)
2430Sstevel@tonic-gate 				setvec[i] = 0;
2440Sstevel@tonic-gate 			follow(v);
2450Sstevel@tonic-gate 			foll[(int)left(v)] = add(setcnt);
2460Sstevel@tonic-gate 			break;
2470Sstevel@tonic-gate 		UNARY
2480Sstevel@tonic-gate 			cfoll(left(v));
2490Sstevel@tonic-gate 			break;
2500Sstevel@tonic-gate 		case CAT:
2510Sstevel@tonic-gate 		case OR:
2520Sstevel@tonic-gate 			cfoll(left(v));
2530Sstevel@tonic-gate 			cfoll(right(v));
2540Sstevel@tonic-gate 			break;
2550Sstevel@tonic-gate 		default:
2560Sstevel@tonic-gate 			error(FATAL, "unknown type %d in cfoll", type(v));
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate 
260731Srobbin int
first(NODE * p)261731Srobbin first(NODE *p)		/* collects initially active leaves of p into setvec */
2620Sstevel@tonic-gate 	/* returns 0 or 1 depending on whether p matches empty string */
2630Sstevel@tonic-gate {
264731Srobbin 	int b;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	switch (type(p)) {
2680Sstevel@tonic-gate 		LEAF
2690Sstevel@tonic-gate 			if (setvec[(int)left(p)] != 1) {
2700Sstevel@tonic-gate 				setvec[(int)left(p)] = 1;
2710Sstevel@tonic-gate 				setcnt++;
2720Sstevel@tonic-gate 			}
2730Sstevel@tonic-gate 			if (type(p) == CCL &&
2740Sstevel@tonic-gate 			(*(ccl_chars_t *)right(p)).cc_cs == (wchar_t)0x0)
2750Sstevel@tonic-gate 				return (0);		/* empty CCL */
2760Sstevel@tonic-gate 			else return (1);
2770Sstevel@tonic-gate 		case FINAL:
2780Sstevel@tonic-gate 		case PLUS:
2790Sstevel@tonic-gate 			if (first(left(p)) == 0)
2800Sstevel@tonic-gate 				return (0);
2810Sstevel@tonic-gate 			return (1);
2820Sstevel@tonic-gate 		case STAR:
2830Sstevel@tonic-gate 		case QUEST:
2840Sstevel@tonic-gate 			first(left(p));
2850Sstevel@tonic-gate 			return (0);
2860Sstevel@tonic-gate 		case CAT:
2870Sstevel@tonic-gate 			if (first(left(p)) == 0 && first(right(p)) == 0)
2880Sstevel@tonic-gate 				return (0);
2890Sstevel@tonic-gate 			return (1);
2900Sstevel@tonic-gate 		case OR:
2910Sstevel@tonic-gate 			b = first(right(p));
2920Sstevel@tonic-gate 			if (first(left(p)) == 0 || b == 0)
2930Sstevel@tonic-gate 				return (0);
2940Sstevel@tonic-gate 			return (1);
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	error(FATAL, "unknown type %d in first\n", type(p));
2970Sstevel@tonic-gate 	return (-1);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
300731Srobbin static void
follow(NODE * v)301731Srobbin follow(NODE *v)
302731Srobbin 		/* collects leaves that can follow v into setvec */
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate 	NODE *p;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 	if (type(v) == FINAL)
3080Sstevel@tonic-gate 		return;
3090Sstevel@tonic-gate 	p = parent(v);
3100Sstevel@tonic-gate 	switch (type(p)) {
3110Sstevel@tonic-gate 		case STAR:
3120Sstevel@tonic-gate 		case PLUS:	first(v);
3130Sstevel@tonic-gate 				follow(p);
3140Sstevel@tonic-gate 				return;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 		case OR:
3180Sstevel@tonic-gate 		case QUEST:	follow(p);
3190Sstevel@tonic-gate 				return;
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		case CAT:	if (v == left(p)) { /* v is left child of p */
3230Sstevel@tonic-gate 					if (first(right(p)) == 0) {
3240Sstevel@tonic-gate 						follow(p);
3250Sstevel@tonic-gate 						return;
3260Sstevel@tonic-gate 					}
3270Sstevel@tonic-gate 				} else		/* v is right child */
3280Sstevel@tonic-gate 					follow(p);
3290Sstevel@tonic-gate 				return;
3300Sstevel@tonic-gate 		case FINAL:	if (setvec[line] != 1) {
3310Sstevel@tonic-gate 					setvec[line] = 1;
3320Sstevel@tonic-gate 					setcnt++;
3330Sstevel@tonic-gate 				}
3340Sstevel@tonic-gate 				return;
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate  * There are three type of functions for checking member ship.  Because I have
3410Sstevel@tonic-gate  * been changed structure of CCL tables.  And some CCL tables end up with NULLs
3420Sstevel@tonic-gate  * but someone has length and will includes NULLs in table as one of data.
3430Sstevel@tonic-gate  * Please note, CCL table which has a length data and data will include NULLs,
3440Sstevel@tonic-gate  * it only used within a this source file("b.c").
3450Sstevel@tonic-gate  */
3460Sstevel@tonic-gate 
347731Srobbin int				/* is cs thru ce in s? */
ccl_member(int ns,wchar_t cs,int ne,wchar_t ce,ccl_chars_t * s)348731Srobbin ccl_member(int ns, wchar_t cs, int ne, wchar_t ce, ccl_chars_t *s)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * The specified range(cs, ce) must be beside the range between
3520Sstevel@tonic-gate 	 * s->cc_start and s->cc_end to determine member.
3530Sstevel@tonic-gate 	 */
3540Sstevel@tonic-gate 	while (s->cc_cs || s->cc_ce) {
3550Sstevel@tonic-gate 		if (MLCMPLE(s->cc_ns, s->cc_cs, ns, cs) &&
3560Sstevel@tonic-gate 				MLCMPLE(ne, ce, s->cc_ne, s->cc_ce))
3570Sstevel@tonic-gate 			return (1);
3580Sstevel@tonic-gate 		s++;
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	return (0);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 
364731Srobbin static int			/* is cs thru ce in s? */
ccln_member(int ns,wchar_t cs,int ne,wchar_t ce,ccl_chars_t * s,int n)365731Srobbin ccln_member(int ns, wchar_t cs, int ne, wchar_t ce, ccl_chars_t *s, int n)
3660Sstevel@tonic-gate {
3670Sstevel@tonic-gate 	/*
3680Sstevel@tonic-gate 	 * The specified range(cs, ce) must be beside the range between
3690Sstevel@tonic-gate 	 * s->cc_start and s->cc_end to determine member.
3700Sstevel@tonic-gate 	 */
3710Sstevel@tonic-gate 	while (n-- > 0) {
3720Sstevel@tonic-gate 		if (MLCMPLE(s->cc_ns, s->cc_cs, ns, cs) &&
3730Sstevel@tonic-gate 				MLCMPLE(ne, ce, s->cc_ne, s->cc_ce))
3740Sstevel@tonic-gate 			return (1);
3750Sstevel@tonic-gate 		s++;
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 	return (0);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 
381731Srobbin int
member(wchar_t c,wchar_t * s)382731Srobbin member(wchar_t c, wchar_t *s)	/* is c in s? */
3830Sstevel@tonic-gate {
3840Sstevel@tonic-gate 	while (*s)
3850Sstevel@tonic-gate 		if (c == *s++)
3860Sstevel@tonic-gate 			return (1);
3870Sstevel@tonic-gate 	return (0);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
390731Srobbin int
notin(int ** array,int n,int * prev)391731Srobbin notin(int **array, int n, int *prev) /* is setvec in array[0] thru array[n]? */
392731Srobbin {
393731Srobbin 	int i, j;
3940Sstevel@tonic-gate 	int *ptr;
3950Sstevel@tonic-gate 	for (i = 0; i <= n; i++) {
3960Sstevel@tonic-gate 		ptr = array[i];
3970Sstevel@tonic-gate 		if (*ptr == setcnt) {
3980Sstevel@tonic-gate 			for (j = 0; j < setcnt; j++)
3990Sstevel@tonic-gate 				if (setvec[*(++ptr)] != 1) goto nxt;
4000Sstevel@tonic-gate 			*prev = i;
4010Sstevel@tonic-gate 			return (0);
4020Sstevel@tonic-gate 		}
4030Sstevel@tonic-gate 		nxt: /* dummy */;
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 	return (1);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 
409731Srobbin int *
add(int n)410731Srobbin add(int n)
411731Srobbin {		/* remember setvec */
4120Sstevel@tonic-gate 	int *ptr, *p;
413731Srobbin 	int i;
4140Sstevel@tonic-gate 	if ((p = ptr = (int *)malloc((n+1)*sizeof (int))) == NULL)
4150Sstevel@tonic-gate 		overflo();
4160Sstevel@tonic-gate 	*ptr = n;
4170Sstevel@tonic-gate 	dprintf("add(%d)\n", n, NULL, NULL);
4180Sstevel@tonic-gate 	for (i = 1; i <= line; i++)
4190Sstevel@tonic-gate 		if (setvec[i] == 1) {
4200Sstevel@tonic-gate 			*(++ptr) = i;
4210Sstevel@tonic-gate 		dprintf("  ptr = %o, *ptr = %d, i = %d\n", ptr, *ptr, i);
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 	dprintf("\n", NULL, NULL, NULL);
4240Sstevel@tonic-gate 	return (p);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate struct fa *
cgotofn()4290Sstevel@tonic-gate cgotofn()
4300Sstevel@tonic-gate {
431731Srobbin 	int i, k;
432731Srobbin 	int *ptr;
433731Srobbin 	int ns, ne;
434731Srobbin 	wchar_t cs, ce;
435731Srobbin 	ccl_chars_t *p;
436731Srobbin 	NODE *cp;
4370Sstevel@tonic-gate 	int j, n, s, ind, numtrans;
4380Sstevel@tonic-gate 	int finflg;
4390Sstevel@tonic-gate 	int curpos, num, prev;
4400Sstevel@tonic-gate 	struct fa *where[NSTATES];
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	struct {
4440Sstevel@tonic-gate 		ccl_chars_t	cc;
4450Sstevel@tonic-gate 		int		n;
4460Sstevel@tonic-gate 	} fatab[257];
447731Srobbin 	struct fa *pfa;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	char index[MAXLIN];
4510Sstevel@tonic-gate 	char iposns[MAXLIN];
4520Sstevel@tonic-gate 	int sposns[MAXLIN];
4530Sstevel@tonic-gate 	int spmax, spinit;
4540Sstevel@tonic-gate 	ccl_chars_t symbol[NCHARS];
4550Sstevel@tonic-gate 	ccl_chars_t isyms[NCHARS];
4560Sstevel@tonic-gate 	ccl_chars_t ssyms[NCHARS];
4570Sstevel@tonic-gate 	int ssmax, symax, ismax, ssinit;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	wchar_t hat;
4610Sstevel@tonic-gate 	int hatcn;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	for (i = 0; i <= line; i++) index[i] = iposns[i] = setvec[i] = 0;
4650Sstevel@tonic-gate 	isyms[0].cc_cs = isyms[0].cc_ce = (wchar_t)0x0;
4660Sstevel@tonic-gate 	for (i = 0; i < NCHARS; i++)
4670Sstevel@tonic-gate 		isyms[i] = symbol[i] = ssyms[i] = isyms[0];
4680Sstevel@tonic-gate 	symax = 0;
4690Sstevel@tonic-gate 	setcnt = 0;
4700Sstevel@tonic-gate 	/* compute initial positions and symbols of state 0 */
4710Sstevel@tonic-gate 	ismax = 0;
4720Sstevel@tonic-gate 	ssmax = 0;
4730Sstevel@tonic-gate 	ptr = state[0] = foll[0];
4740Sstevel@tonic-gate 	spinit = *ptr;
4750Sstevel@tonic-gate 	hat = HAT;
4760Sstevel@tonic-gate 	hatcn = wcsetno(hat);
4770Sstevel@tonic-gate 	for (i = 0; i < spinit; i++) {
4780Sstevel@tonic-gate 		curpos = *(++ptr);
4790Sstevel@tonic-gate 		sposns[i] = curpos;
4800Sstevel@tonic-gate 		iposns[curpos] = 1;
4810Sstevel@tonic-gate 		cp = point[curpos];
4820Sstevel@tonic-gate 		dprintf("i= %d, spinit = %d, curpos = %d\n", i, spinit, curpos);
4830Sstevel@tonic-gate 		switch (type(cp)) {
4840Sstevel@tonic-gate 			case CHAR:
4850Sstevel@tonic-gate 				k = (int)right(cp);
4860Sstevel@tonic-gate 				ns = wcsetno(k);
4870Sstevel@tonic-gate 				if (! ccln_member(ns, k, ns, k,
4880Sstevel@tonic-gate 							isyms, ismax)) {
4890Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
4900Sstevel@tonic-gate 								ns, k, ns, k);
4910Sstevel@tonic-gate 				}
4920Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
4930Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = k;
4940Sstevel@tonic-gate 				ssyms[ssmax].cc_ne = ns;
4950Sstevel@tonic-gate 				ssyms[ssmax++].cc_ce = k;
4960Sstevel@tonic-gate 				break;
4970Sstevel@tonic-gate 			case DOT:
4980Sstevel@tonic-gate 				cs = WC_VERY_SMALL;
4990Sstevel@tonic-gate 				ns = 0;
5000Sstevel@tonic-gate 				ce = HAT - 1;
5010Sstevel@tonic-gate 				ne = hatcn;
5020Sstevel@tonic-gate 				if (! ccln_member(ns, cs, ne, ce,
5030Sstevel@tonic-gate 							isyms, ismax)) {
5040Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
5050Sstevel@tonic-gate 								ns, cs, ne, ce);
5060Sstevel@tonic-gate 				}
5070Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = cs;
5080Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
5090Sstevel@tonic-gate 				ssyms[ssmax].cc_ce = ce;
5100Sstevel@tonic-gate 				ssyms[ssmax++].cc_ne = ne;
5110Sstevel@tonic-gate 				cs = HAT + 1;
5120Sstevel@tonic-gate 				ns = hatcn;
5130Sstevel@tonic-gate 				ce = WC_VERY_LARGE;
5140Sstevel@tonic-gate 				ne = MAX_CODESET;
5150Sstevel@tonic-gate 				if (! ccln_member(ns, cs, ne, ce,
5160Sstevel@tonic-gate 							isyms, ismax)) {
5170Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
5180Sstevel@tonic-gate 								ns, cs, ne, ce);
5190Sstevel@tonic-gate 				}
5200Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = cs;
5210Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
5220Sstevel@tonic-gate 				ssyms[ssmax].cc_ce = ce;
5230Sstevel@tonic-gate 				ssyms[ssmax++].cc_ne = ne;
5240Sstevel@tonic-gate 				break;
5250Sstevel@tonic-gate 			case CCL:
5260Sstevel@tonic-gate 				cs = HAT;
5270Sstevel@tonic-gate 				ns = hatcn;
5280Sstevel@tonic-gate 				for (p = (ccl_chars_t *)right(cp);
5290Sstevel@tonic-gate 					p->cc_cs; p++) {
5300Sstevel@tonic-gate 					if ((p->cc_ns != ns ||\
5310Sstevel@tonic-gate 					p->cc_cs != cs) &&\
5320Sstevel@tonic-gate 				!ccln_member(p->cc_ns, p->cc_cs,
5330Sstevel@tonic-gate 				p->cc_ne, p->cc_ce, isyms, ismax)) {
5340Sstevel@tonic-gate 						ismax = insert_table(isyms,
5350Sstevel@tonic-gate 				ismax, p->cc_ns, p->cc_cs, p->cc_ne, p->cc_ce);
5360Sstevel@tonic-gate 					}
5370Sstevel@tonic-gate 					ssyms[ssmax++] = *p;
5380Sstevel@tonic-gate 				}
5390Sstevel@tonic-gate 				break;
5400Sstevel@tonic-gate 			case NCCL:
5410Sstevel@tonic-gate 				ns = 0;
5420Sstevel@tonic-gate 				cs = WC_VERY_SMALL;
5430Sstevel@tonic-gate 				for (p = (ccl_chars_t *)right(cp);
5440Sstevel@tonic-gate 					p->cc_cs; p++) {
5450Sstevel@tonic-gate 					if ((ns != hatcn || p->cc_cs != HAT) &&
5460Sstevel@tonic-gate 						! ccln_member(ns, cs,
5470Sstevel@tonic-gate 							p->cc_ns, p->cc_cs-1,
5480Sstevel@tonic-gate 								isyms, ismax)) {
5490Sstevel@tonic-gate 						ismax = insert_table(isyms,
5500Sstevel@tonic-gate 								ismax,
5510Sstevel@tonic-gate 								ns, cs,
5520Sstevel@tonic-gate 								p->cc_ns,
5530Sstevel@tonic-gate 								p->cc_cs-1);
5540Sstevel@tonic-gate 					}
5550Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
5560Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
5570Sstevel@tonic-gate 					ssyms[ssmax].cc_ne = p->cc_ns;
5580Sstevel@tonic-gate 					ssyms[ssmax++].cc_ce = p->cc_cs-1;
5590Sstevel@tonic-gate 					if (p->cc_ce == (wchar_t)0x0) {
5600Sstevel@tonic-gate 						ns = p->cc_ns;
5610Sstevel@tonic-gate 						cs = p->cc_cs + 1;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 					} else {
5640Sstevel@tonic-gate 						ns = p->cc_ne;
5650Sstevel@tonic-gate 						cs = p->cc_ce + 1;
5660Sstevel@tonic-gate 					}
5670Sstevel@tonic-gate 				}
5680Sstevel@tonic-gate 				if ((ns != hatcn || cs != HAT) &&
5690Sstevel@tonic-gate 					! ccln_member(ns, cs,
5700Sstevel@tonic-gate 						MAX_CODESET, WC_VERY_LARGE,
5710Sstevel@tonic-gate 							isyms, ismax)) {
5720Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
5730Sstevel@tonic-gate 							ns, cs, MAX_CODESET,
5740Sstevel@tonic-gate 							WC_VERY_LARGE);
5750Sstevel@tonic-gate 				}
5760Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
5770Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = cs;
5780Sstevel@tonic-gate 				ssyms[ssmax].cc_ne = MAX_CODESET;
5790Sstevel@tonic-gate 				ssyms[ssmax++].cc_ce = WC_VERY_LARGE;
5800Sstevel@tonic-gate 				break;
5810Sstevel@tonic-gate 		}
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	ssinit = ssmax;
5840Sstevel@tonic-gate 	symax = 0;
5850Sstevel@tonic-gate 	n = 0;
5860Sstevel@tonic-gate 	for (s = 0; s <= n; s++)  {
5870Sstevel@tonic-gate 		dprintf("s = %d\n", s, NULL, NULL);
5880Sstevel@tonic-gate 		ind = 0;
5890Sstevel@tonic-gate 		numtrans = 0;
5900Sstevel@tonic-gate 		finflg = 0;
5910Sstevel@tonic-gate 		if (*(state[s] + *state[s]) == line) {		/* s final? */
5920Sstevel@tonic-gate 			finflg = 1;
5930Sstevel@tonic-gate 			goto tenter;
5940Sstevel@tonic-gate 		}
5950Sstevel@tonic-gate 		spmax = spinit;
5960Sstevel@tonic-gate 		ssmax = ssinit;
5970Sstevel@tonic-gate 		ptr = state[s];
5980Sstevel@tonic-gate 		num = *ptr;
5990Sstevel@tonic-gate 		for (i = 0; i < num; i++) {
6000Sstevel@tonic-gate 			curpos = *(++ptr);
6010Sstevel@tonic-gate 			if (iposns[curpos] != 1 && index[curpos] != 1) {
6020Sstevel@tonic-gate 				index[curpos] = 1;
6030Sstevel@tonic-gate 				sposns[spmax++] = curpos;
6040Sstevel@tonic-gate 			}
6050Sstevel@tonic-gate 			cp = point[curpos];
6060Sstevel@tonic-gate 			switch (type(cp)) {
6070Sstevel@tonic-gate 				case CHAR:
6080Sstevel@tonic-gate 					k = (int)right(cp);
6090Sstevel@tonic-gate 					ns = wcsetno(k);
6100Sstevel@tonic-gate 					if (! ccln_member(ns, k, ns, k,
6110Sstevel@tonic-gate 							isyms, ismax) &&
6120Sstevel@tonic-gate 						! ccln_member(ns, k, ns, k,
6130Sstevel@tonic-gate 							symbol, symax)) {
6140Sstevel@tonic-gate 						symax = insert_table(symbol,
6150Sstevel@tonic-gate 									symax,
6160Sstevel@tonic-gate 									ns, k,
6170Sstevel@tonic-gate 									ns, k);
6180Sstevel@tonic-gate 					}
6190Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
6200Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = k;
6210Sstevel@tonic-gate 					ssyms[ssmax].cc_ne = ns;
6220Sstevel@tonic-gate 					ssyms[ssmax++].cc_ce = k;
6230Sstevel@tonic-gate 					break;
6240Sstevel@tonic-gate 				case DOT:
6250Sstevel@tonic-gate 					cs = WC_VERY_SMALL;
6260Sstevel@tonic-gate 					ns = 0;
6270Sstevel@tonic-gate 					ce = HAT - 1;
6280Sstevel@tonic-gate 					ne = hatcn;
6290Sstevel@tonic-gate 					if (! ccln_member(ns, cs, ne, ce,
6300Sstevel@tonic-gate 							isyms, ismax) &&
6310Sstevel@tonic-gate 						! ccln_member(ns, cs, ne, ce,
6320Sstevel@tonic-gate 							symbol, symax)) {
6330Sstevel@tonic-gate 						symax = insert_table(symbol,
6340Sstevel@tonic-gate 									symax,
6350Sstevel@tonic-gate 									ns, cs,
6360Sstevel@tonic-gate 									ne, ce);
6370Sstevel@tonic-gate 					}
6380Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
6390Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
6400Sstevel@tonic-gate 					ssyms[ssmax].cc_ce = ce;
6410Sstevel@tonic-gate 					ssyms[ssmax++].cc_ne = ne;
6420Sstevel@tonic-gate 					cs = HAT + 1;
6430Sstevel@tonic-gate 					ns = hatcn;
6440Sstevel@tonic-gate 					ce = WC_VERY_LARGE;
6450Sstevel@tonic-gate 					ne = MAX_CODESET;
6460Sstevel@tonic-gate 					if (! ccln_member(ns, cs, ne, ce,
6470Sstevel@tonic-gate 								isyms, ismax) &&
6480Sstevel@tonic-gate 						! ccln_member(ns, cs, ne, ce,
6490Sstevel@tonic-gate 							symbol, symax)) {
6500Sstevel@tonic-gate 						symax = insert_table(symbol,
6510Sstevel@tonic-gate 									symax,
6520Sstevel@tonic-gate 									ns, cs,
6530Sstevel@tonic-gate 									ne, ce);
6540Sstevel@tonic-gate 					}
6550Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
6560Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
6570Sstevel@tonic-gate 					ssyms[ssmax].cc_ce = ce;
6580Sstevel@tonic-gate 					ssyms[ssmax++].cc_ne = ne;
6590Sstevel@tonic-gate 					break;
6600Sstevel@tonic-gate 				case CCL:
6610Sstevel@tonic-gate 					cs = HAT;
6620Sstevel@tonic-gate 					ns = hatcn;
6630Sstevel@tonic-gate 					for (p = (ccl_chars_t *)right(cp);
6640Sstevel@tonic-gate 						p->cc_cs; p++) {
6650Sstevel@tonic-gate 						if ((p->cc_ns != ns ||
6660Sstevel@tonic-gate 							p->cc_cs != cs) &&
6670Sstevel@tonic-gate 							! ccln_member(p->cc_ns,
6680Sstevel@tonic-gate 							p->cc_cs, p->cc_ne,
6690Sstevel@tonic-gate 						p->cc_ce, isyms, ismax) &&
6700Sstevel@tonic-gate 						!ccln_member(p->cc_ns, p->cc_cs,
6710Sstevel@tonic-gate 						p->cc_ne, p->cc_ce, symbol,
6720Sstevel@tonic-gate 						symax)) {
6730Sstevel@tonic-gate 							symax = insert_table(
6740Sstevel@tonic-gate 						symbol, symax, p->cc_ns,
6750Sstevel@tonic-gate 						p->cc_cs, p->cc_ne, p->cc_ce);
6760Sstevel@tonic-gate 						}
6770Sstevel@tonic-gate 						ssyms[ssmax++] = *p;
6780Sstevel@tonic-gate 					}
6790Sstevel@tonic-gate 					break;
6800Sstevel@tonic-gate 				case NCCL:
6810Sstevel@tonic-gate 					ns = 0;
6820Sstevel@tonic-gate 					cs = WC_VERY_SMALL;
6830Sstevel@tonic-gate 		for (p = (ccl_chars_t *)right(cp); p->cc_cs; p++) {
6840Sstevel@tonic-gate 			if ((p->cc_ns != hatcn || p->cc_cs != HAT) &&
6850Sstevel@tonic-gate 					! ccln_member(ns, cs, p->cc_ns,
6860Sstevel@tonic-gate 					p->cc_cs-1, isyms, ismax) &&
6870Sstevel@tonic-gate 					! ccln_member(ns, cs, p->cc_ns,
6880Sstevel@tonic-gate 					p->cc_cs-1, symbol, symax)) {
6890Sstevel@tonic-gate 				symax = insert_table(symbol,
6900Sstevel@tonic-gate 					symax, ns, cs, p->cc_ns, p->cc_cs-1);
6910Sstevel@tonic-gate 						}
6920Sstevel@tonic-gate 						ssyms[ssmax].cc_ns = ns;
6930Sstevel@tonic-gate 						ssyms[ssmax].cc_cs = cs;
6940Sstevel@tonic-gate 						ssyms[ssmax].cc_ne = p->cc_ns;
6950Sstevel@tonic-gate 						ssyms[ssmax++].cc_ce
6960Sstevel@tonic-gate 								= p->cc_cs-1;
6970Sstevel@tonic-gate 						if (p->cc_ce == (wchar_t)0x0) {
6980Sstevel@tonic-gate 							ns = p->cc_ns;
6990Sstevel@tonic-gate 							cs = p->cc_cs + 1;
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 						} else {
7020Sstevel@tonic-gate 							ns = p->cc_ne;
7030Sstevel@tonic-gate 							cs = p->cc_ce + 1;
7040Sstevel@tonic-gate 						}
7050Sstevel@tonic-gate 					}
7060Sstevel@tonic-gate 		if ((ns != hatcn || cs != HAT) && ! ccln_member(ns, cs,
7070Sstevel@tonic-gate 				MAX_CODESET, WC_VERY_LARGE, isyms, ismax) &&
7080Sstevel@tonic-gate 				! ccln_member(ns, cs, MAX_CODESET,
7090Sstevel@tonic-gate 					WC_VERY_LARGE, symbol, symax)) {
7100Sstevel@tonic-gate 			symax = insert_table(symbol, symax, ns, cs,
7110Sstevel@tonic-gate 								MAX_CODESET,
7120Sstevel@tonic-gate 								WC_VERY_LARGE);
7130Sstevel@tonic-gate 					}
7140Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
7150Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
7160Sstevel@tonic-gate 					ssyms[ssmax].cc_ne = MAX_CODESET;
7170Sstevel@tonic-gate 					ssyms[ssmax++].cc_ce = WC_VERY_LARGE;
7180Sstevel@tonic-gate 					break;
7190Sstevel@tonic-gate 			}
7200Sstevel@tonic-gate 		}
7210Sstevel@tonic-gate 		for (j = 0; j < ssmax; j++) {	/* nextstate(s, ssyms[j]) */
7220Sstevel@tonic-gate 			ns = ssyms[j].cc_ns;
7230Sstevel@tonic-gate 			cs = ssyms[j].cc_cs;
7240Sstevel@tonic-gate 			ne = ssyms[j].cc_ne;
7250Sstevel@tonic-gate 			ce = ssyms[j].cc_ce;
7260Sstevel@tonic-gate dprintf("j = %d, cs = %o, ce = %o\n", j, cs, ce);
7270Sstevel@tonic-gate 			symax = delete_table(symbol, symax, ns, cs, ne, ce);
7280Sstevel@tonic-gate 			setcnt = 0;
7290Sstevel@tonic-gate 			for (k = 0; k <= line; k++) setvec[k] = 0;
7300Sstevel@tonic-gate 			for (i = 0; i < spmax; i++) {
7310Sstevel@tonic-gate 				index[sposns[i]] = 0;
7320Sstevel@tonic-gate 				cp = point[sposns[i]];
7330Sstevel@tonic-gate 				if ((k = type(cp)) != FINAL) {
7340Sstevel@tonic-gate 					if (k == CHAR && ns == ne && cs == ce &&
7350Sstevel@tonic-gate 						cs == (int)right(cp) ||
7360Sstevel@tonic-gate 						k == DOT || k == CCL &&
7370Sstevel@tonic-gate 						ccl_member(ns, cs, ne, ce,
7380Sstevel@tonic-gate 						(ccl_chars_t *)right(cp)) ||
7390Sstevel@tonic-gate 						k == NCCL &&
7400Sstevel@tonic-gate 						!ccl_member(ns, cs, ne, ce,
7410Sstevel@tonic-gate 						(ccl_chars_t *)right(cp))) {
7420Sstevel@tonic-gate 						ptr = foll[sposns[i]];
7430Sstevel@tonic-gate 						num = *ptr;
7440Sstevel@tonic-gate 						for (k = 0; k < num; k++) {
7450Sstevel@tonic-gate 						if (setvec[*(++ptr)] != 1 &&
7460Sstevel@tonic-gate 							iposns[*ptr] != 1) {
7470Sstevel@tonic-gate 							setvec[*ptr] = 1;
7480Sstevel@tonic-gate 								setcnt++;
7490Sstevel@tonic-gate 							}
7500Sstevel@tonic-gate 						}
7510Sstevel@tonic-gate 					}
7520Sstevel@tonic-gate 				}
7530Sstevel@tonic-gate 			} /* end nextstate */
7540Sstevel@tonic-gate 			if (notin(state, n, &prev)) {
7550Sstevel@tonic-gate 				if (n >= NSTATES - 1) {
7560Sstevel@tonic-gate 		printf("cgotofn: notin; state = %d, n = %d\n", state, n, NULL);
7570Sstevel@tonic-gate 					overflo();
7580Sstevel@tonic-gate 				}
7590Sstevel@tonic-gate 				state[++n] = add(setcnt);
7600Sstevel@tonic-gate 				dprintf("	delta(%d,[%o,%o])",
7610Sstevel@tonic-gate 					s, cs, ce);
7620Sstevel@tonic-gate 				dprintf(" = %d, ind = %d\n", n, ind+1, NULL);
7630Sstevel@tonic-gate 				fatab[++ind].cc.cc_ns = ns;
7640Sstevel@tonic-gate 				fatab[ind].cc.cc_cs = cs;
7650Sstevel@tonic-gate 				fatab[ind].cc.cc_ne = ne;
7660Sstevel@tonic-gate 				fatab[ind].cc.cc_ce = ce;
7670Sstevel@tonic-gate 				fatab[ind].n = n;
7680Sstevel@tonic-gate 				numtrans++;
7690Sstevel@tonic-gate 			} else {
7700Sstevel@tonic-gate 				if (prev != 0) {
7710Sstevel@tonic-gate 					dprintf("	delta(%d,[%o,%o])",
7720Sstevel@tonic-gate 						s, cs, ce);
7730Sstevel@tonic-gate 					dprintf("= %d, ind = %d\n",
7740Sstevel@tonic-gate 						prev, ind+1, NULL);
7750Sstevel@tonic-gate 					fatab[++ind].cc.cc_ns = ns;
7760Sstevel@tonic-gate 					fatab[ind].cc.cc_cs = cs;
7770Sstevel@tonic-gate 					fatab[ind].cc.cc_ne = ne;
7780Sstevel@tonic-gate 					fatab[ind].cc.cc_ce = ce;
7790Sstevel@tonic-gate 					fatab[ind].n = prev;
7800Sstevel@tonic-gate 					numtrans++;
7810Sstevel@tonic-gate 				}
7820Sstevel@tonic-gate 			}
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 	tenter:
7850Sstevel@tonic-gate 		if ((pfa = (struct fa *)malloc((numtrans + 1)
7860Sstevel@tonic-gate 						* sizeof (struct fa))) == NULL)
7870Sstevel@tonic-gate 			overflo();
7880Sstevel@tonic-gate 		where[s] = pfa;
7890Sstevel@tonic-gate 		if (finflg)
7900Sstevel@tonic-gate 			pfa->cc.h = -1;		/* s is a final state */
7910Sstevel@tonic-gate 		else
7920Sstevel@tonic-gate 			pfa->cc.h = numtrans;
7930Sstevel@tonic-gate 		pfa->st = 0;
7940Sstevel@tonic-gate 		for (i = 1, pfa += 1; i <= numtrans; i++, pfa++) {
7950Sstevel@tonic-gate 			pfa->cc.s = fatab[i].cc;
7960Sstevel@tonic-gate 			pfa->st = (struct fa *)fatab[i].n;
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 	for (i = 0; i <= n; i++) {
8000Sstevel@tonic-gate 		if (i != 0)	/* state[0] is freed later in freetr() */
8010Sstevel@tonic-gate 			xfree(state[i]);	/* free state[i] */
8020Sstevel@tonic-gate 		pfa = where[i];
8030Sstevel@tonic-gate 		pfa->st = where[0];
8040Sstevel@tonic-gate 		dprintf("state %d: (%o)\n", i, pfa, NULL);
8050Sstevel@tonic-gate 		dprintf("	numtrans = %d,	default = %o\n",
8060Sstevel@tonic-gate 			pfa->cc.h, pfa->st, NULL);
8070Sstevel@tonic-gate 		for (k = 1; k <= pfa->cc.h; k++) {
8080Sstevel@tonic-gate 			(pfa+k)->st = where[(int)(pfa+k)->st];
8090Sstevel@tonic-gate 			dprintf("	char = [%o,%o],	nextstate = %o\n",
8100Sstevel@tonic-gate 				(pfa+k)->cc.s.cc_cs, (pfa+k)->cc.s.cc_ce,
8110Sstevel@tonic-gate 				(pfa+k)->st);
8120Sstevel@tonic-gate 		}
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 	pfa = where[0];
8150Sstevel@tonic-gate 	if ((num = pfa->cc.h) < 0)
8160Sstevel@tonic-gate 		return (where[0]);
8170Sstevel@tonic-gate 	for (pfa += num; num; num--, pfa--)
8180Sstevel@tonic-gate 		if (pfa->cc.s.cc_ns == hatcn && pfa->cc.s.cc_cs == HAT) {
8190Sstevel@tonic-gate 			return (pfa->st);
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 	return (where[0]);
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate  * Insert CCL entry to CCL table with maintain optimized order.
8270Sstevel@tonic-gate  */
8280Sstevel@tonic-gate static int
insert_table(ccl_chars_t * table_base,int table_size,int ns,wchar_t cs,int ne,wchar_t ce)829731Srobbin insert_table(ccl_chars_t *table_base, int table_size, int ns, wchar_t cs,
830731Srobbin 	int ne, wchar_t ce)
8310Sstevel@tonic-gate {
832731Srobbin 	int		i;
833731Srobbin 	int		tns, tne;
834731Srobbin 	wchar_t		tcs, tce;
835731Srobbin 	ccl_chars_t	*table;
836731Srobbin 	ccl_chars_t	*saved_table;
837731Srobbin 	int		saved_i;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	dprintf("Inserting {%o, %o} to table %o\n", cs, ce, table_base);
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * Searching the table to find out where should put the new item.
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	for (i = 0, table = table_base; i < table_size; i++, table++) {
8470Sstevel@tonic-gate 		tns = table->cc_ns;
8480Sstevel@tonic-gate 		tcs = table->cc_cs;
8490Sstevel@tonic-gate 		tne = table->cc_ne;
8500Sstevel@tonic-gate 		tce = table->cc_ce;
8510Sstevel@tonic-gate 		if (MLCMPLT(ne, ce, tns, (tcs - 1))) {
8520Sstevel@tonic-gate 			/*
8530Sstevel@tonic-gate 			 * Quick! insert to font of current table entries.
8540Sstevel@tonic-gate 			 */
8550Sstevel@tonic-gate qinsert:
8560Sstevel@tonic-gate 			table_size++;
8570Sstevel@tonic-gate 			for (; i < table_size; i++, table++) {
8580Sstevel@tonic-gate 				tns = table->cc_ns;
8590Sstevel@tonic-gate 				tcs = table->cc_cs;
8600Sstevel@tonic-gate 				tne = table->cc_ne;
8610Sstevel@tonic-gate 				tce = table->cc_ce;
8620Sstevel@tonic-gate 				table->cc_ns = ns;
8630Sstevel@tonic-gate 				table->cc_cs = cs;
8640Sstevel@tonic-gate 				table->cc_ne = ne;
8650Sstevel@tonic-gate 				table->cc_ce = ce;
8660Sstevel@tonic-gate 				ns = tns;
8670Sstevel@tonic-gate 				cs = tcs;
8680Sstevel@tonic-gate 				ne = tne;
8690Sstevel@tonic-gate 				ce = tce;
8700Sstevel@tonic-gate 			}
8710Sstevel@tonic-gate 			goto add_null;
8720Sstevel@tonic-gate 		} else if (MLCMPLE(tns, (tcs - 1), ns, cs) &&
8730Sstevel@tonic-gate 				MLCMPLE(ns, cs, tne, (tce + 1))) {
8740Sstevel@tonic-gate 			/*
8750Sstevel@tonic-gate 			 * Starting point is within the current entry.
8760Sstevel@tonic-gate 			 */
8770Sstevel@tonic-gate 			if (MLCMPGT(tns, tcs, ns, cs)) {
8780Sstevel@tonic-gate 				table->cc_ns = ns;
8790Sstevel@tonic-gate 				table->cc_cs = cs;
8800Sstevel@tonic-gate 			}
8810Sstevel@tonic-gate 			if (MLCMPLE(ne, ce, tne, tce)) {
8820Sstevel@tonic-gate 				return (table_size);
8830Sstevel@tonic-gate 			}
8840Sstevel@tonic-gate 			goto combine;
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/*
8900Sstevel@tonic-gate 	 * Adding new one to end of table.
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	table->cc_ns = ns;
8930Sstevel@tonic-gate 	table->cc_cs = cs;
8940Sstevel@tonic-gate 	table->cc_ne = ne;
8950Sstevel@tonic-gate 	table->cc_ce = ce;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	table_size++;
8990Sstevel@tonic-gate 	goto add_null;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	combine:
9050Sstevel@tonic-gate 	/*
9060Sstevel@tonic-gate 	 * Check and try to combine the new entry with rest of entries.
9070Sstevel@tonic-gate 	 */
9080Sstevel@tonic-gate 	if ((i + 1) >= table_size) {
9090Sstevel@tonic-gate 		table->cc_ne = ne;
9100Sstevel@tonic-gate 		table->cc_ce = ce;
9110Sstevel@tonic-gate 		return (table_size);
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	saved_table = table++;
9160Sstevel@tonic-gate 	saved_i = i++;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/*
9200Sstevel@tonic-gate 	 * Finding the spot where we should put the end point.
9210Sstevel@tonic-gate 	 */
9220Sstevel@tonic-gate 	for (; i < table_size; i++, table++) {
9230Sstevel@tonic-gate 		if (MLCMPLT(ne, ce, table->cc_ns, (table->cc_cs - 1))) {
9240Sstevel@tonic-gate 			break;
9250Sstevel@tonic-gate 		} else
9260Sstevel@tonic-gate 		if (MLCMPLE(table->cc_ns, (table->cc_cs - 1), ne, ce) &&
9270Sstevel@tonic-gate 			MLCMPLE(ne, ce, table->cc_ne, (table->cc_ce + 1))) {
9280Sstevel@tonic-gate 			/*
9290Sstevel@tonic-gate 			 * Tack with this table.
9300Sstevel@tonic-gate 			 */
9310Sstevel@tonic-gate 			if (MLCMPLT(ne, ce, table->cc_ne, table->cc_ce)) {
9320Sstevel@tonic-gate 				ne = table->cc_ne;
9330Sstevel@tonic-gate 				ce = table->cc_ce;
9340Sstevel@tonic-gate 			}
9350Sstevel@tonic-gate 			table++;
9360Sstevel@tonic-gate 			i++;
9370Sstevel@tonic-gate 			break;
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	saved_table->cc_ne = ne;
9430Sstevel@tonic-gate 	saved_table->cc_ce = ce;
9440Sstevel@tonic-gate 	saved_i = table_size - (i - saved_i - 1);
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/*
9480Sstevel@tonic-gate 	 * Moving the rest of entries.
9490Sstevel@tonic-gate 	 */
9500Sstevel@tonic-gate 	for (; i < table_size; i++, table++)
9510Sstevel@tonic-gate 		*(++saved_table) = *table;
9520Sstevel@tonic-gate 	table_size = saved_i;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate add_null:
9560Sstevel@tonic-gate 	table_base[table_size].cc_cs = (wchar_t)0x0;
9570Sstevel@tonic-gate 	table_base[table_size].cc_ce = (wchar_t)0x0;
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	return (table_size);
9610Sstevel@tonic-gate }
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate static int
delete_table(ccl_chars_t * table_base,int table_size,int ns,wchar_t cs,int ne,wchar_t ce)967731Srobbin delete_table(ccl_chars_t *table_base, int table_size, int ns, wchar_t cs,
968731Srobbin 		int ne, wchar_t ce)
9690Sstevel@tonic-gate {
970731Srobbin 	int		i;
971731Srobbin 	int		saved_i;
972731Srobbin 	ccl_chars_t	*table;
973731Srobbin 	ccl_chars_t	*saved_table;
974731Srobbin 	int		tns;
975731Srobbin 	wchar_t		tcs;
976731Srobbin 	int		tne;
977731Srobbin 	wchar_t		tce;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	for (i = 0, table = table_base; i < table_size; i++, table++) {
9830Sstevel@tonic-gate 		tns = table->cc_ns;
9840Sstevel@tonic-gate 		tcs = table->cc_cs;
9850Sstevel@tonic-gate 		tne = table->cc_ne;
9860Sstevel@tonic-gate 		tce = table->cc_ce;
9870Sstevel@tonic-gate 		if (MLCMPLT(ne, ce, tns, tcs))
9880Sstevel@tonic-gate 			return (table_size);
9890Sstevel@tonic-gate 		else if (MLCMPLT(ne, ce, tne, tce)) {
9900Sstevel@tonic-gate 			if (MLCMPLE(ns, cs, tns, tcs)) {
9910Sstevel@tonic-gate 				/*
9920Sstevel@tonic-gate 				 * Shrink type 1.
9930Sstevel@tonic-gate 				 */
9940Sstevel@tonic-gate 				table->cc_ns = ne;
9950Sstevel@tonic-gate 				table->cc_cs = ce + 1;
9960Sstevel@tonic-gate 				return (table_size);
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 			} else {
9990Sstevel@tonic-gate 				/*
10000Sstevel@tonic-gate 				 * Spliting !!
10010Sstevel@tonic-gate 				 */
10020Sstevel@tonic-gate 				table->cc_ns = ne;
10030Sstevel@tonic-gate 				table->cc_cs = ce + 1;
10040Sstevel@tonic-gate 				tne = ns;
10050Sstevel@tonic-gate 				tce = cs - 1;
10060Sstevel@tonic-gate 				table_size++;
10070Sstevel@tonic-gate 				for (; i < table_size; i++, table++) {
10080Sstevel@tonic-gate 					ns = table->cc_ns;
10090Sstevel@tonic-gate 					cs = table->cc_cs;
10100Sstevel@tonic-gate 					ne = table->cc_ne;
10110Sstevel@tonic-gate 					ce = table->cc_ce;
10120Sstevel@tonic-gate 					table->cc_ns = tns;
10130Sstevel@tonic-gate 					table->cc_cs = tcs;
10140Sstevel@tonic-gate 					table->cc_ne = tne;
10150Sstevel@tonic-gate 					table->cc_ce = tce;
10160Sstevel@tonic-gate 					tns = ns;
10170Sstevel@tonic-gate 					tcs = cs;
10180Sstevel@tonic-gate 					tne = ne;
10190Sstevel@tonic-gate 					tce = ce;
10200Sstevel@tonic-gate 				}
10210Sstevel@tonic-gate 				return (table_size);
10220Sstevel@tonic-gate 			}
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate 		} else if (MLCMPLE(ns, cs, tne, tce)) {
10250Sstevel@tonic-gate 			if (MLCMPGT(ns, cs, tns, tcs)) {
10260Sstevel@tonic-gate 				/*
10270Sstevel@tonic-gate 				 * Shrink current table(type 2).
10280Sstevel@tonic-gate 				 */
10290Sstevel@tonic-gate 				table->cc_ne = ns;
10300Sstevel@tonic-gate 				table->cc_ce = cs - 1;
10310Sstevel@tonic-gate 				table++;
10320Sstevel@tonic-gate 				i++;
10330Sstevel@tonic-gate 			}
10340Sstevel@tonic-gate 			/*
10350Sstevel@tonic-gate 			 * Search for the end point.
10360Sstevel@tonic-gate 			 */
10370Sstevel@tonic-gate 			saved_i = i;
10380Sstevel@tonic-gate 			saved_table = table;
10390Sstevel@tonic-gate 			for (; i < table_size; i++, table++) {
10400Sstevel@tonic-gate 				if (MLCMPLT(ne, ce,
10410Sstevel@tonic-gate 						table->cc_ns, table->cc_cs)) {
10420Sstevel@tonic-gate 					/*
10430Sstevel@tonic-gate 					 * Easy point, no shrinks!
10440Sstevel@tonic-gate 					 */
10450Sstevel@tonic-gate 					break;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 				} else if (MLCMPGT(table->cc_ne, table->cc_ce,
10480Sstevel@tonic-gate 						ne, ce)) {
10490Sstevel@tonic-gate 					/*
10500Sstevel@tonic-gate 					 * Shrinking...
10510Sstevel@tonic-gate 					 */
10520Sstevel@tonic-gate 					table->cc_ns = ne;
10530Sstevel@tonic-gate 					table->cc_cs = ce + 1;
10540Sstevel@tonic-gate 					break;
10550Sstevel@tonic-gate 				}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 			}
10590Sstevel@tonic-gate 			/*
10600Sstevel@tonic-gate 			 * Moving(removing) backword.
10610Sstevel@tonic-gate 			 */
10620Sstevel@tonic-gate 			saved_i = table_size - (i - saved_i);
10630Sstevel@tonic-gate 			for (; i < table_size; i++)
10640Sstevel@tonic-gate 				*saved_table++ = *table++;
10650Sstevel@tonic-gate 			return (saved_i);
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 	return (table_size);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 
10720Sstevel@tonic-gate #ifdef DEBUG
dump_table(ccl_chars_t * table,int size)1073731Srobbin dump_table(ccl_chars_t *table, int size)
10740Sstevel@tonic-gate {
1075731Srobbin 	int	i;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (! dbg)
10810Sstevel@tonic-gate 		return;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	printf("Duming table %o with size %d\n", table, size);
10850Sstevel@tonic-gate 	size++;	/* To watch out NULL */
1086*732Srobbin 	for (i = 0; i < size; i++, table++) {
10870Sstevel@tonic-gate 		printf("{%3o, %3o}, ", table->cc_cs, table->cc_ce);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	printf("\n");
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate #endif /* DEBUG */
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 
1095731Srobbin int
match(struct fa * pfa,wchar_t * p)1096731Srobbin match(struct fa *pfa, wchar_t *p)
10970Sstevel@tonic-gate {
1098731Srobbin 	int count;
1099731Srobbin 	int n, ns, ne;
1100731Srobbin 	wchar_t c, cs, ce;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	if (p == 0)
11040Sstevel@tonic-gate 		return (0);
11050Sstevel@tonic-gate 	if (pfa->cc.h == 1) { /* fast test for first character, if possible */
11060Sstevel@tonic-gate 		ns = (++pfa)->cc.s.cc_ns;
11070Sstevel@tonic-gate 		cs = (pfa)->cc.s.cc_cs;
11080Sstevel@tonic-gate 		ne = (pfa)->cc.s.cc_ne;
11090Sstevel@tonic-gate 		ce = (pfa)->cc.s.cc_ce;
11100Sstevel@tonic-gate 		do {
11110Sstevel@tonic-gate 			c = *p;
11120Sstevel@tonic-gate 			n = wcsetno(c);
11130Sstevel@tonic-gate 			if (MLCMPLE(ns, cs, n, c) &&
11140Sstevel@tonic-gate 				MLCMPLE(n, c, ne, ce)) {
11150Sstevel@tonic-gate 				p++;
11160Sstevel@tonic-gate 				pfa = pfa->st;
11170Sstevel@tonic-gate 				goto adv;
11180Sstevel@tonic-gate 			}
11190Sstevel@tonic-gate 		} while (*p++ != 0);
11200Sstevel@tonic-gate 		return (0);
11210Sstevel@tonic-gate 	}
11220Sstevel@tonic-gate 	adv: if ((count = pfa->cc.h) < 0)
11230Sstevel@tonic-gate 		return (1);
11240Sstevel@tonic-gate 	do {
11250Sstevel@tonic-gate 		c = *p;
11260Sstevel@tonic-gate 		n = wcsetno(c);
11270Sstevel@tonic-gate 		for (pfa += count; count; count--, pfa--) {
11280Sstevel@tonic-gate 			ns = (pfa)->cc.s.cc_ns;
11290Sstevel@tonic-gate 			cs = (pfa)->cc.s.cc_cs;
11300Sstevel@tonic-gate 			ne = (pfa)->cc.s.cc_ne;
11310Sstevel@tonic-gate 			ce = (pfa)->cc.s.cc_ce;
11320Sstevel@tonic-gate 			if (MLCMPLE(ns, cs, n, c) && MLCMPLE(n, c, ne, ce))
11330Sstevel@tonic-gate 				break;
11340Sstevel@tonic-gate 		}
11350Sstevel@tonic-gate 		pfa = pfa->st;
11360Sstevel@tonic-gate 		if ((count = pfa->cc.h) < 0)
11370Sstevel@tonic-gate 			return (1);
11380Sstevel@tonic-gate 	} while (*p++ != 0);
11390Sstevel@tonic-gate 	return (0);
11400Sstevel@tonic-gate }
1141