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