1 /* linenoise.c -- guerrilla line editing library against the idea that a
2 * line editing lib needs to be 20,000 lines of C code.
4 * You can find the latest source code at:
6 * http://github.com/msteveb/linenoise
7 * (forked from http://github.com/antirez/linenoise)
9 * Does a number of crazy assumptions that happen to be true in 99.9999% of
10 * the 2010 UNIX computers around.
12 * ------------------------------------------------------------------------
14 * Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
15 * Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
16 * Copyright (c) 2011, Steve Bennett <steveb at workware dot net dot au>
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions are
24 * * Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
27 * * Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * ------------------------------------------------------------------------
46 * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
47 * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
54 * List of escape sequences used by this program, we do everything just
55 * a few sequences. In order to be so cheap we may have some
56 * flickering effect with some slow terminal, but the lesser sequences
57 * the more compatible.
61 * Effect: clear from cursor to end of line
63 * CUF (CUrsor Forward)
65 * Effect: moves cursor forward n chars
67 * CR (Carriage Return)
69 * Effect: moves cursor to column 1
71 * The following are used to clear the screen: ESC [ H ESC [ 2 J
72 * This is actually composed of two sequences:
76 * Effect: moves the cursor to upper left corner
78 * ED2 (Clear entire screen)
80 * Effect: clear the whole screen
82 * == For highlighting control characters, we also use the following two ==
85 * Effect: Uses some standout mode such as reverse video
89 * Effect: Exit standout mode
91 * == Only used if TIOCGWINSZ fails ==
92 * DSR/CPR (Report cursor position)
94 * Effect: reports current cursor position as ESC [ NNN ; MMM R
96 * == Only used in multiline mode ==
99 * Effect: moves cursor up n chars.
102 * Sequence: ESC [ n B
103 * Effect: moves cursor down n chars.
107 * If __MINGW32__ is defined, the win32 console API is used.
108 * This could probably be made to work for the msvc compiler too.
109 * This support based in part on work by Jon Griffiths.
112 #ifdef _WIN32 /* Windows platform, either MinGW or Visual Studio (MSVC) */
115 #define USE_WINCONSOLE
117 #define HAVE_UNISTD_H
119 /* Microsoft headers don't like old POSIX names */
120 #define strdup _strdup
121 #define snprintf _snprintf
125 #include <sys/ioctl.h>
128 #define HAVE_UNISTD_H
142 #include <sys/types.h>
144 #include "linenoise.h"
145 #include "stringbuf.h"
148 #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
150 #define ctrl(C) ((C) - '@')
152 /* Use -ve numbers here to co-exist with normal unicode chars */
155 /* don't use -1 here since that indicates error */
160 SPECIAL_DELETE = -24,
163 SPECIAL_INSERT = -27,
164 SPECIAL_PAGE_UP = -28,
165 SPECIAL_PAGE_DOWN = -29,
167 /* Some handy names for other special keycodes */
172 static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
173 static int history_len = 0;
174 static char **history = NULL;
176 /* Structure to contain the status of the current (being edited) line */
178 stringbuf *buf; /* Current buffer. Always null terminated */
179 int pos; /* Cursor position, measured in chars */
180 int cols; /* Size of the window, in chars */
181 int nrows; /* How many rows are being used in multiline mode (>= 1) */
182 int rpos; /* The current row containing the cursor - multiline mode only */
184 stringbuf *capture; /* capture buffer, or NULL for none. Always null terminated */
185 stringbuf *output; /* used only during refreshLine() - output accumulator */
186 #if defined(USE_TERMIOS)
187 int fd; /* Terminal fd */
188 #elif defined(USE_WINCONSOLE)
189 HANDLE outh; /* Console output handle */
190 HANDLE inh; /* Console input handle */
191 int rows; /* Screen rows */
192 int x; /* Current column during output */
193 int y; /* Current row */
195 WORD ubuf[132]; /* Accumulates utf16 output */
196 int ubuflen; /* length used in ubuf */
197 int ubufcols; /* how many columns are represented by the chars in ubuf? */
202 static int fd_read(struct current *current);
203 static int getWindowSize(struct current *current);
204 static void cursorDown(struct current *current, int n);
205 static void cursorUp(struct current *current, int n);
206 static void eraseEol(struct current *current);
207 static void refreshLine(struct current *current);
208 static void refreshLineAlt(struct current *current, const char *prompt, const char *buf, int cursor_pos);
209 static void setCursorPos(struct current *current, int x);
210 static void setOutputHighlight(struct current *current, const int *props, int nprops);
211 static void set_current(struct current *current, const char *str);
213 void linenoiseHistoryFree(void) {
217 for (j = 0; j < history_len; j++)
227 EP_START, /* looking for ESC */
228 EP_ESC, /* looking for [ */
229 EP_DIGITS, /* parsing digits */
230 EP_PROPS, /* parsing digits or semicolons */
232 EP_ERROR, /* error */
234 int props[5]; /* properties are stored here */
235 int maxprops; /* size of the props[] array */
236 int numprops; /* number of properties found */
237 int termchar; /* terminator char, or 0 for any alpha */
238 int current; /* current (partial) property value */
242 * Initialise the escape sequence parser at *parser.
244 * If termchar is 0 any alpha char terminates ok. Otherwise only the given
245 * char terminates successfully.
246 * Run the parser state machine with calls to parseEscapeSequence() for each char.
248 static void initParseEscapeSeq(struct esc_parser *parser, int termchar)
250 parser->state = EP_START;
251 parser->maxprops = sizeof(parser->props) / sizeof(*parser->props);
252 parser->numprops = 0;
254 parser->termchar = termchar;
258 * Pass character 'ch' into the state machine to parse:
259 * 'ESC' '[' <digits> (';' <digits>)* <termchar>
261 * The first character must be ESC.
262 * Returns the current state. The state machine is done when it returns either EP_END
265 * On EP_END, the "property/attribute" values can be read from parser->props[]
266 * of length parser->numprops.
268 static int parseEscapeSequence(struct esc_parser *parser, int ch)
270 switch (parser->state) {
272 parser->state = (ch == '\x1b') ? EP_ESC : EP_ERROR;
275 parser->state = (ch == '[') ? EP_DIGITS : EP_ERROR;
279 parser->state = EP_DIGITS;
281 if (parser->numprops + 1 < parser->maxprops) {
282 parser->props[parser->numprops++] = parser->current;
289 if (ch >= '0' && ch <= '9') {
290 parser->current = parser->current * 10 + (ch - '0');
291 parser->state = EP_PROPS;
294 /* must be terminator */
295 if (parser->termchar != ch) {
296 if (parser->termchar != 0 || !((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))) {
297 parser->state = EP_ERROR;
301 parser->state = EP_END;
304 parser->state = EP_ERROR;
309 return parser->state;
312 /*#define DEBUG_REFRESHLINE*/
314 #ifdef DEBUG_REFRESHLINE
315 #define DRL(ARGS...) fprintf(dfh, ARGS)
318 static void DRL_CHAR(int ch)
321 DRL("^%c", ch + '@');
330 static void DRL_STR(const char *str)
334 int n = utf8_tounicode(str, &ch);
345 #if defined(USE_WINCONSOLE)
346 #include "linenoise-win32.c"
349 #if defined(USE_TERMIOS)
350 static void linenoiseAtExit(void);
351 static struct termios orig_termios; /* in order to restore at exit */
352 static int rawmode = 0; /* for atexit() function to check if restore is needed*/
353 static int atexit_registered = 0; /* register atexit just 1 time */
355 static const char *unsupported_term[] = {"dumb","cons25","emacs",NULL};
357 static int isUnsupportedTerm(void) {
358 char *term = getenv("TERM");
362 for (j = 0; unsupported_term[j]; j++) {
363 if (strcmp(term, unsupported_term[j]) == 0) {
371 static int enableRawMode(struct current *current) {
374 current->fd = STDIN_FILENO;
377 if (!isatty(current->fd) || isUnsupportedTerm() ||
378 tcgetattr(current->fd, &orig_termios) == -1) {
384 if (!atexit_registered) {
385 atexit(linenoiseAtExit);
386 atexit_registered = 1;
389 raw = orig_termios; /* modify the original mode */
390 /* input modes: no break, no CR to NL, no parity check, no strip char,
391 * no start/stop output control. */
392 raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
393 /* output modes - actually, no need to disable post processing */
394 /*raw.c_oflag &= ~(OPOST);*/
395 /* control modes - set 8 bit chars */
396 raw.c_cflag |= (CS8);
397 /* local modes - choing off, canonical off, no extended functions,
398 * no signal chars (^Z,^C) */
399 raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
400 /* control chars - set return condition: min number of bytes and timer.
401 * We want read to return every single byte, without timeout. */
402 raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
404 /* put terminal in raw mode after flushing */
405 if (tcsetattr(current->fd,TCSADRAIN,&raw) < 0) {
412 static void disableRawMode(struct current *current) {
413 /* Don't even check the return value as it's too late. */
414 if (rawmode && tcsetattr(current->fd,TCSADRAIN,&orig_termios) != -1)
418 /* At exit we'll try to fix the terminal to the initial conditions. */
419 static void linenoiseAtExit(void) {
421 tcsetattr(STDIN_FILENO, TCSADRAIN, &orig_termios);
423 linenoiseHistoryFree();
426 /* gcc/glibc insists that we care about the return code of write!
427 * Clarification: This means that a void-cast like "(void) (EXPR)"
430 #define IGNORE_RC(EXPR) if (EXPR) {}
433 * Output bytes directly, or accumulate output (if current->output is set)
435 static void outputChars(struct current *current, const char *buf, int len)
440 if (current->output) {
441 sb_append_len(current->output, buf, len);
444 IGNORE_RC(write(current->fd, buf, len));
448 /* Like outputChars, but using printf-style formatting
450 static void outputFormatted(struct current *current, const char *format, ...)
456 va_start(args, format);
457 n = vsnprintf(buf, sizeof(buf), format, args);
458 /* This will never happen because we are sure to use outputFormatted() only for short sequences */
459 assert(n < (int)sizeof(buf));
461 outputChars(current, buf, n);
464 static void cursorToLeft(struct current *current)
466 outputChars(current, "\r", -1);
469 static void setOutputHighlight(struct current *current, const int *props, int nprops)
471 outputChars(current, "\x1b[", -1);
473 outputFormatted(current, "%d%c", *props, (nprops == 0) ? 'm' : ';');
478 static void eraseEol(struct current *current)
480 outputChars(current, "\x1b[0K", -1);
483 static void setCursorPos(struct current *current, int x)
486 cursorToLeft(current);
489 outputFormatted(current, "\r\x1b[%dC", x);
493 static void cursorUp(struct current *current, int n)
496 outputFormatted(current, "\x1b[%dA", n);
500 static void cursorDown(struct current *current, int n)
503 outputFormatted(current, "\x1b[%dB", n);
507 void linenoiseClearScreen(void)
509 write(STDOUT_FILENO, "\x1b[H\x1b[2J", 7);
513 * Reads a char from 'fd', waiting at most 'timeout' milliseconds.
515 * A timeout of -1 means to wait forever.
517 * Returns -1 if no char is received within the time or an error occurs.
519 static int fd_read_char(int fd, int timeout)
527 if (poll(&p, 1, timeout) == 0) {
531 if (read(fd, &c, 1) != 1) {
538 * Reads a complete utf-8 character
539 * and returns the unicode value, or -1 on error.
541 static int fd_read(struct current *current)
544 char buf[MAX_UTF8_LEN];
549 if (read(current->fd, &buf[0], 1) != 1) {
552 n = utf8_charlen(buf[0]);
556 for (i = 1; i < n; i++) {
557 if (read(current->fd, &buf[i], 1) != 1) {
561 /* decode and return the character */
562 utf8_tounicode(buf, &c);
565 return fd_read_char(current->fd, -1);
571 * Stores the current cursor column in '*cols'.
572 * Returns 1 if OK, or 0 if failed to determine cursor pos.
574 static int queryCursor(struct current *current, int* cols)
576 struct esc_parser parser;
579 /* Should not be buffering this output, it needs to go immediately */
580 assert(current->output == NULL);
582 /* control sequence - report cursor location */
583 outputChars(current, "\x1b[6n", -1);
585 /* Parse the response: ESC [ rows ; cols R */
586 initParseEscapeSeq(&parser, 'R');
587 while ((ch = fd_read_char(current->fd, 100)) > 0) {
588 switch (parseEscapeSequence(&parser, ch)) {
592 if (parser.numprops == 2 && parser.props[1] < 1000) {
593 *cols = parser.props[1];
607 * Updates current->cols with the current window size (width)
609 static int getWindowSize(struct current *current)
613 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col != 0) {
614 current->cols = ws.ws_col;
618 /* Failed to query the window size. Perhaps we are on a serial terminal.
619 * Try to query the width by sending the cursor as far to the right
620 * and reading back the cursor position.
621 * Note that this is only done once per call to linenoise rather than
622 * every time the line is refreshed for efficiency reasons.
624 * In more detail, we:
625 * (a) request current cursor position,
626 * (b) move cursor far right,
627 * (c) request cursor position again,
628 * (d) at last move back to the old position.
629 * This gives us the width without messing with the externally
630 * visible cursor position.
633 if (current->cols == 0) {
636 /* If anything fails => default 80 */
640 if (queryCursor (current, &here)) {
642 setCursorPos(current, 999);
644 /* (c). Note: If (a) succeeded, then (c) should as well.
645 * For paranoia we still check and have a fallback action
646 * for (d) in case of failure..
648 if (queryCursor (current, ¤t->cols)) {
649 /* (d) Reset the cursor back to the original location. */
650 if (current->cols > here) {
651 setCursorPos(current, here);
661 * If CHAR_ESCAPE was received, reads subsequent
662 * chars to determine if this is a known special key.
664 * Returns SPECIAL_NONE if unrecognised, or -1 if EOF.
666 * If no additional char is received within a short time,
667 * CHAR_ESCAPE is returned.
669 static int check_special(int fd)
671 int c = fd_read_char(fd, 50);
678 c2 = fd_read_char(fd, 50);
682 if (c == '[' || c == 'O') {
683 /* Potential arrow key */
690 return SPECIAL_RIGHT;
699 if (c == '[' && c2 >= '1' && c2 <= '8') {
700 /* extended escape */
701 c = fd_read_char(fd, 50);
705 return SPECIAL_INSERT;
707 return SPECIAL_DELETE;
709 return SPECIAL_PAGE_UP;
711 return SPECIAL_PAGE_DOWN;
718 while (c != -1 && c != '~') {
719 /* .e.g \e[12~ or '\e[11;2~ discard the complete sequence */
720 c = fd_read_char(fd, 50);
728 static void clearOutputHighlight(struct current *current)
731 setOutputHighlight(current, &nohighlight, 1);
734 static void outputControlChar(struct current *current, char ch)
737 setOutputHighlight(current, &reverse, 1);
738 outputChars(current, "^", 1);
739 outputChars(current, &ch, 1);
740 clearOutputHighlight(current);
743 #ifndef utf8_getchars
744 static int utf8_getchars(char *buf, int c)
747 return utf8_fromunicode(buf, c);
756 * Returns the unicode character at the given offset,
759 static int get_char(struct current *current, int pos)
761 if (pos >= 0 && pos < sb_chars(current->buf)) {
763 int i = utf8_index(sb_str(current->buf), pos);
764 (void)utf8_tounicode(sb_str(current->buf) + i, &c);
770 static int char_display_width(int ch)
773 /* control chars take two positions */
777 return utf8_width(ch);
781 #ifndef NO_COMPLETION
782 static linenoiseCompletionCallback *completionCallback = NULL;
783 static void *completionUserdata = NULL;
784 static int showhints = 1;
785 static linenoiseHintsCallback *hintsCallback = NULL;
786 static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
787 static void *hintsUserdata = NULL;
791 fprintf(stderr, "\x7");
796 static void freeCompletions(linenoiseCompletions *lc) {
798 for (i = 0; i < lc->len; i++)
803 static int completeLine(struct current *current) {
804 linenoiseCompletions lc = { 0, NULL };
807 completionCallback(sb_str(current->buf),&lc,completionUserdata);
811 size_t stop = 0, i = 0;
814 /* Show completion or original buffer */
816 int chars = utf8_strlen(lc.cvec[i], -1);
817 refreshLineAlt(current, current->prompt, lc.cvec[i], chars);
819 refreshLine(current);
822 c = fd_read(current);
829 i = (i+1) % (lc.len+1);
830 if (i == lc.len) beep();
832 case CHAR_ESCAPE: /* escape */
833 /* Re-show original buffer */
835 refreshLine(current);
840 /* Update buffer and return */
842 set_current(current,lc.cvec[i]);
850 freeCompletions(&lc);
851 return c; /* Return last read character */
854 /* Register a callback function to be called for tab-completion.
855 Returns the prior callback so that the caller may (if needed)
856 restore it when done. */
857 linenoiseCompletionCallback * linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn, void *userdata) {
858 linenoiseCompletionCallback * old = completionCallback;
859 completionCallback = fn;
860 completionUserdata = userdata;
864 void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
865 lc->cvec = (char **)realloc(lc->cvec,sizeof(char*)*(lc->len+1));
866 lc->cvec[lc->len++] = strdup(str);
869 void linenoiseSetHintsCallback(linenoiseHintsCallback *callback, void *userdata)
871 hintsCallback = callback;
872 hintsUserdata = userdata;
875 void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *callback)
877 freeHintsCallback = callback;
883 static const char *reduceSingleBuf(const char *buf, int availcols, int *cursor_pos)
885 /* We have availcols columns available.
886 * If necessary, strip chars off the front of buf until *cursor_pos
887 * fits within availcols
891 int new_cursor_pos = *cursor_pos;
892 const char *pt = buf;
894 DRL("reduceSingleBuf: availcols=%d, cursor_pos=%d\n", availcols, *cursor_pos);
898 int n = utf8_tounicode(pt, &ch);
901 needcols += char_display_width(ch);
903 /* If we need too many cols, strip
904 * chars off the front of buf to make it fit.
905 * We keep 3 extra cols to the right of the cursor.
906 * 2 for possible wide chars, 1 for the last column that
909 while (needcols >= availcols - 3) {
910 n = utf8_tounicode(buf, &ch);
912 needcols -= char_display_width(ch);
915 /* and adjust the apparent cursor position */
919 /* can't remove more than this */
924 if (pos++ == *cursor_pos) {
931 DRL("\nafter reduce, needcols=%d, new_cursor_pos=%d\n", needcols, new_cursor_pos);
933 /* Done, now new_cursor_pos contains the adjusted cursor position
934 * and buf points to he adjusted start
936 *cursor_pos = new_cursor_pos;
940 static int mlmode = 0;
942 void linenoiseSetMultiLine(int enableml)
947 /* Helper of refreshSingleLine() and refreshMultiLine() to show hints
948 * to the right of the prompt. */
949 static void refreshShowHints(struct current *current, const char *buf, int availcols) {
950 if (showhints && hintsCallback && availcols > 0) {
953 char *hint = hintsCallback(buf, &color, &bold, hintsUserdata);
956 if (bold == 1 && color == -1) color = 37;
957 if (bold || color > 0) {
958 int props[3] = { bold, color, 49 }; /* bold, color, fgnormal */
959 setOutputHighlight(current, props, 3);
961 DRL("<hint bold=%d,color=%d>", bold, color);
965 int n = utf8_tounicode(pt, &ch);
966 int width = char_display_width(ch);
968 if (width >= availcols) {
975 outputChars(current, pt, n);
978 if (bold || color > 0) {
979 clearOutputHighlight(current);
981 /* Call the function to free the hint returned. */
982 if (freeHintsCallback) freeHintsCallback(hint, hintsUserdata);
988 static void refreshStart(struct current *current)
990 /* We accumulate all output here */
991 assert(current->output == NULL);
992 current->output = sb_alloc();
995 static void refreshEnd(struct current *current)
997 /* Output everything at once */
998 IGNORE_RC(write(current->fd, sb_str(current->output), sb_len(current->output)));
999 sb_free(current->output);
1000 current->output = NULL;
1003 static void refreshStartChars(struct current *current)
1007 static void refreshNewline(struct current *current)
1010 outputChars(current, "\n", 1);
1013 static void refreshEndChars(struct current *current)
1018 static void refreshLineAlt(struct current *current, const char *prompt, const char *buf, int cursor_pos)
1029 struct esc_parser parser;
1031 #ifdef DEBUG_REFRESHLINE
1032 dfh = fopen("linenoise.debuglog", "a");
1035 /* Should intercept SIGWINCH. For now, just get the size every time */
1036 getWindowSize(current);
1038 refreshStart(current);
1040 DRL("wincols=%d, cursor_pos=%d, nrows=%d, rpos=%d\n", current->cols, cursor_pos, current->nrows, current->rpos);
1042 /* Here is the plan:
1043 * (a) move the the bottom row, going down the appropriate number of lines
1044 * (b) move to beginning of line and erase the current line
1045 * (c) go up one line and do the same, until we have erased up to the first row
1046 * (d) output the prompt, counting cols and rows, taking into account escape sequences
1047 * (e) output the buffer, counting cols and rows
1048 * (e') when we hit the current pos, save the cursor position
1049 * (f) move the cursor to the saved cursor position
1050 * (g) save the current cursor row and number of rows
1053 /* (a) - The cursor is currently at row rpos */
1054 cursorDown(current, current->nrows - current->rpos - 1);
1055 DRL("<cud=%d>", current->nrows - current->rpos - 1);
1057 /* (b), (c) - Erase lines upwards until we get to the first row */
1058 for (i = 0; i < current->nrows; i++) {
1061 cursorUp(current, 1);
1064 cursorToLeft(current);
1069 /* (d) First output the prompt. control sequences don't take up display space */
1071 displaycol = 0; /* current display column */
1072 displayrow = 0; /* current display row */
1075 refreshStartChars(current);
1080 int n = utf8_tounicode(pt, &ch);
1082 if (visible && ch == CHAR_ESCAPE) {
1083 /* The start of an escape sequence, so not visible */
1085 initParseEscapeSeq(&parser, 'm');
1086 DRL("<esc-seq-start>");
1089 if (ch == '\n' || ch == '\r') {
1090 /* treat both CR and NL the same and force wrap */
1091 refreshNewline(current);
1096 width = visible * utf8_width(ch);
1098 displaycol += width;
1099 if (displaycol >= current->cols) {
1100 /* need to wrap to the next line because of newline or if it doesn't fit
1101 * XXX this is a problem in single line mode
1103 refreshNewline(current);
1109 #ifdef USE_WINCONSOLE
1111 outputChars(current, pt, n);
1114 outputChars(current, pt, n);
1120 switch (parseEscapeSequence(&parser, ch)) {
1123 setOutputHighlight(current, parser.props, parser.numprops);
1124 DRL("<esc-seq-end,numprops=%d>", parser.numprops);
1127 DRL("<esc-seq-err>");
1134 /* Now we are at the first line with all lines erased */
1135 DRL("\nafter prompt: displaycol=%d, displayrow=%d\n", displaycol, displayrow);
1138 /* (e) output the buffer, counting cols and rows */
1140 /* In this mode we may need to trim chars from the start of the buffer until the
1141 * cursor fits in the window.
1143 pt = reduceSingleBuf(buf, current->cols - displaycol, &cursor_pos);
1154 int n = utf8_tounicode(pt, &ch);
1155 int width = char_display_width(ch);
1157 if (currentpos == cursor_pos) {
1158 /* (e') wherever we output this character is where we want the cursor */
1162 if (displaycol + width >= current->cols) {
1164 /* In single line mode stop once we print as much as we can on one line */
1168 /* need to wrap to the next line since it doesn't fit */
1169 refreshNewline(current);
1174 if (notecursor == 1) {
1175 /* (e') Save this position as the current cursor position */
1176 cursorcol = displaycol;
1177 cursorrow = displayrow;
1182 displaycol += width;
1185 outputControlChar(current, ch + '@');
1188 outputChars(current, pt, n);
1192 DRL("<w=%d>", width);
1199 /* If we didn't see the cursor, it is at the current location */
1202 cursorcol = displaycol;
1203 cursorrow = displayrow;
1206 DRL("\nafter buf: displaycol=%d, displayrow=%d, cursorcol=%d, cursorrow=%d\n\n", displaycol, displayrow, cursorcol, cursorrow);
1208 /* (f) show hints */
1209 refreshShowHints(current, buf, current->cols - displaycol);
1211 refreshEndChars(current);
1213 /* (g) move the cursor to the correct place */
1214 cursorUp(current, displayrow - cursorrow);
1215 setCursorPos(current, cursorcol);
1217 /* (h) Update the number of rows if larger, but never reduce this */
1218 if (displayrow >= current->nrows) {
1219 current->nrows = displayrow + 1;
1221 /* And remember the row that the cursor is on */
1222 current->rpos = cursorrow;
1224 refreshEnd(current);
1226 #ifdef DEBUG_REFRESHLINE
1231 static void refreshLine(struct current *current)
1233 refreshLineAlt(current, current->prompt, sb_str(current->buf), current->pos);
1236 static void set_current(struct current *current, const char *str)
1238 sb_clear(current->buf);
1239 sb_append(current->buf, str);
1240 current->pos = sb_chars(current->buf);
1244 * Removes the char at 'pos'.
1246 * Returns 1 if the line needs to be refreshed, 2 if not
1247 * and 0 if nothing was removed
1249 static int remove_char(struct current *current, int pos)
1251 if (pos >= 0 && pos < sb_chars(current->buf)) {
1252 int offset = utf8_index(sb_str(current->buf), pos);
1253 int nbytes = utf8_index(sb_str(current->buf) + offset, 1);
1255 /* Note that we no longer try to optimise the remove-at-end case
1256 * since control characters and wide characters mess
1257 * up the simple count
1259 sb_delete(current->buf, offset, nbytes);
1261 if (current->pos > pos) {
1270 * Insert 'ch' at position 'pos'
1272 * Returns 1 if the line needs to be refreshed, 2 if not
1273 * and 0 if nothing was inserted (no room)
1275 static int insert_char(struct current *current, int pos, int ch)
1277 if (pos >= 0 && pos <= sb_chars(current->buf)) {
1278 char buf[MAX_UTF8_LEN + 1];
1279 int offset = utf8_index(sb_str(current->buf), pos);
1280 int n = utf8_getchars(buf, ch);
1282 /* null terminate since sb_insert() requires it */
1285 /* Optimisation removed - see reason in remove_char() */
1287 sb_insert(current->buf, offset, buf);
1288 if (current->pos >= pos) {
1297 * Captures up to 'n' characters starting at 'pos' for the cut buffer.
1299 * This replaces any existing characters in the cut buffer.
1301 static void capture_chars(struct current *current, int pos, int nchars)
1303 if (pos >= 0 && (pos + nchars - 1) < sb_chars(current->buf)) {
1304 int offset = utf8_index(sb_str(current->buf), pos);
1305 int nbytes = utf8_index(sb_str(current->buf) + offset, nchars);
1308 if (current->capture) {
1309 sb_clear(current->capture);
1312 current->capture = sb_alloc();
1314 sb_append_len(current->capture, sb_str(current->buf) + offset, nbytes);
1320 * Removes up to 'n' characters at cursor position 'pos'.
1322 * Returns 0 if no chars were removed or non-zero otherwise.
1324 static int remove_chars(struct current *current, int pos, int n)
1328 /* First save any chars which will be removed */
1329 capture_chars(current, pos, n);
1331 while (n-- && remove_char(current, pos)) {
1337 * Inserts the characters (string) 'chars' at the cursor position 'pos'.
1339 * Returns 0 if no chars were inserted or non-zero otherwise.
1341 static int insert_chars(struct current *current, int pos, const char *chars)
1347 int n = utf8_tounicode(chars, &ch);
1348 if (insert_char(current, pos, ch) == 0) {
1359 * Returns the keycode to process, or 0 if none.
1361 static int reverseIncrementalSearch(struct current *current)
1363 /* Display the reverse-i-search prompt and process chars */
1368 int searchpos = history_len - 1;
1374 const char *p = NULL;
1378 snprintf(rprompt, sizeof(rprompt), "(reverse-i-search)'%s': ", rbuf);
1379 refreshLineAlt(current, rprompt, sb_str(current->buf), current->pos);
1380 c = fd_read(current);
1381 if (c == ctrl('H') || c == CHAR_DELETE) {
1383 int p = utf8_index(rbuf, --rchars);
1385 rlen = strlen(rbuf);
1390 if (c == CHAR_ESCAPE) {
1391 c = check_special(current->fd);
1394 if (c == ctrl('P') || c == SPECIAL_UP) {
1395 /* Search for the previous (earlier) match */
1396 if (searchpos > 0) {
1401 else if (c == ctrl('N') || c == SPECIAL_DOWN) {
1402 /* Search for the next (later) match */
1403 if (searchpos < history_len) {
1409 else if (c >= ' ') {
1410 /* >= here to allow for null terminator */
1411 if (rlen >= (int)sizeof(rbuf) - MAX_UTF8_LEN) {
1415 n = utf8_getchars(rbuf + rlen, c);
1420 /* Adding a new char resets the search location */
1421 searchpos = history_len - 1;
1424 /* Exit from incremental search mode */
1428 /* Now search through the history for a match */
1429 for (; searchpos >= 0 && searchpos < history_len; searchpos += searchdir) {
1430 p = strstr(history[searchpos], rbuf);
1433 if (skipsame && strcmp(history[searchpos], sb_str(current->buf)) == 0) {
1434 /* But it is identical, so skip it */
1437 /* Copy the matching line and set the cursor position */
1438 set_current(current,history[searchpos]);
1439 current->pos = utf8_strlen(history[searchpos], p - history[searchpos]);
1444 /* No match, so don't add it */
1450 if (c == ctrl('G') || c == ctrl('C')) {
1451 /* ctrl-g terminates the search with no effect */
1452 set_current(current, "");
1455 else if (c == ctrl('J')) {
1456 /* ctrl-j terminates the search leaving the buffer in place */
1460 /* Go process the char normally */
1461 refreshLine(current);
1465 static int linenoiseEdit(struct current *current) {
1466 int history_index = 0;
1468 /* The latest history entry is always our current buffer, that
1469 * initially is just an empty string. */
1470 linenoiseHistoryAdd("");
1472 set_current(current, "");
1473 refreshLine(current);
1477 int c = fd_read(current);
1479 #ifndef NO_COMPLETION
1480 /* Only autocomplete when the callback is set. It returns < 0 when
1481 * there was an error reading from fd. Otherwise it will return the
1482 * character that should be handled next. */
1483 if (c == '\t' && current->pos == sb_chars(current->buf) && completionCallback != NULL) {
1484 c = completeLine(current);
1487 if (c == ctrl('R')) {
1488 /* reverse incremental search will provide an alternative keycode or 0 for none */
1489 c = reverseIncrementalSearch(current);
1490 /* go on to process the returned char normally */
1494 if (c == CHAR_ESCAPE) { /* escape sequence */
1495 c = check_special(current->fd);
1499 /* Return on errors */
1500 return sb_len(current->buf);
1506 case '\r': /* enter */
1508 free(history[history_len]);
1509 current->pos = sb_chars(current->buf);
1510 if (mlmode || hintsCallback) {
1512 refreshLine(current);
1515 return sb_len(current->buf);
1516 case ctrl('C'): /* ctrl-c */
1519 case ctrl('Z'): /* ctrl-z */
1521 /* send ourselves SIGSUSP */
1522 disableRawMode(current);
1525 enableRawMode(current);
1526 refreshLine(current);
1529 case CHAR_DELETE: /* backspace */
1531 if (remove_char(current, current->pos - 1) == 1) {
1532 refreshLine(current);
1535 case ctrl('D'): /* ctrl-d */
1536 if (sb_len(current->buf) == 0) {
1537 /* Empty line, so EOF */
1539 free(history[history_len]);
1542 /* Otherwise fall through to delete char to right of cursor */
1543 case SPECIAL_DELETE:
1544 if (remove_char(current, current->pos) == 1) {
1545 refreshLine(current);
1548 case SPECIAL_INSERT:
1549 /* Ignore. Expansion Hook.
1550 * Future possibility: Toggle Insert/Overwrite Modes
1553 case ctrl('W'): /* ctrl-w, delete word at left. save deleted chars */
1554 /* eat any spaces on the left */
1556 int pos = current->pos;
1557 while (pos > 0 && get_char(current, pos - 1) == ' ') {
1561 /* now eat any non-spaces on the left */
1562 while (pos > 0 && get_char(current, pos - 1) != ' ') {
1566 if (remove_chars(current, pos, current->pos - pos)) {
1567 refreshLine(current);
1571 case ctrl('T'): /* ctrl-t */
1572 if (current->pos > 0 && current->pos <= sb_chars(current->buf)) {
1573 /* If cursor is at end, transpose the previous two chars */
1574 int fixer = (current->pos == sb_chars(current->buf));
1575 c = get_char(current, current->pos - fixer);
1576 remove_char(current, current->pos - fixer);
1577 insert_char(current, current->pos - 1, c);
1578 refreshLine(current);
1581 case ctrl('V'): /* ctrl-v */
1582 /* Insert the ^V first */
1583 if (insert_char(current, current->pos, c)) {
1584 refreshLine(current);
1585 /* Now wait for the next char. Can insert anything except \0 */
1586 c = fd_read(current);
1588 /* Remove the ^V first */
1589 remove_char(current, current->pos - 1);
1591 /* Insert the actual char, can't be error or null */
1592 insert_char(current, current->pos, c);
1594 refreshLine(current);
1599 if (current->pos > 0) {
1601 refreshLine(current);
1606 if (current->pos < sb_chars(current->buf)) {
1608 refreshLine(current);
1611 case SPECIAL_PAGE_UP:
1612 dir = history_len - history_index - 1; /* move to start of history */
1613 goto history_navigation;
1614 case SPECIAL_PAGE_DOWN:
1615 dir = -history_index; /* move to 0 == end of history, i.e. current */
1616 goto history_navigation;
1620 goto history_navigation;
1624 if (history_len > 1) {
1625 /* Update the current history entry before to
1626 * overwrite it with tne next one. */
1627 free(history[history_len - 1 - history_index]);
1628 history[history_len - 1 - history_index] = strdup(sb_str(current->buf));
1629 /* Show the new entry */
1630 history_index += dir;
1631 if (history_index < 0) {
1634 } else if (history_index >= history_len) {
1635 history_index = history_len - 1;
1638 set_current(current, history[history_len - 1 - history_index]);
1639 refreshLine(current);
1642 case ctrl('A'): /* Ctrl+a, go to the start of the line */
1645 refreshLine(current);
1647 case ctrl('E'): /* ctrl+e, go to the end of the line */
1649 current->pos = sb_chars(current->buf);
1650 refreshLine(current);
1652 case ctrl('U'): /* Ctrl+u, delete to beginning of line, save deleted chars. */
1653 if (remove_chars(current, 0, current->pos)) {
1654 refreshLine(current);
1657 case ctrl('K'): /* Ctrl+k, delete from current to end of line, save deleted chars. */
1658 if (remove_chars(current, current->pos, sb_chars(current->buf) - current->pos)) {
1659 refreshLine(current);
1662 case ctrl('Y'): /* Ctrl+y, insert saved chars at current position */
1663 if (current->capture && insert_chars(current, current->pos, sb_str(current->capture))) {
1664 refreshLine(current);
1667 case ctrl('L'): /* Ctrl+L, clear screen */
1668 linenoiseClearScreen();
1669 /* Force recalc of window size for serial terminals */
1672 refreshLine(current);
1675 /* Only tab is allowed without ^V */
1676 if (c == '\t' || c >= ' ') {
1677 if (insert_char(current, current->pos, c) == 1) {
1678 refreshLine(current);
1684 return sb_len(current->buf);
1687 int linenoiseColumns(void)
1689 struct current current;
1690 enableRawMode (¤t);
1691 getWindowSize (¤t);
1692 disableRawMode (¤t);
1693 return current.cols;
1697 * Reads a line from the file handle (without the trailing NL or CRNL)
1698 * and returns it in a stringbuf.
1699 * Returns NULL if no characters are read before EOF or error.
1701 * Note that the character count will *not* be correct for lines containing
1702 * utf8 sequences. Do not rely on the character count.
1704 static stringbuf *sb_getline(FILE *fh)
1706 stringbuf *sb = sb_alloc();
1710 while ((c = getc(fh)) != EOF) {
1717 if (c == '\n' || c == '\r') {
1721 /* ignore the effect of character count for partial utf8 sequences */
1722 sb_append_len(sb, &ch, 1);
1731 char *linenoise(const char *prompt)
1734 struct current current;
1737 memset(¤t, 0, sizeof(current));
1739 if (enableRawMode(¤t) == -1) {
1740 printf("%s", prompt);
1742 sb = sb_getline(stdin);
1745 current.buf = sb_alloc();
1748 current.prompt = prompt;
1750 count = linenoiseEdit(¤t);
1752 disableRawMode(¤t);
1755 sb_free(current.capture);
1757 sb_free(current.buf);
1762 return sb ? sb_to_string(sb) : NULL;
1765 /* Using a circular buffer is smarter, but a bit more complex to handle. */
1766 int linenoiseHistoryAddAllocated(char *line) {
1768 if (history_max_len == 0) {
1773 if (history == NULL) {
1774 history = (char **)calloc(sizeof(char*), history_max_len);
1777 /* do not insert duplicate lines into history */
1778 if (history_len > 0 && strcmp(line, history[history_len - 1]) == 0) {
1782 if (history_len == history_max_len) {
1784 memmove(history,history+1,sizeof(char*)*(history_max_len-1));
1787 history[history_len] = line;
1792 int linenoiseHistoryAdd(const char *line) {
1793 return linenoiseHistoryAddAllocated(strdup(line));
1796 int linenoiseHistoryGetMaxLen(void) {
1797 return history_max_len;
1800 int linenoiseHistorySetMaxLen(int len) {
1803 if (len < 1) return 0;
1805 int tocopy = history_len;
1807 newHistory = (char **)calloc(sizeof(char*), len);
1809 /* If we can't copy everything, free the elements we'll not use. */
1813 for (j = 0; j < tocopy-len; j++) free(history[j]);
1816 memcpy(newHistory,history+(history_len-tocopy), sizeof(char*)*tocopy);
1818 history = newHistory;
1820 history_max_len = len;
1821 if (history_len > history_max_len)
1822 history_len = history_max_len;
1826 /* Save the history in the specified file. On success 0 is returned
1827 * otherwise -1 is returned. */
1828 int linenoiseHistorySave(const char *filename) {
1829 FILE *fp = fopen(filename,"w");
1832 if (fp == NULL) return -1;
1833 for (j = 0; j < history_len; j++) {
1834 const char *str = history[j];
1835 /* Need to encode backslash, nl and cr */
1840 else if (*str == '\n') {
1843 else if (*str == '\r') {
1858 /* Load the history from the specified file.
1860 * If the file does not exist or can't be opened, no operation is performed
1861 * and -1 is returned.
1862 * Otherwise 0 is returned.
1864 int linenoiseHistoryLoad(const char *filename) {
1865 FILE *fp = fopen(filename,"r");
1868 if (fp == NULL) return -1;
1870 while ((sb = sb_getline(fp)) != NULL) {
1871 /* Take the stringbuf and decode backslash escaped values */
1872 char *buf = sb_to_string(sb);
1876 for (src = buf; *src; src++) {
1884 else if (*src == 'r') {
1894 linenoiseHistoryAddAllocated(buf);
1900 /* Provide access to the history buffer.
1902 * If 'len' is not NULL, the length is stored in *len.
1904 char **linenoiseHistory(int *len) {