commands.c revision 1.1 1 1.1 blymn /* $NetBSD: commands.c,v 1.1 2011/04/10 09:55:10 blymn Exp $ */
2 1.1 blymn
3 1.1 blymn /*-
4 1.1 blymn * Copyright 2009 Brett Lymn <blymn (at) NetBSD.org>
5 1.1 blymn *
6 1.1 blymn * All rights reserved.
7 1.1 blymn *
8 1.1 blymn * This code has been donated to The NetBSD Foundation by the Author.
9 1.1 blymn *
10 1.1 blymn * Redistribution and use in source and binary forms, with or without
11 1.1 blymn * modification, are permitted provided that the following conditions
12 1.1 blymn * are met:
13 1.1 blymn * 1. Redistributions of source code must retain the above copyright
14 1.1 blymn * notice, this list of conditions and the following disclaimer.
15 1.1 blymn * 2. The name of the author may not be used to endorse or promote products
16 1.1 blymn * derived from this software withough specific prior written permission
17 1.1 blymn *
18 1.1 blymn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 blymn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 blymn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 blymn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 blymn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 blymn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 blymn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 blymn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 blymn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 blymn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 blymn *
29 1.1 blymn *
30 1.1 blymn */
31 1.1 blymn
32 1.1 blymn #include <curses.h>
33 1.1 blymn #include <string.h>
34 1.1 blymn #include <stdlib.h>
35 1.1 blymn #include <stdio.h>
36 1.1 blymn #include <sys/types.h>
37 1.1 blymn #include "returns.h"
38 1.1 blymn #include "slave.h"
39 1.1 blymn #include "command_table.h"
40 1.1 blymn
41 1.1 blymn extern int cmdpipe[2];
42 1.1 blymn extern int slvpipe[2];
43 1.1 blymn
44 1.1 blymn static void report_type(returns_enum_t);
45 1.1 blymn static void report_message(int, char *);
46 1.1 blymn
47 1.1 blymn /*
48 1.1 blymn * Match the passed command string and execute the associated test
49 1.1 blymn * function.
50 1.1 blymn */
51 1.1 blymn void
52 1.1 blymn command_execute(char *func, int nargs, char **args)
53 1.1 blymn {
54 1.1 blymn size_t i;
55 1.1 blymn
56 1.1 blymn i = 0;
57 1.1 blymn while (i < ncmds) {
58 1.1 blymn if (strcasecmp(func, commands[i].name) == 0) {
59 1.1 blymn /* matched function */
60 1.1 blymn commands[i].func(nargs, args);
61 1.1 blymn return;
62 1.1 blymn }
63 1.1 blymn i++;
64 1.1 blymn }
65 1.1 blymn
66 1.1 blymn report_status("UNKNOWN_FUNCTION");
67 1.1 blymn }
68 1.1 blymn
69 1.1 blymn /*
70 1.1 blymn * Report an pointer value back to the director
71 1.1 blymn */
72 1.1 blymn void
73 1.1 blymn report_ptr(void *ptr)
74 1.1 blymn {
75 1.1 blymn char *string;
76 1.1 blymn
77 1.1 blymn asprintf(&string, "%td", ptr);
78 1.1 blymn report_status(string);
79 1.1 blymn free(string);
80 1.1 blymn }
81 1.1 blymn
82 1.1 blymn /*
83 1.1 blymn * Report an integer value back to the director
84 1.1 blymn */
85 1.1 blymn void
86 1.1 blymn report_int(int value)
87 1.1 blymn {
88 1.1 blymn char *string;
89 1.1 blymn
90 1.1 blymn asprintf(&string, "%d", value);
91 1.1 blymn report_status(string);
92 1.1 blymn free(string);
93 1.1 blymn }
94 1.1 blymn
95 1.1 blymn /*
96 1.1 blymn * Report either an ERR or OK back to the director
97 1.1 blymn */
98 1.1 blymn void
99 1.1 blymn report_return(int status)
100 1.1 blymn {
101 1.1 blymn if (status == ERR)
102 1.1 blymn report_type(ret_err);
103 1.1 blymn else if (status == OK)
104 1.1 blymn report_type(ret_ok);
105 1.1 blymn else
106 1.1 blymn report_status("INVALID_RETURN");
107 1.1 blymn }
108 1.1 blymn
109 1.1 blymn /*
110 1.1 blymn * Report the type back to the director via the command pipe
111 1.1 blymn */
112 1.1 blymn static void
113 1.1 blymn report_type(returns_enum_t return_type)
114 1.1 blymn {
115 1.1 blymn int type;
116 1.1 blymn
117 1.1 blymn type = return_type;
118 1.1 blymn if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0) {
119 1.1 blymn perror("command pipe write for status type failed");
120 1.1 blymn exit(1);
121 1.1 blymn }
122 1.1 blymn
123 1.1 blymn }
124 1.1 blymn
125 1.1 blymn /*
126 1.1 blymn * Report the number of returns back to the director via the command pipe
127 1.1 blymn */
128 1.1 blymn void
129 1.1 blymn report_count(int count)
130 1.1 blymn {
131 1.1 blymn int type;
132 1.1 blymn
133 1.1 blymn type = ret_count;
134 1.1 blymn if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0) {
135 1.1 blymn perror("command pipe write for count type failed");
136 1.1 blymn exit(1);
137 1.1 blymn }
138 1.1 blymn
139 1.1 blymn if (write(slvpipe[WRITE_PIPE], &count, sizeof(int)) < 0) {
140 1.1 blymn perror("command pipe write for count");
141 1.1 blymn exit(1);
142 1.1 blymn }
143 1.1 blymn }
144 1.1 blymn
145 1.1 blymn /*
146 1.1 blymn * Report the status back to the director via the command pipe
147 1.1 blymn */
148 1.1 blymn void
149 1.1 blymn report_status(char *status)
150 1.1 blymn {
151 1.1 blymn report_message(ret_string, status);
152 1.1 blymn }
153 1.1 blymn
154 1.1 blymn /*
155 1.1 blymn * Report an error message back to the director via the command pipe.
156 1.1 blymn */
157 1.1 blymn void
158 1.1 blymn report_error(char *status)
159 1.1 blymn {
160 1.1 blymn report_message(ret_slave_error, status);
161 1.1 blymn }
162 1.1 blymn
163 1.1 blymn /*
164 1.1 blymn * Report the message with the given type back to the director via the
165 1.1 blymn * command pipe.
166 1.1 blymn */
167 1.1 blymn static void
168 1.1 blymn report_message(int type, char *status)
169 1.1 blymn {
170 1.1 blymn int len;
171 1.1 blymn
172 1.1 blymn len = strlen(status);
173 1.1 blymn
174 1.1 blymn if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0) {
175 1.1 blymn perror("command pipe write for message type failed");
176 1.1 blymn exit(1);
177 1.1 blymn }
178 1.1 blymn
179 1.1 blymn if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0) {
180 1.1 blymn perror("command pipe write for message length failed");
181 1.1 blymn exit(1);
182 1.1 blymn }
183 1.1 blymn
184 1.1 blymn if (write(slvpipe[WRITE_PIPE], status, len) < 0) {
185 1.1 blymn perror("command pipe write of message data failed");
186 1.1 blymn exit(1);
187 1.1 blymn }
188 1.1 blymn }
189 1.1 blymn
190 1.1 blymn /*
191 1.1 blymn * Report a string of chtype back to the director via the command pipe.
192 1.1 blymn */
193 1.1 blymn void
194 1.1 blymn report_nstr(chtype *string)
195 1.1 blymn {
196 1.1 blymn int len, type;
197 1.1 blymn chtype *p;
198 1.1 blymn
199 1.1 blymn len = 0;
200 1.1 blymn p = string;
201 1.1 blymn
202 1.1 blymn while ((*p++ & __CHARTEXT) != 0) {
203 1.1 blymn len++;
204 1.1 blymn }
205 1.1 blymn
206 1.1 blymn type = ret_byte;
207 1.1 blymn if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0) {
208 1.1 blymn perror("report_nstr: command pipe write for status type failed");
209 1.1 blymn exit(1);
210 1.1 blymn }
211 1.1 blymn
212 1.1 blymn if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0) {
213 1.1 blymn perror("report_nstr: command pipe write for status length failed");
214 1.1 blymn exit(1);
215 1.1 blymn }
216 1.1 blymn
217 1.1 blymn if (write(slvpipe[WRITE_PIPE], string, len) < 0) {
218 1.1 blymn perror("report_nstr: command pipe write of status data failed");
219 1.1 blymn exit(1);
220 1.1 blymn }
221 1.1 blymn }
222 1.1 blymn
223 1.1 blymn /*
224 1.1 blymn * Check the number of args we received are what we expect. Return an
225 1.1 blymn * error if they do not match.
226 1.1 blymn */
227 1.1 blymn int
228 1.1 blymn check_arg_count(int nargs, int expected)
229 1.1 blymn {
230 1.1 blymn if (nargs != expected) {
231 1.1 blymn report_count(1);
232 1.1 blymn report_error("INCORRECT_ARGUMENT_NUMBER");
233 1.1 blymn return(1);
234 1.1 blymn }
235 1.1 blymn
236 1.1 blymn return(0);
237 1.1 blymn }
238