PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Runtime/Tests/LinqDlrTests/testenv/perl/lib/File/DosGlob.pm

#
Perl | 254 lines | 165 code | 54 blank | 35 comment | 40 complexity | dd69935bcc3dfe420bb8e4e8436e5a2f MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. #!perl -w
  2. #
  3. # Documentation at the __END__
  4. #
  5. package File::DosGlob;
  6. sub doglob {
  7. my $cond = shift;
  8. my @retval = ();
  9. #print "doglob: ", join('|', @_), "\n";
  10. OUTER:
  11. for my $arg (@_) {
  12. local $_ = $arg;
  13. my @matched = ();
  14. my @globdirs = ();
  15. my $head = '.';
  16. my $sepchr = '/';
  17. next OUTER unless defined $_ and $_ ne '';
  18. # if arg is within quotes strip em and do no globbing
  19. if (/^"(.*)"\z/s) {
  20. $_ = $1;
  21. if ($cond eq 'd') { push(@retval, $_) if -d $_ }
  22. else { push(@retval, $_) if -e $_ }
  23. next OUTER;
  24. }
  25. # wildcards with a drive prefix such as h:*.pm must be changed
  26. # to h:./*.pm to expand correctly
  27. if (m|^([A-Za-z]:)[^/\\]|s) {
  28. substr($_,0,2) = $1 . "./";
  29. }
  30. if (m|^(.*)([\\/])([^\\/]*)\z|s) {
  31. my $tail;
  32. ($head, $sepchr, $tail) = ($1,$2,$3);
  33. #print "div: |$head|$sepchr|$tail|\n";
  34. push (@retval, $_), next OUTER if $tail eq '';
  35. if ($head =~ /[*?]/) {
  36. @globdirs = doglob('d', $head);
  37. push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
  38. next OUTER if @globdirs;
  39. }
  40. $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:\z/s;
  41. $_ = $tail;
  42. }
  43. #
  44. # If file component has no wildcards, we can avoid opendir
  45. unless (/[*?]/) {
  46. $head = '' if $head eq '.';
  47. $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
  48. $head .= $_;
  49. if ($cond eq 'd') { push(@retval,$head) if -d $head }
  50. else { push(@retval,$head) if -e $head }
  51. next OUTER;
  52. }
  53. opendir(D, $head) or next OUTER;
  54. my @leaves = readdir D;
  55. closedir D;
  56. $head = '' if $head eq '.';
  57. $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
  58. # escape regex metachars but not glob chars
  59. s:([].+^\-\${}[|]):\\$1:g;
  60. # and convert DOS-style wildcards to regex
  61. s/\*/.*/g;
  62. s/\?/.?/g;
  63. #print "regex: '$_', head: '$head'\n";
  64. my $matchsub = eval 'sub { $_[0] =~ m|^' . $_ . '\\z|ios }';
  65. warn($@), next OUTER if $@;
  66. INNER:
  67. for my $e (@leaves) {
  68. next INNER if $e eq '.' or $e eq '..';
  69. next INNER if $cond eq 'd' and ! -d "$head$e";
  70. push(@matched, "$head$e"), next INNER if &$matchsub($e);
  71. #
  72. # [DOS compatibility special case]
  73. # Failed, add a trailing dot and try again, but only
  74. # if name does not have a dot in it *and* pattern
  75. # has a dot *and* name is shorter than 9 chars.
  76. #
  77. if (index($e,'.') == -1 and length($e) < 9
  78. and index($_,'\\.') != -1) {
  79. push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
  80. }
  81. }
  82. push @retval, @matched if @matched;
  83. }
  84. return @retval;
  85. }
  86. #
  87. # this can be used to override CORE::glob in a specific
  88. # package by saying C<use File::DosGlob 'glob';> in that
  89. # namespace.
  90. #
  91. # context (keyed by second cxix arg provided by core)
  92. my %iter;
  93. my %entries;
  94. sub glob {
  95. my $pat = shift;
  96. my $cxix = shift;
  97. my @pat;
  98. # glob without args defaults to $_
  99. $pat = $_ unless defined $pat;
  100. # extract patterns
  101. if ($pat =~ /\s/) {
  102. require Text::ParseWords;
  103. @pat = Text::ParseWords::parse_line('\s+',0,$pat);
  104. }
  105. else {
  106. push @pat, $pat;
  107. }
  108. # assume global context if not provided one
  109. $cxix = '_G_' unless defined $cxix;
  110. $iter{$cxix} = 0 unless exists $iter{$cxix};
  111. # if we're just beginning, do it all first
  112. if ($iter{$cxix} == 0) {
  113. $entries{$cxix} = [doglob(1,@pat)];
  114. }
  115. # chuck it all out, quick or slow
  116. if (wantarray) {
  117. delete $iter{$cxix};
  118. return @{delete $entries{$cxix}};
  119. }
  120. else {
  121. if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
  122. return shift @{$entries{$cxix}};
  123. }
  124. else {
  125. # return undef for EOL
  126. delete $iter{$cxix};
  127. delete $entries{$cxix};
  128. return undef;
  129. }
  130. }
  131. }
  132. sub import {
  133. my $pkg = shift;
  134. return unless @_;
  135. my $sym = shift;
  136. my $callpkg = ($sym =~ s/^GLOBAL_//s ? 'CORE::GLOBAL' : caller(0));
  137. *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
  138. }
  139. 1;
  140. __END__
  141. =head1 NAME
  142. File::DosGlob - DOS like globbing and then some
  143. =head1 SYNOPSIS
  144. require 5.004;
  145. # override CORE::glob in current package
  146. use File::DosGlob 'glob';
  147. # override CORE::glob in ALL packages (use with extreme caution!)
  148. use File::DosGlob 'GLOBAL_glob';
  149. @perlfiles = glob "..\\pe?l/*.p?";
  150. print <..\\pe?l/*.p?>;
  151. # from the command line (overrides only in main::)
  152. > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
  153. =head1 DESCRIPTION
  154. A module that implements DOS-like globbing with a few enhancements.
  155. It is largely compatible with perlglob.exe (the M$ setargv.obj
  156. version) in all but one respect--it understands wildcards in
  157. directory components.
  158. For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
  159. that it will find something like '..\lib\File/DosGlob.pm' alright).
  160. Note that all path components are case-insensitive, and that
  161. backslashes and forward slashes are both accepted, and preserved.
  162. You may have to double the backslashes if you are putting them in
  163. literally, due to double-quotish parsing of the pattern by perl.
  164. Spaces in the argument delimit distinct patterns, so
  165. C<glob('*.exe *.dll')> globs all filenames that end in C<.exe>
  166. or C<.dll>. If you want to put in literal spaces in the glob
  167. pattern, you can escape them with either double quotes, or backslashes.
  168. e.g. C<glob('c:/"Program Files"/*/*.dll')>, or
  169. C<glob('c:/Program\ Files/*/*.dll')>. The argument is tokenized using
  170. C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
  171. of the quoting rules used.
  172. Extending it to csh patterns is left as an exercise to the reader.
  173. =head1 EXPORTS (by request only)
  174. glob()
  175. =head1 BUGS
  176. Should probably be built into the core, and needs to stop
  177. pandering to DOS habits. Needs a dose of optimizium too.
  178. =head1 AUTHOR
  179. Gurusamy Sarathy <gsar@activestate.com>
  180. =head1 HISTORY
  181. =over 4
  182. =item *
  183. Support for globally overriding glob() (GSAR 3-JUN-98)
  184. =item *
  185. Scalar context, independent iterator context fixes (GSAR 15-SEP-97)
  186. =item *
  187. A few dir-vs-file optimizations result in glob importation being
  188. 10 times faster than using perlglob.exe, and using perlglob.bat is
  189. only twice as slow as perlglob.exe (GSAR 28-MAY-97)
  190. =item *
  191. Several cleanups prompted by lack of compatible perlglob.exe
  192. under Borland (GSAR 27-MAY-97)
  193. =item *
  194. Initial version (GSAR 20-FEB-97)
  195. =back
  196. =head1 SEE ALSO
  197. perl
  198. perlglob.bat
  199. Text::ParseWords
  200. =cut