xref: /titanic_41/usr/src/cmd/oawk/b.c (revision 45effca3dd1ba645d6bd3955c8ecd0c7bb85298a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28dc5a8425Srobbin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29dc5a8425Srobbin /*	  All Rights Reserved  	*/
30dc5a8425Srobbin 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "awk.def"
347c478bd9Sstevel@tonic-gate #include "stdio.h"
357c478bd9Sstevel@tonic-gate #include "awk.h"
36dc5a8425Srobbin #include <stdlib.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate extern NODE *op2();
407c478bd9Sstevel@tonic-gate extern struct fa *cgotofn();
417c478bd9Sstevel@tonic-gate #define	MAXLIN 256
427c478bd9Sstevel@tonic-gate #define	NCHARS 128
437c478bd9Sstevel@tonic-gate #define	NSTATES 256
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #define	type(v)	v->nobj
477c478bd9Sstevel@tonic-gate #define	left(v)	v->narg[0]
487c478bd9Sstevel@tonic-gate #define	right(v)	v->narg[1]
497c478bd9Sstevel@tonic-gate #define	parent(v)	v->nnext
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	LEAF	case CCL: case NCCL: case CHAR: case DOT:
537c478bd9Sstevel@tonic-gate #define	UNARY	case FINAL: case STAR: case PLUS: case QUEST:
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /*
577c478bd9Sstevel@tonic-gate  * encoding in tree NODEs:
587c478bd9Sstevel@tonic-gate  * leaf (CCL, NCCL, CHAR, DOT): left is index,
597c478bd9Sstevel@tonic-gate  * right contains value or pointer to value
607c478bd9Sstevel@tonic-gate  * unary (FINAL, STAR, PLUS, QUEST): left is child, right is null
617c478bd9Sstevel@tonic-gate  * binary (CAT, OR): left and right are children
627c478bd9Sstevel@tonic-gate  * parent contains pointer to parent
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate struct fa {
677c478bd9Sstevel@tonic-gate union {
687c478bd9Sstevel@tonic-gate 		ccl_chars_t s;
697c478bd9Sstevel@tonic-gate 		int h;
707c478bd9Sstevel@tonic-gate 	} cc;
717c478bd9Sstevel@tonic-gate #define	MLCMPLT(m1, l1, m2, l2) ((m1 != m2 &&\
727c478bd9Sstevel@tonic-gate 				(int)m1 < (int)m2) ||\
737c478bd9Sstevel@tonic-gate 				(m1 == m2 && (int)l1 < (int)l2))
747c478bd9Sstevel@tonic-gate #define	MLCMPLE(m1, l1, m2, l2) ((m1 != m2 &&\
757c478bd9Sstevel@tonic-gate 				(int)m1 <= (int)m2) ||\
767c478bd9Sstevel@tonic-gate 				(m1 == m2 && (int)l1 <= (int)l2))
777c478bd9Sstevel@tonic-gate #define	MLCMPGT(m1, l1, m2, l2) ((m1 != m2 &&\
787c478bd9Sstevel@tonic-gate 				(int)m1 > (int)m2) ||\
797c478bd9Sstevel@tonic-gate 				(m1 == m2 && (int)l1 > (int)l2))
807c478bd9Sstevel@tonic-gate #define	MAX_CODESET	3
817c478bd9Sstevel@tonic-gate 	struct fa *st;
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate int	*state[NSTATES];
867c478bd9Sstevel@tonic-gate int	*foll[MAXLIN];
877c478bd9Sstevel@tonic-gate int	setvec[MAXLIN];
887c478bd9Sstevel@tonic-gate NODE	*point[MAXLIN];
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate int	setcnt;
927c478bd9Sstevel@tonic-gate int	line;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static int	ccln_member();
967c478bd9Sstevel@tonic-gate static int	insert_table();
977c478bd9Sstevel@tonic-gate static int	delete_table();
98dc5a8425Srobbin static void	penter(NODE *p);
99dc5a8425Srobbin static void	follow(NODE *v);
100dc5a8425Srobbin static void	overflo(void);
101dc5a8425Srobbin static void	cfoll(NODE *v);
102dc5a8425Srobbin static void	freetr(NODE *p);
1037c478bd9Sstevel@tonic-gate #ifdef DEBUG
1047c478bd9Sstevel@tonic-gate #define	ddump_table(t, s)	dump_table(t, s)
1057c478bd9Sstevel@tonic-gate #else
1067c478bd9Sstevel@tonic-gate #define	ddump_table(t, s)
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate struct fa *
makedfa(p)1107c478bd9Sstevel@tonic-gate makedfa(p)	/* returns dfa for tree pointed to by p */
1117c478bd9Sstevel@tonic-gate NODE *p;
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	NODE *p1;
1147c478bd9Sstevel@tonic-gate 	struct fa *fap;
1157c478bd9Sstevel@tonic-gate 	p1 = op2(CAT, op2(STAR, op2(DOT, (NODE *) 0,
1167c478bd9Sstevel@tonic-gate 		(NODE *) 0), (NODE *) 0), p);
1177c478bd9Sstevel@tonic-gate 		/* put DOT STAR in front of reg. exp. */
1187c478bd9Sstevel@tonic-gate 	p1 = op2(FINAL, p1, (NODE *) 0);	/* install FINAL NODE */
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	line = 0;
1227c478bd9Sstevel@tonic-gate 	penter(p1);	/* enter parent pointers and leaf indices */
1237c478bd9Sstevel@tonic-gate 	point[line] = p1;	/* FINAL NODE */
1247c478bd9Sstevel@tonic-gate 	setvec[0] = 1;		/* for initial DOT STAR */
1257c478bd9Sstevel@tonic-gate 	cfoll(p1);	/* set up follow sets */
1267c478bd9Sstevel@tonic-gate 	fap = cgotofn();
1277c478bd9Sstevel@tonic-gate 	freetr(p1);	/* add this when alloc works */
1287c478bd9Sstevel@tonic-gate 	return (fap);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
131dc5a8425Srobbin static void
penter(NODE * p)132dc5a8425Srobbin penter(NODE *p)	/* set up parent pointers and leaf indices */
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	switch (type(p)) {
1357c478bd9Sstevel@tonic-gate 		LEAF
1367c478bd9Sstevel@tonic-gate 			left(p) = (NODE *)line;
1377c478bd9Sstevel@tonic-gate 			point[line++] = p;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		UNARY
1407c478bd9Sstevel@tonic-gate 			penter(left(p));
1417c478bd9Sstevel@tonic-gate 			parent(left(p)) = p;
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		case CAT:
1447c478bd9Sstevel@tonic-gate 		case OR:
1457c478bd9Sstevel@tonic-gate 			penter(left(p));
1467c478bd9Sstevel@tonic-gate 			penter(right(p));
1477c478bd9Sstevel@tonic-gate 			parent(left(p)) = p;
1487c478bd9Sstevel@tonic-gate 			parent(right(p)) = p;
1497c478bd9Sstevel@tonic-gate 			break;
1507c478bd9Sstevel@tonic-gate 		default:
1517c478bd9Sstevel@tonic-gate 			error(FATAL, "unknown type %d in penter\n", type(p));
1527c478bd9Sstevel@tonic-gate 			break;
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
156dc5a8425Srobbin static void
freetr(NODE * p)157dc5a8425Srobbin freetr(NODE *p)	/* free parse tree and follow sets */
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	switch (type(p)) {
1607c478bd9Sstevel@tonic-gate 		LEAF
1617c478bd9Sstevel@tonic-gate 			xfree(foll[(int)left(p)]);
1627c478bd9Sstevel@tonic-gate 			xfree(p);
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		UNARY
1657c478bd9Sstevel@tonic-gate 			freetr(left(p));
1667c478bd9Sstevel@tonic-gate 			xfree(p);
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		case CAT:
1697c478bd9Sstevel@tonic-gate 		case OR:
1707c478bd9Sstevel@tonic-gate 			freetr(left(p));
1717c478bd9Sstevel@tonic-gate 			freetr(right(p));
1727c478bd9Sstevel@tonic-gate 			xfree(p);
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		default:
1757c478bd9Sstevel@tonic-gate 			error(FATAL, "unknown type %d in freetr", type(p));
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate ccl_chars_t *
cclenter(wchar_t * p)180dc5a8425Srobbin cclenter(wchar_t *p)
1817c478bd9Sstevel@tonic-gate {
182dc5a8425Srobbin 	int 		i, cn;
183dc5a8425Srobbin 	wchar_t		c, pc;
184dc5a8425Srobbin 	wchar_t		*op;
185dc5a8425Srobbin 	ccl_chars_t	*new;
1867c478bd9Sstevel@tonic-gate 	ccl_chars_t	chars[MAXLIN];
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	op = p;
1897c478bd9Sstevel@tonic-gate 	i = 0;
1907c478bd9Sstevel@tonic-gate 	while ((c = *p++) != 0) {
1917c478bd9Sstevel@tonic-gate 		if (c == '-' && i > 0)  {
1927c478bd9Sstevel@tonic-gate 			if (*p != 0) {
1937c478bd9Sstevel@tonic-gate 				/*
1947c478bd9Sstevel@tonic-gate 				 * If there are not in same code set,  the
1957c478bd9Sstevel@tonic-gate 				 * class should be ignore (make two independent
1967c478bd9Sstevel@tonic-gate 				 * characters)!
1977c478bd9Sstevel@tonic-gate 				 */
1987c478bd9Sstevel@tonic-gate 				c = *p++;
1997c478bd9Sstevel@tonic-gate 				cn = wcsetno(pc);
2007c478bd9Sstevel@tonic-gate 				if (cn != wcsetno(c) || pc > c)
2017c478bd9Sstevel@tonic-gate 					goto char_array;
2027c478bd9Sstevel@tonic-gate 				i = insert_table(chars, i, cn, pc, cn, c);
2037c478bd9Sstevel@tonic-gate 				continue;
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate char_array:
2077c478bd9Sstevel@tonic-gate 		if (i >= MAXLIN)
2087c478bd9Sstevel@tonic-gate 			overflo();
2097c478bd9Sstevel@tonic-gate 		cn = wcsetno(c);
2107c478bd9Sstevel@tonic-gate 		i = insert_table(chars, i, cn, c, cn, c);
2117c478bd9Sstevel@tonic-gate 		pc = c;
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 	dprintf("cclenter: in = |%ws|, ", op, NULL, NULL);
2147c478bd9Sstevel@tonic-gate 	xfree(op);
2157c478bd9Sstevel@tonic-gate 	i = (i + 1) * sizeof (ccl_chars_t);
2167c478bd9Sstevel@tonic-gate 	if ((new = (ccl_chars_t *)malloc(i)) == NULL)
2177c478bd9Sstevel@tonic-gate 		error(FATAL, "out of space in cclenter on %s", op);
2187c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)new, (char *)chars, i);
2197c478bd9Sstevel@tonic-gate 	ddump_table(chars, i / 4);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	return (new);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
225dc5a8425Srobbin static void
overflo(void)226dc5a8425Srobbin overflo(void)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	error(FATAL, "regular expression too long\n");
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
231dc5a8425Srobbin static void
cfoll(NODE * v)232dc5a8425Srobbin cfoll(NODE *v)	/* enter follow set of each leaf of vertex v into foll[leaf] */
2337c478bd9Sstevel@tonic-gate {
234dc5a8425Srobbin 	int i;
2357c478bd9Sstevel@tonic-gate 	int prev;
2367c478bd9Sstevel@tonic-gate 	int *add();
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	switch (type(v)) {
2407c478bd9Sstevel@tonic-gate 		LEAF
2417c478bd9Sstevel@tonic-gate 			setcnt = 0;
2427c478bd9Sstevel@tonic-gate 			for (i = 1; i <= line; i++)
2437c478bd9Sstevel@tonic-gate 				setvec[i] = 0;
2447c478bd9Sstevel@tonic-gate 			follow(v);
2457c478bd9Sstevel@tonic-gate 			foll[(int)left(v)] = add(setcnt);
2467c478bd9Sstevel@tonic-gate 			break;
2477c478bd9Sstevel@tonic-gate 		UNARY
2487c478bd9Sstevel@tonic-gate 			cfoll(left(v));
2497c478bd9Sstevel@tonic-gate 			break;
2507c478bd9Sstevel@tonic-gate 		case CAT:
2517c478bd9Sstevel@tonic-gate 		case OR:
2527c478bd9Sstevel@tonic-gate 			cfoll(left(v));
2537c478bd9Sstevel@tonic-gate 			cfoll(right(v));
2547c478bd9Sstevel@tonic-gate 			break;
2557c478bd9Sstevel@tonic-gate 		default:
2567c478bd9Sstevel@tonic-gate 			error(FATAL, "unknown type %d in cfoll", type(v));
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
260dc5a8425Srobbin int
first(NODE * p)261dc5a8425Srobbin first(NODE *p)		/* collects initially active leaves of p into setvec */
2627c478bd9Sstevel@tonic-gate 	/* returns 0 or 1 depending on whether p matches empty string */
2637c478bd9Sstevel@tonic-gate {
264dc5a8425Srobbin 	int b;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	switch (type(p)) {
2687c478bd9Sstevel@tonic-gate 		LEAF
2697c478bd9Sstevel@tonic-gate 			if (setvec[(int)left(p)] != 1) {
2707c478bd9Sstevel@tonic-gate 				setvec[(int)left(p)] = 1;
2717c478bd9Sstevel@tonic-gate 				setcnt++;
2727c478bd9Sstevel@tonic-gate 			}
2737c478bd9Sstevel@tonic-gate 			if (type(p) == CCL &&
2747c478bd9Sstevel@tonic-gate 			(*(ccl_chars_t *)right(p)).cc_cs == (wchar_t)0x0)
2757c478bd9Sstevel@tonic-gate 				return (0);		/* empty CCL */
2767c478bd9Sstevel@tonic-gate 			else return (1);
2777c478bd9Sstevel@tonic-gate 		case FINAL:
2787c478bd9Sstevel@tonic-gate 		case PLUS:
2797c478bd9Sstevel@tonic-gate 			if (first(left(p)) == 0)
2807c478bd9Sstevel@tonic-gate 				return (0);
2817c478bd9Sstevel@tonic-gate 			return (1);
2827c478bd9Sstevel@tonic-gate 		case STAR:
2837c478bd9Sstevel@tonic-gate 		case QUEST:
2847c478bd9Sstevel@tonic-gate 			first(left(p));
2857c478bd9Sstevel@tonic-gate 			return (0);
2867c478bd9Sstevel@tonic-gate 		case CAT:
2877c478bd9Sstevel@tonic-gate 			if (first(left(p)) == 0 && first(right(p)) == 0)
2887c478bd9Sstevel@tonic-gate 				return (0);
2897c478bd9Sstevel@tonic-gate 			return (1);
2907c478bd9Sstevel@tonic-gate 		case OR:
2917c478bd9Sstevel@tonic-gate 			b = first(right(p));
2927c478bd9Sstevel@tonic-gate 			if (first(left(p)) == 0 || b == 0)
2937c478bd9Sstevel@tonic-gate 				return (0);
2947c478bd9Sstevel@tonic-gate 			return (1);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 	error(FATAL, "unknown type %d in first\n", type(p));
2977c478bd9Sstevel@tonic-gate 	return (-1);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
300dc5a8425Srobbin static void
follow(NODE * v)301dc5a8425Srobbin follow(NODE *v)
302dc5a8425Srobbin 		/* collects leaves that can follow v into setvec */
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 	NODE *p;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (type(v) == FINAL)
3087c478bd9Sstevel@tonic-gate 		return;
3097c478bd9Sstevel@tonic-gate 	p = parent(v);
3107c478bd9Sstevel@tonic-gate 	switch (type(p)) {
3117c478bd9Sstevel@tonic-gate 		case STAR:
3127c478bd9Sstevel@tonic-gate 		case PLUS:	first(v);
3137c478bd9Sstevel@tonic-gate 				follow(p);
3147c478bd9Sstevel@tonic-gate 				return;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 		case OR:
3187c478bd9Sstevel@tonic-gate 		case QUEST:	follow(p);
3197c478bd9Sstevel@tonic-gate 				return;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		case CAT:	if (v == left(p)) { /* v is left child of p */
3237c478bd9Sstevel@tonic-gate 					if (first(right(p)) == 0) {
3247c478bd9Sstevel@tonic-gate 						follow(p);
3257c478bd9Sstevel@tonic-gate 						return;
3267c478bd9Sstevel@tonic-gate 					}
3277c478bd9Sstevel@tonic-gate 				} else		/* v is right child */
3287c478bd9Sstevel@tonic-gate 					follow(p);
3297c478bd9Sstevel@tonic-gate 				return;
3307c478bd9Sstevel@tonic-gate 		case FINAL:	if (setvec[line] != 1) {
3317c478bd9Sstevel@tonic-gate 					setvec[line] = 1;
3327c478bd9Sstevel@tonic-gate 					setcnt++;
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 				return;
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3407c478bd9Sstevel@tonic-gate  * There are three type of functions for checking member ship.  Because I have
3417c478bd9Sstevel@tonic-gate  * been changed structure of CCL tables.  And some CCL tables end up with NULLs
3427c478bd9Sstevel@tonic-gate  * but someone has length and will includes NULLs in table as one of data.
3437c478bd9Sstevel@tonic-gate  * Please note, CCL table which has a length data and data will include NULLs,
3447c478bd9Sstevel@tonic-gate  * it only used within a this source file("b.c").
3457c478bd9Sstevel@tonic-gate  */
3467c478bd9Sstevel@tonic-gate 
347dc5a8425Srobbin int				/* is cs thru ce in s? */
ccl_member(int ns,wchar_t cs,int ne,wchar_t ce,ccl_chars_t * s)348dc5a8425Srobbin ccl_member(int ns, wchar_t cs, int ne, wchar_t ce, ccl_chars_t *s)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * The specified range(cs, ce) must be beside the range between
3527c478bd9Sstevel@tonic-gate 	 * s->cc_start and s->cc_end to determine member.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	while (s->cc_cs || s->cc_ce) {
3557c478bd9Sstevel@tonic-gate 		if (MLCMPLE(s->cc_ns, s->cc_cs, ns, cs) &&
3567c478bd9Sstevel@tonic-gate 				MLCMPLE(ne, ce, s->cc_ne, s->cc_ce))
3577c478bd9Sstevel@tonic-gate 			return (1);
3587c478bd9Sstevel@tonic-gate 		s++;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 
364dc5a8425Srobbin 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)365dc5a8425Srobbin ccln_member(int ns, wchar_t cs, int ne, wchar_t ce, ccl_chars_t *s, int n)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * The specified range(cs, ce) must be beside the range between
3697c478bd9Sstevel@tonic-gate 	 * s->cc_start and s->cc_end to determine member.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	while (n-- > 0) {
3727c478bd9Sstevel@tonic-gate 		if (MLCMPLE(s->cc_ns, s->cc_cs, ns, cs) &&
3737c478bd9Sstevel@tonic-gate 				MLCMPLE(ne, ce, s->cc_ne, s->cc_ce))
3747c478bd9Sstevel@tonic-gate 			return (1);
3757c478bd9Sstevel@tonic-gate 		s++;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 
381dc5a8425Srobbin int
member(wchar_t c,wchar_t * s)382dc5a8425Srobbin member(wchar_t c, wchar_t *s)	/* is c in s? */
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	while (*s)
3857c478bd9Sstevel@tonic-gate 		if (c == *s++)
3867c478bd9Sstevel@tonic-gate 			return (1);
3877c478bd9Sstevel@tonic-gate 	return (0);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
390dc5a8425Srobbin int
notin(int ** array,int n,int * prev)391dc5a8425Srobbin notin(int **array, int n, int *prev) /* is setvec in array[0] thru array[n]? */
392dc5a8425Srobbin {
393dc5a8425Srobbin 	int i, j;
3947c478bd9Sstevel@tonic-gate 	int *ptr;
3957c478bd9Sstevel@tonic-gate 	for (i = 0; i <= n; i++) {
3967c478bd9Sstevel@tonic-gate 		ptr = array[i];
3977c478bd9Sstevel@tonic-gate 		if (*ptr == setcnt) {
3987c478bd9Sstevel@tonic-gate 			for (j = 0; j < setcnt; j++)
3997c478bd9Sstevel@tonic-gate 				if (setvec[*(++ptr)] != 1) goto nxt;
4007c478bd9Sstevel@tonic-gate 			*prev = i;
4017c478bd9Sstevel@tonic-gate 			return (0);
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 		nxt: /* dummy */;
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate 	return (1);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 
409dc5a8425Srobbin int *
add(int n)410dc5a8425Srobbin add(int n)
411dc5a8425Srobbin {		/* remember setvec */
4127c478bd9Sstevel@tonic-gate 	int *ptr, *p;
413dc5a8425Srobbin 	int i;
4147c478bd9Sstevel@tonic-gate 	if ((p = ptr = (int *)malloc((n+1)*sizeof (int))) == NULL)
4157c478bd9Sstevel@tonic-gate 		overflo();
4167c478bd9Sstevel@tonic-gate 	*ptr = n;
4177c478bd9Sstevel@tonic-gate 	dprintf("add(%d)\n", n, NULL, NULL);
4187c478bd9Sstevel@tonic-gate 	for (i = 1; i <= line; i++)
4197c478bd9Sstevel@tonic-gate 		if (setvec[i] == 1) {
4207c478bd9Sstevel@tonic-gate 			*(++ptr) = i;
4217c478bd9Sstevel@tonic-gate 		dprintf("  ptr = %o, *ptr = %d, i = %d\n", ptr, *ptr, i);
4227c478bd9Sstevel@tonic-gate 		}
4237c478bd9Sstevel@tonic-gate 	dprintf("\n", NULL, NULL, NULL);
4247c478bd9Sstevel@tonic-gate 	return (p);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate struct fa *
cgotofn()4297c478bd9Sstevel@tonic-gate cgotofn()
4307c478bd9Sstevel@tonic-gate {
431dc5a8425Srobbin 	int i, k;
432dc5a8425Srobbin 	int *ptr;
433dc5a8425Srobbin 	int ns, ne;
434dc5a8425Srobbin 	wchar_t cs, ce;
435dc5a8425Srobbin 	ccl_chars_t *p;
436dc5a8425Srobbin 	NODE *cp;
4377c478bd9Sstevel@tonic-gate 	int j, n, s, ind, numtrans;
4387c478bd9Sstevel@tonic-gate 	int finflg;
4397c478bd9Sstevel@tonic-gate 	int curpos, num, prev;
4407c478bd9Sstevel@tonic-gate 	struct fa *where[NSTATES];
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	struct {
4447c478bd9Sstevel@tonic-gate 		ccl_chars_t	cc;
4457c478bd9Sstevel@tonic-gate 		int		n;
4467c478bd9Sstevel@tonic-gate 	} fatab[257];
447dc5a8425Srobbin 	struct fa *pfa;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 	char index[MAXLIN];
4517c478bd9Sstevel@tonic-gate 	char iposns[MAXLIN];
4527c478bd9Sstevel@tonic-gate 	int sposns[MAXLIN];
4537c478bd9Sstevel@tonic-gate 	int spmax, spinit;
4547c478bd9Sstevel@tonic-gate 	ccl_chars_t symbol[NCHARS];
4557c478bd9Sstevel@tonic-gate 	ccl_chars_t isyms[NCHARS];
4567c478bd9Sstevel@tonic-gate 	ccl_chars_t ssyms[NCHARS];
4577c478bd9Sstevel@tonic-gate 	int ssmax, symax, ismax, ssinit;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	wchar_t hat;
4617c478bd9Sstevel@tonic-gate 	int hatcn;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	for (i = 0; i <= line; i++) index[i] = iposns[i] = setvec[i] = 0;
4657c478bd9Sstevel@tonic-gate 	isyms[0].cc_cs = isyms[0].cc_ce = (wchar_t)0x0;
4667c478bd9Sstevel@tonic-gate 	for (i = 0; i < NCHARS; i++)
4677c478bd9Sstevel@tonic-gate 		isyms[i] = symbol[i] = ssyms[i] = isyms[0];
4687c478bd9Sstevel@tonic-gate 	symax = 0;
4697c478bd9Sstevel@tonic-gate 	setcnt = 0;
4707c478bd9Sstevel@tonic-gate 	/* compute initial positions and symbols of state 0 */
4717c478bd9Sstevel@tonic-gate 	ismax = 0;
4727c478bd9Sstevel@tonic-gate 	ssmax = 0;
4737c478bd9Sstevel@tonic-gate 	ptr = state[0] = foll[0];
4747c478bd9Sstevel@tonic-gate 	spinit = *ptr;
4757c478bd9Sstevel@tonic-gate 	hat = HAT;
4767c478bd9Sstevel@tonic-gate 	hatcn = wcsetno(hat);
4777c478bd9Sstevel@tonic-gate 	for (i = 0; i < spinit; i++) {
4787c478bd9Sstevel@tonic-gate 		curpos = *(++ptr);
4797c478bd9Sstevel@tonic-gate 		sposns[i] = curpos;
4807c478bd9Sstevel@tonic-gate 		iposns[curpos] = 1;
4817c478bd9Sstevel@tonic-gate 		cp = point[curpos];
4827c478bd9Sstevel@tonic-gate 		dprintf("i= %d, spinit = %d, curpos = %d\n", i, spinit, curpos);
4837c478bd9Sstevel@tonic-gate 		switch (type(cp)) {
4847c478bd9Sstevel@tonic-gate 			case CHAR:
4857c478bd9Sstevel@tonic-gate 				k = (int)right(cp);
4867c478bd9Sstevel@tonic-gate 				ns = wcsetno(k);
4877c478bd9Sstevel@tonic-gate 				if (! ccln_member(ns, k, ns, k,
4887c478bd9Sstevel@tonic-gate 							isyms, ismax)) {
4897c478bd9Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
4907c478bd9Sstevel@tonic-gate 								ns, k, ns, k);
4917c478bd9Sstevel@tonic-gate 				}
4927c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
4937c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = k;
4947c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ne = ns;
4957c478bd9Sstevel@tonic-gate 				ssyms[ssmax++].cc_ce = k;
4967c478bd9Sstevel@tonic-gate 				break;
4977c478bd9Sstevel@tonic-gate 			case DOT:
4987c478bd9Sstevel@tonic-gate 				cs = WC_VERY_SMALL;
4997c478bd9Sstevel@tonic-gate 				ns = 0;
5007c478bd9Sstevel@tonic-gate 				ce = HAT - 1;
5017c478bd9Sstevel@tonic-gate 				ne = hatcn;
5027c478bd9Sstevel@tonic-gate 				if (! ccln_member(ns, cs, ne, ce,
5037c478bd9Sstevel@tonic-gate 							isyms, ismax)) {
5047c478bd9Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
5057c478bd9Sstevel@tonic-gate 								ns, cs, ne, ce);
5067c478bd9Sstevel@tonic-gate 				}
5077c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = cs;
5087c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
5097c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ce = ce;
5107c478bd9Sstevel@tonic-gate 				ssyms[ssmax++].cc_ne = ne;
5117c478bd9Sstevel@tonic-gate 				cs = HAT + 1;
5127c478bd9Sstevel@tonic-gate 				ns = hatcn;
5137c478bd9Sstevel@tonic-gate 				ce = WC_VERY_LARGE;
5147c478bd9Sstevel@tonic-gate 				ne = MAX_CODESET;
5157c478bd9Sstevel@tonic-gate 				if (! ccln_member(ns, cs, ne, ce,
5167c478bd9Sstevel@tonic-gate 							isyms, ismax)) {
5177c478bd9Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
5187c478bd9Sstevel@tonic-gate 								ns, cs, ne, ce);
5197c478bd9Sstevel@tonic-gate 				}
5207c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = cs;
5217c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
5227c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ce = ce;
5237c478bd9Sstevel@tonic-gate 				ssyms[ssmax++].cc_ne = ne;
5247c478bd9Sstevel@tonic-gate 				break;
5257c478bd9Sstevel@tonic-gate 			case CCL:
5267c478bd9Sstevel@tonic-gate 				cs = HAT;
5277c478bd9Sstevel@tonic-gate 				ns = hatcn;
5287c478bd9Sstevel@tonic-gate 				for (p = (ccl_chars_t *)right(cp);
5297c478bd9Sstevel@tonic-gate 					p->cc_cs; p++) {
5307c478bd9Sstevel@tonic-gate 					if ((p->cc_ns != ns ||\
5317c478bd9Sstevel@tonic-gate 					p->cc_cs != cs) &&\
5327c478bd9Sstevel@tonic-gate 				!ccln_member(p->cc_ns, p->cc_cs,
5337c478bd9Sstevel@tonic-gate 				p->cc_ne, p->cc_ce, isyms, ismax)) {
5347c478bd9Sstevel@tonic-gate 						ismax = insert_table(isyms,
5357c478bd9Sstevel@tonic-gate 				ismax, p->cc_ns, p->cc_cs, p->cc_ne, p->cc_ce);
5367c478bd9Sstevel@tonic-gate 					}
5377c478bd9Sstevel@tonic-gate 					ssyms[ssmax++] = *p;
5387c478bd9Sstevel@tonic-gate 				}
5397c478bd9Sstevel@tonic-gate 				break;
5407c478bd9Sstevel@tonic-gate 			case NCCL:
5417c478bd9Sstevel@tonic-gate 				ns = 0;
5427c478bd9Sstevel@tonic-gate 				cs = WC_VERY_SMALL;
5437c478bd9Sstevel@tonic-gate 				for (p = (ccl_chars_t *)right(cp);
5447c478bd9Sstevel@tonic-gate 					p->cc_cs; p++) {
5457c478bd9Sstevel@tonic-gate 					if ((ns != hatcn || p->cc_cs != HAT) &&
5467c478bd9Sstevel@tonic-gate 						! ccln_member(ns, cs,
5477c478bd9Sstevel@tonic-gate 							p->cc_ns, p->cc_cs-1,
5487c478bd9Sstevel@tonic-gate 								isyms, ismax)) {
5497c478bd9Sstevel@tonic-gate 						ismax = insert_table(isyms,
5507c478bd9Sstevel@tonic-gate 								ismax,
5517c478bd9Sstevel@tonic-gate 								ns, cs,
5527c478bd9Sstevel@tonic-gate 								p->cc_ns,
5537c478bd9Sstevel@tonic-gate 								p->cc_cs-1);
5547c478bd9Sstevel@tonic-gate 					}
5557c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
5567c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
5577c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ne = p->cc_ns;
5587c478bd9Sstevel@tonic-gate 					ssyms[ssmax++].cc_ce = p->cc_cs-1;
5597c478bd9Sstevel@tonic-gate 					if (p->cc_ce == (wchar_t)0x0) {
5607c478bd9Sstevel@tonic-gate 						ns = p->cc_ns;
5617c478bd9Sstevel@tonic-gate 						cs = p->cc_cs + 1;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 					} else {
5647c478bd9Sstevel@tonic-gate 						ns = p->cc_ne;
5657c478bd9Sstevel@tonic-gate 						cs = p->cc_ce + 1;
5667c478bd9Sstevel@tonic-gate 					}
5677c478bd9Sstevel@tonic-gate 				}
5687c478bd9Sstevel@tonic-gate 				if ((ns != hatcn || cs != HAT) &&
5697c478bd9Sstevel@tonic-gate 					! ccln_member(ns, cs,
5707c478bd9Sstevel@tonic-gate 						MAX_CODESET, WC_VERY_LARGE,
5717c478bd9Sstevel@tonic-gate 							isyms, ismax)) {
5727c478bd9Sstevel@tonic-gate 					ismax = insert_table(isyms, ismax,
5737c478bd9Sstevel@tonic-gate 							ns, cs, MAX_CODESET,
5747c478bd9Sstevel@tonic-gate 							WC_VERY_LARGE);
5757c478bd9Sstevel@tonic-gate 				}
5767c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ns = ns;
5777c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_cs = cs;
5787c478bd9Sstevel@tonic-gate 				ssyms[ssmax].cc_ne = MAX_CODESET;
5797c478bd9Sstevel@tonic-gate 				ssyms[ssmax++].cc_ce = WC_VERY_LARGE;
5807c478bd9Sstevel@tonic-gate 				break;
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	ssinit = ssmax;
5847c478bd9Sstevel@tonic-gate 	symax = 0;
5857c478bd9Sstevel@tonic-gate 	n = 0;
5867c478bd9Sstevel@tonic-gate 	for (s = 0; s <= n; s++)  {
5877c478bd9Sstevel@tonic-gate 		dprintf("s = %d\n", s, NULL, NULL);
5887c478bd9Sstevel@tonic-gate 		ind = 0;
5897c478bd9Sstevel@tonic-gate 		numtrans = 0;
5907c478bd9Sstevel@tonic-gate 		finflg = 0;
5917c478bd9Sstevel@tonic-gate 		if (*(state[s] + *state[s]) == line) {		/* s final? */
5927c478bd9Sstevel@tonic-gate 			finflg = 1;
5937c478bd9Sstevel@tonic-gate 			goto tenter;
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 		spmax = spinit;
5967c478bd9Sstevel@tonic-gate 		ssmax = ssinit;
5977c478bd9Sstevel@tonic-gate 		ptr = state[s];
5987c478bd9Sstevel@tonic-gate 		num = *ptr;
5997c478bd9Sstevel@tonic-gate 		for (i = 0; i < num; i++) {
6007c478bd9Sstevel@tonic-gate 			curpos = *(++ptr);
6017c478bd9Sstevel@tonic-gate 			if (iposns[curpos] != 1 && index[curpos] != 1) {
6027c478bd9Sstevel@tonic-gate 				index[curpos] = 1;
6037c478bd9Sstevel@tonic-gate 				sposns[spmax++] = curpos;
6047c478bd9Sstevel@tonic-gate 			}
6057c478bd9Sstevel@tonic-gate 			cp = point[curpos];
6067c478bd9Sstevel@tonic-gate 			switch (type(cp)) {
6077c478bd9Sstevel@tonic-gate 				case CHAR:
6087c478bd9Sstevel@tonic-gate 					k = (int)right(cp);
6097c478bd9Sstevel@tonic-gate 					ns = wcsetno(k);
6107c478bd9Sstevel@tonic-gate 					if (! ccln_member(ns, k, ns, k,
6117c478bd9Sstevel@tonic-gate 							isyms, ismax) &&
6127c478bd9Sstevel@tonic-gate 						! ccln_member(ns, k, ns, k,
6137c478bd9Sstevel@tonic-gate 							symbol, symax)) {
6147c478bd9Sstevel@tonic-gate 						symax = insert_table(symbol,
6157c478bd9Sstevel@tonic-gate 									symax,
6167c478bd9Sstevel@tonic-gate 									ns, k,
6177c478bd9Sstevel@tonic-gate 									ns, k);
6187c478bd9Sstevel@tonic-gate 					}
6197c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
6207c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = k;
6217c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ne = ns;
6227c478bd9Sstevel@tonic-gate 					ssyms[ssmax++].cc_ce = k;
6237c478bd9Sstevel@tonic-gate 					break;
6247c478bd9Sstevel@tonic-gate 				case DOT:
6257c478bd9Sstevel@tonic-gate 					cs = WC_VERY_SMALL;
6267c478bd9Sstevel@tonic-gate 					ns = 0;
6277c478bd9Sstevel@tonic-gate 					ce = HAT - 1;
6287c478bd9Sstevel@tonic-gate 					ne = hatcn;
6297c478bd9Sstevel@tonic-gate 					if (! ccln_member(ns, cs, ne, ce,
6307c478bd9Sstevel@tonic-gate 							isyms, ismax) &&
6317c478bd9Sstevel@tonic-gate 						! ccln_member(ns, cs, ne, ce,
6327c478bd9Sstevel@tonic-gate 							symbol, symax)) {
6337c478bd9Sstevel@tonic-gate 						symax = insert_table(symbol,
6347c478bd9Sstevel@tonic-gate 									symax,
6357c478bd9Sstevel@tonic-gate 									ns, cs,
6367c478bd9Sstevel@tonic-gate 									ne, ce);
6377c478bd9Sstevel@tonic-gate 					}
6387c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
6397c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
6407c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ce = ce;
6417c478bd9Sstevel@tonic-gate 					ssyms[ssmax++].cc_ne = ne;
6427c478bd9Sstevel@tonic-gate 					cs = HAT + 1;
6437c478bd9Sstevel@tonic-gate 					ns = hatcn;
6447c478bd9Sstevel@tonic-gate 					ce = WC_VERY_LARGE;
6457c478bd9Sstevel@tonic-gate 					ne = MAX_CODESET;
6467c478bd9Sstevel@tonic-gate 					if (! ccln_member(ns, cs, ne, ce,
6477c478bd9Sstevel@tonic-gate 								isyms, ismax) &&
6487c478bd9Sstevel@tonic-gate 						! ccln_member(ns, cs, ne, ce,
6497c478bd9Sstevel@tonic-gate 							symbol, symax)) {
6507c478bd9Sstevel@tonic-gate 						symax = insert_table(symbol,
6517c478bd9Sstevel@tonic-gate 									symax,
6527c478bd9Sstevel@tonic-gate 									ns, cs,
6537c478bd9Sstevel@tonic-gate 									ne, ce);
6547c478bd9Sstevel@tonic-gate 					}
6557c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
6567c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
6577c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ce = ce;
6587c478bd9Sstevel@tonic-gate 					ssyms[ssmax++].cc_ne = ne;
6597c478bd9Sstevel@tonic-gate 					break;
6607c478bd9Sstevel@tonic-gate 				case CCL:
6617c478bd9Sstevel@tonic-gate 					cs = HAT;
6627c478bd9Sstevel@tonic-gate 					ns = hatcn;
6637c478bd9Sstevel@tonic-gate 					for (p = (ccl_chars_t *)right(cp);
6647c478bd9Sstevel@tonic-gate 						p->cc_cs; p++) {
6657c478bd9Sstevel@tonic-gate 						if ((p->cc_ns != ns ||
6667c478bd9Sstevel@tonic-gate 							p->cc_cs != cs) &&
6677c478bd9Sstevel@tonic-gate 							! ccln_member(p->cc_ns,
6687c478bd9Sstevel@tonic-gate 							p->cc_cs, p->cc_ne,
6697c478bd9Sstevel@tonic-gate 						p->cc_ce, isyms, ismax) &&
6707c478bd9Sstevel@tonic-gate 						!ccln_member(p->cc_ns, p->cc_cs,
6717c478bd9Sstevel@tonic-gate 						p->cc_ne, p->cc_ce, symbol,
6727c478bd9Sstevel@tonic-gate 						symax)) {
6737c478bd9Sstevel@tonic-gate 							symax = insert_table(
6747c478bd9Sstevel@tonic-gate 						symbol, symax, p->cc_ns,
6757c478bd9Sstevel@tonic-gate 						p->cc_cs, p->cc_ne, p->cc_ce);
6767c478bd9Sstevel@tonic-gate 						}
6777c478bd9Sstevel@tonic-gate 						ssyms[ssmax++] = *p;
6787c478bd9Sstevel@tonic-gate 					}
6797c478bd9Sstevel@tonic-gate 					break;
6807c478bd9Sstevel@tonic-gate 				case NCCL:
6817c478bd9Sstevel@tonic-gate 					ns = 0;
6827c478bd9Sstevel@tonic-gate 					cs = WC_VERY_SMALL;
6837c478bd9Sstevel@tonic-gate 		for (p = (ccl_chars_t *)right(cp); p->cc_cs; p++) {
6847c478bd9Sstevel@tonic-gate 			if ((p->cc_ns != hatcn || p->cc_cs != HAT) &&
6857c478bd9Sstevel@tonic-gate 					! ccln_member(ns, cs, p->cc_ns,
6867c478bd9Sstevel@tonic-gate 					p->cc_cs-1, isyms, ismax) &&
6877c478bd9Sstevel@tonic-gate 					! ccln_member(ns, cs, p->cc_ns,
6887c478bd9Sstevel@tonic-gate 					p->cc_cs-1, symbol, symax)) {
6897c478bd9Sstevel@tonic-gate 				symax = insert_table(symbol,
6907c478bd9Sstevel@tonic-gate 					symax, ns, cs, p->cc_ns, p->cc_cs-1);
6917c478bd9Sstevel@tonic-gate 						}
6927c478bd9Sstevel@tonic-gate 						ssyms[ssmax].cc_ns = ns;
6937c478bd9Sstevel@tonic-gate 						ssyms[ssmax].cc_cs = cs;
6947c478bd9Sstevel@tonic-gate 						ssyms[ssmax].cc_ne = p->cc_ns;
6957c478bd9Sstevel@tonic-gate 						ssyms[ssmax++].cc_ce
6967c478bd9Sstevel@tonic-gate 								= p->cc_cs-1;
6977c478bd9Sstevel@tonic-gate 						if (p->cc_ce == (wchar_t)0x0) {
6987c478bd9Sstevel@tonic-gate 							ns = p->cc_ns;
6997c478bd9Sstevel@tonic-gate 							cs = p->cc_cs + 1;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 						} else {
7027c478bd9Sstevel@tonic-gate 							ns = p->cc_ne;
7037c478bd9Sstevel@tonic-gate 							cs = p->cc_ce + 1;
7047c478bd9Sstevel@tonic-gate 						}
7057c478bd9Sstevel@tonic-gate 					}
7067c478bd9Sstevel@tonic-gate 		if ((ns != hatcn || cs != HAT) && ! ccln_member(ns, cs,
7077c478bd9Sstevel@tonic-gate 				MAX_CODESET, WC_VERY_LARGE, isyms, ismax) &&
7087c478bd9Sstevel@tonic-gate 				! ccln_member(ns, cs, MAX_CODESET,
7097c478bd9Sstevel@tonic-gate 					WC_VERY_LARGE, symbol, symax)) {
7107c478bd9Sstevel@tonic-gate 			symax = insert_table(symbol, symax, ns, cs,
7117c478bd9Sstevel@tonic-gate 								MAX_CODESET,
7127c478bd9Sstevel@tonic-gate 								WC_VERY_LARGE);
7137c478bd9Sstevel@tonic-gate 					}
7147c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ns = ns;
7157c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_cs = cs;
7167c478bd9Sstevel@tonic-gate 					ssyms[ssmax].cc_ne = MAX_CODESET;
7177c478bd9Sstevel@tonic-gate 					ssyms[ssmax++].cc_ce = WC_VERY_LARGE;
7187c478bd9Sstevel@tonic-gate 					break;
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 		for (j = 0; j < ssmax; j++) {	/* nextstate(s, ssyms[j]) */
7227c478bd9Sstevel@tonic-gate 			ns = ssyms[j].cc_ns;
7237c478bd9Sstevel@tonic-gate 			cs = ssyms[j].cc_cs;
7247c478bd9Sstevel@tonic-gate 			ne = ssyms[j].cc_ne;
7257c478bd9Sstevel@tonic-gate 			ce = ssyms[j].cc_ce;
7267c478bd9Sstevel@tonic-gate dprintf("j = %d, cs = %o, ce = %o\n", j, cs, ce);
7277c478bd9Sstevel@tonic-gate 			symax = delete_table(symbol, symax, ns, cs, ne, ce);
7287c478bd9Sstevel@tonic-gate 			setcnt = 0;
7297c478bd9Sstevel@tonic-gate 			for (k = 0; k <= line; k++) setvec[k] = 0;
7307c478bd9Sstevel@tonic-gate 			for (i = 0; i < spmax; i++) {
7317c478bd9Sstevel@tonic-gate 				index[sposns[i]] = 0;
7327c478bd9Sstevel@tonic-gate 				cp = point[sposns[i]];
7337c478bd9Sstevel@tonic-gate 				if ((k = type(cp)) != FINAL) {
7347c478bd9Sstevel@tonic-gate 					if (k == CHAR && ns == ne && cs == ce &&
7357c478bd9Sstevel@tonic-gate 						cs == (int)right(cp) ||
7367c478bd9Sstevel@tonic-gate 						k == DOT || k == CCL &&
7377c478bd9Sstevel@tonic-gate 						ccl_member(ns, cs, ne, ce,
7387c478bd9Sstevel@tonic-gate 						(ccl_chars_t *)right(cp)) ||
7397c478bd9Sstevel@tonic-gate 						k == NCCL &&
7407c478bd9Sstevel@tonic-gate 						!ccl_member(ns, cs, ne, ce,
7417c478bd9Sstevel@tonic-gate 						(ccl_chars_t *)right(cp))) {
7427c478bd9Sstevel@tonic-gate 						ptr = foll[sposns[i]];
7437c478bd9Sstevel@tonic-gate 						num = *ptr;
7447c478bd9Sstevel@tonic-gate 						for (k = 0; k < num; k++) {
7457c478bd9Sstevel@tonic-gate 						if (setvec[*(++ptr)] != 1 &&
7467c478bd9Sstevel@tonic-gate 							iposns[*ptr] != 1) {
7477c478bd9Sstevel@tonic-gate 							setvec[*ptr] = 1;
7487c478bd9Sstevel@tonic-gate 								setcnt++;
7497c478bd9Sstevel@tonic-gate 							}
7507c478bd9Sstevel@tonic-gate 						}
7517c478bd9Sstevel@tonic-gate 					}
7527c478bd9Sstevel@tonic-gate 				}
7537c478bd9Sstevel@tonic-gate 			} /* end nextstate */
7547c478bd9Sstevel@tonic-gate 			if (notin(state, n, &prev)) {
7557c478bd9Sstevel@tonic-gate 				if (n >= NSTATES - 1) {
7567c478bd9Sstevel@tonic-gate 		printf("cgotofn: notin; state = %d, n = %d\n", state, n, NULL);
7577c478bd9Sstevel@tonic-gate 					overflo();
7587c478bd9Sstevel@tonic-gate 				}
7597c478bd9Sstevel@tonic-gate 				state[++n] = add(setcnt);
7607c478bd9Sstevel@tonic-gate 				dprintf("	delta(%d,[%o,%o])",
7617c478bd9Sstevel@tonic-gate 					s, cs, ce);
7627c478bd9Sstevel@tonic-gate 				dprintf(" = %d, ind = %d\n", n, ind+1, NULL);
7637c478bd9Sstevel@tonic-gate 				fatab[++ind].cc.cc_ns = ns;
7647c478bd9Sstevel@tonic-gate 				fatab[ind].cc.cc_cs = cs;
7657c478bd9Sstevel@tonic-gate 				fatab[ind].cc.cc_ne = ne;
7667c478bd9Sstevel@tonic-gate 				fatab[ind].cc.cc_ce = ce;
7677c478bd9Sstevel@tonic-gate 				fatab[ind].n = n;
7687c478bd9Sstevel@tonic-gate 				numtrans++;
7697c478bd9Sstevel@tonic-gate 			} else {
7707c478bd9Sstevel@tonic-gate 				if (prev != 0) {
7717c478bd9Sstevel@tonic-gate 					dprintf("	delta(%d,[%o,%o])",
7727c478bd9Sstevel@tonic-gate 						s, cs, ce);
7737c478bd9Sstevel@tonic-gate 					dprintf("= %d, ind = %d\n",
7747c478bd9Sstevel@tonic-gate 						prev, ind+1, NULL);
7757c478bd9Sstevel@tonic-gate 					fatab[++ind].cc.cc_ns = ns;
7767c478bd9Sstevel@tonic-gate 					fatab[ind].cc.cc_cs = cs;
7777c478bd9Sstevel@tonic-gate 					fatab[ind].cc.cc_ne = ne;
7787c478bd9Sstevel@tonic-gate 					fatab[ind].cc.cc_ce = ce;
7797c478bd9Sstevel@tonic-gate 					fatab[ind].n = prev;
7807c478bd9Sstevel@tonic-gate 					numtrans++;
7817c478bd9Sstevel@tonic-gate 				}
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	tenter:
7857c478bd9Sstevel@tonic-gate 		if ((pfa = (struct fa *)malloc((numtrans + 1)
7867c478bd9Sstevel@tonic-gate 						* sizeof (struct fa))) == NULL)
7877c478bd9Sstevel@tonic-gate 			overflo();
7887c478bd9Sstevel@tonic-gate 		where[s] = pfa;
7897c478bd9Sstevel@tonic-gate 		if (finflg)
7907c478bd9Sstevel@tonic-gate 			pfa->cc.h = -1;		/* s is a final state */
7917c478bd9Sstevel@tonic-gate 		else
7927c478bd9Sstevel@tonic-gate 			pfa->cc.h = numtrans;
7937c478bd9Sstevel@tonic-gate 		pfa->st = 0;
7947c478bd9Sstevel@tonic-gate 		for (i = 1, pfa += 1; i <= numtrans; i++, pfa++) {
7957c478bd9Sstevel@tonic-gate 			pfa->cc.s = fatab[i].cc;
7967c478bd9Sstevel@tonic-gate 			pfa->st = (struct fa *)fatab[i].n;
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	for (i = 0; i <= n; i++) {
8007c478bd9Sstevel@tonic-gate 		if (i != 0)	/* state[0] is freed later in freetr() */
8017c478bd9Sstevel@tonic-gate 			xfree(state[i]);	/* free state[i] */
8027c478bd9Sstevel@tonic-gate 		pfa = where[i];
8037c478bd9Sstevel@tonic-gate 		pfa->st = where[0];
8047c478bd9Sstevel@tonic-gate 		dprintf("state %d: (%o)\n", i, pfa, NULL);
8057c478bd9Sstevel@tonic-gate 		dprintf("	numtrans = %d,	default = %o\n",
8067c478bd9Sstevel@tonic-gate 			pfa->cc.h, pfa->st, NULL);
8077c478bd9Sstevel@tonic-gate 		for (k = 1; k <= pfa->cc.h; k++) {
8087c478bd9Sstevel@tonic-gate 			(pfa+k)->st = where[(int)(pfa+k)->st];
8097c478bd9Sstevel@tonic-gate 			dprintf("	char = [%o,%o],	nextstate = %o\n",
8107c478bd9Sstevel@tonic-gate 				(pfa+k)->cc.s.cc_cs, (pfa+k)->cc.s.cc_ce,
8117c478bd9Sstevel@tonic-gate 				(pfa+k)->st);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 	pfa = where[0];
8157c478bd9Sstevel@tonic-gate 	if ((num = pfa->cc.h) < 0)
8167c478bd9Sstevel@tonic-gate 		return (where[0]);
8177c478bd9Sstevel@tonic-gate 	for (pfa += num; num; num--, pfa--)
8187c478bd9Sstevel@tonic-gate 		if (pfa->cc.s.cc_ns == hatcn && pfa->cc.s.cc_cs == HAT) {
8197c478bd9Sstevel@tonic-gate 			return (pfa->st);
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	return (where[0]);
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * Insert CCL entry to CCL table with maintain optimized order.
8277c478bd9Sstevel@tonic-gate  */
8287c478bd9Sstevel@tonic-gate static int
insert_table(ccl_chars_t * table_base,int table_size,int ns,wchar_t cs,int ne,wchar_t ce)829dc5a8425Srobbin insert_table(ccl_chars_t *table_base, int table_size, int ns, wchar_t cs,
830dc5a8425Srobbin 	int ne, wchar_t ce)
8317c478bd9Sstevel@tonic-gate {
832dc5a8425Srobbin 	int		i;
833dc5a8425Srobbin 	int		tns, tne;
834dc5a8425Srobbin 	wchar_t		tcs, tce;
835dc5a8425Srobbin 	ccl_chars_t	*table;
836dc5a8425Srobbin 	ccl_chars_t	*saved_table;
8377c478bd9Sstevel@tonic-gate 	int		saved_i;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	dprintf("Inserting {%o, %o} to table %o\n", cs, ce, table_base);
8437c478bd9Sstevel@tonic-gate 	/*
8447c478bd9Sstevel@tonic-gate 	 * Searching the table to find out where should put the new item.
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 	for (i = 0, table = table_base; i < table_size; i++, table++) {
8477c478bd9Sstevel@tonic-gate 		tns = table->cc_ns;
8487c478bd9Sstevel@tonic-gate 		tcs = table->cc_cs;
8497c478bd9Sstevel@tonic-gate 		tne = table->cc_ne;
8507c478bd9Sstevel@tonic-gate 		tce = table->cc_ce;
8517c478bd9Sstevel@tonic-gate 		if (MLCMPLT(ne, ce, tns, (tcs - 1))) {
8527c478bd9Sstevel@tonic-gate 			/*
8537c478bd9Sstevel@tonic-gate 			 * Quick! insert to font of current table entries.
8547c478bd9Sstevel@tonic-gate 			 */
8557c478bd9Sstevel@tonic-gate qinsert:
8567c478bd9Sstevel@tonic-gate 			table_size++;
8577c478bd9Sstevel@tonic-gate 			for (; i < table_size; i++, table++) {
8587c478bd9Sstevel@tonic-gate 				tns = table->cc_ns;
8597c478bd9Sstevel@tonic-gate 				tcs = table->cc_cs;
8607c478bd9Sstevel@tonic-gate 				tne = table->cc_ne;
8617c478bd9Sstevel@tonic-gate 				tce = table->cc_ce;
8627c478bd9Sstevel@tonic-gate 				table->cc_ns = ns;
8637c478bd9Sstevel@tonic-gate 				table->cc_cs = cs;
8647c478bd9Sstevel@tonic-gate 				table->cc_ne = ne;
8657c478bd9Sstevel@tonic-gate 				table->cc_ce = ce;
8667c478bd9Sstevel@tonic-gate 				ns = tns;
8677c478bd9Sstevel@tonic-gate 				cs = tcs;
8687c478bd9Sstevel@tonic-gate 				ne = tne;
8697c478bd9Sstevel@tonic-gate 				ce = tce;
8707c478bd9Sstevel@tonic-gate 			}
8717c478bd9Sstevel@tonic-gate 			goto add_null;
8727c478bd9Sstevel@tonic-gate 		} else if (MLCMPLE(tns, (tcs - 1), ns, cs) &&
8737c478bd9Sstevel@tonic-gate 				MLCMPLE(ns, cs, tne, (tce + 1))) {
8747c478bd9Sstevel@tonic-gate 			/*
8757c478bd9Sstevel@tonic-gate 			 * Starting point is within the current entry.
8767c478bd9Sstevel@tonic-gate 			 */
8777c478bd9Sstevel@tonic-gate 			if (MLCMPGT(tns, tcs, ns, cs)) {
8787c478bd9Sstevel@tonic-gate 				table->cc_ns = ns;
8797c478bd9Sstevel@tonic-gate 				table->cc_cs = cs;
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 			if (MLCMPLE(ne, ce, tne, tce)) {
8827c478bd9Sstevel@tonic-gate 				return (table_size);
8837c478bd9Sstevel@tonic-gate 			}
8847c478bd9Sstevel@tonic-gate 			goto combine;
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	/*
8907c478bd9Sstevel@tonic-gate 	 * Adding new one to end of table.
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	table->cc_ns = ns;
8937c478bd9Sstevel@tonic-gate 	table->cc_cs = cs;
8947c478bd9Sstevel@tonic-gate 	table->cc_ne = ne;
8957c478bd9Sstevel@tonic-gate 	table->cc_ce = ce;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	table_size++;
8997c478bd9Sstevel@tonic-gate 	goto add_null;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	combine:
9057c478bd9Sstevel@tonic-gate 	/*
9067c478bd9Sstevel@tonic-gate 	 * Check and try to combine the new entry with rest of entries.
9077c478bd9Sstevel@tonic-gate 	 */
9087c478bd9Sstevel@tonic-gate 	if ((i + 1) >= table_size) {
9097c478bd9Sstevel@tonic-gate 		table->cc_ne = ne;
9107c478bd9Sstevel@tonic-gate 		table->cc_ce = ce;
9117c478bd9Sstevel@tonic-gate 		return (table_size);
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	saved_table = table++;
9167c478bd9Sstevel@tonic-gate 	saved_i = i++;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/*
9207c478bd9Sstevel@tonic-gate 	 * Finding the spot where we should put the end point.
9217c478bd9Sstevel@tonic-gate 	 */
9227c478bd9Sstevel@tonic-gate 	for (; i < table_size; i++, table++) {
9237c478bd9Sstevel@tonic-gate 		if (MLCMPLT(ne, ce, table->cc_ns, (table->cc_cs - 1))) {
9247c478bd9Sstevel@tonic-gate 			break;
9257c478bd9Sstevel@tonic-gate 		} else
9267c478bd9Sstevel@tonic-gate 		if (MLCMPLE(table->cc_ns, (table->cc_cs - 1), ne, ce) &&
9277c478bd9Sstevel@tonic-gate 			MLCMPLE(ne, ce, table->cc_ne, (table->cc_ce + 1))) {
9287c478bd9Sstevel@tonic-gate 			/*
9297c478bd9Sstevel@tonic-gate 			 * Tack with this table.
9307c478bd9Sstevel@tonic-gate 			 */
9317c478bd9Sstevel@tonic-gate 			if (MLCMPLT(ne, ce, table->cc_ne, table->cc_ce)) {
9327c478bd9Sstevel@tonic-gate 				ne = table->cc_ne;
9337c478bd9Sstevel@tonic-gate 				ce = table->cc_ce;
9347c478bd9Sstevel@tonic-gate 			}
9357c478bd9Sstevel@tonic-gate 			table++;
9367c478bd9Sstevel@tonic-gate 			i++;
9377c478bd9Sstevel@tonic-gate 			break;
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	saved_table->cc_ne = ne;
9437c478bd9Sstevel@tonic-gate 	saved_table->cc_ce = ce;
9447c478bd9Sstevel@tonic-gate 	saved_i = table_size - (i - saved_i - 1);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
9487c478bd9Sstevel@tonic-gate 	 * Moving the rest of entries.
9497c478bd9Sstevel@tonic-gate 	 */
9507c478bd9Sstevel@tonic-gate 	for (; i < table_size; i++, table++)
9517c478bd9Sstevel@tonic-gate 		*(++saved_table) = *table;
9527c478bd9Sstevel@tonic-gate 	table_size = saved_i;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate add_null:
9567c478bd9Sstevel@tonic-gate 	table_base[table_size].cc_cs = (wchar_t)0x0;
9577c478bd9Sstevel@tonic-gate 	table_base[table_size].cc_ce = (wchar_t)0x0;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	return (table_size);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate static int
delete_table(ccl_chars_t * table_base,int table_size,int ns,wchar_t cs,int ne,wchar_t ce)967dc5a8425Srobbin delete_table(ccl_chars_t *table_base, int table_size, int ns, wchar_t cs,
968dc5a8425Srobbin 		int ne, wchar_t ce)
9697c478bd9Sstevel@tonic-gate {
970dc5a8425Srobbin 	int		i;
9717c478bd9Sstevel@tonic-gate 	int		saved_i;
972dc5a8425Srobbin 	ccl_chars_t	*table;
973dc5a8425Srobbin 	ccl_chars_t	*saved_table;
974dc5a8425Srobbin 	int		tns;
975dc5a8425Srobbin 	wchar_t		tcs;
976dc5a8425Srobbin 	int		tne;
977dc5a8425Srobbin 	wchar_t		tce;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	for (i = 0, table = table_base; i < table_size; i++, table++) {
9837c478bd9Sstevel@tonic-gate 		tns = table->cc_ns;
9847c478bd9Sstevel@tonic-gate 		tcs = table->cc_cs;
9857c478bd9Sstevel@tonic-gate 		tne = table->cc_ne;
9867c478bd9Sstevel@tonic-gate 		tce = table->cc_ce;
9877c478bd9Sstevel@tonic-gate 		if (MLCMPLT(ne, ce, tns, tcs))
9887c478bd9Sstevel@tonic-gate 			return (table_size);
9897c478bd9Sstevel@tonic-gate 		else if (MLCMPLT(ne, ce, tne, tce)) {
9907c478bd9Sstevel@tonic-gate 			if (MLCMPLE(ns, cs, tns, tcs)) {
9917c478bd9Sstevel@tonic-gate 				/*
9927c478bd9Sstevel@tonic-gate 				 * Shrink type 1.
9937c478bd9Sstevel@tonic-gate 				 */
9947c478bd9Sstevel@tonic-gate 				table->cc_ns = ne;
9957c478bd9Sstevel@tonic-gate 				table->cc_cs = ce + 1;
9967c478bd9Sstevel@tonic-gate 				return (table_size);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 			} else {
9997c478bd9Sstevel@tonic-gate 				/*
10007c478bd9Sstevel@tonic-gate 				 * Spliting !!
10017c478bd9Sstevel@tonic-gate 				 */
10027c478bd9Sstevel@tonic-gate 				table->cc_ns = ne;
10037c478bd9Sstevel@tonic-gate 				table->cc_cs = ce + 1;
10047c478bd9Sstevel@tonic-gate 				tne = ns;
10057c478bd9Sstevel@tonic-gate 				tce = cs - 1;
10067c478bd9Sstevel@tonic-gate 				table_size++;
10077c478bd9Sstevel@tonic-gate 				for (; i < table_size; i++, table++) {
10087c478bd9Sstevel@tonic-gate 					ns = table->cc_ns;
10097c478bd9Sstevel@tonic-gate 					cs = table->cc_cs;
10107c478bd9Sstevel@tonic-gate 					ne = table->cc_ne;
10117c478bd9Sstevel@tonic-gate 					ce = table->cc_ce;
10127c478bd9Sstevel@tonic-gate 					table->cc_ns = tns;
10137c478bd9Sstevel@tonic-gate 					table->cc_cs = tcs;
10147c478bd9Sstevel@tonic-gate 					table->cc_ne = tne;
10157c478bd9Sstevel@tonic-gate 					table->cc_ce = tce;
10167c478bd9Sstevel@tonic-gate 					tns = ns;
10177c478bd9Sstevel@tonic-gate 					tcs = cs;
10187c478bd9Sstevel@tonic-gate 					tne = ne;
10197c478bd9Sstevel@tonic-gate 					tce = ce;
10207c478bd9Sstevel@tonic-gate 				}
10217c478bd9Sstevel@tonic-gate 				return (table_size);
10227c478bd9Sstevel@tonic-gate 			}
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 		} else if (MLCMPLE(ns, cs, tne, tce)) {
10257c478bd9Sstevel@tonic-gate 			if (MLCMPGT(ns, cs, tns, tcs)) {
10267c478bd9Sstevel@tonic-gate 				/*
10277c478bd9Sstevel@tonic-gate 				 * Shrink current table(type 2).
10287c478bd9Sstevel@tonic-gate 				 */
10297c478bd9Sstevel@tonic-gate 				table->cc_ne = ns;
10307c478bd9Sstevel@tonic-gate 				table->cc_ce = cs - 1;
10317c478bd9Sstevel@tonic-gate 				table++;
10327c478bd9Sstevel@tonic-gate 				i++;
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 			/*
10357c478bd9Sstevel@tonic-gate 			 * Search for the end point.
10367c478bd9Sstevel@tonic-gate 			 */
10377c478bd9Sstevel@tonic-gate 			saved_i = i;
10387c478bd9Sstevel@tonic-gate 			saved_table = table;
10397c478bd9Sstevel@tonic-gate 			for (; i < table_size; i++, table++) {
10407c478bd9Sstevel@tonic-gate 				if (MLCMPLT(ne, ce,
10417c478bd9Sstevel@tonic-gate 						table->cc_ns, table->cc_cs)) {
10427c478bd9Sstevel@tonic-gate 					/*
10437c478bd9Sstevel@tonic-gate 					 * Easy point, no shrinks!
10447c478bd9Sstevel@tonic-gate 					 */
10457c478bd9Sstevel@tonic-gate 					break;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 				} else if (MLCMPGT(table->cc_ne, table->cc_ce,
10487c478bd9Sstevel@tonic-gate 						ne, ce)) {
10497c478bd9Sstevel@tonic-gate 					/*
10507c478bd9Sstevel@tonic-gate 					 * Shrinking...
10517c478bd9Sstevel@tonic-gate 					 */
10527c478bd9Sstevel@tonic-gate 					table->cc_ns = ne;
10537c478bd9Sstevel@tonic-gate 					table->cc_cs = ce + 1;
10547c478bd9Sstevel@tonic-gate 					break;
10557c478bd9Sstevel@tonic-gate 				}
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 			}
10597c478bd9Sstevel@tonic-gate 			/*
10607c478bd9Sstevel@tonic-gate 			 * Moving(removing) backword.
10617c478bd9Sstevel@tonic-gate 			 */
10627c478bd9Sstevel@tonic-gate 			saved_i = table_size - (i - saved_i);
10637c478bd9Sstevel@tonic-gate 			for (; i < table_size; i++)
10647c478bd9Sstevel@tonic-gate 				*saved_table++ = *table++;
10657c478bd9Sstevel@tonic-gate 			return (saved_i);
10667c478bd9Sstevel@tonic-gate 		}
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 	return (table_size);
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate #ifdef DEBUG
dump_table(ccl_chars_t * table,int size)1073dc5a8425Srobbin dump_table(ccl_chars_t *table, int size)
10747c478bd9Sstevel@tonic-gate {
1075dc5a8425Srobbin 	int	i;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (! dbg)
10817c478bd9Sstevel@tonic-gate 		return;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	printf("Duming table %o with size %d\n", table, size);
10857c478bd9Sstevel@tonic-gate 	size++;	/* To watch out NULL */
1086*45effca3Srobbin 	for (i = 0; i < size; i++, table++) {
10877c478bd9Sstevel@tonic-gate 		printf("{%3o, %3o}, ", table->cc_cs, table->cc_ce);
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate 	printf("\n");
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate #endif /* DEBUG */
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 
1095dc5a8425Srobbin int
match(struct fa * pfa,wchar_t * p)1096dc5a8425Srobbin match(struct fa *pfa, wchar_t *p)
10977c478bd9Sstevel@tonic-gate {
1098dc5a8425Srobbin 	int count;
1099dc5a8425Srobbin 	int n, ns, ne;
1100dc5a8425Srobbin 	wchar_t c, cs, ce;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	if (p == 0)
11047c478bd9Sstevel@tonic-gate 		return (0);
11057c478bd9Sstevel@tonic-gate 	if (pfa->cc.h == 1) { /* fast test for first character, if possible */
11067c478bd9Sstevel@tonic-gate 		ns = (++pfa)->cc.s.cc_ns;
11077c478bd9Sstevel@tonic-gate 		cs = (pfa)->cc.s.cc_cs;
11087c478bd9Sstevel@tonic-gate 		ne = (pfa)->cc.s.cc_ne;
11097c478bd9Sstevel@tonic-gate 		ce = (pfa)->cc.s.cc_ce;
11107c478bd9Sstevel@tonic-gate 		do {
11117c478bd9Sstevel@tonic-gate 			c = *p;
11127c478bd9Sstevel@tonic-gate 			n = wcsetno(c);
11137c478bd9Sstevel@tonic-gate 			if (MLCMPLE(ns, cs, n, c) &&
11147c478bd9Sstevel@tonic-gate 				MLCMPLE(n, c, ne, ce)) {
11157c478bd9Sstevel@tonic-gate 				p++;
11167c478bd9Sstevel@tonic-gate 				pfa = pfa->st;
11177c478bd9Sstevel@tonic-gate 				goto adv;
11187c478bd9Sstevel@tonic-gate 			}
11197c478bd9Sstevel@tonic-gate 		} while (*p++ != 0);
11207c478bd9Sstevel@tonic-gate 		return (0);
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	adv: if ((count = pfa->cc.h) < 0)
11237c478bd9Sstevel@tonic-gate 		return (1);
11247c478bd9Sstevel@tonic-gate 	do {
11257c478bd9Sstevel@tonic-gate 		c = *p;
11267c478bd9Sstevel@tonic-gate 		n = wcsetno(c);
11277c478bd9Sstevel@tonic-gate 		for (pfa += count; count; count--, pfa--) {
11287c478bd9Sstevel@tonic-gate 			ns = (pfa)->cc.s.cc_ns;
11297c478bd9Sstevel@tonic-gate 			cs = (pfa)->cc.s.cc_cs;
11307c478bd9Sstevel@tonic-gate 			ne = (pfa)->cc.s.cc_ne;
11317c478bd9Sstevel@tonic-gate 			ce = (pfa)->cc.s.cc_ce;
11327c478bd9Sstevel@tonic-gate 			if (MLCMPLE(ns, cs, n, c) && MLCMPLE(n, c, ne, ce))
11337c478bd9Sstevel@tonic-gate 				break;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 		pfa = pfa->st;
11367c478bd9Sstevel@tonic-gate 		if ((count = pfa->cc.h) < 0)
11377c478bd9Sstevel@tonic-gate 			return (1);
11387c478bd9Sstevel@tonic-gate 	} while (*p++ != 0);
11397c478bd9Sstevel@tonic-gate 	return (0);
11407c478bd9Sstevel@tonic-gate }
1141