inspect: Don't assume number of captures in match functions
[libguestfs.git] / src / match.c
1 /* libguestfs
2  * Copyright (C) 2010-2011 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 <string.h>
24
25 #include <pcre.h>
26
27 #include "guestfs.h"
28 #include "guestfs-internal.h"
29
30 /* Match a regular expression which contains no captures.  Returns
31  * true if it matches or false if it doesn't.
32  */
33 int
34 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
35 {
36   size_t len = strlen (str);
37   int vec[30], r;
38
39   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
40   if (r == PCRE_ERROR_NOMATCH)
41     return 0;
42
43   return 1;
44 }
45
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.
49  */
50 char *
51 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
52 {
53   size_t len = strlen (str);
54   int vec[30], r;
55
56   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
57   if (r == PCRE_ERROR_NOMATCH)
58     return NULL;
59
60   return r == 2 ? safe_strndup (g, &str[vec[2]], vec[3]-vec[2]) : NULL;
61 }
62
63 /* Match a regular expression which contains exactly two captures. */
64 int
65 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
66                   char **ret1, char **ret2)
67 {
68   size_t len = strlen (str);
69   int vec[30], r;
70
71   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
72   if (r == PCRE_ERROR_NOMATCH)
73     return 0;
74
75   *ret1 = NULL;
76   *ret2 = NULL;
77
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]);
80
81   return 1;
82 }
83
84 /* Match a regular expression which contains exactly three captures. */
85 int
86 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
87                   char **ret1, char **ret2, char **ret3)
88 {
89   size_t len = strlen (str);
90   int vec[30], r;
91
92   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
93   if (r == PCRE_ERROR_NOMATCH)
94     return 0;
95
96   *ret1 = NULL;
97   *ret2 = NULL;
98   *ret3 = NULL;
99
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]);
103
104   return 1;
105 }