2 * CHOICE.C - internal command.
7 * 12 Aug 1999 (Eric Kohl)
10 * 01 Sep 1999 (Eric Kohl)
13 * 26 Sep 1999 (Paolo Pantaleo)
16 * 02 Apr 2005 (Magnus Olsen)
17 * Remove hardcoded strings so that they can be translated.
23 #ifdef INCLUDE_CMD_CHOICE
27 #define GC_NOKEY 0 //an event occurred but it wasn't a key pressed
28 #define GC_KEYREAD 1 //a key has been read
32 GetCharacterTimeout (LPTCH ch
, DWORD dwMilliseconds
)
34 //--------------------------------------------
35 // Get a character from standard input but with a timeout.
36 // The function will wait a limited amount
37 // of time, then the function returns GC_TIMEOUT.
39 // dwMilliseconds is the timeout value, that can
40 // be set to INFINITE, so the function works like
41 // stdio.h's getchar()
46 INPUT_RECORD lpBuffer
;
48 hInput
= GetStdHandle (STD_INPUT_HANDLE
);
50 //if the timeout expired return GC_TIMEOUT
51 if (WaitForSingleObject (hInput
, dwMilliseconds
) == WAIT_TIMEOUT
)
54 //otherwise get the event
55 ReadConsoleInput (hInput
, &lpBuffer
, 1, &dwRead
);
57 //if the event is a key pressed
58 if ((lpBuffer
.EventType
== KEY_EVENT
) &&
59 (lpBuffer
.Event
.KeyEvent
.bKeyDown
!= FALSE
))
63 *ch
= lpBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
65 *ch
= lpBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
75 IsKeyInString (LPTSTR lpString
, TCHAR cKey
, BOOL bCaseSensitive
)
89 if (_totlower (*p
) == _totlower (cKey
))
102 CommandChoice (LPTSTR param
)
106 LPTSTR lpText
= NULL
;
107 BOOL bNoPrompt
= FALSE
;
108 BOOL bCaseSensitive
= FALSE
;
109 BOOL bTimeout
= FALSE
;
111 TCHAR cDefault
= _T('\0');
123 LoadString(CMD_ModuleHandle
, STRING_CHOICE_OPTION
, Options
, 4);
126 if (_tcsncmp (param
, _T("/?"), 2) == 0)
128 ConOutResPaging(TRUE
,STRING_CHOICE_HELP
);
145 np
= _tcschr (p
, _T(' '));
151 /* build parameter array */
152 arg
= split (param
, &argc
, FALSE
, FALSE
);
154 /* evaluate arguments */
157 for (i
= 0; i
< argc
; i
++)
159 if (_tcsnicmp (arg
[i
], _T("/c"), 2) == 0)
161 if (arg
[i
][2] == _T(':'))
162 lpOptions
= &arg
[i
][3];
164 lpOptions
= &arg
[i
][2];
166 if (_tcslen (lpOptions
) == 0)
168 ConErrResPuts(STRING_CHOICE_ERROR
);
173 else if (_tcsnicmp (arg
[i
], _T("/n"), 2) == 0)
177 else if (_tcsnicmp (arg
[i
], _T("/s"), 2) == 0)
179 bCaseSensitive
= TRUE
;
181 else if (_tcsnicmp (arg
[i
], _T("/t"), 2) == 0)
185 if (arg
[i
][2] == _T(':'))
187 cDefault
= arg
[i
][3];
192 cDefault
= arg
[i
][2];
198 ConErrResPuts(STRING_CHOICE_ERROR_TXT
);
207 else if (arg
[i
][0] == _T('/'))
209 ConErrResPrintf(STRING_CHOICE_ERROR_OPTION
, arg
[i
]);
218 ConOutPrintf (_T("%s"), lpText
);
221 if (bNoPrompt
== FALSE
)
223 ConOutPrintf (_T("[%c"), lpOptions
[0]);
225 for (i
= 1; (unsigned)i
< _tcslen (lpOptions
); i
++)
226 ConOutPrintf (_T(",%c"), lpOptions
[i
]);
228 ConOutPrintf (_T("]?"));
239 val
= IsKeyInString (lpOptions
,
241 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
,
243 ir
.Event
.KeyEvent
.uChar
.AsciiChar
,
249 ConOutPrintf (_T("%c\n"), lpOptions
[val
]);
251 nErrorLevel
= val
+ 1;
260 TRACE ("ErrorLevel: %d\n", nErrorLevel
);
264 clk
= GetTickCount ();
265 amount
= nTimeout
*1000;
268 GCret
= GetCharacterTimeout (&Ch
, amount
- (GetTickCount () - clk
));
273 TRACE ("GC_TIMEOUT\n");
274 TRACE ("elapsed %d msecs\n", GetTickCount () - clk
);
278 TRACE ("GC_NOKEY\n");
279 TRACE ("elapsed %d msecs\n", GetTickCount () - clk
);
283 TRACE ("GC_KEYREAD\n");
284 TRACE ("elapsed %d msecs\n", GetTickCount () - clk
);
285 TRACE ("read %c", Ch
);
286 if ((val
=IsKeyInString(lpOptions
,Ch
,bCaseSensitive
))==-1)
295 TRACE ("exiting wait loop after %d msecs\n",
296 GetTickCount () - clk
);
298 val
= IsKeyInString (lpOptions
, cDefault
, bCaseSensitive
);
299 ConOutPrintf(_T("%c\n"), lpOptions
[val
]);
301 nErrorLevel
= val
+ 1;
305 TRACE ("ErrorLevel: %d\n", nErrorLevel
);
309 #endif /* INCLUDE_CMD_CHOICE */