xref: /titanic_53/usr/src/cmd/spell/spellin.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4	*/
27*7c478bd9Sstevel@tonic-gate #include <unistd.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <locale.h>
31*7c478bd9Sstevel@tonic-gate #include "hash.h"
32*7c478bd9Sstevel@tonic-gate #include "huff.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate int	encode(long, long *);
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	S (BYTE * sizeof (long))
37*7c478bd9Sstevel@tonic-gate #define	B (BYTE * sizeof (unsigned))
38*7c478bd9Sstevel@tonic-gate unsigned *table;
39*7c478bd9Sstevel@tonic-gate int hindex[NI];
40*7c478bd9Sstevel@tonic-gate unsigned wp;		/* word pointer */
41*7c478bd9Sstevel@tonic-gate int bp = B;		/* bit pointer */
42*7c478bd9Sstevel@tonic-gate static int ignore;
43*7c478bd9Sstevel@tonic-gate static int extra;
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate static int
46*7c478bd9Sstevel@tonic-gate append(register unsigned w1, register int i)
47*7c478bd9Sstevel@tonic-gate {
48*7c478bd9Sstevel@tonic-gate 	while (wp < ND - 1) {
49*7c478bd9Sstevel@tonic-gate 		table[wp] |= w1>>(B-bp);
50*7c478bd9Sstevel@tonic-gate 		i -= bp;
51*7c478bd9Sstevel@tonic-gate 		if (i < 0) {
52*7c478bd9Sstevel@tonic-gate 			bp = -i;
53*7c478bd9Sstevel@tonic-gate 			return (1);
54*7c478bd9Sstevel@tonic-gate 		}
55*7c478bd9Sstevel@tonic-gate 		w1 <<= bp;
56*7c478bd9Sstevel@tonic-gate 		bp = B;
57*7c478bd9Sstevel@tonic-gate 		wp++;
58*7c478bd9Sstevel@tonic-gate 	}
59*7c478bd9Sstevel@tonic-gate 	return (0);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate /*
64*7c478bd9Sstevel@tonic-gate  *	usage: hashin N
65*7c478bd9Sstevel@tonic-gate  *	where N is number of words in dictionary
66*7c478bd9Sstevel@tonic-gate  *	and standard input contains sorted, unique
67*7c478bd9Sstevel@tonic-gate  *	hashed words in octal
68*7c478bd9Sstevel@tonic-gate  */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate void
71*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	long h, k, d;
74*7c478bd9Sstevel@tonic-gate 	int  i;
75*7c478bd9Sstevel@tonic-gate 	long count;
76*7c478bd9Sstevel@tonic-gate 	long w1;
77*7c478bd9Sstevel@tonic-gate 	long x;
78*7c478bd9Sstevel@tonic-gate 	int t, u;
79*7c478bd9Sstevel@tonic-gate 	double z;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	/* Set locale environment variables local definitions */
82*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
83*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
84*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it wasn't */
85*7c478bd9Sstevel@tonic-gate #endif
86*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	k = 0;
89*7c478bd9Sstevel@tonic-gate 	u = 0;
90*7c478bd9Sstevel@tonic-gate 	if (argc != 2) {
91*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: arg count\n"), argv[0]);
92*7c478bd9Sstevel@tonic-gate 		exit(1);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 	table = (unsigned *)malloc(ND * sizeof (*table));
95*7c478bd9Sstevel@tonic-gate 	if (table == 0) {
96*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: no space for table\n"),
97*7c478bd9Sstevel@tonic-gate 		    argv[0]);
98*7c478bd9Sstevel@tonic-gate 		exit(1);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 	if ((atof(argv[1])) == 0.0) {
101*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: illegal count"), argv[0]);
102*7c478bd9Sstevel@tonic-gate 		exit(1);
103*7c478bd9Sstevel@tonic-gate 	}
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	z = huff((1L<<HASHWIDTH)/atof(argv[1]));
106*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: expected code widths = %f\n"),
107*7c478bd9Sstevel@tonic-gate 	    argv[0], z);
108*7c478bd9Sstevel@tonic-gate 	for (count = 0; scanf("%lo", (unsigned long *)&h) == 1; ++count) {
109*7c478bd9Sstevel@tonic-gate 		if ((t = h >> (HASHWIDTH - INDEXWIDTH)) != u) {
110*7c478bd9Sstevel@tonic-gate 			if (bp != B)
111*7c478bd9Sstevel@tonic-gate 				wp++;
112*7c478bd9Sstevel@tonic-gate 			bp = B;
113*7c478bd9Sstevel@tonic-gate 			while (u < t)
114*7c478bd9Sstevel@tonic-gate 				hindex[++u] = wp;
115*7c478bd9Sstevel@tonic-gate 			k =  (long)t<<(HASHWIDTH-INDEXWIDTH);
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		d = h-k;
118*7c478bd9Sstevel@tonic-gate 		k = h;
119*7c478bd9Sstevel@tonic-gate 		for (;;) {
120*7c478bd9Sstevel@tonic-gate 			for (x = d; ; x /= 2) {
121*7c478bd9Sstevel@tonic-gate 				i = encode(x, &w1);
122*7c478bd9Sstevel@tonic-gate 				if (i > 0)
123*7c478bd9Sstevel@tonic-gate 					break;
124*7c478bd9Sstevel@tonic-gate 			}
125*7c478bd9Sstevel@tonic-gate 			if (i > B) {
126*7c478bd9Sstevel@tonic-gate 				if (!(append((unsigned)(w1>>(long) (i-B)), B) &&
127*7c478bd9Sstevel@tonic-gate 				    append((unsigned)(w1<<(long) (B+B-i)),
128*7c478bd9Sstevel@tonic-gate 				    i-B)))
129*7c478bd9Sstevel@tonic-gate 					ignore++;
130*7c478bd9Sstevel@tonic-gate 			} else
131*7c478bd9Sstevel@tonic-gate 				if (!append((unsigned)(w1<<(long)(B-i)), i))
132*7c478bd9Sstevel@tonic-gate 					ignore++;
133*7c478bd9Sstevel@tonic-gate 			d -= x;
134*7c478bd9Sstevel@tonic-gate 			if (d > 0)
135*7c478bd9Sstevel@tonic-gate 				extra++;
136*7c478bd9Sstevel@tonic-gate 			else
137*7c478bd9Sstevel@tonic-gate 				break;
138*7c478bd9Sstevel@tonic-gate 		}
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	if (bp != B)
141*7c478bd9Sstevel@tonic-gate 		wp++;
142*7c478bd9Sstevel@tonic-gate 	while (++u < NI)
143*7c478bd9Sstevel@tonic-gate 		hindex[u] = wp;
144*7c478bd9Sstevel@tonic-gate 	whuff();
145*7c478bd9Sstevel@tonic-gate 	(void) fwrite((char *)hindex, sizeof (*hindex), NI, stdout);
146*7c478bd9Sstevel@tonic-gate 	(void) fwrite((char *)table, sizeof (*table), wp, stdout);
147*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
148*7c478bd9Sstevel@tonic-gate 	    gettext("%s: %ld items, %d ignored, %d extra, %u words occupied\n"),
149*7c478bd9Sstevel@tonic-gate 	    argv[0], count, ignore, extra, wp);
150*7c478bd9Sstevel@tonic-gate 	count -= ignore;
151*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %f table bits/item, %f table+index bits\n",
152*7c478bd9Sstevel@tonic-gate 	    argv[0], (((float)BYTE * wp) * sizeof (*table) / count),
153*7c478bd9Sstevel@tonic-gate 	    (BYTE * ((float)wp * sizeof (*table) + sizeof (hindex)) / count));
154*7c478bd9Sstevel@tonic-gate 	exit(0);
155*7c478bd9Sstevel@tonic-gate }
156