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