From 903b24f04197bec266882d2ae5cd752dd88ae74d Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 1 Jan 2014 14:54:39 +0200 Subject: [PATCH 1/2] Add readline history support. --- unix/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/unix/main.c b/unix/main.c index 73da1ecfc2f36..177078fa32328 100644 --- a/unix/main.c +++ b/unix/main.c @@ -16,6 +16,7 @@ #include "repl.h" #include +#include static char *str_join(const char *s1, int sep_char, const char *s2) { int l1 = strlen(s1); @@ -38,6 +39,7 @@ static void do_repl(void) { // EOF return; } + add_history(line); if (mp_repl_is_compound_stmt(line)) { for (;;) { char *line2 = readline("... "); From fa027672da6c33356ff83ffee1d8bca4d52b9656 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 1 Jan 2014 18:28:01 +0200 Subject: [PATCH 2/2] Make GNU Readline usage optional (USE_READLINE define). Still enabled. Readline is GPL, so linking with it casts the binary GPL. --- unix/Makefile | 2 +- unix/main.c | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/unix/Makefile b/unix/Makefile index 0ddf11539a090..17a680a13bbdd 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 177078fa32328..79fe1b5153ce6 100644 --- a/unix/main.c +++ b/unix/main.c @@ -15,8 +15,10 @@ #include "runtime.h" #include "repl.h" +#ifdef USE_READLINE #include #include +#endif static char *str_join(const char *s1, int sep_char, const char *s2) { int l1 = strlen(s1); @@ -32,17 +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; } - add_history(line); if (mp_repl_is_compound_stmt(line)) { for (;;) { - char *line2 = readline("... "); + char *line2 = prompt("... "); if (line2 == NULL || strlen(line2) == 0) { break; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy