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