2 * Copyright (C) 2014 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include <sys/types.h>
38 /* RHEL 6 pcre did not define PCRE_PARTIAL_SOFT. However PCRE_PARTIAL
39 * is a synonym so use that.
41 #ifndef PCRE_PARTIAL_SOFT
42 #define PCRE_PARTIAL_SOFT PCRE_PARTIAL
45 #include "miniexpect.h"
52 mexp_h *h = malloc (sizeof *h);
56 /* Initialize the fields to default values. */
63 h->len = h->alloc = 0;
65 h->user1 = h->user2 = h->user3 = NULL;
71 clear_buffer (mexp_h *h)
75 h->alloc = h->len = 0;
80 mexp_close (mexp_h *h)
89 if (waitpid (h->pid, &status, 0) == -1)
99 mexp_spawnl (const char *file, const char *arg, ...)
101 char **argv, **new_argv;
106 argv = malloc (sizeof (char *));
109 argv[0] = (char *) arg;
111 va_start (args, arg);
112 for (i = 1; arg != NULL; ++i) {
113 arg = va_arg (args, const char *);
114 new_argv = realloc (argv, sizeof (char *) * (i+1));
115 if (new_argv == NULL) {
120 argv[i] = (char *) arg;
123 h = mexp_spawnv (file, argv);
129 mexp_spawnv (const char *file, char **argv)
137 fd = posix_openpt (O_RDWR|O_NOCTTY);
141 if (grantpt (fd) == -1)
144 if (unlockpt (fd) == -1)
147 /* Get the slave pty name now, but don't open it in the parent. */
148 if (ptsname_r (fd, slave, sizeof slave) != 0)
151 /* Create the handle last before we fork. */
152 h = create_handle ();
160 if (pid == 0) { /* Child. */
161 struct termios terminal_settings;
166 /* Open the slave side of the pty. We must do this in the child
167 * after setsid so it becomes our controlling tty.
169 slave_fd = open (slave, O_RDWR);
174 tcgetattr (slave_fd, &terminal_settings);
175 cfmakeraw (&terminal_settings);
176 tcsetattr (slave_fd, TCSANOW, &terminal_settings);
178 /* Set up stdin, stdout, stderr to point to the pty. */
184 /* Close the master side of the pty - do this late to avoid a
185 * kernel bug, see sshpass source code.
189 /* Run the subprocess. */
192 _exit (EXIT_FAILURE);
206 waitpid (pid, NULL, 0);
214 mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize)
216 time_t start_t, now_t;
218 struct pollfd pfds[1];
224 if (h->next_match == -1) {
225 /* Fully clear the buffer, then read. */
228 /* See the comment in the manual about h->next_match. We have
229 * some data remaining in the buffer, so begin by matching that.
231 memmove (&h->buffer[0], &h->buffer[h->next_match], h->len - h->next_match);
232 h->len -= h->next_match;
233 h->buffer[h->len] = '\0';
239 /* If we've got a timeout then work out how many seconds are left.
240 * Timeout == 0 is not particularly well-defined, but it probably
241 * means "return immediately if there's no data to be read".
243 if (h->timeout >= 0) {
245 timeout = h->timeout - ((now_t - start_t) * 1000);
253 pfds[0].events = POLLIN;
255 r = poll (pfds, 1, timeout);
257 fprintf (stderr, "DEBUG: poll returned %d\n", r);
265 /* Otherwise we expect there is something to read from the file
268 if (h->alloc - h->len <= h->read_size) {
270 /* +1 here allows us to store \0 after the data read */
271 new_buffer = realloc (h->buffer, h->alloc + h->read_size + 1);
272 if (new_buffer == NULL)
274 h->buffer = new_buffer;
275 h->alloc += h->read_size;
277 rs = read (h->fd, h->buffer + h->len, h->read_size);
279 fprintf (stderr, "DEBUG: read returned %zd\n", rs);
282 /* Annoyingly on Linux (I'm fairly sure this is a bug) if the
283 * writer closes the connection, the entire pty is destroyed,
284 * and read returns -1 / EIO. Handle that special case here.
293 /* We read something. */
295 h->buffer[h->len] = '\0';
297 fprintf (stderr, "DEBUG: read %zd bytes from pty\n", rs);
298 fprintf (stderr, "DEBUG: buffer content: %s\n", h->buffer);
302 /* See if there is a full or partial match against any regexp. */
305 int can_clear_buffer = 1;
307 assert (h->buffer != NULL);
309 for (i = 0; regexps[i].r > 0; ++i) {
310 int options = regexps[i].options | PCRE_PARTIAL_SOFT;
312 r = pcre_exec (regexps[i].re, regexps[i].extra,
313 h->buffer, (int)h->len, 0,
320 if (ovector != NULL && ovecsize >= 1 && ovector[1] >= 0)
321 h->next_match = ovector[1];
327 else if (r == PCRE_ERROR_NOMATCH) {
328 /* No match at all. */
332 else if (r == PCRE_ERROR_PARTIAL) {
333 /* Partial match. Keep the buffer and keep reading. */
334 can_clear_buffer = 0;
338 /* An actual PCRE error. */
339 return MEXP_PCRE_ERROR;
343 /* If none of the regular expressions matched (not partially)
344 * then we can clear the buffer. This is an optimization.
346 if (can_clear_buffer)
354 mexp_printf (mexp_h *h, const char *fs, ...)
364 len = vasprintf (&msg, fs, args);
371 fprintf (stderr, "DEBUG: writing: %s\n", msg);
377 r = write (h->fd, p, n);