Use a linenoise with win32 support

This commit is contained in:
Brian Anderson 2012-11-04 13:23:41 -08:00
parent a450119b0b
commit 799eb105b9
10 changed files with 1419 additions and 386 deletions

View File

@ -78,7 +78,7 @@ RUNTIME_CXXS_$(1) := \
rt/arch/$$(HOST_$(1))/context.cpp \
rt/arch/$$(HOST_$(1))/gpr.cpp
RUNTIME_CS_$(1) := rt/linenoise/linenoise.c
RUNTIME_CS_$(1) := rt/linenoise/linenoise.c rt/linenoise/utf8.c
RUNTIME_S_$(1) := rt/arch/$$(HOST_$(1))/_context.S \
rt/arch/$$(HOST_$(1))/ccall.S \

View File

@ -9,7 +9,6 @@ extern mod rustrt {
fn linenoiseHistoryLoad(file: *c_char) -> c_int;
fn linenoiseSetCompletionCallback(callback: *u8);
fn linenoiseAddCompletion(completions: *(), line: *c_char);
fn linenoiseClearScreen();
}
/// Add a line to history
@ -48,11 +47,6 @@ pub fn read(prompt: ~str) -> Option<~str> {
}
}
/// Clear the screen
pub fn clear() {
rustrt::linenoiseClearScreen();
}
pub type CompletionCb = fn~(~str, fn(~str));
fn complete_key(_v: @CompletionCb) {}

View File

@ -0,0 +1,47 @@
# Linenoise
A minimal, zero-config, BSD licensed, readline replacement.
News: linenoise now includes minimal completion support, thanks to Pieter Noordhuis (@pnoordhuis).
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 (Real 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

View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include "linenoise.h"
#ifndef NO_COMPLETION
void completion(const char *buf, linenoiseCompletions *lc) {
if (buf[0] == 'h') {
linenoiseAddCompletion(lc,"hello");
linenoiseAddCompletion(lc,"hello there");
}
}
#endif
int main(void) {
char *line;
#ifndef NO_COMPLETION
linenoiseSetCompletionCallback(completion);
#endif
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;
}

File diff suppressed because it is too large Load Diff

View File

@ -9,18 +9,18 @@
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@ -37,6 +37,7 @@
#ifndef __LINENOISE_H
#define __LINENOISE_H
#ifndef NO_COMPLETION
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
@ -44,13 +45,15 @@ typedef struct linenoiseCompletions {
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, char *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
#endif
char *linenoise(const char *prompt);
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(char *filename);
int linenoiseHistoryLoad(char *filename);
void linenoiseClearScreen(void);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
void linenoiseHistoryFree(void);
char **linenoiseHistory(int *len);
#endif /* __LINENOISE_H */
#endif /* __LINENOISE_H */

115
src/rt/linenoise/utf8.c Normal file
View File

@ -0,0 +1,115 @@
/**
* UTF-8 utility functions
*
* (c) 2010 Steve Bennett <steveb@workware.net.au>
*
* See LICENCE for licence details.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "utf8.h"
#ifdef USE_UTF8
int utf8_fromunicode(char *p, unsigned short uc)
{
if (uc <= 0x7f) {
*p = uc;
return 1;
}
else if (uc <= 0x7ff) {
*p++ = 0xc0 | ((uc & 0x7c0) >> 6);
*p = 0x80 | (uc & 0x3f);
return 2;
}
else {
*p++ = 0xe0 | ((uc & 0xf000) >> 12);
*p++ = 0x80 | ((uc & 0xfc0) >> 6);
*p = 0x80 | (uc & 0x3f);
return 3;
}
}
int utf8_charlen(int c)
{
if ((c & 0x80) == 0) {
return 1;
}
if ((c & 0xe0) == 0xc0) {
return 2;
}
if ((c & 0xf0) == 0xe0) {
return 3;
}
if ((c & 0xf8) == 0xf0) {
return 4;
}
/* Invalid sequence */
return -1;
}
int utf8_strlen(const char *str, int bytelen)
{
int charlen = 0;
if (bytelen < 0) {
bytelen = strlen(str);
}
while (bytelen) {
int c;
int l = utf8_tounicode(str, &c);
charlen++;
str += l;
bytelen -= l;
}
return charlen;
}
int utf8_index(const char *str, int index)
{
const char *s = str;
while (index--) {
int c;
s += utf8_tounicode(s, &c);
}
return s - str;
}
int utf8_charequal(const char *s1, const char *s2)
{
int c1, c2;
utf8_tounicode(s1, &c1);
utf8_tounicode(s2, &c2);
return c1 == c2;
}
int utf8_tounicode(const char *str, int *uc)
{
unsigned const char *s = (unsigned const char *)str;
if (s[0] < 0xc0) {
*uc = s[0];
return 1;
}
if (s[0] < 0xe0) {
if ((s[1] & 0xc0) == 0x80) {
*uc = ((s[0] & ~0xc0) << 6) | (s[1] & ~0x80);
return 2;
}
}
else if (s[0] < 0xf0) {
if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80)) {
*uc = ((s[0] & ~0xe0) << 12) | ((s[1] & ~0x80) << 6) | (s[2] & ~0x80);
return 3;
}
}
/* Invalid sequence, so just return the byte */
*uc = *s;
return 1;
}
#endif

