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