tinyrl.c 38 KB

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