xref: /titanic_44/usr/src/cmd/refer/inv2.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include <stdio.h>
19*7c478bd9Sstevel@tonic-gate #include <assert.h>
20*7c478bd9Sstevel@tonic-gate #define LINESIZ 1250
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate newkeys (outf, inf, recf, nhash, fd, iflong)
23*7c478bd9Sstevel@tonic-gate FILE *outf, *inf, *recf, *fd;
24*7c478bd9Sstevel@tonic-gate int *iflong;
25*7c478bd9Sstevel@tonic-gate {
26*7c478bd9Sstevel@tonic-gate 	/* reads key lines from inf; hashes and writes on outf;
27*7c478bd9Sstevel@tonic-gate 	 * writes orig key on recf, records pointer on outf too.
28*7c478bd9Sstevel@tonic-gate 	 * format of outf is : hash code space record pointer
29*7c478bd9Sstevel@tonic-gate 	 */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate 	long lp, ftell();
32*7c478bd9Sstevel@tonic-gate 	long ld = 0;
33*7c478bd9Sstevel@tonic-gate 	int ll = 0, lt = 0;
34*7c478bd9Sstevel@tonic-gate 	char line[LINESIZ];
35*7c478bd9Sstevel@tonic-gate 	char key[30], bkeys[40];
36*7c478bd9Sstevel@tonic-gate 	char *p, *s;
37*7c478bd9Sstevel@tonic-gate 	char *keyv[500];
38*7c478bd9Sstevel@tonic-gate 	int i, nk, ndoc = 0, more = 0, c;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 	lp = ftell (recf);
41*7c478bd9Sstevel@tonic-gate 	while (fgets(line, LINESIZ, inf))
42*7c478bd9Sstevel@tonic-gate 	{
43*7c478bd9Sstevel@tonic-gate 		p = line;
44*7c478bd9Sstevel@tonic-gate 		while (*p != '\t') p++;
45*7c478bd9Sstevel@tonic-gate 		*p++ =0;
46*7c478bd9Sstevel@tonic-gate 		fputs(line, recf);
47*7c478bd9Sstevel@tonic-gate 		if (fd)
48*7c478bd9Sstevel@tonic-gate 		{
49*7c478bd9Sstevel@tonic-gate 			sprintf(bkeys, ";%ld", ld);
50*7c478bd9Sstevel@tonic-gate 			ll = strlen(p);
51*7c478bd9Sstevel@tonic-gate 			lt = strlen(bkeys);
52*7c478bd9Sstevel@tonic-gate 			fputs(bkeys, recf);
53*7c478bd9Sstevel@tonic-gate 			sprintf(bkeys, ",%d", ll);
54*7c478bd9Sstevel@tonic-gate 			lt += strlen(bkeys);
55*7c478bd9Sstevel@tonic-gate 			fputs(bkeys, recf);
56*7c478bd9Sstevel@tonic-gate 			ld += ll;
57*7c478bd9Sstevel@tonic-gate 			fputs(p, fd);
58*7c478bd9Sstevel@tonic-gate 		}
59*7c478bd9Sstevel@tonic-gate 		putc('\n',recf);
60*7c478bd9Sstevel@tonic-gate 		for(s=p; *s; s++);
61*7c478bd9Sstevel@tonic-gate 		if (*--s == '\n')
62*7c478bd9Sstevel@tonic-gate 		{
63*7c478bd9Sstevel@tonic-gate 			more=0;
64*7c478bd9Sstevel@tonic-gate 			*s=0;
65*7c478bd9Sstevel@tonic-gate 		}
66*7c478bd9Sstevel@tonic-gate 		else
67*7c478bd9Sstevel@tonic-gate 			more=1;
68*7c478bd9Sstevel@tonic-gate 		assert (fd==0 || more==0);
69*7c478bd9Sstevel@tonic-gate 		nk = getargs(p, keyv);
70*7c478bd9Sstevel@tonic-gate 		if (more)
71*7c478bd9Sstevel@tonic-gate 			nk--;
72*7c478bd9Sstevel@tonic-gate 		for(i=0; i<nk; i++)
73*7c478bd9Sstevel@tonic-gate 			fprintf(outf,"%04d %06ld\n",hash(keyv[i])%nhash, lp);
74*7c478bd9Sstevel@tonic-gate # if D1
75*7c478bd9Sstevel@tonic-gate 		for(i=0; i<nk; i++)
76*7c478bd9Sstevel@tonic-gate 			printf("key %s hash %d\n",keyv[i],hash(keyv[i])%nhash);
77*7c478bd9Sstevel@tonic-gate # endif
78*7c478bd9Sstevel@tonic-gate 		if (more) /* allow more than LINESIZ keys */
79*7c478bd9Sstevel@tonic-gate 		{
80*7c478bd9Sstevel@tonic-gate 			strcpy(key, keyv[nk]);
81*7c478bd9Sstevel@tonic-gate 			for(s=key; *s; s++);
82*7c478bd9Sstevel@tonic-gate 			while ( (c=getc(inf)) != '\n')
83*7c478bd9Sstevel@tonic-gate 			{
84*7c478bd9Sstevel@tonic-gate 				if (c != ' ')
85*7c478bd9Sstevel@tonic-gate 				{
86*7c478bd9Sstevel@tonic-gate 					*s++ = c;
87*7c478bd9Sstevel@tonic-gate 					continue;
88*7c478bd9Sstevel@tonic-gate 				}
89*7c478bd9Sstevel@tonic-gate 				*s=0;
90*7c478bd9Sstevel@tonic-gate 				if (s>key)
91*7c478bd9Sstevel@tonic-gate 					fprintf(outf, "%04d %06ld\n",hash(key)%nhash, lp);
92*7c478bd9Sstevel@tonic-gate 				s = key;
93*7c478bd9Sstevel@tonic-gate 			}
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 		lp += (strlen(line)+lt+1);
96*7c478bd9Sstevel@tonic-gate 		ndoc++;
97*7c478bd9Sstevel@tonic-gate 	}
98*7c478bd9Sstevel@tonic-gate 	*iflong = (lp>=65536L);
99*7c478bd9Sstevel@tonic-gate 	if (sizeof(int)>2) *iflong=1; /* force long on VAX */
100*7c478bd9Sstevel@tonic-gate 	fclose(recf);
101*7c478bd9Sstevel@tonic-gate 	return(ndoc);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate trimnl(p)
105*7c478bd9Sstevel@tonic-gate char *p;
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	while (*p) p++;
108*7c478bd9Sstevel@tonic-gate 	p--;
109*7c478bd9Sstevel@tonic-gate 	if (*p == '\n') *p=0;
110*7c478bd9Sstevel@tonic-gate }
111