From e902b579b4eb5f695680427cb34725159a4fa64f Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Wed, 3 Nov 2010 17:10:12 +0100 Subject: [PATCH] Import linenoise as dependency for redis-cli --- deps/linenoise/.gitignore | 1 + deps/linenoise/Makefile | 10 +++++++ deps/linenoise/README.markdown | 45 +++++++++++++++++++++++++++++ deps/linenoise/example.c | 18 ++++++++++++ {src => deps/linenoise}/linenoise.c | 16 ++++++++-- {src => deps/linenoise}/linenoise.h | 0 src/Makefile | 11 ++++--- 7 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 deps/linenoise/.gitignore create mode 100644 deps/linenoise/Makefile create mode 100644 deps/linenoise/README.markdown create mode 100644 deps/linenoise/example.c rename {src => deps/linenoise}/linenoise.c (96%) rename {src => deps/linenoise}/linenoise.h (100%) diff --git a/deps/linenoise/.gitignore b/deps/linenoise/.gitignore new file mode 100644 index 00000000..28f258a3 --- /dev/null +++ b/deps/linenoise/.gitignore @@ -0,0 +1 @@ +linenoise_example* diff --git a/deps/linenoise/Makefile b/deps/linenoise/Makefile new file mode 100644 index 00000000..e4a5476d --- /dev/null +++ b/deps/linenoise/Makefile @@ -0,0 +1,10 @@ +linenoise_example: linenoise.h linenoise.c + +linenoise_example: linenoise.o example.o + $(CC) -Wall -W -Os -g -o linenoise_example linenoise.o example.o + +.c.o: + $(CC) -c -Wall -W -Os -g $< + +clean: + rm -f linenoise_example diff --git a/deps/linenoise/README.markdown b/deps/linenoise/README.markdown new file mode 100644 index 00000000..9612da47 --- /dev/null +++ b/deps/linenoise/README.markdown @@ -0,0 +1,45 @@ +# Linenoise + +A minimal, zero-config, BSD licensed, readline replacement. + +News: linenoise is now part of [Android](http://android.git.kernel.org/?p=platform/system/core.git;a=tree;f=liblinenoise;h=56450eaed7f783760e5e6a5993ef75cde2e29dea;hb=HEAD Android)! + +## Can a line editing library be 20k lines of code? + +Line editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing? + +So what usually happens is either: + + * Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (Readl world example of this problem: Tclsh). + * Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance). + +The result is a pollution of binaries without line editing support. + +So I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporing line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to linenoise if not. + +## Terminals, in 2010. + +Apparently almost every terminal you can happen to use today has some kind of support for VT100 alike escape sequences. So I tried to write a lib using just very basic VT100 features. The resulting library appears to work everywhere I tried to use it. + +Since it's so young I guess there are a few bugs, or the lib may not compile or work with some operating system, but it's a matter of a few weeks and eventually we'll get it right, and there will be no excuses for not shipping command line tools without built-in line editing support. + +The library is currently less than 400 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is trivial. Linenoise is BSD code, so you can use both in free software and commercial software. + +## Tested with... + + * Linux text only console ($TERM = linux) + * Linux KDE terminal application ($TERM = xterm) + * Linux xterm ($TERM = xterm) + * Mac OS X iTerm ($TERM = xterm) + * Mac OS X default Terminal.app ($TERM = xterm) + * OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen) + * IBM AIX 6.1 + * FreeBSD xterm ($TERM = xterm) + +Please test it everywhere you can and report back! + +## Let's push this forward! + +Please fork it and add something interesting and send me a pull request. What's especially interesting are fixes, new key bindings, completion. + +Send feedbacks to antirez at gmail diff --git a/deps/linenoise/example.c b/deps/linenoise/example.c new file mode 100644 index 00000000..b3f9e9e3 --- /dev/null +++ b/deps/linenoise/example.c @@ -0,0 +1,18 @@ +#include +#include +#include "linenoise.h" + +int main(void) { + char *line; + + linenoiseHistoryLoad("history.txt"); /* Load the history at startup */ + while((line = linenoise("hello> ")) != NULL) { + if (line[0] != '\0') { + printf("echo: '%s'\n", line); + linenoiseHistoryAdd(line); + linenoiseHistorySave("history.txt"); /* Save every new entry */ + } + free(line); + } + return 0; +} diff --git a/src/linenoise.c b/deps/linenoise/linenoise.c similarity index 96% rename from src/linenoise.c rename to deps/linenoise/linenoise.c index 54f5a27d..045862e7 100644 --- a/src/linenoise.c +++ b/deps/linenoise/linenoise.c @@ -69,8 +69,6 @@ * */ -#include "fmacros.h" - #include #include #include @@ -215,7 +213,7 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt) while(1) { char c; int nread; - char seq[2]; + char seq[2], seq2[2]; nread = read(fd,&c,1); if (nread <= 0) return len; @@ -296,6 +294,18 @@ up_down_arrow: len = pos = strlen(buf); refreshLine(fd,prompt,buf,len,pos,cols); } + } else if (seq[0] == 91 && seq[1] > 48 && seq[1] < 55) { + /* extended escape */ + if (read(fd,seq2,2) == -1) break; + if (seq[1] == 51 && seq2[0] == 126) { + /* delete */ + if (len > 0 && pos < len) { + memmove(buf+pos,buf+pos+1,len-pos-1); + len--; + buf[len] = '\0'; + refreshLine(fd,prompt,buf,len,pos,cols); + } + } } break; default: diff --git a/src/linenoise.h b/deps/linenoise/linenoise.h similarity index 100% rename from src/linenoise.h rename to deps/linenoise/linenoise.h diff --git a/src/Makefile b/src/Makefile index bd362bc0..bbba695c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,7 +26,7 @@ INSTALL= cp -p OBJ = adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o vm.o pubsub.o multi.o debug.o sort.o intset.o syncio.o BENCHOBJ = ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o -CLIOBJ = anet.o sds.o adlist.o redis-cli.o zmalloc.o linenoise.o +CLIOBJ = anet.o sds.o adlist.o redis-cli.o zmalloc.o CHECKDUMPOBJ = redis-check-dump.o lzf_c.o lzf_d.o CHECKAOFOBJ = redis-check-aof.o @@ -56,7 +56,6 @@ debug.o: debug.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h sha1.h dict.o: dict.c fmacros.h dict.h zmalloc.h intset.o: intset.c intset.h zmalloc.h -linenoise.o: linenoise.c fmacros.h lzf_c.o: lzf_c.c lzfP.h lzf_d.o: lzf_d.c lzfP.h multi.o: multi.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ @@ -74,8 +73,7 @@ redis-benchmark.o: redis-benchmark.c fmacros.h ae.h anet.h sds.h adlist.h \ zmalloc.h redis-check-aof.o: redis-check-aof.c fmacros.h config.h redis-check-dump.o: redis-check-dump.c lzf.h -redis-cli.o: redis-cli.c fmacros.h version.h anet.h sds.h adlist.h \ - zmalloc.h linenoise.h +redis-cli.o: redis-cli.c fmacros.h version.h sds.h adlist.h zmalloc.h redis.o: redis.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \ zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h release.o: release.c release.h @@ -115,10 +113,11 @@ redis-benchmark: $(BENCHOBJ) redis-cli: $(CLIOBJ) cd ../deps/hiredis && make static - $(CC) -o $(CLIPRGNAME) $(CCOPT) $(DEBUG) $(CLIOBJ) ../deps/hiredis/libhiredis.a + cd ../deps/linenoise && make + $(CC) -o $(CLIPRGNAME) $(CCOPT) $(DEBUG) $(CLIOBJ) ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o redis-cli.o: - $(CC) -c $(CFLAGS) -I../deps/hiredis $(DEBUG) $(COMPILE_TIME) $< + $(CC) -c $(CFLAGS) -I../deps/hiredis -I../deps/linenoise $(DEBUG) $(COMPILE_TIME) $< redis-check-dump: $(CHECKDUMPOBJ) $(CC) -o $(CHECKDUMPPRGNAME) $(CCOPT) $(DEBUG) $(CHECKDUMPOBJ) -- GitLab