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
35 #include "system/filesys.h"
36 #include "system/wait.h"
44 #ifdef HAVE_BSD_LIBUTIL_H
45 #include <bsd/libutil.h>
46 #elif defined HAVE_LIBUTIL_H
52 #endif /* STREAMPTY */
59 const char progname
[] = "unknown program";
61 static void err(int eval
, const char *fmt
, ...)
63 int err_errno
= errno
;
66 fprintf(stderr
, "%s: ", progname
);
68 vfprintf(stderr
, fmt
, ap
);
70 fprintf(stderr
, ": %s\n", strerror(err_errno
));
74 static void errx(int eval
, const char *fmt
, ...)
78 fprintf(stderr
, "%s: ", progname
);
80 vfprintf(stderr
, fmt
, ap
);
82 fprintf(stderr
, "\n");
89 enum { CMD_EXPECT
= 0, CMD_SEND
, CMD_PASSWORD
} type
;
99 static struct command
*commands
, **next
= &commands
;
101 static sig_atomic_t alarmset
= 0;
103 static int opt_timeout
= 10;
104 static int opt_verbose
;
108 static char line
[256] = { 0 };
110 static void caught_signal(int signo
)
116 static void open_pty(void)
119 printf("implement open_pty\n");
122 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
123 if(openpty(&master
, &slave
, line
, 0, 0) == 0)
125 #endif /* HAVE_OPENPTY .... */
137 for(q
= clone
; *q
; q
++){
138 master
= open(*q
, O_RDWR
);
146 strlcpy(line
, ptsname(master
), sizeof(line
));
147 slave
= open(line
, O_RDWR
);
149 errx(1, "failed to open slave when using %s", *q
);
150 ioctl(slave
, I_PUSH
, "ptem");
151 ioctl(slave
, I_PUSH
, "ldterm");
157 #endif /* STREAMSPTY */
159 /* more cases, like open /dev/ptmx, etc */
168 static char *iscmd(const char *buf
, const char *s
)
170 size_t len
= strlen(s
);
172 if (strncmp(buf
, s
, len
) != 0) {
176 return strdup(buf
+ len
);
179 /*******************************************************************
180 A write wrapper that will deal with EINTR.
181 ********************************************************************/
183 static ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
188 ret
= write(fd
, buf
, count
);
189 #if defined(EWOULDBLOCK)
190 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
192 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
197 static void parse_configuration(const char *fn
)
202 unsigned int lineno
= 0;
205 cmd
= fopen(fn
, "r");
207 err(1, "open: %s", fn
);
209 while (fgets(s
, sizeof(s
), cmd
) != NULL
) {
211 s
[strcspn(s
, "#\n")] = '\0';
214 c
= calloc(1, sizeof(*c
));
222 if ((str
= iscmd(s
, "expect ")) != NULL
) {
223 c
->type
= CMD_EXPECT
;
225 } else if ((str
= iscmd(s
, "send ")) != NULL
) {
228 } else if ((str
= iscmd(s
, "password ")) != NULL
) {
229 c
->type
= CMD_PASSWORD
;
232 errx(1, "Invalid command on line %d: %s", lineno
, s
);
238 /* A wrapper to close als file descriptors above the given fd */
239 static int sys_closefrom(int fd
)
241 int num
= getdtablesize();
247 for (; fd
<= num
; fd
++) {
259 static int eval_parent(pid_t pid
)
266 for (c
= commands
; c
!= NULL
; c
= c
->next
) {
270 printf("[expecting %s]\n", c
->str
);
274 while((sret
= read(master
, &in
, sizeof(in
))) > 0) {
277 if (c
->str
[len
] != in
) {
282 if (c
->str
[len
] == '\0') {
287 if (alarmset
== SIGALRM
) {
288 errx(1, "timeout waiting for %s (line %u)",
290 } else if (alarmset
) {
291 errx(1, "got a signal %d waiting for %s (line %u)",
292 (int)alarmset
, c
->str
, c
->lineno
);
296 errx(1, "end command while waiting for %s (line %u)",
303 const char *msg
= (c
->type
== CMD_PASSWORD
) ? "****" : c
->str
;
306 printf("[send %s]\n", msg
);
309 len
= strlen(c
->str
);
312 if (c
->str
[i
] == '\\' && i
< len
- 1) {
327 "unknown control char %c (line %u)",
331 if (sys_write(master
, &ctrl
, 1) != 1) {
332 errx(1, "command refused input (line %u)", c
->lineno
);
335 if (sys_write(master
, &c
->str
[i
], 1) != 1) {
336 errx(1, "command refused input (line %u)", c
->lineno
);
348 while(read(master
, &in
, sizeof(in
)) > 0) {
353 printf("[end of program]\n");
357 * Fetch status from child
362 ret
= waitpid(pid
, &status
, 0);
367 if (WIFEXITED(status
) && WEXITSTATUS(status
)) {
368 return WEXITSTATUS(status
);
369 } else if (WIFSIGNALED(status
)) {
370 printf("killed by signal: %d\n", WTERMSIG(status
));
381 struct poptOption long_options
[] = {
383 {"timeout", 't', POPT_ARG_INT
, &opt_timeout
, 't'},
384 {"verbose", 'v', POPT_ARG_NONE
, &opt_verbose
, 'v'},
388 int main(int argc
, const char **argv
)
393 const char *instruction_file
;
396 char * const *program_args
;
398 pc
= poptGetContext("texpect",
402 POPT_CONTEXT_POSIXMEHARDER
);
405 poptPrintHelp(pc
, stderr
, 0);
409 while ((optidx
= poptGetNextOpt(pc
)) != -1) {
413 instruction_file
= poptGetArg(pc
);
414 args
= poptGetArgs(pc
);
415 program_args
= (char * const *)discard_const_p(char *, args
);
416 program
= program_args
[0];
421 printf("Using instruction_file: %s\n", instruction_file
);
422 printf("Executing '%s' ", program
);
423 for (i
= 0; program_args
&& program_args
[i
] != NULL
; i
++) {
424 printf("'%s' ", program_args
[i
]);
429 parse_configuration(instruction_file
);
436 err(1, "Failed to fork");
442 dup2(slave
, STDIN_FILENO
);
443 dup2(slave
, STDOUT_FILENO
);
444 dup2(slave
, STDERR_FILENO
);
446 sys_closefrom(STDERR_FILENO
+ 1);
448 /* texpect <expect_instructions> <progname> [<args>] */
449 execvp(program
, program_args
);
450 err(1, "Failed to exec: %s", program
);
456 sa
.sa_handler
= caught_signal
;
458 sigemptyset (&sa
.sa_mask
);
460 sigaction(SIGALRM
, &sa
, NULL
);
463 return eval_parent(pid
);