tinyrl.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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/str.h>
  12. #include "private.h"
  13. static void tty_set_raw_mode(tinyrl_t *tinyrl)
  14. {
  15. struct termios new_termios = {};
  16. FILE *istream = NULL;
  17. int fd = -1;
  18. if (!tinyrl)
  19. return;
  20. istream = vt100_istream(tinyrl->term);
  21. if (!istream)
  22. return;
  23. fd = fileno(istream);
  24. if (tcgetattr(fd, &new_termios) < 0)
  25. return;
  26. new_termios.c_iflag = 0;
  27. new_termios.c_oflag = OPOST | ONLCR;
  28. new_termios.c_lflag = 0;
  29. new_termios.c_cc[VMIN] = 1;
  30. new_termios.c_cc[VTIME] = 0;
  31. // Mode switch
  32. tcsetattr(fd, TCSADRAIN, &new_termios);
  33. }
  34. static void tty_restore_mode(tinyrl_t *tinyrl)
  35. {
  36. FILE *istream = NULL;
  37. int fd = -1;
  38. istream = vt100_istream(tinyrl->term);
  39. if (!istream)
  40. return;
  41. fd = fileno(istream);
  42. // Do the mode switch
  43. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  44. }
  45. #if 0
  46. /*----------------------------------------------------------------------- */
  47. /*
  48. tinyrl is called whenever a line is edited in any way.
  49. It signals that if we are currently viewing a history line we should transfer it
  50. to the current buffer
  51. */
  52. static void changed_line(tinyrl_t * tinyrl)
  53. {
  54. /* if the current line is not our buffer then make it so */
  55. if (tinyrl->line != tinyrl->buffer) {
  56. /* replace the current buffer with the new details */
  57. free(tinyrl->buffer);
  58. tinyrl->line = tinyrl->buffer = lub_string_dup(tinyrl->line);
  59. tinyrl->buffer_size = strlen(tinyrl->buffer);
  60. assert(tinyrl->line);
  61. }
  62. }
  63. /*----------------------------------------------------------------------- */
  64. static int tinyrl_timeout_default(tinyrl_t *tinyrl)
  65. {
  66. tinyrl = tinyrl; /* Happy compiler */
  67. /* Return -1 to close session on timeout */
  68. return -1;
  69. }
  70. /*----------------------------------------------------------------------- */
  71. static bool_t tinyrl_key_default(tinyrl_t * tinyrl, int key)
  72. {
  73. bool_t result = BOOL_FALSE;
  74. if (key > 31) {
  75. char tmp[2];
  76. tmp[0] = (key & 0xFF), tmp[1] = '\0';
  77. /* inject tinyrl text into the buffer */
  78. result = tinyrl_insert_text(tinyrl, tmp);
  79. } else {
  80. /* Call the external hotkey analyzer */
  81. if (tinyrl->hotkey_fn)
  82. tinyrl->hotkey_fn(tinyrl, key);
  83. }
  84. return result;
  85. }
  86. /*-------------------------------------------------------- */
  87. static bool_t tinyrl_key_interrupt(tinyrl_t * tinyrl, int key)
  88. {
  89. tinyrl_crlf(tinyrl);
  90. tinyrl_delete_text(tinyrl, 0, tinyrl->end);
  91. tinyrl->done = BOOL_TRUE;
  92. /* keep the compiler happy */
  93. key = key;
  94. return BOOL_TRUE;
  95. }
  96. /*-------------------------------------------------------- */
  97. static bool_t tinyrl_key_start_of_line(tinyrl_t * tinyrl, int key)
  98. {
  99. /* set the insertion point to the start of the line */
  100. tinyrl->point = 0;
  101. /* keep the compiler happy */
  102. key = key;
  103. return BOOL_TRUE;
  104. }
  105. /*-------------------------------------------------------- */
  106. static bool_t tinyrl_key_end_of_line(tinyrl_t * tinyrl, int key)
  107. {
  108. /* set the insertion point to the end of the line */
  109. tinyrl->point = tinyrl->end;
  110. /* keep the compiler happy */
  111. key = key;
  112. return BOOL_TRUE;
  113. }
  114. /*-------------------------------------------------------- */
  115. static bool_t tinyrl_key_kill(tinyrl_t * tinyrl, int key)
  116. {
  117. /* release any old kill string */
  118. lub_string_free(tinyrl->kill_string);
  119. /* store the killed string */
  120. tinyrl->kill_string = lub_string_dup(&tinyrl->buffer[tinyrl->point]);
  121. /* delete the text to the end of the line */
  122. tinyrl_delete_text(tinyrl, tinyrl->point, tinyrl->end);
  123. /* keep the compiler happy */
  124. key = key;
  125. return BOOL_TRUE;
  126. }
  127. /*-------------------------------------------------------- */
  128. static bool_t tinyrl_key_yank(tinyrl_t * tinyrl, int key)
  129. {
  130. bool_t result = BOOL_FALSE;
  131. if (tinyrl->kill_string) {
  132. /* insert the kill string at the current insertion point */
  133. result = tinyrl_insert_text(tinyrl, tinyrl->kill_string);
  134. }
  135. /* keep the compiler happy */
  136. key = key;
  137. return result;
  138. }
  139. /*-------------------------------------------------------- */
  140. static bool_t tinyrl_key_crlf(tinyrl_t * tinyrl, int key)
  141. {
  142. tinyrl_crlf(tinyrl);
  143. tinyrl->done = BOOL_TRUE;
  144. /* keep the compiler happy */
  145. key = key;
  146. return BOOL_TRUE;
  147. }
  148. /*-------------------------------------------------------- */
  149. static bool_t tinyrl_key_up(tinyrl_t * tinyrl, int key)
  150. {
  151. bool_t result = BOOL_FALSE;
  152. tinyrl_history_entry_t *entry = NULL;
  153. if (tinyrl->line == tinyrl->buffer) {
  154. /* go to the last history entry */
  155. entry = tinyrl_history_getlast(tinyrl->history, &tinyrl->hist_iter);
  156. } else {
  157. /* already traversing the history list so get previous */
  158. entry = tinyrl_history_getprevious(&tinyrl->hist_iter);
  159. }
  160. if (entry) {
  161. /* display the entry moving the insertion point
  162. * to the end of the line
  163. */
  164. tinyrl->line = tinyrl_history_entry__get_line(entry);
  165. tinyrl->point = tinyrl->end = strlen(tinyrl->line);
  166. result = BOOL_TRUE;
  167. }
  168. /* keep the compiler happy */
  169. key = key;
  170. return result;
  171. }
  172. /*-------------------------------------------------------- */
  173. static bool_t tinyrl_key_down(tinyrl_t * tinyrl, int key)
  174. {
  175. bool_t result = BOOL_FALSE;
  176. if (tinyrl->line != tinyrl->buffer) {
  177. /* we are not already at the bottom */
  178. /* the iterator will have been set up by the key_up() function */
  179. tinyrl_history_entry_t *entry =
  180. tinyrl_history_getnext(&tinyrl->hist_iter);
  181. if (!entry) {
  182. /* nothing more in the history list */
  183. tinyrl->line = tinyrl->buffer;
  184. } else {
  185. tinyrl->line = tinyrl_history_entry__get_line(entry);
  186. }
  187. /* display the entry moving the insertion point
  188. * to the end of the line
  189. */
  190. tinyrl->point = tinyrl->end = strlen(tinyrl->line);
  191. result = BOOL_TRUE;
  192. }
  193. /* keep the compiler happy */
  194. key = key;
  195. return result;
  196. }
  197. /*-------------------------------------------------------- */
  198. static bool_t tinyrl_key_left(tinyrl_t * tinyrl, int key)
  199. {
  200. bool_t result = BOOL_FALSE;
  201. if (tinyrl->point > 0) {
  202. tinyrl->point--;
  203. utf8_point_left(tinyrl);
  204. result = BOOL_TRUE;
  205. }
  206. /* keep the compiler happy */
  207. key = key;
  208. return result;
  209. }
  210. /*-------------------------------------------------------- */
  211. static bool_t tinyrl_key_right(tinyrl_t * tinyrl, int key)
  212. {
  213. bool_t result = BOOL_FALSE;
  214. if (tinyrl->point < tinyrl->end) {
  215. tinyrl->point++;
  216. utf8_point_right(tinyrl);
  217. result = BOOL_TRUE;
  218. }
  219. /* keep the compiler happy */
  220. key = key;
  221. return result;
  222. }
  223. /*-------------------------------------------------------- */
  224. static bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, int key)
  225. {
  226. bool_t result = BOOL_FALSE;
  227. if (tinyrl->point) {
  228. unsigned int end = --tinyrl->point;
  229. utf8_point_left(tinyrl);
  230. tinyrl_delete_text(tinyrl, tinyrl->point, end);
  231. result = BOOL_TRUE;
  232. }
  233. /* keep the compiler happy */
  234. key = key;
  235. return result;
  236. }
  237. /*-------------------------------------------------------- */
  238. static bool_t tinyrl_key_backword(tinyrl_t *tinyrl, int key)
  239. {
  240. bool_t result = BOOL_FALSE;
  241. /* remove current whitespace before cursor */
  242. while (tinyrl->point > 0 && isspace(tinyrl->line[tinyrl->point - 1]))
  243. tinyrl_key_backspace(tinyrl, KEY_BS);
  244. /* delete word before cusor */
  245. while (tinyrl->point > 0 && !isspace(tinyrl->line[tinyrl->point - 1]))
  246. tinyrl_key_backspace(tinyrl, KEY_BS);
  247. result = BOOL_TRUE;
  248. /* keep the compiler happy */
  249. key = key;
  250. return result;
  251. }
  252. /*-------------------------------------------------------- */
  253. static bool_t tinyrl_key_delete(tinyrl_t * tinyrl, int key)
  254. {
  255. bool_t result = BOOL_FALSE;
  256. if (tinyrl->point < tinyrl->end) {
  257. unsigned int begin = tinyrl->point++;
  258. utf8_point_right(tinyrl);
  259. tinyrl_delete_text(tinyrl, begin, tinyrl->point - 1);
  260. result = BOOL_TRUE;
  261. }
  262. /* keep the compiler happy */
  263. key = key;
  264. return result;
  265. }
  266. /*-------------------------------------------------------- */
  267. static bool_t tinyrl_key_clear_screen(tinyrl_t * tinyrl, int key)
  268. {
  269. tinyrl_vt100_clear_screen(tinyrl->term);
  270. tinyrl_vt100_cursor_home(tinyrl->term);
  271. tinyrl_reset_line_state(tinyrl);
  272. /* keep the compiler happy */
  273. key = key;
  274. tinyrl = tinyrl;
  275. return BOOL_TRUE;
  276. }
  277. /*-------------------------------------------------------- */
  278. static bool_t tinyrl_key_erase_line(tinyrl_t * tinyrl, int key)
  279. {
  280. unsigned int end;
  281. /* release any old kill string */
  282. lub_string_free(tinyrl->kill_string);
  283. if (!tinyrl->point) {
  284. tinyrl->kill_string = NULL;
  285. return BOOL_TRUE;
  286. }
  287. end = tinyrl->point - 1;
  288. /* store the killed string */
  289. tinyrl->kill_string = malloc(tinyrl->point + 1);
  290. memcpy(tinyrl->kill_string, tinyrl->buffer, tinyrl->point);
  291. tinyrl->kill_string[tinyrl->point] = '\0';
  292. /* delete the text from the start of the line */
  293. tinyrl_delete_text(tinyrl, 0, end);
  294. tinyrl->point = 0;
  295. /* keep the compiler happy */
  296. key = key;
  297. tinyrl = tinyrl;
  298. return BOOL_TRUE;
  299. }/*-------------------------------------------------------- */
  300. static bool_t tinyrl_escape_seq(tinyrl_t *tinyrl, const char *esc_seq)
  301. {
  302. int key = 0;
  303. bool_t result = BOOL_FALSE;
  304. switch (tinyrl_vt100_escape_decode(tinyrl->term, esc_seq)) {
  305. case tinyrl_vt100_CURSOR_UP:
  306. result = tinyrl_key_up(tinyrl, key);
  307. break;
  308. case tinyrl_vt100_CURSOR_DOWN:
  309. result = tinyrl_key_down(tinyrl, key);
  310. break;
  311. case tinyrl_vt100_CURSOR_LEFT:
  312. result = tinyrl_key_left(tinyrl, key);
  313. break;
  314. case tinyrl_vt100_CURSOR_RIGHT:
  315. result = tinyrl_key_right(tinyrl, key);
  316. break;
  317. case tinyrl_vt100_HOME:
  318. result = tinyrl_key_start_of_line(tinyrl,key);
  319. break;
  320. case tinyrl_vt100_END:
  321. result = tinyrl_key_end_of_line(tinyrl,key);
  322. break;
  323. case tinyrl_vt100_DELETE:
  324. result = tinyrl_key_delete(tinyrl,key);
  325. break;
  326. case tinyrl_vt100_INSERT:
  327. case tinyrl_vt100_PGDOWN:
  328. case tinyrl_vt100_PGUP:
  329. case tinyrl_vt100_UNKNOWN:
  330. break;
  331. }
  332. return result;
  333. }
  334. /*-------------------------------------------------------- */
  335. static bool_t tinyrl_key_tab(tinyrl_t * tinyrl, int key)
  336. {
  337. bool_t result = BOOL_FALSE;
  338. tinyrl_match_e status = tinyrl_complete_with_extensions(tinyrl);
  339. switch (status) {
  340. case TINYRL_COMPLETED_MATCH:
  341. case TINYRL_MATCH:
  342. /* everything is OK with the world... */
  343. result = tinyrl_insert_text(tinyrl, " ");
  344. break;
  345. case TINYRL_NO_MATCH:
  346. case TINYRL_MATCH_WITH_EXTENSIONS:
  347. case TINYRL_AMBIGUOUS:
  348. case TINYRL_COMPLETED_AMBIGUOUS:
  349. /* oops don't change the result and let the bell ring */
  350. break;
  351. }
  352. /* keep the compiler happy */
  353. key = key;
  354. return result;
  355. }
  356. /*-------------------------------------------------------- */
  357. static void tinyrl_fini(tinyrl_t * tinyrl)
  358. {
  359. /* delete the history session */
  360. tinyrl_history_delete(tinyrl->history);
  361. /* delete the terminal session */
  362. tinyrl_vt100_delete(tinyrl->term);
  363. /* free up any dynamic strings */
  364. lub_string_free(tinyrl->buffer);
  365. lub_string_free(tinyrl->kill_string);
  366. lub_string_free(tinyrl->last_buffer);
  367. lub_string_free(tinyrl->prompt);
  368. }
  369. /*-------------------------------------------------------- */
  370. static void tinyrl_init(tinyrl_t * tinyrl, FILE * istream, FILE * ostream,
  371. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  372. {
  373. int i;
  374. for (i = 0; i < NUM_HANDLERS; i++) {
  375. tinyrl->handlers[i] = tinyrl_key_default;
  376. }
  377. /* Default handlers */
  378. tinyrl->handlers[KEY_CR] = tinyrl_key_crlf;
  379. tinyrl->handlers[KEY_LF] = tinyrl_key_crlf;
  380. tinyrl->handlers[KEY_ETX] = tinyrl_key_interrupt;
  381. tinyrl->handlers[KEY_DEL] = tinyrl_key_backspace;
  382. tinyrl->handlers[KEY_BS] = tinyrl_key_backspace;
  383. tinyrl->handlers[KEY_EOT] = tinyrl_key_delete;
  384. tinyrl->handlers[KEY_FF] = tinyrl_key_clear_screen;
  385. tinyrl->handlers[KEY_NAK] = tinyrl_key_erase_line;
  386. tinyrl->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  387. tinyrl->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  388. tinyrl->handlers[KEY_VT] = tinyrl_key_kill;
  389. tinyrl->handlers[KEY_EM] = tinyrl_key_yank;
  390. tinyrl->handlers[KEY_HT] = tinyrl_key_tab;
  391. tinyrl->handlers[KEY_ETB] = tinyrl_key_backword;
  392. tinyrl->line = NULL;
  393. tinyrl->max_line_length = 0;
  394. tinyrl->prompt = NULL;
  395. tinyrl->prompt_size = 0;
  396. tinyrl->buffer = NULL;
  397. tinyrl->buffer_size = 0;
  398. tinyrl->done = BOOL_FALSE;
  399. tinyrl->completion_over = BOOL_FALSE;
  400. tinyrl->point = 0;
  401. tinyrl->end = 0;
  402. tinyrl->attempted_completion_function = complete_fn;
  403. tinyrl->timeout_fn = tinyrl_timeout_default;
  404. tinyrl->keypress_fn = NULL;
  405. tinyrl->hotkey_fn = NULL;
  406. tinyrl->state = 0;
  407. tinyrl->kill_string = NULL;
  408. tinyrl->echo_char = '\0';
  409. tinyrl->echo_enabled = BOOL_TRUE;
  410. tinyrl->last_buffer = NULL;
  411. tinyrl->last_point = 0;
  412. tinyrl->last_line_size = 0;
  413. tinyrl->utf8 = BOOL_FALSE;
  414. /* create the vt100 terminal */
  415. tinyrl->term = tinyrl_vt100_new(NULL, ostream);
  416. tinyrl__set_istream(tinyrl, istream);
  417. tinyrl->last_width = tinyrl_vt100__get_width(tinyrl->term);
  418. /* create the history */
  419. tinyrl->history = tinyrl_history_new(stifle);
  420. }
  421. /*-------------------------------------------------------- */
  422. int tinyrl_printf(const tinyrl_t * tinyrl, const char *fmt, ...)
  423. {
  424. va_list args;
  425. int len;
  426. va_start(args, fmt);
  427. len = tinyrl_vt100_vprintf(tinyrl->term, fmt, args);
  428. va_end(args);
  429. return len;
  430. }
  431. /*-------------------------------------------------------- */
  432. void tinyrl_delete(tinyrl_t * tinyrl)
  433. {
  434. assert(tinyrl);
  435. if (tinyrl) {
  436. /* let the object tidy itself up */
  437. tinyrl_fini(tinyrl);
  438. /* release the memory associate with tinyrl instance */
  439. free(tinyrl);
  440. }
  441. }
  442. /*-------------------------------------------------------- */
  443. /*#####################################
  444. * EXPORTED INTERFACE
  445. *##################################### */
  446. /*----------------------------------------------------------------------- */
  447. int tinyrl_getchar(const tinyrl_t * tinyrl)
  448. {
  449. return tinyrl_vt100_getchar(tinyrl->term);
  450. }
  451. /*----------------------------------------------------------------------- */
  452. static void tinyrl_internal_print(const tinyrl_t * tinyrl, const char *text)
  453. {
  454. if (tinyrl->echo_enabled) {
  455. /* simply echo the line */
  456. tinyrl_vt100_printf(tinyrl->term, "%s", text);
  457. } else {
  458. /* replace the line with echo char if defined */
  459. if (tinyrl->echo_char) {
  460. unsigned int i = strlen(text);
  461. while (i--) {
  462. tinyrl_vt100_printf(tinyrl->term, "%c",
  463. tinyrl->echo_char);
  464. }
  465. }
  466. }
  467. }
  468. /*----------------------------------------------------------------------- */
  469. static void tinyrl_internal_position(const tinyrl_t *tinyrl, int prompt_len,
  470. int line_len, unsigned int width)
  471. {
  472. int rows, cols;
  473. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  474. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  475. if (cols > 0)
  476. tinyrl_vt100_cursor_back(tinyrl->term, cols);
  477. else if (cols < 0)
  478. tinyrl_vt100_cursor_forward(tinyrl->term, -cols);
  479. if (rows > 0)
  480. tinyrl_vt100_cursor_up(tinyrl->term, rows);
  481. else if (rows < 0)
  482. tinyrl_vt100_cursor_down(tinyrl->term, -rows);
  483. }
  484. /*-------------------------------------------------------- */
  485. /* Jump to first free line after current multiline input */
  486. void tinyrl_multi_crlf(const tinyrl_t * tinyrl)
  487. {
  488. unsigned int line_size = strlen(tinyrl->last_buffer);
  489. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, line_size);
  490. unsigned int count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  491. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + line_len,
  492. - (line_len - count), tinyrl->last_width);
  493. tinyrl_crlf(tinyrl);
  494. tinyrl_vt100_oflush(tinyrl->term);
  495. }
  496. /*----------------------------------------------------------------------- */
  497. void tinyrl_redisplay(tinyrl_t * tinyrl)
  498. {
  499. unsigned int line_size = strlen(tinyrl->line);
  500. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->line, line_size);
  501. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  502. unsigned int count, eq_chars = 0;
  503. int cols;
  504. /* Prepare print position */
  505. if (tinyrl->last_buffer && (width == tinyrl->last_width)) {
  506. unsigned int eq_len = 0;
  507. /* If line and last line have the equal chars at begining */
  508. eq_chars = lub_string_equal_part(tinyrl->line, tinyrl->last_buffer,
  509. tinyrl->utf8);
  510. eq_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, eq_chars);
  511. count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  512. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + eq_len,
  513. count - eq_len, width);
  514. } else {
  515. /* Prepare to resize */
  516. if (width != tinyrl->last_width) {
  517. tinyrl_vt100_next_line(tinyrl->term);
  518. tinyrl_vt100_erase_down(tinyrl->term);
  519. }
  520. tinyrl_vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  521. }
  522. /* Print current line */
  523. tinyrl_internal_print(tinyrl, tinyrl->line + eq_chars);
  524. cols = (tinyrl->prompt_len + line_len) % width;
  525. if (!cols && (line_size - eq_chars))
  526. tinyrl_vt100_next_line(tinyrl->term);
  527. /* Erase down if current line is shorter than previous one */
  528. if (tinyrl->last_line_size > line_size)
  529. tinyrl_vt100_erase_down(tinyrl->term);
  530. /* Move the cursor to the insertion point */
  531. if (tinyrl->point < line_size) {
  532. unsigned int pre_len = utf8_nsyms(tinyrl,
  533. tinyrl->line, tinyrl->point);
  534. count = utf8_nsyms(tinyrl, tinyrl->line + tinyrl->point,
  535. line_size - tinyrl->point);
  536. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + pre_len,
  537. count, width);
  538. }
  539. /* Update the display */
  540. tinyrl_vt100_oflush(tinyrl->term);
  541. /* Save the last line buffer */
  542. lub_string_free(tinyrl->last_buffer);
  543. tinyrl->last_buffer = lub_string_dup(tinyrl->line);
  544. tinyrl->last_point = tinyrl->point;
  545. tinyrl->last_width = width;
  546. tinyrl->last_line_size = line_size;
  547. }
  548. /*----------------------------------------------------------------------- */
  549. tinyrl_t *tinyrl_new(FILE * istream, FILE * ostream,
  550. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  551. {
  552. tinyrl_t *tinyrl = NULL;
  553. tinyrl = malloc(sizeof(tinyrl_t));
  554. if (tinyrl)
  555. tinyrl_init(tinyrl, istream, ostream, stifle, complete_fn);
  556. return tinyrl;
  557. }
  558. /*----------------------------------------------------------------------- */
  559. static char *internal_insertline(tinyrl_t * tinyrl, char *buffer)
  560. {
  561. char *p;
  562. char *s = buffer;
  563. /* strip any spurious '\r' or '\n' */
  564. if ((p = strchr(buffer, '\r')))
  565. *p = '\0';
  566. if ((p = strchr(buffer, '\n')))
  567. *p = '\0';
  568. /* skip any whitespace at the beginning of the line */
  569. if (0 == tinyrl->point) {
  570. while (*s && isspace(*s))
  571. s++;
  572. }
  573. if (*s) {
  574. /* append tinyrl string to the input buffer */
  575. (void)tinyrl_insert_text(tinyrl, s);
  576. }
  577. /* echo the command to the output stream */
  578. tinyrl_redisplay(tinyrl);
  579. return s;
  580. }
  581. /*----------------------------------------------------------------------- */
  582. static char *internal_readline(tinyrl_t * tinyrl,
  583. void *context, const char *str)
  584. {
  585. FILE *istream = tinyrl_vt100__get_istream(tinyrl->term);
  586. char *result = NULL;
  587. int lerrno = 0;
  588. tinyrl->done = BOOL_FALSE;
  589. tinyrl->point = 0;
  590. tinyrl->end = 0;
  591. tinyrl->buffer = lub_string_dup("");
  592. tinyrl->buffer_size = strlen(tinyrl->buffer);
  593. tinyrl->line = tinyrl->buffer;
  594. tinyrl->context = context;
  595. /* Interactive session */
  596. if (tinyrl->isatty && !str) {
  597. unsigned int utf8_cont = 0; /* UTF-8 continue bytes */
  598. unsigned int esc_cont = 0; /* Escape sequence continues */
  599. char esc_seq[10]; /* Buffer for ESC sequence */
  600. char *esc_p = esc_seq;
  601. /* Set the terminal into raw mode */
  602. tty_set_raw_mode(tinyrl);
  603. tinyrl_reset_line_state(tinyrl);
  604. while (!tinyrl->done) {
  605. int key;
  606. key = tinyrl_getchar(tinyrl);
  607. /* Error || EOF || Timeout */
  608. if (key < 0) {
  609. if ((VT100_TIMEOUT == key) &&
  610. !tinyrl->timeout_fn(tinyrl))
  611. continue;
  612. /* It's time to finish the session */
  613. tinyrl->done = BOOL_TRUE;
  614. tinyrl->line = NULL;
  615. lerrno = ENOENT;
  616. continue;
  617. }
  618. /* Real key pressed */
  619. /* Common callback for any key */
  620. if (tinyrl->keypress_fn)
  621. tinyrl->keypress_fn(tinyrl, key);
  622. /* Check for ESC sequence. It's a special case. */
  623. if (!esc_cont && (key == KEY_ESC)) {
  624. esc_cont = 1; /* Start ESC sequence */
  625. esc_p = esc_seq;
  626. continue;
  627. }
  628. if (esc_cont) {
  629. /* Broken sequence */
  630. if (esc_p >= (esc_seq + sizeof(esc_seq) - 1)) {
  631. esc_cont = 0;
  632. continue;
  633. }
  634. /* Dump the control sequence into sequence buffer
  635. ANSI standard control sequences will end
  636. with a character between 64 - 126 */
  637. *esc_p = key & 0xff;
  638. esc_p++;
  639. /* tinyrl is an ANSI control sequence terminator code */
  640. if ((key != '[') && (key > 63)) {
  641. *esc_p = '\0';
  642. tinyrl_escape_seq(tinyrl, esc_seq);
  643. esc_cont = 0;
  644. tinyrl_redisplay(tinyrl);
  645. }
  646. continue;
  647. }
  648. /* Call the handler for tinyrl key */
  649. if (!tinyrl->handlers[key](tinyrl, key))
  650. tinyrl_ding(tinyrl);
  651. if (tinyrl->done) /* Some handler set the done flag */
  652. continue; /* It will break the loop */
  653. if (tinyrl->utf8) {
  654. if (!(UTF8_7BIT_MASK & key)) /* ASCII char */
  655. utf8_cont = 0;
  656. else if (utf8_cont && (UTF8_10 == (key & UTF8_MASK))) /* Continue byte */
  657. utf8_cont--;
  658. else if (UTF8_11 == (key & UTF8_MASK)) { /* First byte of multibyte char */
  659. /* Find out number of char's bytes */
  660. int b = key;
  661. utf8_cont = 0;
  662. while ((utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  663. utf8_cont++;
  664. b = b << 1;
  665. }
  666. }
  667. }
  668. /* For non UTF-8 encoding the utf8_cont is always 0.
  669. For UTF-8 it's 0 when one-byte symbol or we get
  670. all bytes for the current multibyte character. */
  671. if (!utf8_cont)
  672. tinyrl_redisplay(tinyrl);
  673. }
  674. /* If the last character in the line (other than NULL)
  675. is a space remove it. */
  676. if (tinyrl->end && tinyrl->line && isspace(tinyrl->line[tinyrl->end - 1]))
  677. tinyrl_delete_text(tinyrl, tinyrl->end - 1, tinyrl->end);
  678. /* Restores the terminal mode */
  679. tty_restore_mode(tinyrl);
  680. /* Non-interactive session */
  681. } else {
  682. char *s = NULL, buffer[80];
  683. size_t len = sizeof(buffer);
  684. char *tmp = NULL;
  685. /* manually reset the line state without redisplaying */
  686. lub_string_free(tinyrl->last_buffer);
  687. tinyrl->last_buffer = NULL;
  688. if (str) {
  689. tmp = lub_string_dup(str);
  690. internal_insertline(tinyrl, tmp);
  691. } else {
  692. while (istream && (sizeof(buffer) == len) &&
  693. (s = fgets(buffer, sizeof(buffer), istream))) {
  694. s = internal_insertline(tinyrl, buffer);
  695. len = strlen(buffer) + 1; /* account for the '\0' */
  696. }
  697. if (!s || ((tinyrl->line[0] == '\0') && feof(istream))) {
  698. /* time to finish the session */
  699. tinyrl->line = NULL;
  700. lerrno = ENOENT;
  701. }
  702. }
  703. /*
  704. * check against fgets returning null as either error or end of file.
  705. * tinyrl is a measure to stop potential task spin on encountering an
  706. * error from fgets.
  707. */
  708. if (tinyrl->line && !tinyrl->handlers[KEY_LF](tinyrl, KEY_LF)) {
  709. /* an issue has occured */
  710. tinyrl->line = NULL;
  711. lerrno = ENOEXEC;
  712. }
  713. if (str)
  714. lub_string_free(tmp);
  715. }
  716. /*
  717. * duplicate the string for return to the client
  718. * we have to duplicate as we may be referencing a
  719. * history entry or our internal buffer
  720. */
  721. result = tinyrl->line ? lub_string_dup(tinyrl->line) : NULL;
  722. /* free our internal buffer */
  723. free(tinyrl->buffer);
  724. tinyrl->buffer = NULL;
  725. if (!result)
  726. errno = lerrno; /* get saved errno */
  727. return result;
  728. }
  729. /*----------------------------------------------------------------------- */
  730. char *tinyrl_readline(tinyrl_t * tinyrl, void *context)
  731. {
  732. return internal_readline(tinyrl, context, NULL);
  733. }
  734. /*----------------------------------------------------------------------- */
  735. char *tinyrl_forceline(tinyrl_t * tinyrl, void *context, const char *line)
  736. {
  737. return internal_readline(tinyrl, context, line);
  738. }
  739. /*----------------------------------------------------------------------- */
  740. /*
  741. * Ensure that buffer has enough space to hold len characters,
  742. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  743. * if the line is successfully extended, BOOL_FALSE if not.
  744. */
  745. bool_t tinyrl_extend_line_buffer(tinyrl_t * tinyrl, unsigned int len)
  746. {
  747. bool_t result = BOOL_TRUE;
  748. char *new_buffer;
  749. size_t new_len = len;
  750. if (tinyrl->buffer_size >= len)
  751. return result;
  752. /*
  753. * What we do depends on whether we are limited by
  754. * memory or a user imposed limit.
  755. */
  756. if (tinyrl->max_line_length == 0) {
  757. /* make sure we don't realloc too often */
  758. if (new_len < tinyrl->buffer_size + 10)
  759. new_len = tinyrl->buffer_size + 10;
  760. /* leave space for terminator */
  761. new_buffer = realloc(tinyrl->buffer, new_len + 1);
  762. if (!new_buffer) {
  763. tinyrl_ding(tinyrl);
  764. result = BOOL_FALSE;
  765. } else {
  766. tinyrl->buffer_size = new_len;
  767. tinyrl->line = tinyrl->buffer = new_buffer;
  768. }
  769. } else {
  770. if (new_len < tinyrl->max_line_length) {
  771. /* Just reallocate once to the max size */
  772. new_buffer = realloc(tinyrl->buffer,
  773. tinyrl->max_line_length);
  774. if (!new_buffer) {
  775. tinyrl_ding(tinyrl);
  776. result = BOOL_FALSE;
  777. } else {
  778. tinyrl->buffer_size =
  779. tinyrl->max_line_length - 1;
  780. tinyrl->line = tinyrl->buffer = new_buffer;
  781. }
  782. } else {
  783. tinyrl_ding(tinyrl);
  784. result = BOOL_FALSE;
  785. }
  786. }
  787. return result;
  788. }
  789. /*----------------------------------------------------------------------- */
  790. /*
  791. * Insert text into the line at the current cursor position.
  792. */
  793. bool_t tinyrl_insert_text(tinyrl_t * tinyrl, const char *text)
  794. {
  795. unsigned int delta = strlen(text);
  796. /*
  797. * If the client wants to change the line ensure that the line and buffer
  798. * references are in sync
  799. */
  800. changed_line(tinyrl);
  801. if ((delta + tinyrl->end) > (tinyrl->buffer_size)) {
  802. /* extend the current buffer */
  803. if (BOOL_FALSE ==
  804. tinyrl_extend_line_buffer(tinyrl, tinyrl->end + delta))
  805. return BOOL_FALSE;
  806. }
  807. if (tinyrl->point < tinyrl->end) {
  808. /* move the current text to the right (including the terminator) */
  809. memmove(&tinyrl->buffer[tinyrl->point + delta],
  810. &tinyrl->buffer[tinyrl->point],
  811. (tinyrl->end - tinyrl->point) + 1);
  812. } else {
  813. /* terminate the string */
  814. tinyrl->buffer[tinyrl->end + delta] = '\0';
  815. }
  816. /* insert the new text */
  817. strncpy(&tinyrl->buffer[tinyrl->point], text, delta);
  818. /* now update the indexes */
  819. tinyrl->point += delta;
  820. tinyrl->end += delta;
  821. return BOOL_TRUE;
  822. }
  823. /*----------------------------------------------------------------------- */
  824. /*
  825. * A convenience function for displaying a list of strings in columnar
  826. * format on Readline's output stream. matches is the list of strings,
  827. * in argv format, such as a list of completion matches. len is the number
  828. * of strings in matches, and max is the length of the longest string in matches.
  829. * tinyrl function uses the setting of print-completions-horizontally to select
  830. * how the matches are displayed
  831. */
  832. void tinyrl_display_matches(const tinyrl_t *tinyrl,
  833. char *const *matches, unsigned int len, size_t max)
  834. {
  835. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  836. unsigned int cols, rows;
  837. /* Find out column and rows number */
  838. if (max < width)
  839. cols = (width + 1) / (max + 1); /* allow for a space between words */
  840. else
  841. cols = 1;
  842. rows = len / cols + 1;
  843. assert(matches);
  844. if (matches) {
  845. unsigned int r, c;
  846. len--, matches++; /* skip the subtitution string */
  847. /* Print out a table of completions */
  848. for (r = 0; r < rows && len; r++) {
  849. for (c = 0; c < cols && len; c++) {
  850. const char *match = *matches++;
  851. len--;
  852. if ((c + 1) == cols) /* Last str in row */
  853. tinyrl_vt100_printf(tinyrl->term, "%s",
  854. match);
  855. else
  856. tinyrl_vt100_printf(tinyrl->term, "%-*s ",
  857. max, match);
  858. }
  859. tinyrl_crlf(tinyrl);
  860. }
  861. }
  862. }
  863. /*----------------------------------------------------------------------- */
  864. /*
  865. * Delete the text between start and end in the current line. (inclusive)
  866. * tinyrl adjusts the rl_point and rl_end indexes appropriately.
  867. */
  868. void tinyrl_delete_text(tinyrl_t * tinyrl, unsigned int start, unsigned int end)
  869. {
  870. unsigned int delta;
  871. /*
  872. * If the client wants to change the line ensure that the line and buffer
  873. * references are in sync
  874. */
  875. changed_line(tinyrl);
  876. /* make sure we play it safe */
  877. if (start > end) {
  878. unsigned int tmp = end;
  879. start = end;
  880. end = tmp;
  881. }
  882. if (end > tinyrl->end)
  883. end = tinyrl->end;
  884. delta = (end - start) + 1;
  885. /* move any text which is left */
  886. memmove(&tinyrl->buffer[start],
  887. &tinyrl->buffer[start + delta], tinyrl->end - end);
  888. /* now adjust the indexs */
  889. if (tinyrl->point >= start) {
  890. if (tinyrl->point > end) {
  891. /* move the insertion point back appropriately */
  892. tinyrl->point -= delta;
  893. } else {
  894. /* move the insertion point to the start */
  895. tinyrl->point = start;
  896. }
  897. }
  898. if (tinyrl->end > end)
  899. tinyrl->end -= delta;
  900. else
  901. tinyrl->end = start;
  902. /* put a terminator at the end of the buffer */
  903. tinyrl->buffer[tinyrl->end] = '\0';
  904. }
  905. /*----------------------------------------------------------------------- */
  906. bool_t tinyrl_bind_key(tinyrl_t * tinyrl, int key, tinyrl_key_func_t * fn)
  907. {
  908. bool_t result = BOOL_FALSE;
  909. if ((key >= 0) && (key < 256)) {
  910. /* set the key handling function */
  911. tinyrl->handlers[key] = fn;
  912. result = BOOL_TRUE;
  913. }
  914. return result;
  915. }
  916. /*-------------------------------------------------------- */
  917. /*
  918. * Returns an array of strings which is a list of completions for text.
  919. * If there are no completions, returns NULL. The first entry in the
  920. * returned array is the substitution for text. The remaining entries
  921. * are the possible completions. The array is terminated with a NULL pointer.
  922. *
  923. * entry_func is a function of two args, and returns a char *.
  924. * The first argument is text. The second is a state argument;
  925. * it is zero on the first call, and non-zero on subsequent calls.
  926. * entry_func returns a NULL pointer to the caller when there are no
  927. * more matches.
  928. */
  929. char **tinyrl_completion(tinyrl_t * tinyrl,
  930. const char *line, unsigned int start, unsigned int end,
  931. tinyrl_compentry_func_t * entry_func)
  932. {
  933. unsigned int state = 0;
  934. size_t size = 1;
  935. unsigned int offset = 1; /* Need at least one entry for the substitution */
  936. char **matches = NULL;
  937. char *match;
  938. /* duplicate the string upto the insertion point */
  939. char *text = lub_string_dupn(line, end);
  940. /* now try and find possible completions */
  941. while ((match = entry_func(tinyrl, text, start, state++))) {
  942. if (size == offset) {
  943. /* resize the buffer if needed - the +1 is for the NULL terminator */
  944. size += 10;
  945. matches =
  946. realloc(matches, (sizeof(char *) * (size + 1)));
  947. }
  948. /* not much we can do... */
  949. if (!matches)
  950. break;
  951. matches[offset] = match;
  952. /*
  953. * augment the substitute string with tinyrl entry
  954. */
  955. if (1 == offset) {
  956. /* let's be optimistic */
  957. matches[0] = lub_string_dup(match);
  958. } else {
  959. char *p = matches[0];
  960. size_t match_len = strlen(p);
  961. /* identify the common prefix */
  962. while ((tolower(*p) == tolower(*match)) && match_len--) {
  963. p++, match++;
  964. }
  965. /* terminate the prefix string */
  966. *p = '\0';
  967. }
  968. offset++;
  969. }
  970. /* be a good memory citizen */
  971. lub_string_free(text);
  972. if (matches)
  973. matches[offset] = NULL;
  974. return matches;
  975. }
  976. /*-------------------------------------------------------- */
  977. void tinyrl_delete_matches(char **tinyrl)
  978. {
  979. char **matches = tinyrl;
  980. while (*matches) {
  981. /* release the memory for each contained string */
  982. free(*matches++);
  983. }
  984. /* release the memory for the array */
  985. free(tinyrl);
  986. }
  987. /*-------------------------------------------------------- */
  988. void tinyrl_crlf(const tinyrl_t * tinyrl)
  989. {
  990. tinyrl_vt100_printf(tinyrl->term, "\n");
  991. }
  992. /*-------------------------------------------------------- */
  993. /*
  994. * Ring the terminal bell, obeying the setting of bell-style.
  995. */
  996. void tinyrl_ding(const tinyrl_t * tinyrl)
  997. {
  998. tinyrl_vt100_ding(tinyrl->term);
  999. }
  1000. /*-------------------------------------------------------- */
  1001. void tinyrl_reset_line_state(tinyrl_t * tinyrl)
  1002. {
  1003. lub_string_free(tinyrl->last_buffer);
  1004. tinyrl->last_buffer = NULL;
  1005. tinyrl->last_line_size = 0;
  1006. tinyrl_redisplay(tinyrl);
  1007. }
  1008. /*-------------------------------------------------------- */
  1009. void tinyrl_replace_line(tinyrl_t * tinyrl, const char *text, int clear_undo)
  1010. {
  1011. size_t new_len = strlen(text);
  1012. /* ignored for now */
  1013. clear_undo = clear_undo;
  1014. /* ensure there is sufficient space */
  1015. if (tinyrl_extend_line_buffer(tinyrl, new_len)) {
  1016. /* overwrite the current contents of the buffer */
  1017. strcpy(tinyrl->buffer, text);
  1018. /* set the insert point and end point */
  1019. tinyrl->point = tinyrl->end = new_len;
  1020. }
  1021. tinyrl_redisplay(tinyrl);
  1022. }
  1023. /*-------------------------------------------------------- */
  1024. static tinyrl_match_e
  1025. tinyrl_do_complete(tinyrl_t * tinyrl, bool_t with_extensions)
  1026. {
  1027. tinyrl_match_e result = TINYRL_NO_MATCH;
  1028. char **matches = NULL;
  1029. unsigned int start, end;
  1030. bool_t completion = BOOL_FALSE;
  1031. bool_t prefix = BOOL_FALSE;
  1032. int i = 0;
  1033. /* find the start and end of the current word */
  1034. start = end = tinyrl->point;
  1035. while (start && !isspace(tinyrl->line[start - 1]))
  1036. start--;
  1037. if (tinyrl->attempted_completion_function) {
  1038. tinyrl->completion_over = BOOL_FALSE;
  1039. tinyrl->completion_error_over = BOOL_FALSE;
  1040. /* try and complete the current line buffer */
  1041. matches = tinyrl->attempted_completion_function(tinyrl,
  1042. tinyrl->line, start, end);
  1043. }
  1044. if (!matches && (BOOL_FALSE == tinyrl->completion_over)) {
  1045. /* insert default completion call here... */
  1046. }
  1047. if (!matches)
  1048. return result;
  1049. /* identify and insert a common prefix if there is one */
  1050. if (0 != strncmp(matches[0], &tinyrl->line[start],
  1051. strlen(matches[0]))) {
  1052. /*
  1053. * delete the original text not including
  1054. * the current insertion point character
  1055. */
  1056. if (tinyrl->end != end)
  1057. end--;
  1058. tinyrl_delete_text(tinyrl, start, end);
  1059. if (BOOL_FALSE == tinyrl_insert_text(tinyrl, matches[0]))
  1060. return TINYRL_NO_MATCH;
  1061. completion = BOOL_TRUE;
  1062. }
  1063. for (i = 1; matches[i]; i++) {
  1064. /* tinyrl is just a prefix string */
  1065. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  1066. prefix = BOOL_TRUE;
  1067. }
  1068. /* is there more than one completion? */
  1069. if (matches[2]) {
  1070. char **tmp = matches;
  1071. unsigned int max, len;
  1072. max = len = 0;
  1073. while (*tmp) {
  1074. size_t size = strlen(*tmp++);
  1075. len++;
  1076. if (size > max)
  1077. max = size;
  1078. }
  1079. if (completion)
  1080. result = TINYRL_COMPLETED_AMBIGUOUS;
  1081. else if (prefix)
  1082. result = TINYRL_MATCH_WITH_EXTENSIONS;
  1083. else
  1084. result = TINYRL_AMBIGUOUS;
  1085. if (with_extensions || !prefix) {
  1086. /* Either we always want to show extensions or
  1087. * we haven't been able to complete the current line
  1088. * and there is just a prefix, so let the user see the options
  1089. */
  1090. tinyrl_crlf(tinyrl);
  1091. tinyrl_display_matches(tinyrl, matches, len, max);
  1092. tinyrl_reset_line_state(tinyrl);
  1093. }
  1094. } else {
  1095. result = completion ?
  1096. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  1097. }
  1098. /* free the memory */
  1099. tinyrl_delete_matches(matches);
  1100. /* redisplay the line */
  1101. tinyrl_redisplay(tinyrl);
  1102. return result;
  1103. }
  1104. /*-------------------------------------------------------- */
  1105. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * tinyrl)
  1106. {
  1107. return tinyrl_do_complete(tinyrl, BOOL_TRUE);
  1108. }
  1109. /*-------------------------------------------------------- */
  1110. tinyrl_match_e tinyrl_complete(tinyrl_t * tinyrl)
  1111. {
  1112. return tinyrl_do_complete(tinyrl, BOOL_FALSE);
  1113. }
  1114. /*-------------------------------------------------------- */
  1115. void *tinyrl__get_context(const tinyrl_t * tinyrl)
  1116. {
  1117. return tinyrl->context;
  1118. }
  1119. /*--------------------------------------------------------- */
  1120. const char *tinyrl__get_line(const tinyrl_t * tinyrl)
  1121. {
  1122. return tinyrl->line;
  1123. }
  1124. /*--------------------------------------------------------- */
  1125. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * tinyrl)
  1126. {
  1127. return tinyrl->history;
  1128. }
  1129. /*--------------------------------------------------------- */
  1130. void tinyrl_completion_over(tinyrl_t * tinyrl)
  1131. {
  1132. tinyrl->completion_over = BOOL_TRUE;
  1133. }
  1134. /*--------------------------------------------------------- */
  1135. void tinyrl_completion_error_over(tinyrl_t * tinyrl)
  1136. {
  1137. tinyrl->completion_error_over = BOOL_TRUE;
  1138. }
  1139. /*--------------------------------------------------------- */
  1140. bool_t tinyrl_is_completion_error_over(const tinyrl_t * tinyrl)
  1141. {
  1142. return tinyrl->completion_error_over;
  1143. }
  1144. /*--------------------------------------------------------- */
  1145. void tinyrl_done(tinyrl_t * tinyrl)
  1146. {
  1147. tinyrl->done = BOOL_TRUE;
  1148. }
  1149. /*--------------------------------------------------------- */
  1150. void tinyrl_enable_echo(tinyrl_t * tinyrl)
  1151. {
  1152. tinyrl->echo_enabled = BOOL_TRUE;
  1153. }
  1154. /*--------------------------------------------------------- */
  1155. void tinyrl_disable_echo(tinyrl_t * tinyrl, char echo_char)
  1156. {
  1157. tinyrl->echo_enabled = BOOL_FALSE;
  1158. tinyrl->echo_char = echo_char;
  1159. }
  1160. /*--------------------------------------------------------- */
  1161. void tinyrl__set_istream(tinyrl_t * tinyrl, FILE * istream)
  1162. {
  1163. tinyrl_vt100__set_istream(tinyrl->term, istream);
  1164. if (istream) {
  1165. int fd;
  1166. tinyrl->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  1167. /* Save terminal settings to restore on exit */
  1168. fd = fileno(istream);
  1169. tcgetattr(fd, &tinyrl->default_termios);
  1170. } else
  1171. tinyrl->isatty = BOOL_FALSE;
  1172. }
  1173. /*-------------------------------------------------------- */
  1174. bool_t tinyrl__get_isatty(const tinyrl_t * tinyrl)
  1175. {
  1176. return tinyrl->isatty;
  1177. }
  1178. /*-------------------------------------------------------- */
  1179. FILE *tinyrl__get_istream(const tinyrl_t * tinyrl)
  1180. {
  1181. return tinyrl_vt100__get_istream(tinyrl->term);
  1182. }
  1183. /*-------------------------------------------------------- */
  1184. FILE *tinyrl__get_ostream(const tinyrl_t * tinyrl)
  1185. {
  1186. return tinyrl_vt100__get_ostream(tinyrl->term);
  1187. }
  1188. /*-------------------------------------------------------- */
  1189. const char *tinyrl__get_prompt(const tinyrl_t * tinyrl)
  1190. {
  1191. return tinyrl->prompt;
  1192. }
  1193. /*-------------------------------------------------------- */
  1194. void tinyrl__set_prompt(tinyrl_t *tinyrl, const char *prompt)
  1195. {
  1196. if (tinyrl->prompt) {
  1197. lub_string_free(tinyrl->prompt);
  1198. tinyrl->prompt_size = 0;
  1199. tinyrl->prompt_len = 0;
  1200. }
  1201. tinyrl->prompt = lub_string_dup(prompt);
  1202. if (tinyrl->prompt) {
  1203. tinyrl->prompt_size = strlen(tinyrl->prompt);
  1204. tinyrl->prompt_len = utf8_nsyms(tinyrl, tinyrl->prompt,
  1205. tinyrl->prompt_size);
  1206. }
  1207. }
  1208. /*-------------------------------------------------------- */
  1209. bool_t tinyrl__get_utf8(const tinyrl_t * tinyrl)
  1210. {
  1211. return tinyrl->utf8;
  1212. }
  1213. /*-------------------------------------------------------- */
  1214. void tinyrl__set_utf8(tinyrl_t * tinyrl, bool_t utf8)
  1215. {
  1216. tinyrl->utf8 = utf8;
  1217. }
  1218. /*-------------------------------------------------------- */
  1219. void tinyrl__set_timeout(tinyrl_t *tinyrl, int timeout)
  1220. {
  1221. tinyrl_vt100__set_timeout(tinyrl->term, timeout);
  1222. }
  1223. /*-------------------------------------------------------- */
  1224. void tinyrl__set_timeout_fn(tinyrl_t *tinyrl,
  1225. tinyrl_timeout_fn_t *fn)
  1226. {
  1227. tinyrl->timeout_fn = fn;
  1228. }
  1229. /*-------------------------------------------------------- */
  1230. void tinyrl__set_keypress_fn(tinyrl_t *tinyrl,
  1231. tinyrl_keypress_fn_t *fn)
  1232. {
  1233. tinyrl->keypress_fn = fn;
  1234. }
  1235. /*-------------------------------------------------------- */
  1236. void tinyrl__set_hotkey_fn(tinyrl_t *tinyrl,
  1237. tinyrl_key_func_t *fn)
  1238. {
  1239. tinyrl->hotkey_fn = fn;
  1240. }
  1241. /*-------------------------------------------------------- */
  1242. bool_t tinyrl_is_quoting(const tinyrl_t * tinyrl)
  1243. {
  1244. bool_t result = BOOL_FALSE;
  1245. /* count the quotes upto the current insertion point */
  1246. unsigned int i = 0;
  1247. while (i < tinyrl->point) {
  1248. if (result && (tinyrl->line[i] == '\\')) {
  1249. i++;
  1250. if (i >= tinyrl->point)
  1251. break;
  1252. i++;
  1253. continue;
  1254. }
  1255. if (tinyrl->line[i++] == '"') {
  1256. result = result ? BOOL_FALSE : BOOL_TRUE;
  1257. }
  1258. }
  1259. return result;
  1260. }
  1261. /*-------------------------------------------------------- */
  1262. bool_t tinyrl_is_empty(const tinyrl_t *tinyrl)
  1263. {
  1264. return (tinyrl->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1265. }
  1266. /*--------------------------------------------------------- */
  1267. void tinyrl_limit_line_length(tinyrl_t * tinyrl, unsigned int length)
  1268. {
  1269. tinyrl->max_line_length = length;
  1270. }
  1271. /*--------------------------------------------------------- */
  1272. extern unsigned int tinyrl__get_width(const tinyrl_t *tinyrl)
  1273. {
  1274. return tinyrl_vt100__get_width(tinyrl->term);
  1275. }
  1276. /*--------------------------------------------------------- */
  1277. extern unsigned int tinyrl__get_height(const tinyrl_t *tinyrl)
  1278. {
  1279. return tinyrl_vt100__get_height(tinyrl->term);
  1280. }
  1281. /*----------------------------------------------------------*/
  1282. int tinyrl__save_history(const tinyrl_t *tinyrl, const char *fname)
  1283. {
  1284. return tinyrl_history_save(tinyrl->history, fname);
  1285. }
  1286. /*----------------------------------------------------------*/
  1287. int tinyrl__restore_history(tinyrl_t *tinyrl, const char *fname)
  1288. {
  1289. return tinyrl_history_restore(tinyrl->history, fname);
  1290. }
  1291. /*----------------------------------------------------------*/
  1292. void tinyrl__stifle_history(tinyrl_t *tinyrl, unsigned int stifle)
  1293. {
  1294. tinyrl_history_stifle(tinyrl->history, stifle);
  1295. }
  1296. /*--------------------------------------------------------- */
  1297. #endif