79
src/rt/linenoise/utf8.h Normal file
View File

@ -0,0 +1,79 @@
#ifndef UTF8_UTIL_H
#define UTF8_UTIL_H
/**
* UTF-8 utility functions
*
* (c) 2010 Steve Bennett <steveb@workware.net.au>
*
* See LICENCE for licence details.
*/
#ifndef USE_UTF8
#include <ctype.h>
/* No utf-8 support. 1 byte = 1 char */
#define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B))
#define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
#define utf8_index(C, I) (I)
#define utf8_charlen(C) 1
#else
/**
* Converts the given unicode codepoint (0 - 0xffff) to utf-8
* and stores the result at 'p'.
*
* Returns the number of utf-8 characters (1-3).
*/
int utf8_fromunicode(char *p, unsigned short uc);
/**
* Returns the length of the utf-8 sequence starting with 'c'.
*
* Returns 1-4, or -1 if this is not a valid start byte.
*
* Note that charlen=4 is not supported by the rest of the API.
*/
int utf8_charlen(int c);
/**
* Returns the number of characters in the utf-8
* string of the given byte length.
*
* Any bytes which are not part of an valid utf-8
* sequence are treated as individual characters.
*
* The string *must* be null terminated.
*
* Does not support unicode code points > \uffff
*/
int utf8_strlen(const char *str, int bytelen);
/**
* Returns the byte index of the given character in the utf-8 string.
*
* The string *must* be null terminated.
*
* This will return the byte length of a utf-8 string
* if given the char length.
*/
int utf8_index(const char *str, int charindex);
/**
* Returns the unicode codepoint corresponding to the
* utf-8 sequence 'str'.
*
* Stores the result in *uc and returns the number of bytes
* consumed.
*
* If 'str' is null terminated, then an invalid utf-8 sequence
* at the end of the string will be returned as individual bytes.
*
* If it is not null terminated, the length *must* be checked first.
*
* Does not support unicode code points > \uffff
*/
int utf8_tounicode(const char *str, int *uc);
#endif
#endif

View File

@ -211,4 +211,3 @@ linenoiseHistoryAdd
linenoiseHistorySetMaxLen
linenoiseHistorySave
linenoiseHistoryLoad
linenoiseClearScreen

View File

@ -243,7 +243,8 @@ fn run_cmd(repl: &mut Repl, _in: io::Reader, _out: io::Writer,
repl.view_items = ~"";
repl.stmts = ~"";
rl::clear();
// FIXME: Win32 version of linenoise doesn't have the required function
//rl::clear();
}
~"help" => {
io::println(~":clear - clear the screen\n" +