Remove building with NOCRYPTO option
[minix.git] / tests / lib / libcurses / slave / commands.c
blob2f26024579d11a74061de471c7dbc472da9c3a59
1 /* $NetBSD: commands.c,v 1.4 2011/09/15 11:46:19 blymn Exp $ */
3 /*-
4 * Copyright 2009 Brett Lymn <blymn@NetBSD.org>
6 * All rights reserved.
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
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
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.
32 #include <curses.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <err.h>
38 #include <sys/types.h>
39 #include "returns.h"
40 #include "slave.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
51 * function.
53 void
54 command_execute(char *func, int nargs, char **args)
56 size_t i;
58 i = 0;
59 while (i < ncmds) {
60 if (strcasecmp(func, commands[i].name) == 0) {
61 /* matched function */
62 commands[i].func(nargs, args);
63 return;
65 i++;
68 report_status("UNKNOWN_FUNCTION");
72 * Report an pointer value back to the director
74 void
75 report_ptr(void *ptr)
77 char *string;
79 if (ptr == NULL)
80 asprintf(&string, "NULL");
81 else
82 asprintf(&string, "%p", ptr);
83 report_status(string);
84 free(string);
88 * Report an integer value back to the director
90 void
91 report_int(int value)
93 char *string;
95 asprintf(&string, "%d", value);
96 report_status(string);
97 free(string);
101 * Report either an ERR or OK back to the director
103 void
104 report_return(int status)
106 if (status == ERR)
107 report_type(ret_err);
108 else if (status == OK)
109 report_type(ret_ok);
110 else
111 report_status("INVALID_RETURN");
115 * Report the type back to the director via the command pipe
117 static void
118 report_type(returns_enum_t return_type)
120 int type;
122 type = 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
131 void
132 report_count(int count)
134 int type;
136 type = ret_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
147 void
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.
156 void
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
164 * command pipe.
166 static void
167 report_message(int type, const char *status)
169 int len;
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.
186 void
187 report_byte(chtype c)
189 chtype string[2];
191 string[0] = c;
192 string[1] = A_NORMAL | '\0';
193 report_nstr(string);
197 * Report a string of chtype back to the director via the command pipe.
199 void
200 report_nstr(chtype *string)
202 int len, type;
203 chtype *p;
205 len = 0;
206 p = string;
208 while ((*p++ & __CHARTEXT) != 0) {
209 len++;
212 len++; /* add in the termination chtype */
213 len *= sizeof(chtype);
215 type = ret_byte;
216 if (write(slvpipe[WRITE_PIPE], &type, sizeof(int)) < 0)
217 err(1, "%s: command pipe write for status type failed",
218 __func__);
220 if (write(slvpipe[WRITE_PIPE], &len, sizeof(int)) < 0)
221 err(1, "%s: command pipe write for status length failed",
222 __func__);
224 if (write(slvpipe[WRITE_PIPE], string, len) < 0)
225 err(1, "%s: command pipe write of status data failed",
226 __func__);
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) {
237 report_count(1);
238 report_error("INCORRECT_ARGUMENT_NUMBER");
239 return(1);
242 return(0);