Browse files

Import linenoise as dependency for redis-cli

  • Loading branch information...
1 parent 339b9dc commit e902b579b4eb5f695680427cb34725159a4fa64f @pietern pietern committed Nov 3, 2010
@@ -0,0 +1 @@
@@ -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
+ $(CC) -c -Wall -W -Os -g $<
+ rm -f linenoise_example
@@ -0,0 +1,45 @@
+# Linenoise
+A minimal, zero-config, BSD licensed, readline replacement.
+News: linenoise is now part of [Android](;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 ($TERM = xterm)
+ * OpenBSD 4.5 through an OSX ($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
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <stdlib.h>
+#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;
@@ -69,8 +69,6 @@
-#include "fmacros.h"
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
@@ -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 @@ static int linenoisePrompt(int fd, char *buf, size_t buflen, const char *prompt)
len = pos = strlen(buf);
+ } 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);
+ }
+ }
File renamed without changes.
@@ -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 \
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
- $(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)

0 comments on commit e902b57

Please sign in to comment.