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.

275 lines
7.7 KiB

  1. //
  2. // main.c
  3. //
  4. // Program entry. Handle's callbacks, program initialization,
  5. // and commandline data.
  6. //
  7. // Copyright (C) 2001 Microsoft Corporation
  8. //
  9. // Author: a-devjen (Devin Jenson)
  10. //
  11. #include "tbscript.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <crtdbg.h>
  15. // Max number of characters per message.
  16. #define LOG_BUFFER_SIZE 2048
  17. // Set this to TRUE to print all messages to stdout.
  18. static BOOL VerbosePrinting = FALSE;
  19. // IdleCallback
  20. //
  21. // When a client does not respond for 30 seconds, a message is sent
  22. // to this function. Every 10 seconds after that, an additional
  23. // message is sent. Text contains the string in which the script
  24. // is "waiting on" which has not been found.
  25. void __cdecl IdleCallback(LPARAM lParam, LPCSTR Text, DWORD Seconds)
  26. {
  27. // Add any custom handler data here.
  28. }
  29. // PrintMessage
  30. //
  31. // Whenever a message needs to be printed to the console, this function
  32. // will be called.
  33. void PrintMessage(MESSAGETYPE eMsgType, LPCSTR lpszFormat, ...)
  34. {
  35. // Evaluate the message type.
  36. switch (eMsgType)
  37. {
  38. // Do not handle these message types for non-debugging
  39. case ALIVE_MESSAGE:
  40. case INFO_MESSAGE:
  41. if (VerbosePrinting == FALSE)
  42. return;
  43. // The remaining message types are always used
  44. case SCRIPT_MESSAGE:
  45. case IDLE_MESSAGE:
  46. case ERROR_MESSAGE:
  47. case WARNING_MESSAGE:
  48. break;
  49. }
  50. // We probably shouldn't trust the data pointed by lpszFormat
  51. __try {
  52. va_list arglist;
  53. // Allocate a buffer for us
  54. char *pszBuffer = (char *)HeapAlloc(GetProcessHeap(), 0, LOG_BUFFER_SIZE);
  55. // Validate the buffer
  56. if (pszBuffer == NULL) {
  57. printf("%s",
  58. "ERROR: PrintMessage() Failed - Not enough memory.\n");
  59. return;
  60. }
  61. // Format the message
  62. va_start(arglist, lpszFormat);
  63. _vsnprintf(pszBuffer, LOG_BUFFER_SIZE - 1, lpszFormat, arglist);
  64. pszBuffer[LOG_BUFFER_SIZE - 1] = '\0';
  65. va_end(arglist);
  66. // Print it
  67. printf("%s", pszBuffer);
  68. // Free the buffer
  69. HeapFree(GetProcessHeap(), 0, pszBuffer);
  70. }
  71. __except (EXCEPTION_EXECUTE_HANDLER) {
  72. return;
  73. }
  74. }
  75. // ShowUsage
  76. //
  77. // Prints a message to stdout the version, title, usage and other info.
  78. void ShowUsage(void)
  79. {
  80. char UsageString[] =
  81. "\nTerminal Bench Script (BUILD: " __DATE__ ")\n"
  82. "Copyright (C) 2001 Microsoft Corp.\n\n"
  83. "Usage:\n\ntbscript.exe <script> [- options]\n"
  84. "tbscript.exe -l\n\n"
  85. "Optional Parameters:\n\n"
  86. " -s:server - The default server to use.\n"
  87. "-u:username - The default username to use.\n"
  88. "-p:password - The default password to use.\n"
  89. " -d:domain - The default domain to use.\n"
  90. " -l:lang - Interpret as language (default = VBScript)\n"
  91. " -l - Display possible languages (no script).\n"
  92. " -f:flags - cahnge connection flags to <flags>.\n"
  93. " -a:\"args\" - Argument string which can be used in "
  94. "scripts.\n"
  95. " -v - Enable verbose printing.\n"
  96. " -wpm:# - Change default Words-Per-Minute "
  97. "(default = 35)\n"
  98. " -bpp:# - Change the Bits-Per-Pixels (default = "
  99. "MSTSC default)\n"
  100. " -xres:# - Change the resolution on the x-axis "
  101. "(default = 640)\n"
  102. " -yres:# - Change the resolution on the y-axis "
  103. "(default = 480)\n\n";
  104. printf("%s", UsageString);
  105. }
  106. // main
  107. //
  108. // Program entry function. Handles commandline, and
  109. // initializes TCLIENT and TBSCRIPT.
  110. int __cdecl main(int argc, char **argv)
  111. {
  112. int argi;
  113. // Defualt language
  114. WCHAR LangName[MAX_PATH] = L"VBScript";
  115. int ScpLen = 0;
  116. // Will hold the filename of the script
  117. WCHAR Script[MAX_PATH];
  118. // This is all the default data.. it's how MSTSC will be opened.
  119. TSClientData DesiredData = {
  120. L"",
  121. L"",
  122. L"",
  123. L"",
  124. 640, 480,
  125. TSFLAG_COMPRESSION | TSFLAG_BITMAPCACHE,
  126. 0, 0, 0, L""
  127. };
  128. // TCLIENT.DLL callback function
  129. SCINITDATA InitData = { PrintMessage };
  130. // Evaluate to the minimum and maximum number of arguments
  131. if (argc < 2 || argc > 10) {
  132. ShowUsage();
  133. return -1;
  134. }
  135. // Check if the first parameter ends with a question mark.
  136. // This will show the usage for stuff like: -? /? --? etc..
  137. if (argv[1][strlen(argv[1]) - 1] == '?') {
  138. ShowUsage();
  139. return 0;
  140. }
  141. // Record the script to run
  142. mbstowcs(Script, argv[1], sizeof(Script) / sizeof(WCHAR));
  143. // Attempt to auto set the language to JScript
  144. ScpLen = wcslen(Script);
  145. if ((ScpLen > 3 && _wcsicmp(L".js", Script + (ScpLen - 3)) == 0) ||
  146. (ScpLen > 4 && _wcsicmp(L".jvs", Script + (ScpLen - 4)) == 0))
  147. wcscpy(LangName, L"JScript");
  148. // Get all the arguments
  149. for (argi = 2; argi < argc; ++argi) {
  150. // Set the server
  151. if (strncmp("-s:", argv[argi], 3) == 0)
  152. mbstowcs(DesiredData.Server, argv[argi] + 3,
  153. SIZEOF_ARRAY(DesiredData.Server));
  154. // Set the username
  155. else if (strncmp("-u:", argv[argi], 3) == 0)
  156. mbstowcs(DesiredData.User, argv[argi] + 3,
  157. SIZEOF_ARRAY(DesiredData.User));
  158. // Set the password
  159. else if (strncmp("-p:", argv[argi], 3) == 0)
  160. mbstowcs(DesiredData.Pass, argv[argi] + 3,
  161. SIZEOF_ARRAY(DesiredData.Pass));
  162. // Set the domain
  163. else if (strncmp("-d:", argv[argi], 3) == 0)
  164. mbstowcs(DesiredData.Domain, argv[argi] + 3,
  165. SIZEOF_ARRAY(DesiredData.Domain));
  166. // Enable verbose debugging
  167. else if (strncmp("-v", argv[argi], 2) == 0)
  168. VerbosePrinting = TRUE;
  169. // Set the words per minute
  170. else if (strncmp("-wpm:", argv[argi], 5) == 0)
  171. DesiredData.WordsPerMinute = strtoul(argv[argi] + 5, NULL, 10);
  172. // Set the bits per pixel
  173. else if (strncmp("-bpp:", argv[argi], 5) == 0)
  174. DesiredData.BPP = strtoul(argv[argi] + 5, NULL, 10);
  175. // Set the resolution (x)
  176. else if (strncmp("-xres:", argv[argi], 6) == 0)
  177. DesiredData.xRes = strtoul(argv[argi] + 6, NULL, 10);
  178. // Set the resolution (y)
  179. else if (strncmp("-yres:", argv[argi], 6) == 0)
  180. DesiredData.yRes = strtoul(argv[argi] + 6, NULL, 10);
  181. // Set custom arguments
  182. else if (strncmp("-a:", argv[argi], 3) == 0)
  183. mbstowcs(DesiredData.Arguments, argv[argi] + 3,
  184. SIZEOF_ARRAY(DesiredData.Arguments));
  185. // Change the language
  186. else if (strncmp("-l:", argv[argi], 3) == 0)
  187. mbstowcs(LangName, argv[argi] + 3, SIZEOF_ARRAY(LangName));
  188. // Change the connection flags
  189. else if (strncmp("-f:", argv[argi], 3) == 0)
  190. DesiredData.Flags |= strtoul(argv[argi] + 3, NULL, 10);
  191. // Unknown option
  192. else {
  193. ShowUsage();
  194. return -1;
  195. }
  196. }
  197. // Check if we are to just display the engines
  198. if (wcscmp(Script, L"-l") == 0) {
  199. printf("%s", "\nPossible TBScript Scripting Languages:\n\n");
  200. SCPDisplayEngines();
  201. return 0;
  202. }
  203. // Initialize TCLIENT2.DLL and TCLIENT.DLL
  204. SCPStartupLibrary(&InitData, IdleCallback);
  205. // Execute the script...
  206. if (SCPRunScript(LangName, Script, &DesiredData, 0) == FALSE)
  207. printf("\nERROR: Failed to execute the script.\n");
  208. // Cleanup TCLIENT2.DLL
  209. SCPCleanupLibrary();
  210. return 0;
  211. }