xref: /illumos-gate/usr/src/cmd/refer/hunt6.c (revision 2a8bcb4e)
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 #include <stdio.h>
16 #include <assert.h>
17 #include <string.h>
18 #define	TXTLEN 1000
19 
20 char *outbuf = 0;
21 extern char *soutput;
22 extern int soutlen, iflong;
23 extern long indexdate;
24 union ptr {
25 	unsigned *a;
26 	long *b;
27 };
28 
29 extern int corout();
30 extern int fgrep();
31 extern long findline();
32 
33 static int auxil(char *, char *);
34 
35 int
baddrop(unsigned * mptr,int nf,FILE * fc,int nitem,char * qitem[],char * rprog,int full)36 baddrop(unsigned *mptr, int nf, FILE *fc, int nitem,
37 	    char *qitem[], char *rprog, int full)
38 {
39 	/* checks list of drops for real bad drops; finds items with "deliv" */
40 	int i, g, j, need, got, na, len;
41 	long lp;
42 	char res[100], *ar[50], output[TXTLEN], *mput;
43 	union ptr master;
44 	extern int colevel, reached;
45 
46 	if (iflong) {
47 		master.b = (long *)mptr;
48 	} else {
49 		master.a = mptr;
50 	}
51 
52 #if D1
53 	if (iflong)
54 		fprintf(stderr, "in baddrop, nf %d master %ld %ld %ld\n",
55 		    nf, master.b[0], master.b[1], master.b[2]);
56 	else
57 		fprintf(stderr, "in baddrop, nf %d master %d %d %d\n",
58 		    nf, master.a[0], master.a[1], master.a[2]);
59 #endif
60 	for (i = g = 0; i < nf; i++) {
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 			char *s;
77 			int c;
78 #if D1
79 			fprintf(stderr, "not auxil try rprog %c\n",
80 			    rprog ? 'y': 'n');
81 #endif
82 			for (s = res; c = *s; s++)
83 				if (c == ';' || c == '\n') {
84 					*s = 0;
85 					break;
86 				}
87 
88 			if (rprog)
89 				len = corout(res, output, rprog, "", TXTLEN);
90 			else {
91 				len = findline(res, &mput, TXTLEN, indexdate);
92 				if (len > 0) {	/* copy and free */
93 					strncpy(output, mput, TXTLEN);
94 					free(mput);
95 				} else /* insufficient memory or other... */
96 					len = 0;
97 			}
98 		}
99 #if D1
100 		assert(len < TXTLEN);
101 		fprintf(stderr, "item %d of %d, tag %s len %d output\n%s\n..\n",
102 		    i, nf, res, len, output);
103 #endif
104 		if (len == 0)
105 			continue;
106 		need = colevel ? reached : nitem;
107 		na = 0;
108 		ar[na++] = "fgrep";
109 		ar[na++] = "-r";
110 		ar[na++] = "-n";
111 		ar[na++] = (char *)need;
112 		ar[na++] = "-i";
113 		ar[na++] = output;
114 		ar[na++] = (char *)len;
115 		for (j = 0; j < nitem; j++)
116 			ar[na++] = qitem[j];
117 #ifdef D1
118 		fprintf(stderr, "calling fgrep len %d ar[4] %s %o %d \n",
119 		    len, ar[4], ar[5], ar[6]);
120 #endif
121 		if (fgrep(na, ar) == 0) {
122 #ifdef D1
123 			fprintf(stderr, "fgrep found it\n");
124 #endif
125 			if (iflong)
126 				master.b[g++] = master.b[i];
127 			else
128 				master.a[g++] = master.a[i];
129 			if (full >= g)
130 				if (soutput == 0)
131 					fputs(output, stdout);
132 				else
133 					strcpy(soutput, output);
134 		}
135 #ifdef D1
136 		fprintf(stderr, "after fgrep\n");
137 #endif
138 	}
139 	return (g);
140 }
141 
142 static int
auxil(char * res,char * output)143 auxil(char *res, char *output)
144 {
145 	extern FILE *fd;
146 	long lp, c;
147 	int len;
148 	if (fd == 0)
149 		return (0);
150 	while (c = *res++) {
151 		if (c == ';') {
152 			sscanf(res, "%ld,%d", &lp, &len);
153 			fseek(fd, lp, 0);
154 			fgets(output, len, fd);
155 			return (1);
156 		}
157 	}
158 	return (0);
159 }
160