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
33 #include "miniexpect.h"
37 main (int argc, char *argv[])
41 pcre *ls_coreutils_re;
43 const int ovecsize = 12;
44 int ovector[ovecsize];
47 ls_coreutils_re = test_compile_re ("^ls.* ([.\\d]+)");
48 /* Busybox doesn't actually recognize the --version option, but
49 * it prints out the version string in its error message.
51 ls_busybox_re = test_compile_re ("^BusyBox v([.\\d+]) ");
53 h = mexp_spawnl ("ls", "ls", "--version", NULL);
56 switch (mexp_expect (h,
58 { 100, ls_coreutils_re },
59 { 101, ls_busybox_re },
61 }, ovector, ovecsize)) {
64 /* Get the matched version number. */
65 r = pcre_get_substring (h->buffer, ovector,
66 mexp_get_pcre_error (h), 1, &version);
68 fprintf (stderr, "error: PCRE error reading version substring: %d\n",
72 printf ("ls version = %s\n", version);
73 pcre_free_substring (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 pcre_free (ls_coreutils_re);
98 pcre_free (ls_busybox_re);