2 * CMDINPUT.C - handles command input (tab completion, history, etc.).
7 * 01/14/95 (Tim Norman)
10 * 08/08/95 (Matt Rains)
11 * i have cleaned up the source code. changes now bring this source
12 * into guidelines for recommended programming practice.
13 * i have added some constants to help making changes easier.
15 * 12/12/95 (Tim Norman)
16 * added findxy() function to get max x/y coordinates to display
17 * correctly on larger screens
19 * 12/14/95 (Tim Norman)
20 * fixed the Tab completion code that Matt Rains broke by moving local
21 * variables to a more global scope and forgetting to initialize them
25 * fixed a bug in tab completion that caused filenames at the beginning
26 * of the command-line to have their first letter truncated
29 * fixed a silly bug using printf instead of fputs, where typing "%i"
32 * 6/14/97 (Steffan Kaiser)
35 * 6/7/97 (Marc Desrochers)
36 * recoded everything! now properly adjusts when text font is changed.
37 * removed findxy(), reposition(), and reprint(), as these functions
38 * were inefficient. added goxy() function as gotoxy() was buggy when
39 * the screen font was changed. the printf() problem with %i on the
40 * command line was fixed by doing printf("%s",str) instead of
41 * printf(str). Don't ask how I find em just be glad I do :)
43 * 7/12/97 (Tim Norman)
44 * Note: above changes pre-empted Steffan's ctrl-break checking.
46 * 7/7/97 (Marc Desrochers)
47 * rewrote a new findxy() because the new dir() used it. This
48 * findxy() simply returns the values of *maxx *maxy. In the
49 * future, please use the pointers, they will always be correct
50 * since they point to BIOS values.
52 * 7/8/97 (Marc Desrochers)
53 * once again removed findxy(), moved the *maxx, *maxy pointers
54 * global and included them as externs in command.h. Also added
55 * insert/overstrike capability
57 * 7/13/97 (Tim Norman)
58 * added different cursor appearance for insert/overstrike mode
60 * 7/13/97 (Tim Norman)
61 * changed my code to use _setcursortype until I can figure out why
62 * my code is crashing on some machines. It doesn't crash on mine :)
64 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
65 * added config.h include
67 * 28-Jul-1998 (John P Price <linux-guru@gcfl.net>)
68 * put ifdef's around filename completion code.
70 * 30-Jul-1998 (John P Price <linux-guru@gcfl.net>)
71 * moved filename completion code to filecomp.c
72 * made second TAB display list of filename matches
74 * 31-Jul-1998 (John P Price <linux-guru@gcfl.net>)
75 * Fixed bug where if you typed something, then hit HOME, then tried
76 * to type something else in insert mode, it crashed.
78 * 07-Aug-1998 (John P Price <linux-guru@gcfl.net>)
79 * Fixed carrage return output to better match MSDOS with echo
80 * on or off.(marked with "JPP 19980708")
82 * 13-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
83 * Added insert/overwrite cursor.
85 * 25-Jan-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
86 * Replaced CRT io functions by Win32 console io functions.
87 * This can handle <Shift>-<Tab> for 4NT filename completion.
88 * Unicode and redirection safe!
90 * 04-Feb-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
91 * Fixed input bug. A "line feed" character remained in the keyboard
92 * input queue when you pressed <RETURN>. This sometimes caused
93 * some very strange effects.
94 * Fixed some command line editing annoyances.
96 * 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
97 * Fixed problems when the screen was scrolled away.
113 * global command line insert/overwrite flag
115 static BOOL bInsert
= TRUE
;
119 ClearCommandLine (LPTSTR str
, INT maxlen
, SHORT orgx
, SHORT orgy
)
123 SetCursorXY (orgx
, orgy
);
124 for (count
= 0; count
< (INT
)_tcslen (str
); count
++)
125 ConOutChar (_T(' '));
126 _tcsnset (str
, _T('\0'), maxlen
);
127 SetCursorXY (orgx
, orgy
);
131 /* read in a command line */
132 VOID
ReadCommand (LPTSTR str
, INT maxlen
)
134 SHORT orgx
; /* origin x/y */
136 SHORT curx
; /*current x/y cursor position*/
139 INT count
; /*used in some for loops*/
140 INT current
= 0; /*the position of the cursor in the string (str)*/
141 INT charcount
= 0;/*chars in the string (str)*/
145 BOOL bContinue
=FALSE
;/*is TRUE the second case will not be executed*/
147 /* get screen size */
148 GetScreenSize (&maxx
, &maxy
);
150 /* JPP 19980807 - if echo off, don't print prompt */
154 GetCursorXY (&orgx
, &orgy
);
155 GetCursorXY (&curx
, &cury
);
157 memset (str
, 0, maxlen
* sizeof (TCHAR
));
159 SetCursorType (bInsert
, TRUE
);
165 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
166 (RIGHT_ALT_PRESSED
|RIGHT_ALT_PRESSED
|
167 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
170 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
173 #ifdef FEATURE_HISTORY
176 /*add the current command line to the history*/
177 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
178 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
184 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
185 current
= charcount
= 0;
193 /*delete current history entry*/
194 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
195 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
197 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
198 History_del_current_entry(str
);
199 current
= charcount
= _tcslen (str
);
200 ConOutPrintf (_T("%s"), str
);
201 GetCursorXY (&curx
, &cury
);
206 #endif/*FEATURE_HISTORY*/
219 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
222 /* <BACKSPACE> - delete character to left of cursor */
223 if (current
> 0 && charcount
> 0)
225 if (current
== charcount
)
227 /* if at end of line */
228 str
[current
- 1] = _T('\0');
229 if (GetCursorX () != 0)
231 ConOutPrintf (_T("\b \b"));
236 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
237 ConOutChar (_T(' '));
238 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
245 for (count
= current
- 1; count
< charcount
; count
++)
246 str
[count
] = str
[count
+ 1];
247 if (GetCursorX () != 0)
249 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
254 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
258 GetCursorXY (&curx
, &cury
);
259 ConOutPrintf (_T("%s "), &str
[current
- 1]);
260 SetCursorXY (curx
, cury
);
268 /* toggle insert/overstrike mode */
270 SetCursorType (bInsert
, TRUE
);
274 /* delete character under cursor */
275 if (current
!= charcount
&& charcount
> 0)
277 for (count
= current
; count
< charcount
; count
++)
278 str
[count
] = str
[count
+ 1];
280 GetCursorXY (&curx
, &cury
);
281 ConOutPrintf (_T("%s "), &str
[current
]);
282 SetCursorXY (curx
, cury
);
287 /* goto beginning of string */
290 SetCursorXY (orgx
, orgy
);
298 /* goto end of string */
299 if (current
!= charcount
)
301 SetCursorXY (orgx
, orgy
);
302 ConOutPrintf (_T("%s"), str
);
303 GetCursorXY (&curx
, &cury
);
309 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
310 /* expand current file name */
311 if ((current
== charcount
) ||
312 (current
== charcount
- 1 &&
313 str
[current
] == _T('"'))) /* only works at end of line*/
315 if (wLastKey
!= VK_TAB
)
317 /* if first TAB, complete filename*/
318 tempscreen
= charcount
;
319 CompleteFilename (str
, charcount
);
320 charcount
= _tcslen (str
);
324 str
[current
-1] == _T('"'))
327 SetCursorXY (orgx
, orgy
);
328 ConOutPrintf (_T("%s"), str
);
330 if (tempscreen
> charcount
)
332 GetCursorXY (&curx
, &cury
);
333 for (count
= tempscreen
- charcount
; count
--; )
334 ConOutChar (_T(' '));
335 SetCursorXY (curx
, cury
);
339 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
340 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
343 /* set cursor position */
344 SetCursorXY ((orgx
+ current
) % maxx
,
345 orgy
+ (orgx
+ current
) / maxx
);
346 GetCursorXY (&curx
, &cury
);
350 /*if second TAB, list matches*/
351 if (ShowCompletionMatches (str
, charcount
))
354 GetCursorXY (&orgx
, &orgy
);
355 ConOutPrintf (_T("%s"), str
);
357 /* set cursor position */
358 SetCursorXY ((orgx
+ current
) % maxx
,
359 orgy
+ (orgx
+ current
) / maxx
);
360 GetCursorXY (&curx
, &cury
);
374 #ifdef FEATURE_4NT_FILENAME_COMPLETION
375 /* this is not implemented yet */
376 if (ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
)
378 /* get previous match */
390 /* end input, return to main */
391 #ifdef FEATURE_HISTORY
392 /* add to the history */
397 ConOutChar (_T('\n'));
401 /* clear str Make this callable! */
402 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
405 current
= charcount
= 0;
408 #ifdef FEATURE_HISTORY
410 History_move_to_bottom();
413 #ifdef FEATURE_HISTORY
414 /* get previous command from buffer */
415 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
417 current
= charcount
= _tcslen (str
);
418 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
419 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
420 ConOutPrintf (_T("%s"), str
);
421 GetCursorXY (&curx
, &cury
);
426 #ifdef FEATURE_HISTORY
427 /* get next command from buffer */
428 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
430 current
= charcount
= _tcslen (str
);
431 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
432 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
433 ConOutPrintf (_T("%s"), str
);
434 GetCursorXY (&curx
, &cury
);
439 /* move cursor left */
443 if (GetCursorX () == 0)
445 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
451 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
466 /* move cursor right */
467 if (current
!= charcount
)
470 if (GetCursorX () == maxx
- 1)
472 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
478 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
486 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
487 if ((ch
>= 32 && ch
<= 255) && (charcount
!= (maxlen
- 2)))
489 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
490 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)))
491 #endif /* _UNICODE */
493 /* insert character into string... */
494 if (bInsert
&& current
!= charcount
)
496 /* If this character insertion will cause screen scrolling,
497 * adjust the saved origin of the command prompt. */
498 tempscreen
= _tcslen(str
+ current
) + curx
;
499 if ((tempscreen
% maxx
) == (maxx
- 1) &&
500 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
506 for (count
= charcount
; count
> current
; count
--)
507 str
[count
] = str
[count
- 1];
509 if (curx
== maxx
- 1)
513 ConOutPrintf (_T("%s"), &str
[current
- 1]);
514 SetCursorXY (curx
, cury
);
519 if (current
== charcount
)
522 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
524 if (GetCursorX () == maxx
- 1)
544 wLastKey
= ir
.Event
.KeyEvent
.wVirtualKeyCode
;
546 while (ir
.Event
.KeyEvent
.wVirtualKeyCode
!= VK_RETURN
);
548 SetCursorType (bInsert
, TRUE
);