Port to pcre2
[miniexpect.git] / test-ls-version.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 /* Note this test depends on the output of 'ls --version'.  You may
20  * have to extend the regular expressions below if running on a
21  * non-Linux OS.
22  */
23
24 #include <config.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <assert.h>
30
31 #define PCRE2_CODE_UNIT_WIDTH 8
32 #include <pcre2.h>
33
34 #include "miniexpect.h"
35 #include "tests.h"
36
37 int
38 main (int argc, char *argv[])
39 {
40   mexp_h *h;
41   int status, r;
42   pcre2_code *ls_coreutils_re;
43   pcre2_code *ls_busybox_re;
44   pcre2_match_data *match_data = pcre2_match_data_create (4, NULL);
45   PCRE2_UCHAR *version;
46   PCRE2_SIZE verlen;
47
48   ls_coreutils_re = test_compile_re ("^ls.* ([.\\d]+)");
49   /* Busybox doesn't actually recognize the --version option, but
50    * it prints out the version string in its error message.
51    */
52   ls_busybox_re = test_compile_re ("^BusyBox v([.\\d+]) ");
53
54   h = mexp_spawnl ("ls", "ls", "--version", NULL);
55   assert (h != NULL);
56
57   switch (mexp_expect (h,
58                        (mexp_regexp[]) {
59                          { 100, ls_coreutils_re },
60                          { 101, ls_busybox_re },
61                          { 0 },
62                        }, match_data)) {
63   case 100:
64   case 101:
65     /* Get the matched version number. */
66     r = pcre2_substring_get_bynumber (match_data, 1, &version, &verlen);
67     if (r < 0) {
68       fprintf (stderr, "error: PCRE error reading version substring: %d\n",
69                r);
70       exit (EXIT_FAILURE);
71     }
72     printf ("ls version = %s\n", (char *) version);
73     pcre2_substring_free (version);
74     break;
75   case MEXP_EOF:
76     fprintf (stderr, "error: EOF before matching version string\n");
77     exit (EXIT_FAILURE);
78   case MEXP_TIMEOUT:
79     fprintf (stderr, "error: timeout before matching version string\n");
80     exit (EXIT_FAILURE);
81   case MEXP_ERROR:
82     perror ("mexp_expect");
83     exit (EXIT_FAILURE);
84   case MEXP_PCRE_ERROR:
85     fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h));
86     exit (EXIT_FAILURE);
87   }
88
89   status = mexp_close (h);
90   if (status != 0 && !test_is_sighup (status)) {
91     fprintf (stderr, "%s: non-zero exit status from subcommand: ", argv[0]);
92     test_diagnose (status);
93     fprintf (stderr, "\n");
94     exit (EXIT_FAILURE);
95   }
96
97   pcre2_code_free (ls_coreutils_re);
98   pcre2_code_free (ls_busybox_re);
99   pcre2_match_data_free (match_data);
100
101   exit (EXIT_SUCCESS);
102 }