Removed unnecessary #include "regexp.h" line from find.c as per Matt Kraai's
[oweals/busybox.git] / regexp.c
1 /* vi: set sw=4 ts=4: */
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
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
26 #if ( defined BB_GREP || defined BB_SED)
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 {
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);
43 }
44
45 #if defined BB_SED
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. */
48 extern int replace_match(char *haystack, char *needle, char *newNeedle,
49                                                  int ignoreCase)
50 {
51         int status;
52         struct regexp *re;
53         char *s, buf[BUF_SIZE], *d = buf;
54
55         re = regcomp(needle);
56         status = regexec(re, haystack, FALSE, ignoreCase);
57         if (status == TRUE) {
58                 s = haystack;
59
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);
77 }
78 #endif
79
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
119 static char *previous;                  /* the previous regexp, used when null regexp is given */
120
121 #if defined BB_SED
122 static char *previous1;                 /* a copy of the text from the previous substitution for regsub() */
123 #endif
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 */
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 \? */
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 */
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 */
157
158 /* error-handling stuff */
159 jmp_buf errorhandler;
160
161 #define FAIL(why)  do {fprintf(stderr, why); longjmp(errorhandler, 1);} while (0)
162
163
164
165
166 /* This function builds a bitmap for a particular class */
167 /* text -- start of the class */
168 /* bmap -- the bitmap */
169 static char *makeclass(char *text, char *bmap)
170 {
171         int i;
172         int complement = 0;
173
174
175         /* zero the bitmap */
176         for (i = 0; bmap && i < 32; i++) {
177                 bmap[i] = 0;
178         }
179
180         /* see if we're going to complement this class */
181         if (*text == '^') {
182                 text++;
183                 complement = 1;
184         }
185
186         /* add in the characters */
187         while (*text && *text != ']') {
188                 /* is this a span of characters? */
189                 if (text[1] == '-' && text[2]) {
190                         /* spans can't be backwards */
191                         if (text[0] > text[2]) {
192                                 FAIL("Backwards span in []");
193                         }
194
195                         /* add each character in the span to the bitmap */
196                         for (i = text[0]; bmap && i <= text[2]; i++) {
197                                 bmap[i >> 3] |= (1 << (i & 7));
198                         }
199
200                         /* move past this span */
201                         text += 3;
202                 } else {
203                         /* add this single character to the span */
204                         i = *text++;
205                         if (bmap) {
206                                 bmap[i >> 3] |= (1 << (i & 7));
207                         }
208                 }
209         }
210
211         /* make sure the closing ] is missing */
212         if (*text++ != ']') {
213                 FAIL("] missing");
214         }
215
216         /* if we're supposed to complement this class, then do so */
217         if (complement && bmap) {
218                 for (i = 0; i < 32; i++) {
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)
235 char **sptr;
236 regexp *re;
237 {
238         int c;
239
240         c = **sptr;
241         ++*sptr;
242         if (c == '\\') {
243                 c = **sptr;
244                 ++*sptr;
245                 switch (c) {
246                 case '<':
247                         return M_BEGWORD;
248
249                 case '>':
250                         return M_ENDWORD;
251
252                 case '(':
253                         if (start_cnt >= NSUBEXP) {
254                                 FAIL("Too many \\(s");
255                         }
256                         end_stk[end_sp++] = start_cnt;
257                         return M_START(start_cnt++);
258
259                 case ')':
260                         if (end_sp <= 0) {
261                                 FAIL("Mismatched \\)");
262                         }
263                         return M_END(end_stk[--end_sp]);
264
265                 case '*':
266                         return M_SPLAT;
267
268                 case '.':
269                         return M_ANY;
270
271                 case '+':
272                         return M_PLUS;
273
274                 case '?':
275                         return M_QMARK;
276
277                 default:
278                         return c;
279                 }
280         } else {
281                 switch (c) {
282                 case '^':
283                         if (*sptr == retext + 1) {
284                                 return M_BEGLINE;
285                         }
286                         return c;
287
288                 case '$':
289                         if (!**sptr) {
290                                 return M_ENDLINE;
291                         }
292                         return c;
293
294                 case '.':
295                         return M_ANY;
296
297                 case '*':
298                         return M_SPLAT;
299
300                 case '[':
301                         /* make sure we don't have too many classes */
302                         if (class_cnt >= 10) {
303                                 FAIL("Too many []s");
304                         }
305
306                         /* process the character list for this class */
307                         if (re) {
308                                 /* generate the bitmap for this class */
309                                 *sptr = makeclass(*sptr, re->program + 1 + 32 * class_cnt);
310                         } else {
311                                 /* skip to end of the class */
312                                 *sptr = makeclass(*sptr, (char *) 0);
313                         }
314                         return M_CLASS(class_cnt++);
315
316                 default:
317                         return c;
318                 }
319         }
320  /*NOTREACHED*/}
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)
330 char *text;
331 {
332         unsigned size;
333         int token;
334
335         retext = text;
336         class_cnt = 0;
337         start_cnt = 1;
338         end_sp = 0;
339         size = 5;
340         while ((token = gettoken(&text, (regexp *) 0)) != 0) {
341                 if (IS_CLASS(token)) {
342                         size += 34;
343                 } else if (IS_META(token)) {
344                         size += 2;
345                 } else {
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  */
362 static int match1(regexp * re, char ch, int token, int ignoreCase)
363 {
364         if (!ch) {
365                 /* the end of a line can't match any RE of width 1 */
366                 return 1;
367         }
368         if (token == M_ANY) {
369                 return 0;
370         } else if (IS_CLASS(token)) {
371                 if (re->program[1 + 32 * (token - M_CLASS(0)) + (ch >> 3)] & (1 << (ch & 7)))
372                         return 0;
373         }
374 //fprintf(stderr, "match1: ch='%c' token='%c': ", ch, token);
375         if (ch == token || (ignoreCase == TRUE && tolower(ch) == tolower(token))) {
376 //fprintf(stderr, "match\n");
377                 return 0;
378         }
379 //fprintf(stderr, "no match\n");
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 */
393 static int match(regexp * re, char *str, char *prog, char *here,
394                                  int ignoreCase)
395 {
396         int token;
397         int nmatched;
398         int closure;
399
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:
405                         if (*here)
406                                 return 1;
407                         break;
408
409                 case M_BEGWORD:
410                         if (here != str &&
411                                 (here[-1] == '_' ||
412                                  (isascii(here[-1]) && isalnum(here[-1])))) return 1;
413                         break;
414
415                 case M_ENDWORD:
416                         if ((here[0] == '_' || isascii(here[0])) && isalnum(here[0]))
417                                 return 1;
418                         break;
419
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;
431                         break;
432
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)) {
445                                 return 0;
446                         }
447                         break;
448
449                 default:                                /* literal, M_CLASS(n), or M_ANY */
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;
467                  (closure != M_QMARK || nmatched < 1) && *here
468                  && match1(re, *here, token, ignoreCase) == 0; nmatched++, here++) {
469         }
470
471         /* step 3: try to match the remainder, and back off if it doesn't */
472         while (nmatched >= 0 && match(re, str, prog, here, ignoreCase) != 0) {
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. */
485 static regexp *regcomp(char *text)
486 {
487         int needfirst;
488         unsigned size;
489         int token;
490         int peek;
491         char *build;
492         regexp *re;
493
494
495         /* prepare for error handling */
496         re = (regexp *) 0;
497         if (setjmp(errorhandler)) {
498                 if (re) {
499                         free(re);
500                 }
501                 return (regexp *) 0;
502         }
503
504         /* if an empty regexp string was given, use the previous one */
505         if (*text == 0) {
506                 if (!previous) {
507                         FAIL("No previous RE");
508                 }
509                 text = previous;
510         } else {                                        /* non-empty regexp given, so remember it */
511
512                 if (previous)
513                         free(previous);
514                 previous = (char *) malloc((unsigned) (strlen(text) + 1));
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);
525         re = (regexp *) malloc((unsigned) size);
526
527         if (!re) {
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;
534         for (token = 0; token < NSUBEXP; token++) {
535                 re->startp[token] = re->endp[token] = (char *) 0;
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);
546                  token; token = peek, peek = gettoken(&text, re)) {
547
548                 /* special processing for the closure operator */
549                 if (IS_CLOSURE(peek)) {
550
551                         /* detect misuse of closure operator */
552                         if (IS_START(token)) {
553                                 FAIL("* or \\+ or \\? follows nothing");
554                         } else if (IS_META(token) && token != M_ANY && !IS_CLASS(token)) {
555                                 FAIL("* or \\+ or \\? can only follow a normal character or . or []");
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? */
562                         if (needfirst && peek == M_PLUS && !IS_META(token)) {
563                                 re->first = token;
564                         }
565                         needfirst = 0;
566
567                         /* we used "peek" -- need to refill it */
568                         peek = gettoken(&text, re);
569                         if (IS_CLOSURE(peek)) {
570                                 FAIL("* or \\+ or \\? doubled up");
571                         }
572                 } else if (!IS_META(token)) {
573                         /* normal char is NOT argument of closure */
574                         if (needfirst) {
575                                 re->first = token;
576                                 needfirst = 0;
577                         }
578                         re->minlen++;
579                 } else if (token == M_ANY || IS_CLASS(token)) {
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 */
586                 if (token == M_BEGLINE) {
587                         /* set the BOL flag instead of storing M_BEGLINE */
588                         re->bol = 1;
589                 } else if (IS_META(token)) {
590                         ADD_META(build, token);
591                 } else {
592                         *build++ = token;
593                 }
594         }
595
596         /* end it with a \) which MUST MATCH the opening \( */
597         ADD_META(build, M_END(0));
598         if (end_sp > 0) {
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 */
613 static int regexec(struct regexp *re, char *str, int bol, int ignoreCase)
614 {
615         char *prog;                                     /* the entry point of re->program */
616         int len;                                        /* length of the string */
617         char *here;
618
619         /* if must start at the beginning of a line, and this isn't, then fail */
620         if (re->bol && bol == TRUE) {
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 */
628         if (re->bol) {
629                 /* must occur at BOL */
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) {
635                 /* can occur anywhere in the line, noignorecase */
636                 for (here = (char *) str; (re->first && re->first != *here)
637                          || match(re, (char *) str, prog, here, ignoreCase);
638                          here++, len--) {
639                         if (len < re->minlen)
640                                 return FALSE;
641                 }
642         } else {
643                 /* can occur anywhere in the line, ignorecase */
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--) {
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
660 #if defined BB_SED
661 /* This performs substitutions after a regexp match has been found.  */
662 static void regsub(regexp * re, char *src, char *dst)
663 {
664         char *cpy;
665         char *end;
666         char c;
667         char *start;
668         int mod;
669
670         mod = 0;
671
672         start = src;
673         while ((c = *src++) != '\0') {
674                 /* recognize any meta characters */
675                 if (c == '&') {
676                         cpy = re->startp[0];
677                         end = re->endp[0];
678                 } else if (c == '~') {
679                         cpy = previous1;
680                         if (cpy)
681                                 end = cpy + strlen(cpy);
682                 } else if (c == '\\') {
683                         c = *src++;
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':
695                                 /* \0 thru \9 mean "copy subexpression" */
696                                 c -= '0';
697                                 cpy = re->startp[(int) c];
698                                 end = re->endp[(int) c];
699                                 break;
700                         case 'U':
701                         case 'u':
702                         case 'L':
703                         case 'l':
704                                 /* \U and \L mean "convert to upper/lowercase" */
705                                 mod = c;
706                                 continue;
707
708                         case 'E':
709                         case 'e':
710                                 /* \E ends the \U or \L */
711                                 mod = 0;
712                                 continue;
713                         case '&':
714                                 /* "\&" means "original text" */
715                                 *dst++ = c;
716                                 continue;
717
718                         case '~':
719                                 /* "\~" means "previous text, if any" */
720                                 *dst++ = c;
721                                 continue;
722                         default:
723                                 /* ordinary char preceded by backslash */
724                                 *dst++ = c;
725                                 continue;
726                         }
727                 } else {
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 */
743                 while (cpy < end) {
744                         switch (mod) {
745                         case 'U':
746                         case 'u':
747                                 /* convert to uppercase */
748                                 if (isascii(*cpy) && islower(*cpy)) {
749                                         *dst++ = toupper(*cpy);
750                                         cpy++;
751                                 } else {
752                                         *dst++ = *cpy++;
753                                 }
754                                 break;
755
756                         case 'L':
757                         case 'l':
758                                 /* convert to lowercase */
759                                 if (isascii(*cpy) && isupper(*cpy)) {
760                                         *dst++ = tolower(*cpy);
761                                         cpy++;
762                                 } else {
763                                         *dst++ = *cpy++;
764                                 }
765                                 break;
766
767                         default:
768                                 /* copy without any conversion */
769                                 *dst++ = *cpy++;
770                         }
771
772                         /* \u and \l end automatically after the first char */
773                         if (mod && (mod == 'u' || mod == 'l')) {
774                                 mod = 0;
775                         }
776                 }
777         }
778         *dst = '\0';
779
780         /* remember what text we inserted this time */
781         if (previous1)
782                 free(previous1);
783         previous1 = (char *) malloc((unsigned) (strlen(start) + 1));
784         if (previous1)
785                 strcpy(previous1, start);
786 }
787 #endif
788
789 #endif                                                  /* BB_REGEXP */