1
0

tinyrl.c 38 KB

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