Added readline support.

This commit is contained in:
Daniel Beer 2010-03-23 15:49:33 +13:00
parent f775e52fb6
commit 2fa53d6e9d
2 changed files with 46 additions and 11 deletions

View File

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

45
main.c
View File

@ -23,6 +23,11 @@
#include <errno.h>
#include <unistd.h>
#ifdef USE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#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");