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
);
188 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
189 (RIGHT_ALT_PRESSED
|LEFT_ALT_PRESSED
|
190 RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
) )
192 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
194 #ifdef FEATURE_HISTORY
196 /*add the current command line to the history*/
197 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
198 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
203 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
204 current
= charcount
= 0;
212 /*delete current history entry*/
213 if (ir
.Event
.KeyEvent
.dwControlKeyState
&
214 (LEFT_CTRL_PRESSED
|RIGHT_CTRL_PRESSED
))
216 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
217 History_del_current_entry(str
);
218 current
= charcount
= _tcslen (str
);
219 ConOutPrintf (_T("%s"), str
);
220 GetCursorXY (&curx
, &cury
);
225 #endif /*FEATURE_HISTORY*/
231 switch (ir
.Event
.KeyEvent
.wVirtualKeyCode
)
234 /* <BACKSPACE> - delete character to left of cursor */
235 if (current
> 0 && charcount
> 0)
237 if (current
== charcount
)
239 /* if at end of line */
240 str
[current
- 1] = _T('\0');
241 if (GetCursorX () != 0)
243 ConOutPrintf (_T("\b \b"));
248 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
249 ConOutChar (_T(' '));
250 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
257 for (count
= current
- 1; count
< charcount
; count
++)
258 str
[count
] = str
[count
+ 1];
259 if (GetCursorX () != 0)
261 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
266 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
270 GetCursorXY (&curx
, &cury
);
271 ConOutPrintf (_T("%s "), &str
[current
- 1]);
272 SetCursorXY (curx
, cury
);
280 /* toggle insert/overstrike mode */
282 SetCursorType (bInsert
, TRUE
);
286 /* delete character under cursor */
287 if (current
!= charcount
&& charcount
> 0)
289 for (count
= current
; count
< charcount
; count
++)
290 str
[count
] = str
[count
+ 1];
292 GetCursorXY (&curx
, &cury
);
293 ConOutPrintf (_T("%s "), &str
[current
]);
294 SetCursorXY (curx
, cury
);
299 /* goto beginning of string */
302 SetCursorXY (orgx
, orgy
);
310 /* goto end of string */
311 if (current
!= charcount
)
313 SetCursorXY (orgx
, orgy
);
314 ConOutPrintf (_T("%s"), str
);
315 GetCursorXY (&curx
, &cury
);
321 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
322 /* expand current file name */
323 if ((current
== charcount
) ||
324 (current
== charcount
- 1 &&
325 str
[current
] == _T('"'))) /* only works at end of line*/
327 if (wLastKey
!= VK_TAB
)
329 /* if first TAB, complete filename*/
330 tempscreen
= charcount
;
331 CompleteFilename (str
, charcount
);
332 charcount
= _tcslen (str
);
335 SetCursorXY (orgx
, orgy
);
336 ConOutPrintf (_T("%s"), str
);
338 if (tempscreen
> charcount
)
340 GetCursorXY (&curx
, &cury
);
341 for (count
= tempscreen
- charcount
; count
--; )
342 ConOutChar (_T(' '));
343 SetCursorXY (curx
, cury
);
347 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
348 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
351 /* set cursor position */
352 SetCursorXY ((orgx
+ current
) % maxx
,
353 orgy
+ (orgx
+ current
) / maxx
);
354 GetCursorXY (&curx
, &cury
);
358 /*if second TAB, list matches*/
359 if (ShowCompletionMatches (str
, charcount
))
362 GetCursorXY(&orgx
, &orgy
);
363 ConOutPrintf(_T("%s"), str
);
365 /* set cursor position */
366 SetCursorXY((orgx
+ current
) % maxx
,
367 orgy
+ (orgx
+ current
) / maxx
);
368 GetCursorXY(&curx
, &cury
);
378 #ifdef FEATURE_4NT_FILENAME_COMPLETION
379 /* used to later see if we went down to the next line */
380 tempscreen
= charcount
;
383 /* str is the whole things that is on the current line
384 that is and and out. arg 2 is weather it goes back
385 one file or forward one file */
386 CompleteFilename(str
, !(ir
.Event
.KeyEvent
.dwControlKeyState
& SHIFT_PRESSED
), szPath
, current
);
387 /* Attempt to clear the line */
388 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
391 current
= charcount
= 0;
393 /* Everything is deleted, lets add it back in */
396 /* Figure out where cusor is going to be after we print it */
397 charcount
= _tcslen(str
);
400 SetCursorXY(orgx
, orgy
);
401 /* Print out what we have now */
402 ConOutPrintf(_T("%s"), str
);
404 /* Move cursor accordingly */
405 if (tempscreen
> charcount
)
407 GetCursorXY(&curx
, &cury
);
408 for(count
= tempscreen
- charcount
; count
--; )
410 SetCursorXY(curx
, cury
);
414 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
415 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
417 SetCursorXY((short)(((int)orgx
+ current
) % maxx
), (short)((int)orgy
+ ((int)orgx
+ current
) / maxx
));
418 GetCursorXY(&curx
, &cury
);
424 /* ^M does the same as return */
426 if (!(ir
.Event
.KeyEvent
.dwControlKeyState
&
427 (RIGHT_CTRL_PRESSED
|LEFT_CTRL_PRESSED
)))
433 /* end input, return to main */
434 #ifdef FEATURE_HISTORY
435 /* add to the history */
439 str
[charcount
++] = _T('\n');
440 str
[charcount
] = _T('\0');
441 ConOutChar(_T('\n'));
446 /* clear str Make this callable! */
447 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
450 current
= charcount
= 0;
453 #ifdef FEATURE_HISTORY
455 History_move_to_bottom();
458 #ifdef FEATURE_HISTORY
459 /* get previous command from buffer */
460 ClearCommandLine (str
, maxlen
, orgx
, orgy
);
462 current
= charcount
= _tcslen (str
);
463 if (((charcount
+ orgx
) / maxx
) + orgy
> maxy
- 1)
464 orgy
+= maxy
- ((charcount
+ orgx
) / maxx
+ orgy
+ 1);
465 ConOutPrintf (_T("%s"), str
);
466 GetCursorXY (&curx
, &cury
);
471 #ifdef FEATURE_HISTORY
472 /* get next 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 /* move cursor left */
488 if (GetCursorX () == 0)
490 SetCursorXY ((SHORT
)(maxx
- 1), (SHORT
)(GetCursorY () - 1));
496 SetCursorXY ((SHORT
)(GetCursorX () - 1), GetCursorY ());
507 /* move cursor right */
508 if (current
!= charcount
)
511 if (GetCursorX () == maxx
- 1)
513 SetCursorXY (0, (SHORT
)(GetCursorY () + 1));
519 SetCursorXY ((SHORT
)(GetCursorX () + 1), GetCursorY ());
523 #ifdef FEATURE_HISTORY
526 LPCTSTR last
= PeekHistory(-1);
527 if (last
&& charcount
< (INT
)_tcslen (last
))
529 PreviousChar
= last
[current
];
530 ConOutChar(PreviousChar
);
531 GetCursorXY(&curx
, &cury
);
532 str
[current
++] = PreviousChar
;
540 /* This input is just a normal char */
545 ch
= ir
.Event
.KeyEvent
.uChar
.UnicodeChar
;
546 if (ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
548 ch
= ir
.Event
.KeyEvent
.uChar
.AsciiChar
;
549 if ((UCHAR
)ch
>= 32 && (charcount
!= (maxlen
- 2)) && bCharInput
)
550 #endif /* _UNICODE */
552 /* insert character into string... */
553 if (bInsert
&& current
!= charcount
)
555 /* If this character insertion will cause screen scrolling,
556 * adjust the saved origin of the command prompt. */
557 tempscreen
= _tcslen(str
+ current
) + curx
;
558 if ((tempscreen
% maxx
) == (maxx
- 1) &&
559 (tempscreen
/ maxx
) + cury
== (maxy
- 1))
565 for (count
= charcount
; count
> current
; count
--)
566 str
[count
] = str
[count
- 1];
568 if (curx
== maxx
- 1)
572 ConOutPrintf (_T("%s"), &str
[current
- 1]);
573 SetCursorXY (curx
, cury
);
578 if (current
== charcount
)
581 if (GetCursorX () == maxx
- 1 && GetCursorY () == maxy
- 1)
583 if (GetCursorX () == maxx
- 1)
591 //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode;
595 SetCursorType (bInsert
, TRUE
);
597 #ifdef FEATURE_ALIASES
598 /* expand all aliases */
599 ExpandAlias (str
, maxlen
);
600 #endif /* FEATURE_ALIAS */