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
31 #include <sys/types.h>
37 #include "miniexpect.h"
44 mexp_h *h = malloc (sizeof *h);
48 /* Initialize the fields to default values. */
55 h->len = h->alloc = 0;
56 h->user1 = h->user2 = h->user3 = NULL;
62 clear_buffer (mexp_h *h)
66 h->alloc = h->len = 0;
70 mexp_close (mexp_h *h)
79 if (waitpid (h->pid, &status, 0) == -1)
87 mexp_spawnl (const char *file, const char *arg, ...)
89 char **argv, **new_argv;
94 argv = malloc (sizeof (char *));
97 argv[0] = (char *) arg;
100 for (i = 1; arg != NULL; ++i) {
101 arg = va_arg (args, const char *);
102 new_argv = realloc (argv, sizeof (char *) * (i+1));
103 if (new_argv == NULL) {
108 argv[i] = (char *) arg;
111 h = mexp_spawnv (file, argv);
117 mexp_spawnv (const char *file, char **argv)
125 fd = posix_openpt (O_RDWR|O_NOCTTY);
129 if (grantpt (fd) == -1)
132 if (unlockpt (fd) == -1)
135 /* Get the slave pty name now, but don't open it in the parent. */
136 if (ptsname_r (fd, slave, sizeof slave) != 0)
139 /* Create the handle last before we fork. */
140 h = create_handle ();
148 if (pid == 0) { /* Child. */
149 struct termios terminal_settings;
154 /* Open the slave side of the pty. We must do this in the child
155 * after setsid so it becomes our controlling tty.
157 slave_fd = open (slave, O_RDWR);
162 tcgetattr (slave_fd, &terminal_settings);
163 cfmakeraw (&terminal_settings);
164 tcsetattr (slave_fd, TCSANOW, &terminal_settings);
166 /* Set up stdin, stdout, stderr to point to the pty. */
172 /* Close the master side of the pty - do this late to avoid a
173 * kernel bug, see sshpass source code.
177 /* Run the subprocess. */
180 _exit (EXIT_FAILURE);
194 waitpid (pid, NULL, 0);
200 mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize)
202 time_t start_t, now_t;
204 struct pollfd pfds[1];
210 /* Clear the read buffer. */
214 /* If we've got a timeout then work out how many seconds are left.
215 * Timeout == 0 is not particularly well-defined, but it probably
216 * means "return immediately if there's no data to be read".
218 if (h->timeout >= 0) {
220 timeout = h->timeout - ((now_t - start_t) * 1000);
228 pfds[0].events = POLLIN;
230 r = poll (pfds, 1, timeout);
232 fprintf (stderr, "DEBUG: poll returned %d\n", r);
240 /* Otherwise we expect there is something to read from the file
243 if (h->alloc - h->len <= h->read_size) {
245 /* +1 here allows us to store \0 after the data read */
246 new_buffer = realloc (h->buffer, h->alloc + h->read_size + 1);
247 if (new_buffer == NULL)
249 h->buffer = new_buffer;
250 h->alloc += h->read_size;
252 rs = read (h->fd, h->buffer + h->len, h->read_size);
254 fprintf (stderr, "DEBUG: read returned %zd\n", rs);
257 /* Annoyingly on Linux (I'm fairly sure this is a bug) if the
258 * writer closes the connection, the entire pty is destroyed,
259 * and read returns -1 / EIO. Handle that special case here.
268 /* We read something. */
270 h->buffer[h->len] = '\0';
272 fprintf (stderr, "DEBUG: read %zd bytes from pty\n", rs);
273 fprintf (stderr, "DEBUG: buffer content: %s\n", h->buffer);
276 /* See if there is a full or partial match against any regexp. */
279 int can_clear_buffer = 1;
281 assert (h->buffer != NULL);
283 for (i = 0; regexps[i].r > 0; ++i) {
284 int options = regexps[i].options | PCRE_PARTIAL_SOFT;
286 r = pcre_exec (regexps[i].re, regexps[i].extra,
287 h->buffer, (int)h->len, 0,
297 else if (r == PCRE_ERROR_NOMATCH) {
298 /* No match at all. */
302 else if (r == PCRE_ERROR_PARTIAL) {
303 /* Partial match. Keep the buffer and keep reading. */
304 can_clear_buffer = 0;
308 /* An actual PCRE error. */
309 return MEXP_PCRE_ERROR;
313 /* If none of the regular expressions matched (not partially)
314 * then we can clear the buffer. This is an optimization.
316 if (can_clear_buffer)
324 mexp_printf (mexp_h *h, const char *fs, ...)
334 len = vasprintf (&msg, fs, args);
341 fprintf (stderr, "DEBUG: writing: %s\n", msg);
347 r = write (h->fd, p, n);