Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenconsole.c
Go to the documentation of this file.
00001 /* 00002 * CONSOLE.C - console input/output functions. 00003 * 00004 * 00005 * History: 00006 * 00007 * 20-Jan-1999 (Eric Kohl) 00008 * started 00009 * 00010 * 03-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>) 00011 * Remove all hardcode string to En.rc 00012 * 00013 * 01-Jul-2005 (Brandon Turner) <turnerb7@msu.edu>) 00014 * Added ConPrintfPaging and ConOutPrintfPaging 00015 * 00016 * 02-Feb-2007 (Paolo Devoti) <devotip at gmail.com>) 00017 * Fixed ConPrintfPaging 00018 */ 00019 00020 #include <precomp.h> 00021 00022 00023 #define OUTPUT_BUFFER_SIZE 4096 00024 00025 00026 UINT InputCodePage; 00027 UINT OutputCodePage; 00028 00029 00030 VOID ConInDisable (VOID) 00031 { 00032 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); 00033 DWORD dwMode; 00034 00035 GetConsoleMode (hInput, &dwMode); 00036 dwMode &= ~ENABLE_PROCESSED_INPUT; 00037 SetConsoleMode (hInput, dwMode); 00038 } 00039 00040 00041 VOID ConInEnable (VOID) 00042 { 00043 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); 00044 DWORD dwMode; 00045 00046 GetConsoleMode (hInput, &dwMode); 00047 dwMode |= ENABLE_PROCESSED_INPUT; 00048 SetConsoleMode (hInput, dwMode); 00049 } 00050 00051 00052 VOID ConInFlush (VOID) 00053 { 00054 FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE)); 00055 } 00056 00057 00058 VOID ConInKey (PINPUT_RECORD lpBuffer) 00059 { 00060 HANDLE hInput = GetStdHandle (STD_INPUT_HANDLE); 00061 DWORD dwRead; 00062 00063 if (hInput == INVALID_HANDLE_VALUE) 00064 WARN ("Invalid input handle!!!\n"); 00065 00066 do 00067 { 00068 ReadConsoleInput (hInput, lpBuffer, 1, &dwRead); 00069 if ((lpBuffer->EventType == KEY_EVENT) && 00070 (lpBuffer->Event.KeyEvent.bKeyDown == TRUE)) 00071 break; 00072 } 00073 while (TRUE); 00074 } 00075 00076 00077 VOID ConInString (LPTSTR lpInput, DWORD dwLength) 00078 { 00079 DWORD dwOldMode; 00080 DWORD dwRead = 0; 00081 HANDLE hFile; 00082 00083 LPTSTR p; 00084 PCHAR pBuf; 00085 00086 #ifdef _UNICODE 00087 pBuf = (PCHAR)cmd_alloc(dwLength - 1); 00088 #else 00089 pBuf = lpInput; 00090 #endif 00091 ZeroMemory (lpInput, dwLength * sizeof(TCHAR)); 00092 hFile = GetStdHandle (STD_INPUT_HANDLE); 00093 GetConsoleMode (hFile, &dwOldMode); 00094 00095 SetConsoleMode (hFile, ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); 00096 00097 ReadFile (hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL); 00098 00099 #ifdef _UNICODE 00100 MultiByteToWideChar(InputCodePage, 0, pBuf, dwRead, lpInput, dwLength - 1); 00101 cmd_free(pBuf); 00102 #endif 00103 for (p = lpInput; *p; p++) 00104 { 00105 if (*p == _T('\x0d')) 00106 { 00107 *p = _T('\0'); 00108 break; 00109 } 00110 } 00111 00112 SetConsoleMode (hFile, dwOldMode); 00113 } 00114 00115 static VOID ConWrite(TCHAR *str, DWORD len, DWORD nStdHandle) 00116 { 00117 DWORD dwWritten; 00118 HANDLE hOutput = GetStdHandle(nStdHandle); 00119 00120 if (WriteConsole(hOutput, str, len, &dwWritten, NULL)) 00121 return; 00122 00123 /* We're writing to a file or pipe instead of the console. Convert the 00124 * string from TCHARs to the desired output format, if the two differ */ 00125 if (bUnicodeOutput) 00126 { 00127 #ifndef _UNICODE 00128 WCHAR *buffer = cmd_alloc(len * sizeof(WCHAR)); 00129 if (!buffer) 00130 { 00131 error_out_of_memory(); 00132 return; 00133 } 00134 len = MultiByteToWideChar(OutputCodePage, 0, str, len, buffer, len); 00135 str = (PVOID)buffer; 00136 #endif 00137 WriteFile(hOutput, str, len * sizeof(WCHAR), &dwWritten, NULL); 00138 #ifndef _UNICODE 00139 cmd_free(buffer); 00140 #endif 00141 } 00142 else 00143 { 00144 #ifdef _UNICODE 00145 CHAR *buffer = cmd_alloc(len * MB_LEN_MAX * sizeof(CHAR)); 00146 if (!buffer) 00147 { 00148 error_out_of_memory(); 00149 return; 00150 } 00151 len = WideCharToMultiByte(OutputCodePage, 0, str, len, buffer, len * MB_LEN_MAX, NULL, NULL); 00152 str = (PVOID)buffer; 00153 #endif 00154 WriteFile(hOutput, str, len, &dwWritten, NULL); 00155 #ifdef _UNICODE 00156 cmd_free(buffer); 00157 #endif 00158 } 00159 } 00160 00161 VOID ConOutChar (TCHAR c) 00162 { 00163 ConWrite(&c, 1, STD_OUTPUT_HANDLE); 00164 } 00165 00166 VOID ConPuts(LPTSTR szText, DWORD nStdHandle) 00167 { 00168 ConWrite(szText, _tcslen(szText), nStdHandle); 00169 } 00170 00171 VOID ConOutResPaging(BOOL NewPage, UINT resID) 00172 { 00173 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00174 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE); 00175 ConOutPrintfPaging(NewPage, szMsg); 00176 } 00177 00178 VOID ConOutResPuts (UINT resID) 00179 { 00180 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00181 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE); 00182 00183 ConPuts(szMsg, STD_OUTPUT_HANDLE); 00184 } 00185 00186 VOID ConOutPuts (LPTSTR szText) 00187 { 00188 ConPuts(szText, STD_OUTPUT_HANDLE); 00189 } 00190 00191 00192 VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) 00193 { 00194 TCHAR szOut[OUTPUT_BUFFER_SIZE]; 00195 ConWrite(szOut, _vstprintf(szOut, szFormat, arg_ptr), nStdHandle); 00196 } 00197 00198 INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) 00199 { 00200 INT len; 00201 CONSOLE_SCREEN_BUFFER_INFO csbi; 00202 TCHAR szOut[OUTPUT_BUFFER_SIZE]; 00203 DWORD dwWritten; 00204 HANDLE hOutput = GetStdHandle(nStdHandle); 00205 00206 /* used to count number of lines since last pause */ 00207 static int LineCount = 0; 00208 00209 /* used to see how big the screen is */ 00210 int ScreenLines = 0; 00211 00212 /* chars since start of line */ 00213 int CharSL; 00214 00215 int from = 0, i = 0; 00216 00217 if(NewPage == TRUE) 00218 LineCount = 0; 00219 00220 /* rest LineCount and return if no string have been given */ 00221 if (szFormat == NULL) 00222 return 0; 00223 00224 00225 //get the size of the visual screen that can be printed too 00226 if (!GetConsoleScreenBufferInfo(hOutput, &csbi)) 00227 { 00228 // we assuming its a file handle 00229 ConPrintf(szFormat, arg_ptr, nStdHandle); 00230 return 0; 00231 } 00232 //subtract 2 to account for "press any key..." and for the blank line at the end of PagePrompt() 00233 ScreenLines = (csbi.srWindow.Bottom - csbi.srWindow.Top) - 4; 00234 CharSL = csbi.dwCursorPosition.X; 00235 00236 //make sure they didnt make the screen to small 00237 if(ScreenLines<4) 00238 { 00239 ConPrintf(szFormat, arg_ptr, nStdHandle); 00240 return 0; 00241 } 00242 00243 len = _vstprintf (szOut, szFormat, arg_ptr); 00244 00245 while (i < len) 00246 { 00247 // Search until the end of a line is reached 00248 if (szOut[i++] != _T('\n') && ++CharSL < csbi.dwSize.X) 00249 continue; 00250 00251 LineCount++; 00252 CharSL=0; 00253 00254 if(LineCount >= ScreenLines) 00255 { 00256 WriteConsole(hOutput, &szOut[from], i-from, &dwWritten, NULL); 00257 from = i; 00258 00259 if(PagePrompt() != PROMPT_YES) 00260 { 00261 return 1; 00262 } 00263 //reset the number of lines being printed 00264 LineCount = 0; 00265 } 00266 } 00267 00268 WriteConsole(hOutput, &szOut[from], i-from, &dwWritten, NULL); 00269 00270 return 0; 00271 } 00272 00273 VOID ConErrFormatMessage (DWORD MessageId, ...) 00274 { 00275 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00276 DWORD ret; 00277 LPTSTR text; 00278 va_list arg_ptr; 00279 00280 va_start (arg_ptr, MessageId); 00281 ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 00282 NULL, 00283 MessageId, 00284 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 00285 (LPTSTR) &text, 00286 0, 00287 &arg_ptr); 00288 00289 va_end (arg_ptr); 00290 if(ret > 0) 00291 { 00292 ConErrPuts (text); 00293 LocalFree(text); 00294 } 00295 else 00296 { 00297 LoadString(CMD_ModuleHandle, STRING_CONSOLE_ERROR, szMsg, RC_STRING_MAX_SIZE); 00298 ConErrPrintf(szMsg); 00299 } 00300 } 00301 00302 VOID ConOutFormatMessage (DWORD MessageId, ...) 00303 { 00304 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00305 DWORD ret; 00306 LPTSTR text; 00307 va_list arg_ptr; 00308 00309 va_start (arg_ptr, MessageId); 00310 ret = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 00311 NULL, 00312 MessageId, 00313 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 00314 (LPTSTR) &text, 00315 0, 00316 &arg_ptr); 00317 00318 va_end (arg_ptr); 00319 if(ret > 0) 00320 { 00321 ConErrPuts (text); 00322 LocalFree(text); 00323 } 00324 else 00325 { 00326 LoadString(CMD_ModuleHandle, STRING_CONSOLE_ERROR, szMsg, RC_STRING_MAX_SIZE); 00327 ConErrPrintf(szMsg); 00328 } 00329 } 00330 00331 VOID ConOutResPrintf (UINT resID, ...) 00332 { 00333 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00334 va_list arg_ptr; 00335 00336 va_start (arg_ptr, resID); 00337 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE); 00338 ConPrintf(szMsg, arg_ptr, STD_OUTPUT_HANDLE); 00339 va_end (arg_ptr); 00340 } 00341 00342 VOID ConOutPrintf (LPTSTR szFormat, ...) 00343 { 00344 va_list arg_ptr; 00345 00346 va_start (arg_ptr, szFormat); 00347 ConPrintf(szFormat, arg_ptr, STD_OUTPUT_HANDLE); 00348 va_end (arg_ptr); 00349 } 00350 00351 INT ConOutPrintfPaging (BOOL NewPage, LPTSTR szFormat, ...) 00352 { 00353 INT iReturn; 00354 va_list arg_ptr; 00355 00356 va_start (arg_ptr, szFormat); 00357 iReturn = ConPrintfPaging(NewPage, szFormat, arg_ptr, STD_OUTPUT_HANDLE); 00358 va_end (arg_ptr); 00359 return iReturn; 00360 } 00361 00362 VOID ConErrChar (TCHAR c) 00363 { 00364 ConWrite(&c, 1, STD_ERROR_HANDLE); 00365 } 00366 00367 00368 VOID ConErrResPuts (UINT resID) 00369 { 00370 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00371 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE); 00372 ConPuts(szMsg, STD_ERROR_HANDLE); 00373 } 00374 00375 VOID ConErrPuts (LPTSTR szText) 00376 { 00377 ConPuts(szText, STD_ERROR_HANDLE); 00378 } 00379 00380 00381 VOID ConErrResPrintf (UINT resID, ...) 00382 { 00383 TCHAR szMsg[RC_STRING_MAX_SIZE]; 00384 va_list arg_ptr; 00385 00386 va_start (arg_ptr, resID); 00387 LoadString(CMD_ModuleHandle, resID, szMsg, RC_STRING_MAX_SIZE); 00388 ConPrintf(szMsg, arg_ptr, STD_ERROR_HANDLE); 00389 va_end (arg_ptr); 00390 } 00391 00392 VOID ConErrPrintf (LPTSTR szFormat, ...) 00393 { 00394 va_list arg_ptr; 00395 00396 va_start (arg_ptr, szFormat); 00397 ConPrintf(szFormat, arg_ptr, STD_ERROR_HANDLE); 00398 va_end (arg_ptr); 00399 } 00400 00401 VOID SetCursorXY (SHORT x, SHORT y) 00402 { 00403 COORD coPos; 00404 00405 coPos.X = x; 00406 coPos.Y = y; 00407 SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coPos); 00408 } 00409 00410 00411 VOID GetCursorXY (PSHORT x, PSHORT y) 00412 { 00413 CONSOLE_SCREEN_BUFFER_INFO csbi; 00414 00415 GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi); 00416 00417 *x = csbi.dwCursorPosition.X; 00418 *y = csbi.dwCursorPosition.Y; 00419 } 00420 00421 00422 SHORT GetCursorX (VOID) 00423 { 00424 CONSOLE_SCREEN_BUFFER_INFO csbi; 00425 00426 GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi); 00427 00428 return csbi.dwCursorPosition.X; 00429 } 00430 00431 00432 SHORT GetCursorY (VOID) 00433 { 00434 CONSOLE_SCREEN_BUFFER_INFO csbi; 00435 00436 GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi); 00437 00438 return csbi.dwCursorPosition.Y; 00439 } 00440 00441 00442 VOID GetScreenSize (PSHORT maxx, PSHORT maxy) 00443 { 00444 CONSOLE_SCREEN_BUFFER_INFO csbi; 00445 00446 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) 00447 { 00448 csbi.dwSize.X = 80; 00449 csbi.dwSize.Y = 25; 00450 } 00451 00452 if (maxx) 00453 *maxx = csbi.dwSize.X; 00454 if (maxy) 00455 *maxy = csbi.dwSize.Y; 00456 } 00457 00458 00459 VOID SetCursorType (BOOL bInsert, BOOL bVisible) 00460 { 00461 CONSOLE_CURSOR_INFO cci; 00462 00463 cci.dwSize = bInsert ? 10 : 99; 00464 cci.bVisible = bVisible; 00465 00466 SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci); 00467 } 00468 00469 /* EOF */ Generated on Sat May 26 2012 04:15:50 for ReactOS by
1.7.6.1
|