tisp

tiny lisp
git clone git://edryd.org/tisp
Log | Files | Refs | LICENSE

linenoise.c (39203B)


      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.
      3  *
      4  * You can find the latest source code at:
      5  *
      6  *   http://github.com/antirez/linenoise
      7  *
      8  * Does a number of crazy assumptions that happen to be true in 99.9999% of
      9  * the 2010 UNIX computers around.
     10  *
     11  * ------------------------------------------------------------------------
     12  *
     13  * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
     14  * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
     15  *
     16  * All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions are
     20  * met:
     21  *
     22  *  *  Redistributions of source code must retain the above copyright
     23  *     notice, this list of conditions and the following disclaimer.
     24  *
     25  *  *  Redistributions in binary form must reproduce the above copyright
     26  *     notice, this list of conditions and the following disclaimer in the
     27  *     documentation and/or other materials provided with the distribution.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     33  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     40  *
     41  * ------------------------------------------------------------------------
     42  *
     43  * References:
     44  * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
     45  * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
     46  *
     47  * Todo list:
     48  * - Filter bogus Ctrl+<char> combinations.
     49  * - Win32 support
     50  *
     51  * Bloat:
     52  * - History search like Ctrl+r in readline?
     53  *
     54  * List of escape sequences used by this program, we do everything just
     55  * with three 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.
     58  *
     59  * EL (Erase Line)
     60  *    Sequence: ESC [ n K
     61  *    Effect: if n is 0 or missing, clear from cursor to end of line
     62  *    Effect: if n is 1, clear from beginning of line to cursor
     63  *    Effect: if n is 2, clear entire line
     64  *
     65  * CUF (CUrsor Forward)
     66  *    Sequence: ESC [ n C
     67  *    Effect: moves cursor forward n chars
     68  *
     69  * CUB (CUrsor Backward)
     70  *    Sequence: ESC [ n D
     71  *    Effect: moves cursor backward n chars
     72  *
     73  * The following is used to get the terminal width if getting
     74  * the width with the TIOCGWINSZ ioctl fails
     75  *
     76  * DSR (Device Status Report)
     77  *    Sequence: ESC [ 6 n
     78  *    Effect: reports the current cusor position as ESC [ n ; m R
     79  *            where n is the row and m is the column
     80  *
     81  * When multi line mode is enabled, we also use an additional escape
     82  * sequence. However multi line editing is disabled by default.
     83  *
     84  * CUU (Cursor Up)
     85  *    Sequence: ESC [ n A
     86  *    Effect: moves cursor up of n chars.
     87  *
     88  * CUD (Cursor Down)
     89  *    Sequence: ESC [ n B
     90  *    Effect: moves cursor down of n chars.
     91  *
     92  * When linenoiseClearScreen() is called, two additional escape sequences
     93  * are used in order to clear the screen and position the cursor at home
     94  * position.
     95  *
     96  * CUP (Cursor position)
     97  *    Sequence: ESC [ H
     98  *    Effect: moves the cursor to upper left corner
     99  *
    100  * ED (Erase display)
    101  *    Sequence: ESC [ 2 J
    102  *    Effect: clear the whole screen
    103  *
    104  */
    105 
    106 #include <termios.h>
    107 #include <unistd.h>
    108 #include <stdlib.h>
    109 #include <stdio.h>
    110 #include <errno.h>
    111 #include <string.h>
    112 #include <stdlib.h>
    113 #include <ctype.h>
    114 #include <sys/stat.h>
    115 #include <sys/types.h>
    116 #include <sys/ioctl.h>
    117 #include <unistd.h>
    118 #include "linenoise.h"
    119 
    120 #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
    121 #define LINENOISE_MAX_LINE 4096
    122 static char *unsupported_term[] = {"dumb","cons25","emacs",NULL};
    123 static linenoiseCompletionCallback *completionCallback = NULL;
    124 static linenoiseHintsCallback *hintsCallback = NULL;
    125 static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
    126 
    127 static struct termios orig_termios; /* In order to restore at exit.*/
    128 static int rawmode = 0; /* For atexit() function to check if restore is needed*/
    129 static int mlmode = 0;  /* Multi line mode. Default is single line. */
    130 static int atexit_registered = 0; /* Register atexit just 1 time. */
    131 static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
    132 static int history_len = 0;
    133 static char **history = NULL;
    134 
    135 /* The linenoiseState structure represents the state during line editing.
    136  * We pass this state to functions implementing specific editing
    137  * functionalities. */
    138 struct linenoiseState {
    139     int ifd;            /* Terminal stdin file descriptor. */
    140     int ofd;            /* Terminal stdout file descriptor. */
    141     char *buf;          /* Edited line buffer. */
    142     size_t buflen;      /* Edited line buffer size. */
    143     const char *prompt; /* Prompt to display. */
    144     size_t plen;        /* Prompt length. */
    145     size_t pos;         /* Current cursor position. */
    146     size_t oldpos;      /* Previous refresh cursor position. */
    147     size_t len;         /* Current edited line length. */
    148     size_t cols;        /* Number of columns in terminal. */
    149     size_t maxrows;     /* Maximum num of rows used so far (multiline mode) */
    150     int history_index;  /* The history index we are currently editing. */
    151 };
    152 
    153 enum KEY_ACTION{
    154 	KEY_NULL = 0,	    /* NULL */
    155 	CTRL_A = 1,         /* Ctrl+a */
    156 	CTRL_B = 2,         /* Ctrl-b */
    157 	CTRL_C = 3,         /* Ctrl-c */
    158 	CTRL_D = 4,         /* Ctrl-d */
    159 	CTRL_E = 5,         /* Ctrl-e */
    160 	CTRL_F = 6,         /* Ctrl-f */
    161 	CTRL_H = 8,         /* Ctrl-h */
    162 	TAB = 9,            /* Tab */
    163 	CTRL_K = 11,        /* Ctrl+k */
    164 	CTRL_L = 12,        /* Ctrl+l */
    165 	ENTER = 13,         /* Enter */
    166 	CTRL_N = 14,        /* Ctrl-n */
    167 	CTRL_P = 16,        /* Ctrl-p */
    168 	CTRL_T = 20,        /* Ctrl-t */
    169 	CTRL_U = 21,        /* Ctrl+u */
    170 	CTRL_W = 23,        /* Ctrl+w */
    171 	ESC = 27,           /* Escape */
    172 	BACKSPACE =  127    /* Backspace */
    173 };
    174 
    175 static void linenoiseAtExit(void);
    176 int linenoiseHistoryAdd(const char *line);
    177 static void refreshLine(struct linenoiseState *l);
    178 
    179 /* Debugging macro. */
    180 #if 0
    181 FILE *lndebug_fp = NULL;
    182 #define lndebug(...) \
    183     do { \
    184         if (lndebug_fp == NULL) { \
    185             lndebug_fp = fopen("/tmp/lndebug.txt","a"); \
    186             fprintf(lndebug_fp, \
    187             "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \
    188             (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \
    189             (int)l->maxrows,old_rows); \
    190         } \
    191         fprintf(lndebug_fp, ", " __VA_ARGS__); \
    192         fflush(lndebug_fp); \
    193     } while (0)
    194 #else
    195 #define lndebug(fmt, ...)
    196 #endif
    197 
    198 /* ======================= Low level terminal handling ====================== */
    199 
    200 /* Set if to use or not the multi line mode. */
    201 void linenoiseSetMultiLine(int ml) {
    202     mlmode = ml;
    203 }
    204 
    205 /* Return true if the terminal name is in the list of terminals we know are
    206  * not able to understand basic escape sequences. */
    207 static int isUnsupportedTerm(void) {
    208     char *term = getenv("TERM");
    209     int j;
    210 
    211     if (term == NULL) return 0;
    212     for (j = 0; unsupported_term[j]; j++)
    213         if (!strcasecmp(term,unsupported_term[j])) return 1;
    214     return 0;
    215 }
    216 
    217 /* Raw mode: 1960 magic shit. */
    218 static int enableRawMode(int fd) {
    219     struct termios raw;
    220 
    221     if (!isatty(STDIN_FILENO)) goto fatal;
    222     if (!atexit_registered) {
    223         atexit(linenoiseAtExit);
    224         atexit_registered = 1;
    225     }
    226     if (tcgetattr(fd,&orig_termios) == -1) goto fatal;
    227 
    228     raw = orig_termios;  /* modify the original mode */
    229     /* input modes: no break, no CR to NL, no parity check, no strip char,
    230      * no start/stop output control. */
    231     raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
    232     /* output modes - disable post processing */
    233     raw.c_oflag &= ~(OPOST);
    234     /* control modes - set 8 bit chars */
    235     raw.c_cflag |= (CS8);
    236     /* local modes - choing off, canonical off, no extended functions,
    237      * no signal chars (^Z,^C) */
    238     raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
    239     /* control chars - set return condition: min number of bytes and timer.
    240      * We want read to return every single byte, without timeout. */
    241     raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
    242 
    243     /* put terminal in raw mode after flushing */
    244     if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
    245     rawmode = 1;
    246     return 0;
    247 
    248 fatal:
    249     errno = ENOTTY;
    250     return -1;
    251 }
    252 
    253 static void disableRawMode(int fd) {
    254     /* Don't even check the return value as it's too late. */
    255     if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
    256         rawmode = 0;
    257 }
    258 
    259 /* Use the ESC [6n escape sequence to query the horizontal cursor position
    260  * and return it. On error -1 is returned, on success the position of the
    261  * cursor. */
    262 static int getCursorPosition(int ifd, int ofd) {
    263     char buf[32];
    264     int cols, rows;
    265     unsigned int i = 0;
    266 
    267     /* Report cursor location */
    268     if (write(ofd, "\x1b[6n", 4) != 4) return -1;
    269 
    270     /* Read the response: ESC [ rows ; cols R */
    271     while (i < sizeof(buf)-1) {
    272         if (read(ifd,buf+i,1) != 1) break;
    273         if (buf[i] == 'R') break;
    274         i++;
    275     }
    276     buf[i] = '\0';
    277 
    278     /* Parse it. */
    279     if (buf[0] != ESC || buf[1] != '[') return -1;
    280     if (sscanf(buf+2,"%d;%d",&rows,&cols) != 2) return -1;
    281     return cols;
    282 }
    283 
    284 /* Try to get the number of columns in the current terminal, or assume 80
    285  * if it fails. */
    286 static int getColumns(int ifd, int ofd) {
    287     struct winsize ws;
    288 
    289     if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
    290         /* ioctl() failed. Try to query the terminal itself. */
    291         int start, cols;
    292 
    293         /* Get the initial position so we can restore it later. */
    294         start = getCursorPosition(ifd,ofd);
    295         if (start == -1) goto failed;
    296 
    297         /* Go to right margin and get position. */
    298         if (write(ofd,"\x1b[999C",6) != 6) goto failed;
    299         cols = getCursorPosition(ifd,ofd);
    300         if (cols == -1) goto failed;
    301 
    302         /* Restore position. */
    303         if (cols > start) {
    304             char seq[32];
    305             snprintf(seq,32,"\x1b[%dD",cols-start);
    306             if (write(ofd,seq,strlen(seq)) == -1) {
    307                 /* Can't recover... */
    308             }
    309         }
    310         return cols;
    311     } else {
    312         return ws.ws_col;
    313     }
    314 
    315 failed:
    316     return 80;
    317 }
    318 
    319 /* Clear the screen. Used to handle ctrl+l */
    320 void linenoiseClearScreen(void) {
    321     if (write(STDOUT_FILENO,"\x1b[H\x1b[2J",7) <= 0) {
    322         /* nothing to do, just to avoid warning. */
    323     }
    324 }
    325 
    326 /* Beep, used for completion when there is nothing to complete or when all
    327  * the choices were already shown. */
    328 static void linenoiseBeep(void) {
    329     fprintf(stderr, "\x7");
    330     fflush(stderr);
    331 }
    332 
    333 /* ============================== Completion ================================ */
    334 
    335 /* Free a list of completion option populated by linenoiseAddCompletion(). */
    336 static void freeCompletions(linenoiseCompletions *lc) {
    337     size_t i;
    338     for (i = 0; i < lc->len; i++)
    339         free(lc->cvec[i]);
    340     if (lc->cvec != NULL)
    341         free(lc->cvec);
    342 }
    343 
    344 /* This is an helper function for linenoiseEdit() and is called when the
    345  * user types the <tab> key in order to complete the string currently in the
    346  * input.
    347  *
    348  * The state of the editing is encapsulated into the pointed linenoiseState
    349  * structure as described in the structure definition. */
    350 static int completeLine(struct linenoiseState *ls) {
    351     linenoiseCompletions lc = { 0, NULL };
    352     int nread, nwritten;
    353     char c = 0;
    354 
    355     completionCallback(ls->buf,&lc);
    356     if (lc.len == 0) {
    357         linenoiseBeep();
    358     } else {
    359         size_t stop = 0, i = 0;
    360 
    361         while(!stop) {
    362             /* Show completion or original buffer */
    363             if (i < lc.len) {
    364                 struct linenoiseState saved = *ls;
    365 
    366                 ls->len = ls->pos = strlen(lc.cvec[i]);
    367                 ls->buf = lc.cvec[i];
    368                 refreshLine(ls);
    369                 ls->len = saved.len;
    370                 ls->pos = saved.pos;
    371                 ls->buf = saved.buf;
    372             } else {
    373                 refreshLine(ls);
    374             }
    375 
    376             nread = read(ls->ifd,&c,1);
    377             if (nread <= 0) {
    378                 freeCompletions(&lc);
    379                 return -1;
    380             }
    381 
    382             switch(c) {
    383                 case 9: /* tab */
    384                     i = (i+1) % (lc.len+1);
    385                     if (i == lc.len) linenoiseBeep();
    386                     break;
    387                 case 27: /* escape */
    388                     /* Re-show original buffer */
    389                     if (i < lc.len) refreshLine(ls);
    390                     stop = 1;
    391                     break;
    392                 default:
    393                     /* Update buffer and return */
    394                     if (i < lc.len) {
    395                         nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]);
    396                         ls->len = ls->pos = nwritten;
    397                     }
    398                     stop = 1;
    399                     break;
    400             }
    401         }
    402     }
    403 
    404     freeCompletions(&lc);
    405     return c; /* Return last read character */
    406 }
    407 
    408 /* Register a callback function to be called for tab-completion. */
    409 void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
    410     completionCallback = fn;
    411 }
    412 
    413 /* Register a hits function to be called to show hits to the user at the
    414  * right of the prompt. */
    415 void linenoiseSetHintsCallback(linenoiseHintsCallback *fn) {
    416     hintsCallback = fn;
    417 }
    418 
    419 /* Register a function to free the hints returned by the hints callback
    420  * registered with linenoiseSetHintsCallback(). */
    421 void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) {
    422     freeHintsCallback = fn;
    423 }
    424 
    425 /* This function is used by the callback function registered by the user
    426  * in order to add completion options given the input string when the
    427  * user typed <tab>. See the example.c source code for a very easy to
    428  * understand example. */
    429 void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
    430     size_t len = strlen(str);
    431     char *copy, **cvec;
    432 
    433     copy = malloc(len+1);
    434     if (copy == NULL) return;
    435     memcpy(copy,str,len+1);
    436     cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
    437     if (cvec == NULL) {
    438         free(copy);
    439         return;
    440     }
    441     lc->cvec = cvec;
    442     lc->cvec[lc->len++] = copy;
    443 }
    444 
    445 /* =========================== Line editing ================================= */
    446 
    447 /* We define a very simple "append buffer" structure, that is an heap
    448  * allocated string where we can append to. This is useful in order to
    449  * write all the escape sequences in a buffer and flush them to the standard
    450  * output in a single call, to avoid flickering effects. */
    451 struct abuf {
    452     char *b;
    453     int len;
    454 };
    455 
    456 static void abInit(struct abuf *ab) {
    457     ab->b = NULL;
    458     ab->len = 0;
    459 }
    460 
    461 static void abAppend(struct abuf *ab, const char *s, int len) {
    462     char *new = realloc(ab->b,ab->len+len);
    463 
    464     if (new == NULL) return;
    465     memcpy(new+ab->len,s,len);
    466     ab->b = new;
    467     ab->len += len;
    468 }
    469 
    470 static void abFree(struct abuf *ab) {
    471     free(ab->b);
    472 }
    473 
    474 /* Helper of refreshSingleLine() and refreshMultiLine() to show hints
    475  * to the right of the prompt. */
    476 void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) {
    477     char seq[64];
    478     if (hintsCallback && plen+l->len < l->cols) {
    479         int color = -1, bold = 0;
    480         char *hint = hintsCallback(l->buf,&color,&bold);
    481         if (hint) {
    482             int hintlen = strlen(hint);
    483             int hintmaxlen = l->cols-(plen+l->len);
    484             if (hintlen > hintmaxlen) hintlen = hintmaxlen;
    485             if (bold == 1 && color == -1) color = 37;
    486             if (color != -1 || bold != 0)
    487                 snprintf(seq,64,"\033[%d;%d;49m",bold,color);
    488             abAppend(ab,seq,strlen(seq));
    489             abAppend(ab,hint,hintlen);
    490             if (color != -1 || bold != 0)
    491                 abAppend(ab,"\033[0m",4);
    492             /* Call the function to free the hint returned. */
    493             if (freeHintsCallback) freeHintsCallback(hint);
    494         }
    495     }
    496 }
    497 
    498 /* Single line low level line refresh.
    499  *
    500  * Rewrite the currently edited line accordingly to the buffer content,
    501  * cursor position, and number of columns of the terminal. */
    502 static void refreshSingleLine(struct linenoiseState *l) {
    503     char seq[64];
    504     size_t plen = strlen(l->prompt);
    505     int fd = l->ofd;
    506     char *buf = l->buf;
    507     size_t len = l->len;
    508     size_t pos = l->pos;
    509     struct abuf ab;
    510 
    511     while((plen+pos) >= l->cols) {
    512         buf++;
    513         len--;
    514         pos--;
    515     }
    516     while (plen+len > l->cols) {
    517         len--;
    518     }
    519 
    520     abInit(&ab);
    521     /* Cursor to left edge */
    522     snprintf(seq,64,"\r");
    523     abAppend(&ab,seq,strlen(seq));
    524     /* Write the prompt and the current buffer content */
    525     abAppend(&ab,l->prompt,strlen(l->prompt));
    526     abAppend(&ab,buf,len);
    527     /* Show hits if any. */
    528     refreshShowHints(&ab,l,plen);
    529     /* Erase to right */
    530     snprintf(seq,64,"\x1b[0K");
    531     abAppend(&ab,seq,strlen(seq));
    532     /* Move cursor to original position. */
    533     snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen));
    534     abAppend(&ab,seq,strlen(seq));
    535     if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
    536     abFree(&ab);
    537 }
    538 
    539 /* Multi line low level line refresh.
    540  *
    541  * Rewrite the currently edited line accordingly to the buffer content,
    542  * cursor position, and number of columns of the terminal. */
    543 static void refreshMultiLine(struct linenoiseState *l) {
    544     char seq[64];
    545     int plen = strlen(l->prompt);
    546     int rows = (plen+l->len+l->cols-1)/l->cols; /* rows used by current buf. */
    547     int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */
    548     int rpos2; /* rpos after refresh. */
    549     int col; /* colum position, zero-based. */
    550     int old_rows = l->maxrows;
    551     int fd = l->ofd, j;
    552     struct abuf ab;
    553 
    554     /* Update maxrows if needed. */
    555     if (rows > (int)l->maxrows) l->maxrows = rows;
    556 
    557     /* First step: clear all the lines used before. To do so start by
    558      * going to the last row. */
    559     abInit(&ab);
    560     if (old_rows-rpos > 0) {
    561         lndebug("go down %d", old_rows-rpos);
    562         snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
    563         abAppend(&ab,seq,strlen(seq));
    564     }
    565 
    566     /* Now for every row clear it, go up. */
    567     for (j = 0; j < old_rows-1; j++) {
    568         lndebug("clear+up");
    569         snprintf(seq,64,"\r\x1b[0K\x1b[1A");
    570         abAppend(&ab,seq,strlen(seq));
    571     }
    572 
    573     /* Clean the top line. */
    574     lndebug("clear");
    575     snprintf(seq,64,"\r\x1b[0K");
    576     abAppend(&ab,seq,strlen(seq));
    577 
    578     /* Write the prompt and the current buffer content */
    579     abAppend(&ab,l->prompt,strlen(l->prompt));
    580     abAppend(&ab,l->buf,l->len);
    581 
    582     /* Show hits if any. */
    583     refreshShowHints(&ab,l,plen);
    584 
    585     /* If we are at the very end of the screen with our prompt, we need to
    586      * emit a newline and move the prompt to the first column. */
    587     if (l->pos &&
    588         l->pos == l->len &&
    589         (l->pos+plen) % l->cols == 0)
    590     {
    591         lndebug("<newline>");
    592         abAppend(&ab,"\n",1);
    593         snprintf(seq,64,"\r");
    594         abAppend(&ab,seq,strlen(seq));
    595         rows++;
    596         if (rows > (int)l->maxrows) l->maxrows = rows;
    597     }
    598 
    599     /* Move cursor to right position. */
    600     rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
    601     lndebug("rpos2 %d", rpos2);
    602 
    603     /* Go up till we reach the expected positon. */
    604     if (rows-rpos2 > 0) {
    605         lndebug("go-up %d", rows-rpos2);
    606         snprintf(seq,64,"\x1b[%dA", rows-rpos2);
    607         abAppend(&ab,seq,strlen(seq));
    608     }
    609 
    610     /* Set column. */
    611     col = (plen+(int)l->pos) % (int)l->cols;
    612     lndebug("set col %d", 1+col);
    613     if (col)
    614         snprintf(seq,64,"\r\x1b[%dC", col);
    615     else
    616         snprintf(seq,64,"\r");
    617     abAppend(&ab,seq,strlen(seq));
    618 
    619     lndebug("\n");
    620     l->oldpos = l->pos;
    621 
    622     if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
    623     abFree(&ab);
    624 }
    625 
    626 /* Calls the two low level functions refreshSingleLine() or
    627  * refreshMultiLine() according to the selected mode. */
    628 static void refreshLine(struct linenoiseState *l) {
    629     if (mlmode)
    630         refreshMultiLine(l);
    631     else
    632         refreshSingleLine(l);
    633 }
    634 
    635 /* Insert the character 'c' at cursor current position.
    636  *
    637  * On error writing to the terminal -1 is returned, otherwise 0. */
    638 int linenoiseEditInsert(struct linenoiseState *l, char c) {
    639     if (l->len < l->buflen) {
    640         if (l->len == l->pos) {
    641             l->buf[l->pos] = c;
    642             l->pos++;
    643             l->len++;
    644             l->buf[l->len] = '\0';
    645             if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) {
    646                 /* Avoid a full update of the line in the
    647                  * trivial case. */
    648                 if (write(l->ofd,&c,1) == -1) return -1;
    649             } else {
    650                 refreshLine(l);
    651             }
    652         } else {
    653             memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
    654             l->buf[l->pos] = c;
    655             l->len++;
    656             l->pos++;
    657             l->buf[l->len] = '\0';
    658             refreshLine(l);
    659         }
    660     }
    661     return 0;
    662 }
    663 
    664 /* Move cursor on the left. */
    665 void linenoiseEditMoveLeft(struct linenoiseState *l) {
    666     if (l->pos > 0) {
    667         l->pos--;
    668         refreshLine(l);
    669     }
    670 }
    671 
    672 /* Move cursor on the right. */
    673 void linenoiseEditMoveRight(struct linenoiseState *l) {
    674     if (l->pos != l->len) {
    675         l->pos++;
    676         refreshLine(l);
    677     }
    678 }
    679 
    680 /* Move cursor to the start of the line. */
    681 void linenoiseEditMoveHome(struct linenoiseState *l) {
    682     if (l->pos != 0) {
    683         l->pos = 0;
    684         refreshLine(l);
    685     }
    686 }
    687 
    688 /* Move cursor to the end of the line. */
    689 void linenoiseEditMoveEnd(struct linenoiseState *l) {
    690     if (l->pos != l->len) {
    691         l->pos = l->len;
    692         refreshLine(l);
    693     }
    694 }
    695 
    696 /* Substitute the currently edited line with the next or previous history
    697  * entry as specified by 'dir'. */
    698 #define LINENOISE_HISTORY_NEXT 0
    699 #define LINENOISE_HISTORY_PREV 1
    700 void linenoiseEditHistoryNext(struct linenoiseState *l, int dir) {
    701     if (history_len > 1) {
    702         /* Update the current history entry before to
    703          * overwrite it with the next one. */
    704         free(history[history_len - 1 - l->history_index]);
    705         history[history_len - 1 - l->history_index] = strdup(l->buf);
    706         /* Show the new entry */
    707         l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
    708         if (l->history_index < 0) {
    709             l->history_index = 0;
    710             return;
    711         } else if (l->history_index >= history_len) {
    712             l->history_index = history_len-1;
    713             return;
    714         }
    715         strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);
    716         l->buf[l->buflen-1] = '\0';
    717         l->len = l->pos = strlen(l->buf);
    718         refreshLine(l);
    719     }
    720 }
    721 
    722 /* Delete the character at the right of the cursor without altering the cursor
    723  * position. Basically this is what happens with the "Delete" keyboard key. */
    724 void linenoiseEditDelete(struct linenoiseState *l) {
    725     if (l->len > 0 && l->pos < l->len) {
    726         memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1);
    727         l->len--;
    728         l->buf[l->len] = '\0';
    729         refreshLine(l);
    730     }
    731 }
    732 
    733 /* Backspace implementation. */
    734 void linenoiseEditBackspace(struct linenoiseState *l) {
    735     if (l->pos > 0 && l->len > 0) {
    736         memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos);
    737         l->pos--;
    738         l->len--;
    739         l->buf[l->len] = '\0';
    740         refreshLine(l);
    741     }
    742 }
    743 
    744 /* Delete the previosu word, maintaining the cursor at the start of the
    745  * current word. */
    746 void linenoiseEditDeletePrevWord(struct linenoiseState *l) {
    747     size_t old_pos = l->pos;
    748     size_t diff;
    749 
    750     while (l->pos > 0 && l->buf[l->pos-1] == ' ')
    751         l->pos--;
    752     while (l->pos > 0 && l->buf[l->pos-1] != ' ')
    753         l->pos--;
    754     diff = old_pos - l->pos;
    755     memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);
    756     l->len -= diff;
    757     refreshLine(l);
    758 }
    759 
    760 /* This function is the core of the line editing capability of linenoise.
    761  * It expects 'fd' to be already in "raw mode" so that every key pressed
    762  * will be returned ASAP to read().
    763  *
    764  * The resulting string is put into 'buf' when the user type enter, or
    765  * when ctrl+d is typed.
    766  *
    767  * The function returns the length of the current buffer. */
    768 static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt)
    769 {
    770     struct linenoiseState l;
    771 
    772     /* Populate the linenoise state that we pass to functions implementing
    773      * specific editing functionalities. */
    774     l.ifd = stdin_fd;
    775     l.ofd = stdout_fd;
    776     l.buf = buf;
    777     l.buflen = buflen;
    778     l.prompt = prompt;
    779     l.plen = strlen(prompt);
    780     l.oldpos = l.pos = 0;
    781     l.len = 0;
    782     l.cols = getColumns(stdin_fd, stdout_fd);
    783     l.maxrows = 0;
    784     l.history_index = 0;
    785 
    786     /* Buffer starts empty. */
    787     l.buf[0] = '\0';
    788     l.buflen--; /* Make sure there is always space for the nulterm */
    789 
    790     /* The latest history entry is always our current buffer, that
    791      * initially is just an empty string. */
    792     linenoiseHistoryAdd("");
    793 
    794     if (write(l.ofd,prompt,l.plen) == -1) return -1;
    795     while(1) {
    796         char c;
    797         int nread;
    798         char seq[3];
    799 
    800         nread = read(l.ifd,&c,1);
    801         if (nread <= 0) return l.len;
    802 
    803         /* Only autocomplete when the callback is set. It returns < 0 when
    804          * there was an error reading from fd. Otherwise it will return the
    805          * character that should be handled next. */
    806         if (c == 9 && completionCallback != NULL) {
    807             c = completeLine(&l);
    808             /* Return on errors */
    809             if (c < 0) return l.len;
    810             /* Read next character when 0 */
    811             if (c == 0) continue;
    812         }
    813 
    814         switch(c) {
    815         case ENTER:    /* enter */
    816             history_len--;
    817             free(history[history_len]);
    818             if (mlmode) linenoiseEditMoveEnd(&l);
    819             if (hintsCallback) {
    820                 /* Force a refresh without hints to leave the previous
    821                  * line as the user typed it after a newline. */
    822                 linenoiseHintsCallback *hc = hintsCallback;
    823                 hintsCallback = NULL;
    824                 refreshLine(&l);
    825                 hintsCallback = hc;
    826             }
    827             return (int)l.len;
    828         case CTRL_C:     /* ctrl-c */
    829             errno = EAGAIN;
    830             return -1;
    831         case BACKSPACE:   /* backspace */
    832         case 8:     /* ctrl-h */
    833             linenoiseEditBackspace(&l);
    834             break;
    835         case CTRL_D:     /* ctrl-d, remove char at right of cursor, or if the
    836                             line is empty, act as end-of-file. */
    837             if (l.len > 0) {
    838                 linenoiseEditDelete(&l);
    839             } else {
    840                 history_len--;
    841                 free(history[history_len]);
    842                 return -1;
    843             }
    844             break;
    845         case CTRL_T:    /* ctrl-t, swaps current character with previous. */
    846             if (l.pos > 0 && l.pos < l.len) {
    847                 int aux = buf[l.pos-1];
    848                 buf[l.pos-1] = buf[l.pos];
    849                 buf[l.pos] = aux;
    850                 if (l.pos != l.len-1) l.pos++;
    851                 refreshLine(&l);
    852             }
    853             break;
    854         case CTRL_B:     /* ctrl-b */
    855             linenoiseEditMoveLeft(&l);
    856             break;
    857         case CTRL_F:     /* ctrl-f */
    858             linenoiseEditMoveRight(&l);
    859             break;
    860         case CTRL_P:    /* ctrl-p */
    861             linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
    862             break;
    863         case CTRL_N:    /* ctrl-n */
    864             linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
    865             break;
    866         case ESC:    /* escape sequence */
    867             /* Read the next two bytes representing the escape sequence.
    868              * Use two calls to handle slow terminals returning the two
    869              * chars at different times. */
    870             if (read(l.ifd,seq,1) == -1) break;
    871             if (read(l.ifd,seq+1,1) == -1) break;
    872 
    873             /* ESC [ sequences. */
    874             if (seq[0] == '[') {
    875                 if (seq[1] >= '0' && seq[1] <= '9') {
    876                     /* Extended escape, read additional byte. */
    877                     if (read(l.ifd,seq+2,1) == -1) break;
    878                     if (seq[2] == '~') {
    879                         switch(seq[1]) {
    880                         case '3': /* Delete key. */
    881                             linenoiseEditDelete(&l);
    882                             break;
    883                         }
    884                     }
    885                 } else {
    886                     switch(seq[1]) {
    887                     case 'A': /* Up */
    888                         linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
    889                         break;
    890                     case 'B': /* Down */
    891                         linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
    892                         break;
    893                     case 'C': /* Right */
    894                         linenoiseEditMoveRight(&l);
    895                         break;
    896                     case 'D': /* Left */
    897                         linenoiseEditMoveLeft(&l);
    898                         break;
    899                     case 'H': /* Home */
    900                         linenoiseEditMoveHome(&l);
    901                         break;
    902                     case 'F': /* End*/
    903                         linenoiseEditMoveEnd(&l);
    904                         break;
    905                     }
    906                 }
    907             }
    908 
    909             /* ESC O sequences. */
    910             else if (seq[0] == 'O') {
    911                 switch(seq[1]) {
    912                 case 'H': /* Home */
    913                     linenoiseEditMoveHome(&l);
    914                     break;
    915                 case 'F': /* End*/
    916                     linenoiseEditMoveEnd(&l);
    917                     break;
    918                 }
    919             }
    920             break;
    921         default:
    922             if (linenoiseEditInsert(&l,c)) return -1;
    923             break;
    924         case CTRL_U: /* Ctrl+u, delete the whole line. */
    925             buf[0] = '\0';
    926             l.pos = l.len = 0;
    927             refreshLine(&l);
    928             break;
    929         case CTRL_K: /* Ctrl+k, delete from current to end of line. */
    930             buf[l.pos] = '\0';
    931             l.len = l.pos;
    932             refreshLine(&l);
    933             break;
    934         case CTRL_A: /* Ctrl+a, go to the start of the line */
    935             linenoiseEditMoveHome(&l);
    936             break;
    937         case CTRL_E: /* ctrl+e, go to the end of the line */
    938             linenoiseEditMoveEnd(&l);
    939             break;
    940         case CTRL_L: /* ctrl+l, clear screen */
    941             linenoiseClearScreen();
    942             refreshLine(&l);
    943             break;
    944         case CTRL_W: /* ctrl+w, delete previous word */
    945             linenoiseEditDeletePrevWord(&l);
    946             break;
    947         }
    948     }
    949     return l.len;
    950 }
    951 
    952 /* This special mode is used by linenoise in order to print scan codes
    953  * on screen for debugging / development purposes. It is implemented
    954  * by the linenoise_example program using the --keycodes option. */
    955 void linenoisePrintKeyCodes(void) {
    956     char quit[4];
    957 
    958     printf("Linenoise key codes debugging mode.\n"
    959             "Press keys to see scan codes. Type 'quit' at any time to exit.\n");
    960     if (enableRawMode(STDIN_FILENO) == -1) return;
    961     memset(quit,' ',4);
    962     while(1) {
    963         char c;
    964         int nread;
    965 
    966         nread = read(STDIN_FILENO,&c,1);
    967         if (nread <= 0) continue;
    968         memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */
    969         quit[sizeof(quit)-1] = c; /* Insert current char on the right. */
    970         if (memcmp(quit,"quit",sizeof(quit)) == 0) break;
    971 
    972         printf("'%c' %02x (%d) (type quit to exit)\n",
    973             isprint(c) ? c : '?', (int)c, (int)c);
    974         printf("\r"); /* Go left edge manually, we are in raw mode. */
    975         fflush(stdout);
    976     }
    977     disableRawMode(STDIN_FILENO);
    978 }
    979 
    980 /* This function calls the line editing function linenoiseEdit() using
    981  * the STDIN file descriptor set in raw mode. */
    982 static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) {
    983     int count;
    984 
    985     if (buflen == 0) {
    986         errno = EINVAL;
    987         return -1;
    988     }
    989 
    990     if (enableRawMode(STDIN_FILENO) == -1) return -1;
    991     count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt);
    992     disableRawMode(STDIN_FILENO);
    993     printf("\n");
    994     return count;
    995 }
    996 
    997 /* This function is called when linenoise() is called with the standard
    998  * input file descriptor not attached to a TTY. So for example when the
    999  * program using linenoise is called in pipe or with a file redirected
   1000  * to its standard input. In this case, we want to be able to return the
   1001  * line regardless of its length (by default we are limited to 4k). */
   1002 static char *linenoiseNoTTY(void) {
   1003     char *line = NULL;
   1004     size_t len = 0, maxlen = 0;
   1005 
   1006     while(1) {
   1007         if (len == maxlen) {
   1008             if (maxlen == 0) maxlen = 16;
   1009             maxlen *= 2;
   1010             char *oldval = line;
   1011             line = realloc(line,maxlen);
   1012             if (line == NULL) {
   1013                 if (oldval) free(oldval);
   1014                 return NULL;
   1015             }
   1016         }
   1017         int c = fgetc(stdin);
   1018         if (c == EOF || c == '\n') {
   1019             if (c == EOF && len == 0) {
   1020                 free(line);
   1021                 return NULL;
   1022             } else {
   1023                 line[len] = '\0';
   1024                 return line;
   1025             }
   1026         } else {
   1027             line[len] = c;
   1028             len++;
   1029         }
   1030     }
   1031 }
   1032 
   1033 /* The high level function that is the main API of the linenoise library.
   1034  * This function checks if the terminal has basic capabilities, just checking
   1035  * for a blacklist of stupid terminals, and later either calls the line
   1036  * editing function or uses dummy fgets() so that you will be able to type
   1037  * something even in the most desperate of the conditions. */
   1038 char *linenoise(const char *prompt) {
   1039     char buf[LINENOISE_MAX_LINE];
   1040     int count;
   1041 
   1042     if (!isatty(STDIN_FILENO)) {
   1043         /* Not a tty: read from file / pipe. In this mode we don't want any
   1044          * limit to the line size, so we call a function to handle that. */
   1045         return linenoiseNoTTY();
   1046     } else if (isUnsupportedTerm()) {
   1047         size_t len;
   1048 
   1049         printf("%s",prompt);
   1050         fflush(stdout);
   1051         if (fgets(buf,LINENOISE_MAX_LINE,stdin) == NULL) return NULL;
   1052         len = strlen(buf);
   1053         while(len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
   1054             len--;
   1055             buf[len] = '\0';
   1056         }
   1057         return strdup(buf);
   1058     } else {
   1059         count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt);
   1060         if (count == -1) return NULL;
   1061         return strdup(buf);
   1062     }
   1063 }
   1064 
   1065 /* This is just a wrapper the user may want to call in order to make sure
   1066  * the linenoise returned buffer is freed with the same allocator it was
   1067  * created with. Useful when the main program is using an alternative
   1068  * allocator. */
   1069 void linenoiseFree(void *ptr) {
   1070     free(ptr);
   1071 }
   1072 
   1073 /* ================================ History ================================= */
   1074 
   1075 /* Free the history, but does not reset it. Only used when we have to
   1076  * exit() to avoid memory leaks are reported by valgrind & co. */
   1077 static void freeHistory(void) {
   1078     if (history) {
   1079         int j;
   1080 
   1081         for (j = 0; j < history_len; j++)
   1082             free(history[j]);
   1083         free(history);
   1084     }
   1085 }
   1086 
   1087 /* At exit we'll try to fix the terminal to the initial conditions. */
   1088 static void linenoiseAtExit(void) {
   1089     disableRawMode(STDIN_FILENO);
   1090     freeHistory();
   1091 }
   1092 
   1093 /* This is the API call to add a new entry in the linenoise history.
   1094  * It uses a fixed array of char pointers that are shifted (memmoved)
   1095  * when the history max length is reached in order to remove the older
   1096  * entry and make room for the new one, so it is not exactly suitable for huge
   1097  * histories, but will work well for a few hundred of entries.
   1098  *
   1099  * Using a circular buffer is smarter, but a bit more complex to handle. */
   1100 int linenoiseHistoryAdd(const char *line) {
   1101     char *linecopy;
   1102 
   1103     if (history_max_len == 0) return 0;
   1104 
   1105     /* Initialization on first call. */
   1106     if (history == NULL) {
   1107         history = malloc(sizeof(char*)*history_max_len);
   1108         if (history == NULL) return 0;
   1109         memset(history,0,(sizeof(char*)*history_max_len));
   1110     }
   1111 
   1112     /* Don't add duplicated lines. */
   1113     if (history_len && !strcmp(history[history_len-1], line)) return 0;
   1114 
   1115     /* Add an heap allocated copy of the line in the history.
   1116      * If we reached the max length, remove the older line. */
   1117     linecopy = strdup(line);
   1118     if (!linecopy) return 0;
   1119     if (history_len == history_max_len) {
   1120         free(history[0]);
   1121         memmove(history,history+1,sizeof(char*)*(history_max_len-1));
   1122         history_len--;
   1123     }
   1124     history[history_len] = linecopy;
   1125     history_len++;
   1126     return 1;
   1127 }
   1128 
   1129 /* Set the maximum length for the history. This function can be called even
   1130  * if there is already some history, the function will make sure to retain
   1131  * just the latest 'len' elements if the new history length value is smaller
   1132  * than the amount of items already inside the history. */
   1133 int linenoiseHistorySetMaxLen(int len) {
   1134     char **new;
   1135 
   1136     if (len < 1) return 0;
   1137     if (history) {
   1138         int tocopy = history_len;
   1139 
   1140         new = malloc(sizeof(char*)*len);
   1141         if (new == NULL) return 0;
   1142 
   1143         /* If we can't copy everything, free the elements we'll not use. */
   1144         if (len < tocopy) {
   1145             int j;
   1146 
   1147             for (j = 0; j < tocopy-len; j++) free(history[j]);
   1148             tocopy = len;
   1149         }
   1150         memset(new,0,sizeof(char*)*len);
   1151         memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy);
   1152         free(history);
   1153         history = new;
   1154     }
   1155     history_max_len = len;
   1156     if (history_len > history_max_len)
   1157         history_len = history_max_len;
   1158     return 1;
   1159 }
   1160 
   1161 /* Save the history in the specified file. On success 0 is returned
   1162  * otherwise -1 is returned. */
   1163 int linenoiseHistorySave(const char *filename) {
   1164     mode_t old_umask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
   1165     FILE *fp;
   1166     int j;
   1167 
   1168     fp = fopen(filename,"w");
   1169     umask(old_umask);
   1170     if (fp == NULL) return -1;
   1171     chmod(filename,S_IRUSR|S_IWUSR);
   1172     for (j = 0; j < history_len; j++)
   1173         fprintf(fp,"%s\n",history[j]);
   1174     fclose(fp);
   1175     return 0;
   1176 }
   1177 
   1178 /* Load the history from the specified file. If the file does not exist
   1179  * zero is returned and no operation is performed.
   1180  *
   1181  * If the file exists and the operation succeeded 0 is returned, otherwise
   1182  * on error -1 is returned. */
   1183 int linenoiseHistoryLoad(const char *filename) {
   1184     FILE *fp = fopen(filename,"r");
   1185     char buf[LINENOISE_MAX_LINE];
   1186 
   1187     if (fp == NULL) return -1;
   1188 
   1189     while (fgets(buf,LINENOISE_MAX_LINE,fp) != NULL) {
   1190         char *p;
   1191 
   1192         p = strchr(buf,'\r');
   1193         if (!p) p = strchr(buf,'\n');
   1194         if (p) *p = '\0';
   1195         linenoiseHistoryAdd(buf);
   1196     }
   1197     fclose(fp);
   1198     return 0;
   1199 }