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