2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Adapted from the following:
34 * linenoise.c -- guerrilla line editing library against the idea that a
35 * line editing lib needs to be 20,000 lines of C code.
37 * You can find the original source code at:
38 * http://github.com/antirez/linenoise
40 * You can find the fork that this code is based on at:
41 * https://github.com/rain-1/linenoise-mob
43 * ------------------------------------------------------------------------
45 * This code is also under the following license:
47 * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
48 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions are
54 * * Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
57 * * Redistributions in binary form must reproduce the above copyright
58 * notice, this list of conditions and the following disclaimer in the
59 * documentation and/or other materials provided with the distribution.
61 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
62 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
63 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
64 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
65 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
67 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
68 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
69 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
71 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 * *****************************************************************************
75 * Definitions for line history.
82 #ifndef BC_ENABLE_HISTORY
83 #define BC_ENABLE_HISTORY (1)
84 #endif // BC_ENABLE_HISTORY
89 #error History is not supported on Windows.
100 #include <sys/select.h>
108 #endif // BC_DEBUG_CODE
110 #define BC_HIST_DEF_COLS (80)
111 #define BC_HIST_MAX_LEN (128)
112 #define BC_HIST_MAX_LINE (4095)
113 #define BC_HIST_SEQ_SIZE (64)
115 #define BC_HIST_BUF_LEN(h) ((h)->buf.len - 1)
116 #define BC_HIST_READ(s, n) (bc_history_read((s), (n)) == -1)
118 #define BC_HIST_NEXT (false)
119 #define BC_HIST_PREV (true)
123 #define BC_HISTORY_DEBUG_BUF_SIZE (1024)
125 #define lndebug(...) \
127 if (bc_history_debug_fp.fd == 0) { \
128 bc_history_debug_buf = bc_vm_malloc(BC_HISTORY_DEBUG_BUF_SIZE); \
129 bc_file_init(&bc_history_debug_fp, \
130 open("/tmp/lndebug.txt", O_APPEND), \
131 BC_HISTORY_DEBUG_BUF_SIZE); \
132 bc_file_printf(&bc_history_debug_fp, \
133 "[%zu %zu %zu] p: %d, rows: %d, " \
134 "rpos: %d, max: %zu, oldmax: %d\n", \
135 l->len, l->pos, l->oldcolpos, plen, rows, rpos, \
136 l->maxrows, old_rows); \
138 bc_file_printf(&bc_history_debug_fp, ", " __VA_ARGS__); \
139 bc_file_flush(&bc_history_debug_fp); \
141 #else // BC_DEBUG_CODE
142 #define lndebug(fmt, ...)
143 #endif // BC_DEBUG_CODE
145 #if !BC_ENABLE_PROMPT
146 #define bc_history_line(h, vec, prompt) bc_history_line(h, vec)
147 #define bc_history_raw(h, prompt) bc_history_raw(h)
148 #define bc_history_edit(h, prompt) bc_history_edit(h)
149 #endif // BC_ENABLE_PROMPT
151 typedef enum BcHistoryAction {
154 BC_ACTION_CTRL_A = 1,
155 BC_ACTION_CTRL_B = 2,
156 BC_ACTION_CTRL_C = 3,
157 BC_ACTION_CTRL_D = 4,
158 BC_ACTION_CTRL_E = 5,
159 BC_ACTION_CTRL_F = 6,
160 BC_ACTION_CTRL_H = 8,
162 BC_ACTION_LINE_FEED = 10,
163 BC_ACTION_CTRL_K = 11,
164 BC_ACTION_CTRL_L = 12,
165 BC_ACTION_ENTER = 13,
166 BC_ACTION_CTRL_N = 14,
167 BC_ACTION_CTRL_P = 16,
168 BC_ACTION_CTRL_T = 20,
169 BC_ACTION_CTRL_U = 21,
170 BC_ACTION_CTRL_W = 23,
171 BC_ACTION_CTRL_Z = 26,
173 BC_ACTION_BACKSPACE = 127
178 * This represents the state during line editing. We pass this state
179 * to functions implementing specific editing functionalities.
181 typedef struct BcHistory {
183 /// Edited line buffer.
190 /// Prompt to display.
195 #endif // BC_ENABLE_PROMPT
197 /// Prompt column length.
200 /// Current cursor position.
203 /// Previous refresh cursor column position.
206 /// Number of columns in terminal.
209 /// The history index we are currently editing.
212 /// The original terminal state.
213 struct termios orig_termios;
215 /// These next three are here because pahole found a 4 byte hole here.
217 /// This is to signal that there is more, so we don't process yet.
220 /// Whether we are in rawmode.
223 /// Whether the terminal is bad.
226 /// This is to check if stdin has more data.
229 /// This is to check if stdin has more data.
232 /// This is to check if stdin has more data.
237 BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt);
239 void bc_history_init(BcHistory *h);
240 void bc_history_free(BcHistory *h);
242 extern const char *bc_history_bad_terms[];
243 extern const char bc_history_tab[];
244 extern const size_t bc_history_tab_len;
245 extern const char bc_history_ctrlc[];
246 extern const uint32_t bc_history_wchars[][2];
247 extern const size_t bc_history_wchars_len;
248 extern const uint32_t bc_history_combo_chars[];
249 extern const size_t bc_history_combo_chars_len;
251 extern BcFile bc_history_debug_fp;
252 extern char *bc_history_debug_buf;
253 void bc_history_printKeyCodes(BcHistory* l);
254 #endif // BC_DEBUG_CODE
256 #endif // BC_ENABLE_HISTORY
258 #endif // BC_HISTORY_H