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