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 #include <ccan/err/err.h>
63 enum { CMD_EXPECT
= 0, CMD_SEND
, CMD_PASSWORD
} type
;
73 static struct command
*commands
, **next
= &commands
;
75 static sig_atomic_t alarmset
= 0;
77 static int opt_timeout
= 10;
78 static int opt_verbose
;
82 static char line
[256] = { 0 };
84 static void caught_signal(int signo
)
90 static void open_pty(void)
93 printf("implement open_pty\n");
96 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
97 if(openpty(&master
, &slave
, line
, 0, 0) == 0)
99 #endif /* HAVE_OPENPTY .... */
111 for(q
= clone
; *q
; q
++){
112 master
= open(*q
, O_RDWR
);
120 strlcpy(line
, ptsname(master
), sizeof(line
));
121 slave
= open(line
, O_RDWR
);
123 errx(1, "failed to open slave when using %s", *q
);
124 ioctl(slave
, I_PUSH
, "ptem");
125 ioctl(slave
, I_PUSH
, "ldterm");
131 #endif /* STREAMSPTY */
133 /* more cases, like open /dev/ptmx, etc */
142 static char *iscmd(const char *buf
, const char *s
)
144 size_t len
= strlen(s
);
146 if (strncmp(buf
, s
, len
) != 0) {
150 return strdup(buf
+ len
);
153 /*******************************************************************
154 A write wrapper that will deal with EINTR.
155 ********************************************************************/
157 static ssize_t
sys_write(int fd
, const void *buf
, size_t count
)
162 ret
= write(fd
, buf
, count
);
163 #if defined(EWOULDBLOCK)
164 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
|| errno
== EWOULDBLOCK
));
166 } while (ret
== -1 && (errno
== EINTR
|| errno
== EAGAIN
));
171 static void parse_configuration(const char *fn
)
176 unsigned int lineno
= 0;
179 cmd
= fopen(fn
, "r");
181 err(1, "open: %s", fn
);
183 while (fgets(s
, sizeof(s
), cmd
) != NULL
) {
185 s
[strcspn(s
, "#\n")] = '\0';
188 c
= calloc(1, sizeof(*c
));
196 if ((str
= iscmd(s
, "expect ")) != NULL
) {
197 c
->type
= CMD_EXPECT
;
199 } else if ((str
= iscmd(s
, "send ")) != NULL
) {
202 } else if ((str
= iscmd(s
, "password ")) != NULL
) {
203 c
->type
= CMD_PASSWORD
;
206 errx(1, "Invalid command on line %d: %s", lineno
, s
);
212 /* A wrapper to close als file descriptors above the given fd */
213 static int sys_closefrom(int fd
)
215 int num
= getdtablesize();
221 for (; fd
<= num
; fd
++) {
233 static int eval_parent(pid_t pid
)
240 for (c
= commands
; c
!= NULL
; c
= c
->next
) {
244 printf("[expecting %s]\n", c
->str
);
248 while((sret
= read(master
, &in
, sizeof(in
))) > 0) {
251 if (c
->str
[len
] != in
) {
256 if (c
->str
[len
] == '\0') {
261 if (alarmset
== SIGALRM
) {
262 errx(1, "timeout waiting for %s (line %u)",
264 } else if (alarmset
) {
265 errx(1, "got a signal %d waiting for %s (line %u)",
266 (int)alarmset
, c
->str
, c
->lineno
);
270 errx(1, "end command while waiting for %s (line %u)",
277 const char *msg
= (c
->type
== CMD_PASSWORD
) ? "****" : c
->str
;
280 printf("[send %s]\n", msg
);
283 len
= strlen(c
->str
);
286 if (c
->str
[i
] == '\\' && i
< len
- 1) {
301 "unknown control char %c (line %u)",
305 if (sys_write(master
, &ctrl
, 1) != 1) {
306 errx(1, "command refused input (line %u)", c
->lineno
);
309 if (sys_write(master
, &c
->str
[i
], 1) != 1) {
310 errx(1, "command refused input (line %u)", c
->lineno
);
322 while(read(master
, &in
, sizeof(in
)) > 0) {
327 printf("[end of program]\n");
331 * Fetch status from child
336 ret
= waitpid(pid
, &status
, 0);
341 if (WIFEXITED(status
) && WEXITSTATUS(status
)) {
342 return WEXITSTATUS(status
);
343 } else if (WIFSIGNALED(status
)) {
344 printf("killed by signal: %d\n", WTERMSIG(status
));
355 struct poptOption long_options
[] = {
357 {"timeout", 't', POPT_ARG_INT
, &opt_timeout
, 't'},
358 {"verbose", 'v', POPT_ARG_NONE
, &opt_verbose
, 'v'},
362 int main(int argc
, const char **argv
)
367 const char *instruction_file
;
370 char * const *program_args
;
372 pc
= poptGetContext("texpect",
376 POPT_CONTEXT_POSIXMEHARDER
);
379 poptPrintHelp(pc
, stderr
, 0);
383 while ((optidx
= poptGetNextOpt(pc
)) != -1) {
387 instruction_file
= poptGetArg(pc
);
388 args
= poptGetArgs(pc
);
389 program_args
= (char * const *)discard_const_p(char *, args
);
390 program
= program_args
[0];
395 printf("Using instruction_file: %s\n", instruction_file
);
396 printf("Executing '%s' ", program
);
397 for (i
= 0; program_args
&& program_args
[i
] != NULL
; i
++) {
398 printf("'%s' ", program_args
[i
]);
403 parse_configuration(instruction_file
);
410 err(1, "Failed to fork");
416 dup2(slave
, STDIN_FILENO
);
417 dup2(slave
, STDOUT_FILENO
);
418 dup2(slave
, STDERR_FILENO
);
420 sys_closefrom(STDERR_FILENO
+ 1);
422 /* texpect <expect_instructions> <progname> [<args>] */
423 execvp(program
, program_args
);
424 err(1, "Failed to exec: %s", program
);
430 sa
.sa_handler
= caught_signal
;
432 sigemptyset (&sa
.sa_mask
);
434 sigaction(SIGALRM
, &sa
, NULL
);
437 return eval_parent(pid
);