1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#!/usr/bin/env perl
#use strict; # TODO: explore why this breaks the builtins.
# $Id: makeppbuiltin,v 1.24 2013/04/14 19:35:42 pfeiffer Exp $
package Mpp;
our $datadir;
BEGIN {
#@@setdatadir
#
# Find the location of our data directory that contains the auxiliary files.
# This is normally built into the program by install.pl, but if makepp hasn't
# been installed, then we look in the directory we were run from.
#
$datadir = $0; # Assume it's running from the same place that
# we're running from.
unless( $datadir =~ s@/[^/]+$@@ ) { # No path specified?
# See if we can find ourselves in the path.
foreach( split( /:/, $ENV{'PATH'} ), '.' ) {
# Add '.' to the path in case the user is
# running it with "perl makepp" even if
# . is not in his path.
if( -d "$_/Mpp" ) { # Found something we need?
$datadir = $_;
last;
}
}
}
$datadir or die "makepp: can't find library files\n";
$datadir = eval "use Cwd; cwd . '/$datadir'"
if $datadir =~ /^\./; # Make it absolute, if it's a relative path.
do "$datadir/Mpp/DB.pm" if exists $DB::{sh};
#@@
unshift @INC, $datadir;
}
$Mpp::Subs::rule->{MAKEFILE}{PACKAGE} = 'Mpp'; # Make it same as ours in eval_or_die
use Mpp::Utils;
use Mpp::Text ();
use Mpp::Cmds;
$Mpp::Text::helpline = 'For command info, click on sidebar "Features" and then "Builtin Commands".';
$Mpp::Text::extraman = '_builtins';
sub eval_or_die($) {
if( wantarray ) {
my @result = eval $_[0];
&maybe_die;
@result;
} else {
my $result = eval $_[0];
&maybe_die;
$result;
}
}
{ no warnings 'redefine'; *Mpp::Cmds::eval_or_die = \&eval_or_die }
# Drop in replacement for getopts, which parses and pretty prints the option
# specification.
sub getopts_help(@) {
my @opts = @_;
print STDERR "$0 options:";
shift @opts if 'HASH' eq ref $opts[0];
my %short;
for( @opts ) {
my $long = Mpp::Text::_getopts_long( $_ );
my $short = $_->[0]; # Show only the 1st short opt (e.g. --fields & --force)
$_ = (($short && !$short{$short}) ? "$_->[0], --$long" : $long) .
($_->[3] ? '=arg' : '');
$short{$short} ||= 1 if $short;
}
for( sort { lc( substr $a, 0, 1 ) cmp lc( substr $b, 0, 1 ) ||
substr( $b, 0, 1 ) cmp substr( $a, 0, 1 ) ||
$a cmp $b } @opts ) {
s/^/-/;
s/^(-.[^,])/ -$1/; # indent only long option further
print STDERR "\n $_";
}
die "\n";
}
my $re = join '|', grep { exists &{"Mpp::Cmds::$_"} && s/^c_// } keys %Mpp::Cmds::;
# This function exists so we can efficiently run many tests in one process.
sub doit {
# Look in our called name to see if it contains a builtin's name.
my $cmd = $0;
if( $0 =~ /($re)[^\/]*$/ ) { # Yes, found a builtin.
$0 = $1;
$cmd = "Mpp::Cmds::c_$0";
} else {
my $tmp;
Mpp::Text::getopts 1,
['I', qr/include(?:[-_]?dir)?/, \$tmp, 1, sub { unshift @INC, $tmp }],
[qw(M module), \$tmp, 1, sub { $tmp =~ s/=(.*)/ qw($1)/ and $tmp =~ tr/,/ /; eval_or_die "use $tmp" }],
splice @Mpp::Text::common_opts;
&Mpp::Text::help unless @ARGV;
$tmp = $0; # 1st argument must be the command name.
$0 = shift @ARGV;
unless( exists &{$cmd = "c_$0"} ) {
$cmd = "Mpp::Cmds::c_$0";
die "$tmp: $0 is not a makepp builtin command.\n" unless exists &$cmd;
}
}
if( @ARGV and $ARGV[0] eq '-?' || $ARGV[0] eq '--help' ) {
no warnings;
*Mpp::Text::getopts = \&getopts_help; # Let the seemingly normal call handle this.
}
if( !$ENV{INSTALL_LOG} && $cmd =~ /install$/ ) {
require Mpp::Makefile;
Mpp::Makefile::find_root_makefile_upwards( $CWD_INFO );
# Fill ->{ROOT} if RootMakeppfile found.
} elsif( $cmd =~ /preprocess$/ ) {
require Mpp::Makefile;
}
&$cmd( @ARGV );
}
doit;
__DATA__
[metaoption ...] command [option ...] [argument ...]
to see options do: makeppbuiltin command --help
Options depend on the command, while metaoptions are these:
|