1*11be35a1SLionel Sambuc /* $NetBSD: commands.c,v 1.4 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
32*11be35a1SLionel Sambuc #include <curses.h>
33*11be35a1SLionel Sambuc #include <string.h>
34*11be35a1SLionel Sambuc #include <stdlib.h>
35*11be35a1SLionel Sambuc #include <stdio.h>
36*11be35a1SLionel Sambuc #include <unistd.h>
37*11be35a1SLionel Sambuc #include <err.h>
38*11be35a1SLionel Sambuc #include <sys/types.h>
39*11be35a1SLionel Sambuc #include "returns.h"
40*11be35a1SLionel Sambuc #include "slave.h"
41*11be35a1SLionel Sambuc #include "command_table.h"
42*11be35a1SLionel Sambuc
43*11be35a1SLionel Sambuc extern int cmdpipe[2];
44*11be35a1SLionel Sambuc extern int slvpipe[2];
45*11be35a1SLionel Sambuc
46*11be35a1SLionel Sambuc static void report_type(returns_enum_t);
47*11be35a1SLionel Sambuc static void report_message(int, const char *);
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc /*
50*11be35a1SLionel Sambuc * Match the passed command string and execute the associated test
51*11be35a1SLionel Sambuc * function.
52*11be35a1SLionel Sambuc */
53*11be35a1SLionel Sambuc void
command_execute(char * func,int nargs,char ** args)54*11be35a1SLionel Sambuc command_execute(char *func, int nargs, char **args)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc size_t i;
57*11be35a1SLionel Sambuc
58*11be35a1SLionel Sambuc i = 0;
59*11be35a1SLionel Sambuc while (i < ncmds) {
60*11be35a1SLionel Sambuc if (strcasecmp(func, commands[i].name) == 0) {
61*11be35a1SLionel Sambuc /* matched function */
62*11be35a1SLionel Sambuc commands[i].func(nargs, args);
63*11be35a1SLionel Sambuc return;
64*11be35a1SLionel Sambuc }
65*11be35a1SLionel Sambuc i++;
66*11be35a1SLionel Sambuc }
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc report_status("UNKNOWN_FUNCTION");
69*11be35a1SLionel Sambuc }
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc /*
72*11be35a1SLionel Sambuc * Report an pointer value back to the director
73*11be35a1SLionel Sambuc */
74*11be35a1SLionel Sambuc void
report_ptr(void * ptr)75*11be35a1SLionel Sambuc report_ptr(void *ptr)
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc char *string;
78*11be35a1SLionel Sambuc
79*11be35a1SLionel Sambuc if (ptr == NULL)
80*11be35a1SLionel Sambuc asprintf(&string, "NULL");
81*11be35a1SLionel Sambuc else
82*11be35a1SLionel Sambuc asprintf(&string, "%p", ptr);
83*11be35a1SLionel Sambuc report_status(string);
84*11be35a1SLionel Sambuc free(string);
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc
87*11be35a1SLionel Sambuc /*
88*11be35a1SLionel Sambuc * Report an integer value back to the director
89*11be35a1SLionel Sambuc */
90*11be35a1SLionel Sambuc void
report_int(int value)91*11be35a1SLionel Sambuc report_int(int value)
92*11be35a1SLionel Sambuc {
93*11be35a1SLionel Sambuc char *string;
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc asprintf(&string, "%d", value);
96*11be35a1SLionel Sambuc report_status(string);
97*11be35a1SLionel Sambuc free(string);
98*11be35a1SLionel Sambuc }
99*11be35a1SLionel Sambuc
100*11be35a1SLionel Sambuc /*
101*11be35a1SLionel Sambuc * Report either an ERR or OK back to the director
102*11be35a1SLionel Sambuc */
103*11be35a1SLionel Sambuc void
report_return(int status)104*11be35a1SLionel Sambuc report_return(int status)
105*11be35a1SLionel Sambuc {
106*11be35a1SLionel Sambuc if (status == ERR)
107*11be35a1SLionel Sambuc report_type(ret_err);
108*11be35a1SLionel Sambuc else if (status == OK)
109*11be35a1SLionel Sambuc report_type(ret_ok);
110*11be35a1SLionel Sambuc else
111*11be35a1SLionel Sambuc report_status("INVALID_RETURN");
112*11be35a1SLionel Sambuc }
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc /*
115*11be35a1SLionel Sambuc * Report the type back to the director via the command pipe
116*11be35a1SLionel Sambuc */
117*11be35a1SLionel Sambuc static void
report_type(returns_enum_t return_type)118*11be35a1SLionel Sambuc report_type(returns_enum_t return_type)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc int type;
121*11be35a1SLionel Sambuc
122*11be35a1SLionel Sambuc type = return_type;
123*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
124*11be35a1SLionel Sambuc err(1, "command pipe write for status type failed");
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc }
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc /*
129*11be35a1SLionel Sambuc * Report the number of returns back to the director via the command pipe
130*11be35a1SLionel Sambuc */
131*11be35a1SLionel Sambuc void
report_count(int count)132*11be35a1SLionel Sambuc report_count(int count)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc int type;
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc type = ret_count;
137*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
138*11be35a1SLionel Sambuc err(1, "command pipe write for count type failed");
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &count, sizeof(int)) < 0)
141*11be35a1SLionel Sambuc err(1, "command pipe write for count");
142*11be35a1SLionel Sambuc }
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc /*
145*11be35a1SLionel Sambuc * Report the status back to the director via the command pipe
146*11be35a1SLionel Sambuc */
147*11be35a1SLionel Sambuc void
report_status(const char * status)148*11be35a1SLionel Sambuc report_status(const char *status)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc report_message(ret_string, status);
151*11be35a1SLionel Sambuc }
152*11be35a1SLionel Sambuc
153*11be35a1SLionel Sambuc /*
154*11be35a1SLionel Sambuc * Report an error message back to the director via the command pipe.
155*11be35a1SLionel Sambuc */
156*11be35a1SLionel Sambuc void
report_error(const char * status)157*11be35a1SLionel Sambuc report_error(const char *status)
158*11be35a1SLionel Sambuc {
159*11be35a1SLionel Sambuc report_message(ret_slave_error, status);
160*11be35a1SLionel Sambuc }
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambuc /*
163*11be35a1SLionel Sambuc * Report the message with the given type back to the director via the
164*11be35a1SLionel Sambuc * command pipe.
165*11be35a1SLionel Sambuc */
166*11be35a1SLionel Sambuc static void
report_message(int type,const char * status)167*11be35a1SLionel Sambuc report_message(int type, const char *status)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc int len;
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc len = strlen(status);
172*11be35a1SLionel Sambuc
173*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
174*11be35a1SLionel Sambuc err(1, "command pipe write for message type failed");
175*11be35a1SLionel Sambuc
176*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
177*11be35a1SLionel Sambuc err(1, "command pipe write for message length failed");
178*11be35a1SLionel Sambuc
179*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], status, len) < 0)
180*11be35a1SLionel Sambuc err(1, "command pipe write of message data failed");
181*11be35a1SLionel Sambuc }
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc /*
184*11be35a1SLionel Sambuc * Report a string of chtype back to the director via the command pipe.
185*11be35a1SLionel Sambuc */
186*11be35a1SLionel Sambuc void
report_byte(chtype c)187*11be35a1SLionel Sambuc report_byte(chtype c)
188*11be35a1SLionel Sambuc {
189*11be35a1SLionel Sambuc chtype string[2];
190*11be35a1SLionel Sambuc
191*11be35a1SLionel Sambuc string[0] = c;
192*11be35a1SLionel Sambuc string[1] = A_NORMAL | '\0';
193*11be35a1SLionel Sambuc report_nstr(string);
194*11be35a1SLionel Sambuc }
195*11be35a1SLionel Sambuc
196*11be35a1SLionel Sambuc /*
197*11be35a1SLionel Sambuc * Report a string of chtype back to the director via the command pipe.
198*11be35a1SLionel Sambuc */
199*11be35a1SLionel Sambuc void
report_nstr(chtype * string)200*11be35a1SLionel Sambuc report_nstr(chtype *string)
201*11be35a1SLionel Sambuc {
202*11be35a1SLionel Sambuc int len, type;
203*11be35a1SLionel Sambuc chtype *p;
204*11be35a1SLionel Sambuc
205*11be35a1SLionel Sambuc len = 0;
206*11be35a1SLionel Sambuc p = string;
207*11be35a1SLionel Sambuc
208*11be35a1SLionel Sambuc while ((*p++ & __CHARTEXT) != 0) {
209*11be35a1SLionel Sambuc len++;
210*11be35a1SLionel Sambuc }
211*11be35a1SLionel Sambuc
212*11be35a1SLionel Sambuc len++; /* add in the termination chtype */
213*11be35a1SLionel Sambuc len *= sizeof(chtype);
214*11be35a1SLionel Sambuc
215*11be35a1SLionel Sambuc type = ret_byte;
216*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
217*11be35a1SLionel Sambuc err(1, "%s: command pipe write for status type failed",
218*11be35a1SLionel Sambuc __func__);
219*11be35a1SLionel Sambuc
220*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
221*11be35a1SLionel Sambuc err(1, "%s: command pipe write for status length failed",
222*11be35a1SLionel Sambuc __func__);
223*11be35a1SLionel Sambuc
224*11be35a1SLionel Sambuc if (write(slvpipe[WRITE_PIPE], string, len) < 0)
225*11be35a1SLionel Sambuc err(1, "%s: command pipe write of status data failed",
226*11be35a1SLionel Sambuc __func__);
227*11be35a1SLionel Sambuc }
228*11be35a1SLionel Sambuc
229*11be35a1SLionel Sambuc /*
230*11be35a1SLionel Sambuc * Check the number of args we received are what we expect. Return an
231*11be35a1SLionel Sambuc * error if they do not match.
232*11be35a1SLionel Sambuc */
233*11be35a1SLionel Sambuc int
check_arg_count(int nargs,int expected)234*11be35a1SLionel Sambuc check_arg_count(int nargs, int expected)
235*11be35a1SLionel Sambuc {
236*11be35a1SLionel Sambuc if (nargs != expected) {
237*11be35a1SLionel Sambuc report_count(1);
238*11be35a1SLionel Sambuc report_error("INCORRECT_ARGUMENT_NUMBER");
239*11be35a1SLionel Sambuc return(1);
240*11be35a1SLionel Sambuc }
241*11be35a1SLionel Sambuc
242*11be35a1SLionel Sambuc return(0);
243*11be35a1SLionel Sambuc }
244