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.

372 lines
7.1 KiB

  1. /*++
  2. MAIN.CXX
  3. Copyright (C) 1999 Microsoft Corporation, all rights reserved.
  4. DESCRIPTION: main() for ksetup
  5. Created, May 21, 1999 by DavidCHR.
  6. --*/
  7. #define EXTERN // nothing
  8. #include "everything.hxx"
  9. extern
  10. CommandPair Commands[ ANYSIZE_ARRAY ];
  11. extern
  12. ULONG cCommands;
  13. extern TestFunc DumpState;
  14. extern NTSTATUS
  15. OpenLsa( VOID ); // in support.cxx
  16. BOOL
  17. FindCommand( IN LPWSTR CommandName,
  18. OUT PULONG pulCommandId ) {
  19. ULONG iCommand;
  20. for (iCommand = 0; iCommand < cCommands ; iCommand++ ) {
  21. if ( 0 == lstrcmpi( CommandName,
  22. Commands[iCommand].Name) ) {
  23. // found the command.
  24. DEBUGPRINT( DEBUG_OPTIONS,
  25. ( "Found %ws (command %ld)",
  26. CommandName,
  27. iCommand ) );
  28. *pulCommandId = iCommand;
  29. return TRUE;
  30. }
  31. }
  32. printf( "%ws: no such argument.\n",
  33. CommandName );
  34. return FALSE;
  35. }
  36. BOOL
  37. StoreArguments( IN ULONG iArg,
  38. IN LPWSTR *argv,
  39. OUT PULONG piArg,
  40. IN OUT PAction pAction ) {
  41. ULONG iCommand = pAction->CommandNumber;
  42. PCommandPair pCommand = Commands + iCommand;
  43. ULONG iParam = 0;
  44. BOOL ret = TRUE;
  45. BOOL KeepParsing = FALSE;
  46. DEBUGPRINT( DEBUG_OPTIONS, ( "Building %ld-arg array.\n",
  47. Commands[ iCommand ].Parameter ) );
  48. if ( pCommand->Parameter != 0 ) {
  49. /* this command has required parameters.
  50. Copy them as we go. */
  51. for ( iParam = 0;
  52. ret && argv[ iArg ] && ( iParam < MAX_COMMANDS -1 );
  53. iArg++, iParam ++ ) {
  54. ASSERT( argv[ iArg ] != NULL );
  55. DEBUGPRINT( DEBUG_OPTIONS,
  56. ( "Evaluating iParam=%ld, iArg=%ld, \"%ws\"\n",
  57. iParam,
  58. iArg,
  59. argv[ iArg ] ) );
  60. if ( argv[ iArg ][ 0 ] == L'/' ) {
  61. // this parameter is a switch. We're done.
  62. break;
  63. }
  64. // if we don't need any more arguments, don't consume this one.
  65. if ( ( iParam > pCommand->Parameter ) &&
  66. !( pCommand->flags & CAN_HAVE_MORE_ARGUMENTS ) ) {
  67. printf( "%ws only takes %ld arguments.\n",
  68. pCommand->Name,
  69. pCommand->Parameter );
  70. ret = FALSE;
  71. break;
  72. }
  73. // at this point, the options are consumable.
  74. pAction->Parameter[ iParam ] = argv[ iArg ];
  75. DEBUGPRINT( DEBUG_OPTIONS,
  76. ( "%ws's arg %ld is %ws\n",
  77. pCommand->Name,
  78. iParam,
  79. pAction->Parameter[ iParam ] ) );
  80. } // for loop
  81. if ( ret ) {
  82. // left the loop without error
  83. if ( ( iParam < pCommand->Parameter ) &&
  84. !( pCommand->flags &
  85. CAN_HAVE_FEWER_ARGUMENTS ) ) {
  86. // too few options.
  87. printf( "%ws requires %ld options (only %ld supplied).\n",
  88. pCommand->Name,
  89. pCommand->Parameter,
  90. iParam );
  91. ret = FALSE;
  92. }
  93. }
  94. } // parameter count check.
  95. if ( ret ) {
  96. pAction->Parameter[ iParam ] = NULL; /* null-terminate
  97. in all success cases. */
  98. *piArg = iArg;
  99. #if DBG
  100. DEBUGPRINT( DEBUG_OPTIONS,
  101. ( "Leaving StoreOptions-- %ws with iArg=%ld. %ld args:\n",
  102. pCommand->Name,
  103. iArg,
  104. iParam ));
  105. for ( iParam = 0;
  106. pAction->Parameter[ iParam ] != NULL ;
  107. iParam++ ) {
  108. DEBUGPRINT( DEBUG_OPTIONS,
  109. ( "arg %ld: %ws\n",
  110. iParam,
  111. pAction->Parameter[ iParam ] ) );
  112. }
  113. #endif
  114. }
  115. return ret;
  116. }
  117. /*++**************************************************************
  118. NAME: main()
  119. main() function, the primary entry point into the program.
  120. When this function exits, the program exits.
  121. TAKES: argc : count of string entries in argv
  122. argv : array of space-delimited strings on the command line
  123. RETURNS: the exit code (or errorlevel) for the process
  124. CALLED BY: the system
  125. FREE WITH: n/a
  126. **************************************************************--*/
  127. extern "C"
  128. int __cdecl
  129. wmain(ULONG argc,
  130. LPWSTR argv[]) {
  131. ULONG Command = 0;
  132. ULONG iAction, iArg, iCommand, iParam ;
  133. BOOLEAN Found;
  134. NTSTATUS Status;
  135. PAction Actions;
  136. ULONG ActionCount = 0;
  137. BOOL StuffToDo = FALSE; // used only for debugging
  138. // GlobalDebugFlags = 0xFF;
  139. // lazy way to do this.
  140. Actions = (PAction) malloc( argc * sizeof( *Actions ) );
  141. if ( !Actions ) {
  142. printf( "Failed to allocate array.\n" );
  143. return -1;
  144. }
  145. for (iArg = 1; iArg < argc ; ) // iArg++ )
  146. {
  147. Found = FALSE;
  148. DEBUGPRINT( DEBUG_OPTIONS,
  149. ( "Searching for iArg=%ld, %ws..\n",
  150. iArg,
  151. argv[ iArg ] ) );
  152. // first, find the command.
  153. if ( FindCommand( argv[ iArg ],
  154. &iCommand ) ) {
  155. iArg++;
  156. Actions[ActionCount].CommandNumber = iCommand;
  157. if ( StoreArguments( iArg, // starting here
  158. argv,
  159. &iArg, // moves past last arg
  160. &Actions[ ActionCount ] ) ) {
  161. if ( Commands[iCommand].flags & DO_COMMAND_IMMEDIATELY ) {
  162. DEBUGPRINT( DEBUG_LAUNCH,
  163. ( "Doing %ws immediately:\n",
  164. Commands[ iCommand ].Name ) );
  165. Status = Commands[iCommand].Function(
  166. Actions[ActionCount].Parameter );
  167. DEBUGPRINT( DEBUG_OPTIONS,
  168. ( "%ws returned 0x%x\n",
  169. Commands[ iCommand ].Name,
  170. Status ) );
  171. if ( NT_SUCCESS( Status ) ) {
  172. if ( Commands[ iCommand ].ConfirmationText ) {
  173. printf( "NOTE: %ws %hs\n",
  174. Commands[ iCommand ].Name,
  175. Commands[ iCommand ].ConfirmationText );
  176. }
  177. } else {
  178. printf( "%ws failed: 0x%x.\n",
  179. Commands[iCommand].Name,
  180. Status);
  181. goto Cleanup;
  182. }
  183. } else {
  184. // need to do this command later.
  185. StuffToDo = TRUE;
  186. ActionCount++;
  187. }
  188. } else { // StoreArgs
  189. printf( "use %ws /? for help.\n",
  190. argv[ 0 ] );
  191. return -1;
  192. }
  193. } else {
  194. printf( "use %ws /? for help.\n",
  195. argv[ 0 ] );
  196. return -1;
  197. }
  198. } // argument loop.
  199. Status = OpenLsa();
  200. if (!NT_SUCCESS(Status))
  201. {
  202. printf("Failed to open lsa: 0x%x\n",Status);
  203. goto Cleanup;
  204. }
  205. if ( StuffToDo ) {
  206. DEBUGPRINT( DEBUG_OPTIONS,
  207. ( "------------------ %hs -------------------\n",
  208. "performing delayed actions now" ) );
  209. }
  210. for (iAction = 0; iAction < ActionCount ; iAction++ )
  211. {
  212. if (!( Commands[Actions[iAction].CommandNumber].flags &
  213. DO_COMMAND_IMMEDIATELY ))
  214. {
  215. DEBUGPRINT( DEBUG_LAUNCH,
  216. ( "Doing %ws:\n",
  217. Commands[ Actions[ iAction ].CommandNumber ].Name ) );
  218. Status = Commands[Actions[iAction].CommandNumber].Function(
  219. Actions[iAction].Parameter);
  220. DEBUGPRINT( DEBUG_OPTIONS,
  221. ( "%ws: 0x%x\n",
  222. Commands[ Actions[ iAction ].CommandNumber ].Name,
  223. Status ) );
  224. if (!NT_SUCCESS(Status))
  225. {
  226. printf("Failed %ws : 0x%x\n",
  227. Commands[Actions[iAction].CommandNumber].Name,
  228. Status);
  229. goto Cleanup;
  230. } else {
  231. if ( Commands[ iCommand ].ConfirmationText ) {
  232. printf( "NOTE: %ws %hs\n",
  233. Commands[ iCommand ].Name,
  234. Commands[ iCommand ].ConfirmationText );
  235. }
  236. }
  237. }
  238. }
  239. if (ActionCount == 0)
  240. {
  241. DumpState(NULL);
  242. }
  243. Cleanup:
  244. if (LsaHandle != NULL)
  245. {
  246. Status = LsaClose(LsaHandle);
  247. if (!NT_SUCCESS(Status))
  248. {
  249. printf("Failed to close handle: 0x%x\n",Status);
  250. }
  251. }
  252. return 0;
  253. }