Leaked source code of windows server 2003
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.

448 lines
12 KiB

  1. # NOTE: Derived from ../LIB\Getopt\Long.pm.
  2. # Changes made here will be lost when autosplit is run again.
  3. # See AutoSplit.pm.
  4. package Getopt::Long;
  5. #line 216 "../LIB\Getopt\Long.pm (autosplit into ..\lib\auto\Getopt\Long\GetOptions.al)"
  6. ################ AutoLoading subroutines ################
  7. # RCS Status : $Id: GetoptLongAl.pl,v 2.30 2001-01-31 10:21:11+01 jv Exp $
  8. # Author : Johan Vromans
  9. # Created On : Fri Mar 27 11:50:30 1998
  10. # Last Modified By: Johan Vromans
  11. # Last Modified On: Tue Dec 26 18:01:16 2000
  12. # Update Count : 98
  13. # Status : Released
  14. sub GetOptions {
  15. my @optionlist = @_; # local copy of the option descriptions
  16. my $argend = '--'; # option list terminator
  17. my %opctl = (); # table of arg.specs (long and abbrevs)
  18. my %bopctl = (); # table of arg.specs (bundles)
  19. my $pkg = $caller || (caller)[0]; # current context
  20. # Needed if linkage is omitted.
  21. my %aliases= (); # alias table
  22. my @ret = (); # accum for non-options
  23. my %linkage; # linkage
  24. my $userlinkage; # user supplied HASH
  25. my $opt; # current option
  26. my $genprefix = $genprefix; # so we can call the same module many times
  27. my @opctl; # the possible long option names
  28. $error = '';
  29. print STDERR ("GetOpt::Long $Getopt::Long::VERSION ",
  30. "called from package \"$pkg\".",
  31. "\n ",
  32. 'GetOptionsAl $Revision: 2.30 $ ',
  33. "\n ",
  34. "ARGV: (@ARGV)",
  35. "\n ",
  36. "autoabbrev=$autoabbrev,".
  37. "bundling=$bundling,",
  38. "getopt_compat=$getopt_compat,",
  39. "gnu_compat=$gnu_compat,",
  40. "order=$order,",
  41. "\n ",
  42. "ignorecase=$ignorecase,",
  43. "passthrough=$passthrough,",
  44. "genprefix=\"$genprefix\".",
  45. "\n")
  46. if $debug;
  47. # Check for ref HASH as first argument.
  48. # First argument may be an object. It's OK to use this as long
  49. # as it is really a hash underneath.
  50. $userlinkage = undef;
  51. if ( ref($optionlist[0]) and
  52. "$optionlist[0]" =~ /^(?:.*\=)?HASH\([^\(]*\)$/ ) {
  53. $userlinkage = shift (@optionlist);
  54. print STDERR ("=> user linkage: $userlinkage\n") if $debug;
  55. }
  56. # See if the first element of the optionlist contains option
  57. # starter characters.
  58. # Be careful not to interpret '<>' as option starters.
  59. if ( $optionlist[0] =~ /^\W+$/
  60. && !($optionlist[0] eq '<>'
  61. && @optionlist > 0
  62. && ref($optionlist[1])) ) {
  63. $genprefix = shift (@optionlist);
  64. # Turn into regexp. Needs to be parenthesized!
  65. $genprefix =~ s/(\W)/\\$1/g;
  66. $genprefix = "([" . $genprefix . "])";
  67. }
  68. # Verify correctness of optionlist.
  69. %opctl = ();
  70. %bopctl = ();
  71. while ( @optionlist > 0 ) {
  72. my $opt = shift (@optionlist);
  73. # Strip leading prefix so people can specify "--foo=i" if they like.
  74. $opt = $+ if $opt =~ /^$genprefix+(.*)$/s;
  75. if ( $opt eq '<>' ) {
  76. if ( (defined $userlinkage)
  77. && !(@optionlist > 0 && ref($optionlist[0]))
  78. && (exists $userlinkage->{$opt})
  79. && ref($userlinkage->{$opt}) ) {
  80. unshift (@optionlist, $userlinkage->{$opt});
  81. }
  82. unless ( @optionlist > 0
  83. && ref($optionlist[0]) && ref($optionlist[0]) eq 'CODE' ) {
  84. $error .= "Option spec <> requires a reference to a subroutine\n";
  85. next;
  86. }
  87. $linkage{'<>'} = shift (@optionlist);
  88. next;
  89. }
  90. # Match option spec. Allow '?' as an alias only.
  91. if ( $opt !~ /^((\w+[-\w]*)(\|(\?|\w[-\w]*)?)*)?([!~+]|[=:][infse][@%]?)?$/ ) {
  92. $error .= "Error in option spec: \"$opt\"\n";
  93. next;
  94. }
  95. my ($o, $c, $a) = ($1, $5);
  96. $c = '' unless defined $c;
  97. # $linko keeps track of the primary name the user specified.
  98. # This name will be used for the internal or external linkage.
  99. # In other words, if the user specifies "FoO|BaR", it will
  100. # match any case combinations of 'foo' and 'bar', but if a global
  101. # variable needs to be set, it will be $opt_FoO in the exact case
  102. # as specified.
  103. my $linko;
  104. if ( ! defined $o ) {
  105. # empty -> '-' option
  106. $linko = $o = '';
  107. $opctl{''} = $c;
  108. $bopctl{''} = $c if $bundling;
  109. }
  110. else {
  111. # Handle alias names
  112. my @o = split (/\|/, $o);
  113. $linko = $o = $o[0];
  114. # Force an alias if the option name is not locase.
  115. $a = $o unless $o eq lc($o);
  116. $o = lc ($o)
  117. if $ignorecase > 1
  118. || ($ignorecase
  119. && ($bundling ? length($o) > 1 : 1));
  120. foreach ( @o ) {
  121. if ( $bundling && length($_) == 1 ) {
  122. $_ = lc ($_) if $ignorecase > 1;
  123. if ( $c eq '!' ) {
  124. $opctl{"no$_"} = $c;
  125. warn ("Ignoring '!' modifier for short option $_\n");
  126. $opctl{$_} = $bopctl{$_} = '';
  127. }
  128. else {
  129. $opctl{$_} = $bopctl{$_} = $c;
  130. }
  131. }
  132. else {
  133. $_ = lc ($_) if $ignorecase;
  134. if ( $c eq '!' ) {
  135. $opctl{"no$_"} = $c;
  136. $opctl{$_} = ''
  137. }
  138. else {
  139. $opctl{$_} = $c;
  140. }
  141. }
  142. if ( defined $a ) {
  143. # Note alias.
  144. $aliases{$_} = $a;
  145. }
  146. else {
  147. # Set primary name.
  148. $a = $_;
  149. }
  150. }
  151. }
  152. # If no linkage is supplied in the @optionlist, copy it from
  153. # the userlinkage if available.
  154. if ( defined $userlinkage ) {
  155. unless ( @optionlist > 0 && ref($optionlist[0]) ) {
  156. if ( exists $userlinkage->{$linko} &&
  157. ref($userlinkage->{$linko}) ) {
  158. print STDERR ("=> found userlinkage for \"$linko\": ",
  159. "$userlinkage->{$linko}\n")
  160. if $debug;
  161. unshift (@optionlist, $userlinkage->{$linko});
  162. }
  163. else {
  164. # Do nothing. Being undefined will be handled later.
  165. next;
  166. }
  167. }
  168. }
  169. # Copy the linkage. If omitted, link to global variable.
  170. if ( @optionlist > 0 && ref($optionlist[0]) ) {
  171. print STDERR ("=> link \"$linko\" to $optionlist[0]\n")
  172. if $debug;
  173. if ( ref($optionlist[0]) =~ /^(SCALAR|CODE)$/ ) {
  174. $linkage{$linko} = shift (@optionlist);
  175. }
  176. elsif ( ref($optionlist[0]) =~ /^(ARRAY)$/ ) {
  177. $linkage{$linko} = shift (@optionlist);
  178. $opctl{$o} .= '@'
  179. if $opctl{$o} ne '' and $opctl{$o} !~ /\@$/;
  180. $bopctl{$o} .= '@'
  181. if $bundling and defined $bopctl{$o} and
  182. $bopctl{$o} ne '' and $bopctl{$o} !~ /\@$/;
  183. }
  184. elsif ( ref($optionlist[0]) =~ /^(HASH)$/ ) {
  185. $linkage{$linko} = shift (@optionlist);
  186. $opctl{$o} .= '%'
  187. if $opctl{$o} ne '' and $opctl{$o} !~ /\%$/;
  188. $bopctl{$o} .= '%'
  189. if $bundling and defined $bopctl{$o} and
  190. $bopctl{$o} ne '' and $bopctl{$o} !~ /\%$/;
  191. }
  192. else {
  193. $error .= "Invalid option linkage for \"$opt\"\n";
  194. }
  195. }
  196. else {
  197. # Link to global $opt_XXX variable.
  198. # Make sure a valid perl identifier results.
  199. my $ov = $linko;
  200. $ov =~ s/\W/_/g;
  201. if ( $c =~ /@/ ) {
  202. print STDERR ("=> link \"$linko\" to \@$pkg","::opt_$ov\n")
  203. if $debug;
  204. eval ("\$linkage{\$linko} = \\\@".$pkg."::opt_$ov;");
  205. }
  206. elsif ( $c =~ /%/ ) {
  207. print STDERR ("=> link \"$linko\" to \%$pkg","::opt_$ov\n")
  208. if $debug;
  209. eval ("\$linkage{\$linko} = \\\%".$pkg."::opt_$ov;");
  210. }
  211. else {
  212. print STDERR ("=> link \"$linko\" to \$$pkg","::opt_$ov\n")
  213. if $debug;
  214. eval ("\$linkage{\$linko} = \\\$".$pkg."::opt_$ov;");
  215. }
  216. }
  217. }
  218. # Bail out if errors found.
  219. die ($error) if $error;
  220. $error = 0;
  221. # Sort the possible long option names.
  222. @opctl = sort(keys (%opctl)) if $autoabbrev;
  223. # Show the options tables if debugging.
  224. if ( $debug ) {
  225. my ($arrow, $k, $v);
  226. $arrow = "=> ";
  227. while ( ($k,$v) = each(%opctl) ) {
  228. print STDERR ($arrow, "\$opctl{\"$k\"} = \"$v\"\n");
  229. $arrow = " ";
  230. }
  231. $arrow = "=> ";
  232. while ( ($k,$v) = each(%bopctl) ) {
  233. print STDERR ($arrow, "\$bopctl{\"$k\"} = \"$v\"\n");
  234. $arrow = " ";
  235. }
  236. }
  237. # Process argument list
  238. my $goon = 1;
  239. while ( $goon && @ARGV > 0 ) {
  240. #### Get next argument ####
  241. $opt = shift (@ARGV);
  242. print STDERR ("=> option \"", $opt, "\"\n") if $debug;
  243. #### Determine what we have ####
  244. # Double dash is option list terminator.
  245. if ( $opt eq $argend ) {
  246. # Finish. Push back accumulated arguments and return.
  247. unshift (@ARGV, @ret)
  248. if $order == $PERMUTE;
  249. return ($error == 0);
  250. }
  251. my $tryopt = $opt;
  252. my $found; # success status
  253. my $dsttype; # destination type ('@' or '%')
  254. my $incr; # destination increment
  255. my $key; # key (if hash type)
  256. my $arg; # option argument
  257. ($found, $opt, $arg, $dsttype, $incr, $key) =
  258. FindOption ($genprefix, $argend, $opt,
  259. \%opctl, \%bopctl, \@opctl, \%aliases);
  260. if ( $found ) {
  261. # FindOption undefines $opt in case of errors.
  262. next unless defined $opt;
  263. if ( defined $arg ) {
  264. if ( defined $aliases{$opt} ) {
  265. print STDERR ("=> alias \"$opt\" -> \"$aliases{$opt}\"\n")
  266. if $debug;
  267. $opt = $aliases{$opt};
  268. }
  269. if ( defined $linkage{$opt} ) {
  270. print STDERR ("=> ref(\$L{$opt}) -> ",
  271. ref($linkage{$opt}), "\n") if $debug;
  272. if ( ref($linkage{$opt}) eq 'SCALAR' ) {
  273. if ( $incr ) {
  274. print STDERR ("=> \$\$L{$opt} += \"$arg\"\n")
  275. if $debug;
  276. if ( defined ${$linkage{$opt}} ) {
  277. ${$linkage{$opt}} += $arg;
  278. }
  279. else {
  280. ${$linkage{$opt}} = $arg;
  281. }
  282. }
  283. else {
  284. print STDERR ("=> \$\$L{$opt} = \"$arg\"\n")
  285. if $debug;
  286. ${$linkage{$opt}} = $arg;
  287. }
  288. }
  289. elsif ( ref($linkage{$opt}) eq 'ARRAY' ) {
  290. print STDERR ("=> push(\@{\$L{$opt}, \"$arg\")\n")
  291. if $debug;
  292. push (@{$linkage{$opt}}, $arg);
  293. }
  294. elsif ( ref($linkage{$opt}) eq 'HASH' ) {
  295. print STDERR ("=> \$\$L{$opt}->{$key} = \"$arg\"\n")
  296. if $debug;
  297. $linkage{$opt}->{$key} = $arg;
  298. }
  299. elsif ( ref($linkage{$opt}) eq 'CODE' ) {
  300. print STDERR ("=> &L{$opt}(\"$opt\", \"$arg\")\n")
  301. if $debug;
  302. local ($@);
  303. eval {
  304. &{$linkage{$opt}}($opt, $arg);
  305. };
  306. print STDERR ("=> die($@)\n") if $debug && $@ ne '';
  307. if ( $@ =~ /^!/ ) {
  308. if ( $@ =~ /^!FINISH\b/ ) {
  309. $goon = 0;
  310. }
  311. }
  312. elsif ( $@ ne '' ) {
  313. warn ($@);
  314. $error++;
  315. }
  316. }
  317. else {
  318. print STDERR ("Invalid REF type \"", ref($linkage{$opt}),
  319. "\" in linkage\n");
  320. Croak ("Getopt::Long -- internal error!\n");
  321. }
  322. }
  323. # No entry in linkage means entry in userlinkage.
  324. elsif ( $dsttype eq '@' ) {
  325. if ( defined $userlinkage->{$opt} ) {
  326. print STDERR ("=> push(\@{\$L{$opt}}, \"$arg\")\n")
  327. if $debug;
  328. push (@{$userlinkage->{$opt}}, $arg);
  329. }
  330. else {
  331. print STDERR ("=>\$L{$opt} = [\"$arg\"]\n")
  332. if $debug;
  333. $userlinkage->{$opt} = [$arg];
  334. }
  335. }
  336. elsif ( $dsttype eq '%' ) {
  337. if ( defined $userlinkage->{$opt} ) {
  338. print STDERR ("=> \$L{$opt}->{$key} = \"$arg\"\n")
  339. if $debug;
  340. $userlinkage->{$opt}->{$key} = $arg;
  341. }
  342. else {
  343. print STDERR ("=>\$L{$opt} = {$key => \"$arg\"}\n")
  344. if $debug;
  345. $userlinkage->{$opt} = {$key => $arg};
  346. }
  347. }
  348. else {
  349. if ( $incr ) {
  350. print STDERR ("=> \$L{$opt} += \"$arg\"\n")
  351. if $debug;
  352. if ( defined $userlinkage->{$opt} ) {
  353. $userlinkage->{$opt} += $arg;
  354. }
  355. else {
  356. $userlinkage->{$opt} = $arg;
  357. }
  358. }
  359. else {
  360. print STDERR ("=>\$L{$opt} = \"$arg\"\n") if $debug;
  361. $userlinkage->{$opt} = $arg;
  362. }
  363. }
  364. }
  365. }
  366. # Not an option. Save it if we $PERMUTE and don't have a <>.
  367. elsif ( $order == $PERMUTE ) {
  368. # Try non-options call-back.
  369. my $cb;
  370. if ( (defined ($cb = $linkage{'<>'})) ) {
  371. local ($@);
  372. eval {
  373. &$cb ($tryopt);
  374. };
  375. print STDERR ("=> die($@)\n") if $debug && $@ ne '';
  376. if ( $@ =~ /^!/ ) {
  377. if ( $@ =~ /^!FINISH\b/ ) {
  378. $goon = 0;
  379. }
  380. }
  381. elsif ( $@ ne '' ) {
  382. warn ($@);
  383. $error++;
  384. }
  385. }
  386. else {
  387. print STDERR ("=> saving \"$tryopt\" ",
  388. "(not an option, may permute)\n") if $debug;
  389. push (@ret, $tryopt);
  390. }
  391. next;
  392. }
  393. # ...otherwise, terminate.
  394. else {
  395. # Push this one back and exit.
  396. unshift (@ARGV, $tryopt);
  397. return ($error == 0);
  398. }
  399. }
  400. # Finish.
  401. if ( $order == $PERMUTE ) {
  402. # Push back accumulated arguments
  403. print STDERR ("=> restoring \"", join('" "', @ret), "\"\n")
  404. if $debug && @ret > 0;
  405. unshift (@ARGV, @ret) if @ret > 0;
  406. }
  407. return ($error == 0);
  408. }
  409. # end of Getopt::Long::GetOptions
  410. 1;