1*11be35a1SLionel Sambuc /* $NetBSD: slave.c,v 1.6 2011/09/15 11:46:19 blymn Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
5*11be35a1SLionel Sambuc *
6*11be35a1SLionel Sambuc * All rights reserved.
7*11be35a1SLionel Sambuc *
8*11be35a1SLionel Sambuc * This code has been donated to The NetBSD Foundation by the Author.
9*11be35a1SLionel Sambuc *
10*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc * are met:
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. The name of the author may not be used to endorse or promote products
16*11be35a1SLionel Sambuc * derived from this software withough specific prior written permission
17*11be35a1SLionel Sambuc *
18*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*11be35a1SLionel Sambuc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*11be35a1SLionel Sambuc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*11be35a1SLionel Sambuc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*11be35a1SLionel Sambuc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*11be35a1SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*11be35a1SLionel Sambuc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc *
29*11be35a1SLionel Sambuc *
30*11be35a1SLionel Sambuc */
31*11be35a1SLionel Sambuc #include <fcntl.h>
32*11be35a1SLionel Sambuc #include <sys/ioctl.h>
33*11be35a1SLionel Sambuc #include <unistd.h>
34*11be35a1SLionel Sambuc #include <err.h>
35*11be35a1SLionel Sambuc #include <stdio.h>
36*11be35a1SLionel Sambuc #include <stdlib.h>
37*11be35a1SLionel Sambuc #include <string.h>
38*11be35a1SLionel Sambuc #include <curses.h>
39*11be35a1SLionel Sambuc #include "returns.h"
40*11be35a1SLionel Sambuc #include "slave.h"
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc int cmdpipe[2];
43*11be35a1SLionel Sambuc int slvpipe[2];
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc #if 0
46*11be35a1SLionel Sambuc static const char *returns_enum_names[] = {
47*11be35a1SLionel Sambuc "unused", "numeric", "string", "byte", "ERR", "OK", "NULL", "not NULL",
48*11be35a1SLionel Sambuc "variable"
49*11be35a1SLionel Sambuc };
50*11be35a1SLionel Sambuc #endif
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc /*
53*11be35a1SLionel Sambuc * Read the command pipe for the function to execute, gather the args
54*11be35a1SLionel Sambuc * and then process the command.
55*11be35a1SLionel Sambuc */
56*11be35a1SLionel Sambuc static void
process_commands(WINDOW * mainscr)57*11be35a1SLionel Sambuc process_commands(WINDOW *mainscr)
58*11be35a1SLionel Sambuc {
59*11be35a1SLionel Sambuc int len, maxlen, argslen, i, ret, type;
60*11be35a1SLionel Sambuc char *cmdbuf, *tmpbuf, **args, **tmpargs;
61*11be35a1SLionel Sambuc
62*11be35a1SLionel Sambuc len = maxlen = 30;
63*11be35a1SLionel Sambuc if ((cmdbuf = malloc(maxlen)) == NULL)
64*11be35a1SLionel Sambuc err(1, "slave cmdbuf malloc failed");
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc while(1) {
67*11be35a1SLionel Sambuc if (read(cmdpipe[READ_PIPE], &type, sizeof(int)) < 0)
68*11be35a1SLionel Sambuc err(1, "slave command type read failed");
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc if (type != ret_string)
71*11be35a1SLionel Sambuc errx(1, "Unexpected type for command, got %d", type);
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc if (read(cmdpipe[READ_PIPE], &len, sizeof(int)) < 0)
74*11be35a1SLionel Sambuc err(1, "slave command len read failed");
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc if ((len + 1) > maxlen) {
77*11be35a1SLionel Sambuc maxlen = len + 1;
78*11be35a1SLionel Sambuc if ((tmpbuf = realloc(cmdbuf, maxlen)) == NULL)
79*11be35a1SLionel Sambuc err(1, "slave cmdbuf realloc to %d "
80*11be35a1SLionel Sambuc "bytes failed", maxlen);
81*11be35a1SLionel Sambuc cmdbuf = tmpbuf;
82*11be35a1SLionel Sambuc }
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc if (read(cmdpipe[READ_PIPE], cmdbuf, len) < 0)
85*11be35a1SLionel Sambuc err(1, "slave command read failed");
86*11be35a1SLionel Sambuc cmdbuf[len] = '\0';
87*11be35a1SLionel Sambuc argslen = 0;
88*11be35a1SLionel Sambuc args = NULL;
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc do {
91*11be35a1SLionel Sambuc if (read(cmdpipe[READ_PIPE], &type, sizeof(int)) < 0)
92*11be35a1SLionel Sambuc err(1, "slave arg type read failed");
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc if (read(cmdpipe[READ_PIPE], &len, sizeof(int)) < 0)
95*11be35a1SLionel Sambuc err(1, "slave arg len read failed");
96*11be35a1SLionel Sambuc
97*11be35a1SLionel Sambuc if (len >= 0) {
98*11be35a1SLionel Sambuc tmpargs = realloc(args,
99*11be35a1SLionel Sambuc (argslen + 1) * sizeof(char *));
100*11be35a1SLionel Sambuc if (tmpargs == NULL)
101*11be35a1SLionel Sambuc err(1, "slave realloc of args array "
102*11be35a1SLionel Sambuc "failed");
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc args = tmpargs;
105*11be35a1SLionel Sambuc if (type != ret_null) {
106*11be35a1SLionel Sambuc args[argslen] = malloc(len + 1);
107*11be35a1SLionel Sambuc
108*11be35a1SLionel Sambuc if (args[argslen] == NULL)
109*11be35a1SLionel Sambuc err(1, "slave alloc of %d bytes"
110*11be35a1SLionel Sambuc " for args failed", len);
111*11be35a1SLionel Sambuc }
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambuc if (len == 0) {
114*11be35a1SLionel Sambuc if (type == ret_null)
115*11be35a1SLionel Sambuc args[argslen] = NULL;
116*11be35a1SLionel Sambuc else
117*11be35a1SLionel Sambuc args[argslen][0] = '\0';
118*11be35a1SLionel Sambuc } else {
119*11be35a1SLionel Sambuc read(cmdpipe[READ_PIPE], args[argslen],
120*11be35a1SLionel Sambuc len);
121*11be35a1SLionel Sambuc if (type != ret_byte)
122*11be35a1SLionel Sambuc args[argslen][len] = '\0';
123*11be35a1SLionel Sambuc
124*11be35a1SLionel Sambuc if (len == 6) {
125*11be35a1SLionel Sambuc if (strcmp(args[argslen],
126*11be35a1SLionel Sambuc "STDSCR") == 0) {
127*11be35a1SLionel Sambuc ret = asprintf(&tmpbuf,
128*11be35a1SLionel Sambuc "%p",
129*11be35a1SLionel Sambuc stdscr);
130*11be35a1SLionel Sambuc if (ret < 0)
131*11be35a1SLionel Sambuc err(2,
132*11be35a1SLionel Sambuc "asprintf of stdscr failed");
133*11be35a1SLionel Sambuc free(args[argslen]);
134*11be35a1SLionel Sambuc args[argslen] = tmpbuf;
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc }
137*11be35a1SLionel Sambuc }
138*11be35a1SLionel Sambuc
139*11be35a1SLionel Sambuc argslen++;
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc while(len >= 0);
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc command_execute(cmdbuf, argslen, args);
145*11be35a1SLionel Sambuc
146*11be35a1SLionel Sambuc if (args != NULL) {
147*11be35a1SLionel Sambuc for (i = 0; i < argslen; i++)
148*11be35a1SLionel Sambuc free(args[i]);
149*11be35a1SLionel Sambuc
150*11be35a1SLionel Sambuc free(args);
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc }
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc int
main(int argc,char * argv[])156*11be35a1SLionel Sambuc main(int argc, char *argv[])
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc WINDOW *mainscr;
159*11be35a1SLionel Sambuc
160*11be35a1SLionel Sambuc if (argc != 5) {
161*11be35a1SLionel Sambuc fprintf(stderr, "Usage: %s <cmdin> <cmdout> <slvin> slvout>\n",
162*11be35a1SLionel Sambuc getprogname());
163*11be35a1SLionel Sambuc return 0;
164*11be35a1SLionel Sambuc }
165*11be35a1SLionel Sambuc sscanf(argv[1], "%d", &cmdpipe[0]);
166*11be35a1SLionel Sambuc sscanf(argv[2], "%d", &cmdpipe[1]);
167*11be35a1SLionel Sambuc sscanf(argv[3], "%d", &slvpipe[0]);
168*11be35a1SLionel Sambuc sscanf(argv[4], "%d", &slvpipe[1]);
169*11be35a1SLionel Sambuc
170*11be35a1SLionel Sambuc mainscr = initscr();
171*11be35a1SLionel Sambuc if (mainscr == NULL)
172*11be35a1SLionel Sambuc err(1, "initscr failed");
173*11be35a1SLionel Sambuc
174*11be35a1SLionel Sambuc process_commands(mainscr);
175*11be35a1SLionel Sambuc
176*11be35a1SLionel Sambuc return 0;
177*11be35a1SLionel Sambuc }
178