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.

272 lines
8.0 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 656 "../LIB\Getopt\Long.pm (autosplit into ..\lib\auto\Getopt\Long\FindOption.al)"
  6. # Option lookup.
  7. sub FindOption ($$$$$$$) {
  8. # returns (1, $opt, $arg, $dsttype, $incr, $key) if okay,
  9. # returns (0) otherwise.
  10. my ($prefix, $argend, $opt, $opctl, $bopctl, $names, $aliases) = @_;
  11. my $key; # hash key for a hash option
  12. my $arg;
  13. print STDERR ("=> find \"$opt\", prefix=\"$prefix\"\n") if $debug;
  14. return 0 unless $opt =~ /^$prefix(.*)$/s;
  15. return 0 if $opt eq "-" && !defined $opctl->{""};
  16. $opt = $+;
  17. my ($starter) = $1;
  18. print STDERR ("=> split \"$starter\"+\"$opt\"\n") if $debug;
  19. my $optarg = undef; # value supplied with --opt=value
  20. my $rest = undef; # remainder from unbundling
  21. # If it is a long option, it may include the value.
  22. if (($starter eq "--" || ($getopt_compat && !$bundling))
  23. && $opt =~ /^([^=]+)=(.*)$/s ) {
  24. $opt = $1;
  25. $optarg = $2;
  26. print STDERR ("=> option \"", $opt,
  27. "\", optarg = \"$optarg\"\n") if $debug;
  28. }
  29. #### Look it up ###
  30. my $tryopt = $opt; # option to try
  31. my $optbl = $opctl; # table to look it up (long names)
  32. my $type;
  33. my $dsttype = '';
  34. my $incr = 0;
  35. if ( $bundling && $starter eq '-' ) {
  36. # Unbundle single letter option.
  37. $rest = length ($tryopt) > 0 ? substr ($tryopt, 1) : "";
  38. $tryopt = substr ($tryopt, 0, 1);
  39. $tryopt = lc ($tryopt) if $ignorecase > 1;
  40. print STDERR ("=> $starter$tryopt unbundled from ",
  41. "$starter$tryopt$rest\n") if $debug;
  42. $rest = undef unless $rest ne '';
  43. $optbl = $bopctl; # look it up in the short names table
  44. # If bundling == 2, long options can override bundles.
  45. if ( $bundling == 2 and
  46. defined ($rest) and
  47. defined ($type = $opctl->{$tryopt.$rest}) ) {
  48. print STDERR ("=> $starter$tryopt rebundled to ",
  49. "$starter$tryopt$rest\n") if $debug;
  50. $tryopt .= $rest;
  51. undef $rest;
  52. }
  53. }
  54. # Try auto-abbreviation.
  55. elsif ( $autoabbrev ) {
  56. # Downcase if allowed.
  57. $tryopt = $opt = lc ($opt) if $ignorecase;
  58. # Turn option name into pattern.
  59. my $pat = quotemeta ($opt);
  60. # Look up in option names.
  61. my @hits = grep (/^$pat/, @{$names});
  62. print STDERR ("=> ", scalar(@hits), " hits (@hits) with \"$pat\" ",
  63. "out of ", scalar(@{$names}), "\n") if $debug;
  64. # Check for ambiguous results.
  65. unless ( (@hits <= 1) || (grep ($_ eq $opt, @hits) == 1) ) {
  66. # See if all matches are for the same option.
  67. my %hit;
  68. foreach ( @hits ) {
  69. $_ = $aliases->{$_} if defined $aliases->{$_};
  70. $hit{$_} = 1;
  71. }
  72. # Now see if it really is ambiguous.
  73. unless ( keys(%hit) == 1 ) {
  74. return (0) if $passthrough;
  75. warn ("Option ", $opt, " is ambiguous (",
  76. join(", ", @hits), ")\n");
  77. $error++;
  78. undef $opt;
  79. return (1, $opt,$arg,$dsttype,$incr,$key);
  80. }
  81. @hits = keys(%hit);
  82. }
  83. # Complete the option name, if appropriate.
  84. if ( @hits == 1 && $hits[0] ne $opt ) {
  85. $tryopt = $hits[0];
  86. $tryopt = lc ($tryopt) if $ignorecase;
  87. print STDERR ("=> option \"$opt\" -> \"$tryopt\"\n")
  88. if $debug;
  89. }
  90. }
  91. # Map to all lowercase if ignoring case.
  92. elsif ( $ignorecase ) {
  93. $tryopt = lc ($opt);
  94. }
  95. # Check validity by fetching the info.
  96. $type = $optbl->{$tryopt} unless defined $type;
  97. unless ( defined $type ) {
  98. return (0) if $passthrough;
  99. warn ("Unknown option: ", $opt, "\n");
  100. $error++;
  101. return (1, $opt,$arg,$dsttype,$incr,$key);
  102. }
  103. # Apparently valid.
  104. $opt = $tryopt;
  105. print STDERR ("=> found \"$type\" for \"", $opt, "\"\n") if $debug;
  106. #### Determine argument status ####
  107. # If it is an option w/o argument, we're almost finished with it.
  108. if ( $type eq '' || $type eq '!' || $type eq '+' ) {
  109. if ( defined $optarg ) {
  110. return (0) if $passthrough;
  111. warn ("Option ", $opt, " does not take an argument\n");
  112. $error++;
  113. undef $opt;
  114. }
  115. elsif ( $type eq '' || $type eq '+' ) {
  116. $arg = 1; # supply explicit value
  117. $incr = $type eq '+';
  118. }
  119. else {
  120. substr ($opt, 0, 2) = ''; # strip NO prefix
  121. $arg = 0; # supply explicit value
  122. }
  123. unshift (@ARGV, $starter.$rest) if defined $rest;
  124. return (1, $opt,$arg,$dsttype,$incr,$key);
  125. }
  126. # Get mandatory status and type info.
  127. my $mand;
  128. ($mand, $type, $dsttype, $key) = $type =~ /^(.)(.)([@%]?)$/;
  129. # Check if there is an option argument available.
  130. if ( $gnu_compat ) {
  131. return (1, $opt, $optarg, $dsttype, $incr, $key)
  132. if defined $optarg;
  133. return (1, $opt, $type eq "s" ? '' : 0, $dsttype, $incr, $key)
  134. if $mand eq ':';
  135. }
  136. # Check if there is an option argument available.
  137. if ( defined $optarg
  138. ? ($optarg eq '')
  139. : !(defined $rest || @ARGV > 0) ) {
  140. # Complain if this option needs an argument.
  141. if ( $mand eq "=" ) {
  142. return (0) if $passthrough;
  143. warn ("Option ", $opt, " requires an argument\n");
  144. $error++;
  145. undef $opt;
  146. }
  147. return (1, $opt, $type eq "s" ? '' : 0, $dsttype, $incr, $key);
  148. }
  149. # Get (possibly optional) argument.
  150. $arg = (defined $rest ? $rest
  151. : (defined $optarg ? $optarg : shift (@ARGV)));
  152. # Get key if this is a "name=value" pair for a hash option.
  153. $key = undef;
  154. if ($dsttype eq '%' && defined $arg) {
  155. ($key, $arg) = ($arg =~ /^([^=]*)=(.*)$/s) ? ($1, $2) : ($arg, 1);
  156. }
  157. #### Check if the argument is valid for this option ####
  158. if ( $type eq "s" ) { # string
  159. # A mandatory string takes anything.
  160. return (1, $opt,$arg,$dsttype,$incr,$key) if $mand eq "=";
  161. # An optional string takes almost anything.
  162. return (1, $opt,$arg,$dsttype,$incr,$key)
  163. if defined $optarg || defined $rest;
  164. return (1, $opt,$arg,$dsttype,$incr,$key) if $arg eq "-"; # ??
  165. # Check for option or option list terminator.
  166. if ($arg eq $argend ||
  167. $arg =~ /^$prefix.+/) {
  168. # Push back.
  169. unshift (@ARGV, $arg);
  170. # Supply empty value.
  171. $arg = '';
  172. }
  173. }
  174. elsif ( $type eq "n" || $type eq "i" ) { # numeric/integer
  175. if ( $bundling && defined $rest && $rest =~ /^([-+]?[0-9]+)(.*)$/s ) {
  176. $arg = $1;
  177. $rest = $2;
  178. unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
  179. }
  180. elsif ( $arg !~ /^[-+]?[0-9]+$/ ) {
  181. if ( defined $optarg || $mand eq "=" ) {
  182. if ( $passthrough ) {
  183. unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
  184. unless defined $optarg;
  185. return (0);
  186. }
  187. warn ("Value \"", $arg, "\" invalid for option ",
  188. $opt, " (number expected)\n");
  189. $error++;
  190. undef $opt;
  191. # Push back.
  192. unshift (@ARGV, $starter.$rest) if defined $rest;
  193. }
  194. else {
  195. # Push back.
  196. unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
  197. # Supply default value.
  198. $arg = 0;
  199. }
  200. }
  201. }
  202. elsif ( $type eq "f" ) { # real number, int is also ok
  203. # We require at least one digit before a point or 'e',
  204. # and at least one digit following the point and 'e'.
  205. # [-]NN[.NN][eNN]
  206. if ( $bundling && defined $rest &&
  207. $rest =~ /^([-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?)(.*)$/s ) {
  208. $arg = $1;
  209. $rest = $+;
  210. unshift (@ARGV, $starter.$rest) if defined $rest && $rest ne '';
  211. }
  212. elsif ( $arg !~ /^[-+]?[0-9.]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/ ) {
  213. if ( defined $optarg || $mand eq "=" ) {
  214. if ( $passthrough ) {
  215. unshift (@ARGV, defined $rest ? $starter.$rest : $arg)
  216. unless defined $optarg;
  217. return (0);
  218. }
  219. warn ("Value \"", $arg, "\" invalid for option ",
  220. $opt, " (real number expected)\n");
  221. $error++;
  222. undef $opt;
  223. # Push back.
  224. unshift (@ARGV, $starter.$rest) if defined $rest;
  225. }
  226. else {
  227. # Push back.
  228. unshift (@ARGV, defined $rest ? $starter.$rest : $arg);
  229. # Supply default value.
  230. $arg = 0.0;
  231. }
  232. }
  233. }
  234. else {
  235. Croak ("GetOpt::Long internal error (Can't happen)\n");
  236. }
  237. return (1, $opt, $arg, $dsttype, $incr, $key);
  238. }
  239. # end of Getopt::Long::FindOption
  240. 1;