1 static const char CVSID
[] = "$Id: regexConvert.c,v 1.10 2004/07/21 11:32:05 yooden Exp $";
2 /*------------------------------------------------------------------------*
3 * `CompileRE', `ExecRE', and `ConvertSubstituteRE' -- regular expression parsing
5 * This is a HIGHLY ALTERED VERSION of Henry Spencer's `regcomp'
6 * code adapted for NEdit.
8 * .-------------------------------------------------------------------.
9 * | ORIGINAL COPYRIGHT NOTICE: |
11 * | Copyright (c) 1986 by University of Toronto. |
12 * | Written by Henry Spencer. Not derived from licensed software. |
14 * | Permission is granted to anyone to use this software for any |
15 * | purpose on any computer system, and to redistribute it freely, |
16 * | subject to the following restrictions: |
18 * | 1. The author is not responsible for the consequences of use of |
19 * | this software, no matter how awful, even if they arise |
20 * | from defects in it. |
22 * | 2. The origin of this software must not be misrepresented, either |
23 * | by explicit claim or by omission. |
25 * | 3. Altered versions must be plainly marked as such, and must not |
26 * | be misrepresented as being the original software. |
27 * `-------------------------------------------------------------------'
29 * This is free software; you can redistribute it and/or modify it under the
30 * terms of the GNU General Public License as published by the Free Software
31 * Foundation; either version 2 of the License, or (at your option) any later
32 * version. In addition, you may distribute version of this program linked to
33 * Motif or Open Motif. See README for details.
35 * This software is distributed in the hope that it will be useful, but WITHOUT
36 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
37 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
40 * You should have received a copy of the GNU General Public License along with
41 * software; if not, write to the Free Software Foundation, Inc., 59 Temple
42 * Place, Suite 330, Boston, MA 02111-1307 USA
47 #include "../config.h"
50 #include "regexConvert.h"
58 #include <X11/Intrinsic.h>
65 /* Utility definitions. */
69 #define CONVERT_FAIL(m) {*Error_Ptr = (m); return 0;}
70 #define IS_QUANTIFIER(c) ((c) == '*' || (c) == '+' || (c) == '?')
71 #define U_CHAR_AT(p) ((unsigned int) *(unsigned char *)(p))
73 /* Flags to be passed up and down via function parameters during compile. */
75 #define WORST 0 /* Worst case. No assumptions can be made.*/
76 #define HAS_WIDTH 1 /* Known never to match null string. */
77 #define SIMPLE 2 /* Simple enough to be STAR/PLUS operand. */
79 #define NO_PAREN 0 /* Only set by initial call to "chunk". */
80 #define PAREN 1 /* Used for normal capturing parentheses. */
85 /* Global work variables for `ConvertRE'. */
87 static unsigned char *Reg_Parse
; /* Input scan ptr (scans user's regex) */
88 static int Total_Paren
; /* Parentheses, (), counter. */
89 static unsigned long Convert_Size
; /* Address of this used as flag. */
90 static unsigned char *Code_Emit_Ptr
; /* When Code_Emit_Ptr is set to
91 &Compute_Size no code is emitted.
92 Instead, the size of code that WOULD
93 have been generated is accumulated in
94 Convert_Size. Otherwise,
95 Code_Emit_Ptr points to where compiled
96 regex code is to be written. */
97 static unsigned char Compute_Size
;
98 static char **Error_Ptr
; /* Place to store error messages so
99 they can be returned by `ConvertRE' */
100 static char Error_Text
[128];/* Sting to build error messages in. */
102 static unsigned char Meta_Char
[] = ".*+?[(|)^<>$";
104 static unsigned char *Convert_Str
;
106 /* Forward declarations for functions used by `ConvertRE'. */
108 static int alternative (int *flag_param
);
109 static int chunk (int paren
, int *flag_param
);
110 static void emit_convert_byte (unsigned char c
);
111 static unsigned char literal_escape (unsigned char c
, int);
112 static int atom (int *flag_param
);
113 static void reg_error (char *str
);
114 static int piece (int *flag_param
);
116 /*----------------------------------------------------------------------*
119 * Compiles a regular expression into the internal format used by
122 * Beware that the optimization and preparation code in here knows about
123 * some of the structure of the compiled regexp.
124 *----------------------------------------------------------------------*/
126 char * ConvertRE (const char *exp
, char **errorText
) {
128 int flags_local
, pass
;
130 /* Set up `errorText' to receive failure reports. */
132 Error_Ptr
= errorText
;
135 if (exp
== NULL
) CONVERT_FAIL ("NULL argument to `ConvertRE\'");
137 Code_Emit_Ptr
= &Compute_Size
;
140 /* We can't allocate space until we know how big the compiled form will be,
141 but we can't compile it (and thus know how big it is) until we've got a
142 place to put the code. So we cheat: we compile it twice, once with code
143 generation turned off and size counting turned on, and once "for real".
144 This also means that we don't allocate space until we are sure that the
145 thing really will compile successfully, and we never have to move the
146 code and thus invalidate pointers into it. (Note that it has to be in
147 one piece because free() must be able to free it all.) */
149 for (pass
= 1; pass
<= 2; pass
++) {
150 /*-------------------------------------------*
151 * FIRST PASS: Determine size and legality. *
152 * SECOND PASS: Emit converted code. *
153 *-------------------------------------------*/
155 Reg_Parse
= (unsigned char *) exp
;
158 if (chunk (NO_PAREN
, &flags_local
) == 0) return (NULL
); /* Something
160 emit_convert_byte ('\0');
163 /* Allocate memory. */
166 (unsigned char *) XtMalloc (sizeof (unsigned char) * Convert_Size
);
168 if (Convert_Str
== NULL
) {
169 CONVERT_FAIL ("out of memory in `ConvertRE\'");
172 Code_Emit_Ptr
= Convert_Str
;
176 return (char *) Convert_Str
;
179 /*----------------------------------------------------------------------*
182 * Process main body of regex or process a parenthesized "thing". *
184 * Caller must absorb opening parenthesis.
185 *----------------------------------------------------------------------*/
187 static int chunk (int paren
, int *flag_param
) {
189 register int this_branch
;
192 *flag_param
= HAS_WIDTH
; /* Tentatively. */
194 /* Make an OPEN node, if parenthesized. */
196 if (paren
== PAREN
) {
197 if (Total_Paren
>= NSUBEXP
) {
198 sprintf (Error_Text
, "number of ()'s > %d", (int) NSUBEXP
);
199 CONVERT_FAIL (Error_Text
);
205 /* Pick up the branches, linking them together. */
208 this_branch
= alternative (&flags_local
);
210 if (this_branch
== 0) return 0;
212 /* If any alternative could be zero width, consider the whole
213 parenthisized thing to be zero width. */
215 if (!(flags_local
& HAS_WIDTH
)) *flag_param
&= ~HAS_WIDTH
;
217 /* Are there more alternatives to process? */
219 if (*Reg_Parse
!= '|') break;
221 emit_convert_byte ('|');
226 /* Check for proper termination. */
228 if (paren
!= NO_PAREN
&& *Reg_Parse
!= ')') {
229 CONVERT_FAIL ("missing right parenthesis \')\'");
231 } else if (paren
!= NO_PAREN
) {
232 emit_convert_byte (')');
235 } else if (paren
== NO_PAREN
&& *Reg_Parse
!= '\0') {
236 if (*Reg_Parse
== ')') {
237 CONVERT_FAIL ("missing left parenthesis \'(\'");
239 CONVERT_FAIL ("junk on end"); /* "Can't happen" - NOTREACHED */
246 /*----------------------------------------------------------------------*
247 * alternative - Processes one alternative of an '|' operator.
248 *----------------------------------------------------------------------*/
250 static int alternative (int *flag_param
) {
255 *flag_param
= WORST
; /* Tentatively. */
257 /* Loop until we hit the start of the next alternative, the end of this set
258 of alternatives (end of parentheses), or the end of the regex. */
260 while (*Reg_Parse
!= '|' && *Reg_Parse
!= ')' && *Reg_Parse
!= '\0') {
261 ret_val
= piece (&flags_local
);
263 if (ret_val
== 0) return 0; /* Something went wrong. */
265 *flag_param
|= flags_local
& HAS_WIDTH
;
271 /*----------------------------------------------------------------------*
272 * piece - something followed by possible '*', '+', or '?'.
273 *----------------------------------------------------------------------*/
275 static int piece (int *flag_param
) {
277 register int ret_val
;
278 register unsigned char op_code
;
279 unsigned long min_val
= REG_ZERO
;
282 ret_val
= atom (&flags_local
);
284 if (ret_val
== 0) return 0; /* Something went wrong. */
286 op_code
= *Reg_Parse
;
288 if (!IS_QUANTIFIER (op_code
)) {
289 *flag_param
= flags_local
;
296 if (op_code
== '+') min_val
= REG_ONE
;
298 /* It is dangerous to apply certain quantifiers to a possibly zero width
301 if (!(flags_local
& HAS_WIDTH
) && min_val
> REG_ZERO
) {
302 sprintf (Error_Text
, "%c operand could be empty", op_code
);
304 CONVERT_FAIL (Error_Text
);
307 *flag_param
= (min_val
> REG_ZERO
) ? (WORST
| HAS_WIDTH
) : WORST
;
309 if ( !((op_code
== '*') || (op_code
== '+') || (op_code
== '?')) ) {
310 /* We get here if the IS_QUANTIFIER macro is not coordinated properly
311 with this function. */
313 CONVERT_FAIL ("internal error #2, `piece\'");
316 if (IS_QUANTIFIER (*Reg_Parse
)) {
317 sprintf (Error_Text
, "nested quantifiers, %c%c", op_code
, *Reg_Parse
);
319 CONVERT_FAIL (Error_Text
);
322 emit_convert_byte (op_code
);
327 /*----------------------------------------------------------------------*
328 * atom - Process one regex item at the lowest level
329 *----------------------------------------------------------------------*/
331 static int atom (int *flag_param
) {
336 *flag_param
= WORST
; /* Tentatively. */
338 switch (*Reg_Parse
++) {
340 emit_convert_byte ('^');
344 emit_convert_byte ('$');
348 emit_convert_byte ('<');
352 emit_convert_byte ('>');
356 emit_convert_byte ('.');
358 *flag_param
|= (HAS_WIDTH
| SIMPLE
); break;
361 emit_convert_byte ('(');
363 ret_val
= chunk (PAREN
, &flags_local
);
365 if (ret_val
== 0) return 0; /* Something went wrong. */
367 /* Add HAS_WIDTH flag if it was set by call to chunk. */
369 *flag_param
|= flags_local
& HAS_WIDTH
;
376 CONVERT_FAIL ("internal error #3, `atom\'"); /* Supposed to be */
377 /* caught earlier. */
381 sprintf (Error_Text
, "%c follows nothing", *(Reg_Parse
- 1));
382 CONVERT_FAIL (Error_Text
);
385 emit_convert_byte ('\\'); /* Quote braces. */
386 emit_convert_byte ('{');
392 register unsigned int last_value
;
393 unsigned char last_emit
= 0;
394 unsigned char buffer
[500];
401 int u_score_flag
= 0;
405 /* Handle characters that can only occur at the start of a class. */
407 if (*Reg_Parse
== '^') { /* Complement of range. */
413 if (*Reg_Parse
== ']' || *Reg_Parse
== '-') {
414 /* If '-' or ']' is the first character in a class,
415 it is a literal character in the class. */
417 last_emit
= *Reg_Parse
;
420 CONVERT_FAIL ("too much data in [] to convert.");
423 buffer
[head
++] = '\\'; /* Escape `]' and '-' for clarity. */
424 buffer
[head
++] = *Reg_Parse
;
429 /* Handle the rest of the class characters. */
431 while (*Reg_Parse
!= '\0' && *Reg_Parse
!= ']') {
432 if (*Reg_Parse
== '-') { /* Process a range, e.g [a-z]. */
435 if (*Reg_Parse
== ']' || *Reg_Parse
== '\0') {
436 /* If '-' is the last character in a class it is a literal
437 character. If `Reg_Parse' points to the end of the
438 regex string, an error will be generated later. */
443 CONVERT_FAIL ("too much data in [] to convert.");
446 buffer
[head
++] = '\\'; /* Escape '-' for clarity. */
447 buffer
[head
++] = '-';
450 if (*Reg_Parse
== '\\') {
451 /* Handle escaped characters within a class range. */
455 if ((test
= literal_escape (*Reg_Parse
, 0))) {
457 buffer
[head
++] = '-';
459 if (*Reg_Parse
!= '\"') {
460 emit_convert_byte ('\\');
463 buffer
[head
++] = *Reg_Parse
;
464 last_value
= (unsigned int) test
;
468 "\\%c is an invalid escape sequence(3)",
471 CONVERT_FAIL (Error_Text
);
474 last_value
= U_CHAR_AT (Reg_Parse
);
476 if (last_emit
== '0' && last_value
== '9') {
479 } else if (last_emit
== 'a' && last_value
== 'z') {
482 } else if (last_emit
== 'A' && last_value
== 'Z') {
486 buffer
[head
++] = '-';
488 if ((test
= literal_escape (*Reg_Parse
, 1))) {
489 /* Ordinary character matches an escape sequence;
490 convert it to the escape sequence. */
494 "too much data in [] to convert.");
497 buffer
[head
++] = '\\';
499 if (test
== '0') { /* Make octal escape. */
501 buffer
[head
++] = '0';
502 buffer
[head
++] = ('0' + (test
/ 64));
503 test
-= (test
/ 64) * 64;
504 buffer
[head
++] = ('0' + (test
/ 8));
505 test
-= (test
/ 8) * 8;
506 buffer
[head
++] = ('0' + test
);
508 buffer
[head
++] = test
;
511 buffer
[head
++] = last_value
;
516 if (last_emit
> last_value
) {
517 CONVERT_FAIL ("invalid [] range");
520 last_emit
= (unsigned char) last_value
;
524 } /* End class character range code. */
525 } else if (*Reg_Parse
== '\\') {
528 if ((test
= literal_escape (*Reg_Parse
, 0)) != '\0') {
532 CONVERT_FAIL ("too much data in [] to convert.");
535 if (*Reg_Parse
!= '\"') {
536 buffer
[head
++] = '\\';
539 buffer
[head
++] = *Reg_Parse
;
543 "\\%c is an invalid escape sequence(1)",
546 CONVERT_FAIL (Error_Text
);
551 /* End of class escaped sequence code */
553 last_emit
= *Reg_Parse
;
555 if (*Reg_Parse
== '_') {
556 u_score_flag
= 1; /* Emit later if we can't do `\w'. */
558 } else if ((test
= literal_escape (*Reg_Parse
, 1))) {
559 /* Ordinary character matches an escape sequence;
560 convert it to the escape sequence. */
563 CONVERT_FAIL ("too much data in [] to convert.");
566 buffer
[head
++] = '\\';
568 if (test
== '0') { /* Make octal escape. */
570 buffer
[head
++] = '0';
571 buffer
[head
++] = ('0' + (test
/ 64));
572 test
-= (test
/ 64) * 64;
573 buffer
[head
++] = ('0' + (test
/ 8));
574 test
-= (test
/ 8) * 8;
575 buffer
[head
++] = ('0' + test
);
578 CONVERT_FAIL ("too much data in [] to convert.");
581 buffer
[head
++] = test
;
585 CONVERT_FAIL ("too much data in [] to convert.");
588 buffer
[head
++] = *Reg_Parse
;
593 } /* End of while (*Reg_Parse != '\0' && *Reg_Parse != ']') */
595 if (*Reg_Parse
!= ']') CONVERT_FAIL ("missing right \']\'");
597 buffer
[head
] = '\0';
599 /* NOTE: it is impossible to specify an empty class. This is
600 because [] would be interpreted as "begin character class"
601 followed by a literal ']' character and no "end character class"
602 delimiter (']'). Because of this, it is always safe to assume
603 that a class HAS_WIDTH. */
605 Reg_Parse
++; *flag_param
|= HAS_WIDTH
| SIMPLE
;
608 if (( a_z_flag
&& A_Z_flag
&& zero_nine
&& u_score_flag
) ||
609 ( a_z_flag
&& A_Z_flag
&& !zero_nine
&& !u_score_flag
) ||
610 (!a_z_flag
&& !A_Z_flag
&& zero_nine
&& !u_score_flag
)) {
617 emit_convert_byte ('[');
618 if (negated
) emit_convert_byte ('^');
621 /* Output any shortcut escapes if we can. */
623 while (a_z_flag
|| A_Z_flag
|| zero_nine
|| u_score_flag
) {
624 if (a_z_flag
&& A_Z_flag
&& zero_nine
&& u_score_flag
) {
625 emit_convert_byte ('\\');
627 if (negated
&& !do_brackets
) {
628 emit_convert_byte ('W');
630 emit_convert_byte ('w');
633 a_z_flag
= A_Z_flag
= zero_nine
= u_score_flag
= 0;
634 } else if (a_z_flag
&& A_Z_flag
) {
635 emit_convert_byte ('\\');
637 if (negated
&& !do_brackets
) {
638 emit_convert_byte ('L');
640 emit_convert_byte ('l');
643 a_z_flag
= A_Z_flag
= 0;
644 } else if (zero_nine
) {
645 emit_convert_byte ('\\');
647 if (negated
&& !do_brackets
) {
648 emit_convert_byte ('D');
650 emit_convert_byte ('d');
654 } else if (a_z_flag
) {
655 emit_convert_byte ('a');
656 emit_convert_byte ('-');
657 emit_convert_byte ('z');
660 } else if (A_Z_flag
) {
661 emit_convert_byte ('A');
662 emit_convert_byte ('-');
663 emit_convert_byte ('Z');
666 } else if (u_score_flag
) {
667 emit_convert_byte ('_');
673 /* Output our buffered class characters. */
675 for (head
= 0; buffer
[head
] != '\0'; head
++) {
676 emit_convert_byte (buffer
[head
]);
680 emit_convert_byte (']');
684 break; /* End of character class code. */
686 /* Fall through to Default case to handle literal escapes. */
689 Reg_Parse
--; /* If we fell through from the above code, we are now
690 pointing at the back slash (\) character. */
692 unsigned char *parse_save
, *emit_save
;
693 int emit_diff
, len
= 0;
695 /* Loop until we find a meta character or end of regex string. */
697 for (; *Reg_Parse
!= '\0' &&
698 !strchr ((char *) Meta_Char
, (int) *Reg_Parse
);
701 /* Save where we are in case we have to back
702 this character out. */
704 parse_save
= Reg_Parse
;
705 emit_save
= Code_Emit_Ptr
;
707 if (*Reg_Parse
== '\\') {
708 if ((test
= literal_escape (*(Reg_Parse
+ 1), 0))) {
709 if (*(Reg_Parse
+ 1) != '\"') {
710 emit_convert_byte ('\\');
713 Reg_Parse
++; /* Point to escaped character */
714 emit_convert_byte (*Reg_Parse
);
718 "\\%c is an invalid escape sequence(2)",
721 CONVERT_FAIL (Error_Text
);
726 /* Ordinary character */
728 if ((test
= literal_escape (*Reg_Parse
, 1))) {
729 /* Ordinary character matches an escape sequence;
730 convert it to the escape sequence. */
732 emit_convert_byte ('\\');
736 emit_convert_byte ('0');
737 emit_convert_byte ('0' + (test
/ 64));
738 test
-= (test
/ 64) * 64;
739 emit_convert_byte ('0' + (test
/ 8));
740 test
-= (test
/ 8) * 8;
741 emit_convert_byte ('0' + test
);
743 emit_convert_byte (test
);
746 emit_convert_byte (*Reg_Parse
);
752 /* If next regex token is a quantifier (?, +. *, or {m,n}) and
753 our EXACTLY node so far is more than one character, leave the
754 last character to be made into an EXACTLY node one character
755 wide for the multiplier to act on. For example 'abcd* would
756 have an EXACTLY node with an 'abc' operand followed by a STAR
757 node followed by another EXACTLY node with a 'd' operand. */
759 if (IS_QUANTIFIER (*Reg_Parse
) && len
> 0) {
760 Reg_Parse
= parse_save
; /* Point to previous regex token. */
761 emit_diff
= (Code_Emit_Ptr
- emit_save
);
763 if (Code_Emit_Ptr
== &Compute_Size
) {
764 Convert_Size
-= emit_diff
;
765 } else { /* Write over previously emitted byte. */
766 Code_Emit_Ptr
= emit_save
;
773 if (len
<= 0) CONVERT_FAIL ("internal error #4, `atom\'");
775 *flag_param
|= HAS_WIDTH
;
777 if (len
== 1) *flag_param
|= SIMPLE
;
779 } /* END switch (*Reg_Parse++) */
784 /*----------------------------------------------------------------------*
787 * Emit (if appropriate) a byte of converted code.
788 *----------------------------------------------------------------------*/
790 static void emit_convert_byte (unsigned char c
) {
792 if (Code_Emit_Ptr
== &Compute_Size
) {
795 *Code_Emit_Ptr
++ = c
;
799 /*--------------------------------------------------------------------*
802 * Recognize escaped literal characters (prefixed with backslash),
803 * and translate them into the corresponding character.
805 * Returns the proper character value or NULL if not a valid literal
807 *--------------------------------------------------------------------*/
809 static unsigned char literal_escape (unsigned char c
, int action
) {
811 static unsigned char control_escape
[] = {
814 'f', 'n', 'r', 't', 'v', '\0'
817 static unsigned char control_actual
[] = {
819 #ifdef EBCDIC_CHARSET
820 0x27, /* Escape character in IBM's EBCDIC character set. */
822 0x1B, /* Escape character in ASCII character set. */
824 '\f', '\n', '\r', '\t', '\v', '\0'
827 static unsigned char valid_escape
[] = {
828 'a', 'b', 'f', 'n', 'r', 't', 'v', '(', ')', '[',
829 ']', '<', '>', '.', '\\', '|', '^', '$', '*', '+',
833 static unsigned char value
[] = {
834 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '(', ')', '[',
835 ']', '<', '>', '.', '\\', '|', '^', '$', '*', '+',
842 for (i
= 0; valid_escape
[i
] != '\0'; i
++) {
843 if (c
== valid_escape
[i
]) return value
[i
];
845 } else if (action
== 1) {
846 for (i
= 0; control_actual
[i
] != '\0'; i
++) {
847 if (c
== control_actual
[i
]) {
848 return control_escape
[i
];
855 /* Signal to generate an numeric (octal) escape. */
863 /*----------------------------------------------------------------------*
864 * ConvertSubstituteRE - Perform substitutions after a `regexp' match.
865 *----------------------------------------------------------------------*/
867 void ConvertSubstituteRE (
872 register unsigned char *src
;
873 register unsigned char *dst
;
874 register unsigned char c
;
875 register unsigned char test
;
877 if (source
== NULL
|| dest
== NULL
) {
878 reg_error ("NULL parm to `ConvertSubstituteRE\'");
883 src
= (unsigned char *) source
;
884 dst
= (unsigned char *) dest
;
886 while ((c
= *src
++) != '\0') {
889 /* Process any case altering tokens, i.e \u, \U, \l, \L. */
891 if (*src
== 'u' || *src
== 'U' || *src
== 'l' || *src
== 'L') {
907 } else if (c
== '\\') {
909 /* Convert `\0' to `&' */
913 } else if ('1' <= *src
&& *src
<= '9') {
917 } else if ((test
= literal_escape (*src
, 0)) != '\0') {
921 } else if (*src
== '\0') {
922 /* If '\' is the last character of the replacement string, it is
923 interpreted as a literal backslash. */
927 /* Old regex's allowed any escape sequence. Convert these to
928 unescaped characters that replace themselves; i.e. they don't
929 need to be escaped. */
934 /* Ordinary character. */
936 if (((char *) dst
- (char *) dest
) >= (max
- 1)) {
939 if ((test
= literal_escape (c
, 1))) {
940 /* Ordinary character matches an escape sequence;
941 convert it to the escape sequence. */
945 if (test
== '0') { /* Make octal escape. */
948 *dst
++ = ('0' + (test
/ 64));
949 test
-= (test
/ 64) * 64;
950 *dst
++ = ('0' + (test
/ 8));
951 test
-= (test
/ 8) * 8;
952 *dst
++ = ('0' + test
);
967 /*----------------------------------------------------------------------*
969 *----------------------------------------------------------------------*/
971 static void reg_error (char *str
) {
975 "NEdit: Internal error processing regular expression (%s)\n",