OSSP CVS Repository

ossp - Difference in ossp-pkg/fsl/fsl.c versions 1.7 and 1.8
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/fsl/fsl.c 1.7 -> 1.8

--- fsl.c        2002/07/16 15:59:44     1.7
+++ fsl.c        2002/07/17 09:57:44     1.8
@@ -200,6 +200,61 @@
     return;
 }
 
+/* Substitutes $[0-9] in inbuf[inlen] with captured strings passed through capary[nary].
+ * Returns the number of characters (to be) written to output.  Output can be suppressed
+ * by passing a NULL outpbuf. The terminating NUL is not counted but will be written!
+ */
+int substcapture(const char *inbuf, int inlen, const char **capary, int nary, char *outbuf)
+{
+    int  i; /* input cursor, current position */
+    int  n; /* outbuf cursor, next possible write position */
+    int  m; /* index to capary */
+    char c; /* scratch variable */
+
+    i = 0;
+    n = 0;
+    while (i < inlen - 1 /* last char cannot start a two-character sequence, skipping it allows safe look ahead */) {
+        c = inbuf[i];
+        if (c == '$') {
+            c = inbuf[i+1];
+            if (c == '$') {
+                if (outbuf != NULL)
+                    outbuf[n] = '$';
+                i+=2;
+                n++;
+            }
+            else if ((c >= '0') && (c <= '9')) {
+                m = c - '0';
+                if (outbuf != NULL) {
+                    outbuf[n] = '\0';
+                    strcat(&outbuf[n], ((m < nary) && (capary[m] != NULL)) ? capary[m] : "");
+                }
+                i+=2;
+                n+= ((m < nary) && (capary[m] != NULL)) ? strlen(capary[m]) : 0;
+            }
+            else {
+                if (outbuf != NULL)
+                    outbuf[n] = '$';
+                i++;
+                n++;
+                if (outbuf != NULL)
+                    outbuf[n] = c;
+                i++;
+                n++;
+            }
+        }
+        else {
+            if (outbuf != NULL)
+                outbuf[n] = c;
+            i++;
+            n++;
+        }
+    }
+    if (outbuf != NULL)
+        outbuf[n] = '\0';
+    return n;
+}
+
 void openlog(const char *ident, int logopt, int facility)
 {
     int rc;
@@ -215,8 +270,9 @@
     char *argident;
     char *argmatch;
     char *argl2spec;
-    char *cp;
-    int   i;
+    char *cp; /* scratch variable */
+    int   i;  /* scratch variable */
+    int   n;  /* scratch variable */
     char *cpIdent;
     char *cpFacility;
     char *cpISF;
@@ -228,12 +284,14 @@
     buf.base = NULL;
     buf.used = 0;
     buf.size = 0;
+    cpIdent = NULL;
     cpISF = NULL;
 
 fprintf(stderr, "DEBUG: BEGIN ident=\"%s\", logopt=0x%.8lx, facility=0x%.8lx\n", ident, (unsigned long)logopt, (unsigned long)facility);
 
     /* create IdentSlashFacility */
-    cpIdent = ident != NULL ? ident : "unknown";
+    if ((cpIdent = strdup((ident != NULL) ? ident : "unknown")) == NULL) {
+        fprintf(stderr, "ERROR: strdup() failed\n"); CU(1); }
     cpFacility = "unknown";
     for (i = 0; facility2string[i].string != NULL; i++) {
         if (facility == facility2string[i].facility) {
@@ -349,12 +407,17 @@
             nMatch = pcre_exec(pcreRegex, pcreExtra, cpISF, strlen(cpISF), 0, 0, ovec, OVECSIZE);
             fprintf(stderr, "DEBUG: nMatch=%d when \"%s\" is used against \"%s\"\n", nMatch, argmatch, cpISF);
             if (nMatch >= 1) {
-                int i;
-                char *cp;
                 pcre_get_substring_list(cpISF, ovec, nMatch, &acpMatch);
                 if (acpMatch != NULL)
                     for (i = 0; i < nMatch; i++)
                         fprintf(stderr, "DEBUG: regex reference[%d]=\'%s\'\n", i, acpMatch[i] == NULL ? "(UNDEFINED)" : acpMatch[i]);
+                n = substcapture(argl2spec, strlen(argl2spec), acpMatch, nMatch, NULL);
+                if ((cp = (char *)malloc(n + 1)) == NULL) {
+                    fprintf(stderr, "ERROR: malloc() failed\n"); CU(1); }
+                if (substcapture(argl2spec, strlen(argl2spec), acpMatch, nMatch, cp) != n) {
+                    fprintf(stderr, "ERROR: substcapture() failed\n"); CU(1); }
+                fprintf(stderr, "DEBUG: cp=\"%s\"\n", cp);
+                free(cp);
             }
         }
 
@@ -369,6 +432,8 @@
 CUS:
     if (cpISF != NULL)
         free(cpISF);
+    if (cpIdent != NULL)
+        free(cpIdent);
 #if 0
     if (cfg != NULL)
         if ((cfgrv = cfg_destroy(cfg)) != CFG_OK) {

CVSTrac 2.0.1