tinyrl.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * tinyrl.c
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include "private.h"
  14. #define LINE_CHUNK 80
  15. tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
  16. const char *hist_fname, size_t hist_stifle)
  17. {
  18. tinyrl_t *tinyrl = NULL;
  19. int i = 0;
  20. tinyrl = faux_zmalloc(sizeof(tinyrl_t));
  21. if (!tinyrl)
  22. return NULL;
  23. // Line
  24. faux_bzero(&tinyrl->line, sizeof(tinyrl->line));
  25. tinyrl_line_extend(tinyrl, LINE_CHUNK);
  26. // Last line
  27. tinyrl_reset_line_state(tinyrl);
  28. // Input processing vars
  29. tinyrl->utf8_cont = 0;
  30. tinyrl->esc_cont = BOOL_FALSE;
  31. tinyrl->esc_seq[0] = '\0';
  32. tinyrl->esc_p = tinyrl->esc_seq;
  33. // Prompt
  34. tinyrl_set_prompt(tinyrl, "> ");
  35. // Key handlers
  36. for (i = 0; i < NUM_HANDLERS; i++) {
  37. tinyrl->handlers[i] = tinyrl_key_default;
  38. }
  39. tinyrl->handlers[KEY_CR] = tinyrl_key_crlf;
  40. tinyrl->handlers[KEY_LF] = tinyrl_key_crlf;
  41. tinyrl->handlers[KEY_ETX] = tinyrl_key_interrupt;
  42. tinyrl->handlers[KEY_DEL] = tinyrl_key_backspace;
  43. tinyrl->handlers[KEY_BS] = tinyrl_key_backspace;
  44. tinyrl->handlers[KEY_EOT] = tinyrl_key_delete;
  45. tinyrl->handlers[KEY_FF] = tinyrl_key_clear_screen;
  46. tinyrl->handlers[KEY_NAK] = tinyrl_key_erase_line;
  47. tinyrl->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  48. tinyrl->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  49. tinyrl->handlers[KEY_VT] = tinyrl_key_kill;
  50. tinyrl->handlers[KEY_EM] = tinyrl_key_yank;
  51. tinyrl->handlers[KEY_HT] = tinyrl_key_tab;
  52. tinyrl->handlers[KEY_ETB] = tinyrl_key_backword;
  53. tinyrl->max_line_length = 0;
  54. tinyrl->buffer = NULL;
  55. tinyrl->buffer_size = 0;
  56. tinyrl->done = BOOL_FALSE;
  57. tinyrl->completion_over = BOOL_FALSE;
  58. tinyrl->attempted_completion_function = NULL;
  59. tinyrl->hotkey_fn = NULL;
  60. tinyrl->state = 0;
  61. tinyrl->kill_string = NULL;
  62. tinyrl->echo_char = '\0';
  63. tinyrl->echo_enabled = BOOL_TRUE;
  64. tinyrl->utf8 = BOOL_TRUE;
  65. tinyrl->busy = BOOL_FALSE;
  66. // VT100 terminal
  67. tinyrl->term = vt100_new(istream, ostream);
  68. // To save terminal settings
  69. tinyrl_set_istream(tinyrl, istream);
  70. tinyrl->width = vt100_width(tinyrl->term);
  71. // History object
  72. tinyrl->hist = hist_new(hist_fname, hist_stifle);
  73. tinyrl_hist_restore(tinyrl);
  74. tty_raw_mode(tinyrl);
  75. return tinyrl;
  76. }
  77. void tinyrl_free(tinyrl_t *tinyrl)
  78. {
  79. assert(tinyrl);
  80. if (!tinyrl)
  81. return;
  82. tty_restore_mode(tinyrl);
  83. hist_save(tinyrl->hist);
  84. hist_free(tinyrl->hist);
  85. vt100_free(tinyrl->term);
  86. faux_str_free(tinyrl->prompt);
  87. tinyrl_reset_line_state(tinyrl); // It's really reset 'last' string
  88. faux_str_free(tinyrl->line.str);
  89. faux_free(tinyrl);
  90. }
  91. void tty_raw_mode(tinyrl_t *tinyrl)
  92. {
  93. struct termios new_termios = {};
  94. FILE *istream = NULL;
  95. int fd = -1;
  96. if (!tinyrl)
  97. return;
  98. istream = vt100_istream(tinyrl->term);
  99. if (!istream)
  100. return;
  101. fd = fileno(istream);
  102. if (tcgetattr(fd, &new_termios) < 0)
  103. return;
  104. new_termios.c_iflag = 0;
  105. new_termios.c_oflag = OPOST | ONLCR;
  106. new_termios.c_lflag = 0;
  107. new_termios.c_cc[VMIN] = 1;
  108. new_termios.c_cc[VTIME] = 0;
  109. // Mode switch
  110. tcsetattr(fd, TCSADRAIN, &new_termios);
  111. }
  112. void tty_restore_mode(tinyrl_t *tinyrl)
  113. {
  114. FILE *istream = NULL;
  115. int fd = -1;
  116. istream = vt100_istream(tinyrl->term);
  117. if (!istream)
  118. return;
  119. fd = fileno(istream);
  120. // Do the mode switch
  121. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  122. }
  123. bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn)
  124. {
  125. assert(tinyrl);
  126. if (!tinyrl)
  127. return BOOL_FALSE;
  128. if ((key < 0) || (key > 255))
  129. return BOOL_FALSE;
  130. tinyrl->handlers[key] = fn;
  131. return BOOL_TRUE;
  132. }
  133. void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn)
  134. {
  135. tinyrl->hotkey_fn = fn;
  136. }
  137. void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream)
  138. {
  139. assert(tinyrl);
  140. if (!tinyrl)
  141. return;
  142. vt100_set_istream(tinyrl->term, istream);
  143. // Save terminal settings to restore on exit
  144. if (istream)
  145. tcgetattr(fileno(istream), &tinyrl->default_termios);
  146. }
  147. FILE *tinyrl_istream(const tinyrl_t *tinyrl)
  148. {
  149. return vt100_istream(tinyrl->term);
  150. }
  151. void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream)
  152. {
  153. assert(tinyrl);
  154. if (!tinyrl)
  155. return;
  156. vt100_set_ostream(tinyrl->term, ostream);
  157. }
  158. FILE *tinyrl_ostream(const tinyrl_t *tinyrl)
  159. {
  160. return vt100_ostream(tinyrl->term);
  161. }
  162. bool_t tinyrl_utf8(const tinyrl_t *tinyrl)
  163. {
  164. assert(tinyrl);
  165. if (!tinyrl)
  166. return BOOL_TRUE;
  167. return tinyrl->utf8;
  168. }
  169. void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8)
  170. {
  171. assert(tinyrl);
  172. if (!tinyrl)
  173. return;
  174. tinyrl->utf8 = utf8;
  175. }
  176. bool_t tinyrl_busy(const tinyrl_t *tinyrl)
  177. {
  178. assert(tinyrl);
  179. if (!tinyrl)
  180. return BOOL_FALSE;
  181. return tinyrl->busy;
  182. }
  183. void tinyrl_set_busy(tinyrl_t *tinyrl, bool_t busy)
  184. {
  185. assert(tinyrl);
  186. if (!tinyrl)
  187. return;
  188. tinyrl->busy = busy;
  189. }
  190. const char *tinyrl_prompt(const tinyrl_t *tinyrl)
  191. {
  192. assert(tinyrl);
  193. if (!tinyrl)
  194. return NULL;
  195. return tinyrl->prompt;
  196. }
  197. void tinyrl_set_prompt(tinyrl_t *tinyrl, const char *prompt)
  198. {
  199. assert(tinyrl);
  200. if (!tinyrl)
  201. return;
  202. if (tinyrl->prompt)
  203. faux_str_free(tinyrl->prompt);
  204. tinyrl->prompt = faux_str_dup(prompt);
  205. tinyrl->prompt_len = strlen(tinyrl->prompt);
  206. tinyrl->prompt_chars = utf8_nsyms(tinyrl->prompt, tinyrl->prompt_len);
  207. }
  208. void *tinyrl_udata(const tinyrl_t *tinyrl)
  209. {
  210. assert(tinyrl);
  211. if (!tinyrl)
  212. return NULL;
  213. return tinyrl->udata;
  214. }
  215. void tinyrl_set_udata(tinyrl_t *tinyrl, void *udata)
  216. {
  217. assert(tinyrl);
  218. if (!tinyrl)
  219. return;
  220. tinyrl->udata = udata;
  221. }
  222. const char *tinyrl_line(const tinyrl_t *tinyrl)
  223. {
  224. return tinyrl->line.str;
  225. }
  226. bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
  227. {
  228. assert(tinyrl);
  229. if (!tinyrl)
  230. return BOOL_FALSE;
  231. return hist_save(tinyrl->hist);
  232. }
  233. bool_t tinyrl_hist_restore(tinyrl_t *tinyrl)
  234. {
  235. assert(tinyrl);
  236. if (!tinyrl)
  237. return BOOL_FALSE;
  238. return hist_restore(tinyrl->hist);
  239. }
  240. static bool_t process_char(tinyrl_t *tinyrl, char key)
  241. {
  242. // Begin of ESC sequence
  243. if (!tinyrl->esc_cont && (KEY_ESC == key)) {
  244. tinyrl->esc_cont = BOOL_TRUE; // Start ESC sequence
  245. tinyrl->esc_p = tinyrl->esc_seq;
  246. // Note: Don't put ESC symbol itself to buffer
  247. return BOOL_TRUE;
  248. }
  249. // Continue ESC sequence
  250. if (tinyrl->esc_cont) {
  251. // Broken sequence. Too long
  252. if ((tinyrl->esc_p - tinyrl->esc_seq) >= (sizeof(tinyrl->esc_seq) - 1)) {
  253. tinyrl->esc_cont = BOOL_FALSE;
  254. return BOOL_FALSE;
  255. }
  256. // Save the curren char to sequence buffer
  257. *tinyrl->esc_p = key;
  258. tinyrl->esc_p++;
  259. // ANSI standard control sequences will end
  260. // with a character between 64 - 126
  261. if ((key != '[') && (key > 63)) {
  262. *tinyrl->esc_p = '\0';
  263. tinyrl_esc_seq(tinyrl, tinyrl->esc_seq);
  264. tinyrl->esc_cont = BOOL_FALSE;
  265. //tinyrl_redisplay(tinyrl);
  266. }
  267. return BOOL_TRUE;
  268. }
  269. // Call the handler for key
  270. // Handler (that has no special meaning) will put new char to line buffer
  271. if (!tinyrl->handlers[(unsigned char)key](tinyrl, key))
  272. vt100_ding(tinyrl->term);
  273. // if (tinyrl->done) // Some handler set the done flag
  274. // continue; // It will break the loop
  275. if (tinyrl->utf8) {
  276. // ASCII char (one byte)
  277. if (!(UTF8_7BIT_MASK & key)) {
  278. tinyrl->utf8_cont = 0;
  279. // First byte of multibyte symbol
  280. } else if (UTF8_11 == (key & UTF8_MASK)) {
  281. // Find out number of symbol's bytes
  282. unsigned int b = (unsigned int)key;
  283. tinyrl->utf8_cont = 0;
  284. while ((tinyrl->utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  285. tinyrl->utf8_cont++;
  286. b = b << 1;
  287. }
  288. // Continue of multibyte symbol
  289. } else if ((tinyrl->utf8_cont > 0) && (UTF8_10 == (key & UTF8_MASK))) {
  290. tinyrl->utf8_cont--;
  291. }
  292. }
  293. // For non UTF-8 encoding the utf8_cont is always 0.
  294. // For UTF-8 it's 0 when one-byte symbol or we get
  295. // all bytes for the current multibyte character
  296. // if (!tinyrl->utf8_cont) {
  297. // //tinyrl_redisplay(tinyrl);
  298. // printf("%s\n", tinyrl->line.str);
  299. // }
  300. return BOOL_TRUE;
  301. }
  302. int tinyrl_read(tinyrl_t *tinyrl)
  303. {
  304. int rc = 0;
  305. unsigned char key = 0;
  306. int count = 0;
  307. assert(tinyrl);
  308. tinyrl_set_busy(tinyrl, BOOL_FALSE);
  309. while ((rc = vt100_getchar(tinyrl->term, &key)) > 0) {
  310. count++;
  311. process_char(tinyrl, key);
  312. // Some commands can't be processed immediately by handlers and
  313. // need some network exchange for example. In this case we will
  314. // not execute redisplay() here.
  315. if (!tinyrl->utf8_cont && !tinyrl_busy(tinyrl)) {
  316. tinyrl_redisplay(tinyrl);
  317. // printf("%s\n", tinyrl->line.str);
  318. }
  319. //printf("key=%u, pos=%lu, len=%lu\n", key, tinyrl->line.pos, tinyrl->line.len);
  320. }
  321. if ((rc < 0) && (EAGAIN == errno))
  322. return count;
  323. return rc;
  324. }
  325. /*
  326. * Ensure that buffer has enough space to hold len characters,
  327. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  328. * if the line is successfully extended, BOOL_FALSE if not.
  329. */
  330. bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
  331. {
  332. char *new_buf = NULL;
  333. size_t new_size = 0;
  334. size_t chunk_num = 0;
  335. if (tinyrl->line.len >= len)
  336. return BOOL_TRUE;
  337. chunk_num = len / LINE_CHUNK;
  338. if ((len % LINE_CHUNK) > 0)
  339. chunk_num++;
  340. new_size = chunk_num * LINE_CHUNK;
  341. // First initialization
  342. if (tinyrl->line.str == NULL) {
  343. tinyrl->line.str = faux_zmalloc(new_size);
  344. if (!tinyrl->line.str)
  345. return BOOL_FALSE;
  346. tinyrl->line.size = new_size;
  347. return BOOL_TRUE;
  348. }
  349. new_buf = realloc(tinyrl->line.str, new_size);
  350. if (!new_buf)
  351. return BOOL_FALSE;
  352. tinyrl->line.str = new_buf;
  353. tinyrl->line.size = new_size;
  354. return BOOL_TRUE;
  355. }
  356. bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
  357. {
  358. bool_t result = BOOL_FALSE;
  359. switch (vt100_esc_decode(tinyrl->term, esc_seq)) {
  360. case VT100_CURSOR_UP:
  361. result = tinyrl_key_up(tinyrl, 0);
  362. break;
  363. case VT100_CURSOR_DOWN:
  364. result = tinyrl_key_down(tinyrl, 0);
  365. break;
  366. case VT100_CURSOR_LEFT:
  367. result = tinyrl_key_left(tinyrl, 0);
  368. break;
  369. case VT100_CURSOR_RIGHT:
  370. result = tinyrl_key_right(tinyrl, 0);
  371. break;
  372. case VT100_HOME:
  373. result = tinyrl_key_start_of_line(tinyrl, 0);
  374. break;
  375. case VT100_END:
  376. result = tinyrl_key_end_of_line(tinyrl, 0);
  377. break;
  378. case VT100_DELETE:
  379. result = tinyrl_key_delete(tinyrl, 0);
  380. break;
  381. case VT100_INSERT:
  382. case VT100_PGDOWN:
  383. case VT100_PGUP:
  384. case VT100_UNKNOWN:
  385. break;
  386. }
  387. return result;
  388. }
  389. bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
  390. {
  391. size_t new_size = tinyrl->line.len + len + 1;
  392. if (len == 0)
  393. return BOOL_TRUE;
  394. tinyrl_line_extend(tinyrl, new_size);
  395. if (tinyrl->line.pos < tinyrl->line.len) {
  396. memmove(tinyrl->line.str + tinyrl->line.pos + len,
  397. tinyrl->line.str + tinyrl->line.pos,
  398. tinyrl->line.len - tinyrl->line.pos);
  399. }
  400. memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
  401. tinyrl->line.pos += len;
  402. tinyrl->line.len += len;
  403. tinyrl->line.str[tinyrl->line.len] = '\0';
  404. return BOOL_TRUE;
  405. }
  406. bool_t tinyrl_line_delete(tinyrl_t *tinyrl, off_t start, size_t len)
  407. {
  408. if (start >= tinyrl->line.len)
  409. return BOOL_TRUE;
  410. if ((start + len) >= tinyrl->line.len) {
  411. tinyrl->line.len = start;
  412. } else {
  413. memmove(tinyrl->line.str + start,
  414. tinyrl->line.str + start + len,
  415. tinyrl->line.len - (start + len));
  416. tinyrl->line.len -= len;
  417. }
  418. tinyrl->line.pos = start;
  419. tinyrl->line.str[tinyrl->line.len] = '\0';
  420. return BOOL_TRUE;
  421. }
  422. bool_t tinyrl_line_replace(tinyrl_t *tinyrl, const char *text)
  423. {
  424. size_t len = 0;
  425. if (faux_str_is_empty(text)) {
  426. tinyrl_reset_line(tinyrl);
  427. return BOOL_TRUE;
  428. }
  429. len = strlen(text);
  430. tinyrl_line_extend(tinyrl, len + 1);
  431. memcpy(tinyrl->line.str, text, len);
  432. tinyrl->line.pos = len;
  433. tinyrl->line.len = len;
  434. tinyrl->line.str[tinyrl->line.len] = '\0';
  435. return BOOL_TRUE;
  436. }
  437. static void move_cursor(const tinyrl_t *tinyrl, size_t cur_pos, size_t target_pos)
  438. {
  439. int rows = 0;
  440. int cols = 0;
  441. // Note: The '/' is not real math division. It's integer part of division
  442. // so we need separate division for two values.
  443. rows = (target_pos / tinyrl->width) - (cur_pos / tinyrl->width);
  444. cols = (target_pos % tinyrl->width) - (cur_pos % tinyrl->width);
  445. if (cols > 0)
  446. vt100_cursor_forward(tinyrl->term, cols);
  447. else if (cols < 0)
  448. vt100_cursor_back(tinyrl->term, -cols);
  449. if (rows > 0)
  450. vt100_cursor_down(tinyrl->term, rows);
  451. else if (rows < 0)
  452. vt100_cursor_up(tinyrl->term, -rows);
  453. }
  454. static size_t str_equal_part(const tinyrl_t *tinyrl,
  455. const char *s1, const char *s2)
  456. {
  457. const char *str1 = s1;
  458. const char *str2 = s2;
  459. if (!str1 || !str2)
  460. return 0;
  461. while (*str1 && *str2) {
  462. if (*str1 != *str2)
  463. break;
  464. str1++;
  465. str2++;
  466. }
  467. if (!tinyrl->utf8)
  468. return str1 - s1;
  469. // UTF8
  470. // If UTF8_10 byte (intermediate byte of UTF-8 sequence) is not equal
  471. // then we need to find starting of this UTF-8 character because whole
  472. // UTF-8 character is not equal.
  473. if (UTF8_10 == (*str1 & UTF8_MASK)) {
  474. // Skip intermediate bytes
  475. while ((str1 > s1) && (UTF8_10 == (*str1 & UTF8_MASK)))
  476. str1--;
  477. }
  478. return str1 - s1;
  479. }
  480. void tinyrl_save_last(tinyrl_t *tinyrl)
  481. {
  482. faux_str_free(tinyrl->last.str);
  483. tinyrl->last = tinyrl->line;
  484. tinyrl->last.str = faux_str_dup(tinyrl->line.str);
  485. }
  486. void tinyrl_reset_line_state(tinyrl_t *tinyrl)
  487. {
  488. faux_str_free(tinyrl->last.str);
  489. faux_bzero(&tinyrl->last, sizeof(tinyrl->last));
  490. }
  491. void tinyrl_reset_line(tinyrl_t *tinyrl)
  492. {
  493. tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
  494. }
  495. void tinyrl_redisplay(tinyrl_t *tinyrl)
  496. {
  497. size_t width = vt100_width(tinyrl->term);
  498. // unsigned int line_size = strlen(tinyrl->line);
  499. unsigned int line_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.len);
  500. size_t cols = 0;
  501. size_t eq_bytes = 0;
  502. // Prepare print position
  503. if (tinyrl->last.str && (width == tinyrl->width)) {
  504. size_t eq_chars = 0; // Printable symbols
  505. size_t last_pos_chars = 0;
  506. // If line and last line have the equal chars at begining
  507. eq_bytes = str_equal_part(tinyrl, tinyrl->line.str, tinyrl->last.str);
  508. eq_chars = utf8_nsyms(tinyrl->last.str, eq_bytes);
  509. last_pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  510. move_cursor(tinyrl, tinyrl->prompt_chars + last_pos_chars,
  511. tinyrl->prompt_chars + eq_chars);
  512. } else {
  513. // Prepare to resize
  514. if (width != tinyrl->width) {
  515. vt100_next_line(tinyrl->term);
  516. vt100_erase_down(tinyrl->term);
  517. }
  518. vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  519. }
  520. // Print current line
  521. vt100_printf(tinyrl->term, "%s", tinyrl->line.str + eq_bytes);
  522. cols = (tinyrl->prompt_chars + line_chars) % width;
  523. if ((cols == 0) && (tinyrl->line.len - eq_bytes))
  524. vt100_next_line(tinyrl->term);
  525. // Erase down if current line is shorter than previous one
  526. if (tinyrl->last.len > tinyrl->line.len)
  527. vt100_erase_down(tinyrl->term);
  528. // Move the cursor to the insertion point
  529. if (tinyrl->line.pos < tinyrl->line.len) {
  530. size_t pos_chars = utf8_nsyms(tinyrl->line.str, tinyrl->line.pos);
  531. move_cursor(tinyrl, tinyrl->prompt_chars + line_chars,
  532. tinyrl->prompt_chars + pos_chars);
  533. }
  534. // Update the display
  535. vt100_oflush(tinyrl->term);
  536. // Save the last line buffer
  537. tinyrl_save_last(tinyrl);
  538. tinyrl->width = width;
  539. }
  540. void tinyrl_crlf(const tinyrl_t *tinyrl)
  541. {
  542. vt100_printf(tinyrl->term, "\n");
  543. }
  544. // Jump to first free line after current multiline input
  545. void tinyrl_multi_crlf(const tinyrl_t *tinyrl)
  546. {
  547. size_t full_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.len);
  548. size_t pos_chars = utf8_nsyms(tinyrl->last.str, tinyrl->last.pos);
  549. move_cursor(tinyrl, tinyrl->prompt_chars + pos_chars,
  550. tinyrl->prompt_chars + full_chars);
  551. tinyrl_crlf(tinyrl);
  552. vt100_oflush(tinyrl->term);
  553. }
  554. void tinyrl_line_to_hist(tinyrl_t *tinyrl)
  555. {
  556. if (tinyrl->line.len == 0)
  557. return;
  558. hist_add(tinyrl->hist, tinyrl->line.str, BOOL_FALSE);
  559. }
  560. void tinyrl_reset_hist_pos(tinyrl_t *tinyrl)
  561. {
  562. hist_pos_reset(tinyrl->hist);
  563. }
  564. #if 0
  565. /*----------------------------------------------------------------------- */
  566. /*
  567. tinyrl is called whenever a line is edited in any way.
  568. It signals that if we are currently viewing a history line we should transfer it
  569. to the current buffer
  570. */
  571. static void changed_line(tinyrl_t * tinyrl)
  572. {
  573. /* if the current line is not our buffer then make it so */
  574. if (tinyrl->line != tinyrl->buffer) {
  575. /* replace the current buffer with the new details */
  576. free(tinyrl->buffer);
  577. tinyrl->line = tinyrl->buffer = lub_string_dup(tinyrl->line);
  578. tinyrl->buffer_size = strlen(tinyrl->buffer);
  579. assert(tinyrl->line);
  580. }
  581. }
  582. /*----------------------------------------------------------------------- */
  583. /*
  584. * A convenience function for displaying a list of strings in columnar
  585. * format on Readline's output stream. matches is the list of strings,
  586. * in argv format, such as a list of completion matches. len is the number
  587. * of strings in matches, and max is the length of the longest string in matches.
  588. * tinyrl function uses the setting of print-completions-horizontally to select
  589. * how the matches are displayed
  590. */
  591. void tinyrl_display_matches(const tinyrl_t *tinyrl,
  592. char *const *matches, unsigned int len, size_t max)
  593. {
  594. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  595. unsigned int cols, rows;
  596. /* Find out column and rows number */
  597. if (max < width)
  598. cols = (width + 1) / (max + 1); /* allow for a space between words */
  599. else
  600. cols = 1;
  601. rows = len / cols + 1;
  602. assert(matches);
  603. if (matches) {
  604. unsigned int r, c;
  605. len--, matches++; /* skip the subtitution string */
  606. /* Print out a table of completions */
  607. for (r = 0; r < rows && len; r++) {
  608. for (c = 0; c < cols && len; c++) {
  609. const char *match = *matches++;
  610. len--;
  611. if ((c + 1) == cols) /* Last str in row */
  612. tinyrl_vt100_printf(tinyrl->term, "%s",
  613. match);
  614. else
  615. tinyrl_vt100_printf(tinyrl->term, "%-*s ",
  616. max, match);
  617. }
  618. tinyrl_crlf(tinyrl);
  619. }
  620. }
  621. }
  622. /*-------------------------------------------------------- */
  623. /*
  624. * Returns an array of strings which is a list of completions for text.
  625. * If there are no completions, returns NULL. The first entry in the
  626. * returned array is the substitution for text. The remaining entries
  627. * are the possible completions. The array is terminated with a NULL pointer.
  628. *
  629. * entry_func is a function of two args, and returns a char *.
  630. * The first argument is text. The second is a state argument;
  631. * it is zero on the first call, and non-zero on subsequent calls.
  632. * entry_func returns a NULL pointer to the caller when there are no
  633. * more matches.
  634. */
  635. char **tinyrl_completion(tinyrl_t * tinyrl,
  636. const char *line, unsigned int start, unsigned int end,
  637. tinyrl_compentry_func_t * entry_func)
  638. {
  639. unsigned int state = 0;
  640. size_t size = 1;
  641. unsigned int offset = 1; /* Need at least one entry for the substitution */
  642. char **matches = NULL;
  643. char *match;
  644. /* duplicate the string upto the insertion point */
  645. char *text = lub_string_dupn(line, end);
  646. /* now try and find possible completions */
  647. while ((match = entry_func(tinyrl, text, start, state++))) {
  648. if (size == offset) {
  649. /* resize the buffer if needed - the +1 is for the NULL terminator */
  650. size += 10;
  651. matches =
  652. realloc(matches, (sizeof(char *) * (size + 1)));
  653. }
  654. /* not much we can do... */
  655. if (!matches)
  656. break;
  657. matches[offset] = match;
  658. /*
  659. * augment the substitute string with tinyrl entry
  660. */
  661. if (1 == offset) {
  662. /* let's be optimistic */
  663. matches[0] = lub_string_dup(match);
  664. } else {
  665. char *p = matches[0];
  666. size_t match_len = strlen(p);
  667. /* identify the common prefix */
  668. while ((tolower(*p) == tolower(*match)) && match_len--) {
  669. p++, match++;
  670. }
  671. /* terminate the prefix string */
  672. *p = '\0';
  673. }
  674. offset++;
  675. }
  676. /* be a good memory citizen */
  677. lub_string_free(text);
  678. if (matches)
  679. matches[offset] = NULL;
  680. return matches;
  681. }
  682. /*-------------------------------------------------------- */
  683. void tinyrl_delete_matches(char **tinyrl)
  684. {
  685. char **matches = tinyrl;
  686. while (*matches) {
  687. /* release the memory for each contained string */
  688. free(*matches++);
  689. }
  690. /* release the memory for the array */
  691. free(tinyrl);
  692. }
  693. /*-------------------------------------------------------- */
  694. void tinyrl_replace_line(tinyrl_t * tinyrl, const char *text, int clear_undo)
  695. {
  696. size_t new_len = strlen(text);
  697. /* ignored for now */
  698. clear_undo = clear_undo;
  699. /* ensure there is sufficient space */
  700. if (tinyrl_extend_line_buffer(tinyrl, new_len)) {
  701. /* overwrite the current contents of the buffer */
  702. strcpy(tinyrl->buffer, text);
  703. /* set the insert point and end point */
  704. tinyrl->point = tinyrl->end = new_len;
  705. }
  706. tinyrl_redisplay(tinyrl);
  707. }
  708. /*-------------------------------------------------------- */
  709. static tinyrl_match_e
  710. tinyrl_do_complete(tinyrl_t * tinyrl, bool_t with_extensions)
  711. {
  712. tinyrl_match_e result = TINYRL_NO_MATCH;
  713. char **matches = NULL;
  714. unsigned int start, end;
  715. bool_t completion = BOOL_FALSE;
  716. bool_t prefix = BOOL_FALSE;
  717. int i = 0;
  718. /* find the start and end of the current word */
  719. start = end = tinyrl->point;
  720. while (start && !isspace(tinyrl->line[start - 1]))
  721. start--;
  722. if (tinyrl->attempted_completion_function) {
  723. tinyrl->completion_over = BOOL_FALSE;
  724. tinyrl->completion_error_over = BOOL_FALSE;
  725. /* try and complete the current line buffer */
  726. matches = tinyrl->attempted_completion_function(tinyrl,
  727. tinyrl->line, start, end);
  728. }
  729. if (!matches && (BOOL_FALSE == tinyrl->completion_over)) {
  730. /* insert default completion call here... */
  731. }
  732. if (!matches)
  733. return result;
  734. /* identify and insert a common prefix if there is one */
  735. if (0 != strncmp(matches[0], &tinyrl->line[start],
  736. strlen(matches[0]))) {
  737. /*
  738. * delete the original text not including
  739. * the current insertion point character
  740. */
  741. if (tinyrl->end != end)
  742. end--;
  743. tinyrl_delete_text(tinyrl, start, end);
  744. if (BOOL_FALSE == tinyrl_insert_text(tinyrl, matches[0]))
  745. return TINYRL_NO_MATCH;
  746. completion = BOOL_TRUE;
  747. }
  748. for (i = 1; matches[i]; i++) {
  749. /* tinyrl is just a prefix string */
  750. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  751. prefix = BOOL_TRUE;
  752. }
  753. /* is there more than one completion? */
  754. if (matches[2]) {
  755. char **tmp = matches;
  756. unsigned int max, len;
  757. max = len = 0;
  758. while (*tmp) {
  759. size_t size = strlen(*tmp++);
  760. len++;
  761. if (size > max)
  762. max = size;
  763. }
  764. if (completion)
  765. result = TINYRL_COMPLETED_AMBIGUOUS;
  766. else if (prefix)
  767. result = TINYRL_MATCH_WITH_EXTENSIONS;
  768. else
  769. result = TINYRL_AMBIGUOUS;
  770. if (with_extensions || !prefix) {
  771. /* Either we always want to show extensions or
  772. * we haven't been able to complete the current line
  773. * and there is just a prefix, so let the user see the options
  774. */
  775. tinyrl_crlf(tinyrl);
  776. tinyrl_display_matches(tinyrl, matches, len, max);
  777. tinyrl_reset_line_state(tinyrl);
  778. }
  779. } else {
  780. result = completion ?
  781. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  782. }
  783. /* free the memory */
  784. tinyrl_delete_matches(matches);
  785. /* redisplay the line */
  786. tinyrl_redisplay(tinyrl);
  787. return result;
  788. }
  789. /*-------------------------------------------------------- */
  790. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * tinyrl)
  791. {
  792. return tinyrl_do_complete(tinyrl, BOOL_TRUE);
  793. }
  794. /*-------------------------------------------------------- */
  795. tinyrl_match_e tinyrl_complete(tinyrl_t * tinyrl)
  796. {
  797. return tinyrl_do_complete(tinyrl, BOOL_FALSE);
  798. }
  799. /*--------------------------------------------------------- */
  800. void tinyrl_completion_over(tinyrl_t * tinyrl)
  801. {
  802. tinyrl->completion_over = BOOL_TRUE;
  803. }
  804. /*--------------------------------------------------------- */
  805. void tinyrl_completion_error_over(tinyrl_t * tinyrl)
  806. {
  807. tinyrl->completion_error_over = BOOL_TRUE;
  808. }
  809. /*--------------------------------------------------------- */
  810. bool_t tinyrl_is_completion_error_over(const tinyrl_t * tinyrl)
  811. {
  812. return tinyrl->completion_error_over;
  813. }
  814. #endif