| 1 |
#!/usr/bin/perl -w |
| 2 |
# Detect whether to run einfo.epub or edirect -info. |
| 3 |
|
| 4 |
use strict; |
| 5 |
|
| 6 |
my $usage = <<EOF; |
| 7 |
USAGE: einfo [<epub einfo options>] <filename> |
| 8 |
-or- einfo [<NCBI Entrez Direct einfo options>] |
| 9 |
|
| 10 |
per, respectively, |
| 11 |
|
| 12 |
einfo.epub -h (or man einfo.epub) |
| 13 |
einfo.ncbi -help (or man einfo) |
| 14 |
EOF |
| 15 |
|
| 16 |
if ( !@ARGV || ! -x '/usr/bin/einfo.epub' ) { |
| 17 |
ExecNCBI(); |
| 18 |
} elsif (grep /--?help/, @ARGV) { |
| 19 |
print $usage; |
| 20 |
exit 0; |
| 21 |
} |
| 22 |
|
| 23 |
my $epub_flags = 'qvdp'; |
| 24 |
my $epub_keys = 't'; |
| 25 |
|
| 26 |
my %ncbi_supported = ( |
| 27 |
'db' => 's', |
| 28 |
'dbs' => undef, |
| 29 |
'field' => undef, |
| 30 |
'fields' => undef, |
| 31 |
'link' => undef, |
| 32 |
'links' => undef, |
| 33 |
'test' => undef, |
| 34 |
'tests' => undef, |
| 35 |
'external' => undef, |
| 36 |
'ext' => undef, |
| 37 |
'email' => 's', |
| 38 |
'tool' => 's', |
| 39 |
'repeat' => undef, |
| 40 |
'repeats' => undef, |
| 41 |
'error' => undef, |
| 42 |
'errors' => undef, |
| 43 |
'help' => undef, |
| 44 |
'debug' => undef, |
| 45 |
'oldmode' => undef, |
| 46 |
'newmode' => undef, |
| 47 |
'verbose' => undef, |
| 48 |
'tranquil' => undef, |
| 49 |
'log' => undef, |
| 50 |
'immediate' => undef, |
| 51 |
'express' => undef, |
| 52 |
'base' => 's', |
| 53 |
'web' => 's', |
| 54 |
'step' => 'i', |
| 55 |
'label' => 's', |
| 56 |
'timer' => undef, |
| 57 |
'version' => undef); |
| 58 |
|
| 59 |
my %ncbi_abbrev = (); |
| 60 |
{ |
| 61 |
my @names = sort keys %ncbi_supported; |
| 62 |
unshift @names, ''; |
| 63 |
push @names, ''; |
| 64 |
my $i; |
| 65 |
my $j; |
| 66 |
for ($i = 1; $i + 1 < @names; ++$i) { |
| 67 |
my $name = $names[$i]; |
| 68 |
my $prev_name = $names[$i - 1]; |
| 69 |
my $next_name = $names[$i + 1]; |
| 70 |
for ($j = 1; $j < length($name); ++$j) { |
| 71 |
my $abbrev = substr($name, 0, $j); |
| 72 |
if ($abbrev ne substr($prev_name, 0, $j) |
| 73 |
&& $abbrev ne substr($next_name, 0, $j)) { |
| 74 |
$ncbi_abbrev{$abbrev} = $name; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
my @epub_red_flags; |
| 81 |
my @epub_yellow_flags; |
| 82 |
my $epub_value_expected; |
| 83 |
my $epub_query; |
| 84 |
my @ncbi_red_flags; |
| 85 |
my @ncbi_yellow_flags; |
| 86 |
my $ncbi_value_expected; |
| 87 |
|
| 88 |
for (@ARGV) { |
| 89 |
if (/^-\d+$/ || !/^-/) { |
| 90 |
if ( !defined $epub_value_expected ) { |
| 91 |
if (defined $epub_query) { |
| 92 |
push @epub_red_flags, "$epub_query $_"; |
| 93 |
} |
| 94 |
$epub_query = $_; |
| 95 |
} |
| 96 |
if ( !defined $ncbi_value_expected ) { |
| 97 |
push @ncbi_red_flags, $_; |
| 98 |
} |
| 99 |
undef $epub_value_expected; |
| 100 |
undef $ncbi_value_expected; |
| 101 |
} else { |
| 102 |
my $epub_type; |
| 103 |
if (defined $epub_value_expected) { |
| 104 |
push @epub_red_flags, "$epub_value_expected $_"; |
| 105 |
$epub_type = 'nominal-value'; |
| 106 |
undef $epub_value_expected; |
| 107 |
} elsif (/^-[ho]$/ || /^-[$epub_flags]*(?:[$epub_keys].+)?$/) { |
| 108 |
$epub_type = 'flag'; |
| 109 |
} elsif (/^-[$epub_flags]*([$epub_keys])$/) { |
| 110 |
$epub_type = 'key'; |
| 111 |
$epub_value_expected = "-$1"; |
| 112 |
} else { |
| 113 |
$epub_type = 'invalid'; |
| 114 |
push @epub_red_flags, $_; |
| 115 |
} |
| 116 |
|
| 117 |
my $ncbi_type; |
| 118 |
if (defined $ncbi_value_expected) { |
| 119 |
push @ncbi_red_flags, "$ncbi_value_expected $_"; |
| 120 |
$ncbi_type = 'nominal-value'; |
| 121 |
undef $ncbi_value_expected; |
| 122 |
} elsif (/^--?([^=]+)(=.*)?/) { |
| 123 |
my $base = lc($1); |
| 124 |
my $has_value = defined $2; |
| 125 |
if (exists $ncbi_abbrev{$base}) { |
| 126 |
$base = $ncbi_abbrev{$base}; |
| 127 |
push @ncbi_yellow_flags, $_; |
| 128 |
} |
| 129 |
if ( !exists $ncbi_supported{$base} |
| 130 |
|| ($has_value && !defined $ncbi_supported{$base})) { |
| 131 |
$ncbi_type = 'invalid'; |
| 132 |
push @ncbi_red_flags, $_; |
| 133 |
} else { |
| 134 |
if (lc($1) eq $base && $1 =~ /[A-Z]/) { |
| 135 |
# Worry about capitalization only if otherwise OK |
| 136 |
push @ncbi_yellow_flags, $_; |
| 137 |
} |
| 138 |
if (defined $ncbi_supported{$base} && !$has_value) { |
| 139 |
$ncbi_type = 'key'; |
| 140 |
$ncbi_value_expected = $_; |
| 141 |
} else { |
| 142 |
$ncbi_type = 'flag'; |
| 143 |
} |
| 144 |
} |
| 145 |
} else { |
| 146 |
$ncbi_type = 'invalid'; |
| 147 |
push @ncbi_red_flags, $_; |
| 148 |
} |
| 149 |
|
| 150 |
if ($epub_type =~ /^[fk]/ && $ncbi_type =~ /^[fk]/) { |
| 151 |
# Could technically be valid for einfo.epub, but far likelier |
| 152 |
# to have been intended for edirect -info. |
| 153 |
push @epub_yellow_flags, $_; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if ( !defined $epub_query ) { |
| 159 |
push @epub_red_flags, "no filename"; |
| 160 |
} |
| 161 |
if (defined $epub_value_expected) { |
| 162 |
push @epub_red_flags, "$epub_value_expected with no value"; |
| 163 |
} |
| 164 |
if (defined $ncbi_value_expected) { |
| 165 |
push @ncbi_red_flags, "$ncbi_value_expected with no value"; |
| 166 |
} |
| 167 |
|
| 168 |
my $epub_red = @epub_red_flags; |
| 169 |
my $epub_ylw = @epub_yellow_flags; |
| 170 |
my $ncbi_red = @ncbi_red_flags; |
| 171 |
my $ncbi_ylw = @ncbi_yellow_flags; |
| 172 |
|
| 173 |
if ($epub_red && !$ncbi_red) { |
| 174 |
# clear-cut, even if there happen to be NCBI yellow flags |
| 175 |
ExecNCBI(); |
| 176 |
} elsif ($ncbi_red && !$epub_red) { |
| 177 |
ExecEpub(); |
| 178 |
} |
| 179 |
|
| 180 |
my $epub_misgivings = "EPUB misgivings ($epub_red major, $epub_ylw minor):"; |
| 181 |
my $ncbi_misgivings = "NCBI misgivings ($ncbi_red major, $ncbi_ylw minor):"; |
| 182 |
|
| 183 |
for (@epub_red_flags) { |
| 184 |
$epub_misgivings .= "\n $_ (major)"; |
| 185 |
} |
| 186 |
for (@epub_yellow_flags) { |
| 187 |
$epub_misgivings .= "\n $_ (minor)"; |
| 188 |
} |
| 189 |
for (@ncbi_red_flags) { |
| 190 |
$ncbi_misgivings .= "\n $_ (major)"; |
| 191 |
} |
| 192 |
for (@ncbi_yellow_flags) { |
| 193 |
$ncbi_misgivings .= "\n $_ (minor)"; |
| 194 |
} |
| 195 |
|
| 196 |
if ($ncbi_red > $epub_red |
| 197 |
|| ($ncbi_red == $epub_red && $ncbi_ylw > $epub_ylw)) { |
| 198 |
if ($epub_red || $epub_ylw) { |
| 199 |
warn <<EOF; |
| 200 |
Launching EPUB einfo rather than NCBI einfo despite misgivings, |
| 201 |
due to more severe misgivings about NCBI syntax compatibility. If you |
| 202 |
meant to run NCBI einfo, please explicitly run it via edirect -info. |
| 203 |
$ncbi_misgivings |
| 204 |
$epub_misgivings |
| 205 |
EOF |
| 206 |
} |
| 207 |
ExecEpub(); |
| 208 |
} |
| 209 |
|
| 210 |
if ($epub_red > $ncbi_red |
| 211 |
|| ($epub_red == $ncbi_red && $epub_ylw > $ncbi_ylw)) { |
| 212 |
if ($ncbi_red || $ncbi_ylw) { |
| 213 |
warn <<EOF; |
| 214 |
Launching NCBI einfo rather than EPUB einfo despite misgivings, |
| 215 |
due to more severe misgivings about EPUB syntax compatibility. If you |
| 216 |
meant to run EPUB einfo, please explicitly run it via einfo.epub. |
| 217 |
$epub_misgivings |
| 218 |
$ncbi_misgivings |
| 219 |
EOF |
| 220 |
} |
| 221 |
ExecNCBI(); |
| 222 |
} |
| 223 |
|
| 224 |
if ($epub_red) { |
| 225 |
die <<EOF; |
| 226 |
Usage equally bad for both EPUB einfo and NCBI einfo. Please double |
| 227 |
check your usage or explicitly run einfo.epub or edirect -info. |
| 228 |
$epub_misgivings |
| 229 |
$ncbi_misgivings |
| 230 |
|
| 231 |
$usage |
| 232 |
EOF |
| 233 |
} elsif ($epub_ylw) { |
| 234 |
die <<EOF; |
| 235 |
Usage equally suspect (but still technically valid, at least to first |
| 236 |
approximation) for both EPUB einfo and NCBI einfo. Please double |
| 237 |
check your usage or explicitly run einfo.epub or edirect -info. |
| 238 |
$epub_misgivings |
| 239 |
$ncbi_misgivings |
| 240 |
|
| 241 |
$usage |
| 242 |
EOF |
| 243 |
} else { |
| 244 |
die <<EOF; |
| 245 |
Usage apparently valid (at least to first approximation) for both |
| 246 |
EPUB einfo and NCBI einfo. Please disambiguate your usage or |
| 247 |
explicitly run einfo.epub or edirect -info. |
| 248 |
EOF |
| 249 |
} |
| 250 |
|
| 251 |
sub ExecEpub |
| 252 |
{ |
| 253 |
# exec {'/usr/bin/einfo.epub'} ('/usr/bin/einfo', @ARGV); |
| 254 |
exec('/usr/bin/einfo.epub', @ARGV); |
| 255 |
} |
| 256 |
sub ExecNCBI |
| 257 |
{ |
| 258 |
exec('/usr/bin/einfo.ncbi', @ARGV); |
| 259 |
} |