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