diff --git a/rosapps/cmd/choice.c b/rosapps/cmd/choice.c index 98b3dd2eec3..22614e86dd9 100644 --- a/rosapps/cmd/choice.c +++ b/rosapps/cmd/choice.c @@ -4,11 +4,14 @@ * * History: * - * 12-Aug-1999 (Eric Kohl) - * Started. + * 12 Aug 1999 (Eric Kohl) + * started. * * 01 Sep 1999 (Eric Kohl) * Fixed help text. + * + * 26 Sep 1999 (Paolo Pantaleo) + * Fixed timeout. */ #include "config.h" @@ -25,11 +28,63 @@ #include "batch.h" +#define GC_TIMEOUT -1 +#define GC_NOKEY 0 //an event occurred but it wasn't a key pressed +#define GC_KEYREAD 1 //a key has been read + + +static INT +GetCharacterTimeout (LPTCH ch, DWORD dwMilliseconds) +{ +//-------------------------------------------- +// Get a character from standard input but with a timeout. +// The function will wait a limited amount +// of time, then the function returns GC_TIMEOUT. +// +// dwMilliseconds is the timeout value, that can +// be set to INFINITE, so the function works like +// stdio.h's getchar() + + HANDLE hInput; + DWORD dwRead; + + INPUT_RECORD lpBuffer; + + hInput = GetStdHandle (STD_INPUT_HANDLE); + +#ifdef _DEBUG + if (hInput == INVALID_HANDLE_VALUE) + DebugPrintf ("Invalid input handle!!!\n"); +#endif + //if the timeout experied return GC_TIMEOUT + if (WaitForSingleObject (hInput, dwMilliseconds) == WAIT_TIMEOUT) + return GC_TIMEOUT; + + //otherwise get the event + ReadConsoleInput (hInput, &lpBuffer, 1, &dwRead); + + //if the event is a key pressed + if ((lpBuffer.EventType == KEY_EVENT) && + (lpBuffer.Event.KeyEvent.bKeyDown == TRUE)) + { + //read the key +#ifdef _UNICODE + *ch = lpBuffer.Event.KeyEvent.uChar.UnicodeChar; +#else + *ch = lpBuffer.Event.KeyEvent.uChar.AsciiChar; +#endif + return GC_KEYREAD; + } + + //else return no key + return GC_NOKEY; +} + static INT IsKeyInString (LPTSTR lpString, TCHAR cKey, BOOL bCaseSensitive) { - LPTSTR p = lpString; - INT val = 0; + LPTCH p = lpString; + INT val = 0; while (*p) { @@ -69,6 +124,10 @@ CommandChoice (LPTSTR cmd, LPTSTR param) INT i; INT val; + INT GCret; + TCHAR Ch; + DWORD amount,clk; + if (_tcsncmp (param, _T("/?"), 2) == 0) { ConOutPuts (_T("Waits for the user to choose one of a set of choices.\n" @@ -96,13 +155,11 @@ CommandChoice (LPTSTR cmd, LPTSTR param) if (*p != _T('/')) { lpText = p; - break; + break; } - np = _tcschr (p, _T(' ')); if (!np) break; - p = np + 1; } @@ -123,7 +180,7 @@ CommandChoice (LPTSTR cmd, LPTSTR param) if (_tcslen (lpOptions) == 0) { - ConErrPuts (_T("Invalid choise switch syntax. Expected format: /C[:]choices\n")); + ConErrPuts (_T("Invalid option. Expected format: /C[:]options")); freep (arg); return 1; } @@ -143,17 +200,17 @@ CommandChoice (LPTSTR cmd, LPTSTR param) if (arg[i][2] == _T(':')) { cDefault = arg[i][3]; - s = & arg[i][4]; + s = &arg[i][4]; } else { cDefault = arg[i][2]; - s = & arg[i][3]; + s = &arg[i][3]; } if (*s != _T(',')) { - ConErrPrintf (_T("Invalid timeout syntax. Expected format: /T[:]c,nn\n")); + ConErrPuts (_T("Invalid option. Expected format: /T[:]c,nn")); freep (arg); return 1; } @@ -164,8 +221,7 @@ CommandChoice (LPTSTR cmd, LPTSTR param) } else if (arg[i][0] == _T('/')) { - ConErrPrintf (_T("Invalid switch on command line. Expected format:\n" - " CHOICE [/C[:]choices][/N][/S][/T[:]c,nn][text]\n")); + ConErrPrintf (_T("Illegal Option: %s"), arg[i]); freep (arg); return 1; } @@ -181,7 +237,7 @@ CommandChoice (LPTSTR cmd, LPTSTR param) { ConOutPrintf (_T("[%c"), lpOptions[0]); - for (i = 1; i < _tcslen (lpOptions); i++) + for (i = 1; (unsigned)i < _tcslen (lpOptions); i++) ConOutPrintf (_T(",%c"), lpOptions[i]); ConOutPrintf (_T("]?")); @@ -189,13 +245,18 @@ CommandChoice (LPTSTR cmd, LPTSTR param) ConInFlush (); - if (bTimeout) + if(!bTimeout) { - if (WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE), - nTimeout * 1000) == WAIT_TIMEOUT) + while (TRUE) { + ConInKey (&ir); + val = IsKeyInString (lpOptions, - cDefault, +#ifdef _UNICODE + ir.Event.KeyEvent.uChar.UnicodeChar, +#else + ir.Event.KeyEvent.uChar.AsciiChar, +#endif /* _UNICODE */ bCaseSensitive); if (val >= 0) @@ -204,40 +265,62 @@ CommandChoice (LPTSTR cmd, LPTSTR param) nErrorLevel = val + 1; - freep (arg); + break; + } + + Beep (440, 50); + } + + freep (arg); + return 0; + } + + clk = GetTickCount (); + amount = nTimeout*1000; + +loop: + GCret = GetCharacterTimeout (&Ch, amount - (GetTickCount () - clk)); + + switch (GCret) + { + case GC_TIMEOUT: +#ifdef DEBUG + DebugPrintf (_T("GC_TIMEOUT\n")); + DebugPrintf (_T("elapsed %d msecs\n"), GetTickCount () - clk); +#endif /* DEBUG */ + break; + + case GC_NOKEY: +#ifdef DEBUG + DebugPrintf(_T("GC_NOKEY\n")); + DebugPrintf(_T("elapsed %d msecs\n"), GetTickCount () - clk); +#endif /* DEBUG */ + goto loop; + + case GC_KEYREAD: +#ifdef DEBUG + DebugPrintf(_T("GC_KEYREAD\n")); + DebugPrintf(_T("elapsed %d msecs\n"), GetTickCount () - clk); + DebugPrintf(_T("read %c"), Ch); +#endif /* DEBUG */ + if ((val=IsKeyInString(lpOptions,Ch,bCaseSensitive))==-1) + { + Beep (440, 50); + goto loop; + } + cDefault=Ch; + break; + } #ifdef DEBUG - DebugPrintf (_T("ErrorLevel: %d\n"), nErrorLevel); + DebugPrintf(_T("exiting waiting loop after %d msecs\n"), + GetTickCount () - clk); #endif /* DEBUG */ - return 0; - } - } - } + val = IsKeyInString (lpOptions, cDefault, bCaseSensitive); + ConOutPrintf (_T("%c\n"), lpOptions[val]); - while (TRUE) - { - ConInKey (&ir); - - val = IsKeyInString (lpOptions, -#ifdef _UNICODE - ir.Event.KeyEvent.uChar.UnicodeChar, -#else - ir.Event.KeyEvent.uChar.AsciiChar, -#endif /* _UNICODE */ - bCaseSensitive); - - if (val >= 0) - { - ConOutPrintf (_T("%c\n"), lpOptions[val]); - - nErrorLevel = val + 1; - - break; - } - - Beep (440, 50); - } + nErrorLevel = val + 1; freep (arg); diff --git a/rosapps/cmd/console.c b/rosapps/cmd/console.c index 62f80a64922..213758bb270 100644 --- a/rosapps/cmd/console.c +++ b/rosapps/cmd/console.c @@ -25,7 +25,7 @@ #ifdef _DEBUG VOID DebugPrintf (LPTSTR szFormat, ...) { - TCHAR szOut[OUTPUT_BUFFER_SIZE]; + TCHAR szOut[OUTPUT_BUFFER_SIZE]; va_list arg_ptr; DWORD dwWritten; @@ -57,7 +57,7 @@ VOID ConInDummy (VOID) VOID ConInFlush (VOID) { - FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE)); + FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE)); } diff --git a/rosapps/cmd/misc.c b/rosapps/cmd/misc.c index 65231823c97..ccf16819eb6 100644 --- a/rosapps/cmd/misc.c +++ b/rosapps/cmd/misc.c @@ -300,23 +300,26 @@ BOOL FileGetString (HANDLE hFile, LPTSTR lpBuffer, INT nBufferLength) /* * GetConsoleWindow - returns the handle to the current console window */ -HWND GetConsoleWindow(VOID) +HWND GetConsoleWindow (VOID) { - TCHAR original[256]; /* stores original title*/ - TCHAR temp[256]; /* stores temp title*/ - + TCHAR original[256]; + TCHAR temp[256]; HWND h=0; - GetConsoleTitle(original,sizeof(original)); + GetConsoleTitle (original, sizeof(original)); - _tcscpy(temp,original); - _tcscat(temp,_T("-xxx ")); + _tcscpy (temp, original); + _tcscat (temp, _T("-xxx ")); - if((h = FindWindow("tty",temp)) == NULL ) + if (FindWindow (0, temp) == NULL ) { - SetConsoleTitle(temp); - h=FindWindow("tty",temp); - SetConsoleTitle(original); + SetConsoleTitle (temp); + Sleep (0); + + while(!(h = FindWindow (0, temp))) + ; + + SetConsoleTitle (original); } return h; diff --git a/rosapps/cmd/ver.c b/rosapps/cmd/ver.c index 2ec82d67f62..04cdc9cb5ec 100644 --- a/rosapps/cmd/ver.c +++ b/rosapps/cmd/ver.c @@ -128,7 +128,7 @@ INT cmd_ver (LPTSTR cmd, LPTSTR param) "\n" "ReactOS version written by:\n" " Eric Kohl Emanuele Aliberti\n" - " Dr. Faustus\n")); + " Paolo Pantaleo\n")); } else { diff --git a/rosapps/cmd/window.c b/rosapps/cmd/window.c index 4e207b58ae8..254b699be0e 100644 --- a/rosapps/cmd/window.c +++ b/rosapps/cmd/window.c @@ -1,14 +1,15 @@ /* - * WINDOW.C - internal command. + * WINDOW.C - activate internal command. * * clone from 4nt window command * * 10 Sep 1999 - * started - Dr.F + * started - Paolo Pantaleo * * */ + #include "config.h" #ifdef INCLUDE_CMD_WINDOW @@ -27,69 +28,65 @@ #define A_SIZE 0x10 - - INT CommandWindow (LPTSTR cmd, LPTSTR param) { - LPTSTR *p; + LPTSTR *p,p_tmp; INT argc,i; - INT iAction=0; - LPTSTR title=0; - HWND hWnd; WINDOWPLACEMENT wp; RECT pos; - LPTSTR tmp; if (_tcsncmp (param, _T("/?"), 2) == 0) { - ConOutPuts(_T( - "change console window aspect\n" + ConOutPuts(_T("change console window aspect\n" "\n" - "WINDOW [/POS[=]left,top,width,heigth]\n" - " [MIN|MAX|RESTORE]\n" - "\n" - "/POS specify window placement and dimensions\n" - "MIN minimize the winodw\n" - "MAX maximize the winodw\n" - "RESTORE restore the window" - )); + "WINDOW [/POS[=]left,top,width,heigth]\n" + " [MIN|MAX|RESTORE]\n" + "\n" + "/POS specify window placement and dimensions\n" + "MIN minimize the window\n" + "MAX maximize the window\n" + "RESTORE restore the window")); return 0; } p=split(param,&argc); - for(i=0;i