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.

196 lines
6.1 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. getopt.c
  5. Abstract:
  6. Utility function to parse command line options.
  7. Inspired by UNIX getopt - but coded from scratch.
  8. Not thread-safe currently (don't call this from
  9. multiple threads simultaneously)
  10. Author:
  11. Ravisankar Pudipeddi (ravisp) 27 June 1997
  12. Environment:
  13. User
  14. Notes:
  15. Revision History:
  16. --*/
  17. #include <pch.h>
  18. PUCHAR optarg;
  19. ULONG optind=1;
  20. static ULONG optcharind;
  21. static ULONG hyphen=0;
  22. CHAR
  23. getopt (ULONG Argc, PUCHAR *Argv, PCHAR Opts)
  24. /*++
  25. Routine Description:
  26. Parses command line arguments.
  27. This function should be repeatedly called
  28. to parse all the supplied command line options.
  29. A command line argument prefixed with a hyphen/slash
  30. ('-' or '/') is treated as a command line option(s).
  31. The set of options the caller is interested in
  32. is passed via the Opts argument.
  33. The format of this desired options list is:
  34. "<option-letter1>[:]<option-letter2>[:]......."
  35. Examples: "ds:g", "x:o:r" etc.
  36. Each letter in this string is an option the
  37. caller is interested in.
  38. If there is a colon (':') after the option letter,
  39. then the caller expects an argument with this option
  40. letter.
  41. On each call, successive options are processed and
  42. the next matching option in the list of desired options
  43. is returned. If the option requires an argument, then
  44. the option argument is in the global 'optarg'.
  45. If all the options have been processed then the value
  46. EOF is returned at which point caller should desist
  47. calling this function again for the lifetime of the process.
  48. A single hyphen/slash ('-' or '/) unaccompanied by any option
  49. letter in the command line indicates getopt to stop processing
  50. command line options and treat the rest of the arguments
  51. as regular command line arguments.
  52. After all the options have been processed (i.e. getopt
  53. returned EOF), the global 'optind' contains the index
  54. to the start of the non-option arguments which may be
  55. processed by the caller.
  56. Note: This function *does not* return an error code if
  57. an non-desired option is encountered in the command line.
  58. Arguments:
  59. Argc - number of command line arguments
  60. Argv - pointer to array of command line arguments
  61. (Argv[0] is skipped in processing the options,
  62. treated as the base filename of the executable)
  63. Opts - String containing the desired options
  64. Return Value:
  65. EOF - No more options. Don't call this function again.
  66. The global 'optind' points to index of the first argument
  67. following the options on the command line
  68. 0 - Error in specifying command line options
  69. Any other character - Next option on the command line.
  70. The value 'optarg' points to the command line
  71. argument string following this option, if
  72. the option was indicated as requiring an argument
  73. (i.e. preceding a colon in the Opts string)
  74. --*/
  75. {
  76. CHAR ch;
  77. PCHAR indx;
  78. do {
  79. if (optind >= Argc) {
  80. return EOF;
  81. }
  82. ch = Argv[optind][optcharind++];
  83. if (ch == '\0') {
  84. optind++; optcharind=0;
  85. hyphen = 0;
  86. continue;
  87. }
  88. if ( hyphen || (ch == '-') || (ch == '/')) {
  89. if (!hyphen) {
  90. ch = Argv[optind][optcharind++];
  91. if (ch == '\0') {
  92. //
  93. // just a '-' (or '/') without any other
  94. // char after it indicates to stop
  95. // processing options, the rest are
  96. // regular command line arguments
  97. // optind points to the arguments after
  98. // this lone hyphen
  99. //
  100. optind++;
  101. return EOF;
  102. }
  103. } else if (ch == '\0') {
  104. //
  105. // End of options on this arg.
  106. // continue to next...
  107. optind++;
  108. optcharind = 0;
  109. continue;
  110. }
  111. indx = strchr(Opts, ch);
  112. if (indx == NULL) {
  113. //
  114. // Non-desired option encountered
  115. // We just ignore it
  116. //
  117. continue;
  118. }
  119. if (*(indx+1) == ':') {
  120. if (Argv[optind][optcharind] != '\0'){
  121. optarg = &Argv[optind][optcharind];
  122. } else {
  123. if ((optind + 1) >= Argc ||
  124. (Argv[optind+1][0] == '-' ||
  125. Argv[optind+1][0] == '/' )) {
  126. //
  127. // This is a case when one of the following error
  128. // condition exists:
  129. // 1. The user didn't supply an argument to an option
  130. // which requires one (ie, this option was the last
  131. // command line argument on the line)
  132. // 2. The supplied another option as an argument to this
  133. // option. Currently we treat this as an error
  134. //
  135. return 0;
  136. }
  137. optarg = Argv[++optind];
  138. }
  139. optind++;
  140. hyphen = optcharind = 0;
  141. return ch;
  142. }
  143. //
  144. // Argument not required for this option
  145. // So any other characters in the same
  146. // argument would be other valid options
  147. //
  148. hyphen = 1;
  149. return ch;
  150. } else {
  151. //
  152. // Non option encountered.
  153. // No more options present..
  154. //
  155. return EOF;
  156. }
  157. } while (1);
  158. }