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