xref: /titanic_51/usr/src/cmd/refer/glue4.c (revision bdfc6d18da790deeec2e0eb09c625902defe2498)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
17 
18 
19 #include <stdio.h>
20 #include <ctype.h>
21 
22 extern char gfile[];
23 
24 grepcall (in, out, arg)
25 char *in, *out, *arg;
26 {
27 	char line[200], *s, argig[100], *cv[50];
28 	char *inp, inb[500];
29 	FILE *qf, *gf;
30 	int c, oldc = 0, alph = 0, nv = 0;
31 	int sv0, sv1;
32 	strcpy (argig, arg);
33 	strcat(argig, ".ig");
34 	strcpy (inp=inb, in);
35 	if (gfile[0]==0)
36 		sprintf(gfile, "/tmp/rj%dg", getpid());
37 # if D1
38 	fprintf(stderr, "in grepcall, gfile %s in %o out %o\n", gfile,in,out);
39 # endif
40 	for(cv[nv++] = "fgrep"; c = *inp; inp++)
41 	{
42 		if (c== ' ')
43 			c = *inp = 0;
44 		else if (isupper(c))
45 			*inp = tolower(c);
46 		alph = (c==0) ? 0 : alph+1;
47 		if (alph == 1)
48 			cv[nv++] = inp;
49 		if (alph > 6)
50 			*inp = 0;
51 		oldc=c;
52 	}
53 # if D1
54 	fprintf(stderr, "%d args set up\n", nv);
55 # endif
56 	{
57 		sv0 = dup(0);
58 		close(0);
59 		if (open (argig, 0) != 0)
60 			err("Can't read fgrep index %s", argig);
61 		sv1 = dup(1);
62 		close(1);
63 		if (creat(gfile, 0666) != 1)
64 			err("Can't write fgrep output %s", gfile);
65 		fgrep(nv, cv);
66 # if D1
67 		fprintf(stderr, "fgrep returned, output is..\n");
68 # endif
69 		close (0);
70 		dup(sv0);
71 		close(sv0);
72 		close (1);
73 		dup(sv1);
74 		close(sv1);
75 	}
76 
77 # if D1
78 	fprintf(stderr, "back from fgrep\n");
79 # endif
80 	gf = fopen(gfile, "r");
81 	if (gf==NULL)
82 		err("can't read fgrep output %s", gfile);
83 	while (fgets(line, 100, gf) == line)
84 	{
85 		line[100]=0;
86 # if D1
87 		fprintf(stderr, "read line as //%s//\n",line);
88 # endif
89 		for(s=line; *s && (*s != '\t'); s++);
90 		if (*s == '\t')
91 		{
92 			*s++ = '\n';
93 			*s++ = 0;
94 		}
95 		if (line[0])
96 			strcat(out, line);
97 # if D1
98 		fprintf(stderr, "out now /%s/\n",out);
99 # endif
100 		while (*s) s++;
101 # if D1
102 		fprintf(stderr, "line %o s %o s-1 %o\n",line,s,s[-1]);
103 # endif
104 		if (s[-1]!= '\n')
105 			while (!feof(gf) && getc(gf)!= '\n') ;
106 	}
107 	fclose(gf);
108 # if D1
109 	fprintf(stderr, "back from reading %, out %s\n",out);
110 # else
111 	unlink (gfile);
112 # endif
113 	return(0);
114 }
115 
116 clfgrep()
117 {
118 	if (gfile[0])
119 		unlink(gfile);
120 }
121