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"
37 #include "lib/util/sys_rw.h"
45 #ifdef HAVE_BSD_LIBUTIL_H
46 #include <bsd/libutil.h>
47 #elif defined HAVE_LIBUTIL_H
53 #endif /* STREAMPTY */
60 const char progname
[] = "unknown program";
62 static void err(int eval
, const char *fmt
, ...) PRINTF_ATTRIBUTE(2, 0);
63 static void errx(int eval
, const char *fmt
, ...) PRINTF_ATTRIBUTE(2, 0);
65 static void err(int eval
, const char *fmt
, ...)
67 int err_errno
= errno
;
70 fprintf(stderr
, "%s: ", progname
);
72 vfprintf(stderr
, fmt
, ap
);
74 fprintf(stderr
, ": %s\n", strerror(err_errno
));
78 static void errx(int eval
, const char *fmt
, ...)
82 fprintf(stderr
, "%s: ", progname
);
84 vfprintf(stderr
, fmt
, ap
);
86 fprintf(stderr
, "\n");
93 enum { CMD_EXPECT
= 0, CMD_SEND
, CMD_PASSWORD
} type
;
103 static struct command
*commands
, **next
= &commands
;
105 static sig_atomic_t alarmset
= 0;
107 static int opt_timeout
= 10;
108 static int opt_verbose
;
112 static char line
[256] = { 0 };
114 static void caught_signal(int signo
)
120 static void open_pty(void)
123 printf("implement open_pty\n");
126 #if defined(HAVE_OPENPTY) || defined(__linux) || defined(__osf__) /* XXX */
127 if(openpty(&master
, &slave
, line
, 0, 0) == 0)
129 #endif /* HAVE_OPENPTY .... */
141 for(q
= clone
; *q
; q
++){
142 master
= open(*q
, O_RDWR
);
150 strlcpy(line
, ptsname(master
), sizeof(line
));
151 slave
= open(line
, O_RDWR
);
153 errx(1, "failed to open slave when using %s", *q
);
154 ioctl(slave
, I_PUSH
, "ptem");
155 ioctl(slave
, I_PUSH
, "ldterm");
161 #endif /* STREAMSPTY */
163 /* more cases, like open /dev/ptmx, etc */
172 static char *iscmd(const char *buf
, const char *s
)
174 size_t len
= strlen(s
);
176 if (strncmp(buf
, s
, len
) != 0) {
180 return strdup(buf
+ len
);
183 static void parse_configuration(const char *fn
)
188 unsigned int lineno
= 0;
191 cmd
= fopen(fn
, "r");
193 err(1, "open: %s", fn
);
195 while (fgets(s
, sizeof(s
), cmd
) != NULL
) {
197 s
[strcspn(s
, "#\n")] = '\0';
200 c
= calloc(1, sizeof(*c
));
208 if ((str
= iscmd(s
, "expect ")) != NULL
) {
209 c
->type
= CMD_EXPECT
;
211 } else if ((str
= iscmd(s
, "send ")) != NULL
) {
214 } else if ((str
= iscmd(s
, "password ")) != NULL
) {
215 c
->type
= CMD_PASSWORD
;
218 errx(1, "Invalid command on line %d: %s", lineno
, s
);
228 static int eval_parent(pid_t pid
)
235 for (c
= commands
; c
!= NULL
; c
= c
->next
) {
239 printf("[expecting %s]\n", c
->str
);
243 while((sret
= read(master
, &in
, sizeof(in
))) > 0) {
246 if (c
->str
[len
] != in
) {
251 if (c
->str
[len
] == '\0') {
256 if (alarmset
== SIGALRM
) {
257 errx(1, "timeout waiting for %s (line %u)",
259 } else if (alarmset
) {
260 errx(1, "got a signal %d waiting for %s (line %u)",
261 (int)alarmset
, c
->str
, c
->lineno
);
265 errx(1, "end command while waiting for %s (line %u)",
272 const char *msg
= (c
->type
== CMD_PASSWORD
) ? "****" : c
->str
;
275 printf("[send %s]\n", msg
);
278 len
= strlen(c
->str
);
281 if (c
->str
[i
] == '\\' && i
< len
- 1) {
296 "unknown control char %c (line %u)",
300 if (sys_write(master
, &ctrl
, 1) != 1) {
301 errx(1, "command refused input (line %u)", c
->lineno
);
304 if (sys_write(master
, &c
->str
[i
], 1) != 1) {
305 errx(1, "command refused input (line %u)", c
->lineno
);
317 while(read(master
, &in
, sizeof(in
)) > 0) {
322 printf("[end of program]\n");
326 * Fetch status from child
331 ret
= waitpid(pid
, &status
, 0);
336 if (WIFEXITED(status
) && WEXITSTATUS(status
)) {
337 return WEXITSTATUS(status
);
338 } else if (WIFSIGNALED(status
)) {
339 printf("killed by signal: %d\n", WTERMSIG(status
));
350 struct poptOption long_options
[] = {
353 .longName
= "timeout",
355 .argInfo
= POPT_ARG_INT
,
360 .longName
= "verbose",
362 .argInfo
= POPT_ARG_NONE
,
369 int main(int argc
, const char **argv
)
373 poptContext pc
= NULL
;
374 const char *instruction_file
;
377 char * const *program_args
;
379 pc
= poptGetContext("texpect",
383 POPT_CONTEXT_POSIXMEHARDER
);
386 poptPrintHelp(pc
, stderr
, 0);
390 while ((optidx
= poptGetNextOpt(pc
)) != -1) {
392 case POPT_ERROR_BADOPT
:
393 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
394 poptBadOption(pc
, 0), poptStrerror(optidx
));
395 poptPrintUsage(pc
, stderr
, 0);
400 instruction_file
= poptGetArg(pc
);
401 args
= poptGetArgs(pc
);
403 poptPrintHelp(pc
, stderr
, 0);
407 program_args
= (char * const *)discard_const_p(char *, args
);
408 program
= program_args
[0];
413 printf("Using instruction_file: %s\n", instruction_file
);
414 printf("Executing '%s' ", program
);
415 for (i
= 0; program_args
[i
] != NULL
; i
++) {
416 printf("'%s' ", program_args
[i
]);
421 parse_configuration(instruction_file
);
428 err(1, "Failed to fork");
437 dup2(slave
, STDIN_FILENO
);
438 dup2(slave
, STDOUT_FILENO
);
439 dup2(slave
, STDERR_FILENO
);
441 closefrom(STDERR_FILENO
+ 1);
443 /* texpect <expect_instructions> <progname> [<args>] */
444 execvp(program
, program_args
);
445 err(1, "Failed to exec: %s", program
);
454 sa
.sa_handler
= caught_signal
;
456 sigemptyset (&sa
.sa_mask
);
458 sigaction(SIGALRM
, &sa
, NULL
);
462 return eval_parent(pid
);