Make cmd able to (sort of) work without a console.
authorJeffrey Morlan <mrnobo1024@yahoo.com>
Sun, 29 Mar 2009 19:17:45 +0000 (19:17 +0000)
committerJeffrey Morlan <mrnobo1024@yahoo.com>
Sun, 29 Mar 2009 19:17:45 +0000 (19:17 +0000)
svn path=/trunk/; revision=40289

reactos/base/shell/cmd/cmd.c
reactos/base/shell/cmd/cmd.h
reactos/base/shell/cmd/cmdinput.c
reactos/base/shell/cmd/console.c

index 9859156..78a1ed0 100644 (file)
@@ -164,7 +164,6 @@ DWORD dwChildProcessId = 0;
 OSVERSIONINFO osvi;
 HANDLE hIn;
 HANDLE hOut;
-HANDLE hConsole;
 LPTSTR lpOriginalEnvironment;
 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))
                {
                        ConOutPuts(_T("\n"));
@@ -1805,6 +1809,7 @@ static VOID Cleanup()
  */
 int cmd_main (int argc, const TCHAR *argv[])
 {
+       HANDLE hConsole;
        TCHAR startPath[MAX_PATH];
        CONSOLE_SCREEN_BUFFER_INFO Info;
        INT nExitCode;
@@ -1821,12 +1826,16 @@ int cmd_main (int argc, const TCHAR *argv[])
        hConsole = CreateFile(_T("CONOUT$"), GENERIC_READ|GENERIC_WRITE,
                FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
                OPEN_EXISTING, 0, NULL);
-       if (GetConsoleScreenBufferInfo(hConsole, &Info) == FALSE)
+       if (hConsole != INVALID_HANDLE_VALUE)
        {
-               ConErrFormatMessage(GetLastError());
-               return(1);
+               if (!GetConsoleScreenBufferInfo(hConsole, &Info))
+               {
+                       ConErrFormatMessage(GetLastError());
+                       return(1);
+               }
+               wDefColor = Info.wAttributes;
+               CloseHandle(hConsole);
        }
-       wDefColor = Info.wAttributes;
 
        InputCodePage= GetConsoleCP();
        OutputCodePage = GetConsoleOutputCP();
index 26ebd04..206f9db 100644 (file)
@@ -53,7 +53,6 @@
 /* global variables */
 extern HANDLE hOut;
 extern HANDLE hIn;
-extern HANDLE hConsole;
 extern LPTSTR lpOriginalEnvironment;
 extern WORD   wColor;
 extern WORD   wDefColor;
@@ -120,7 +119,7 @@ extern HANDLE CMD_ModuleHandle;
 
 
 /* Prototypes for CMDINPUT.C */
-VOID ReadCommand (LPTSTR, INT);
+BOOL ReadCommand (LPTSTR, INT);
 
 
 /* Prototypes for CMDTABLE.C */
index fd3560a..0937872 100644 (file)
@@ -126,8 +126,9 @@ ClearCommandLine (LPTSTR str, INT maxlen, SHORT orgx, SHORT orgy)
 
 
 /* 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 orgy;
        SHORT curx;                     /*current x/y cursor position*/
@@ -149,11 +150,30 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
        TCHAR PreviousChar;
 #endif
 
+       if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
+       {
+               /* 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;
+       }
+
        /* get screen size */
-       GetScreenSize (&maxx, &maxy);
+       maxx = csbi.dwSize.X;
+       maxy = csbi.dwSize.Y;
 
-       GetCursorXY (&orgx, &orgy);
-       GetCursorXY (&curx, &cury);
+       curx = orgx = csbi.dwCursorPosition.X;
+       cury = orgy = csbi.dwCursorPosition.Y;
 
        memset (str, 0, maxlen * sizeof (TCHAR));
 
@@ -590,4 +610,5 @@ VOID ReadCommand (LPTSTR str, INT maxlen)
        /* expand all aliases */
        ExpandAlias (str, maxlen);
 #endif /* FEATURE_ALIAS */
+       return TRUE;
 }
index bcaf388..21c9aa6 100644 (file)
@@ -413,7 +413,7 @@ VOID GetCursorXY (PSHORT x, PSHORT y)
 {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
 
-       GetConsoleScreenBufferInfo (hConsole, &csbi);
+       GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
 
        *x = csbi.dwCursorPosition.X;
        *y = csbi.dwCursorPosition.Y;
@@ -424,7 +424,7 @@ SHORT GetCursorX (VOID)
 {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
 
-       GetConsoleScreenBufferInfo (hConsole, &csbi);
+       GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
 
        return csbi.dwCursorPosition.X;
 }
@@ -434,7 +434,7 @@ SHORT GetCursorY (VOID)
 {
        CONSOLE_SCREEN_BUFFER_INFO csbi;
 
-       GetConsoleScreenBufferInfo (hConsole, &csbi);
+       GetConsoleScreenBufferInfo (GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
 
        return csbi.dwCursorPosition.Y;
 }
@@ -444,7 +444,11 @@ VOID GetScreenSize (PSHORT maxx, PSHORT maxy)
 {
        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)
                *maxx = csbi.dwSize.X;