xref: /original-bsd/old/refer/refer/glue4.c (revision 2301fdfb)
1 #ifndef lint
2 static char *sccsid = "@(#)glue4.c	4.2 (Berkeley) 04/24/88";
3 #endif
4 
5 #include <stdio.h>
6 #include <ctype.h>
7 
8 extern char gfile[];
9 
10 grepcall (in, out, arg)
11 char *in, *out, *arg;
12 {
13 	char line[200], *s, argig[100], *cv[50];
14 	char *inp, inb[500];
15 	FILE *qf, *gf;
16 	int c, oldc = 0, alph = 0, nv = 0;
17 	int sv0, sv1;
18 	strcpy (argig, arg);
19 	strcat(argig, ".ig");
20 	strcpy (inp=inb, in);
21 	if (gfile[0]==0)
22 		sprintf(gfile, "/tmp/rj%dg", getpid());
23 # if D1
24 	fprintf(stderr, "in grepcall, gfile %s in %o out %o\n", gfile,in,out);
25 # endif
26 	for(cv[nv++] = "fgrep"; c = *inp; inp++)
27 	{
28 		if (c== ' ')
29 			c = *inp = 0;
30 		else if (isupper(c))
31 			*inp = tolower(c);
32 		alph = (c==0) ? 0 : alph+1;
33 		if (alph == 1)
34 			cv[nv++] = inp;
35 		if (alph > 6)
36 			*inp = 0;
37 		oldc=c;
38 	}
39 # if D1
40 	fprintf(stderr, "%d args set up\n", nv);
41 # endif
42 	{
43 		sv0 = dup(0);
44 		close(0);
45 		if (open (argig, 0) != 0)
46 			err("Can't read fgrep index %s", argig);
47 		sv1 = dup(1);
48 		close(1);
49 		if (creat(gfile, 0666) != 1)
50 			err("Can't write fgrep output %s", gfile);
51 		fgrep(nv, cv);
52 # if D1
53 		fprintf(stderr, "fgrep returned, output is..\n");
54 # endif
55 		close (0);
56 		dup(sv0);
57 		close(sv0);
58 		close (1);
59 		dup(sv1);
60 		close(sv1);
61 	}
62 
63 # if D1
64 	fprintf(stderr, "back from fgrep\n");
65 # endif
66 	gf = fopen(gfile, "r");
67 	if (gf==NULL)
68 		err("can't read fgrep output %s", gfile);
69 	while (fgets(line, 100, gf) == line)
70 	{
71 		line[100]=0;
72 # if D1
73 		fprintf(stderr, "read line as //%s//\n",line);
74 # endif
75 		for(s=line; *s && (*s != '\t'); s++);
76 		if (*s == '\t')
77 		{
78 			*s++ = '\n';
79 			*s++ = 0;
80 		}
81 		if (line[0])
82 			strcat(out, line);
83 # if D1
84 		fprintf(stderr, "out now /%s/\n",out);
85 # endif
86 		while (*s) s++;
87 # if D1
88 		fprintf(stderr, "line %o s %o s-1 %o\n",line,s,s[-1]);
89 # endif
90 		if (s[-1]!= '\n')
91 			while (!feof(gf) && getc(gf)!= '\n') ;
92 	}
93 	fclose(gf);
94 # if D1
95 	fprintf(stderr, "back from reading %, out %s\n",out);
96 # else
97 	unlink (gfile);
98 # endif
99 	return(0);
100 }
101 
102 clfgrep()
103 {
104 	if (gfile[0])
105 		unlink(gfile);
106 }
107