blob: 6fedb01bae6f20f22dc519e74e63834d892f2ada [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenaa0765e1999-10-22 04:30:20 +00002/* 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 Whitley268b8c42000-06-28 21:59:31 +000011#define NSUBEXP 10
12typedef 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
22static regexp *regcomp(char* text);
23static int regexec(struct regexp* re, char* str, int bol, int ignoreCase);
24static void regsub(struct regexp* re, char* src, char* dst);
25
Eric Andersenb186d981999-12-03 09:19:54 +000026#if ( defined BB_GREP || defined BB_SED)
Eric Andersenaa0765e1999-10-22 04:30:20 +000027
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 */
34extern int find_match(char *haystack, char *needle, int ignoreCase)
35{
Erik Andersene49d5ec2000-02-08 19:58:47 +000036 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 Andersenaa0765e1999-10-22 04:30:20 +000043}
44
Eric Andersen7f1acfd1999-10-29 23:09:13 +000045#if defined BB_SED
Eric Andersenc1525e81999-10-29 00:07:31 +000046/* 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 Andersene49d5ec2000-02-08 19:58:47 +000048extern int replace_match(char *haystack, char *needle, char *newNeedle,
49 int ignoreCase)
Eric Andersenc1525e81999-10-29 00:07:31 +000050{
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 int status;
52 struct regexp *re;
53 char *s, buf[BUF_SIZE], *d = buf;
Eric Andersen7f1acfd1999-10-29 23:09:13 +000054
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 re = regcomp(needle);
56 status = regexec(re, haystack, FALSE, ignoreCase);
57 if (status == TRUE) {
58 s = haystack;
Eric Andersenc1525e81999-10-29 00:07:31 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 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 Andersenc1525e81999-10-29 00:07:31 +000077}
Eric Andersen7f1acfd1999-10-29 23:09:13 +000078#endif
Eric Andersenc1525e81999-10-29 00:07:31 +000079
Eric Andersenaa0765e1999-10-22 04:30:20 +000080
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 Andersene49d5ec2000-02-08 19:58:47 +0000119static char *previous; /* the previous regexp, used when null regexp is given */
120
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000121#if defined BB_SED
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122static char *previous1; /* a copy of the text from the previous substitution for regsub() */
Eric Andersenabc0f4f1999-12-08 23:19:36 +0000123#endif
Eric Andersenaa0765e1999-10-22 04:30:20 +0000124
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 Andersene49d5ec2000-02-08 19:58:47 +0000139#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 Andersenaa0765e1999-10-22 04:30:20 +0000147#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 Andersene49d5ec2000-02-08 19:58:47 +0000152static int class_cnt; /* used to assign class IDs */
153static int start_cnt; /* used to assign start IDs */
154static int end_stk[NSUBEXP]; /* used to assign end IDs */
155static int end_sp;
156static char *retext; /* points to the text being compiled */
Eric Andersenaa0765e1999-10-22 04:30:20 +0000157
158/* error-handling stuff */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159jmp_buf errorhandler;
160
Eric Andersenb6a44b81999-11-13 04:47:09 +0000161#define FAIL(why) do {fprintf(stderr, why); longjmp(errorhandler, 1);} while (0)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000162
163
164
165
166/* This function builds a bitmap for a particular class */
167/* text -- start of the class */
168/* bmap -- the bitmap */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169static char *makeclass(char *text, char *bmap)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000170{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000171 int i;
172 int complement = 0;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000173
174
175 /* zero the bitmap */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000176 for (i = 0; bmap && i < 32; i++) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000177 bmap[i] = 0;
178 }
179
180 /* see if we're going to complement this class */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 if (*text == '^') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000182 text++;
183 complement = 1;
184 }
185
186 /* add in the characters */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 while (*text && *text != ']') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000188 /* is this a span of characters? */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 if (text[1] == '-' && text[2]) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000190 /* spans can't be backwards */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 if (text[0] > text[2]) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000192 FAIL("Backwards span in []");
193 }
194
195 /* add each character in the span to the bitmap */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 for (i = text[0]; bmap && i <= text[2]; i++) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000197 bmap[i >> 3] |= (1 << (i & 7));
198 }
199
200 /* move past this span */
201 text += 3;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000203 /* add this single character to the span */
204 i = *text++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 if (bmap) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000206 bmap[i >> 3] |= (1 << (i & 7));
207 }
208 }
209 }
210
211 /* make sure the closing ] is missing */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 if (*text++ != ']') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000213 FAIL("] missing");
214 }
215
216 /* if we're supposed to complement this class, then do so */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 if (complement && bmap) {
218 for (i = 0; i < 32; i++) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000219 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 */
234static int gettoken(sptr, re)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235char **sptr;
236regexp *re;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000237{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 int c;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000239
240 c = **sptr;
241 ++*sptr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000242 if (c == '\\') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000243 c = **sptr;
244 ++*sptr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245 switch (c) {
246 case '<':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000247 return M_BEGWORD;
248
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 case '>':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000250 return M_ENDWORD;
251
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 case '(':
253 if (start_cnt >= NSUBEXP) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000254 FAIL("Too many \\(s");
255 }
256 end_stk[end_sp++] = start_cnt;
257 return M_START(start_cnt++);
258
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259 case ')':
260 if (end_sp <= 0) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000261 FAIL("Mismatched \\)");
262 }
263 return M_END(end_stk[--end_sp]);
264
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 case '*':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000266 return M_SPLAT;
267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 case '.':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000269 return M_ANY;
270
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 case '+':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000272 return M_PLUS;
273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 case '?':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000275 return M_QMARK;
276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 default:
Eric Andersenaa0765e1999-10-22 04:30:20 +0000278 return c;
279 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000280 } else {
281 switch (c) {
282 case '^':
283 if (*sptr == retext + 1) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000284 return M_BEGLINE;
285 }
286 return c;
287
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 case '$':
289 if (!**sptr) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000290 return M_ENDLINE;
291 }
292 return c;
293
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 case '.':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000295 return M_ANY;
296
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 case '*':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000298 return M_SPLAT;
299
Erik Andersene49d5ec2000-02-08 19:58:47 +0000300 case '[':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000301 /* make sure we don't have too many classes */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 if (class_cnt >= 10) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000303 FAIL("Too many []s");
304 }
305
306 /* process the character list for this class */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 if (re) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000308 /* generate the bitmap for this class */
309 *sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000310 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000311 /* skip to end of the class */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 *sptr = makeclass(*sptr, (char *) 0);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000313 }
314 return M_CLASS(class_cnt++);
315
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 default:
Eric Andersenaa0765e1999-10-22 04:30:20 +0000317 return c;
318 }
319 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 /*NOTREACHED*/}
Eric Andersenaa0765e1999-10-22 04:30:20 +0000321
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 */
329static unsigned calcsize(text)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330char *text;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000331{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 unsigned size;
333 int token;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000334
335 retext = text;
336 class_cnt = 0;
337 start_cnt = 1;
338 end_sp = 0;
339 size = 5;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 while ((token = gettoken(&text, (regexp *) 0)) != 0) {
341 if (IS_CLASS(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000342 size += 34;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343 } else if (IS_META(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000344 size += 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000345 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000346 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 Andersene49d5ec2000-02-08 19:58:47 +0000362static int match1(regexp * re, char ch, int token, int ignoreCase)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000363{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000364 if (!ch) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000365 /* the end of a line can't match any RE of width 1 */
366 return 1;
367 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 if (token == M_ANY) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000369 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000370 } else if (IS_CLASS(token)) {
Mark Whitleyd1666582000-05-01 22:08:54 +0000371 if (re->program[1 + 32 * (token - M_CLASS(0)) + (ch >> 3)] & (1 << (ch & 7)))
Eric Andersenaa0765e1999-10-22 04:30:20 +0000372 return 0;
373 }
Erik Andersenf4a99cc2000-01-05 05:49:51 +0000374//fprintf(stderr, "match1: ch='%c' token='%c': ", ch, token);
Mark Whitleyd1666582000-05-01 22:08:54 +0000375 if (ch == token || (ignoreCase == TRUE && tolower(ch) == tolower(token))) {
Erik Andersenf4a99cc2000-01-05 05:49:51 +0000376//fprintf(stderr, "match\n");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000377 return 0;
378 }
Erik Andersenf4a99cc2000-01-05 05:49:51 +0000379//fprintf(stderr, "no match\n");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000380 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 Andersene49d5ec2000-02-08 19:58:47 +0000393static int match(regexp * re, char *str, char *prog, char *here,
394 int ignoreCase)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000395{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000396 int token;
397 int nmatched;
398 int closure;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000399
Erik Andersene49d5ec2000-02-08 19:58:47 +0000400 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 Andersenaa0765e1999-10-22 04:30:20 +0000405 if (*here)
406 return 1;
407 break;
408
Erik Andersene49d5ec2000-02-08 19:58:47 +0000409 case M_BEGWORD:
Eric Andersenaa0765e1999-10-22 04:30:20 +0000410 if (here != str &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 (here[-1] == '_' ||
412 (isascii(here[-1]) && isalnum(here[-1])))) return 1;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000413 break;
414
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415 case M_ENDWORD:
Eric Andersenaa0765e1999-10-22 04:30:20 +0000416 if ((here[0] == '_' || isascii(here[0])) && isalnum(here[0]))
417 return 1;
418 break;
419
Erik Andersene49d5ec2000-02-08 19:58:47 +0000420 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 Andersenaa0765e1999-10-22 04:30:20 +0000431 break;
432
Erik Andersene49d5ec2000-02-08 19:58:47 +0000433 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 Andersenaa0765e1999-10-22 04:30:20 +0000445 return 0;
446 }
447 break;
448
Erik Andersene49d5ec2000-02-08 19:58:47 +0000449 default: /* literal, M_CLASS(n), or M_ANY */
Eric Andersenaa0765e1999-10-22 04:30:20 +0000450 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 Andersene49d5ec2000-02-08 19:58:47 +0000467 (closure != M_QMARK || nmatched < 1) && *here
468 && match1(re, *here, token, ignoreCase) == 0; nmatched++, here++) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000469 }
470
471 /* step 3: try to match the remainder, and back off if it doesn't */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472 while (nmatched >= 0 && match(re, str, prog, here, ignoreCase) != 0) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000473 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 Whitley268b8c42000-06-28 21:59:31 +0000485static regexp *regcomp(char *text)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000486{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 int needfirst;
488 unsigned size;
489 int token;
490 int peek;
491 char *build;
Erik Andersen330fd2b2000-05-19 05:35:19 +0000492 regexp *re;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000493
494
495 /* prepare for error handling */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 re = (regexp *) 0;
497 if (setjmp(errorhandler)) {
498 if (re) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000499 free(re);
500 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000501 return (regexp *) 0;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000502 }
503
504 /* if an empty regexp string was given, use the previous one */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 if (*text == 0) {
506 if (!previous) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000507 FAIL("No previous RE");
508 }
509 text = previous;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 } else { /* non-empty regexp given, so remember it */
511
Eric Andersenaa0765e1999-10-22 04:30:20 +0000512 if (previous)
513 free(previous);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000514 previous = (char *) malloc((unsigned) (strlen(text) + 1));
Eric Andersenaa0765e1999-10-22 04:30:20 +0000515 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 Andersene49d5ec2000-02-08 19:58:47 +0000525 re = (regexp *) malloc((unsigned) size);
Eric Andersenaa0765e1999-10-22 04:30:20 +0000526
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527 if (!re) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000528 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 Andersene49d5ec2000-02-08 19:58:47 +0000534 for (token = 0; token < NSUBEXP; token++) {
535 re->startp[token] = re->endp[token] = (char *) 0;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000536 }
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 Andersene49d5ec2000-02-08 19:58:47 +0000546 token; token = peek, peek = gettoken(&text, re)) {
Mark Whitleyd1666582000-05-01 22:08:54 +0000547
Eric Andersenaa0765e1999-10-22 04:30:20 +0000548 /* special processing for the closure operator */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 if (IS_CLOSURE(peek)) {
Mark Whitleyd1666582000-05-01 22:08:54 +0000550
Eric Andersenaa0765e1999-10-22 04:30:20 +0000551 /* detect misuse of closure operator */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000552 if (IS_START(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000553 FAIL("* or \\+ or \\? follows nothing");
Mark Whitleyd1666582000-05-01 22:08:54 +0000554 } else if (IS_META(token) && token != M_ANY && !IS_CLASS(token)) {
555 FAIL("* or \\+ or \\? can only follow a normal character or . or []");
Eric Andersenaa0765e1999-10-22 04:30:20 +0000556 }
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 Andersene49d5ec2000-02-08 19:58:47 +0000562 if (needfirst && peek == M_PLUS && !IS_META(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000563 re->first = token;
564 }
565 needfirst = 0;
566
567 /* we used "peek" -- need to refill it */
568 peek = gettoken(&text, re);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000569 if (IS_CLOSURE(peek)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000570 FAIL("* or \\+ or \\? doubled up");
571 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000572 } else if (!IS_META(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000573 /* normal char is NOT argument of closure */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000574 if (needfirst) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000575 re->first = token;
576 needfirst = 0;
577 }
578 re->minlen++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000579 } else if (token == M_ANY || IS_CLASS(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000580 /* . 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 Andersene49d5ec2000-02-08 19:58:47 +0000586 if (token == M_BEGLINE) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000587 /* set the BOL flag instead of storing M_BEGLINE */
588 re->bol = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000589 } else if (IS_META(token)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000590 ADD_META(build, token);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000591 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000592 *build++ = token;
593 }
594 }
595
596 /* end it with a \) which MUST MATCH the opening \( */
597 ADD_META(build, M_END(0));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000598 if (end_sp > 0) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000599 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 Whitley268b8c42000-06-28 21:59:31 +0000613static int regexec(struct regexp *re, char *str, int bol, int ignoreCase)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000614{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000615 char *prog; /* the entry point of re->program */
616 int len; /* length of the string */
617 char *here;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000618
619 /* if must start at the beginning of a line, and this isn't, then fail */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000620 if (re->bol && bol == TRUE) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000621 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 Andersene49d5ec2000-02-08 19:58:47 +0000628 if (re->bol) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000629 /* must occur at BOL */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000630 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 Andersenaa0765e1999-10-22 04:30:20 +0000635 /* can occur anywhere in the line, noignorecase */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636 for (here = (char *) str; (re->first && re->first != *here)
637 || match(re, (char *) str, prog, here, ignoreCase);
638 here++, len--) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000639 if (len < re->minlen)
640 return FALSE;
641 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000642 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000643 /* can occur anywhere in the line, ignorecase */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000644 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 Andersenaa0765e1999-10-22 04:30:20 +0000648 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 Andersen7f1acfd1999-10-29 23:09:13 +0000660#if defined BB_SED
Eric Andersenaa0765e1999-10-22 04:30:20 +0000661/* This performs substitutions after a regexp match has been found. */
Mark Whitley268b8c42000-06-28 21:59:31 +0000662static void regsub(regexp * re, char *src, char *dst)
Eric Andersenaa0765e1999-10-22 04:30:20 +0000663{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000664 char *cpy;
665 char *end;
666 char c;
667 char *start;
668 int mod;
Eric Andersenaa0765e1999-10-22 04:30:20 +0000669
670 mod = 0;
671
672 start = src;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000673 while ((c = *src++) != '\0') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000674 /* recognize any meta characters */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000675 if (c == '&') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000676 cpy = re->startp[0];
677 end = re->endp[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000678 } else if (c == '~') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000679 cpy = previous1;
680 if (cpy)
681 end = cpy + strlen(cpy);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000682 } else if (c == '\\') {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000683 c = *src++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000684 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 Andersenaa0765e1999-10-22 04:30:20 +0000695 /* \0 thru \9 mean "copy subexpression" */
696 c -= '0';
Erik Andersene49d5ec2000-02-08 19:58:47 +0000697 cpy = re->startp[(int) c];
698 end = re->endp[(int) c];
Eric Andersenaa0765e1999-10-22 04:30:20 +0000699 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000700 case 'U':
701 case 'u':
702 case 'L':
703 case 'l':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000704 /* \U and \L mean "convert to upper/lowercase" */
705 mod = c;
706 continue;
707
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 case 'E':
709 case 'e':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000710 /* \E ends the \U or \L */
711 mod = 0;
712 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000713 case '&':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000714 /* "\&" means "original text" */
715 *dst++ = c;
716 continue;
717
Erik Andersene49d5ec2000-02-08 19:58:47 +0000718 case '~':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000719 /* "\~" means "previous text, if any" */
720 *dst++ = c;
721 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000722 default:
Eric Andersenaa0765e1999-10-22 04:30:20 +0000723 /* ordinary char preceded by backslash */
724 *dst++ = c;
725 continue;
726 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000727 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000728 /* 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 Andersene49d5ec2000-02-08 19:58:47 +0000743 while (cpy < end) {
744 switch (mod) {
745 case 'U':
746 case 'u':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000747 /* convert to uppercase */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000748 if (isascii(*cpy) && islower(*cpy)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000749 *dst++ = toupper(*cpy);
750 cpy++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000751 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000752 *dst++ = *cpy++;
753 }
754 break;
755
Erik Andersene49d5ec2000-02-08 19:58:47 +0000756 case 'L':
757 case 'l':
Eric Andersenaa0765e1999-10-22 04:30:20 +0000758 /* convert to lowercase */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000759 if (isascii(*cpy) && isupper(*cpy)) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000760 *dst++ = tolower(*cpy);
761 cpy++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 } else {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000763 *dst++ = *cpy++;
764 }
765 break;
766
Erik Andersene49d5ec2000-02-08 19:58:47 +0000767 default:
Eric Andersenaa0765e1999-10-22 04:30:20 +0000768 /* copy without any conversion */
769 *dst++ = *cpy++;
770 }
771
772 /* \u and \l end automatically after the first char */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000773 if (mod && (mod == 'u' || mod == 'l')) {
Eric Andersenaa0765e1999-10-22 04:30:20 +0000774 mod = 0;
775 }
776 }
777 }
778 *dst = '\0';
779
780 /* remember what text we inserted this time */
781 if (previous1)
782 free(previous1);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783 previous1 = (char *) malloc((unsigned) (strlen(start) + 1));
Eric Andersenaa0765e1999-10-22 04:30:20 +0000784 if (previous1)
785 strcpy(previous1, start);
786}
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000787#endif
Eric Andersenaa0765e1999-10-22 04:30:20 +0000788
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789#endif /* BB_REGEXP */