1 /* $NetBSD: slave.c,v 1.2 2011/04/21 10:23:50 blymn Exp $ */ 2 3 /*- 4 * Copyright 2009 Brett Lymn <blymn@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 if (type != ret_null) 104 args[argslen] = malloc(len + 1); 105 106 if (args[argslen] == NULL) 107 err(1, "slave alloc of %d bytes for" 108 " args failed", len); 109 110 if (len == 0) { 111 if (type == ret_null) 112 args[argslen] = NULL; 113 else 114 args[argslen][0] = '\0'; 115 } else { 116 read(cmdpipe[READ_PIPE], args[argslen], 117 len); 118 if (type != ret_byte) 119 args[argslen][len] = '\0'; 120 121 if (len == 6) { 122 if (strcmp(args[argslen], 123 "STDSCR") == 0) { 124 ret = asprintf(&tmpbuf, 125 "%td", 126 stdscr); 127 if (ret < 0) 128 err(2, 129 "asprintf of stdscr failed"); 130 free(args[argslen]); 131 args[argslen] = tmpbuf; 132 } 133 } 134 } 135 136 argslen++; 137 } 138 } 139 while(len >= 0); 140 141 142 command_execute(cmdbuf, argslen, args); 143 144 if (args != NULL) { 145 for (i = 0; i < argslen; i++) 146 free(args[i]); 147 148 free(args); 149 } 150 } 151 } 152 153 int 154 main(int argc, char *argv[]) 155 { 156 WINDOW *mainscr; 157 158 sscanf(argv[1], "%d", &cmdpipe[0]); 159 sscanf(argv[2], "%d", &cmdpipe[1]); 160 sscanf(argv[3], "%d", &slvpipe[0]); 161 sscanf(argv[4], "%d", &slvpipe[1]); 162 163 mainscr = initscr(); 164 if (mainscr == NULL) { 165 fprintf(stderr, "initscr failed\n"); 166 exit(1); 167 } 168 169 process_commands(mainscr); 170 171 exit(0); 172 } 173