Commit | Line | Data |
---|---|---|
513d759c TL |
1 | # include "stdio.h" |
2 | # include "assert.h" | |
3 | extern char refdir[]; | |
4 | extern int keepold; | |
5 | extern char *fgnames[]; | |
6 | extern char **fgnamp; | |
7 | FILE *fd = 0; | |
8 | int lmaster = 500; | |
9 | int *hfreq, hfrflg; | |
10 | int colevel = 0; | |
11 | int soutlen = 1000; | |
12 | int reached = 0; | |
13 | int iflong = 0; | |
14 | int prfreqs = 0; | |
15 | char usedir[100]; | |
16 | char gfile[50]; | |
17 | static int full = 1000; | |
18 | static int tags = 0; | |
19 | char *sinput, *soutput, *tagout; | |
20 | long indexdate = 0, gdate(); | |
21 | extern char *todir(); | |
22 | ||
23 | main(argc,argv) | |
24 | char *argv[]; | |
25 | { | |
26 | /* read query from stdin, expect name of indexes in argv[1] */ | |
27 | static FILE *fa, *fb, *fc; | |
28 | char nma[100], nmb[100], nmc[100], *qitem[100], *rprog = 0; | |
29 | char nmd[100], grepquery[256]; | |
30 | static char oldname[30] ; | |
31 | static int was = 0; | |
32 | /* these pointers are unions of pointer to int and pointer to long */ | |
33 | long *hpt; | |
34 | unsigned *master = 0; | |
35 | int falseflg, nhash, nitem, nfound, frtbl, kk; | |
36 | ||
37 | /* special wart for refpart: default is tags only */ | |
38 | ||
39 | while (argv[1][0] == '-') | |
40 | { | |
41 | switch(argv[1][1]) | |
42 | { | |
43 | case 'a': /* all output, incl. false drops */ | |
44 | falseflg = 1; break; | |
45 | case 'r': | |
46 | argc--; argv++; | |
47 | rprog = argv[1]; | |
48 | break; | |
49 | case 'F': /* put out full text */ | |
50 | full = setfrom(argv[1][2]); | |
51 | break; | |
52 | case 'T': /* put out tags */ | |
53 | tags = setfrom(argv[1][2]); | |
54 | break; | |
55 | case 'i': /* input in argument string */ | |
56 | argc--; argv++; | |
57 | sinput = argv[1]; | |
58 | break; | |
59 | case 's': /*text output to string */ | |
60 | case 'o': | |
61 | argc--; argv++; | |
62 | soutput = argv[1]; | |
63 | if (argv[2]<16000) | |
64 | { | |
65 | soutlen = argv[2]; | |
66 | argc--; argv++; | |
67 | } | |
68 | break; | |
69 | case 't': /*tag output to string */ | |
70 | argc--; argv++; | |
71 | tagout = argv[1]; | |
72 | break; | |
73 | case 'l': /* length of internal lists */ | |
74 | argc--; argv++; | |
75 | lmaster = atoi(argv[1]); | |
76 | break; | |
77 | case 'g': /* suppress fgrep search on old files */ | |
78 | keepold = 0; | |
79 | break; | |
80 | case 'C': /* coordination level */ | |
81 | colevel = atoi(argv[1]+2); | |
82 | # if D1 | |
83 | fprintf(stderr, "colevel set to %d\n",colevel); | |
84 | # endif | |
85 | break; | |
86 | case 'P': /* print term freqs */ | |
87 | prfreqs=1; break; | |
88 | } | |
89 | argc--; argv++; | |
90 | } | |
91 | strcpy (nma, todir(argv[1])); | |
92 | if (was == 0 || strcmp (oldname, nma) !=0) | |
93 | { | |
94 | strcpy (oldname,nma); | |
95 | strcpy (nmb, nma); strcpy (nmc, nmb); strcpy(nmd,nma); | |
96 | strcat (nma, ".ia"); | |
97 | strcat (nmb, ".ib"); | |
98 | strcat (nmc, ".ic"); | |
99 | strcat (nmd, ".id"); | |
100 | if (was) | |
101 | {fclose(fa); fclose(fb); fclose(fc);} | |
102 | ||
103 | fa = fopen(nma, "r"); | |
104 | if (fa==NULL) | |
105 | { | |
106 | err("No files %s",nma); | |
107 | exit(1); | |
108 | } | |
109 | fb = fopen(nmb, "r"); | |
110 | fc = fopen(nmc, "r"); | |
111 | was =1; | |
112 | if (fb== NULL || fc ==NULL) | |
113 | { | |
114 | err("Index incomplete %s", nmb); | |
115 | exit(1); | |
116 | } | |
117 | indexdate = gdate(fb); | |
118 | fd = fopen(nmd, "r"); | |
119 | } | |
120 | fseek (fa, 0L, 0); | |
121 | fread (&nhash, sizeof(nhash), 1, fa); | |
122 | fread (&iflong, sizeof(iflong), 1, fa); | |
123 | if(master==0) | |
124 | master = calloc (lmaster, iflong? sizeof(long): sizeof(int)); | |
125 | hpt = calloc(nhash, sizeof(*hpt)); | |
126 | kk=fread( hpt, sizeof(*hpt), nhash, fa); | |
127 | # if D1 | |
128 | fprintf(stderr,"read %d hashes, iflong %d, nhash %d\n", kk, iflong, nhash); | |
129 | # endif | |
130 | _assert (kk==nhash); | |
131 | hfreq = calloc(nhash, sizeof(*hfreq)); | |
132 | _assert (hfreq != NULL); | |
133 | frtbl = fread(hfreq, sizeof(*hfreq), nhash, fa); | |
134 | hfrflg = (frtbl == nhash); | |
135 | # if D1 | |
136 | fprintf(stderr, "read freqs %d\n", frtbl); | |
137 | # endif | |
138 | ||
139 | while (1) | |
140 | { | |
141 | nitem = getq(qitem); | |
142 | if (nitem==0) continue; | |
143 | if (nitem < 0) break; | |
144 | nfound = doquery(hpt, nhash, fb, nitem, qitem, master); | |
145 | # if D1 | |
146 | fprintf(stderr,"after doquery nfound %d\n", nfound); | |
147 | # endif | |
148 | fgnamp=fgnames; | |
149 | if (falseflg == 0) | |
150 | nfound = baddrop(master, nfound, fc, nitem, qitem, rprog, full); | |
151 | # if D1 | |
152 | fprintf(stderr,"after baddrop nfound %d\n", nfound); | |
153 | # endif | |
154 | if (fgnamp>fgnames) | |
155 | { | |
156 | char **fgp, tgbuff[100]; | |
157 | int k; | |
158 | # if D1 | |
159 | fprintf(stderr, "were %d bad files\n", fgnamp-fgnames); | |
160 | # endif | |
161 | grepquery[0]=0; | |
162 | for(k=0; k<nitem; k++) | |
163 | { | |
164 | strcat(grepquery, " "); | |
165 | strcat(grepquery, qitem[k]); | |
166 | } | |
167 | # if D1 | |
168 | fprintf(stderr, "grepquery %s\n",grepquery); | |
169 | # endif | |
170 | for(fgp=fgnames; fgp<fgnamp; fgp++) | |
171 | { | |
172 | # if D1 | |
173 | fprintf(stderr, "Now on %s query /%s/\n", *fgp, grepquery); | |
174 | # endif | |
175 | makefgrep(*fgp); | |
176 | # if D1 | |
177 | fprintf(stderr, "grepmade\n"); | |
178 | # endif | |
179 | if (tagout==0) | |
180 | tagout=tgbuff; | |
181 | grepcall(grepquery, tagout, *fgp); | |
182 | # if D1 | |
183 | fprintf(stderr, "tagout now /%s/\n", tagout); | |
184 | # endif | |
185 | if (full) | |
186 | { | |
187 | char bout[1000]; | |
188 | findline(tagout, bout, 1000); | |
189 | fputs(bout,stdout); | |
190 | } | |
191 | } | |
192 | } | |
193 | if (tags) | |
194 | result (master, nfound >tags ? tags: nfound, fc); | |
195 | } | |
196 | } | |
197 | ||
198 | char * | |
199 | todir(t) | |
200 | char *t; | |
201 | { | |
202 | char *s; | |
203 | s=t; | |
204 | while (*s) s++; | |
205 | while (s>=t && *s != '/') s--; | |
206 | if (s<t) return(t); | |
207 | *s++ = 0; | |
208 | t = (*t ? t : "/"); | |
209 | chdir (t); | |
210 | strcpy (usedir,t); | |
211 | return(s); | |
212 | } | |
213 | setfrom(c) | |
214 | { | |
215 | switch(c) | |
216 | { | |
217 | case 'y': case '\0': | |
218 | default: | |
219 | return(1000); | |
220 | case '1': | |
221 | case '2': case '3': case '4': case '5': | |
222 | case '6': case '7': case '8': case '9': | |
223 | return(c-'0'); | |
224 | case 'n': case '0': | |
225 | return(0); | |
226 | } | |
227 | } |