2014-02-03 03:25:11 +08:00
|
|
|
//===-- EditLineWin.cpp -----------------------------------------*- C++ -*-===//
|
2013-10-15 23:46:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-10-15 23:46:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// this file is only relevant for Visual C++
|
2014-03-12 18:45:23 +08:00
|
|
|
#if defined(_WIN32)
|
2013-10-15 23:46:40 +08:00
|
|
|
|
|
|
|
#include "lldb/Host/windows/windows.h"
|
|
|
|
|
2014-02-01 02:48:46 +08:00
|
|
|
#include "lldb/Host/windows/editlinewin.h"
|
2017-01-06 20:41:15 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-10-15 23:46:40 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// edit line EL_ADDFN function pointer type
|
2014-02-18 01:52:22 +08:00
|
|
|
typedef unsigned char (*el_addfn_func)(EditLine *e, int ch);
|
|
|
|
typedef const char *(*el_prompt_func)(EditLine *);
|
2013-10-15 23:46:40 +08:00
|
|
|
|
|
|
|
// edit line wrapper binding container
|
|
|
|
struct el_binding {
|
|
|
|
//
|
|
|
|
const char *name;
|
|
|
|
const char *help;
|
|
|
|
// function pointer to callback routine
|
|
|
|
el_addfn_func func;
|
|
|
|
// ascii key this function is bound to
|
|
|
|
const char *key;
|
|
|
|
};
|
|
|
|
|
|
|
|
// stored key bindings
|
|
|
|
static std::vector<el_binding *> _bindings;
|
|
|
|
|
2015-07-22 08:16:02 +08:00
|
|
|
// TODO: this should in fact be related to the exact edit line context we create
|
2013-10-15 23:46:40 +08:00
|
|
|
static void *clientData = NULL;
|
|
|
|
|
|
|
|
// store the current prompt string
|
|
|
|
// default to what we expect to receive anyway
|
|
|
|
static const char *_prompt = "(lldb) ";
|
|
|
|
|
|
|
|
#if !defined(_WIP_INPUT_METHOD)
|
|
|
|
|
|
|
|
static char *el_get_s(char *buffer, int chars) { return gets_s(buffer, chars); }
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void con_output(char _in) {
|
|
|
|
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DWORD written = 0;
|
|
|
|
// get the cursor position
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
GetConsoleScreenBufferInfo(hout, &info);
|
|
|
|
// output this char
|
|
|
|
WriteConsoleOutputCharacterA(hout, &_in, 1, info.dwCursorPosition, &written);
|
|
|
|
// advance cursor position
|
|
|
|
info.dwCursorPosition.X++;
|
|
|
|
SetConsoleCursorPosition(hout, info.dwCursorPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void con_backspace(void) {
|
|
|
|
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DWORD written = 0;
|
|
|
|
// get cursor position
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
GetConsoleScreenBufferInfo(hout, &info);
|
|
|
|
// nudge cursor backwards
|
|
|
|
info.dwCursorPosition.X--;
|
|
|
|
SetConsoleCursorPosition(hout, info.dwCursorPosition);
|
|
|
|
// blank out the last character
|
|
|
|
WriteConsoleOutputCharacterA(hout, " ", 1, info.dwCursorPosition, &written);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void con_return(void) {
|
|
|
|
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
DWORD written = 0;
|
|
|
|
// get cursor position
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO info;
|
|
|
|
GetConsoleScreenBufferInfo(hout, &info);
|
|
|
|
// move onto the new line
|
|
|
|
info.dwCursorPosition.X = 0;
|
|
|
|
info.dwCursorPosition.Y++;
|
|
|
|
SetConsoleCursorPosition(hout, info.dwCursorPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool runBind(char _key) {
|
|
|
|
for (int i = 0; i < _bindings.size(); i++) {
|
|
|
|
el_binding *bind = _bindings[i];
|
|
|
|
if (bind->key[0] == _key) {
|
|
|
|
bind->func((EditLine *)-1, _key);
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-10-15 23:46:40 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replacement get_s which is EL_BIND aware
|
|
|
|
static char *el_get_s(char *buffer, int chars) {
|
2016-09-07 04:57:50 +08:00
|
|
|
//
|
2013-10-15 23:46:40 +08:00
|
|
|
char *head = buffer;
|
2016-09-07 04:57:50 +08:00
|
|
|
//
|
2013-10-15 23:46:40 +08:00
|
|
|
for (;; Sleep(10)) {
|
|
|
|
//
|
|
|
|
INPUT_RECORD _record;
|
|
|
|
//
|
|
|
|
DWORD _read = 0;
|
|
|
|
if (ReadConsoleInputA(GetStdHandle(STD_INPUT_HANDLE), &_record, 1,
|
|
|
|
&_read) == FALSE)
|
|
|
|
break;
|
2015-07-22 08:16:02 +08:00
|
|
|
// if we didn't read a key
|
2013-10-15 23:46:40 +08:00
|
|
|
if (_read == 0)
|
|
|
|
continue;
|
|
|
|
// only interested in key events
|
|
|
|
if (_record.EventType != KEY_EVENT)
|
|
|
|
continue;
|
|
|
|
// is the key down
|
|
|
|
if (!_record.Event.KeyEvent.bKeyDown)
|
|
|
|
continue;
|
|
|
|
// read the ascii key character
|
|
|
|
char _key = _record.Event.KeyEvent.uChar.AsciiChar;
|
|
|
|
// non ascii conformant key press
|
|
|
|
if (_key == 0) {
|
|
|
|
// check the scan code
|
|
|
|
// if VK_UP scroll back through history
|
|
|
|
// if VK_DOWN scroll forward through history
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// try to execute any bind this key may have
|
|
|
|
if (runBind(_key))
|
|
|
|
continue;
|
|
|
|
// if we read a return key
|
|
|
|
if (_key == '\n' || _key == '\r') {
|
|
|
|
con_return();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// key is backspace
|
|
|
|
if (_key == 0x8) {
|
|
|
|
// avoid deleting past beginning
|
|
|
|
if (head > buffer) {
|
|
|
|
con_backspace();
|
|
|
|
head--;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add this key to the input buffer
|
|
|
|
if ((head - buffer) < (chars - 1)) {
|
|
|
|
con_output(_key);
|
|
|
|
*(head++) = _key;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-15 23:46:40 +08:00
|
|
|
// insert end of line character
|
|
|
|
*head = '\0';
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-07-22 08:16:02 +08:00
|
|
|
// edit line initialize
|
2013-10-15 23:46:40 +08:00
|
|
|
EditLine *el_init(const char *, FILE *, FILE *, FILE *) {
|
|
|
|
//
|
|
|
|
SetConsoleTitleA("lldb");
|
|
|
|
// return dummy handle
|
|
|
|
return (EditLine *)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *el_gets(EditLine *el, int *length) {
|
|
|
|
// print the prompt if we have one
|
|
|
|
if (_prompt != NULL)
|
2015-04-03 04:57:38 +08:00
|
|
|
printf("%s", _prompt);
|
2013-10-15 23:46:40 +08:00
|
|
|
// create a buffer for the user input
|
2013-10-22 20:27:43 +08:00
|
|
|
char *buffer = new char[MAX_PATH];
|
2013-10-15 23:46:40 +08:00
|
|
|
// try to get user input string
|
2013-10-22 20:27:43 +08:00
|
|
|
if (el_get_s(buffer, MAX_PATH)) {
|
2013-10-15 23:46:40 +08:00
|
|
|
// get the string length in 'length'
|
|
|
|
while (buffer[*length] != '\0')
|
|
|
|
(*length)++;
|
|
|
|
// return the input buffer
|
|
|
|
// remember that this memory has the be free'd somewhere
|
|
|
|
return buffer;
|
|
|
|
} else {
|
|
|
|
// on error
|
|
|
|
delete[] buffer;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int el_set(EditLine *el, int code, ...) {
|
2014-02-18 01:52:22 +08:00
|
|
|
va_list vl;
|
|
|
|
va_start(vl, code);
|
2013-10-15 23:46:40 +08:00
|
|
|
//
|
|
|
|
switch (code) {
|
|
|
|
// edit line set prompt message
|
|
|
|
case (EL_PROMPT): {
|
|
|
|
// EL_PROMPT, char *(*f)( EditLine *)
|
|
|
|
// define a prompt printing function as 'f', which is to return a
|
|
|
|
// string that
|
|
|
|
// contains the prompt.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
// get the function pointer from the arg list
|
2014-02-18 01:52:22 +08:00
|
|
|
void *func_vp = (void *)va_arg(vl, el_prompt_func);
|
2013-10-15 23:46:40 +08:00
|
|
|
// cast to suitable prototype
|
2014-02-18 01:52:22 +08:00
|
|
|
el_prompt_func func_fp = (el_prompt_func)func_vp;
|
2013-10-15 23:46:40 +08:00
|
|
|
// call to get the prompt as a string
|
|
|
|
_prompt = func_fp(el);
|
|
|
|
} break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-02-18 01:52:22 +08:00
|
|
|
case (EL_PROMPT_ESC): {
|
|
|
|
// EL_PROMPT, char *(*f)( EditLine *)
|
|
|
|
// define a prompt printing function as 'f', which is to return a
|
|
|
|
// string that
|
|
|
|
// contains the prompt.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2014-02-18 01:52:22 +08:00
|
|
|
// get the function pointer from the arg list
|
|
|
|
void *func_vp = (void *)va_arg(vl, el_prompt_func);
|
2015-04-03 04:57:38 +08:00
|
|
|
va_arg(vl, int);
|
2014-02-18 01:52:22 +08:00
|
|
|
// call to get the prompt as a string
|
|
|
|
el_prompt_func func_fp = (el_prompt_func)func_vp;
|
2015-04-03 04:57:38 +08:00
|
|
|
_prompt = func_fp(el);
|
2014-02-18 01:52:22 +08:00
|
|
|
} break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
case (EL_EDITOR): {
|
|
|
|
// EL_EDITOR, const char *mode
|
|
|
|
// set editing mode to "emacs" or "vi"
|
|
|
|
} break;
|
|
|
|
case (EL_HIST): {
|
|
|
|
// EL_HIST, History *(*fun)(History *, int op, ... ), const char *ptr
|
2015-07-22 08:16:02 +08:00
|
|
|
// defines which history function to use, which is usually history().
|
|
|
|
// Ptr should be the
|
2013-10-15 23:46:40 +08:00
|
|
|
// value returned by history_init().
|
|
|
|
} break;
|
|
|
|
case (EL_ADDFN): {
|
|
|
|
// EL_ADDFN, const char *name, const char *help, unsigned char
|
|
|
|
// (*func)(EditLine *e, int ch)
|
|
|
|
// add a user defined function, func), referred to as 'name' which is
|
|
|
|
// invoked when a key which is bound to 'name' is
|
2015-07-22 08:16:02 +08:00
|
|
|
// entered. 'help' is a description of 'name'. at invocation time, 'ch'
|
|
|
|
// is the key which caused the invocation. the
|
2013-10-15 23:46:40 +08:00
|
|
|
// return value of 'func()' should be one of:
|
|
|
|
// CC_NORM add a normal character
|
|
|
|
// CC_NEWLINE end of line was entered
|
|
|
|
// CC_EOF EOF was entered
|
|
|
|
// CC_ARGHACK expecting further command input as arguments, do
|
|
|
|
// nothing visually.
|
|
|
|
// CC_REFRESH refresh display.
|
|
|
|
// CC_REFRESH_BEEP refresh display and beep.
|
|
|
|
// CC_CURSOR cursor moved so update and perform CC_REFRESH
|
2015-07-22 08:16:02 +08:00
|
|
|
// CC_REDISPLAY redisplay entire input line. this is useful
|
|
|
|
// if a key binding outputs extra information.
|
|
|
|
// CC_ERROR an error occurred. beep and flush tty.
|
2013-10-15 23:46:40 +08:00
|
|
|
// CC_FATAL fatal error, reset tty to known state.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
el_binding *binding = new el_binding;
|
2014-02-18 01:52:22 +08:00
|
|
|
binding->name = va_arg(vl, const char *);
|
|
|
|
binding->help = va_arg(vl, const char *);
|
|
|
|
binding->func = va_arg(vl, el_addfn_func);
|
2013-10-15 23:46:40 +08:00
|
|
|
binding->key = 0;
|
|
|
|
// add this to the bindings list
|
|
|
|
_bindings.push_back(binding);
|
|
|
|
} break;
|
|
|
|
case (EL_BIND): {
|
|
|
|
// EL_BIND, const char *, ..., NULL
|
2015-07-22 08:16:02 +08:00
|
|
|
// perform the BIND built-in command. Refer to editrc(5) for more
|
|
|
|
// information.
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
const char *name = va_arg(vl, const char *);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2016-10-06 01:07:34 +08:00
|
|
|
for (auto bind : _bindings) {
|
2013-10-15 23:46:40 +08:00
|
|
|
if (strcmp(bind->name, name) == 0) {
|
2014-02-18 01:52:22 +08:00
|
|
|
bind->key = va_arg(vl, const char *);
|
2013-10-15 23:46:40 +08:00
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-10-15 23:46:40 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
} break;
|
2013-10-15 23:46:40 +08:00
|
|
|
case (EL_CLIENTDATA): {
|
2014-03-03 23:39:47 +08:00
|
|
|
clientData = va_arg(vl, void *);
|
2016-09-07 04:57:50 +08:00
|
|
|
} break;
|
|
|
|
}
|
2013-10-15 23:46:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void el_end(EditLine *el) {
|
|
|
|
// assert( !"Not implemented!" );
|
|
|
|
}
|
|
|
|
|
2017-01-06 08:38:06 +08:00
|
|
|
void el_reset(EditLine *) { llvm_unreachable("Not implemented!"); }
|
2013-10-15 23:46:40 +08:00
|
|
|
|
2014-02-01 02:48:46 +08:00
|
|
|
int el_getc(EditLine *, char *) {
|
2017-01-06 08:38:06 +08:00
|
|
|
llvm_unreachable("Not implemented!");
|
2013-10-15 23:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void el_push(EditLine *, const char *) {}
|
|
|
|
|
|
|
|
void el_beep(EditLine *) { Beep(1000, 500); }
|
|
|
|
|
|
|
|
int el_parse(EditLine *, int, const char **) {
|
2017-01-06 08:38:06 +08:00
|
|
|
llvm_unreachable("Not implemented!");
|
2013-10-15 23:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int el_get(EditLine *el, int code, ...) {
|
2014-02-18 01:52:22 +08:00
|
|
|
va_list vl;
|
2013-10-15 23:46:40 +08:00
|
|
|
va_start(vl, code);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
switch (code) {
|
|
|
|
case (EL_CLIENTDATA): {
|
|
|
|
void **dout = va_arg(vl, void **);
|
|
|
|
*dout = clientData;
|
|
|
|
} break;
|
2016-09-07 04:57:50 +08:00
|
|
|
default:
|
2017-01-06 08:38:06 +08:00
|
|
|
llvm_unreachable("Not implemented!");
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-10-15 23:46:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int el_source(EditLine *el, const char *file) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// init edit line by reading the contents of 'file' nothing to do here on
|
|
|
|
// windows...
|
2013-10-15 23:46:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-06 08:38:06 +08:00
|
|
|
void el_resize(EditLine *) { llvm_unreachable("Not implemented!"); }
|
2013-10-15 23:46:40 +08:00
|
|
|
|
|
|
|
const LineInfo *el_line(EditLine *el) { return 0; }
|
|
|
|
|
|
|
|
int el_insertstr(EditLine *, const char *) {
|
|
|
|
// assert( !"Not implemented!" );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-06 08:38:06 +08:00
|
|
|
void el_deletestr(EditLine *, int) { llvm_unreachable("Not implemented!"); }
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-10-15 23:46:40 +08:00
|
|
|
History *history_init(void) {
|
|
|
|
// return dummy handle
|
|
|
|
return (History *)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void history_end(History *) {
|
2014-03-03 23:39:47 +08:00
|
|
|
// assert( !"Not implemented!" );
|
2013-10-15 23:46:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int history(History *, HistEvent *, int op, ...) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// perform operation 'op' on the history list with optional arguments as
|
|
|
|
// needed by the operation.
|
2013-10-15 23:46:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|