Small fix to documentation.
[miniexpect.git] / miniexpect.c
index f525b68..e1a184c 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <poll.h>
 
 #include <pcre.h>
 
+/* RHEL 6 pcre did not define PCRE_PARTIAL_SOFT.  However PCRE_PARTIAL
+ * is a synonym so use that.
+ */
+#ifndef PCRE_PARTIAL_SOFT
+#define PCRE_PARTIAL_SOFT PCRE_PARTIAL
+#endif
+
 #include "miniexpect.h"
 
 #define DEBUG 0
@@ -50,8 +58,10 @@ create_handle (void)
   h->pid = 0;
   h->timeout = 60000;
   h->read_size = 1024;
+  h->pcre_error = 0;
   h->buffer = NULL;
   h->len = h->alloc = 0;
+  h->next_match = -1;
   h->user1 = h->user2 = h->user3 = NULL;
 
   return h;
@@ -63,6 +73,7 @@ clear_buffer (mexp_h *h)
   free (h->buffer);
   h->buffer = NULL;
   h->alloc = h->len = 0;
+  h->next_match = -1;
 }
 
 int
@@ -79,6 +90,8 @@ mexp_close (mexp_h *h)
       return -1;
   }
 
+  free (h);
+
   return status;
 }
 
@@ -101,6 +114,7 @@ mexp_spawnl (const char *file, const char *arg, ...)
     new_argv = realloc (argv, sizeof (char *) * (i+1));
     if (new_argv == NULL) {
       free (argv);
+      va_end (args);
       return NULL;
     }
     argv = new_argv;
@@ -109,13 +123,14 @@ mexp_spawnl (const char *file, const char *arg, ...)
 
   h = mexp_spawnv (file, argv);
   free (argv);
+  va_end (args);
   return h;
 }
 
 mexp_h *
 mexp_spawnv (const char *file, char **argv)
 {
-  mexp_h *h;
+  mexp_h *h = NULL;
   int fd = -1;
   int err;
   char slave[1024];
@@ -191,15 +206,14 @@ mexp_spawnv (const char *file, char **argv)
     close (fd);
   if (pid > 0)
     waitpid (pid, NULL, 0);
+  if (h != NULL)
+    mexp_close (h);
   errno = err;
   return NULL;
 }
 
 enum mexp_status
-mexp_expect (mexp_h *h, const pcre *code,
-             const pcre_extra *extra,
-             int options, int *ovector, int ovecsize,
-             int *pcre_ret)
+mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize)
 {
   time_t start_t, now_t;
   int timeout;
@@ -209,10 +223,19 @@ mexp_expect (mexp_h *h, const pcre *code,
 
   time (&start_t);
 
-  options |= PCRE_PARTIAL_SOFT;
-
-  /* Clear the read buffer. */
-  clear_buffer (h);
+  if (h->next_match == -1) {
+    /* Fully clear the buffer, then read. */
+    clear_buffer (h);
+  } else {
+    /* See the comment in the manual about h->next_match.  We have
+     * some data remaining in the buffer, so begin by matching that.
+     */
+    memmove (&h->buffer[0], &h->buffer[h->next_match], h->len - h->next_match);
+    h->len -= h->next_match;
+    h->buffer[h->len] = '\0';
+    h->next_match = -1;
+    goto try_match;
+  }
 
   for (;;) {
     /* If we've got a timeout then work out how many seconds are left.
@@ -277,34 +300,55 @@ mexp_expect (mexp_h *h, const pcre *code,
     fprintf (stderr, "DEBUG: buffer content: %s\n", h->buffer);
 #endif
 
-    /* See if there is a full or partial match against the regular expression. */
-    if (code) {
+  try_match:
+    /* See if there is a full or partial match against any regexp. */
+    if (regexps) {
+      size_t i;
+      int can_clear_buffer = 1;
+
       assert (h->buffer != NULL);
-      r = pcre_exec (code, extra, h->buffer, (int)h->len, 0,
-                     options, ovector, ovecsize);
-      if (pcre_ret)
-        *pcre_ret = r;
-
-      if (r >= 0) {
-        /* A full match. */
-        return MEXP_MATCHED;
-      }
 
-      else if (r == PCRE_ERROR_NOMATCH) {
-        /* No match at all, so we can dump the input buffer. */
-        clear_buffer (h);
+      for (i = 0; regexps[i].r > 0; ++i) {
+        int options = regexps[i].options | PCRE_PARTIAL_SOFT;
+
+        r = pcre_exec (regexps[i].re, regexps[i].extra,
+                       h->buffer, (int)h->len, 0,
+                       options,
+                       ovector, ovecsize);
+        h->pcre_error = r;
+
+        if (r >= 0) {
+          /* A full match. */
+          if (ovector != NULL && ovecsize >= 1 && ovector[1] >= 0)
+            h->next_match = ovector[1];
+          else
+            h->next_match = -1;
+          return regexps[i].r;
+        }
+
+        else if (r == PCRE_ERROR_NOMATCH) {
+          /* No match at all. */
+          /* (nothing here) */
+        }
+
+        else if (r == PCRE_ERROR_PARTIAL) {
+          /* Partial match.  Keep the buffer and keep reading. */
+          can_clear_buffer = 0;
+        }
+
+        else {
+          /* An actual PCRE error. */
+          return MEXP_PCRE_ERROR;
+        }
       }
 
-      else if (r == PCRE_ERROR_PARTIAL) {
-        /* Partial match.  Keep the buffer and keep reading. */
-        /* (nothing here) */
-      }
+      /* If none of the regular expressions matched (not partially)
+       * then we can clear the buffer.  This is an optimization.
+       */
+      if (can_clear_buffer)
+        clear_buffer (h);
 
-      else {
-        /* An actual PCRE error. */
-        return MEXP_PCRE_ERROR;
-      }
-    } /* if (code) */
+    } /* if (regexps) */
   }
 }