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