Require PCRE library.
[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   if (r != 1) {
43     /* Internal error -- should not happen. */
44     warning (g, "%s: %s: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
45              __FILE__, __func__, r, str);
46     return 0;
47   }
48
49   return 1;
50 }
51
52 /* Match a regular expression which contains exactly one capture.  If
53  * the string matches, return the capture, otherwise return NULL.  The
54  * caller must free the result.
55  */
56 char *
57 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
58 {
59   size_t len = strlen (str);
60   int vec[30], r;
61
62   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
63   if (r == PCRE_ERROR_NOMATCH)
64     return NULL;
65   if (r != 2) {
66     /* Internal error -- should not happen. */
67     warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
68              __FILE__, __func__, r, str);
69     return NULL;
70   }
71
72   return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
73 }
74
75 /* Match a regular expression which contains exactly two captures. */
76 int
77 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
78                   char **ret1, char **ret2)
79 {
80   size_t len = strlen (str);
81   int vec[30], r;
82
83   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
84   if (r == PCRE_ERROR_NOMATCH)
85     return 0;
86   if (r != 3) {
87     /* Internal error -- should not happen. */
88     warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
89              __FILE__, __func__, r, str);
90     return 0;
91   }
92
93   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
94   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
95
96   return 1;
97 }
98
99 /* Match a regular expression which contains exactly three captures. */
100 int
101 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
102                   char **ret1, char **ret2, char **ret3)
103 {
104   size_t len = strlen (str);
105   int vec[30], r;
106
107   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
108   if (r == PCRE_ERROR_NOMATCH)
109     return 0;
110   if (r != 4) {
111     /* Internal error -- should not happen. */
112     warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
113              __FILE__, __func__, r, str);
114     return 0;
115   }
116
117   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
118   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
119   *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
120
121   return 1;
122 }