tinyrl.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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. /* set the terminal into raw input mode */
  627. tty_set_raw_mode(this);
  628. tinyrl_reset_line_state(this);
  629. while (!this->done) {
  630. int key;
  631. /* get a key */
  632. key = tinyrl_getchar(this);
  633. /* has the input stream terminated? */
  634. if (key >= 0) { /* Real key pressed */
  635. /* Common callback for any key */
  636. if (this->keypress_fn)
  637. this->keypress_fn(this, key);
  638. /* Call the handler for this key */
  639. if (!this->handlers[key](this, key))
  640. tinyrl_ding(this);
  641. if (this->done) {
  642. /*
  643. * If the last character in the line (other than
  644. * the null) is a space remove it.
  645. */
  646. if (this->end &&
  647. isspace(this->line[this->end - 1]))
  648. tinyrl_delete_text(this,
  649. this->end - 1,
  650. this->end);
  651. } else {
  652. /* Update the display if the key
  653. is not first UTF8 byte */
  654. if (!(this->utf8 &&
  655. (UTF8_11 == (key & UTF8_MASK))))
  656. tinyrl_redisplay(this);
  657. }
  658. } else { /* Error || EOF || Timeout */
  659. if ((VT100_TIMEOUT == key) &&
  660. !this->timeout_fn(this))
  661. continue;
  662. /* time to finish the session */
  663. this->done = BOOL_TRUE;
  664. this->line = NULL;
  665. lerrno = ENOENT;
  666. }
  667. }
  668. /* restores the terminal mode */
  669. tty_restore_mode(this);
  670. } else {
  671. /* This is a non-interactive set of commands */
  672. char *s = NULL, buffer[80];
  673. size_t len = sizeof(buffer);
  674. char *tmp = NULL;
  675. /* manually reset the line state without redisplaying */
  676. lub_string_free(this->last_buffer);
  677. this->last_buffer = NULL;
  678. if (str) {
  679. tmp = lub_string_dup(str);
  680. s = internal_insertline(this, tmp);
  681. } else {
  682. while (istream && (sizeof(buffer) == len) &&
  683. (s = fgets(buffer, sizeof(buffer), istream))) {
  684. s = internal_insertline(this, buffer);
  685. len = strlen(buffer) + 1; /* account for the '\0' */
  686. }
  687. if (!s || ((this->line[0] == '\0') && feof(istream))) {
  688. /* time to finish the session */
  689. this->line = NULL;
  690. lerrno = ENOENT;
  691. }
  692. }
  693. /*
  694. * check against fgets returning null as either error or end of file.
  695. * This is a measure to stop potential task spin on encountering an
  696. * error from fgets.
  697. */
  698. if (this->line && !this->handlers[KEY_LF](this, KEY_LF)) {
  699. /* an issue has occured */
  700. this->line = NULL;
  701. lerrno = ENOEXEC;
  702. }
  703. if (str)
  704. lub_string_free(tmp);
  705. }
  706. /*
  707. * duplicate the string for return to the client
  708. * we have to duplicate as we may be referencing a
  709. * history entry or our internal buffer
  710. */
  711. result = this->line ? lub_string_dup(this->line) : NULL;
  712. /* free our internal buffer */
  713. free(this->buffer);
  714. this->buffer = NULL;
  715. if (!result)
  716. errno = lerrno; /* get saved errno */
  717. return result;
  718. }
  719. /*----------------------------------------------------------------------- */
  720. char *tinyrl_readline(tinyrl_t * this, void *context)
  721. {
  722. return internal_readline(this, context, NULL);
  723. }
  724. /*----------------------------------------------------------------------- */
  725. char *tinyrl_forceline(tinyrl_t * this, void *context, const char *line)
  726. {
  727. return internal_readline(this, context, line);
  728. }
  729. /*----------------------------------------------------------------------- */
  730. /*
  731. * Ensure that buffer has enough space to hold len characters,
  732. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  733. * if the line is successfully extended, BOOL_FALSE if not.
  734. */
  735. bool_t tinyrl_extend_line_buffer(tinyrl_t * this, unsigned len)
  736. {
  737. bool_t result = BOOL_TRUE;
  738. char *new_buffer;
  739. size_t new_len = len;
  740. if (this->buffer_size >= len)
  741. return result;
  742. /*
  743. * What we do depends on whether we are limited by
  744. * memory or a user imposed limit.
  745. */
  746. if (this->max_line_length == 0) {
  747. /* make sure we don't realloc too often */
  748. if (new_len < this->buffer_size + 10)
  749. new_len = this->buffer_size + 10;
  750. /* leave space for terminator */
  751. new_buffer = realloc(this->buffer, new_len + 1);
  752. if (!new_buffer) {
  753. tinyrl_ding(this);
  754. result = BOOL_FALSE;
  755. } else {
  756. this->buffer_size = new_len;
  757. this->line = this->buffer = new_buffer;
  758. }
  759. } else {
  760. if (new_len < this->max_line_length) {
  761. /* Just reallocate once to the max size */
  762. new_buffer = realloc(this->buffer,
  763. this->max_line_length);
  764. if (!new_buffer) {
  765. tinyrl_ding(this);
  766. result = BOOL_FALSE;
  767. } else {
  768. this->buffer_size =
  769. this->max_line_length - 1;
  770. this->line = this->buffer = new_buffer;
  771. }
  772. } else {
  773. tinyrl_ding(this);
  774. result = BOOL_FALSE;
  775. }
  776. }
  777. return result;
  778. }
  779. /*----------------------------------------------------------------------- */
  780. /*
  781. * Insert text into the line at the current cursor position.
  782. */
  783. bool_t tinyrl_insert_text(tinyrl_t * this, const char *text)
  784. {
  785. unsigned delta = strlen(text);
  786. /*
  787. * If the client wants to change the line ensure that the line and buffer
  788. * references are in sync
  789. */
  790. changed_line(this);
  791. if ((delta + this->end) > (this->buffer_size)) {
  792. /* extend the current buffer */
  793. if (BOOL_FALSE ==
  794. tinyrl_extend_line_buffer(this, this->end + delta))
  795. return BOOL_FALSE;
  796. }
  797. if (this->point < this->end) {
  798. /* move the current text to the right (including the terminator) */
  799. memmove(&this->buffer[this->point + delta],
  800. &this->buffer[this->point],
  801. (this->end - this->point) + 1);
  802. } else {
  803. /* terminate the string */
  804. this->buffer[this->end + delta] = '\0';
  805. }
  806. /* insert the new text */
  807. strncpy(&this->buffer[this->point], text, delta);
  808. /* now update the indexes */
  809. this->point += delta;
  810. this->end += delta;
  811. return BOOL_TRUE;
  812. }
  813. /*----------------------------------------------------------------------- */
  814. /*
  815. * A convenience function for displaying a list of strings in columnar
  816. * format on Readline's output stream. matches is the list of strings,
  817. * in argv format, such as a list of completion matches. len is the number
  818. * of strings in matches, and max is the length of the longest string in matches.
  819. * This function uses the setting of print-completions-horizontally to select
  820. * how the matches are displayed
  821. */
  822. void tinyrl_display_matches(const tinyrl_t *this,
  823. char *const *matches, unsigned int len, size_t max)
  824. {
  825. unsigned int width = tinyrl_vt100__get_width(this->term);
  826. unsigned int cols, rows;
  827. /* Find out column and rows number */
  828. if (max < width)
  829. cols = (width + 1) / (max + 1); /* allow for a space between words */
  830. else
  831. cols = 1;
  832. rows = len / cols + 1;
  833. assert(matches);
  834. if (matches) {
  835. unsigned int r, c;
  836. len--, matches++; /* skip the subtitution string */
  837. /* Print out a table of completions */
  838. for (r = 0; r < rows && len; r++) {
  839. for (c = 0; c < cols && len; c++) {
  840. const char *match = *matches++;
  841. len--;
  842. if ((c + 1) == cols) /* Last str in row */
  843. tinyrl_vt100_printf(this->term, "%s",
  844. match);
  845. else
  846. tinyrl_vt100_printf(this->term, "%-*s ",
  847. max, match);
  848. }
  849. tinyrl_crlf(this);
  850. }
  851. }
  852. }
  853. /*----------------------------------------------------------------------- */
  854. /*
  855. * Delete the text between start and end in the current line. (inclusive)
  856. * This adjusts the rl_point and rl_end indexes appropriately.
  857. */
  858. void tinyrl_delete_text(tinyrl_t * this, unsigned start, unsigned end)
  859. {
  860. unsigned delta;
  861. /*
  862. * If the client wants to change the line ensure that the line and buffer
  863. * references are in sync
  864. */
  865. changed_line(this);
  866. /* make sure we play it safe */
  867. if (start > end) {
  868. unsigned tmp = end;
  869. start = end;
  870. end = tmp;
  871. }
  872. if (end > this->end)
  873. end = this->end;
  874. delta = (end - start) + 1;
  875. /* move any text which is left */
  876. memmove(&this->buffer[start],
  877. &this->buffer[start + delta], this->end - end);
  878. /* now adjust the indexs */
  879. if (this->point >= start) {
  880. if (this->point > end) {
  881. /* move the insertion point back appropriately */
  882. this->point -= delta;
  883. } else {
  884. /* move the insertion point to the start */
  885. this->point = start;
  886. }
  887. }
  888. if (this->end > end)
  889. this->end -= delta;
  890. else
  891. this->end = start;
  892. /* put a terminator at the end of the buffer */
  893. this->buffer[this->end] = '\0';
  894. }
  895. /*----------------------------------------------------------------------- */
  896. bool_t tinyrl_bind_key(tinyrl_t * this, int key, tinyrl_key_func_t * fn)
  897. {
  898. bool_t result = BOOL_FALSE;
  899. if ((key >= 0) && (key < 256)) {
  900. /* set the key handling function */
  901. this->handlers[key] = fn;
  902. result = BOOL_TRUE;
  903. }
  904. return result;
  905. }
  906. /*-------------------------------------------------------- */
  907. /*
  908. * Returns an array of strings which is a list of completions for text.
  909. * If there are no completions, returns NULL. The first entry in the
  910. * returned array is the substitution for text. The remaining entries
  911. * are the possible completions. The array is terminated with a NULL pointer.
  912. *
  913. * entry_func is a function of two args, and returns a char *.
  914. * The first argument is text. The second is a state argument;
  915. * it is zero on the first call, and non-zero on subsequent calls.
  916. * entry_func returns a NULL pointer to the caller when there are no
  917. * more matches.
  918. */
  919. char **tinyrl_completion(tinyrl_t * this,
  920. const char *line, unsigned start, unsigned end,
  921. tinyrl_compentry_func_t * entry_func)
  922. {
  923. unsigned state = 0;
  924. size_t size = 1;
  925. unsigned offset = 1; /* need at least one entry for the substitution */
  926. char **matches = NULL;
  927. char *match;
  928. /* duplicate the string upto the insertion point */
  929. char *text = lub_string_dupn(line, end);
  930. /* now try and find possible completions */
  931. while ((match = entry_func(this, text, start, state++))) {
  932. if (size == offset) {
  933. /* resize the buffer if needed - the +1 is for the NULL terminator */
  934. size += 10;
  935. matches =
  936. realloc(matches, (sizeof(char *) * (size + 1)));
  937. }
  938. /* not much we can do... */
  939. if (!matches)
  940. break;
  941. matches[offset] = match;
  942. /*
  943. * augment the substitute string with this entry
  944. */
  945. if (1 == offset) {
  946. /* let's be optimistic */
  947. matches[0] = lub_string_dup(match);
  948. } else {
  949. char *p = matches[0];
  950. size_t match_len = strlen(p);
  951. /* identify the common prefix */
  952. while ((tolower(*p) == tolower(*match)) && match_len--) {
  953. p++, match++;
  954. }
  955. /* terminate the prefix string */
  956. *p = '\0';
  957. }
  958. offset++;
  959. }
  960. /* be a good memory citizen */
  961. lub_string_free(text);
  962. if (matches)
  963. matches[offset] = NULL;
  964. return matches;
  965. }
  966. /*-------------------------------------------------------- */
  967. void tinyrl_delete_matches(char **this)
  968. {
  969. char **matches = this;
  970. while (*matches) {
  971. /* release the memory for each contained string */
  972. free(*matches++);
  973. }
  974. /* release the memory for the array */
  975. free(this);
  976. }
  977. /*-------------------------------------------------------- */
  978. void tinyrl_crlf(const tinyrl_t * this)
  979. {
  980. tinyrl_vt100_printf(this->term, "\n");
  981. }
  982. /*-------------------------------------------------------- */
  983. /*
  984. * Ring the terminal bell, obeying the setting of bell-style.
  985. */
  986. void tinyrl_ding(const tinyrl_t * this)
  987. {
  988. tinyrl_vt100_ding(this->term);
  989. }
  990. /*-------------------------------------------------------- */
  991. void tinyrl_reset_line_state(tinyrl_t * this)
  992. {
  993. /* start from scratch */
  994. lub_string_free(this->last_buffer);
  995. this->last_buffer = NULL;
  996. tinyrl_redisplay(this);
  997. }
  998. /*-------------------------------------------------------- */
  999. void tinyrl_replace_line(tinyrl_t * this, const char *text, int clear_undo)
  1000. {
  1001. size_t new_len = strlen(text);
  1002. /* ignored for now */
  1003. clear_undo = clear_undo;
  1004. /* ensure there is sufficient space */
  1005. if (tinyrl_extend_line_buffer(this, new_len)) {
  1006. /* overwrite the current contents of the buffer */
  1007. strcpy(this->buffer, text);
  1008. /* set the insert point and end point */
  1009. this->point = this->end = new_len;
  1010. }
  1011. tinyrl_redisplay(this);
  1012. }
  1013. /*-------------------------------------------------------- */
  1014. static tinyrl_match_e
  1015. tinyrl_do_complete(tinyrl_t * this, bool_t with_extensions)
  1016. {
  1017. tinyrl_match_e result = TINYRL_NO_MATCH;
  1018. char **matches = NULL;
  1019. unsigned start, end;
  1020. bool_t completion = BOOL_FALSE;
  1021. bool_t prefix = BOOL_FALSE;
  1022. int i = 0;
  1023. /* find the start and end of the current word */
  1024. start = end = this->point;
  1025. while (start && !isspace(this->line[start - 1]))
  1026. start--;
  1027. if (this->attempted_completion_function) {
  1028. this->completion_over = BOOL_FALSE;
  1029. this->completion_error_over = BOOL_FALSE;
  1030. /* try and complete the current line buffer */
  1031. matches = this->attempted_completion_function(this,
  1032. this->line, start, end);
  1033. }
  1034. if (!matches && (BOOL_FALSE == this->completion_over)) {
  1035. /* insert default completion call here... */
  1036. }
  1037. if (!matches)
  1038. return result;
  1039. /* identify and insert a common prefix if there is one */
  1040. if (0 != strncmp(matches[0], &this->line[start],
  1041. strlen(matches[0]))) {
  1042. /*
  1043. * delete the original text not including
  1044. * the current insertion point character
  1045. */
  1046. if (this->end != end)
  1047. end--;
  1048. tinyrl_delete_text(this, start, end);
  1049. if (BOOL_FALSE == tinyrl_insert_text(this, matches[0]))
  1050. return TINYRL_NO_MATCH;
  1051. completion = BOOL_TRUE;
  1052. }
  1053. for (i = 1; matches[i]; i++) {
  1054. /* this is just a prefix string */
  1055. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  1056. prefix = BOOL_TRUE;
  1057. }
  1058. /* is there more than one completion? */
  1059. if (matches[2]) {
  1060. char **tmp = matches;
  1061. unsigned max, len;
  1062. max = len = 0;
  1063. while (*tmp) {
  1064. size_t size = strlen(*tmp++);
  1065. len++;
  1066. if (size > max)
  1067. max = size;
  1068. }
  1069. if (completion)
  1070. result = TINYRL_COMPLETED_AMBIGUOUS;
  1071. else if (prefix)
  1072. result = TINYRL_MATCH_WITH_EXTENSIONS;
  1073. else
  1074. result = TINYRL_AMBIGUOUS;
  1075. if (with_extensions || !prefix) {
  1076. /* Either we always want to show extensions or
  1077. * we haven't been able to complete the current line
  1078. * and there is just a prefix, so let the user see the options
  1079. */
  1080. tinyrl_crlf(this);
  1081. tinyrl_display_matches(this, matches, len, max);
  1082. tinyrl_reset_line_state(this);
  1083. }
  1084. } else {
  1085. result = completion ?
  1086. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  1087. }
  1088. /* free the memory */
  1089. tinyrl_delete_matches(matches);
  1090. /* redisplay the line */
  1091. tinyrl_redisplay(this);
  1092. return result;
  1093. }
  1094. /*-------------------------------------------------------- */
  1095. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * this)
  1096. {
  1097. return tinyrl_do_complete(this, BOOL_TRUE);
  1098. }
  1099. /*-------------------------------------------------------- */
  1100. tinyrl_match_e tinyrl_complete(tinyrl_t * this)
  1101. {
  1102. return tinyrl_do_complete(this, BOOL_FALSE);
  1103. }
  1104. /*-------------------------------------------------------- */
  1105. void *tinyrl__get_context(const tinyrl_t * this)
  1106. {
  1107. return this->context;
  1108. }
  1109. /*--------------------------------------------------------- */
  1110. const char *tinyrl__get_line(const tinyrl_t * this)
  1111. {
  1112. return this->line;
  1113. }
  1114. /*--------------------------------------------------------- */
  1115. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * this)
  1116. {
  1117. return this->history;
  1118. }
  1119. /*--------------------------------------------------------- */
  1120. void tinyrl_completion_over(tinyrl_t * this)
  1121. {
  1122. this->completion_over = BOOL_TRUE;
  1123. }
  1124. /*--------------------------------------------------------- */
  1125. void tinyrl_completion_error_over(tinyrl_t * this)
  1126. {
  1127. this->completion_error_over = BOOL_TRUE;
  1128. }
  1129. /*--------------------------------------------------------- */
  1130. bool_t tinyrl_is_completion_error_over(const tinyrl_t * this)
  1131. {
  1132. return this->completion_error_over;
  1133. }
  1134. /*--------------------------------------------------------- */
  1135. void tinyrl_done(tinyrl_t * this)
  1136. {
  1137. this->done = BOOL_TRUE;
  1138. }
  1139. /*--------------------------------------------------------- */
  1140. void tinyrl_enable_echo(tinyrl_t * this)
  1141. {
  1142. this->echo_enabled = BOOL_TRUE;
  1143. }
  1144. /*--------------------------------------------------------- */
  1145. void tinyrl_disable_echo(tinyrl_t * this, char echo_char)
  1146. {
  1147. this->echo_enabled = BOOL_FALSE;
  1148. this->echo_char = echo_char;
  1149. }
  1150. /*--------------------------------------------------------- */
  1151. void tinyrl__set_istream(tinyrl_t * this, FILE * istream)
  1152. {
  1153. tinyrl_vt100__set_istream(this->term, istream);
  1154. if (istream) {
  1155. int fd;
  1156. this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  1157. /* Save terminal settings to restore on exit */
  1158. fd = fileno(istream);
  1159. tcgetattr(fd, &this->default_termios);
  1160. } else
  1161. this->isatty = BOOL_FALSE;
  1162. }
  1163. /*-------------------------------------------------------- */
  1164. bool_t tinyrl__get_isatty(const tinyrl_t * this)
  1165. {
  1166. return this->isatty;
  1167. }
  1168. /*-------------------------------------------------------- */
  1169. FILE *tinyrl__get_istream(const tinyrl_t * this)
  1170. {
  1171. return tinyrl_vt100__get_istream(this->term);
  1172. }
  1173. /*-------------------------------------------------------- */
  1174. FILE *tinyrl__get_ostream(const tinyrl_t * this)
  1175. {
  1176. return tinyrl_vt100__get_ostream(this->term);
  1177. }
  1178. /*-------------------------------------------------------- */
  1179. const char *tinyrl__get_prompt(const tinyrl_t * this)
  1180. {
  1181. return this->prompt;
  1182. }
  1183. /*-------------------------------------------------------- */
  1184. void tinyrl__set_prompt(tinyrl_t *this, const char *prompt)
  1185. {
  1186. if (this->prompt) {
  1187. lub_string_free(this->prompt);
  1188. this->prompt_size = 0;
  1189. this->prompt_len = 0;
  1190. }
  1191. this->prompt = lub_string_dup(prompt);
  1192. if (this->prompt) {
  1193. this->prompt_size = strlen(this->prompt);
  1194. this->prompt_len = utf8_nsyms(this, this->prompt,
  1195. this->prompt_size);
  1196. }
  1197. }
  1198. /*-------------------------------------------------------- */
  1199. bool_t tinyrl__get_utf8(const tinyrl_t * this)
  1200. {
  1201. return this->utf8;
  1202. }
  1203. /*-------------------------------------------------------- */
  1204. void tinyrl__set_utf8(tinyrl_t * this, bool_t utf8)
  1205. {
  1206. this->utf8 = utf8;
  1207. }
  1208. /*-------------------------------------------------------- */
  1209. void tinyrl__set_timeout(tinyrl_t *this, int timeout)
  1210. {
  1211. tinyrl_vt100__set_timeout(this->term, timeout);
  1212. }
  1213. /*-------------------------------------------------------- */
  1214. void tinyrl__set_timeout_fn(tinyrl_t *this,
  1215. tinyrl_timeout_fn_t *fn)
  1216. {
  1217. this->timeout_fn = fn;
  1218. }
  1219. /*-------------------------------------------------------- */
  1220. void tinyrl__set_keypress_fn(tinyrl_t *this,
  1221. tinyrl_keypress_fn_t *fn)
  1222. {
  1223. this->keypress_fn = fn;
  1224. }
  1225. /*-------------------------------------------------------- */
  1226. void tinyrl__set_hotkey_fn(tinyrl_t *this,
  1227. tinyrl_key_func_t *fn)
  1228. {
  1229. this->hotkey_fn = fn;
  1230. }
  1231. /*-------------------------------------------------------- */
  1232. bool_t tinyrl_is_quoting(const tinyrl_t * this)
  1233. {
  1234. bool_t result = BOOL_FALSE;
  1235. /* count the quotes upto the current insertion point */
  1236. unsigned i = 0;
  1237. while (i < this->point) {
  1238. if (result && (this->line[i] == '\\')) {
  1239. i++;
  1240. if (i >= this->point)
  1241. break;
  1242. i++;
  1243. continue;
  1244. }
  1245. if (this->line[i++] == '"') {
  1246. result = result ? BOOL_FALSE : BOOL_TRUE;
  1247. }
  1248. }
  1249. return result;
  1250. }
  1251. /*-------------------------------------------------------- */
  1252. bool_t tinyrl_is_empty(const tinyrl_t *this)
  1253. {
  1254. return (this->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1255. }
  1256. /*--------------------------------------------------------- */
  1257. void tinyrl_limit_line_length(tinyrl_t * this, unsigned length)
  1258. {
  1259. this->max_line_length = length;
  1260. }
  1261. /*--------------------------------------------------------- */
  1262. extern unsigned tinyrl__get_width(const tinyrl_t *this)
  1263. {
  1264. return tinyrl_vt100__get_width(this->term);
  1265. }
  1266. /*--------------------------------------------------------- */
  1267. extern unsigned tinyrl__get_height(const tinyrl_t *this)
  1268. {
  1269. return tinyrl_vt100__get_height(this->term);
  1270. }
  1271. /*----------------------------------------------------------*/
  1272. int tinyrl__save_history(const tinyrl_t *this, const char *fname)
  1273. {
  1274. return tinyrl_history_save(this->history, fname);
  1275. }
  1276. /*----------------------------------------------------------*/
  1277. int tinyrl__restore_history(tinyrl_t *this, const char *fname)
  1278. {
  1279. return tinyrl_history_restore(this->history, fname);
  1280. }
  1281. /*----------------------------------------------------------*/
  1282. void tinyrl__stifle_history(tinyrl_t *this, unsigned int stifle)
  1283. {
  1284. tinyrl_history_stifle(this->history, stifle);
  1285. }
  1286. /*--------------------------------------------------------- */