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