* Better compliance with XSD regexps (when transforming to Posix regexps)
* Added `\p{L}` and `\p{N}`
* Added escaping of `$`
* Added regexp [test/test_pattern.sh]
This commit is contained in:
parent
7e109d1d4b
commit
f7771d86c2
8 changed files with 724 additions and 32 deletions
|
|
@ -33,6 +33,7 @@
|
|||
|
||||
* Utility for compiling regexp and checking validity
|
||||
* gcc -I /usr/include/libxml2 regex.c -o regex -lxml2
|
||||
* @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028
|
||||
*/
|
||||
#ifdef HAVE_CONFIG_H
|
||||
#include "clixon_config.h" /* generated by config & autoconf */
|
||||
|
|
@ -45,8 +46,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
#undef HAVE_LIBXML2
|
||||
#ifdef HAVE_LIBXML2 /* Actually it should check for a header file */
|
||||
#include <libxml/xmlregexp.h>
|
||||
#endif
|
||||
|
|
@ -66,7 +65,9 @@
|
|||
static int
|
||||
regex_libxml2(char *regexp0,
|
||||
char *content0,
|
||||
int nr)
|
||||
int nr,
|
||||
int debug)
|
||||
|
||||
{
|
||||
int retval = -1;
|
||||
#ifdef HAVE_LIBXML2
|
||||
|
|
@ -92,7 +93,8 @@ regex_libxml2(char *regexp0,
|
|||
static int
|
||||
regex_posix(char *regexp,
|
||||
char *content,
|
||||
int nr)
|
||||
int nr,
|
||||
int debug)
|
||||
{
|
||||
int retval = -1;
|
||||
char *posix = NULL;
|
||||
|
|
@ -105,6 +107,7 @@ regex_posix(char *regexp,
|
|||
|
||||
if (regexp_xsd2posix(regexp, &posix) < 0)
|
||||
goto done;
|
||||
clicon_debug(1, "posix: %s", posix);
|
||||
len0 = strlen(posix);
|
||||
if (len0 > sizeof(pattern)-5){
|
||||
fprintf(stderr, "pattern too long\n");
|
||||
|
|
@ -134,15 +137,15 @@ regex_posix(char *regexp,
|
|||
static int
|
||||
usage(char *argv0)
|
||||
{
|
||||
fprintf(stderr, "usage:%s [options] (either one of -p or -x)\n"
|
||||
fprintf(stderr, "usage:%s [options]\n"
|
||||
"where options are\n"
|
||||
"\t-h \t\tHelp\n"
|
||||
"\t-D <level>\tDebug\n"
|
||||
"\t-p \txsd->posix translation regexp\n"
|
||||
"\t-x \tlibxml2 regexp\n"
|
||||
"\t-n <nr> \tIterate content match (0 means only compile)\n"
|
||||
"\t-p \txsd->posix translation regexp (default)\n"
|
||||
"\t-x \tlibxml2 regexp (alternative to -p)\n"
|
||||
"\t-n <nr> \tIterate content match (default: 1, 0: no match only compile)\n"
|
||||
"\t-r <regexp> \tregexp (mandatory)\n"
|
||||
"\t-c <string> \tValue content string(mandatory)\n",
|
||||
"\t-c <string> \tValue content string(mandatory if -n > 0)\n",
|
||||
argv0
|
||||
);
|
||||
exit(0);
|
||||
|
|
@ -157,10 +160,9 @@ main(int argc,
|
|||
int c;
|
||||
char *regexp = NULL;
|
||||
char *content = NULL;
|
||||
int posix = 0;
|
||||
int libxml2 = 0;
|
||||
int ret;
|
||||
int nr = 1;
|
||||
int mode = 0; /* 0 is posix, 1 is libxml */
|
||||
|
||||
optind = 1;
|
||||
opterr = 0;
|
||||
|
|
@ -174,14 +176,14 @@ main(int argc,
|
|||
usage(argv0);
|
||||
break;
|
||||
case 'p': /* xsd->posix */
|
||||
posix++;
|
||||
mode = 0;
|
||||
break;
|
||||
case 'n': /* Number of iterations */
|
||||
if ((nr = atoi(optarg)) < 0)
|
||||
usage(argv0);
|
||||
break;
|
||||
case 'x': /* libxml2 */
|
||||
libxml2++;
|
||||
mode = 1;
|
||||
break;
|
||||
case 'r': /* regexp */
|
||||
regexp = optarg;
|
||||
|
|
@ -194,22 +196,31 @@ main(int argc,
|
|||
break;
|
||||
}
|
||||
clicon_log_init(__FILE__, debug?LOG_DEBUG:LOG_INFO, CLICON_LOG_STDERR);
|
||||
if (regexp == NULL || content == NULL)
|
||||
if (regexp == NULL){
|
||||
fprintf(stderr, "-r mandatory\n");
|
||||
usage(argv0);
|
||||
if (posix == libxml2)
|
||||
}
|
||||
if (nr > 0 && content == NULL){
|
||||
fprintf(stderr, "-c mandatory (if -n > 0)\n");
|
||||
usage(argv0);
|
||||
}
|
||||
if (mode != 0 && mode != 1){
|
||||
fprintf(stderr, "Neither posix or libxml2 set\n");
|
||||
usage(argv0);
|
||||
}
|
||||
clicon_debug(1, "regexp:%s", regexp);
|
||||
clicon_debug(1, "content:%s", content);
|
||||
if (libxml2){
|
||||
if ((ret = regex_libxml2(regexp, content, nr)) < 0)
|
||||
if (mode == 0){
|
||||
if ((ret = regex_posix(regexp, content, nr, debug)) < 0)
|
||||
goto done;
|
||||
|
||||
}
|
||||
else if (posix){
|
||||
if ((ret = regex_posix(regexp, content, nr)) < 0)
|
||||
else if (mode == 1){
|
||||
if ((ret = regex_libxml2(regexp, content, nr, debug)) < 0)
|
||||
goto done;
|
||||
}
|
||||
else
|
||||
goto done;
|
||||
usage(argv0);
|
||||
fprintf(stdout, "%d\n", ret);
|
||||
exit(ret);
|
||||
retval = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue