Add caution subdirectory containing safety and liveness tests.
[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 #ifdef HAVE_PCRE
26 #include <pcre.h>
27 #endif
28
29 #include "guestfs.h"
30 #include "guestfs-internal.h"
31
32 #ifdef HAVE_PCRE
33
34 /* Match a regular expression which contains no captures.  Returns
35  * true if it matches or false if it doesn't.
36  */
37 int
38 guestfs___match (guestfs_h *g, const char *str, const pcre *re)
39 {
40   size_t len = strlen (str);
41   int vec[30], r;
42
43   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
44   if (r == PCRE_ERROR_NOMATCH)
45     return 0;
46   if (r != 1) {
47     /* Internal error -- should not happen. */
48     warning (g, "%s: %s: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
49              __FILE__, __func__, r, str);
50     return 0;
51   }
52
53   return 1;
54 }
55
56 /* Match a regular expression which contains exactly one capture.  If
57  * the string matches, return the capture, otherwise return NULL.  The
58  * caller must free the result.
59  */
60 char *
61 guestfs___match1 (guestfs_h *g, const char *str, const pcre *re)
62 {
63   size_t len = strlen (str);
64   int vec[30], r;
65
66   r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
67   if (r == PCRE_ERROR_NOMATCH)
68     return NULL;
69   if (r != 2) {
70     /* Internal error -- should not happen. */
71     warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
72              __FILE__, __func__, r, str);
73     return NULL;
74   }
75
76   return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
77 }
78
79 /* Match a regular expression which contains exactly two captures. */
80 int
81 guestfs___match2 (guestfs_h *g, const char *str, const pcre *re,
82                   char **ret1, char **ret2)
83 {
84   size_t len = strlen (str);
85   int vec[30], r;
86
87   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
88   if (r == PCRE_ERROR_NOMATCH)
89     return 0;
90   if (r != 3) {
91     /* Internal error -- should not happen. */
92     warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
93              __FILE__, __func__, r, str);
94     return 0;
95   }
96
97   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
98   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
99
100   return 1;
101 }
102
103 /* Match a regular expression which contains exactly three captures. */
104 int
105 guestfs___match3 (guestfs_h *g, const char *str, const pcre *re,
106                   char **ret1, char **ret2, char **ret3)
107 {
108   size_t len = strlen (str);
109   int vec[30], r;
110
111   r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30);
112   if (r == PCRE_ERROR_NOMATCH)
113     return 0;
114   if (r != 4) {
115     /* Internal error -- should not happen. */
116     warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"",
117              __FILE__, __func__, r, str);
118     return 0;
119   }
120
121   *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
122   *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]);
123   *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]);
124
125   return 1;
126 }
127
128 #endif /* HAVE_PCRE */