Moved memory/environ.c, memory/virtual.c and misc/cpu.c to
[wine] / dlls / kernel / editline.c
1 /*
2  * line edition function for Win32 console
3  *
4  * Copyright 2001 Eric Pouech
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wincon.h"
30 #include "wine/unicode.h"
31 #include "winnls.h"
32 #include "wine/debug.h"
33 #include "console_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(console);
36
37 struct WCEL_Context;
38
39 typedef struct
40 {
41     WCHAR                       val;            /* vk or unicode char */
42     void                        (*func)(struct WCEL_Context* ctx);
43 } KeyEntry;
44
45 typedef struct
46 {
47     DWORD                       keyState;       /* keyState (from INPUT_RECORD) to match */
48     BOOL                        chkChar;        /* check vk or char */
49     KeyEntry*                   entries;        /* array of entries */
50 } KeyMap;
51
52 typedef struct WCEL_Context {
53     WCHAR*                      line;           /* the line being edited */
54     size_t                      alloc;          /* number of WCHAR in line */
55     unsigned                    len;            /* number of chars in line */
56     unsigned                    ofs;            /* offset for cursor in current line */
57     WCHAR*                      yanked;         /* yanked line */
58     unsigned                    mark;           /* marked point (emacs mode only) */
59     CONSOLE_SCREEN_BUFFER_INFO  csbi;           /* current state (initial cursor, window size, attribute) */
60     HANDLE                      hConIn;
61     HANDLE                      hConOut;
62     unsigned                    done : 1,       /* to 1 when we're done with editing */
63                                 error : 1,      /* to 1 when an error occurred in the editing */
64                                 can_wrap : 1;   /* to 1 when multi-line edition can take place */
65     unsigned                    histSize;
66     unsigned                    histPos;
67     WCHAR*                      histCurr;
68 } WCEL_Context;
69
70 #if 0
71 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
72 {
73     MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
74             "\t\thist=(size=%u pos=%u curr=%s)\n"
75             "\t\tyanked=%s\n",
76             pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
77             ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
78             ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
79             ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
80             debugstr_w(ctx->yanked));
81 }
82 #endif
83
84 /* ====================================================================
85  *
86  * Console helper functions
87  *
88  * ====================================================================*/
89
90 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
91 {
92     if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
93     ERR("hmm bad situation\n");
94     ctx->error = 1;
95     return FALSE;
96 }
97
98 static inline void WCEL_Beep(WCEL_Context* ctx)
99 {
100     Beep(400, 300);
101 }
102
103 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
104 {
105     return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
106 }
107
108 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
109 {
110     COORD       c;
111     unsigned    len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
112
113     c.Y = ctx->csbi.dwCursorPosition.Y;
114     if (ofs >= len)
115     {
116         ofs -= len;
117         c.X = ofs % ctx->csbi.dwSize.X;
118         c.Y += 1 + ofs / ctx->csbi.dwSize.X;
119     }
120     else c.X = ctx->csbi.dwCursorPosition.X + ofs;
121     return c;
122 }
123
124 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
125 {
126     WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[beg], len,
127                                  WCEL_GetCoord(ctx, beg), NULL);
128     FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, len,
129                                WCEL_GetCoord(ctx, beg), NULL);
130 }
131
132 /* ====================================================================
133  *
134  * context manipulation functions
135  *
136  * ====================================================================*/
137
138 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
139 {
140     if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
141     {
142         FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
143         return FALSE;
144     }
145
146     if (ctx->len + len >= ctx->alloc)
147     {
148         WCHAR*  newline;
149         size_t  newsize;
150
151         /* round up size to 32 byte-WCHAR boundary */
152         newsize = (ctx->len + len + 1 + 31) & ~31;
153
154         if (ctx->line)
155             newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
156         else
157             newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
158
159         if (!newline) return FALSE;
160         ctx->line = newline;
161         ctx->alloc = newsize;
162     }
163     return TRUE;
164 }
165
166 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
167 {
168     unsigned    str_len = end - beg;
169     COORD       cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
170     COORD       cend = WCEL_GetCoord(ctx, ctx->len);
171     CHAR_INFO   ci;
172
173     if (end < ctx->len)
174         memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
175     /* we need to clean from ctx->len - str_len to ctx->len */
176
177     ci.Char.UnicodeChar = ' ';
178     ci.Attributes = ctx->csbi.wAttributes;
179
180     if (cbeg.Y == cend.Y)
181     {
182         /* partial erase of sole line */
183         CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
184                                 cend.X - cbeg.X, &ci);
185     }
186     else
187     {
188         int         i;
189         /* erase til eol on first line */
190         CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
191                                 ctx->csbi.dwSize.X - cbeg.X, &ci);
192         /* completly erase all the others (full lines) */
193         for (i = cbeg.Y + 1; i < cend.Y; i++)
194             CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
195         /* erase from beg of line until last pos on last line */
196         CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
197     }
198     ctx->len -= str_len;
199     WCEL_Update(ctx, 0, ctx->len);
200     ctx->line[ctx->len] = 0;
201 }
202
203 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
204 {
205     size_t      len = lstrlenW(str);
206
207     if (!len || !WCEL_Grow(ctx, len)) return;
208     if (ctx->len > ctx->ofs)
209         memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
210     memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
211     ctx->len += len;
212     ctx->line[ctx->len] = 0;
213     WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs);
214
215     ctx->ofs += len;
216 }
217
218 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
219 {
220     WCHAR       buffer[2];
221
222     /* do not insert 0..31 control characters */
223     if (c < ' ' && c != '\t') return;
224
225     buffer[0] = c;
226     buffer[1] = 0;
227     WCEL_InsertString(ctx, buffer);
228 }
229
230 static void WCEL_FreeYank(WCEL_Context* ctx)
231 {
232     if (ctx->yanked)
233     {
234         HeapFree(GetProcessHeap(), 0, ctx->yanked);
235         ctx->yanked = NULL;
236     }
237 }
238
239 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
240 {
241     int len = end - beg;
242     if (len <= 0) return;
243
244     WCEL_FreeYank(ctx);
245     /* After WCEL_FreeYank ctx->yanked is empty */
246     ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
247     if (!ctx->yanked) return;
248     memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
249     ctx->yanked[len] = 0;
250 }
251
252 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
253  * of the data lay in unicode lib
254  */
255 static inline BOOL WCEL_iswalnum(WCHAR wc)
256 {
257     return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
258 }
259
260 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
261 {
262     ofs--;
263     while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
264     while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
265     if (ofs >= 0) ofs++;
266     return max(ofs, 0);
267 }
268
269 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
270 {
271     ofs++;
272     while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
273     while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
274     return min(ofs, ctx->len);
275 }
276
277 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
278 {
279     WCHAR*      ptr;
280
281     if (idx == ctx->histSize - 1)
282     {
283         ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
284         lstrcpyW(ptr, ctx->histCurr);
285     }
286     else
287     {
288         int     len = CONSOLE_GetHistory(idx, NULL, 0);
289
290         if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
291         {
292             CONSOLE_GetHistory(idx, ptr, len);
293         }
294     }
295     return ptr;
296 }
297
298 static void     WCEL_HistoryInit(WCEL_Context* ctx)
299 {
300     ctx->histPos  = CONSOLE_GetNumHistoryEntries();
301     ctx->histSize = ctx->histPos + 1;
302     ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
303 }
304
305 static void    WCEL_MoveToHist(WCEL_Context* ctx, int idx)
306 {
307     WCHAR*      data = WCEL_GetHistory(ctx, idx);
308     int         len = lstrlenW(data) + 1;
309
310     /* save current line edition for recall when needed (FIXME seems broken to me) */
311     if (ctx->histPos == ctx->histSize - 1)
312     {
313         if (ctx->histCurr) HeapFree(GetProcessHeap(), 0, ctx->histCurr);
314         ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
315         memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
316     }
317     /* need to clean also the screen if new string is shorter than old one */
318     WCEL_DeleteString(ctx, 0, ctx->len);
319     ctx->ofs = 0;
320     /* insert new string */
321     if (WCEL_Grow(ctx, len))
322     {
323         WCEL_InsertString(ctx, data);
324         HeapFree(GetProcessHeap(), 0, data);
325         ctx->histPos = idx;
326     }
327 }
328
329 static void    WCEL_FindPrevInHist(WCEL_Context* ctx)
330 {
331     int startPos = ctx->histPos;
332     WCHAR*      data;
333     int         len, oldofs;
334
335     if (ctx->histPos && ctx->histPos == ctx->histSize) {
336         startPos--;
337         ctx->histPos--;
338     }
339
340     do {
341        data = WCEL_GetHistory(ctx, ctx->histPos);
342
343        if (ctx->histPos) ctx->histPos--;
344        else ctx->histPos = (ctx->histSize-1);
345
346        len = lstrlenW(data) + 1;
347        if ((len >= ctx->ofs) &&
348            (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
349
350            /* need to clean also the screen if new string is shorter than old one */
351            WCEL_DeleteString(ctx, 0, ctx->len);
352
353            if (WCEL_Grow(ctx, len))
354            {
355               oldofs = ctx->ofs;
356               ctx->ofs = 0;
357               WCEL_InsertString(ctx, data);
358               ctx->ofs = oldofs;
359               SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
360               HeapFree(GetProcessHeap(), 0, data);
361               return;
362            }
363        }
364     } while (ctx->histPos != startPos);
365
366     return;
367 }
368
369 /* ====================================================================
370  *
371  * basic edition functions
372  *
373  * ====================================================================*/
374
375 static void WCEL_Done(WCEL_Context* ctx)
376 {
377     WCHAR       nl = '\n';
378     if (!WCEL_Grow(ctx, 1)) return;
379     ctx->line[ctx->len++] = '\n';
380     ctx->line[ctx->len] = 0;
381     WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
382     ctx->done = 1;
383 }
384
385 static void WCEL_MoveLeft(WCEL_Context* ctx)
386 {
387     if (ctx->ofs > 0) ctx->ofs--;
388 }
389
390 static void WCEL_MoveRight(WCEL_Context* ctx)
391 {
392     if (ctx->ofs < ctx->len) ctx->ofs++;
393 }
394
395 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
396 {
397     int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
398     if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
399 }
400
401 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
402 {
403     int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
404     if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
405 }
406
407 static void WCEL_MoveToBeg(WCEL_Context* ctx)
408 {
409     ctx->ofs = 0;
410 }
411
412 static void WCEL_MoveToEnd(WCEL_Context* ctx)
413 {
414     ctx->ofs = ctx->len;
415 }
416
417 static void WCEL_SetMark(WCEL_Context* ctx)
418 {
419     ctx->mark = ctx->ofs;
420 }
421
422 static void WCEL_ExchangeMark(WCEL_Context* ctx)
423 {
424     unsigned tmp;
425
426     if (ctx->mark > ctx->len) return;
427     tmp = ctx->ofs;
428     ctx->ofs = ctx->mark;
429     ctx->mark = tmp;
430 }
431
432 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
433 {
434     unsigned beg, end;
435
436     if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
437     if (ctx->mark > ctx->ofs)
438     {
439         beg = ctx->ofs;         end = ctx->mark;
440     }
441     else
442     {
443         beg = ctx->mark;        end = ctx->ofs;
444     }
445     WCEL_SaveYank(ctx, beg, end);
446 }
447
448 static void WCEL_TransposeChar(WCEL_Context* ctx)
449 {
450     WCHAR       c;
451
452     if (!ctx->ofs || ctx->ofs == ctx->len) return;
453
454     c = ctx->line[ctx->ofs];
455     ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
456     ctx->line[ctx->ofs - 1] = c;
457
458     WCEL_Update(ctx, ctx->ofs - 1, 2);
459     ctx->ofs++;
460 }
461
462 static void WCEL_TransposeWords(WCEL_Context* ctx)
463 {
464     int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
465         right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
466     if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
467     {
468         unsigned len_r = right_ofs - ctx->ofs;
469         unsigned len_l = ctx->ofs - left_ofs;
470
471         char*   tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
472         if (!tmp) return;
473
474         memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
475         memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
476         memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
477
478         HeapFree(GetProcessHeap(), 0, tmp);
479         WCEL_Update(ctx, left_ofs, len_l + len_r);
480         ctx->ofs = right_ofs;
481     }
482 }
483
484 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
485 {
486     int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
487     if (new_ofs != ctx->ofs)
488     {
489         int     i;
490         for (i = ctx->ofs; i <= new_ofs; i++)
491             ctx->line[i] = tolowerW(ctx->line[i]);
492         WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
493         ctx->ofs = new_ofs;
494     }
495 }
496
497 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
498 {
499     int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
500     if (new_ofs != ctx->ofs)
501     {
502         int     i;
503         for (i = ctx->ofs; i <= new_ofs; i++)
504             ctx->line[i] = toupperW(ctx->line[i]);
505         WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
506         ctx->ofs = new_ofs;
507     }
508 }
509
510 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
511 {
512     int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
513     if (new_ofs != ctx->ofs)
514     {
515         int     i;
516
517         ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
518         for (i = ctx->ofs + 1; i <= new_ofs; i++)
519             ctx->line[i] = tolowerW(ctx->line[i]);
520         WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
521         ctx->ofs = new_ofs;
522     }
523 }
524
525 static void WCEL_Yank(WCEL_Context* ctx)
526 {
527     WCEL_InsertString(ctx, ctx->yanked);
528 }
529
530 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
531 {
532     WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
533     WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
534 }
535
536 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
537 {
538     unsigned beg, end;
539
540     if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
541     if (ctx->mark > ctx->ofs)
542     {
543         beg = ctx->ofs;         end = ctx->mark;
544     }
545     else
546     {
547         beg = ctx->mark;        end = ctx->ofs;
548     }
549     WCEL_SaveYank(ctx, beg, end);
550     WCEL_DeleteString(ctx, beg, end);
551     ctx->ofs = beg;
552 }
553
554 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
555 {
556     if (ctx->ofs)
557     {
558         WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
559         ctx->ofs--;
560     }
561 }
562
563 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
564 {
565     if (ctx->ofs < ctx->len)
566         WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
567 }
568
569 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
570 {
571     int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
572     if (new_ofs != ctx->ofs)
573     {
574         WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
575         ctx->ofs = new_ofs;
576     }
577 }
578
579 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
580 {
581     int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
582     if (new_ofs != ctx->ofs)
583     {
584         WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
585     }
586 }
587
588 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
589 {
590     if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
591 }
592
593 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
594 {
595     if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
596 }
597
598 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
599 {
600     if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
601 }
602
603 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
604 {
605     if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
606 }
607
608 static void WCEL_Redraw(WCEL_Context* ctx)
609 {
610     COORD       c = WCEL_GetCoord(ctx, ctx->len);
611     CHAR_INFO   ci;
612
613     WCEL_Update(ctx, 0, ctx->len);
614
615     ci.Char.UnicodeChar = ' ';
616     ci.Attributes = ctx->csbi.wAttributes;
617
618     CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
619 }
620
621 static void WCEL_RepeatCount(WCEL_Context* ctx)
622 {
623 #if 0
624 /* FIXME: wait until all console code is in kernel32 */
625     INPUT_RECORD        ir;
626     unsigned            repeat = 0;
627
628     while (WCEL_Get(ctx, &ir, FALSE))
629     {
630         if (ir.EventType != KEY_EVENT) break;
631         if (ir.Event.KeyEvent.bKeyDown)
632         {
633             if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
634                 break;
635             if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
636                 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
637                 break;
638             repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
639         }
640         WCEL_Get(ctx, &ir, TRUE);
641     }
642     FIXME("=> %u\n", repeat);
643 #endif
644 }
645
646 /* ====================================================================
647  *
648  *              Key Maps
649  *
650  * ====================================================================*/
651
652 #define CTRL(x) ((x) - '@')
653 static KeyEntry StdKeyMap[] =
654 {
655     {/*BACK*/0x08,      WCEL_DeletePrevChar     },
656     {/*RETURN*/0x0d,    WCEL_Done               },
657     {/*DEL*/127,        WCEL_DeleteCurrChar     },
658     {   0,              NULL                    }
659 };
660
661 static KeyEntry Win32ExtraStdKeyMap[] =
662 {
663     {/*VK_F8*/   0x77,  WCEL_FindPrevInHist     },
664     {   0,              NULL                    }
665 };
666
667
668 static  KeyEntry EmacsKeyMapCtrl[] =
669 {
670     {   CTRL('@'),      WCEL_SetMark            },
671     {   CTRL('A'),      WCEL_MoveToBeg          },
672     {   CTRL('B'),      WCEL_MoveLeft           },
673     /* C: done in server */
674     {   CTRL('D'),      WCEL_DeleteCurrChar     },
675     {   CTRL('E'),      WCEL_MoveToEnd          },
676     {   CTRL('F'),      WCEL_MoveRight          },
677     {   CTRL('G'),      WCEL_Beep               },
678     {   CTRL('H'),      WCEL_DeletePrevChar     },
679     /* I: meaningless (or tab ???) */
680     {   CTRL('J'),      WCEL_Done               },
681     {   CTRL('K'),      WCEL_KillToEndOfLine    },
682     {   CTRL('L'),      WCEL_Redraw             },
683     {   CTRL('M'),      WCEL_Done               },
684     {   CTRL('N'),      WCEL_MoveToNextHist     },
685     /* O; insert line... meaningless */
686     {   CTRL('P'),      WCEL_MoveToPrevHist     },
687     /* Q: [NIY] quoting... */
688     /* R: [NIY] search backwards... */
689     /* S: [NIY] search forwards... */
690     {   CTRL('T'),      WCEL_TransposeChar      },
691     {   CTRL('U'),      WCEL_RepeatCount        },
692     /* V: paragraph down... meaningless */
693     {   CTRL('W'),      WCEL_KillMarkedZone     },
694     {   CTRL('X'),      WCEL_ExchangeMark       },
695     {   CTRL('Y'),      WCEL_Yank               },
696     /* Z: meaningless */
697     {   0,              NULL                    }
698 };
699
700 static KeyEntry EmacsKeyMapAlt[] =
701 {
702     {/*DEL*/127,        WCEL_DeleteLeftWord     },
703     {   '<',            WCEL_MoveToFirstHist    },
704     {   '>',            WCEL_MoveToLastHist     },
705     {   '?',            WCEL_Beep               },
706     {   'b',            WCEL_MoveToLeftWord     },
707     {   'c',            WCEL_CapitalizeWord     },
708     {   'd',            WCEL_DeleteRightWord    },
709     {   'f',            WCEL_MoveToRightWord    },
710     {   'l',            WCEL_LowerCaseWord      },
711     {   't',            WCEL_TransposeWords     },
712     {   'u',            WCEL_UpperCaseWord      },
713     {   'w',            WCEL_CopyMarkedZone     },
714     {   0,              NULL                    }
715 };
716
717 static KeyEntry EmacsKeyMapExtended[] =
718 {
719     {/*RETURN*/  0x0d,  WCEL_Done },
720     {/*VK_PRIOR*/0x21,  WCEL_MoveToPrevHist     },
721     {/*VK_NEXT*/ 0x22,  WCEL_MoveToNextHist     },
722     {/*VK_END*/  0x23,  WCEL_MoveToEnd          },
723     {/*VK_HOME*/ 0x24,  WCEL_MoveToBeg          },
724     {/*VK_RIGHT*/0x27,  WCEL_MoveRight          },
725     {/*VK_LEFT*/ 0x25,  WCEL_MoveLeft           },
726     {/*VK_DEL*/  0x2e,  WCEL_DeleteCurrChar     },
727     {   0,              NULL                    }
728 };
729
730 static KeyMap   EmacsKeyMap[] =
731 {
732     {0x00000000, 1, StdKeyMap},
733     {0x00000001, 1, EmacsKeyMapAlt},    /* left  alt  */
734     {0x00000002, 1, EmacsKeyMapAlt},    /* right alt  */
735     {0x00000004, 1, EmacsKeyMapCtrl},   /* left  ctrl */
736     {0x00000008, 1, EmacsKeyMapCtrl},   /* right ctrl */
737     {0x00000100, 0, EmacsKeyMapExtended},
738     {0,          0, 0}
739 };
740
741 static  KeyEntry Win32KeyMapExtended[] =
742 {
743     {/*VK_LEFT*/ 0x25,  WCEL_MoveLeft           },
744     {/*VK_RIGHT*/0x27,  WCEL_MoveRight          },
745     {/*VK_HOME*/ 0x24,  WCEL_MoveToBeg          },
746     {/*VK_END*/  0x23,  WCEL_MoveToEnd          },
747     {/*VK_UP*/   0x26,  WCEL_MoveToPrevHist     },
748     {/*VK_DOWN*/ 0x28,  WCEL_MoveToNextHist     },
749     {/*VK_DEL*/  0x2e,  WCEL_DeleteCurrChar     },
750     {   0,              NULL                    }
751 };
752
753 static  KeyEntry Win32KeyMapCtrlExtended[] =
754 {
755     {/*VK_LEFT*/ 0x25,  WCEL_MoveToLeftWord     },
756     {/*VK_RIGHT*/0x27,  WCEL_MoveToRightWord    },
757     {/*VK_END*/  0x23,  WCEL_KillToEndOfLine    },
758     {   0,              NULL                    }
759 };
760
761 KeyMap  Win32KeyMap[] =
762 {
763     {0x00000000, 1, StdKeyMap},
764     {0x00000000, 0, Win32ExtraStdKeyMap},
765     {0x00000100, 0, Win32KeyMapExtended},
766     {0x00000104, 0, Win32KeyMapCtrlExtended},
767     {0x00000108, 0, Win32KeyMapCtrlExtended},
768     {0,          0, 0}
769 };
770 #undef CTRL
771
772 /* ====================================================================
773  *
774  *              Read line master function
775  *
776  * ====================================================================*/
777
778 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn)
779 {
780     WCEL_Context        ctx;
781     INPUT_RECORD        ir;
782     KeyMap*             km;
783     KeyEntry*           ke;
784     unsigned            ofs;
785     void                (*func)(struct WCEL_Context* ctx);
786     DWORD               ks;
787     int                 use_emacs;
788
789     memset(&ctx, 0, sizeof(ctx));
790     ctx.hConIn = hConsoleIn;
791     WCEL_HistoryInit(&ctx);
792
793     if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
794         use_emacs = 0;
795
796     if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
797                                     OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
798         !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
799         return NULL;
800     ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0;
801
802     if (!WCEL_Grow(&ctx, 1))
803     {
804         CloseHandle(ctx.hConOut);
805         return NULL;
806     }
807     ctx.line[0] = 0;
808
809 /* EPP     WCEL_Dump(&ctx, "init"); */
810
811     while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
812     {
813         if (ir.EventType != KEY_EVENT) continue;
814         TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08lx\n",
815               ir.Event.KeyEvent.bKeyDown ? "Down" : "Up  ", ir.Event.KeyEvent.wRepeatCount,
816               ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
817               ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
818         if (!ir.Event.KeyEvent.bKeyDown) continue;
819
820 /* EPP          WCEL_Dump(&ctx, "before func"); */
821         ofs = ctx.ofs;
822         /* mask out some bits which don't interest us */
823         ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON);
824
825         func = NULL;
826         for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
827         {
828             if (km->keyState != ks)
829                 continue;
830             if (km->chkChar)
831             {
832                 for (ke = &km->entries[0]; ke->func != 0; ke++)
833                     if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
834             }
835             else
836             {
837                 for (ke = &km->entries[0]; ke->func != 0; ke++)
838                     if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
839
840             }
841             if (ke->func)
842             {
843                 func = ke->func;
844                 break;
845             }
846         }
847
848         if (func)
849             (func)(&ctx);
850         else if (!(ir.Event.KeyEvent.dwControlKeyState & (ENHANCED_KEY|LEFT_ALT_PRESSED)))
851             WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
852         else TRACE("Dropped event\n");
853
854 /* EPP         WCEL_Dump(&ctx, "after func"); */
855         if (ctx.ofs != ofs)
856             SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
857     }
858     if (ctx.error)
859     {
860         HeapFree(GetProcessHeap(), 0, ctx.line);
861         ctx.line = NULL;
862     }
863     WCEL_FreeYank(&ctx);
864     if (ctx.line)
865         CONSOLE_AppendHistory(ctx.line);
866
867     CloseHandle(ctx.hConOut);
868     if (ctx.histCurr) HeapFree(GetProcessHeap(), 0, ctx.histCurr);
869     return ctx.line;
870 }