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.

330 lines
9.2 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. // ***************************************************************************
  28. // Routine Description:
  29. // processes and validates the command line inputs
  30. //
  31. // Arguments:
  32. // [ in ] argc : no. of input arguments specified
  33. // [ in ] argv : input arguments specified at command prompt
  34. //
  35. // Return Value:
  36. // TRUE : if inputs are valid
  37. // FALSE : if inputs were errorneously specified
  38. //
  39. // ***************************************************************************
  40. BOOL CSystemInfo::ProcessOptions( DWORD argc, LPCTSTR argv[] )
  41. {
  42. // local variables
  43. CHString strFormat;
  44. BOOL bNoHeader = FALSE;
  45. PTCMDPARSER pcmdOptions = NULL;
  46. // temporary local variables
  47. LPWSTR pwszFormat = NULL;
  48. LPWSTR pwszServer = NULL;
  49. LPWSTR pwszUserName = NULL;
  50. LPWSTR pwszPassword = NULL;
  51. PTCMDPARSER pOption = NULL;
  52. PTCMDPARSER pOptionServer = NULL;
  53. PTCMDPARSER pOptionUserName = NULL;
  54. PTCMDPARSER pOptionPassword = NULL;
  55. //
  56. // prepare the command options
  57. pcmdOptions = new TCMDPARSER[ MAX_OPTIONS ];
  58. if ( pcmdOptions == NULL )
  59. {
  60. SetLastError( E_OUTOFMEMORY );
  61. SaveLastError();
  62. return FALSE;
  63. }
  64. else
  65. {
  66. ZeroMemory( pcmdOptions, MAX_OPTIONS * sizeof( TCMDPARSER ) );
  67. }
  68. try
  69. {
  70. // get the internal buffers
  71. pwszFormat = strFormat.GetBufferSetLength( MAX_STRING_LENGTH );
  72. pwszServer = m_strServer.GetBufferSetLength( MAX_STRING_LENGTH );
  73. pwszUserName = m_strUserName.GetBufferSetLength( MAX_STRING_LENGTH );
  74. pwszPassword = m_strPassword.GetBufferSetLength( MAX_STRING_LENGTH );
  75. // init the password contents with '*'
  76. lstrcpy( pwszPassword, L"*" );
  77. }
  78. catch( ... )
  79. {
  80. SetLastError( E_OUTOFMEMORY );
  81. SaveLastError();
  82. return FALSE;
  83. }
  84. // -?
  85. pOption = pcmdOptions + OI_USAGE;
  86. pOption->dwCount = 1;
  87. pOption->dwActuals = 0;
  88. pOption->dwFlags = CP_USAGE;
  89. pOption->pValue = &m_bUsage;
  90. pOption->pFunction = NULL;
  91. pOption->pFunctionData = NULL;
  92. lstrcpy( pOption->szValues, NULL_STRING );
  93. lstrcpy( pOption->szOption, OPTION_USAGE );
  94. // -s
  95. pOption = pcmdOptions + OI_SERVER;
  96. pOption->dwCount = 1;
  97. pOption->dwActuals = 0;
  98. pOption->pValue = pwszServer;
  99. pOption->pFunction = NULL;
  100. pOption->pFunctionData = NULL;
  101. pOption->dwFlags = CP_TYPE_TEXT | CP_VALUE_MANDATORY;
  102. lstrcpy( pOption->szValues, NULL_STRING );
  103. lstrcpy( pOption->szOption, OPTION_SERVER );
  104. // -u
  105. pOption = pcmdOptions + OI_USERNAME;
  106. pOption->dwCount = 1;
  107. pOption->dwActuals = 0;
  108. pOption->dwFlags = CP_TYPE_TEXT | CP_VALUE_MANDATORY;
  109. pOption->pValue = pwszUserName;
  110. pOption->pFunction = NULL;
  111. pOption->pFunctionData = NULL;
  112. lstrcpy( pOption->szValues, NULL_STRING );
  113. lstrcpy( pOption->szOption, OPTION_USERNAME );
  114. // -p
  115. pOption = pcmdOptions + OI_PASSWORD;
  116. pOption->dwCount = 1;
  117. pOption->dwActuals = 0;
  118. pOption->dwFlags = CP_TYPE_TEXT | CP_VALUE_OPTIONAL;
  119. pOption->pValue = pwszPassword;
  120. pOption->pFunction = NULL;
  121. pOption->pFunctionData = NULL;
  122. lstrcpy( pOption->szValues, NULL_STRING );
  123. lstrcpy( pOption->szOption, OPTION_PASSWORD );
  124. // -format
  125. pOption = pcmdOptions + OI_FORMAT;
  126. pOption->dwCount = 1;
  127. pOption->dwActuals = 0;
  128. pOption->dwFlags = CP_TYPE_TEXT | CP_VALUE_MANDATORY | CP_MODE_VALUES;
  129. pOption->pValue = pwszFormat;
  130. pOption->pFunction = NULL;
  131. pOption->pFunctionData = NULL;
  132. lstrcpy( pOption->szValues, OVALUES_FORMAT );
  133. lstrcpy( pOption->szOption, OPTION_FORMAT );
  134. // -noheader
  135. pOption = pcmdOptions + OI_NOHEADER;
  136. pOption->dwCount = 1;
  137. pOption->dwActuals = 0;
  138. pOption->dwFlags = 0;
  139. pOption->pValue = &bNoHeader;
  140. pOption->pFunction = NULL;
  141. pOption->pFunctionData = NULL;
  142. lstrcpy( pOption->szValues, NULL_STRING );
  143. lstrcpy( pOption->szOption, OPTION_NOHEADER );
  144. //
  145. // now, check the mutually exclusive options
  146. pOptionServer = pcmdOptions + OI_SERVER;
  147. pOptionUserName = pcmdOptions + OI_USERNAME;
  148. pOptionPassword = pcmdOptions + OI_PASSWORD;
  149. //
  150. // do the parsing
  151. if ( DoParseParam( argc, argv, MAX_OPTIONS, pcmdOptions ) == FALSE )
  152. {
  153. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  154. return FALSE; // invalid syntax
  155. }
  156. // release the buffers
  157. strFormat.ReleaseBuffer();
  158. m_strServer.ReleaseBuffer();
  159. m_strUserName.ReleaseBuffer();
  160. m_strPassword.ReleaseBuffer();
  161. // check the usage option
  162. if ( m_bUsage && ( argc > 2 ) )
  163. {
  164. // no other options are accepted along with -? option
  165. SetLastError( MK_E_SYNTAX );
  166. SetReason( ERROR_INVALID_USAGE_REQUEST );
  167. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  168. return FALSE;
  169. }
  170. else if ( m_bUsage == TRUE )
  171. {
  172. // should not do the furthur validations
  173. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  174. return TRUE;
  175. }
  176. // empty server name is not valid
  177. if ( pOptionServer->dwActuals != 0 && m_strServer.GetLength() == 0 )
  178. {
  179. SetReason( ERROR_SERVERNAME_EMPTY );
  180. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  181. return FALSE; // indicate failure
  182. }
  183. // empty user is not valid
  184. if ( pOptionUserName->dwActuals != 0 && m_strUserName.GetLength() == 0 )
  185. {
  186. SetReason( ERROR_USERNAME_EMPTY );
  187. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  188. return FALSE;
  189. }
  190. // "-u" should not be specified without machine names
  191. if ( pOptionServer->dwActuals == 0 && pOptionUserName->dwActuals != 0 )
  192. {
  193. // invalid syntax
  194. SetReason( ERROR_USERNAME_BUT_NOMACHINE );
  195. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  196. return FALSE; // indicate failure
  197. }
  198. // "-p" should not be specified without "-u"
  199. if ( pOptionUserName->dwActuals == 0 && pOptionPassword->dwActuals != 0 )
  200. {
  201. // invalid syntax
  202. SetReason( ERROR_PASSWORD_BUT_NOUSERNAME );
  203. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  204. return FALSE; // indicate failure
  205. }
  206. // determine the format in which the process information has to be displayed
  207. m_dwFormat = SR_FORMAT_LIST; // default format
  208. if ( strFormat.CompareNoCase( TEXT_FORMAT_LIST ) == 0 )
  209. m_dwFormat = SR_FORMAT_LIST;
  210. else if ( strFormat.CompareNoCase( TEXT_FORMAT_TABLE ) == 0 )
  211. m_dwFormat = SR_FORMAT_TABLE;
  212. else if ( strFormat.CompareNoCase( TEXT_FORMAT_CSV ) == 0 )
  213. m_dwFormat = SR_FORMAT_CSV;
  214. // user might have given no header option for a LIST format which is invalid
  215. if ( bNoHeader == TRUE && m_dwFormat == SR_FORMAT_LIST )
  216. {
  217. // invalid syntax
  218. SetReason( ERROR_NH_NOTSUPPORTED );
  219. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  220. return FALSE; // indicate failure
  221. }
  222. // check for the no header info and apply to the format variable
  223. if ( bNoHeader == TRUE )
  224. m_dwFormat |= SR_NOHEADER;
  225. // check whether caller should accept the password or not
  226. // if user has specified -s (or) -u and no "-p", then utility should accept password
  227. // the user will be prompter for the password only if establish connection
  228. // is failed without the credentials information
  229. if ( pOptionPassword->dwActuals != 0 && m_strPassword.Compare( L"*" ) == 0 )
  230. {
  231. // user wants the utility to prompt for the password before trying to connect
  232. m_bNeedPassword = TRUE;
  233. }
  234. else if ( (pOptionPassword->dwActuals == 0 &&
  235. (pOptionServer->dwActuals != 0 || pOptionUserName->dwActuals != 0)) )
  236. {
  237. // utility needs to try to connect first and if it fails then prompt for the password
  238. m_bNeedPassword = TRUE;
  239. m_strPassword.Empty();
  240. }
  241. // command-line parsing is successfull
  242. RELEASE_MEMORY_EX( pcmdOptions ); // clear memory
  243. return TRUE;
  244. }
  245. // ***************************************************************************
  246. // Routine Description:
  247. // show the system configuration information
  248. //
  249. // Arguments:
  250. // NONE
  251. //
  252. // Return Value:
  253. // NONE
  254. //
  255. // ***************************************************************************
  256. VOID CSystemInfo::ShowOutput( DWORD dwStart, DWORD dwEnd )
  257. {
  258. // local variables
  259. PTCOLUMNS pColumn = NULL;
  260. // dynamically show / hide columns on need basis
  261. for( DWORD dw = 0; dw < MAX_COLUMNS; dw++ )
  262. {
  263. // point to the column info
  264. pColumn = m_pColumns + dw;
  265. // remove the hide flag from the column
  266. pColumn->dwFlags &= ~( SR_HIDECOLUMN );
  267. // now if the column should not be shown, set the hide flag)
  268. if ( dw < dwStart || dw > dwEnd )
  269. pColumn->dwFlags |= SR_HIDECOLUMN;
  270. }
  271. // if the data is being displayed from the first line onwards,
  272. // add a blank line
  273. if ( dwStart == 0 )
  274. ShowMessage( stdout, L"\n" );
  275. //
  276. // display the results
  277. ShowResults( MAX_COLUMNS, m_pColumns, m_dwFormat, m_arrData );
  278. }
  279. // ***************************************************************************
  280. // Routine Description:
  281. // This function fetches usage information from resource file and shows it
  282. //
  283. // Arguments:
  284. // NONE
  285. //
  286. // Return Value:
  287. // NONE
  288. // ***************************************************************************
  289. VOID CSystemInfo::ShowUsage()
  290. {
  291. // local variables
  292. DWORD dw = 0;
  293. // start displaying the usage
  294. for( dw = ID_HELP_START; dw <= ID_HELP_END; dw++ )
  295. ShowMessage( stdout, GetResString( dw ) );
  296. }