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.

317 lines
9.3 KiB

  1. // *********************************************************************************
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // Module Name:
  6. //
  7. // parseAndshow.cpp
  8. //
  9. // Abstract:
  10. //
  11. // This module implements the command-line parsing and validating the filters
  12. //
  13. // Author:
  14. //
  15. // Sunil G.V.N. Murali ([email protected]) 27-Dec-2000
  16. //
  17. // Revision History:
  18. //
  19. // Sunil G.V.N. Murali ([email protected]) 27-Dec-2000 : Created It.
  20. //
  21. // *********************************************************************************
  22. #include "pch.h"
  23. #include "systeminfo.h"
  24. //
  25. // local function prototypes
  26. //
  27. BOOL
  28. CSystemInfo::ProcessOptions(
  29. IN DWORD argc,
  30. IN LPCTSTR argv[]
  31. )
  32. /*++
  33. // Routine Description:
  34. // processes and validates the command line inputs
  35. //
  36. // Arguments:
  37. // [ in ] argc : no. of input arguments specified
  38. // [ in ] argv : input arguments specified at command prompt
  39. //
  40. // Return Value:
  41. // TRUE : if inputs are valid
  42. // FALSE : if inputs were errorneously specified
  43. //
  44. --*/
  45. {
  46. // local variables
  47. CHString strFormat;
  48. BOOL bNoHeader = FALSE;
  49. BOOL bNullPassword = FALSE;
  50. // temporary local variables
  51. PTCMDPARSER2 pOptionServer = NULL;
  52. PTCMDPARSER2 pOptionUserName = NULL;
  53. PTCMDPARSER2 pOptionPassword = NULL;
  54. PTCMDPARSER2 pOptionFormat = NULL;
  55. // local variables
  56. PTCMDPARSER2 pOption = NULL;
  57. TCMDPARSER2 pcmdOptions[ MAX_OPTIONS ];
  58. //
  59. // set all the fields to 0
  60. SecureZeroMemory( pcmdOptions, sizeof( TCMDPARSER2 ) * MAX_OPTIONS );
  61. // -? option
  62. pOption = &pcmdOptions[ OI_USAGE ];
  63. StringCopyA( pOption->szSignature, "PARSER2", 8 );
  64. pOption->dwCount = 1;
  65. pOption->dwFlags = CP2_USAGE;
  66. pOption->dwType = CP_TYPE_BOOLEAN;
  67. pOption->pValue = &m_bUsage;
  68. pOption->pwszOptions = OPTION_USAGE;
  69. // -s option
  70. pOption = &pcmdOptions[ OI_SERVER ];
  71. StringCopyA( pOption->szSignature, "PARSER2", 8 );
  72. pOption->dwCount = 1;
  73. pOption->dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  74. pOption->dwType = CP_TYPE_TEXT;
  75. pOption->pwszOptions = OPTION_SERVER;
  76. // -u option
  77. pOption = &pcmdOptions[ OI_USERNAME ];
  78. StringCopyA( pOption->szSignature, "PARSER2", 8 );
  79. pOption->dwCount = 1;
  80. pOption->dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_TRIMINPUT | CP2_VALUE_NONULL;
  81. pOption->dwType = CP_TYPE_TEXT;
  82. pOption->pwszOptions = OPTION_USERNAME;
  83. // -p option
  84. pOption = &pcmdOptions[ OI_PASSWORD ];
  85. StringCopyA( pOption->szSignature, "PARSER2", 8 );
  86. pOption->dwCount = 1;
  87. pOption->dwFlags = CP2_ALLOCMEMORY | CP2_VALUE_OPTIONAL;
  88. pOption->dwType = CP_TYPE_TEXT;
  89. pOption->pwszOptions = OPTION_PASSWORD;
  90. // -fo option
  91. pOption = &pcmdOptions[ OI_FORMAT ];
  92. StringCopyA( pOption->szSignature, "PARSER2", 8 );
  93. pOption->dwCount = 1;
  94. pOption->dwFlags = CP2_ALLOCMEMORY| CP2_MODE_VALUES| CP2_VALUE_TRIMINPUT| CP2_VALUE_NONULL;
  95. pOption->dwType = CP_TYPE_TEXT;
  96. pOption->pwszOptions = OPTION_FORMAT;
  97. pOption->pwszValues = OVALUES_FORMAT;
  98. // -nh option
  99. pOption = &pcmdOptions[ OI_NOHEADER ];
  100. StringCopyA( pOption->szSignature, "PARSER2", 8 );
  101. pOption->dwCount = 1;
  102. pOption->dwFlags = 0;
  103. pOption->dwType = CP_TYPE_BOOLEAN;
  104. pOption->pValue = &bNoHeader;
  105. pOption->pwszOptions = OPTION_NOHEADER;
  106. //
  107. // now, check the mutually exclusive options
  108. pOptionServer = pcmdOptions + OI_SERVER;
  109. pOptionUserName = pcmdOptions + OI_USERNAME;
  110. pOptionPassword = pcmdOptions + OI_PASSWORD;
  111. pOptionFormat = pcmdOptions + OI_FORMAT;
  112. //
  113. // do the parsing
  114. //
  115. if ( DoParseParam2( argc, argv, -1, MAX_OPTIONS, pcmdOptions, 0 ) == FALSE )
  116. {
  117. return FALSE; // invalid syntax
  118. }
  119. //check whether /p without any value is specified or not..
  120. if ( NULL == pOptionPassword->pValue )
  121. {
  122. bNullPassword = TRUE;
  123. }
  124. // release the buffers
  125. m_strServer = (LPWSTR)pOptionServer->pValue;
  126. m_strUserName = (LPWSTR)pOptionUserName->pValue;
  127. m_strPassword = (LPWSTR)pOptionPassword->pValue;
  128. strFormat = (LPWSTR)pOptionFormat->pValue;
  129. // since CHString assignment does the copy operation..
  130. // release the buffers allocated by common library
  131. FreeMemory( &pOptionServer->pValue );
  132. FreeMemory( &pOptionUserName->pValue );
  133. FreeMemory( &pOptionPassword->pValue );
  134. FreeMemory( &pOptionFormat->pValue );
  135. // check the usage option
  136. if ( m_bUsage && ( argc > 2 ) )
  137. {
  138. // no other options are accepted along with -? option
  139. SetLastError( (DWORD)MK_E_SYNTAX );
  140. SetReason( ERROR_INVALID_USAGE_REQUEST );
  141. return FALSE;
  142. }
  143. else if ( m_bUsage == TRUE )
  144. {
  145. // should not do the furthur validations
  146. return TRUE;
  147. }
  148. // "-u" should not be specified without machine names
  149. if ( pOptionServer->dwActuals == 0 && pOptionUserName->dwActuals != 0 )
  150. {
  151. // invalid syntax
  152. SetReason( ERROR_USERNAME_BUT_NOMACHINE );
  153. return FALSE; // indicate failure
  154. }
  155. // "-p" should not be specified without "-u"
  156. if ( pOptionUserName->dwActuals == 0 && pOptionPassword->dwActuals != 0 )
  157. {
  158. // invalid syntax
  159. SetReason( ERROR_PASSWORD_BUT_NOUSERNAME );
  160. return FALSE; // indicate failure
  161. }
  162. // determine the format in which the process information has to be displayed
  163. m_dwFormat = SR_FORMAT_LIST; // default format
  164. if ( strFormat.CompareNoCase( TEXT_FORMAT_LIST ) == 0 )
  165. {
  166. m_dwFormat = SR_FORMAT_LIST;
  167. }
  168. else if ( strFormat.CompareNoCase( TEXT_FORMAT_TABLE ) == 0 )
  169. {
  170. m_dwFormat = SR_FORMAT_TABLE;
  171. }
  172. else if ( strFormat.CompareNoCase( TEXT_FORMAT_CSV ) == 0 )
  173. {
  174. m_dwFormat = SR_FORMAT_CSV;
  175. }
  176. // user might have given no header option for a LIST format which is invalid
  177. if ( bNoHeader == TRUE && m_dwFormat == SR_FORMAT_LIST )
  178. {
  179. // invalid syntax
  180. SetReason( ERROR_NH_NOTSUPPORTED );
  181. return FALSE; // indicate failure
  182. }
  183. // check for the no header info and apply to the format variable
  184. if ( bNoHeader == TRUE )
  185. {
  186. m_dwFormat |= SR_NOHEADER;
  187. }
  188. // check whether caller should accept the password or not
  189. // if user has specified -s (or) -u and no "-p", then utility should accept password
  190. // the user will be prompted for the password only if establish connection
  191. // is failed without the credentials information
  192. if ( pOptionPassword->dwActuals != 0 )
  193. {
  194. if (m_strPassword.Compare( L"*" ) == 0 )
  195. {
  196. // user wants the utility to prompt for the password before trying to connect
  197. m_bNeedPassword = TRUE;
  198. }
  199. else if ( TRUE == bNullPassword )
  200. {
  201. m_strPassword = L"*";
  202. // user wants the utility to prompt for the password before trying to connect
  203. m_bNeedPassword = TRUE;
  204. }
  205. }
  206. else if ( (pOptionPassword->dwActuals == 0 &&
  207. (pOptionServer->dwActuals != 0 || pOptionUserName->dwActuals != 0)) )
  208. {
  209. // utility needs to try to connect first and if it fails then prompt for the password
  210. m_bNeedPassword = TRUE;
  211. m_strPassword.Empty();
  212. }
  213. // command-line parsing is successfull
  214. return TRUE;
  215. }
  216. VOID
  217. CSystemInfo::ShowOutput(
  218. IN DWORD dwStart,
  219. IN DWORD dwEnd
  220. )
  221. /*++
  222. // Routine Description:
  223. // show the system configuration information
  224. //
  225. // Arguments:
  226. // [in] dwStart : start index
  227. // [in] dwEnd : end index
  228. //
  229. // Return Value:
  230. // NONE
  231. //
  232. --*/
  233. {
  234. // local variables
  235. PTCOLUMNS pColumn = NULL;
  236. // dynamically show / hide columns on need basis
  237. for( DWORD dw = 0; dw < MAX_COLUMNS; dw++ )
  238. {
  239. // point to the column info
  240. pColumn = m_pColumns + dw;
  241. // remove the hide flag from the column
  242. pColumn->dwFlags &= ~( SR_HIDECOLUMN );
  243. // now if the column should not be shown, set the hide flag)
  244. if ( dw < dwStart || dw > dwEnd )
  245. pColumn->dwFlags |= SR_HIDECOLUMN;
  246. }
  247. // if the data is being displayed from the first line onwards,
  248. // add a blank line.. If the format is CSV then there is no need
  249. // to display any blank line..
  250. if ( ( dwStart == 0 ) && (( m_dwFormat & SR_FORMAT_CSV ) != SR_FORMAT_CSV) )
  251. {
  252. ShowMessage( stdout, L"\n" );
  253. }
  254. //
  255. // display the results
  256. ShowResults( MAX_COLUMNS, m_pColumns, m_dwFormat, m_arrData );
  257. }
  258. VOID CSystemInfo::ShowUsage()
  259. /*++
  260. // Routine Description:
  261. // This function fetches usage information from resource file and display it
  262. //
  263. // Arguments:
  264. // NONE
  265. //
  266. // Return Value:
  267. // NONE
  268. --*/
  269. {
  270. // local variables
  271. DWORD dw = 0;
  272. // start displaying the usage
  273. for( dw = ID_HELP_START; dw <= ID_HELP_END; dw++ )
  274. {
  275. ShowMessage( stdout, GetResString( dw ) );
  276. }
  277. }