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)*/
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
);
191 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
192 (RIGHT_ALT_PRESSED
|LEFT_ALT_PRESSED
|
193 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
196 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
199 #ifdef FEATURE_HISTORY
202 /*add the current command line to the history*/
203 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
204 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
210 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
211 current
= charcount
= 0;
219 /*delete current history entry*/
220 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
221 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
223 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
224 History_del_current_entry(str
);
225 current
= charcount
= _tcslen (str
);
226 ConOutPrintf (_T("%s"), str
);
227 GetCursorXY (&curx
, &cury
);
232 #endif/*FEATURE_HISTORY*/
242 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
245 /* <BACKSPACE> - delete character to left of cursor */
246 if (current
> 0 && charcount
> 0)
248 if (current
== charcount
)
250 /* if at end of line */
251 str
[current
- 1] = _T('\0');
252 if (GetCursorX () != 0)
254 ConOutPrintf (_T("\b \b"));
259 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
260 ConOutChar (_T(' '));
261 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
268 for (count
= current
- 1; count
< charcount
; count
++)
269 str
[count
] = str
[count
+ 1];
270 if (GetCursorX () != 0)
272 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
277 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
281 GetCursorXY (&curx
, &cury
);
282 ConOutPrintf (_T("%s "), &str
[current
- 1]);
283 SetCursorXY (curx
, cury
);
291 /* toggle insert/overstrike mode */
293 SetCursorType (bInsert
, TRUE
);
297 /* delete character under cursor */
298 if (current
!= charcount
&& charcount
> 0)
300 for (count
= current
; count
< charcount
; count
++)
301 str
[count
] = str
[count
+ 1];
303 GetCursorXY (&curx
, &cury
);
304 ConOutPrintf (_T("%s "), &str
[current
]);
305 SetCursorXY (curx
, cury
);
310 /* goto beginning of string */
313 SetCursorXY (orgx
, orgy
);
321 /* goto end of string */
322 if (current
!= charcount
)
324 SetCursorXY (orgx
, orgy
);
325 ConOutPrintf (_T("%s"), str
);
326 GetCursorXY (&curx
, &cury
);
332 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
333 /* expand current file name */
334 if ((current
== charcount
) ||
335 (current
== charcount
- 1 &&
336 str
[current
] == _T('"'))) /* only works at end of line*/
338 if (wLastKey
!= VK_TAB
)
340 /* if first TAB, complete filename*/
341 tempscreen
= charcount
;
342 CompleteFilename (str
, charcount
);
343 charcount
= _tcslen (str
);
346 SetCursorXY (orgx
, orgy
);
347 ConOutPrintf (_T("%s"), str
);
349 if (tempscreen
> charcount
)
351 GetCursorXY (&curx
, &cury
);
352 for (count
= tempscreen
- charcount
; count
--; )
353 ConOutChar (_T(' '));
354 SetCursorXY (curx
, cury
);
358 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
359 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
362 /* set cursor position */
363 SetCursorXY ((orgx
+ current
) % maxx
,
364 orgy
+ (orgx
+ current
) / maxx
);
365 GetCursorXY (&curx
, &cury
);
369 /*if second TAB, list matches*/
370 if (ShowCompletionMatches (str
, charcount
))
373 GetCursorXY (&orgx
, &orgy
);
374 ConOutPrintf (_T("%s"), str
);
376 /* set cursor position */
377 SetCursorXY ((orgx
+ current
) % maxx
,
378 orgy
+ (orgx
+ current
) / maxx
);
379 GetCursorXY (&curx
, &cury
);
389 #ifdef FEATURE_4NT_FILENAME_COMPLETION
391 /* used to later see if we went down to the next line */
392 tempscreen
= charcount
;
395 /* str is the whole things that is on the current line
396 that is and and out. arg 2 is weather it goes back
397 one file or forward one file */
398 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
399 /* Attempt to clear the line */
400 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
403 current
= charcount
= 0;
405 /* Everything is deleted, lets add it back in */
408 /* Figure out where cusor is going to be after we print it */
409 charcount
= _tcslen (str
);
412 SetCursorXY (orgx
, orgy
);
413 /* Print out what we have now */
414 ConOutPrintf (_T("%s"), str
);
416 /* Move cursor accordingly */
417 if(tempscreen
> charcount
)
419 GetCursorXY (&curx
, &cury
);
420 for(count
= tempscreen
- charcount
; count
--; )
421 ConOutChar (_T(' '));
422 SetCursorXY (curx
, cury
);
426 if(((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
427 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
429 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
430 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 /* move cursor left */
501 if (GetCursorX () == 0)
503 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
509 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
520 /* move cursor right */
521 if (current
!= charcount
)
524 if (GetCursorX () == maxx
- 1)
526 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
532 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
536 #ifdef FEATURE_HISTORY
539 LPCTSTR last
= PeekHistory(-1);
540 if (last
&& charcount
< (INT
)_tcslen (last
))
542 PreviousChar
= last
[current
];
543 ConOutChar(PreviousChar
);
544 GetCursorXY(&curx
, &cury
);
545 str
[current
++] = PreviousChar
;
553 /* This input is just a normal char */
558 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
559 if (ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
561 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
562 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
563 #endif /* _UNICODE */
565 /* insert character into string... */
566 if (bInsert
&& current
!= charcount
)
568 /* If this character insertion will cause screen scrolling,
569 * adjust the saved origin of the command prompt. */
570 tempscreen
= _tcslen(str
+ current
) + curx
;
571 if ((tempscreen
% maxx
) == (maxx
- 1) &&
572 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
578 for (count
= charcount
; count
> current
; count
--)
579 str
[count
] = str
[count
- 1];
581 if (curx
== maxx
- 1)
585 ConOutPrintf (_T("%s"), &str
[current
- 1]);
586 SetCursorXY (curx
, cury
);
591 if (current
== charcount
)
594 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
596 if (GetCursorX () == maxx
- 1)
604 //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode;
608 SetCursorType (bInsert
, TRUE
);
610 #ifdef FEATURE_ALIASES
611 /* expand all aliases */
612 ExpandAlias (str
, maxlen
);
613 #endif /* FEATURE_ALIAS */