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