regex.c (89857) | regex.c (130561) |
---|---|
1/* Extended regular expression matching and search library, 2 version 0.12. 3 (Implements POSIX draft P1003.2/D11.2, except for some of the 4 internationalization features.) | 1/* Extended regular expression matching and search library, 2 version 0.12. 3 (Implements POSIX draft P1003.2/D11.2, except for some of the 4 internationalization features.) |
5 Copyright (C) 1993-1999, 2000, 2001 Free Software Foundation, Inc. | 5 Copyright (C) 1993-1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
6 This file is part of the GNU C Library. 7 8 The GNU C Library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 The GNU C Library is distributed in the hope that it will be useful, --- 6 unchanged lines hidden (view full) --- 20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 21 02111-1307 USA. */ 22 23/* This file has been modified for usage in libiberty. It includes "xregex.h" 24 instead of <regex.h>. The "xregex.h" header file renames all external 25 routines with an "x" prefix so they do not collide with the native regex 26 routines or with other components regex routines. */ 27/* AIX requires this to be the first thing in the file. */ | 6 This file is part of the GNU C Library. 7 8 The GNU C Library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU Lesser General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 The GNU C Library is distributed in the hope that it will be useful, --- 6 unchanged lines hidden (view full) --- 20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 21 02111-1307 USA. */ 22 23/* This file has been modified for usage in libiberty. It includes "xregex.h" 24 instead of <regex.h>. The "xregex.h" header file renames all external 25 routines with an "x" prefix so they do not collide with the native regex 26 routines or with other components regex routines. */ 27/* AIX requires this to be the first thing in the file. */ |
28#if defined _AIX && !defined REGEX_MALLOC | 28#if defined _AIX && !defined __GNUC__ && !defined REGEX_MALLOC |
29 #pragma alloca 30#endif 31 32#undef _GNU_SOURCE 33#define _GNU_SOURCE 34 35#ifdef HAVE_CONFIG_H 36# include <config.h> 37#endif 38 | 29 #pragma alloca 30#endif 31 32#undef _GNU_SOURCE 33#define _GNU_SOURCE 34 35#ifdef HAVE_CONFIG_H 36# include <config.h> 37#endif 38 |
39#include <ansidecl.h> 40 |
|
39#ifndef PARAMS 40# if defined __GNUC__ || (defined __STDC__ && __STDC__) 41# define PARAMS(args) args 42# else 43# define PARAMS(args) () 44# endif /* GCC. */ 45#endif /* Not PARAMS. */ 46 --- 1321 unchanged lines hidden (view full) --- 1368weak_alias (__re_set_syntax, re_set_syntax) 1369# endif 1370 1371/* This table gives an error message for each of the error codes listed 1372 in regex.h. Obviously the order here has to be same as there. 1373 POSIX doesn't require that we do anything for REG_NOERROR, 1374 but why not be nice? */ 1375 | 41#ifndef PARAMS 42# if defined __GNUC__ || (defined __STDC__ && __STDC__) 43# define PARAMS(args) args 44# else 45# define PARAMS(args) () 46# endif /* GCC. */ 47#endif /* Not PARAMS. */ 48 --- 1321 unchanged lines hidden (view full) --- 1370weak_alias (__re_set_syntax, re_set_syntax) 1371# endif 1372 1373/* This table gives an error message for each of the error codes listed 1374 in regex.h. Obviously the order here has to be same as there. 1375 POSIX doesn't require that we do anything for REG_NOERROR, 1376 but why not be nice? */ 1377 |
1376static const char re_error_msgid[] = | 1378static const char *re_error_msgid[] = |
1377 { | 1379 { |
1378# define REG_NOERROR_IDX 0 1379 gettext_noop ("Success") /* REG_NOERROR */ 1380 "\0" 1381# define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success") 1382 gettext_noop ("No match") /* REG_NOMATCH */ 1383 "\0" 1384# define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match") 1385 gettext_noop ("Invalid regular expression") /* REG_BADPAT */ 1386 "\0" 1387# define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression") 1388 gettext_noop ("Invalid collation character") /* REG_ECOLLATE */ 1389 "\0" 1390# define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character") 1391 gettext_noop ("Invalid character class name") /* REG_ECTYPE */ 1392 "\0" 1393# define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name") 1394 gettext_noop ("Trailing backslash") /* REG_EESCAPE */ 1395 "\0" 1396# define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash") 1397 gettext_noop ("Invalid back reference") /* REG_ESUBREG */ 1398 "\0" 1399# define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference") 1400 gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */ 1401 "\0" 1402# define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^") 1403 gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */ 1404 "\0" 1405# define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(") 1406 gettext_noop ("Unmatched \\{") /* REG_EBRACE */ 1407 "\0" 1408# define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{") 1409 gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */ 1410 "\0" 1411# define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}") 1412 gettext_noop ("Invalid range end") /* REG_ERANGE */ 1413 "\0" 1414# define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end") 1415 gettext_noop ("Memory exhausted") /* REG_ESPACE */ 1416 "\0" 1417# define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted") 1418 gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */ 1419 "\0" 1420# define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression") 1421 gettext_noop ("Premature end of regular expression") /* REG_EEND */ 1422 "\0" 1423# define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression") 1424 gettext_noop ("Regular expression too big") /* REG_ESIZE */ 1425 "\0" 1426# define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big") | 1380 gettext_noop ("Success"), /* REG_NOERROR */ 1381 gettext_noop ("No match"), /* REG_NOMATCH */ 1382 gettext_noop ("Invalid regular expression"), /* REG_BADPAT */ 1383 gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */ 1384 gettext_noop ("Invalid character class name"), /* REG_ECTYPE */ 1385 gettext_noop ("Trailing backslash"), /* REG_EESCAPE */ 1386 gettext_noop ("Invalid back reference"), /* REG_ESUBREG */ 1387 gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */ 1388 gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */ 1389 gettext_noop ("Unmatched \\{"), /* REG_EBRACE */ 1390 gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */ 1391 gettext_noop ("Invalid range end"), /* REG_ERANGE */ 1392 gettext_noop ("Memory exhausted"), /* REG_ESPACE */ 1393 gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */ 1394 gettext_noop ("Premature end of regular expression"), /* REG_EEND */ 1395 gettext_noop ("Regular expression too big"), /* REG_ESIZE */ |
1427 gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */ 1428 }; | 1396 gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */ 1397 }; |
1429 1430static const size_t re_error_msgid_idx[] = 1431 { 1432 REG_NOERROR_IDX, 1433 REG_NOMATCH_IDX, 1434 REG_BADPAT_IDX, 1435 REG_ECOLLATE_IDX, 1436 REG_ECTYPE_IDX, 1437 REG_EESCAPE_IDX, 1438 REG_ESUBREG_IDX, 1439 REG_EBRACK_IDX, 1440 REG_EPAREN_IDX, 1441 REG_EBRACE_IDX, 1442 REG_BADBR_IDX, 1443 REG_ERANGE_IDX, 1444 REG_ESPACE_IDX, 1445 REG_BADRPT_IDX, 1446 REG_EEND_IDX, 1447 REG_ESIZE_IDX, 1448 REG_ERPAREN_IDX 1449 }; | |
1450 1451#endif /* INSIDE_RECURSION */ 1452 1453#ifndef DEFINED_ONCE 1454/* Avoiding alloca during matching, to placate r_alloc. */ 1455 1456/* Define MATCH_MAY_ALLOCATE unless we need to make sure that the 1457 searching and matching functions should not call alloca. On some --- 3239 unchanged lines hidden (view full) --- 4697 4698 INIT_FAIL_STACK (); 4699 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */ 4700 bufp->fastmap_accurate = 1; /* It will be when we're done. */ 4701 bufp->can_be_null = 0; 4702 4703 while (1) 4704 { | 1398 1399#endif /* INSIDE_RECURSION */ 1400 1401#ifndef DEFINED_ONCE 1402/* Avoiding alloca during matching, to placate r_alloc. */ 1403 1404/* Define MATCH_MAY_ALLOCATE unless we need to make sure that the 1405 searching and matching functions should not call alloca. On some --- 3239 unchanged lines hidden (view full) --- 4645 4646 INIT_FAIL_STACK (); 4647 bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */ 4648 bufp->fastmap_accurate = 1; /* It will be when we're done. */ 4649 bufp->can_be_null = 0; 4650 4651 while (1) 4652 { |
4705 if (p == pend || *p == succeed) | 4653 if (p == pend || *p == (UCHAR_T) succeed) |
4706 { 4707 /* We have reached the (effective) end of pattern. */ 4708 if (!FAIL_STACK_EMPTY ()) 4709 { 4710 bufp->can_be_null |= path_can_be_null; 4711 4712 /* Reset for next path. */ 4713 path_can_be_null = true; --- 3225 unchanged lines hidden (view full) --- 7939 if (MB_CUR_MAX != 1) 7940 ret = wcs_regex_compile (pattern, length, re_syntax_options, bufp); 7941 else 7942# endif 7943 ret = byte_regex_compile (pattern, length, re_syntax_options, bufp); 7944 7945 if (!ret) 7946 return NULL; | 4654 { 4655 /* We have reached the (effective) end of pattern. */ 4656 if (!FAIL_STACK_EMPTY ()) 4657 { 4658 bufp->can_be_null |= path_can_be_null; 4659 4660 /* Reset for next path. */ 4661 path_can_be_null = true; --- 3225 unchanged lines hidden (view full) --- 7887 if (MB_CUR_MAX != 1) 7888 ret = wcs_regex_compile (pattern, length, re_syntax_options, bufp); 7889 else 7890# endif 7891 ret = byte_regex_compile (pattern, length, re_syntax_options, bufp); 7892 7893 if (!ret) 7894 return NULL; |
7947 return gettext (re_error_msgid + re_error_msgid_idx[(int) ret]); | 7895 return gettext (re_error_msgid[(int) ret]); |
7948} 7949#ifdef _LIBC 7950weak_alias (__re_compile_pattern, re_compile_pattern) 7951#endif 7952 7953/* Entry points compatible with 4.2 BSD regex library. We don't define 7954 them unless specifically requested. */ 7955 --- 20 unchanged lines hidden (view full) --- 7976 return gettext ("No previous regular expression"); 7977 return 0; 7978 } 7979 7980 if (!re_comp_buf.buffer) 7981 { 7982 re_comp_buf.buffer = (unsigned char *) malloc (200); 7983 if (re_comp_buf.buffer == NULL) | 7896} 7897#ifdef _LIBC 7898weak_alias (__re_compile_pattern, re_compile_pattern) 7899#endif 7900 7901/* Entry points compatible with 4.2 BSD regex library. We don't define 7902 them unless specifically requested. */ 7903 --- 20 unchanged lines hidden (view full) --- 7924 return gettext ("No previous regular expression"); 7925 return 0; 7926 } 7927 7928 if (!re_comp_buf.buffer) 7929 { 7930 re_comp_buf.buffer = (unsigned char *) malloc (200); 7931 if (re_comp_buf.buffer == NULL) |
7984 return (char *) gettext (re_error_msgid 7985 + re_error_msgid_idx[(int) REG_ESPACE]); | 7932 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]); |
7986 re_comp_buf.allocated = 200; 7987 7988 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH); 7989 if (re_comp_buf.fastmap == NULL) | 7933 re_comp_buf.allocated = 200; 7934 7935 re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH); 7936 if (re_comp_buf.fastmap == NULL) |
7990 return (char *) gettext (re_error_msgid 7991 + re_error_msgid_idx[(int) REG_ESPACE]); | 7937 return (char *) gettext (re_error_msgid[(int) REG_ESPACE]); |
7992 } 7993 7994 /* Since `re_exec' always passes NULL for the `regs' argument, we 7995 don't need to initialize the pattern buffer fields which affect it. */ 7996 7997 /* Match anchors at newlines. */ 7998 re_comp_buf.newline_anchor = 1; 7999 8000# ifdef MBS_SUPPORT 8001 if (MB_CUR_MAX != 1) 8002 ret = wcs_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf); 8003 else 8004# endif 8005 ret = byte_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf); 8006 8007 if (!ret) 8008 return NULL; 8009 8010 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */ | 7938 } 7939 7940 /* Since `re_exec' always passes NULL for the `regs' argument, we 7941 don't need to initialize the pattern buffer fields which affect it. */ 7942 7943 /* Match anchors at newlines. */ 7944 re_comp_buf.newline_anchor = 1; 7945 7946# ifdef MBS_SUPPORT 7947 if (MB_CUR_MAX != 1) 7948 ret = wcs_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf); 7949 else 7950# endif 7951 ret = byte_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf); 7952 7953 if (!ret) 7954 return NULL; 7955 7956 /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */ |
8011 return (char *) gettext (re_error_msgid + re_error_msgid_idx[(int) ret]); | 7957 return (char *) gettext (re_error_msgid[(int) ret]); |
8012} 8013 8014 8015int 8016#ifdef _LIBC 8017weak_function 8018#endif 8019re_exec (s) --- 71 unchanged lines hidden (view full) --- 8091 preg->translate 8092 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE 8093 * sizeof (*(RE_TRANSLATE_TYPE)0)); 8094 if (preg->translate == NULL) 8095 return (int) REG_ESPACE; 8096 8097 /* Map uppercase characters to corresponding lowercase ones. */ 8098 for (i = 0; i < CHAR_SET_SIZE; i++) | 7958} 7959 7960 7961int 7962#ifdef _LIBC 7963weak_function 7964#endif 7965re_exec (s) --- 71 unchanged lines hidden (view full) --- 8037 preg->translate 8038 = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE 8039 * sizeof (*(RE_TRANSLATE_TYPE)0)); 8040 if (preg->translate == NULL) 8041 return (int) REG_ESPACE; 8042 8043 /* Map uppercase characters to corresponding lowercase ones. */ 8044 for (i = 0; i < CHAR_SET_SIZE; i++) |
8099 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i; | 8045 preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : (int) i; |
8100 } 8101 else 8102 preg->translate = NULL; 8103 8104 /* If REG_NEWLINE is set, newlines are treated differently. */ 8105 if (cflags & REG_NEWLINE) 8106 { /* REG_NEWLINE implies neither . nor [^...] match newline. */ 8107 syntax &= ~RE_DOT_NEWLINE; --- 118 unchanged lines hidden (view full) --- 8226 8227 8228/* Returns a message corresponding to an error code, ERRCODE, returned 8229 from either regcomp or regexec. We don't use PREG here. */ 8230 8231size_t 8232regerror (errcode, preg, errbuf, errbuf_size) 8233 int errcode; | 8046 } 8047 else 8048 preg->translate = NULL; 8049 8050 /* If REG_NEWLINE is set, newlines are treated differently. */ 8051 if (cflags & REG_NEWLINE) 8052 { /* REG_NEWLINE implies neither . nor [^...] match newline. */ 8053 syntax &= ~RE_DOT_NEWLINE; --- 118 unchanged lines hidden (view full) --- 8172 8173 8174/* Returns a message corresponding to an error code, ERRCODE, returned 8175 from either regcomp or regexec. We don't use PREG here. */ 8176 8177size_t 8178regerror (errcode, preg, errbuf, errbuf_size) 8179 int errcode; |
8234 const regex_t *preg; | 8180 const regex_t *preg ATTRIBUTE_UNUSED; |
8235 char *errbuf; 8236 size_t errbuf_size; 8237{ 8238 const char *msg; 8239 size_t msg_size; 8240 8241 if (errcode < 0 | 8181 char *errbuf; 8182 size_t errbuf_size; 8183{ 8184 const char *msg; 8185 size_t msg_size; 8186 8187 if (errcode < 0 |
8242 || errcode >= (int) (sizeof (re_error_msgid_idx) 8243 / sizeof (re_error_msgid_idx[0]))) | 8188 || errcode >= (int) (sizeof (re_error_msgid) 8189 / sizeof (re_error_msgid[0]))) |
8244 /* Only error codes returned by the rest of the code should be passed 8245 to this routine. If we are given anything else, or if other regex 8246 code generates an invalid error code, then the program has a bug. 8247 Dump core so we can fix it. */ 8248 abort (); 8249 | 8190 /* Only error codes returned by the rest of the code should be passed 8191 to this routine. If we are given anything else, or if other regex 8192 code generates an invalid error code, then the program has a bug. 8193 Dump core so we can fix it. */ 8194 abort (); 8195 |
8250 msg = gettext (re_error_msgid + re_error_msgid_idx[errcode]); | 8196 msg = gettext (re_error_msgid[errcode]); |
8251 8252 msg_size = strlen (msg) + 1; /* Includes the null. */ 8253 8254 if (errbuf_size != 0) 8255 { 8256 if (msg_size > errbuf_size) 8257 { 8258#if defined HAVE_MEMPCPY || defined _LIBC | 8197 8198 msg_size = strlen (msg) + 1; /* Includes the null. */ 8199 8200 if (errbuf_size != 0) 8201 { 8202 if (msg_size > errbuf_size) 8203 { 8204#if defined HAVE_MEMPCPY || defined _LIBC |
8259 *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0'; | 8205 *((char *) mempcpy (errbuf, msg, errbuf_size - 1)) = '\0'; |
8260#else 8261 memcpy (errbuf, msg, errbuf_size - 1); 8262 errbuf[errbuf_size - 1] = 0; 8263#endif 8264 } 8265 else 8266 memcpy (errbuf, msg, msg_size); 8267 } --- 106 unchanged lines hidden --- | 8206#else 8207 memcpy (errbuf, msg, errbuf_size - 1); 8208 errbuf[errbuf_size - 1] = 0; 8209#endif 8210 } 8211 else 8212 memcpy (errbuf, msg, msg_size); 8213 } --- 106 unchanged lines hidden --- |