Separate input drivers for GNU readline and plain console input.

This commit is contained in:
Ingo van Lil 2016-01-23 18:37:54 +01:00
parent 0b52f40933
commit 811567a7bf
5 changed files with 125 additions and 13 deletions

View File

@ -28,9 +28,11 @@ LIBDIR = ${PREFIX}/lib/
ifdef WITHOUT_READLINE ifdef WITHOUT_READLINE
READLINE_CFLAGS = READLINE_CFLAGS =
READLINE_LIBS = READLINE_LIBS =
CONSOLE_INPUT_OBJ = ui/input_console.o
else else
READLINE_CFLAGS = -DUSE_READLINE READLINE_CFLAGS = -DUSE_READLINE
READLINE_LIBS = -lreadline READLINE_LIBS = -lreadline
CONSOLE_INPUT_OBJ = ui/input_readline.o
endif endif
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
@ -195,8 +197,8 @@ OBJ=\
ui/aliasdb.o \ ui/aliasdb.o \
ui/power.o \ ui/power.o \
ui/input.o \ ui/input.o \
ui/input_console.o \
ui/input_async.o \ ui/input_async.o \
$(CONSOLE_INPUT_OBJ) \
ui/main.o ui/main.o
$(BINARY): $(OBJ) $(BINARY): $(OBJ)

View File

@ -17,7 +17,19 @@
*/ */
#include "input.h" #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" #include "input_console.h"
/* Default input module */ /* Default input module */
const struct input_interface *input_module = &input_console; const struct input_interface *input_module = &input_console;
#endif

View File

@ -21,15 +21,9 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "input_console.h" #include "input_console.h"
#include "util.h" #include "util.h"
#ifndef USE_READLINE
#define LINE_BUF_SIZE 128 #define LINE_BUF_SIZE 128
static char *readline(const char *prompt) static char *readline(const char *prompt)
@ -66,9 +60,6 @@ static char *readline(const char *prompt)
return NULL; return NULL;
} }
#define add_history(x)
#endif
static int console_init(void) static int console_init(void)
{ {
return 0; return 0;
@ -85,9 +76,6 @@ static int console_read_command(char *out, int max_len)
return 1; return 1;
} }
if (*buf)
add_history(buf);
strncpy(out, buf, max_len); strncpy(out, buf, max_len);
out[max_len - 1] = 0; out[max_len - 1] = 0;
free(buf); free(buf);

84
ui/input_readline.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <readline/readline.h>
#include <readline/history.h>
#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
};

26
ui/input_readline.h Normal file
View File

@ -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