From: Ævar Arnfjörð Bjarmason Date: Thu, 27 Dec 2007 18:54:22 +0000 (+0000) Subject: 0.03 from cpan X-Git-Tag: v0.04~10 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2Fre-engine-Plugin.git;a=commitdiff_plain;h=4d5d5872e8624a2e37fb68c6a280f497806313e8 0.03 from cpan --- 4d5d5872e8624a2e37fb68c6a280f497806313e8 diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..50a3310 --- /dev/null +++ b/ChangeLog @@ -0,0 +1,15 @@ +2007-05-17 Ævar Arnfjörð Bjarmason (0.03) + + * Plugin.xs: The exec callback would call_sv on a NULL value + causing a segfault + * Plugin.pod: Use %hash ~~ 'i' instead of $hash{i}, more stylish! + * inc: Bumping Module::Install to 0.65 + +2007-05-17 Ævar Arnfjörð Bjarmason (0.02) + + * Brought up to date with current blead after lots of hacking on + blead itself, too many changes to list + +2007-02-18 Ævar Arnfjörð Bjarmason (0.01) + + * Initial release diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..97d9d7e --- /dev/null +++ b/MANIFEST @@ -0,0 +1,43 @@ +ChangeLog +inc/Module/AutoInstall.pm +inc/Module/Install.pm +inc/Module/Install/AutoInstall.pm +inc/Module/Install/Base.pm +inc/Module/Install/Can.pm +inc/Module/Install/Fetch.pm +inc/Module/Install/Include.pm +inc/Module/Install/Makefile.pm +inc/Module/Install/Makefile/Version.pm +inc/Module/Install/Metadata.pm +inc/Module/Install/Win32.pm +inc/Module/Install/WriteAll.pm +Makefile.PL +MANIFEST This list of files +META.yml +Plugin.h +Plugin.pm +Plugin.pod +Plugin.xs +t/00-compile.t +t/eval-comp.t +t/eval-exec.t +t/Example.pm +t/Example.t +t/import.t +t/methods.t +t/methods/free.t +t/methods/minlen/bytes.t +t/methods/minlen/get.t +t/methods/minlen/set.t +t/methods/mod.t +t/methods/pattern/modify.t +t/methods/pattern/types.t +t/methods/stash.t +t/methods/str/types.t +t/methods/str/undef.t +t/num_buff/FETCH.t +t/num_buff/LENGTH.t +t/num_buff/STORE.t +t/taint/rx.t +t/taint/util.t +typemap diff --git a/META.yml b/META.yml new file mode 100644 index 0000000..b334027 --- /dev/null +++ b/META.yml @@ -0,0 +1,19 @@ +--- +abstract: API to write custom regex engines +author: 'Ævar Arnfjörð Bjarmason ' +build_requires: + Test::More: 0 +distribution_type: module +generated_by: Module::Install version 0.65 +license: perl +meta-spec: + url: http://module-build.sourceforge.net/META-spec-v1.3.html + version: 1.3 +name: re-engine-Plugin +no_index: + directory: + - inc + - t +requires: + perl: 5.9.5 +tests: t/*.t t/*/*.t t/*/*/*.t diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..c1224f4 --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,19 @@ +use strict; +use inc::Module::Install; + +name 're-engine-Plugin'; + +perl_version '5.009005'; + +author 'Ævar Arnfjörð Bjarmason '; + +abstract_from 'Plugin.pod'; +license_from 'Plugin.pod'; + +# t/ +build_requires 'Test::More' => 0; # 5.007003 + +tests 't/*.t t/*/*.t t/*/*/*.t'; + +auto_install; +WriteAll; diff --git a/Plugin.h b/Plugin.h new file mode 100644 index 0000000..2124ad8 --- /dev/null +++ b/Plugin.h @@ -0,0 +1,84 @@ + +#define GET_SELF_FROM_PPRIVATE(pprivate) \ + re__engine__Plugin self; \ + SELF_FROM_PPRIVATE(self,pprivate); + +/* re__engine__Plugin self; SELF_FROM_PPRIVATE(self,rx->pprivate) */ +#define SELF_FROM_PPRIVATE(self, pprivate) \ + if (sv_isobject(pprivate)) { \ + SV * ref = SvRV((SV*)pprivate); \ + IV tmp = SvIV((SV*)ref); \ + self = INT2PTR(re__engine__Plugin,tmp); \ + } else { \ + Perl_croak(aTHX_ "Not an object"); \ + } + +START_EXTERN_C +EXTERN_C const regexp_engine engine_plugin; +EXTERN_C REGEXP * Plugin_comp(pTHX_ const SV const *, const U32); +EXTERN_C I32 Plugin_exec(pTHX_ REGEXP * const, char *, char *, + char *, I32, SV *, void *, U32); +EXTERN_C char * Plugin_intuit(pTHX_ REGEXP * const, SV *, char *, + char *, U32, re_scream_pos_data *); +EXTERN_C SV * Plugin_checkstr(pTHX_ REGEXP * const); +EXTERN_C void Plugin_free(pTHX_ REGEXP * const); +EXTERN_C void Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const, + const I32, SV * const); +EXTERN_C void Plugin_numbered_buff_STORE(pTHX_ REGEXP * const, + const I32, SV const * const); +EXTERN_C I32 Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const, + const SV * const, const I32); +EXTERN_C SV * Plugin_named_buff_FETCH(pTHX_ REGEXP * const, SV * const, + const U32); +EXTERN_C SV * Plugin_package(pTHX_ REGEXP * const); +#ifdef USE_ITHREADS +EXTERN_C void * Plugin_dupe(pTHX_ REGEXP * const, CLONE_PARAMS *); +#endif +END_EXTERN_C + +START_EXTERN_C +EXTERN_C const regexp_engine engine_plugin; +END_EXTERN_C + +#define RE_ENGINE_PLUGIN (&engine_plugin) +const regexp_engine engine_plugin = { + Plugin_comp, + Plugin_exec, + Plugin_intuit, + Plugin_checkstr, + Plugin_free, + Plugin_numbered_buff_FETCH, + Plugin_numbered_buff_STORE, + Plugin_numbered_buff_LENGTH, + Plugin_named_buff_FETCH, + Plugin_package, +#if defined(USE_ITHREADS) + Plugin_dupe, +#endif +}; + +typedef struct replug { + /* Pointer back to the containing REGEXP struct so that accessors + * can modify nparens, gofs etc. */ + REGEXP * rx; + + /* A copy of the pattern given to comp, for ->pattern */ + SV * pattern; + + /* A copy of the string being matched against, for ->str */ + SV * str; + + /* The ->stash */ + SV * stash; + + /* + * Callbacks + */ + + SV * cb_free; + + /* ->num_captures */ + SV * cb_num_capture_buff_FETCH; + SV * cb_num_capture_buff_STORE; + SV * cb_num_capture_buff_LENGTH; +} *re__engine__Plugin; diff --git a/Plugin.pm b/Plugin.pm new file mode 100644 index 0000000..220dfdf --- /dev/null +++ b/Plugin.pm @@ -0,0 +1,86 @@ +# See Plugin.pod for documentation +package re::engine::Plugin; +use 5.009005; +use base 'Regexp'; +use strict; +use XSLoader (); + +our $VERSION = '0.03'; + +XSLoader::load __PACKAGE__, $VERSION; + +my $RE_ENGINE_PLUGIN = ENGINE(); + +# How many? Used to cheat %^H +my $callback = 1; + +# Where we store our CODE refs +my %callback; + +# Generate a key to use in the %^H hash from a string, prefix the +# package name like L does +my $key = sub { __PACKAGE__ . "::" . $_[0] }; + +sub import +{ + my ($pkg, %sub) = @_; + + # Valid callbacks + my @callback = qw(comp exec intuit checkstr free dupe); + + for (@callback) { + next unless exists $sub{$_}; + my $cb = delete $sub{$_}; + + unless (ref $cb eq 'CODE') { + require Carp; + Carp::croak("'$_' is not CODE"); + } + + # Get an ID to use + my $id = $callback ++; + + # Insert into our callback storage, + $callback{$_}->{$id} = $cb; + + # Instert into our cache with a key we can retrive later + # knowing the ID in %^H and what callback we're getting + $^H{ $key->($_) } = $id; + } + + $^H{regcomp} = $RE_ENGINE_PLUGIN; +} + +sub unimport +{ + # Delete the regcomp hook + delete $^H{regcomp} + if $^H{regcomp} == $RE_ENGINE_PLUGIN; +} + +# Minimal function to get CODE for a given key to be called by the +# get_H_callback C function. +sub _get_callback +{ + my ($name) = @_; # 'comp', 'exec', ... + + my $h = (caller(0))[10]; + my $id = $h->{ $key->($name) }; + + my $cb = defined $id ? $callback{$name}->{$id} : 0; + + return $cb; +} + +sub num_captures +{ + my ($re, %callback) = @_; + + for my $key (keys %callback) { + $key =~ y/a-z/A-Z/; # ASCII uc + my $name = '_num_capture_buff_' . $key; + $re->$name( $callback{$key} ); + } +} + +1; diff --git a/Plugin.pod b/Plugin.pod new file mode 100644 index 0000000..3ae9882 --- /dev/null +++ b/Plugin.pod @@ -0,0 +1,405 @@ +=head1 NAME + +re::engine::Plugin - API to write custom regex engines + +=head1 DESCRIPTION + +As of perl 5.9.5 it's possible to lexically replace perl's built-in +regular expression engine with your own (see L and +L). This module provides a glue interface to the relevant +parts of the perl C API enabling you to write an engine in Perl +instead of the C/XS interface provided by the core. + +=head2 The gory details + +Each regex in perl is compiled into an internal C structure +(see L), this can happen +either during compile time in the case of patterns in the format +C or runtime for C patterns, or something inbetween +depending on variable interpolation etc. + +When this module is loaded into a scope it inserts a hook into +C<$^H{regcomp}> (as described in L) to have each regexp +constructed in its lexical scope handled by this engine, but it +differs from other engines in that it also inserts other hooks into +C<%^H> in the same scope that point to user-defined subroutines to use +during compilation, execution etc, these are described in +L below. + +The callbacks (e.g. L) then get called with a +L object as their first argument. This object +provies access to perl's internal REGEXP struct in addition to its own +state (e.g. a L). The L on this object +allow for altering the C struct's internal state, adding new +callbacks, etc. + +=head1 CALLBACKS + +Callbacks are specified in the C import list as +key-value pairs of names and subroutine references: + + use re::engine::Plugin ( + comp => sub {}, + exec => sub {}, + ); + +To write a custom engine which imports your functions into the +caller's scope use use the following snippet: + + package re::engine::Example; + use re::engine::Plugin (); + + sub import + { + # Populates the caller's %^H with our callbacks + re::engine::Plugin->import( + comp => \&comp, + exec => \&exec, + ); + } + + *unimport = \&re::engine::Plugin::unimport; + + # Implementation of the engine + sub comp { ... } + sub exec { ... } + + 1; + +=head2 comp + + comp => sub { + my ($rx) = @_; + + # return value discarded + } + +Called when a regex is compiled by perl, this is always the first +callback to be called and may be called multiple times or not at all +depending on what perl sees fit at the time. + +The first argument will be a freshly constructed C +object (think of it as C<$self>) which you can interact with using the +L below, this object will be passed around the other +L and L for the lifetime of +the regex. + +Calling C or anything that uses it (such as C) here will +not be trapped by an C block that the pattern is in, i.e. + + use Carp 'croak'; + use re::engine::Plugin( + comp => sub { + my $rx = shift; + croak "Your pattern is invalid" + unless $rx->pattern ~~ /pony/; + } + ); + + # Ignores the eval block + eval { /you die in C, you die for real/ }; + +This happens because the real subroutine call happens indirectly at +compile time and not in the scope of the C block. This is how +perl's own engine would behave in the same situation if given an +invalid pattern such as C. + +=head2 exec + + exec => sub { + my ($rx, $str) = @_; + + # We always like ponies! + return 1 if $str ~~ /pony/; + + # Failed to match + return; + } + +Called when a regex is being executed, i.e. when it's being matched +against something. The scalar being matched against the pattern is +available as the second argument (C<$str>) and through the L +method. The routine should return a true value if the match was +successful, and a false one if it wasn't. + +=head1 METHODS + +=head2 str + + "str" ~~ /pattern/; + # in comp/exec/methods: + my $str = $rx->str; + +The last scalar to be matched against the L or +C if there hasn't been a match yet. + +perl's own engine always stringifies the scalar being matched against +a given pattern, however a custom engine need not have such +restrictions. One could write a engine that matched a file handle +against a pattern or any other complex data structure. + +=head2 pattern + +The pattern that the engine was asked to compile, this can be either a +classic Perl pattern with modifiers like C or C or +an arbitary scalar. The latter allows for passing anything that +doesn't fit in a string and five L characters, such as +hashrefs, objects, etc. + +=head2 mod + + my %mod = $rx->mod; + say "has /ix" if %mod ~~ 'i' and %mod ~~ 'x'; + +A key-value pair list of the modifiers the pattern was compiled with. +The keys will zero or more of C and the values will be true +values (so that you don't have to write C). + +You don't get to know if the C modifiers were attached to the +pattern since these are internal to perl and shouldn't matter to +regexp engines. + +=head2 stash + + comp => sub { shift->stash( [ 1 .. 5 ) }, + exec => sub { shift->stash }, # Get [ 1 .. 5 ] + +Returns or sets a user defined stash that's passed around as part of +the C<$rx> object, useful for passing around all sorts of data between +the callback routines and methods. + +=head2 minlen + + $rx->minlen($num); + my $minlen = $rx->minlen // "not set"; + +The minimum C a string must be to match the pattern, perl will +use this internally during matching to check whether the stringified +form of the string (or other object) being matched is at least this +long, if not the regexp engine in effect (that means you!) will not be +called at all. + +The length specified will be used as a a byte length (using +L), not a character length. + +=head2 num_captures + + $re->num_captures( + FETCH => sub { + my ($re, $paren) = @_; + + return "value"; + }, + STORE => sub { + my ($re, $paren, $rhs) = @_; + + # return value discarded + }, + LENGTH => sub { + my ($re, $paren) = @_; + + return 123; + }, + ); + +Takes a list of key-value pairs of names and subroutines that +implement numbered capture variables. C will be called on value +retrieval (C), C on assignment (C<$1 = "ook">) and +C on C. + +The second paramater of each routine is the paren number being +requested/stored, the following mapping applies for those numbers: + + -2 => $` or ${^PREMATCH} + -1 => $' or ${^POSTMATCH} + 0 => $& or ${^MATCH} + 1 => $1 + # ... + +Assignment to capture variables makes it possible to implement +something like Perl 6 C<:rw> semantics, and since it's possible to +make the capture variables return any scalar instead of just a string +it becomes possible to implement Perl 6 match object semantics (to +name an example). + +=head2 named_captures + +B: implement + +perl internals still needs to be changed to support this but when it's +done it'll allow the binding of C<%+> and C<%-> and support the +L methods FETCH, STORE, DELETE, CLEAR, EXISTS, FIRSTKEY, +NEXTKEY and SCALAR. + +=head1 Tainting + +The only way to untaint an existing variable in Perl is to use it as a +hash key or referencing subpatterns from a regular expression match +(see L), the +latter only works in perl's regex engine because it explicitly +untaints capture variables which a custom engine will also need to do +if it wants its capture variables to be untanted. + +There are basically two ways to go about this, the first and obvious +one is to make use of Perl'l lexical scoping which enables the use of +its built-in regex engine in the scope of the overriding engine's +callbacks: + + use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; # $str is tainted + + $re->num_captures( + FETCH => sub { + my ($re, $paren) = @_; + + # This is perl's engine doing the match + $str ~~ /(.*)/; + + # $1 has been untainted + return $1; + }, + ); + }, + ); + +The second is to use something like L which flips the +taint flag on the scalar without invoking the perl's regex engine: + + use Taint::Util; + use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; # $str is tainted + + $re->num_captures( + FETCH => sub { + my ($re, $paren) = @_; + + # Copy $str and untaint the copy + untaint(my $ret = $str); + + # Return the untainted value + return $ret; + }, + ); + }, + ); + +In either case a regex engine using perl's L or +this module is responsible for how and if it untaints its variables. + +=head1 SEE ALSO + +L, L + +=head1 TODO / CAVEATS + +I + +=over + +=item * + +Engines implemented with this module don't support C and C, the appropriate parts of the C struct need to be wrapped +and documented. + +=item * + +Still not a complete wrapper for L in other ways, needs +methods for some C struct members, some callbacks aren't +implemented etc. + +=item * + +Support overloading operations on the C object, this allow +control over the of C objects in a manner that isn't limited by +C/C. + + $re->overload( + '""' => sub { ... }, + '@{}' => sub { ... }, + ... + ); + +=item * + +Support the dispatch of arbitary methods from the re::engine::Plugin +qr// object to user defined subroutines via AUTOLOAD; + + package re::engine::Plugin; + sub AUTOLOAD + { + our $AUTOLOAD; + my ($name) = $AUTOLOAD =~ /.*::(.*?)/; + my $cv = getmeth($name); # or something like that + goto &$cv; + } + + package re::engine::SomeEngine; + + sub comp + { + my $re = shift; + + $re->add_method( # or something like that + foshizzle => sub { + my ($re, @arg) = @_; # re::engine::Plugin, 1..5 + }, + ); + } + + package main; + use re::engine::SomeEngine; + later: + + my $re = qr//; + $re->foshizzle(1..5); + +=item * + +Implement the dupe callback, test this on a threaded perl (and learn +how to use threads and how they break the current model). + +=item * + +Allow the user to specify ->offs either as an array or a packed +string. Can pack() even pack I32? Only IV? int? + +=item * + +Add tests that check for different behavior when curpm is and is not +set. + +=item * + +Add tests that check the refcount of the stash and other things I'm +mucking with, run valgrind and make sure everything is destroyed when +it should. + +=item * + +Run the debugger on the testsuite and find cases when the intuit and +checkstr callbacks are called. Write wrappers around them and add +tests. + +=back + +=head1 BUGS + +Please report any bugs that aren't already listed at +L to +L + +=head1 AUTHOR + +Evar ArnfjErE Bjarmason + +=head1 LICENSE + +Copyright 2007 Evar ArnfjErE Bjarmason. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut diff --git a/Plugin.xs b/Plugin.xs new file mode 100644 index 0000000..aa95e8b --- /dev/null +++ b/Plugin.xs @@ -0,0 +1,469 @@ +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#include "Plugin.h" + +SV* +get_H_callback(const char* key) +{ + dVAR; + dSP; + + SV * callback; + + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(sv_2mortal(newSVpv(key, 0))); + PUTBACK; + + call_pv("re::engine::Plugin::_get_callback", G_SCALAR); + + SPAGAIN; + + callback = POPs; + SvREFCNT_inc(callback); /* refcount++ or FREETMPS below will collect us */ + + /* If we don't get a valid CODE value return a NULL callback, in + * that case the hooks won't call back into Perl space */ + if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) { + callback = NULL; + } + + PUTBACK; + FREETMPS; + LEAVE; + + return callback; +} + +REGEXP * +Plugin_comp(pTHX_ const SV * const pattern, const U32 flags) +{ + dSP; + REGEXP * rx; + re__engine__Plugin re; + I32 count; + I32 buffers; + + /* exp/xend version of the pattern & length */ + STRLEN plen; + char* exp = SvPV((SV*)pattern, plen); + char* xend = exp + plen; + + /* The REGEXP structure to return to perl */ + Newxz(rx, 1, REGEXP); + + /* Our blessed object */ + SV *obj = newSV(0); + SvREFCNT_inc(obj); + Newxz(re, 1, struct replug); + sv_setref_pv(obj, "re::engine::Plugin", (void*)re); + + re->rx = rx; /* Make the rx accessible from self->rx */ + rx->refcnt = 1; /* Refcount so we won' be destroyed */ + rx->intflags = flags; /* Flags for internal use */ + rx->extflags = flags; /* Flags for perl to use */ + rx->engine = RE_ENGINE_PLUGIN; /* Compile to use this engine */ + + /* Store a precompiled regexp for pp_regcomp to use */ + rx->prelen = plen; + rx->precomp = savepvn(exp, rx->prelen); + + /* Set up qr// stringification to be equivalent to the supplied + * pattern, this should be done via overload eventually. + */ + rx->wraplen = rx->prelen; + Newx(rx->wrapped, rx->wraplen, char); + Copy(rx->precomp, rx->wrapped, rx->wraplen, char); + + /* Store our private object */ + rx->pprivate = obj; + + /* Store the pattern for ->pattern */ + re->pattern = (SV*)pattern; + SvREFCNT_inc(re->pattern); + + /* + * Call our callback function if one was defined, if not we've + * already set up all the stuff we're going to to need for + * subsequent exec and other calls + */ + SV * callback = get_H_callback("comp"); + + if (callback) { + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(obj); + PUTBACK; + + call_sv(callback, G_DISCARD); + + FREETMPS; + LEAVE; + } + + /* If any of the comp-time accessors were called we'll have to + * update the regexp struct with the new info. + */ + + buffers = rx->nparens; + + Newxz(rx->offs, buffers, regexp_paren_pair); + + return rx; +} + +I32 +Plugin_exec(pTHX_ REGEXP * const rx, char *stringarg, char *strend, + char *strbeg, I32 minend, SV *sv, void *data, U32 flags) +{ + dSP; + I32 matched; + SV * callback = get_H_callback("exec"); + GET_SELF_FROM_PPRIVATE(rx->pprivate); + + if (callback) { + /* Store the current str for ->str */ + self->str = (SV*)sv; + SvREFCNT_inc(self->str); + + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(rx->pprivate); + XPUSHs(sv); + PUTBACK; + + call_sv(callback, G_SCALAR); + + SPAGAIN; + + SV * ret = POPs; + + if (SvTRUE(ret)) + matched = 1; + else + matched = 0; + + PUTBACK; + FREETMPS; + LEAVE; + } else { + matched = 0; + } + + return matched; +} + +char * +Plugin_intuit(pTHX_ REGEXP * const rx, SV *sv, char *strpos, + char *strend, U32 flags, re_scream_pos_data *data) +{ + PERL_UNUSED_ARG(rx); + PERL_UNUSED_ARG(sv); + PERL_UNUSED_ARG(strpos); + PERL_UNUSED_ARG(strend); + PERL_UNUSED_ARG(flags); + PERL_UNUSED_ARG(data); + return NULL; +} + +SV * +Plugin_checkstr(pTHX_ REGEXP * const rx) +{ + PERL_UNUSED_ARG(rx); + return NULL; +} + +void +Plugin_free(pTHX_ REGEXP * const rx) +{ + PERL_UNUSED_ARG(rx); +/* + dSP; + SV * callback; + GET_SELF_FROM_PPRIVATE(rx->pprivate); + + callback = self->cb_free; + + if (callback) { + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(rx->pprivate); + PUTBACK; + + call_sv(callback, G_DISCARD); + + PUTBACK; + FREETMPS; + LEAVE; + } + return; +*/ +} + +void * +Plugin_dupe(pTHX_ const REGEXP * rx, CLONE_PARAMS *param) +{ + Perl_croak("dupe not supported yet"); + return rx->pprivate; +} + + +void +Plugin_numbered_buff_FETCH(pTHX_ REGEXP * const rx, const I32 paren, + SV * const sv) +{ + dSP; + I32 items; + SV * callback; + GET_SELF_FROM_PPRIVATE(rx->pprivate); + + callback = self->cb_num_capture_buff_FETCH; + + if (callback) { + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(rx->pprivate); + XPUSHs(sv_2mortal(newSViv(paren))); + PUTBACK; + + items = call_sv(callback, G_SCALAR); + + if (items == 1) { + SPAGAIN; + + SV * ret = POPs; + sv_setsv(sv, ret); + } else { + sv_setsv(sv, &PL_sv_undef); + } + + PUTBACK; + FREETMPS; + LEAVE; + } else { + sv_setsv(sv, &PL_sv_undef); + } +} + +void +Plugin_numbered_buff_STORE(pTHX_ REGEXP * const rx, const I32 paren, + SV const * const value) +{ + dSP; + I32 items; + SV * callback; + GET_SELF_FROM_PPRIVATE(rx->pprivate); + + callback = self->cb_num_capture_buff_STORE; + + if (callback) { + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(rx->pprivate); + XPUSHs(sv_2mortal(newSViv(paren))); + XPUSHs(SvREFCNT_inc(value)); + PUTBACK; + + call_sv(callback, G_DISCARD); + + PUTBACK; + FREETMPS; + LEAVE; + } +} + +I32 +Plugin_numbered_buff_LENGTH(pTHX_ REGEXP * const rx, const SV * const sv, + const I32 paren) +{ + dSP; + I32 items; + SV * callback; + re__engine__Plugin self; + + SELF_FROM_PPRIVATE(self,rx->pprivate); + + callback = self->cb_num_capture_buff_LENGTH; + + if (callback) { + ENTER; + SAVETMPS; + + PUSHMARK(SP); + XPUSHs(rx->pprivate); + XPUSHs(sv_2mortal(newSViv(paren))); + PUTBACK; + + call_sv(callback, G_SCALAR); + + SPAGAIN; + + IV ret = POPi; + + PUTBACK; + FREETMPS; + LEAVE; + + return (I32)ret; + } else { + /* TODO: call FETCH and get the length on that value */ + return 0; + } +} + + +SV* +Plugin_named_buff_FETCH(pTHX_ REGEXP * const rx, SV * const key, U32 flags) +{ + PERL_UNUSED_ARG(rx); + PERL_UNUSED_ARG(key); + PERL_UNUSED_ARG(flags); + + return NULL; +} + +SV* +Plugin_package(pTHX_ REGEXP * const rx) +{ + PERL_UNUSED_ARG(rx); + return newSVpvs("re::engine::Plugin"); +} + +MODULE = re::engine::Plugin PACKAGE = re::engine::Plugin +PROTOTYPES: ENABLE + +SV * +pattern(re::engine::Plugin self, ...) +CODE: + SvREFCNT_inc(self->pattern); + RETVAL = self->pattern; +OUTPUT: + RETVAL + +SV * +str(re::engine::Plugin self, ...) +CODE: + SvREFCNT_inc(self->str); + RETVAL = self->str; +OUTPUT: + RETVAL + +char* +mod(re::engine::Plugin self, ...) +PPCODE: + /* /i */ + if (self->rx->intflags & PMf_FOLD) { + XPUSHs(sv_2mortal(newSVpvs("i"))); + XPUSHs(&PL_sv_yes); + } + + /* /m */ + if (self->rx->intflags & PMf_MULTILINE) { + XPUSHs(sv_2mortal(newSVpvs("m"))); + XPUSHs(&PL_sv_yes); + } + + /* /s */ + if (self->rx->intflags & PMf_SINGLELINE) { + XPUSHs(sv_2mortal(newSVpvs("s"))); + XPUSHs(&PL_sv_yes); + } + + /* /x */ + if (self->rx->intflags & PMf_EXTENDED) { + XPUSHs(sv_2mortal(newSVpvs("x"))); + XPUSHs(&PL_sv_yes); + } + + /* /p */ + if (self->rx->intflags & RXf_PMf_KEEPCOPY) { + XPUSHs(sv_2mortal(newSVpvs("p"))); + XPUSHs(&PL_sv_yes); + } + +SV * +stash(re::engine::Plugin self, ...) +PREINIT: + SV * stash; +CODE: + if (items > 1) { + self->stash = sv_mortalcopy(ST(1)); + SvREFCNT_inc(self->stash); + } + SvREFCNT_inc(self->stash); + RETVAL = self->stash; +OUTPUT: + RETVAL + +SV * +minlen(re::engine::Plugin self, ...) +CODE: + if (items > 1) { + self->rx->minlen = (I32)SvIV(ST(1)); + } + + RETVAL = self->rx->minlen ? newSViv(self->rx->minlen) : &PL_sv_undef; +OUTPUT: + RETVAL + +SV * +gofs(re::engine::Plugin self, ...) +CODE: + if (items > 1) { + self->rx->gofs = (U32)SvIV(ST(1)); + } + RETVAL = self->rx->gofs ? newSVuv(self->rx->gofs) : &PL_sv_undef; +OUTPUT: + RETVAL + +SV * +nparens(re::engine::Plugin self, ...) +CODE: + if (items > 1) { + self->rx->nparens = (U32)SvIV(ST(1)); + } + RETVAL = self->rx->nparens ? newSVuv(self->rx->nparens) : &PL_sv_undef; +OUTPUT: + RETVAL + +void +_num_capture_buff_FETCH(re::engine::Plugin self, ...) +PPCODE: + if (items > 1) { + self->cb_num_capture_buff_FETCH = ST(1); + SvREFCNT_inc(self->cb_num_capture_buff_FETCH); + } + +void +_num_capture_buff_STORE(re::engine::Plugin self, ...) +PPCODE: + if (items > 1) { + self->cb_num_capture_buff_STORE = ST(1); + SvREFCNT_inc(self->cb_num_capture_buff_STORE); + } + +void +_num_capture_buff_LENGTH(re::engine::Plugin self, ...) +PPCODE: + if (items > 1) { + self->cb_num_capture_buff_LENGTH = ST(1); + SvREFCNT_inc(self->cb_num_capture_buff_LENGTH); + } + +void +ENGINE() +PPCODE: + XPUSHs(sv_2mortal(newSViv(PTR2IV(&engine_plugin)))); diff --git a/inc/Module/AutoInstall.pm b/inc/Module/AutoInstall.pm new file mode 100644 index 0000000..7efc552 --- /dev/null +++ b/inc/Module/AutoInstall.pm @@ -0,0 +1,768 @@ +#line 1 +package Module::AutoInstall; + +use strict; +use Cwd (); +use ExtUtils::MakeMaker (); + +use vars qw{$VERSION}; +BEGIN { + $VERSION = '1.03'; +} + +# special map on pre-defined feature sets +my %FeatureMap = ( + '' => 'Core Features', # XXX: deprecated + '-core' => 'Core Features', +); + +# various lexical flags +my ( @Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS ); +my ( $Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly ); +my ( $PostambleActions, $PostambleUsed ); + +# See if it's a testing or non-interactive session +_accept_default( $ENV{AUTOMATED_TESTING} or ! -t STDIN ); +_init(); + +sub _accept_default { + $AcceptDefault = shift; +} + +sub missing_modules { + return @Missing; +} + +sub do_install { + __PACKAGE__->install( + [ + $Config + ? ( UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) + : () + ], + @Missing, + ); +} + +# initialize various flags, and/or perform install +sub _init { + foreach my $arg ( + @ARGV, + split( + /[\s\t]+/, + $ENV{PERL_AUTOINSTALL} || $ENV{PERL_EXTUTILS_AUTOINSTALL} || '' + ) + ) + { + if ( $arg =~ /^--config=(.*)$/ ) { + $Config = [ split( ',', $1 ) ]; + } + elsif ( $arg =~ /^--installdeps=(.*)$/ ) { + __PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) ); + exit 0; + } + elsif ( $arg =~ /^--default(?:deps)?$/ ) { + $AcceptDefault = 1; + } + elsif ( $arg =~ /^--check(?:deps)?$/ ) { + $CheckOnly = 1; + } + elsif ( $arg =~ /^--skip(?:deps)?$/ ) { + $SkipInstall = 1; + } + elsif ( $arg =~ /^--test(?:only)?$/ ) { + $TestOnly = 1; + } + } +} + +# overrides MakeMaker's prompt() to automatically accept the default choice +sub _prompt { + goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault; + + my ( $prompt, $default ) = @_; + my $y = ( $default =~ /^[Yy]/ ); + + print $prompt, ' [', ( $y ? 'Y' : 'y' ), '/', ( $y ? 'n' : 'N' ), '] '; + print "$default\n"; + return $default; +} + +# the workhorse +sub import { + my $class = shift; + my @args = @_ or return; + my $core_all; + + print "*** $class version " . $class->VERSION . "\n"; + print "*** Checking for Perl dependencies...\n"; + + my $cwd = Cwd::cwd(); + + $Config = []; + + my $maxlen = length( + ( + sort { length($b) <=> length($a) } + grep { /^[^\-]/ } + map { + ref($_) + ? ( ( ref($_) eq 'HASH' ) ? keys(%$_) : @{$_} ) + : '' + } + map { +{@args}->{$_} } + grep { /^[^\-]/ or /^-core$/i } keys %{ +{@args} } + )[0] + ); + + while ( my ( $feature, $modules ) = splice( @args, 0, 2 ) ) { + my ( @required, @tests, @skiptests ); + my $default = 1; + my $conflict = 0; + + if ( $feature =~ m/^-(\w+)$/ ) { + my $option = lc($1); + + # check for a newer version of myself + _update_to( $modules, @_ ) and return if $option eq 'version'; + + # sets CPAN configuration options + $Config = $modules if $option eq 'config'; + + # promote every features to core status + $core_all = ( $modules =~ /^all$/i ) and next + if $option eq 'core'; + + next unless $option eq 'core'; + } + + print "[" . ( $FeatureMap{ lc($feature) } || $feature ) . "]\n"; + + $modules = [ %{$modules} ] if UNIVERSAL::isa( $modules, 'HASH' ); + + unshift @$modules, -default => &{ shift(@$modules) } + if ( ref( $modules->[0] ) eq 'CODE' ); # XXX: bugward combatability + + while ( my ( $mod, $arg ) = splice( @$modules, 0, 2 ) ) { + if ( $mod =~ m/^-(\w+)$/ ) { + my $option = lc($1); + + $default = $arg if ( $option eq 'default' ); + $conflict = $arg if ( $option eq 'conflict' ); + @tests = @{$arg} if ( $option eq 'tests' ); + @skiptests = @{$arg} if ( $option eq 'skiptests' ); + + next; + } + + printf( "- %-${maxlen}s ...", $mod ); + + if ( $arg and $arg =~ /^\D/ ) { + unshift @$modules, $arg; + $arg = 0; + } + + # XXX: check for conflicts and uninstalls(!) them. + if ( + defined( my $cur = _version_check( _load($mod), $arg ||= 0 ) ) ) + { + print "loaded. ($cur" . ( $arg ? " >= $arg" : '' ) . ")\n"; + push @Existing, $mod => $arg; + $DisabledTests{$_} = 1 for map { glob($_) } @skiptests; + } + else { + print "missing." . ( $arg ? " (would need $arg)" : '' ) . "\n"; + push @required, $mod => $arg; + } + } + + next unless @required; + + my $mandatory = ( $feature eq '-core' or $core_all ); + + if ( + !$SkipInstall + and ( + $CheckOnly + or _prompt( + qq{==> Auto-install the } + . ( @required / 2 ) + . ( $mandatory ? ' mandatory' : ' optional' ) + . qq{ module(s) from CPAN?}, + $default ? 'y' : 'n', + ) =~ /^[Yy]/ + ) + ) + { + push( @Missing, @required ); + $DisabledTests{$_} = 1 for map { glob($_) } @skiptests; + } + + elsif ( !$SkipInstall + and $default + and $mandatory + and + _prompt( qq{==> The module(s) are mandatory! Really skip?}, 'n', ) + =~ /^[Nn]/ ) + { + push( @Missing, @required ); + $DisabledTests{$_} = 1 for map { glob($_) } @skiptests; + } + + else { + $DisabledTests{$_} = 1 for map { glob($_) } @tests; + } + } + + $UnderCPAN = _check_lock(); # check for $UnderCPAN + + if ( @Missing and not( $CheckOnly or $UnderCPAN ) ) { + require Config; + print +"*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n"; + + # make an educated guess of whether we'll need root permission. + print " (You may need to do that as the 'root' user.)\n" + if eval '$>'; + } + print "*** $class configuration finished.\n"; + + chdir $cwd; + + # import to main:: + no strict 'refs'; + *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main'; +} + +# Check to see if we are currently running under CPAN.pm and/or CPANPLUS; +# if we are, then we simply let it taking care of our dependencies +sub _check_lock { + return unless @Missing; + + if ($ENV{PERL5_CPANPLUS_IS_RUNNING}) { + print <<'END_MESSAGE'; + +*** Since we're running under CPANPLUS, I'll just let it take care + of the dependency's installation later. +END_MESSAGE + return 1; + } + + _load_cpan(); + + # Find the CPAN lock-file + my $lock = MM->catfile( $CPAN::Config->{cpan_home}, ".lock" ); + return unless -f $lock; + + # Check the lock + local *LOCK; + return unless open(LOCK, $lock); + + if ( + ( $^O eq 'MSWin32' ? _under_cpan() : == getppid() ) + and ( $CPAN::Config->{prerequisites_policy} || '' ) ne 'ignore' + ) { + print <<'END_MESSAGE'; + +*** Since we're running under CPAN, I'll just let it take care + of the dependency's installation later. +END_MESSAGE + return 1; + } + + close LOCK; + return; +} + +sub install { + my $class = shift; + + my $i; # used below to strip leading '-' from config keys + my @config = ( map { s/^-// if ++$i; $_ } @{ +shift } ); + + my ( @modules, @installed ); + while ( my ( $pkg, $ver ) = splice( @_, 0, 2 ) ) { + + # grep out those already installed + if ( defined( _version_check( _load($pkg), $ver ) ) ) { + push @installed, $pkg; + } + else { + push @modules, $pkg, $ver; + } + } + + return @installed unless @modules; # nothing to do + return @installed if _check_lock(); # defer to the CPAN shell + + print "*** Installing dependencies...\n"; + + return unless _connected_to('cpan.org'); + + my %args = @config; + my %failed; + local *FAILED; + if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) { + while () { chomp; $failed{$_}++ } + close FAILED; + + my @newmod; + while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) { + push @newmod, ( $k => $v ) unless $failed{$k}; + } + @modules = @newmod; + } + + if ( _has_cpanplus() ) { + _install_cpanplus( \@modules, \@config ); + } else { + _install_cpan( \@modules, \@config ); + } + + print "*** $class installation finished.\n"; + + # see if we have successfully installed them + while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { + if ( defined( _version_check( _load($pkg), $ver ) ) ) { + push @installed, $pkg; + } + elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) { + print FAILED "$pkg\n"; + } + } + + close FAILED if $args{do_once}; + + return @installed; +} + +sub _install_cpanplus { + my @modules = @{ +shift }; + my @config = _cpanplus_config( @{ +shift } ); + my $installed = 0; + + require CPANPLUS::Backend; + my $cp = CPANPLUS::Backend->new; + my $conf = $cp->configure_object; + + return unless $conf->can('conf') # 0.05x+ with "sudo" support + or _can_write($conf->_get_build('base')); # 0.04x + + # if we're root, set UNINST=1 to avoid trouble unless user asked for it. + my $makeflags = $conf->get_conf('makeflags') || ''; + if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) { + # 0.03+ uses a hashref here + $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST}; + + } else { + # 0.02 and below uses a scalar + $makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' ) + if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } ); + + } + $conf->set_conf( makeflags => $makeflags ); + $conf->set_conf( prereqs => 1 ); + + + + while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) { + $conf->set_conf( $key, $val ); + } + + my $modtree = $cp->module_tree; + while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { + print "*** Installing $pkg...\n"; + + MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall; + + my $success; + my $obj = $modtree->{$pkg}; + + if ( $obj and defined( _version_check( $obj->{version}, $ver ) ) ) { + my $pathname = $pkg; + $pathname =~ s/::/\\W/; + + foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) { + delete $INC{$inc}; + } + + my $rv = $cp->install( modules => [ $obj->{module} ] ); + + if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) { + print "*** $pkg successfully installed.\n"; + $success = 1; + } else { + print "*** $pkg installation cancelled.\n"; + $success = 0; + } + + $installed += $success; + } else { + print << "."; +*** Could not find a version $ver or above for $pkg; skipping. +. + } + + MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall; + } + + return $installed; +} + +sub _cpanplus_config { + my @config = (); + while ( @_ ) { + my ($key, $value) = (shift(), shift()); + if ( $key eq 'prerequisites_policy' ) { + if ( $value eq 'follow' ) { + $value = CPANPLUS::Internals::Constants::PREREQ_INSTALL(); + } elsif ( $value eq 'ask' ) { + $value = CPANPLUS::Internals::Constants::PREREQ_ASK(); + } elsif ( $value eq 'ignore' ) { + $value = CPANPLUS::Internals::Constants::PREREQ_IGNORE(); + } else { + die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n"; + } + } else { + die "*** Cannot convert option $key to CPANPLUS version.\n"; + } + } + return @config; +} + +sub _install_cpan { + my @modules = @{ +shift }; + my @config = @{ +shift }; + my $installed = 0; + my %args; + + _load_cpan(); + require Config; + + if (CPAN->VERSION < 1.80) { + # no "sudo" support, probe for writableness + return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) ) + and _can_write( $Config::Config{sitelib} ); + } + + # if we're root, set UNINST=1 to avoid trouble unless user asked for it. + my $makeflags = $CPAN::Config->{make_install_arg} || ''; + $CPAN::Config->{make_install_arg} = + join( ' ', split( ' ', $makeflags ), 'UNINST=1' ) + if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } ); + + # don't show start-up info + $CPAN::Config->{inhibit_startup_message} = 1; + + # set additional options + while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) { + ( $args{$opt} = $arg, next ) + if $opt =~ /^force$/; # pseudo-option + $CPAN::Config->{$opt} = $arg; + } + + local $CPAN::Config->{prerequisites_policy} = 'follow'; + + while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { + MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall; + + print "*** Installing $pkg...\n"; + + my $obj = CPAN::Shell->expand( Module => $pkg ); + my $success = 0; + + if ( $obj and defined( _version_check( $obj->cpan_version, $ver ) ) ) { + my $pathname = $pkg; + $pathname =~ s/::/\\W/; + + foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) { + delete $INC{$inc}; + } + + my $rv = $args{force} ? CPAN::Shell->force( install => $pkg ) + : CPAN::Shell->install($pkg); + $rv ||= eval { + $CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, ) + ->{install} + if $CPAN::META; + }; + + if ( $rv eq 'YES' ) { + print "*** $pkg successfully installed.\n"; + $success = 1; + } + else { + print "*** $pkg installation failed.\n"; + $success = 0; + } + + $installed += $success; + } + else { + print << "."; +*** Could not find a version $ver or above for $pkg; skipping. +. + } + + MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall; + } + + return $installed; +} + +sub _has_cpanplus { + return ( + $HasCPANPLUS = ( + $INC{'CPANPLUS/Config.pm'} + or _load('CPANPLUS::Shell::Default') + ) + ); +} + +# make guesses on whether we're under the CPAN installation directory +sub _under_cpan { + require Cwd; + require File::Spec; + + my $cwd = File::Spec->canonpath( Cwd::cwd() ); + my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} ); + + return ( index( $cwd, $cpan ) > -1 ); +} + +sub _update_to { + my $class = __PACKAGE__; + my $ver = shift; + + return + if defined( _version_check( _load($class), $ver ) ); # no need to upgrade + + if ( + _prompt( "==> A newer version of $class ($ver) is required. Install?", + 'y' ) =~ /^[Nn]/ + ) + { + die "*** Please install $class $ver manually.\n"; + } + + print << "."; +*** Trying to fetch it from CPAN... +. + + # install ourselves + _load($class) and return $class->import(@_) + if $class->install( [], $class, $ver ); + + print << '.'; exit 1; + +*** Cannot bootstrap myself. :-( Installation terminated. +. +} + +# check if we're connected to some host, using inet_aton +sub _connected_to { + my $site = shift; + + return ( + ( _load('Socket') and Socket::inet_aton($site) ) or _prompt( + qq( +*** Your host cannot resolve the domain name '$site', which + probably means the Internet connections are unavailable. +==> Should we try to install the required module(s) anyway?), 'n' + ) =~ /^[Yy]/ + ); +} + +# check if a directory is writable; may create it on demand +sub _can_write { + my $path = shift; + mkdir( $path, 0755 ) unless -e $path; + + return 1 if -w $path; + + print << "."; +*** You are not allowed to write to the directory '$path'; + the installation may fail due to insufficient permissions. +. + + if ( + eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt( + qq( +==> Should we try to re-execute the autoinstall process with 'sudo'?), + ((-t STDIN) ? 'y' : 'n') + ) =~ /^[Yy]/ + ) + { + + # try to bootstrap ourselves from sudo + print << "."; +*** Trying to re-execute the autoinstall process with 'sudo'... +. + my $missing = join( ',', @Missing ); + my $config = join( ',', + UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) + if $Config; + + return + unless system( 'sudo', $^X, $0, "--config=$config", + "--installdeps=$missing" ); + + print << "."; +*** The 'sudo' command exited with error! Resuming... +. + } + + return _prompt( + qq( +==> Should we try to install the required module(s) anyway?), 'n' + ) =~ /^[Yy]/; +} + +# load a module and return the version it reports +sub _load { + my $mod = pop; # class/instance doesn't matter + my $file = $mod; + + $file =~ s|::|/|g; + $file .= '.pm'; + + local $@; + return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 ); +} + +# Load CPAN.pm and it's configuration +sub _load_cpan { + return if $CPAN::VERSION; + require CPAN; + if ( $CPAN::HandleConfig::VERSION ) { + # Newer versions of CPAN have a HandleConfig module + CPAN::HandleConfig->load; + } else { + # Older versions had the load method in Config directly + CPAN::Config->load; + } +} + +# compare two versions, either use Sort::Versions or plain comparison +sub _version_check { + my ( $cur, $min ) = @_; + return unless defined $cur; + + $cur =~ s/\s+$//; + + # check for version numbers that are not in decimal format + if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) { + if ( ( $version::VERSION or defined( _load('version') )) and + version->can('new') + ) { + + # use version.pm if it is installed. + return ( + ( version->new($cur) >= version->new($min) ) ? $cur : undef ); + } + elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) ) + { + + # use Sort::Versions as the sorting algorithm for a.b.c versions + return ( ( Sort::Versions::versioncmp( $cur, $min ) != -1 ) + ? $cur + : undef ); + } + + warn "Cannot reliably compare non-decimal formatted versions.\n" + . "Please install version.pm or Sort::Versions.\n"; + } + + # plain comparison + local $^W = 0; # shuts off 'not numeric' bugs + return ( $cur >= $min ? $cur : undef ); +} + +# nothing; this usage is deprecated. +sub main::PREREQ_PM { return {}; } + +sub _make_args { + my %args = @_; + + $args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing } + if $UnderCPAN or $TestOnly; + + if ( $args{EXE_FILES} and -e 'MANIFEST' ) { + require ExtUtils::Manifest; + my $manifest = ExtUtils::Manifest::maniread('MANIFEST'); + + $args{EXE_FILES} = + [ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ]; + } + + $args{test}{TESTS} ||= 't/*.t'; + $args{test}{TESTS} = join( ' ', + grep { !exists( $DisabledTests{$_} ) } + map { glob($_) } split( /\s+/, $args{test}{TESTS} ) ); + + my $missing = join( ',', @Missing ); + my $config = + join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) + if $Config; + + $PostambleActions = ( + $missing + ? "\$(PERL) $0 --config=$config --installdeps=$missing" + : "\$(NOECHO) \$(NOOP)" + ); + + return %args; +} + +# a wrapper to ExtUtils::MakeMaker::WriteMakefile +sub Write { + require Carp; + Carp::croak "WriteMakefile: Need even number of args" if @_ % 2; + + if ($CheckOnly) { + print << "."; +*** Makefile not written in check-only mode. +. + return; + } + + my %args = _make_args(@_); + + no strict 'refs'; + + $PostambleUsed = 0; + local *MY::postamble = \&postamble unless defined &MY::postamble; + ExtUtils::MakeMaker::WriteMakefile(%args); + + print << "." unless $PostambleUsed; +*** WARNING: Makefile written with customized MY::postamble() without + including contents from Module::AutoInstall::postamble() -- + auto installation features disabled. Please contact the author. +. + + return 1; +} + +sub postamble { + $PostambleUsed = 1; + + return << "."; + +config :: installdeps +\t\$(NOECHO) \$(NOOP) + +checkdeps :: +\t\$(PERL) $0 --checkdeps + +installdeps :: +\t$PostambleActions + +. + +} + +1; + +__END__ + +#line 1003 diff --git a/inc/Module/Install.pm b/inc/Module/Install.pm new file mode 100644 index 0000000..af6a59c --- /dev/null +++ b/inc/Module/Install.pm @@ -0,0 +1,281 @@ +#line 1 +package Module::Install; + +# For any maintainers: +# The load order for Module::Install is a bit magic. +# It goes something like this... +# +# IF ( host has Module::Install installed, creating author mode ) { +# 1. Makefile.PL calls "use inc::Module::Install" +# 2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install +# 3. The installed version of inc::Module::Install loads +# 4. inc::Module::Install calls "require Module::Install" +# 5. The ./inc/ version of Module::Install loads +# } ELSE { +# 1. Makefile.PL calls "use inc::Module::Install" +# 2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install +# 3. The ./inc/ version of Module::Install loads +# } + +use 5.004; +use strict 'vars'; + +use vars qw{$VERSION}; +BEGIN { + # All Module::Install core packages now require synchronised versions. + # This will be used to ensure we don't accidentally load old or + # different versions of modules. + # This is not enforced yet, but will be some time in the next few + # releases once we can make sure it won't clash with custom + # Module::Install extensions. + $VERSION = '0.65'; +} + +# Whether or not inc::Module::Install is actually loaded, the +# $INC{inc/Module/Install.pm} is what will still get set as long as +# the caller loaded module this in the documented manner. +# If not set, the caller may NOT have loaded the bundled version, and thus +# they may not have a MI version that works with the Makefile.PL. This would +# result in false errors or unexpected behaviour. And we don't want that. +my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm'; +unless ( $INC{$file} ) { + die <<"END_DIE"; +Please invoke ${\__PACKAGE__} with: + + use inc::${\__PACKAGE__}; + +not: + + use ${\__PACKAGE__}; + +END_DIE +} + +# If the script that is loading Module::Install is from the future, +# then make will detect this and cause it to re-run over and over +# again. This is bad. Rather than taking action to touch it (which +# is unreliable on some platforms and requires write permissions) +# for now we should catch this and refuse to run. +if ( -f $0 and (stat($0))[9] > time ) { + die << "END_DIE"; +Your installer $0 has a modification time in the future. + +This is known to create infinite loops in make. + +Please correct this, then run $0 again. + +END_DIE +} + +use Cwd (); +use File::Find (); +use File::Path (); +use FindBin; + +*inc::Module::Install::VERSION = *VERSION; +@inc::Module::Install::ISA = __PACKAGE__; + +sub autoload { + my $self = shift; + my $who = $self->_caller; + my $cwd = Cwd::cwd(); + my $sym = "${who}::AUTOLOAD"; + $sym->{$cwd} = sub { + my $pwd = Cwd::cwd(); + if ( my $code = $sym->{$pwd} ) { + # delegate back to parent dirs + goto &$code unless $cwd eq $pwd; + } + $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym"; + unshift @_, ($self, $1); + goto &{$self->can('call')} unless uc($1) eq $1; + }; +} + +sub import { + my $class = shift; + my $self = $class->new(@_); + my $who = $self->_caller; + + unless ( -f $self->{file} ) { + require "$self->{path}/$self->{dispatch}.pm"; + File::Path::mkpath("$self->{prefix}/$self->{author}"); + $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self ); + $self->{admin}->init; + @_ = ($class, _self => $self); + goto &{"$self->{name}::import"}; + } + + *{"${who}::AUTOLOAD"} = $self->autoload; + $self->preload; + + # Unregister loader and worker packages so subdirs can use them again + delete $INC{"$self->{file}"}; + delete $INC{"$self->{path}.pm"}; +} + +sub preload { + my ($self) = @_; + + unless ( $self->{extensions} ) { + $self->load_extensions( + "$self->{prefix}/$self->{path}", $self + ); + } + + my @exts = @{$self->{extensions}}; + unless ( @exts ) { + my $admin = $self->{admin}; + @exts = $admin->load_all_extensions; + } + + my %seen; + foreach my $obj ( @exts ) { + while (my ($method, $glob) = each %{ref($obj) . '::'}) { + next unless $obj->can($method); + next if $method =~ /^_/; + next if $method eq uc($method); + $seen{$method}++; + } + } + + my $who = $self->_caller; + foreach my $name ( sort keys %seen ) { + *{"${who}::$name"} = sub { + ${"${who}::AUTOLOAD"} = "${who}::$name"; + goto &{"${who}::AUTOLOAD"}; + }; + } +} + +sub new { + my ($class, %args) = @_; + + # ignore the prefix on extension modules built from top level. + my $base_path = Cwd::abs_path($FindBin::Bin); + unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) { + delete $args{prefix}; + } + + return $args{_self} if $args{_self}; + + $args{dispatch} ||= 'Admin'; + $args{prefix} ||= 'inc'; + $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author'); + $args{bundle} ||= 'inc/BUNDLES'; + $args{base} ||= $base_path; + $class =~ s/^\Q$args{prefix}\E:://; + $args{name} ||= $class; + $args{version} ||= $class->VERSION; + unless ( $args{path} ) { + $args{path} = $args{name}; + $args{path} =~ s!::!/!g; + } + $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm"; + + bless( \%args, $class ); +} + +sub call { + my ($self, $method) = @_; + my $obj = $self->load($method) or return; + splice(@_, 0, 2, $obj); + goto &{$obj->can($method)}; +} + +sub load { + my ($self, $method) = @_; + + $self->load_extensions( + "$self->{prefix}/$self->{path}", $self + ) unless $self->{extensions}; + + foreach my $obj (@{$self->{extensions}}) { + return $obj if $obj->can($method); + } + + my $admin = $self->{admin} or die <<"END_DIE"; +The '$method' method does not exist in the '$self->{prefix}' path! +Please remove the '$self->{prefix}' directory and run $0 again to load it. +END_DIE + + my $obj = $admin->load($method, 1); + push @{$self->{extensions}}, $obj; + + $obj; +} + +sub load_extensions { + my ($self, $path, $top) = @_; + + unless ( grep { lc $_ eq lc $self->{prefix} } @INC ) { + unshift @INC, $self->{prefix}; + } + + foreach my $rv ( $self->find_extensions($path) ) { + my ($file, $pkg) = @{$rv}; + next if $self->{pathnames}{$pkg}; + + local $@; + my $new = eval { require $file; $pkg->can('new') }; + unless ( $new ) { + warn $@ if $@; + next; + } + $self->{pathnames}{$pkg} = delete $INC{$file}; + push @{$self->{extensions}}, &{$new}($pkg, _top => $top ); + } + + $self->{extensions} ||= []; +} + +sub find_extensions { + my ($self, $path) = @_; + + my @found; + File::Find::find( sub { + my $file = $File::Find::name; + return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is; + my $subpath = $1; + return if lc($subpath) eq lc($self->{dispatch}); + + $file = "$self->{path}/$subpath.pm"; + my $pkg = "$self->{name}::$subpath"; + $pkg =~ s!/!::!g; + + # If we have a mixed-case package name, assume case has been preserved + # correctly. Otherwise, root through the file to locate the case-preserved + # version of the package name. + if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) { + open PKGFILE, "<$subpath.pm" or die "find_extensions: Can't open $subpath.pm: $!"; + my $in_pod = 0; + while ( ) { + $in_pod = 1 if /^=\w/; + $in_pod = 0 if /^=cut/; + next if ($in_pod || /^=cut/); # skip pod text + next if /^\s*#/; # and comments + if ( m/^\s*package\s+($pkg)\s*;/i ) { + $pkg = $1; + last; + } + } + close PKGFILE; + } + + push @found, [ $file, $pkg ]; + }, $path ) if -d $path; + + @found; +} + +sub _caller { + my $depth = 0; + my $call = caller($depth); + while ( $call eq __PACKAGE__ ) { + $depth++; + $call = caller($depth); + } + return $call; +} + +1; diff --git a/inc/Module/Install/AutoInstall.pm b/inc/Module/Install/AutoInstall.pm new file mode 100644 index 0000000..b4b55af --- /dev/null +++ b/inc/Module/Install/AutoInstall.pm @@ -0,0 +1,61 @@ +#line 1 +package Module::Install::AutoInstall; + +use strict; +use Module::Install::Base; + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +sub AutoInstall { $_[0] } + +sub run { + my $self = shift; + $self->auto_install_now(@_); +} + +sub write { + my $self = shift; + $self->auto_install(@_); +} + +sub auto_install { + my $self = shift; + return if $self->{done}++; + + # Flatten array of arrays into a single array + my @core = map @$_, map @$_, grep ref, + $self->build_requires, $self->requires; + + my @config = @_; + + # We'll need Module::AutoInstall + $self->include('Module::AutoInstall'); + require Module::AutoInstall; + + Module::AutoInstall->import( + (@config ? (-config => \@config) : ()), + (@core ? (-core => \@core) : ()), + $self->features, + ); + + $self->makemaker_args( Module::AutoInstall::_make_args() ); + + my $class = ref($self); + $self->postamble( + "# --- $class section:\n" . + Module::AutoInstall::postamble() + ); +} + +sub auto_install_now { + my $self = shift; + $self->auto_install(@_); + Module::AutoInstall::do_install(); +} + +1; diff --git a/inc/Module/Install/Base.pm b/inc/Module/Install/Base.pm new file mode 100644 index 0000000..b46a8ca --- /dev/null +++ b/inc/Module/Install/Base.pm @@ -0,0 +1,70 @@ +#line 1 +package Module::Install::Base; + +$VERSION = '0.65'; + +# Suspend handler for "redefined" warnings +BEGIN { + my $w = $SIG{__WARN__}; + $SIG{__WARN__} = sub { $w }; +} + +### This is the ONLY module that shouldn't have strict on +# use strict; + +#line 41 + +sub new { + my ($class, %args) = @_; + + foreach my $method ( qw(call load) ) { + *{"$class\::$method"} = sub { + shift()->_top->$method(@_); + } unless defined &{"$class\::$method"}; + } + + bless( \%args, $class ); +} + +#line 61 + +sub AUTOLOAD { + my $self = shift; + local $@; + my $autoload = eval { $self->_top->autoload } or return; + goto &$autoload; +} + +#line 76 + +sub _top { $_[0]->{_top} } + +#line 89 + +sub admin { + $_[0]->_top->{admin} or Module::Install::Base::FakeAdmin->new; +} + +sub is_admin { + $_[0]->admin->VERSION; +} + +sub DESTROY {} + +package Module::Install::Base::FakeAdmin; + +my $Fake; +sub new { $Fake ||= bless(\@_, $_[0]) } + +sub AUTOLOAD {} + +sub DESTROY {} + +# Restore warning handler +BEGIN { + $SIG{__WARN__} = $SIG{__WARN__}->(); +} + +1; + +#line 138 diff --git a/inc/Module/Install/Can.pm b/inc/Module/Install/Can.pm new file mode 100644 index 0000000..9bcf278 --- /dev/null +++ b/inc/Module/Install/Can.pm @@ -0,0 +1,82 @@ +#line 1 +package Module::Install::Can; + +use strict; +use Module::Install::Base; +use Config (); +### This adds a 5.005 Perl version dependency. +### This is a bug and will be fixed. +use File::Spec (); +use ExtUtils::MakeMaker (); + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +# check if we can load some module +### Upgrade this to not have to load the module if possible +sub can_use { + my ($self, $mod, $ver) = @_; + $mod =~ s{::|\\}{/}g; + $mod .= '.pm' unless $mod =~ /\.pm$/i; + + my $pkg = $mod; + $pkg =~ s{/}{::}g; + $pkg =~ s{\.pm$}{}i; + + local $@; + eval { require $mod; $pkg->VERSION($ver || 0); 1 }; +} + +# check if we can run some command +sub can_run { + my ($self, $cmd) = @_; + + my $_cmd = $cmd; + return $_cmd if (-x $_cmd or $_cmd = MM->maybe_command($_cmd)); + + for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') { + my $abs = File::Spec->catfile($dir, $_[1]); + return $abs if (-x $abs or $abs = MM->maybe_command($abs)); + } + + return; +} + +# can we locate a (the) C compiler +sub can_cc { + my $self = shift; + my @chunks = split(/ /, $Config::Config{cc}) or return; + + # $Config{cc} may contain args; try to find out the program part + while (@chunks) { + return $self->can_run("@chunks") || (pop(@chunks), next); + } + + return; +} + +# Fix Cygwin bug on maybe_command(); +if ( $^O eq 'cygwin' ) { + require ExtUtils::MM_Cygwin; + require ExtUtils::MM_Win32; + if ( ! defined(&ExtUtils::MM_Cygwin::maybe_command) ) { + *ExtUtils::MM_Cygwin::maybe_command = sub { + my ($self, $file) = @_; + if ($file =~ m{^/cygdrive/}i and ExtUtils::MM_Win32->can('maybe_command')) { + ExtUtils::MM_Win32->maybe_command($file); + } else { + ExtUtils::MM_Unix->maybe_command($file); + } + } + } +} + +1; + +__END__ + +#line 157 diff --git a/inc/Module/Install/Fetch.pm b/inc/Module/Install/Fetch.pm new file mode 100644 index 0000000..0d2c39c --- /dev/null +++ b/inc/Module/Install/Fetch.pm @@ -0,0 +1,93 @@ +#line 1 +package Module::Install::Fetch; + +use strict; +use Module::Install::Base; + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +sub get_file { + my ($self, %args) = @_; + my ($scheme, $host, $path, $file) = + $args{url} =~ m|^(\w+)://([^/]+)(.+)/(.+)| or return; + + if ( $scheme eq 'http' and ! eval { require LWP::Simple; 1 } ) { + $args{url} = $args{ftp_url} + or (warn("LWP support unavailable!\n"), return); + ($scheme, $host, $path, $file) = + $args{url} =~ m|^(\w+)://([^/]+)(.+)/(.+)| or return; + } + + $|++; + print "Fetching '$file' from $host... "; + + unless (eval { require Socket; Socket::inet_aton($host) }) { + warn "'$host' resolve failed!\n"; + return; + } + + return unless $scheme eq 'ftp' or $scheme eq 'http'; + + require Cwd; + my $dir = Cwd::getcwd(); + chdir $args{local_dir} or return if exists $args{local_dir}; + + if (eval { require LWP::Simple; 1 }) { + LWP::Simple::mirror($args{url}, $file); + } + elsif (eval { require Net::FTP; 1 }) { eval { + # use Net::FTP to get past firewall + my $ftp = Net::FTP->new($host, Passive => 1, Timeout => 600); + $ftp->login("anonymous", 'anonymous@example.com'); + $ftp->cwd($path); + $ftp->binary; + $ftp->get($file) or (warn("$!\n"), return); + $ftp->quit; + } } + elsif (my $ftp = $self->can_run('ftp')) { eval { + # no Net::FTP, fallback to ftp.exe + require FileHandle; + my $fh = FileHandle->new; + + local $SIG{CHLD} = 'IGNORE'; + unless ($fh->open("|$ftp -n")) { + warn "Couldn't open ftp: $!\n"; + chdir $dir; return; + } + + my @dialog = split(/\n/, <<"END_FTP"); +open $host +user anonymous anonymous\@example.com +cd $path +binary +get $file $file +quit +END_FTP + foreach (@dialog) { $fh->print("$_\n") } + $fh->close; + } } + else { + warn "No working 'ftp' program available!\n"; + chdir $dir; return; + } + + unless (-f $file) { + warn "Fetching failed: $@\n"; + chdir $dir; return; + } + + return if exists $args{size} and -s $file != $args{size}; + system($args{run}) if exists $args{run}; + unlink($file) if $args{remove}; + + print(((!exists $args{check_for} or -e $args{check_for}) + ? "done!" : "failed! ($!)"), "\n"); + chdir $dir; return !$?; +} + +1; diff --git a/inc/Module/Install/Include.pm b/inc/Module/Install/Include.pm new file mode 100644 index 0000000..964b93d --- /dev/null +++ b/inc/Module/Install/Include.pm @@ -0,0 +1,34 @@ +#line 1 +package Module::Install::Include; + +use strict; +use Module::Install::Base; + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +sub include { + shift()->admin->include(@_); +} + +sub include_deps { + shift()->admin->include_deps(@_); +} + +sub auto_include { + shift()->admin->auto_include(@_); +} + +sub auto_include_deps { + shift()->admin->auto_include_deps(@_); +} + +sub auto_include_dependent_dists { + shift()->admin->auto_include_dependent_dists(@_); +} + +1; diff --git a/inc/Module/Install/Makefile.pm b/inc/Module/Install/Makefile.pm new file mode 100644 index 0000000..eb67033 --- /dev/null +++ b/inc/Module/Install/Makefile.pm @@ -0,0 +1,212 @@ +#line 1 +package Module::Install::Makefile; + +use strict 'vars'; +use Module::Install::Base; +use ExtUtils::MakeMaker (); + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +sub Makefile { $_[0] } + +my %seen = (); + +sub prompt { + shift; + + # Infinite loop protection + my @c = caller(); + if ( ++$seen{"$c[1]|$c[2]|$_[0]"} > 3 ) { + die "Caught an potential prompt infinite loop ($c[1]|$c[2]|$_[0])"; + } + + # In automated testing, always use defaults + if ( $ENV{AUTOMATED_TESTING} and ! $ENV{PERL_MM_USE_DEFAULT} ) { + local $ENV{PERL_MM_USE_DEFAULT} = 1; + goto &ExtUtils::MakeMaker::prompt; + } else { + goto &ExtUtils::MakeMaker::prompt; + } +} + +sub makemaker_args { + my $self = shift; + my $args = ($self->{makemaker_args} ||= {}); + %$args = ( %$args, @_ ) if @_; + $args; +} + +# For mm args that take multiple space-seperated args, +# append an argument to the current list. +sub makemaker_append { + my $self = shift; + my $name = shift; + my $args = $self->makemaker_args; + $args->{name} = defined $args->{$name} + ? join( ' ', $args->{name}, @_ ) + : join( ' ', @_ ); +} + +sub build_subdirs { + my $self = shift; + my $subdirs = $self->makemaker_args->{DIR} ||= []; + for my $subdir (@_) { + push @$subdirs, $subdir; + } +} + +sub clean_files { + my $self = shift; + my $clean = $self->makemaker_args->{clean} ||= {}; + %$clean = ( + %$clean, + FILES => join(' ', grep length, $clean->{FILES}, @_), + ); +} + +sub realclean_files { + my $self = shift; + my $realclean = $self->makemaker_args->{realclean} ||= {}; + %$realclean = ( + %$realclean, + FILES => join(' ', grep length, $realclean->{FILES}, @_), + ); +} + +sub libs { + my $self = shift; + my $libs = ref $_[0] ? shift : [ shift ]; + $self->makemaker_args( LIBS => $libs ); +} + +sub inc { + my $self = shift; + $self->makemaker_args( INC => shift ); +} + +sub write { + my $self = shift; + die "&Makefile->write() takes no arguments\n" if @_; + + my $args = $self->makemaker_args; + $args->{DISTNAME} = $self->name; + $args->{NAME} = $self->module_name || $self->name || $self->determine_NAME($args); + $args->{VERSION} = $self->version || $self->determine_VERSION($args); + $args->{NAME} =~ s/-/::/g; + if ( $self->tests ) { + $args->{test} = { TESTS => $self->tests }; + } + if ($] >= 5.005) { + $args->{ABSTRACT} = $self->abstract; + $args->{AUTHOR} = $self->author; + } + if ( eval($ExtUtils::MakeMaker::VERSION) >= 6.10 ) { + $args->{NO_META} = 1; + } + if ( eval($ExtUtils::MakeMaker::VERSION) > 6.17 and $self->sign ) { + $args->{SIGN} = 1; + } + unless ( $self->is_admin ) { + delete $args->{SIGN}; + } + + # merge both kinds of requires into prereq_pm + my $prereq = ($args->{PREREQ_PM} ||= {}); + %$prereq = ( %$prereq, map { @$_ } map { @$_ } grep $_, + ($self->build_requires, $self->requires) ); + + # merge both kinds of requires into prereq_pm + my $subdirs = ($args->{DIR} ||= []); + if ($self->bundles) { + foreach my $bundle (@{ $self->bundles }) { + my ($file, $dir) = @$bundle; + push @$subdirs, $dir if -d $dir; + delete $prereq->{$file}; + } + } + + if ( my $perl_version = $self->perl_version ) { + eval "use $perl_version; 1" + or die "ERROR: perl: Version $] is installed, " + . "but we need version >= $perl_version"; + } + + $args->{INSTALLDIRS} = $self->installdirs; + + my %args = map { ( $_ => $args->{$_} ) } grep {defined($args->{$_})} keys %$args; + + my $user_preop = delete $args{dist}->{PREOP}; + if (my $preop = $self->admin->preop($user_preop)) { + $args{dist} = $preop; + } + + my $mm = ExtUtils::MakeMaker::WriteMakefile(%args); + $self->fix_up_makefile($mm->{FIRST_MAKEFILE} || 'Makefile'); +} + +sub fix_up_makefile { + my $self = shift; + my $makefile_name = shift; + my $top_class = ref($self->_top) || ''; + my $top_version = $self->_top->VERSION || ''; + + my $preamble = $self->preamble + ? "# Preamble by $top_class $top_version\n" + . $self->preamble + : ''; + my $postamble = "# Postamble by $top_class $top_version\n" + . ($self->postamble || ''); + + local *MAKEFILE; + open MAKEFILE, "< $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!"; + my $makefile = do { local $/; }; + close MAKEFILE or die $!; + + $makefile =~ s/\b(test_harness\(\$\(TEST_VERBOSE\), )/$1'inc', /; + $makefile =~ s/( -I\$\(INST_ARCHLIB\))/ -Iinc$1/g; + $makefile =~ s/( "-I\$\(INST_LIB\)")/ "-Iinc"$1/g; + $makefile =~ s/^(FULLPERL = .*)/$1 "-Iinc"/m; + $makefile =~ s/^(PERL = .*)/$1 "-Iinc"/m; + + # Module::Install will never be used to build the Core Perl + # Sometimes PERL_LIB and PERL_ARCHLIB get written anyway, which breaks + # PREFIX/PERL5LIB, and thus, install_share. Blank them if they exist + $makefile =~ s/^PERL_LIB = .+/PERL_LIB =/m; + #$makefile =~ s/^PERL_ARCHLIB = .+/PERL_ARCHLIB =/m; + + # Perl 5.005 mentions PERL_LIB explicitly, so we have to remove that as well. + $makefile =~ s/("?)-I\$\(PERL_LIB\)\1//g; + + # XXX - This is currently unused; not sure if it breaks other MM-users + # $makefile =~ s/^pm_to_blib\s+:\s+/pm_to_blib :: /mg; + + open MAKEFILE, "> $makefile_name" or die "fix_up_makefile: Couldn't open $makefile_name: $!"; + print MAKEFILE "$preamble$makefile$postamble" or die $!; + close MAKEFILE or die $!; + + 1; +} + +sub preamble { + my ($self, $text) = @_; + $self->{preamble} = $text . $self->{preamble} if defined $text; + $self->{preamble}; +} + +sub postamble { + my ($self, $text) = @_; + $self->{postamble} ||= $self->admin->postamble; + $self->{postamble} .= $text if defined $text; + $self->{postamble} +} + +1; + +__END__ + +#line 338 diff --git a/inc/Module/Install/Makefile/Version.pm b/inc/Module/Install/Makefile/Version.pm new file mode 100644 index 0000000..349afc0 --- /dev/null +++ b/inc/Module/Install/Makefile/Version.pm @@ -0,0 +1,42 @@ +#line 1 +package Module::Install::Makefile::Version; + +use Module::Install::Base; +@ISA = qw(Module::Install::Base); + +$VERSION = '0.65'; + +use strict; + +sub determine_VERSION { + my $self = shift; + my @modules = glob('*.pm'); + + require File::Find; + File::Find::find( sub { + push @modules, $File::Find::name =~ /\.pm\z/i; + }, 'lib' ); + + if (@modules == 1) { + eval { + $self->version( + ExtUtils::MM_Unix->parse_version($modules[0]) + ); + }; + print STDERR $@ if $@; + + } elsif ( my $file = "lib/" . $self->name . ".pm" ) { + $file =~ s!-!/!g; + $self->version( + ExtUtils::MM_Unix->parse_version($file) + ) if -f $file; + + } + + $self->version or die << "END_MESSAGE"; +Can't determine a VERSION for this distribution. +Please call the 'version' or 'version_from' function in Makefile.PL. +END_MESSAGE +} + +1; diff --git a/inc/Module/Install/Metadata.pm b/inc/Module/Install/Metadata.pm new file mode 100644 index 0000000..b5658c9 --- /dev/null +++ b/inc/Module/Install/Metadata.pm @@ -0,0 +1,323 @@ +#line 1 +package Module::Install::Metadata; + +use strict 'vars'; +use Module::Install::Base; + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +my @scalar_keys = qw{ + name module_name abstract author version license + distribution_type perl_version tests installdirs +}; + +my @tuple_keys = qw{ + build_requires requires recommends bundles +}; + +sub Meta { shift } +sub Meta_ScalarKeys { @scalar_keys } +sub Meta_TupleKeys { @tuple_keys } + +foreach my $key (@scalar_keys) { + *$key = sub { + my $self = shift; + return $self->{values}{$key} if defined wantarray and !@_; + $self->{values}{$key} = shift; + return $self; + }; +} + +foreach my $key (@tuple_keys) { + *$key = sub { + my $self = shift; + return $self->{values}{$key} unless @_; + + my @rv; + while (@_) { + my $module = shift or last; + my $version = shift || 0; + if ( $module eq 'perl' ) { + $version =~ s{^(\d+)\.(\d+)\.(\d+)} + {$1 + $2/1_000 + $3/1_000_000}e; + $self->perl_version($version); + next; + } + my $rv = [ $module, $version ]; + push @rv, $rv; + } + push @{ $self->{values}{$key} }, @rv; + @rv; + }; +} + +sub install_as_core { $_[0]->installdirs('perl') } +sub install_as_cpan { $_[0]->installdirs('site') } +sub install_as_site { $_[0]->installdirs('site') } +sub install_as_vendor { $_[0]->installdirs('vendor') } + +sub sign { + my $self = shift; + return $self->{'values'}{'sign'} if defined wantarray and !@_; + $self->{'values'}{'sign'} = ( @_ ? $_[0] : 1 ); + return $self; +} + +sub dynamic_config { + my $self = shift; + unless ( @_ ) { + warn "You MUST provide an explicit true/false value to dynamic_config, skipping\n"; + return $self; + } + $self->{'values'}{'dynamic_config'} = $_[0] ? 1 : 0; + return $self; +} + +sub all_from { + my ( $self, $file ) = @_; + + unless ( defined($file) ) { + my $name = $self->name + or die "all_from called with no args without setting name() first"; + $file = join('/', 'lib', split(/-/, $name)) . '.pm'; + $file =~ s{.*/}{} unless -e $file; + die "all_from: cannot find $file from $name" unless -e $file; + } + + $self->version_from($file) unless $self->version; + $self->perl_version_from($file) unless $self->perl_version; + + # The remaining probes read from POD sections; if the file + # has an accompanying .pod, use that instead + my $pod = $file; + if ( $pod =~ s/\.pm$/.pod/i and -e $pod ) { + $file = $pod; + } + + $self->author_from($file) unless $self->author; + $self->license_from($file) unless $self->license; + $self->abstract_from($file) unless $self->abstract; +} + +sub provides { + my $self = shift; + my $provides = ( $self->{values}{provides} ||= {} ); + %$provides = (%$provides, @_) if @_; + return $provides; +} + +sub auto_provides { + my $self = shift; + return $self unless $self->is_admin; + + unless (-e 'MANIFEST') { + warn "Cannot deduce auto_provides without a MANIFEST, skipping\n"; + return $self; + } + + # Avoid spurious warnings as we are not checking manifest here. + + local $SIG{__WARN__} = sub {1}; + require ExtUtils::Manifest; + local *ExtUtils::Manifest::manicheck = sub { return }; + + require Module::Build; + my $build = Module::Build->new( + dist_name => $self->name, + dist_version => $self->version, + license => $self->license, + ); + $self->provides(%{ $build->find_dist_packages || {} }); +} + +sub feature { + my $self = shift; + my $name = shift; + my $features = ( $self->{values}{features} ||= [] ); + + my $mods; + + if ( @_ == 1 and ref( $_[0] ) ) { + # The user used ->feature like ->features by passing in the second + # argument as a reference. Accomodate for that. + $mods = $_[0]; + } else { + $mods = \@_; + } + + my $count = 0; + push @$features, ( + $name => [ + map { + ref($_) ? ( ref($_) eq 'HASH' ) ? %$_ + : @$_ + : $_ + } @$mods + ] + ); + + return @$features; +} + +sub features { + my $self = shift; + while ( my ( $name, $mods ) = splice( @_, 0, 2 ) ) { + $self->feature( $name, @$mods ); + } + return $self->{values}->{features} + ? @{ $self->{values}->{features} } + : (); +} + +sub no_index { + my $self = shift; + my $type = shift; + push @{ $self->{values}{no_index}{$type} }, @_ if $type; + return $self->{values}{no_index}; +} + +sub read { + my $self = shift; + $self->include_deps( 'YAML', 0 ); + + require YAML; + my $data = YAML::LoadFile('META.yml'); + + # Call methods explicitly in case user has already set some values. + while ( my ( $key, $value ) = each %$data ) { + next unless $self->can($key); + if ( ref $value eq 'HASH' ) { + while ( my ( $module, $version ) = each %$value ) { + $self->can($key)->($self, $module => $version ); + } + } + else { + $self->can($key)->($self, $value); + } + } + return $self; +} + +sub write { + my $self = shift; + return $self unless $self->is_admin; + $self->admin->write_meta; + return $self; +} + +sub version_from { + my ( $self, $file ) = @_; + require ExtUtils::MM_Unix; + $self->version( ExtUtils::MM_Unix->parse_version($file) ); +} + +sub abstract_from { + my ( $self, $file ) = @_; + require ExtUtils::MM_Unix; + $self->abstract( + bless( + { DISTNAME => $self->name }, + 'ExtUtils::MM_Unix' + )->parse_abstract($file) + ); +} + +sub _slurp { + my ( $self, $file ) = @_; + + local *FH; + open FH, "< $file" or die "Cannot open $file.pod: $!"; + do { local $/; }; +} + +sub perl_version_from { + my ( $self, $file ) = @_; + + if ( + $self->_slurp($file) =~ m/ + ^ + use \s* + v? + ([\d_\.]+) + \s* ; + /ixms + ) + { + my $v = $1; + $v =~ s{_}{}g; + $self->perl_version($1); + } + else { + warn "Cannot determine perl version info from $file\n"; + return; + } +} + +sub author_from { + my ( $self, $file ) = @_; + my $content = $self->_slurp($file); + if ($content =~ m/ + =head \d \s+ (?:authors?)\b \s* + ([^\n]*) + | + =head \d \s+ (?:licen[cs]e|licensing|copyright|legal)\b \s* + .*? copyright .*? \d\d\d[\d.]+ \s* (?:\bby\b)? \s* + ([^\n]*) + /ixms) { + my $author = $1 || $2; + $author =~ s{E}{<}g; + $author =~ s{E}{>}g; + $self->author($author); + } + else { + warn "Cannot determine author info from $file\n"; + } +} + +sub license_from { + my ( $self, $file ) = @_; + + if ( + $self->_slurp($file) =~ m/ + ( + =head \d \s+ + (?:licen[cs]e|licensing|copyright|legal)\b + .*? + ) + (=head\\d.*|=cut.*|) + \z + /ixms + ) + { + my $license_text = $1; + my @phrases = ( + 'under the same (?:terms|license) as perl itself' => 'perl', + 'GNU public license' => 'gpl', + 'GNU lesser public license' => 'gpl', + 'BSD license' => 'bsd', + 'Artistic license' => 'artistic', + 'GPL' => 'gpl', + 'LGPL' => 'lgpl', + 'BSD' => 'bsd', + 'Artistic' => 'artistic', + 'MIT' => 'MIT', + ); + while ( my ( $pattern, $license ) = splice( @phrases, 0, 2 ) ) { + $pattern =~ s{\s+}{\\s+}g; + if ( $license_text =~ /\b$pattern\b/i ) { + $self->license($license); + return 1; + } + } + } + + warn "Cannot determine license info from $file\n"; + return 'unknown'; +} + +1; diff --git a/inc/Module/Install/Win32.pm b/inc/Module/Install/Win32.pm new file mode 100644 index 0000000..42cb653 --- /dev/null +++ b/inc/Module/Install/Win32.pm @@ -0,0 +1,65 @@ +#line 1 +package Module::Install::Win32; + +use strict; +use Module::Install::Base; + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +# determine if the user needs nmake, and download it if needed +sub check_nmake { + my $self = shift; + $self->load('can_run'); + $self->load('get_file'); + + require Config; + return unless ( + $^O eq 'MSWin32' and + $Config::Config{make} and + $Config::Config{make} =~ /^nmake\b/i and + ! $self->can_run('nmake') + ); + + print "The required 'nmake' executable not found, fetching it...\n"; + + require File::Basename; + my $rv = $self->get_file( + url => 'http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe', + ftp_url => 'ftp://ftp.microsoft.com/Softlib/MSLFILES/Nmake15.exe', + local_dir => File::Basename::dirname($^X), + size => 51928, + run => 'Nmake15.exe /o > nul', + check_for => 'Nmake.exe', + remove => 1, + ); + + if (!$rv) { + die <<'END_MESSAGE'; + +------------------------------------------------------------------------------- + +Since you are using Microsoft Windows, you will need the 'nmake' utility +before installation. It's available at: + + http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/Nmake15.exe + or + ftp://ftp.microsoft.com/Softlib/MSLFILES/Nmake15.exe + +Please download the file manually, save it to a directory in %PATH% (e.g. +C:\WINDOWS\COMMAND\), then launch the MS-DOS command line shell, "cd" to +that directory, and run "Nmake15.exe" from there; that will create the +'nmake.exe' file needed by this module. + +You may then resume the installation process described in README. + +------------------------------------------------------------------------------- +END_MESSAGE + } +} + +1; diff --git a/inc/Module/Install/WriteAll.pm b/inc/Module/Install/WriteAll.pm new file mode 100644 index 0000000..d0908fb --- /dev/null +++ b/inc/Module/Install/WriteAll.pm @@ -0,0 +1,43 @@ +#line 1 +package Module::Install::WriteAll; + +use strict; +use Module::Install::Base; + +use vars qw{$VERSION $ISCORE @ISA}; +BEGIN { + $VERSION = '0.65'; + $ISCORE = 1; + @ISA = qw{Module::Install::Base}; +} + +sub WriteAll { + my $self = shift; + my %args = ( + meta => 1, + sign => 0, + inline => 0, + check_nmake => 1, + @_ + ); + + $self->sign(1) if $args{sign}; + $self->Meta->write if $args{meta}; + $self->admin->WriteAll(%args) if $self->is_admin; + + if ( $0 =~ /Build.PL$/i ) { + $self->Build->write; + } else { + $self->check_nmake if $args{check_nmake}; + unless ( $self->makemaker_args->{'PL_FILES'} ) { + $self->makemaker_args( PL_FILES => {} ); + } + if ($args{inline}) { + $self->Inline->write; + } else { + $self->Makefile->write; + } + } +} + +1; diff --git a/t/00-compile.t b/t/00-compile.t new file mode 100644 index 0000000..f43f2dc --- /dev/null +++ b/t/00-compile.t @@ -0,0 +1,5 @@ +use strict; + +use Test::More tests => 1; + +use_ok 're::engine::Plugin'; diff --git a/t/Example.pm b/t/Example.pm new file mode 100644 index 0000000..efe9bab --- /dev/null +++ b/t/Example.pm @@ -0,0 +1,31 @@ +package Example; +use strict; + +# Note the (), doesn't call ->import +use re::engine::Plugin (); + +sub import { + # Populates %^H with re::engine::Plugin hooks + re::engine::Plugin->import( + exec => \&exec, + ); +} + +*unimport = \&re::engine::Plugin::unimport; + +sub exec +{ + my ($re, $str) = @_; + + $re->num_captures( + FETCH => sub { + my ($re, $paren) = @_; + + $str . "_" . $paren; + } + ); + + 1; +} + +1; diff --git a/t/Example.t b/t/Example.t new file mode 100644 index 0000000..f9080c0 --- /dev/null +++ b/t/Example.t @@ -0,0 +1,9 @@ +use strict; +use lib 't'; +use Test::More tests => 1; + +use Example; + +"str" =~ /pattern/; + +is($1, "str_1"); diff --git a/t/eval-comp.t b/t/eval-comp.t new file mode 100644 index 0000000..104f111 --- /dev/null +++ b/t/eval-comp.t @@ -0,0 +1,23 @@ +=pod + +This will always die, see L for why. + +This can be made to live regardless of C by adding C<| G_EVAL> +to C in C. + +=cut + +use strict; + +use Test::More skip_all => 'Always dies'; + +use re::engine::Plugin ( + comp => sub { die "died at comp time" }, +); + +eval { "str" ~~ /noes/ }; + +TODO: { + local $TODO = 'passing tests for known bug with how we handle eval'; + pass; +} diff --git a/t/eval-exec.t b/t/eval-exec.t new file mode 100644 index 0000000..4ec6475 --- /dev/null +++ b/t/eval-exec.t @@ -0,0 +1,18 @@ +=pod + +Copy of F that tests another callback sub. + +=cut + +use strict; + +use Test::More tests => 1; + +use re::engine::Plugin ( + exec => sub { die "died at exec time" }, +); + +eval { 'oh' =~ /noes/ }; +ok index($@ => 'at exec') => 'die in exec works'; + + diff --git a/t/import.t b/t/import.t new file mode 100644 index 0000000..f2eae22 --- /dev/null +++ b/t/import.t @@ -0,0 +1,51 @@ +=pod + +Test that lexical importing works, check BEGIN-ish stuff etc. + +=cut + +use strict; +use Test::More tests => 8; +use re::engine::Plugin (); + +like "a", qr/^a$/, "import didn't run, perl's regex engine in effect"; + +BEGIN { + re::engine::Plugin->import( + exec => sub { $_[0]->pattern eq $_[1] } + ); +} + +ok "^hello" =~ /^hello/ => "regex modified to match a literal pattern"; + +{ + BEGIN { + re::engine::Plugin->import( + exec => sub { $_[0]->pattern ne $_[1] } + ); + } + + ok "^hello" !~ /^hello/ => "regex modified not to match a literal pattern"; + { + BEGIN { + re::engine::Plugin->import( + exec => sub { $_[0]->pattern eq '^[abc]$' } + ); + } + ok "whatever" =~ /^[abc]$/ => "regex modified to match some exact nonsense"; + BEGIN { re::engine::Plugin->unimport }; + ok "whatever" !~ /^[abc]$/ => "regex modified to match some exact nonsense unimported"; + } + ok "^hello" !~ /^hello/ => "regex modified not to match a literal pattern"; +} + +ok "^hello" =~ /^hello/ => "regex modified to match a literal pattern"; + +# Another import at the same scope +BEGIN { + re::engine::Plugin->import( + exec => sub { $_[0]->pattern ne $_[1] } + ); +} + +ok "^hello" !~ /^hello/ => "regex modified not to match a literal pattern"; diff --git a/t/methods.t b/t/methods.t new file mode 100644 index 0000000..6df32fa --- /dev/null +++ b/t/methods.t @@ -0,0 +1,54 @@ +=pod + +Tests methods on the re object + +=cut + +use strict; + +use feature ':5.10'; + +use Test::More tests => 9; + +use re::engine::Plugin ( + comp => sub { + my $re = shift; + + # Use a stash to pass a single scalar value to each executing + # routine, references work perfectly a reference to anything + # can be passed as well + $re->stash( { x => 5, y => sub { 6 } } ); + + # Return value not used for now.. + }, + exec => sub { + my ($re, $str) = @_; + + # pattern + cmp_ok($re->pattern, 'eq', ' foobar zoobar ' => '->pattern ok'); + + # modifiers + my %mod = $re->mod; + ok(exists $mod{i}, 'str flags /i'); + ok(exists $mod{x}, 'str flags /i'); + like(join('', keys %mod), qr/^[cgimosx]+$/, 'flags contain all-good characters'); + + # stash + cmp_ok($re->stash->{"x"}, '==', 5, "data correct in stash"); + cmp_ok(ref $re->stash->{"y"}, 'eq', 'CODE', "data correct in stash"); + cmp_ok(ref $re->stash->{"y"}, 'eq', 'CODE', "data correct in stash"); + cmp_ok($re->stash->{"y"}->(), '==', 6, "data correct in stash"); + + # Pattern contains "foo", "bar" and "zoo", return a true + return $re->pattern =~ /zoo/; + } +); + +my $re = qr< foobar zoobar >xi; + +if ("input" =~ $re ) { + pass 'pattern matched'; +} else { + fail "pattern didn't match"; +} + diff --git a/t/methods/free.t b/t/methods/free.t new file mode 100644 index 0000000..61c5dff --- /dev/null +++ b/t/methods/free.t @@ -0,0 +1,18 @@ +=pod + +Test the C method + +=cut + +use strict; +use Test::More skip_all => "Doesn't work currently (where did my scope go?!)"; + +use re::engine::Plugin ( + comp => sub { + my ($re) = @_; + + $re->free( sub { pass "ran free" } ); + } +); + +"str" ~~ /pattern/; diff --git a/t/methods/minlen/bytes.t b/t/methods/minlen/bytes.t new file mode 100644 index 0000000..643f118 --- /dev/null +++ b/t/methods/minlen/bytes.t @@ -0,0 +1,23 @@ +=pod + +C speaks bytes, not characters. + +=cut + +use strict; +use Test::More tests => 3; +use re::engine::Plugin ( + comp => sub { shift->minlen(5) }, + exec => sub { + my ($re, $str) = @_; + pass "Called with $str"; + }, +); + +my $str = "ævar"; +is(length $str, 5, "$str is 5 char long"); # Chars +$str =~ /pattern/; # no ->exec + +chop $str; +is(length $str, 4, "$str is 4 char long"); # Chars +$str =~ /pattern/; # yes ->exec diff --git a/t/methods/minlen/get.t b/t/methods/minlen/get.t new file mode 100644 index 0000000..80f7f80 --- /dev/null +++ b/t/methods/minlen/get.t @@ -0,0 +1,20 @@ +use strict; + +use Test::More tests => 3; + +use re::engine::Plugin ( + comp => sub { + my $re = shift; + $re->minlen(2); + }, + exec => sub { + my $re = shift; + my $minlen = $re->minlen; + cmp_ok $minlen, '==', 2, 'minlen accessor'; + }, +); + +pass "making match"; +"s" =~ /pattern/; +"st" =~ /pattern/; +"str" =~ /pattern/; diff --git a/t/methods/minlen/set.t b/t/methods/minlen/set.t new file mode 100644 index 0000000..51fa823 --- /dev/null +++ b/t/methods/minlen/set.t @@ -0,0 +1,14 @@ +use strict; + +use Test::More tests => 1; + +use re::engine::Plugin ( + comp => sub { + my $re = shift; + $re->minlen(length("str") + 1); # make "str" too short + }, + exec => sub { fail "exec called" }, +); + +pass "making match"; +"str" =~ /pattern/; diff --git a/t/methods/mod.t b/t/methods/mod.t new file mode 100644 index 0000000..426182d --- /dev/null +++ b/t/methods/mod.t @@ -0,0 +1,76 @@ +=pod + +Test the C or C method + +=cut + +use strict; +use feature ':5.10'; +use Test::More tests => 25; + +my @tests = ( + sub { cmp_ok shift, 'eq', '', => 'no flags' }, + sub { cmp_ok shift, 'eq', '', => '/c' }, + sub { cmp_ok shift, 'eq', '' => '/g' }, + sub { cmp_ok shift, 'eq', 'i' => '/i' }, + sub { cmp_ok shift, 'eq', 'm' => '/m' }, + sub { cmp_ok shift, 'eq', '' => '/o' }, + sub { cmp_ok shift, 'eq', 's' => '/s' }, + sub { cmp_ok shift, 'eq', 'x' => '/x' }, + sub { cmp_ok shift, 'eq', 'p' => '/p' }, + sub { like $_[0], qr/$_/ => "/$_ in $_[0]" for unpack "(Z)*", "xi" }, + sub { like $_[0], qr/$_/ => "/$_ in $_[0]" for unpack "(Z)*", "xs" }, + sub { + for (unpack "(Z)*", "cgimsxp") { + /[cg]/ and next; + like $_[0], qr/$_/ => "/$_ in $_[0]" + } + }, + sub { cmp_ok shift, 'eq', '', => '/e' }, + sub { + for (unpack "(Z)*", "egimsxp") { + /[ge]/ and next; + like $_[0], qr/$_/ => "/$_ in $_[0]"; + } + }, + + sub { cmp_ok shift, 'eq', '' => '??' }, + # Leave this as the last + ,sub { die "add more tests" } +); + +use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; + + my $t = shift @tests; + + my %mod = $re->mod; + + my $mod_str = join '', keys %mod; + + $t->($mod_str); + } +); + +# Provide a pattern that can match to avoid running into regexp +# optimizations that won't call exec on C<"" =~ //>; + +"" =~ /x/; +"" =~ /x/cg; # meaningless without /g +"" =~ /x/g; +"" =~ /x/i; +"" =~ /x/m; +"" =~ /x/o; +"" =~ /x/s; +"" =~ /x/x; +"" =~ /x/p; +"" =~ /x/xi; +"" =~ /x/xs; +"" =~ /x/cgimosxp; + +my $_ = ""; + +$_ =~ s/1/2/e; +$_ =~ s/1/2/egimosxp; +$_ =~ ??; diff --git a/t/methods/pattern/modify.t b/t/methods/pattern/modify.t new file mode 100644 index 0000000..f5eb45e --- /dev/null +++ b/t/methods/pattern/modify.t @@ -0,0 +1,18 @@ +use strict; +use Test::More tests => 1; +use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; + my $pattern = $re->pattern; + + $$pattern = "eek"; + + return 1; + }, +); + +my $sv = "ook"; +if ("ook" =~ \$sv) { + is($sv, "eek"); +} + diff --git a/t/methods/pattern/types.t b/t/methods/pattern/types.t new file mode 100644 index 0000000..8675336 --- /dev/null +++ b/t/methods/pattern/types.t @@ -0,0 +1,19 @@ +use strict; +use Test::More tests => 7; +use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; + my $pattern = $re->pattern; + + isa_ok($pattern, $str); + }, +); + +my $sv; +"SCALAR" =~ \$sv; +"REF" =~ \\$sv; +"ARRAY" =~ []; +"HASH" =~ {}; +"GLOB" =~ \*STDIN; +"CODE" =~ sub {}; +"main" =~ bless {} => "main"; diff --git a/t/methods/stash.t b/t/methods/stash.t new file mode 100644 index 0000000..b719753 --- /dev/null +++ b/t/methods/stash.t @@ -0,0 +1,29 @@ +=pod + +Test the C method + +=cut + +use strict; +use Test::More tests => 4; + +use re::engine::Plugin ( + comp => sub { + my ($re) = @_; + + my $sv = [ qw( a o e u ) ]; + + $re->stash( $sv ); + }, + exec => sub { + my ($re, $str) = @_; + + my $stash = $re->stash; + my %h = qw( 0 a 1 o 2 e 3 u ); + for (keys %h) { + is($h{$_}, $stash->[$_]); + } + } +); + +"ook" =~ /eek/; diff --git a/t/methods/str/types.t b/t/methods/str/types.t new file mode 100644 index 0000000..f79ba36 --- /dev/null +++ b/t/methods/str/types.t @@ -0,0 +1,21 @@ +use strict; +use Test::More tests => 7; +use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; + + is_deeply($str, $re->str); + + return 1; + }, +); + +my $sv; +"SCALAR" =~ \$sv; +"REF" =~ \\$sv; +"ARRAY" =~ []; +"HASH" =~ {}; +"GLOB" =~ \*STDIN; +"CODE" =~ sub {}; +"main" =~ bless {} => "main"; + diff --git a/t/methods/str/undef.t b/t/methods/str/undef.t new file mode 100644 index 0000000..34e24b7 --- /dev/null +++ b/t/methods/str/undef.t @@ -0,0 +1,14 @@ +use strict; +use Test::More tests => 3; +use re::engine::Plugin ( + comp => sub { + my ($re, $str) = @_; + + # Runs three times apperently. + is($re->str, undef, 'str is undef'); + + return; + }, +); + +qr/pattern/; diff --git a/t/num_buff/FETCH.t b/t/num_buff/FETCH.t new file mode 100644 index 0000000..39d3f6c --- /dev/null +++ b/t/num_buff/FETCH.t @@ -0,0 +1,35 @@ +use strict; +use Test::More tests => 7; + +use re::engine::Plugin ( + exec => sub { + my $re = shift; + + $re->num_captures( + FETCH => sub { + my ($re, $paren) = @_; + + my %ret = ( + -2 => 10, + -1 => 20, + 0 => 30, + 1 => 40, + ); + + $ret{$paren}; + } + ); + + 1; + }, +); + +"a" =~ /a/; + +is($`, 10, '$`'); +is(${^PREMATCH}, 10, '${^PREMATCH}'); +is($', 20, q($')); +is(${^POSTMATCH}, 20, '${^POSTMATCH}'); +is($&, 30, '$&'); +is(${^MATCH}, 30, '${^MATCH}'); +is($1, 40, '$1'); diff --git a/t/num_buff/LENGTH.t b/t/num_buff/LENGTH.t new file mode 100644 index 0000000..7e8afdd --- /dev/null +++ b/t/num_buff/LENGTH.t @@ -0,0 +1,35 @@ +use strict; +use Test::More tests => 7; + +use re::engine::Plugin ( + exec => sub { + my $re = shift; + + $re->stash( [ + 10, 10, + 20, 20, + 30, 30, + 40, + ]); + + $re->num_captures( + LENGTH => sub { + my ($re, $paren) = @_; + + shift @{ $re->stash }; + }, + ); + + 1; + }, +); + +"a" =~ /a/; + +is(length $`, 10); +is(length ${^PREMATCH}, 10); +is(length $', 20); +is(length ${^POSTMATCH}, 20); +is(length $&, 30); +is(length ${^MATCH}, 30); +is(length $1, 40); diff --git a/t/num_buff/STORE.t b/t/num_buff/STORE.t new file mode 100644 index 0000000..3d693a0 --- /dev/null +++ b/t/num_buff/STORE.t @@ -0,0 +1,40 @@ +use strict; +use Test::More tests => 14; + +use re::engine::Plugin ( + exec => sub { + my $re = shift; + + $re->stash( [ + [ -2, "a" ], + [ -2, "a" ], + [ -1, "o" ], + [ -1, "o" ], + [ 0, "e" ], + [ 0, "e" ], + [ 1, "u" ], + ]); + + $re->num_captures( + STORE => sub { + my ($re, $paren, $sv) = @_; + my $test = shift @{ $re->stash }; + + is($paren, $test->[0]); + is($sv, $test->[1]); + }, + ); + + 1; + }, +); + +"a" =~ /a/; + +$` = "a"; +${^PREMATCH} = "a"; +$' = "o"; +${^POSTMATCH} = "o"; +$& = "e"; +${^MATCH} = "e"; +$1 = "u"; diff --git a/t/taint/rx.t b/t/taint/rx.t new file mode 100644 index 0000000..acaf6df --- /dev/null +++ b/t/taint/rx.t @@ -0,0 +1,58 @@ +#!/usr/bin/env perl -T + +=pod + +Equivalent to F but uses perl regexes to untaint. + +=cut + +use strict; +use Test::More; + +BEGIN { + eval { + require Taint::Util; + Taint::Util->import; + }; + + plan $@ + ? (skip_all => "Taint::Util required for taint tests") + : (tests => 8); +} + +use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; + + ok(tainted($str) => 'matched string tainted'); + + my $one = $str; + my $two = $str; $two ~~ /(.*)/; $two = $1; + + ok(tainted($one)); + ok(!tainted($two)); + + $re->num_captures( + FETCH => sub { + my ($re, $p) = @_; + + return $one if $p == 1; + return $two if $p == 2; + }, + ); + + 1; + } +); + +my $str = "string"; +taint($str); +ok(tainted($str)); + +if ($str =~ /pattern/) { + cmp_ok $1, 'eq', $str; + ok(tainted($1) => '$1 is tainted'); + + cmp_ok $2, 'eq', $str; + ok(!tainted($2) => '$2 is untainted'); +} diff --git a/t/taint/util.t b/t/taint/util.t new file mode 100644 index 0000000..11905b3 --- /dev/null +++ b/t/taint/util.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl -T +use strict; +use Test::More; + +BEGIN { + eval { + require Taint::Util; + Taint::Util->import; + }; + + plan $@ + ? (skip_all => "Taint::Util required for taint tests") + : (tests => 8); +} + +use re::engine::Plugin ( + exec => sub { + my ($re, $str) = @_; + + ok(tainted($str) => 'matched string tainted'); + + my $one = $str; + my $two = $str; untaint($two); + + ok(tainted($one)); + ok(!tainted($two)); + + $re->num_captures( + FETCH => sub { + my ($re, $p) = @_; + + return $one if $p == 1; + return $two if $p == 2; + }, + ); + + 1; + } +); + +my $str = "string"; +taint($str); +ok(tainted($str)); + +if ($str =~ /pattern/) { + cmp_ok $1, 'eq', $str; + ok(tainted($1) => '$1 is tainted'); + + cmp_ok $2, 'eq', $str; + ok(!tainted($2) => '$2 is untainted'); +} diff --git a/typemap b/typemap new file mode 100644 index 0000000..fc3a3fa --- /dev/null +++ b/typemap @@ -0,0 +1 @@ +re::engine::Plugin T_PTROBJ \ No newline at end of file