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