Small fix to documentation.
[miniexpect.git] / test-multi-match.c
1 /* miniexpect test suite
2  * Copyright (C) 2014 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <assert.h>
25
26 #include "miniexpect.h"
27 #include "tests.h"
28
29 int
30 main (int argc, char *argv[])
31 {
32   mexp_h *h;
33   int status;
34   int r;
35   int rv[5];
36   size_t i;
37   pcre *multi_re = test_compile_re ("multi");
38   pcre *match_re = test_compile_re ("match");
39   pcre *ing_re = test_compile_re ("ing");
40   pcre *str_re = test_compile_re ("str");
41   pcre *s_re = test_compile_re ("s");
42   const int ovecsize = 12;
43   int ovector[ovecsize];
44
45   /* If the subprocess prints multiple things, we should be able to
46    * repeatedly call mexp_expect to match on each one.  This didn't
47    * work correctly in earlier versions of the library.
48    */
49   h = mexp_spawnl ("echo", "echo", "multimatchingstrs", NULL);
50   assert (h != NULL);
51
52   for (i = 0; i < 5; ++i) {
53     r = mexp_expect (h,
54                      (mexp_regexp[]) {
55                        { 100, multi_re },
56                        { 101, match_re },
57                        { 102, ing_re },
58                        { 103, str_re },
59                        { 104, s_re },
60                        { 0 },
61                      }, ovector, ovecsize);
62     switch (r) {
63     case 100: case 101: case 102: case 103: case 104:
64       printf ("iteration %zu: matched %d\n", i, r);
65       rv[i] = r;
66       break;
67     case MEXP_EOF:
68       fprintf (stderr, "error: unexpected EOF in iteration %zu\n", i);
69       exit (EXIT_FAILURE);
70     case MEXP_TIMEOUT:
71       fprintf (stderr, "error: unexpected timeout in iteration %zu\n", i);
72       exit (EXIT_FAILURE);
73     case MEXP_ERROR:
74         perror ("mexp_expect");
75       exit (EXIT_FAILURE);
76     case MEXP_PCRE_ERROR:
77       fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h));
78       exit (EXIT_FAILURE);
79     }
80   }
81
82   assert (rv[0] == 100); /* multi */
83   assert (rv[1] == 101); /* match */
84   assert (rv[2] == 102); /* ing */
85   assert (rv[3] == 103); /* str */
86   assert (rv[4] == 104); /* s */
87
88   status = mexp_close (h);
89   if (status != 0 && !test_is_sighup (status)) {
90     fprintf (stderr, "%s: non-zero exit status from subcommand: ", argv[0]);
91     test_diagnose (status);
92     fprintf (stderr, "\n");
93     exit (EXIT_FAILURE);
94   }
95
96   exit (EXIT_SUCCESS);
97 }