Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 2 | /* regexp.c */ |
| 3 | |
| 4 | #include "internal.h" |
| 5 | #include "regexp.h" |
| 6 | #include <setjmp.h> |
| 7 | #include <stdio.h> |
| 8 | #include <ctype.h> |
| 9 | |
| 10 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 11 | #if ( defined BB_GREP || defined BB_SED) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 12 | |
| 13 | /* This also tries to find a needle in a haystack, but uses |
| 14 | * real regular expressions.... The fake regular expression |
| 15 | * version of find_match lives in utility.c. Using this version |
| 16 | * will add 3.9k to busybox... |
| 17 | * -Erik Andersen |
| 18 | */ |
| 19 | extern int find_match(char *haystack, char *needle, int ignoreCase) |
| 20 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 21 | int status; |
| 22 | struct regexp *re; |
| 23 | |
| 24 | re = regcomp(needle); |
| 25 | status = regexec(re, haystack, FALSE, ignoreCase); |
| 26 | free(re); |
| 27 | return (status); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 30 | #if defined BB_SED |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 31 | /* This performs substitutions after a regexp match has been found. |
| 32 | * The new string is returned. It is malloc'ed, and do must be freed. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | extern int replace_match(char *haystack, char *needle, char *newNeedle, |
| 34 | int ignoreCase) |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 35 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | int status; |
| 37 | struct regexp *re; |
| 38 | char *s, buf[BUF_SIZE], *d = buf; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 39 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 40 | re = regcomp(needle); |
| 41 | status = regexec(re, haystack, FALSE, ignoreCase); |
| 42 | if (status == TRUE) { |
| 43 | s = haystack; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 44 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 45 | do { |
| 46 | /* copy stuff from before the match */ |
| 47 | while (s < re->startp[0]) |
| 48 | *d++ = *s++; |
| 49 | /* substitute for the matched part */ |
| 50 | regsub(re, newNeedle, d); |
| 51 | s = re->endp[0]; |
| 52 | d += strlen(d); |
| 53 | } while (regexec(re, s, FALSE, ignoreCase) == TRUE); |
| 54 | /* copy stuff from after the match */ |
| 55 | while ((*d++ = *s++)) { |
| 56 | } |
| 57 | d[0] = '\0'; |
| 58 | strcpy(haystack, buf); |
| 59 | } |
| 60 | free(re); |
| 61 | return (status); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 62 | } |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 63 | #endif |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 64 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 65 | |
| 66 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to |
| 67 | * suit the needs of busybox by Erik Andersen. |
| 68 | * |
| 69 | * From the README: |
| 70 | * "Elvis is freely redistributable, in either source form or executable form. |
| 71 | * There are no restrictions on how you may use it". |
| 72 | * Elvis was written by Steve Kirkendall <kirkenda@cs.pdx.edu> |
| 73 | * |
| 74 | * |
| 75 | * This file contains the code that compiles regular expressions and executes |
| 76 | * them. It supports the same syntax and features as vi's regular expression |
| 77 | * code. Specifically, the meta characters are: |
| 78 | * ^ matches the beginning of a line |
| 79 | * $ matches the end of a line |
| 80 | * \< matches the beginning of a word |
| 81 | * \> matches the end of a word |
| 82 | * . matches any single character |
| 83 | * [] matches any character in a character class |
| 84 | * \( delimits the start of a subexpression |
| 85 | * \) delimits the end of a subexpression |
| 86 | * * repeats the preceding 0 or more times |
| 87 | * NOTE: You cannot follow a \) with a *. |
| 88 | * |
| 89 | * The physical structure of a compiled RE is as follows: |
| 90 | * - First, there is a one-byte value that says how many character classes |
| 91 | * are used in this regular expression |
| 92 | * - Next, each character class is stored as a bitmap that is 256 bits |
| 93 | * (32 bytes) long. |
| 94 | * - A mixture of literal characters and compiled meta characters follows. |
| 95 | * This begins with M_BEGIN(0) and ends with M_END(0). All meta chars |
| 96 | * are stored as a \n followed by a one-byte code, so they take up two |
| 97 | * bytes apiece. Literal characters take up one byte apiece. \n can't |
| 98 | * be used as a literal character. |
| 99 | * |
| 100 | */ |
| 101 | |
| 102 | |
| 103 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 104 | static char *previous; /* the previous regexp, used when null regexp is given */ |
| 105 | |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 106 | #if defined BB_SED |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 107 | static char *previous1; /* a copy of the text from the previous substitution for regsub() */ |
Eric Andersen | abc0f4f | 1999-12-08 23:19:36 +0000 | [diff] [blame] | 108 | #endif |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | /* These are used to classify or recognize meta-characters */ |
| 112 | #define META '\0' |
| 113 | #define BASE_META(m) ((m) - 256) |
| 114 | #define INT_META(c) ((c) + 256) |
| 115 | #define IS_META(m) ((m) >= 256) |
| 116 | #define IS_CLASS(m) ((m) >= M_CLASS(0) && (m) <= M_CLASS(9)) |
| 117 | #define IS_START(m) ((m) >= M_START(0) && (m) <= M_START(9)) |
| 118 | #define IS_END(m) ((m) >= M_END(0) && (m) <= M_END(9)) |
| 119 | #define IS_CLOSURE(m) ((m) >= M_SPLAT && (m) <= M_QMARK) |
| 120 | #define ADD_META(s,m) (*(s)++ = META, *(s)++ = BASE_META(m)) |
| 121 | #define GET_META(s) (*(s) == META ? INT_META(*++(s)) : *s) |
| 122 | |
| 123 | /* These are the internal codes used for each type of meta-character */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 124 | #define M_BEGLINE 256 /* internal code for ^ */ |
| 125 | #define M_ENDLINE 257 /* internal code for $ */ |
| 126 | #define M_BEGWORD 258 /* internal code for \< */ |
| 127 | #define M_ENDWORD 259 /* internal code for \> */ |
| 128 | #define M_ANY 260 /* internal code for . */ |
| 129 | #define M_SPLAT 261 /* internal code for * */ |
| 130 | #define M_PLUS 262 /* internal code for \+ */ |
| 131 | #define M_QMARK 263 /* internal code for \? */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 132 | #define M_CLASS(n) (264+(n)) /* internal code for [] */ |
| 133 | #define M_START(n) (274+(n)) /* internal code for \( */ |
| 134 | #define M_END(n) (284+(n)) /* internal code for \) */ |
| 135 | |
| 136 | /* These are used during compilation */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 137 | static int class_cnt; /* used to assign class IDs */ |
| 138 | static int start_cnt; /* used to assign start IDs */ |
| 139 | static int end_stk[NSUBEXP]; /* used to assign end IDs */ |
| 140 | static int end_sp; |
| 141 | static char *retext; /* points to the text being compiled */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 142 | |
| 143 | /* error-handling stuff */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | jmp_buf errorhandler; |
| 145 | |
Eric Andersen | b6a44b8 | 1999-11-13 04:47:09 +0000 | [diff] [blame] | 146 | #define FAIL(why) do {fprintf(stderr, why); longjmp(errorhandler, 1);} while (0) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | /* This function builds a bitmap for a particular class */ |
| 152 | /* text -- start of the class */ |
| 153 | /* bmap -- the bitmap */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 154 | static char *makeclass(char *text, char *bmap) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 155 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | int i; |
| 157 | int complement = 0; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 158 | |
| 159 | |
| 160 | /* zero the bitmap */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | for (i = 0; bmap && i < 32; i++) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 162 | bmap[i] = 0; |
| 163 | } |
| 164 | |
| 165 | /* see if we're going to complement this class */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 166 | if (*text == '^') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 167 | text++; |
| 168 | complement = 1; |
| 169 | } |
| 170 | |
| 171 | /* add in the characters */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 172 | while (*text && *text != ']') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 173 | /* is this a span of characters? */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 174 | if (text[1] == '-' && text[2]) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 175 | /* spans can't be backwards */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 176 | if (text[0] > text[2]) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 177 | FAIL("Backwards span in []"); |
| 178 | } |
| 179 | |
| 180 | /* add each character in the span to the bitmap */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | for (i = text[0]; bmap && i <= text[2]; i++) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 182 | bmap[i >> 3] |= (1 << (i & 7)); |
| 183 | } |
| 184 | |
| 185 | /* move past this span */ |
| 186 | text += 3; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 188 | /* add this single character to the span */ |
| 189 | i = *text++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 190 | if (bmap) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 191 | bmap[i >> 3] |= (1 << (i & 7)); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* make sure the closing ] is missing */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | if (*text++ != ']') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 198 | FAIL("] missing"); |
| 199 | } |
| 200 | |
| 201 | /* if we're supposed to complement this class, then do so */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 202 | if (complement && bmap) { |
| 203 | for (i = 0; i < 32; i++) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 204 | bmap[i] = ~bmap[i]; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return text; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | /* This function gets the next character or meta character from a string. |
| 215 | * The pointer is incremented by 1, or by 2 for \-quoted characters. For [], |
| 216 | * a bitmap is generated via makeclass() (if re is given), and the |
| 217 | * character-class text is skipped. |
| 218 | */ |
| 219 | static int gettoken(sptr, re) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | char **sptr; |
| 221 | regexp *re; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 222 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | int c; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 224 | |
| 225 | c = **sptr; |
| 226 | ++*sptr; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 227 | if (c == '\\') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 228 | c = **sptr; |
| 229 | ++*sptr; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 230 | switch (c) { |
| 231 | case '<': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 232 | return M_BEGWORD; |
| 233 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 234 | case '>': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 235 | return M_ENDWORD; |
| 236 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 237 | case '(': |
| 238 | if (start_cnt >= NSUBEXP) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 239 | FAIL("Too many \\(s"); |
| 240 | } |
| 241 | end_stk[end_sp++] = start_cnt; |
| 242 | return M_START(start_cnt++); |
| 243 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 244 | case ')': |
| 245 | if (end_sp <= 0) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 246 | FAIL("Mismatched \\)"); |
| 247 | } |
| 248 | return M_END(end_stk[--end_sp]); |
| 249 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 250 | case '*': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 251 | return M_SPLAT; |
| 252 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 253 | case '.': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 254 | return M_ANY; |
| 255 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 256 | case '+': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 257 | return M_PLUS; |
| 258 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 259 | case '?': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 260 | return M_QMARK; |
| 261 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 262 | default: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 263 | return c; |
| 264 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 265 | } else { |
| 266 | switch (c) { |
| 267 | case '^': |
| 268 | if (*sptr == retext + 1) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 269 | return M_BEGLINE; |
| 270 | } |
| 271 | return c; |
| 272 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 273 | case '$': |
| 274 | if (!**sptr) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 275 | return M_ENDLINE; |
| 276 | } |
| 277 | return c; |
| 278 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 279 | case '.': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 280 | return M_ANY; |
| 281 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 282 | case '*': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 283 | return M_SPLAT; |
| 284 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 285 | case '[': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 286 | /* make sure we don't have too many classes */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 287 | if (class_cnt >= 10) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 288 | FAIL("Too many []s"); |
| 289 | } |
| 290 | |
| 291 | /* process the character list for this class */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 292 | if (re) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 293 | /* generate the bitmap for this class */ |
| 294 | *sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 295 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 296 | /* skip to end of the class */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 297 | *sptr = makeclass(*sptr, (char *) 0); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 298 | } |
| 299 | return M_CLASS(class_cnt++); |
| 300 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 301 | default: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 302 | return c; |
| 303 | } |
| 304 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 305 | /*NOTREACHED*/} |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | /* This function calculates the number of bytes that will be needed for a |
| 311 | * compiled RE. Its argument is the uncompiled version. It is not clever |
| 312 | * about catching syntax errors; that is done in a later pass. |
| 313 | */ |
| 314 | static unsigned calcsize(text) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 315 | char *text; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 316 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 317 | unsigned size; |
| 318 | int token; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 319 | |
| 320 | retext = text; |
| 321 | class_cnt = 0; |
| 322 | start_cnt = 1; |
| 323 | end_sp = 0; |
| 324 | size = 5; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 325 | while ((token = gettoken(&text, (regexp *) 0)) != 0) { |
| 326 | if (IS_CLASS(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 327 | size += 34; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 328 | } else if (IS_META(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 329 | size += 2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 330 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 331 | size++; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return size; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | /*---------------------------------------------------------------------------*/ |
| 341 | |
| 342 | |
| 343 | /* This function checks for a match between a character and a token which is |
| 344 | * known to represent a single character. It returns 0 if they match, or |
| 345 | * 1 if they don't. |
| 346 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 347 | static int match1(regexp * re, char ch, int token, int ignoreCase) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 348 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 349 | if (!ch) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 350 | /* the end of a line can't match any RE of width 1 */ |
| 351 | return 1; |
| 352 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 353 | if (token == M_ANY) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 354 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 355 | } else if (IS_CLASS(token)) { |
Mark Whitley | d166658 | 2000-05-01 22:08:54 +0000 | [diff] [blame] | 356 | if (re->program[1 + 32 * (token - M_CLASS(0)) + (ch >> 3)] & (1 << (ch & 7))) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | } |
Erik Andersen | f4a99cc | 2000-01-05 05:49:51 +0000 | [diff] [blame] | 359 | //fprintf(stderr, "match1: ch='%c' token='%c': ", ch, token); |
Mark Whitley | d166658 | 2000-05-01 22:08:54 +0000 | [diff] [blame] | 360 | if (ch == token || (ignoreCase == TRUE && tolower(ch) == tolower(token))) { |
Erik Andersen | f4a99cc | 2000-01-05 05:49:51 +0000 | [diff] [blame] | 361 | //fprintf(stderr, "match\n"); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 362 | return 0; |
| 363 | } |
Erik Andersen | f4a99cc | 2000-01-05 05:49:51 +0000 | [diff] [blame] | 364 | //fprintf(stderr, "no match\n"); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 365 | return 1; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | |
| 370 | /* This function checks characters up to and including the next closure, at |
| 371 | * which point it does a recursive call to check the rest of it. This function |
| 372 | * returns 0 if everything matches, or 1 if something doesn't match. |
| 373 | */ |
| 374 | /* re -- the regular expression */ |
| 375 | /* str -- the string */ |
| 376 | /* prog -- a portion of re->program, an compiled RE */ |
| 377 | /* here -- a portion of str, the string to compare it to */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 378 | static int match(regexp * re, char *str, char *prog, char *here, |
| 379 | int ignoreCase) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 380 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 381 | int token; |
| 382 | int nmatched; |
| 383 | int closure; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 384 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 385 | for (token = GET_META(prog); !IS_CLOSURE(token); |
| 386 | prog++, token = GET_META(prog)) { |
| 387 | switch (token) { |
| 388 | /*case M_BEGLINE: can't happen; re->bol is used instead */ |
| 389 | case M_ENDLINE: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 390 | if (*here) |
| 391 | return 1; |
| 392 | break; |
| 393 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 394 | case M_BEGWORD: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 395 | if (here != str && |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 396 | (here[-1] == '_' || |
| 397 | (isascii(here[-1]) && isalnum(here[-1])))) return 1; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 398 | break; |
| 399 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 400 | case M_ENDWORD: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 401 | if ((here[0] == '_' || isascii(here[0])) && isalnum(here[0])) |
| 402 | return 1; |
| 403 | break; |
| 404 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 405 | case M_START(0): |
| 406 | case M_START(1): |
| 407 | case M_START(2): |
| 408 | case M_START(3): |
| 409 | case M_START(4): |
| 410 | case M_START(5): |
| 411 | case M_START(6): |
| 412 | case M_START(7): |
| 413 | case M_START(8): |
| 414 | case M_START(9): |
| 415 | re->startp[token - M_START(0)] = (char *) here; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 416 | break; |
| 417 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 418 | case M_END(0): |
| 419 | case M_END(1): |
| 420 | case M_END(2): |
| 421 | case M_END(3): |
| 422 | case M_END(4): |
| 423 | case M_END(5): |
| 424 | case M_END(6): |
| 425 | case M_END(7): |
| 426 | case M_END(8): |
| 427 | case M_END(9): |
| 428 | re->endp[token - M_END(0)] = (char *) here; |
| 429 | if (token == M_END(0)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | break; |
| 433 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 434 | default: /* literal, M_CLASS(n), or M_ANY */ |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 435 | if (match1(re, *here, token, ignoreCase) != 0) |
| 436 | return 1; |
| 437 | here++; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /* C L O S U R E */ |
| 442 | |
| 443 | /* step 1: see what we have to match against, and move "prog" to point |
| 444 | * the the remainder of the compiled RE. |
| 445 | */ |
| 446 | closure = token; |
| 447 | prog++, token = GET_META(prog); |
| 448 | prog++; |
| 449 | |
| 450 | /* step 2: see how many times we can match that token against the string */ |
| 451 | for (nmatched = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | (closure != M_QMARK || nmatched < 1) && *here |
| 453 | && match1(re, *here, token, ignoreCase) == 0; nmatched++, here++) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* step 3: try to match the remainder, and back off if it doesn't */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 457 | while (nmatched >= 0 && match(re, str, prog, here, ignoreCase) != 0) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 458 | nmatched--; |
| 459 | here--; |
| 460 | } |
| 461 | |
| 462 | /* so how did it work out? */ |
| 463 | if (nmatched >= ((closure == M_PLUS) ? 1 : 0)) |
| 464 | return 0; |
| 465 | return 1; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* This function compiles a regexp. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 470 | extern regexp *regcomp(char *text) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 471 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 472 | int needfirst; |
| 473 | unsigned size; |
| 474 | int token; |
| 475 | int peek; |
| 476 | char *build; |
| 477 | regexp *re; // Ignore compiler whining. If we longjmp, we don't use re anymore. |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 478 | |
| 479 | |
| 480 | /* prepare for error handling */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 481 | re = (regexp *) 0; |
| 482 | if (setjmp(errorhandler)) { |
| 483 | if (re) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 484 | free(re); |
| 485 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 486 | return (regexp *) 0; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* if an empty regexp string was given, use the previous one */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 490 | if (*text == 0) { |
| 491 | if (!previous) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 492 | FAIL("No previous RE"); |
| 493 | } |
| 494 | text = previous; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 495 | } else { /* non-empty regexp given, so remember it */ |
| 496 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 497 | if (previous) |
| 498 | free(previous); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 499 | previous = (char *) malloc((unsigned) (strlen(text) + 1)); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 500 | if (previous) |
| 501 | strcpy(previous, text); |
| 502 | } |
| 503 | |
| 504 | /* allocate memory */ |
| 505 | class_cnt = 0; |
| 506 | start_cnt = 1; |
| 507 | end_sp = 0; |
| 508 | retext = text; |
| 509 | size = calcsize(text) + sizeof(regexp); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 510 | re = (regexp *) malloc((unsigned) size); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 511 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 512 | if (!re) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 513 | FAIL("Not enough memory for this RE"); |
| 514 | } |
| 515 | |
| 516 | /* compile it */ |
| 517 | build = &re->program[1 + 32 * class_cnt]; |
| 518 | re->program[0] = class_cnt; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 519 | for (token = 0; token < NSUBEXP; token++) { |
| 520 | re->startp[token] = re->endp[token] = (char *) 0; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 521 | } |
| 522 | re->first = 0; |
| 523 | re->bol = 0; |
| 524 | re->minlen = 0; |
| 525 | needfirst = 1; |
| 526 | class_cnt = 0; |
| 527 | start_cnt = 1; |
| 528 | end_sp = 0; |
| 529 | retext = text; |
| 530 | for (token = M_START(0), peek = gettoken(&text, re); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 531 | token; token = peek, peek = gettoken(&text, re)) { |
Mark Whitley | d166658 | 2000-05-01 22:08:54 +0000 | [diff] [blame] | 532 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 533 | /* special processing for the closure operator */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 534 | if (IS_CLOSURE(peek)) { |
Mark Whitley | d166658 | 2000-05-01 22:08:54 +0000 | [diff] [blame] | 535 | |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 536 | /* detect misuse of closure operator */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 537 | if (IS_START(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 538 | FAIL("* or \\+ or \\? follows nothing"); |
Mark Whitley | d166658 | 2000-05-01 22:08:54 +0000 | [diff] [blame] | 539 | } else if (IS_META(token) && token != M_ANY && !IS_CLASS(token)) { |
| 540 | FAIL("* or \\+ or \\? can only follow a normal character or . or []"); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* it is okay -- make it prefix instead of postfix */ |
| 544 | ADD_META(build, peek); |
| 545 | |
| 546 | /* take care of "needfirst" - is this the first char? */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 547 | if (needfirst && peek == M_PLUS && !IS_META(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 548 | re->first = token; |
| 549 | } |
| 550 | needfirst = 0; |
| 551 | |
| 552 | /* we used "peek" -- need to refill it */ |
| 553 | peek = gettoken(&text, re); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 554 | if (IS_CLOSURE(peek)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 555 | FAIL("* or \\+ or \\? doubled up"); |
| 556 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 557 | } else if (!IS_META(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 558 | /* normal char is NOT argument of closure */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 559 | if (needfirst) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 560 | re->first = token; |
| 561 | needfirst = 0; |
| 562 | } |
| 563 | re->minlen++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 564 | } else if (token == M_ANY || IS_CLASS(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 565 | /* . or [] is NOT argument of closure */ |
| 566 | needfirst = 0; |
| 567 | re->minlen++; |
| 568 | } |
| 569 | |
| 570 | /* the "token" character is not closure -- process it normally */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 571 | if (token == M_BEGLINE) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 572 | /* set the BOL flag instead of storing M_BEGLINE */ |
| 573 | re->bol = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 574 | } else if (IS_META(token)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 575 | ADD_META(build, token); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 576 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 577 | *build++ = token; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | /* end it with a \) which MUST MATCH the opening \( */ |
| 582 | ADD_META(build, M_END(0)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 583 | if (end_sp > 0) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 584 | FAIL("Not enough \\)s"); |
| 585 | } |
| 586 | |
| 587 | return re; |
| 588 | } |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | /* This function searches through a string for text that matches an RE. */ |
| 594 | /* re -- the compiled regexp to search for */ |
| 595 | /* str -- the string to search through */ |
| 596 | /* bol -- does str start at the beginning of a line? (boolean) */ |
| 597 | /* ignoreCase -- ignoreCase or not */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 598 | extern int regexec(struct regexp *re, char *str, int bol, int ignoreCase) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 599 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 600 | char *prog; /* the entry point of re->program */ |
| 601 | int len; /* length of the string */ |
| 602 | char *here; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 603 | |
| 604 | /* if must start at the beginning of a line, and this isn't, then fail */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 605 | if (re->bol && bol == TRUE) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 606 | return FALSE; |
| 607 | } |
| 608 | |
| 609 | len = strlen(str); |
| 610 | prog = re->program + 1 + 32 * re->program[0]; |
| 611 | |
| 612 | /* search for the RE in the string */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 613 | if (re->bol) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 614 | /* must occur at BOL */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 615 | if ((re->first && match1(re, *(char *) str, re->first, ignoreCase)) /* wrong first letter? */ |
| 616 | ||len < re->minlen /* not long enough? */ |
| 617 | || match(re, (char *) str, prog, str, ignoreCase)) /* doesn't match? */ |
| 618 | return FALSE; /* THEN FAIL! */ |
| 619 | } else if (ignoreCase == FALSE) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 620 | /* can occur anywhere in the line, noignorecase */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 621 | for (here = (char *) str; (re->first && re->first != *here) |
| 622 | || match(re, (char *) str, prog, here, ignoreCase); |
| 623 | here++, len--) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 624 | if (len < re->minlen) |
| 625 | return FALSE; |
| 626 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 627 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 628 | /* can occur anywhere in the line, ignorecase */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 629 | for (here = (char *) str; |
| 630 | (re->first && match1(re, *here, (int) re->first, ignoreCase)) |
| 631 | || match(re, (char *) str, prog, here, ignoreCase); |
| 632 | here++, len--) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 633 | if (len < re->minlen) |
| 634 | return FALSE; |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /* if we didn't fail, then we must have succeeded */ |
| 639 | return TRUE; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | |
| 644 | |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 645 | #if defined BB_SED |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 646 | /* This performs substitutions after a regexp match has been found. */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 647 | extern void regsub(regexp * re, char *src, char *dst) |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 648 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 649 | char *cpy; |
| 650 | char *end; |
| 651 | char c; |
| 652 | char *start; |
| 653 | int mod; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 654 | |
| 655 | mod = 0; |
| 656 | |
| 657 | start = src; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 658 | while ((c = *src++) != '\0') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 659 | /* recognize any meta characters */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 660 | if (c == '&') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 661 | cpy = re->startp[0]; |
| 662 | end = re->endp[0]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 663 | } else if (c == '~') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 664 | cpy = previous1; |
| 665 | if (cpy) |
| 666 | end = cpy + strlen(cpy); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 667 | } else if (c == '\\') { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 668 | c = *src++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 669 | switch (c) { |
| 670 | case '0': |
| 671 | case '1': |
| 672 | case '2': |
| 673 | case '3': |
| 674 | case '4': |
| 675 | case '5': |
| 676 | case '6': |
| 677 | case '7': |
| 678 | case '8': |
| 679 | case '9': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 680 | /* \0 thru \9 mean "copy subexpression" */ |
| 681 | c -= '0'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 682 | cpy = re->startp[(int) c]; |
| 683 | end = re->endp[(int) c]; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 684 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 685 | case 'U': |
| 686 | case 'u': |
| 687 | case 'L': |
| 688 | case 'l': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 689 | /* \U and \L mean "convert to upper/lowercase" */ |
| 690 | mod = c; |
| 691 | continue; |
| 692 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 693 | case 'E': |
| 694 | case 'e': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 695 | /* \E ends the \U or \L */ |
| 696 | mod = 0; |
| 697 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 698 | case '&': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 699 | /* "\&" means "original text" */ |
| 700 | *dst++ = c; |
| 701 | continue; |
| 702 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | case '~': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 704 | /* "\~" means "previous text, if any" */ |
| 705 | *dst++ = c; |
| 706 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 707 | default: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 708 | /* ordinary char preceded by backslash */ |
| 709 | *dst++ = c; |
| 710 | continue; |
| 711 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 712 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 713 | /* ordinary character, so just copy it */ |
| 714 | *dst++ = c; |
| 715 | continue; |
| 716 | } |
| 717 | |
| 718 | /* Note: to reach this point in the code, we must have evaded |
| 719 | * all "continue" statements. To do that, we must have hit |
| 720 | * a metacharacter that involves copying. |
| 721 | */ |
| 722 | |
| 723 | /* if there is nothing to copy, loop */ |
| 724 | if (!cpy) |
| 725 | continue; |
| 726 | |
| 727 | /* copy over a portion of the original */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 728 | while (cpy < end) { |
| 729 | switch (mod) { |
| 730 | case 'U': |
| 731 | case 'u': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 732 | /* convert to uppercase */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 733 | if (isascii(*cpy) && islower(*cpy)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 734 | *dst++ = toupper(*cpy); |
| 735 | cpy++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 736 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 737 | *dst++ = *cpy++; |
| 738 | } |
| 739 | break; |
| 740 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 741 | case 'L': |
| 742 | case 'l': |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 743 | /* convert to lowercase */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 744 | if (isascii(*cpy) && isupper(*cpy)) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 745 | *dst++ = tolower(*cpy); |
| 746 | cpy++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 747 | } else { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 748 | *dst++ = *cpy++; |
| 749 | } |
| 750 | break; |
| 751 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 752 | default: |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 753 | /* copy without any conversion */ |
| 754 | *dst++ = *cpy++; |
| 755 | } |
| 756 | |
| 757 | /* \u and \l end automatically after the first char */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 758 | if (mod && (mod == 'u' || mod == 'l')) { |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 759 | mod = 0; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | *dst = '\0'; |
| 764 | |
| 765 | /* remember what text we inserted this time */ |
| 766 | if (previous1) |
| 767 | free(previous1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 768 | previous1 = (char *) malloc((unsigned) (strlen(start) + 1)); |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 769 | if (previous1) |
| 770 | strcpy(previous1, start); |
| 771 | } |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 772 | #endif |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 773 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 774 | #endif /* BB_REGEXP */ |