1 /* $NetBSD: commands.c,v 1.4 2011/09/15 11:46:19 blymn Exp $ */
4 * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
8 * This code has been donated to The NetBSD Foundation by the Author.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
41 #include "command_table.h"
43 extern int cmdpipe
[2];
44 extern int slvpipe
[2];
46 static void report_type(returns_enum_t
);
47 static void report_message(int, const char *);
50 * Match the passed command string and execute the associated test
54 command_execute(char *func
, int nargs
, char **args
)
60 if (strcasecmp(func
, commands
[i
].name
) == 0) {
61 /* matched function */
62 commands
[i
].func(nargs
, args
);
68 report_status("UNKNOWN_FUNCTION");
72 * Report an pointer value back to the director
80 asprintf(&string
, "NULL");
82 asprintf(&string
, "%p", ptr
);
83 report_status(string
);
88 * Report an integer value back to the director
95 asprintf(&string
, "%d", value
);
96 report_status(string
);
101 * Report either an ERR or OK back to the director
104 report_return(int status
)
107 report_type(ret_err
);
108 else if (status
== OK
)
111 report_status("INVALID_RETURN");
115 * Report the type back to the director via the command pipe
118 report_type(returns_enum_t return_type
)
123 if (write(slvpipe
[WRITE_PIPE
], &type
, sizeof(int)) < 0)
124 err(1, "command pipe write for status type failed");
129 * Report the number of returns back to the director via the command pipe
132 report_count(int count
)
137 if (write(slvpipe
[WRITE_PIPE
], &type
, sizeof(int)) < 0)
138 err(1, "command pipe write for count type failed");
140 if (write(slvpipe
[WRITE_PIPE
], &count
, sizeof(int)) < 0)
141 err(1, "command pipe write for count");
145 * Report the status back to the director via the command pipe
148 report_status(const char *status
)
150 report_message(ret_string
, status
);
154 * Report an error message back to the director via the command pipe.
157 report_error(const char *status
)
159 report_message(ret_slave_error
, status
);
163 * Report the message with the given type back to the director via the
167 report_message(int type
, const char *status
)
171 len
= strlen(status
);
173 if (write(slvpipe
[WRITE_PIPE
], &type
, sizeof(int)) < 0)
174 err(1, "command pipe write for message type failed");
176 if (write(slvpipe
[WRITE_PIPE
], &len
, sizeof(int)) < 0)
177 err(1, "command pipe write for message length failed");
179 if (write(slvpipe
[WRITE_PIPE
], status
, len
) < 0)
180 err(1, "command pipe write of message data failed");
184 * Report a string of chtype back to the director via the command pipe.
187 report_byte(chtype c
)
192 string
[1] = A_NORMAL
| '\0';
197 * Report a string of chtype back to the director via the command pipe.
200 report_nstr(chtype
*string
)
208 while ((*p
++ & __CHARTEXT
) != 0) {
212 len
++; /* add in the termination chtype */
213 len
*= sizeof(chtype
);
216 if (write(slvpipe
[WRITE_PIPE
], &type
, sizeof(int)) < 0)
217 err(1, "%s: command pipe write for status type failed",
220 if (write(slvpipe
[WRITE_PIPE
], &len
, sizeof(int)) < 0)
221 err(1, "%s: command pipe write for status length failed",
224 if (write(slvpipe
[WRITE_PIPE
], string
, len
) < 0)
225 err(1, "%s: command pipe write of status data failed",
230 * Check the number of args we received are what we expect. Return an
231 * error if they do not match.
234 check_arg_count(int nargs
, int expected
)
236 if (nargs
!= expected
) {
238 report_error("INCORRECT_ARGUMENT_NUMBER");