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