2 * Copyright (C) 2010-2011 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
28 #include "guestfs-internal.h"
30 /* Match a regular expression which contains no captures. Returns
31 * true if it matches or false if it doesn't.
34 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
36 size_t len = strlen (str);
39 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
40 if (r == PCRE_ERROR_NOMATCH)
46 /* Match a regular expression which contains exactly one capture. If
47 * the string matches, return the capture, otherwise return NULL. The
48 * caller must free the result.
51 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
53 size_t len = strlen (str);
56 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
57 if (r == PCRE_ERROR_NOMATCH)
60 return r == 2 ? safe_strndup (g, &str[vec[2]], vec[3]-vec[2]) : NULL;
63 /* Match a regular expression which contains exactly two captures. */
65 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
66 char **ret1, char **ret2)
68 size_t len = strlen (str);
71 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
72 if (r == PCRE_ERROR_NOMATCH)
78 if (r > 1) *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
79 if (r > 2) *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
84 /* Match a regular expression which contains exactly three captures. */
86 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
87 char **ret1, char **ret2, char **ret3)
89 size_t len = strlen (str);
92 r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
93 if (r == PCRE_ERROR_NOMATCH)
100 if (r > 1) *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
101 if (r > 2) *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
102 if (r > 3) *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);