aboutsummaryrefslogtreecommitdiff
path: root/linenoise.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2013-08-12 15:40:54 +1000
committerSteve Bennett <steveb@workware.net.au>2013-08-12 18:52:32 +1000
commit6b2555bd8fa569c2ac1c320e657d3eda8973874f (patch)
tree78bdd4735e80f11b22a41ef9f760d1a1edb34496 /linenoise.c
parentbcbd5e3eef6a79cd533f54122a63dd1e52da21e5 (diff)
downloadjimtcl-6b2555bd8fa569c2ac1c320e657d3eda8973874f.zip
jimtcl-6b2555bd8fa569c2ac1c320e657d3eda8973874f.tar.gz
jimtcl-6b2555bd8fa569c2ac1c320e657d3eda8973874f.tar.bz2
Update linenoise.c from github
For the following change: Fix a couple of compiler warnings With the colour prompt support. Also recognize '\e[m' as an escape sequence. Simplify countColorControlChars(). Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'linenoise.c')
-rw-r--r--linenoise.c44
1 files changed, 16 insertions, 28 deletions
diff --git a/linenoise.c b/linenoise.c
index 6a16eee..63d4899 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -387,63 +387,51 @@ static int fd_read(struct current *current)
#endif
}
-static int countColorControlChars(const char* prompt, int plen)
+static int countColorControlChars(const char* prompt)
{
/* ANSI color control sequences have the form:
- * "\x1b" "[" [0-9;]+ "m"
+ * "\x1b" "[" [0-9;]* "m"
* We parse them with a simple state machine.
*/
enum {
search_esc,
expect_bracket,
- expect_inner,
expect_trail
} state = search_esc;
- int len, found = 0;
+ int len = 0, found = 0;
char ch;
/* XXX: Strictly we should be checking utf8 chars rather than
* bytes in case of the extremely unlikely scenario where
* an ANSI sequence is part of a utf8 sequence.
*/
- for (; plen ; plen--, prompt++) {
- ch = *prompt;
-
+ while ((ch = *prompt++) != 0) {
switch (state) {
case search_esc:
- len = 0;
if (ch == '\x1b') {
state = expect_bracket;
- len++;
}
break;
case expect_bracket:
if (ch == '[') {
- state = expect_inner;
- len++;
- } else {
- state = search_esc;
- }
- break;
- case expect_inner:
- if (ch >= '0' && ch <= '9') {
- len++;
state = expect_trail;
- } else {
- state = search_esc;
+ /* 3 for "\e[ ... m" */
+ len = 3;
+ break;
}
+ state = search_esc;
break;
case expect_trail:
- if (ch == 'm') {
+ if ((ch == ';') || ((ch >= '0' && ch <= '9'))) {
+ /* 0-9, or semicolon */
len++;
+ break;
+ }
+ if (ch == 'm') {
found += len;
- state = search_esc;
- } else if ((ch != ';') && ((ch < '0') || (ch > '9'))) {
- state = search_esc;
}
- /* 0-9, or semicolon */
- len++;
+ state = search_esc;
break;
}
}
@@ -703,7 +691,7 @@ static int fd_read(struct current *current)
return -1;
}
-static int countColorControlChars(char* prompt, int plen)
+static int countColorControlChars(const char* prompt)
{
/* For windows we assume that there are no embedded ansi color
* control sequences.
@@ -776,7 +764,7 @@ static void refreshLine(const char *prompt, struct current *current)
/* Scan the prompt for embedded ansi color control sequences and
* discount them as characters/columns.
*/
- pchars -= countColorControlChars(prompt, plen);
+ pchars -= countColorControlChars(prompt);
/* Account for a line which is too long to fit in the window.
* Note that control chars require an extra column