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.

537 lines
13 KiB

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <memory.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <windows.h>
  7. #include "list.h"
  8. BOOL IsValidKey (PINPUT_RECORD pRecord);
  9. int set_mode1(PCONSOLE_SCREEN_BUFFER_INFO pMode, int mono);
  10. void
  11. ShowHelp (
  12. void
  13. )
  14. {
  15. static struct {
  16. int x, y;
  17. char *text;
  18. } *pHelp, rgHelp[] = {
  19. 0, 0, "List - Help",
  20. 40, 0, "Rev 1.0j",
  21. 0, 1, "Keyboard:",
  22. 0, 2, "Up, Down, Left, Right",
  23. 0, 3, "PgUp - Up one page",
  24. 0, 4, "PgDn - Down one page",
  25. 0, 5, "Home - Top of listing",
  26. 0, 6, "End - End of listing",
  27. 0, 8, "W - Toggle word wrap",
  28. 0, 9, "^L - Refresh display",
  29. 0, 10, "Q, ESC - Quit",
  30. 0, 12, "/ - Search for string",
  31. 0, 13, "\\ - Search for string. Any case",
  32. 0, 14, "F4 - Toggle multifile search",
  33. 0, 15, "n, F3 - Next occurance of string",
  34. 0, 16, "N - Previous occurance of string",
  35. 0, 18, "C - Clear highlight line",
  36. 0, 19, "J - Jump to highlighted line",
  37. 0, 20, "M - Mark highlighed",
  38. 38, 1, "[list] - in tools.ini",
  39. 40, 2, "width - Width of crt",
  40. 40, 3, "height - Height of crt",
  41. 40, 4, "buffer - K to use for buffers (200K)",
  42. 40, 5, "tab - Tab alignment #",
  43. 40, 6, "tcolor - Color of title line",
  44. 40, 7, "lcolor - Color of listing",
  45. 40, 8, "hcolor - Color of highlighted",
  46. 40, 9, "bcolor - Color of scroll bar",
  47. 40, 10, "ccolor - Color of command line",
  48. 40, 11, "kcolor - Color of keyed input",
  49. 40, 12, "nobeep - Disables beeps",
  50. 40, 14, "^ Up - Pull copy buffer up",
  51. 40, 15, "^ Down - Pull copy buffer down",
  52. 40, 16, "^ Home - Slide copy buffer up",
  53. 40, 17, "^ End - Slide copy buffer down",
  54. 40, 18, "G - Goto Line number",
  55. 40, 20, "^ PgUp - Previous File",
  56. 40, 21, "^ PgDn - Next File",
  57. 40, 22, "F - New File",
  58. 0, 0, NULL
  59. } ;
  60. int Index;
  61. int hLines, hWidth;
  62. INPUT_RECORD InpBuffer;
  63. DWORD dwNumRead;
  64. //
  65. // Block reader thread.
  66. //
  67. SyncReader ();
  68. hLines = vLines;
  69. hWidth = vWidth;
  70. set_mode (25, 80, 0);
  71. ClearScr ();
  72. for (pHelp = rgHelp; pHelp->text; pHelp++)
  73. dis_str ((Uchar)(pHelp->x), (Uchar)(pHelp->y), pHelp->text);
  74. setattr (vLines+1, (char)vAttrList);
  75. setattr (vLines+2, (char)vAttrCmd);
  76. DisLn (0, (UCHAR)vLines+2, "Press enter.");
  77. for (; ;) {
  78. ReadConsoleInput( vStdIn,
  79. &InpBuffer,
  80. 1,
  81. &dwNumRead );
  82. if( IsValidKey( &InpBuffer ) &&
  83. ( ( InpBuffer.Event.KeyEvent.wVirtualKeyCode == 0x0d ) ||
  84. ( InpBuffer.Event.KeyEvent.wVirtualKeyCode == 0x1b ) ) ) {
  85. break;
  86. }
  87. }
  88. //
  89. // Free reader thread
  90. //
  91. for( Index = 0; Index < MAXLINES; Index++ ) {
  92. vrgLen[Index] = ( Uchar )vWidth-1;
  93. }
  94. set_mode (hLines+2, hWidth, 0);
  95. setattr (vLines+1, (char)vAttrCmd);
  96. setattr (vLines+2, (char)vAttrList);
  97. vReaderFlag = F_CHECK;
  98. SetEvent (vSemReader);
  99. }
  100. void
  101. GetInput (
  102. char *prompt,
  103. char *string,
  104. int len
  105. )
  106. {
  107. COORD dwCursorPosition;
  108. DWORD cb;
  109. SetUpdate (U_NONE);
  110. DisLn (0, (Uchar)(vLines+1), prompt);
  111. setattr2 (vLines+1, CMDPOS, len, (char)vAttrKey);
  112. if (!ReadFile( vStdIn, string, len, &cb, NULL ))
  113. return;
  114. if( (string[cb - 2] == 0x0d) || (string[cb - 2] == 0x0a) ) {
  115. string[cb - 2] = 0; // Get rid of CR LF
  116. }
  117. setattr2 (vLines+1, CMDPOS, len, (char)vAttrCmd);
  118. string[ cb - 1] = 0;
  119. if (string[0] < ' ')
  120. string[0] = 0;
  121. dwCursorPosition.X = CMDPOS;
  122. dwCursorPosition.Y = (SHORT)(vLines+1);
  123. SetConsoleCursorPosition( vhConsoleOutput, dwCursorPosition );
  124. }
  125. void
  126. beep (
  127. void
  128. )
  129. {
  130. if (vIniFlag & I_NOBEEP)
  131. return;
  132. }
  133. int
  134. _abort (
  135. void
  136. )
  137. {
  138. INPUT_RECORD InpBuffer;
  139. DWORD dwNumRead;
  140. static char WFlag = 0;
  141. if (! (vStatCode & S_WAIT) ) {
  142. DisLn ((Uchar)(vWidth-6), (Uchar)(vLines+1), "WAIT");
  143. vStatCode |= S_WAIT;
  144. }
  145. if( PeekConsoleInput( vStdIn, &InpBuffer, 1, &dwNumRead ) && dwNumRead ) {
  146. ReadConsoleInput( vStdIn, &InpBuffer, 1, &dwNumRead );
  147. if( IsValidKey( &InpBuffer ) ) {
  148. return( 1 );
  149. }
  150. }
  151. return( 0 );
  152. }
  153. void
  154. ClearScr ()
  155. {
  156. COORD dwCursorPosition;
  157. COORD dwWriteCoord;
  158. DWORD dwNumWritten;
  159. SMALL_RECT ScrollRectangle;
  160. SMALL_RECT ClipRectangle;
  161. COORD dwDestinationOrigin;
  162. CHAR_INFO Fill;
  163. setattr (0, (char)vAttrTitle);
  164. dwWriteCoord.X = 0;
  165. dwWriteCoord.Y = 1;
  166. FillConsoleOutputCharacter( vhConsoleOutput,
  167. ' ',
  168. vWidth*(vLines),
  169. dwWriteCoord,
  170. &dwNumWritten );
  171. FillConsoleOutputAttribute( vhConsoleOutput,
  172. vAttrList,
  173. vWidth*(vLines),
  174. dwWriteCoord,
  175. &dwNumWritten );
  176. ScrollRectangle.Left = (SHORT)(vWidth-1);
  177. ScrollRectangle.Top = 1;
  178. ScrollRectangle.Right = (SHORT)(vWidth-1);
  179. ScrollRectangle.Bottom = (SHORT)(vLines);
  180. ClipRectangle.Left = (SHORT)(vWidth-2);
  181. ClipRectangle.Top = 1;
  182. ClipRectangle.Right = (SHORT)(vWidth+1);
  183. ClipRectangle.Bottom = (SHORT)(vLines);
  184. dwDestinationOrigin.X = (SHORT)(vWidth-2);
  185. dwDestinationOrigin.Y = 1;
  186. Fill.Char.AsciiChar = ' ';
  187. Fill.Attributes = vAttrBar;
  188. ScrollConsoleScreenBuffer(
  189. vhConsoleOutput,
  190. &ScrollRectangle,
  191. &ClipRectangle,
  192. dwDestinationOrigin,
  193. &Fill );
  194. setattr (vLines+1, (char)vAttrCmd);
  195. dwCursorPosition.X = CMDPOS;
  196. dwCursorPosition.Y = (SHORT)(vLines+1);
  197. SetConsoleCursorPosition( vhConsoleOutput, dwCursorPosition );
  198. vHLBot = vHLTop = 0;
  199. }
  200. int
  201. set_mode (
  202. int nlines,
  203. int ncols,
  204. int mono
  205. )
  206. {
  207. WORD attrib;
  208. int i;
  209. CONSOLE_SCREEN_BUFFER_INFO Mode, Mode1;
  210. if (!GetConsoleScreenBufferInfo( vhConsoleOutput,
  211. &Mode )) {
  212. printf("Unable to get screen buffer info, code = %x \n", GetLastError());
  213. exit(-1);
  214. }
  215. Mode1 = Mode;
  216. if (nlines) {
  217. Mode.dwSize.Y = (SHORT)nlines;
  218. Mode.srWindow.Bottom = (SHORT)(Mode.srWindow.Top + nlines - 1);
  219. Mode.dwMaximumWindowSize.Y = (SHORT)nlines;
  220. }
  221. if (ncols) {
  222. Mode.dwSize.X = (SHORT)ncols;
  223. Mode.srWindow.Right = (SHORT)(Mode.srWindow.Left + ncols - 1);
  224. Mode.dwMaximumWindowSize.X = (SHORT)ncols;
  225. }
  226. if (mono) { // Toggle mono setting?
  227. attrib = vAttrTitle;
  228. vAttrTitle = vSaveAttrTitle;
  229. vSaveAttrTitle = attrib;
  230. attrib = vAttrList;
  231. vAttrList = vSaveAttrList;
  232. vSaveAttrList = attrib;
  233. attrib = vAttrHigh;
  234. vAttrHigh = vSaveAttrHigh;
  235. vSaveAttrHigh = attrib;
  236. attrib = vAttrKey;
  237. vAttrKey = vSaveAttrKey;
  238. vSaveAttrKey = attrib;
  239. attrib = vAttrCmd;
  240. vAttrCmd = vSaveAttrCmd;
  241. vSaveAttrCmd = attrib;
  242. attrib = vAttrBar;
  243. vAttrBar = vSaveAttrBar;
  244. vSaveAttrBar = attrib;
  245. }
  246. //
  247. // Try to set the hardware into the correct video mode
  248. //
  249. if ( !set_mode1 (&Mode, mono) ) {
  250. return( 0 );
  251. }
  252. vConsoleCurScrBufferInfo = Mode;
  253. vLines = Mode.dwSize.Y - 2; /* Not top or bottom line */
  254. vWidth = Mode.dwSize.X;
  255. if (vScrBuf) {
  256. free (vScrBuf);
  257. vScrBuf=NULL;
  258. }
  259. vSizeScrBuf = (vLines) * (vWidth);
  260. vScrBuf = malloc (vSizeScrBuf);
  261. vLines--;
  262. for (i=0; i < vLines; i++)
  263. vrgLen[i] = (Uchar)(vWidth-1);
  264. return (1);
  265. }
  266. int
  267. set_mode1 (
  268. PCONSOLE_SCREEN_BUFFER_INFO pMode,
  269. int mono
  270. )
  271. {
  272. mono = 0; // To get rid of warning message
  273. ClearScr();
  274. return( SetConsoleScreenBufferSize( vhConsoleOutput, pMode->dwSize ) );
  275. }
  276. struct Block *
  277. alloc_block(
  278. long offset
  279. )
  280. {
  281. struct Block *pt, **pt1;
  282. if (vpBCache) {
  283. pt1 = &vpBCache;
  284. for (; ;) { /* Scan cache */
  285. if ((*pt1)->offset == offset) {
  286. pt = *pt1; /* Found in cache */
  287. *pt1 = (*pt1)->next; /* remove from list */
  288. goto Alloc_Exit; /* Done. */
  289. }
  290. if ( (*pt1)->next == NULL) break;
  291. else pt1=&(*pt1)->next;
  292. }
  293. /* Warning: don't stomp on pt1! it's used at the end to
  294. * return a block from the cache if everything else is in use.
  295. */
  296. }
  297. /*
  298. * Was not in cache, so...
  299. * return block from free list, or...
  300. * allocate a new block, or...
  301. * return block from other list, or...
  302. * return from far end of cache list.
  303. * [works if cache list is in sorted order.. noramly is]
  304. */
  305. if (vpBFree) {
  306. pt = vpBFree;
  307. vpBFree = vpBFree->next;
  308. goto Alloc_Exit1;
  309. }
  310. if (vAllocBlks != vMaxBlks) {
  311. pt = (struct Block *) malloc (sizeof (struct Block));
  312. ckerr (pt == NULL, MEMERR);
  313. pt->Data = GlobalAlloc( 0, BLOCKSIZE );
  314. ckerr( !pt->Data, MEMERR );
  315. vAllocBlks++;
  316. goto Alloc_Exit1;
  317. }
  318. if (vpBOther) {
  319. pt = vpBOther;
  320. vpBOther = vpBOther->next;
  321. goto Alloc_Exit1;
  322. }
  323. /* Note: there should be a cache list to get here, if not */
  324. /* somebody called alloc_block, and everything was full */
  325. ckdebug (!vpBCache, "No cache");
  326. if (offset < vpBCache->offset) { /* Look for far end of cache */
  327. pt = *pt1; /* Return one from tail */
  328. *pt1 = NULL;
  329. goto Alloc_Exit1;
  330. } /* else, */
  331. pt = vpBCache; /* Return one from head */
  332. vpBCache = vpBCache->next;
  333. goto Alloc_Exit1;
  334. Alloc_Exit1:
  335. pt->offset = -1L;
  336. Alloc_Exit:
  337. pt->pFile = vpFlCur;
  338. vCntBlks++;
  339. return (pt);
  340. }
  341. void
  342. MoveBlk (
  343. struct Block **pBlk,
  344. struct Block **pHead
  345. )
  346. {
  347. struct Block *pt;
  348. pt = (*pBlk)->next;
  349. (*pBlk)->next = *pHead;
  350. *pHead = *pBlk;
  351. *pBlk = pt;
  352. }
  353. char *
  354. alloc_page()
  355. {
  356. char *pt;
  357. pt = (char *)malloc( 64*1024 );
  358. return (pt);
  359. }
  360. void
  361. FreePages(
  362. struct Flist *pFl
  363. )
  364. {
  365. int i;
  366. long * fpt;
  367. for (i=0; i < MAXTPAGE; i++) {
  368. fpt = pFl->prgLineTable [i];
  369. if (fpt == 0L) break;
  370. free ( fpt );
  371. pFl->prgLineTable[i] = NULL;
  372. }
  373. }
  374. void
  375. ListErr (
  376. char *file,
  377. int line,
  378. char *cond,
  379. int value,
  380. char *mess
  381. )
  382. {
  383. char s[80];
  384. char* c;
  385. printf ("ERROR in file %s, line %d, %s = %d, %s (%s)\n",
  386. file, line, cond, value, mess, GetErrorCode (value));
  387. c = fgets (s, sizeof(s), stdin);
  388. CleanUp();
  389. ExitProcess(0);
  390. }
  391. char *
  392. GetErrorCode(
  393. int code
  394. )
  395. {
  396. static struct {
  397. int errnum;
  398. char *desc;
  399. } EList[] = {
  400. 2, "File not found",
  401. 3, "Path not found",
  402. 4, "Too many open files",
  403. 5, "Access denied",
  404. 8, "Not enough memory",
  405. 15, "Invalid drive",
  406. 21, "Device not ready",
  407. 27, "Sector not found",
  408. 28, "Read fault",
  409. 32, "Sharing violation",
  410. 107, "Disk changed",
  411. 110, "File not found",
  412. 123, "Invalid name",
  413. 130, "Invalid for direct access handle",
  414. 131, "Seek on device",
  415. 206, "Filename exceeds range",
  416. -1, "Char DEV not supported",
  417. -2, "Pipe not supported",
  418. 0, NULL
  419. };
  420. static char s[15];
  421. int i;
  422. for (i=0; EList[i].errnum != code; i++)
  423. if (EList[i].errnum == 0) {
  424. sprintf (s, "Error %d", code);
  425. return (s);
  426. }
  427. return (EList[i].desc);
  428. }
  429. BOOL
  430. IsValidKey (
  431. PINPUT_RECORD pRecord
  432. )
  433. {
  434. if ( (pRecord->EventType != KEY_EVENT) ||
  435. !(pRecord->Event).KeyEvent.bKeyDown ||
  436. ((pRecord->Event).KeyEvent.wVirtualKeyCode == 0) || // ALT
  437. ((pRecord->Event).KeyEvent.wVirtualKeyCode == 0x10) || // SHIFT
  438. ((pRecord->Event).KeyEvent.wVirtualKeyCode == 0x11) || // CONTROL
  439. ((pRecord->Event).KeyEvent.wVirtualKeyCode == 0x14) ) { // CAPITAL
  440. return( FALSE );
  441. }
  442. return( TRUE );
  443. }