xref: /titanic_41/usr/src/cmd/refer/hunt6.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 #include <stdio.h>
19 #include <assert.h>
20 #include <string.h>
21 #define TXTLEN 1000
22 
23 char *outbuf = 0;
24 extern char *soutput;
25 extern int soutlen, iflong;
26 extern long indexdate;
27 union ptr {
28 	unsigned *a;
29 	long *b;
30 };
31 
32 baddrop(mptr, nf, fc, nitem, qitem, rprog, full)
33 unsigned *mptr;
34 FILE *fc;
35 char *qitem[], *rprog;
36 {
37 	/* checks list of drops for real bad drops; finds items with "deliv" */
38 	int i, g, j, need, got, na, len;
39 	long lp;
40 	char res[100], *ar[50], output[TXTLEN], *mput;
41 	union ptr master;
42 	extern int colevel, reached;
43 
44 	if (iflong) {
45 		master.b = (long *) mptr;
46 	}
47 	else {
48 		master.a = mptr;
49 	}
50 
51 # if D1
52 	if (iflong)
53 		fprintf(stderr,"in baddrop, nf %d master %ld %ld %ld\n",
54 			nf, master.b[0], master.b[1], master.b[2]);
55 	else
56 		fprintf(stderr,"in baddrop, nf %d master %d %d %d\n",
57 			nf, master.a[0], master.a[1], master.a[2]);
58 # endif
59 	for (i=g=0; i<nf; i++)
60 	{
61 		lp = iflong ? master.b[i] : master.a[i];
62 # if D1
63 		if (iflong)
64 			fprintf(stderr, "i %d master %lo lp %lo\n",
65 				i, master.b[i], lp);
66 		else
67 			fprintf(stderr, "i %d master %o lp %lo\n",
68 				i, master.a[i], lp);
69 # endif
70 		fseek (fc, lp, 0);
71 		fgets( res, 100, fc);
72 # if D1
73 		fprintf(stderr, "tag %s", res);
74 # endif
75 		if (!auxil(res,output))
76 		{
77 			char *s;
78 			int c;
79 # if D1
80 			fprintf(stderr, "not auxil try rprog %c\n",
81 				rprog? 'y': 'n');
82 # endif
83 			for(s=res; c= *s; s++)
84 				if (c == ';' || c == '\n')
85 				{
86 					*s=0;
87 					break;
88 				}
89 
90 			if (rprog)
91 				len = corout(res, output, rprog, 0, TXTLEN);
92 			else
93 			{
94 				len = findline(res, &mput, TXTLEN, indexdate);
95 				if (len > 0) /* copy and free */
96 				{
97 					strncpy(output, mput, TXTLEN);
98 					free(mput);
99 				}
100 				else /* insufficient memory or other... */
101 					len = 0;
102 			}
103 		}
104 # if D1
105 		assert (len <TXTLEN);
106 		fprintf(stderr,"item %d of %d, tag %s len %d output\n%s\n..\n",
107 			i, nf, res, len, output);
108 # endif
109 		if (len==0)
110 			continue;
111 		need = colevel ? reached : nitem;
112 		na=0;
113 		ar[na++] = "fgrep";
114 		ar[na++] = "-r";
115 		ar[na++] = "-n";
116 		ar[na++] = (char *) need;
117 		ar[na++] = "-i";
118 		ar[na++] = output;
119 		ar[na++] = (char *) len;
120 		for(j=0; j<nitem; j++)
121 			ar[na++] = qitem[j];
122 # ifdef D1
123 		fprintf(stderr, "calling fgrep len %d ar[4] %s %o %d \n",
124 			len,ar[4],ar[5],ar[6]);
125 # endif
126 		if (fgrep(na, ar)==0)
127 		{
128 # ifdef D1
129 			fprintf(stderr, "fgrep found it\n");
130 # endif
131 			if (iflong)
132 				master.b[g++] = master.b[i];
133 			else
134 				master.a[g++] = master.a[i];
135 			if (full >= g)
136 				if (soutput==0)
137 					fputs(output, stdout);
138 				else
139 					strcpy (soutput, output);
140 		}
141 # ifdef D1
142 		fprintf(stderr, "after fgrep\n");
143 # endif
144 	}
145 	return(g);
146 }
147 
148 auxil( res, output)
149 char *res, *output;
150 {
151 	extern FILE *fd;
152 	long lp, c;
153 	int len;
154 	if (fd==0)return(0);
155 	while (c = *res++)
156 	{
157 		if (c == ';')
158 		{
159 			sscanf(res, "%ld,%d", &lp, &len);
160 			fseek (fd, lp, 0);
161 			fgets(output, len, fd);
162 			return(1);
163 		}
164 	}
165 	return(0);
166 }
167