1*11a8fa6cSceastha /*
2*11a8fa6cSceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3*11a8fa6cSceastha * Use is subject to license terms.
4*11a8fa6cSceastha */
5*11a8fa6cSceastha
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
167c478bd9Sstevel@tonic-gate
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <ctype.h>
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gate extern char gfile[];
227c478bd9Sstevel@tonic-gate
23*11a8fa6cSceastha void
grepcall(char * in,char * out,char * arg)24*11a8fa6cSceastha grepcall(char *in, char *out, char *arg)
257c478bd9Sstevel@tonic-gate {
267c478bd9Sstevel@tonic-gate char line[200], *s, argig[100], *cv[50];
277c478bd9Sstevel@tonic-gate char *inp, inb[500];
287c478bd9Sstevel@tonic-gate FILE *qf, *gf;
297c478bd9Sstevel@tonic-gate int c, oldc = 0, alph = 0, nv = 0;
307c478bd9Sstevel@tonic-gate int sv0, sv1;
317c478bd9Sstevel@tonic-gate strcpy(argig, arg);
327c478bd9Sstevel@tonic-gate strcat(argig, ".ig");
337c478bd9Sstevel@tonic-gate strcpy(inp = inb, in);
347c478bd9Sstevel@tonic-gate if (gfile[0] == 0)
357c478bd9Sstevel@tonic-gate sprintf(gfile, "/tmp/rj%dg", getpid());
367c478bd9Sstevel@tonic-gate #if D1
37*11a8fa6cSceastha fprintf(stderr, "in grepcall, gfile %s in %o out %o\n",
38*11a8fa6cSceastha gfile, in, out);
397c478bd9Sstevel@tonic-gate #endif
40*11a8fa6cSceastha for (cv[nv++] = "fgrep"; c = *inp; inp++) {
417c478bd9Sstevel@tonic-gate if (c == ' ')
427c478bd9Sstevel@tonic-gate c = *inp = 0;
437c478bd9Sstevel@tonic-gate else if (isupper(c))
447c478bd9Sstevel@tonic-gate *inp = tolower(c);
457c478bd9Sstevel@tonic-gate alph = (c == 0) ? 0 : alph+1;
467c478bd9Sstevel@tonic-gate if (alph == 1)
477c478bd9Sstevel@tonic-gate cv[nv++] = inp;
487c478bd9Sstevel@tonic-gate if (alph > 6)
497c478bd9Sstevel@tonic-gate *inp = 0;
507c478bd9Sstevel@tonic-gate oldc = c;
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate #if D1
537c478bd9Sstevel@tonic-gate fprintf(stderr, "%d args set up\n", nv);
547c478bd9Sstevel@tonic-gate #endif
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate sv0 = dup(0);
577c478bd9Sstevel@tonic-gate close(0);
587c478bd9Sstevel@tonic-gate if (open(argig, 0) != 0)
597c478bd9Sstevel@tonic-gate err("Can't read fgrep index %s", argig);
607c478bd9Sstevel@tonic-gate sv1 = dup(1);
617c478bd9Sstevel@tonic-gate close(1);
627c478bd9Sstevel@tonic-gate if (creat(gfile, 0666) != 1)
637c478bd9Sstevel@tonic-gate err("Can't write fgrep output %s", gfile);
647c478bd9Sstevel@tonic-gate fgrep(nv, cv);
657c478bd9Sstevel@tonic-gate #if D1
667c478bd9Sstevel@tonic-gate fprintf(stderr, "fgrep returned, output is..\n");
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate close(0);
697c478bd9Sstevel@tonic-gate dup(sv0);
707c478bd9Sstevel@tonic-gate close(sv0);
717c478bd9Sstevel@tonic-gate close(1);
727c478bd9Sstevel@tonic-gate dup(sv1);
737c478bd9Sstevel@tonic-gate close(sv1);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate #if D1
777c478bd9Sstevel@tonic-gate fprintf(stderr, "back from fgrep\n");
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate gf = fopen(gfile, "r");
807c478bd9Sstevel@tonic-gate if (gf == NULL)
817c478bd9Sstevel@tonic-gate err("can't read fgrep output %s", gfile);
82*11a8fa6cSceastha while (fgets(line, 100, gf) == line) {
837c478bd9Sstevel@tonic-gate line[100] = 0;
847c478bd9Sstevel@tonic-gate #if D1
857c478bd9Sstevel@tonic-gate fprintf(stderr, "read line as //%s//\n", line);
867c478bd9Sstevel@tonic-gate #endif
87*11a8fa6cSceastha for (s = line; *s && (*s != '\t'); s++)
88*11a8fa6cSceastha ;
89*11a8fa6cSceastha if (*s == '\t') {
907c478bd9Sstevel@tonic-gate *s++ = '\n';
917c478bd9Sstevel@tonic-gate *s++ = 0;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate if (line[0])
947c478bd9Sstevel@tonic-gate strcat(out, line);
957c478bd9Sstevel@tonic-gate #if D1
967c478bd9Sstevel@tonic-gate fprintf(stderr, "out now /%s/\n", out);
977c478bd9Sstevel@tonic-gate #endif
987c478bd9Sstevel@tonic-gate while (*s) s++;
997c478bd9Sstevel@tonic-gate #if D1
1007c478bd9Sstevel@tonic-gate fprintf(stderr, "line %o s %o s-1 %o\n", line, s, s[-1]);
1017c478bd9Sstevel@tonic-gate #endif
1027c478bd9Sstevel@tonic-gate if (s[-1] != '\n')
103*11a8fa6cSceastha while (!feof(gf) && getc(gf) != '\n')
104*11a8fa6cSceastha ;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate fclose(gf);
1077c478bd9Sstevel@tonic-gate #if D1
1087c478bd9Sstevel@tonic-gate fprintf(stderr, "back from reading %, out %s\n", out);
1097c478bd9Sstevel@tonic-gate #else
1107c478bd9Sstevel@tonic-gate unlink(gfile);
1117c478bd9Sstevel@tonic-gate #endif
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate
114*11a8fa6cSceastha void
clfgrep(void)115*11a8fa6cSceastha clfgrep(void)
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate if (gfile[0])
1187c478bd9Sstevel@tonic-gate unlink(gfile);
1197c478bd9Sstevel@tonic-gate }
120