]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/history.h
Import new 2-clause BSD licenced implementation of the bc and dc commands
[FreeBSD/FreeBSD.git] / include / history.h
1 /*
2  * *****************************************************************************
3  *
4  * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
5  *
6  * All rights reserved.
7  *
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.
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  *
40  * You can find the fork that this code is based on at:
41  *   https://github.com/rain-1/linenoise-mob
42  *
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  *
50  * All rights reserved.
51  *
52  * Redistribution and use in source and binary forms, with or without
53  * modification, are permitted provided that the following conditions are
54  * met:
55  *
56  *  *  Redistributions of source code must retain the above copyright
57  *     notice, this list of conditions and the following disclaimer.
58  *
59  *  *  Redistributions in binary form must reproduce the above copyright
60  *     notice, this list of conditions and the following disclaimer in the
61  *     documentation and/or other materials provided with the distribution.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74  *
75  * *****************************************************************************
76  *
77  * Definitions for line history.
78  *
79  */
80
81 #ifndef BC_HISTORY_H
82 #define BC_HISTORY_H
83
84 #ifndef BC_ENABLE_HISTORY
85 #define BC_ENABLE_HISTORY (1)
86 #endif // BC_ENABLE_HISTORY
87
88 #if BC_ENABLE_HISTORY
89
90 #ifdef _WIN32
91 #error History is not supported on Windows.
92 #endif // _WIN32
93
94 #include <stdbool.h>
95 #include <stddef.h>
96
97 #include <signal.h>
98
99 #include <termios.h>
100 #include <time.h>
101 #include <unistd.h>
102 #include <sys/select.h>
103
104 #include <status.h>
105 #include <vector.h>
106 #include <read.h>
107
108 #if BC_DEBUG_CODE
109 #include <file.h>
110 #endif // BC_DEBUG_CODE
111
112 #define BC_HIST_DEF_COLS (80)
113 #define BC_HIST_MAX_LEN (128)
114 #define BC_HIST_MAX_LINE (4095)
115 #define BC_HIST_SEQ_SIZE (64)
116
117 #define BC_HIST_BUF_LEN(h) ((h)->buf.len - 1)
118 #define BC_HIST_READ(s, n) (bc_history_read((s), (n)) == -1)
119
120 #define BC_HIST_NEXT (false)
121 #define BC_HIST_PREV (true)
122
123 #if BC_DEBUG_CODE
124
125 #define BC_HISTORY_DEBUG_BUF_SIZE (1024)
126
127 #define lndebug(...)                                                        \
128         do {                                                                    \
129                 if (bc_history_debug_fp.fd == 0) {                                  \
130                         bc_history_debug_buf = bc_vm_malloc(BC_HISTORY_DEBUG_BUF_SIZE); \
131                         bc_file_init(&bc_history_debug_fp,                              \
132                                      open("/tmp/lndebug.txt", O_APPEND),                \
133                                  BC_HISTORY_DEBUG_BUF_SIZE);                        \
134                         bc_file_printf(&bc_history_debug_fp,                            \
135                                "[%zu %zu %zu] p: %d, rows: %d, "                        \
136                                "rpos: %d, max: %zu, oldmax: %d\n",                      \
137                                l->len, l->pos, l->oldcolpos, plen, rows, rpos,          \
138                                l->maxrows, old_rows);                                   \
139                 }                                                                   \
140                 bc_file_printf(&bc_history_debug_fp, ", " __VA_ARGS__);             \
141                 bc_file_flush(&bc_history_debug_fp);                                \
142         } while (0)
143 #else // BC_DEBUG_CODE
144 #define lndebug(fmt, ...)
145 #endif // BC_DEBUG_CODE
146
147 #if !BC_ENABLE_PROMPT
148 #define bc_history_line(h, vec, prompt) bc_history_line(h, vec)
149 #define bc_history_raw(h, prompt) bc_history_raw(h)
150 #define bc_history_edit(h, prompt) bc_history_edit(h)
151 #endif // BC_ENABLE_PROMPT
152
153 typedef enum BcHistoryAction {
154
155         BC_ACTION_NULL = 0,
156         BC_ACTION_CTRL_A = 1,
157         BC_ACTION_CTRL_B = 2,
158         BC_ACTION_CTRL_C = 3,
159         BC_ACTION_CTRL_D = 4,
160         BC_ACTION_CTRL_E = 5,
161         BC_ACTION_CTRL_F = 6,
162         BC_ACTION_CTRL_H = 8,
163         BC_ACTION_TAB = 9,
164         BC_ACTION_LINE_FEED = 10,
165         BC_ACTION_CTRL_K = 11,
166         BC_ACTION_CTRL_L = 12,
167         BC_ACTION_ENTER = 13,
168         BC_ACTION_CTRL_N = 14,
169         BC_ACTION_CTRL_P = 16,
170         BC_ACTION_CTRL_T = 20,
171         BC_ACTION_CTRL_U = 21,
172         BC_ACTION_CTRL_W = 23,
173         BC_ACTION_CTRL_Z = 26,
174         BC_ACTION_ESC = 27,
175         BC_ACTION_BACKSPACE =  127
176
177 } BcHistoryAction;
178
179 /**
180  * This represents the state during line editing. We pass this state
181  * to functions implementing specific editing functionalities.
182  */
183 typedef struct BcHistory {
184
185         /// Edited line buffer.
186         BcVec buf;
187
188         /// The history.
189         BcVec history;
190
191 #if BC_ENABLE_PROMPT
192         /// Prompt to display.
193         const char *prompt;
194
195         /// Prompt length.
196         size_t plen;
197 #endif // BC_ENABLE_PROMPT
198
199         /// Prompt column length.
200         size_t pcol;
201
202         /// Current cursor position.
203         size_t pos;
204
205         /// Previous refresh cursor column position.
206         size_t oldcolpos;
207
208         /// Number of columns in terminal.
209         size_t cols;
210
211         /// The history index we are currently editing.
212         size_t idx;
213
214         /// The original terminal state.
215         struct termios orig_termios;
216
217         /// These next three are here because pahole found a 4 byte hole here.
218
219         /// This is to signal that there is more, so we don't process yet.
220         bool stdin_has_data;
221
222         /// Whether we are in rawmode.
223         bool rawMode;
224
225         /// Whether the terminal is bad.
226         bool badTerm;
227
228         /// This is to check if stdin has more data.
229         fd_set rdset;
230
231         /// This is to check if stdin has more data.
232         struct timespec ts;
233
234         /// This is to check if stdin has more data.
235         sigset_t sigmask;
236
237 } BcHistory;
238
239 BcStatus bc_history_line(BcHistory *h, BcVec *vec, const char *prompt);
240
241 void bc_history_init(BcHistory *h);
242 void bc_history_free(BcHistory *h);
243
244 extern const char *bc_history_bad_terms[];
245 extern const char bc_history_tab[];
246 extern const size_t bc_history_tab_len;
247 extern const char bc_history_ctrlc[];
248 extern const uint32_t bc_history_wchars[][2];
249 extern const size_t bc_history_wchars_len;
250 extern const uint32_t bc_history_combo_chars[];
251 extern const size_t bc_history_combo_chars_len;
252 #if BC_DEBUG_CODE
253 extern BcFile bc_history_debug_fp;
254 extern char *bc_history_debug_buf;
255 void bc_history_printKeyCodes(BcHistory* l);
256 #endif // BC_DEBUG_CODE
257
258 #endif // BC_ENABLE_HISTORY
259
260 #endif // BC_HISTORY_H