tinyrl.c 42 KB

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