199767f8919635c4928607450d9e0abb932109ceToomas Soome/* linenoise.c -- VERSION 1.0
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You can find the latest source code at:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * http://github.com/antirez/linenoise
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * ------------------------------------------------------------------------
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * * Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 * ------------------------------------------------------------------------
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Todo list:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - Filter bogus Ctrl+<char> combinations.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - Win32 support
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Bloat:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - History search like Ctrl+r in readline?
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUF (CUrsor Forward)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n C
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor forward n chars
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUB (CUrsor Backward)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n D
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor backward n chars
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The following is used to get the terminal width if getting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the width with the TIOCGWINSZ ioctl fails
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 *
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUU (Cursor Up)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n A
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor up of n chars.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUD (Cursor Down)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ n B
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves cursor down of n chars.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 * position.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * CUP (Cursor position)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ H
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: moves the cursor to upper left corner
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ED (Erase display)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Sequence: ESC [ 2 J
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Effect: clear the whole screen
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "linenoise.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LINENOISE_MAX_LINE 256
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic linenoiseCompletionCallback *completionCallback = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int mlmode = 1; /* Multi line mode. Default is single line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int history_len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic char **history = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soomestruct linenoiseState {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf; /* Edited line buffer. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t buflen; /* Edited line buffer size. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *prompt; /* Prompt to display. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t plen; /* Prompt length. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t pos; /* Current cursor position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t oldpos; /* Previous refresh cursor position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t len; /* Current edited line length. */
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 Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeenum KEY_ACTION{
199767f8919635c4928607450d9e0abb932109ceToomas Soome KEY_NULL = 0, /* NULL */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_A = 1, /* Ctrl+a */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_B = 2, /* Ctrl-b */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_C = 3, /* Ctrl-c */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_D = 4, /* Ctrl-d */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_E = 5, /* Ctrl-e */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_F = 6, /* Ctrl-f */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_H = 8, /* Ctrl-h */
199767f8919635c4928607450d9e0abb932109ceToomas Soome TAB = 9, /* Tab */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_K = 11, /* Ctrl+k */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_L = 12, /* Ctrl+l */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ENTER = 13, /* Enter */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_N = 14, /* Ctrl-n */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_P = 16, /* Ctrl-p */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_T = 20, /* Ctrl-t */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_U = 21, /* Ctrl+u */
199767f8919635c4928607450d9e0abb932109ceToomas Soome CTRL_W = 23, /* Ctrl+w */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ESC = 27, /* Escape */
199767f8919635c4928607450d9e0abb932109ceToomas Soome BACKSPACE = 127 /* Backspace */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void refreshLine(struct linenoiseState *l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ======================= Low level terminal handling ====================== */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomeput_bytes(const char *s, int len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (s == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < len; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome putchar(s[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (i);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Set if to use or not the multi line mode. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid linenoiseSetMultiLine(int ml) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome mlmode = ml;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Clear the screen. Used to handle ctrl+l */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid linenoiseClearScreen(void) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (put_bytes("\x1b[H\x1b[J", 6) <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* nothing to do, just to avoid warning. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomegetColumns(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *columns = getenv("COLUMNS");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (columns == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (80);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (strtol(columns, NULL, 0));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Beep, used for completion when there is nothing to complete or when all
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the choices were already shown. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void linenoiseBeep(void) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_bytes("\x7", 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ============================== Completion ================================ */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Free a list of completion option populated by linenoiseAddCompletion(). */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void freeCompletions(linenoiseCompletions *lc) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < lc->len; i++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(lc->cvec[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lc->cvec != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(lc->cvec);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 * input.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 linenoiseCompletions lc = { 0, NULL };
199767f8919635c4928607450d9e0abb932109ceToomas Soome int nwritten;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome completionCallback(ls->buf,&lc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (lc.len == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseBeep();
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t stop = 0, i = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(!stop) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Show completion or original buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i < lc.len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct linenoiseState saved = *ls;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome ls->len = ls->pos = strlen(lc.cvec[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ls->buf = lc.cvec[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(ls);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ls->len = saved.len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ls->pos = saved.pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ls->buf = saved.buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(ls);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = getchar();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome freeCompletions(&lc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(c) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 9: /* tab */
199767f8919635c4928607450d9e0abb932109ceToomas Soome i = (i+1) % (lc.len+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i == lc.len) linenoiseBeep();
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 27: /* escape */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Re-show original buffer */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i < lc.len) refreshLine(ls);
199767f8919635c4928607450d9e0abb932109ceToomas Soome stop = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Update buffer and return */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (i < lc.len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ls->len = ls->pos = nwritten;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome stop = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome freeCompletions(&lc);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return c; /* Return last read character */
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Register a callback function to be called for tab-completion. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome completionCallback = fn;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 size_t len = strlen(str);
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *copy, **cvec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome copy = malloc(len+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (copy == NULL) return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(copy,str,len+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cvec = realloc(lc->cvec,sizeof(char*)*(lc->len+1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (cvec == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(copy);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome lc->cvec = cvec;
199767f8919635c4928607450d9e0abb932109ceToomas Soome lc->cvec[lc->len++] = copy;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* =========================== Line editing ================================= */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soomestruct abuf {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void abInit(struct abuf *ab) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome ab->b = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ab->len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void abAppend(struct abuf *ab, const char *s, int len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *new = malloc(ab->len+len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (new == NULL) return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(new, ab->b, ab->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(new+ab->len,s,len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(ab->b);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ab->b = new;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ab->len += len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void abFree(struct abuf *ab) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(ab->b);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Single line low level line refresh.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 char seq[64];
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t plen = strlen(l->prompt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *buf = l->buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t len = l->len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t pos = l->pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct abuf ab;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while((plen+pos) >= l->cols) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome len--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome pos--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (plen+len > l->cols) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome len--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome abInit(&ab);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Cursor to left edge */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r");
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Write the prompt and the current buffer content */
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,l->prompt,strlen(l->prompt));
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,buf,len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Erase to right */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\x1b[K");
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Move cursor to original position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen));
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_bytes(ab.b, ab.len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome abFree(&ab);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Multi line low level line refresh.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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 char seq[64];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int plen = strlen(l->prompt);
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 int rpos2; /* rpos after refresh. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int col; /* colum position, zero-based. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int old_rows = l->maxrows;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct abuf ab;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Update maxrows if needed. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rows > (int)l->maxrows) l->maxrows = rows;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* First step: clear all the lines used before. To do so start by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * going to the last row. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome abInit(&ab);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (old_rows-rpos > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Now for every row clear it, go up. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < old_rows-1; j++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r\x1b[0K\x1b[1A");
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Clean the top line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r\x1b[0K");
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Write the prompt and the current buffer content */
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,l->prompt,strlen(l->prompt));
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,l->buf,l->len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 if (l->pos &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos == l->len &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (l->pos+plen) % l->cols == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome {
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,"\n",1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r");
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome rows++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rows > (int)l->maxrows) l->maxrows = rows;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Move cursor to right position. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Go up till we reach the expected positon. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rows-rpos2 > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\x1b[%dA", rows-rpos2);
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set column. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome col = (plen+(int)l->pos) % (int)l->cols;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (col)
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r\x1b[%dC", col);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome snprintf(seq,64,"\r");
199767f8919635c4928607450d9e0abb932109ceToomas Soome abAppend(&ab,seq,strlen(seq));
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->oldpos = l->pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome put_bytes(ab.b, ab.len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome abFree(&ab);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 if (mlmode)
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshMultiLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshSingleLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Insert the character 'c' at cursor current position.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * On error writing to the terminal -1 is returned, otherwise 0. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditInsert(struct linenoiseState *l, char c) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->len < l->buflen) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->len == l->pos) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->pos] = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->len++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->len] = '\0';
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 putchar(c);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->pos] = c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->len++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->len] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor on the left. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveLeft(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor on the right. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveRight(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos != l->len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor to the start of the line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveHome(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Move cursor to the end of the line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditMoveEnd(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos != l->len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos = l->len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Substitute the currently edited line with the next or previous history
199767f8919635c4928607450d9e0abb932109ceToomas Soome * entry as specified by 'dir'. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LINENOISE_HISTORY_NEXT 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define LINENOISE_HISTORY_PREV 1
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditHistoryNext(struct linenoiseState *l, int dir) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_len > 1) {
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 if (l->history_index < 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->history_index = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (l->history_index >= history_len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->history_index = history_len-1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->buflen-1] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->len = l->pos = strlen(l->buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditDelete(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->len > 0 && l->pos < l->len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->len--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->len] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Backspace implementation. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditBackspace(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l->pos > 0 && l->len > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos);
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->len--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->buf[l->len] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Delete the previosu word, maintaining the cursor at the start of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * current word. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void
199767f8919635c4928607450d9e0abb932109ceToomas SoomelinenoiseEditDeletePrevWord(struct linenoiseState *l) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t old_pos = l->pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t diff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (l->pos > 0 && l->buf[l->pos-1] == ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome while (l->pos > 0 && l->buf[l->pos-1] != ' ')
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->pos--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome diff = old_pos - l->pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome l->len -= diff;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The resulting string is put into 'buf' when the user type enter, or
199767f8919635c4928607450d9e0abb932109ceToomas Soome * when ctrl+d is typed.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
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{
199767f8919635c4928607450d9e0abb932109ceToomas Soome struct linenoiseState l;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Populate the linenoise state that we pass to functions implementing
199767f8919635c4928607450d9e0abb932109ceToomas Soome * specific editing functionalities. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.buf = buf;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.buflen = buflen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.prompt = prompt;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.plen = strlen(prompt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.oldpos = l.pos = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.cols = getColumns();
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.maxrows = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.history_index = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Buffer starts empty. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.buf[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.buflen--; /* Make sure there is always space for the nulterm */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The latest history entry is always our current buffer, that
199767f8919635c4928607450d9e0abb932109ceToomas Soome * initially is just an empty string. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseHistoryAdd("");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf ("%s", prompt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char c;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char seq[3];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = getchar();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c == -1)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 if (c == 9 && completionCallback != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome c = completeLine(&l);
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 }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(c) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case ENTER: /* enter */
199767f8919635c4928607450d9e0abb932109ceToomas Soome history_len--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(history[history_len]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (mlmode) linenoiseEditMoveEnd(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (int)l.len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_C: /* ctrl-c */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.pos = l.len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome case BACKSPACE: /* backspace */
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 8: /* ctrl-h */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditBackspace(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_D: /* ctrl-d, remove char at right of cursor. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l.len > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditDelete(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_T: /* ctrl-t, swaps current character with previous. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l.pos > 0 && l.pos < l.len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int aux = buf[l.pos-1];
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[l.pos-1] = buf[l.pos];
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[l.pos] = aux;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (l.pos != l.len-1) l.pos++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_B: /* ctrl-b */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveLeft(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_F: /* ctrl-f */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveRight(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_P: /* ctrl-p */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_N: /* ctrl-n */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case ESC: /* escape sequence */
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 seq[0] = getchar();
199767f8919635c4928607450d9e0abb932109ceToomas Soome seq[1] = getchar();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ESC [ sequences. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (seq[0] == '[') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (seq[1] >= '0' && seq[1] <= '9') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Extended escape, read additional byte. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome seq[2] = getchar();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (seq[2] == '~') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(seq[1]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case '3': /* Delete key. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditDelete(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(seq[1]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'A': /* Up */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'B': /* Down */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'C': /* Right */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveRight(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'D': /* Left */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveLeft(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'H': /* Home */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveHome(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'F': /* End*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveEnd(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* ESC O sequences. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (seq[0] == 'O') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome switch(seq[1]) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'H': /* Home */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveHome(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case 'F': /* End*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveEnd(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome default:
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (linenoiseEditInsert(&l,c)) return -1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_U: /* Ctrl+u, delete the whole line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[0] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.pos = l.len = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_K: /* Ctrl+k, delete from current to end of line. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[l.pos] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome l.len = l.pos;
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_A: /* Ctrl+a, go to the start of the line */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveHome(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_E: /* ctrl+e, go to the end of the line */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditMoveEnd(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_L: /* ctrl+l, clear screen */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseClearScreen();
199767f8919635c4928607450d9e0abb932109ceToomas Soome refreshLine(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome case CTRL_W: /* ctrl+w, delete previous word */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseEditDeletePrevWord(&l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return l.len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soomechar *linenoise(const char *prompt) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[LINENOISE_MAX_LINE];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int count;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome cons_mode(C_MODERAW);
199767f8919635c4928607450d9e0abb932109ceToomas Soome count = linenoiseEdit(buf,LINENOISE_MAX_LINE,prompt);
199767f8919635c4928607450d9e0abb932109ceToomas Soome cons_mode(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (count == -1) return NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return strdup(buf);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* ================================ History ================================= */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Using a circular buffer is smarter, but a bit more complex to handle. */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint linenoiseHistoryAdd(const char *line) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *linecopy;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_max_len == 0) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Initialization on first call. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history == NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome history = malloc(sizeof(char*)*history_max_len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history == NULL) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(history,0,(sizeof(char*)*history_max_len));
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Don't add duplicated lines. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_len && !strcmp(history[history_len-1], line)) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 linecopy = strdup(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!linecopy) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_len == history_max_len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(history[0]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memmove(history,history+1,sizeof(char*)*(history_max_len-1));
199767f8919635c4928607450d9e0abb932109ceToomas Soome history_len--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome history[history_len] = linecopy;
199767f8919635c4928607450d9e0abb932109ceToomas Soome history_len++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
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 Soomeint linenoiseHistorySetMaxLen(int len) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char **new;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len < 1) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int tocopy = history_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome new = malloc(sizeof(char*)*len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (new == NULL) return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If we can't copy everything, free the elements we'll not use. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (len < tocopy) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome int j;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (j = 0; j < tocopy-len; j++) free(history[j]);
199767f8919635c4928607450d9e0abb932109ceToomas Soome tocopy = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome memset(new,0,sizeof(char*)*len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome memcpy(new,history+(history_len-tocopy), sizeof(char*)*tocopy);
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(history);
199767f8919635c4928607450d9e0abb932109ceToomas Soome history = new;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome history_max_len = len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (history_len > history_max_len)
199767f8919635c4928607450d9e0abb932109ceToomas Soome history_len = history_max_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}