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