tinyrl.c 24 KB

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