From 3df4c0d3e4192cb6bb8e9ed7126346ab6aa98043 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 24 Jan 2016 14:59:55 +0000 Subject: [PATCH 1/1] Introduce methods to access some fields in the handle. Actually these are implemented as C macros. --- example-sshpass.c | 6 ++-- miniexpect.h | 13 +++++++++ miniexpect.pod | 86 +++++++++++++++++++++++++++++++----------------------- test-ls-version.c | 5 ++-- test-multi-match.c | 2 +- 5 files changed, 69 insertions(+), 43 deletions(-) diff --git a/example-sshpass.c b/example-sshpass.c index be9f8d0..3449a20 100644 --- a/example-sshpass.c +++ b/example-sshpass.c @@ -87,7 +87,7 @@ main (int argc, char *argv[]) perror ("mexp_expect"); goto error; case MEXP_PCRE_ERROR: - fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error); + fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h)); goto error; } @@ -128,7 +128,7 @@ main (int argc, char *argv[]) perror ("mexp_expect"); goto error; case MEXP_PCRE_ERROR: - fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error); + fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h)); goto error; } @@ -160,7 +160,7 @@ main (int argc, char *argv[]) perror ("mexp_expect"); goto error; case MEXP_PCRE_ERROR: - fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error); + fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h)); goto error; } diff --git a/miniexpect.h b/miniexpect.h index f987655..192d180 100644 --- a/miniexpect.h +++ b/miniexpect.h @@ -50,6 +50,19 @@ struct mexp_h { }; typedef struct mexp_h mexp_h; +/* Methods to access (some) fields in the handle. */ +#define mexp_get_fd(h) ((h)->fd) +#define mexp_get_pid(h) ((h)->pid) +#define mexp_get_timeout_ms(h) ((h)->timeout) +#define mexp_set_timeout_ms(h, ms) ((h)->timeout = (ms)) +/* If secs == -1, then this sets h->timeout to -1000, but the main + * code handles this since it only checks for h->timeout < 0. + */ +#define mexp_set_timeout(h, secs) ((h)->timeout = 1000 * (secs)) +#define mexp_get_read_size(h) ((h)->read_size) +#define mexp_set_read_size(h, size) ((h)->read_size = (size)) +#define mexp_get_pcre_error(h) ((h)->pcre_error) + /* Spawn a subprocess. */ extern mexp_h *mexp_spawnv (const char *file, char **argv); extern mexp_h *mexp_spawnl (const char *file, const char *arg, ...); diff --git a/miniexpect.pod b/miniexpect.pod index 827a268..baca020 100644 --- a/miniexpect.pod +++ b/miniexpect.pod @@ -85,28 +85,56 @@ in a NULL-terminated array. =head1 HANDLES -After spawning a subprocess, you get back a handle. There are various -fields in this handle which you can read or write: +After spawning a subprocess, you get back a handle which is a pointer +to a struct: - struct mexp_h { - int fd; - pid_t pid; + struct mexp_h; + typedef struct mexp_h mexp_h; + +Various methods can be used on the handle: + +B + +Return the file descriptor of the pty of the subprocess. You can read +and write to this if you want, although convenience functions are also +provided (see below). + +B + +Return the process ID of the subprocess. You can send it signals if +you want. -C is the pty of the subprocess. You can read and write to this if -you want, although convenience functions are also provided (see -below). C is the process ID of the subprocess. You can send it -signals if you want. +B - int timeout; +B -C is the timeout in milliseconds (1/1000th of a second) used -by C (see below). You can set this before calling -C if you want. Setting it to -1 means no timeout. The -default setting is 60000 (60 seconds). +B - char *buffer; - size_t len; - size_t alloc; +Get and set the timeout used by C (see below). The +resolution is milliseconds (1/1000th of a second). Set this before +calling C. Passing -1 to either of the C methods +means no timeout. The default setting is 60000 milliseconds (60 +seconds). + +B + +B + +Get or set the natural size (in bytes) for reads from the subprocess. +The default is 1024. Most callers will not need to change this. + +B + +If C returns C, then the actual PCRE +error code returned by L is available by calling this +method. For a list of PCRE error codes, see L. + +The following fields in the handle do not have methods, but can be +accessed directly instead: + + char *buffer; + size_t len; + size_t alloc; If C returns a match then these variables contain the read buffer. Note this buffer does not contain the full input from @@ -114,7 +142,7 @@ the process, but it will contain at least the part matched by the regular expression (and maybe some more). C is the read buffer and C is the number of bytes of data in the buffer. - ssize_t next_match; + ssize_t next_match; If C returns a match, then C points to the first byte in the buffer I the fully matched expression. (It @@ -127,29 +155,13 @@ C<-1> in order to ignore the remainder of the buffer. In most cases callers can ignore this field, and C will just do the right thing when called repeatedly. - size_t read_size; - -Callers may set this to the natural size (in bytes) for reads from the -subprocess. The default is 1024. Most callers will not need to -change this. - - int pcre_error; - -If C returns C, then the actual PCRE -error code returned by L is available here. For a list -of PCRE error codes, see L. - - void *user1; - void *user2; - void *user3; + void *user1; + void *user2; + void *user3; Opaque pointers for use by the caller. The library will not touch these. - }; - - typedef struct mexp_h mexp_h; - =head1 CLOSING THE HANDLE To close the handle and clean up the subprocess, call: diff --git a/test-ls-version.c b/test-ls-version.c index 3b7bbc6..8880b5e 100644 --- a/test-ls-version.c +++ b/test-ls-version.c @@ -62,7 +62,8 @@ main (int argc, char *argv[]) case 100: case 101: /* Get the matched version number. */ - r = pcre_get_substring (h->buffer, ovector, h->pcre_error, 1, &version); + r = pcre_get_substring (h->buffer, ovector, + mexp_get_pcre_error (h), 1, &version); if (r < 0) { fprintf (stderr, "error: PCRE error reading version substring: %d\n", r); @@ -81,7 +82,7 @@ main (int argc, char *argv[]) perror ("mexp_expect"); exit (EXIT_FAILURE); case MEXP_PCRE_ERROR: - fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error); + fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h)); exit (EXIT_FAILURE); } diff --git a/test-multi-match.c b/test-multi-match.c index 7237202..5b4a16c 100644 --- a/test-multi-match.c +++ b/test-multi-match.c @@ -74,7 +74,7 @@ main (int argc, char *argv[]) perror ("mexp_expect"); exit (EXIT_FAILURE); case MEXP_PCRE_ERROR: - fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error); + fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h)); exit (EXIT_FAILURE); } } -- 1.8.3.1