X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=src%2Fmatch.c;h=6038e41c55e568a3013329d6bb1094bd090d91e9;hb=aa67ea2b9b800305a00615f3a463cdd46eae9181;hp=869850c10e7574e5f5707551d640fe578930e094;hpb=daf55c07978cd29fe6675911bf76da0b056fdae1;p=libguestfs.git diff --git a/src/match.c b/src/match.c index 869850c..6038e41 100644 --- a/src/match.c +++ b/src/match.c @@ -22,15 +22,11 @@ #include #include -#ifdef HAVE_PCRE #include -#endif #include "guestfs.h" #include "guestfs-internal.h" -#ifdef HAVE_PCRE - /* Match a regular expression which contains no captures. Returns * true if it matches or false if it doesn't. */ @@ -43,12 +39,6 @@ guestfs___match (guestfs_h *g, const char *str, const pcre *re) r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]); if (r == PCRE_ERROR_NOMATCH) return 0; - if (r != 1) { - /* Internal error -- should not happen. */ - warning (g, "%s: %s: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n", - __FILE__, __func__, r, str); - return 0; - } return 1; } @@ -66,14 +56,8 @@ guestfs___match1 (guestfs_h *g, const char *str, const pcre *re) r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]); if (r == PCRE_ERROR_NOMATCH) return NULL; - if (r != 2) { - /* Internal error -- should not happen. */ - warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"", - __FILE__, __func__, r, str); - return NULL; - } - return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]); + return r == 2 ? safe_strndup (g, &str[vec[2]], vec[3]-vec[2]) : NULL; } /* Match a regular expression which contains exactly two captures. */ @@ -87,15 +71,12 @@ guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30); if (r == PCRE_ERROR_NOMATCH) return 0; - if (r != 3) { - /* Internal error -- should not happen. */ - warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"", - __FILE__, __func__, r, str); - return 0; - } - *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]); - *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]); + *ret1 = NULL; + *ret2 = NULL; + + if (r > 1) *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]); + if (r > 2) *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]); return 1; } @@ -111,18 +92,14 @@ guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, r = pcre_exec (re, NULL, str, len, 0, 0, vec, 30); if (r == PCRE_ERROR_NOMATCH) return 0; - if (r != 4) { - /* Internal error -- should not happen. */ - warning (g, "%s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"", - __FILE__, __func__, r, str); - return 0; - } - *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]); - *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]); - *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]); + *ret1 = NULL; + *ret2 = NULL; + *ret3 = NULL; + + if (r > 1) *ret1 = safe_strndup (g, &str[vec[2]], vec[3]-vec[2]); + if (r > 2) *ret2 = safe_strndup (g, &str[vec[4]], vec[5]-vec[4]); + if (r > 3) *ret3 = safe_strndup (g, &str[vec[6]], vec[7]-vec[6]); return 1; } - -#endif /* HAVE_PCRE */