Lines Matching defs:h
149 #include <string.h>
150 #include <errno.h>
151 #include <setjmp.h>
153 #include <history.h>
154 #include <vm.h>
173 bc_history_init(BcHistory* h)
197 h->hist = history_init();
198 if (BC_ERR(h->hist == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
200 h->el = el_init(vm->name, stdin, stdout, stderr);
201 if (BC_ERR(h->el == NULL)) bc_vm_fatalError(BC_ERR_FATAL_ALLOC_ERR);
204 history(h->hist, &bc_history_event, H_SETSIZE, 100);
205 history(h->hist, &bc_history_event, H_SETUNIQUE, 1);
206 el_set(h->el, EL_EDITOR, "emacs");
207 el_set(h->el, EL_HIST, history, h->hist);
208 el_set(h->el, EL_PROMPT, bc_history_promptFunc);
211 el_source(h->el, v.v);
215 h->badTerm = false;
220 bc_history_free(BcHistory* h)
223 el_end(h->el);
224 history_end(h->hist);
228 bc_history_line(BcHistory* h, BcVec* vec, const char* prompt)
280 line = el_gets(h->el, &len);
306 history(h->hist, &bc_history_event, H_ENTER, line);
325 #include <assert.h>
326 #include <setjmp.h>
327 #include <string.h>
329 #include <history.h>
330 #include <vm.h>
336 bc_history_init(BcHistory* h)
338 h->line = NULL;
339 h->badTerm = false;
346 bc_history_free(BcHistory* h)
348 if (h->line != NULL) free(h->line);
352 bc_history_line(BcHistory* h, BcVec* vec, const char* prompt)
370 if (h->line != NULL)
372 free(h->line);
373 h->line = NULL;
377 h->line = readline(BC_PROMPT ? prompt : "");
381 if (h->line != NULL && h->line[0])
383 add_history(h->line);
385 len = strlen(h->line);
389 bc_vec_string(vec, len, h->line);
392 else if (h->line == NULL)
410 #include <assert.h>
411 #include <stdlib.h>
412 #include <errno.h>
413 #include <string.h>
414 #include <ctype.h>
416 #include <signal.h>
417 #include <sys/stat.h>
418 #include <sys/types.h>
421 #include <strings.h>
422 #include <termios.h>
423 #include <unistd.h>
424 #include <sys/ioctl.h>
425 #include <sys/select.h>
428 #include <status.h>
429 #include <vector.h>
430 #include <history.h>
431 #include <read.h>
432 #include <file.h>
433 #include <vm.h>
828 * @param h The history data.
831 bc_history_enableRaw(BcHistory* h)
842 if (h->rawMode) return;
846 if (BC_ERR(tcgetattr(STDIN_FILENO, &h->orig_termios) == -1))
854 raw = h->orig_termios;
886 h->rawMode = true;
891 * @param h The history data.
894 bc_history_disableRaw(BcHistory* h)
898 if (!h->rawMode) return;
903 if (BC_ERR(tcsetattr(STDIN_FILENO, TCSAFLUSH, &h->orig_termios) != -1))
905 h->rawMode = false;
1042 * @param h The history data.
1045 bc_history_refresh(BcHistory* h)
1047 char* buf = h->buf.v;
1048 size_t colpos, len = BC_HIST_BUF_LEN(h), pos = h->pos, extras_len = 0;
1055 while (h->pcol + bc_history_colPos(buf, len, pos) >= h->cols)
1065 while (h->pcol + bc_history_colPos(buf, len, len) > h->cols)
1075 if (h->extras.len > 1)
1077 extras_len = h->extras.len - 1;
1079 bc_vec_grow(&h->buf, extras_len);
1084 bc_file_write(&vm->fout, bc_flush_none, h->extras.v, extras_len);
1088 if (BC_PROMPT) bc_file_write(&vm->fout, bc_flush_none, h->prompt, h->plen);
1090 bc_file_write(&vm->fout, bc_flush_none, h->buf.v, len - extras_len);
1096 if (pos >= h->buf.len - extras_len) bc_vec_grow(&h->buf, pos + extras_len);
1102 colpos = bc_history_colPos(h->buf.v, len - extras_len, pos) + h->pcol;
1112 * @param h The history data.
1117 bc_history_edit_insert(BcHistory* h, const char* cbuf, size_t clen)
1121 bc_vec_grow(&h->buf, clen);
1124 if (h->pos == BC_HIST_BUF_LEN(h))
1129 memcpy(bc_vec_item(&h->buf, h->pos), cbuf, clen);
1132 h->pos += clen;
1133 h->buf.len += clen - 1;
1134 bc_vec_pushByte(&h->buf, '\0');
1137 len = BC_HIST_BUF_LEN(h) + h->extras.len - 1;
1138 colpos = bc_history_promptColLen(h->prompt, h->plen);
1139 colpos += bc_history_colPos(h->buf.v, len, len);
1142 if (colpos < h->cols)
1148 else bc_history_refresh(h);
1153 size_t amt = BC_HIST_BUF_LEN(h) - h->pos;
1156 memmove(h->buf.v + h->pos + clen, h->buf.v + h->pos, amt);
1157 memcpy(h->buf.v + h->pos, cbuf, clen);
1160 h->pos += clen;
1161 h->buf.len += clen;
1162 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';
1164 bc_history_refresh(h);
1170 * @param h The history data.
1173 bc_history_edit_left(BcHistory* h)
1178 if (h->pos <= 0) return;
1180 h->pos -= bc_history_prevLen(h->buf.v, h->pos);
1182 bc_history_refresh(h);
1187 * @param h The history data.
1190 bc_history_edit_right(BcHistory* h)
1195 if (h->pos == BC_HIST_BUF_LEN(h)) return;
1197 h->pos += bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL);
1199 bc_history_refresh(h);
1204 * @param h The history data.
1207 bc_history_edit_wordEnd(BcHistory* h)
1209 size_t len = BC_HIST_BUF_LEN(h);
1214 if (!len || h->pos >= len) return;
1217 while (h->pos < len && isspace(h->buf.v[h->pos]))
1219 h->pos += 1;
1221 while (h->pos < len && !isspace(h->buf.v[h->pos]))
1223 h->pos += 1;
1226 bc_history_refresh(h);
1231 * @param h The history data.
1234 bc_history_edit_wordStart(BcHistory* h)
1236 size_t len = BC_HIST_BUF_LEN(h);
1244 while (h->pos > 0 && isspace(h->buf.v[h->pos - 1]))
1246 h->pos -= 1;
1248 while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1]))
1250 h->pos -= 1;
1253 bc_history_refresh(h);
1258 * @param h The history data.
1261 bc_history_edit_home(BcHistory* h)
1266 if (!h->pos) return;
1268 h->pos = 0;
1270 bc_history_refresh(h);
1275 * @param h The history data.
1278 bc_history_edit_end(BcHistory* h)
1283 if (h->pos == BC_HIST_BUF_LEN(h)) return;
1285 h->pos = BC_HIST_BUF_LEN(h);
1287 bc_history_refresh(h);
1293 * @param h The history data.
1297 bc_history_edit_next(BcHistory* h, bool dir)
1305 if (h->history.len <= 1) return;
1308 if (h->buf.v[0]) dup = bc_vm_strdup(h->buf.v);
1312 bc_vec_replaceAt(&h->history, h->history.len - 1 - h->idx, &dup);
1315 h->idx += (dir == BC_HIST_PREV ? 1 : SIZE_MAX);
1318 if (h->idx == SIZE_MAX)
1320 h->idx = 0;
1323 else if (h->idx >= h->history.len)
1325 h->idx = h->history.len - 1;
1330 str = *((char**) bc_vec_item(&h->history, h->history.len - 1 - h->idx));
1331 bc_vec_string(&h->buf, strlen(str), str);
1333 assert(h->buf.len > 0);
1336 h->pos = BC_HIST_BUF_LEN(h);
1338 bc_history_refresh(h);
1344 * @param h The history data.
1347 bc_history_edit_delete(BcHistory* h)
1349 size_t chlen, len = BC_HIST_BUF_LEN(h);
1354 if (!len || h->pos >= len) return;
1357 chlen = bc_history_nextLen(h->buf.v, len, h->pos, NULL);
1360 memmove(h->buf.v + h->pos, h->buf.v + h->pos + chlen, len - h->pos - chlen);
1363 h->buf.len -= chlen;
1364 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';
1366 bc_history_refresh(h);
1372 * @param h The history data.
1375 bc_history_edit_backspace(BcHistory* h)
1377 size_t chlen, len = BC_HIST_BUF_LEN(h);
1382 if (!h->pos || !len) return;
1385 chlen = bc_history_prevLen(h->buf.v, h->pos);
1388 memmove(h->buf.v + h->pos - chlen, h->buf.v + h->pos, len - h->pos);
1391 h->pos -= chlen;
1392 h->buf.len -= chlen;
1393 h->buf.v[BC_HIST_BUF_LEN(h)] = '\0';
1395 bc_history_refresh(h);
1401 * @param h The history data.
1404 bc_history_edit_deletePrevWord(BcHistory* h)
1406 size_t diff, old_pos = h->pos;
1414 while (h->pos > 0 && isspace(h->buf.v[h->pos - 1]))
1416 h->pos -= 1;
1418 while (h->pos > 0 && !isspace(h->buf.v[h->pos - 1]))
1420 h->pos -= 1;
1424 diff = old_pos - h->pos;
1427 memmove(h->buf.v + h->pos, h->buf.v + old_pos,
1428 BC_HIST_BUF_LEN(h) - old_pos + 1);
1431 h->buf.len -= diff;
1433 bc_history_refresh(h);
1438 * @param h The history data.
1441 bc_history_edit_deleteNextWord(BcHistory* h)
1443 size_t next_end = h->pos, len = BC_HIST_BUF_LEN(h);
1451 while (next_end < len && isspace(h->buf.v[next_end]))
1455 while (next_end < len && !isspace(h->buf.v[next_end]))
1461 memmove(h->buf.v + h->pos, h->buf.v + next_end, len - next_end);
1464 h->buf.len -= next_end - h->pos;
1466 bc_history_refresh(h);
1471 * @param h The history data.
1474 bc_history_swap(BcHistory* h)
1482 if (!h->pos) return;
1485 pcl = bc_history_prevLen(h->buf.v, h->pos);
1486 ncl = bc_history_nextLen(h->buf.v, BC_HIST_BUF_LEN(h), h->pos, NULL);
1491 if (pcl && h->pos != BC_HIST_BUF_LEN(h) && pcl < 5 && ncl < 5)
1494 memcpy(auxb, h->buf.v + h->pos - pcl, pcl);
1495 memcpy(h->buf.v + h->pos - pcl, h->buf.v + h->pos, ncl);
1496 memcpy(h->buf.v + h->pos - pcl + ncl, auxb, pcl);
1499 h->pos += ((~pcl) + 1) + ncl;
1501 bc_history_refresh(h);
1507 * @param h The history data.
1511 bc_history_raise(BcHistory* h, int sig)
1514 bc_history_disableRaw(h);
1521 * @param h The history data.
1524 bc_history_escape(BcHistory* h)
1538 if (c == 'f') bc_history_edit_wordEnd(h);
1539 else if (c == 'b') bc_history_edit_wordStart(h);
1540 else if (c == 'd') bc_history_edit_deleteNextWord(h);
1569 bc_history_edit_home(h);
1575 bc_history_edit_delete(h);
1581 bc_history_edit_end(h);
1600 else if (seq[1] == 'C') bc_history_edit_wordEnd(h);
1601 else if (seq[1] == 'D') bc_history_edit_wordStart(h);
1611 bc_history_edit_next(h, BC_HIST_PREV);
1618 bc_history_edit_next(h, BC_HIST_NEXT);
1625 bc_history_edit_right(h);
1632 bc_history_edit_left(h);
1640 bc_history_edit_home(h);
1648 bc_history_edit_end(h);
1654 bc_history_edit_deleteNextWord(h);
1667 bc_history_edit_next(h, BC_HIST_PREV);
1673 bc_history_edit_next(h, BC_HIST_NEXT);
1679 bc_history_edit_right(h);
1685 bc_history_edit_left(h);
1691 bc_history_edit_end(h);
1697 bc_history_edit_home(h);
1707 * @param h The history data.
1711 bc_history_add(BcHistory* h, char* line)
1716 if (h->history.len)
1719 char* s = *((char**) bc_vec_item_rev(&h->history, 0));
1729 bc_vec_push(&h->history, &line);
1735 * @param h The history data.
1738 bc_history_add_empty(BcHistory* h)
1745 if (h->history.len)
1748 char* s = *((char**) bc_vec_item_rev(&h->history, 0));
1754 bc_vec_push(&h->history, &line);
1759 * @param h The history data.
1762 bc_history_reset(BcHistory* h)
1766 h->oldcolpos = h->pos = h->idx = 0;
1767 h->cols = bc_history_columns();
1771 bc_history_add_empty(h);
1774 bc_vec_empty(&h->buf);
1779 * @param h The history data.
1783 bc_history_printCtrl(BcHistory* h, unsigned int c)
1794 bc_vec_concat(&h->buf, str);
1796 h->pos = BC_HIST_BUF_LEN(h);
1797 bc_history_refresh(h);
1800 bc_vec_npop(&h->buf, sizeof(str));
1801 bc_vec_pushByte(&h->buf, '\0');
1802 h->pos = 0;
1809 bc_history_refresh(h);
1817 * @param h The history data.
1822 bc_history_edit(BcHistory* h, const char* prompt)
1826 bc_history_reset(h);
1831 // bc_file_write(&vm->fout, bc_flush_none, h->extras.v, h->extras.len - 1);
1836 h->prompt = prompt;
1837 h->plen = strlen(prompt);
1838 h->pcol = bc_history_promptColLen(prompt, h->plen);
1840 bc_file_write(&vm->fout, bc_flush_none, prompt, h->plen);
1866 bc_vec_pop(&h->history);
1875 bc_history_edit_insert(h, cbuf, bc_history_tab_len);
1881 bc_history_printCtrl(h, c);
1895 bc_history_reset(h);
1896 bc_history_refresh(h);
1904 bc_history_edit_backspace(h);
1913 if (BC_HIST_BUF_LEN(h) == 0)
1915 bc_history_printCtrl(h, c);
1920 bc_history_edit_delete(h);
1928 bc_history_swap(h);
1934 bc_history_edit_left(h);
1940 bc_history_edit_right(h);
1946 bc_history_edit_next(h, BC_HIST_PREV);
1952 bc_history_edit_next(h, BC_HIST_NEXT);
1958 bc_history_escape(h);
1965 bc_vec_string(&h->buf, 0, "");
1966 h->pos = 0;
1968 bc_history_refresh(h);
1976 bc_vec_npop(&h->buf, h->buf.len - h->pos);
1977 bc_vec_pushByte(&h->buf, '\0');
1978 bc_history_refresh(h);
1985 bc_history_edit_home(h);
1992 bc_history_edit_end(h);
2000 bc_history_refresh(h);
2007 bc_history_edit_deletePrevWord(h);
2018 bc_history_printCtrl(h, c);
2020 if (c == BC_ACTION_CTRL_Z) bc_history_raise(h, SIGTSTP);
2021 if (c == BC_ACTION_CTRL_S) bc_history_raise(h, SIGSTOP);
2024 bc_history_raise(h, SIGQUIT);
2033 else bc_history_edit_insert(h, cbuf, nread);
2047 * @param h The history data.
2050 bc_history_stdinHasData(BcHistory* h)
2054 return pselect(1, &h->rdset, NULL, NULL, &h->ts, &h->sigmask) > 0 ||
2062 bc_history_line(BcHistory* h, BcVec* vec, const char* prompt)
2069 bc_history_enableRaw(h);
2074 s = bc_history_edit(h, prompt);
2083 if (h->buf.v[0])
2086 line = bc_vm_strdup(h->buf.v);
2089 bc_history_add(h, line);
2092 else bc_history_add_empty(h);
2097 bc_vec_concat(vec, h->buf.v);
2100 while (!s && bc_history_stdinHasData(h));
2104 bc_history_disableRaw(h);
2118 bc_history_init(BcHistory* h)
2127 h->rawMode = false;
2128 h->badTerm = bc_history_isBadTerm();
2131 if (h->badTerm) return;
2135 h->orig_in = 0;
2136 h->orig_out = 0;
2146 if (!GetConsoleMode(in, &h->orig_in) || !GetConsoleMode(out, &h->orig_out))
2149 h->badTerm = true;
2155 DWORD reqOut = h->orig_out | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
2156 DWORD reqIn = h->orig_in | ENABLE_VIRTUAL_TERMINAL_INPUT;
2170 h->badTerm = true;
2176 bc_vec_init(&h->buf, sizeof(char), BC_DTOR_NONE);
2177 bc_vec_init(&h->history, sizeof(char*), BC_DTOR_HISTORY_STRING);
2178 bc_vec_init(&h->extras, sizeof(char), BC_DTOR_NONE);
2181 FD_ZERO(&h->rdset);
2182 FD_SET(STDIN_FILENO, &h->rdset);
2183 h->ts.tv_sec = 0;
2184 h->ts.tv_nsec = 0;
2186 sigemptyset(&h->sigmask);
2187 sigaddset(&h->sigmask, SIGINT);
2192 bc_history_free(BcHistory* h)
2196 bc_history_disableRaw(h);
2198 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), h->orig_in);
2199 SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), h->orig_out);
2202 bc_vec_free(&h->buf);
2203 bc_vec_free(&h->history);
2204 bc_vec_free(&h->extras);
2213 * @param h The history data.
2216 bc_history_printKeyCodes(BcHistory* h)
2224 bc_history_enableRaw(h);
2250 bc_history_disableRaw(h);