fe769791cae66c6beea54cd4c95bfb64d6bcff11
[miniexpect.git] / miniexpect.pod
1 =encoding utf8
2
3 =head1 NAME
4
5 miniexpect - A very simple expect library for C.
6
7 =head1 SYNOPSIS
8
9  #include <errno.h>
10  #include <sys/wait.h>
11  #include <pcre.h>
12  #include <miniexpect.h>
13  
14  mexp_h *h;
15  h = mexp_spawnl ("ssh", "ssh", "host", NULL);
16  switch (mexp_expect (h, regexps, ovector, ovecsize)) {
17    ...
18  }
19  mexp_close (h);
20
21  cc prog.c -o prog -lminiexpect -lpcre
22
23 =head1 DESCRIPTION
24
25 Miniexpect is a very simple expect-like library for C.  Expect is a
26 way to control an external program that wants to be run interactively.
27
28 Miniexpect has a saner interface than libexpect, and doesn't depend on
29 Tcl.  It is also thread safe, const-correct and uses modern C
30 standards.
31
32 Miniexpect is a standalone library, except for a single dependency: it
33 requires the PCRE (Perl Compatible Regular Expressions) library from
34 L<http://www.pcre.org/>.  The PCRE dependency is fundamental because
35 we want to offer the most powerful regular expression syntax to match
36 on, but more importantly because PCRE has a convenient way to detect
37 partial matches which made this library very simple to implement.
38
39 This manual page documents the API.  Examples of how to use the API
40 can be found in the source directory.
41
42 =head1 CONCEPTS
43
44 Miniexpect lets you start up an external program, control it (by
45 sending commands to it), and close it down gracefully.  Two things
46 make this different from other APIs like L<popen(3)> and L<system(3)>:
47 Firstly miniexpect creates a pseudoterminal (pty).  Secondly
48 miniexpect lets you match the output of the program using regular
49 expressions.  Both of these are handy for controlling interactive
50 programs that might (for example) ask for passwords, but you can use
51 miniexpect on just about any external program.
52
53 You can control multiple programs at the same time.
54
55 =head1 SPAWNING THE SUBPROCESS
56
57 There are four calls for creating a subprocess:
58
59 B<mexp_h *mexp_spawnl (const char *file, const char *arg, ...);>
60
61 This creates a subprocess running the external program C<file> (the
62 current C<$PATH> is searched unless you give an absolute path).
63 C<arg, ...> are the arguments to the program.  You should terminate
64 the list of arguments with C<NULL>.  Usually the first argument should
65 be the name of the program.
66
67 The return value is a handle (see next section).
68
69 If there was an error running the subprocess, C<NULL> is returned and
70 the error is available in C<errno>.
71
72 For example, to run an ssh subprocess you could do:
73
74  h = mexp_spawnl ("ssh", "ssh", "-l", "root", "host", NULL);
75
76 or to run a particular ssh binary:
77
78  h = mexp_spawnl ("/usr/local/bin/ssh", "ssh", "-l", "root", "host", NULL);
79
80 An alternative to C<mexp_spawnl> is:
81
82 B<mexp_h *mexp_spawnv (const char *file, char **argv);>
83
84 This is the same as C<mexp_spawnl> except that you pass the arguments
85 in a NULL-terminated array.
86
87 There are also two versions of the above calls which take flags:
88
89 B<mexp_h *mexp_spawnlf (unsigned flags, const char *file, const char *arg, ...);>
90
91 B<mexp_h *mexp_spawnvf (unsigned flags, const char *file, char **argv);>
92
93 The flags may contain the following values, logically ORed together:
94
95 =over 4
96
97 =item B<MEXP_SPAWN_KEEP_SIGNALS>
98
99 Do not reset signal handlers to C<SIG_DFL> in the subprocess.
100
101 =item B<MEXP_SPAWN_KEEP_FDS>
102
103 Do not close file descriptors E<ge> 3 in the subprocess.
104
105 =item B<MEXP_SPAWN_COOKED_MODE> or B<MEXP_SPAWN_RAW_MODE>
106
107 Configure the pty in cooked mode or raw mode.  Raw mode is the
108 default.
109
110 =back
111
112 =head1 HANDLES
113
114 After spawning a subprocess, you get back a handle which is a pointer
115 to a struct:
116
117  struct mexp_h;
118  typedef struct mexp_h mexp_h;
119
120 Various methods can be used on the handle:
121
122 B<int mexp_get_fd (mexp_h *h);>
123
124 Return the file descriptor of the pty of the subprocess.  You can read
125 and write to this if you want, although convenience functions are also
126 provided (see below).
127
128 B<pid_t mexp_get_pid (mexp_h *h);>
129
130 Return the process ID of the subprocess.  You can send it signals if
131 you want.
132
133 B<int mexp_get_timeout_ms (mexp_h *h);>
134
135 B<void mexp_set_timeout_ms (mexp_h *h, int millisecs);>
136
137 B<void mexp_set_timeout (mexp_h *h, int secs);>
138
139 Get or set the timeout used by C<mexp_expect> [see below].  The
140 resolution is milliseconds (1/1000th of a second).  Set this before
141 calling C<mexp_expect>.  Passing -1 to either of the C<set_> methods
142 means no timeout.  The default setting is 60000 milliseconds (60
143 seconds).
144
145 B<size_t mexp_get_read_size (mexp *h);>
146
147 B<void mexp_set_read_size (mexp *h, size_t read_size);>
148
149 Get or set the natural size (in bytes) for reads from the subprocess.
150 The default is 1024.  Most callers will not need to change this.
151
152 B<int mexp_get_pcre_error (mexp *h);>
153
154 When C<mexp_expect> [see below] calls the PCRE function
155 L<pcre_exec(3)>, it stashes the return value in the C<pcre_error>
156 field in the handle, and that field is returned by this method.
157
158 There are two uses for this:
159
160 =over 4
161
162 =item 1.
163
164 If C<mexp_expect> returns C<MEXP_PCRE_ERROR>, then the actual PCRE
165 error code returned by L<pcre_exec(3)> is available by calling this
166 method.  For a list of PCRE error codes, see L<pcreapi(3)>.
167
168 =item 2.
169
170 A more unusual use is if you ever need to get the captured substrings
171 from your regular expression (calling L<pcre_get_substring(3)>).  The
172 third parameter of that function (C<stringcount>) is the value
173 returned by L<pcre_exec(3)>, and so you can call it like this:
174
175  pcre_get_substring (h->buffer, ovector,
176                      mexp_get_pcre_error (h), 1, &matched);
177
178 =back
179
180 B<void mexp_set_debug_file (mexp *h, FILE *fp);>
181
182 B<FILE *mexp_get_debug_file (mexp *h);>
183
184 Set or get the debug file of the handle.  To enable debugging, pass a
185 non-C<NULL> file handle, eg. C<stderr>.  To disable debugging, pass
186 C<NULL>.  Debugging messages are printed on the file handle.
187
188 Note that all output and input gets printed, including passwords.  To
189 prevent passwords from being printed, modify your code to call
190 C<mexp_printf_password> instead of C<mexp_printf>.
191
192 The following fields in the handle do not have methods, but can be
193 accessed directly instead:
194
195  char *buffer;
196  size_t len;
197  size_t alloc;
198
199 If C<mexp_expect> returns a match then these variables contain the
200 read buffer.  Note this buffer does not contain the full input from
201 the process, but it will contain at least the part matched by the
202 regular expression (and maybe some more).  C<buffer> is the read
203 buffer and C<len> is the number of bytes of data in the buffer.
204
205  ssize_t next_match;
206
207 If C<mexp_expect> returns a match, then C<next_match> points to the
208 first byte in the buffer I<after> the fully matched expression.  (It
209 may be C<-1> which means it is invalid).  The next time that
210 C<mexp_expect> is called, it will start by consuming the data
211 C<buffer[next_match...len-1]>.  Callers may also need to read from
212 that point in the buffer before calling L<read(2)> on the file
213 descriptor.  Callers may also set this, for example setting it to
214 C<-1> in order to ignore the remainder of the buffer.  In most cases
215 callers can ignore this field, and C<mexp_expect> will just do the
216 right thing when called repeatedly.
217
218  void *user1;
219  void *user2;
220  void *user3;
221
222 Opaque pointers for use by the caller.  The library will not touch
223 these.
224
225 =head1 CLOSING THE HANDLE
226
227 To close the handle and clean up the subprocess, call:
228
229 B<int mexp_close (mexp_h *h);>
230
231 This returns the status code from the subprocess.  This is in the form
232 of a L<waitpid(2)>/L<system(3)> status so you have to use the macros
233 C<WIFEXITED>, C<WEXITSTATUS>, C<WIFSIGNALED>, C<WTERMSIG> etc defined
234 in C<E<lt>sys/wait.hE<gt>> to parse it.
235
236 If there was a system call error, then C<-1> is returned.  The error
237 will be in C<errno>.
238
239 Notes:
240
241 =over 4
242
243 =item *
244
245 Even in error cases, the handle is always closed and its memory is
246 freed by this call.
247
248 =item *
249
250 It is normal for the kernel to send SIGHUP to the subprocess.
251
252 If the subprocess doesn't catch the SIGHUP, then it will die
253 with status:
254
255  WIFSIGNALED (status) && WTERMSIG (status) == SIGHUP
256
257 This case should not necessarily be considered an error.
258
259 =back
260
261 This is how code should check for and print errors from C<mexp_close>:
262
263   status = mexp_close (h);
264   if (status == -1) {
265     perror ("mexp_close");
266     return -1;
267   }
268   if (WIFSIGNALED (status) && WTERMSIG (status) == SIGHUP)
269     goto ignore; /* not an error */
270   if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
271     /* You could use the W* macros to print a better error message. */
272     fprintf (stderr, "error: subprocess failed, status = %d", status);
273     return -1;
274   }
275  ignore:
276   /* no error case */
277
278 =head1 EXPECT FUNCTION
279
280 Miniexpect contains a powerful regular expression matching function
281 based on L<pcre(3)>:
282
283 B<int mexp_expect (mexp_h *h, const mexp_regexp *regexps,
284 int *ovector, int ovecsize);>
285
286 The output of the subprocess is matched against the list of PCRE
287 regular expressions in C<regexps>.  C<regexps> is a list of regular
288 expression structures:
289
290  struct mexp_regexp {
291    int r;
292    const pcre *re;
293    const pcre_extra *extra;
294    int options;
295  };
296  typedef struct mexp_regexp mexp_regexp;
297
298 C<r> is the integer code returned from C<mexp_expect> if this regular
299 expression matches.  It B<must> be E<gt> 0.  C<r == 0> indicates the
300 end of the list of regular expressions.  C<re> is the compiled regular
301 expression.
302
303 Possible return values are:
304
305 =over 4
306
307 =item C<MEXP_TIMEOUT>
308
309 No input matched before the timeout (C<h-E<gt>timeout>) was
310 reached.
311
312 =item C<MEXP_EOF>
313
314 The subprocess closed the connection.
315
316 =item C<MEXP_ERROR>
317
318 There was a system call error (eg. from the read call).  The error is
319 returned in C<errno>.
320
321 =item C<MEXP_PCRE_ERROR>
322
323 There was a C<pcre_exec> error.  C<h-E<gt>pcre_error> is set to the
324 error code.  See L<pcreapi(3)> for a list of the C<PCRE_*> error codes
325 and what they mean.
326
327 =item C<r> E<gt> 0
328
329 If any regexp matches, the associated integer code (C<regexps[].r>)
330 is returned.
331
332 =back
333
334 Notes:
335
336 =over 4
337
338 =item *
339
340 C<regexps> may be NULL or an empty list, which means we don't match
341 against a regular expression.  This is useful if you just want to wait
342 for EOF or timeout.
343
344 =item *
345
346 C<regexps[].re>, C<regexps[].extra>, C<regexps[].options>, C<ovector>
347 and C<ovecsize> are passed through to the L<pcre_exec(3)> function.
348
349 =item *
350
351 If multiple regular expressions are passed, then they are checked in
352 turn and the I<first> regular expression that matches is returned
353 I<even if the match happens later in the input than another regular
354 expression>.
355
356 For example if the input is C<"hello world"> and you pass the two
357 regular expressions:
358
359  regexps[0].re = world
360  regexps[1].re = hello
361
362 then the first regular expression (C<"world">) may match and the
363 C<"hello"> part of the input may be ignored.
364
365 In some cases this can even lead to unpredictable matching.  In the
366 case above, if we only happened to read C<"hello wor">, then the
367 second regular expression (C<"hello">) I<would> match.
368
369 If this is a concern, combine your regular expressions into a single
370 one, eg. C<(hello)|(world)>.
371
372 =back
373
374 =head2 mexp_expect example
375
376 It is easier to understand C<mexp_expect> by considering a simple
377 example.
378
379 In this example we are waiting for ssh to either send us a password
380 prompt, or (if no password was required) a command prompt, and based
381 on the output we will either send back a password or a command.
382
383 The unusual C<(mexp_regexp[]){...}> syntax is called a "compound
384 literal" and is available in C99.  If you need to use an older
385 compiler, you can just use a local variable instead.
386
387  mexp_h *h;
388  char *errptr;
389  int offset;
390  pcre *password_re, *prompt_re;
391  const int ovecsize = 12;
392  int ovector[ovecsize];
393  
394  password_re = pcre_compile ("assword", 0, &errptr, &offset, NULL);
395  prompt_re = pcre_compile ("[$#] ", 0, &errptr, &offset, NULL);
396  
397  switch (mexp_expect (h,
398                       (mexp_regexp[]) {
399                         { 100, .re = password_re },
400                         { 101, .re = prompt_re },
401                         { 0 },
402                       }, ovector, ovecsize)) {
403   case 100:
404     /* here you would send a password */
405     break;
406   case 101:
407     /* here you would send a command */
408     break;
409   case MEXP_EOF:
410     fprintf (stderr, "error: ssh closed the connection unexpectedly\n");
411     exit (EXIT_FAILURE);
412   case MEXP_TIMEOUT:
413     fprintf (stderr, "error: timeout before reaching the prompt\n");
414     exit (EXIT_FAILURE);
415   case MEXP_ERROR:
416     perror ("mexp_expect");
417     exit (EXIT_FAILURE);
418   case MEXP_PCRE_ERROR:
419     fprintf (stderr, "error: PCRE error: %d\n", h->pcre_error);
420     exit (EXIT_FAILURE);
421  }
422
423 =head1 SENDING COMMANDS TO THE SUBPROCESS
424
425 You can write to the subprocess simply by writing to C<h-E<gt>fd>.
426 However we also provide a convenience function:
427
428 B<int mexp_printf (mexp_h *h, const char *fs, ...);>
429
430 B<int mexp_printf_password (mexp_h *h, const char *fs, ...);>
431
432 This returns the number of bytes, if the whole message was written OK.
433 If there was an error, -1 is returned and the error is available in
434 C<errno>.
435
436 Notes:
437
438 =over 4
439
440 =item *
441
442 C<mexp_printf> will not do a partial write.  If it cannot write all
443 the data, then it will return an error.
444
445 =item *
446
447 This function does not write a newline automatically.  If you want to
448 send a command followed by a newline you have to do something like:
449
450  mexp_printf (h, "exit\n");
451
452 =item *
453
454 C<mexp_printf_password> works identically to C<mexp_printf> except
455 that the output is I<not> sent to the debugging file if debugging is
456 enabled.  As the name suggests, use this for passwords so that they
457 don't appear in debugging output.
458
459 =back
460
461 B<int mexp_send_interrupt (mexp_h *h);>
462
463 Send the interrupt character (C<^C>, Ctrl-C, C<\003>).  This is like
464 pressing C<^C> - the subprocess (or remote process, if using C<ssh>)
465 is gracefully killed.
466
467 Note this only works if the pty is in cooked mode
468 (ie. C<MEXP_SPAWN_COOKED_MODE> was passed to C<mexp_spawnlf> or
469 C<mexp_spawnvf>).  In raw mode, all characters are passed through
470 without any special interpretation.
471
472 =head1 SOURCE
473
474 Source is available from:
475 L<http://git.annexia.org/?p=miniexpect.git;a=summary>
476
477 =head1 SEE ALSO
478
479 L<pcre(3)>,
480 L<pcre_exec(3)>,
481 L<pcreapi(3)>,
482 L<waitpid(2)>,
483 L<system(3)>.
484
485 =head1 AUTHORS
486
487 Richard W.M. Jones (C<rjones at redhat dot com>)
488
489 =head1 LICENSE
490
491 The library is released under the Library GPL (LGPL) version 2 or at
492 your option any later version.
493
494 =head1 COPYRIGHT
495
496 Copyright (C) 2014 Red Hat Inc.