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.
110 * global command line insert/overwrite flag
112 static BOOL bInsert
= TRUE
;
116 ClearCommandLine (LPTSTR str
, INT maxlen
, SHORT orgx
, SHORT orgy
)
120 SetCursorXY (orgx
, orgy
);
121 for (count
= 0; count
< (INT
)_tcslen (str
); count
++)
122 ConOutChar (_T(' '));
123 _tcsnset (str
, _T('\0'), maxlen
);
124 SetCursorXY (orgx
, orgy
);
128 /* read in a command line */
129 BOOL
ReadCommand (LPTSTR str
, INT maxlen
)
131 CONSOLE_SCREEN_BUFFER_INFO csbi
;
132 SHORT orgx
; /* origin x/y */
134 SHORT curx
; /*current x/y cursor position*/
137 INT count
; /*used in some for loops*/
138 INT current
= 0; /*the position of the cursor in the string (str)*/
139 INT charcount
= 0;/*chars in the string (str)*/
143 BOOL bContinue
=FALSE
;/*is TRUE the second case will not be executed*/
144 BOOL bReturn
= FALSE
;
146 #ifdef FEATURE_4NT_FILENAME_COMPLETION
147 TCHAR szPath
[MAX_PATH
];
149 #ifdef FEATURE_HISTORY
153 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &csbi
))
156 HANDLE hStdin
= GetStdHandle(STD_INPUT_HANDLE
);
161 if (!ReadFile(hStdin
, &chr
, 1, &dwRead
, NULL
) || !dwRead
)
164 MultiByteToWideChar(InputCodePage
, 0, &chr
, 1, &str
[charcount
++], 1);
166 } while (chr
!= '\n' && charcount
< maxlen
);
167 str
[charcount
] = _T('\0');
171 /* get screen size */
172 maxx
= csbi
.dwSize
.X
;
173 maxy
= csbi
.dwSize
.Y
;
175 curx
= orgx
= csbi
.dwCursorPosition
.X
;
176 cury
= orgy
= csbi
.dwCursorPosition
.Y
;
178 memset (str
, 0, maxlen
* sizeof (TCHAR
));
180 SetCursorType (bInsert
, TRUE
);
189 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
190 (RIGHT_ALT_PRESSED
|LEFT_ALT_PRESSED
|
191 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
194 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
197 #ifdef FEATURE_HISTORY
200 /*add the current command line to the history*/
201 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
202 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
208 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
209 current
= charcount
= 0;
217 /*delete current history entry*/
218 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
219 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
221 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
222 History_del_current_entry(str
);
223 current
= charcount
= _tcslen (str
);
224 ConOutPrintf (_T("%s"), str
);
225 GetCursorXY (&curx
, &cury
);
230 #endif/*FEATURE_HISTORY*/
240 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
243 /* <BACKSPACE> - delete character to left of cursor */
244 if (current
> 0 && charcount
> 0)
246 if (current
== charcount
)
248 /* if at end of line */
249 str
[current
- 1] = _T('\0');
250 if (GetCursorX () != 0)
252 ConOutPrintf (_T("\b \b"));
257 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
258 ConOutChar (_T(' '));
259 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
266 for (count
= current
- 1; count
< charcount
; count
++)
267 str
[count
] = str
[count
+ 1];
268 if (GetCursorX () != 0)
270 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
275 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
279 GetCursorXY (&curx
, &cury
);
280 ConOutPrintf (_T("%s "), &str
[current
- 1]);
281 SetCursorXY (curx
, cury
);
289 /* toggle insert/overstrike mode */
291 SetCursorType (bInsert
, TRUE
);
295 /* delete character under cursor */
296 if (current
!= charcount
&& charcount
> 0)
298 for (count
= current
; count
< charcount
; count
++)
299 str
[count
] = str
[count
+ 1];
301 GetCursorXY (&curx
, &cury
);
302 ConOutPrintf (_T("%s "), &str
[current
]);
303 SetCursorXY (curx
, cury
);
308 /* goto beginning of string */
311 SetCursorXY (orgx
, orgy
);
319 /* goto end of string */
320 if (current
!= charcount
)
322 SetCursorXY (orgx
, orgy
);
323 ConOutPrintf (_T("%s"), str
);
324 GetCursorXY (&curx
, &cury
);
330 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
331 /* expand current file name */
332 if ((current
== charcount
) ||
333 (current
== charcount
- 1 &&
334 str
[current
] == _T('"'))) /* only works at end of line*/
336 if (wLastKey
!= VK_TAB
)
338 /* if first TAB, complete filename*/
339 tempscreen
= charcount
;
340 CompleteFilename (str
, charcount
);
341 charcount
= _tcslen (str
);
344 SetCursorXY (orgx
, orgy
);
345 ConOutPrintf (_T("%s"), str
);
347 if (tempscreen
> charcount
)
349 GetCursorXY (&curx
, &cury
);
350 for (count
= tempscreen
- charcount
; count
--; )
351 ConOutChar (_T(' '));
352 SetCursorXY (curx
, cury
);
356 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
357 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
360 /* set cursor position */
361 SetCursorXY ((orgx
+ current
) % maxx
,
362 orgy
+ (orgx
+ current
) / maxx
);
363 GetCursorXY (&curx
, &cury
);
367 /*if second TAB, list matches*/
368 if (ShowCompletionMatches (str
, charcount
))
371 GetCursorXY (&orgx
, &orgy
);
372 ConOutPrintf (_T("%s"), str
);
374 /* set cursor position */
375 SetCursorXY ((orgx
+ current
) % maxx
,
376 orgy
+ (orgx
+ current
) / maxx
);
377 GetCursorXY (&curx
, &cury
);
387 #ifdef FEATURE_4NT_FILENAME_COMPLETION
389 /* used to later see if we went down to the next line */
390 tempscreen
= charcount
;
393 /* str is the whole things that is on the current line
394 that is and and out. arg 2 is weather it goes back
395 one file or forward one file */
396 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
397 /* Attempt to clear the line */
398 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
401 current
= charcount
= 0;
403 /* Everything is deleted, lets add it back in */
406 /* Figure out where cusor is going to be after we print it */
407 charcount
= _tcslen (str
);
410 SetCursorXY (orgx
, orgy
);
411 /* Print out what we have now */
412 ConOutPrintf (_T("%s"), str
);
414 /* Move cursor accordingly */
415 if(tempscreen
> charcount
)
417 GetCursorXY (&curx
, &cury
);
418 for(count
= tempscreen
- charcount
; count
--; )
419 ConOutChar (_T(' '));
420 SetCursorXY (curx
, cury
);
424 if(((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
425 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
427 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
428 GetCursorXY(&curx
, &cury
);
435 /* ^M does the same as return */
437 if(!(ir
.Event
.KeyEvent
.dwControlKeyState
&
438 (RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
)))
444 /* end input, return to main */
445 #ifdef FEATURE_HISTORY
446 /* add to the history */
450 str
[charcount
++] = _T('\n');
451 str
[charcount
] = _T('\0');
452 ConOutChar (_T('\n'));
457 /* clear str Make this callable! */
458 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
461 current
= charcount
= 0;
464 #ifdef FEATURE_HISTORY
466 History_move_to_bottom();
469 #ifdef FEATURE_HISTORY
470 /* get previous command from buffer */
471 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
473 current
= charcount
= _tcslen (str
);
474 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
475 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
476 ConOutPrintf (_T("%s"), str
);
477 GetCursorXY (&curx
, &cury
);
482 #ifdef FEATURE_HISTORY
483 /* get next command from buffer */
484 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
486 current
= charcount
= _tcslen (str
);
487 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
488 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
489 ConOutPrintf (_T("%s"), str
);
490 GetCursorXY (&curx
, &cury
);
495 /* move cursor left */
499 if (GetCursorX () == 0)
501 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
507 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
518 /* move cursor right */
519 if (current
!= charcount
)
522 if (GetCursorX () == maxx
- 1)
524 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
530 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
534 #ifdef FEATURE_HISTORY
537 LPCTSTR last
= PeekHistory(-1);
538 if (last
&& charcount
< (INT
)_tcslen (last
))
540 PreviousChar
= last
[current
];
541 ConOutChar(PreviousChar
);
542 GetCursorXY(&curx
, &cury
);
543 str
[current
++] = PreviousChar
;
551 /* This input is just a normal char */
556 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
557 if (ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
559 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
560 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
561 #endif /* _UNICODE */
563 /* insert character into string... */
564 if (bInsert
&& current
!= charcount
)
566 /* If this character insertion will cause screen scrolling,
567 * adjust the saved origin of the command prompt. */
568 tempscreen
= _tcslen(str
+ current
) + curx
;
569 if ((tempscreen
% maxx
) == (maxx
- 1) &&
570 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
576 for (count
= charcount
; count
> current
; count
--)
577 str
[count
] = str
[count
- 1];
579 if (curx
== maxx
- 1)
583 ConOutPrintf (_T("%s"), &str
[current
- 1]);
584 SetCursorXY (curx
, cury
);
589 if (current
== charcount
)
592 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
594 if (GetCursorX () == maxx
- 1)
602 wLastKey
= ir
.Event
.KeyEvent
.wVirtualKeyCode
;
606 SetCursorType (bInsert
, TRUE
);
608 #ifdef FEATURE_ALIASES
609 /* expand all aliases */
610 ExpandAlias (str
, maxlen
);
611 #endif /* FEATURE_ALIAS */