Source code of Windows XP (NT5)
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.

162 lines
4.1 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1999
  3. All rights reserved.
  4. Module Name:
  5. getopt.cxx
  6. Abstract:
  7. This module is an implementation of System V get option
  8. routine.
  9. Author:
  10. Steve Kiraly (SteveKi) 12/10/95
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include <stdio.h>
  16. #include <string.h>
  17. #ifdef USE_ERRORNO
  18. #include <errno.h>
  19. #endif
  20. #include "getopt.hxx"
  21. /*
  22. Parse the command line options, System V style.
  23. Standard option syntax is:
  24. option ::= SW [optLetter]* [argLetter space* argument]
  25. where
  26. - SW is either '-'
  27. - there is no space before any optLetter or argLetter.
  28. - opt/arg letters are alphabetic, not punctuation characters.
  29. - optLetters, if present, must be matched in optionS.
  30. - argLetters, if present, are found in optionS followed by ':'.
  31. - argument is any white-space delimited string. Note that it
  32. can include the SW character.
  33. - upper and lower case letters are distinct.
  34. There may be multiple option clusters on a command line, each
  35. beginning with a SW, but all must appear before any non-option
  36. arguments (arguments not introduced by SW). Opt/arg letters may
  37. be repeated: it is up to the caller to decide if that is an error.
  38. The character SW appearing alone as the last argument is an error.
  39. The lead-in sequence SWSW ("--" or "//") causes itself and all the
  40. rest of the line to be ignored (allowing non-options which begin
  41. with the switch char).
  42. The string *optionS allows valid opt/arg letters to be recognized.
  43. argLetters are followed with ':'. Getopt () returns the value of
  44. the option character found, or EOF if no more options are in the
  45. command line. If option is an argLetter then the global optarg is
  46. set to point to the argument string (having skipped any white-space).
  47. The global optind is initially 1 and is always left as the index
  48. of the next argument of argv[] which getopt has not taken. Note
  49. that if "--" or "//" are used then optind is stepped to the next
  50. argument before getopt() returns EOF.
  51. If an error occurs, that is an SW char precedes an unknown letter,
  52. then getopt() will return a '?' character and normally prints an
  53. error message via perror(). If the global variable opterr is set
  54. to false (zero) before calling getopt() then the error message is
  55. not printed.
  56. For example, if the MSDOS switch char is '/' (the MSDOS norm) and
  57. *optionS == "A:F:PuU:wXZ:"
  58. then 'P', 'u', 'w', and 'X' are option letters and 'F', 'U', 'Z'
  59. are followed by arguments. A valid command line may be:
  60. aCommand /uPFPi /X /A L someFile
  61. where:
  62. - 'u' and 'P' will be returned as isolated option letters.
  63. - 'F' will return with "Pi" as its argument string.
  64. - 'X' is an isolated option.
  65. - 'A' will return with "L" as its argument.
  66. - "someFile" is not an option, and terminates getOpt. The
  67. caller may collect remaining arguments using argv pointers.
  68. */
  69. extern "C"
  70. INT
  71. getopt(
  72. INT argc,
  73. TCHAR *argv[],
  74. TCHAR *optionS,
  75. TGetOptContext &context
  76. )
  77. {
  78. TCHAR ch;
  79. TCHAR *optP;
  80. if (argc > context.optind)
  81. {
  82. if (context.letP == NULL)
  83. {
  84. if ((context.letP = argv[context.optind]) == NULL || *(context.letP++) != context.SW)
  85. goto gopEOF;
  86. if (*context.letP == context.SW)
  87. {
  88. context.optind++; goto gopEOF;
  89. }
  90. }
  91. if (0 == (ch = *(context.letP++)))
  92. {
  93. context.optind++;
  94. goto gopEOF;
  95. }
  96. if (TEXT(':') == ch || (optP = _tcschr(optionS, ch)) == NULL)
  97. goto gopError;
  98. if (TEXT(':') == *(++optP))
  99. {
  100. context.optind++;
  101. if (0 == *context.letP)
  102. {
  103. if (argc <= context.optind)
  104. goto gopError;
  105. context.letP = argv[context.optind++];
  106. }
  107. context.optarg = context.letP;
  108. context.letP = NULL;
  109. }
  110. else
  111. {
  112. if (0 == *context.letP)
  113. {
  114. context.optind++;
  115. context.letP = NULL;
  116. }
  117. context.optarg = NULL;
  118. }
  119. return (ch);
  120. }
  121. gopEOF:
  122. context.optarg = context.letP = NULL;
  123. return (EOF);
  124. gopError:
  125. context.optarg = NULL;
  126. #ifdef USE_ERRORNO
  127. errno = EINVAL;
  128. if (opterr)
  129. perror ("get command line option");
  130. #endif
  131. return INVALID_COMAND;
  132. }