From 55e6ccb23e0b1f954107b4d0284d3d41f7a4e7b8 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sat, 13 Nov 2010 14:39:21 +0000 Subject: [PATCH] lib: Add match3 function. --- src/guestfs-internal.h | 2 ++ src/inspect.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/guestfs-internal.h b/src/guestfs-internal.h index 5ac4dcf..299cf74 100644 --- a/src/guestfs-internal.h +++ b/src/guestfs-internal.h @@ -257,6 +257,7 @@ extern void guestfs___print_BufferIn (FILE *out, const char *buf, size_t buf_siz extern int guestfs___match (guestfs_h *g, const char *str, const pcre *re); extern char *guestfs___match1 (guestfs_h *g, const char *str, const pcre *re); extern int guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2); +extern int guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, char **ret1, char **ret2, char **ret3); #endif extern int guestfs___feature_available (guestfs_h *g, const char *feature); extern void guestfs___free_string_list (char **); @@ -276,6 +277,7 @@ extern void guestfs___rollback_cmdline (guestfs_h *g, int pos); #define match guestfs___match #define match1 guestfs___match1 #define match2 guestfs___match2 +#define match3 guestfs___match3 #endif #endif /* GUESTFS_INTERNAL_H_ */ diff --git a/src/inspect.c b/src/inspect.c index 3b2720c..f5e4490 100644 --- a/src/inspect.c +++ b/src/inspect.c @@ -1526,4 +1526,29 @@ guestfs___match2 (guestfs_h *g, const char *str, const pcre *re, return 1; } +/* Match a regular expression which contains exactly three captures. */ +int +guestfs___match3 (guestfs_h *g, const char *str, const pcre *re, + char **ret1, char **ret2, char **ret3) +{ + size_t len = strlen (str); + int vec[30], r; + + 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. */ + fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n", + __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]); + + return 1; +} + #endif /* HAVE_PCRE */ -- 1.8.3.1