1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
|
/*------------------------------------------------------------------------*
* `CompileRE', `ExecRE', and `ConvertSubstituteRE' -- regular expression parsing
*
* This is a HIGHLY ALTERED VERSION of Henry Spencer's `regcomp'
* code adapted for NEdit.
*
* .-------------------------------------------------------------------.
* | ORIGINAL COPYRIGHT NOTICE: |
* | |
* | Copyright (c) 1986 by University of Toronto. |
* | Written by Henry Spencer. Not derived from licensed software. |
* | |
* | Permission is granted to anyone to use this software for any |
* | purpose on any computer system, and to redistribute it freely, |
* | subject to the following restrictions: |
* | |
* | 1. The author is not responsible for the consequences of use of |
* | this software, no matter how awful, even if they arise |
* | from defects in it. |
* | |
* | 2. The origin of this software must not be misrepresented, either |
* | by explicit claim or by omission. |
* | |
* | 3. Altered versions must be plainly marked as such, and must not |
* | be misrepresented as being the original software. |
* `-------------------------------------------------------------------'
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version. In addition, you may distribute version of this program linked to
* Motif or Open Motif. See README for details.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* software; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include "regexConvert.h"
#include "../util/nedit_malloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#include <X11/Intrinsic.h>
#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif
/* Utility definitions. */
#define NSUBEXP 50
#define CONVERT_FAIL(m) {*Error_Ptr = (m); return 0;}
#define IS_QUANTIFIER(c) ((c) == '*' || (c) == '+' || (c) == '?')
#define U_CHAR_AT(p) ((unsigned int) *(unsigned char *)(p))
/* Flags to be passed up and down via function parameters during compile. */
#define WORST 0 /* Worst case. No assumptions can be made.*/
#define HAS_WIDTH 1 /* Known never to match null string. */
#define SIMPLE 2 /* Simple enough to be STAR/PLUS operand. */
#define NO_PAREN 0 /* Only set by initial call to "chunk". */
#define PAREN 1 /* Used for normal capturing parentheses. */
#define REG_ZERO 0UL
#define REG_ONE 1UL
/* Global work variables for `ConvertRE'. */
static unsigned char *Reg_Parse; /* Input scan ptr (scans user's regex) */
static int Total_Paren; /* Parentheses, (), counter. */
static unsigned long Convert_Size; /* Address of this used as flag. */
static unsigned char *Code_Emit_Ptr; /* When Code_Emit_Ptr is set to
&Compute_Size no code is emitted.
Instead, the size of code that WOULD
have been generated is accumulated in
Convert_Size. Otherwise,
Code_Emit_Ptr points to where compiled
regex code is to be written. */
static unsigned char Compute_Size;
static char **Error_Ptr; /* Place to store error messages so
they can be returned by `ConvertRE' */
static char Error_Text [128];/* Sting to build error messages in. */
static unsigned char Meta_Char [] = ".*+?[(|)^<>$";
static unsigned char *Convert_Str;
/* Forward declarations for functions used by `ConvertRE'. */
static int alternative (int *flag_param);
static int chunk (int paren, int *flag_param);
static void emit_convert_byte (unsigned char c);
static unsigned char literal_escape (unsigned char c, int);
static int atom (int *flag_param);
static void reg_error (char *str);
static int piece (int *flag_param);
/*----------------------------------------------------------------------*
* ConvertRE
*
* Compiles a regular expression into the internal format used by
* `ExecRE'.
*
* Beware that the optimization and preparation code in here knows about
* some of the structure of the compiled regexp.
*----------------------------------------------------------------------*/
char * ConvertRE (const char *exp, char **errorText) {
int flags_local, pass;
/* Set up `errorText' to receive failure reports. */
Error_Ptr = errorText;
*Error_Ptr = "";
if (exp == NULL) CONVERT_FAIL ("NULL argument to `ConvertRE\'");
Code_Emit_Ptr = &Compute_Size;
Convert_Size = 0UL;
/* We can't allocate space until we know how big the compiled form will be,
but we can't compile it (and thus know how big it is) until we've got a
place to put the code. So we cheat: we compile it twice, once with code
generation turned off and size counting turned on, and once "for real".
This also means that we don't allocate space until we are sure that the
thing really will compile successfully, and we never have to move the
code and thus invalidate pointers into it. (Note that it has to be in
one piece because free() must be able to free it all.) */
for (pass = 1; pass <= 2; pass++) {
/*-------------------------------------------*
* FIRST PASS: Determine size and legality. *
* SECOND PASS: Emit converted code. *
*-------------------------------------------*/
Reg_Parse = (unsigned char *) exp;
Total_Paren = 1;
if (chunk (NO_PAREN, &flags_local) == 0) return (NULL); /* Something
went wrong */
emit_convert_byte ('\0');
if (pass == 1) {
/* Allocate memory. */
Convert_Str =
(unsigned char *) NEditMalloc(sizeof (unsigned char) * Convert_Size);
if (Convert_Str == NULL) {
CONVERT_FAIL ("out of memory in `ConvertRE\'");
}
Code_Emit_Ptr = Convert_Str;
}
}
return (char *) Convert_Str;
}
/*----------------------------------------------------------------------*
* chunk *
* *
* Process main body of regex or process a parenthesized "thing". *
* *
* Caller must absorb opening parenthesis.
*----------------------------------------------------------------------*/
static int chunk (int paren, int *flag_param) {
register int this_branch;
int flags_local;
*flag_param = HAS_WIDTH; /* Tentatively. */
/* Make an OPEN node, if parenthesized. */
if (paren == PAREN) {
if (Total_Paren >= NSUBEXP) {
sprintf (Error_Text, "number of ()'s > %d", (int) NSUBEXP);
CONVERT_FAIL (Error_Text);
}
Total_Paren++;
}
/* Pick up the branches, linking them together. */
do {
this_branch = alternative (&flags_local);
if (this_branch == 0) return 0;
/* If any alternative could be zero width, consider the whole
parenthisized thing to be zero width. */
if (!(flags_local & HAS_WIDTH)) *flag_param &= ~HAS_WIDTH;
/* Are there more alternatives to process? */
if (*Reg_Parse != '|') break;
emit_convert_byte ('|');
Reg_Parse++;
} while (1);
/* Check for proper termination. */
if (paren != NO_PAREN && *Reg_Parse != ')') {
CONVERT_FAIL ("missing right parenthesis \')\'");
} else if (paren != NO_PAREN) {
emit_convert_byte (')');
Reg_Parse++;
} else if (paren == NO_PAREN && *Reg_Parse != '\0') {
if (*Reg_Parse == ')') {
CONVERT_FAIL ("missing left parenthesis \'(\'");
} else {
CONVERT_FAIL ("junk on end"); /* "Can't happen" - NOTREACHED */
}
}
return 1;
}
/*----------------------------------------------------------------------*
* alternative - Processes one alternative of an '|' operator.
*----------------------------------------------------------------------*/
static int alternative (int *flag_param) {
int ret_val;
int flags_local;
*flag_param = WORST; /* Tentatively. */
/* Loop until we hit the start of the next alternative, the end of this set
of alternatives (end of parentheses), or the end of the regex. */
while (*Reg_Parse != '|' && *Reg_Parse != ')' && *Reg_Parse != '\0') {
ret_val = piece (&flags_local);
if (ret_val == 0) return 0; /* Something went wrong. */
*flag_param |= flags_local & HAS_WIDTH;
}
return 1;
}
/*----------------------------------------------------------------------*
* piece - something followed by possible '*', '+', or '?'.
*----------------------------------------------------------------------*/
static int piece (int *flag_param) {
register int ret_val;
register unsigned char op_code;
unsigned long min_val = REG_ZERO;
int flags_local;
ret_val = atom (&flags_local);
if (ret_val == 0) return 0; /* Something went wrong. */
op_code = *Reg_Parse;
if (!IS_QUANTIFIER (op_code)) {
*flag_param = flags_local;
return (ret_val);
}
Reg_Parse++;
if (op_code == '+') min_val = REG_ONE;
/* It is dangerous to apply certain quantifiers to a possibly zero width
item. */
if (!(flags_local & HAS_WIDTH) && min_val > REG_ZERO) {
sprintf (Error_Text, "%c operand could be empty", op_code);
CONVERT_FAIL (Error_Text);
}
*flag_param = (min_val > REG_ZERO) ? (WORST | HAS_WIDTH) : WORST;
if ( !((op_code == '*') || (op_code == '+') || (op_code == '?')) ) {
/* We get here if the IS_QUANTIFIER macro is not coordinated properly
with this function. */
CONVERT_FAIL ("internal error #2, `piece\'");
}
if (IS_QUANTIFIER (*Reg_Parse)) {
sprintf (Error_Text, "nested quantifiers, %c%c", op_code, *Reg_Parse);
CONVERT_FAIL (Error_Text);
}
emit_convert_byte (op_code);
return (ret_val);
}
/*----------------------------------------------------------------------*
* atom - Process one regex item at the lowest level
*----------------------------------------------------------------------*/
static int atom (int *flag_param) {
int ret_val = 1;
unsigned char test;
int flags_local;
*flag_param = WORST; /* Tentatively. */
switch (*Reg_Parse++) {
case '^':
emit_convert_byte ('^');
break;
case '$':
emit_convert_byte ('$');
break;
case '<':
emit_convert_byte ('<');
break;
case '>':
emit_convert_byte ('>');
break;
case '.':
emit_convert_byte ('.');
*flag_param |= (HAS_WIDTH | SIMPLE); break;
case '(':
emit_convert_byte ('(');
ret_val = chunk (PAREN, &flags_local);
if (ret_val == 0) return 0; /* Something went wrong. */
/* Add HAS_WIDTH flag if it was set by call to chunk. */
*flag_param |= flags_local & HAS_WIDTH;
break;
case '\0':
case '|':
case ')':
CONVERT_FAIL ("internal error #3, `atom\'"); /* Supposed to be */
/* caught earlier. */
case '?':
case '+':
case '*':
sprintf (Error_Text, "%c follows nothing", *(Reg_Parse - 1));
CONVERT_FAIL (Error_Text);
case '{':
emit_convert_byte ('\\'); /* Quote braces. */
emit_convert_byte ('{');
break;
case '[':
{
register unsigned int last_value;
unsigned char last_emit = 0;
unsigned char buffer [500];
int head = 0;
int negated = 0;
int do_brackets = 1;
int a_z_flag = 0;
int A_Z_flag = 0;
int zero_nine = 0;
int u_score_flag = 0;
buffer [0] = '\0';
/* Handle characters that can only occur at the start of a class. */
if (*Reg_Parse == '^') { /* Complement of range. */
negated = 1;
Reg_Parse++;
}
if (*Reg_Parse == ']' || *Reg_Parse == '-') {
/* If '-' or ']' is the first character in a class,
it is a literal character in the class. */
last_emit = *Reg_Parse;
if (head >= 498) {
CONVERT_FAIL ("too much data in [] to convert.");
}
buffer [head++] = '\\'; /* Escape `]' and '-' for clarity. */
buffer [head++] = *Reg_Parse;
Reg_Parse++;
}
/* Handle the rest of the class characters. */
while (*Reg_Parse != '\0' && *Reg_Parse != ']') {
if (*Reg_Parse == '-') { /* Process a range, e.g [a-z]. */
Reg_Parse++;
if (*Reg_Parse == ']' || *Reg_Parse == '\0') {
/* If '-' is the last character in a class it is a literal
character. If `Reg_Parse' points to the end of the
regex string, an error will be generated later. */
last_emit = '-';
if (head >= 498) {
CONVERT_FAIL ("too much data in [] to convert.");
}
buffer [head++] = '\\'; /* Escape '-' for clarity. */
buffer [head++] = '-';
} else {
if (*Reg_Parse == '\\') {
/* Handle escaped characters within a class range. */
Reg_Parse++;
if ((test = literal_escape (*Reg_Parse, 0))) {
buffer [head++] = '-';
if (*Reg_Parse != '\"') {
emit_convert_byte ('\\');
}
buffer [head++] = *Reg_Parse;
last_value = (unsigned int) test;
} else {
sprintf (
Error_Text,
"\\%c is an invalid escape sequence(3)",
*Reg_Parse);
CONVERT_FAIL (Error_Text);
}
} else {
last_value = U_CHAR_AT (Reg_Parse);
if (last_emit == '0' && last_value == '9') {
zero_nine = 1;
head--;
} else if (last_emit == 'a' && last_value == 'z') {
a_z_flag = 1;
head--;
} else if (last_emit == 'A' && last_value == 'Z') {
A_Z_flag = 1;
head--;
} else {
buffer [head++] = '-';
if ((test = literal_escape (*Reg_Parse, 1))) {
/* Ordinary character matches an escape sequence;
convert it to the escape sequence. */
if (head >= 495) {
CONVERT_FAIL (
"too much data in [] to convert.");
}
buffer [head++] = '\\';
if (test == '0') { /* Make octal escape. */
test = *Reg_Parse;
buffer [head++] = '0';
buffer [head++] = ('0' + (test / 64));
test -= (test / 64) * 64;
buffer [head++] = ('0' + (test / 8));
test -= (test / 8) * 8;
buffer [head++] = ('0' + test);
} else {
buffer [head++] = test;
}
} else {
buffer [head++] = last_value;
}
}
}
if (last_emit > last_value) {
CONVERT_FAIL ("invalid [] range");
}
last_emit = (unsigned char) last_value;
Reg_Parse++;
} /* End class character range code. */
} else if (*Reg_Parse == '\\') {
Reg_Parse++;
if ((test = literal_escape (*Reg_Parse, 0)) != '\0') {
last_emit = test;
if (head >= 498) {
CONVERT_FAIL ("too much data in [] to convert.");
}
if (*Reg_Parse != '\"') {
buffer [head++] = '\\';
}
buffer [head++] = *Reg_Parse;
} else {
sprintf (Error_Text,
"\\%c is an invalid escape sequence(1)",
*Reg_Parse);
CONVERT_FAIL (Error_Text);
}
Reg_Parse++;
/* End of class escaped sequence code */
} else {
last_emit = *Reg_Parse;
if (*Reg_Parse == '_') {
u_score_flag = 1; /* Emit later if we can't do `\w'. */
} else if ((test = literal_escape (*Reg_Parse, 1))) {
/* Ordinary character matches an escape sequence;
convert it to the escape sequence. */
if (head >= 495) {
CONVERT_FAIL ("too much data in [] to convert.");
}
buffer [head++] = '\\';
if (test == '0') { /* Make octal escape. */
test = *Reg_Parse;
buffer [head++] = '0';
buffer [head++] = ('0' + (test / 64));
test -= (test / 64) * 64;
buffer [head++] = ('0' + (test / 8));
test -= (test / 8) * 8;
buffer [head++] = ('0' + test);
} else {
if (head >= 499) {
CONVERT_FAIL ("too much data in [] to convert.");
}
buffer [head++] = test;
}
} else {
if (head >= 499) {
CONVERT_FAIL ("too much data in [] to convert.");
}
buffer [head++] = *Reg_Parse;
}
Reg_Parse++;
}
} /* End of while (*Reg_Parse != '\0' && *Reg_Parse != ']') */
if (*Reg_Parse != ']') CONVERT_FAIL ("missing right \']\'");
buffer [head] = '\0';
/* NOTE: it is impossible to specify an empty class. This is
because [] would be interpreted as "begin character class"
followed by a literal ']' character and no "end character class"
delimiter (']'). Because of this, it is always safe to assume
that a class HAS_WIDTH. */
Reg_Parse++; *flag_param |= HAS_WIDTH | SIMPLE;
if (head == 0) {
if (( a_z_flag && A_Z_flag && zero_nine && u_score_flag) ||
( a_z_flag && A_Z_flag && !zero_nine && !u_score_flag) ||
(!a_z_flag && !A_Z_flag && zero_nine && !u_score_flag)) {
do_brackets = 0;
}
}
if (do_brackets) {
emit_convert_byte ('[');
if (negated) emit_convert_byte ('^');
}
/* Output any shortcut escapes if we can. */
while (a_z_flag || A_Z_flag || zero_nine || u_score_flag) {
if (a_z_flag && A_Z_flag && zero_nine && u_score_flag) {
emit_convert_byte ('\\');
if (negated && !do_brackets) {
emit_convert_byte ('W');
} else {
emit_convert_byte ('w');
}
a_z_flag = A_Z_flag = zero_nine = u_score_flag = 0;
} else if (a_z_flag && A_Z_flag) {
emit_convert_byte ('\\');
if (negated && !do_brackets) {
emit_convert_byte ('L');
} else {
emit_convert_byte ('l');
}
a_z_flag = A_Z_flag = 0;
} else if (zero_nine) {
emit_convert_byte ('\\');
if (negated && !do_brackets) {
emit_convert_byte ('D');
} else {
emit_convert_byte ('d');
}
zero_nine = 0;
} else if (a_z_flag) {
emit_convert_byte ('a');
emit_convert_byte ('-');
emit_convert_byte ('z');
a_z_flag = 0;
} else if (A_Z_flag) {
emit_convert_byte ('A');
emit_convert_byte ('-');
emit_convert_byte ('Z');
A_Z_flag = 0;
} else if (u_score_flag) {
emit_convert_byte ('_');
u_score_flag = 0;
}
}
/* Output our buffered class characters. */
for (head = 0; buffer [head] != '\0'; head++) {
emit_convert_byte (buffer [head]);
}
if (do_brackets) {
emit_convert_byte (']');
}
}
break; /* End of character class code. */
/* Fall through to Default case to handle literal escapes. */
default:
Reg_Parse--; /* If we fell through from the above code, we are now
pointing at the back slash (\) character. */
{
unsigned char *parse_save, *emit_save;
int emit_diff, len = 0;
/* Loop until we find a meta character or end of regex string. */
for (; *Reg_Parse != '\0' &&
!strchr ((char *) Meta_Char, (int) *Reg_Parse);
len++) {
/* Save where we are in case we have to back
this character out. */
parse_save = Reg_Parse;
emit_save = Code_Emit_Ptr;
if (*Reg_Parse == '\\') {
if ((test = literal_escape (*(Reg_Parse + 1), 0))) {
if (*(Reg_Parse + 1) != '\"') {
emit_convert_byte ('\\');
}
Reg_Parse++; /* Point to escaped character */
emit_convert_byte (*Reg_Parse);
} else {
sprintf (Error_Text,
"\\%c is an invalid escape sequence(2)",
*(Reg_Parse + 1));
CONVERT_FAIL (Error_Text);
}
Reg_Parse++;
} else {
/* Ordinary character */
if ((test = literal_escape (*Reg_Parse, 1))) {
/* Ordinary character matches an escape sequence;
convert it to the escape sequence. */
emit_convert_byte ('\\');
if (test == '0') {
test = *Reg_Parse;
emit_convert_byte ('0');
emit_convert_byte ('0' + (test / 64));
test -= (test / 64) * 64;
emit_convert_byte ('0' + (test / 8));
test -= (test / 8) * 8;
emit_convert_byte ('0' + test);
} else {
emit_convert_byte (test);
}
} else {
emit_convert_byte (*Reg_Parse);
}
Reg_Parse++;
}
/* If next regex token is a quantifier (?, +. *, or {m,n}) and
our EXACTLY node so far is more than one character, leave the
last character to be made into an EXACTLY node one character
wide for the multiplier to act on. For example 'abcd* would
have an EXACTLY node with an 'abc' operand followed by a STAR
node followed by another EXACTLY node with a 'd' operand. */
if (IS_QUANTIFIER (*Reg_Parse) && len > 0) {
Reg_Parse = parse_save; /* Point to previous regex token. */
emit_diff = (Code_Emit_Ptr - emit_save);
if (Code_Emit_Ptr == &Compute_Size) {
Convert_Size -= emit_diff;
} else { /* Write over previously emitted byte. */
Code_Emit_Ptr = emit_save;
}
break;
}
}
if (len <= 0) CONVERT_FAIL ("internal error #4, `atom\'");
*flag_param |= HAS_WIDTH;
if (len == 1) *flag_param |= SIMPLE;
}
} /* END switch (*Reg_Parse++) */
return (ret_val);
}
/*----------------------------------------------------------------------*
* emit_convert_byte
*
* Emit (if appropriate) a byte of converted code.
*----------------------------------------------------------------------*/
static void emit_convert_byte (unsigned char c) {
if (Code_Emit_Ptr == &Compute_Size) {
Convert_Size++;
} else {
*Code_Emit_Ptr++ = c;
}
}
/*--------------------------------------------------------------------*
* literal_escape
*
* Recognize escaped literal characters (prefixed with backslash),
* and translate them into the corresponding character.
*
* Returns the proper character value or NULL if not a valid literal
* escape.
*--------------------------------------------------------------------*/
static unsigned char literal_escape (unsigned char c, int action) {
static unsigned char control_escape [] = {
'a', 'b',
'e',
'f', 'n', 'r', 't', 'v', '\0'
};
static unsigned char control_actual [] = {
'\a', '\b',
#ifdef EBCDIC_CHARSET
0x27, /* Escape character in IBM's EBCDIC character set. */
#else
0x1B, /* Escape character in ASCII character set. */
#endif
'\f', '\n', '\r', '\t', '\v', '\0'
};
static unsigned char valid_escape [] = {
'a', 'b', 'f', 'n', 'r', 't', 'v', '(', ')', '[',
']', '<', '>', '.', '\\', '|', '^', '$', '*', '+',
'?', '&', '\"', '\0'
};
static unsigned char value [] = {
'\a', '\b', '\f', '\n', '\r', '\t', '\v', '(', ')', '[',
']', '<', '>', '.', '\\', '|', '^', '$', '*', '+',
'?', '&', '\"', '\0'
};
int i;
if (action == 0) {
for (i = 0; valid_escape [i] != '\0'; i++) {
if (c == valid_escape [i]) return value [i];
}
} else if (action == 1) {
for (i = 0; control_actual [i] != '\0'; i++) {
if (c == control_actual [i]) {
return control_escape [i];
}
}
}
if (action == 1) {
if (!isprint (c)) {
/* Signal to generate an numeric (octal) escape. */
return '0';
}
}
return 0;
}
/*----------------------------------------------------------------------*
* ConvertSubstituteRE - Perform substitutions after a `regexp' match.
*----------------------------------------------------------------------*/
void ConvertSubstituteRE (
const char *source,
char *dest,
int max) {
register unsigned char *src;
register unsigned char *dst;
register unsigned char c;
register unsigned char test;
if (source == NULL || dest == NULL) {
reg_error ("NULL parm to `ConvertSubstituteRE\'");
return;
}
src = (unsigned char *) source;
dst = (unsigned char *) dest;
while ((c = *src++) != '\0') {
if (c == '\\') {
/* Process any case altering tokens, i.e \u, \U, \l, \L. */
if (*src == 'u' || *src == 'U' || *src == 'l' || *src == 'L') {
*dst++ = '\\';
c = *src++;
*dst++ = c;
if (c == '\0') {
break;
} else {
c = *src++;
}
}
}
if (c == '&') {
*dst++ = '&';
} else if (c == '\\') {
if (*src == '0') {
/* Convert `\0' to `&' */
*dst++ = '&'; src++;
} else if ('1' <= *src && *src <= '9') {
*dst++ = '\\';
*dst++ = *src++;
} else if ((test = literal_escape (*src, 0)) != '\0') {
*dst++ = '\\';
*dst++ = *src++;
} else if (*src == '\0') {
/* If '\' is the last character of the replacement string, it is
interpreted as a literal backslash. */
*dst++ = '\\';
} else {
/* Old regex's allowed any escape sequence. Convert these to
unescaped characters that replace themselves; i.e. they don't
need to be escaped. */
*dst++ = *src++;
}
} else {
/* Ordinary character. */
if (((char *) dst - (char *) dest) >= (max - 1)) {
break;
} else {
if ((test = literal_escape (c, 1))) {
/* Ordinary character matches an escape sequence;
convert it to the escape sequence. */
*dst++ = '\\';
if (test == '0') { /* Make octal escape. */
test = c;
*dst++ = '0';
*dst++ = ('0' + (test / 64));
test -= (test / 64) * 64;
*dst++ = ('0' + (test / 8));
test -= (test / 8) * 8;
*dst++ = ('0' + test);
} else {
*dst++ = test;
}
} else {
*dst++ = c;
}
}
}
}
*dst = '\0';
}
/*----------------------------------------------------------------------*
* reg_error
*----------------------------------------------------------------------*/
static void reg_error (char *str) {
fprintf (
stderr,
"NEdit: Internal error processing regular expression (%s)\n",
str);
}
|