OSSP CVS Repository

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

ossp-pkg/var/var.c 1.6 -> 1.7

--- var.c        2001/11/13 13:32:58     1.6
+++ var.c        2001/11/13 14:36:55     1.7
@@ -340,6 +340,13 @@
                 return VAR_INCOMPLETE_NAMED_CHARACTER;
             switch (*src)
                 {
+                case '\\':
+                    if (!unescape_all)
+                        {
+                        *dst++ = '\\';
+                        }
+                    *dst++ = '\\';
+                    break;
                 case 'n':
                     *dst++ = '\n';
                     break;
@@ -925,6 +932,67 @@
     return VAR_OK;
     }
 
+static int expand_regex_replace(const char* data, tokenbuf* orig, regmatch_t* pmatch, tokenbuf* expanded)
+    {
+    const char* p = orig->begin;
+    size_t i;
+
+    init_tokenbuf(expanded);
+
+    while(p != orig->end)
+        {
+        if (*p == '\\')
+            {
+            if (orig->end - p <= 1)
+                {
+                free_tokenbuf(expanded);
+                return VAR_INCOMPLETE_QUOTED_PAIR;
+                }
+            else
+                ++p;
+            if (*p == '\\')
+                {
+                if (!append_to_tokenbuf(expanded, p, 1))
+                    {
+                    free_tokenbuf(expanded);
+                    return VAR_OUT_OF_MEMORY;
+                    }
+                ++p;
+                continue;
+                }
+            if (!isdigit(*p))
+                {
+                free_tokenbuf(expanded);
+                return VAR_UNKNOWN_QUOTED_PAIR_IN_REPLACE;
+                }
+            i = *p - '0';
+            ++p;
+            if (pmatch[i].rm_so == -1)
+                {
+                free_tokenbuf(expanded);
+                return VAR_SUBMATCH_OUT_OF_RANGE;
+                }
+            if (!append_to_tokenbuf(expanded, data + pmatch[i].rm_so,
+                                    pmatch[i].rm_eo - pmatch[i].rm_so))
+                {
+                free_tokenbuf(expanded);
+                return VAR_OUT_OF_MEMORY;
+                }
+            }
+        else
+            {
+            if (!append_to_tokenbuf(expanded, p, 1))
+                {
+                free_tokenbuf(expanded);
+                return VAR_OUT_OF_MEMORY;
+                }
+            ++p;
+            }
+        }
+
+    return VAR_OK;
+    }
+
 static int search_and_replace(tokenbuf* data, tokenbuf* search, tokenbuf* replace, tokenbuf* flags)
     {
     const char* p;
@@ -936,24 +1004,18 @@
     if (search->begin == search->end)
         return VAR_EMPTY_SEARCH_STRING;
 
-    printf("Search '%s' in '%s' and replace it with '%s'.\n",
-           search->begin, data->begin, replace->begin);
-
     for (p = flags->begin; p != flags->end; ++p)
         {
         switch (tolower(*p))
             {
             case 'i':
                 case_insensitive = 1;
-                printf("case_insensitive = 1;\n");
                 break;
             case 'g':
                 global = 1;
-                printf("global = 1;\n");
                 break;
             case 't':
                 no_regex = 1;
-                printf("no_regex = 1;\n");
                 break;
             default:
                 return VAR_UNKNOWN_REPLACE_FLAG;
@@ -1003,8 +1065,9 @@
         {
         tokenbuf tmp;
         tokenbuf mydata;
+        tokenbuf myreplace;
         regex_t preg;
-        regmatch_t pmatch[33];
+        regmatch_t pmatch[10];
         int regexec_flag;
 
         /* Copy the pattern and the data to our own buffer to make
@@ -1020,9 +1083,6 @@
 
         /* Compile the pattern. */
 
-        printf("data is.................: '%s'\n", mydata.begin);
-        printf("regex search pattern is.: '%s'\n", tmp.begin);
-        printf("regex replace pattern is: '%s'\n", replace->begin);
         rc = regcomp(&preg, tmp.begin, REG_EXTENDED | ((case_insensitive) ? REG_ICASE : 0));
         free_tokenbuf(&tmp);
         if (rc != 0)
@@ -1030,7 +1090,6 @@
             free_tokenbuf(&mydata);
             return VAR_INVALID_REGEX_IN_REPLACE;
             }
-        printf("Subexpression in pattern: '%d'\n", preg.re_nsub);
 
         /* Match the pattern and create the result string in the tmp
            buffer. */
@@ -1043,22 +1102,33 @@
                 regexec_flag = REG_NOTBOL;
             if (regexec(&preg, p, sizeof(pmatch) / sizeof(regmatch_t), pmatch, regexec_flag) == REG_NOMATCH)
                 {
-                printf("No match; appending remainder ('%s') to output string.\n", p);
                 append_to_tokenbuf(&tmp, p, mydata.end - p);
                 break;
                 }
             else
                 {
+                rc = expand_regex_replace(p, replace, pmatch, &myreplace);
+                if (rc != VAR_OK)
+                    {
+                    regfree(&preg);
+                    free_tokenbuf(&tmp);
+                    free_tokenbuf(&mydata);
+                    return rc;
+                    }
                 if (!append_to_tokenbuf(&tmp, p, pmatch[0].rm_so) ||
-                    !append_to_tokenbuf(&tmp, replace->begin, replace->end - replace->begin))
+                    !append_to_tokenbuf(&tmp, myreplace.begin, myreplace.end - myreplace.begin))
                     {
                     regfree(&preg);
                     free_tokenbuf(&tmp);
                     free_tokenbuf(&mydata);
+                    free_tokenbuf(&myreplace);
                     return VAR_OUT_OF_MEMORY;
                     }
                 else
-                    p += pmatch[0].rm_eo;
+                    {
+                    p += (pmatch[0].rm_eo > 0) ? pmatch[0].rm_eo : 1;
+                    free_tokenbuf(&myreplace);
+                    }
                 if (!global)
                     {
                     append_to_tokenbuf(&tmp, p, mydata.end - p);

CVSTrac 2.0.1