drop whitespace at end of line.
[heimdal.git] / lib / roken / rkpty.c
bloba978062e556a4178a853929e7d6c904b18719332
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "config.h"
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <util.h>
40 #include "roken.h"
41 #include <getarg.h>
43 struct command {
44 enum { CMD_EXPECT = 0, CMD_SEND } type;
45 char *str;
46 struct command *next;
53 static struct command *commands, **next = &commands;
55 static int verbose;
56 static int master;
57 static int slave;
58 static char line[256] = { 0 };
60 static void
61 open_pty(void)
63 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
64 if(openpty(&master, &slave, line, 0, 0) == 0)
65 return;
66 #endif /* HAVE_OPENPTY .... */
67 /* more cases, like open /dev/ptmx, etc */
69 exit(77);
76 static char *
77 iscmd(const char *buf, const char *s)
79 size_t len = strlen(s);
80 if (strncmp(buf, s, len) != 0)
81 return NULL;
82 return estrdup(buf + len);
85 static void
86 parse_configuration(const char *fn)
88 struct command *c;
89 char s[1024];
90 char *str;
91 int lineno = 0;
92 FILE *cmd;
94 cmd = fopen(fn, "r");
95 if (cmd == NULL)
96 err(1, "open: %s", fn);
98 while (fgets(s, sizeof(s), cmd) != NULL) {
100 s[strcspn(s, "#\n")] = '\0';
101 lineno++;
103 c = calloc(1, sizeof(*c));
104 if (c == NULL)
105 errx(1, "malloc");
107 (*next) = c;
108 next = &(c->next);
110 if ((str = iscmd(s, "expect ")) != NULL) {
111 c->type = CMD_EXPECT;
112 c->str = str;
113 } else if ((str = iscmd(s, "send ")) != NULL) {
114 c->type = CMD_SEND;
115 c->str = str;
116 } else
117 errx(1, "Invalid command on line %d: %s", lineno, s);
120 fclose(cmd);
128 static int
129 eval_parent(pid_t pid)
131 struct command *c;
132 char in;
133 size_t len = 0;
134 ssize_t sret;
136 for (c = commands; c != NULL; c = c->next) {
137 switch(c->type) {
138 case CMD_EXPECT:
139 len = 0;
140 while((sret = read(master, &in, sizeof(in))) > 0) {
141 if (verbose)
142 printf("%c", in);
143 if (c->str[len] != in) {
144 len = 0;
145 continue;
147 len++;
148 if (c->str[len] == '\0')
149 break;
151 if (sret <= 0)
152 errx(1, "end command while waiting for %s", c->str);
153 break;
154 case CMD_SEND: {
155 size_t i = 0;
157 if (verbose)
158 printf("[output]");
160 len = strlen(c->str);
162 while (i < len) {
163 if (c->str[i] == '\\' && i < len - 1) {
164 char ctrl;
165 i++;
166 switch(c->str[i]) {
167 case 'n': ctrl = '\n'; break;
168 case 'r': ctrl = '\r'; break;
169 case 't': ctrl = '\t'; break;
170 default:
171 errx(1, "unknown control char %c", c->str[i]);
173 if (net_write(master, &ctrl, 1) != 1)
174 errx(1, "command refused input");
175 } else {
176 if (net_write(master, &c->str[i], 1) != 1)
177 errx(1, "command refused input");
179 i++;
181 break;
183 default:
184 abort();
187 while(read(master, &in, sizeof(in)) > 0)
188 if (verbose)
189 printf("%c", in);
192 * Fetch status from child
195 int ret, status;
197 ret = waitpid(pid, &status, 0);
198 if (ret == -1)
199 err(1, "waitpid");
200 if (WIFEXITED(status) && WEXITSTATUS(status))
201 return WEXITSTATUS(status);
202 else if (WIFSIGNALED(status)) {
203 printf("killed by signal: %d\n", WTERMSIG(status));
204 return 1;
207 return 0;
214 static struct getargs args[] = {
215 { "verbose", 'v', arg_flag, &verbose, "verbose debugging" }
218 static void
219 usage(int ret)
221 arg_printusage (args, sizeof(args)/sizeof(*args), NULL, "infile command..");
222 exit (ret);
226 main(int argc, char **argv)
228 int optidx = 0;
229 pid_t pid;
231 setprogname(argv[0]);
233 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx))
234 usage(1);
236 argv += optidx;
237 argc -= optidx;
239 if (argc < 2)
240 errx(1, "to few arguments");
242 parse_configuration(argv[0]);
244 argv += 1;
245 argc -= 1;
248 open_pty();
250 pid = fork();
251 switch (pid) {
252 case -1:
253 err(1, "Failed to fork");
254 case 0:
256 if(setsid()<0)
257 err(1, "setsid");
259 dup2(slave, STDIN_FILENO);
260 dup2(slave, STDOUT_FILENO);
261 dup2(slave, STDERR_FILENO);
262 closefrom(STDERR_FILENO + 1);
264 execvp(argv[0], argv); /* add NULL to end of array ? */
265 err(1, "Failed to exec: %s", argv[0]);
266 default:
267 close(slave);
269 return eval_parent(pid);