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