xref: /original-bsd/old/refer/hunt/hunt6.c (revision f052b07a)
1 #ifndef lint
2 static char *sccsid = "@(#)hunt6.c	4.3 (Berkeley) 09/28/87";
3 #endif
4 
5 #include <stdio.h>
6 #include <assert.h>
7 #define TXTLEN 1000
8 
9 char *outbuf = 0;
10 extern char *soutput;
11 extern int soutlen, iflong;
12 extern long indexdate;
13 
14 baddrop(master, nf, fc, nitem, qitem, rprog, full)
15 unsigned *master;
16 FILE *fc;
17 char *qitem[], *rprog;
18 {
19 	/* checks list of drops for real bad drops; finds items with "deliv" */
20 	union ptr {
21 		unsigned *a;
22 		long *b;
23 	} umaster;
24 	int i, g, j, need, got, na, len;
25 	long lp;
26 	char res[100], *ar[50], output[TXTLEN];
27 	extern int colevel, reached;
28 
29 	if (iflong)
30 		umaster.b = (long *) master;
31 	else
32 		umaster.a = master;
33 # if D1
34 	if (iflong)
35 		fprintf(stderr,"in baddrop, nf %d umaster %ld %ld %ld\n",
36 			nf, umaster.b[0], umaster.b[1], umaster.b[2]);
37 	else
38 		fprintf(stderr,"in baddrop, nf %d umaster %d %d %d\n",
39 			nf, umaster.a[0], umaster.a[1], umaster.a[2]);
40 # endif
41 	for (i=g=0; i<nf; i++)
42 	{
43 		lp = iflong ? umaster.b[i] : umaster.a[i];
44 # if D1
45 		if (iflong)
46 			fprintf(stderr, "i %d umaster %lo lp %lo\n",
47 				i, umaster.b[i], lp);
48 		else
49 			fprintf(stderr, "i %d umaster %o lp %lo\n",
50 				i, umaster.a[i], lp);
51 # endif
52 		fseek (fc, lp, 0);
53 		fgets( res, 100, fc);
54 # if D1
55 		fprintf(stderr, "tag %s", res);
56 # endif
57 		if (!auxil(res,output))
58 		{
59 			char *s;
60 			int c;
61 # if D1
62 			fprintf(stderr, "not auxil try rprog %c\n",
63 				rprog? 'y': 'n');
64 # endif
65 			for(s=res; c= *s; s++)
66 				if (c == ';' || c == '\n')
67 				{
68 					*s=0;
69 					break;
70 				}
71 			len = rprog ?
72 			corout (res, output, rprog, 0, TXTLEN) :
73 			findline (res, output, TXTLEN, indexdate);
74 		}
75 # if D1
76 		_assert (len <TXTLEN);
77 		fprintf(stderr,"item %d of %d, tag %s len %d output\n%s\n..\n",
78 			i, nf, res, len, output);
79 # endif
80 		if (len==0)
81 			continue;
82 		need = colevel ? reached : nitem;
83 		na=0;
84 		ar[na++] = "fgrep";
85 		ar[na++] = "-r";
86 		ar[na++] = "-n";
87 		ar[na++] = (char *) need;
88 		ar[na++] = "-i";
89 		ar[na++] = output;
90 		ar[na++] = (char *) len;
91 		for(j=0; j<nitem; j++)
92 			ar[na++] = qitem[j];
93 # ifdef D1
94 		fprintf(stderr, "calling fgrep len %d ar[4] %s %o %d \n",
95 			len,ar[4],ar[5],ar[6]);
96 # endif
97 		if (fgrep(na, ar)==0)
98 		{
99 # ifdef D1
100 			fprintf(stderr, "fgrep found it\n");
101 # endif
102 			if (iflong)
103 				umaster.b[g++] = umaster.b[i];
104 			else
105 				umaster.a[g++] = umaster.a[i];
106 			if (full >= g)
107 				if (soutput==0)
108 					fputs(output, stdout);
109 				else
110 					strcpy (soutput, output);
111 		}
112 # ifdef D1
113 		fprintf(stderr, "after fgrep\n");
114 # endif
115 	}
116 	return(g);
117 }
118 
119 auxil( res, output)
120 char *res, *output;
121 {
122 	extern FILE *fd;
123 	long lp, c;
124 	int len;
125 	if (fd==0)return(0);
126 	while (c = *res++)
127 	{
128 		if (c == ';')
129 		{
130 			sscanf(res, "%ld,%d", &lp, &len);
131 			fseek (fd, lp, 0);
132 			fgets(output, len, fd);
133 			return(1);
134 		}
135 	}
136 	return(0);
137 }
138