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.

365 lines
9.0 KiB

  1. //Copyright (c) 1998 - 1999 Microsoft Corporation
  2. /******************************************************************************
  3. *
  4. * QAPPSRV.C
  5. *
  6. * query appserver information
  7. *
  8. *
  9. *******************************************************************************/
  10. /*
  11. * Includes
  12. */
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <locale.h>
  18. #include <lm.h>
  19. #include <winstaw.h>
  20. #include <utilsub.h>
  21. #include <printfoa.h>
  22. #include <winnlsp.h>
  23. #include "qappsrv.h"
  24. // max length of the locale string
  25. #define MAX_LOCALE_STRING 64
  26. /*=============================================================================
  27. == Global data
  28. =============================================================================*/
  29. WCHAR CurrentAppServer[MAXNAME];
  30. WCHAR AppServer[MAXNAME];
  31. WCHAR Domain[MAX_IDS_LEN+1];
  32. USHORT help_flag = FALSE;
  33. BOOLEAN MatchedOne = FALSE;
  34. USHORT fAddress = FALSE;
  35. USHORT fNoPage = FALSE;
  36. ULONG Rows = 23;
  37. HANDLE hConIn;
  38. HANDLE hConOut;
  39. TOKMAP ptm[] = {
  40. {L" ", TMFLAG_OPTIONAL, TMFORM_STRING, MAXNAME, AppServer},
  41. {L"/DOMAIN", TMFLAG_OPTIONAL, TMFORM_STRING, MAX_IDS_LEN, Domain},
  42. {L"/ADDRESS", TMFLAG_OPTIONAL, TMFORM_BOOLEAN,sizeof(USHORT), &fAddress},
  43. {L"/Continue",TMFLAG_OPTIONAL, TMFORM_BOOLEAN,sizeof(USHORT), &fNoPage },
  44. {L"/?", TMFLAG_OPTIONAL, TMFORM_BOOLEAN,sizeof(USHORT), &help_flag},
  45. {0, 0, 0, 0, 0}
  46. };
  47. /*=============================================================================
  48. == Internal Functions Defined
  49. =============================================================================*/
  50. void DisplayServer( LPTSTR, LPTSTR );
  51. void Usage( BOOLEAN bError );
  52. int _getch( void );
  53. /*=============================================================================
  54. == Functions used
  55. =============================================================================*/
  56. int AppServerEnum( void );
  57. void TreeTraverse( PTREETRAVERSE );
  58. /*******************************************************************************
  59. *
  60. * main
  61. *
  62. * main routine
  63. *
  64. * ENTRY:
  65. * argc (input)
  66. * number of command line arguments
  67. * argv (input)
  68. * pointer to arrary of command line arguments
  69. *
  70. * EXIT:
  71. * ERROR_SUCCESS - no error
  72. *
  73. ******************************************************************************/
  74. int __cdecl
  75. main(INT argc, CHAR **argv)
  76. {
  77. CONSOLE_SCREEN_BUFFER_INFO ScreenInfo;
  78. PSERVER_INFO_101 pCurrentServer;
  79. ULONG Status;
  80. ULONG rc;
  81. WCHAR **argvW;
  82. int i;
  83. WCHAR wszString[MAX_LOCALE_STRING + 1];
  84. Domain[0] = UNICODE_NULL;
  85. setlocale(LC_ALL, ".OCP");
  86. // We don't want LC_CTYPE set the same as the others or else we will see
  87. // garbage output in the localized version, so we need to explicitly
  88. // set it to correct console output code page
  89. _snwprintf(wszString, sizeof(wszString)/sizeof(WCHAR), L".%d", GetConsoleOutputCP());
  90. wszString[sizeof(wszString)/sizeof(WCHAR) - 1] = L'\0';
  91. _wsetlocale(LC_CTYPE, wszString);
  92. SetThreadUILanguage(0);
  93. /*
  94. * Massage the command line.
  95. */
  96. argvW = MassageCommandLine((DWORD)argc);
  97. if (argvW == NULL) {
  98. ErrorPrintf(IDS_ERROR_MALLOC);
  99. return(FAILURE);
  100. }
  101. /*
  102. * parse the cmd line without parsing the program name (argc-1, argv+1)
  103. */
  104. rc = ParseCommandLine(argc-1, argvW+1, ptm, 0);
  105. /*
  106. * Check for error from ParseCommandLine
  107. */
  108. if ( help_flag || (rc && !(rc & PARSE_FLAG_NO_PARMS)) ) {
  109. if ( !help_flag ) {
  110. Usage(TRUE);
  111. return(FAILURE);
  112. } else {
  113. Usage(FALSE);
  114. return(SUCCESS);
  115. }
  116. }
  117. /*
  118. * Get handle to console
  119. */
  120. hConIn = CreateFile( L"CONIN$", GENERIC_READ | GENERIC_WRITE,
  121. FILE_SHARE_READ | FILE_SHARE_WRITE,
  122. NULL, OPEN_EXISTING, 0, NULL );
  123. hConOut = CreateFile( L"CONOUT$", GENERIC_READ | GENERIC_WRITE,
  124. FILE_SHARE_READ | FILE_SHARE_WRITE,
  125. NULL, OPEN_EXISTING, 0, NULL );
  126. /*
  127. * Get the number of rows on the screen
  128. */
  129. if ( GetConsoleScreenBufferInfo( hConOut, &ScreenInfo ) )
  130. Rows = ScreenInfo.dwSize.Y - 2;
  131. /*
  132. * Get current server
  133. */
  134. Status = NetServerGetInfo( NULL, 101, (LPBYTE *) &pCurrentServer );
  135. if ( Status ) {
  136. ErrorPrintf(IDS_ERROR_SERVER_INFO, Status);
  137. PutStdErr( Status, 0 );
  138. return(FAILURE);
  139. }
  140. lstrcpyn( CurrentAppServer, pCurrentServer->sv101_name, MAXNAME );
  141. /*
  142. * Get the names and the count
  143. */
  144. //if ( rc = AppServerEnum() ) {
  145. // ErrorPrintf(IDS_ERROR_SERVER_ENUMERATE, rc );
  146. // PutStdErr( rc, 0 );
  147. // return(FAILURE);
  148. //}
  149. AppServerEnum();
  150. /*
  151. * Display names
  152. */
  153. TreeTraverse( DisplayServer );
  154. if (!MatchedOne)
  155. {
  156. if ( AppServer[0])
  157. {
  158. Message(IDS_ERROR_TERMSERVER_NOT_FOUND);
  159. }
  160. else
  161. {
  162. Message(IDS_ERROR_NO_TERMSERVER_IN_DOMAIN);
  163. }
  164. }
  165. if( pCurrentServer != NULL )
  166. {
  167. NetApiBufferFree( pCurrentServer );
  168. }
  169. return(SUCCESS);
  170. }
  171. /*******************************************************************************
  172. *
  173. * DisplayServer
  174. *
  175. * This routine displays information for one server
  176. *
  177. *
  178. * ENTRY:
  179. * pName (input)
  180. * pointer to server name
  181. * pAddress (input)
  182. * pointer to server address
  183. *
  184. * EXIT:
  185. * nothing
  186. *
  187. ******************************************************************************/
  188. void
  189. DisplayServer( LPTSTR pName, LPTSTR pAddress )
  190. {
  191. static ULONG RowCount = 0;
  192. /*
  193. * If appserver name was specified only show it
  194. */
  195. if ( AppServer[0] && _wcsicmp( pName, AppServer ) )
  196. return;
  197. /*
  198. * Page pause
  199. */
  200. if ( !(++RowCount % Rows) && !fNoPage ) {
  201. Message(IDS_PAUSE_MSG);
  202. _getch();
  203. wprintf(L"\n");
  204. }
  205. /*
  206. * If first time - output title
  207. */
  208. if ( !MatchedOne ) {
  209. Message( fAddress ? IDS_TITLE_ADDR : IDS_TITLE );
  210. Message( fAddress ? IDS_TITLE_ADDR1 : IDS_TITLE1 );
  211. MatchedOne = TRUE;
  212. }
  213. if ( fAddress ) {
  214. My_wprintf( L"%-37s%-21s\n", _wcsupr(pName), pAddress );
  215. } else {
  216. My_wprintf( L"%s\n", _wcsupr(pName) );
  217. }
  218. }
  219. /*******************************************************************************
  220. *
  221. * Usage
  222. *
  223. * Output the usage message for this utility.
  224. *
  225. * ENTRY:
  226. * bError (input)
  227. * TRUE if the 'invalid parameter(s)' message should preceed the usage
  228. * message and the output go to stderr; FALSE for no such error
  229. * string and output goes to stdout.
  230. *
  231. * EXIT:
  232. *
  233. *
  234. ******************************************************************************/
  235. void
  236. Usage( BOOLEAN bError )
  237. {
  238. if ( bError ) {
  239. ErrorPrintf(IDS_ERROR_INVALID_PARAMETERS);
  240. ErrorPrintf(IDS_HELP_USAGE1);
  241. ErrorPrintf(IDS_HELP_USAGE2);
  242. ErrorPrintf(IDS_HELP_USAGE3);
  243. ErrorPrintf(IDS_HELP_USAGE4);
  244. ErrorPrintf(IDS_HELP_USAGE5);
  245. ErrorPrintf(IDS_HELP_USAGE6);
  246. ErrorPrintf(IDS_HELP_USAGE7);
  247. } else {
  248. Message(IDS_HELP_USAGE1);
  249. Message(IDS_HELP_USAGE2);
  250. Message(IDS_HELP_USAGE3);
  251. Message(IDS_HELP_USAGE4);
  252. Message(IDS_HELP_USAGE5);
  253. Message(IDS_HELP_USAGE6);
  254. Message(IDS_HELP_USAGE7);
  255. }
  256. }
  257. int _getch( void )
  258. {
  259. INPUT_RECORD ConInpRec;
  260. DWORD NumRead;
  261. int ch = 0; /* single character buffer */
  262. DWORD oldstate = 0;
  263. /*
  264. * Switch to raw mode (no line input, no echo input)
  265. */
  266. GetConsoleMode( hConIn, &oldstate );
  267. SetConsoleMode( hConIn, 0L );
  268. for ( ; ; ) {
  269. /*
  270. * Get a console input event.
  271. */
  272. if ( !ReadConsoleInput( hConIn,
  273. &ConInpRec,
  274. 1L,
  275. &NumRead )
  276. || (NumRead == 0L) )
  277. {
  278. ch = EOF;
  279. break;
  280. }
  281. /*
  282. * Look for, and decipher, key events.
  283. */
  284. if ( (ConInpRec.EventType == KEY_EVENT) &&
  285. ConInpRec.Event.KeyEvent.bKeyDown ) {
  286. /*
  287. * Easy case: if uChar.AsciiChar is non-zero, just stuff it
  288. * into ch and quit.
  289. */
  290. if ( ch = (unsigned char)ConInpRec.Event.KeyEvent.uChar.AsciiChar )
  291. break;
  292. }
  293. }
  294. /*
  295. * Restore previous console mode.
  296. */
  297. SetConsoleMode( hConIn, oldstate );
  298. return ch;
  299. }