tinyrl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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. // Key handlers
  32. for (i = 0; i < NUM_HANDLERS; i++) {
  33. tinyrl->handlers[i] = tinyrl_key_default;
  34. }
  35. tinyrl->handlers[KEY_CR] = tinyrl_key_crlf;
  36. tinyrl->handlers[KEY_LF] = tinyrl_key_crlf;
  37. tinyrl->handlers[KEY_ETX] = tinyrl_key_interrupt;
  38. tinyrl->handlers[KEY_DEL] = tinyrl_key_backspace;
  39. tinyrl->handlers[KEY_BS] = tinyrl_key_backspace;
  40. tinyrl->handlers[KEY_EOT] = tinyrl_key_delete;
  41. tinyrl->handlers[KEY_FF] = tinyrl_key_clear_screen;
  42. tinyrl->handlers[KEY_NAK] = tinyrl_key_erase_line;
  43. tinyrl->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  44. tinyrl->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  45. tinyrl->handlers[KEY_VT] = tinyrl_key_kill;
  46. tinyrl->handlers[KEY_EM] = tinyrl_key_yank;
  47. tinyrl->handlers[KEY_HT] = tinyrl_key_tab;
  48. tinyrl->handlers[KEY_ETB] = tinyrl_key_backword;
  49. tinyrl->max_line_length = 0;
  50. tinyrl->prompt = NULL;
  51. tinyrl->prompt_size = 0;
  52. tinyrl->buffer = NULL;
  53. tinyrl->buffer_size = 0;
  54. tinyrl->done = BOOL_FALSE;
  55. tinyrl->completion_over = BOOL_FALSE;
  56. tinyrl->attempted_completion_function = NULL;
  57. tinyrl->hotkey_fn = NULL;
  58. tinyrl->state = 0;
  59. tinyrl->kill_string = NULL;
  60. tinyrl->echo_char = '\0';
  61. tinyrl->echo_enabled = BOOL_TRUE;
  62. tinyrl->last_buffer = NULL;
  63. tinyrl->last_point = 0;
  64. tinyrl->last_line_size = 0;
  65. tinyrl->utf8 = BOOL_TRUE;
  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_free(tinyrl->hist);
  84. vt100_free(tinyrl->term);
  85. faux_str_free(tinyrl->buffer);
  86. faux_str_free(tinyrl->kill_string);
  87. faux_str_free(tinyrl->last_buffer);
  88. faux_str_free(tinyrl->prompt);
  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_hist_save(const tinyrl_t *tinyrl)
  177. {
  178. assert(tinyrl);
  179. if (!tinyrl)
  180. return BOOL_FALSE;
  181. return hist_save(tinyrl->hist);
  182. }
  183. bool_t tinyrl_hist_restore(tinyrl_t *tinyrl)
  184. {
  185. assert(tinyrl);
  186. if (!tinyrl)
  187. return BOOL_FALSE;
  188. return hist_restore(tinyrl->hist);
  189. }
  190. static bool_t process_char(tinyrl_t *tinyrl, char key)
  191. {
  192. // Begin of ESC sequence
  193. if (!tinyrl->esc_cont && (KEY_ESC == key)) {
  194. tinyrl->esc_cont = BOOL_TRUE; // Start ESC sequence
  195. tinyrl->esc_p = tinyrl->esc_seq;
  196. // Note: Don't put ESC symbol itself to buffer
  197. return BOOL_TRUE;
  198. }
  199. // Continue ESC sequence
  200. if (tinyrl->esc_cont) {
  201. // Broken sequence. Too long
  202. if ((tinyrl->esc_p - tinyrl->esc_seq) >= (sizeof(tinyrl->esc_seq) - 1)) {
  203. tinyrl->esc_cont = BOOL_FALSE;
  204. return BOOL_FALSE;
  205. }
  206. // Save the curren char to sequence buffer
  207. *tinyrl->esc_p = key;
  208. tinyrl->esc_p++;
  209. // ANSI standard control sequences will end
  210. // with a character between 64 - 126
  211. if ((key != '[') && (key > 63)) {
  212. *tinyrl->esc_p = '\0';
  213. tinyrl_esc_seq(tinyrl, tinyrl->esc_seq);
  214. tinyrl->esc_cont = BOOL_FALSE;
  215. //tinyrl_redisplay(tinyrl);
  216. }
  217. return BOOL_TRUE;
  218. }
  219. // Call the handler for key
  220. // Handler (that has no special meaning) will put new char to line buffer
  221. if (!tinyrl->handlers[(unsigned char)key](tinyrl, key))
  222. vt100_ding(tinyrl->term);
  223. // if (tinyrl->done) // Some handler set the done flag
  224. // continue; // It will break the loop
  225. if (tinyrl->utf8) {
  226. // ASCII char (one byte)
  227. if (!(UTF8_7BIT_MASK & key)) {
  228. tinyrl->utf8_cont = 0;
  229. // First byte of multibyte symbol
  230. } else if (UTF8_11 == (key & UTF8_MASK)) {
  231. // Find out number of symbol's bytes
  232. unsigned int b = (unsigned int)key;
  233. tinyrl->utf8_cont = 0;
  234. while ((tinyrl->utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  235. tinyrl->utf8_cont++;
  236. b = b << 1;
  237. }
  238. // Continue of multibyte symbol
  239. } else if ((tinyrl->utf8_cont > 0) && (UTF8_10 == (key & UTF8_MASK))) {
  240. tinyrl->utf8_cont--;
  241. }
  242. }
  243. // For non UTF-8 encoding the utf8_cont is always 0.
  244. // For UTF-8 it's 0 when one-byte symbol or we get
  245. // all bytes for the current multibyte character
  246. if (!tinyrl->utf8_cont) {
  247. //tinyrl_redisplay(tinyrl);
  248. printf("%s\n", tinyrl->line.str);
  249. }
  250. return BOOL_TRUE;
  251. }
  252. int tinyrl_read(tinyrl_t *tinyrl)
  253. {
  254. int rc = 0;
  255. char key = 0;
  256. int count = 0;
  257. assert(tinyrl);
  258. while ((rc = vt100_getchar(tinyrl->term, &key)) > 0) {
  259. count++;
  260. process_char(tinyrl, key);
  261. }
  262. if ((rc < 0) && (EAGAIN == errno))
  263. return count;
  264. return rc;
  265. }
  266. /*
  267. * Ensure that buffer has enough space to hold len characters,
  268. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  269. * if the line is successfully extended, BOOL_FALSE if not.
  270. */
  271. bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
  272. {
  273. char *new_buf = NULL;
  274. size_t new_size = 0;
  275. size_t chunk_num = 0;
  276. if (tinyrl->line.len >= len)
  277. return BOOL_TRUE;
  278. chunk_num = len / LINE_CHUNK;
  279. if ((len % LINE_CHUNK) > 0)
  280. chunk_num++;
  281. new_size = chunk_num * LINE_CHUNK;
  282. // First initialization
  283. if (tinyrl->line.str == NULL) {
  284. tinyrl->line.str = faux_zmalloc(new_size);
  285. if (!tinyrl->line.str)
  286. return BOOL_FALSE;
  287. tinyrl->line.size = new_size;
  288. return BOOL_TRUE;
  289. }
  290. new_buf = realloc(tinyrl->line.str, new_size);
  291. if (!new_buf)
  292. return BOOL_FALSE;
  293. tinyrl->line.str = new_buf;
  294. tinyrl->line.size = new_size;
  295. return BOOL_TRUE;
  296. }
  297. bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
  298. {
  299. bool_t result = BOOL_FALSE;
  300. switch (vt100_esc_decode(tinyrl->term, esc_seq)) {
  301. case VT100_CURSOR_UP:
  302. result = tinyrl_key_up(tinyrl, 0);
  303. break;
  304. case VT100_CURSOR_DOWN:
  305. result = tinyrl_key_down(tinyrl, 0);
  306. break;
  307. case VT100_CURSOR_LEFT:
  308. result = tinyrl_key_left(tinyrl, 0);
  309. break;
  310. case VT100_CURSOR_RIGHT:
  311. result = tinyrl_key_right(tinyrl, 0);
  312. break;
  313. case VT100_HOME:
  314. result = tinyrl_key_start_of_line(tinyrl, 0);
  315. break;
  316. case VT100_END:
  317. result = tinyrl_key_end_of_line(tinyrl, 0);
  318. break;
  319. case VT100_DELETE:
  320. result = tinyrl_key_delete(tinyrl, 0);
  321. break;
  322. case VT100_INSERT:
  323. case VT100_PGDOWN:
  324. case VT100_PGUP:
  325. case VT100_UNKNOWN:
  326. break;
  327. }
  328. return result;
  329. }
  330. bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
  331. {
  332. size_t new_size = tinyrl->line.len + len + 1;
  333. if (len == 0)
  334. return BOOL_TRUE;
  335. tinyrl_line_extend(tinyrl, new_size);
  336. if (tinyrl->line.pos < tinyrl->line.len) {
  337. memmove(tinyrl->line.str + tinyrl->line.pos + len,
  338. tinyrl->line.str + tinyrl->line.pos,
  339. len);
  340. }
  341. memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
  342. tinyrl->line.pos += len;
  343. tinyrl->line.len += len;
  344. tinyrl->line.str[tinyrl->line.len] = '\0';
  345. return BOOL_TRUE;
  346. }
  347. bool_t tinyrl_line_delete(tinyrl_t *tinyrl, off_t start, size_t len)
  348. {
  349. if (start >= tinyrl->line.len)
  350. return BOOL_TRUE;
  351. if ((start + len) >= tinyrl->line.len) {
  352. tinyrl->line.len = start;
  353. } else {
  354. memmove(tinyrl->line.str + start,
  355. tinyrl->line.str + start + len,
  356. tinyrl->line.len - (start + len));
  357. tinyrl->line.len -= len;
  358. }
  359. tinyrl->line.pos = start;
  360. tinyrl->line.str[tinyrl->line.len] = '\0';
  361. return BOOL_TRUE;
  362. }
  363. #if 0
  364. /*----------------------------------------------------------------------- */
  365. /*
  366. tinyrl is called whenever a line is edited in any way.
  367. It signals that if we are currently viewing a history line we should transfer it
  368. to the current buffer
  369. */
  370. static void changed_line(tinyrl_t * tinyrl)
  371. {
  372. /* if the current line is not our buffer then make it so */
  373. if (tinyrl->line != tinyrl->buffer) {
  374. /* replace the current buffer with the new details */
  375. free(tinyrl->buffer);
  376. tinyrl->line = tinyrl->buffer = lub_string_dup(tinyrl->line);
  377. tinyrl->buffer_size = strlen(tinyrl->buffer);
  378. assert(tinyrl->line);
  379. }
  380. }
  381. /*-------------------------------------------------------- */
  382. int tinyrl_printf(const tinyrl_t * tinyrl, const char *fmt, ...)
  383. {
  384. va_list args;
  385. int len;
  386. va_start(args, fmt);
  387. len = tinyrl_vt100_vprintf(tinyrl->term, fmt, args);
  388. va_end(args);
  389. return len;
  390. }
  391. /*----------------------------------------------------------------------- */
  392. static void tinyrl_internal_print(const tinyrl_t * tinyrl, const char *text)
  393. {
  394. if (tinyrl->echo_enabled) {
  395. /* simply echo the line */
  396. tinyrl_vt100_printf(tinyrl->term, "%s", text);
  397. } else {
  398. /* replace the line with echo char if defined */
  399. if (tinyrl->echo_char) {
  400. unsigned int i = strlen(text);
  401. while (i--) {
  402. tinyrl_vt100_printf(tinyrl->term, "%c",
  403. tinyrl->echo_char);
  404. }
  405. }
  406. }
  407. }
  408. /*----------------------------------------------------------------------- */
  409. static void tinyrl_internal_position(const tinyrl_t *tinyrl, int prompt_len,
  410. int line_len, unsigned int width)
  411. {
  412. int rows, cols;
  413. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  414. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  415. if (cols > 0)
  416. tinyrl_vt100_cursor_back(tinyrl->term, cols);
  417. else if (cols < 0)
  418. tinyrl_vt100_cursor_forward(tinyrl->term, -cols);
  419. if (rows > 0)
  420. tinyrl_vt100_cursor_up(tinyrl->term, rows);
  421. else if (rows < 0)
  422. tinyrl_vt100_cursor_down(tinyrl->term, -rows);
  423. }
  424. /*-------------------------------------------------------- */
  425. /* Jump to first free line after current multiline input */
  426. void tinyrl_multi_crlf(const tinyrl_t * tinyrl)
  427. {
  428. unsigned int line_size = strlen(tinyrl->last_buffer);
  429. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, line_size);
  430. unsigned int count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  431. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + line_len,
  432. - (line_len - count), tinyrl->width);
  433. tinyrl_crlf(tinyrl);
  434. tinyrl_vt100_oflush(tinyrl->term);
  435. }
  436. /*----------------------------------------------------------------------- */
  437. void tinyrl_redisplay(tinyrl_t * tinyrl)
  438. {
  439. unsigned int line_size = strlen(tinyrl->line);
  440. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->line, line_size);
  441. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  442. unsigned int count, eq_chars = 0;
  443. int cols;
  444. /* Prepare print position */
  445. if (tinyrl->last_buffer && (width == tinyrl->width)) {
  446. unsigned int eq_len = 0;
  447. /* If line and last line have the equal chars at begining */
  448. eq_chars = lub_string_equal_part(tinyrl->line, tinyrl->last_buffer,
  449. tinyrl->utf8);
  450. eq_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, eq_chars);
  451. count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  452. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + eq_len,
  453. count - eq_len, width);
  454. } else {
  455. /* Prepare to resize */
  456. if (width != tinyrl->width) {
  457. tinyrl_vt100_next_line(tinyrl->term);
  458. tinyrl_vt100_erase_down(tinyrl->term);
  459. }
  460. tinyrl_vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  461. }
  462. /* Print current line */
  463. tinyrl_internal_print(tinyrl, tinyrl->line + eq_chars);
  464. cols = (tinyrl->prompt_len + line_len) % width;
  465. if (!cols && (line_size - eq_chars))
  466. tinyrl_vt100_next_line(tinyrl->term);
  467. /* Erase down if current line is shorter than previous one */
  468. if (tinyrl->last_line_size > line_size)
  469. tinyrl_vt100_erase_down(tinyrl->term);
  470. /* Move the cursor to the insertion point */
  471. if (tinyrl->point < line_size) {
  472. unsigned int pre_len = utf8_nsyms(tinyrl,
  473. tinyrl->line, tinyrl->point);
  474. count = utf8_nsyms(tinyrl, tinyrl->line + tinyrl->point,
  475. line_size - tinyrl->point);
  476. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + pre_len,
  477. count, width);
  478. }
  479. /* Update the display */
  480. tinyrl_vt100_oflush(tinyrl->term);
  481. /* Save the last line buffer */
  482. lub_string_free(tinyrl->last_buffer);
  483. tinyrl->last_buffer = lub_string_dup(tinyrl->line);
  484. tinyrl->last_point = tinyrl->point;
  485. tinyrl->width = width;
  486. tinyrl->last_line_size = line_size;
  487. }
  488. /*----------------------------------------------------------------------- */
  489. /*
  490. * A convenience function for displaying a list of strings in columnar
  491. * format on Readline's output stream. matches is the list of strings,
  492. * in argv format, such as a list of completion matches. len is the number
  493. * of strings in matches, and max is the length of the longest string in matches.
  494. * tinyrl function uses the setting of print-completions-horizontally to select
  495. * how the matches are displayed
  496. */
  497. void tinyrl_display_matches(const tinyrl_t *tinyrl,
  498. char *const *matches, unsigned int len, size_t max)
  499. {
  500. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  501. unsigned int cols, rows;
  502. /* Find out column and rows number */
  503. if (max < width)
  504. cols = (width + 1) / (max + 1); /* allow for a space between words */
  505. else
  506. cols = 1;
  507. rows = len / cols + 1;
  508. assert(matches);
  509. if (matches) {
  510. unsigned int r, c;
  511. len--, matches++; /* skip the subtitution string */
  512. /* Print out a table of completions */
  513. for (r = 0; r < rows && len; r++) {
  514. for (c = 0; c < cols && len; c++) {
  515. const char *match = *matches++;
  516. len--;
  517. if ((c + 1) == cols) /* Last str in row */
  518. tinyrl_vt100_printf(tinyrl->term, "%s",
  519. match);
  520. else
  521. tinyrl_vt100_printf(tinyrl->term, "%-*s ",
  522. max, match);
  523. }
  524. tinyrl_crlf(tinyrl);
  525. }
  526. }
  527. }
  528. /*-------------------------------------------------------- */
  529. /*
  530. * Returns an array of strings which is a list of completions for text.
  531. * If there are no completions, returns NULL. The first entry in the
  532. * returned array is the substitution for text. The remaining entries
  533. * are the possible completions. The array is terminated with a NULL pointer.
  534. *
  535. * entry_func is a function of two args, and returns a char *.
  536. * The first argument is text. The second is a state argument;
  537. * it is zero on the first call, and non-zero on subsequent calls.
  538. * entry_func returns a NULL pointer to the caller when there are no
  539. * more matches.
  540. */
  541. char **tinyrl_completion(tinyrl_t * tinyrl,
  542. const char *line, unsigned int start, unsigned int end,
  543. tinyrl_compentry_func_t * entry_func)
  544. {
  545. unsigned int state = 0;
  546. size_t size = 1;
  547. unsigned int offset = 1; /* Need at least one entry for the substitution */
  548. char **matches = NULL;
  549. char *match;
  550. /* duplicate the string upto the insertion point */
  551. char *text = lub_string_dupn(line, end);
  552. /* now try and find possible completions */
  553. while ((match = entry_func(tinyrl, text, start, state++))) {
  554. if (size == offset) {
  555. /* resize the buffer if needed - the +1 is for the NULL terminator */
  556. size += 10;
  557. matches =
  558. realloc(matches, (sizeof(char *) * (size + 1)));
  559. }
  560. /* not much we can do... */
  561. if (!matches)
  562. break;
  563. matches[offset] = match;
  564. /*
  565. * augment the substitute string with tinyrl entry
  566. */
  567. if (1 == offset) {
  568. /* let's be optimistic */
  569. matches[0] = lub_string_dup(match);
  570. } else {
  571. char *p = matches[0];
  572. size_t match_len = strlen(p);
  573. /* identify the common prefix */
  574. while ((tolower(*p) == tolower(*match)) && match_len--) {
  575. p++, match++;
  576. }
  577. /* terminate the prefix string */
  578. *p = '\0';
  579. }
  580. offset++;
  581. }
  582. /* be a good memory citizen */
  583. lub_string_free(text);
  584. if (matches)
  585. matches[offset] = NULL;
  586. return matches;
  587. }
  588. /*-------------------------------------------------------- */
  589. void tinyrl_delete_matches(char **tinyrl)
  590. {
  591. char **matches = tinyrl;
  592. while (*matches) {
  593. /* release the memory for each contained string */
  594. free(*matches++);
  595. }
  596. /* release the memory for the array */
  597. free(tinyrl);
  598. }
  599. /*-------------------------------------------------------- */
  600. void tinyrl_crlf(const tinyrl_t * tinyrl)
  601. {
  602. tinyrl_vt100_printf(tinyrl->term, "\n");
  603. }
  604. /*-------------------------------------------------------- */
  605. /*
  606. * Ring the terminal bell, obeying the setting of bell-style.
  607. */
  608. void tinyrl_ding(const tinyrl_t * tinyrl)
  609. {
  610. tinyrl_vt100_ding(tinyrl->term);
  611. }
  612. /*-------------------------------------------------------- */
  613. void tinyrl_reset_line_state(tinyrl_t * tinyrl)
  614. {
  615. lub_string_free(tinyrl->last_buffer);
  616. tinyrl->last_buffer = NULL;
  617. tinyrl->last_line_size = 0;
  618. tinyrl_redisplay(tinyrl);
  619. }
  620. /*-------------------------------------------------------- */
  621. void tinyrl_replace_line(tinyrl_t * tinyrl, const char *text, int clear_undo)
  622. {
  623. size_t new_len = strlen(text);
  624. /* ignored for now */
  625. clear_undo = clear_undo;
  626. /* ensure there is sufficient space */
  627. if (tinyrl_extend_line_buffer(tinyrl, new_len)) {
  628. /* overwrite the current contents of the buffer */
  629. strcpy(tinyrl->buffer, text);
  630. /* set the insert point and end point */
  631. tinyrl->point = tinyrl->end = new_len;
  632. }
  633. tinyrl_redisplay(tinyrl);
  634. }
  635. /*-------------------------------------------------------- */
  636. static tinyrl_match_e
  637. tinyrl_do_complete(tinyrl_t * tinyrl, bool_t with_extensions)
  638. {
  639. tinyrl_match_e result = TINYRL_NO_MATCH;
  640. char **matches = NULL;
  641. unsigned int start, end;
  642. bool_t completion = BOOL_FALSE;
  643. bool_t prefix = BOOL_FALSE;
  644. int i = 0;
  645. /* find the start and end of the current word */
  646. start = end = tinyrl->point;
  647. while (start && !isspace(tinyrl->line[start - 1]))
  648. start--;
  649. if (tinyrl->attempted_completion_function) {
  650. tinyrl->completion_over = BOOL_FALSE;
  651. tinyrl->completion_error_over = BOOL_FALSE;
  652. /* try and complete the current line buffer */
  653. matches = tinyrl->attempted_completion_function(tinyrl,
  654. tinyrl->line, start, end);
  655. }
  656. if (!matches && (BOOL_FALSE == tinyrl->completion_over)) {
  657. /* insert default completion call here... */
  658. }
  659. if (!matches)
  660. return result;
  661. /* identify and insert a common prefix if there is one */
  662. if (0 != strncmp(matches[0], &tinyrl->line[start],
  663. strlen(matches[0]))) {
  664. /*
  665. * delete the original text not including
  666. * the current insertion point character
  667. */
  668. if (tinyrl->end != end)
  669. end--;
  670. tinyrl_delete_text(tinyrl, start, end);
  671. if (BOOL_FALSE == tinyrl_insert_text(tinyrl, matches[0]))
  672. return TINYRL_NO_MATCH;
  673. completion = BOOL_TRUE;
  674. }
  675. for (i = 1; matches[i]; i++) {
  676. /* tinyrl is just a prefix string */
  677. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  678. prefix = BOOL_TRUE;
  679. }
  680. /* is there more than one completion? */
  681. if (matches[2]) {
  682. char **tmp = matches;
  683. unsigned int max, len;
  684. max = len = 0;
  685. while (*tmp) {
  686. size_t size = strlen(*tmp++);
  687. len++;
  688. if (size > max)
  689. max = size;
  690. }
  691. if (completion)
  692. result = TINYRL_COMPLETED_AMBIGUOUS;
  693. else if (prefix)
  694. result = TINYRL_MATCH_WITH_EXTENSIONS;
  695. else
  696. result = TINYRL_AMBIGUOUS;
  697. if (with_extensions || !prefix) {
  698. /* Either we always want to show extensions or
  699. * we haven't been able to complete the current line
  700. * and there is just a prefix, so let the user see the options
  701. */
  702. tinyrl_crlf(tinyrl);
  703. tinyrl_display_matches(tinyrl, matches, len, max);
  704. tinyrl_reset_line_state(tinyrl);
  705. }
  706. } else {
  707. result = completion ?
  708. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  709. }
  710. /* free the memory */
  711. tinyrl_delete_matches(matches);
  712. /* redisplay the line */
  713. tinyrl_redisplay(tinyrl);
  714. return result;
  715. }
  716. /*-------------------------------------------------------- */
  717. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * tinyrl)
  718. {
  719. return tinyrl_do_complete(tinyrl, BOOL_TRUE);
  720. }
  721. /*-------------------------------------------------------- */
  722. tinyrl_match_e tinyrl_complete(tinyrl_t * tinyrl)
  723. {
  724. return tinyrl_do_complete(tinyrl, BOOL_FALSE);
  725. }
  726. /*-------------------------------------------------------- */
  727. void *tinyrl__get_context(const tinyrl_t * tinyrl)
  728. {
  729. return tinyrl->context;
  730. }
  731. /*--------------------------------------------------------- */
  732. const char *tinyrl__get_line(const tinyrl_t * tinyrl)
  733. {
  734. return tinyrl->line;
  735. }
  736. /*--------------------------------------------------------- */
  737. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * tinyrl)
  738. {
  739. return tinyrl->history;
  740. }
  741. /*--------------------------------------------------------- */
  742. void tinyrl_completion_over(tinyrl_t * tinyrl)
  743. {
  744. tinyrl->completion_over = BOOL_TRUE;
  745. }
  746. /*--------------------------------------------------------- */
  747. void tinyrl_completion_error_over(tinyrl_t * tinyrl)
  748. {
  749. tinyrl->completion_error_over = BOOL_TRUE;
  750. }
  751. /*--------------------------------------------------------- */
  752. bool_t tinyrl_is_completion_error_over(const tinyrl_t * tinyrl)
  753. {
  754. return tinyrl->completion_error_over;
  755. }
  756. /*--------------------------------------------------------- */
  757. void tinyrl_done(tinyrl_t * tinyrl)
  758. {
  759. tinyrl->done = BOOL_TRUE;
  760. }
  761. /*--------------------------------------------------------- */
  762. void tinyrl_enable_echo(tinyrl_t * tinyrl)
  763. {
  764. tinyrl->echo_enabled = BOOL_TRUE;
  765. }
  766. /*--------------------------------------------------------- */
  767. void tinyrl_disable_echo(tinyrl_t * tinyrl, char echo_char)
  768. {
  769. tinyrl->echo_enabled = BOOL_FALSE;
  770. tinyrl->echo_char = echo_char;
  771. }
  772. /*-------------------------------------------------------- */
  773. const char *tinyrl__get_prompt(const tinyrl_t * tinyrl)
  774. {
  775. return tinyrl->prompt;
  776. }
  777. /*-------------------------------------------------------- */
  778. void tinyrl__set_prompt(tinyrl_t *tinyrl, const char *prompt)
  779. {
  780. if (tinyrl->prompt) {
  781. lub_string_free(tinyrl->prompt);
  782. tinyrl->prompt_size = 0;
  783. tinyrl->prompt_len = 0;
  784. }
  785. tinyrl->prompt = lub_string_dup(prompt);
  786. if (tinyrl->prompt) {
  787. tinyrl->prompt_size = strlen(tinyrl->prompt);
  788. tinyrl->prompt_len = utf8_nsyms(tinyrl, tinyrl->prompt,
  789. tinyrl->prompt_size);
  790. }
  791. }
  792. /*-------------------------------------------------------- */
  793. bool_t tinyrl_is_quoting(const tinyrl_t * tinyrl)
  794. {
  795. bool_t result = BOOL_FALSE;
  796. /* count the quotes upto the current insertion point */
  797. unsigned int i = 0;
  798. while (i < tinyrl->point) {
  799. if (result && (tinyrl->line[i] == '\\')) {
  800. i++;
  801. if (i >= tinyrl->point)
  802. break;
  803. i++;
  804. continue;
  805. }
  806. if (tinyrl->line[i++] == '"') {
  807. result = result ? BOOL_FALSE : BOOL_TRUE;
  808. }
  809. }
  810. return result;
  811. }
  812. /*-------------------------------------------------------- */
  813. bool_t tinyrl_is_empty(const tinyrl_t *tinyrl)
  814. {
  815. return (tinyrl->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  816. }
  817. /*--------------------------------------------------------- */
  818. void tinyrl_limit_line_length(tinyrl_t * tinyrl, unsigned int length)
  819. {
  820. tinyrl->max_line_length = length;
  821. }
  822. #endif