xref: /illumos-gate/usr/src/cmd/refer/hunt6.c (revision 2a8bcb4e)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <stdio.h>
167c478bd9Sstevel@tonic-gate #include <assert.h>
177c478bd9Sstevel@tonic-gate #include <string.h>
18*11a8fa6cSceastha #define	TXTLEN 1000
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate char *outbuf = 0;
217c478bd9Sstevel@tonic-gate extern char *soutput;
227c478bd9Sstevel@tonic-gate extern int soutlen, iflong;
237c478bd9Sstevel@tonic-gate extern long indexdate;
247c478bd9Sstevel@tonic-gate union ptr {
25*11a8fa6cSceastha 	unsigned *a;
26*11a8fa6cSceastha 	long *b;
277c478bd9Sstevel@tonic-gate };
287c478bd9Sstevel@tonic-gate 
29*11a8fa6cSceastha extern int corout();
30*11a8fa6cSceastha extern int fgrep();
31*11a8fa6cSceastha extern long findline();
32*11a8fa6cSceastha 
33*11a8fa6cSceastha static int auxil(char *, char *);
34*11a8fa6cSceastha 
35*11a8fa6cSceastha int
baddrop(unsigned * mptr,int nf,FILE * fc,int nitem,char * qitem[],char * rprog,int full)36*11a8fa6cSceastha baddrop(unsigned *mptr, int nf, FILE *fc, int nitem,
37*11a8fa6cSceastha 	    char *qitem[], char *rprog, int full)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	/* checks list of drops for real bad drops; finds items with "deliv" */
407c478bd9Sstevel@tonic-gate 	int i, g, j, need, got, na, len;
417c478bd9Sstevel@tonic-gate 	long lp;
427c478bd9Sstevel@tonic-gate 	char res[100], *ar[50], output[TXTLEN], *mput;
437c478bd9Sstevel@tonic-gate 	union ptr master;
447c478bd9Sstevel@tonic-gate 	extern int colevel, reached;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if (iflong) {
47*11a8fa6cSceastha 		master.b = (long *)mptr;
48*11a8fa6cSceastha 	} else {
497c478bd9Sstevel@tonic-gate 		master.a = mptr;
507c478bd9Sstevel@tonic-gate 	}
517c478bd9Sstevel@tonic-gate 
52*11a8fa6cSceastha #if D1
537c478bd9Sstevel@tonic-gate 	if (iflong)
54*11a8fa6cSceastha 		fprintf(stderr, "in baddrop, nf %d master %ld %ld %ld\n",
55*11a8fa6cSceastha 		    nf, master.b[0], master.b[1], master.b[2]);
567c478bd9Sstevel@tonic-gate 	else
57*11a8fa6cSceastha 		fprintf(stderr, "in baddrop, nf %d master %d %d %d\n",
58*11a8fa6cSceastha 		    nf, master.a[0], master.a[1], master.a[2]);
59*11a8fa6cSceastha #endif
60*11a8fa6cSceastha 	for (i = g = 0; i < nf; i++) {
617c478bd9Sstevel@tonic-gate 		lp = iflong ? master.b[i] : master.a[i];
62*11a8fa6cSceastha #if D1
637c478bd9Sstevel@tonic-gate 		if (iflong)
647c478bd9Sstevel@tonic-gate 			fprintf(stderr, "i %d master %lo lp %lo\n",
65*11a8fa6cSceastha 			    i, master.b[i], lp);
667c478bd9Sstevel@tonic-gate 		else
677c478bd9Sstevel@tonic-gate 			fprintf(stderr, "i %d master %o lp %lo\n",
68*11a8fa6cSceastha 			    i, master.a[i], lp);
69*11a8fa6cSceastha #endif
70*11a8fa6cSceastha 		fseek(fc, lp, 0);
71*11a8fa6cSceastha 		fgets(res, 100, fc);
72*11a8fa6cSceastha #if D1
737c478bd9Sstevel@tonic-gate 		fprintf(stderr, "tag %s", res);
74*11a8fa6cSceastha #endif
75*11a8fa6cSceastha 		if (!auxil(res, output)) {
76*11a8fa6cSceastha 			char *s;
777c478bd9Sstevel@tonic-gate 			int c;
78*11a8fa6cSceastha #if D1
797c478bd9Sstevel@tonic-gate 			fprintf(stderr, "not auxil try rprog %c\n",
80*11a8fa6cSceastha 			    rprog ? 'y': 'n');
81*11a8fa6cSceastha #endif
82*11a8fa6cSceastha 			for (s = res; c = *s; s++)
83*11a8fa6cSceastha 				if (c == ';' || c == '\n') {
84*11a8fa6cSceastha 					*s = 0;
857c478bd9Sstevel@tonic-gate 					break;
867c478bd9Sstevel@tonic-gate 				}
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 			if (rprog)
89*11a8fa6cSceastha 				len = corout(res, output, rprog, "", TXTLEN);
90*11a8fa6cSceastha 			else {
917c478bd9Sstevel@tonic-gate 				len = findline(res, &mput, TXTLEN, indexdate);
92*11a8fa6cSceastha 				if (len > 0) {	/* copy and free */
937c478bd9Sstevel@tonic-gate 					strncpy(output, mput, TXTLEN);
947c478bd9Sstevel@tonic-gate 					free(mput);
95*11a8fa6cSceastha 				} else /* insufficient memory or other... */
967c478bd9Sstevel@tonic-gate 					len = 0;
977c478bd9Sstevel@tonic-gate 			}
987c478bd9Sstevel@tonic-gate 		}
99*11a8fa6cSceastha #if D1
100*11a8fa6cSceastha 		assert(len < TXTLEN);
101*11a8fa6cSceastha 		fprintf(stderr, "item %d of %d, tag %s len %d output\n%s\n..\n",
102*11a8fa6cSceastha 		    i, nf, res, len, output);
103*11a8fa6cSceastha #endif
104*11a8fa6cSceastha 		if (len == 0)
1057c478bd9Sstevel@tonic-gate 			continue;
1067c478bd9Sstevel@tonic-gate 		need = colevel ? reached : nitem;
107*11a8fa6cSceastha 		na = 0;
1087c478bd9Sstevel@tonic-gate 		ar[na++] = "fgrep";
1097c478bd9Sstevel@tonic-gate 		ar[na++] = "-r";
1107c478bd9Sstevel@tonic-gate 		ar[na++] = "-n";
111*11a8fa6cSceastha 		ar[na++] = (char *)need;
1127c478bd9Sstevel@tonic-gate 		ar[na++] = "-i";
1137c478bd9Sstevel@tonic-gate 		ar[na++] = output;
114*11a8fa6cSceastha 		ar[na++] = (char *)len;
115*11a8fa6cSceastha 		for (j = 0; j < nitem; j++)
1167c478bd9Sstevel@tonic-gate 			ar[na++] = qitem[j];
117*11a8fa6cSceastha #ifdef D1
1187c478bd9Sstevel@tonic-gate 		fprintf(stderr, "calling fgrep len %d ar[4] %s %o %d \n",
119*11a8fa6cSceastha 		    len, ar[4], ar[5], ar[6]);
120*11a8fa6cSceastha #endif
121*11a8fa6cSceastha 		if (fgrep(na, ar) == 0) {
122*11a8fa6cSceastha #ifdef D1
1237c478bd9Sstevel@tonic-gate 			fprintf(stderr, "fgrep found it\n");
124*11a8fa6cSceastha #endif
1257c478bd9Sstevel@tonic-gate 			if (iflong)
1267c478bd9Sstevel@tonic-gate 				master.b[g++] = master.b[i];
1277c478bd9Sstevel@tonic-gate 			else
1287c478bd9Sstevel@tonic-gate 				master.a[g++] = master.a[i];
1297c478bd9Sstevel@tonic-gate 			if (full >= g)
130*11a8fa6cSceastha 				if (soutput == 0)
1317c478bd9Sstevel@tonic-gate 					fputs(output, stdout);
1327c478bd9Sstevel@tonic-gate 				else
133*11a8fa6cSceastha 					strcpy(soutput, output);
1347c478bd9Sstevel@tonic-gate 		}
135*11a8fa6cSceastha #ifdef D1
1367c478bd9Sstevel@tonic-gate 		fprintf(stderr, "after fgrep\n");
137*11a8fa6cSceastha #endif
1387c478bd9Sstevel@tonic-gate 	}
139*11a8fa6cSceastha 	return (g);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate 
142*11a8fa6cSceastha static int
auxil(char * res,char * output)143*11a8fa6cSceastha auxil(char *res, char *output)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	extern FILE *fd;
146*11a8fa6cSceastha 	long lp, c;
1477c478bd9Sstevel@tonic-gate 	int len;
148*11a8fa6cSceastha 	if (fd == 0)
149*11a8fa6cSceastha 		return (0);
150*11a8fa6cSceastha 	while (c = *res++) {
151*11a8fa6cSceastha 		if (c == ';') {
1527c478bd9Sstevel@tonic-gate 			sscanf(res, "%ld,%d", &lp, &len);
153*11a8fa6cSceastha 			fseek(fd, lp, 0);
1547c478bd9Sstevel@tonic-gate 			fgets(output, len, fd);
155*11a8fa6cSceastha 			return (1);
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 	}
158*11a8fa6cSceastha 	return (0);
1597c478bd9Sstevel@tonic-gate }
160