diff --git a/Makefile b/Makefile index c3b5261..36da6cc 100644 --- a/Makefile +++ b/Makefile @@ -28,9 +28,11 @@ LIBDIR = ${PREFIX}/lib/ ifdef WITHOUT_READLINE READLINE_CFLAGS = READLINE_LIBS = + CONSOLE_INPUT_OBJ = ui/input_console.o else READLINE_CFLAGS = -DUSE_READLINE READLINE_LIBS = -lreadline + CONSOLE_INPUT_OBJ = ui/input_readline.o endif ifeq ($(OS),Windows_NT) @@ -195,8 +197,8 @@ OBJ=\ ui/aliasdb.o \ ui/power.o \ ui/input.o \ - ui/input_console.o \ ui/input_async.o \ + $(CONSOLE_INPUT_OBJ) \ ui/main.o $(BINARY): $(OBJ) diff --git a/ui/input.c b/ui/input.c index d21788f..2943b4e 100644 --- a/ui/input.c +++ b/ui/input.c @@ -17,7 +17,19 @@ */ #include "input.h" + +#ifdef USE_READLINE + +#include "input_readline.h" + +/* Default input module */ +const struct input_interface *input_module = &input_readline; + +#else + #include "input_console.h" /* Default input module */ const struct input_interface *input_module = &input_console; + +#endif diff --git a/ui/input_console.c b/ui/input_console.c index e9ca1aa..6c78180 100644 --- a/ui/input_console.c +++ b/ui/input_console.c @@ -21,15 +21,9 @@ #include #include -#ifdef USE_READLINE -#include -#include -#endif - #include "input_console.h" #include "util.h" -#ifndef USE_READLINE #define LINE_BUF_SIZE 128 static char *readline(const char *prompt) @@ -66,9 +60,6 @@ static char *readline(const char *prompt) return NULL; } -#define add_history(x) -#endif - static int console_init(void) { return 0; @@ -85,9 +76,6 @@ static int console_read_command(char *out, int max_len) return 1; } - if (*buf) - add_history(buf); - strncpy(out, buf, max_len); out[max_len - 1] = 0; free(buf); diff --git a/ui/input_readline.c b/ui/input_readline.c new file mode 100644 index 0000000..5b20674 --- /dev/null +++ b/ui/input_readline.c @@ -0,0 +1,84 @@ +/* MSPDebug - debugging tool for MSP430 MCUs + * Copyright (C) 2009-2016 Daniel Beer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +#include +#include + +#include "input_readline.h" + +static int readline_init(void) +{ + return 0; +} + +static void readline_exit(void) { } + +static int readline_read_command(char *out, int max_len) +{ + char *buf = readline("(mspdebug) "); + + if (!buf) { + printf("\n"); + return 1; + } + + if (*buf) + add_history(buf); + + strncpy(out, buf, max_len); + out[max_len - 1] = 0; + free(buf); + + return 0; +} + +static int readline_prompt_abort(const char *message) +{ + char buf[32]; + + for (;;) { + printf("%s ", message); + fflush(stdout); + + if (!fgets(buf, sizeof(buf), stdin)) { + printf("\n"); + return 1; + } + + if (toupper(buf[0]) == 'Y') + return 0; + if (toupper(buf[0]) == 'N') + return 1; + + printf("Please answer \"y\" or \"n\".\n"); + } + + return 0; +} + +const struct input_interface input_readline = { + .init = readline_init, + .exit = readline_exit, + .read_command = readline_read_command, + .prompt_abort = readline_prompt_abort +}; diff --git a/ui/input_readline.h b/ui/input_readline.h new file mode 100644 index 0000000..fcaf8c1 --- /dev/null +++ b/ui/input_readline.h @@ -0,0 +1,26 @@ +/* MSPDebug - debugging tool for MSP430 MCUs + * Copyright (C) 2009-2016 Daniel Beer + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef INPUT_READLINE_H_ +#define INPUT_READLINE_H_ + +#include "input.h" + +extern const struct input_interface input_readline; + +#endif