xref: /illumos-gate/usr/src/cmd/refer/inv2.c (revision 2a8bcb4e)
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 #include <stdio.h>
16 #include <assert.h>
17 #define	LINESIZ 1250
18 
19 extern int hash();
20 
21 int
newkeys(FILE * outf,FILE * inf,FILE * recf,int nhash,FILE * fd,int * iflong)22 newkeys(FILE *outf, FILE *inf, FILE *recf, int nhash, FILE *fd, int *iflong)
23 {
24 	/*
25 	 * reads key lines from inf; hashes and writes on outf;
26 	 * writes orig key on recf, records pointer on outf too.
27 	 * format of outf is : hash code space record pointer
28 	 */
29 
30 	long lp, ftell();
31 	long ld = 0;
32 	int ll = 0, lt = 0;
33 	char line[LINESIZ];
34 	char key[30], bkeys[40];
35 	char *p, *s;
36 	char *keyv[500];
37 	int i, nk, ndoc = 0, more = 0, c;
38 
39 	lp = ftell(recf);
40 	while (fgets(line, LINESIZ, inf)) {
41 		p = line;
42 		while (*p != '\t') p++;
43 		*p++ = 0;
44 		fputs(line, recf);
45 		if (fd) {
46 			sprintf(bkeys, ";%ld", ld);
47 			ll = strlen(p);
48 			lt = strlen(bkeys);
49 			fputs(bkeys, recf);
50 			sprintf(bkeys, ",%d", ll);
51 			lt += strlen(bkeys);
52 			fputs(bkeys, recf);
53 			ld += ll;
54 			fputs(p, fd);
55 		}
56 		putc('\n', recf);
57 		for (s = p; *s; s++)
58 			;
59 		if (*--s == '\n') {
60 			more = 0;
61 			*s = 0;
62 		} else
63 			more = 1;
64 		assert(fd == 0 || more == 0);
65 		nk = getargs(p, keyv);
66 		if (more)
67 			nk--;
68 		for (i = 0; i < nk; i++)
69 			fprintf(outf, "%04d %06ld\n", hash(keyv[i])%nhash, lp);
70 #if D1
71 		for (i = 0; i < nk; i++)
72 			printf("key %s hash %d\n",
73 			    keyv[i], hash(keyv[i])%nhash);
74 #endif
75 		if (more) {	/* allow more than LINESIZ keys */
76 			strcpy(key, keyv[nk]);
77 			for (s = key; *s; s++)
78 				;
79 			while ((c = getc(inf)) != '\n') {
80 				if (c != ' ') {
81 					*s++ = c;
82 					continue;
83 				}
84 				*s = 0;
85 				if (s > key)
86 					fprintf(outf, "%04d %06ld\n",
87 					    hash(key)%nhash, lp);
88 				s = key;
89 			}
90 		}
91 		lp += (strlen(line)+lt+1);
92 		ndoc++;
93 	}
94 	*iflong = (lp >= 65536L);
95 	if (sizeof (int) > 2) *iflong = 1; /* force long on VAX */
96 	fclose(recf);
97 	return (ndoc);
98 }
99 
100 
101 void
trimnl(char * p)102 trimnl(char *p)
103 {
104 	while (*p) p++;
105 	p--;
106 	if (*p == '\n') *p = 0;
107 }
108