tinyrl.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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. if (this->point) {
  313. unsigned end = this->point - 1;
  314. tinyrl_delete_text(this, 0, end);
  315. this->point = 0;
  316. }
  317. /* keep the compiler happy */
  318. key = key;
  319. this = this;
  320. return BOOL_TRUE;
  321. }/*-------------------------------------------------------- */
  322. static bool_t tinyrl_key_escape(tinyrl_t * this, int key)
  323. {
  324. bool_t result = BOOL_FALSE;
  325. switch (tinyrl_vt100_escape_decode(this->term)) {
  326. case tinyrl_vt100_CURSOR_UP:
  327. result = tinyrl_key_up(this, key);
  328. break;
  329. case tinyrl_vt100_CURSOR_DOWN:
  330. result = tinyrl_key_down(this, key);
  331. break;
  332. case tinyrl_vt100_CURSOR_LEFT:
  333. result = tinyrl_key_left(this, key);
  334. break;
  335. case tinyrl_vt100_CURSOR_RIGHT:
  336. result = tinyrl_key_right(this, key);
  337. break;
  338. case tinyrl_vt100_HOME:
  339. result = tinyrl_key_start_of_line(this,key);
  340. break;
  341. case tinyrl_vt100_END:
  342. result = tinyrl_key_end_of_line(this,key);
  343. break;
  344. case tinyrl_vt100_DELETE:
  345. result = tinyrl_key_delete(this,key);
  346. break;
  347. case tinyrl_vt100_INSERT:
  348. case tinyrl_vt100_PGDOWN:
  349. case tinyrl_vt100_PGUP:
  350. case tinyrl_vt100_UNKNOWN:
  351. break;
  352. }
  353. return result;
  354. }
  355. /*-------------------------------------------------------- */
  356. static bool_t tinyrl_key_tab(tinyrl_t * this, int key)
  357. {
  358. bool_t result = BOOL_FALSE;
  359. tinyrl_match_e status = tinyrl_complete_with_extensions(this);
  360. switch (status) {
  361. case TINYRL_COMPLETED_MATCH:
  362. case TINYRL_MATCH:
  363. /* everything is OK with the world... */
  364. result = tinyrl_insert_text(this, " ");
  365. break;
  366. case TINYRL_NO_MATCH:
  367. case TINYRL_MATCH_WITH_EXTENSIONS:
  368. case TINYRL_AMBIGUOUS:
  369. case TINYRL_COMPLETED_AMBIGUOUS:
  370. /* oops don't change the result and let the bell ring */
  371. break;
  372. }
  373. /* keep the compiler happy */
  374. key = key;
  375. return result;
  376. }
  377. /*-------------------------------------------------------- */
  378. static void tinyrl_fini(tinyrl_t * this)
  379. {
  380. /* delete the history session */
  381. tinyrl_history_delete(this->history);
  382. /* delete the terminal session */
  383. tinyrl_vt100_delete(this->term);
  384. /* free up any dynamic strings */
  385. lub_string_free(this->buffer);
  386. lub_string_free(this->kill_string);
  387. lub_string_free(this->last_buffer);
  388. lub_string_free(this->prompt);
  389. }
  390. /*-------------------------------------------------------- */
  391. static void
  392. tinyrl_init(tinyrl_t * this,
  393. FILE * istream, FILE * ostream,
  394. unsigned stifle, tinyrl_completion_func_t * complete_fn)
  395. {
  396. int i;
  397. for (i = 0; i < NUM_HANDLERS; i++) {
  398. this->handlers[i] = tinyrl_key_default;
  399. }
  400. /* default handlers */
  401. this->handlers[KEY_CR] = tinyrl_key_crlf;
  402. this->handlers[KEY_LF] = tinyrl_key_crlf;
  403. this->handlers[KEY_ETX] = tinyrl_key_interrupt;
  404. this->handlers[KEY_DEL] = tinyrl_key_backspace;
  405. this->handlers[KEY_BS] = tinyrl_key_backspace;
  406. this->handlers[KEY_EOT] = tinyrl_key_delete;
  407. this->handlers[KEY_ESC] = tinyrl_key_escape;
  408. this->handlers[KEY_FF] = tinyrl_key_clear_screen;
  409. this->handlers[KEY_NAK] = tinyrl_key_erase_line;
  410. this->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  411. this->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  412. this->handlers[KEY_VT] = tinyrl_key_kill;
  413. this->handlers[KEY_EM] = tinyrl_key_yank;
  414. this->handlers[KEY_HT] = tinyrl_key_tab;
  415. this->handlers[KEY_ETB] = tinyrl_key_backword;
  416. this->line = NULL;
  417. this->max_line_length = 0;
  418. this->prompt = NULL;
  419. this->prompt_size = 0;
  420. this->buffer = NULL;
  421. this->buffer_size = 0;
  422. this->done = BOOL_FALSE;
  423. this->completion_over = BOOL_FALSE;
  424. this->point = 0;
  425. this->end = 0;
  426. this->attempted_completion_function = complete_fn;
  427. this->timeout_fn = tinyrl_timeout_default;
  428. this->keypress_fn = NULL;
  429. this->hotkey_fn = NULL;
  430. this->state = 0;
  431. this->kill_string = NULL;
  432. this->echo_char = '\0';
  433. this->echo_enabled = BOOL_TRUE;
  434. this->last_buffer = NULL;
  435. this->last_point = 0;
  436. this->utf8 = BOOL_FALSE;
  437. /* create the vt100 terminal */
  438. this->term = tinyrl_vt100_new(NULL, ostream);
  439. tinyrl__set_istream(this, istream);
  440. this->last_width = tinyrl_vt100__get_width(this->term);
  441. /* create the history */
  442. this->history = tinyrl_history_new(stifle);
  443. }
  444. /*-------------------------------------------------------- */
  445. int tinyrl_printf(const tinyrl_t * this, const char *fmt, ...)
  446. {
  447. va_list args;
  448. int len;
  449. va_start(args, fmt);
  450. len = tinyrl_vt100_vprintf(this->term, fmt, args);
  451. va_end(args);
  452. return len;
  453. }
  454. /*-------------------------------------------------------- */
  455. void tinyrl_delete(tinyrl_t * this)
  456. {
  457. assert(this);
  458. if (this) {
  459. /* let the object tidy itself up */
  460. tinyrl_fini(this);
  461. /* release the memory associate with this instance */
  462. free(this);
  463. }
  464. }
  465. /*-------------------------------------------------------- */
  466. /*#####################################
  467. * EXPORTED INTERFACE
  468. *##################################### */
  469. /*----------------------------------------------------------------------- */
  470. int tinyrl_getchar(const tinyrl_t * this)
  471. {
  472. return tinyrl_vt100_getchar(this->term);
  473. }
  474. /*----------------------------------------------------------------------- */
  475. static void tinyrl_internal_print(const tinyrl_t * this, const char *text)
  476. {
  477. if (this->echo_enabled) {
  478. /* simply echo the line */
  479. tinyrl_vt100_printf(this->term, "%s", text);
  480. } else {
  481. /* replace the line with echo char if defined */
  482. if (this->echo_char) {
  483. unsigned i = strlen(text);
  484. while (i--) {
  485. tinyrl_vt100_printf(this->term, "%c",
  486. this->echo_char);
  487. }
  488. }
  489. }
  490. }
  491. /*----------------------------------------------------------------------- */
  492. static void tinyrl_internal_position(const tinyrl_t *this, int prompt_len,
  493. int line_len, unsigned int width)
  494. {
  495. int rows, cols;
  496. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  497. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  498. if (cols > 0)
  499. tinyrl_vt100_cursor_back(this->term, cols);
  500. else if (cols < 0)
  501. tinyrl_vt100_cursor_forward(this->term, -cols);
  502. if (rows > 0)
  503. tinyrl_vt100_cursor_up(this->term, rows);
  504. else if (rows < 0)
  505. tinyrl_vt100_cursor_down(this->term, -rows);
  506. }
  507. /*-------------------------------------------------------- */
  508. /* Jump to first free line after current multiline input */
  509. void tinyrl_multi_crlf(const tinyrl_t * this)
  510. {
  511. unsigned int line_size = strlen(this->last_buffer);
  512. unsigned int line_len = utf8_nsyms(this, this->last_buffer, line_size);
  513. unsigned int count = utf8_nsyms(this, this->last_buffer, this->last_point);
  514. tinyrl_internal_position(this, this->prompt_len + line_len,
  515. - (line_len - count), this->last_width);
  516. tinyrl_crlf(this);
  517. }
  518. /*----------------------------------------------------------------------- */
  519. void tinyrl_redisplay(tinyrl_t * this)
  520. {
  521. unsigned int line_size = strlen(this->line);
  522. unsigned int line_len = utf8_nsyms(this, this->line, line_size);
  523. unsigned int width = tinyrl_vt100__get_width(this->term);
  524. unsigned int count, eq_chars = 0;
  525. int cols;
  526. /* Prepare print position */
  527. if (this->last_buffer && (width == this->last_width)) {
  528. unsigned int eq_len = 0;
  529. /* If line and last line have the equal chars at begining */
  530. eq_chars = lub_string_equal_part(this->line, this->last_buffer,
  531. this->utf8);
  532. eq_len = utf8_nsyms(this, this->last_buffer, eq_chars);
  533. count = utf8_nsyms(this, this->last_buffer, this->last_point);
  534. tinyrl_internal_position(this, this->prompt_len + eq_len,
  535. count - eq_len, width);
  536. } else {
  537. /* Prepare to resize */
  538. if (width != this->last_width) {
  539. tinyrl_vt100_next_line(this->term);
  540. tinyrl_vt100_erase_down(this->term);
  541. }
  542. tinyrl_vt100_printf(this->term, "%s", this->prompt);
  543. }
  544. /* Print current line */
  545. tinyrl_internal_print(this, this->line + eq_chars);
  546. cols = (this->prompt_len + line_len) % width;
  547. if (!cols && (line_size - eq_chars))
  548. tinyrl_vt100_next_line(this->term);
  549. tinyrl_vt100_erase_down(this->term);
  550. /* Move the cursor to the insertion point */
  551. if (this->point < line_size) {
  552. unsigned int pre_len = utf8_nsyms(this,
  553. this->line, this->point);
  554. count = utf8_nsyms(this, this->line + this->point,
  555. line_size - this->point);
  556. tinyrl_internal_position(this, this->prompt_len + pre_len,
  557. count, width);
  558. }
  559. /* Update the display */
  560. (void)tinyrl_vt100_oflush(this->term);
  561. /* Save the last line buffer */
  562. lub_string_free(this->last_buffer);
  563. this->last_buffer = lub_string_dup(this->line);
  564. this->last_point = this->point;
  565. this->last_width = width;
  566. }
  567. /*----------------------------------------------------------------------- */
  568. tinyrl_t *tinyrl_new(FILE * istream, FILE * ostream,
  569. unsigned stifle, tinyrl_completion_func_t * complete_fn)
  570. {
  571. tinyrl_t *this = NULL;
  572. this = malloc(sizeof(tinyrl_t));
  573. if (this)
  574. tinyrl_init(this, istream, ostream, stifle, complete_fn);
  575. return this;
  576. }
  577. /*----------------------------------------------------------------------- */
  578. static char *internal_insertline(tinyrl_t * this, char *buffer)
  579. {
  580. char *p;
  581. char *s = buffer;
  582. /* strip any spurious '\r' or '\n' */
  583. if ((p = strchr(buffer, '\r')))
  584. *p = '\0';
  585. if ((p = strchr(buffer, '\n')))
  586. *p = '\0';
  587. /* skip any whitespace at the beginning of the line */
  588. if (0 == this->point) {
  589. while (*s && isspace(*s))
  590. s++;
  591. }
  592. if (*s) {
  593. /* append this string to the input buffer */
  594. (void)tinyrl_insert_text(this, s);
  595. }
  596. /* echo the command to the output stream */
  597. tinyrl_redisplay(this);
  598. return s;
  599. }
  600. /*----------------------------------------------------------------------- */
  601. static char *internal_readline(tinyrl_t * this,
  602. void *context, const char *str)
  603. {
  604. FILE *istream = tinyrl_vt100__get_istream(this->term);
  605. char *result = NULL;
  606. int lerrno = 0;
  607. /* initialise for reading a line */
  608. this->done = BOOL_FALSE;
  609. this->point = 0;
  610. this->end = 0;
  611. this->buffer = lub_string_dup("");
  612. this->buffer_size = strlen(this->buffer);
  613. this->line = this->buffer;
  614. this->context = context;
  615. if (this->isatty && !str) {
  616. /* set the terminal into raw input mode */
  617. tty_set_raw_mode(this);
  618. tinyrl_reset_line_state(this);
  619. while (!this->done) {
  620. int key;
  621. /* get a key */
  622. key = tinyrl_getchar(this);
  623. /* has the input stream terminated? */
  624. if (key >= 0) { /* Real key pressed */
  625. /* Common callback for any key */
  626. if (this->keypress_fn)
  627. this->keypress_fn(this, key);
  628. /* Call the handler for this key */
  629. if (!this->handlers[key](this, key))
  630. tinyrl_ding(this);
  631. if (this->done) {
  632. /*
  633. * If the last character in the line (other than
  634. * the null) is a space remove it.
  635. */
  636. if (this->end &&
  637. isspace(this->line[this->end - 1]))
  638. tinyrl_delete_text(this,
  639. this->end - 1,
  640. this->end);
  641. } else {
  642. /* Update the display if the key
  643. is not first UTF8 byte */
  644. if (!(this->utf8 &&
  645. (UTF8_11 == (key & UTF8_MASK))))
  646. tinyrl_redisplay(this);
  647. }
  648. } else { /* Error || EOF || Timeout */
  649. if ((VT100_TIMEOUT == key) &&
  650. !this->timeout_fn(this))
  651. continue;
  652. /* time to finish the session */
  653. this->done = BOOL_TRUE;
  654. this->line = NULL;
  655. lerrno = ENOENT;
  656. }
  657. }
  658. /* restores the terminal mode */
  659. tty_restore_mode(this);
  660. } else {
  661. /* This is a non-interactive set of commands */
  662. char *s = NULL, buffer[80];
  663. size_t len = sizeof(buffer);
  664. char *tmp = NULL;
  665. /* manually reset the line state without redisplaying */
  666. lub_string_free(this->last_buffer);
  667. this->last_buffer = NULL;
  668. if (str) {
  669. tmp = lub_string_dup(str);
  670. s = internal_insertline(this, tmp);
  671. } else {
  672. while (istream && (sizeof(buffer) == len) &&
  673. (s = fgets(buffer, sizeof(buffer), istream))) {
  674. s = internal_insertline(this, buffer);
  675. len = strlen(buffer) + 1; /* account for the '\0' */
  676. }
  677. if (!s || ((this->line[0] == '\0') && feof(istream))) {
  678. /* time to finish the session */
  679. this->line = NULL;
  680. lerrno = ENOENT;
  681. }
  682. }
  683. /*
  684. * check against fgets returning null as either error or end of file.
  685. * This is a measure to stop potential task spin on encountering an
  686. * error from fgets.
  687. */
  688. if (this->line && !this->handlers[KEY_LF](this, KEY_LF)) {
  689. /* an issue has occured */
  690. this->line = NULL;
  691. lerrno = ENOEXEC;
  692. }
  693. if (str)
  694. lub_string_free(tmp);
  695. }
  696. /*
  697. * duplicate the string for return to the client
  698. * we have to duplicate as we may be referencing a
  699. * history entry or our internal buffer
  700. */
  701. result = this->line ? lub_string_dup(this->line) : NULL;
  702. /* free our internal buffer */
  703. free(this->buffer);
  704. this->buffer = NULL;
  705. if (!result)
  706. errno = lerrno; /* get saved errno */
  707. return result;
  708. }
  709. /*----------------------------------------------------------------------- */
  710. char *tinyrl_readline(tinyrl_t * this, void *context)
  711. {
  712. return internal_readline(this, context, NULL);
  713. }
  714. /*----------------------------------------------------------------------- */
  715. char *tinyrl_forceline(tinyrl_t * this, void *context, const char *line)
  716. {
  717. return internal_readline(this, context, line);
  718. }
  719. /*----------------------------------------------------------------------- */
  720. /*
  721. * Ensure that buffer has enough space to hold len characters,
  722. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  723. * if the line is successfully extended, BOOL_FALSE if not.
  724. */
  725. bool_t tinyrl_extend_line_buffer(tinyrl_t * this, unsigned len)
  726. {
  727. bool_t result = BOOL_TRUE;
  728. char *new_buffer;
  729. size_t new_len = len;
  730. if (this->buffer_size >= len)
  731. return result;
  732. /*
  733. * What we do depends on whether we are limited by
  734. * memory or a user imposed limit.
  735. */
  736. if (this->max_line_length == 0) {
  737. /* make sure we don't realloc too often */
  738. if (new_len < this->buffer_size + 10)
  739. new_len = this->buffer_size + 10;
  740. /* leave space for terminator */
  741. new_buffer = realloc(this->buffer, new_len + 1);
  742. if (!new_buffer) {
  743. tinyrl_ding(this);
  744. result = BOOL_FALSE;
  745. } else {
  746. this->buffer_size = new_len;
  747. this->line = this->buffer = new_buffer;
  748. }
  749. } else {
  750. if (new_len < this->max_line_length) {
  751. /* Just reallocate once to the max size */
  752. new_buffer = realloc(this->buffer,
  753. this->max_line_length);
  754. if (!new_buffer) {
  755. tinyrl_ding(this);
  756. result = BOOL_FALSE;
  757. } else {
  758. this->buffer_size =
  759. this->max_line_length - 1;
  760. this->line = this->buffer = new_buffer;
  761. }
  762. } else {
  763. tinyrl_ding(this);
  764. result = BOOL_FALSE;
  765. }
  766. }
  767. return result;
  768. }
  769. /*----------------------------------------------------------------------- */
  770. /*
  771. * Insert text into the line at the current cursor position.
  772. */
  773. bool_t tinyrl_insert_text(tinyrl_t * this, const char *text)
  774. {
  775. unsigned delta = strlen(text);
  776. /*
  777. * If the client wants to change the line ensure that the line and buffer
  778. * references are in sync
  779. */
  780. changed_line(this);
  781. if ((delta + this->end) > (this->buffer_size)) {
  782. /* extend the current buffer */
  783. if (BOOL_FALSE ==
  784. tinyrl_extend_line_buffer(this, this->end + delta))
  785. return BOOL_FALSE;
  786. }
  787. if (this->point < this->end) {
  788. /* move the current text to the right (including the terminator) */
  789. memmove(&this->buffer[this->point + delta],
  790. &this->buffer[this->point],
  791. (this->end - this->point) + 1);
  792. } else {
  793. /* terminate the string */
  794. this->buffer[this->end + delta] = '\0';
  795. }
  796. /* insert the new text */
  797. strncpy(&this->buffer[this->point], text, delta);
  798. /* now update the indexes */
  799. this->point += delta;
  800. this->end += delta;
  801. return BOOL_TRUE;
  802. }
  803. /*----------------------------------------------------------------------- */
  804. /*
  805. * A convenience function for displaying a list of strings in columnar
  806. * format on Readline's output stream. matches is the list of strings,
  807. * in argv format, such as a list of completion matches. len is the number
  808. * of strings in matches, and max is the length of the longest string in matches.
  809. * This function uses the setting of print-completions-horizontally to select
  810. * how the matches are displayed
  811. */
  812. void tinyrl_display_matches(const tinyrl_t *this,
  813. char *const *matches, unsigned int len, size_t max)
  814. {
  815. unsigned int width = tinyrl_vt100__get_width(this->term);
  816. unsigned int cols, rows;
  817. /* Find out column and rows number */
  818. if (max < width)
  819. cols = (width + 1) / (max + 1); /* allow for a space between words */
  820. else
  821. cols = 1;
  822. rows = len / cols + 1;
  823. assert(matches);
  824. if (matches) {
  825. unsigned int r, c;
  826. len--, matches++; /* skip the subtitution string */
  827. /* Print out a table of completions */
  828. for (r = 0; r < rows && len; r++) {
  829. for (c = 0; c < cols && len; c++) {
  830. const char *match = *matches++;
  831. len--;
  832. if ((c + 1) == cols) /* Last str in row */
  833. tinyrl_vt100_printf(this->term, "%s",
  834. match);
  835. else
  836. tinyrl_vt100_printf(this->term, "%-*s ",
  837. max, match);
  838. }
  839. tinyrl_crlf(this);
  840. }
  841. }
  842. }
  843. /*----------------------------------------------------------------------- */
  844. /*
  845. * Delete the text between start and end in the current line. (inclusive)
  846. * This adjusts the rl_point and rl_end indexes appropriately.
  847. */
  848. void tinyrl_delete_text(tinyrl_t * this, unsigned start, unsigned end)
  849. {
  850. unsigned delta;
  851. /*
  852. * If the client wants to change the line ensure that the line and buffer
  853. * references are in sync
  854. */
  855. changed_line(this);
  856. /* make sure we play it safe */
  857. if (start > end) {
  858. unsigned tmp = end;
  859. start = end;
  860. end = tmp;
  861. }
  862. if (end > this->end)
  863. end = this->end;
  864. delta = (end - start) + 1;
  865. /* move any text which is left */
  866. memmove(&this->buffer[start],
  867. &this->buffer[start + delta], this->end - end);
  868. /* now adjust the indexs */
  869. if (this->point >= start) {
  870. if (this->point > end) {
  871. /* move the insertion point back appropriately */
  872. this->point -= delta;
  873. } else {
  874. /* move the insertion point to the start */
  875. this->point = start;
  876. }
  877. }
  878. if (this->end > end)
  879. this->end -= delta;
  880. else
  881. this->end = start;
  882. /* put a terminator at the end of the buffer */
  883. this->buffer[this->end] = '\0';
  884. }
  885. /*----------------------------------------------------------------------- */
  886. bool_t tinyrl_bind_key(tinyrl_t * this, int key, tinyrl_key_func_t * fn)
  887. {
  888. bool_t result = BOOL_FALSE;
  889. if ((key >= 0) && (key < 256)) {
  890. /* set the key handling function */
  891. this->handlers[key] = fn;
  892. result = BOOL_TRUE;
  893. }
  894. return result;
  895. }
  896. /*-------------------------------------------------------- */
  897. /*
  898. * Returns an array of strings which is a list of completions for text.
  899. * If there are no completions, returns NULL. The first entry in the
  900. * returned array is the substitution for text. The remaining entries
  901. * are the possible completions. The array is terminated with a NULL pointer.
  902. *
  903. * entry_func is a function of two args, and returns a char *.
  904. * The first argument is text. The second is a state argument;
  905. * it is zero on the first call, and non-zero on subsequent calls.
  906. * entry_func returns a NULL pointer to the caller when there are no
  907. * more matches.
  908. */
  909. char **tinyrl_completion(tinyrl_t * this,
  910. const char *line, unsigned start, unsigned end,
  911. tinyrl_compentry_func_t * entry_func)
  912. {
  913. unsigned state = 0;
  914. size_t size = 1;
  915. unsigned offset = 1; /* need at least one entry for the substitution */
  916. char **matches = NULL;
  917. char *match;
  918. /* duplicate the string upto the insertion point */
  919. char *text = lub_string_dupn(line, end);
  920. /* now try and find possible completions */
  921. while ((match = entry_func(this, text, start, state++))) {
  922. if (size == offset) {
  923. /* resize the buffer if needed - the +1 is for the NULL terminator */
  924. size += 10;
  925. matches =
  926. realloc(matches, (sizeof(char *) * (size + 1)));
  927. }
  928. /* not much we can do... */
  929. if (!matches)
  930. break;
  931. matches[offset] = match;
  932. /*
  933. * augment the substitute string with this entry
  934. */
  935. if (1 == offset) {
  936. /* let's be optimistic */
  937. matches[0] = lub_string_dup(match);
  938. } else {
  939. char *p = matches[0];
  940. size_t match_len = strlen(p);
  941. /* identify the common prefix */
  942. while ((tolower(*p) == tolower(*match)) && match_len--) {
  943. p++, match++;
  944. }
  945. /* terminate the prefix string */
  946. *p = '\0';
  947. }
  948. offset++;
  949. }
  950. /* be a good memory citizen */
  951. lub_string_free(text);
  952. if (matches)
  953. matches[offset] = NULL;
  954. return matches;
  955. }
  956. /*-------------------------------------------------------- */
  957. void tinyrl_delete_matches(char **this)
  958. {
  959. char **matches = this;
  960. while (*matches) {
  961. /* release the memory for each contained string */
  962. free(*matches++);
  963. }
  964. /* release the memory for the array */
  965. free(this);
  966. }
  967. /*-------------------------------------------------------- */
  968. void tinyrl_crlf(const tinyrl_t * this)
  969. {
  970. tinyrl_vt100_printf(this->term, "\n");
  971. }
  972. /*-------------------------------------------------------- */
  973. /*
  974. * Ring the terminal bell, obeying the setting of bell-style.
  975. */
  976. void tinyrl_ding(const tinyrl_t * this)
  977. {
  978. tinyrl_vt100_ding(this->term);
  979. }
  980. /*-------------------------------------------------------- */
  981. void tinyrl_reset_line_state(tinyrl_t * this)
  982. {
  983. /* start from scratch */
  984. lub_string_free(this->last_buffer);
  985. this->last_buffer = NULL;
  986. tinyrl_redisplay(this);
  987. }
  988. /*-------------------------------------------------------- */
  989. void tinyrl_replace_line(tinyrl_t * this, const char *text, int clear_undo)
  990. {
  991. size_t new_len = strlen(text);
  992. /* ignored for now */
  993. clear_undo = clear_undo;
  994. /* ensure there is sufficient space */
  995. if (tinyrl_extend_line_buffer(this, new_len)) {
  996. /* overwrite the current contents of the buffer */
  997. strcpy(this->buffer, text);
  998. /* set the insert point and end point */
  999. this->point = this->end = new_len;
  1000. }
  1001. tinyrl_redisplay(this);
  1002. }
  1003. /*-------------------------------------------------------- */
  1004. static tinyrl_match_e
  1005. tinyrl_do_complete(tinyrl_t * this, bool_t with_extensions)
  1006. {
  1007. tinyrl_match_e result = TINYRL_NO_MATCH;
  1008. char **matches = NULL;
  1009. unsigned start, end;
  1010. bool_t completion = BOOL_FALSE;
  1011. bool_t prefix = BOOL_FALSE;
  1012. int i = 0;
  1013. /* find the start and end of the current word */
  1014. start = end = this->point;
  1015. while (start && !isspace(this->line[start - 1]))
  1016. start--;
  1017. if (this->attempted_completion_function) {
  1018. this->completion_over = BOOL_FALSE;
  1019. this->completion_error_over = BOOL_FALSE;
  1020. /* try and complete the current line buffer */
  1021. matches = this->attempted_completion_function(this,
  1022. this->line, start, end);
  1023. }
  1024. if (!matches && (BOOL_FALSE == this->completion_over)) {
  1025. /* insert default completion call here... */
  1026. }
  1027. if (!matches)
  1028. return result;
  1029. /* identify and insert a common prefix if there is one */
  1030. if (0 != strncmp(matches[0], &this->line[start],
  1031. strlen(matches[0]))) {
  1032. /*
  1033. * delete the original text not including
  1034. * the current insertion point character
  1035. */
  1036. if (this->end != end)
  1037. end--;
  1038. tinyrl_delete_text(this, start, end);
  1039. if (BOOL_FALSE == tinyrl_insert_text(this, matches[0]))
  1040. return TINYRL_NO_MATCH;
  1041. completion = BOOL_TRUE;
  1042. }
  1043. for (i = 1; matches[i]; i++) {
  1044. /* this is just a prefix string */
  1045. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  1046. prefix = BOOL_TRUE;
  1047. }
  1048. /* is there more than one completion? */
  1049. if (matches[2]) {
  1050. char **tmp = matches;
  1051. unsigned max, len;
  1052. max = len = 0;
  1053. while (*tmp) {
  1054. size_t size = strlen(*tmp++);
  1055. len++;
  1056. if (size > max)
  1057. max = size;
  1058. }
  1059. if (completion)
  1060. result = TINYRL_COMPLETED_AMBIGUOUS;
  1061. else if (prefix)
  1062. result = TINYRL_MATCH_WITH_EXTENSIONS;
  1063. else
  1064. result = TINYRL_AMBIGUOUS;
  1065. if (with_extensions || !prefix) {
  1066. /* Either we always want to show extensions or
  1067. * we haven't been able to complete the current line
  1068. * and there is just a prefix, so let the user see the options
  1069. */
  1070. tinyrl_crlf(this);
  1071. tinyrl_display_matches(this, matches, len, max);
  1072. tinyrl_reset_line_state(this);
  1073. }
  1074. } else {
  1075. result = completion ?
  1076. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  1077. }
  1078. /* free the memory */
  1079. tinyrl_delete_matches(matches);
  1080. /* redisplay the line */
  1081. tinyrl_redisplay(this);
  1082. return result;
  1083. }
  1084. /*-------------------------------------------------------- */
  1085. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * this)
  1086. {
  1087. return tinyrl_do_complete(this, BOOL_TRUE);
  1088. }
  1089. /*-------------------------------------------------------- */
  1090. tinyrl_match_e tinyrl_complete(tinyrl_t * this)
  1091. {
  1092. return tinyrl_do_complete(this, BOOL_FALSE);
  1093. }
  1094. /*-------------------------------------------------------- */
  1095. void *tinyrl__get_context(const tinyrl_t * this)
  1096. {
  1097. return this->context;
  1098. }
  1099. /*--------------------------------------------------------- */
  1100. const char *tinyrl__get_line(const tinyrl_t * this)
  1101. {
  1102. return this->line;
  1103. }
  1104. /*--------------------------------------------------------- */
  1105. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * this)
  1106. {
  1107. return this->history;
  1108. }
  1109. /*--------------------------------------------------------- */
  1110. void tinyrl_completion_over(tinyrl_t * this)
  1111. {
  1112. this->completion_over = BOOL_TRUE;
  1113. }
  1114. /*--------------------------------------------------------- */
  1115. void tinyrl_completion_error_over(tinyrl_t * this)
  1116. {
  1117. this->completion_error_over = BOOL_TRUE;
  1118. }
  1119. /*--------------------------------------------------------- */
  1120. bool_t tinyrl_is_completion_error_over(const tinyrl_t * this)
  1121. {
  1122. return this->completion_error_over;
  1123. }
  1124. /*--------------------------------------------------------- */
  1125. void tinyrl_done(tinyrl_t * this)
  1126. {
  1127. this->done = BOOL_TRUE;
  1128. }
  1129. /*--------------------------------------------------------- */
  1130. void tinyrl_enable_echo(tinyrl_t * this)
  1131. {
  1132. this->echo_enabled = BOOL_TRUE;
  1133. }
  1134. /*--------------------------------------------------------- */
  1135. void tinyrl_disable_echo(tinyrl_t * this, char echo_char)
  1136. {
  1137. this->echo_enabled = BOOL_FALSE;
  1138. this->echo_char = echo_char;
  1139. }
  1140. /*--------------------------------------------------------- */
  1141. void tinyrl__set_istream(tinyrl_t * this, FILE * istream)
  1142. {
  1143. tinyrl_vt100__set_istream(this->term, istream);
  1144. if (istream) {
  1145. int fd;
  1146. this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  1147. /* Save terminal settings to restore on exit */
  1148. fd = fileno(istream);
  1149. tcgetattr(fd, &this->default_termios);
  1150. } else
  1151. this->isatty = BOOL_FALSE;
  1152. }
  1153. /*-------------------------------------------------------- */
  1154. bool_t tinyrl__get_isatty(const tinyrl_t * this)
  1155. {
  1156. return this->isatty;
  1157. }
  1158. /*-------------------------------------------------------- */
  1159. FILE *tinyrl__get_istream(const tinyrl_t * this)
  1160. {
  1161. return tinyrl_vt100__get_istream(this->term);
  1162. }
  1163. /*-------------------------------------------------------- */
  1164. FILE *tinyrl__get_ostream(const tinyrl_t * this)
  1165. {
  1166. return tinyrl_vt100__get_ostream(this->term);
  1167. }
  1168. /*-------------------------------------------------------- */
  1169. const char *tinyrl__get_prompt(const tinyrl_t * this)
  1170. {
  1171. return this->prompt;
  1172. }
  1173. /*-------------------------------------------------------- */
  1174. void tinyrl__set_prompt(tinyrl_t *this, const char *prompt)
  1175. {
  1176. if (this->prompt) {
  1177. lub_string_free(this->prompt);
  1178. this->prompt_size = 0;
  1179. this->prompt_len = 0;
  1180. }
  1181. this->prompt = lub_string_dup(prompt);
  1182. if (this->prompt) {
  1183. this->prompt_size = strlen(this->prompt);
  1184. this->prompt_len = utf8_nsyms(this, this->prompt,
  1185. this->prompt_size);
  1186. }
  1187. }
  1188. /*-------------------------------------------------------- */
  1189. bool_t tinyrl__get_utf8(const tinyrl_t * this)
  1190. {
  1191. return this->utf8;
  1192. }
  1193. /*-------------------------------------------------------- */
  1194. void tinyrl__set_utf8(tinyrl_t * this, bool_t utf8)
  1195. {
  1196. this->utf8 = utf8;
  1197. }
  1198. /*-------------------------------------------------------- */
  1199. void tinyrl__set_timeout(tinyrl_t *this, int timeout)
  1200. {
  1201. tinyrl_vt100__set_timeout(this->term, timeout);
  1202. }
  1203. /*-------------------------------------------------------- */
  1204. void tinyrl__set_timeout_fn(tinyrl_t *this,
  1205. tinyrl_timeout_fn_t *fn)
  1206. {
  1207. this->timeout_fn = fn;
  1208. }
  1209. /*-------------------------------------------------------- */
  1210. void tinyrl__set_keypress_fn(tinyrl_t *this,
  1211. tinyrl_keypress_fn_t *fn)
  1212. {
  1213. this->keypress_fn = fn;
  1214. }
  1215. /*-------------------------------------------------------- */
  1216. void tinyrl__set_hotkey_fn(tinyrl_t *this,
  1217. tinyrl_key_func_t *fn)
  1218. {
  1219. this->hotkey_fn = fn;
  1220. }
  1221. /*-------------------------------------------------------- */
  1222. bool_t tinyrl_is_quoting(const tinyrl_t * this)
  1223. {
  1224. bool_t result = BOOL_FALSE;
  1225. /* count the quotes upto the current insertion point */
  1226. unsigned i = 0;
  1227. while (i < this->point) {
  1228. if (result && (this->line[i] == '\\')) {
  1229. i++;
  1230. if (i >= this->point)
  1231. break;
  1232. i++;
  1233. continue;
  1234. }
  1235. if (this->line[i++] == '"') {
  1236. result = result ? BOOL_FALSE : BOOL_TRUE;
  1237. }
  1238. }
  1239. return result;
  1240. }
  1241. /*-------------------------------------------------------- */
  1242. bool_t tinyrl_is_empty(const tinyrl_t *this)
  1243. {
  1244. return (this->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1245. }
  1246. /*--------------------------------------------------------- */
  1247. void tinyrl_limit_line_length(tinyrl_t * this, unsigned length)
  1248. {
  1249. this->max_line_length = length;
  1250. }
  1251. /*--------------------------------------------------------- */
  1252. extern unsigned tinyrl__get_width(const tinyrl_t *this)
  1253. {
  1254. return tinyrl_vt100__get_width(this->term);
  1255. }
  1256. /*--------------------------------------------------------- */
  1257. extern unsigned tinyrl__get_height(const tinyrl_t *this)
  1258. {
  1259. return tinyrl_vt100__get_height(this->term);
  1260. }
  1261. /*----------------------------------------------------------*/
  1262. int tinyrl__save_history(const tinyrl_t *this, const char *fname)
  1263. {
  1264. return tinyrl_history_save(this->history, fname);
  1265. }
  1266. /*----------------------------------------------------------*/
  1267. int tinyrl__restore_history(tinyrl_t *this, const char *fname)
  1268. {
  1269. return tinyrl_history_restore(this->history, fname);
  1270. }
  1271. /*----------------------------------------------------------*/
  1272. void tinyrl__stifle_history(tinyrl_t *this, unsigned int stifle)
  1273. {
  1274. tinyrl_history_stifle(this->history, stifle);
  1275. }
  1276. /*--------------------------------------------------------- */