the beginnings of a proper man page for busybox.
[oweals/busybox.git] / regexp.c
index 6017d79b4645e06befb111cd04af93495b0e25f5..11b46c72029677cc1788ffa4a742455172a3ae89 100644 (file)
--- a/regexp.c
+++ b/regexp.c
@@ -7,7 +7,7 @@
 #include <ctype.h>
 
 
-#if ( defined BB_GREP || defined BB_FIND )
+#if ( defined BB_GREP || defined BB_SED)
 
 /* This also tries to find a needle in a haystack, but uses
  * real regular expressions....  The fake regular expression
@@ -25,6 +25,39 @@ extern int find_match(char *haystack, char *needle, int ignoreCase)
     return( status);
 }
 
+#if defined BB_SED
+/* This performs substitutions after a regexp match has been found.  
+ * The new string is returned.  It is malloc'ed, and do must be freed. */
+extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase)
+{
+    int status;
+    struct regexp*  re;
+    char *s, buf[BUF_SIZE], *d = buf;
+
+    re = regcomp( needle);
+    status = regexec(re, haystack, FALSE, ignoreCase);
+    if (status==TRUE) {
+       s=haystack;
+
+       do {
+           /* copy stuff from before the match */
+           while (s < re->startp[0])
+               *d++ = *s++;
+           /* substitute for the matched part */
+           regsub(re, newNeedle, d);
+           s = re->endp[0];
+           d += strlen(d);
+       } while (regexec(re, s, FALSE, ignoreCase) == TRUE);
+        /* copy stuff from after the match */
+       while ( (*d++ = *s++) ) {}
+       d[0] = '\0';
+       strcpy(haystack, buf);
+    }
+    free( re);
+    return( status);
+}
+#endif
+
 
 /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to 
  * suit the needs of busybox by Erik Andersen.
@@ -65,7 +98,9 @@ extern int find_match(char *haystack, char *needle, int ignoreCase)
 
 
 static char *previous; /* the previous regexp, used when null regexp is given */
+#if defined BB_SED
 static char *previous1;        /* a copy of the text from the previous substitution for regsub()*/
+#endif
 
 
 /* These are used to classify or recognize meta-characters */
@@ -102,7 +137,7 @@ static char *retext;        /* points to the text being compiled */
 
 /* error-handling stuff */
 jmp_buf        errorhandler;
-#define FAIL(why)      fprintf(stderr, why); longjmp(errorhandler, 1)
+#define FAIL(why)  do {fprintf(stderr, why); longjmp(errorhandler, 1);} while (0)
 
 
 
@@ -350,11 +385,14 @@ static int match1(regexp* re, char ch, int token, int ignoreCase)
                if (re->program[1 + 32 * (token - M_CLASS(0)) + (ch >> 3)] & (1 << (ch & 7)))
                        return 0;
        }
-       else if (ch == token
-               || (ignoreCase==TRUE && isupper(ch) && tolower(ch) == token))
+//fprintf(stderr, "match1: ch='%c' token='%c': ", ch, token);
+       if (ch == token
+               || (ignoreCase==TRUE && tolower(ch) == tolower(token)))
        {
+//fprintf(stderr, "match\n");
                return 0;
        }
+//fprintf(stderr, "no match\n");
        return 1;
 }
 
@@ -471,7 +509,7 @@ extern regexp *regcomp(char* text)
        int             token;
        int             peek;
        char            *build;
-       regexp          *re;
+       regexp          *re; // Ignore compiler whining.  If we longjmp, we don't use re anymore.
 
 
        /* prepare for error handling */
@@ -673,7 +711,7 @@ extern int regexec(struct regexp* re, char* str, int bol, int ignoreCase)
 
 
 
-
+#if defined BB_SED
 /* This performs substitutions after a regexp match has been found.  */
 extern void regsub(regexp* re, char* src, char* dst)
 {
@@ -819,7 +857,7 @@ extern void regsub(regexp* re, char* src, char* dst)
        if (previous1)
                strcpy(previous1, start);
 }
-
+#endif
 
 #endif /* BB_REGEXP */