tinyrl.c 37 KB

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