Home | History | Annotate | Line # | Download | only in OpenSSL
      1  1.1  christos # Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
      2  1.1  christos #
      3  1.1  christos # Licensed under the Apache License 2.0 (the "License").  You may not use
      4  1.1  christos # this file except in compliance with the License.  You can obtain a copy
      5  1.1  christos # in the file LICENSE in the source distribution or at
      6  1.1  christos # https://www.openssl.org/source/license.html
      7  1.1  christos 
      8  1.1  christos =head1 NAME
      9  1.1  christos 
     10  1.1  christos OpenSSL::fallback - push directories to the end of @INC at compile time
     11  1.1  christos 
     12  1.1  christos =cut
     13  1.1  christos 
     14  1.1  christos package OpenSSL::fallback;
     15  1.1  christos 
     16  1.1  christos use strict;
     17  1.1  christos use warnings;
     18  1.1  christos use Carp;
     19  1.1  christos 
     20  1.1  christos our $VERSION = '0.01';
     21  1.1  christos 
     22  1.1  christos =head1 SYNOPSIS
     23  1.1  christos 
     24  1.1  christos     use OpenSSL::fallback LIST;
     25  1.1  christos 
     26  1.1  christos =head1 DESCRIPTION
     27  1.1  christos 
     28  1.1  christos This small simple module simplifies the addition of fallback directories
     29  1.1  christos in @INC at compile time.
     30  1.1  christos 
     31  1.1  christos It is used to add extra directories at the end of perl's search path so
     32  1.1  christos that later "use" or "require" statements will find modules which are not
     33  1.1  christos located on perl's default search path.
     34  1.1  christos 
     35  1.1  christos This is similar to L<lib>, except the paths are I<appended> to @INC rather
     36  1.1  christos than prepended, thus allowing the use of a newer module on perl's default
     37  1.1  christos search path if there is one.
     38  1.1  christos 
     39  1.1  christos =head1 CAVEAT
     40  1.1  christos 
     41  1.1  christos Just like with B<lib>, this only works with Unix filepaths.
     42  1.1  christos Just like with L<lib>, this doesn't mean that it only works on Unix, but that
     43  1.1  christos non-Unix users must first translate their file paths to Unix conventions.
     44  1.1  christos 
     45  1.1  christos     # VMS users wanting to put [.my.stuff] into their @INC should write:
     46  1.1  christos     use fallback 'my/stuff';
     47  1.1  christos 
     48  1.1  christos =head1 NOTES
     49  1.1  christos 
     50  1.1  christos If you try to add a file to @INC as follows, you will be warned, and the file
     51  1.1  christos will be ignored:
     52  1.1  christos 
     53  1.1  christos     use fallback 'file.txt';
     54  1.1  christos 
     55  1.1  christos The sole exception is the file F<MODULES.txt>, which must contain a list of
     56  1.1  christos sub-directories relative to the location of that F<MODULES.txt> file.
     57  1.1  christos All these sub-directories will be appended to @INC.
     58  1.1  christos 
     59  1.1  christos =cut
     60  1.1  christos 
     61  1.1  christos # Forward declare
     62  1.1  christos sub glob;
     63  1.1  christos 
     64  1.1  christos use constant DEBUG => 0;
     65  1.1  christos 
     66  1.1  christos sub import {
     67  1.1  christos     shift;                      # Skip module name
     68  1.1  christos 
     69  1.1  christos     foreach (@_) {
     70  1.1  christos         my $path = $_;
     71  1.1  christos 
     72  1.1  christos         if ($path eq '') {
     73  1.1  christos             carp "Empty compile time value given to use fallback";
     74  1.1  christos             next;
     75  1.1  christos         }
     76  1.1  christos 
     77  1.1  christos         print STDERR "DEBUG: $path\n" if DEBUG;
     78  1.1  christos 
     79  1.1  christos         unless (-e $path
     80  1.1  christos                 && ($path =~ m/(?:^|\/)MODULES.txt/ || -d $path)) {
     81  1.1  christos             croak "Parameter to use fallback must be a directory, not a file";
     82  1.1  christos             next;
     83  1.1  christos         }
     84  1.1  christos 
     85  1.1  christos         my @dirs = ();
     86  1.1  christos         if (-f $path) {         # It's a MODULES.txt file
     87  1.1  christos             (my $dir = $path) =~ s|/[^/]*$||; # quick dirname
     88  1.1  christos             open my $fh, $path or die "Could not open $path: $!\n";
     89  1.1  christos             while (my $l = <$fh>) {
     90  1.1  christos                 $l =~ s|\R$||;        # Better chomp
     91  1.1  christos                 my $d = "$dir/$l";
     92  1.1  christos                 my $checked = $d;
     93  1.1  christos 
     94  1.1  christos                 if ($^O eq 'VMS') {
     95  1.1  christos                     # Some VMS unpackers replace periods with underscores
     96  1.1  christos                     # We must be real careful not to convert the directories
     97  1.1  christos                     # '.' and '..', though.
     98  1.1  christos                     $checked =
     99  1.1  christos                         join('/',
    100  1.1  christos                              map { my $x = $_;
    101  1.1  christos                                    $x =~ s|\.|_|g
    102  1.1  christos                                        if ($x ne '..' && $x ne '.');
    103  1.1  christos                                    $x }
    104  1.1  christos                              split(m|/|, $checked))
    105  1.1  christos                         unless -e $checked && -d $checked;
    106  1.1  christos                 }
    107  1.1  christos                 croak "All lines in $path must be a directory, not a file: $l"
    108  1.1  christos                     unless -e $checked && -d $checked;
    109  1.1  christos                 push @INC, $checked;
    110  1.1  christos             }
    111  1.1  christos         } else {                # It's a directory
    112  1.1  christos             push @INC, $path;
    113  1.1  christos         }
    114  1.1  christos     }
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos =head1 SEE ALSO
    118  1.1  christos 
    119  1.1  christos L<FindBin> - optional module which deals with paths relative to the source
    120  1.1  christos file.
    121  1.1  christos 
    122  1.1  christos =head1 AUTHOR
    123  1.1  christos 
    124  1.1  christos Richard Levitte, 2019
    125  1.1  christos 
    126  1.1  christos =cut
    127  1.1  christos 
    128