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