|
cemu
|
REPL工具库实现 更多...
#include <termios.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#include <ctype.h>#include <sys/stat.h>#include <sys/types.h>#include <sys/ioctl.h>#include "linenoise.h"类 | |
| struct | abuf |
函数 | |
| static char * | linenoiseNoTTY (void) |
| static void | refreshLineWithCompletion (struct linenoiseState *ls, linenoiseCompletions *lc, int flags) |
| static void | refreshLineWithFlags (struct linenoiseState *l, int flags) |
| static void | linenoiseAtExit (void) |
| int | linenoiseHistoryAdd (const char *line) |
| static void | refreshLine (struct linenoiseState *l) |
| void | linenoiseMaskModeEnable (void) |
| void | linenoiseMaskModeDisable (void) |
| void | linenoiseSetMultiLine (int ml) |
| static int | isUnsupportedTerm (void) |
| static int | enableRawMode (int fd) |
| static void | disableRawMode (int fd) |
| static int | getCursorPosition (int ifd, int ofd) |
| static int | getColumns (int ifd, int ofd) |
| void | linenoiseClearScreen (void) |
| static void | linenoiseBeep (void) |
| static void | freeCompletions (linenoiseCompletions *lc) |
| static int | completeLine (struct linenoiseState *ls, int keypressed) |
| void | linenoiseSetCompletionCallback (linenoiseCompletionCallback *fn) |
| void | linenoiseSetHintsCallback (linenoiseHintsCallback *fn) |
| void | linenoiseSetFreeHintsCallback (linenoiseFreeHintsCallback *fn) |
| void | linenoiseAddCompletion (linenoiseCompletions *lc, const char *str) |
| static void | abInit (struct abuf *ab) |
| static void | abAppend (struct abuf *ab, const char *s, int len) |
| static void | abFree (struct abuf *ab) |
| void | refreshShowHints (struct abuf *ab, struct linenoiseState *l, int plen) |
| static void | refreshSingleLine (struct linenoiseState *l, int flags) |
| static void | refreshMultiLine (struct linenoiseState *l, int flags) |
| void | linenoiseHide (struct linenoiseState *l) |
| void | linenoiseShow (struct linenoiseState *l) |
| int | linenoiseEditInsert (struct linenoiseState *l, char c) |
| void | linenoiseEditMoveLeft (struct linenoiseState *l) |
| void | linenoiseEditMoveRight (struct linenoiseState *l) |
| void | linenoiseEditMoveHome (struct linenoiseState *l) |
| void | linenoiseEditMoveEnd (struct linenoiseState *l) |
| void | linenoiseEditHistoryNext (struct linenoiseState *l, int dir) |
| void | linenoiseEditDelete (struct linenoiseState *l) |
| void | linenoiseEditBackspace (struct linenoiseState *l) |
| void | linenoiseEditDeletePrevWord (struct linenoiseState *l) |
| int | linenoiseEditStart (struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) |
| char * | linenoiseEditFeed (struct linenoiseState *l) |
| void | linenoiseEditStop (struct linenoiseState *l) |
| static char * | linenoiseBlockingEdit (int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) |
| void | linenoisePrintKeyCodes (void) |
| char * | linenoise (const char *prompt) |
| void | linenoiseFree (void *ptr) |
| static void | freeHistory (void) |
| int | linenoiseHistorySetMaxLen (int len) |
| int | linenoiseHistorySave (const char *filename) |
| int | linenoiseHistoryLoad (const char *filename) |
变量 | |
| static char * | unsupported_term [] = {"dumb","cons25","emacs",NULL} |
| static linenoiseCompletionCallback * | completionCallback = NULL |
| static linenoiseHintsCallback * | hintsCallback = NULL |
| static linenoiseFreeHintsCallback * | freeHintsCallback = NULL |
| static struct termios | orig_termios |
| static int | maskmode = 0 |
| static int | rawmode = 0 |
| static int | mlmode = 0 |
| static int | atexit_registered = 0 |
| static int | history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN |
| static int | history_len = 0 |
| static char ** | history = NULL |
| char * | linenoiseEditMore = "If you see this, you are misusing the API: when linenoiseEditFeed() is called, if it returns linenoiseEditMore the user is yet editing the line. See the README file for more information." |
REPL工具库实现
linenoise.c – guerrilla line editing library against the idea that a line editing lib needs to be 20,000 lines of C code.
You can find the latest source code at:
http://github.com/antirez/linenoise
Does a number of crazy assumptions that happen to be true in 99.9999% of the 2010 UNIX computers around.
Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com> Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
References:
Todo list:
Bloat:
List of escape sequences used by this program, we do everything just with three sequences. In order to be so cheap we may have some flickering effect with some slow terminal, but the lesser sequences the more compatible.
EL (Erase Line) Sequence: ESC [ n K Effect: if n is 0 or missing, clear from cursor to end of line Effect: if n is 1, clear from beginning of line to cursor Effect: if n is 2, clear entire line
CUF (CUrsor Forward) Sequence: ESC [ n C Effect: moves cursor forward n chars
CUB (CUrsor Backward) Sequence: ESC [ n D Effect: moves cursor backward n chars
The following is used to get the terminal width if getting the width with the TIOCGWINSZ ioctl fails
DSR (Device Status Report) Sequence: ESC [ 6 n Effect: reports the current cusor position as ESC [ n ; m R where n is the row and m is the column
When multi line mode is enabled, we also use an additional escape sequence. However multi line editing is disabled by default.
CUU (Cursor Up) Sequence: ESC [ n A Effect: moves cursor up of n chars.
CUD (Cursor Down) Sequence: ESC [ n B Effect: moves cursor down of n chars.
When linenoiseClearScreen() is called, two additional escape sequences are used in order to clear the screen and position the cursor at home position.
CUP (Cursor position) Sequence: ESC [ H Effect: moves the cursor to upper left corner
ED (Erase display) Sequence: ESC [ 2 J Effect: clear the whole screen