* Sync up to trunk head (r65426).
[reactos.git] / base / shell / cmd / cmdinput.c
1 /*
2 * CMDINPUT.C - handles command input (tab completion, history, etc.).
3 *
4 *
5 * History:
6 *
7 * 01/14/95 (Tim Norman)
8 * started.
9 *
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.
14 *
15 * 12/12/95 (Tim Norman)
16 * added findxy() function to get max x/y coordinates to display
17 * correctly on larger screens
18 *
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
22 * when needed
23 *
24 * 8/1/96 (Tim Norman)
25 * fixed a bug in tab completion that caused filenames at the beginning
26 * of the command-line to have their first letter truncated
27 *
28 * 9/1/96 (Tim Norman)
29 * fixed a silly bug using printf instead of fputs, where typing "%i"
30 * confused printf :)
31 *
32 * 6/14/97 (Steffan Kaiser)
33 * ctrl-break checking
34 *
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 :)
42 *
43 * 7/12/97 (Tim Norman)
44 * Note: above changes pre-empted Steffan's ctrl-break checking.
45 *
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.
51 *
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
56 *
57 * 7/13/97 (Tim Norman)
58 * added different cursor appearance for insert/overstrike mode
59 *
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 :)
63 *
64 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
65 * added config.h include
66 *
67 * 28-Jul-1998 (John P Price <linux-guru@gcfl.net>)
68 * put ifdef's around filename completion code.
69 *
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
73 *
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.
77 *
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")
81 *
82 * 13-Dec-1998 (Eric Kohl)
83 * Added insert/overwrite cursor.
84 *
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!
89 *
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.
95 *
96 * 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
97 * Fixed problems when the screen was scrolled away.
98 *
99 * 28-September-2007 (Hervé Poussineau)
100 * Added history possibilities to right key.
101 */
102
103 #include "precomp.h"
104
105 SHORT maxx;
106 SHORT maxy;
107
108 /*
109 * global command line insert/overwrite flag
110 */
111 static BOOL bInsert = TRUE;
112
113
114 static VOID
115 ClearCommandLine(LPTSTR str, INT maxlen, SHORT orgx, SHORT orgy)
116 {
117 INT count;
118
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);
124 }
125
126
127 /* read in a command line */
128 BOOL ReadCommand(LPTSTR str, INT maxlen)
129 {
130 CONSOLE_SCREEN_BUFFER_INFO csbi;
131 SHORT orgx; /* origin x/y */
132 SHORT orgy;
133 SHORT curx; /*current x/y cursor position*/
134 SHORT cury;
135 SHORT tempscreen;
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)*/
139 INPUT_RECORD ir;
140 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
141 WORD wLastKey = 0;
142 #endif
143 TCHAR ch;
144 BOOL bReturn = FALSE;
145 BOOL bCharInput;
146 #ifdef FEATURE_4NT_FILENAME_COMPLETION
147 TCHAR szPath[MAX_PATH];
148 #endif
149 #ifdef FEATURE_HISTORY
150 //BOOL bContinue=FALSE;/*is TRUE the second case will not be executed*/
151 TCHAR PreviousChar;
152 #endif
153
154 if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
155 {
156 /* No console */
157 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
158 DWORD dwRead;
159 CHAR chr;
160 do
161 {
162 if (!ReadFile(hStdin, &chr, 1, &dwRead, NULL) || !dwRead)
163 return FALSE;
164 #ifdef _UNICODE
165 MultiByteToWideChar(InputCodePage, 0, &chr, 1, &str[charcount++], 1);
166 #endif
167 } while (chr != '\n' && charcount < maxlen);
168 str[charcount] = _T('\0');
169 return TRUE;
170 }
171
172 /* get screen size */
173 maxx = csbi.dwSize.X;
174 maxy = csbi.dwSize.Y;
175
176 curx = orgx = csbi.dwCursorPosition.X;
177 cury = orgy = csbi.dwCursorPosition.Y;
178
179 memset (str, 0, maxlen * sizeof (TCHAR));
180
181 SetCursorType (bInsert, TRUE);
182
183 do
184 {
185 bReturn = FALSE;
186 ConInKey (&ir);
187
188 if (ir.Event.KeyEvent.dwControlKeyState &
189 (RIGHT_ALT_PRESSED |LEFT_ALT_PRESSED|
190 RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED) )
191 {
192 switch (ir.Event.KeyEvent.wVirtualKeyCode)
193 {
194 #ifdef FEATURE_HISTORY
195 case 'K':
196 /*add the current command line to the history*/
197 if (ir.Event.KeyEvent.dwControlKeyState &
198 (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED))
199 {
200 if (str[0])
201 History(0,str);
202
203 ClearCommandLine (str, maxlen, orgx, orgy);
204 current = charcount = 0;
205 curx = orgx;
206 cury = orgy;
207 //bContinue=TRUE;
208 break;
209 }
210
211 case 'D':
212 /*delete current history entry*/
213 if (ir.Event.KeyEvent.dwControlKeyState &
214 (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED))
215 {
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);
221 //bContinue=TRUE;
222 break;
223 }
224
225 #endif /*FEATURE_HISTORY*/
226 }
227 }
228
229 bCharInput = FALSE;
230
231 switch (ir.Event.KeyEvent.wVirtualKeyCode)
232 {
233 case VK_BACK:
234 /* <BACKSPACE> - delete character to left of cursor */
235 if (current > 0 && charcount > 0)
236 {
237 if (current == charcount)
238 {
239 /* if at end of line */
240 str[current - 1] = _T('\0');
241 if (GetCursorX () != 0)
242 {
243 ConOutPrintf (_T("\b \b"));
244 curx--;
245 }
246 else
247 {
248 SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1));
249 ConOutChar (_T(' '));
250 SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1));
251 cury--;
252 curx = maxx - 1;
253 }
254 }
255 else
256 {
257 for (count = current - 1; count < charcount; count++)
258 str[count] = str[count + 1];
259 if (GetCursorX () != 0)
260 {
261 SetCursorXY ((SHORT)(GetCursorX () - 1), GetCursorY ());
262 curx--;
263 }
264 else
265 {
266 SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1));
267 cury--;
268 curx = maxx - 1;
269 }
270 GetCursorXY (&curx, &cury);
271 ConOutPrintf (_T("%s "), &str[current - 1]);
272 SetCursorXY (curx, cury);
273 }
274 charcount--;
275 current--;
276 }
277 break;
278
279 case VK_INSERT:
280 /* toggle insert/overstrike mode */
281 bInsert ^= TRUE;
282 SetCursorType (bInsert, TRUE);
283 break;
284
285 case VK_DELETE:
286 /* delete character under cursor */
287 if (current != charcount && charcount > 0)
288 {
289 for (count = current; count < charcount; count++)
290 str[count] = str[count + 1];
291 charcount--;
292 GetCursorXY (&curx, &cury);
293 ConOutPrintf (_T("%s "), &str[current]);
294 SetCursorXY (curx, cury);
295 }
296 break;
297
298 case VK_HOME:
299 /* goto beginning of string */
300 if (current != 0)
301 {
302 SetCursorXY (orgx, orgy);
303 curx = orgx;
304 cury = orgy;
305 current = 0;
306 }
307 break;
308
309 case VK_END:
310 /* goto end of string */
311 if (current != charcount)
312 {
313 SetCursorXY (orgx, orgy);
314 ConOutPrintf (_T("%s"), str);
315 GetCursorXY (&curx, &cury);
316 current = charcount;
317 }
318 break;
319
320 case VK_TAB:
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*/
326 {
327 if (wLastKey != VK_TAB)
328 {
329 /* if first TAB, complete filename*/
330 tempscreen = charcount;
331 CompleteFilename (str, charcount);
332 charcount = _tcslen (str);
333 current = charcount;
334
335 SetCursorXY (orgx, orgy);
336 ConOutPrintf (_T("%s"), str);
337
338 if (tempscreen > charcount)
339 {
340 GetCursorXY (&curx, &cury);
341 for (count = tempscreen - charcount; count--; )
342 ConOutChar (_T(' '));
343 SetCursorXY (curx, cury);
344 }
345 else
346 {
347 if (((charcount + orgx) / maxx) + orgy > maxy - 1)
348 orgy += maxy - ((charcount + orgx) / maxx + orgy + 1);
349 }
350
351 /* set cursor position */
352 SetCursorXY ((orgx + current) % maxx,
353 orgy + (orgx + current) / maxx);
354 GetCursorXY (&curx, &cury);
355 }
356 else
357 {
358 /*if second TAB, list matches*/
359 if (ShowCompletionMatches (str, charcount))
360 {
361 PrintPrompt();
362 GetCursorXY(&orgx, &orgy);
363 ConOutPrintf(_T("%s"), str);
364
365 /* set cursor position */
366 SetCursorXY((orgx + current) % maxx,
367 orgy + (orgx + current) / maxx);
368 GetCursorXY(&curx, &cury);
369 }
370
371 }
372 }
373 else
374 {
375 MessageBeep(-1);
376 }
377 #endif
378 #ifdef FEATURE_4NT_FILENAME_COMPLETION
379 /* used to later see if we went down to the next line */
380 tempscreen = charcount;
381 szPath[0]=_T('\0');
382
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);
389 curx = orgx;
390 cury = orgy;
391 current = charcount = 0;
392
393 /* Everything is deleted, lets add it back in */
394 _tcscpy(str,szPath);
395
396 /* Figure out where cusor is going to be after we print it */
397 charcount = _tcslen(str);
398 current = charcount;
399
400 SetCursorXY(orgx, orgy);
401 /* Print out what we have now */
402 ConOutPrintf(_T("%s"), str);
403
404 /* Move cursor accordingly */
405 if (tempscreen > charcount)
406 {
407 GetCursorXY(&curx, &cury);
408 for(count = tempscreen - charcount; count--; )
409 ConOutChar(_T(' '));
410 SetCursorXY(curx, cury);
411 }
412 else
413 {
414 if (((charcount + orgx) / maxx) + orgy > maxy - 1)
415 orgy += maxy - ((charcount + orgx) / maxx + orgy + 1);
416 }
417 SetCursorXY((short)(((int)orgx + current) % maxx), (short)((int)orgy + ((int)orgx + current) / maxx));
418 GetCursorXY(&curx, &cury);
419 #endif
420 break;
421
422 case _T('M'):
423 case _T('C'):
424 /* ^M does the same as return */
425 bCharInput = TRUE;
426 if (!(ir.Event.KeyEvent.dwControlKeyState &
427 (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED)))
428 {
429 break;
430 }
431
432 case VK_RETURN:
433 /* end input, return to main */
434 #ifdef FEATURE_HISTORY
435 /* add to the history */
436 if (str[0])
437 History (0, str);
438 #endif
439 str[charcount++] = _T('\n');
440 str[charcount] = _T('\0');
441 ConOutChar(_T('\n'));
442 bReturn = TRUE;
443 break;
444
445 case VK_ESCAPE:
446 /* clear str Make this callable! */
447 ClearCommandLine (str, maxlen, orgx, orgy);
448 curx = orgx;
449 cury = orgy;
450 current = charcount = 0;
451 break;
452
453 #ifdef FEATURE_HISTORY
454 case VK_F3:
455 History_move_to_bottom();
456 #endif
457 case VK_UP:
458 #ifdef FEATURE_HISTORY
459 /* get previous command from buffer */
460 ClearCommandLine (str, maxlen, orgx, orgy);
461 History (-1, str);
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);
467 #endif
468 break;
469
470 case VK_DOWN:
471 #ifdef FEATURE_HISTORY
472 /* get next command from buffer */
473 ClearCommandLine (str, maxlen, orgx, orgy);
474 History (1, str);
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);
480 #endif
481 break;
482
483 case VK_LEFT:
484 /* move cursor left */
485 if (current > 0)
486 {
487 current--;
488 if (GetCursorX () == 0)
489 {
490 SetCursorXY ((SHORT)(maxx - 1), (SHORT)(GetCursorY () - 1));
491 curx = maxx - 1;
492 cury--;
493 }
494 else
495 {
496 SetCursorXY ((SHORT)(GetCursorX () - 1), GetCursorY ());
497 curx--;
498 }
499 }
500 else
501 {
502 MessageBeep (-1);
503 }
504 break;
505
506 case VK_RIGHT:
507 /* move cursor right */
508 if (current != charcount)
509 {
510 current++;
511 if (GetCursorX () == maxx - 1)
512 {
513 SetCursorXY (0, (SHORT)(GetCursorY () + 1));
514 curx = 0;
515 cury++;
516 }
517 else
518 {
519 SetCursorXY ((SHORT)(GetCursorX () + 1), GetCursorY ());
520 curx++;
521 }
522 }
523 #ifdef FEATURE_HISTORY
524 else
525 {
526 LPCTSTR last = PeekHistory(-1);
527 if (last && charcount < (INT)_tcslen (last))
528 {
529 PreviousChar = last[current];
530 ConOutChar(PreviousChar);
531 GetCursorXY(&curx, &cury);
532 str[current++] = PreviousChar;
533 charcount++;
534 }
535 }
536 #endif
537 break;
538
539 default:
540 /* This input is just a normal char */
541 bCharInput = TRUE;
542
543 }
544 #ifdef _UNICODE
545 ch = ir.Event.KeyEvent.uChar.UnicodeChar;
546 if (ch >= 32 && (charcount != (maxlen - 2)) && bCharInput)
547 #else
548 ch = ir.Event.KeyEvent.uChar.AsciiChar;
549 if ((UCHAR)ch >= 32 && (charcount != (maxlen - 2)) && bCharInput)
550 #endif /* _UNICODE */
551 {
552 /* insert character into string... */
553 if (bInsert && current != charcount)
554 {
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))
560 {
561 orgy--;
562 cury--;
563 }
564
565 for (count = charcount; count > current; count--)
566 str[count] = str[count - 1];
567 str[current++] = ch;
568 if (curx == maxx - 1)
569 curx = 0, cury++;
570 else
571 curx++;
572 ConOutPrintf (_T("%s"), &str[current - 1]);
573 SetCursorXY (curx, cury);
574 charcount++;
575 }
576 else
577 {
578 if (current == charcount)
579 charcount++;
580 str[current++] = ch;
581 if (GetCursorX () == maxx - 1 && GetCursorY () == maxy - 1)
582 orgy--, cury--;
583 if (GetCursorX () == maxx - 1)
584 curx = 0, cury++;
585 else
586 curx++;
587 ConOutChar (ch);
588 }
589 }
590
591 //wLastKey = ir.Event.KeyEvent.wVirtualKeyCode;
592 }
593 while (!bReturn);
594
595 SetCursorType (bInsert, TRUE);
596
597 #ifdef FEATURE_ALIASES
598 /* expand all aliases */
599 ExpandAlias (str, maxlen);
600 #endif /* FEATURE_ALIAS */
601 return TRUE;
602 }