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.

336 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1988-1999 Microsoft Corporation
  3. Module Name:
  4. cother.c
  5. Abstract:
  6. Miscellaneous commands
  7. --*/
  8. #include "cmd.h"
  9. extern TCHAR Fmt19[] ; /* M006 */
  10. extern PTCHAR pszTitleOrg;
  11. extern WORD wDefaultColor;
  12. extern TCHAR GotoStr[] ;
  13. extern TCHAR GotoEofStr[] ;
  14. extern int LastRetCode ;
  15. /*** eCls - execute the Cls command
  16. *
  17. * Purpose:
  18. * Output to STDOUT, the ANSI escape sequences used to clear a screen.
  19. *
  20. * int eCls(struct cmdnode *n)
  21. *
  22. * Args:
  23. * n - the parse tree node containing the cls command
  24. *
  25. * Returns:
  26. * SUCCESS always.
  27. *
  28. * Notes:
  29. * M001 - Replaced old ANSI sequence with VIO interface.
  30. * M006 - To insure we get correct background color, we print a space
  31. * and then read the cell just printed using it to clear with.
  32. */
  33. int
  34. eCls(
  35. struct cmdnode *n
  36. )
  37. {
  38. CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenInfo;
  39. HANDLE handle;
  40. #ifdef WIN95_CMD
  41. DWORD dwWritten;
  42. DWORD nCells;
  43. #else
  44. COORD ScrollTarget;
  45. CHAR_INFO chinfo;
  46. SMALL_RECT ScrollRect;
  47. #endif
  48. UNREFERENCED_PARAMETER( n );
  49. //
  50. // for compatibility with DOS errorlevels, don't set LastRetCode for cls
  51. //
  52. if (!FileIsDevice(STDOUT)) {
  53. cmd_printf( TEXT("\014") ); // ^L
  54. return(SUCCESS) ;
  55. }
  56. handle = GetStdHandle(STD_OUTPUT_HANDLE);
  57. if (!GetConsoleScreenBufferInfo( handle, &ConsoleScreenInfo)) {
  58. cmd_printf( TEXT("\014") ); // ^L
  59. return(SUCCESS) ;
  60. }
  61. #ifndef WIN95_CMD
  62. ScrollTarget.Y = (SHORT)(0 - ConsoleScreenInfo.dwSize.Y);
  63. ScrollTarget.X = 0;
  64. ScrollRect.Top = 0;
  65. ScrollRect.Left = 0;
  66. ScrollRect.Bottom = ConsoleScreenInfo.dwSize.Y;
  67. ScrollRect.Right = ConsoleScreenInfo.dwSize.X;
  68. chinfo.Char.UnicodeChar = TEXT(' ');
  69. chinfo.Attributes = ConsoleScreenInfo.wAttributes;
  70. ScrollConsoleScreenBuffer(handle, &ScrollRect, NULL, ScrollTarget, &chinfo);
  71. ConsoleScreenInfo.dwCursorPosition.X = 0;
  72. ConsoleScreenInfo.dwCursorPosition.Y = 0;
  73. #else
  74. ConsoleScreenInfo.dwCursorPosition.X = 0;
  75. ConsoleScreenInfo.dwCursorPosition.Y = 0;
  76. nCells = ConsoleScreenInfo.dwSize.Y*ConsoleScreenInfo.dwSize.X;
  77. FillConsoleOutputCharacterA( handle, ' ', nCells, ConsoleScreenInfo.dwCursorPosition, &dwWritten);
  78. FillConsoleOutputAttribute( handle, ConsoleScreenInfo.wAttributes, nCells, ConsoleScreenInfo.dwCursorPosition, &dwWritten );
  79. #endif
  80. SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), ConsoleScreenInfo.dwCursorPosition );
  81. return(SUCCESS) ;
  82. }
  83. extern unsigned DosErr ;
  84. /*** eExit - execute the Exit command
  85. *
  86. * Purpose:
  87. * Set the LastRetCode to SUCCESS because this command can never fail.
  88. * Then call SigHand() and let it decide whether or not to exit.
  89. *
  90. * eExit(struct cmdnode *n)
  91. *
  92. * Args:
  93. * n - the parse tree node containing the exit command
  94. *
  95. */
  96. int
  97. eExit(
  98. struct cmdnode *n
  99. )
  100. {
  101. TCHAR *tas;
  102. LONG exitCode;
  103. tas = n->argptr;
  104. //
  105. // Find first non-blank argument.
  106. //
  107. while (tas && *tas && *tas <= SPACE)
  108. tas += 1;
  109. //
  110. // If first argument is /B, then remember that in cmdnode
  111. //
  112. if (tas != NULL && !_tcsnicmp(tas, TEXT("/B"), 2)) {
  113. n->type = GOTYP;
  114. n->argptr = GotoEofStr;
  115. //
  116. // Skip over /B and trailing spaces
  117. //
  118. tas += 2;
  119. while (tas && *tas && *tas <= SPACE)
  120. tas += 1;
  121. }
  122. //
  123. // See numeric argument given. If so, set LastRetCode
  124. // with it.
  125. //
  126. if (tas && _stscanf( tas, TEXT("%d"), &exitCode ) == 1) {
  127. LastRetCode = exitCode;
  128. }
  129. if (n->type == GOTYP && CurrentBatchFile != NULL)
  130. return eGoto(n);
  131. else {
  132. ResetConTitle(pszTitleOrg);
  133. CMDexit(LastRetCode);
  134. }
  135. return(SUCCESS) ;
  136. }
  137. /*** eVerify - execute the Verify command
  138. *
  139. * Purpose:
  140. * To set the verify mode or display the current verify mode.
  141. *
  142. * int eVerify(struct cmdnode *n)
  143. *
  144. * Args:
  145. * n - the parse tree node containing the verify command
  146. *
  147. * Returns:
  148. * SUCCESS if a valid argument was given.
  149. * FAILURE if an invalid argument was given.
  150. *
  151. */
  152. int
  153. eVerify(
  154. struct cmdnode *n
  155. )
  156. {
  157. return( LastRetCode = VerifyWork(n) );
  158. }
  159. int
  160. VerifyWork(
  161. struct cmdnode *n
  162. )
  163. {
  164. int oocret ; /* The return code from OnOffCheck() */
  165. DEBUG((OCGRP, VELVL, "eVERIFY: Entered.")) ;
  166. switch (oocret = OnOffCheck(n->argptr, OOC_ERROR)) {
  167. case OOC_EMPTY:
  168. /* M005 */ PutStdOut(((GetSetVerMode(GSVM_GET)) ? MSG_VERIFY_ON : MSG_VERIFY_OFF), NOARGS);
  169. break ;
  170. case OOC_OTHER:
  171. return(FAILURE) ;
  172. default:
  173. GetSetVerMode((BYTE)oocret) ;
  174. } ;
  175. return(SUCCESS) ;
  176. }
  177. BOOLEAN Verify=FALSE;
  178. /*** GetSetVerMode - change the verify mode
  179. *
  180. * Purpose:
  181. * Get old verify mode and, optionally, set verify mode as specified.
  182. *
  183. * TCHAR GetSetVerMode(TCHAR newmode)
  184. *
  185. * Args:
  186. * newmode - the new verify mode or GSVM_GET if mode isn't to be changed
  187. *
  188. * Returns:
  189. * The old verify mode.
  190. *
  191. */
  192. BOOLEAN
  193. GetSetVerMode(
  194. BYTE newmode
  195. )
  196. {
  197. if (newmode != GSVM_GET) {
  198. Verify = (BOOLEAN)(newmode == GSVM_ON ? TRUE : FALSE);
  199. }
  200. return Verify;
  201. }
  202. // execute the COLOR internal command....
  203. int
  204. eColor(
  205. struct cmdnode *n
  206. )
  207. {
  208. WORD wColor = 0;
  209. int ocRet, digit;
  210. TCHAR* arg;
  211. ocRet = OnOffCheck( n->argptr, OOC_NOERROR );
  212. switch( ocRet )
  213. {
  214. case OOC_EMPTY:
  215. wColor = wDefaultColor; // reset to default
  216. break;
  217. case OOC_OTHER:
  218. arg = n->argptr;
  219. arg = SkipWhiteSpace( arg );
  220. for( ; *arg && _istxdigit(*arg) ; ++arg) {
  221. digit = (int) (*arg <= TEXT('9'))
  222. ? (int)*arg - (int)'0'
  223. : (int)_totlower(*arg)-(int)'W' ;
  224. wColor = (wColor << 4)+digit ;
  225. }
  226. arg = SkipWhiteSpace( arg );
  227. // make sure nothing left and value between 0 and 0xff...
  228. if ( !(*arg) && (wColor < 0x100) )
  229. break;
  230. // else fall through to display help....
  231. default:
  232. // display help string....
  233. BeginHelpPause();
  234. PutStdOut( MSG_HELP_COLOR, NOARGS );
  235. EndHelpPause();
  236. return SUCCESS;
  237. }
  238. return LastRetCode = SetColor(wColor);
  239. }
  240. // set the console to a given color -- if a console....
  241. int
  242. SetColor(
  243. WORD attr
  244. )
  245. {
  246. CONSOLE_SCREEN_BUFFER_INFO csbi;
  247. HANDLE hStdOut;
  248. COORD coord;
  249. DWORD dwWritten;
  250. //
  251. // Fail if foreground and background color the same.
  252. //
  253. if ((attr & 0xF) == ((attr >> 4) & 0xF)) {
  254. return FAILURE;
  255. }
  256. // get the handle....
  257. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  258. // get the console info....
  259. if (GetConsoleScreenBufferInfo( hStdOut, &csbi)) {
  260. // fill the screen with the color attribute....
  261. coord.Y = 0;
  262. coord.X = 0;
  263. FillConsoleOutputAttribute( hStdOut, attr, csbi.dwSize.Y*csbi.dwSize.X, coord, &dwWritten );
  264. // make sure the color sticks....
  265. SetConsoleTextAttribute( hStdOut, attr );
  266. return SUCCESS;
  267. }
  268. return FAILURE;
  269. }