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