From 7bcdd2ea101eb44a4ffe53705e3cb045d6b04524 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Sun, 22 Aug 1999 21:46:50 +0000 Subject: [PATCH] Added CHOICE command and fixed a cursor bug. svn path=/trunk/; revision=627 --- reactos/apps/utils/cmd/choice.c | 245 +++++++++++++++++++++++++++++ reactos/apps/utils/cmd/cmd.h | 7 +- reactos/apps/utils/cmd/cmdtable.c | 5 + reactos/apps/utils/cmd/config.h | 3 +- reactos/apps/utils/cmd/console.c | 7 +- reactos/apps/utils/cmd/files.txt | 3 +- reactos/apps/utils/cmd/history.txt | 3 +- reactos/apps/utils/cmd/makefile | 10 +- 8 files changed, 273 insertions(+), 10 deletions(-) create mode 100644 reactos/apps/utils/cmd/choice.c diff --git a/reactos/apps/utils/cmd/choice.c b/reactos/apps/utils/cmd/choice.c new file mode 100644 index 00000000000..2e7117ac6ae --- /dev/null +++ b/reactos/apps/utils/cmd/choice.c @@ -0,0 +1,245 @@ +/* + * CHOICE.C - internal command. + * + * + * History: + * + * 12 Jul 1999 (Eric Kohl) + * started. + */ + +#include "config.h" + +#ifdef INCLUDE_CMD_CHOICE + +#include +#include +#include +#include +#include + +#include "cmd.h" +#include "batch.h" + + +static INT +IsKeyInString (LPTSTR lpString, TCHAR cKey, BOOL bCaseSensitive) +{ + PTCHAR p = lpString; + INT val = 0; + + while (*p) + { + if (bCaseSensitive) + { + if (*p == cKey) + return val; + } + else + { + if (_totlower (*p) == _totlower (cKey)) + return val; + } + + val++; + p++; + } + + return -1; +} + + +INT +CommandChoice (LPTSTR cmd, LPTSTR param) +{ + LPTSTR lpOptions = "YN"; + LPTSTR lpText = NULL; + BOOL bNoPrompt = FALSE; + BOOL bCaseSensitive = FALSE; + BOOL bTimeout = FALSE; + INT nTimeout = 0; + TCHAR cDefault = _T('\0'); + INPUT_RECORD ir; + LPTSTR p, np; + LPTSTR *arg; + INT argc; + INT i; + INT val; + + if (_tcsncmp (param, _T("/?"), 2) == 0) + { + ConOutPuts (_T("Prompts the user to select an option.\n" + "\n" + "CHOICE [/C[:]options][/N][/S][/T[:]c,nn][text]\n" + "\n" + " /C[:]options Allowed option characters. Default is YN.\n" + " /N The option characters and the question mark will\n" + " not be appended.\n" + " /S Input is case sensitive.\n" + " /T[:]c,nn Default to c after nn seconds.\n" + " text Displayed prompt.\n" + "\n" + "ERRORLEVEL ...\n")); + return 0; + } + + /* retrieve text */ + p = param; + + while (TRUE) + { + if (*p == _T('\0')) + break; + if (*p != _T('/')) + { + lpText = p; + break; + } + np = _tcschr (p, _T(' ')); + if (!np) + break; + p = np + 1; + } + + /* build parameter array */ + arg = split (param, &argc); + + /* evaluate arguments */ + if (argc > 0) + { + for (i = 0; i < argc; i++) + { + if (_tcsnicmp (arg[i], _T("/c"), 2) == 0) + { + if (arg[i][2] == _T(':')) + lpOptions = &arg[i][3]; + else + lpOptions = &arg[i][2]; + + if (_tcslen (lpOptions) == 0) + { + ConErrPuts (_T("CHOICE: Error!!")); + freep (arg); + return 1; + } + } + else if (_tcsnicmp (arg[i], _T("/n"), 2) == 0) + { + bNoPrompt = TRUE; + } + else if (_tcsnicmp (arg[i], _T("/s"), 2) == 0) + { + bCaseSensitive = TRUE; + } + else if (_tcsnicmp (arg[i], _T("/t"), 2) == 0) + { + LPTSTR s; + + if (arg[i][2] == _T(':')) + { + cDefault = arg[i][3]; + s = & arg[i][4]; + } + else + { + cDefault = arg[i][2]; + s = & arg[i][3]; + } + + if (*s != _T(',')) + { + ConErrPrintf (_T("Format Error: /T!!\n")); + freep (arg); + return 1; + } + + s++; + nTimeout = _ttoi(s); + bTimeout = TRUE; + } + else if (arg[i][0] == _T('/')) + { + ConErrPrintf (_T("Illegal Option: %s"), arg[i]); + freep (arg); + return 1; + } + } + } + + /* print text */ + if (lpText) + ConOutPrintf (_T("%s"), lpText); + + /* print options */ + if (bNoPrompt == FALSE) + { + ConOutPrintf (_T("[%c"), lpOptions[0]); + + for (i = 1; i < _tcslen (lpOptions); i++) + ConOutPrintf (_T(",%c"), lpOptions[i]); + + ConOutPrintf (_T("]?")); + } + + ConInFlush (); + + if (bTimeout) + { + if (WaitForSingleObject (GetStdHandle(STD_INPUT_HANDLE), + nTimeout * 1000) == WAIT_TIMEOUT) + { + val = IsKeyInString (lpOptions, + cDefault, + bCaseSensitive); + + if (val >= 0) + { + ConOutPrintf (_T("%c\n"), lpOptions[val]); + + nErrorLevel = val + 1; + + freep (arg); +#ifdef _DEBUG + DebugPrintf (_T("ErrorLevel: %d\n"), nErrorLevel); +#endif /* _DEBUG */ + return 0; + } + + } + } + + 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); + } + + freep (arg); + +#ifdef _DEBUG + DebugPrintf (_T("ErrorLevel: %d\n"), nErrorLevel); +#endif /* _DEBUG */ + + return 0; +} +#endif /* INCLUDE_CMD_CHOICE */ + +/* EOF */ diff --git a/reactos/apps/utils/cmd/cmd.h b/reactos/apps/utils/cmd/cmd.h index 95313a36db7..38e9bcc37f6 100644 --- a/reactos/apps/utils/cmd/cmd.h +++ b/reactos/apps/utils/cmd/cmd.h @@ -30,7 +30,7 @@ #include -#define CMD_VER "0.1 pre 2" +#define CMD_VER "0.1 pre 5" #ifdef _MSC_VER #define SHELLVER "Version " CMD_VER " [" __DATE__ ", msc]" @@ -105,6 +105,10 @@ INT cmd_beep (LPTSTR, LPTSTR); INT cmd_call (LPTSTR, LPTSTR); +/* Prototypes for CHOICE.C */ +INT CommandChoice (LPTSTR, LPTSTR); + + /* Prototypes for CLS.C */ INT cmd_cls (LPTSTR, LPTSTR); @@ -136,6 +140,7 @@ VOID DebugPrintf (LPTSTR, ...); #endif /* _DEBUG */ VOID ConInDummy (VOID); +VOID ConInFlush (VOID); VOID ConInKey (PINPUT_RECORD); VOID ConInString (LPTSTR, DWORD); diff --git a/reactos/apps/utils/cmd/cmdtable.c b/reactos/apps/utils/cmd/cmdtable.c index 850b12a9246..d0f7d3a53c5 100644 --- a/reactos/apps/utils/cmd/cmdtable.c +++ b/reactos/apps/utils/cmd/cmdtable.c @@ -68,6 +68,11 @@ COMMAND cmds[] = {_T("chcp"), 0, CommandChcp}, #endif +#ifdef INCLUDE_CMD_CHOICE + {_T("choice"), 0, CommandChoice}, +#endif + + #ifdef INCLUDE_CMD_CLS {_T("cls"), 0, cmd_cls}, #endif diff --git a/reactos/apps/utils/cmd/config.h b/reactos/apps/utils/cmd/config.h index 27910cac438..7d617ae9cda 100644 --- a/reactos/apps/utils/cmd/config.h +++ b/reactos/apps/utils/cmd/config.h @@ -59,6 +59,7 @@ /*#define INCLUDE_CMD_BREAK*/ #define INCLUDE_CMD_CHCP #define INCLUDE_CMD_CHDIR +#define INCLUDE_CMD_CHOICE #define INCLUDE_CMD_CLS #define INCLUDE_CMD_COLOR #define INCLUDE_CMD_COPY @@ -86,7 +87,7 @@ #define INCLUDE_CMD_VOL /* -command that do not have a define: +commands that do not have a define: exit call diff --git a/reactos/apps/utils/cmd/console.c b/reactos/apps/utils/cmd/console.c index a4881d63442..931a7ef4838 100644 --- a/reactos/apps/utils/cmd/console.c +++ b/reactos/apps/utils/cmd/console.c @@ -52,6 +52,11 @@ VOID ConInDummy (VOID) ReadConsoleInput (hInput, &dummy, 1, &dwRead); } +VOID ConInFlush (VOID) +{ + FlushConsoleInputBuffer (GetStdHandle (STD_INPUT_HANDLE)); +} + VOID ConInKey (PINPUT_RECORD lpBuffer) { @@ -232,7 +237,7 @@ VOID SetCursorType (BOOL bInsert, BOOL bVisible) { CONSOLE_CURSOR_INFO cci; - cci.dwSize = bInsert ? 10 : 100; + cci.dwSize = bInsert ? 10 : 99; cci.bVisible = bVisible; SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci); diff --git a/reactos/apps/utils/cmd/files.txt b/reactos/apps/utils/cmd/files.txt index 8f1dfefd3da..d7eb1e2fba8 100644 --- a/reactos/apps/utils/cmd/files.txt +++ b/reactos/apps/utils/cmd/files.txt @@ -18,6 +18,7 @@ batch.c Batch file interpreter beep.c Implements beep command call.c Implements call command chcp.c Implements chcp command +choice.c Implements choice command cls.c Implements cls command cmdinput.c Command-line input functions cmdtable.c Table of available internal commands @@ -53,4 +54,4 @@ time.c Implements time command type.c Implements type command ver.c Implements ver command where.c Code to search path for executables -verify.c Implements verify command \ No newline at end of file +verify.c Implements verify command diff --git a/reactos/apps/utils/cmd/history.txt b/reactos/apps/utils/cmd/history.txt index 4fc998935ee..310a754806b 100644 --- a/reactos/apps/utils/cmd/history.txt +++ b/reactos/apps/utils/cmd/history.txt @@ -344,10 +344,11 @@ o Added CHCP command. o Fixed keyboard input bug. o Rewrote DEL and MOVE commands. -30-Mar-1999 ReactOS CMD version 0.1 pre 2 (Eric Kohl ) +19-Aug-1999 ReactOS CMD version 0.1 pre 5 (Eric Kohl ) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ o Cleaned up DIR command. o Searching for executables in the right order. o Fixed some little but nasty bugs. o Added TITLE command. Thanks to Emanuele Aliberti! o Added "/Q", "/W" and "/Z" options to DEL command. +o Added CHOICE command. diff --git a/reactos/apps/utils/cmd/makefile b/reactos/apps/utils/cmd/makefile index 39fd9793d7a..64308bbdf00 100644 --- a/reactos/apps/utils/cmd/makefile +++ b/reactos/apps/utils/cmd/makefile @@ -5,11 +5,11 @@ all: cmd.exe OBJECTS = ../common/crt0.o cmd.o attrib.o alias.o batch.o beep.o call.o \ - chcp.o cls.o cmdinput.o cmdtable.o color.o console.o copy.o date.o \ - del.o dir.o dirstack.o echo.o error.o filecomp.o for.o goto.o \ - history.o if.o internal.o label.o locale.o misc.o move.o path.o \ - pause.o prompt.o redir.o ren.o set.o shift.o start.o time.o title.o \ - type.o ver.o verify.o vol.o where.o + chcp.o choice.o cls.o cmdinput.o cmdtable.o color.o console.o \ + copy.o date.o del.o dir.o dirstack.o echo.o error.o filecomp.o \ + for.o goto.o history.o if.o internal.o label.o locale.o misc.o \ + move.o path.o pause.o prompt.o redir.o ren.o set.o shift.o \ + start.o time.o title.o type.o ver.o verify.o vol.o where.o LIBS= ../../lib/kernel32/kernel32.a ../../lib/crtdll/crtdll.a