LP#1468422 API uses pcre instead of regex.h -- needs autoconf -lpcre update
authorBill Erickson <berickxx@gmail.com>
Thu, 23 Jul 2015 21:40:34 +0000 (17:40 -0400)
committerBill Erickson <berickxx@gmail.com>
Mon, 23 Nov 2015 16:17:05 +0000 (11:17 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/c-apps/oils_auth.c

index 9549c8e..56f3a32 100644 (file)
@@ -6,7 +6,7 @@
 #include "openils/oils_utils.h"
 #include "openils/oils_constants.h"
 #include "openils/oils_event.h"
-#include <regex.h>
+#include <pcre.h>
 
 #define OILS_AUTH_CACHE_PRFX "oils_auth_"
 #define OILS_AUTH_COUNT_SFFX "_count"
@@ -341,33 +341,43 @@ static int oilsAuthIdentIsBarcode(const char* identifier) {
     if (!bc_regex) {
         // if no regex is set, assume any identifier starting
         // with a number is a barcode.
-        bc_regex = strdup("^[[:digit:]]"); // dupe for later free'ing
+        bc_regex = strdup("^\\d"); // dupe for later free'ing
     }
 
-    regex_t regex;
-    int is_barcode = 0;
-    int regret;
-    char regerr[100];
+    const char *err_str;
+    int err_offset, match_ret;
 
-    regret = regcomp(&regex, bc_regex, 0);
-    if (regret) {
-        osrfLogError(OSRF_LOG_MARK, 
-            "Cannot compile barcode regex: %s", bc_regex);
-    } else {
-        regret = regexec(&regex, identifier, 0, NULL, 0);
-        if (!regret) {
-            is_barcode = true;
-        } else if (regret != REG_NOMATCH) {
-            regerror(regret, &regex, regerr, sizeof(regerr));
-            osrfLogError(OSRF_LOG_MARK, 
-                "Error processing regex %s on %s : %s", 
-                bc_regex, identifier, regerr);
-        }
+    pcre *compiled = pcre_compile(
+        bc_regex, 0, &err_str, &err_offset, NULL);
+
+    if (compiled == NULL) {
+        osrfLogError(OSRF_LOG_MARK,
+            "Could not compile '%s': %s", bc_regex, err_str);
+        pcre_free(compiled);
+        return 0;
     }
 
-    regfree(&regex);
-    free(bc_regex);
-    return is_barcode;
+    pcre_extra *extra = pcre_study(compiled, 0, &err_str);
+
+    if(err_str != NULL) {
+        osrfLogError(OSRF_LOG_MARK,
+            "Could not study regex '%s': %s", bc_regex, err_str);
+        pcre_free(compiled);
+        return 0;
+    } 
+
+    match_ret = pcre_exec(
+        compiled, extra, identifier, strlen(identifier), 0, 0, NULL, 0);       
+
+    pcre_free(compiled);
+    if (extra) pcre_free(extra);
+
+    if (match_ret >= 0) return 1; // regex matched
+
+    if (match_ret != PCRE_ERROR_NOMATCH) 
+        osrfLogError(OSRF_LOG_MARK, "Unknown error processing barcode regex");
+
+    return 0; // regex did not match
 }