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> 18*11a8fa6cSceastha 197c478bd9Sstevel@tonic-gate #define unopen(fil) {if (fil != NULL) {fclose(fil); fil = NULL; }} 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate extern char refdir[]; 227c478bd9Sstevel@tonic-gate int lmaster = 1000; 237c478bd9Sstevel@tonic-gate int reached = 0; 247c478bd9Sstevel@tonic-gate FILE *fd = 0; 257c478bd9Sstevel@tonic-gate int *hfreq, hfrflg; 267c478bd9Sstevel@tonic-gate int colevel = 0; 277c478bd9Sstevel@tonic-gate static union firetruck { 287c478bd9Sstevel@tonic-gate unsigned *a; 297c478bd9Sstevel@tonic-gate long *b; 307c478bd9Sstevel@tonic-gate } master; 317c478bd9Sstevel@tonic-gate int iflong; 327c478bd9Sstevel@tonic-gate extern char *fgnames[], **fgnamp; 337c478bd9Sstevel@tonic-gate extern FILE *iopen(); 347c478bd9Sstevel@tonic-gate int prfreqs = 0; 357c478bd9Sstevel@tonic-gate int typeindex = 0; 367c478bd9Sstevel@tonic-gate char usedir[100]; 377c478bd9Sstevel@tonic-gate static int full = 1000; 387c478bd9Sstevel@tonic-gate static int tags = 0; 397c478bd9Sstevel@tonic-gate char *sinput, *soutput, *tagout; 407c478bd9Sstevel@tonic-gate long indexdate = 0, gdate(); 417c478bd9Sstevel@tonic-gate int soutlen = 1000; 427c478bd9Sstevel@tonic-gate int taglen = 1000; 437c478bd9Sstevel@tonic-gate 44*11a8fa6cSceastha extern int baddrop(); 45*11a8fa6cSceastha extern int ckexist(); 46*11a8fa6cSceastha extern int doquery(); 47*11a8fa6cSceastha extern void err(); 48*11a8fa6cSceastha extern int getq(); 49*11a8fa6cSceastha extern void grepcall(); 50*11a8fa6cSceastha extern int makefgrep(); 51*11a8fa6cSceastha extern void restodir(); 52*11a8fa6cSceastha extern void result(); 53*11a8fa6cSceastha extern void savedir(); 54*11a8fa6cSceastha extern void *zalloc(); 55*11a8fa6cSceastha 56*11a8fa6cSceastha static int setfrom(char); 57*11a8fa6cSceastha char *todir(char *); 58*11a8fa6cSceastha 59*11a8fa6cSceastha void 60*11a8fa6cSceastha huntmain(int argc, char *argv[]) 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate /* read query from stdin, expect name of indexes in argv[1] */ 637c478bd9Sstevel@tonic-gate static FILE *fa, *fb, *fc; 647c478bd9Sstevel@tonic-gate char indexname[100], *qitem[100], *rprog = 0; 657c478bd9Sstevel@tonic-gate char grepquery[200]; 667c478bd9Sstevel@tonic-gate static char oldname[30]; 677c478bd9Sstevel@tonic-gate static int nhash = 0; 687c478bd9Sstevel@tonic-gate static int maxhash = 0; 697c478bd9Sstevel@tonic-gate int falseflg = 0, nitem, nfound, frtbl; 707c478bd9Sstevel@tonic-gate static long *hpt = 0; 717c478bd9Sstevel@tonic-gate unsigned *masterp; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate #if D1 74*11a8fa6cSceastha fprintf(stderr, "in glue1 argc %d argv %o %o\n", 75*11a8fa6cSceastha argc, argv[0], argv[1]); 767c478bd9Sstevel@tonic-gate #endif 777c478bd9Sstevel@tonic-gate savedir(); 78*11a8fa6cSceastha while (argv[1][0] == '-') { 797c478bd9Sstevel@tonic-gate #if D1 807c478bd9Sstevel@tonic-gate fprintf(stderr, "argv.1 is %s\n", argv[1]); 817c478bd9Sstevel@tonic-gate #endif 82*11a8fa6cSceastha switch (argv[1][1]) { 837c478bd9Sstevel@tonic-gate case 'a': /* all output, incl. false drops */ 847c478bd9Sstevel@tonic-gate falseflg = 1; 857c478bd9Sstevel@tonic-gate break; 867c478bd9Sstevel@tonic-gate case 'r': 877c478bd9Sstevel@tonic-gate argc--; 887c478bd9Sstevel@tonic-gate argv++; 897c478bd9Sstevel@tonic-gate rprog = argv[1]; 907c478bd9Sstevel@tonic-gate break; 917c478bd9Sstevel@tonic-gate case 'F': /* put out full text */ 927c478bd9Sstevel@tonic-gate full = setfrom(argv[1][2]); 937c478bd9Sstevel@tonic-gate break; 947c478bd9Sstevel@tonic-gate case 'T': /* put out tags */ 957c478bd9Sstevel@tonic-gate tags = setfrom(argv[1][2]); 967c478bd9Sstevel@tonic-gate break; 977c478bd9Sstevel@tonic-gate case 'i': /* input in argument string */ 987c478bd9Sstevel@tonic-gate argc--; 997c478bd9Sstevel@tonic-gate argv++; 1007c478bd9Sstevel@tonic-gate sinput = argv[1]; 1017c478bd9Sstevel@tonic-gate break; 1027c478bd9Sstevel@tonic-gate case 's': /* text output to string */ 1037c478bd9Sstevel@tonic-gate case 'o': 1047c478bd9Sstevel@tonic-gate argc--; 1057c478bd9Sstevel@tonic-gate argv++; 1067c478bd9Sstevel@tonic-gate soutput = argv[1]; 107*11a8fa6cSceastha if ((int)argv[2] < 16000) { 1087c478bd9Sstevel@tonic-gate soutlen = (int)argv[2]; 1097c478bd9Sstevel@tonic-gate argc--; 1107c478bd9Sstevel@tonic-gate argv++; 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate break; 1137c478bd9Sstevel@tonic-gate case 't': /* tag output to string */ 1147c478bd9Sstevel@tonic-gate argc--; 1157c478bd9Sstevel@tonic-gate argv++; 1167c478bd9Sstevel@tonic-gate tagout = argv[1]; 117*11a8fa6cSceastha if ((int)argv[2] < 16000) { 1187c478bd9Sstevel@tonic-gate taglen = (int)argv[2]; 1197c478bd9Sstevel@tonic-gate argc--; 1207c478bd9Sstevel@tonic-gate argv++; 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate break; 1237c478bd9Sstevel@tonic-gate case 'l': /* specify length of lists */ 1247c478bd9Sstevel@tonic-gate argc--; 1257c478bd9Sstevel@tonic-gate argv++; 1267c478bd9Sstevel@tonic-gate lmaster = atoi(argv[1]); 1277c478bd9Sstevel@tonic-gate #if D1 1287c478bd9Sstevel@tonic-gate fprintf(stderr, "lmaster now %d\n", lmaster); 1297c478bd9Sstevel@tonic-gate #endif 1307c478bd9Sstevel@tonic-gate break; 1317c478bd9Sstevel@tonic-gate case 'C': 1327c478bd9Sstevel@tonic-gate argc--; 1337c478bd9Sstevel@tonic-gate argv++; 1347c478bd9Sstevel@tonic-gate colevel = atoi(argv[1]); 1357c478bd9Sstevel@tonic-gate break; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate argc--; 1387c478bd9Sstevel@tonic-gate argv++; 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate strcpy(indexname, todir(argv[1])); 1417c478bd9Sstevel@tonic-gate #if D1 142*11a8fa6cSceastha fprintf(stderr, "in huntmain indexname %s typeindex %d\n", 143*11a8fa6cSceastha indexname, typeindex); 1447c478bd9Sstevel@tonic-gate #endif 145*11a8fa6cSceastha if (typeindex == 0 || strcmp(oldname, indexname) != 0) { 1467c478bd9Sstevel@tonic-gate strcpy(oldname, indexname); 1477c478bd9Sstevel@tonic-gate unopen(fa); 1487c478bd9Sstevel@tonic-gate unopen(fb); 1497c478bd9Sstevel@tonic-gate unopen(fc); 1507c478bd9Sstevel@tonic-gate 151*11a8fa6cSceastha if (ckexist(indexname, ".ib")) { 1527c478bd9Sstevel@tonic-gate #if D1 1537c478bd9Sstevel@tonic-gate fprintf(stderr, "found old index\n"); 1547c478bd9Sstevel@tonic-gate #endif 1557c478bd9Sstevel@tonic-gate fa = iopen(indexname, ".ia"); 1567c478bd9Sstevel@tonic-gate fb = iopen(indexname, ".ib"); 1577c478bd9Sstevel@tonic-gate fc = iopen(indexname, ".ic"); 1587c478bd9Sstevel@tonic-gate typeindex = 1; 1597c478bd9Sstevel@tonic-gate #if D1 1607c478bd9Sstevel@tonic-gate fprintf(stderr, "opened f's as %o %o %o\n", fa, fb, fc); 1617c478bd9Sstevel@tonic-gate #endif 1627c478bd9Sstevel@tonic-gate indexdate = gdate(fb); 1637c478bd9Sstevel@tonic-gate fread(&nhash, sizeof (nhash), 1, fa); 1647c478bd9Sstevel@tonic-gate fread(&iflong, sizeof (iflong), 1, fa); 165*11a8fa6cSceastha if (nhash > maxhash) { 1667c478bd9Sstevel@tonic-gate if (hpt) 1677c478bd9Sstevel@tonic-gate free(hpt, maxhash, sizeof (*hpt)); 1687c478bd9Sstevel@tonic-gate hpt = 0; 1697c478bd9Sstevel@tonic-gate if (hfreq) 1707c478bd9Sstevel@tonic-gate free(hfreq, maxhash, sizeof (*hfreq)); 1717c478bd9Sstevel@tonic-gate hfreq = 0; 1727c478bd9Sstevel@tonic-gate maxhash = nhash; 1737c478bd9Sstevel@tonic-gate #if D1 174*11a8fa6cSceastha fprintf(stderr, "Freed if needed maxhash %d\n", 175*11a8fa6cSceastha maxhash); 1767c478bd9Sstevel@tonic-gate #endif 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate if (hpt == 0) 1797c478bd9Sstevel@tonic-gate hpt = (long *)zalloc(nhash, sizeof (*hpt)); 1807c478bd9Sstevel@tonic-gate #if D1 1817c478bd9Sstevel@tonic-gate fprintf(stderr, "hpt now %o\n", hpt); 1827c478bd9Sstevel@tonic-gate #endif 1837c478bd9Sstevel@tonic-gate if (hpt == NULL) 1847c478bd9Sstevel@tonic-gate /* 1857c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 1867c478bd9Sstevel@tonic-gate * %d is the size of the hash table - not 1877c478bd9Sstevel@tonic-gate * very interesting info for the end users. 1887c478bd9Sstevel@tonic-gate * Hash is a computer science terminology. 1897c478bd9Sstevel@tonic-gate */ 1907c478bd9Sstevel@tonic-gate err(gettext("No space for hash list (%d)"), 1917c478bd9Sstevel@tonic-gate nhash); 1927c478bd9Sstevel@tonic-gate fread(hpt, sizeof (*hpt), nhash, fa); 1937c478bd9Sstevel@tonic-gate if (hfreq == 0) 1947c478bd9Sstevel@tonic-gate hfreq = (int *)zalloc(nhash, sizeof (*hfreq)); 1957c478bd9Sstevel@tonic-gate if (hfreq == NULL) 1967c478bd9Sstevel@tonic-gate /* 1977c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE 1987c478bd9Sstevel@tonic-gate * %d is the size of the hash table. 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate err(gettext( 2017c478bd9Sstevel@tonic-gate "No space for hash frequencies (%d)"), 2027c478bd9Sstevel@tonic-gate nhash); 2037c478bd9Sstevel@tonic-gate frtbl = fread(hfreq, sizeof (*hfreq), nhash, fa); 2047c478bd9Sstevel@tonic-gate hfrflg = (frtbl == nhash); 2057c478bd9Sstevel@tonic-gate #if D1 2067c478bd9Sstevel@tonic-gate fprintf(stderr, "Read pointer files\n"); 2077c478bd9Sstevel@tonic-gate #endif 2087c478bd9Sstevel@tonic-gate if (master.a == NULL) 2097c478bd9Sstevel@tonic-gate if (iflong) 210*11a8fa6cSceastha master.b = (long *)zalloc(lmaster, 211*11a8fa6cSceastha sizeof (long)); 2127c478bd9Sstevel@tonic-gate else 213*11a8fa6cSceastha master.a = (unsigned *)zalloc(lmaster, 214*11a8fa6cSceastha sizeof (int)); 2157c478bd9Sstevel@tonic-gate if (master.a == NULL) 2167c478bd9Sstevel@tonic-gate err(gettext("no space for answer list"), 0); 217*11a8fa6cSceastha } else 2187c478bd9Sstevel@tonic-gate if (makefgrep(indexname)) 2197c478bd9Sstevel@tonic-gate typeindex = 2; 220*11a8fa6cSceastha else { 2217c478bd9Sstevel@tonic-gate err(gettext("No files %s\n"), indexname); 2227c478bd9Sstevel@tonic-gate exit(1); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate if (iflong) 2277c478bd9Sstevel@tonic-gate masterp = (unsigned *)master.b; 2287c478bd9Sstevel@tonic-gate else 2297c478bd9Sstevel@tonic-gate masterp = master.a; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate #if D1 2327c478bd9Sstevel@tonic-gate fprintf(stderr, "typeindex now %d\n", typeindex); 2337c478bd9Sstevel@tonic-gate #endif 2347c478bd9Sstevel@tonic-gate tagout[0] = 0; 235*11a8fa6cSceastha if (typeindex == 2) { 2367c478bd9Sstevel@tonic-gate grepcall(sinput, tagout, indexname); 2377c478bd9Sstevel@tonic-gate #if D1 2387c478bd9Sstevel@tonic-gate fprintf(stderr, " back from grepcall\n"); 2397c478bd9Sstevel@tonic-gate #endif 2407c478bd9Sstevel@tonic-gate restodir(); 2417c478bd9Sstevel@tonic-gate return; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate nitem = getq(qitem); 2447c478bd9Sstevel@tonic-gate #if D1 2457c478bd9Sstevel@tonic-gate fprintf(stderr, "approaching doquery fb %o\n", fb); 2467c478bd9Sstevel@tonic-gate #endif 2477c478bd9Sstevel@tonic-gate nfound = doquery(hpt, nhash, fb, nitem, qitem, masterp); 2487c478bd9Sstevel@tonic-gate #ifdef D1 2497c478bd9Sstevel@tonic-gate fprintf(stderr, "return from doquery with nfound %d\n", nfound); 2507c478bd9Sstevel@tonic-gate #endif 2517c478bd9Sstevel@tonic-gate if (falseflg == 0) 252*11a8fa6cSceastha nfound = baddrop(masterp, nfound, fc, nitem, qitem, 253*11a8fa6cSceastha rprog, full); 2547c478bd9Sstevel@tonic-gate #ifdef D1 2557c478bd9Sstevel@tonic-gate fprintf(stderr, "after baddrop with nfound %d\n", nfound); 2567c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout is /%s/, sout /%s/\n", tagout, soutput); 2577c478bd9Sstevel@tonic-gate #endif 2587c478bd9Sstevel@tonic-gate if (tags) 2597c478bd9Sstevel@tonic-gate result(masterp, nfound > tags ? tags : nfound, fc); 2607c478bd9Sstevel@tonic-gate #if D1 2617c478bd9Sstevel@tonic-gate fprintf(stderr, "done with huntmain\n"); 2627c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout is /%s/\n", tagout); 2637c478bd9Sstevel@tonic-gate fprintf(stderr, "string out is /%s/\n", soutput); 2647c478bd9Sstevel@tonic-gate #endif 265*11a8fa6cSceastha if (fgnamp > fgnames) { 2667c478bd9Sstevel@tonic-gate char **fgp; 2677c478bd9Sstevel@tonic-gate int k; 2687c478bd9Sstevel@tonic-gate #if D1 2697c478bd9Sstevel@tonic-gate fprintf(stderr, "were %d bad files\n", fgnamp-fgnames); 2707c478bd9Sstevel@tonic-gate #endif 2717c478bd9Sstevel@tonic-gate grepquery[0] = 0; 272*11a8fa6cSceastha for (k = 0; k < nitem; k++) { 2737c478bd9Sstevel@tonic-gate strcat(grepquery, " "); 2747c478bd9Sstevel@tonic-gate strcat(grepquery, qitem[k]); 2757c478bd9Sstevel@tonic-gate } 276*11a8fa6cSceastha for (fgp = fgnames; fgp < fgnamp; fgp++) { 2777c478bd9Sstevel@tonic-gate #if D1 278*11a8fa6cSceastha fprintf(stderr, "Now on %s query /%s/\n", 279*11a8fa6cSceastha *fgp, grepquery); 2807c478bd9Sstevel@tonic-gate #endif 2817c478bd9Sstevel@tonic-gate makefgrep(*fgp); 2827c478bd9Sstevel@tonic-gate grepcall(grepquery, tagout, *fgp); 2837c478bd9Sstevel@tonic-gate #if D1 2847c478bd9Sstevel@tonic-gate fprintf(stderr, "tagout now /%s/\n", tagout); 2857c478bd9Sstevel@tonic-gate #endif 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate restodir(); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate char * 292*11a8fa6cSceastha todir(char *t) 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate char *s; 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate usedir[0] = 0; 2977c478bd9Sstevel@tonic-gate s = t; 2987c478bd9Sstevel@tonic-gate while (*s) s++; 2997c478bd9Sstevel@tonic-gate while (s >= t && *s != '/') s--; 300*11a8fa6cSceastha if (s < t) 301*11a8fa6cSceastha return (t); 3027c478bd9Sstevel@tonic-gate *s++ = 0; 3037c478bd9Sstevel@tonic-gate t = (*t ? t : "/"); 3047c478bd9Sstevel@tonic-gate chdir(t); 3057c478bd9Sstevel@tonic-gate strcpy(usedir, t); 3067c478bd9Sstevel@tonic-gate return (s); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 309*11a8fa6cSceastha static int 310*11a8fa6cSceastha setfrom(char c) 3117c478bd9Sstevel@tonic-gate { 312*11a8fa6cSceastha switch (c) { 3137c478bd9Sstevel@tonic-gate case 'y': 3147c478bd9Sstevel@tonic-gate case '\0': 3157c478bd9Sstevel@tonic-gate default: 3167c478bd9Sstevel@tonic-gate return (1000); 3177c478bd9Sstevel@tonic-gate case '1': 3187c478bd9Sstevel@tonic-gate case '2': 3197c478bd9Sstevel@tonic-gate case '3': 3207c478bd9Sstevel@tonic-gate case '4': 3217c478bd9Sstevel@tonic-gate case '5': 3227c478bd9Sstevel@tonic-gate case '6': 3237c478bd9Sstevel@tonic-gate case '7': 3247c478bd9Sstevel@tonic-gate case '8': 3257c478bd9Sstevel@tonic-gate case '9': 3267c478bd9Sstevel@tonic-gate return (c-'0'); 3277c478bd9Sstevel@tonic-gate case 'n': 3287c478bd9Sstevel@tonic-gate case '0': 3297c478bd9Sstevel@tonic-gate return (0); 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate } 332