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.
101 #include "resource.h"
108 * global command line insert/overwrite flag
110 static BOOL bInsert
= TRUE
;
114 ClearCommandLine (LPTSTR str
, INT maxlen
, SHORT orgx
, SHORT orgy
)
118 SetCursorXY (orgx
, orgy
);
119 for (count
= 0; count
< (INT
)_tcslen (str
); count
++)
120 ConOutChar (_T(' '));
121 _tcsnset (str
, _T('\0'), maxlen
);
122 SetCursorXY (orgx
, orgy
);
126 /* read in a command line */
127 VOID
ReadCommand (LPTSTR str
, INT maxlen
)
129 SHORT orgx
; /* origin x/y */
131 SHORT curx
; /*current x/y cursor position*/
134 INT count
; /*used in some for loops*/
135 INT current
= 0; /*the position of the cursor in the string (str)*/
136 INT charcount
= 0;/*chars in the string (str)*/
140 BOOL bContinue
=FALSE
;/*is TRUE the second case will not be executed*/
141 BOOL bReturn
= FALSE
;
142 TCHAR szPath
[MAX_PATH
];
145 /* get screen size */
146 GetScreenSize (&maxx
, &maxy
);
148 /* JPP 19980807 - if echo off, don't print prompt */
152 GetCursorXY (&orgx
, &orgy
);
153 GetCursorXY (&curx
, &cury
);
155 memset (str
, 0, maxlen
* sizeof (TCHAR
));
157 SetCursorType (bInsert
, TRUE
);
166 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
167 (RIGHT_ALT_PRESSED
|RIGHT_ALT_PRESSED
|
168 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
171 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
174 #ifdef FEATURE_HISTORY
177 /*add the current command line to the history*/
178 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
179 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
185 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
186 current
= charcount
= 0;
194 /*delete current history entry*/
195 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
196 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
198 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
199 History_del_current_entry(str
);
200 current
= charcount
= _tcslen (str
);
201 ConOutPrintf (_T("%s"), str
);
202 GetCursorXY (&curx
, &cury
);
207 #endif/*FEATURE_HISTORY*/
221 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
224 /* <BACKSPACE> - delete character to left of cursor */
225 if (current
> 0 && charcount
> 0)
227 if (current
== charcount
)
229 /* if at end of line */
230 str
[current
- 1] = _T('\0');
231 if (GetCursorX () != 0)
233 ConOutPrintf (_T("\b \b"));
238 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
239 ConOutChar (_T(' '));
240 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
247 for (count
= current
- 1; count
< charcount
; count
++)
248 str
[count
] = str
[count
+ 1];
249 if (GetCursorX () != 0)
251 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
256 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
260 GetCursorXY (&curx
, &cury
);
261 ConOutPrintf (_T("%s "), &str
[current
- 1]);
262 SetCursorXY (curx
, cury
);
270 /* toggle insert/overstrike mode */
272 SetCursorType (bInsert
, TRUE
);
276 /* delete character under cursor */
277 if (current
!= charcount
&& charcount
> 0)
279 for (count
= current
; count
< charcount
; count
++)
280 str
[count
] = str
[count
+ 1];
282 GetCursorXY (&curx
, &cury
);
283 ConOutPrintf (_T("%s "), &str
[current
]);
284 SetCursorXY (curx
, cury
);
289 /* goto beginning of string */
292 SetCursorXY (orgx
, orgy
);
300 /* goto end of string */
301 if (current
!= charcount
)
303 SetCursorXY (orgx
, orgy
);
304 ConOutPrintf (_T("%s"), str
);
305 GetCursorXY (&curx
, &cury
);
311 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
312 /* expand current file name */
313 if ((current
== charcount
) ||
314 (current
== charcount
- 1 &&
315 str
[current
] == _T('"'))) /* only works at end of line*/
317 if (wLastKey
!= VK_TAB
)
319 /* if first TAB, complete filename*/
320 tempscreen
= charcount
;
321 CompleteFilename (str
, charcount
);
322 charcount
= _tcslen (str
);
325 SetCursorXY (orgx
, orgy
);
326 ConOutPrintf (_T("%s"), str
);
328 if (tempscreen
> charcount
)
330 GetCursorXY (&curx
, &cury
);
331 for (count
= tempscreen
- charcount
; count
--; )
332 ConOutChar (_T(' '));
333 SetCursorXY (curx
, cury
);
337 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
338 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
341 /* set cursor position */
342 SetCursorXY ((orgx
+ current
) % maxx
,
343 orgy
+ (orgx
+ current
) / maxx
);
344 GetCursorXY (&curx
, &cury
);
348 /*if second TAB, list matches*/
349 if (ShowCompletionMatches (str
, charcount
))
352 GetCursorXY (&orgx
, &orgy
);
353 ConOutPrintf (_T("%s"), str
);
355 /* set cursor position */
356 SetCursorXY ((orgx
+ current
) % maxx
,
357 orgy
+ (orgx
+ current
) / maxx
);
358 GetCursorXY (&curx
, &cury
);
368 #ifdef FEATURE_4NT_FILENAME_COMPLETION
370 /* used to later see if we went down to the next line */
371 tempscreen
= charcount
;
374 /* str is the whole things that is on the current line
375 that is and and out. arg 2 is weather it goes back
376 one file or forward one file */
377 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
378 /* Attempt to clear the line */
379 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
382 current
= charcount
= 0;
385 /* Everything is deleted, lets add it back in */
388 /* Figure out where cusor is going to be after we print it */
389 charcount
= _tcslen (str
);
392 SetCursorXY (orgx
, orgy
);
393 /* Print out what we have now */
394 ConOutPrintf (_T("%s"), str
);
396 /* Move cursor accordingly */
397 if(tempscreen
> charcount
)
399 GetCursorXY (&curx
, &cury
);
400 for(count
= tempscreen
- charcount
; count
--; )
401 ConOutChar (_T(' '));
402 SetCursorXY (curx
, cury
);
406 if(((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
407 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
409 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
410 GetCursorXY(&curx
, &cury
);
417 /* ^M does the same as return */
419 if(!(ir
.Event
.KeyEvent
.dwControlKeyState
&
420 (RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
)))
426 /* end input, return to main */
427 #ifdef FEATURE_HISTORY
428 /* add to the history */
433 ConOutChar (_T('\n'));
438 /* clear str Make this callable! */
439 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
442 current
= charcount
= 0;
445 #ifdef FEATURE_HISTORY
447 History_move_to_bottom();
450 #ifdef FEATURE_HISTORY
451 /* get previous command from buffer */
452 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
454 current
= charcount
= _tcslen (str
);
455 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
456 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
457 ConOutPrintf (_T("%s"), str
);
458 GetCursorXY (&curx
, &cury
);
463 #ifdef FEATURE_HISTORY
464 /* get next command from buffer */
465 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
467 current
= charcount
= _tcslen (str
);
468 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
469 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
470 ConOutPrintf (_T("%s"), str
);
471 GetCursorXY (&curx
, &cury
);
476 /* move cursor left */
480 if (GetCursorX () == 0)
482 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
488 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
499 /* move cursor right */
500 if (current
!= charcount
)
503 if (GetCursorX () == maxx
- 1)
505 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
511 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
518 /* This input is just a normal char */
523 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
524 if ((ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
526 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
527 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
528 #endif /* _UNICODE */
530 /* insert character into string... */
531 if (bInsert
&& current
!= charcount
)
533 /* If this character insertion will cause screen scrolling,
534 * adjust the saved origin of the command prompt. */
535 tempscreen
= _tcslen(str
+ current
) + curx
;
536 if ((tempscreen
% maxx
) == (maxx
- 1) &&
537 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
543 for (count
= charcount
; count
> current
; count
--)
544 str
[count
] = str
[count
- 1];
546 if (curx
== maxx
- 1)
550 ConOutPrintf (_T("%s"), &str
[current
- 1]);
551 SetCursorXY (curx
, cury
);
556 if (current
== charcount
)
559 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
561 if (GetCursorX () == maxx
- 1)
569 wLastKey
= ir
.Event
.KeyEvent
.wVirtualKeyCode
;
573 SetCursorType (bInsert
, TRUE
);