msvcrt: Improved locale initialization.
[wine] / dlls / kernel32 / term.c
1 /*
2  * Interface to terminfo and termcap libraries
3  *
4  * Copyright 2010 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #ifdef HAVE_NCURSES_H
27 # include <ncurses.h>
28 #elif defined(HAVE_CURSES_H)
29 # include <curses.h>
30 #endif
31 /* avoid redefinition warnings */
32 #undef KEY_EVENT
33 #undef MOUSE_MOVED
34
35 #if defined(HAVE_CURSES_H) || defined(HAVE_NCURSES_H)
36 #include <term.h>
37 #endif
38
39 #include <windef.h>
40 #include <winbase.h>
41 #include <winnls.h>
42 #include <wincon.h>
43 #include "console_private.h"
44 #include "wine/library.h"
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(console);
48
49 static const int vkkeyscan_table[256] =
50 {
51      0,0,0,0,0,0,0,0,8,9,0,0,0,13,0,0,0,0,0,19,145,556,0,0,0,0,0,27,0,0,0,
52      0,32,305,478,307,308,309,311,222,313,304,312,443,188,189,190,191,48,
53      49,50,51,52,53,54,55,56,57,442,186,444,187,446,447,306,321,322,323,
54      324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,
55      341,342,343,344,345,346,219,220,221,310,445,192,65,66,67,68,69,70,71,
56      72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,475,476,477,
57      448,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
58      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
59      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
60      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,400,0,0,0,0,0,0
61 };
62
63 static const int mapvkey_0[256] =
64 {
65      0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,69,58,0,0,0,0,0,0,1,0,0,
66      0,0,57,73,81,79,71,75,72,77,80,0,0,0,55,82,83,0,11,2,3,4,5,6,7,8,9,
67      10,0,0,0,0,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,
68      19,31,20,22,47,17,45,21,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,78,0,74,
69      0,53,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70      0,0,0,0,0,0,69,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
71      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,
72      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,76,96,0,0,0,0,0,0,0,0,
73      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
74 };
75
76 static inline void init_complex_char(INPUT_RECORD* ir, BOOL down, WORD vk, WORD kc, DWORD cks)
77 {
78     ir->EventType                        = KEY_EVENT;
79     ir->Event.KeyEvent.bKeyDown          = down;
80     ir->Event.KeyEvent.wRepeatCount      = 1;
81     ir->Event.KeyEvent.wVirtualScanCode  = vk;
82     ir->Event.KeyEvent.wVirtualKeyCode   = kc;
83     ir->Event.KeyEvent.dwControlKeyState = cks;
84     ir->Event.KeyEvent.uChar.UnicodeChar = 0;
85 }
86
87 /******************************************************************
88  *              TERM_FillSimpleChar
89  *
90  */
91 unsigned TERM_FillSimpleChar(WCHAR real_inchar, INPUT_RECORD* ir)
92 {
93     unsigned            vk;
94     WCHAR               inchar;
95     unsigned            numEvent = 0;
96     DWORD               cks = 0;
97
98     switch (real_inchar)
99     {
100     case   9: inchar = real_inchar;
101         real_inchar = 27; /* so that we don't think key is ctrl- something */
102         break;
103     case  13:
104     case  10: inchar = '\r';
105         real_inchar = 27; /* Fixme: so that we don't think key is ctrl- something */
106         break;
107     case 127: inchar = '\b';
108         break;
109     default:
110         inchar = real_inchar;
111         break;
112     }
113     vk = (inchar < 256) ? vkkeyscan_table[inchar] : 0;
114     if (vk & 0x0100)
115         init_complex_char(&ir[numEvent++], 1, 0x2a, 0x10, SHIFT_PRESSED);
116     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
117         init_complex_char(&ir[numEvent++], 1, 0x1d, 0x11, LEFT_CTRL_PRESSED);
118     if (vk & 0x0400)
119         init_complex_char(&ir[numEvent++], 1, 0x38, 0x12, LEFT_ALT_PRESSED);
120
121     ir[numEvent].EventType                        = KEY_EVENT;
122     ir[numEvent].Event.KeyEvent.bKeyDown          = 1;
123     ir[numEvent].Event.KeyEvent.wRepeatCount      = 1;
124     ir[numEvent].Event.KeyEvent.dwControlKeyState = cks;
125     if (vk & 0x0100)
126         ir[numEvent].Event.KeyEvent.dwControlKeyState |= SHIFT_PRESSED;
127     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
128         ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_CTRL_PRESSED;
129     if (vk & 0x0400)
130         ir[numEvent].Event.KeyEvent.dwControlKeyState |= LEFT_ALT_PRESSED;
131     ir[numEvent].Event.KeyEvent.wVirtualKeyCode = vk;
132     ir[numEvent].Event.KeyEvent.wVirtualScanCode = mapvkey_0[vk & 0x00ff]; /* VirtualKeyCodes to ScanCode */
133
134     ir[numEvent].Event.KeyEvent.uChar.UnicodeChar = inchar;
135     ir[numEvent + 1] = ir[numEvent];
136     ir[numEvent + 1].Event.KeyEvent.bKeyDown = 0;
137
138     numEvent += 2;
139
140     if (vk & 0x0400)
141         init_complex_char(&ir[numEvent++], 0, 0x38, 0x12, LEFT_ALT_PRESSED);
142     if ((vk & 0x0200) || (unsigned char)real_inchar <= 26)
143         init_complex_char(&ir[numEvent++], 0, 0x1d, 0x11, 0);
144     if (vk & 0x0100)
145         init_complex_char(&ir[numEvent++], 0, 0x2a, 0x10, 0);
146     return numEvent;
147 }
148
149 #if defined(SONAME_LIBCURSES) || defined(SONAME_LIBNCURSES)
150
151 #ifdef HAVE_NCURSES_H
152 # define CURSES_NAME "ncurses"
153 #else
154 # define CURSES_NAME "curses"
155 #endif
156
157 static void *nc_handle = NULL;
158
159 #define MAKE_FUNCPTR(f) static typeof(f) * p_##f;
160
161 MAKE_FUNCPTR(putp)
162 MAKE_FUNCPTR(setupterm)
163 MAKE_FUNCPTR(tigetstr)
164 MAKE_FUNCPTR(tparm)
165
166 #undef MAKE_FUNCPTR
167
168 /**********************************************************************/
169
170 static BOOL TERM_bind_libcurses(void)
171 {
172 #ifdef SONAME_LIBNCURSES
173     static const char ncname[] = SONAME_LIBNCURSES;
174 #else
175     static const char ncname[] = SONAME_LIBCURSES;
176 #endif
177
178     if (!(nc_handle = wine_dlopen(ncname, RTLD_NOW, NULL, 0)))
179     {
180         WINE_MESSAGE("Wine cannot find the " CURSES_NAME " library (%s).\n",
181                      ncname);
182         return FALSE;
183     }
184
185 #define LOAD_FUNCPTR(f)                                      \
186     if((p_##f = wine_dlsym(nc_handle, #f, NULL, 0)) == NULL) \
187     {                                                        \
188         WINE_WARN("Can't find symbol %s\n", #f);             \
189         goto sym_not_found;                                  \
190     }
191
192     LOAD_FUNCPTR(putp)
193     LOAD_FUNCPTR(setupterm)
194     LOAD_FUNCPTR(tigetstr)
195     LOAD_FUNCPTR(tparm)
196
197 #undef LOAD_FUNCPTR
198
199     return TRUE;
200
201 sym_not_found:
202     WINE_MESSAGE(
203       "Wine cannot find certain functions that it needs inside the "
204        CURSES_NAME "\nlibrary.  To enable Wine to use " CURSES_NAME
205       " please upgrade your " CURSES_NAME "\nlibraries\n");
206     wine_dlclose(nc_handle, NULL, 0);
207     nc_handle = NULL;
208     return FALSE;
209 }
210
211 #define putp      p_putp
212 #define setupterm p_setupterm
213 #define tigetstr  p_tigetstr
214 #define tparm     p_tparm
215
216 struct dbkey_descr
217 {
218     enum dbkey_kind {dbk_simple, dbk_complex} kind;
219     DWORD_PTR   p1;
220     DWORD_PTR   p2;
221     DWORD_PTR   p3;
222 };
223
224 struct dbkey_init
225 {
226     const char*         string_normal;
227     const char*         string_xterm;
228     struct dbkey_descr  descr;
229 };
230
231 static struct dbkey_init TERM_dbkey_init[] = {
232     {"kcud1", "kDN",  {dbk_complex, 0x50, 0x28, 0}},
233     {"kcuu1", "kUP",  {dbk_complex, 0x48, 0x26, 0}},
234     {"kcub1", "kLFT", {dbk_complex, 0x4b, 0x25, 0}},
235     {"kcuf1", "kRIT", {dbk_complex, 0x4d, 0x27, 0}},
236     {"khome", "kHOM", {dbk_complex, 0x47, 0x24, 0}},
237     {"kbs",   NULL,   {dbk_simple,  0x7f, 0x00, 0}},
238     {"kf1",   NULL,   {dbk_complex, 0x3b, 0x70, 0}},
239     {"kf2",   NULL,   {dbk_complex, 0x3c, 0x71, 0}},
240     {"kf3",   NULL,   {dbk_complex, 0x3d, 0x72, 0}},
241     {"kf4",   NULL,   {dbk_complex, 0x3e, 0x73, 0}},
242     {"kf5",   NULL,   {dbk_complex, 0x3f, 0x74, 0}},
243     {"kf6",   NULL,   {dbk_complex, 0x40, 0x75, 0}},
244     {"kf7",   NULL,   {dbk_complex, 0x41, 0x76, 0}},
245     {"kf8",   NULL,   {dbk_complex, 0x42, 0x77, 0}},
246     {"kf9",   NULL,   {dbk_complex, 0x43, 0x78, 0}},
247     {"kf10",  NULL,   {dbk_complex, 0x44, 0x79, 0}},
248     {"kf11",  NULL,   {dbk_complex, 0xd9, 0x7a, 0}},
249     {"kf12",  NULL,   {dbk_complex, 0xda, 0x7b, 0}},
250     {"kf13",  NULL,   {dbk_complex, 0x3b, 0x70, SHIFT_PRESSED}},
251     {"kf14",  NULL,   {dbk_complex, 0x3c, 0x71, SHIFT_PRESSED}},
252     {"kf15",  NULL,   {dbk_complex, 0x3d, 0x72, SHIFT_PRESSED}},
253     {"kf16",  NULL,   {dbk_complex, 0x3e, 0x73, SHIFT_PRESSED}},
254     {"kf17",  NULL,   {dbk_complex, 0x3f, 0x74, SHIFT_PRESSED}},
255     {"kf18",  NULL,   {dbk_complex, 0x40, 0x75, SHIFT_PRESSED}},
256     {"kf19",  NULL,   {dbk_complex, 0x41, 0x76, SHIFT_PRESSED}},
257     {"kf20",  NULL,   {dbk_complex, 0x42, 0x77, SHIFT_PRESSED}},
258     {"kf21",  NULL,   {dbk_complex, 0x43, 0x78, SHIFT_PRESSED}},
259     {"kf22",  NULL,   {dbk_complex, 0x44, 0x79, SHIFT_PRESSED}},
260     {"kf23",  NULL,   {dbk_complex, 0xd9, 0x7a, SHIFT_PRESSED}},
261     {"kf24",  NULL,   {dbk_complex, 0xda, 0x7b, SHIFT_PRESSED}},
262     {"kf25",  NULL,   {dbk_complex, 0x3b, 0x70, LEFT_CTRL_PRESSED}},
263     {"kf26",  NULL,   {dbk_complex, 0x3c, 0x71, LEFT_CTRL_PRESSED}},
264     {"kf27",  NULL,   {dbk_complex, 0x3d, 0x72, LEFT_CTRL_PRESSED}},
265     {"kf28",  NULL,   {dbk_complex, 0x3e, 0x73, LEFT_CTRL_PRESSED}},
266     {"kf29",  NULL,   {dbk_complex, 0x3f, 0x74, LEFT_CTRL_PRESSED}},
267     {"kf30",  NULL,   {dbk_complex, 0x40, 0x75, LEFT_CTRL_PRESSED}},
268     {"kf31",  NULL,   {dbk_complex, 0x41, 0x76, LEFT_CTRL_PRESSED}},
269     {"kf32",  NULL,   {dbk_complex, 0x42, 0x77, LEFT_CTRL_PRESSED}},
270     {"kf33",  NULL,   {dbk_complex, 0x43, 0x78, LEFT_CTRL_PRESSED}},
271     {"kf34",  NULL,   {dbk_complex, 0x44, 0x79, LEFT_CTRL_PRESSED}},
272     {"kf35",  NULL,   {dbk_complex, 0xd9, 0x7a, LEFT_CTRL_PRESSED}},
273     {"kf36",  NULL,   {dbk_complex, 0xda, 0x7b, LEFT_CTRL_PRESSED}},
274     {"kf37",  NULL,   {dbk_complex, 0x3b, 0x70, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
275     {"kf38",  NULL,   {dbk_complex, 0x3c, 0x71, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
276     {"kf39",  NULL,   {dbk_complex, 0x3d, 0x72, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
277     {"kf40",  NULL,   {dbk_complex, 0x3e, 0x73, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
278     {"kf41",  NULL,   {dbk_complex, 0x3f, 0x74, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
279     {"kf42",  NULL,   {dbk_complex, 0x40, 0x75, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
280     {"kf43",  NULL,   {dbk_complex, 0x41, 0x76, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
281     {"kf44",  NULL,   {dbk_complex, 0x42, 0x77, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
282     {"kf45",  NULL,   {dbk_complex, 0x43, 0x78, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
283     {"kf46",  NULL,   {dbk_complex, 0x44, 0x79, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
284     {"kf47",  NULL,   {dbk_complex, 0xd9, 0x7a, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
285     {"kf48",  NULL,   {dbk_complex, 0xda, 0x7b, LEFT_CTRL_PRESSED|SHIFT_PRESSED}},
286     {"kf49",  NULL,   {dbk_complex, 0x3b, 0x70, LEFT_ALT_PRESSED}},
287     {"kf50",  NULL,   {dbk_complex, 0x3c, 0x71, LEFT_ALT_PRESSED}},
288     {"kf51",  NULL,   {dbk_complex, 0x3d, 0x72, LEFT_ALT_PRESSED}},
289     {"kf52",  NULL,   {dbk_complex, 0x3e, 0x73, LEFT_ALT_PRESSED}},
290     {"kf53",  NULL,   {dbk_complex, 0x3f, 0x74, LEFT_ALT_PRESSED}},
291     {"kf54",  NULL,   {dbk_complex, 0x40, 0x75, LEFT_ALT_PRESSED}},
292     {"kf55",  NULL,   {dbk_complex, 0x41, 0x76, LEFT_ALT_PRESSED}},
293     {"kf56",  NULL,   {dbk_complex, 0x42, 0x77, LEFT_ALT_PRESSED}},
294     {"kf57",  NULL,   {dbk_complex, 0x43, 0x78, LEFT_ALT_PRESSED}},
295     {"kf58",  NULL,   {dbk_complex, 0x44, 0x79, LEFT_ALT_PRESSED}},
296     {"kf59",  NULL,   {dbk_complex, 0xd9, 0x7a, LEFT_ALT_PRESSED}},
297     {"kf60",  NULL,   {dbk_complex, 0xda, 0x7b, LEFT_ALT_PRESSED}},
298     {"kf61",  NULL,   {dbk_complex, 0x3b, 0x70, LEFT_ALT_PRESSED|SHIFT_PRESSED}},
299     {"kf62",  NULL,   {dbk_complex, 0x3c, 0x71, LEFT_ALT_PRESSED|SHIFT_PRESSED}},
300     {"kf63",  NULL,   {dbk_complex, 0x3d, 0x72, LEFT_ALT_PRESSED|SHIFT_PRESSED}},
301
302     {"kdch1", "kDC",  {dbk_complex, 0x53, 0x2e, 0}},
303     {"kich1", "kIC",  {dbk_complex, 0x52, 0x2d, 0}},
304     {"knp",   "kNXT", {dbk_complex, 0x51, 0x22, 0}},
305     {"kpp",   "kPRV", {dbk_complex, 0x49, 0x21, 0}},
306     {"kcbt",  NULL,   {dbk_simple,  0x09, 0x00, SHIFT_PRESSED}},
307     {"kend",  "kEND", {dbk_complex, 0x4f, 0x23, 0}},
308
309     /* {"kmous", NULL, }, */
310     /* Still some keys to manage:
311        KEY_DL           KEY_IL          KEY_EIC         KEY_CLEAR               KEY_EOS
312        KEY_EOL          KEY_SF          KEY_SR          KEY_STAB                KEY_CTAB
313        KEY_CATAB        KEY_ENTER       KEY_SRESET      KEY_RESET               KEY_PRINT
314        KEY_LL           KEY_A1          KEY_A3          KEY_B2                  KEY_C1
315        KEY_C3           KEY_BEG         KEY_CANCEL      KEY_CLOSE               KEY_COMMAND
316        KEY_COPY         KEY_CREATE      KEY_EXIT        KEY_FIND                KEY_HELP
317        KEY_MARK         KEY_MESSAGE     KEY_MOVE        KEY_NEXT                KEY_OPEN
318        KEY_OPTIONS      KEY_PREVIOUS    KEY_REDO        KEY_REFERENCE           KEY_REFRESH
319        KEY_REPLACE      KEY_RESTART     KEY_RESUME      KEY_SAVE                KEY_SBEG
320        KEY_SCANCEL      KEY_SCOMMAND    KEY_SCOPY       KEY_SCREATE             KEY_RESIZE
321        KEY_SDL          KEY_SELECT      KEY_SEOL        KEY_SEXIT               KEY_SFIND
322        KEY_SHELP        KEY_SMESSAGE    KEY_SMOVE       KEY_SNEXT               KEY_SOPTIONS
323        KEY_SPREVIOUS    KEY_SPRINT      KEY_SREDO       KEY_SREPLACE            KEY_SRSUME
324        KEY_SSAVE        KEY_SSUSPEND    KEY_SUNDO       KEY_SUSPEND             KEY_UNDO
325     */
326 };
327
328 struct dbkey_pair
329 {
330     const char*         string;
331     unsigned            string_len;
332     struct dbkey_descr  descr;
333 };
334
335 static struct dbkey_pair*       TERM_dbkey;
336 static unsigned                 TERM_dbkey_size;
337 static unsigned                 TERM_dbkey_index;
338
339 static int  TERM_dbkey_cmp(const void* p1, const void* p2)
340 {
341     const struct dbkey_pair*  kp1 = p1;
342     const struct dbkey_pair*  kp2 = p2;
343     return strcmp(kp1->string, kp2->string);
344 }
345
346 static BOOL TERM_AddKeyDescr(const char* string, struct dbkey_descr* descr)
347 {
348     if (!string || string == (const char*)-1) return TRUE;
349     if (!TERM_dbkey)
350     {
351         TERM_dbkey_size = 32;
352         TERM_dbkey = HeapAlloc(GetProcessHeap(), 0, TERM_dbkey_size * sizeof(struct dbkey_pair));
353         if (!TERM_dbkey) return FALSE;
354     }
355     if (TERM_dbkey_index == TERM_dbkey_size)
356     {
357         struct dbkey_pair*      new;
358
359         new = HeapReAlloc(GetProcessHeap(), 0, TERM_dbkey, (2 * TERM_dbkey_size) * sizeof(struct dbkey_pair));
360         if (!new) return FALSE;
361         TERM_dbkey = new;
362         TERM_dbkey_size *= 2;
363     }
364     TERM_dbkey[TERM_dbkey_index].string     = string;
365     TERM_dbkey[TERM_dbkey_index].string_len = strlen(string);
366     TERM_dbkey[TERM_dbkey_index].descr      = *descr;
367     TERM_dbkey_index++;
368     return TRUE;
369 }
370
371 static BOOL TERM_BuildKeyDB(void)
372 {
373     unsigned i, j, len;
374     struct dbkey_descr descr;
375     char tmp[64];
376
377     for (i = 0; i < sizeof(TERM_dbkey_init) / sizeof(TERM_dbkey_init[0]); i++)
378     {
379         if (!TERM_AddKeyDescr(tigetstr((char *)TERM_dbkey_init[i].string_normal), &TERM_dbkey_init[i].descr))
380             return FALSE;
381         if (TERM_dbkey_init[i].string_xterm)
382         {
383             descr = TERM_dbkey_init[i].descr;
384             strcpy(tmp, TERM_dbkey_init[i].string_xterm);
385             len = strlen(tmp);
386             tmp[len + 1] = '\0';
387 #define X(v, f) do { tmp[len] = v; descr.p3 = (f); if (!TERM_AddKeyDescr(tigetstr(tmp), &descr)) return FALSE; } while (0)
388             X('\0', SHIFT_PRESSED);
389             X('3',  LEFT_ALT_PRESSED);
390             X('4',  SHIFT_PRESSED | LEFT_ALT_PRESSED);
391             X('5',  LEFT_CTRL_PRESSED);
392             X('6',  LEFT_CTRL_PRESSED|SHIFT_PRESSED);
393             X('7',  LEFT_CTRL_PRESSED|LEFT_ALT_PRESSED);
394             X('8',  LEFT_CTRL_PRESSED|LEFT_ALT_PRESSED|SHIFT_PRESSED);
395 #undef X
396         }
397     }
398     for (i = 0; i < TERM_dbkey_index; i++)
399     {
400         for (j = 0; j < TERM_dbkey_index; j++)
401         {
402             if (i != j &&
403                 TERM_dbkey[i].string_len >= TERM_dbkey[j].string_len &&
404                 !memcmp(TERM_dbkey[i].string, TERM_dbkey[j].string, TERM_dbkey[j].string_len))
405                 FIXME("substring %d/%s %d/%s\n", i, TERM_dbkey[i].string, j, TERM_dbkey[j].string);
406         }
407     }
408     qsort(TERM_dbkey, TERM_dbkey_index, sizeof(TERM_dbkey[0]), TERM_dbkey_cmp);
409     return TRUE;
410 }
411
412 static BOOL TERM_init_done /* = FALSE */;
413
414 BOOL TERM_Init(void)
415 {
416     /* if we're not attached to a tty, don't fire the curses support */
417     if (!isatty(0) || !isatty(1)) return FALSE;
418     if (!getenv("TERM")) return FALSE;
419     if (!TERM_bind_libcurses()) return FALSE;
420     if (setupterm(NULL, 1 /* really ?? */, NULL) == -1) return FALSE;
421     TERM_init_done = TRUE;
422     TERM_BuildKeyDB();
423     /* set application key mode */
424     putp(tigetstr((char *)"smkx"));
425     return TRUE;
426 }
427
428 BOOL TERM_Exit(void)
429 {
430     if (TERM_init_done)
431     {
432         /* put back the cursor key mode */
433         putp(tigetstr((char *)"rmkx"));
434     }
435     return TRUE;
436 }
437
438 /* -1 not found, 0 cannot decide, > 0 found */
439 int TERM_FillInputRecord(const char* in, size_t len, INPUT_RECORD* ir)
440 {
441     int low = 0, high = TERM_dbkey_index - 1, mid, res;
442     struct dbkey_descr* found;
443
444     while (low <= high)
445     {
446         mid = low + (high - low) / 2;
447         res = memcmp(in, TERM_dbkey[mid].string, len);
448         if (!res)
449         {
450             if (len < TERM_dbkey[mid].string_len) return 0;
451             found = &TERM_dbkey[mid].descr;
452             switch (found->kind)
453             {
454             case dbk_simple:
455                 return TERM_FillSimpleChar(found->p1, ir);
456             case dbk_complex:
457                 init_complex_char(&ir[0], 1, found->p1, found->p2, ENHANCED_KEY | found->p3);
458                 init_complex_char(&ir[1], 0, found->p1, found->p2, ENHANCED_KEY | found->p3);
459                 return 2;
460             }
461             return -1;
462         }
463         else if (res < 0) high = mid - 1;
464         else low = mid + 1;
465     }
466     return -1;
467 }
468
469 #else
470 BOOL     TERM_Init(void) {return FALSE;}
471 BOOL     TERM_Exit(void) {return FALSE;}
472 int      TERM_FillInputRecord(const char* in, size_t len, INPUT_RECORD* ir) {return -1;}
473 #endif