Skip to content
Snippets Groups Projects
Commit a6f8bea2 authored by Henrik (Grubba) Grubbström's avatar Henrik (Grubba) Grubbström
Browse files

Added support for using the system regexps.

Rev: src/modules/Regexp/Makefile.in:1.2
Rev: src/modules/Regexp/configure.in:1.2
Rev: src/modules/Regexp/glue.c:1.2
parent 4c76f66f
No related branches found
No related tags found
No related merge requests found
SRCDIR=@srcdir@ SRCDIR=@srcdir@
VPATH=@srcdir@:@srcdir@/../..:../.. VPATH=@srcdir@:@srcdir@/../..:../..
OBJS=regexp.o glue.o OBJS=glue.o @REGEXP_OBJS@
@dynamic_module_makefile@ @dynamic_module_makefile@
@dependencies@ @dependencies@
\ No newline at end of file
AC_INIT(regexp.c) AC_INIT(pike_regexp.c)
AC_CONFIG_HEADER(config.h)
sinclude(../module_configure.in) sinclude(../module_configure.in)
AC_HAVE_HEADERS(regexp.h)
AC_CHECK_FUNCS(regcomp regexec regerror regfree)
AC_MSG_CHECKING(C lib has regexps)
if test $ac_cv_header_regexp_h$ac_cv_func_regcomp$ac_cv_func_regexec$ac_cv_func_regerror$ac_cv_func_regfree = yesyesyesyesyes ; then
AC_DEFINE(USE_SYSTEM_REGEXP)
REGEXP_OBJS=""
AC_MSG_RESULT(yes)
else
REGEXP_OBJS="pike_regexp.o"
AC_MSG_RESULT(no)
fi
AC_SUBST(REGEXP_OBJS)
AC_OUTPUT(Makefile,echo FOO >stamp-h ) AC_OUTPUT(Makefile,echo FOO >stamp-h )
......
...@@ -3,6 +3,11 @@ ...@@ -3,6 +3,11 @@
||| Pike is distributed as GPL (General Public License) ||| Pike is distributed as GPL (General Public License)
||| See the files COPYING and DISCLAIMER for more information. ||| See the files COPYING and DISCLAIMER for more information.
\*/ \*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "global.h" #include "global.h"
#include "types.h" #include "types.h"
#include "interpret.h" #include "interpret.h"
...@@ -11,47 +16,93 @@ ...@@ -11,47 +16,93 @@
#include "array.h" #include "array.h"
#include "object.h" #include "object.h"
#include "macros.h" #include "macros.h"
#include "threads.h"
#ifdef USE_SYSTEM_REGEXP
#include <regexp.h> #include <regexp.h>
struct regexp_glue
{
regex_t regexp;
int num_paren;
};
#else
#include <pike_regexp.h>
struct regexp_glue struct regexp_glue
{ {
struct regexp *regexp; struct regexp *regexp;
}; };
#endif /* USE_SYSTEM_REGEXP */
#define THIS ((struct regexp_glue *)(fp->current_storage)) #define THIS ((struct regexp_glue *)(fp->current_storage))
static void do_free() static void do_free()
{ {
if(THIS->regexp) if(THIS->regexp)
{ {
#ifdef USE_SYSTEM_REGEXP
regfree(&(THIS->regexp));
#else
free((char *)THIS->regexp); free((char *)THIS->regexp);
THIS->regexp=0; THIS->regexp=0;
#endif /* USE_SYSTEM_REGEXP */
} }
} }
static void regexp_create(INT32 args) static void regexp_create(INT32 args)
{ {
const char *str;
do_free(); do_free();
if(args) if(args)
{ {
if(sp[-args].type != T_STRING) get_all_args("Regexp.regexp->create", args, "%s", &str);
error("Bad argument 1 to regexp->create()\n");
#ifdef USE_SYSTEM_REGEXP
{
int err = regcomp(&(THIS->regexp), str, 0);
int i;
char *paren_ptr;
if (err) {
char buf[1024];
regerror(err, &(THIS->regexp), buf, 1024);
error("Regexp.regexp->create(): Compilation failed:%s\n", buf);
}
for (i=0,paren_ptr=str; paren_ptr = strchr(paren_ptr, '('); i++) {
paren_ptr++;
}
THIS->num_paren = i;
}
#else
THIS->regexp=regcomp(sp[-args].u.string->str, 0); THIS->regexp=regcomp(sp[-args].u.string->str, 0);
#endif
} }
} }
static void regexp_match(INT32 args) static void regexp_match(INT32 args)
{ {
int i; int i;
if(!args) const char *str;
error("Too few arguments to regexp->match()\n"); #ifdef USE_SYSTEM_REGEXP
regex_t *regexp = &(THIS->regexp);
if(sp[-args].type != T_STRING) #else
error("Bad argument 1 to regexp->match()\n"); struct regexp *regexp = THIS->regexp;
#endif /* USE_SYSTEM_REGEXP */
i=regexec(THIS->regexp, sp[-args].u.string->str);
get_all_args("Regexp.regexp->match", args, "%s", &str);
#ifdef USE_SYSTEM_REGEXP
ALLOW_THREADS();
i = !regexec(regexp, str, 0, NULL, 0);
DISALLOW_THREADS();
#else
i=regexec(regexp, str);
#endif /* USE_SYSTEM_REGEXP */
pop_n_elems(args); pop_n_elems(args);
push_int(i); push_int(i);
} }
...@@ -59,14 +110,47 @@ static void regexp_match(INT32 args) ...@@ -59,14 +110,47 @@ static void regexp_match(INT32 args)
static void regexp_split(INT32 args) static void regexp_split(INT32 args)
{ {
struct pike_string *s; struct pike_string *s;
#ifdef USE_SYSTEM_REGEXP
regex_t *r;
regmatch_t *pmatch;
size_t nmatch;
int match;
#else
struct regexp *r; struct regexp *r;
if(!args) #endif /* USE_SYSTEM_REGEXP */
error("Too few arguments to regexp->split()\n");
get_all_args("Regexp.regexp->split", args, "%S", &s);
#ifdef USE_SYSTEM_REGEXP
r = &(THIS->regexp);
nmatch = THIS->num_paren+1;
pmatch = xalloc(sizeof(regmatch_t)*nmatch);
if(sp[-args].type != T_STRING) THREADS_ALLOW();
error("Bad argument 1 to regexp->split()\n"); match = !regexec(r, s->str, nmatch, pmatch, 0);
THREADS_DISALLOW();
if (match) {
int i,j;
s=sp[-args].u.string; s->refs++;
pop_n_elems(args);
for (i=1; i < nmatch; i++) {
if (pmatch[i].rm_sp && pmatch[i].rm_so != -1) {
push_string(make_shared_binary_string(pmatch[i].rm_sp,
pmatch[i].rm_eo-pmatch[i].rm_so));
} else {
push_int(0);
}
}
f_aggregate_array(nmatch-1);
free_string(s);
} else {
pop_n_elems(args);
push_int(0);
}
#else
if(regexec(r=THIS->regexp, s->str)) if(regexec(r=THIS->regexp, s->str))
{ {
int i,j; int i,j;
...@@ -90,11 +174,16 @@ static void regexp_split(INT32 args) ...@@ -90,11 +174,16 @@ static void regexp_split(INT32 args)
pop_n_elems(args); pop_n_elems(args);
push_int(0); push_int(0);
} }
#endif /* USE_SYSTEM_REGEXP */
} }
static void init_regexp_glue(struct object *o) static void init_regexp_glue(struct object *o)
{ {
#ifdef USE_SYSTEM_REGEXP
MEMCLR(THIS, sizeof(struct regexp_glue));
#else
THIS->regexp=0; THIS->regexp=0;
#endif /* USE_SYSTEM_REGEXP */
} }
static void exit_regexp_glue(struct object *o) static void exit_regexp_glue(struct object *o)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment