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