Once again syncing after too long an absence
[p5sagit/p5-mst-13.2.git] / lib / File / DosGlob.pm
1 #!perl -w
2
3 # use strict fails
4 #Can't use string ("main::glob") as a symbol ref while "strict refs" in use at /usr/lib/perl5/5.005/File/DosGlob.pm line 191.
5
6 #
7 # Documentation at the __END__
8 #
9
10 package File::DosGlob;
11
12 our $VERSION = '1.00';
13 use strict;
14
15 sub doglob {
16     my $cond = shift;
17     my @retval = ();
18     #print "doglob: ", join('|', @_), "\n";
19   OUTER:
20     for my $pat (@_) {
21         my @matched = ();
22         my @globdirs = ();
23         my $head = '.';
24         my $sepchr = '/';
25         my $tail;
26         next OUTER unless defined $pat and $pat ne '';
27         # if arg is within quotes strip em and do no globbing
28         if ($pat =~ /^"(.*)"\z/s) {
29             $pat = $1;
30             if ($cond eq 'd') { push(@retval, $pat) if -d $pat }
31             else              { push(@retval, $pat) if -e $pat }
32             next OUTER;
33         }
34         # wildcards with a drive prefix such as h:*.pm must be changed
35         # to h:./*.pm to expand correctly
36         if ($pat =~ m|^([A-Za-z]:)[^/\\]|s) {
37             substr($_,0,2) = $1 . "./";
38         }
39         if ($pat =~ m|^(.*)([\\/])([^\\/]*)\z|s) {
40             ($head, $sepchr, $tail) = ($1,$2,$3);
41             #print "div: |$head|$sepchr|$tail|\n";
42             push (@retval, $pat), next OUTER if $tail eq '';
43             if ($head =~ /[*?]/) {
44                 @globdirs = doglob('d', $head);
45                 push(@retval, doglob($cond, map {"$_$sepchr$tail"} @globdirs)),
46                     next OUTER if @globdirs;
47             }
48             $head .= $sepchr if $head eq '' or $head =~ /^[A-Za-z]:\z/s;
49             $pat = $tail;
50         }
51         #
52         # If file component has no wildcards, we can avoid opendir
53         unless ($pat =~ /[*?]/) {
54             $head = '' if $head eq '.';
55             $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
56             $head .= $pat;
57             if ($cond eq 'd') { push(@retval,$head) if -d $head }
58             else              { push(@retval,$head) if -e $head }
59             next OUTER;
60         }
61         opendir(D, $head) or next OUTER;
62         my @leaves = readdir D;
63         closedir D;
64         $head = '' if $head eq '.';
65         $head .= $sepchr unless $head eq '' or substr($head,-1) eq $sepchr;
66
67         # escape regex metachars but not glob chars
68         $pat =~ s:([].+^\-\${}[|]):\\$1:g;
69         # and convert DOS-style wildcards to regex
70         $pat =~ s/\*/.*/g;
71         $pat =~ s/\?/.?/g;
72
73         #print "regex: '$pat', head: '$head'\n";
74         my $matchsub = sub { $_[0] =~ m|^$pat\z|is };
75       INNER:
76         for my $e (@leaves) {
77             next INNER if $e eq '.' or $e eq '..';
78             next INNER if $cond eq 'd' and ! -d "$head$e";
79             push(@matched, "$head$e"), next INNER if &$matchsub($e);
80             #
81             # [DOS compatibility special case]
82             # Failed, add a trailing dot and try again, but only
83             # if name does not have a dot in it *and* pattern
84             # has a dot *and* name is shorter than 9 chars.
85             #
86             if (index($e,'.') == -1 and length($e) < 9
87                 and index($pat,'\\.') != -1) {
88                 push(@matched, "$head$e"), next INNER if &$matchsub("$e.");
89             }
90         }
91         push @retval, @matched if @matched;
92     }
93     return @retval;
94 }
95
96 #
97 # this can be used to override CORE::glob in a specific
98 # package by saying C<use File::DosGlob 'glob';> in that
99 # namespace.
100 #
101
102 # context (keyed by second cxix arg provided by core)
103 my %iter;
104 my %entries;
105
106 sub glob {
107     my($pat,$cxix) = @_;
108     my @pat;
109
110     # glob without args defaults to $_
111     $pat = $_ unless defined $pat;
112
113     # extract patterns
114     if ($pat =~ /\s/) {
115         require Text::ParseWords;
116         @pat = Text::ParseWords::parse_line('\s+',0,$pat);
117     }
118     else {
119         push @pat, $pat;
120     }
121
122     # Mike Mestnik: made to do abc{1,2,3} == abc1 abc2 abc3.
123     #   abc3 will be the original {3} (and drop the {}).
124     #   abc1 abc2 will be put in @appendpat.
125     # This was just the esiest way, not nearly the best.
126     REHASH: {
127         my @appendpat = ();
128         for (@pat) {
129             # There must be a "," I.E. abc{efg} is not what we want.
130             while ( /^(.*)(?<!\\)\{(.*?)(?<!\\)\,.*?(?<!\\)\}(.*)$/ ) {
131                 my ($start, $match, $end) = ($1, $2, $3);
132                 #print "Got: \n\t$start\n\t$match\n\t$end\n";
133                 my $tmp = "$start$match$end";
134                 while ( $tmp =~ s/^(.*?)(?<!\\)\{(?:.*(?<!\\)\,)?(.*\Q$match\E.*?)(?:(?<!\\)\,.*)?(?<!\\)\}(.*)$/$1$2$3/ ) {
135                     #print "Striped: $tmp\n";
136                     #  these expanshions will be preformed by the original,
137                     #  when we call REHASH.
138                 }
139                 push @appendpat, ("$tmp");
140                 s/^\Q$start\E(?<!\\)\{\Q$match\E(?<!\\)\,/$start\{/;
141                 if ( /^\Q$start\E(?<!\\)\{(?!.*?(?<!\\)\,.*?\Q$end\E$)(.*)(?<!\\)\}\Q$end\E$/ ) {
142                     $match = $1;
143                     #print "GOT: \n\t$start\n\t$match\n\t$end\n\n";
144                     $_ = "$start$match$end";
145                 }
146             }
147             #print "Sould have "GOT" vs "Got"!\n";
148                 #FIXME: There should be checking for this.
149                 #  How or what should be done about failure is beond me.
150         }
151         if ( $#appendpat != -1
152                 ) {
153             #print "LOOP\n";
154             #FIXME: Max loop, no way! :")
155             for ( @appendpat ) {
156                 push @pat, $_;
157             }
158             goto REHASH;
159         }
160     }
161     for ( @pat ) {
162         s/\\{/{/g;
163         s/\\}/}/g;
164         s/\\,/,/g;
165     }
166     #print join ("\n", @pat). "\n";
167  
168     # assume global context if not provided one
169     $cxix = '_G_' unless defined $cxix;
170     $iter{$cxix} = 0 unless exists $iter{$cxix};
171
172     # if we're just beginning, do it all first
173     if ($iter{$cxix} == 0) {
174         $entries{$cxix} = [doglob(1,@pat)];
175     }
176
177     # chuck it all out, quick or slow
178     if (wantarray) {
179         delete $iter{$cxix};
180         return @{delete $entries{$cxix}};
181     }
182     else {
183         if ($iter{$cxix} = scalar @{$entries{$cxix}}) {
184             return shift @{$entries{$cxix}};
185         }
186         else {
187             # return undef for EOL
188             delete $iter{$cxix};
189             delete $entries{$cxix};
190             return undef;
191         }
192     }
193 }
194
195 {
196     no strict 'refs';
197
198     sub import {
199     my $pkg = shift;
200     return unless @_;
201     my $sym = shift;
202     my $callpkg = ($sym =~ s/^GLOBAL_//s ? 'CORE::GLOBAL' : caller(0));
203     *{$callpkg.'::'.$sym} = \&{$pkg.'::'.$sym} if $sym eq 'glob';
204     }
205 }
206 1;
207
208 __END__
209
210 =head1 NAME
211
212 File::DosGlob - DOS like globbing and then some
213
214 =head1 SYNOPSIS
215
216     require 5.004;
217
218     # override CORE::glob in current package
219     use File::DosGlob 'glob';
220
221     # override CORE::glob in ALL packages (use with extreme caution!)
222     use File::DosGlob 'GLOBAL_glob';
223
224     @perlfiles = glob  "..\\pe?l/*.p?";
225     print <..\\pe?l/*.p?>;
226
227     # from the command line (overrides only in main::)
228     > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
229
230 =head1 DESCRIPTION
231
232 A module that implements DOS-like globbing with a few enhancements.
233 It is largely compatible with perlglob.exe (the M$ setargv.obj
234 version) in all but one respect--it understands wildcards in
235 directory components.
236
237 For example, C<<..\\l*b\\file/*glob.p?>> will work as expected (in
238 that it will find something like '..\lib\File/DosGlob.pm' alright).
239 Note that all path components are case-insensitive, and that
240 backslashes and forward slashes are both accepted, and preserved.
241 You may have to double the backslashes if you are putting them in
242 literally, due to double-quotish parsing of the pattern by perl.
243
244 Spaces in the argument delimit distinct patterns, so
245 C<glob('*.exe *.dll')> globs all filenames that end in C<.exe>
246 or C<.dll>.  If you want to put in literal spaces in the glob
247 pattern, you can escape them with either double quotes, or backslashes.
248 e.g. C<glob('c:/"Program Files"/*/*.dll')>, or
249 C<glob('c:/Program\ Files/*/*.dll')>.  The argument is tokenized using
250 C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
251 of the quoting rules used.
252
253 Extending it to csh patterns is left as an exercise to the reader.
254
255 =head1 EXPORTS (by request only)
256
257 glob()
258
259 =head1 BUGS
260
261 Should probably be built into the core, and needs to stop
262 pandering to DOS habits.  Needs a dose of optimizium too.
263
264 =head1 AUTHOR
265
266 Gurusamy Sarathy <gsar@activestate.com>
267
268 =head1 HISTORY
269
270 =over 4
271
272 =item *
273
274 Support for globally overriding glob() (GSAR 3-JUN-98)
275
276 =item *
277
278 Scalar context, independent iterator context fixes (GSAR 15-SEP-97)
279
280 =item *
281
282 A few dir-vs-file optimizations result in glob importation being
283 10 times faster than using perlglob.exe, and using perlglob.bat is
284 only twice as slow as perlglob.exe (GSAR 28-MAY-97)
285
286 =item *
287
288 Several cleanups prompted by lack of compatible perlglob.exe
289 under Borland (GSAR 27-MAY-97)
290
291 =item *
292
293 Initial version (GSAR 20-FEB-97)
294
295 =back
296
297 =head1 SEE ALSO
298
299 perl
300
301 perlglob.bat
302
303 Text::ParseWords
304
305 =cut
306