2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
36 #ifndef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
39 #ifdef HAVE_SYS_WAIT_H
60 #endif /* STREAMPTY */
67 enum { CMD_EXPECT = 0, CMD_SEND, CMD_PASSWORD } type;
77 static struct command *commands, **next = &commands;
79 static sig_atomic_t alarmset = 0;
81 static int opt_timeout = 10;
82 static int opt_verbose;
86 static char line[256] = { 0 };
88 static void caught_signal(int signo)
94 static void open_pty(void)
97 printf("implement open_pty\n");
100 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
101 if(openpty(&master, &slave, line, 0, 0) == 0)
103 #endif /* HAVE_OPENPTY .... */
115 for(q = clone; *q; q++){
116 master = open(*q, O_RDWR);
124 strlcpy(line, ptsname(master), sizeof(line));
125 slave = open(line, O_RDWR);
127 errx(1, "failed to open slave when using %s", *q);
128 ioctl(slave, I_PUSH, "ptem");
129 ioctl(slave, I_PUSH, "ldterm");
135 #endif /* STREAMSPTY */
137 /* more cases, like open /dev/ptmx, etc */
146 static char *iscmd(const char *buf, const char *s)
148 size_t len = strlen(s);
150 if (strncmp(buf, s, len) != 0) {
154 return strdup(buf + len);
157 /*******************************************************************
158 A write wrapper that will deal with EINTR.
159 ********************************************************************/
161 static ssize_t sys_write(int fd, const void *buf, size_t count)
166 ret = write(fd, buf, count);
167 #if defined(EWOULDBLOCK)
168 } while (ret == -1 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK));
170 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
175 static void parse_configuration(const char *fn)
180 unsigned int lineno = 0;
183 cmd = fopen(fn, "r");
185 err(1, "open: %s", fn);
187 while (fgets(s, sizeof(s), cmd) != NULL) {
189 s[strcspn(s, "#\n")] = '\0';
192 c = calloc(1, sizeof(*c));
200 if ((str = iscmd(s, "expect ")) != NULL) {
201 c->type = CMD_EXPECT;
203 } else if ((str = iscmd(s, "send ")) != NULL) {
206 } else if ((str = iscmd(s, "password ")) != NULL) {
207 c->type = CMD_PASSWORD;
210 errx(1, "Invalid command on line %d: %s", lineno, s);
216 /* A wrapper to close als file descriptors above the given fd */
217 static int sys_closefrom(int fd)
219 int num = getdtablesize();
225 for (; fd <= num; fd++) {
237 static int eval_parent(pid_t pid)
244 for (c = commands; c != NULL; c = c->next) {
248 printf("[expecting %s]\n", c->str);
252 while((sret = read(master, &in, sizeof(in))) > 0) {
255 if (c->str[len] != in) {
260 if (c->str[len] == '\0') {
265 if (alarmset == SIGALRM) {
266 errx(1, "timeout waiting for %s (line %u)",
268 } else if (alarmset) {
269 errx(1, "got a signal %d waiting for %s (line %u)",
270 (int)alarmset, c->str, c->lineno);
274 errx(1, "end command while waiting for %s (line %u)",
281 const char *msg = (c->type == CMD_PASSWORD) ? "****" : c->str;
284 printf("[send %s]\n", msg);
287 len = strlen(c->str);
290 if (c->str[i] == '\\' && i < len - 1) {
305 "unknown control char %c (line %u)",
309 if (sys_write(master, &ctrl, 1) != 1) {
310 errx(1, "command refused input (line %u)", c->lineno);
313 if (sys_write(master, &c->str[i], 1) != 1) {
314 errx(1, "command refused input (line %u)", c->lineno);
326 while(read(master, &in, sizeof(in)) > 0) {
331 printf("[end of program]\n");
335 * Fetch status from child
340 ret = waitpid(pid, &status, 0);
345 if (WIFEXITED(status) && WEXITSTATUS(status)) {
346 return WEXITSTATUS(status);
347 } else if (WIFSIGNALED(status)) {
348 printf("killed by signal: %d\n", WTERMSIG(status));
359 struct poptOption long_options[] = {
361 {"timeout", 't', POPT_ARG_INT, &opt_timeout, 't'},
362 {"verbose", 'v', POPT_ARG_NONE, &opt_verbose, 'v'},
366 int main(int argc, const char **argv)
371 const char *instruction_file;
373 char* const *program_args;
375 pc = poptGetContext("texpect",
379 POPT_CONTEXT_POSIXMEHARDER);
382 poptPrintHelp(pc, stderr, 0);
386 while ((optidx = poptGetNextOpt(pc)) != -1) {
390 instruction_file = poptGetArg(pc);
391 program_args = poptGetArgs(pc);
392 program = program_args[0];
397 printf("Using instruction_file: %s\n", instruction_file);
398 printf("Executing '%s' ", program);
399 for (i = 0; program_args && program_args[i] != NULL; i++) {
400 printf("'%s' ", program_args[i]);
405 parse_configuration(instruction_file);
412 err(1, "Failed to fork");
418 dup2(slave, STDIN_FILENO);
419 dup2(slave, STDOUT_FILENO);
420 dup2(slave, STDERR_FILENO);
422 sys_closefrom(STDERR_FILENO + 1);
424 /* texpect <expect_instructions> <progname> [<args>] */
425 execvp(program, program_args);
426 err(1, "Failed to exec: %s", program);
432 sa.sa_handler = caught_signal;
434 sigemptyset (&sa.sa_mask);
436 sigaction(SIGALRM, &sa, NULL);
439 return eval_parent(pid);