xref: /illumos-gate/usr/src/cmd/refer/inv2.c (revision 2a8bcb4e)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <stdio.h>
167c478bd9Sstevel@tonic-gate #include <assert.h>
17*11a8fa6cSceastha #define	LINESIZ 1250
18*11a8fa6cSceastha 
19*11a8fa6cSceastha extern int hash();
207c478bd9Sstevel@tonic-gate 
21*11a8fa6cSceastha int
newkeys(FILE * outf,FILE * inf,FILE * recf,int nhash,FILE * fd,int * iflong)22*11a8fa6cSceastha newkeys(FILE *outf, FILE *inf, FILE *recf, int nhash, FILE *fd, int *iflong)
237c478bd9Sstevel@tonic-gate {
24*11a8fa6cSceastha 	/*
25*11a8fa6cSceastha 	 * reads key lines from inf; hashes and writes on outf;
267c478bd9Sstevel@tonic-gate 	 * writes orig key on recf, records pointer on outf too.
277c478bd9Sstevel@tonic-gate 	 * format of outf is : hash code space record pointer
287c478bd9Sstevel@tonic-gate 	 */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 	long lp, ftell();
31*11a8fa6cSceastha 	long ld = 0;
327c478bd9Sstevel@tonic-gate 	int ll = 0, lt = 0;
337c478bd9Sstevel@tonic-gate 	char line[LINESIZ];
347c478bd9Sstevel@tonic-gate 	char key[30], bkeys[40];
357c478bd9Sstevel@tonic-gate 	char *p, *s;
367c478bd9Sstevel@tonic-gate 	char *keyv[500];
377c478bd9Sstevel@tonic-gate 	int i, nk, ndoc = 0, more = 0, c;
387c478bd9Sstevel@tonic-gate 
39*11a8fa6cSceastha 	lp = ftell(recf);
40*11a8fa6cSceastha 	while (fgets(line, LINESIZ, inf)) {
417c478bd9Sstevel@tonic-gate 		p = line;
427c478bd9Sstevel@tonic-gate 		while (*p != '\t') p++;
43*11a8fa6cSceastha 		*p++ = 0;
447c478bd9Sstevel@tonic-gate 		fputs(line, recf);
45*11a8fa6cSceastha 		if (fd) {
467c478bd9Sstevel@tonic-gate 			sprintf(bkeys, ";%ld", ld);
477c478bd9Sstevel@tonic-gate 			ll = strlen(p);
487c478bd9Sstevel@tonic-gate 			lt = strlen(bkeys);
497c478bd9Sstevel@tonic-gate 			fputs(bkeys, recf);
507c478bd9Sstevel@tonic-gate 			sprintf(bkeys, ",%d", ll);
517c478bd9Sstevel@tonic-gate 			lt += strlen(bkeys);
527c478bd9Sstevel@tonic-gate 			fputs(bkeys, recf);
537c478bd9Sstevel@tonic-gate 			ld += ll;
547c478bd9Sstevel@tonic-gate 			fputs(p, fd);
557c478bd9Sstevel@tonic-gate 		}
56*11a8fa6cSceastha 		putc('\n', recf);
57*11a8fa6cSceastha 		for (s = p; *s; s++)
58*11a8fa6cSceastha 			;
59*11a8fa6cSceastha 		if (*--s == '\n') {
60*11a8fa6cSceastha 			more = 0;
61*11a8fa6cSceastha 			*s = 0;
62*11a8fa6cSceastha 		} else
63*11a8fa6cSceastha 			more = 1;
64*11a8fa6cSceastha 		assert(fd == 0 || more == 0);
657c478bd9Sstevel@tonic-gate 		nk = getargs(p, keyv);
667c478bd9Sstevel@tonic-gate 		if (more)
677c478bd9Sstevel@tonic-gate 			nk--;
68*11a8fa6cSceastha 		for (i = 0; i < nk; i++)
69*11a8fa6cSceastha 			fprintf(outf, "%04d %06ld\n", hash(keyv[i])%nhash, lp);
70*11a8fa6cSceastha #if D1
71*11a8fa6cSceastha 		for (i = 0; i < nk; i++)
72*11a8fa6cSceastha 			printf("key %s hash %d\n",
73*11a8fa6cSceastha 			    keyv[i], hash(keyv[i])%nhash);
74*11a8fa6cSceastha #endif
75*11a8fa6cSceastha 		if (more) {	/* allow more than LINESIZ keys */
767c478bd9Sstevel@tonic-gate 			strcpy(key, keyv[nk]);
77*11a8fa6cSceastha 			for (s = key; *s; s++)
78*11a8fa6cSceastha 				;
79*11a8fa6cSceastha 			while ((c = getc(inf)) != '\n') {
80*11a8fa6cSceastha 				if (c != ' ') {
817c478bd9Sstevel@tonic-gate 					*s++ = c;
827c478bd9Sstevel@tonic-gate 					continue;
837c478bd9Sstevel@tonic-gate 				}
84*11a8fa6cSceastha 				*s = 0;
85*11a8fa6cSceastha 				if (s > key)
86*11a8fa6cSceastha 					fprintf(outf, "%04d %06ld\n",
87*11a8fa6cSceastha 					    hash(key)%nhash, lp);
887c478bd9Sstevel@tonic-gate 				s = key;
897c478bd9Sstevel@tonic-gate 			}
907c478bd9Sstevel@tonic-gate 		}
917c478bd9Sstevel@tonic-gate 		lp += (strlen(line)+lt+1);
927c478bd9Sstevel@tonic-gate 		ndoc++;
937c478bd9Sstevel@tonic-gate 	}
94*11a8fa6cSceastha 	*iflong = (lp >= 65536L);
95*11a8fa6cSceastha 	if (sizeof (int) > 2) *iflong = 1; /* force long on VAX */
967c478bd9Sstevel@tonic-gate 	fclose(recf);
97*11a8fa6cSceastha 	return (ndoc);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
100*11a8fa6cSceastha 
101*11a8fa6cSceastha void
trimnl(char * p)102*11a8fa6cSceastha trimnl(char *p)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	while (*p) p++;
1057c478bd9Sstevel@tonic-gate 	p--;
106*11a8fa6cSceastha 	if (*p == '\n') *p = 0;
1077c478bd9Sstevel@tonic-gate }
108