tinyrl.c 36 KB

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