tinyrl.c 24 KB

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