]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/llvm/tools/lldb/tools/driver/ELWrapper.cpp
MFC r258054: Update LLDB to upstream r194122 snapshot
[FreeBSD/stable/10.git] / contrib / llvm / tools / lldb / tools / driver / ELWrapper.cpp
1 //===-- ELWrapper.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // this file is only relevant for Visual C++
11 #if defined( _MSC_VER )
12
13 #include "lldb/Host/windows/windows.h"
14
15 #include "ELWrapper.h"
16 #include <vector>
17 #include <assert.h>
18
19 // index one of the variable arguments
20 //  presuming "(EditLine *el, ..." is first in the argument list
21 #define GETARG( X ) ( (void* ) *( ( (int**) &el ) + ((X) + 2) ) )
22
23 // edit line EL_ADDFN function pointer type
24 typedef unsigned char (*el_addfn_func)(EditLine *e, int ch);
25
26 // edit line wrapper binding container
27 struct el_binding
28 {
29     //
30     const char   *name;
31     const char   *help;
32     // function pointer to callback routine
33     el_addfn_func func;
34     // ascii key this function is bound to
35     const char   *key;
36 };
37
38 // stored key bindings
39 static std::vector<el_binding*> _bindings;
40
41 //TODO: this should infact be related to the exact edit line context we create
42 static void *clientData = NULL;
43
44 // store the current prompt string
45 // default to what we expect to receive anyway
46 static const char *_prompt = "(lldb) ";
47
48 #if !defined( _WIP_INPUT_METHOD )
49
50 static char *
51 el_get_s (char *buffer, int chars)
52 {
53     return gets_s(buffer, chars);
54 }
55 #else
56
57 static void
58 con_output (char _in)
59 {
60     HANDLE hout = GetStdHandle( STD_OUTPUT_HANDLE );
61     DWORD written = 0;
62     // get the cursor position
63     CONSOLE_SCREEN_BUFFER_INFO info;
64     GetConsoleScreenBufferInfo( hout, &info );
65     // output this char
66     WriteConsoleOutputCharacterA( hout, &_in, 1, info.dwCursorPosition, &written );
67     // advance cursor position
68     info.dwCursorPosition.X++;
69     SetConsoleCursorPosition( hout, info.dwCursorPosition );
70 }
71
72 static void
73 con_backspace (void)
74 {
75     HANDLE hout = GetStdHandle( STD_OUTPUT_HANDLE );
76     DWORD written = 0;
77     // get cursor position
78     CONSOLE_SCREEN_BUFFER_INFO info;
79     GetConsoleScreenBufferInfo( hout, &info );
80     // nudge cursor backwards
81     info.dwCursorPosition.X--;
82     SetConsoleCursorPosition( hout, info.dwCursorPosition );
83     // blank out the last character
84     WriteConsoleOutputCharacterA( hout, " ", 1, info.dwCursorPosition, &written );
85 }
86
87 static void
88 con_return (void)
89 {
90     HANDLE hout = GetStdHandle( STD_OUTPUT_HANDLE );
91     DWORD written = 0;
92     // get cursor position
93     CONSOLE_SCREEN_BUFFER_INFO info;
94     GetConsoleScreenBufferInfo( hout, &info );
95     // move onto the new line
96     info.dwCursorPosition.X = 0;
97     info.dwCursorPosition.Y++;
98     SetConsoleCursorPosition( hout, info.dwCursorPosition );
99 }
100
101 static bool
102 runBind (char _key)
103 {
104     for ( int i=0; i<_bindings.size(); i++ )
105     {
106         el_binding *bind = _bindings[i];
107         if ( bind->key[0] == _key )
108         {
109             bind->func( (EditLine*) -1, _key );
110             return true;
111         }
112     }
113     return false;
114 }
115
116 // replacement get_s which is EL_BIND aware
117 static char *
118 el_get_s (char *buffer, int chars)
119 {
120     //
121     char *head = buffer;
122     //
123     for ( ;; Sleep( 10 ) )
124     {
125         //
126         INPUT_RECORD _record;
127         //
128         DWORD _read = 0;
129         if ( ReadConsoleInputA( GetStdHandle( STD_INPUT_HANDLE ), &_record, 1, &_read ) == FALSE )
130             break;
131         // if we didnt read a key
132         if ( _read == 0 )
133             continue;
134         // only interested in key events
135         if ( _record.EventType != KEY_EVENT )
136             continue;
137         // is the key down
138         if (! _record.Event.KeyEvent.bKeyDown )
139             continue;
140         // read the ascii key character
141         char _key = _record.Event.KeyEvent.uChar.AsciiChar;
142         // non ascii conformant key press
143         if ( _key == 0 )
144         {
145             // check the scan code
146             // if VK_UP scroll back through history
147             // if VK_DOWN scroll forward through history
148             continue;
149         }
150         // try to execute any bind this key may have
151         if ( runBind( _key ) )
152             continue;
153         // if we read a return key
154         if ( _key == '\n' || _key == '\r' )
155         {
156             con_return( );
157             break;
158         }
159         // key is backspace
160         if ( _key == 0x8 )
161         {
162             // avoid deleting past beginning
163             if ( head > buffer )
164             {
165                 con_backspace( );
166                 head--;
167             }
168             continue;
169         }
170
171         // add this key to the input buffer
172         if ( (head-buffer) < (chars-1) )
173         {
174             con_output( _key );
175             *(head++) = _key;
176         }
177     }
178     // insert end of line character
179     *head = '\0';
180
181     return buffer;
182 }
183 #endif
184
185 // edit line initalise
186 EditLine *
187 el_init (const char *, FILE *, FILE *, FILE *)
188 {
189     //
190     SetConsoleTitleA( "lldb" );
191     // return dummy handle
192     return (EditLine*) -1;
193 }
194
195 const char *
196 el_gets (EditLine *el, int *length)
197 {
198     // print the prompt if we have one
199     if ( _prompt != NULL )
200         printf( _prompt );
201     // create a buffer for the user input
202     char *buffer = new char[ MAX_PATH ];
203     // try to get user input string
204     if ( el_get_s( buffer, MAX_PATH ) )
205     {
206         // get the string length in 'length'
207         while ( buffer[ *length ] != '\0' )
208             (*length)++;
209         // return the input buffer
210         // remember that this memory has the be free'd somewhere
211         return buffer;
212     }
213     else
214     {
215         // on error
216         delete [] buffer;
217         return NULL;
218     }
219 }
220
221 int
222 el_set (EditLine *el, int code, ...)
223 {
224     int **arg = (int**) &el;
225     //
226     switch ( code )
227     {
228     // edit line set prompt message
229     case ( EL_PROMPT ):
230         {
231             // EL_PROMPT, char *(*f)( EditLine *)
232             //      define a prompt printing function as 'f', which is to return a string that
233             //      contains the prompt.
234
235             // get the function pointer from the arg list
236             void *func_vp = (void*) *(arg+2);
237             // cast to suitable prototype
238             const char* (*func_fp)(EditLine*) = (const char*(*)(EditLine *)) func_vp;
239             // call to get the prompt as a string
240             _prompt = func_fp( el );
241         }
242         break;
243     case ( EL_EDITOR ):
244         {
245             // EL_EDITOR, const char *mode
246             //      set editing mode to "emacs" or "vi"
247         }
248         break;
249     case ( EL_HIST ):
250         {
251             // EL_HIST, History *(*fun)(History *, int op, ... ), const char *ptr
252             //      defines which histroy function to use, which is usualy history(). Ptr should be the
253             //      value returned by history_init().
254         }
255         break;
256     case ( EL_ADDFN ):
257         {
258             // EL_ADDFN, const char *name, const char *help, unsigned char (*func)(EditLine *e, int ch)
259             //      add a user defined function, func), referred to as 'name' which is invoked when a key which is bound to 'name' is
260             //      entered. 'help' is a description of 'name'. at involcation time, 'ch' is the key which caused the invocation. the
261             //      return value of 'func()' should be one of:
262             //          CC_NORM         add a normal character
263             //          CC_NEWLINE      end of line was entered
264             //          CC_EOF          EOF was entered
265             //          CC_ARGHACK      expecting further command input as arguments, do nothing visually.
266             //          CC_REFRESH      refresh display.
267             //          CC_REFRESH_BEEP refresh display and beep.
268             //          CC_CURSOR       cursor moved so update and perform CC_REFRESH
269             //          CC_REDISPLAY        redisplay entire input line. this is usefull if a key binding outputs extra information.
270             //          CC_ERROR            an error occured. beep and flush tty.
271             //          CC_FATAL            fatal error, reset tty to known state.
272
273             el_binding *binding = new el_binding;
274             binding->name = (const char *)  GETARG( 0 );
275             binding->help = (const char *)  GETARG( 1 );
276             binding->func = (el_addfn_func) GETARG( 2 );
277             binding->key  = 0;
278             // add this to the bindings list
279             _bindings.push_back( binding );
280         }
281         break;
282     case ( EL_BIND ):
283         {
284             // EL_BIND, const char *, ..., NULL
285             //      perform the BIND buildin command.  Refer to editrc(5) for more information.
286
287             const char *name = (const char*) GETARG( 1 );
288
289             for ( int i=0; i<_bindings.size(); i++ )
290             {
291                 el_binding *bind = _bindings[i];
292                 if ( strcmp( bind->name, name ) == 0 )
293                 {
294                     bind->key = (const char *) GETARG( 0 );
295                     break;
296                 }
297             }
298
299         }
300         break;
301     case ( EL_CLIENTDATA ):
302         {
303             clientData = GETARG( 0 );
304         }
305         break;
306     default:
307         assert( !"Not Implemented!" );
308     }
309     return 0;
310 }
311
312 void
313 el_end (EditLine *el)
314 {
315     assert( !"Not implemented!" );
316 }
317
318 void
319 el_reset (EditLine *)
320 {
321     assert( !"Not implemented!" );
322 }
323
324 int
325 el_getc (EditLine *, char *)
326 {
327     assert( !"Not implemented!" );
328     return 0;
329 }
330
331 void
332 el_push (EditLine *, char *)
333 {
334     assert( !"Not implemented!" );
335 }
336
337 void
338 el_beep (EditLine *)
339 {
340     Beep( 1000, 500 );
341 }
342
343 int
344 el_parse (EditLine *, int, const char **)
345 {
346     assert( !"Not implemented!" );
347     return 0;
348 }
349
350 int
351 el_get (EditLine *el, int code, ...)
352 {
353     switch ( code )
354     {
355     case ( EL_CLIENTDATA ):
356         {
357             void **dout = (void**) GETARG( 0 );
358             *dout = clientData;
359         }
360         break;
361     default:
362         assert( !"Not implemented!" );
363     }
364     return 0;
365 }
366
367 int
368 el_source (EditLine *el, const char *file)
369 {
370     // init edit line by reading the contents of 'file'
371     // nothing to do here on windows...
372     return 0;
373 }
374
375 void
376 el_resize (EditLine *)
377 {
378     assert( !"Not implemented!" );
379 }
380
381 const LineInfo *
382 el_line (EditLine *el)
383 {
384     assert( !"Not implemented!" );
385     return 0;
386 }
387
388 int
389 el_insertstr (EditLine *, const char *)
390 {
391     assert( !"Not implemented!" );
392     return 0;
393 }
394
395 void
396 el_deletestr (EditLine *, int)
397 {
398     assert( !"Not implemented!" );
399 }
400
401 History *
402 history_init (void)
403 {
404     // return dummy handle
405     return (History*) -1;
406 }
407
408 void
409 history_end (History *)
410 {
411     assert( !"Not implemented!" );
412 }
413
414 int
415 history (History *, HistEvent *, int op, ...)
416 {
417     // perform operation 'op' on the history list with optional argumetns as needed by
418     // the operation.
419     return 0;
420 }
421
422 #endif