199767f8919635c4928607450d9e0abb932109ceToomas Soome/* linenoise.c -- VERSION 1.0
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Guerrilla line editing library against the idea that a line editing lib
199767f8919635c4928607450d9e0abb932109ceToomas Soome * needs to be 20,000 lines of C code.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You can find the latest source code at:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Does a number of crazy assumptions that happen to be true in 99.9999% of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the 2010 UNIX computers around.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions are
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ------------------------------------------------------------------------
199767f8919635c4928607450d9e0abb932109ceToomas Soome * References:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Todo list:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - Filter bogus Ctrl+<char> combinations.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - Win32 support
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - History search like Ctrl+r in readline?
199767f8919635c4928607450d9e0abb932109ceToomas Soome * List of escape sequences used by this program, we do everything just
199767f8919635c4928607450d9e0abb932109ceToomas Soome * with three sequences. In order to be so cheap we may have some
199767f8919635c4928607450d9e0abb932109ceToomas Soome * flickering effect with some slow terminal, but the lesser sequences
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the more compatible.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * EL (Erase Line)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n K
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: if n is 0 or missing, clear from cursor to end of line
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: if n is 1, clear from beginning of line to cursor
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: if n is 2, clear entire line
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUF (CUrsor Forward)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n C
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor forward n chars
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUB (CUrsor Backward)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n D
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor backward n chars
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The following is used to get the terminal width if getting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the width with the TIOCGWINSZ ioctl fails
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DSR (Device Status Report)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ 6 n
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: reports the current cusor position as ESC [ n ; m R
199767f8919635c4928607450d9e0abb932109ceToomas Soome * where n is the row and m is the column
199767f8919635c4928607450d9e0abb932109ceToomas Soome * When multi line mode is enabled, we also use an additional escape
199767f8919635c4928607450d9e0abb932109ceToomas Soome * sequence. However multi line editing is disabled by default.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUU (Cursor Up)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n A
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor up of n chars.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUD (Cursor Down)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n B
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor down of n chars.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * When linenoiseClearScreen() is called, two additional escape sequences
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are used in order to clear the screen and position the cursor at home
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUP (Cursor position)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ H
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves the cursor to upper left corner
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ED (Erase display)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ 2 J
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: clear the whole screen
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic linenoiseCompletionCallback *completionCallback = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int mlmode = 1; /* Multi line mode. Default is single line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* The linenoiseState structure represents the state during line editing.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * We pass this state to functions implementing specific editing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * functionalities. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t oldpos; /* Previous refresh cursor position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t cols; /* Number of columns in terminal. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t maxrows; /* Maximum num of rows used so far (multiline mode) */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int history_index; /* The history index we are currently editing. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void refreshLine(struct linenoiseState *l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ======================= Low level terminal handling ====================== */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Set if to use or not the multi line mode. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Clear the screen. Used to handle ctrl+l */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* nothing to do, just to avoid warning. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (80);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Beep, used for completion when there is nothing to complete or when all
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the choices were already shown. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ============================== Completion ================================ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Free a list of completion option populated by linenoiseAddCompletion(). */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void freeCompletions(linenoiseCompletions *lc) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This is an helper function for linenoiseEdit() and is called when the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * user types the <tab> key in order to complete the string currently in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The state of the editing is encapsulated into the pointed linenoiseState
199767f8919635c4928607450d9e0abb932109ceToomas Soome * structure as described in the structure definition. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int completeLine(struct linenoiseState *ls) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Show completion or original buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Re-show original buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Update buffer and return */
199767f8919635c4928607450d9e0abb932109ceToomas Soome nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return c; /* Return last read character */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Register a callback function to be called for tab-completion. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This function is used by the callback function registered by the user
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in order to add completion options given the input string when the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * user typed <tab>. See the example.c source code for a very easy to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * understand example. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* =========================== Line editing ================================= */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* We define a very simple "append buffer" structure, that is an heap
199767f8919635c4928607450d9e0abb932109ceToomas Soome * allocated string where we can append to. This is useful in order to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * write all the escape sequences in a buffer and flush them to the standard
199767f8919635c4928607450d9e0abb932109ceToomas Soome * output in a single call, to avoid flickering effects. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void abAppend(struct abuf *ab, const char *s, int len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Single line low level line refresh.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Rewrite the currently edited line accordingly to the buffer content,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * cursor position, and number of columns of the terminal. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void refreshSingleLine(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Cursor to left edge */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Write the prompt and the current buffer content */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Erase to right */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Move cursor to original position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen));
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Multi line low level line refresh.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Rewrite the currently edited line accordingly to the buffer content,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * cursor position, and number of columns of the terminal. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void refreshMultiLine(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rows = (plen+l->len+l->cols)/l->cols; /* rows used by current buf. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Update maxrows if needed. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* First step: clear all the lines used before. To do so start by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * going to the last row. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Now for every row clear it, go up. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Clean the top line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Write the prompt and the current buffer content */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we are at the very end of the screen with our prompt, we need to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * emit a newline and move the prompt to the first column. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Move cursor to right position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Go up till we reach the expected positon. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set column. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Calls the two low level functions refreshSingleLine() or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * refreshMultiLine() according to the selected mode. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void refreshLine(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Insert the character 'c' at cursor current position.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * On error writing to the terminal -1 is returned, otherwise 0. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditInsert(struct linenoiseState *l, char c) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((!mlmode && l->plen+l->len < l->cols) /* || mlmode */) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Avoid a full update of the line in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * trivial case. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor on the left. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveLeft(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor on the right. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveRight(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor to the start of the line. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveHome(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor to the end of the line. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveEnd(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Substitute the currently edited line with the next or previous history
199767f8919635c4928607450d9e0abb932109ceToomas Soome * entry as specified by 'dir'. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditHistoryNext(struct linenoiseState *l, int dir) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Update the current history entry before to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * overwrite it with the next one. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(history[history_len - 1 - l->history_index]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome history[history_len - 1 - l->history_index] = strdup(l->buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Show the new entry */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Delete the character at the right of the cursor without altering the cursor
199767f8919635c4928607450d9e0abb932109ceToomas Soome * position. Basically this is what happens with the "Delete" keyboard key. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Backspace implementation. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditBackspace(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Delete the previosu word, maintaining the cursor at the start of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * current word. */
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditDeletePrevWord(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This function is the core of the line editing capability of linenoise.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It expects 'fd' to be already in "raw mode" so that every key pressed
199767f8919635c4928607450d9e0abb932109ceToomas Soome * will be returned ASAP to read().
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The resulting string is put into 'buf' when the user type enter, or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * when ctrl+d is typed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The function returns the length of the current buffer. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int linenoiseEdit(char *buf, size_t buflen, const char *prompt)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Populate the linenoise state that we pass to functions implementing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * specific editing functionalities. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Buffer starts empty. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.buflen--; /* Make sure there is always space for the nulterm */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The latest history entry is always our current buffer, that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * initially is just an empty string. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Only autocomplete when the callback is set. It returns < 0 when
199767f8919635c4928607450d9e0abb932109ceToomas Soome * there was an error reading from fd. Otherwise it will return the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * character that should be handled next. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Return on errors */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c < 0) return l.len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Read next character when 0 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == 0) continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (int)l.len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_D: /* ctrl-d, remove char at right of cursor. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l.len > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_T: /* ctrl-t, swaps current character with previous. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Read the next two bytes representing the escape sequence.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use two calls to handle slow terminals returning the two
199767f8919635c4928607450d9e0abb932109ceToomas Soome * chars at different times. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ESC [ sequences. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Extended escape, read additional byte. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ESC O sequences. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_U: /* Ctrl+u, delete the whole line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_K: /* Ctrl+k, delete from current to end of line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_A: /* Ctrl+a, go to the start of the line */
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_E: /* ctrl+e, go to the end of the line */
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_W: /* ctrl+w, delete previous word */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* The high level function that is the main API of the linenoise library.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This function checks if the terminal has basic capabilities, just checking
199767f8919635c4928607450d9e0abb932109ceToomas Soome * for a blacklist of stupid terminals, and later either calls the line
199767f8919635c4928607450d9e0abb932109ceToomas Soome * editing function or uses dummy fgets() so that you will be able to type
199767f8919635c4928607450d9e0abb932109ceToomas Soome * something even in the most desperate of the conditions. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = linenoiseEdit(buf,LINENOISE_MAX_LINE,prompt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ================================ History ================================= */
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* This is the API call to add a new entry in the linenoise history.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It uses a fixed array of char pointers that are shifted (memmoved)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * when the history max length is reached in order to remove the older
199767f8919635c4928607450d9e0abb932109ceToomas Soome * entry and make room for the new one, so it is not exactly suitable for huge
199767f8919635c4928607450d9e0abb932109ceToomas Soome * histories, but will work well for a few hundred of entries.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Using a circular buffer is smarter, but a bit more complex to handle. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_max_len == 0) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialization on first call. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome history = malloc(sizeof(char*)*history_max_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(history,0,(sizeof(char*)*history_max_len));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Don't add duplicated lines. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_len && !strcmp(history[history_len-1], line)) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Add an heap allocated copy of the line in the history.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we reached the max length, remove the older line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!linecopy) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(history,history+1,sizeof(char*)*(history_max_len-1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Set the maximum length for the history. This function can be called even
199767f8919635c4928607450d9e0abb932109ceToomas Soome * if there is already some history, the function will make sure to retain
199767f8919635c4928607450d9e0abb932109ceToomas Soome * just the latest 'len' elements if the new history length value is smaller
199767f8919635c4928607450d9e0abb932109ceToomas Soome * than the amount of items already inside the history. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we can't copy everything, free the elements we'll not use. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < tocopy-len; j++) free(history[j]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy);