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.

224 lines
5.7 KiB

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