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