tinyrl.c 39 KB

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