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
10 KiB

  1. #include "pch.h"
  2. #pragma hdrstop
  3. #include "stdio.h"
  4. #include "ncstring.h"
  5. #include "ssdpapi.h"
  6. #define _SECOND ((__int64) 10000000)
  7. #define _MINUTE (60 * _SECOND)
  8. #define _HOUR (60 * _MINUTE)
  9. #define _DAY (24 * _HOUR)
  10. const DWORD MAX_DATA = 1024;
  11. #define BUFFER_SIZE 64*1024
  12. #define MAX_BUFFER_SIZE 10*1024*1024
  13. ULONG CurrentBufferSize;
  14. PUCHAR PreviousBuffer;
  15. PUCHAR CurrentBuffer;
  16. PUCHAR TempBuffer;
  17. BOOLEAN Interactive;
  18. ULONG NumberOfInputRecords;
  19. INPUT_RECORD InputRecord;
  20. HANDLE InputHandle;
  21. HANDLE OriginalOutputHandle;
  22. HANDLE OutputHandle;
  23. DWORD OriginalInputMode;
  24. ULONG NumberOfCols;
  25. ULONG NumberOfRows;
  26. ULONG NumberOfDetailLines;
  27. ULONG FirstDetailLine;
  28. CONSOLE_SCREEN_BUFFER_INFO OriginalConsoleInfo;
  29. struct EVTSRC_DATA
  30. {
  31. CHAR szEvtSrc[MAX_PATH];
  32. DWORD dwLastDiff;
  33. };
  34. EVTSRC_DATA g_rgData[MAX_DATA];
  35. DWORD CsecDiffFileTime(FILETIME * pft1, FILETIME *pft2)
  36. {
  37. ULONGLONG qwResult1;
  38. ULONGLONG qwResult2;
  39. LONG lDiff;
  40. // Copy the time into a quadword.
  41. qwResult1 = (((ULONGLONG) pft1->dwHighDateTime) << 32) + pft1->dwLowDateTime;
  42. qwResult2 = (((ULONGLONG) pft2->dwHighDateTime) << 32) + pft2->dwLowDateTime;
  43. lDiff = (LONG) (((LONGLONG)(qwResult2 - qwResult1)) / _SECOND);
  44. lDiff = max(0, lDiff);
  45. return lDiff;
  46. }
  47. BOOL
  48. WriteConsoleLine(
  49. HANDLE OutputHandle,
  50. WORD LineNumber,
  51. LPSTR Text,
  52. BOOL Highlight
  53. )
  54. {
  55. COORD WriteCoord;
  56. DWORD NumberWritten;
  57. DWORD TextLength;
  58. WriteCoord.X = 0;
  59. WriteCoord.Y = LineNumber;
  60. if (!FillConsoleOutputCharacter(OutputHandle,' ', NumberOfCols,
  61. WriteCoord, &NumberWritten))
  62. {
  63. return FALSE;
  64. }
  65. if (Text == NULL || (TextLength = strlen( Text )) == 0)
  66. {
  67. return TRUE;
  68. }
  69. else
  70. {
  71. return WriteConsoleOutputCharacter(OutputHandle, Text, TextLength,
  72. WriteCoord, &NumberWritten);
  73. }
  74. }
  75. EXTERN_C
  76. VOID
  77. __cdecl
  78. main (
  79. IN INT argc,
  80. IN PCSTR argv[])
  81. {
  82. FILE * pInputFile = NULL;
  83. CHAR szBuf[1024];
  84. DWORD iData = 0;
  85. DWORD cData = 0;
  86. EVTSRC_INFO info = {0};
  87. EVTSRC_INFO * pinfo = &info;
  88. BOOL fDone = FALSE;
  89. int i;
  90. ULONG DelayTimeMsec;
  91. ULONG DelayTimeTicks;
  92. ULONG LastCount;
  93. COORD cp;
  94. BOOLEAN Active;
  95. CHAR OutputBuffer[1024];
  96. UCHAR LastKey;
  97. LONG ScrollDelta;
  98. WORD DisplayLine, LastDetailRow;
  99. BOOLEAN DoQuit = FALSE;
  100. ULONG SkipLine;
  101. ULONG Hint;
  102. ULONG Offset1;
  103. SIZE_T SumCommit;
  104. int num;
  105. int lastnum;
  106. INPUT_RECORD InputRecord;
  107. DWORD NumRead;
  108. ULONG Cpu;
  109. ULONG NoScreenChanges = FALSE;
  110. if (argc != 2)
  111. {
  112. printf("No input file specified!\n");
  113. exit(0);
  114. }
  115. SsdpStartup();
  116. pInputFile = fopen(argv[1], "r");
  117. while (!fDone)
  118. {
  119. if (pInputFile)
  120. {
  121. // If there was an error reading the file
  122. //
  123. if (!fgets(szBuf, sizeof(szBuf), pInputFile))
  124. {
  125. // If it wasn't eof, print an error
  126. //
  127. if (!feof(pInputFile))
  128. {
  129. _tprintf(TEXT("\nFailure reading script file\n\n"));
  130. }
  131. else
  132. {
  133. _tprintf(TEXT("\n[Script complete]\n\n"));
  134. }
  135. fclose(pInputFile);
  136. fDone = TRUE;
  137. }
  138. else
  139. {
  140. CHAR *pch;
  141. // Strip trailing linefeeds
  142. pch = strtok(szBuf, "\r\n");
  143. lstrcpy(g_rgData[iData].szEvtSrc, szBuf);
  144. g_rgData[iData].dwLastDiff = 0xFFFFFFFF;
  145. iData++;
  146. }
  147. }
  148. }
  149. cData = iData;
  150. InputHandle = GetStdHandle( STD_INPUT_HANDLE );
  151. OriginalOutputHandle = GetStdHandle( STD_OUTPUT_HANDLE );
  152. Interactive = TRUE;
  153. if (Interactive)
  154. {
  155. if (InputHandle == NULL ||
  156. OriginalOutputHandle == NULL ||
  157. !GetConsoleMode( InputHandle, &OriginalInputMode )
  158. )
  159. {
  160. Interactive = FALSE;
  161. }
  162. else
  163. {
  164. OutputHandle = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE,
  165. FILE_SHARE_WRITE | FILE_SHARE_READ,
  166. NULL,
  167. CONSOLE_TEXTMODE_BUFFER,
  168. NULL
  169. );
  170. if (OutputHandle == NULL ||
  171. !GetConsoleScreenBufferInfo( OriginalOutputHandle, &OriginalConsoleInfo ) ||
  172. !SetConsoleScreenBufferSize( OutputHandle, OriginalConsoleInfo.dwSize ) ||
  173. !SetConsoleActiveScreenBuffer( OutputHandle ) ||
  174. !SetConsoleMode( InputHandle, 0 )
  175. )
  176. {
  177. if (OutputHandle != NULL)
  178. {
  179. CloseHandle( OutputHandle );
  180. OutputHandle = NULL;
  181. }
  182. Interactive = FALSE;
  183. }
  184. else
  185. {
  186. NumberOfCols = OriginalConsoleInfo.dwSize.X;
  187. NumberOfRows = OriginalConsoleInfo.dwSize.Y;
  188. NumberOfDetailLines = NumberOfRows - 7;
  189. }
  190. }
  191. }
  192. DelayTimeMsec = 500;
  193. DelayTimeTicks = DelayTimeMsec * 10000;
  194. Active = TRUE;
  195. sprintf(OutputBuffer,
  196. " "
  197. "%-15s "
  198. "%-35s "
  199. "%-6s "
  200. "%-30s "
  201. "%-4s "
  202. "%-32s",
  203. "EvtSrc",
  204. "Notif To",
  205. "Seq",
  206. "Expires",
  207. "TO",
  208. "SID");
  209. WriteConsoleLine(OutputHandle, 0, OutputBuffer, FALSE);
  210. while (TRUE)
  211. {
  212. for (DisplayLine = 1, iData = 0; iData < cData; iData++)
  213. {
  214. if (GetEventSourceInfo(g_rgData[iData].szEvtSrc, &pinfo))
  215. {
  216. DWORD iSub;
  217. for (iSub = 0;
  218. iSub < pinfo->cSubs ? pinfo->cSubs : 0;
  219. iSub++)
  220. {
  221. SYSTEMTIME st;
  222. FILETIME ftCur;
  223. TCHAR szLocalDate[255];
  224. TCHAR szLocalTime[255];
  225. CHAR szExp[255];
  226. DWORD csecDiff;
  227. FileTimeToSystemTime(&pinfo->rgSubs[iSub].ftTimeout, &st);
  228. GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL,
  229. szLocalDate, 255);
  230. GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL,
  231. szLocalTime, 255);
  232. GetSystemTimeAsFileTime(&ftCur);
  233. FileTimeToLocalFileTime(&ftCur, &ftCur);
  234. csecDiff = CsecDiffFileTime(&ftCur,
  235. &pinfo->rgSubs[iSub].ftTimeout);
  236. if (csecDiff < g_rgData[iData].dwLastDiff)
  237. {
  238. g_rgData[iData].dwLastDiff = csecDiff;
  239. }
  240. if (g_rgData[iData].dwLastDiff < 10)
  241. {
  242. MessageBeep(MB_OK);
  243. }
  244. sprintf(szExp, "%s %s (%d) (%d)",
  245. szLocalDate, szLocalTime,
  246. csecDiff, g_rgData[iData].dwLastDiff);
  247. sprintf(OutputBuffer,
  248. "%ld) "
  249. "%-15s "
  250. "%-35s "
  251. "%-6ld "
  252. "%-30s "
  253. "%-4ld "
  254. "%-32s",
  255. DisplayLine,
  256. g_rgData[iData].szEvtSrc,
  257. pinfo->rgSubs[iSub].szDestUrl,
  258. pinfo->rgSubs[iSub].iSeq,
  259. szExp,
  260. pinfo->rgSubs[iSub].csecTimeout,
  261. pinfo->rgSubs[iSub].szSid);
  262. free(pinfo->rgSubs[iSub].szDestUrl);
  263. free(pinfo->rgSubs[iSub].szSid);
  264. }
  265. // Just free the rest of them
  266. for (iSub = 1; iSub < pinfo->cSubs; iSub++)
  267. {
  268. free(pinfo->rgSubs[iSub].szDestUrl);
  269. free(pinfo->rgSubs[iSub].szSid);
  270. }
  271. free(pinfo->rgSubs);
  272. ZeroMemory(pinfo, sizeof(EVTSRC_INFO));
  273. WriteConsoleLine(OutputHandle, DisplayLine++, OutputBuffer, FALSE);
  274. }
  275. }
  276. while (WaitForSingleObject( InputHandle, DelayTimeMsec ) == STATUS_WAIT_0)
  277. {
  278. //
  279. // Check for input record
  280. //
  281. if (ReadConsoleInput( InputHandle, &InputRecord, 1, &NumberOfInputRecords ) &&
  282. InputRecord.EventType == KEY_EVENT &&
  283. InputRecord.Event.KeyEvent.bKeyDown
  284. )
  285. {
  286. LastKey = InputRecord.Event.KeyEvent.uChar.AsciiChar;
  287. if (LastKey < ' ')
  288. {
  289. ScrollDelta = 0;
  290. if (LastKey == 'C'-'A'+1)
  291. {
  292. DoQuit = TRUE;
  293. }
  294. else switch (InputRecord.Event.KeyEvent.wVirtualKeyCode)
  295. {
  296. case VK_ESCAPE:
  297. DoQuit = TRUE;
  298. break;
  299. }
  300. }
  301. break;
  302. }
  303. }
  304. if (DoQuit)
  305. {
  306. if (Interactive)
  307. {
  308. SetConsoleActiveScreenBuffer( OriginalOutputHandle );
  309. SetConsoleMode( InputHandle, OriginalInputMode );
  310. CloseHandle( OutputHandle );
  311. }
  312. break;
  313. }
  314. }
  315. SsdpCleanup();
  316. }