Make cmd able to (sort of) work without a console.

svn path=/trunk/; revision=40289
This commit is contained in:
Jeffrey Morlan 2009-03-29 19:17:45 +00:00
parent c233cfcaf8
commit c1ff41171d
4 changed files with 50 additions and 17 deletions

View file

@ -164,7 +164,6 @@ DWORD dwChildProcessId = 0;
OSVERSIONINFO osvi; OSVERSIONINFO osvi;
HANDLE hIn; HANDLE hIn;
HANDLE hOut; HANDLE hOut;
HANDLE hConsole;
LPTSTR lpOriginalEnvironment; LPTSTR lpOriginalEnvironment;
HANDLE CMD_ModuleHandle; HANDLE CMD_ModuleHandle;
@ -1380,7 +1379,12 @@ ReadLine (TCHAR *commandline, BOOL bMore)
} }
} }
ReadCommand (readline, CMDLINE_LENGTH - 1); if (!ReadCommand(readline, CMDLINE_LENGTH - 1))
{
bExit = TRUE;
return FALSE;
}
if (CheckCtrlBreak(BREAK_INPUT)) if (CheckCtrlBreak(BREAK_INPUT))
{ {
ConOutPuts(_T("\n")); ConOutPuts(_T("\n"));
@ -1805,6 +1809,7 @@ static VOID Cleanup()
*/ */
int cmd_main (int argc, const TCHAR *argv[]) int cmd_main (int argc, const TCHAR *argv[])
{ {
HANDLE hConsole;
TCHAR startPath[MAX_PATH]; TCHAR startPath[MAX_PATH];
CONSOLE_SCREEN_BUFFER_INFO Info; CONSOLE_SCREEN_BUFFER_INFO Info;
INT nExitCode; INT nExitCode;
@ -1821,12 +1826,16 @@ int cmd_main (int argc, const TCHAR *argv[])
hConsole = CreateFile(_T("CONOUT$"), GENERIC_READ|GENERIC_WRITE, hConsole = CreateFile(_T("CONOUT$"), GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL); OPEN_EXISTING, 0, NULL);
if (GetConsoleScreenBufferInfo(hConsole, &Info) == FALSE) if (hConsole != INVALID_HANDLE_VALUE)
{ {
ConErrFormatMessage(GetLastError()); if (!GetConsoleScreenBufferInfo(hConsole, &Info))
return(1); {
ConErrFormatMessage(GetLastError());
return(1);
}
wDefColor = Info.wAttributes;
CloseHandle(hConsole);
} }
wDefColor = Info.wAttributes;
InputCodePage= GetConsoleCP(); InputCodePage= GetConsoleCP();
OutputCodePage = GetConsoleOutputCP(); OutputCodePage = GetConsoleOutputCP();

View file

@ -53,7 +53,6 @@
/* global variables */ /* global variables */
extern HANDLE hOut; extern HANDLE hOut;
extern HANDLE hIn; extern HANDLE hIn;
extern HANDLE hConsole;
extern LPTSTR lpOriginalEnvironment; extern LPTSTR lpOriginalEnvironment;
extern WORD wColor; extern WORD wColor;
extern WORD wDefColor; extern WORD wDefColor;
@ -120,7 +119,7 @@ extern HANDLE CMD_ModuleHandle;
/* Prototypes for CMDINPUT.C */ /* Prototypes for CMDINPUT.C */
VOID ReadCommand (LPTSTR, INT); BOOL ReadCommand (LPTSTR, INT);
/* Prototypes for CMDTABLE.C */ /* Prototypes for CMDTABLE.C */

View file

@ -126,8 +126,9 @@ ClearCommandLine (LPTSTR str, INT maxlen, SHORT orgx, SHORT orgy)
/* read in a command line */ /* read in a command line */
VOID ReadCommand (LPTSTR str, INT maxlen) BOOL ReadCommand (LPTSTR str, INT maxlen)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi;
SHORT orgx; /* origin x/y */ SHORT orgx; /* origin x/y */
SHORT orgy; SHORT orgy;
SHORT curx; /*current x/y cursor position*/ SHORT curx; /*current x/y cursor position*/
@ -149,11 +150,30 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
TCHAR PreviousChar; TCHAR PreviousChar;
#endif #endif
/* get screen size */ if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
GetScreenSize (&maxx, &maxy); {
/* No console */
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD dwRead;
CHAR chr;
do
{
if (!ReadFile(hStdin, &chr, 1, &dwRead, NULL) || !dwRead)
return FALSE;
#ifdef _UNICODE
MultiByteToWideChar(InputCodePage, 0, &chr, 1, &str[charcount++], 1);
#endif
} while (chr != '\n' && charcount < maxlen);
str[charcount] = _T('\0');
return TRUE;
}
GetCursorXY (&orgx, &orgy); /* get screen size */
GetCursorXY (&curx, &cury); maxx = csbi.dwSize.X;
maxy = csbi.dwSize.Y;
curx = orgx = csbi.dwCursorPosition.X;
cury = orgy = csbi.dwCursorPosition.Y;
memset (str, 0, maxlen * sizeof (TCHAR)); memset (str, 0, maxlen * sizeof (TCHAR));
@ -590,4 +610,5 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
/* expand all aliases */ /* expand all aliases */
ExpandAlias (str, maxlen); ExpandAlias (str, maxlen);
#endif /* FEATURE_ALIAS */ #endif /* FEATURE_ALIAS */
return TRUE;
} }

View file

@ -413,7 +413,7 @@ VOID GetCursorXY (PSHORT x, PSHORT y)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hConsole, &csbi); GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
*x = csbi.dwCursorPosition.X; *x = csbi.dwCursorPosition.X;
*y = csbi.dwCursorPosition.Y; *y = csbi.dwCursorPosition.Y;
@ -424,7 +424,7 @@ SHORT GetCursorX (VOID)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hConsole, &csbi); GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.X; return csbi.dwCursorPosition.X;
} }
@ -434,7 +434,7 @@ SHORT GetCursorY (VOID)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hConsole, &csbi); GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
return csbi.dwCursorPosition.Y; return csbi.dwCursorPosition.Y;
} }
@ -444,7 +444,11 @@ VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
{ {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hConsole, &csbi); if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
{
csbi.dwSize.X = 80;
csbi.dwSize.Y = 25;
}
if (maxx) if (maxx)
*maxx = csbi.dwSize.X; *maxx = csbi.dwSize.X;