*
* 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"
#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)
{
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"
if (*p != _T('/'))
{
lpText = p;
- break;
+ break;
}
-
np = _tcschr (p, _T(' '));
if (!np)
break;
-
p = np + 1;
}
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;
}
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;
}
}
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;
}
{
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("]?"));
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)
nErrorLevel = val + 1;
- freep (arg);
-
-#ifdef DEBUG
- DebugPrintf (_T("ErrorLevel: %d\n"), nErrorLevel);
-#endif /* DEBUG */
-
- return 0;
+ break;
}
+
+ Beep (440, 50);
}
+
+ freep (arg);
+ return 0;
}
- while (TRUE)
- {
- ConInKey (&ir);
+ clk = GetTickCount ();
+ amount = nTimeout*1000;
- val = IsKeyInString (lpOptions,
-#ifdef _UNICODE
- ir.Event.KeyEvent.uChar.UnicodeChar,
-#else
- ir.Event.KeyEvent.uChar.AsciiChar,
-#endif /* _UNICODE */
- bCaseSensitive);
+loop:
+ GCret = GetCharacterTimeout (&Ch, amount - (GetTickCount () - clk));
- if (val >= 0)
- {
- ConOutPrintf (_T("%c\n"), lpOptions[val]);
+ switch (GCret)
+ {
+ case GC_TIMEOUT:
+#ifdef DEBUG
+ DebugPrintf (_T("GC_TIMEOUT\n"));
+ DebugPrintf (_T("elapsed %d msecs\n"), GetTickCount () - clk);
+#endif /* DEBUG */
+ break;
- nErrorLevel = val + 1;
+ 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;
- }
-
- Beep (440, 50);
}
+#ifdef DEBUG
+ DebugPrintf(_T("exiting waiting loop after %d msecs\n"),
+ GetTickCount () - clk);
+#endif /* DEBUG */
+
+ val = IsKeyInString (lpOptions, cDefault, bCaseSensitive);
+ ConOutPrintf (_T("%c\n"), lpOptions[val]);
+
+ nErrorLevel = val + 1;
+
freep (arg);
#ifdef DEBUG
#ifdef _DEBUG
VOID DebugPrintf (LPTSTR szFormat, ...)
{
- TCHAR szOut[OUTPUT_BUFFER_SIZE];
+ TCHAR szOut[OUTPUT_BUFFER_SIZE];
va_list arg_ptr;
DWORD dwWritten;
VOID ConInFlush (VOID)
{
- FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
+ FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE));
}
/*
* 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;
"\n"
"ReactOS version written by:\n"
" Eric Kohl Emanuele Aliberti\n"
- " Dr. Faustus\n"));
+ " Paolo Pantaleo\n"));
}
else
{
/*
- * WINDOW.C - internal command.
+ * WINDOW.C - activate internal command.
*
* clone from 4nt window command
*
* 10 Sep 1999
- * started - Dr.F <dfaustus@freemail.it>
+ * started - Paolo Pantaleo <dfaustus@freemail.it>
*
*
*/
+
#include "config.h"
#ifdef INCLUDE_CMD_WINDOW
#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"
- "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"
- ));
+ "/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 <argc;i++)
+ for(i = 0; i < argc; i++)
{
- if (_tcsicmp(p[i],_T("min"))==0)
+ p_tmp=p[i];
+ if (*p_tmp == _T('/'))
+ p_tmp++;
+
+ if (_tcsicmp(p_tmp,_T("min"))==0)
{
- iAction |=A_MIN;
+ iAction |= A_MIN;
continue;
}
- if (_tcsicmp(p[i],_T("max"))==0)
+ if (_tcsicmp(p_tmp,_T("max"))==0)
{
- iAction |=A_MAX;
+ iAction |= A_MAX;
continue;
}
- if (_tcsicmp(p[i],_T("restore"))==0)
+ if (_tcsicmp(p_tmp,_T("restore"))==0)
{
- iAction |=A_RESTORE;
+ iAction |= A_RESTORE;
continue;
}
- if (_tcsnicmp(p[i],_T("/pos"),4)==0)
+ if (_tcsnicmp(p_tmp,_T("pos"),3)==0)
{
- iAction |=A_POS;
- tmp = p[i]+4;
+ iAction |= A_POS;
+ tmp = p_tmp+3;
if (*tmp == _T('='))
tmp++;
- pos.left=_ttoi(tmp);
+ pos.left= _ttoi(tmp);
if(!(tmp=_tcschr(tmp,_T(','))))
{
error_invalid_parameter_format(p[i]);
return 1;
}
- pos.top=_ttoi(++tmp);
+ pos.top = _ttoi (++tmp);
if(!(tmp=_tcschr(tmp,_T(','))))
{
error_invalid_parameter_format(p[i]);
return 1;
}
- pos.right=_ttoi(++tmp)+pos.left;
+ pos.right = _ttoi(++tmp)+pos.left;
if(!(tmp=_tcschr(tmp,_T(','))))
{
error_invalid_parameter_format(p[i]);
freep(p);
return 1;
}
- pos.bottom=_ttoi(++tmp)+pos.top;
-
+ pos.bottom = _ttoi(++tmp) + pos.top;
continue;
}
- if (_tcsnicmp(p[i],_T("/size"),5)==0)
+ if (_tcsnicmp(p_tmp,_T("size"),4)==0)
{
iAction |=A_SIZE;
continue;
}
#if 0
- if(*p[i] != _T('"'))
+ if(*p_tmp != '"')
{
error_invalid_parameter_format(p[i]);
return 1;
}
- if (p[i][0] ==_T('"'))
+ if (p_tmp[0] == _T('"'))
{
- title = (p[i]+1);
- *_tcschr(p[i]+1,_T('"'))=0;
+ title = (p_tmp+1);
+ *_tcschr(p_tmp+1,_T('"'))=0;
continue;
}
- title = p[i];
+ title = p_tmp;
}
if(title)
wp.length=sizeof(WINDOWPLACEMENT);
SetWindowPlacement(hWnd,&wp);
+ freep(p);
return 0;
}
-#endif /* INCLUDE_CMD_WINDOW */
+#endif /* INCLUDE_CMD_WINDOW */
\ No newline at end of file