Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
6.1 KiB

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