xref: /titanic_52/usr/src/cmd/refer/glue3.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
13*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #include "refer..c"
20*7c478bd9Sstevel@tonic-gate #include <string.h>
21*7c478bd9Sstevel@tonic-gate #define move(x, y) close(y); dup(x); close(x);
22*7c478bd9Sstevel@tonic-gate 
23*7c478bd9Sstevel@tonic-gate corout(in, out, rprog, arg, outlen)
24*7c478bd9Sstevel@tonic-gate char *in, *out, *rprog;
25*7c478bd9Sstevel@tonic-gate {
26*7c478bd9Sstevel@tonic-gate 	int pipev[2], fr1, fr2, fw1, fw2, n;
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate # if D1
29*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "in corout, rprog /%s/ in /%s/\n",
30*7c478bd9Sstevel@tonic-gate 		rprog ? rprog : "", strlen(in) ? in : "");
31*7c478bd9Sstevel@tonic-gate # endif
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate 	if (strcmp (rprog, "hunt") ==0)
34*7c478bd9Sstevel@tonic-gate 		return(callhunt(in, out, arg, outlen));
35*7c478bd9Sstevel@tonic-gate 	if (strcmp (rprog, "deliv")==0)
36*7c478bd9Sstevel@tonic-gate 		return(dodeliv(in, out, arg, outlen));
37*7c478bd9Sstevel@tonic-gate 	pipe (pipev);
38*7c478bd9Sstevel@tonic-gate 	fr1= pipev[0];
39*7c478bd9Sstevel@tonic-gate 	fw1 = pipev[1];
40*7c478bd9Sstevel@tonic-gate 	pipe (pipev);
41*7c478bd9Sstevel@tonic-gate 	fr2= pipev[0];
42*7c478bd9Sstevel@tonic-gate 	fw2 = pipev[1];
43*7c478bd9Sstevel@tonic-gate 	if (fork()==0)
44*7c478bd9Sstevel@tonic-gate 	{
45*7c478bd9Sstevel@tonic-gate 		close (fw1);
46*7c478bd9Sstevel@tonic-gate 		close (fr2);
47*7c478bd9Sstevel@tonic-gate 		move (fr1, 0);
48*7c478bd9Sstevel@tonic-gate 		move (fw2, 1);
49*7c478bd9Sstevel@tonic-gate 		if (rprog[0]!= '/')
50*7c478bd9Sstevel@tonic-gate 			chdir("/usr/lib/refer");
51*7c478bd9Sstevel@tonic-gate 		execl(rprog, "deliv", arg, 0);
52*7c478bd9Sstevel@tonic-gate 		err(gettext("Can't run %s"), rprog);
53*7c478bd9Sstevel@tonic-gate 	}
54*7c478bd9Sstevel@tonic-gate 	close(fw2);
55*7c478bd9Sstevel@tonic-gate 	close(fr1);
56*7c478bd9Sstevel@tonic-gate 	if (strlen(in) > 0)
57*7c478bd9Sstevel@tonic-gate 		write (fw1, in , strlen(in));
58*7c478bd9Sstevel@tonic-gate 	close(fw1);
59*7c478bd9Sstevel@tonic-gate 	wait(0);
60*7c478bd9Sstevel@tonic-gate 	n = read (fr2, out, outlen);
61*7c478bd9Sstevel@tonic-gate 	out[n]=0;
62*7c478bd9Sstevel@tonic-gate 	close(fr2);
63*7c478bd9Sstevel@tonic-gate }
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate # define ALEN 50
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate callhunt(in, out, arg, outlen)
68*7c478bd9Sstevel@tonic-gate char *in, *out, *arg;
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	char *argv[20], abuff[ALEN];
71*7c478bd9Sstevel@tonic-gate 	extern int typeindex;
72*7c478bd9Sstevel@tonic-gate 	int argc;
73*7c478bd9Sstevel@tonic-gate 	extern char one[];
74*7c478bd9Sstevel@tonic-gate 	extern int onelen;
75*7c478bd9Sstevel@tonic-gate 	argv[0] = "hunt";
76*7c478bd9Sstevel@tonic-gate 	argv[1] = "-i";
77*7c478bd9Sstevel@tonic-gate 	argv[2] = in;
78*7c478bd9Sstevel@tonic-gate 	argv[3] = "-t";
79*7c478bd9Sstevel@tonic-gate 	argv[4] = out;
80*7c478bd9Sstevel@tonic-gate 	argv[5] = (char *)outlen;
81*7c478bd9Sstevel@tonic-gate 	argv[6] = "-T";
82*7c478bd9Sstevel@tonic-gate 	argv[7] = "-F1";
83*7c478bd9Sstevel@tonic-gate 	argv[8] = "-o";
84*7c478bd9Sstevel@tonic-gate 	argv[9] = one;
85*7c478bd9Sstevel@tonic-gate 	argv[10] = (char *)onelen;
86*7c478bd9Sstevel@tonic-gate 	argv[11] = abuff;
87*7c478bd9Sstevel@tonic-gate 	strcpy (abuff,arg);
88*7c478bd9Sstevel@tonic-gate 	if (strlen(abuff) > ALEN)
89*7c478bd9Sstevel@tonic-gate 		err("abuff not big enough %d", strlen(abuff));
90*7c478bd9Sstevel@tonic-gate 	argc = 6;
91*7c478bd9Sstevel@tonic-gate 	huntmain (argc,argv);
92*7c478bd9Sstevel@tonic-gate 	return(0);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate dodeliv(in, out, arg, outlen)
96*7c478bd9Sstevel@tonic-gate char *in, *out, *arg;
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	char *mout;
99*7c478bd9Sstevel@tonic-gate 	int mlen;
100*7c478bd9Sstevel@tonic-gate # if D1
101*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "in dodeliv, arg /%s/\n", arg?arg:"");
102*7c478bd9Sstevel@tonic-gate # endif
103*7c478bd9Sstevel@tonic-gate 	if (arg && arg[0])
104*7c478bd9Sstevel@tonic-gate 		chdir(arg);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	mlen = findline(in, &mout, outlen,0L);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	if (mlen>0)
109*7c478bd9Sstevel@tonic-gate 	{
110*7c478bd9Sstevel@tonic-gate 		strncpy(out, mout, outlen);
111*7c478bd9Sstevel@tonic-gate 		free (mout);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	restodir();
114*7c478bd9Sstevel@tonic-gate }
115