From 0e7c3854bf1b1e8ac14ef867de3cc536bc5394ee Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Sat, 30 Mar 2002 12:53:50 +0000 Subject: [PATCH] Added MATCH rule [SVN r13314] --- historic/jam/src/builtins.c | 57 +++++++++++++++++++++++++++++++++++++ historic/jam/src/builtins.h | 1 + jam_src/builtins.c | 57 +++++++++++++++++++++++++++++++++++++ jam_src/builtins.h | 1 + 4 files changed, 116 insertions(+) diff --git a/historic/jam/src/builtins.c b/historic/jam/src/builtins.c index 38d58eea4..a434fee44 100644 --- a/historic/jam/src/builtins.c +++ b/historic/jam/src/builtins.c @@ -12,6 +12,7 @@ # include "rules.h" # include "filesys.h" # include "newstr.h" +# include "regexp.h" # include "frames.h" # include "hash.h" # include "strings.h" @@ -101,6 +102,10 @@ load_builtins() bind_builtin( "LEAVES" , builtin_flags, T_FLAG_LEAVES, 0 ) ); + duplicate_rule( "Match" , + bind_builtin( "MATCH" , + builtin_match, 0, 0 ) ); + duplicate_rule( "NoCare" , bind_builtin( "NOCARE" , builtin_flags, T_FLAG_NOCARE, 0 ) ); @@ -307,6 +312,58 @@ builtin_glob( return globbing.results; } +/* + * builtin_match() - MATCH rule, regexp matching + */ + +LIST * +builtin_match( + PARSE *parse, + FRAME *frame ) +{ + LIST *l = lol_get( frame->args, 0 ); + LIST *r = lol_get( frame->args, 1 ); + LIST *result = 0; + regexp *re; + + /* No pattern or string? No results. */ + + if( !l || !r ) + return L0; + + /* Just use first arg of each list. */ + + re = regcomp( l->string ); + + if( regexec( re, r->string ) ) + { + int i, top; + string buf[1]; + string_new(buf); + + /* Find highest parameter */ + + for( top = NSUBEXP; top-- > 1; ) + if( re->startp[top] != re->endp[top] ) + break; + + /* And add all parameters up to highest onto list. */ + /* Must have parameters to have results! */ + + for( i = 1; i <= top; i++ ) + { + string_append_range( buf, re->startp[i], re->endp[i] ); + result = list_new( result, newstr( buf->value ) ); + } + + string_free( buf ); + } + + free( (char *)re ); + + return result; +} + LIST * builtin_hdrmacro( PARSE *parse, diff --git a/historic/jam/src/builtins.h b/historic/jam/src/builtins.h index 8a57ad850..ec56129e5 100644 --- a/historic/jam/src/builtins.h +++ b/historic/jam/src/builtins.h @@ -21,6 +21,7 @@ LIST *builtin_exit( PARSE *parse, FRAME *args ); LIST *builtin_flags( PARSE *parse, FRAME *args ); LIST *builtin_glob( PARSE *parse, FRAME *args ); LIST *builtin_subst( PARSE *parse, FRAME *args ); +LIST *builtin_match( PARSE *parse, FRAME *args ); LIST *builtin_hdrmacro( PARSE *parse, FRAME *args ); LIST *builtin_rulenames( PARSE *parse, FRAME *args ); LIST *builtin_import( PARSE *parse, FRAME *args ); diff --git a/jam_src/builtins.c b/jam_src/builtins.c index 38d58eea4..a434fee44 100644 --- a/jam_src/builtins.c +++ b/jam_src/builtins.c @@ -12,6 +12,7 @@ # include "rules.h" # include "filesys.h" # include "newstr.h" +# include "regexp.h" # include "frames.h" # include "hash.h" # include "strings.h" @@ -101,6 +102,10 @@ load_builtins() bind_builtin( "LEAVES" , builtin_flags, T_FLAG_LEAVES, 0 ) ); + duplicate_rule( "Match" , + bind_builtin( "MATCH" , + builtin_match, 0, 0 ) ); + duplicate_rule( "NoCare" , bind_builtin( "NOCARE" , builtin_flags, T_FLAG_NOCARE, 0 ) ); @@ -307,6 +312,58 @@ builtin_glob( return globbing.results; } +/* + * builtin_match() - MATCH rule, regexp matching + */ + +LIST * +builtin_match( + PARSE *parse, + FRAME *frame ) +{ + LIST *l = lol_get( frame->args, 0 ); + LIST *r = lol_get( frame->args, 1 ); + LIST *result = 0; + regexp *re; + + /* No pattern or string? No results. */ + + if( !l || !r ) + return L0; + + /* Just use first arg of each list. */ + + re = regcomp( l->string ); + + if( regexec( re, r->string ) ) + { + int i, top; + string buf[1]; + string_new(buf); + + /* Find highest parameter */ + + for( top = NSUBEXP; top-- > 1; ) + if( re->startp[top] != re->endp[top] ) + break; + + /* And add all parameters up to highest onto list. */ + /* Must have parameters to have results! */ + + for( i = 1; i <= top; i++ ) + { + string_append_range( buf, re->startp[i], re->endp[i] ); + result = list_new( result, newstr( buf->value ) ); + } + + string_free( buf ); + } + + free( (char *)re ); + + return result; +} + LIST * builtin_hdrmacro( PARSE *parse, diff --git a/jam_src/builtins.h b/jam_src/builtins.h index 8a57ad850..ec56129e5 100644 --- a/jam_src/builtins.h +++ b/jam_src/builtins.h @@ -21,6 +21,7 @@ LIST *builtin_exit( PARSE *parse, FRAME *args ); LIST *builtin_flags( PARSE *parse, FRAME *args ); LIST *builtin_glob( PARSE *parse, FRAME *args ); LIST *builtin_subst( PARSE *parse, FRAME *args ); +LIST *builtin_match( PARSE *parse, FRAME *args ); LIST *builtin_hdrmacro( PARSE *parse, FRAME *args ); LIST *builtin_rulenames( PARSE *parse, FRAME *args ); LIST *builtin_import( PARSE *parse, FRAME *args );