tinyrl.c 37 KB

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