cemu
载入中...
搜索中...
未找到
linenoise.c 文件参考

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
 

宏定义

#define LINENOISE_DEFAULT_HISTORY_MAX_LEN   100
 
#define LINENOISE_MAX_LINE   4096
 
#define REFRESH_CLEAN   (1<<0)
 
#define REFRESH_WRITE   (1<<1)
 
#define REFRESH_ALL   (REFRESH_CLEAN|REFRESH_WRITE)
 
#define lndebug(fmt, ...)
 
#define LINENOISE_HISTORY_NEXT   0
 
#define LINENOISE_HISTORY_PREV   1
 

枚举

enum  KEY_ACTION {
  KEY_NULL = 0 , CTRL_A = 1 , CTRL_B = 2 , CTRL_C = 3 ,
  CTRL_D = 4 , CTRL_E = 5 , CTRL_F = 6 , CTRL_H = 8 ,
  TAB = 9 , CTRL_K = 11 , CTRL_L = 12 , ENTER = 13 ,
  CTRL_N = 14 , CTRL_P = 16 , CTRL_T = 20 , CTRL_U = 21 ,
  CTRL_W = 23 , ESC = 27 , BACKSPACE = 127
}
 

函数

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工具库实现

作者
lancer (lance.nosp@m.rsta.nosp@m.dium@.nosp@m.163..nosp@m.com)
版本
0.1
日期
2023-12-28
注解
参考项目:Github | linenoise

Introduce

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:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

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:

  • Filter bogus Ctrl+<char> combinations.
  • Win32 support

Bloat:

  • History search like Ctrl+r in readline?

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