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.

340 lines
7.8 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. int rc = SUCCESS;
  104. tas = n->argptr;
  105. //
  106. // Find first non-blank argument.
  107. //
  108. while (tas && *tas && *tas <= SPACE)
  109. tas += 1;
  110. //
  111. // If first argument is /B, then remember that in cmdnode
  112. //
  113. if (tas != NULL && !_tcsnicmp(tas, TEXT("/B"), 2)) {
  114. n->type = GOTYP;
  115. n->argptr = GotoEofStr;
  116. //
  117. // Skip over /B and trailing spaces
  118. //
  119. tas += 2;
  120. while (tas && *tas && *tas <= SPACE)
  121. tas += 1;
  122. }
  123. //
  124. // See numeric argument given. If so, set LastRetCode
  125. // with it.
  126. //
  127. if (tas && _stscanf( tas, TEXT("%d"), &exitCode ) == 1) {
  128. LastRetCode = exitCode;
  129. if (SingleCommandInvocation) {
  130. rc = LastRetCode;
  131. }
  132. }
  133. if (n->type == GOTYP && CurrentBatchFile != NULL) {
  134. eGoto( n );
  135. } else {
  136. ResetConTitle(pszTitleOrg);
  137. CMDexit(LastRetCode);
  138. }
  139. return(rc) ;
  140. }
  141. /*** eVerify - execute the Verify command
  142. *
  143. * Purpose:
  144. * To set the verify mode or display the current verify mode.
  145. *
  146. * int eVerify(struct cmdnode *n)
  147. *
  148. * Args:
  149. * n - the parse tree node containing the verify command
  150. *
  151. * Returns:
  152. * SUCCESS if a valid argument was given.
  153. * FAILURE if an invalid argument was given.
  154. *
  155. */
  156. int
  157. eVerify(
  158. struct cmdnode *n
  159. )
  160. {
  161. return( LastRetCode = VerifyWork(n) );
  162. }
  163. int
  164. VerifyWork(
  165. struct cmdnode *n
  166. )
  167. {
  168. int oocret ; /* The return code from OnOffCheck() */
  169. DEBUG((OCGRP, VELVL, "eVERIFY: Entered.")) ;
  170. switch (oocret = OnOffCheck(n->argptr, OOC_ERROR)) {
  171. case OOC_EMPTY:
  172. /* M005 */ PutStdOut(((GetSetVerMode(GSVM_GET)) ? MSG_VERIFY_ON : MSG_VERIFY_OFF), NOARGS);
  173. break ;
  174. case OOC_OTHER:
  175. return(FAILURE) ;
  176. default:
  177. GetSetVerMode((BYTE)oocret) ;
  178. } ;
  179. return(SUCCESS) ;
  180. }
  181. BOOLEAN Verify=FALSE;
  182. /*** GetSetVerMode - change the verify mode
  183. *
  184. * Purpose:
  185. * Get old verify mode and, optionally, set verify mode as specified.
  186. *
  187. * TCHAR GetSetVerMode(TCHAR newmode)
  188. *
  189. * Args:
  190. * newmode - the new verify mode or GSVM_GET if mode isn't to be changed
  191. *
  192. * Returns:
  193. * The old verify mode.
  194. *
  195. */
  196. BOOLEAN
  197. GetSetVerMode(
  198. BYTE newmode
  199. )
  200. {
  201. if (newmode != GSVM_GET) {
  202. Verify = (BOOLEAN)(newmode == GSVM_ON ? TRUE : FALSE);
  203. }
  204. return Verify;
  205. }
  206. // execute the COLOR internal command....
  207. int
  208. eColor(
  209. struct cmdnode *n
  210. )
  211. {
  212. WORD wColor = 0;
  213. int ocRet, digit;
  214. TCHAR* arg;
  215. ocRet = OnOffCheck( n->argptr, OOC_NOERROR );
  216. switch( ocRet )
  217. {
  218. case OOC_EMPTY:
  219. wColor = wDefaultColor; // reset to default
  220. break;
  221. case OOC_OTHER:
  222. arg = n->argptr;
  223. arg = SkipWhiteSpace( arg );
  224. for( ; *arg && _istxdigit(*arg) ; ++arg) {
  225. digit = (int) (*arg <= TEXT('9'))
  226. ? (int)*arg - (int)'0'
  227. : (int)_totlower(*arg)-(int)'W' ;
  228. wColor = (wColor << 4)+digit ;
  229. }
  230. arg = SkipWhiteSpace( arg );
  231. // make sure nothing left and value between 0 and 0xff...
  232. if ( !(*arg) && (wColor < 0x100) )
  233. break;
  234. // else fall through to display help....
  235. default:
  236. // display help string....
  237. BeginHelpPause();
  238. PutStdOut( MSG_HELP_COLOR, NOARGS );
  239. EndHelpPause();
  240. return SUCCESS;
  241. }
  242. return LastRetCode = SetColor(wColor);
  243. }
  244. // set the console to a given color -- if a console....
  245. int
  246. SetColor(
  247. WORD attr
  248. )
  249. {
  250. CONSOLE_SCREEN_BUFFER_INFO csbi;
  251. HANDLE hStdOut;
  252. COORD coord;
  253. DWORD dwWritten;
  254. //
  255. // Fail if foreground and background color the same.
  256. //
  257. if ((attr & 0xF) == ((attr >> 4) & 0xF)) {
  258. return FAILURE;
  259. }
  260. // get the handle....
  261. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  262. // get the console info....
  263. if (GetConsoleScreenBufferInfo( hStdOut, &csbi)) {
  264. // fill the screen with the color attribute....
  265. coord.Y = 0;
  266. coord.X = 0;
  267. FillConsoleOutputAttribute( hStdOut, attr, csbi.dwSize.Y*csbi.dwSize.X, coord, &dwWritten );
  268. // make sure the color sticks....
  269. SetConsoleTextAttribute( hStdOut, attr );
  270. return SUCCESS;
  271. }
  272. return FAILURE;
  273. }