xref: /titanic_44/usr/src/cmd/refer/inv2.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <assert.h>
197c478bd9Sstevel@tonic-gate #define	LINESIZ 1250
207c478bd9Sstevel@tonic-gate 
21*11a8fa6cSceastha extern int hash();
22*11a8fa6cSceastha 
23*11a8fa6cSceastha int
newkeys(FILE * outf,FILE * inf,FILE * recf,int nhash,FILE * fd,int * iflong)24*11a8fa6cSceastha newkeys(FILE *outf, FILE *inf, FILE *recf, int nhash, FILE *fd, int *iflong)
257c478bd9Sstevel@tonic-gate {
26*11a8fa6cSceastha 	/*
27*11a8fa6cSceastha 	 * reads key lines from inf; hashes and writes on outf;
287c478bd9Sstevel@tonic-gate 	 * writes orig key on recf, records pointer on outf too.
297c478bd9Sstevel@tonic-gate 	 * format of outf is : hash code space record pointer
307c478bd9Sstevel@tonic-gate 	 */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate 	long lp, ftell();
337c478bd9Sstevel@tonic-gate 	long ld = 0;
347c478bd9Sstevel@tonic-gate 	int ll = 0, lt = 0;
357c478bd9Sstevel@tonic-gate 	char line[LINESIZ];
367c478bd9Sstevel@tonic-gate 	char key[30], bkeys[40];
377c478bd9Sstevel@tonic-gate 	char *p, *s;
387c478bd9Sstevel@tonic-gate 	char *keyv[500];
397c478bd9Sstevel@tonic-gate 	int i, nk, ndoc = 0, more = 0, c;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	lp = ftell(recf);
42*11a8fa6cSceastha 	while (fgets(line, LINESIZ, inf)) {
437c478bd9Sstevel@tonic-gate 		p = line;
447c478bd9Sstevel@tonic-gate 		while (*p != '\t') p++;
457c478bd9Sstevel@tonic-gate 		*p++ = 0;
467c478bd9Sstevel@tonic-gate 		fputs(line, recf);
47*11a8fa6cSceastha 		if (fd) {
487c478bd9Sstevel@tonic-gate 			sprintf(bkeys, ";%ld", ld);
497c478bd9Sstevel@tonic-gate 			ll = strlen(p);
507c478bd9Sstevel@tonic-gate 			lt = strlen(bkeys);
517c478bd9Sstevel@tonic-gate 			fputs(bkeys, recf);
527c478bd9Sstevel@tonic-gate 			sprintf(bkeys, ",%d", ll);
537c478bd9Sstevel@tonic-gate 			lt += strlen(bkeys);
547c478bd9Sstevel@tonic-gate 			fputs(bkeys, recf);
557c478bd9Sstevel@tonic-gate 			ld += ll;
567c478bd9Sstevel@tonic-gate 			fputs(p, fd);
577c478bd9Sstevel@tonic-gate 		}
587c478bd9Sstevel@tonic-gate 		putc('\n', recf);
59*11a8fa6cSceastha 		for (s = p; *s; s++)
60*11a8fa6cSceastha 			;
61*11a8fa6cSceastha 		if (*--s == '\n') {
627c478bd9Sstevel@tonic-gate 			more = 0;
637c478bd9Sstevel@tonic-gate 			*s = 0;
64*11a8fa6cSceastha 		} else
657c478bd9Sstevel@tonic-gate 			more = 1;
667c478bd9Sstevel@tonic-gate 		assert(fd == 0 || more == 0);
677c478bd9Sstevel@tonic-gate 		nk = getargs(p, keyv);
687c478bd9Sstevel@tonic-gate 		if (more)
697c478bd9Sstevel@tonic-gate 			nk--;
707c478bd9Sstevel@tonic-gate 		for (i = 0; i < nk; i++)
717c478bd9Sstevel@tonic-gate 			fprintf(outf, "%04d %06ld\n", hash(keyv[i])%nhash, lp);
727c478bd9Sstevel@tonic-gate #if D1
737c478bd9Sstevel@tonic-gate 		for (i = 0; i < nk; i++)
74*11a8fa6cSceastha 			printf("key %s hash %d\n",
75*11a8fa6cSceastha 			    keyv[i], hash(keyv[i])%nhash);
767c478bd9Sstevel@tonic-gate #endif
77*11a8fa6cSceastha 		if (more) {	/* allow more than LINESIZ keys */
787c478bd9Sstevel@tonic-gate 			strcpy(key, keyv[nk]);
79*11a8fa6cSceastha 			for (s = key; *s; s++)
80*11a8fa6cSceastha 				;
81*11a8fa6cSceastha 			while ((c = getc(inf)) != '\n') {
82*11a8fa6cSceastha 				if (c != ' ') {
837c478bd9Sstevel@tonic-gate 					*s++ = c;
847c478bd9Sstevel@tonic-gate 					continue;
857c478bd9Sstevel@tonic-gate 				}
867c478bd9Sstevel@tonic-gate 				*s = 0;
877c478bd9Sstevel@tonic-gate 				if (s > key)
88*11a8fa6cSceastha 					fprintf(outf, "%04d %06ld\n",
89*11a8fa6cSceastha 					    hash(key)%nhash, lp);
907c478bd9Sstevel@tonic-gate 				s = key;
917c478bd9Sstevel@tonic-gate 			}
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		lp += (strlen(line)+lt+1);
947c478bd9Sstevel@tonic-gate 		ndoc++;
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	*iflong = (lp >= 65536L);
977c478bd9Sstevel@tonic-gate 	if (sizeof (int) > 2) *iflong = 1; /* force long on VAX */
987c478bd9Sstevel@tonic-gate 	fclose(recf);
997c478bd9Sstevel@tonic-gate 	return (ndoc);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
102*11a8fa6cSceastha 
103*11a8fa6cSceastha void
trimnl(char * p)104*11a8fa6cSceastha trimnl(char *p)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	while (*p) p++;
1077c478bd9Sstevel@tonic-gate 	p--;
1087c478bd9Sstevel@tonic-gate 	if (*p == '\n') *p = 0;
1097c478bd9Sstevel@tonic-gate }
110