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