From 2fa53d6e9dea5129922b749d679c4f21eeb99a3d Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Tue, 23 Mar 2010 15:49:33 +1300 Subject: [PATCH] Added readline support. --- Makefile | 12 ++++++++++-- main.c | 45 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index b50a0f9..d02e125 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,14 @@ CC = gcc INSTALL = /usr/bin/install PREFIX ?= /usr/local +ifdef WITHOUT_READLINE +READLINE_CFLAGS = +READLINE_LIBS = +else +READLINE_CFLAGS = -DUSE_READLINE +READLINE_LIBS = -lreadline +endif + all: mspdebug clean: @@ -33,7 +41,7 @@ install: mspdebug mspdebug.man mspdebug: main.o fet.o rf2500.o dis.o uif.o ihex.o elf32.o stab.o util.o \ bsl.o sim.o symmap.o gdb.o - $(CC) $(CFLAGS) -o $@ $^ -lusb + $(CC) $(LDFLAGS) -o $@ $^ -lusb $(READLINE_LIBS) .c.o: - $(CC) $(CFLAGS) -O1 -Wall -o $@ -c $*.c + $(CC) $(CFLAGS) $(READLINE_CFLAGS) -O1 -Wall -o $@ -c $*.c diff --git a/main.c b/main.c index 50fcfcd..9edffaa 100644 --- a/main.c +++ b/main.c @@ -23,6 +23,11 @@ #include #include +#ifdef USE_READLINE +#include +#include +#endif + #include "dis.h" #include "device.h" #include "binfile.h" @@ -921,24 +926,46 @@ static void usage(const char *progname) progname, progname, progname, progname); } +#ifndef USE_READLINE +#define LINE_BUF_SIZE 128 + +static char *readline(const char *prompt) +{ + char *buf = malloc(LINE_BUF_SIZE); + + if (!buf) { + perror("readline: can't allocate memory"); + return NULL; + } + + do { + printf("(mspdebug) "); + + if (!fgets(buf, LINE_BUF_SIZE, stdin)) + return buf; + } while (!feof(stdin)); + + free(buf); + return NULL; +} + +#define add_history(x) +#endif + static void reader_loop(void) { printf("\n"); cmd_help(NULL); for (;;) { - char buf[128]; + char *buf = readline("(mspdebug) "); - printf("(mspdebug) "); - fflush(stdout); - if (!fgets(buf, sizeof(buf), stdin)) { - if (feof(stdin)) - break; - printf("\n"); - continue; - } + if (!buf) + break; + add_history(buf); process_command(buf); + free(buf); } printf("\n");