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