summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-01 11:17:27 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-01 11:17:27 -0800
commit17b161333b6a4c8d942de1582df310f519e667e1 (patch)
treed9c5abdf440343b8a10b2220d3dda7419e767f42
parent209d1b18355836c382f7a1179f80207b14bca92a (diff)
parentfa027672da6c33356ff83ffee1d8bca4d52b9656 (diff)
downloadmicropython-17b161333b6a4c8d942de1582df310f519e667e1.tar.gz
micropython-17b161333b6a4c8d942de1582df310f519e667e1.zip
Merge pull request #21 from pfalcon/readline-improve
Support Readline history and make it optional
-rw-r--r--unix/Makefile2
-rw-r--r--unix/main.c32
2 files changed, 31 insertions, 3 deletions
diff --git a/unix/Makefile b/unix/Makefile
index 0ddf11539a..17a680a13b 100644
--- a/unix/Makefile
+++ b/unix/Makefile
@@ -2,7 +2,7 @@ PYSRC=../py
BUILD=build
CC = gcc
-CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG
+CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os -DUSE_READLINE #-DNDEBUG
LDFLAGS = -lm
SRC_C = \
diff --git a/unix/main.c b/unix/main.c
index 73da1ecfc2..79fe1b5153 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -15,7 +15,10 @@
#include "runtime.h"
#include "repl.h"
+#ifdef USE_READLINE
#include <readline/readline.h>
+#include <readline/history.h>
+#endif
static char *str_join(const char *s1, int sep_char, const char *s2) {
int l1 = strlen(s1);
@@ -31,16 +34,41 @@ static char *str_join(const char *s1, int sep_char, const char *s2) {
return s;
}
+static char *prompt(char *p) {
+#ifdef USE_READLINE
+ char *line = readline(p);
+ if (line) {
+ add_history(line);
+ }
+#else
+ static char buf[256];
+ fputs(p, stdout);
+ char *s = fgets(buf, sizeof(buf), stdin);
+ if (!s) {
+ return NULL;
+ }
+ int l = strlen(buf);
+ if (buf[l - 1] == '\n') {
+ buf[l - 1] = 0;
+ } else {
+ l++;
+ }
+ char *line = m_new(char, l);
+ memcpy(line, buf, l);
+#endif
+ return line;
+}
+
static void do_repl(void) {
for (;;) {
- char *line = readline(">>> ");
+ char *line = prompt(">>> ");
if (line == NULL) {
// EOF
return;
}
if (mp_repl_is_compound_stmt(line)) {
for (;;) {
- char *line2 = readline("... ");
+ char *line2 = prompt("... ");
if (line2 == NULL || strlen(line2) == 0) {
break;
}