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
15*11a8fa6cSceastha #include <locale.h>
16*11a8fa6cSceastha #include <stdio.h>
17*11a8fa6cSceastha #include <assert.h>
187c478bd9Sstevel@tonic-gate extern char refdir[];
197c478bd9Sstevel@tonic-gate extern int keepold;
207c478bd9Sstevel@tonic-gate extern char *fgnames[];
217c478bd9Sstevel@tonic-gate extern char **fgnamp;
22*11a8fa6cSceastha FILE *fd = NULL;
23*11a8fa6cSceastha int lmaster = 500;
247c478bd9Sstevel@tonic-gate int *hfreq, hfrflg;
25*11a8fa6cSceastha int colevel = 0;
26*11a8fa6cSceastha int measure = 0;
27*11a8fa6cSceastha int soutlen = 1000;
28*11a8fa6cSceastha int reached = 0;
29*11a8fa6cSceastha int iflong = 0;
30*11a8fa6cSceastha int prfreqs = 0;
317c478bd9Sstevel@tonic-gate char usedir[100];
32*11a8fa6cSceastha char *calloc();
33*11a8fa6cSceastha char *todir();
347c478bd9Sstevel@tonic-gate char gfile[50];
35*11a8fa6cSceastha static int full = 1000;
36*11a8fa6cSceastha static int tags = 0;
377c478bd9Sstevel@tonic-gate char *sinput, *soutput, *tagout;
38*11a8fa6cSceastha long indexdate = 0, gdate();
39*11a8fa6cSceastha
40*11a8fa6cSceastha extern int baddrop();
41*11a8fa6cSceastha extern int doquery();
42*11a8fa6cSceastha extern void err();
43*11a8fa6cSceastha extern long findline();
44*11a8fa6cSceastha extern int getq();
45*11a8fa6cSceastha extern void grepcall();
46*11a8fa6cSceastha extern int makefgrep();
47*11a8fa6cSceastha extern void result();
48*11a8fa6cSceastha extern void tick();
49*11a8fa6cSceastha extern void tock();
507c478bd9Sstevel@tonic-gate
51*11a8fa6cSceastha static int setfrom(char);
52*11a8fa6cSceastha
53*11a8fa6cSceastha int
main(int argc,char * argv[])54*11a8fa6cSceastha main(int argc, char *argv[])
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate /* read query from stdin, expect name of indexes in argv[1] */
577c478bd9Sstevel@tonic-gate static FILE *fa, *fb, *fc;
587c478bd9Sstevel@tonic-gate char nma[100], nmb[100], nmc[100], *qitem[100], *rprog = NULL;
597c478bd9Sstevel@tonic-gate char nmd[100], grepquery[256];
60*11a8fa6cSceastha static char oldname[30];
61*11a8fa6cSceastha static int was = 0;
627c478bd9Sstevel@tonic-gate /* these pointers are unions of pointer to int and pointer to long */
637c478bd9Sstevel@tonic-gate long *hpt;
64*11a8fa6cSceastha unsigned *master = 0;
657c478bd9Sstevel@tonic-gate int falseflg, nhash, nitem, nfound, frtbl, kk;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /* special wart for refpart: default is tags only */
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
72*11a8fa6cSceastha #define TEXT_DOMAIN "SYS_TEST"
737c478bd9Sstevel@tonic-gate #endif
747c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate falseflg = 0;
777c478bd9Sstevel@tonic-gate
78*11a8fa6cSceastha while (argc > 1 && argv[1][0] == '-') {
79*11a8fa6cSceastha switch (argv[1][1]) {
807c478bd9Sstevel@tonic-gate case 'a': /* all output, incl. false drops */
81*11a8fa6cSceastha falseflg = 1;
827c478bd9Sstevel@tonic-gate break;
837c478bd9Sstevel@tonic-gate case 'r':
84*11a8fa6cSceastha argc--;
857c478bd9Sstevel@tonic-gate argv++;
867c478bd9Sstevel@tonic-gate rprog = argv[1];
877c478bd9Sstevel@tonic-gate break;
887c478bd9Sstevel@tonic-gate case 'F': /* put out full text */
897c478bd9Sstevel@tonic-gate full = setfrom(argv[1][2]);
907c478bd9Sstevel@tonic-gate break;
917c478bd9Sstevel@tonic-gate case 'T': /* put out tags */
927c478bd9Sstevel@tonic-gate tags = setfrom(argv[1][2]);
937c478bd9Sstevel@tonic-gate break;
947c478bd9Sstevel@tonic-gate case 'i': /* input in argument string */
95*11a8fa6cSceastha argc--;
967c478bd9Sstevel@tonic-gate argv++;
977c478bd9Sstevel@tonic-gate sinput = argv[1];
987c478bd9Sstevel@tonic-gate break;
99*11a8fa6cSceastha case 's': /* text output to string */
1007c478bd9Sstevel@tonic-gate case 'o':
101*11a8fa6cSceastha argc--;
1027c478bd9Sstevel@tonic-gate argv++;
1037c478bd9Sstevel@tonic-gate soutput = argv[1];
104*11a8fa6cSceastha if ((int)argv[2] < 16000) {
1057c478bd9Sstevel@tonic-gate soutlen = (int)argv[2];
106*11a8fa6cSceastha argc--;
1077c478bd9Sstevel@tonic-gate argv++;
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate break;
110*11a8fa6cSceastha case 't': /* tag output to string */
111*11a8fa6cSceastha argc--;
1127c478bd9Sstevel@tonic-gate argv++;
1137c478bd9Sstevel@tonic-gate tagout = argv[1];
1147c478bd9Sstevel@tonic-gate break;
1157c478bd9Sstevel@tonic-gate case 'l': /* length of internal lists */
116*11a8fa6cSceastha argc--;
1177c478bd9Sstevel@tonic-gate argv++;
1187c478bd9Sstevel@tonic-gate lmaster = atoi(argv[1]);
1197c478bd9Sstevel@tonic-gate break;
1207c478bd9Sstevel@tonic-gate case 'g': /* suppress fgrep search on old files */
1217c478bd9Sstevel@tonic-gate keepold = 0;
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate case 'C': /* coordination level */
1247c478bd9Sstevel@tonic-gate colevel = atoi(argv[1]+2);
125*11a8fa6cSceastha #if D1
126*11a8fa6cSceastha fprintf(stderr, "colevel set to %d\n", colevel);
127*11a8fa6cSceastha #endif
1287c478bd9Sstevel@tonic-gate break;
1297c478bd9Sstevel@tonic-gate case 'P': /* print term freqs */
130*11a8fa6cSceastha prfreqs = 1;
1317c478bd9Sstevel@tonic-gate break;
1327c478bd9Sstevel@tonic-gate case 'm':
133*11a8fa6cSceastha measure = 1;
1347c478bd9Sstevel@tonic-gate break;
1357c478bd9Sstevel@tonic-gate }
136*11a8fa6cSceastha argc--;
1377c478bd9Sstevel@tonic-gate argv++;
1387c478bd9Sstevel@tonic-gate }
139*11a8fa6cSceastha if (argc < 2)
1407c478bd9Sstevel@tonic-gate exit(1);
141*11a8fa6cSceastha strcpy(nma, todir(argv[1]));
142*11a8fa6cSceastha if (was == 0 || strcmp(oldname, nma) != 0) {
143*11a8fa6cSceastha strcpy(oldname, nma);
144*11a8fa6cSceastha strcpy(nmb, nma);
145*11a8fa6cSceastha strcpy(nmc, nmb);
146*11a8fa6cSceastha strcpy(nmd, nma);
147*11a8fa6cSceastha strcat(nma, ".ia");
148*11a8fa6cSceastha strcat(nmb, ".ib");
149*11a8fa6cSceastha strcat(nmc, ".ic");
150*11a8fa6cSceastha strcat(nmd, ".id");
151*11a8fa6cSceastha if (was) {
152*11a8fa6cSceastha fclose(fa);
153*11a8fa6cSceastha fclose(fb);
1547c478bd9Sstevel@tonic-gate fclose(fc);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate fa = fopen(nma, "r");
158*11a8fa6cSceastha if (fa == NULL) {
159*11a8fa6cSceastha strcpy(*fgnamp++ = calloc(strlen(oldname)+2, 1),
160*11a8fa6cSceastha oldname);
161*11a8fa6cSceastha fb = NULL;
1627c478bd9Sstevel@tonic-gate goto search;
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate fb = fopen(nmb, "r");
1657c478bd9Sstevel@tonic-gate fc = fopen(nmc, "r");
166*11a8fa6cSceastha was = 1;
167*11a8fa6cSceastha if (fb == NULL || fc == NULL) {
1687c478bd9Sstevel@tonic-gate err(gettext("Index incomplete %s"), nmb);
1697c478bd9Sstevel@tonic-gate exit(1);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate indexdate = gdate(fb);
1727c478bd9Sstevel@tonic-gate fd = fopen(nmd, "r");
1737c478bd9Sstevel@tonic-gate }
174*11a8fa6cSceastha fseek(fa, 0L, 0);
175*11a8fa6cSceastha fread(&nhash, sizeof (nhash), 1, fa);
176*11a8fa6cSceastha fread(&iflong, sizeof (iflong), 1, fa);
177*11a8fa6cSceastha if (master == 0)
178*11a8fa6cSceastha master = (unsigned *)calloc(lmaster, iflong ?
179*11a8fa6cSceastha sizeof (long) : sizeof (unsigned));
180*11a8fa6cSceastha hpt = (long *)calloc(nhash, sizeof (*hpt));
181*11a8fa6cSceastha kk = fread(hpt, sizeof (*hpt), nhash, fa);
182*11a8fa6cSceastha #if D1
183*11a8fa6cSceastha fprintf(stderr, "read %d hashes, iflong %d, nhash %d\n",
184*11a8fa6cSceastha kk, iflong, nhash);
185*11a8fa6cSceastha #endif
186*11a8fa6cSceastha assert(kk == nhash);
187*11a8fa6cSceastha hfreq = (int *)calloc(nhash, sizeof (*hfreq));
188*11a8fa6cSceastha assert(hfreq != NULL);
189*11a8fa6cSceastha frtbl = fread(hfreq, sizeof (*hfreq), nhash, fa);
1907c478bd9Sstevel@tonic-gate hfrflg = (frtbl == nhash);
191*11a8fa6cSceastha #if D1
1927c478bd9Sstevel@tonic-gate fprintf(stderr, "read freqs %d\n", frtbl);
193*11a8fa6cSceastha #endif
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate search:
196*11a8fa6cSceastha while (1) {
1977c478bd9Sstevel@tonic-gate nitem = getq(qitem);
1987c478bd9Sstevel@tonic-gate if (measure) tick();
199*11a8fa6cSceastha if (nitem == 0) continue;
2007c478bd9Sstevel@tonic-gate if (nitem < 0) break;
201*11a8fa6cSceastha if (tagout) tagout[0] = 0;
202*11a8fa6cSceastha if (fb != NULL) {
2037c478bd9Sstevel@tonic-gate nfound = doquery(hpt, nhash, fb, nitem, qitem, master);
204*11a8fa6cSceastha #if D1
205*11a8fa6cSceastha fprintf(stderr, "after doquery nfound %d\n", nfound);
206*11a8fa6cSceastha #endif
207*11a8fa6cSceastha fgnamp = fgnames;
2087c478bd9Sstevel@tonic-gate if (falseflg == 0)
209*11a8fa6cSceastha nfound = baddrop(master, nfound, fc,
210*11a8fa6cSceastha nitem, qitem, rprog, full);
211*11a8fa6cSceastha #if D1
212*11a8fa6cSceastha fprintf(stderr, "after baddrop nfound %d\n", nfound);
213*11a8fa6cSceastha #endif
2147c478bd9Sstevel@tonic-gate }
215*11a8fa6cSceastha if (fgnamp > fgnames) {
2167c478bd9Sstevel@tonic-gate char **fgp, tgbuff[100];
2177c478bd9Sstevel@tonic-gate int k;
218*11a8fa6cSceastha #if D1
2197c478bd9Sstevel@tonic-gate fprintf(stderr, "were %d bad files\n", fgnamp-fgnames);
220*11a8fa6cSceastha #endif
2217c478bd9Sstevel@tonic-gate (void) memset(tgbuff, 0, sizeof (tgbuff));
222*11a8fa6cSceastha grepquery[0] = 0;
223*11a8fa6cSceastha for (k = 0; k < nitem; k++) {
2247c478bd9Sstevel@tonic-gate strcat(grepquery, " ");
2257c478bd9Sstevel@tonic-gate strcat(grepquery, qitem[k]);
2267c478bd9Sstevel@tonic-gate }
227*11a8fa6cSceastha #if D1
228*11a8fa6cSceastha fprintf(stderr, "grepquery %s\n", grepquery);
229*11a8fa6cSceastha #endif
230*11a8fa6cSceastha for (fgp = fgnames; fgp < fgnamp; fgp++) {
231*11a8fa6cSceastha #if D1
232*11a8fa6cSceastha fprintf(stderr, "Now on %s query /%s/\n",
233*11a8fa6cSceastha *fgp, grepquery);
234*11a8fa6cSceastha #endif
2357c478bd9Sstevel@tonic-gate makefgrep(*fgp);
236*11a8fa6cSceastha #if D1
2377c478bd9Sstevel@tonic-gate fprintf(stderr, "grepmade\n");
238*11a8fa6cSceastha #endif
239*11a8fa6cSceastha if (tagout == 0)
240*11a8fa6cSceastha tagout = tgbuff;
2417c478bd9Sstevel@tonic-gate grepcall(grepquery, tagout, *fgp);
242*11a8fa6cSceastha #if D1
2437c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout now /%s/\n", tagout);
244*11a8fa6cSceastha #endif
245*11a8fa6cSceastha if (full) {
2467c478bd9Sstevel@tonic-gate int nout;
2477c478bd9Sstevel@tonic-gate char *bout;
2487c478bd9Sstevel@tonic-gate char *tagp;
2497c478bd9Sstevel@tonic-gate char *oldtagp;
2507c478bd9Sstevel@tonic-gate tagp = tagout;
2517c478bd9Sstevel@tonic-gate while (*tagp) {
2527c478bd9Sstevel@tonic-gate oldtagp = tagp;
253*11a8fa6cSceastha while (*tagp &&
254*11a8fa6cSceastha (*tagp != '\n'))
2557c478bd9Sstevel@tonic-gate tagp++;
256*11a8fa6cSceastha if (*tagp)
2577c478bd9Sstevel@tonic-gate tagp++;
258*11a8fa6cSceastha nout = findline(oldtagp, &bout,
259*11a8fa6cSceastha 1000, 0L);
260*11a8fa6cSceastha if (nout > 0) {
2617c478bd9Sstevel@tonic-gate fputs(bout, stdout);
262*11a8fa6cSceastha free(bout);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate if (tags)
269*11a8fa6cSceastha result(master, nfound > tags ? tags : nfound, fc);
2707c478bd9Sstevel@tonic-gate if (measure) tock();
2717c478bd9Sstevel@tonic-gate }
272*11a8fa6cSceastha return (0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate char *
todir(char * t)276*11a8fa6cSceastha todir(char *t)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate char *s;
279*11a8fa6cSceastha s = t;
2807c478bd9Sstevel@tonic-gate while (*s) s++;
281*11a8fa6cSceastha while (s >= t && *s != '/') s--;
282*11a8fa6cSceastha if (s < t)
283*11a8fa6cSceastha return (t);
2847c478bd9Sstevel@tonic-gate *s++ = 0;
2857c478bd9Sstevel@tonic-gate t = (*t ? t : "/");
286*11a8fa6cSceastha chdir(t);
287*11a8fa6cSceastha strcpy(usedir, t);
288*11a8fa6cSceastha return (s);
2897c478bd9Sstevel@tonic-gate }
290*11a8fa6cSceastha
291*11a8fa6cSceastha static int
setfrom(char c)292*11a8fa6cSceastha setfrom(char c)
2937c478bd9Sstevel@tonic-gate {
294*11a8fa6cSceastha switch (c) {
295*11a8fa6cSceastha case 'y':
2967c478bd9Sstevel@tonic-gate case '\0':
2977c478bd9Sstevel@tonic-gate default:
298*11a8fa6cSceastha return (1000);
2997c478bd9Sstevel@tonic-gate case '1':
300*11a8fa6cSceastha case '2':
301*11a8fa6cSceastha case '3':
302*11a8fa6cSceastha case '4':
3037c478bd9Sstevel@tonic-gate case '5':
304*11a8fa6cSceastha case '6':
305*11a8fa6cSceastha case '7':
306*11a8fa6cSceastha case '8':
3077c478bd9Sstevel@tonic-gate case '9':
308*11a8fa6cSceastha return (c-'0');
309*11a8fa6cSceastha case 'n':
3107c478bd9Sstevel@tonic-gate case '0':
311*11a8fa6cSceastha return (0);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate }
314