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.

361 lines
9.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. VOID
  118. PrintUsage(
  119. IN ULONG iCommand
  120. );
  121. /*++**************************************************************
  122. NAME: main()
  123. main() function, the primary entry point into the program.
  124. When this function exits, the program exits.
  125. TAKES: argc : count of string entries in argv
  126. argv : array of space-delimited strings on the command line
  127. RETURNS: the exit code (or errorlevel) for the process
  128. CALLED BY: the system
  129. FREE WITH: n/a
  130. **************************************************************--*/
  131. extern "C"
  132. int __cdecl
  133. wmain(ULONG argc,
  134. LPWSTR argv[]) {
  135. ULONG Command = 0;
  136. ULONG iAction, iArg, iCommand, iParam ;
  137. BOOLEAN Found;
  138. NTSTATUS Status;
  139. PAction Actions;
  140. ULONG ActionCount = 0;
  141. BOOL StuffToDo = FALSE; // used only for debugging
  142. // GlobalDebugFlags = 0xFF;
  143. // lazy way to do this.
  144. Actions = (PAction) malloc( argc * sizeof( *Actions ) );
  145. if ( !Actions ) {
  146. printf( "Failed to allocate array.\n" );
  147. return -1;
  148. }
  149. ZeroMemory( Actions, argc * sizeof( *Actions ));
  150. for ( iArg = 1; iArg < argc; /* iArg++ */ ) {
  151. Found = FALSE;
  152. DEBUGPRINT( DEBUG_OPTIONS,
  153. ( "Searching for iArg=%ld, %ws..\n",
  154. iArg,
  155. argv[ iArg ] ) );
  156. // first, find the command.
  157. if ( FindCommand( argv[ iArg ],
  158. &iCommand ) ) {
  159. iArg++;
  160. Actions[ActionCount].CommandNumber = iCommand;
  161. if ( StoreArguments( iArg, // starting here
  162. argv,
  163. &iArg, // moves past last arg
  164. &Actions[ ActionCount ] ) ) {
  165. if ( Commands[iCommand].flags & DO_COMMAND_IMMEDIATELY ) {
  166. DEBUGPRINT( DEBUG_LAUNCH,
  167. ( "Doing %ws immediately:\n",
  168. Commands[ iCommand ].Name ) );
  169. Status = Commands[iCommand].Function(
  170. Actions[ActionCount].Parameter );
  171. DEBUGPRINT( DEBUG_OPTIONS,
  172. ( "%ws returned 0x%x\n",
  173. Commands[ iCommand ].Name,
  174. Status ) );
  175. if ( NT_SUCCESS( Status ) ) {
  176. if ( Commands[ iCommand ].ConfirmationText ) {
  177. printf( "NOTE: %ws %hs\n",
  178. Commands[ iCommand ].Name,
  179. Commands[ iCommand ].ConfirmationText );
  180. }
  181. } else {
  182. if ( Status == STATUS_INVALID_PARAMETER ) {
  183. PrintUsage( iCommand );
  184. } else {
  185. printf( "%ws failed: 0x%x.\n",
  186. Commands[ iCommand ].Name,
  187. Status);
  188. }
  189. goto Cleanup;
  190. }
  191. } else {
  192. // need to do this command later.
  193. StuffToDo = TRUE;
  194. ActionCount++;
  195. }
  196. } else { // StoreArgs
  197. printf( "use %ws /? for help.\n",
  198. argv[ 0 ] );
  199. return -1;
  200. }
  201. } else {
  202. printf( "use %ws /? for help.\n",
  203. argv[ 0 ] );
  204. return -1;
  205. }
  206. } // argument loop.
  207. Status = OpenLsa();
  208. if (!NT_SUCCESS(Status)) {
  209. printf("Failed to open lsa: 0x%x\n",Status);
  210. goto Cleanup;
  211. }
  212. if ( StuffToDo ) {
  213. DEBUGPRINT( DEBUG_OPTIONS,
  214. ( "------------------ %hs -------------------\n",
  215. "performing delayed actions now" ) );
  216. }
  217. for (iAction = 0; iAction < ActionCount ; iAction++ ) {
  218. if (!( Commands[Actions[iAction].CommandNumber].flags &
  219. DO_COMMAND_IMMEDIATELY )) {
  220. DEBUGPRINT( DEBUG_LAUNCH,
  221. ( "Doing %ws:\n",
  222. Commands[ Actions[ iAction ].CommandNumber ].Name ) );
  223. Status = Commands[Actions[iAction].CommandNumber].Function(
  224. Actions[iAction].Parameter);
  225. DEBUGPRINT( DEBUG_OPTIONS,
  226. ( "%ws: 0x%x\n",
  227. Commands[ Actions[ iAction ].CommandNumber ].Name,
  228. Status ) );
  229. if (!NT_SUCCESS(Status)) {
  230. if (Status == STATUS_INVALID_PARAMETER) {
  231. PrintUsage( Actions[ iAction ].CommandNumber );
  232. } else {
  233. printf("Failed %ws : 0x%x\n",
  234. Commands[ Actions[ iAction ].CommandNumber ].Name,
  235. Status);
  236. }
  237. goto Cleanup;
  238. } else {
  239. if ( Commands[ Actions[ iAction ].CommandNumber ].ConfirmationText ) {
  240. printf( "NOTE: %ws %hs\n",
  241. Commands[ Actions[ iAction ].CommandNumber ].Name,
  242. Commands[ Actions[ iAction ].CommandNumber ].ConfirmationText );
  243. }
  244. }
  245. }
  246. }
  247. if (ActionCount == 0) {
  248. DumpState(NULL);
  249. }
  250. Cleanup:
  251. if (LsaHandle != NULL) {
  252. Status = LsaClose(LsaHandle);
  253. if (!NT_SUCCESS(Status)) {
  254. printf("Failed to close handle: 0x%x\n",Status);
  255. }
  256. }
  257. return 0;
  258. }