* Move the KdbEntryCount (check for exception inside KDB) into the critical section to prevent KDB from not handling simultaneous exceptions correctly.
* Flush input when KDB is entered.
* Detect if terminal is connected and filter out escape sequences if no terminal is present.
Modified: trunk/reactos/ntoskrnl/dbg/kdb.c
Modified: trunk/reactos/ntoskrnl/dbg/kdb_cli.c

Modified: trunk/reactos/ntoskrnl/dbg/kdb.c
--- trunk/reactos/ntoskrnl/dbg/kdb.c	2005-03-31 14:23:11 UTC (rev 14388)
+++ trunk/reactos/ntoskrnl/dbg/kdb.c	2005-03-31 16:08:09 UTC (rev 14389)
@@ -1141,12 +1141,6 @@
    BOOLEAN EnterConditionMet = TRUE;
    ULONG OldEflags;
 
-   /* Exception inside the debugger? Game over. */
-   if (InterlockedIncrement(&KdbEntryCount) > 1)
-   {
-      return kdHandleException;
-   }
-
    KdbCurrentProcess = PsGetCurrentProcess();
 
    /* Set continue type to kdContinue for single steps and breakpoints */
@@ -1342,7 +1336,6 @@
       {
          if (!EnterConditionMet)
          {
-            InterlockedDecrement(&KdbEntryCount);
             return ContinueType;
          }
          DbgPrint("Entered debugger on unexpected debug trap!\n");
@@ -1357,7 +1350,6 @@
       }
       if (!EnterConditionMet)
       {
-         InterlockedDecrement(&KdbEntryCount);
          return ContinueType;
       }
 
@@ -1372,7 +1364,6 @@
 
       if (!EnterConditionMet)
       {
-         InterlockedDecrement(&KdbEntryCount);
          return ContinueType;
       }
 
@@ -1419,6 +1410,12 @@
    Ke386SaveFlags(OldEflags);
    Ke386DisableInterrupts();
 
+   /* Exception inside the debugger? Game over. */
+   if (InterlockedIncrement(&KdbEntryCount) > 1)
+   {
+      return kdHandleException;
+   }
+
    /* Call the main loop. */
    KdbpInternalEnter();
 
@@ -1461,6 +1458,9 @@
           "r"(KdbTrapFrame.Cr3), "r"(KdbTrapFrame.Cr4));
 #endif
 
+   /* Decrement the entry count */
+   InterlockedDecrement(&KdbEntryCount);
+
    /* Leave critical section */
    Ke386RestoreFlags(OldEflags);
 
@@ -1479,7 +1479,6 @@
 
    }
 
-   InterlockedDecrement(&KdbEntryCount);
    return ContinueType;
 }
 

