1 /* miniexpect test suite
2 * Copyright (C) 2014 Red Hat Inc.
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.
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.
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
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
31 #define PCRE2_CODE_UNIT_WIDTH 8
34 #include "miniexpect.h"
38 main (int argc, char *argv[])
42 pcre2_code *ls_coreutils_re;
43 pcre2_code *ls_busybox_re;
44 pcre2_match_data *match_data = pcre2_match_data_create (4, NULL);
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.
52 ls_busybox_re = test_compile_re ("^BusyBox v([.\\d+]) ");
54 h = mexp_spawnl ("ls", "ls", "--version", NULL);
57 switch (mexp_expect (h,
59 { 100, ls_coreutils_re },
60 { 101, ls_busybox_re },
65 /* Get the matched version number. */
66 r = pcre2_substring_get_bynumber (match_data, 1, &version, &verlen);
68 fprintf (stderr, "error: PCRE error reading version substring: %d\n",
72 printf ("ls version = %s\n", (char *) version);
73 pcre2_substring_free (version);
76 fprintf (stderr, "error: EOF before matching version string\n");
79 fprintf (stderr, "error: timeout before matching version string\n");
82 perror ("mexp_expect");
85 fprintf (stderr, "error: PCRE error: %d\n", mexp_get_pcre_error (h));
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");
97 pcre2_code_free (ls_coreutils_re);
98 pcre2_code_free (ls_busybox_re);
99 pcre2_match_data_free (match_data);