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 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
144 BOOL bReturn
= FALSE
;
146 #ifdef FEATURE_4NT_FILENAME_COMPLETION
147 TCHAR szPath
[MAX_PATH
];
149 #ifdef FEATURE_HISTORY
150 //BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/
154 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &csbi
))
157 HANDLE hStdin
= GetStdHandle(STD_INPUT_HANDLE
);
162 if (!ReadFile(hStdin
, &chr
, 1, &dwRead
, NULL
) || !dwRead
)
165 MultiByteToWideChar(InputCodePage
, 0, &chr
, 1, &str
[charcount
++], 1);
167 } while (chr
!= '\n' && charcount
< maxlen
);
168 str
[charcount
] = _T('\0');
172 /* get screen size */
173 maxx
= csbi
.dwSize
.X
;
174 maxy
= csbi
.dwSize
.Y
;
176 curx
= orgx
= csbi
.dwCursorPosition
.X
;
177 cury
= orgy
= csbi
.dwCursorPosition
.Y
;
179 memset (str
, 0, maxlen
* sizeof (TCHAR
));
181 SetCursorType (bInsert
, TRUE
);
190 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
191 (RIGHT_ALT_PRESSED
|LEFT_ALT_PRESSED
|
192 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
195 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
198 #ifdef FEATURE_HISTORY
201 /*add the current command line to the history*/
202 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
203 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
209 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
210 current
= charcount
= 0;
218 /*delete current history entry*/
219 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
220 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
222 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
223 History_del_current_entry(str
);
224 current
= charcount
= _tcslen (str
);
225 ConOutPrintf (_T("%s"), str
);
226 GetCursorXY (&curx
, &cury
);
231 #endif/*FEATURE_HISTORY*/
241 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
244 /* <BACKSPACE> - delete character to left of cursor */
245 if (current
> 0 && charcount
> 0)
247 if (current
== charcount
)
249 /* if at end of line */
250 str
[current
- 1] = _T('\0');
251 if (GetCursorX () != 0)
253 ConOutPrintf (_T("\b \b"));
258 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
259 ConOutChar (_T(' '));
260 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
267 for (count
= current
- 1; count
< charcount
; count
++)
268 str
[count
] = str
[count
+ 1];
269 if (GetCursorX () != 0)
271 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
276 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
280 GetCursorXY (&curx
, &cury
);
281 ConOutPrintf (_T("%s "), &str
[current
- 1]);
282 SetCursorXY (curx
, cury
);
290 /* toggle insert/overstrike mode */
292 SetCursorType (bInsert
, TRUE
);
296 /* delete character under cursor */
297 if (current
!= charcount
&& charcount
> 0)
299 for (count
= current
; count
< charcount
; count
++)
300 str
[count
] = str
[count
+ 1];
302 GetCursorXY (&curx
, &cury
);
303 ConOutPrintf (_T("%s "), &str
[current
]);
304 SetCursorXY (curx
, cury
);
309 /* goto beginning of string */
312 SetCursorXY (orgx
, orgy
);
320 /* goto end of string */
321 if (current
!= charcount
)
323 SetCursorXY (orgx
, orgy
);
324 ConOutPrintf (_T("%s"), str
);
325 GetCursorXY (&curx
, &cury
);
331 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
332 /* expand current file name */
333 if ((current
== charcount
) ||
334 (current
== charcount
- 1 &&
335 str
[current
] == _T('"'))) /* only works at end of line*/
337 if (wLastKey
!= VK_TAB
)
339 /* if first TAB, complete filename*/
340 tempscreen
= charcount
;
341 CompleteFilename (str
, charcount
);
342 charcount
= _tcslen (str
);
345 SetCursorXY (orgx
, orgy
);
346 ConOutPrintf (_T("%s"), str
);
348 if (tempscreen
> charcount
)
350 GetCursorXY (&curx
, &cury
);
351 for (count
= tempscreen
- charcount
; count
--; )
352 ConOutChar (_T(' '));
353 SetCursorXY (curx
, cury
);
357 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
358 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
361 /* set cursor position */
362 SetCursorXY ((orgx
+ current
) % maxx
,
363 orgy
+ (orgx
+ current
) / maxx
);
364 GetCursorXY (&curx
, &cury
);
368 /*if second TAB, list matches*/
369 if (ShowCompletionMatches (str
, charcount
))
372 GetCursorXY (&orgx
, &orgy
);
373 ConOutPrintf (_T("%s"), str
);
375 /* set cursor position */
376 SetCursorXY ((orgx
+ current
) % maxx
,
377 orgy
+ (orgx
+ current
) / maxx
);
378 GetCursorXY (&curx
, &cury
);
388 #ifdef FEATURE_4NT_FILENAME_COMPLETION
390 /* used to later see if we went down to the next line */
391 tempscreen
= charcount
;
394 /* str is the whole things that is on the current line
395 that is and and out. arg 2 is weather it goes back
396 one file or forward one file */
397 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
398 /* Attempt to clear the line */
399 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
402 current
= charcount
= 0;
404 /* Everything is deleted, lets add it back in */
407 /* Figure out where cusor is going to be after we print it */
408 charcount
= _tcslen (str
);
411 SetCursorXY (orgx
, orgy
);
412 /* Print out what we have now */
413 ConOutPrintf (_T("%s"), str
);
415 /* Move cursor accordingly */
416 if(tempscreen
> charcount
)
418 GetCursorXY (&curx
, &cury
);
419 for(count
= tempscreen
- charcount
; count
--; )
420 ConOutChar (_T(' '));
421 SetCursorXY (curx
, cury
);
425 if(((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
426 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
428 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
429 GetCursorXY(&curx
, &cury
);
436 /* ^M does the same as return */
438 if(!(ir
.Event
.KeyEvent
.dwControlKeyState
&
439 (RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
)))
445 /* end input, return to main */
446 #ifdef FEATURE_HISTORY
447 /* add to the history */
451 str
[charcount
++] = _T('\n');
452 str
[charcount
] = _T('\0');
453 ConOutChar (_T('\n'));
458 /* clear str Make this callable! */
459 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
462 current
= charcount
= 0;
465 #ifdef FEATURE_HISTORY
467 History_move_to_bottom();
470 #ifdef FEATURE_HISTORY
471 /* get previous command from buffer */
472 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
474 current
= charcount
= _tcslen (str
);
475 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
476 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
477 ConOutPrintf (_T("%s"), str
);
478 GetCursorXY (&curx
, &cury
);
483 #ifdef FEATURE_HISTORY
484 /* get next command from buffer */
485 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
487 current
= charcount
= _tcslen (str
);
488 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
489 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
490 ConOutPrintf (_T("%s"), str
);
491 GetCursorXY (&curx
, &cury
);
496 /* move cursor left */
500 if (GetCursorX () == 0)
502 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
508 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
519 /* move cursor right */
520 if (current
!= charcount
)
523 if (GetCursorX () == maxx
- 1)
525 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
531 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
535 #ifdef FEATURE_HISTORY
538 LPCTSTR last
= PeekHistory(-1);
539 if (last
&& charcount
< (INT
)_tcslen (last
))
541 PreviousChar
= last
[current
];
542 ConOutChar(PreviousChar
);
543 GetCursorXY(&curx
, &cury
);
544 str
[current
++] = PreviousChar
;
552 /* This input is just a normal char */
557 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
558 if (ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
560 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
561 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
562 #endif /* _UNICODE */
564 /* insert character into string... */
565 if (bInsert
&& current
!= charcount
)
567 /* If this character insertion will cause screen scrolling,
568 * adjust the saved origin of the command prompt. */
569 tempscreen
= _tcslen(str
+ current
) + curx
;
570 if ((tempscreen
% maxx
) == (maxx
- 1) &&
571 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
577 for (count
= charcount
; count
> current
; count
--)
578 str
[count
] = str
[count
- 1];
580 if (curx
== maxx
- 1)
584 ConOutPrintf (_T("%s"), &str
[current
- 1]);
585 SetCursorXY (curx
, cury
);
590 if (current
== charcount
)
593 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
595 if (GetCursorX () == maxx
- 1)
603 //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode;
607 SetCursorType (bInsert
, TRUE
);
609 #ifdef FEATURE_ALIASES
610 /* expand all aliases */
611 ExpandAlias (str
, maxlen
);
612 #endif /* FEATURE_ALIAS */