diff options
Diffstat (limited to 'regexp.c')
-rw-r--r-- | regexp.c | 40 |
1 files changed, 26 insertions, 14 deletions
@@ -7,7 +7,7 @@ | |||
7 | #include <ctype.h> | 7 | #include <ctype.h> |
8 | 8 | ||
9 | 9 | ||
10 | #if ( defined BB_GREP || defined BB_FIND ) | 10 | #if ( defined BB_GREP || defined BB_FIND || defined BB_SED) |
11 | 11 | ||
12 | /* This also tries to find a needle in a haystack, but uses | 12 | /* This also tries to find a needle in a haystack, but uses |
13 | * real regular expressions.... The fake regular expression | 13 | * real regular expressions.... The fake regular expression |
@@ -25,27 +25,39 @@ extern int find_match(char *haystack, char *needle, int ignoreCase) | |||
25 | return( status); | 25 | return( status); |
26 | } | 26 | } |
27 | 27 | ||
28 | #if defined BB_SED | ||
28 | /* This performs substitutions after a regexp match has been found. | 29 | /* This performs substitutions after a regexp match has been found. |
29 | * The new string is returned. It is malloc'ed, and do must be freed. */ | 30 | * The new string is returned. It is malloc'ed, and do must be freed. */ |
30 | extern char* replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) | 31 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) |
31 | { | 32 | { |
32 | int status; | 33 | int status; |
33 | char* newHaystack; | ||
34 | struct regexp* re; | 34 | struct regexp* re; |
35 | newHaystack = (char *)malloc((unsigned)(strlen(haystack) - | 35 | char *s, buf[BUF_SIZE], *d = buf; |
36 | strlen(needle) + strlen(newNeedle)); | 36 | |
37 | re = regcomp( needle); | 37 | re = regcomp( needle); |
38 | status = regexec(re, haystack, FALSE, ignoreCase); | 38 | status = regexec(re, haystack, FALSE, ignoreCase); |
39 | 39 | if (status==TRUE) { | |
40 | return( newHaystack) | 40 | s=haystack; |
41 | } | 41 | |
42 | 42 | do { | |
43 | 43 | /* copy stuff from before the match */ | |
44 | extern void regsub(regexp* re, char* src, char* dst) | 44 | while (s < re->startp[0]) |
45 | 45 | *d++ = *s++; | |
46 | /* substitute for the matched part */ | ||
47 | regsub(re, newNeedle, d); | ||
48 | s = re->endp[0]; | ||
49 | d += strlen(d); | ||
50 | } while (regexec(re, s, FALSE, ignoreCase) == TRUE); | ||
51 | /* copy stuff from after the match */ | ||
52 | while ( (*d++ = *s++) ) {} | ||
53 | d[-1] = '\n'; | ||
54 | d[0] = '\0'; | ||
55 | strcpy(haystack, buf); | ||
56 | } | ||
46 | free( re); | 57 | free( re); |
47 | return( status); | 58 | return( status); |
48 | } | 59 | } |
60 | #endif | ||
49 | 61 | ||
50 | 62 | ||
51 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to | 63 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to |
@@ -695,7 +707,7 @@ extern int regexec(struct regexp* re, char* str, int bol, int ignoreCase) | |||
695 | 707 | ||
696 | 708 | ||
697 | 709 | ||
698 | 710 | #if defined BB_SED | |
699 | /* This performs substitutions after a regexp match has been found. */ | 711 | /* This performs substitutions after a regexp match has been found. */ |
700 | extern void regsub(regexp* re, char* src, char* dst) | 712 | extern void regsub(regexp* re, char* src, char* dst) |
701 | { | 713 | { |
@@ -841,7 +853,7 @@ extern void regsub(regexp* re, char* src, char* dst) | |||
841 | if (previous1) | 853 | if (previous1) |
842 | strcpy(previous1, start); | 854 | strcpy(previous1, start); |
843 | } | 855 | } |
844 | 856 | #endif | |
845 | 857 | ||
846 | #endif /* BB_REGEXP */ | 858 | #endif /* BB_REGEXP */ |
847 | 859 | ||