1 /* $MirOS: src/usr.bin/oldroff/refer/glue4.c,v 1.2 2008/11/08 23:04:44 tg Exp $ */
2
3 /*-
4 * Copyright (c) 1979, 1980, 1981, 1986, 1988, 1990, 1991, 1992
5 * The Regents of the University of California.
6 * Copyright (C) Caldera International Inc. 2001-2002.
7 * Copyright (c) 2003, 2004
8 * Thorsten "mirabilos" Glaser <tg@mirbsd.org>
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms,
12 * with or without modification, are permitted provided
13 * that the following conditions are met:
14 *
15 * Redistributions of source code and documentation must retain
16 * the above copyright notice, this list of conditions and the
17 * following disclaimer. Redistributions in binary form must
18 * reproduce the above copyright notice, this list of conditions
19 * and the following disclaimer in the documentation and/or other
20 * materials provided with the distribution.
21 *
22 * All advertising materials mentioning features or use of this
23 * software must display the following acknowledgement:
24 * This product includes software developed or owned by
25 * Caldera International, Inc.
26 *
27 * Neither the name of Caldera International, Inc. nor the names
28 * of other contributors may be used to endorse or promote products
29 * derived from this software without specific prior written permission.
30 *
31 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
32 * INTERNATIONAL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
33 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
36 * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
40 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
41 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45 #ifndef lint
46 static char sccsid[] = "@(#)glue4.c 4.4 (Berkeley) 4/18/91";
47 #endif /* not lint */
48
49 #include <stdio.h>
50 #include <ctype.h>
51 #include "pathnames.h"
52
53 extern char gfile[];
54
grepcall(in,out,arg)55 grepcall (in, out, arg)
56 char *in, *out, *arg;
57 {
58 char line[200], *s, argig[100], *cv[50];
59 char *inp, inb[500];
60 FILE *qf, *gf;
61 int c, oldc = 0, alph = 0, nv = 0;
62 int sv0, sv1;
63 strcpy (argig, arg);
64 strcat(argig, ".ig");
65 strcpy (inp=inb, in);
66 if (gfile[0]==0)
67 sprintf(gfile, "%s/rj%dg", _PATH_TMP, getpid());
68 # if D1
69 fprintf(stderr, "in grepcall, gfile %s in %o out %o\n", gfile,in,out);
70 # endif
71 for(cv[nv++] = "fgrep"; c = *inp; inp++)
72 {
73 if (c== ' ')
74 c = *inp = 0;
75 else if (isupper(c))
76 *inp = tolower(c);
77 alph = (c==0) ? 0 : alph+1;
78 if (alph == 1)
79 cv[nv++] = inp;
80 if (alph > 6)
81 *inp = 0;
82 oldc=c;
83 }
84 # if D1
85 fprintf(stderr, "%d args set up\n", nv);
86 # endif
87 {
88 sv0 = dup(0);
89 close(0);
90 if (open (argig, 0) != 0)
91 err("Can't read fgrep index %s", argig);
92 sv1 = dup(1);
93 close(1);
94 if (creat(gfile, 0666) != 1)
95 err("Can't write fgrep output %s", gfile);
96 fgrep(nv, cv);
97 # if D1
98 fprintf(stderr, "fgrep returned, output is..\n");
99 # endif
100 close (0);
101 dup(sv0);
102 close(sv0);
103 close (1);
104 dup(sv1);
105 close(sv1);
106 }
107
108 # if D1
109 fprintf(stderr, "back from fgrep\n");
110 # endif
111 gf = fopen(gfile, "r");
112 if (gf==NULL)
113 err("can't read fgrep output %s", gfile);
114 while (fgets(line, 100, gf) == line)
115 {
116 line[100]=0;
117 # if D1
118 fprintf(stderr, "read line as //%s//\n",line);
119 # endif
120 for(s=line; *s && (*s != '\t'); s++);
121 if (*s == '\t')
122 {
123 *s++ = '\n';
124 *s++ = 0;
125 }
126 if (line[0])
127 strcat(out, line);
128 # if D1
129 fprintf(stderr, "out now /%s/\n",out);
130 # endif
131 while (*s) s++;
132 # if D1
133 fprintf(stderr, "line %o s %o s-1 %o\n",line,s,s[-1]);
134 # endif
135 if (s[-1]!= '\n')
136 while (!feof(gf) && getc(gf)!= '\n') ;
137 }
138 fclose(gf);
139 # if D1
140 fprintf(stderr, "back from reading %, out %s\n",out);
141 # else
142 unlink (gfile);
143 # endif
144 return(0);
145 }
146
clfgrep()147 clfgrep()
148 {
149 if (gfile[0])
150 unlink(gfile);
151 }
152