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 /*	from OpenSolaris "glue3.c	1.7	05/06/02 SMI"	*/
17 
18 /*
19  * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
20  *
21  * Sccsid @(#)glue3.c	1.5 (gritter) 9/7/08
22  */
23 
24 
25 #include "refer..c"
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/wait.h>
29 #include <stdlib.h>
30 #include <inttypes.h>
31 #define move(x, y) close(y); dup(x); close(x);
32 
33 int
corout(char * in,char * out,char * rprog,char * arg,int outlen)34 corout(char *in, char *out, char *rprog, char *arg, int outlen)
35 {
36 	int pipev[2], fr1, fr2, fw1, fw2, n;
37 	int	pid;
38 
39 # if D1
40 	fprintf(stderr, "in corout, rprog /%s/ in /%s/\n",
41 		rprog ? rprog : "", strlen(in) ? in : "");
42 # endif
43 
44 	if (strcmp (rprog, "hunt") ==0)
45 		return(callhunt(in, out, arg, outlen));
46 	if (strcmp (rprog, "deliv")==0)
47 		return(dodeliv(in, out, arg, outlen));
48 	pipe (pipev);
49 	fr1= pipev[0];
50 	fw1 = pipev[1];
51 	pipe (pipev);
52 	fr2= pipev[0];
53 	fw2 = pipev[1];
54 	if ((pid = fork())==0)
55 	{
56 		close (fw1);
57 		close (fr2);
58 		move (fr1, 0);
59 		move (fw2, 1);
60 		if (rprog[0]!= '/')
61 			chdir(REFDIR);
62 		execl(rprog, "deliv", arg, NULL);
63 		err("Can't run %s", rprog);
64 	}
65 	close(fw2);
66 	close(fr1);
67 	if (strlen(in) > 0)
68 		write (fw1, in , strlen(in));
69 	close(fw1);
70 	while (wait(0) != pid);
71 	n = read (fr2, out, outlen);
72 	out[n]=0;
73 	close(fr2);
74 	return 0;
75 }
76 
77 # define ALEN 50
78 
79 int
callhunt(char * in,char * out,char * arg,int outlen)80 callhunt(char *in, char *out, char *arg, int outlen)
81 {
82 	char *argv[20], abuff[ALEN];
83 	int argc;
84 	extern char one[];
85 	extern int onelen;
86 	argv[0] = "hunt";
87 	argv[1] = "-i";
88 	argv[2] = in;
89 	argv[3] = "-t";
90 	argv[4] = out;
91 	argv[5] = (char *)(intptr_t)outlen;
92 	argv[6] = "-T";
93 	argv[7] = "-F1";
94 	argv[8] = "-o";
95 	argv[9] = one;
96 	argv[10] = (char *)(intptr_t)onelen;
97 	argv[11] = abuff;
98 	if (strlen(arg) > ALEN)
99 		err("abuff not big enough %d", strlen(arg));
100 	strcpy (abuff,arg);
101 	argc = 6;
102 	huntmain (argc,argv);
103 	return(0);
104 }
105 
106 int
dodeliv(char * in,char * out,char * arg,int outlen)107 dodeliv(char *in, char *out, char *arg, int outlen)
108 {
109 	char *mout;
110 	int mlen;
111 # if D1
112 	fprintf(stderr, "in dodeliv, arg /%s/\n", arg?arg:"");
113 # endif
114 	if (arg && arg[0])
115 		chdir(arg);
116 
117 	mlen = findline(in, &mout, outlen,0L);
118 
119 	if (mlen>0)
120 	{
121 		strncpy(out, mout, outlen);
122 		free (mout);
123 	}
124 	restodir();
125 	return 0;
126 }
127