xref: /illumos-gate/usr/src/cmd/refer/glue3.c (revision 2a8bcb4e)
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 #include "refer..c"
167c478bd9Sstevel@tonic-gate #include <string.h>
177c478bd9Sstevel@tonic-gate #define	move(x, y) close(y); dup(x); close(x);
187c478bd9Sstevel@tonic-gate 
19*11a8fa6cSceastha extern void err();
20*11a8fa6cSceastha extern long findline();
21*11a8fa6cSceastha extern void huntmain();
22*11a8fa6cSceastha extern void restodir();
23*11a8fa6cSceastha 
24*11a8fa6cSceastha static int callhunt(char *, char *, char *, int);
25*11a8fa6cSceastha static int dodeliv(char *, char *, char *, int);
26*11a8fa6cSceastha 
27*11a8fa6cSceastha int
corout(char * in,char * out,char * rprog,char * arg,int outlen)28*11a8fa6cSceastha corout(char *in, char *out, char *rprog, char *arg, int outlen)
297c478bd9Sstevel@tonic-gate {
307c478bd9Sstevel@tonic-gate 	int pipev[2], fr1, fr2, fw1, fw2, n;
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #if D1
337c478bd9Sstevel@tonic-gate 	fprintf(stderr, "in corout, rprog /%s/ in /%s/\n",
347c478bd9Sstevel@tonic-gate 	    rprog ? rprog : "", strlen(in) ? in : "");
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate 	if (strcmp(rprog, "hunt") == 0)
387c478bd9Sstevel@tonic-gate 		return (callhunt(in, out, arg, outlen));
397c478bd9Sstevel@tonic-gate 	if (strcmp(rprog, "deliv") == 0)
407c478bd9Sstevel@tonic-gate 		return (dodeliv(in, out, arg, outlen));
417c478bd9Sstevel@tonic-gate 	pipe(pipev);
427c478bd9Sstevel@tonic-gate 	fr1 = pipev[0];
437c478bd9Sstevel@tonic-gate 	fw1 = pipev[1];
447c478bd9Sstevel@tonic-gate 	pipe(pipev);
457c478bd9Sstevel@tonic-gate 	fr2 = pipev[0];
467c478bd9Sstevel@tonic-gate 	fw2 = pipev[1];
47*11a8fa6cSceastha 	if (fork() == 0) {
487c478bd9Sstevel@tonic-gate 		close(fw1);
497c478bd9Sstevel@tonic-gate 		close(fr2);
507c478bd9Sstevel@tonic-gate 		move(fr1, 0);
517c478bd9Sstevel@tonic-gate 		move(fw2, 1);
527c478bd9Sstevel@tonic-gate 		if (rprog[0] != '/')
537c478bd9Sstevel@tonic-gate 			chdir("/usr/lib/refer");
547c478bd9Sstevel@tonic-gate 		execl(rprog, "deliv", arg, 0);
557c478bd9Sstevel@tonic-gate 		err(gettext("Can't run %s"), rprog);
567c478bd9Sstevel@tonic-gate 	}
577c478bd9Sstevel@tonic-gate 	close(fw2);
587c478bd9Sstevel@tonic-gate 	close(fr1);
597c478bd9Sstevel@tonic-gate 	if (strlen(in) > 0)
607c478bd9Sstevel@tonic-gate 		write(fw1, in, strlen(in));
617c478bd9Sstevel@tonic-gate 	close(fw1);
627c478bd9Sstevel@tonic-gate 	wait(0);
637c478bd9Sstevel@tonic-gate 	n = read(fr2, out, outlen);
647c478bd9Sstevel@tonic-gate 	out[n] = 0;
657c478bd9Sstevel@tonic-gate 	close(fr2);
66*11a8fa6cSceastha 	return (0);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	ALEN 50
707c478bd9Sstevel@tonic-gate 
71*11a8fa6cSceastha static int
callhunt(char * in,char * out,char * arg,int outlen)72*11a8fa6cSceastha callhunt(char *in, char *out, char *arg, int outlen)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	char *argv[20], abuff[ALEN];
757c478bd9Sstevel@tonic-gate 	extern int typeindex;
767c478bd9Sstevel@tonic-gate 	int argc;
777c478bd9Sstevel@tonic-gate 	extern char one[];
787c478bd9Sstevel@tonic-gate 	extern int onelen;
797c478bd9Sstevel@tonic-gate 	argv[0] = "hunt";
807c478bd9Sstevel@tonic-gate 	argv[1] = "-i";
817c478bd9Sstevel@tonic-gate 	argv[2] = in;
827c478bd9Sstevel@tonic-gate 	argv[3] = "-t";
837c478bd9Sstevel@tonic-gate 	argv[4] = out;
847c478bd9Sstevel@tonic-gate 	argv[5] = (char *)outlen;
857c478bd9Sstevel@tonic-gate 	argv[6] = "-T";
867c478bd9Sstevel@tonic-gate 	argv[7] = "-F1";
877c478bd9Sstevel@tonic-gate 	argv[8] = "-o";
887c478bd9Sstevel@tonic-gate 	argv[9] = one;
897c478bd9Sstevel@tonic-gate 	argv[10] = (char *)onelen;
907c478bd9Sstevel@tonic-gate 	argv[11] = abuff;
917c478bd9Sstevel@tonic-gate 	strcpy(abuff, arg);
927c478bd9Sstevel@tonic-gate 	if (strlen(abuff) > ALEN)
937c478bd9Sstevel@tonic-gate 		err("abuff not big enough %d", strlen(abuff));
947c478bd9Sstevel@tonic-gate 	argc = 6;
957c478bd9Sstevel@tonic-gate 	huntmain(argc, argv);
967c478bd9Sstevel@tonic-gate 	return (0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
99*11a8fa6cSceastha static int
dodeliv(char * in,char * out,char * arg,int outlen)100*11a8fa6cSceastha dodeliv(char *in, char *out, char *arg, int outlen)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	char *mout;
1037c478bd9Sstevel@tonic-gate 	int mlen;
1047c478bd9Sstevel@tonic-gate #if D1
1057c478bd9Sstevel@tonic-gate 	fprintf(stderr, "in dodeliv, arg /%s/\n", arg?arg:"");
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 	if (arg && arg[0])
1087c478bd9Sstevel@tonic-gate 		chdir(arg);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	mlen = findline(in, &mout, outlen, 0L);
1117c478bd9Sstevel@tonic-gate 
112*11a8fa6cSceastha 	if (mlen > 0) {
1137c478bd9Sstevel@tonic-gate 		strncpy(out, mout, outlen);
1147c478bd9Sstevel@tonic-gate 		free(mout);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 	restodir();
117*11a8fa6cSceastha 	return (0);
1187c478bd9Sstevel@tonic-gate }
119