tinyrl.c 38 KB

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