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 preempted 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 carriage return output to better match MSDOS with echo
80 * on or off.(marked with "JPP 19980708")
82 * 13-Dec-1998 (Eric Kohl)
83 * Added insert/overwrite cursor.
85 * 25-Jan-1998 (Eric Kohl)
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)
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.
99 * 28-September-2007 (Hervé Poussineau)
100 * Added history possibilities to right key.
106 * See https://technet.microsoft.com/en-us/library/cc978715.aspx
107 * and https://technet.microsoft.com/en-us/library/cc940805.aspx
108 * to know the differences between those two settings.
109 * Values 0x00, 0x0D (carriage return) and 0x20 (space) disable completion.
111 TCHAR AutoCompletionChar
= _T('\t'); // Default is 0x20
112 TCHAR PathCompletionChar
= _T('\t'); // Default is 0x20
119 * global command line insert/overwrite flag
121 static BOOL bInsert
= TRUE
;
125 ClearCommandLine(LPTSTR str
, INT maxlen
, SHORT orgx
, SHORT orgy
)
129 SetCursorXY (orgx
, orgy
);
130 for (count
= 0; count
< (INT
)_tcslen (str
); count
++)
131 ConOutChar (_T(' '));
132 _tcsnset (str
, _T('\0'), maxlen
);
133 SetCursorXY (orgx
, orgy
);
137 /* read in a command line */
138 BOOL
ReadCommand(LPTSTR str
, INT maxlen
)
140 CONSOLE_SCREEN_BUFFER_INFO csbi
;
141 SHORT orgx
; /* origin x/y */
143 SHORT curx
; /*current x/y cursor position*/
146 INT count
; /*used in some for loops*/
147 INT current
= 0; /*the position of the cursor in the string (str)*/
148 INT charcount
= 0;/*chars in the string (str)*/
150 DWORD dwControlKeyState
;
151 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
155 BOOL bReturn
= FALSE
;
157 #ifdef FEATURE_4NT_FILENAME_COMPLETION
158 TCHAR szPath
[MAX_PATH
];
160 #ifdef FEATURE_HISTORY
161 //BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/
165 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &csbi
))
168 HANDLE hStdin
= GetStdHandle(STD_INPUT_HANDLE
);
173 if (!ReadFile(hStdin
, &chr
, 1, &dwRead
, NULL
) || !dwRead
)
176 MultiByteToWideChar(InputCodePage
, 0, &chr
, 1, &str
[charcount
++], 1);
178 } while (chr
!= '\n' && charcount
< maxlen
);
179 str
[charcount
] = _T('\0');
183 /* get screen size */
184 maxx
= csbi
.dwSize
.X
;
185 maxy
= csbi
.dwSize
.Y
;
187 curx
= orgx
= csbi
.dwCursorPosition
.X
;
188 cury
= orgy
= csbi
.dwCursorPosition
.Y
;
190 memset (str
, 0, maxlen
* sizeof (TCHAR
));
192 SetCursorType (bInsert
, TRUE
);
199 dwControlKeyState
= ir
.Event
.KeyEvent
.dwControlKeyState
;
201 if (dwControlKeyState
&
202 (RIGHT_ALT_PRESSED
|LEFT_ALT_PRESSED
|
203 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
205 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
207 #ifdef FEATURE_HISTORY
209 /*add the current command line to the history*/
210 if (dwControlKeyState
&
211 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
216 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
217 current
= charcount
= 0;
225 /*delete current history entry*/
226 if (dwControlKeyState
&
227 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
229 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
230 History_del_current_entry(str
);
231 current
= charcount
= _tcslen (str
);
232 ConOutPrintf (_T("%s"), str
);
233 GetCursorXY (&curx
, &cury
);
238 #endif /*FEATURE_HISTORY*/
244 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
247 /* <BACKSPACE> - delete character to left of cursor */
248 if (current
> 0 && charcount
> 0)
250 if (current
== charcount
)
252 /* if at end of line */
253 str
[current
- 1] = _T('\0');
254 if (GetCursorX () != 0)
256 ConOutPrintf (_T("\b \b"));
261 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
262 ConOutChar (_T(' '));
263 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
270 for (count
= current
- 1; count
< charcount
; count
++)
271 str
[count
] = str
[count
+ 1];
272 if (GetCursorX () != 0)
274 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
279 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
283 GetCursorXY (&curx
, &cury
);
284 ConOutPrintf (_T("%s "), &str
[current
- 1]);
285 SetCursorXY (curx
, cury
);
293 /* toggle insert/overstrike mode */
295 SetCursorType (bInsert
, TRUE
);
299 /* delete character under cursor */
300 if (current
!= charcount
&& charcount
> 0)
302 for (count
= current
; count
< charcount
; count
++)
303 str
[count
] = str
[count
+ 1];
305 GetCursorXY (&curx
, &cury
);
306 ConOutPrintf (_T("%s "), &str
[current
]);
307 SetCursorXY (curx
, cury
);
312 /* goto beginning of string */
315 SetCursorXY (orgx
, orgy
);
323 /* goto end of string */
324 if (current
!= charcount
)
326 SetCursorXY (orgx
, orgy
);
327 ConOutPrintf (_T("%s"), str
);
328 GetCursorXY (&curx
, &cury
);
334 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
335 /* expand current file name */
336 if ((current
== charcount
) ||
337 (current
== charcount
- 1 &&
338 str
[current
] == _T('"'))) /* only works at end of line*/
340 if (wLastKey
!= VK_TAB
)
342 /* if first TAB, complete filename*/
343 tempscreen
= charcount
;
344 CompleteFilename (str
, charcount
);
345 charcount
= _tcslen (str
);
348 SetCursorXY (orgx
, orgy
);
349 ConOutPrintf (_T("%s"), str
);
351 if (tempscreen
> charcount
)
353 GetCursorXY (&curx
, &cury
);
354 for (count
= tempscreen
- charcount
; count
--; )
355 ConOutChar (_T(' '));
356 SetCursorXY (curx
, cury
);
360 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
361 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
364 /* set cursor position */
365 SetCursorXY ((orgx
+ current
) % maxx
,
366 orgy
+ (orgx
+ current
) / maxx
);
367 GetCursorXY (&curx
, &cury
);
371 /*if second TAB, list matches*/
372 if (ShowCompletionMatches (str
, charcount
))
375 GetCursorXY(&orgx
, &orgy
);
376 ConOutPrintf(_T("%s"), str
);
378 /* set cursor position */
379 SetCursorXY((orgx
+ current
) % maxx
,
380 orgy
+ (orgx
+ current
) / maxx
);
381 GetCursorXY(&curx
, &cury
);
391 #ifdef FEATURE_4NT_FILENAME_COMPLETION
392 /* used to later see if we went down to the next line */
393 tempscreen
= charcount
;
396 /* str is the whole things that is on the current line
397 that is and and out. arg 2 is weather it goes back
398 one file or forward one file */
399 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
400 /* Attempt to clear the line */
401 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
404 current
= charcount
= 0;
406 /* Everything is deleted, lets add it back in */
409 /* Figure out where cusor is going to be after we print it */
410 charcount
= _tcslen(str
);
413 SetCursorXY(orgx
, orgy
);
414 /* Print out what we have now */
415 ConOutPrintf(_T("%s"), str
);
417 /* Move cursor accordingly */
418 if (tempscreen
> charcount
)
420 GetCursorXY(&curx
, &cury
);
421 for(count
= tempscreen
- charcount
; count
--; )
423 SetCursorXY(curx
, cury
);
427 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
428 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
430 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
431 GetCursorXY(&curx
, &cury
);
437 /* ^M does the same as return */
439 if (!(ir
.Event
.KeyEvent
.dwControlKeyState
&
440 (RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
)))
446 /* end input, return to main */
447 #ifdef FEATURE_HISTORY
448 /* add to the history */
452 str
[charcount
++] = _T('\n');
453 str
[charcount
] = _T('\0');
454 ConOutChar(_T('\n'));
459 /* clear str Make this callable! */
460 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
463 current
= charcount
= 0;
466 #ifdef FEATURE_HISTORY
468 History_move_to_bottom();
471 #ifdef FEATURE_HISTORY
472 /* get previous command from buffer */
473 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
475 current
= charcount
= _tcslen (str
);
476 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
477 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
478 ConOutPrintf (_T("%s"), str
);
479 GetCursorXY (&curx
, &cury
);
484 #ifdef FEATURE_HISTORY
485 /* get next command from buffer */
486 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
488 current
= charcount
= _tcslen (str
);
489 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
490 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
491 ConOutPrintf (_T("%s"), str
);
492 GetCursorXY (&curx
, &cury
);
497 if (dwControlKeyState
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
499 /* move cursor to the previous word */
502 while (current
> 0 && str
[current
- 1] == _T(' '))
516 while (current
> 0 && str
[current
-1] != _T(' '))
530 SetCursorXY(curx
, cury
);
535 /* move cursor left */
539 if (GetCursorX () == 0)
541 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
547 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
559 if (dwControlKeyState
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
561 /* move cursor to the next word */
562 if (current
!= charcount
)
564 while (current
!= charcount
&& str
[current
] != _T(' '))
567 if (curx
== maxx
- 1)
578 while (current
!= charcount
&& str
[current
] == _T(' '))
581 if (curx
== maxx
- 1)
592 SetCursorXY(curx
, cury
);
597 /* move cursor right */
598 if (current
!= charcount
)
601 if (GetCursorX () == maxx
- 1)
603 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
609 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
613 #ifdef FEATURE_HISTORY
616 LPCTSTR last
= PeekHistory(-1);
617 if (last
&& charcount
< (INT
)_tcslen (last
))
619 PreviousChar
= last
[current
];
620 ConOutChar(PreviousChar
);
621 GetCursorXY(&curx
, &cury
);
622 str
[current
++] = PreviousChar
;
631 /* This input is just a normal char */
636 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
637 if (ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
639 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
640 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
641 #endif /* _UNICODE */
643 /* insert character into string... */
644 if (bInsert
&& current
!= charcount
)
646 /* If this character insertion will cause screen scrolling,
647 * adjust the saved origin of the command prompt. */
648 tempscreen
= _tcslen(str
+ current
) + curx
;
649 if ((tempscreen
% maxx
) == (maxx
- 1) &&
650 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
656 for (count
= charcount
; count
> current
; count
--)
657 str
[count
] = str
[count
- 1];
659 if (curx
== maxx
- 1)
663 ConOutPrintf (_T("%s"), &str
[current
- 1]);
664 SetCursorXY (curx
, cury
);
669 if (current
== charcount
)
672 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
674 if (GetCursorX () == maxx
- 1)
682 //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode;
686 SetCursorType (bInsert
, TRUE
);
688 #ifdef FEATURE_ALIASES
689 /* expand all aliases */
690 ExpandAlias (str
, maxlen
);
691 #endif /* FEATURE_ALIAS */