Modified: trunk/reactos/ntoskrnl/dbg/kdb_cli.c
--- trunk/reactos/ntoskrnl/dbg/kdb_cli.c	2005-03-31 14:23:11 UTC (rev 14388)
+++ trunk/reactos/ntoskrnl/dbg/kdb_cli.c	2005-03-31 16:08:09 UTC (rev 14389)
@@ -1678,11 +1678,12 @@
 {
    STATIC CHAR Buffer[4096];
    STATIC BOOLEAN TerminalInitialized = FALSE;
+   STATIC BOOLEAN TerminalConnected = FALSE;
    STATIC BOOLEAN TerminalReportsSize = TRUE;
    CHAR c = '\0';
-   PCHAR p;
+   PCHAR p, p2;
    INT Length;
-   INT i;
+   INT i, j;
    INT RowsPrintedByTerminal;
    ULONG ScanCode;
    va_list ap;
@@ -1695,7 +1696,25 @@
    if (!TerminalInitialized)
    {
       DbgPrint("\x1b[7h");      /* Enable linewrap */
+      
+      /* Query terminal type */
+      /*DbgPrint("\x1b[Z");*/
+      DbgPrint("\x05");
+      
       TerminalInitialized = TRUE;
+      Length = 0;
+      for (;;)
+      {
+         c = KdbpTryGetCharSerial(5000);
+         if (c == -1)
+            break;
+         Buffer[Length++] = c;
+         if (Length >= (sizeof (Buffer) - 1))
+            break;
+      }
+      Buffer[Length] = '\0';
+      if (Length > 0)
+         TerminalConnected = TRUE;
    }
 
    /* Get number of rows and columns in terminal */
@@ -1706,17 +1725,17 @@
       {
          /* Try to query number of rows from terminal. A reply looks like "\x1b[8;24;80t" */
          TerminalReportsSize = FALSE;
-         //DbgPrint("\x1b[18t");
-         c = KdbpTryGetCharSerial(10);
+         DbgPrint("\x1b[18t");
+         c = KdbpTryGetCharSerial(5000);
          if (c == KEY_ESC)
          {
-            c = KdbpTryGetCharSerial(5);
+            c = KdbpTryGetCharSerial(5000);
             if (c == '[')
             {
                Length = 0;
                for (;;)
                {
-                  c = KdbpTryGetCharSerial(5);
+                  c = KdbpTryGetCharSerial(5000);
                   if (c == -1)
                      break;
                   Buffer[Length++] = c;
@@ -1815,6 +1834,20 @@
       {
          c = '\0';
       }
+      
+      /* Remove escape sequences from the line if there's no terminal connected */
+      if (!TerminalConnected)
+      {
+         while ((p2 = strrchr(p, '\x1b')) != NULL) /* Look for escape character */
+         {
+            if (p2[1] == '[')
+            {
+               j = 2;
+               while (!isalpha(p2[j++]));
+               strcpy(p2, p2 + j);
+            }
+         }
+      }
 
       DbgPrint("%s", p);
 
@@ -1920,12 +1953,12 @@
    OUT PCHAR Buffer,
    IN  ULONG Size)
 {
-   CHAR Key, NextKey;
+   CHAR Key;
    PCHAR Orig = Buffer;
    ULONG ScanCode = 0;
    BOOLEAN EchoOn;
    STATIC CHAR LastCommand[1024] = "";
-   STATIC CHAR LastKey = '\0';
+   STATIC CHAR NextKey = '\0';
    INT CmdHistIndex = -1;
    INT i;
 
@@ -1935,7 +1968,7 @@
    {
       if (KdDebugState & KD_DEBUG_KDSERIAL)
       {
-         Key = KdbpGetCharSerial();
+         Key = (NextKey == '\0') ? KdbpGetCharSerial() : NextKey;
          ScanCode = 0;
          if (Key == KEY_ESC) /* ESC */
          {
@@ -1961,7 +1994,8 @@
       }
       else
       {
-         Key = KdbpGetCharKeyboard(&ScanCode);
+         ScanCode = 0;
+         Key = (NextKey == '\0') ? KdbpGetCharKeyboard(&ScanCode) : NextKey;
       }
 
       if ((Buffer - Orig) >= (Size - 1))
@@ -1980,6 +2014,8 @@
             NextKey = KdbpTryGetCharSerial(5);
          else
             NextKey = KdbpTryGetCharKeyboard(&ScanCode, 5);
+         if (NextKey == '\n')
+            NextKey = '\0';
          DbgPrint("\n");
 	 /*
 	  * Repeat the last command if the user presses enter. Reduces the
@@ -1996,7 +2032,6 @@
             strncpy(LastCommand, Orig, sizeof (LastCommand));
             LastCommand[sizeof (LastCommand) - 1] = '\0';
          }
-         LastKey = Key;
          return;
       }
       else if (Key == KEY_BS || Key == KEY_DEL)
@@ -2079,7 +2114,6 @@
          *Buffer = Key;
          Buffer++;
       }
-      LastKey = Key;
    }
 }
 
@@ -2163,6 +2197,18 @@
       DbgPrint("\n");
    }
 
+   /* Flush the input buffer */
+   if (KdDebugState & KD_DEBUG_KDSERIAL)
+   {
+      while (KdbpTryGetCharSerial(1) != -1);
+   }
+   else
+   {
+      ULONG ScanCode;
+      while (KdbpTryGetCharKeyboard(&ScanCode, 1) != -1);
+   }
+
+   /* Main loop */
    do
    {
       /* Print the prompt */