Home | History | Annotate | Line # | Download | only in slave
slave.c revision 1.1
      1 /*	$NetBSD: slave.c,v 1.1 2011/04/10 09:55:10 blymn 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 #include <fcntl.h>
     32 #include <sys/ioctl.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <curses.h>
     37 #include "returns.h"
     38 #include "slave.h"
     39 
     40 int cmdpipe[2];
     41 int slvpipe[2];
     42 
     43 char *returns_enum_names[] = {
     44 	"unused", "numeric", "string", "byte", "ERR", "OK", "NULL", "not NULL",
     45 	"variable"
     46 };
     47 
     48 /*
     49  * Read the command pipe for the function to execute, gather the args
     50  * and then process the command.
     51  */
     52 void
     53 process_commands(WINDOW *mainscr)
     54 {
     55 	int len, maxlen, argslen, i, ret, type;
     56 	char *cmdbuf, *tmpbuf, **args, **tmpargs;
     57 	int farg;
     58 
     59 	len = maxlen = 30;
     60 	if ((cmdbuf = malloc(maxlen)) == NULL)
     61 		err(1, "slave cmdbuf malloc failed");
     62 
     63 	while(1) {
     64 		if (read(cmdpipe[READ_PIPE], &type, sizeof(int)) < 0)
     65 			err(1, "slave command type read failed");
     66 
     67 		if (type != ret_string)
     68 			err(1, "Unexpected type for command, got %d", type);
     69 
     70 		if (read(cmdpipe[READ_PIPE], &len, sizeof(int)) < 0)
     71 			err(1, "slave command len read failed");
     72 
     73 		if ((len + 1) > maxlen) {
     74 			maxlen = len + 1;
     75 			if ((tmpbuf = realloc(cmdbuf, maxlen)) == NULL)
     76 				err(1, "slave cmdbuf realloc to %d "
     77 				    "bytes failed", maxlen);
     78 			cmdbuf = tmpbuf;
     79 		}
     80 
     81 		if (read(cmdpipe[READ_PIPE], cmdbuf, len) < 0)
     82 			err(1, "slave command read failed");
     83 		cmdbuf[len] = '\0';
     84 
     85 		argslen = 0;
     86 		args = NULL;
     87 
     88 		do {
     89 			if (read(cmdpipe[READ_PIPE], &type, sizeof(int)) < 0)
     90 				err(1, "slave arg type read failed");
     91 
     92 			if (read(cmdpipe[READ_PIPE], &len, sizeof(int)) < 0)
     93 				err(1, "slave arg len read failed");
     94 
     95 			if (len >= 0) {
     96 				tmpargs = realloc(args,
     97 				    (argslen + 1) * sizeof(char *));
     98 				if (tmpargs == NULL)
     99 					err(1, "slave realloc of args array "
    100 					    "failed");
    101 
    102 				args = tmpargs;
    103 				args[argslen] = malloc(len + 1);
    104 
    105 				if (args[argslen] == NULL)
    106 					err(1, "slave alloc of %d bytes for"
    107 					    " args failed", len);
    108 
    109 				if (len == 0)
    110 					args[argslen][0] = '\0';
    111 				else {
    112 					read(cmdpipe[READ_PIPE], args[argslen],
    113 					     len);
    114 					if (type != ret_byte)
    115 						args[argslen][len] = '\0';
    116 
    117 					if (len == 6) {
    118 						if (strcmp(args[argslen],
    119 							   "STDSCR") == 0) {
    120 							ret = asprintf(&tmpbuf,
    121 								 "%td",
    122 								 stdscr);
    123 							if (ret < 0)
    124 								err(2,
    125 								    "asprintf of stdscr failed");
    126 							free(args[argslen]);
    127 							args[argslen] = tmpbuf;
    128 						}
    129 					}
    130 				}
    131 
    132 				argslen++;
    133 			}
    134 		}
    135 		while(len >= 0);
    136 
    137 
    138 		command_execute(cmdbuf, argslen, args);
    139 
    140 		if (args != NULL) {
    141 			for (i = 0; i < argslen; i++)
    142 				free(args[i]);
    143 
    144 			free(args);
    145 		}
    146 	}
    147 }
    148 
    149 int
    150 main(int argc, char *argv[])
    151 {
    152 	WINDOW *mainscr;
    153 
    154 	sscanf(argv[1], "%d", &cmdpipe[0]);
    155 	sscanf(argv[2], "%d", &cmdpipe[1]);
    156 	sscanf(argv[3], "%d", &slvpipe[0]);
    157 	sscanf(argv[4], "%d", &slvpipe[1]);
    158 
    159 	mainscr = initscr();
    160 	if (mainscr == NULL) {
    161 		fprintf(stderr, "initscr failed\n");
    162 		exit(1);
    163 	}
    164 
    165 	process_commands(mainscr);
    166 
    167 	exit(0);
    168 }
    169