tinyrl.c 36 KB

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