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.

223 lines
6.0 KiB

  1. /*****************************************************************************\
  2. Author: Corey Morgan (coreym)
  3. Copyright (c) 1998-2001 Microsoft Corporation
  4. \*****************************************************************************/
  5. #ifndef _VARG_H_012599_
  6. #define _VARG_H_012599_
  7. #define MAXSTR 1025
  8. //Class CToken
  9. //It represents a Single Token.
  10. class CToken
  11. {
  12. public:
  13. CToken(PCWSTR psz, BOOL bQuote);
  14. CToken();
  15. ~CToken();
  16. BOOL Init(PCWSTR psz, BOOL bQuote);
  17. PWSTR GetToken() const;
  18. BOOL IsSwitch() const;
  19. BOOL IsSlash() const;
  20. private:
  21. LPWSTR m_pszToken;
  22. BOOL m_bInitQuote;
  23. };
  24. typedef CToken * LPTOKEN;
  25. #define ARG_TYPE_INT 0
  26. #define ARG_TYPE_BOOL 1
  27. #define ARG_TYPE_STR 2
  28. #define ARG_TYPE_HELP 3
  29. #define ARG_TYPE_DEBUG 4
  30. #define ARG_TYPE_MSZ 5
  31. #define ARG_TYPE_INTSTR 6
  32. #define ARG_TYPE_VERB 7 // Verbs are not preceeded by a switch character
  33. #define ARG_TYPE_LAST 8
  34. #define ARG_FLAG_OPTIONAL 0x00000001
  35. #define ARG_FLAG_REQUIRED 0x00000002
  36. #define ARG_FLAG_DEFAULTABLE 0x00000004
  37. //#define ARG_FLAG_NOFLAG 0x00000008 // unused.
  38. #define ARG_FLAG_HIDDEN 0x00000010
  39. #define ARG_FLAG_VERB 0x00000020 // For operation and sub-operation params that do not have a switch char.
  40. // Verbs are two state: present or not present; they do not have qualifers such as a user-entered string.
  41. #define ARG_FLAG_STDIN 0x00000040 // This must be Required. If not sepcified read from standard input
  42. #define ARG_FLAG_ATLEASTONE 0x00000080 // If this flag is specified on one or more switch, at
  43. // least one of those switches must be defined
  44. #define ARG_FLAG_OBJECT 0x00000100 // The object arg is the 3rd param for most commands.
  45. #define ARG_TERMINATOR 0,NULL,0,NULL,ARG_TYPE_LAST,0,(CMD_TYPE)0,FALSE,NULL
  46. #define ID_ARG2_NULL (LONG)-1
  47. #define CMD_TYPE void*
  48. typedef struct _ARG_RECORD
  49. {
  50. LONG idArg1;
  51. LPTSTR strArg1;
  52. LONG idArg2;
  53. LPTSTR strArg2;
  54. int fType;
  55. DWORD fFlag;
  56. union{
  57. void* vValue;
  58. LPTSTR strValue;
  59. int nValue;
  60. BOOL bValue;
  61. };
  62. BOOL bDefined;
  63. DWORD (*fntValidation)(PVOID pArg);
  64. } ARG_RECORD, *PARG_RECORD;
  65. //Error Source
  66. #define ERROR_FROM_PARSER 1
  67. #define ERROR_FROM_VLDFN 2
  68. #define ERROR_WIN32_ERROR 3
  69. //Parse Errors for when ERROR_SOURCE is ERROR_FROM_PARSER
  70. /*
  71. SWITCH value is incorrect.
  72. ArgRecIndex is index of record.
  73. ArgvIndex is index of token.
  74. */
  75. #define PARSE_ERROR_SWITCH_VALUE 1
  76. /*No Value is given for a swich when one is expected.
  77. ArgRecIndex is index of record.
  78. ArgvIndex is -1.
  79. */
  80. #define PARSE_ERROR_SWICH_NO_VALUE 2
  81. /*
  82. Invalid Input
  83. ArgRecIndex is -1,
  84. ArgvIndex is index of token.
  85. */
  86. #define PARSE_ERROR_UNKNOWN_INPUT_PARAMETER 3
  87. /*
  88. Required switch is not defined.
  89. ArgRecIndex is index of record.
  90. ArgvIndex is -1.
  91. */
  92. #define PARSE_ERROR_SWITCH_NOTDEFINED 4
  93. /*
  94. Switch or Parameter is defined twice.
  95. ArgRecIndex is index of record.
  96. ArgvIndex is -1
  97. */
  98. #define PARSE_ERROR_MULTIPLE_DEF 5
  99. /*
  100. Error Reading From STDIN.
  101. ArgRecIndex is -1.
  102. ArgvIndex is -1.
  103. */
  104. #define ERROR_READING_FROM_STDIN 6
  105. /*
  106. Parser Encountered Help Switch
  107. ArgRecIndex is index of record.
  108. ArgvIndex is -1
  109. */
  110. #define PARSE_ERROR_HELP_SWITCH 7
  111. /*
  112. The ARG_FLAG_ATLEASTONE flag was
  113. defined on one or more switch yet
  114. none of these switches were defined
  115. ArgRecIndex is -1
  116. ArgvIndex is -1
  117. */
  118. #define PARSE_ERROR_ATLEASTONE_NOTDEFINED 8
  119. //Parse Errors for when ERROR_SOURCE is VLDFN
  120. /*
  121. Use this error code when Validation Function has handled the error and
  122. Shown appropriate error message.
  123. */
  124. #define VLDFN_ERROR_NO_ERROR 1
  125. //Error is returned by Parser in PARSE_ERROR structure
  126. //ErrorSource: Source of Error. Parser or Validation Function
  127. //Error This is the actual error code. Its value depend on ErrorSource value.
  128. // if( ErrorSource == PARSE_ERROR )
  129. // possible values are ERROR_FROM_PARSER ERROR_FROM_VLDFN
  130. // if( ErrorSource == ERROR_FROM_VLDFN )
  131. // depends on the function
  132. // ArgRecIndex is appropriate index in the ARG_RECORD, if applicable else -1
  133. // ArgvIndex is approproate index in the agrv array, if applicable else -1
  134. typedef struct _PARSE_ERROR
  135. {
  136. INT ErrorSource;
  137. DWORD Error;
  138. INT ArgRecIndex;
  139. INT ArgvIndex;
  140. } PARSE_ERROR, *PPARSE_ERROR;
  141. BOOL ParseCmd(IN ARG_RECORD *Commands,
  142. IN int argc,
  143. IN CToken *pToken,
  144. IN bool fSkipObject,
  145. OUT PPARSE_ERROR pError,
  146. IN BOOL bValidate = FALSE);
  147. void FreeCmd(ARG_RECORD *Commands);
  148. DWORD GetCommandInput(OUT int *pargc, //Number of Tokens
  149. OUT LPTOKEN *ppToken); //Array of CToken
  150. BOOL LoadCmd(ARG_RECORD *Commands);
  151. DWORD Tokenize(IN LPWSTR pBuf,
  152. IN LONG BufLen,
  153. IN LPWSTR pszDelimiters,
  154. OUT CToken **ppToken,
  155. OUT int *argc);
  156. LONG GetToken(IN LPWSTR pBuf,
  157. IN LONG BufLen,
  158. IN LPWSTR pszDelimiters,
  159. OUT BOOL *bQuote,
  160. OUT LPWSTR *ppToken);
  161. BOOL DisplayParseError(IN PPARSE_ERROR pError,
  162. IN ARG_RECORD *Commands,
  163. IN CToken *pToken);
  164. //Function to display string to STDERR
  165. VOID DisplayError(IN LPWSTR pszError);
  166. //Function to display string to STDOUT, appending newline
  167. VOID DisplayOutput(IN LPWSTR pszOut);
  168. //Function to display string to STDOUT, without newline
  169. VOID DisplayOutputNoNewline(IN LPWSTR pszOut);
  170. //Function to display Message to Standard Error.
  171. VOID DisplayMessage(DWORD MessageId,...);
  172. // Copied from JSchwart on 2/19/2001
  173. void
  174. MyWriteConsole(
  175. HANDLE fp,
  176. LPWSTR lpBuffer,
  177. DWORD cchBuffer
  178. );
  179. void
  180. WriteStandardOut(PCWSTR pszFormat, ...);
  181. void
  182. WriteStandardError(PCWSTR pszFormat, ...);
  183. #endif //_VARG_H_012599_