Lines Matching +full:com +full:- +full:seq

4  * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2024 Gavin D. Howard and contributors.
34 * linenoise.c -- guerrilla line editing library against the idea that a
38 * http://github.com/antirez/linenoise
41 * https://github.com/rain-1/linenoise-mob
43 * ------------------------------------------------------------------------
47 * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com>
48 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
73 * ------------------------------------------------------------------------
79 * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
80 * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
83 * - Filter bogus Ctrl+<char> combinations.
84 * - Win32 support
87 * - History search like Ctrl+r in readline?
189 bc_vec_string(&v, bc_history_editrc_len - 1, bc_history_editrc + 1); in bc_history_init()
197 h->hist = history_init(); in bc_history_init()
198 if (BC_ERR(h->hist == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR); in bc_history_init()
200 h->el = el_init(vm->name, stdin, stdout, stderr); in bc_history_init()
201 if (BC_ERR(h->el == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR); in bc_history_init()
204 history(h->hist, &bc_history_event, H_SETSIZE, 100); in bc_history_init()
205 history(h->hist, &bc_history_event, H_SETUNIQUE, 1); in bc_history_init()
206 el_set(h->el, EL_EDITOR, "emacs"); in bc_history_init()
207 el_set(h->el, EL_HIST, history, h->hist); in bc_history_init()
208 el_set(h->el, EL_PROMPT, bc_history_promptFunc); in bc_history_init()
211 el_source(h->el, v.v); in bc_history_init()
215 h->badTerm = false; in bc_history_init()
223 el_end(h->el); in bc_history_free()
224 history_end(h->hist); in bc_history_free()
263 len = -1; in bc_history_line()
280 line = el_gets(h->el, &len); in bc_history_line()
288 if (len == -1) in bc_history_line()
295 bc_file_printf(&vm->fout, "\n"); in bc_history_line()
306 history(h->hist, &bc_history_event, H_ENTER, line); in bc_history_line()
338 h->line = NULL; in bc_history_init()
339 h->badTerm = false; in bc_history_init()
348 if (h->line != NULL) free(h->line); in bc_history_free()
370 if (h->line != NULL) in bc_history_line()
372 free(h->line); in bc_history_line()
373 h->line = NULL; in bc_history_line()
377 h->line = readline(BC_PROMPT ? prompt : ""); in bc_history_line()
381 if (h->line != NULL && h->line[0]) in bc_history_line()
383 add_history(h->line); in bc_history_line()
385 len = strlen(h->line); in bc_history_line()
389 bc_vec_string(vec, len, h->line); in bc_history_line()
392 else if (h->line == NULL) in bc_history_line()
394 bc_file_printf(&vm->fout, "%s\n", "^D"); in bc_history_line()
502 for (pos -= 1; pos < end && (buf[pos] & 0xC0) == 0x80; --pos) in bc_history_prevCharLen()
506 return end - (pos >= end ? 0 : pos); in bc_history_prevCharLen()
510 * Converts UTF-8 to a Unicode code point.
523 // This is literally the UTF-8 decoding algorithm. Look that up if you in bc_history_codePoint()
586 size_t len = bc_history_codePoint(buf + pos, buf_len - pos, &cp); in bc_history_nextLen()
604 // Find the first non-combining character. in bc_history_nextLen()
607 len = bc_history_codePoint(buf + pos, buf_len - pos, &cp); in bc_history_nextLen()
609 if (!bc_history_comboChar(cp)) return pos - beg; in bc_history_nextLen()
614 return pos - beg; in bc_history_nextLen()
628 // Find the first non-combining character. in bc_history_prevLen()
634 pos -= len; in bc_history_prevLen()
637 // The original linenoise-mob had an extra parameter col_len, like in bc_history_prevLen()
640 if (!bc_history_comboChar(cp)) return end - pos; in bc_history_prevLen()
681 ret = (read != n || !good) ? -1 : 1; in bc_history_read()
716 // Once again, this is the UTF-8 decoding algorithm, but it has reads in bc_history_readCode()
758 n = -1; in bc_history_readCode()
842 if (h->rawMode) return; in bc_history_enableRaw()
846 if (BC_ERR(tcgetattr(STDIN_FILENO, &h->orig_termios) == -1)) in bc_history_enableRaw()
854 raw = h->orig_termios; in bc_history_enableRaw()
863 // Local modes - choing off, canonical off, no extended functions, in bc_history_enableRaw()
867 // Control chars - set return condition: min number of bytes and timer. in bc_history_enableRaw()
886 h->rawMode = true; in bc_history_enableRaw()
898 if (!h->rawMode) return; in bc_history_disableRaw()
903 if (BC_ERR(tcsetattr(STDIN_FILENO, TCSAFLUSH, &h->orig_termios) != -1)) in bc_history_disableRaw()
905 h->rawMode = false; in bc_history_disableRaw()
914 * and return it. On error -1 is returned, on success the position of the
929 bc_file_write(&vm->fout, bc_flush_none, "\x1b[6n", 4); in bc_history_cursorPos()
930 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_cursorPos()
933 for (i = 0; i < sizeof(buf) - 1; ++i) in bc_history_cursorPos()
973 ret = ioctl(vm->fout.fd, TIOCGWINSZ, &ws); in bc_history_columns()
975 if (BC_ERR(ret == -1 || !ws.ws_col)) in bc_history_columns()
985 bc_file_write(&vm->fout, bc_flush_none, "\x1b[999C", 6); in bc_history_columns()
986 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_columns()
993 bc_file_printf(&vm->fout, "\x1b[%zuD", cols - start); in bc_history_columns()
994 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_columns()
1011 return ((size_t) (csbi.srWindow.Right)) - csbi.srWindow.Left + 1; in bc_history_columns()
1029 // The original linenoise-mob checked for ANSI escapes here on the prompt. I in bc_history_promptColLen()
1047 char* buf = h->buf.v; in bc_history_refresh()
1048 size_t colpos, len = BC_HIST_BUF_LEN(h), pos = h->pos, extras_len = 0; in bc_history_refresh()
1052 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_refresh()
1055 while (h->pcol + bc_history_colPos(buf, len, pos) >= h->cols) in bc_history_refresh()
1060 len -= chlen; in bc_history_refresh()
1061 pos -= chlen; in bc_history_refresh()
1065 while (h->pcol + bc_history_colPos(buf, len, len) > h->cols) in bc_history_refresh()
1067 len -= bc_history_prevLen(buf, len); in bc_history_refresh()
1071 bc_file_write(&vm->fout, bc_flush_none, "\r", 1); in bc_history_refresh()
1075 if (h->extras.len > 1) in bc_history_refresh()
1077 extras_len = h->extras.len - 1; in bc_history_refresh()
1079 bc_vec_grow(&h->buf, extras_len); in bc_history_refresh()
1084 bc_file_write(&vm->fout, bc_flush_none, h->extras.v, extras_len); in bc_history_refresh()
1088 if (BC_PROMPT) bc_file_write(&vm->fout, bc_flush_none, h->prompt, h->plen); in bc_history_refresh()
1090 bc_file_write(&vm->fout, bc_flush_none, h->buf.v, len - extras_len); in bc_history_refresh()
1093 bc_file_write(&vm->fout, bc_flush_none, "\x1b[0K", 4); in bc_history_refresh()
1096 if (pos >= h->buf.len - extras_len) bc_vec_grow(&h->buf, pos + extras_len); in bc_history_refresh()
1101 bc_file_putchar(&vm->fout, bc_flush_none, '\r'); in bc_history_refresh()
1102 colpos = bc_history_colPos(h->buf.v, len - extras_len, pos) + h->pcol; in bc_history_refresh()
1105 if (colpos) bc_file_printf(&vm->fout, "\x1b[%zuC", colpos); in bc_history_refresh()
1107 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_refresh()
1121 bc_vec_grow(&h->buf, clen); in bc_history_edit_insert()
1124 if (h->pos == BC_HIST_BUF_LEN(h)) in bc_history_edit_insert()
1129 memcpy(bc_vec_item(&h->buf, h->pos), cbuf, clen); in bc_history_edit_insert()
1132 h->pos += clen; in bc_history_edit_insert()
1133 h->buf.len += clen - 1; in bc_history_edit_insert()
1134 bc_vec_pushByte(&h->buf, '\0'); in bc_history_edit_insert()
1137 len = BC_HIST_BUF_LEN(h) + h->extras.len - 1; in bc_history_edit_insert()
1138 colpos = bc_history_promptColLen(h->prompt, h->plen); in bc_history_edit_insert()
1139 colpos += bc_history_colPos(h->buf.v, len, len); in bc_history_edit_insert()
1142 if (colpos < h->cols) in bc_history_edit_insert()
1145 bc_file_write(&vm->fout, bc_flush_none, cbuf, clen); in bc_history_edit_insert()
1146 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_edit_insert()
1153 size_t amt = BC_HIST_BUF_LEN(h) - h->pos; in bc_history_edit_insert()
1156 memmove(h->buf.v + h->pos + clen, h->buf.v + h->pos, amt); in bc_history_edit_insert()
1157 memcpy(h->buf.v + h->pos, cbuf, clen); in bc_history_edit_insert()
1160 h->pos += clen; in bc_history_edit_insert()
1161 h->buf.len += clen; in bc_history_edit_insert()
1162 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0'; in bc_history_edit_insert()
1178 if (h->pos <= 0) return; in bc_history_edit_left()
1180 h->pos -= bc_history_prevLen(h->buf.v, h->pos); in bc_history_edit_left()
1195 if (h->pos == BC_HIST_BUF_LEN(h)) return; in bc_history_edit_right()
1197 h->pos += bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL); in bc_history_edit_right()
1214 if (!len || h->pos >= len) return; in bc_history_edit_wordEnd()
1217 while (h->pos < len && isspace(h->buf.v[h->pos])) in bc_history_edit_wordEnd()
1219 h->pos += 1; in bc_history_edit_wordEnd()
1221 while (h->pos < len && !isspace(h->buf.v[h->pos])) in bc_history_edit_wordEnd()
1223 h->pos += 1; in bc_history_edit_wordEnd()
1244 while (h->pos > 0 && isspace(h->buf.v[h->pos - 1])) in bc_history_edit_wordStart()
1246 h->pos -= 1; in bc_history_edit_wordStart()
1248 while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1])) in bc_history_edit_wordStart()
1250 h->pos -= 1; in bc_history_edit_wordStart()
1266 if (!h->pos) return; in bc_history_edit_home()
1268 h->pos = 0; in bc_history_edit_home()
1283 if (h->pos == BC_HIST_BUF_LEN(h)) return; in bc_history_edit_end()
1285 h->pos = BC_HIST_BUF_LEN(h); in bc_history_edit_end()
1305 if (h->history.len <= 1) return; in bc_history_edit_next()
1308 if (h->buf.v[0]) dup = bc_vm_strdup(h->buf.v); in bc_history_edit_next()
1312 bc_vec_replaceAt(&h->history, h->history.len - 1 - h->idx, &dup); in bc_history_edit_next()
1315 h->idx += (dir == BC_HIST_PREV ? 1 : SIZE_MAX); in bc_history_edit_next()
1318 if (h->idx == SIZE_MAX) in bc_history_edit_next()
1320 h->idx = 0; in bc_history_edit_next()
1323 else if (h->idx >= h->history.len) in bc_history_edit_next()
1325 h->idx = h->history.len - 1; in bc_history_edit_next()
1330 str = *((char**) bc_vec_item(&h->history, h->history.len - 1 - h->idx)); in bc_history_edit_next()
1331 bc_vec_string(&h->buf, strlen(str), str); in bc_history_edit_next()
1333 assert(h->buf.len > 0); in bc_history_edit_next()
1336 h->pos = BC_HIST_BUF_LEN(h); in bc_history_edit_next()
1354 if (!len || h->pos >= len) return; in bc_history_edit_delete()
1357 chlen = bc_history_nextLen(h->buf.v, len, h->pos, NULL); in bc_history_edit_delete()
1360 memmove(h->buf.v + h->pos, h->buf.v + h->pos + chlen, len - h->pos - chlen); in bc_history_edit_delete()
1363 h->buf.len -= chlen; in bc_history_edit_delete()
1364 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0'; in bc_history_edit_delete()
1382 if (!h->pos || !len) return; in bc_history_edit_backspace()
1385 chlen = bc_history_prevLen(h->buf.v, h->pos); in bc_history_edit_backspace()
1388 memmove(h->buf.v + h->pos - chlen, h->buf.v + h->pos, len - h->pos); in bc_history_edit_backspace()
1391 h->pos -= chlen; in bc_history_edit_backspace()
1392 h->buf.len -= chlen; in bc_history_edit_backspace()
1393 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0'; in bc_history_edit_backspace()
1406 size_t diff, old_pos = h->pos; in bc_history_edit_deletePrevWord()
1414 while (h->pos > 0 && isspace(h->buf.v[h->pos - 1])) in bc_history_edit_deletePrevWord()
1416 h->pos -= 1; in bc_history_edit_deletePrevWord()
1418 while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1])) in bc_history_edit_deletePrevWord()
1420 h->pos -= 1; in bc_history_edit_deletePrevWord()
1424 diff = old_pos - h->pos; in bc_history_edit_deletePrevWord()
1427 memmove(h->buf.v + h->pos, h->buf.v + old_pos, in bc_history_edit_deletePrevWord()
1428 BC_HIST_BUF_LEN(h) - old_pos + 1); in bc_history_edit_deletePrevWord()
1431 h->buf.len -= diff; in bc_history_edit_deletePrevWord()
1443 size_t next_end = h->pos, len = BC_HIST_BUF_LEN(h); in bc_history_edit_deleteNextWord()
1451 while (next_end < len && isspace(h->buf.v[next_end])) in bc_history_edit_deleteNextWord()
1455 while (next_end < len && !isspace(h->buf.v[next_end])) in bc_history_edit_deleteNextWord()
1461 memmove(h->buf.v + h->pos, h->buf.v + next_end, len - next_end); in bc_history_edit_deleteNextWord()
1464 h->buf.len -= next_end - h->pos; in bc_history_edit_deleteNextWord()
1482 if (!h->pos) return; in bc_history_swap()
1485 pcl = bc_history_prevLen(h->buf.v, h->pos); in bc_history_swap()
1486 ncl = bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL); in bc_history_swap()
1491 if (pcl && h->pos != BC_HIST_BUF_LEN(h) && pcl < 5 && ncl < 5) in bc_history_swap()
1494 memcpy(auxb, h->buf.v + h->pos - pcl, pcl); in bc_history_swap()
1495 memcpy(h->buf.v + h->pos - pcl, h->buf.v + h->pos, ncl); in bc_history_swap()
1496 memcpy(h->buf.v + h->pos - pcl + ncl, auxb, pcl); in bc_history_swap()
1499 h->pos += ((~pcl) + 1) + ncl; in bc_history_swap()
1526 char c, seq[3]; in bc_history_escape() local
1530 // Read a character into seq. in bc_history_escape()
1531 if (BC_ERR(BC_HIST_READ(seq, 1))) return; in bc_history_escape()
1533 c = seq[0]; in bc_history_escape()
1544 // Read a character into seq. in bc_history_escape()
1545 if (BC_ERR(BC_HIST_READ(seq + 1, 1))) in bc_history_escape()
1553 c = seq[1]; in bc_history_escape()
1558 if (BC_ERR(BC_HIST_READ(seq + 2, 1))) in bc_history_escape()
1563 if (seq[2] == '~') in bc_history_escape()
1591 else if (seq[2] == ';') in bc_history_escape()
1593 // Read two characters into seq. in bc_history_escape()
1594 if (BC_ERR(BC_HIST_READ(seq, 2))) in bc_history_escape()
1599 if (seq[0] != '5') return; in bc_history_escape()
1600 else if (seq[1] == 'C') bc_history_edit_wordEnd(h); in bc_history_escape()
1601 else if (seq[1] == 'D') bc_history_edit_wordStart(h); in bc_history_escape()
1663 switch (seq[1]) in bc_history_escape()
1716 if (h->history.len) in bc_history_add()
1719 char* s = *((char**) bc_vec_item_rev(&h->history, 0)); in bc_history_add()
1729 bc_vec_push(&h->history, &line); in bc_history_add()
1745 if (h->history.len) in bc_history_add_empty()
1748 char* s = *((char**) bc_vec_item_rev(&h->history, 0)); in bc_history_add_empty()
1754 bc_vec_push(&h->history, &line); in bc_history_add_empty()
1766 h->oldcolpos = h->pos = h->idx = 0; in bc_history_reset()
1767 h->cols = bc_history_columns(); in bc_history_reset()
1774 bc_vec_empty(&h->buf); in bc_history_reset()
1791 str[1] = (char) (c + 'A' - BC_ACTION_CTRL_A); in bc_history_printCtrl()
1794 bc_vec_concat(&h->buf, str); in bc_history_printCtrl()
1796 h->pos = BC_HIST_BUF_LEN(h); in bc_history_printCtrl()
1800 bc_vec_npop(&h->buf, sizeof(str)); in bc_history_printCtrl()
1801 bc_vec_pushByte(&h->buf, '\0'); in bc_history_printCtrl()
1802 h->pos = 0; in bc_history_printCtrl()
1808 bc_file_write(&vm->fout, bc_flush_none, newline, sizeof(newline) - 1); in bc_history_printCtrl()
1831 // bc_file_write(&vm->fout, bc_flush_none, h->extras.v, h->extras.len - 1); in bc_history_edit()
1836 h->prompt = prompt; in bc_history_edit()
1837 h->plen = strlen(prompt); in bc_history_edit()
1838 h->pcol = bc_history_promptColLen(prompt, h->plen); in bc_history_edit()
1840 bc_file_write(&vm->fout, bc_flush_none, prompt, h->plen); in bc_history_edit()
1841 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_edit()
1866 bc_vec_pop(&h->history); in bc_history_edit()
1886 vm->status = BC_STATUS_QUIT; in bc_history_edit()
1892 bc_file_write(&vm->fout, bc_flush_none, vm->sigmsg, vm->siglen); in bc_history_edit()
1893 bc_file_write(&vm->fout, bc_flush_none, bc_program_ready_msg, in bc_history_edit()
1908 // Act as end-of-file or delete-forward-char. in bc_history_edit()
1965 bc_vec_string(&h->buf, 0, ""); in bc_history_edit()
1966 h->pos = 0; in bc_history_edit()
1976 bc_vec_npop(&h->buf, h->buf.len - h->pos); in bc_history_edit()
1977 bc_vec_pushByte(&h->buf, '\0'); in bc_history_edit()
1999 bc_file_write(&vm->fout, bc_flush_none, "\x1b[H\x1b[2J", 7); in bc_history_edit()
2027 vm->status = BC_STATUS_QUIT; in bc_history_edit()
2045 * Returns true if stdin has more data. This is for multi-line pasting, and it
2054 return pselect(1, &h->rdset, NULL, NULL, &h->ts, &h->sigmask) > 0 || in bc_history_stdinHasData()
2067 assert(vm->fout.len == 0); in bc_history_line()
2077 bc_file_write(&vm->fout, bc_flush_none, "\n", 1); in bc_history_line()
2078 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_line()
2083 if (h->buf.v[0]) in bc_history_line()
2086 line = bc_vm_strdup(h->buf.v); in bc_history_line()
2097 bc_vec_concat(vec, h->buf.v); in bc_history_line()
2127 h->rawMode = false; in bc_history_init()
2128 h->badTerm = bc_history_isBadTerm(); in bc_history_init()
2131 if (h->badTerm) return; in bc_history_init()
2135 h->orig_in = 0; in bc_history_init()
2136 h->orig_out = 0; in bc_history_init()
2146 if (!GetConsoleMode(in, &h->orig_in) || !GetConsoleMode(out, &h->orig_out)) in bc_history_init()
2149 h->badTerm = true; in bc_history_init()
2155 DWORD reqOut = h->orig_out | ENABLE_VIRTUAL_TERMINAL_PROCESSING; in bc_history_init()
2156 DWORD reqIn = h->orig_in | ENABLE_VIRTUAL_TERMINAL_INPUT; in bc_history_init()
2170 h->badTerm = true; in bc_history_init()
2176 bc_vec_init(&h->buf, sizeof(char), BC_DTOR_NONE); in bc_history_init()
2177 bc_vec_init(&h->history, sizeof(char*), BC_DTOR_HISTORY_STRING); in bc_history_init()
2178 bc_vec_init(&h->extras, sizeof(char), BC_DTOR_NONE); in bc_history_init()
2181 FD_ZERO(&h->rdset); in bc_history_init()
2182 FD_SET(STDIN_FILENO, &h->rdset); in bc_history_init()
2183 h->ts.tv_sec = 0; in bc_history_init()
2184 h->ts.tv_nsec = 0; in bc_history_init()
2186 sigemptyset(&h->sigmask); in bc_history_init()
2187 sigaddset(&h->sigmask, SIGINT); in bc_history_init()
2198 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), h->orig_in); in bc_history_free()
2199 SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), h->orig_out); in bc_history_free()
2202 bc_vec_free(&h->buf); in bc_history_free()
2203 bc_vec_free(&h->history); in bc_history_free()
2204 bc_vec_free(&h->extras); in bc_history_free()
2236 memmove(quit, quit + 1, sizeof(quit) - 1); in bc_history_printKeyCodes()
2239 quit[sizeof(quit) - 1] = c; in bc_history_printKeyCodes()
2247 bc_file_flush(&vm->fout, bc_flush_none); in bc_history_printKeyCodes()