xref: /illumos-gate/usr/src/cmd/refer/glue4.c (revision d7c57852794a9ac50d4b6a9c4161cb10919658aa)
111a8fa6cSceastha /*
2*d7c57852SGary Mills  * Copyright 2017 Gary Mills
311a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
411a8fa6cSceastha  * Use is subject to license terms.
511a8fa6cSceastha  */
611a8fa6cSceastha 
77c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
87c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate /*
117c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
127c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
137c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
147c478bd9Sstevel@tonic-gate  */
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate extern char gfile[];
217c478bd9Sstevel@tonic-gate 
2211a8fa6cSceastha void
grepcall(char * in,char * out,char * arg)2311a8fa6cSceastha grepcall(char *in, char *out, char *arg)
247c478bd9Sstevel@tonic-gate {
257c478bd9Sstevel@tonic-gate 	char line[200], *s, argig[100], *cv[50];
267c478bd9Sstevel@tonic-gate 	char *inp, inb[500];
277c478bd9Sstevel@tonic-gate 	FILE *qf, *gf;
28*d7c57852SGary Mills 	int c, alph = 0, nv = 0;
297c478bd9Sstevel@tonic-gate 	int sv0, sv1;
307c478bd9Sstevel@tonic-gate 	strcpy(argig, arg);
317c478bd9Sstevel@tonic-gate 	strcat(argig, ".ig");
327c478bd9Sstevel@tonic-gate 	strcpy(inp = inb, in);
337c478bd9Sstevel@tonic-gate 	if (gfile[0] == 0)
347c478bd9Sstevel@tonic-gate 		sprintf(gfile, "/tmp/rj%dg", getpid());
3511a8fa6cSceastha 	for (cv[nv++] = "fgrep"; c = *inp; inp++) {
367c478bd9Sstevel@tonic-gate 		if (c == ' ')
377c478bd9Sstevel@tonic-gate 			c = *inp = 0;
387c478bd9Sstevel@tonic-gate 		else if (isupper(c))
397c478bd9Sstevel@tonic-gate 			*inp = tolower(c);
407c478bd9Sstevel@tonic-gate 		alph = (c == 0) ? 0 : alph+1;
417c478bd9Sstevel@tonic-gate 		if (alph == 1)
427c478bd9Sstevel@tonic-gate 			cv[nv++] = inp;
437c478bd9Sstevel@tonic-gate 		if (alph > 6)
447c478bd9Sstevel@tonic-gate 			*inp = 0;
457c478bd9Sstevel@tonic-gate 	}
467c478bd9Sstevel@tonic-gate 	{
477c478bd9Sstevel@tonic-gate 		sv0 = dup(0);
487c478bd9Sstevel@tonic-gate 		close(0);
497c478bd9Sstevel@tonic-gate 		if (open(argig, 0) != 0)
507c478bd9Sstevel@tonic-gate 			err("Can't read fgrep index %s", argig);
517c478bd9Sstevel@tonic-gate 		sv1 = dup(1);
527c478bd9Sstevel@tonic-gate 		close(1);
537c478bd9Sstevel@tonic-gate 		if (creat(gfile, 0666) != 1)
547c478bd9Sstevel@tonic-gate 			err("Can't write fgrep output %s", gfile);
557c478bd9Sstevel@tonic-gate 		fgrep(nv, cv);
567c478bd9Sstevel@tonic-gate 		close(0);
577c478bd9Sstevel@tonic-gate 		dup(sv0);
587c478bd9Sstevel@tonic-gate 		close(sv0);
597c478bd9Sstevel@tonic-gate 		close(1);
607c478bd9Sstevel@tonic-gate 		dup(sv1);
617c478bd9Sstevel@tonic-gate 		close(sv1);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	gf = fopen(gfile, "r");
657c478bd9Sstevel@tonic-gate 	if (gf == NULL)
667c478bd9Sstevel@tonic-gate 		err("can't read fgrep output %s", gfile);
6711a8fa6cSceastha 	while (fgets(line, 100, gf) == line) {
687c478bd9Sstevel@tonic-gate 		line[100] = 0;
6911a8fa6cSceastha 		for (s = line; *s && (*s != '\t'); s++)
7011a8fa6cSceastha 			;
7111a8fa6cSceastha 		if (*s == '\t') {
727c478bd9Sstevel@tonic-gate 			*s++ = '\n';
737c478bd9Sstevel@tonic-gate 			*s++ = 0;
747c478bd9Sstevel@tonic-gate 		}
757c478bd9Sstevel@tonic-gate 		if (line[0])
767c478bd9Sstevel@tonic-gate 			strcat(out, line);
777c478bd9Sstevel@tonic-gate 		while (*s) s++;
787c478bd9Sstevel@tonic-gate 		if (s[-1] != '\n')
7911a8fa6cSceastha 			while (!feof(gf) && getc(gf) != '\n')
8011a8fa6cSceastha 				;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	fclose(gf);
837c478bd9Sstevel@tonic-gate 	unlink(gfile);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
8611a8fa6cSceastha void
clfgrep(void)8711a8fa6cSceastha clfgrep(void)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	if (gfile[0])
907c478bd9Sstevel@tonic-gate 		unlink(gfile);
917c478bd9Sstevel@tonic-gate }
92