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