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)
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.
109 * global command line insert/overwrite flag
111 static BOOL bInsert
= TRUE
;
115 ClearCommandLine(LPTSTR str
, INT maxlen
, SHORT orgx
, SHORT orgy
)
119 SetCursorXY (orgx
, orgy
);
120 for (count
= 0; count
< (INT
)_tcslen (str
); count
++)
121 ConOutChar (_T(' '));
122 _tcsnset (str
, _T('\0'), maxlen
);
123 SetCursorXY (orgx
, orgy
);
127 /* read in a command line */
128 BOOL
ReadCommand(LPTSTR str
, INT maxlen
)
130 CONSOLE_SCREEN_BUFFER_INFO csbi
;
131 SHORT orgx
; /* origin x/y */
133 SHORT curx
; /*current x/y cursor position*/
136 INT count
; /*used in some for loops*/
137 INT current
= 0; /*the position of the cursor in the string (str)*/
138 INT charcount
= 0;/*chars in the string (str)*/
140 DWORD dwControlKeyState
;
141 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
145 BOOL bReturn
= FALSE
;
147 #ifdef FEATURE_4NT_FILENAME_COMPLETION
148 TCHAR szPath
[MAX_PATH
];
150 #ifdef FEATURE_HISTORY
151 //BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/
155 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &csbi
))
158 HANDLE hStdin
= GetStdHandle(STD_INPUT_HANDLE
);
163 if (!ReadFile(hStdin
, &chr
, 1, &dwRead
, NULL
) || !dwRead
)
166 MultiByteToWideChar(InputCodePage
, 0, &chr
, 1, &str
[charcount
++], 1);
168 } while (chr
!= '\n' && charcount
< maxlen
);
169 str
[charcount
] = _T('\0');
173 /* get screen size */
174 maxx
= csbi
.dwSize
.X
;
175 maxy
= csbi
.dwSize
.Y
;
177 curx
= orgx
= csbi
.dwCursorPosition
.X
;
178 cury
= orgy
= csbi
.dwCursorPosition
.Y
;
180 memset (str
, 0, maxlen
* sizeof (TCHAR
));
182 SetCursorType (bInsert
, TRUE
);
189 dwControlKeyState
= ir
.Event
.KeyEvent
.dwControlKeyState
;
191 if (dwControlKeyState
&
192 (RIGHT_ALT_PRESSED
|LEFT_ALT_PRESSED
|
193 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
195 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
197 #ifdef FEATURE_HISTORY
199 /*add the current command line to the history*/
200 if (dwControlKeyState
&
201 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
206 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
207 current
= charcount
= 0;
215 /*delete current history entry*/
216 if (dwControlKeyState
&
217 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
219 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
220 History_del_current_entry(str
);
221 current
= charcount
= _tcslen (str
);
222 ConOutPrintf (_T("%s"), str
);
223 GetCursorXY (&curx
, &cury
);
228 #endif /*FEATURE_HISTORY*/
234 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
237 /* <BACKSPACE> - delete character to left of cursor */
238 if (current
> 0 && charcount
> 0)
240 if (current
== charcount
)
242 /* if at end of line */
243 str
[current
- 1] = _T('\0');
244 if (GetCursorX () != 0)
246 ConOutPrintf (_T("\b \b"));
251 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
252 ConOutChar (_T(' '));
253 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
260 for (count
= current
- 1; count
< charcount
; count
++)
261 str
[count
] = str
[count
+ 1];
262 if (GetCursorX () != 0)
264 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
269 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
273 GetCursorXY (&curx
, &cury
);
274 ConOutPrintf (_T("%s "), &str
[current
- 1]);
275 SetCursorXY (curx
, cury
);
283 /* toggle insert/overstrike mode */
285 SetCursorType (bInsert
, TRUE
);
289 /* delete character under cursor */
290 if (current
!= charcount
&& charcount
> 0)
292 for (count
= current
; count
< charcount
; count
++)
293 str
[count
] = str
[count
+ 1];
295 GetCursorXY (&curx
, &cury
);
296 ConOutPrintf (_T("%s "), &str
[current
]);
297 SetCursorXY (curx
, cury
);
302 /* goto beginning of string */
305 SetCursorXY (orgx
, orgy
);
313 /* goto end of string */
314 if (current
!= charcount
)
316 SetCursorXY (orgx
, orgy
);
317 ConOutPrintf (_T("%s"), str
);
318 GetCursorXY (&curx
, &cury
);
324 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
325 /* expand current file name */
326 if ((current
== charcount
) ||
327 (current
== charcount
- 1 &&
328 str
[current
] == _T('"'))) /* only works at end of line*/
330 if (wLastKey
!= VK_TAB
)
332 /* if first TAB, complete filename*/
333 tempscreen
= charcount
;
334 CompleteFilename (str
, charcount
);
335 charcount
= _tcslen (str
);
338 SetCursorXY (orgx
, orgy
);
339 ConOutPrintf (_T("%s"), str
);
341 if (tempscreen
> charcount
)
343 GetCursorXY (&curx
, &cury
);
344 for (count
= tempscreen
- charcount
; count
--; )
345 ConOutChar (_T(' '));
346 SetCursorXY (curx
, cury
);
350 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
351 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
354 /* set cursor position */
355 SetCursorXY ((orgx
+ current
) % maxx
,
356 orgy
+ (orgx
+ current
) / maxx
);
357 GetCursorXY (&curx
, &cury
);
361 /*if second TAB, list matches*/
362 if (ShowCompletionMatches (str
, charcount
))
365 GetCursorXY(&orgx
, &orgy
);
366 ConOutPrintf(_T("%s"), str
);
368 /* set cursor position */
369 SetCursorXY((orgx
+ current
) % maxx
,
370 orgy
+ (orgx
+ current
) / maxx
);
371 GetCursorXY(&curx
, &cury
);
381 #ifdef FEATURE_4NT_FILENAME_COMPLETION
382 /* used to later see if we went down to the next line */
383 tempscreen
= charcount
;
386 /* str is the whole things that is on the current line
387 that is and and out. arg 2 is weather it goes back
388 one file or forward one file */
389 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
390 /* Attempt to clear the line */
391 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
394 current
= charcount
= 0;
396 /* Everything is deleted, lets add it back in */
399 /* Figure out where cusor is going to be after we print it */
400 charcount
= _tcslen(str
);
403 SetCursorXY(orgx
, orgy
);
404 /* Print out what we have now */
405 ConOutPrintf(_T("%s"), str
);
407 /* Move cursor accordingly */
408 if (tempscreen
> charcount
)
410 GetCursorXY(&curx
, &cury
);
411 for(count
= tempscreen
- charcount
; count
--; )
413 SetCursorXY(curx
, cury
);
417 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
418 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
420 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
421 GetCursorXY(&curx
, &cury
);
427 /* ^M does the same as return */
429 if (!(ir
.Event
.KeyEvent
.dwControlKeyState
&
430 (RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
)))
436 /* end input, return to main */
437 #ifdef FEATURE_HISTORY
438 /* add to the history */
442 str
[charcount
++] = _T('\n');
443 str
[charcount
] = _T('\0');
444 ConOutChar(_T('\n'));
449 /* clear str Make this callable! */
450 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
453 current
= charcount
= 0;
456 #ifdef FEATURE_HISTORY
458 History_move_to_bottom();
461 #ifdef FEATURE_HISTORY
462 /* get previous command from buffer */
463 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
465 current
= charcount
= _tcslen (str
);
466 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
467 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
468 ConOutPrintf (_T("%s"), str
);
469 GetCursorXY (&curx
, &cury
);
474 #ifdef FEATURE_HISTORY
475 /* get next command from buffer */
476 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
478 current
= charcount
= _tcslen (str
);
479 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
480 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
481 ConOutPrintf (_T("%s"), str
);
482 GetCursorXY (&curx
, &cury
);
487 if (dwControlKeyState
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
489 /* move cursor to the previous word */
492 while (current
> 0 && str
[current
- 1] == _T(' '))
506 while (current
> 0 && str
[current
-1] != _T(' '))
520 SetCursorXY(curx
, cury
);
525 /* move cursor left */
529 if (GetCursorX () == 0)
531 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
537 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
549 if (dwControlKeyState
& (RIGHT_CTRL_PRESSED
| LEFT_CTRL_PRESSED
))
551 /* move cursor to the next word */
552 if (current
!= charcount
)
554 while (current
!= charcount
&& str
[current
] != _T(' '))
557 if (curx
== maxx
- 1)
568 while (current
!= charcount
&& str
[current
] == _T(' '))
571 if (curx
== maxx
- 1)
582 SetCursorXY(curx
, cury
);
587 /* move cursor right */
588 if (current
!= charcount
)
591 if (GetCursorX () == maxx
- 1)
593 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
599 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
603 #ifdef FEATURE_HISTORY
606 LPCTSTR last
= PeekHistory(-1);
607 if (last
&& charcount
< (INT
)_tcslen (last
))
609 PreviousChar
= last
[current
];
610 ConOutChar(PreviousChar
);
611 GetCursorXY(&curx
, &cury
);
612 str
[current
++] = PreviousChar
;
621 /* This input is just a normal char */
626 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
627 if (ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
629 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
630 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
631 #endif /* _UNICODE */
633 /* insert character into string... */
634 if (bInsert
&& current
!= charcount
)
636 /* If this character insertion will cause screen scrolling,
637 * adjust the saved origin of the command prompt. */
638 tempscreen
= _tcslen(str
+ current
) + curx
;
639 if ((tempscreen
% maxx
) == (maxx
- 1) &&
640 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
646 for (count
= charcount
; count
> current
; count
--)
647 str
[count
] = str
[count
- 1];
649 if (curx
== maxx
- 1)
653 ConOutPrintf (_T("%s"), &str
[current
- 1]);
654 SetCursorXY (curx
, cury
);
659 if (current
== charcount
)
662 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
664 if (GetCursorX () == maxx
- 1)
672 //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode;
676 SetCursorType (bInsert
, TRUE
);
678 #ifdef FEATURE_ALIASES
679 /* expand all aliases */
680 ExpandAlias (str
, maxlen
);
681 #endif /* FEATURE_ALIAS */