X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=miniexpect.c;h=85826a0603e5260783ecf8647cc561403501a0da;hb=6a57e58c3c2bd2df7d22545f850c469f5fe7f58f;hp=b370cfef1a32b3e375535da884a8ddb203c2afc1;hpb=e30567e92b95f7310e6b5811666b497645f31288;p=miniexpect.git diff --git a/miniexpect.c b/miniexpect.c index b370cfe..85826a0 100644 --- a/miniexpect.c +++ b/miniexpect.c @@ -46,11 +46,7 @@ #include "miniexpect.h" -#define DEBUG 0 - -#if DEBUG static void debug_buffer (FILE *, const char *); -#endif static mexp_h * create_handle (void) @@ -68,6 +64,7 @@ create_handle (void) h->buffer = NULL; h->len = h->alloc = 0; h->next_match = -1; + h->debug_fp = NULL; h->user1 = h->user2 = h->user3 = NULL; return h; @@ -296,9 +293,8 @@ mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize) pfds[0].events = POLLIN; pfds[0].revents = 0; r = poll (pfds, 1, timeout); -#if DEBUG - fprintf (stderr, "DEBUG: poll returned %d\n", r); -#endif + if (h->debug_fp) + fprintf (h->debug_fp, "DEBUG: poll returned %d\n", r); if (r == -1) return MEXP_ERROR; @@ -318,9 +314,8 @@ mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize) h->alloc += h->read_size; } rs = read (h->fd, h->buffer + h->len, h->read_size); -#if DEBUG - fprintf (stderr, "DEBUG: read returned %zd\n", rs); -#endif + if (h->debug_fp) + fprintf (h->debug_fp, "DEBUG: read returned %zd\n", rs); if (rs == -1) { /* Annoyingly on Linux (I'm fairly sure this is a bug) if the * writer closes the connection, the entire pty is destroyed, @@ -336,12 +331,12 @@ mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize) /* We read something. */ h->len += rs; h->buffer[h->len] = '\0'; -#if DEBUG - fprintf (stderr, "DEBUG: read %zd bytes from pty\n", rs); - fprintf (stderr, "DEBUG: buffer content: "); - debug_buffer (stderr, h->buffer); - fprintf (stderr, "\n"); -#endif + if (h->debug_fp) { + fprintf (h->debug_fp, "DEBUG: read %zd bytes from pty\n", rs); + fprintf (h->debug_fp, "DEBUG: buffer content: "); + debug_buffer (h->debug_fp, h->buffer); + fprintf (h->debug_fp, "\n"); + } try_match: /* See if there is a full or partial match against any regexp. */ @@ -366,6 +361,9 @@ mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize) h->next_match = ovector[1]; else h->next_match = -1; + if (h->debug_fp) + fprintf (h->debug_fp, "DEBUG: next_match at buffer offset %zu\n", + h->next_match); return regexps[i].r; } @@ -395,28 +393,32 @@ mexp_expect (mexp_h *h, const mexp_regexp *regexps, int *ovector, int ovecsize) } } -int -mexp_printf (mexp_h *h, const char *fs, ...) +static int mexp_vprintf (mexp_h *h, int password, const char *fs, va_list args) + __attribute__((format(printf,3,0))); + +static int +mexp_vprintf (mexp_h *h, int password, const char *fs, va_list args) { - va_list args; char *msg; int len; size_t n; ssize_t r; char *p; - va_start (args, fs); len = vasprintf (&msg, fs, args); - va_end (args); if (len < 0) return -1; -#if DEBUG - fprintf (stderr, "DEBUG: writing: "); - debug_buffer (stderr, msg); - fprintf (stderr, "\n"); -#endif + if (h->debug_fp) { + if (!password) { + fprintf (h->debug_fp, "DEBUG: writing: "); + debug_buffer (h->debug_fp, msg); + fprintf (h->debug_fp, "\n"); + } + else + fprintf (h->debug_fp, "DEBUG: writing the password\n"); + } n = len; p = msg; @@ -435,12 +437,35 @@ mexp_printf (mexp_h *h, const char *fs, ...) } int +mexp_printf (mexp_h *h, const char *fs, ...) +{ + int r; + va_list args; + + va_start (args, fs); + r = mexp_vprintf (h, 0, fs, args); + va_end (args); + return r; +} + +int +mexp_printf_password (mexp_h *h, const char *fs, ...) +{ + int r; + va_list args; + + va_start (args, fs); + r = mexp_vprintf (h, 1, fs, args); + va_end (args); + return r; +} + +int mexp_send_interrupt (mexp_h *h) { return write (h->fd, "\003", 1); } -#if DEBUG /* Print escaped buffer to fp. */ static void debug_buffer (FILE *fp, const char *buf) @@ -465,5 +490,3 @@ debug_buffer (FILE *fp, const char *buf) buf++; } } -#endif -