1
0

tinyrl.c 38 KB

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