Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ***************************************************************************** |
| 3 | * |
Gavin Howard | 29e00ba | 2020-06-30 09:25:21 -0600 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-2-Clause |
| 5 | * |
Zach van Rijn | 6d2cf3f | 2020-01-14 22:05:02 +0000 | [diff] [blame] | 6 | * Copyright (c) 2018-2020 Gavin D. Howard and contributors. |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 7 | * |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright notice, this |
| 12 | * list of conditions and the following disclaimer. |
| 13 | * |
| 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. |
| 17 | * |
| 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. |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 29 | * |
| 30 | * ***************************************************************************** |
| 31 | * |
| 32 | * Adapted from the following: |
| 33 | * |
| 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. |
| 36 | * |
| 37 | * You can find the original source code at: |
| 38 | * http://github.com/antirez/linenoise |
| 39 | * |
Gavin Howard | f15f904 | 2018-12-12 12:40:41 -0700 | [diff] [blame] | 40 | * You can find the fork that this code is based on at: |
| 41 | * https://github.com/rain-1/linenoise-mob |
| 42 | * |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 43 | * ------------------------------------------------------------------------ |
| 44 | * |
| 45 | * This code is also under the following license: |
| 46 | * |
| 47 | * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com> |
| 48 | * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com> |
| 49 | * |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 50 | * Redistribution and use in source and binary forms, with or without |
| 51 | * modification, are permitted provided that the following conditions are |
| 52 | * met: |
| 53 | * |
| 54 | * * Redistributions of source code must retain the above copyright |
| 55 | * notice, this list of conditions and the following disclaimer. |
| 56 | * |
| 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. |
| 60 | * |
| 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. |
| 72 | * |
| 73 | * ***************************************************************************** |
| 74 | * |
| 75 | * Definitions for line history. |
| 76 | * |
| 77 | */ |
| 78 | |
Gavin Howard | 641fe97 | 2018-12-12 04:06:03 -0700 | [diff] [blame] | 79 | #ifndef BC_HISTORY_H |
| 80 | #define BC_HISTORY_H |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 81 | |
Gavin Howard | 77d016f | 2019-04-22 09:44:43 -0600 | [diff] [blame] | 82 | #ifndef BC_ENABLE_HISTORY |
| 83 | #define BC_ENABLE_HISTORY (1) |
| 84 | #endif // BC_ENABLE_HISTORY |
| 85 | |
Gavin Howard | 1545ffe | 2018-12-24 11:26:46 -0700 | [diff] [blame] | 86 | #if BC_ENABLE_HISTORY |
| 87 | |
Gavin Howard | 7fa6a34 | 2018-12-24 11:57:54 -0700 | [diff] [blame] | 88 | #ifdef _WIN32 |
Gavin Howard | 8c34191 | 2018-12-24 11:54:41 -0700 | [diff] [blame] | 89 | #error History is not supported on Windows. |
Gavin Howard | dac1cb8 | 2020-04-09 14:01:49 -0600 | [diff] [blame] | 90 | #endif // _WIN32 |
Gavin Howard | 8c34191 | 2018-12-24 11:54:41 -0700 | [diff] [blame] | 91 | |
Gavin Howard | bceb32d | 2018-12-12 22:23:51 -0700 | [diff] [blame] | 92 | #include <stdbool.h> |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 93 | #include <stddef.h> |
| 94 | |
Gavin Howard | 95e244b | 2019-06-14 23:33:33 -0600 | [diff] [blame] | 95 | #include <signal.h> |
| 96 | |
Gavin Howard | bceb32d | 2018-12-12 22:23:51 -0700 | [diff] [blame] | 97 | #include <termios.h> |
Gavin Howard | 95e244b | 2019-06-14 23:33:33 -0600 | [diff] [blame] | 98 | #include <time.h> |
Gavin Howard | bceb32d | 2018-12-12 22:23:51 -0700 | [diff] [blame] | 99 | #include <unistd.h> |
Gavin Howard | 95e244b | 2019-06-14 23:33:33 -0600 | [diff] [blame] | 100 | #include <sys/select.h> |
Gavin Howard | bceb32d | 2018-12-12 22:23:51 -0700 | [diff] [blame] | 101 | |
Gavin Howard | 7fd3e3f | 2018-12-13 16:19:46 -0700 | [diff] [blame] | 102 | #include <status.h> |
Gavin Howard | 9f9bef4 | 2020-10-22 21:37:46 -0600 | [diff] [blame^] | 103 | #include "vector.h" |
| 104 | #include "read.h" |
Gavin Howard | 7fd3e3f | 2018-12-13 16:19:46 -0700 | [diff] [blame] | 105 | |
Gavin Howard | c86dcd7 | 2020-05-22 19:23:57 -0600 | [diff] [blame] | 106 | #if BC_DEBUG_CODE |
Gavin Howard | 9f9bef4 | 2020-10-22 21:37:46 -0600 | [diff] [blame^] | 107 | #include "file.h" |
Gavin Howard | c86dcd7 | 2020-05-22 19:23:57 -0600 | [diff] [blame] | 108 | #endif // BC_DEBUG_CODE |
| 109 | |
Gavin Howard | 5f349d4 | 2019-04-09 14:14:00 -0600 | [diff] [blame] | 110 | #define BC_HIST_DEF_COLS (80) |
| 111 | #define BC_HIST_MAX_LEN (128) |
| 112 | #define BC_HIST_MAX_LINE (4095) |
Gavin Howard | aa0b756 | 2019-06-17 22:34:11 -0600 | [diff] [blame] | 113 | #define BC_HIST_SEQ_SIZE (64) |
Gavin Howard | 939bbe4 | 2018-12-12 12:04:49 -0700 | [diff] [blame] | 114 | |
Gavin Howard | 5f349d4 | 2019-04-09 14:14:00 -0600 | [diff] [blame] | 115 | #define BC_HIST_BUF_LEN(h) ((h)->buf.len - 1) |
Gavin Howard | 1f1fab7 | 2020-06-04 19:13:44 -0600 | [diff] [blame] | 116 | #define BC_HIST_READ(s, n) (bc_history_read((s), (n)) == -1) |
Gavin Howard | 63c12c6 | 2018-12-13 18:20:55 -0700 | [diff] [blame] | 117 | |
Gavin Howard | 5f349d4 | 2019-04-09 14:14:00 -0600 | [diff] [blame] | 118 | #define BC_HIST_NEXT (false) |
| 119 | #define BC_HIST_PREV (true) |
Gavin Howard | 20f6edb | 2018-12-13 00:41:19 -0700 | [diff] [blame] | 120 | |
Gavin Howard | 15afde9 | 2019-04-11 10:05:32 -0600 | [diff] [blame] | 121 | #if BC_DEBUG_CODE |
Gavin Howard | c86dcd7 | 2020-05-22 19:23:57 -0600 | [diff] [blame] | 122 | |
| 123 | #define BC_HISTORY_DEBUG_BUF_SIZE (1024) |
| 124 | |
| 125 | #define lndebug(...) \ |
| 126 | do { \ |
| 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); \ |
| 137 | } \ |
| 138 | bc_file_printf(&bc_history_debug_fp, ", " __VA_ARGS__); \ |
| 139 | bc_file_flush(&bc_history_debug_fp); \ |
Gavin Howard | 939bbe4 | 2018-12-12 12:04:49 -0700 | [diff] [blame] | 140 | } while (0) |
Gavin Howard | 15afde9 | 2019-04-11 10:05:32 -0600 | [diff] [blame] | 141 | #else // BC_DEBUG_CODE |
Gavin Howard | 939bbe4 | 2018-12-12 12:04:49 -0700 | [diff] [blame] | 142 | #define lndebug(fmt, ...) |
Gavin Howard | 15afde9 | 2019-04-11 10:05:32 -0600 | [diff] [blame] | 143 | #endif // BC_DEBUG_CODE |
Gavin Howard | 7a64f2e | 2018-12-12 11:27:41 -0700 | [diff] [blame] | 144 | |
Gavin Howard | 1444c9f | 2019-06-21 21:16:11 -0600 | [diff] [blame] | 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 |
| 150 | |
Gavin Howard | 800db28 | 2018-12-12 11:07:25 -0700 | [diff] [blame] | 151 | typedef enum BcHistoryAction { |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 152 | |
Gavin Howard | a5b3988 | 2018-12-12 11:27:08 -0700 | [diff] [blame] | 153 | BC_ACTION_NULL = 0, |
| 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, |
Gavin Howard | 95e244b | 2019-06-14 23:33:33 -0600 | [diff] [blame] | 161 | BC_ACTION_TAB = 9, |
Gavin Howard | a5b3988 | 2018-12-12 11:27:08 -0700 | [diff] [blame] | 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, |
Gavin Howard | 348cf2a | 2019-01-08 23:21:25 -0700 | [diff] [blame] | 171 | BC_ACTION_CTRL_Z = 26, |
Gavin Howard | a5b3988 | 2018-12-12 11:27:08 -0700 | [diff] [blame] | 172 | BC_ACTION_ESC = 27, |
| 173 | BC_ACTION_BACKSPACE = 127 |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 174 | |
Gavin Howard | 800db28 | 2018-12-12 11:07:25 -0700 | [diff] [blame] | 175 | } BcHistoryAction; |
| 176 | |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 177 | /** |
| 178 | * This represents the state during line editing. We pass this state |
| 179 | * to functions implementing specific editing functionalities. |
| 180 | */ |
Gavin Howard | 6e04ac2 | 2018-12-12 22:33:53 -0700 | [diff] [blame] | 181 | typedef struct BcHistory { |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 182 | |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 183 | /// Edited line buffer. |
Gavin Howard | fa9cb56 | 2018-12-13 17:06:24 -0700 | [diff] [blame] | 184 | BcVec buf; |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 185 | |
Gavin Howard | 22db819 | 2019-01-04 10:52:21 -0700 | [diff] [blame] | 186 | /// The history. |
| 187 | BcVec history; |
| 188 | |
Gavin Howard | 1444c9f | 2019-06-21 21:16:11 -0600 | [diff] [blame] | 189 | #if BC_ENABLE_PROMPT |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 190 | /// Prompt to display. |
| 191 | const char *prompt; |
| 192 | |
| 193 | /// Prompt length. |
| 194 | size_t plen; |
Gavin Howard | 1444c9f | 2019-06-21 21:16:11 -0600 | [diff] [blame] | 195 | #endif // BC_ENABLE_PROMPT |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 196 | |
Gavin Howard | 5e59c39 | 2019-05-20 07:51:10 -0600 | [diff] [blame] | 197 | /// Prompt column length. |
| 198 | size_t pcol; |
| 199 | |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 200 | /// Current cursor position. |
| 201 | size_t pos; |
| 202 | |
| 203 | /// Previous refresh cursor column position. |
| 204 | size_t oldcolpos; |
| 205 | |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 206 | /// Number of columns in terminal. |
| 207 | size_t cols; |
| 208 | |
| 209 | /// The history index we are currently editing. |
Gavin Howard | 09f5af3 | 2018-12-13 16:48:23 -0700 | [diff] [blame] | 210 | size_t idx; |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 211 | |
| 212 | /// The original terminal state. |
Gavin Howard | bceb32d | 2018-12-12 22:23:51 -0700 | [diff] [blame] | 213 | struct termios orig_termios; |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 214 | |
Gavin Howard | 415cea6 | 2020-05-15 15:51:06 -0600 | [diff] [blame] | 215 | /// These next three are here because pahole found a 4 byte hole here. |
Gavin Howard | 95e244b | 2019-06-14 23:33:33 -0600 | [diff] [blame] | 216 | |
| 217 | /// This is to signal that there is more, so we don't process yet. |
| 218 | bool stdin_has_data; |
| 219 | |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 220 | /// Whether we are in rawmode. |
Gavin Howard | 7fd3e3f | 2018-12-13 16:19:46 -0700 | [diff] [blame] | 221 | bool rawMode; |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 222 | |
Gavin Howard | 417446d | 2018-12-13 14:34:15 -0700 | [diff] [blame] | 223 | /// Whether the terminal is bad. |
| 224 | bool badTerm; |
| 225 | |
Gavin Howard | 415cea6 | 2020-05-15 15:51:06 -0600 | [diff] [blame] | 226 | /// This is to check if stdin has more data. |
| 227 | fd_set rdset; |
| 228 | |
| 229 | /// This is to check if stdin has more data. |
| 230 | struct timespec ts; |
| 231 | |
| 232 | /// This is to check if stdin has more data. |
| 233 | sigset_t sigmask; |
| 234 | |
Gavin Howard | 6e04ac2 | 2018-12-12 22:33:53 -0700 | [diff] [blame] | 235 | } BcHistory; |
Gavin Howard | 939bbe4 | 2018-12-12 12:04:49 -0700 | [diff] [blame] | 236 | |
Gavin Howard | 1d4c99d | 2018-12-13 22:43:37 -0700 | [diff] [blame] | 237 | BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt); |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 238 | |
Gavin Howard | 31488e4 | 2018-12-13 00:57:51 -0700 | [diff] [blame] | 239 | void bc_history_init(BcHistory *h); |
| 240 | void bc_history_free(BcHistory *h); |
Gavin Howard | bceb32d | 2018-12-12 22:23:51 -0700 | [diff] [blame] | 241 | |
Gavin Howard | 939bbe4 | 2018-12-12 12:04:49 -0700 | [diff] [blame] | 242 | extern const char *bc_history_bad_terms[]; |
Gavin Howard | 95e244b | 2019-06-14 23:33:33 -0600 | [diff] [blame] | 243 | extern const char bc_history_tab[]; |
| 244 | extern const size_t bc_history_tab_len; |
Gavin Howard | 286da2a | 2018-12-20 12:33:53 -0700 | [diff] [blame] | 245 | extern const char bc_history_ctrlc[]; |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 246 | extern const uint32_t bc_history_wchars[][2]; |
Gavin Howard | f15f904 | 2018-12-12 12:40:41 -0700 | [diff] [blame] | 247 | extern const size_t bc_history_wchars_len; |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 248 | extern const uint32_t bc_history_combo_chars[]; |
Gavin Howard | f15f904 | 2018-12-12 12:40:41 -0700 | [diff] [blame] | 249 | extern const size_t bc_history_combo_chars_len; |
Gavin Howard | 15afde9 | 2019-04-11 10:05:32 -0600 | [diff] [blame] | 250 | #if BC_DEBUG_CODE |
Gavin Howard | c86dcd7 | 2020-05-22 19:23:57 -0600 | [diff] [blame] | 251 | extern BcFile bc_history_debug_fp; |
| 252 | extern char *bc_history_debug_buf; |
| 253 | void bc_history_printKeyCodes(BcHistory* l); |
Gavin Howard | 15afde9 | 2019-04-11 10:05:32 -0600 | [diff] [blame] | 254 | #endif // BC_DEBUG_CODE |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 255 | |
Gavin Howard | 641fe97 | 2018-12-12 04:06:03 -0700 | [diff] [blame] | 256 | #endif // BC_ENABLE_HISTORY |
Gavin Howard | b0faf44 | 2018-12-12 03:42:55 -0700 | [diff] [blame] | 257 | |
Gavin Howard | 641fe97 | 2018-12-12 04:06:03 -0700 | [diff] [blame] | 258 | #endif // BC_HISTORY_H |