2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 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
86 #ifndef BC_ENABLE_EDITLINE
87 #define BC_ENABLE_EDITLINE (0)
88 #endif // BC_ENABLE_EDITLINE
90 #ifndef BC_ENABLE_READLINE
91 #define BC_ENABLE_READLINE (0)
92 #endif // BC_ENABLE_READLINE
94 #if BC_ENABLE_EDITLINE && BC_ENABLE_READLINE
95 #error Must enable only one of editline or readline, not both.
96 #endif // BC_ENABLE_EDITLINE && BC_ENABLE_READLINE
98 #if BC_ENABLE_EDITLINE || BC_ENABLE_READLINE
99 #define BC_ENABLE_LINE_LIB (1)
100 #else // BC_ENABLE_EDITLINE || BC_ENABLE_READLINE
101 #define BC_ENABLE_LINE_LIB (0)
102 #endif // BC_ENABLE_EDITLINE || BC_ENABLE_READLINE
104 #if BC_ENABLE_LINE_LIB
113 extern sigjmp_buf bc_history_jmpbuf;
114 extern volatile sig_atomic_t bc_history_inlinelib;
116 #endif // BC_ENABLE_LINE_LIB
118 #if BC_ENABLE_EDITLINE
121 #include <histedit.h>
124 * The history struct for editline.
126 typedef struct BcHistory
128 /// A place to store the current line.
134 /// Whether the terminal is bad. This is more or less not used.
139 // The path to the editrc and its length.
140 extern const char bc_history_editrc[];
141 extern const size_t bc_history_editrc_len;
143 #else // BC_ENABLE_EDITLINE
145 #if BC_ENABLE_READLINE
148 #include <readline/readline.h>
149 #include <readline/history.h>
152 * The history struct for readline.
154 typedef struct BcHistory
156 /// A place to store the current line.
159 /// Whether the terminal is bad. This is more or less not used.
164 #else // BC_ENABLE_READLINE
166 #if BC_ENABLE_HISTORY
176 #include <sys/select.h>
179 #ifndef WIN32_LEAN_AND_MEAN
180 #define WIN32_LEAN_AND_MEAN
181 #endif // WIN32_LEAN_AND_MEAN
187 #define strncasecmp _strnicmp
188 #define strcasecmp _stricmp
198 #endif // BC_DEBUG_CODE
201 #define BC_HIST_DEF_COLS (80)
203 /// Max number of history entries.
204 #define BC_HIST_MAX_LEN (128)
206 /// Max length of a line.
207 #define BC_HIST_MAX_LINE (4095)
209 /// Max size for cursor position buffer.
210 #define BC_HIST_SEQ_SIZE (64)
213 * The number of entries in the history.
214 * @param h The history data.
216 #define BC_HIST_BUF_LEN(h) ((h)->buf.len - 1)
219 * Read n characters into s and check the error.
220 * @param s The buffer to read into.
221 * @param n The number of bytes to read.
222 * @return True if there was an error, false otherwise.
224 #define BC_HIST_READ(s, n) (bc_history_read((s), (n)) == -1)
226 /// Markers for direction when using arrow keys.
227 #define BC_HIST_NEXT (false)
228 #define BC_HIST_PREV (true)
232 // These are just for debugging.
234 #define BC_HISTORY_DEBUG_BUF_SIZE (1024)
237 #define lndebug(...) \
240 if (bc_history_debug_fp.fd == 0) \
242 bc_history_debug_buf = bc_vm_malloc(BC_HISTORY_DEBUG_BUF_SIZE); \
243 bc_file_init(&bc_history_debug_fp, \
244 open("/tmp/lndebug.txt", O_APPEND), \
245 BC_HISTORY_DEBUG_BUF_SIZE); \
246 bc_file_printf(&bc_history_debug_fp, \
247 "[%zu %zu %zu] p: %d, rows: %d, " \
248 "rpos: %d, max: %zu, oldmax: %d\n", \
249 l->len, l->pos, l->oldcolpos, plen, rows, rpos, \
250 l->maxrows, old_rows); \
252 bc_file_printf(&bc_history_debug_fp, ", " __VA_ARGS__); \
253 bc_file_flush(&bc_history_debug_fp); \
256 #else // BC_DEBUG_CODE
257 #define lndebug(fmt, ...)
258 #endif // BC_DEBUG_CODE
261 /// An enum of useful actions. To understand what these mean, check terminal
262 /// emulators for their shortcuts or the VT100 codes.
263 typedef enum BcHistoryAction
266 BC_ACTION_CTRL_A = 1,
267 BC_ACTION_CTRL_B = 2,
268 BC_ACTION_CTRL_C = 3,
269 BC_ACTION_CTRL_D = 4,
270 BC_ACTION_CTRL_E = 5,
271 BC_ACTION_CTRL_F = 6,
272 BC_ACTION_CTRL_H = 8,
274 BC_ACTION_LINE_FEED = 10,
275 BC_ACTION_CTRL_K = 11,
276 BC_ACTION_CTRL_L = 12,
277 BC_ACTION_ENTER = 13,
278 BC_ACTION_CTRL_N = 14,
279 BC_ACTION_CTRL_P = 16,
280 BC_ACTION_CTRL_S = 19,
281 BC_ACTION_CTRL_T = 20,
282 BC_ACTION_CTRL_U = 21,
283 BC_ACTION_CTRL_W = 23,
284 BC_ACTION_CTRL_Z = 26,
286 BC_ACTION_CTRL_BSLASH = 28,
287 BC_ACTION_BACKSPACE = 127
292 * This represents the state during line editing. We pass this state
293 * to functions implementing specific editing functionalities.
295 typedef struct BcHistory
297 /// Edited line buffer.
303 /// Any material printed without a trailing newline.
306 /// Prompt to display.
312 /// Prompt column length.
315 /// Current cursor position.
318 /// Previous refresh cursor column position.
321 /// Number of columns in terminal.
324 /// The history index we are currently editing.
328 /// The original terminal state.
329 struct termios orig_termios;
331 /// The original input console mode.
334 /// The original output console mode.
338 /// These next two are here because pahole found a 4 byte hole here.
340 /// Whether we are in rawmode.
343 /// Whether the terminal is bad.
347 /// This is to check if stdin has more data.
350 /// This is to check if stdin has more data.
353 /// This is to check if stdin has more data.
360 * Frees strings used by history.
361 * @param str The string to free.
364 bc_history_string_free(void* str);
366 // A list of terminals that don't work.
367 extern const char* bc_history_bad_terms[];
369 // A tab in history and its length.
370 extern const char bc_history_tab[];
371 extern const size_t bc_history_tab_len;
374 extern const char bc_history_ctrlc[];
376 // UTF-8 data arrays.
377 extern const uint32_t bc_history_wchars[][2];
378 extern const size_t bc_history_wchars_len;
379 extern const uint32_t bc_history_combo_chars[];
380 extern const size_t bc_history_combo_chars_len;
385 extern BcFile bc_history_debug_fp;
386 extern char* bc_history_debug_buf;
389 * A function to print keycodes for debugging.
390 * @param h The history data.
393 bc_history_printKeyCodes(BcHistory* h);
395 #endif // BC_DEBUG_CODE
397 #endif // BC_ENABLE_HISTORY
399 #endif // BC_ENABLE_READLINE
401 #endif // BC_ENABLE_EDITLINE
403 #if BC_ENABLE_HISTORY
406 * Get a line from stdin using history. This returns a status because I don't
407 * want to throw errors while the terminal is in raw mode.
408 * @param h The history data.
409 * @param vec A vector to put the line into.
410 * @param prompt The prompt to display, if desired.
411 * @return A status indicating an error, if any. Returning a status here
412 * is better because if we throw an error out of history, we
413 * leave the terminal in raw mode or in some other half-baked
417 bc_history_line(BcHistory* h, BcVec* vec, const char* prompt);
420 * Initialize history data.
421 * @param h The struct to initialize.
424 bc_history_init(BcHistory* h);
427 * Free history data (and recook the terminal).
428 * @param h The struct to free.
431 bc_history_free(BcHistory* h);
433 #endif // BC_ENABLE_HISTORY
435 #endif // BC_HISTORY_H