tinyrl.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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. if (instream)
  421. this->isatty = isatty(fileno(instream)) ? BOOL_TRUE : BOOL_FALSE;
  422. else
  423. this->isatty = BOOL_FALSE;
  424. this->last_buffer = NULL;
  425. this->last_point = 0;
  426. /* create the vt100 terminal */
  427. this->term = tinyrl_vt100_new(instream,outstream);
  428. /* create the history */
  429. this->history = tinyrl_history_new(stifle);
  430. }
  431. /*-------------------------------------------------------- */
  432. int
  433. tinyrl_printf(const tinyrl_t *this,
  434. const char *fmt,
  435. ...)
  436. {
  437. va_list args;
  438. int len;
  439. va_start(args, fmt);
  440. len = tinyrl_vt100_vprintf(this->term, fmt, args);
  441. va_end(args);
  442. return len;
  443. }
  444. /*-------------------------------------------------------- */
  445. void
  446. tinyrl_delete(tinyrl_t *this)
  447. {
  448. assert(this);
  449. if(this)
  450. {
  451. /* let the object tidy itself up */
  452. tinyrl_fini(this);
  453. /* release the memory associate with this instance */
  454. free(this);
  455. }
  456. }
  457. /*-------------------------------------------------------- */
  458. /*#####################################
  459. * EXPORTED INTERFACE
  460. *##################################### */
  461. /*----------------------------------------------------------------------- */
  462. int
  463. tinyrl_getchar(const tinyrl_t *this)
  464. {
  465. return tinyrl_vt100_getchar(this->term);
  466. }
  467. /*----------------------------------------------------------------------- */
  468. static void
  469. tinyrl_internal_print(const tinyrl_t *this,
  470. const char *text)
  471. {
  472. if(BOOL_TRUE == this->echo_enabled)
  473. {
  474. /* simply echo the line */
  475. tinyrl_vt100_printf(this->term,"%s",text);
  476. }
  477. else
  478. {
  479. /* replace the line with echo char if defined */
  480. if(this->echo_char)
  481. {
  482. unsigned i = strlen(text);
  483. while(i--)
  484. {
  485. tinyrl_vt100_printf(this->term,"%c",this->echo_char);
  486. }
  487. }
  488. }
  489. }
  490. /*----------------------------------------------------------------------- */
  491. void
  492. tinyrl_redisplay(tinyrl_t *this)
  493. {
  494. int delta;
  495. unsigned line_len, last_line_len,count;
  496. line_len = strlen(this->line);
  497. last_line_len = (this->last_buffer ? strlen(this->last_buffer) : 0);
  498. do
  499. {
  500. if(this->last_buffer)
  501. {
  502. delta = (int)(line_len - last_line_len);
  503. if(delta > 0)
  504. {
  505. count = (unsigned)delta;
  506. /* is the current line simply an extension of the previous one? */
  507. if(0 == strncmp(this->line,this->last_buffer,last_line_len))
  508. {
  509. /* output the line accounting for the echo behaviour */
  510. tinyrl_internal_print(this,&this->line[line_len - count]);
  511. break;
  512. }
  513. }
  514. else if(delta < 0)
  515. {
  516. /* is the current line simply a deletion of some characters from the end? */
  517. if(0 == strncmp(this->line,this->last_buffer,line_len))
  518. {
  519. if(this->echo_enabled || this->echo_char)
  520. {
  521. int shift = (int)(this->last_point - this->point);
  522. /* just get the terminal to delete the characters */
  523. if(shift > 0)
  524. {
  525. count = (unsigned)shift;
  526. /* we've moved the cursor backwards */
  527. tinyrl_vt100_cursor_back(this->term,count);
  528. }
  529. else if(shift < 0)
  530. {
  531. count = (unsigned)-shift;
  532. /* we've moved the cursor forwards */
  533. tinyrl_vt100_cursor_forward(this->term,count);
  534. }
  535. /* now delete the characters */
  536. count = (unsigned)-delta;
  537. tinyrl_vt100_erase(this->term,count);
  538. }
  539. break;
  540. }
  541. }
  542. else
  543. {
  544. /* are the lines are the same content? */
  545. if(0 == strcmp(this->line,this->last_buffer))
  546. {
  547. if(this->echo_enabled || this->echo_char)
  548. {
  549. delta = (int)(this->point - this->last_point);
  550. if(delta > 0)
  551. {
  552. count = (unsigned)delta;
  553. /* move the point forwards */
  554. tinyrl_vt100_cursor_forward(this->term,count);
  555. }
  556. else if(delta < 0)
  557. {
  558. count = (unsigned)-delta;
  559. /* move the cursor backwards */
  560. tinyrl_vt100_cursor_back(this->term,count);
  561. }
  562. }
  563. /* done for now */
  564. break;
  565. }
  566. }
  567. }
  568. else
  569. {
  570. /* simply display the prompt and the line */
  571. tinyrl_vt100_printf(this->term,"%s",this->prompt);
  572. tinyrl_internal_print(this,this->line);
  573. if(this->point < line_len)
  574. {
  575. /* move the cursor to the insertion point */
  576. tinyrl_vt100_cursor_back(this->term,line_len-this->point);
  577. }
  578. break;
  579. }
  580. /*
  581. * to have got this far we must have edited the middle of a line.
  582. */
  583. if(this->last_point)
  584. {
  585. /* move to just after the prompt of the line */
  586. tinyrl_vt100_cursor_back(this->term,this->last_point);
  587. }
  588. /* erase the previous line */
  589. tinyrl_vt100_erase(this->term,strlen(this->last_buffer));
  590. /* output the line accounting for the echo behaviour */
  591. tinyrl_internal_print(this,this->line);
  592. delta = (int)(line_len - this->point);
  593. if(delta)
  594. {
  595. /* move the cursor back to the insertion point */
  596. tinyrl_vt100_cursor_back(this->term,
  597. (strlen(this->line) - this->point));
  598. }
  599. } /*lint -e717 */ while(0) /*lint +e717 */;
  600. /* update the display */
  601. (void)tinyrl_vt100_oflush(this->term);
  602. /* set up the last line buffer */
  603. lub_string_free(this->last_buffer);
  604. this->last_buffer = lub_string_dup(this->line);
  605. this->last_point = this->point;
  606. }
  607. /*----------------------------------------------------------------------- */
  608. tinyrl_t *
  609. tinyrl_new(FILE *instream,
  610. FILE *outstream,
  611. unsigned stifle,
  612. tinyrl_completion_func_t *complete_fn)
  613. {
  614. tinyrl_t *this=NULL;
  615. this = malloc(sizeof(tinyrl_t));
  616. if(NULL != this)
  617. {
  618. tinyrl_init(this,instream,outstream,stifle,complete_fn);
  619. }
  620. return this;
  621. }
  622. /*----------------------------------------------------------------------- */
  623. static char *
  624. internal_insertline(tinyrl_t *this,
  625. char *buffer)
  626. {
  627. char *p;
  628. char *s = buffer;
  629. /* strip any spurious '\r' or '\n' */
  630. p = strchr(buffer,'\r');
  631. if(NULL == p)
  632. {
  633. p = strchr(buffer,'\n');
  634. }
  635. if (NULL != p)
  636. {
  637. *p = '\0';
  638. }
  639. /* skip any whitespace at the beginning of the line */
  640. if(0 == this->point)
  641. {
  642. while(*s && isspace(*s))
  643. {
  644. s++;
  645. }
  646. }
  647. if(*s)
  648. {
  649. /* append this string to the input buffer */
  650. (void) tinyrl_insert_text(this,s);
  651. /* echo the command to the output stream */
  652. tinyrl_redisplay(this);
  653. }
  654. return s;
  655. }
  656. /*----------------------------------------------------------------------- */
  657. static char *
  658. internal_readline(tinyrl_t *this,
  659. const char *prompt,
  660. void *context,
  661. const char *str)
  662. {
  663. FILE *istream = tinyrl_vt100__get_istream(this->term);
  664. int crlf = 1; /* Enable crlf if result is NULL */
  665. /* initialise for reading a line */
  666. this->done = BOOL_FALSE;
  667. this->point = 0;
  668. this->end = 0;
  669. this->buffer = lub_string_dup("");
  670. this->buffer_size = strlen(this->buffer);
  671. this->line = this->buffer;
  672. this->prompt = prompt;
  673. this->prompt_size = strlen(prompt);
  674. this->context = context;
  675. if((BOOL_TRUE == this->isatty) && (!str))
  676. {
  677. /* set the terminal into raw input mode */
  678. tty_set_raw_mode(this);
  679. tinyrl_reset_line_state(this);
  680. while(!this->done)
  681. {
  682. int key;
  683. /* update the display */
  684. tinyrl_redisplay(this);
  685. /* get a key */
  686. key = tinyrl_getchar(this);
  687. /* has the input stream terminated? */
  688. if(EOF != key)
  689. {
  690. /* call the handler for this key */
  691. if(BOOL_FALSE == this->handlers[key](this,key))
  692. {
  693. /* an issue has occured */
  694. tinyrl_ding(this);
  695. }
  696. if (BOOL_TRUE == this->done)
  697. {
  698. /*
  699. * If the last character in the line (other than
  700. * the null) is a space remove it.
  701. */
  702. if (this->end && isspace(this->line[this->end-1]))
  703. {
  704. tinyrl_delete_text(this,this->end-1,this->end);
  705. }
  706. }
  707. }
  708. else
  709. {
  710. /* time to finish the session */
  711. this->done = BOOL_TRUE;
  712. this->line = NULL;
  713. }
  714. }
  715. /* restores the terminal mode */
  716. tty_restore_mode(this);
  717. }
  718. else
  719. {
  720. /* This is a non-interactive set of commands */
  721. char *s = 0, buffer[80];
  722. size_t len = sizeof(buffer);
  723. char *tmp = NULL;
  724. /* manually reset the line state without redisplaying */
  725. lub_string_free(this->last_buffer);
  726. this->last_buffer = NULL;
  727. if (str) {
  728. tmp = lub_string_dup(str);
  729. s = internal_insertline(this, tmp);
  730. } else {
  731. while((sizeof(buffer) == len) &&
  732. (s = fgets(buffer,sizeof(buffer),istream)))
  733. {
  734. s = internal_insertline(this, buffer);
  735. len = strlen(buffer) + 1; /* account for the '\0' */
  736. }
  737. if (s == NULL || (this->line[0] == '\0' && feof(istream)))
  738. {
  739. /* time to finish the session */
  740. this->line = NULL;
  741. crlf = 0;
  742. }
  743. }
  744. /*
  745. * check against fgets returning null as either error or end of file.
  746. * This is a measure to stop potential task spin on encountering an
  747. * error from fgets.
  748. */
  749. if (this->line)
  750. {
  751. if (this->line[0] == '\0')
  752. {
  753. tinyrl_reset_line_state(this);
  754. } /* call the handler for the newline key */
  755. else if(BOOL_FALSE == this->handlers[KEY_LF](this,KEY_LF))
  756. {
  757. /* an issue has occured */
  758. tinyrl_ding(this);
  759. this->line = NULL;
  760. }
  761. }
  762. if (str)
  763. lub_string_free(tmp);
  764. }
  765. /*
  766. * duplicate the string for return to the client
  767. * we have to duplicate as we may be referencing a
  768. * history entry or our internal buffer
  769. */
  770. {
  771. char *result = this->line ? lub_string_dup(this->line) : NULL;
  772. /* free our internal buffer */
  773. free(this->buffer);
  774. this->buffer = NULL;
  775. if(crlf && ((NULL == result) || ('\0' == *result)))
  776. {
  777. /* make sure we're not left on a prompt line */
  778. tinyrl_crlf(this);
  779. }
  780. return result;
  781. }
  782. }
  783. /*----------------------------------------------------------------------- */
  784. char *
  785. tinyrl_readline(tinyrl_t *this,
  786. const char *prompt,
  787. void *context)
  788. {
  789. return internal_readline(this, prompt, context, NULL);
  790. }
  791. /*----------------------------------------------------------------------- */
  792. char *
  793. tinyrl_forceline(tinyrl_t *this,
  794. const char *prompt,
  795. void *context,
  796. const char *line)
  797. {
  798. return internal_readline(this, prompt, context, line);
  799. }
  800. /*----------------------------------------------------------------------- */
  801. /*
  802. * Ensure that buffer has enough space to hold len characters,
  803. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  804. * if the line is successfully extended, BOOL_FALSE if not.
  805. */
  806. bool_t
  807. tinyrl_extend_line_buffer(tinyrl_t *this,
  808. unsigned len)
  809. {
  810. bool_t result = BOOL_TRUE;
  811. if(this->buffer_size < len)
  812. {
  813. char * new_buffer;
  814. size_t new_len = len;
  815. /*
  816. * What we do depends on whether we are limited by
  817. * memory or a user imposed limit.
  818. */
  819. if (this->max_line_length == 0)
  820. {
  821. if(new_len < this->buffer_size + 10)
  822. {
  823. /* make sure we don't realloc too often */
  824. new_len = this->buffer_size + 10;
  825. }
  826. /* leave space for terminator */
  827. new_buffer = realloc(this->buffer,new_len+1);
  828. if(NULL == new_buffer)
  829. {
  830. tinyrl_ding(this);
  831. result = BOOL_FALSE;
  832. }
  833. else
  834. {
  835. this->buffer_size = new_len;
  836. this->line = this->buffer = new_buffer;
  837. }
  838. }
  839. else
  840. {
  841. if(new_len < this->max_line_length)
  842. {
  843. /* Just reallocate once to the max size */
  844. new_buffer = realloc(this->buffer,this->max_line_length);
  845. if(NULL == new_buffer)
  846. {
  847. tinyrl_ding(this);
  848. result = BOOL_FALSE;
  849. }
  850. else
  851. {
  852. this->buffer_size = this->max_line_length - 1;
  853. this->line = this->buffer = new_buffer;
  854. }
  855. }
  856. else
  857. {
  858. tinyrl_ding(this);
  859. result = BOOL_FALSE;
  860. }
  861. }
  862. }
  863. return result;
  864. }
  865. /*----------------------------------------------------------------------- */
  866. /*
  867. * Insert text into the line at the current cursor position.
  868. */
  869. bool_t
  870. tinyrl_insert_text(tinyrl_t *this,
  871. const char *text)
  872. {
  873. unsigned delta = strlen(text);
  874. /*
  875. * If the client wants to change the line ensure that the line and buffer
  876. * references are in sync
  877. */
  878. changed_line(this);
  879. if((delta + this->end) > (this->buffer_size))
  880. {
  881. /* extend the current buffer */
  882. if (BOOL_FALSE == tinyrl_extend_line_buffer(this,this->end + delta))
  883. {
  884. return BOOL_FALSE;
  885. }
  886. }
  887. if(this->point < this->end)
  888. {
  889. /* move the current text to the right (including the terminator) */
  890. memmove(&this->buffer[this->point+delta],
  891. &this->buffer[this->point],
  892. (this->end - this->point) + 1);
  893. }
  894. else
  895. {
  896. /* terminate the string */
  897. this->buffer[this->end+delta] = '\0';
  898. }
  899. /* insert the new text */
  900. strncpy(&this->buffer[this->point],text,delta);
  901. /* now update the indexes */
  902. this->point += delta;
  903. this->end += delta;
  904. return BOOL_TRUE;
  905. }
  906. /*----------------------------------------------------------------------- */
  907. /*
  908. * A convenience function for displaying a list of strings in columnar
  909. * format on Readline's output stream. matches is the list of strings,
  910. * in argv format, such as a list of completion matches. len is the number
  911. * of strings in matches, and max is the length of the longest string in matches.
  912. * This function uses the setting of print-completions-horizontally to select
  913. * how the matches are displayed
  914. */
  915. void
  916. tinyrl_display_matches(const tinyrl_t *this,
  917. char *const *matches,
  918. unsigned len,
  919. size_t max)
  920. {
  921. unsigned r,c;
  922. unsigned width = tinyrl_vt100__get_width(this->term);
  923. unsigned cols = width/(max+1); /* allow for a space between words */
  924. unsigned rows = len/cols + 1;
  925. assert(matches);
  926. if(matches)
  927. {
  928. len--,matches++; /* skip the subtitution string */
  929. /* print out a table of completions */
  930. for(r=0; r<rows && len; r++)
  931. {
  932. for(c=0; c<cols && len; c++)
  933. {
  934. const char *match = *matches++;
  935. len--;
  936. tinyrl_vt100_printf(this->term,"%-*s ",max,match);
  937. }
  938. tinyrl_crlf(this);
  939. }
  940. }
  941. }
  942. /*----------------------------------------------------------------------- */
  943. /*
  944. * Delete the text between start and end in the current line. (inclusive)
  945. * This adjusts the rl_point and rl_end indexes appropriately.
  946. */
  947. void
  948. tinyrl_delete_text(tinyrl_t *this,
  949. unsigned start,
  950. unsigned end)
  951. {
  952. unsigned delta;
  953. /*
  954. * If the client wants to change the line ensure that the line and buffer
  955. * references are in sync
  956. */
  957. changed_line(this);
  958. /* make sure we play it safe */
  959. if(start > end)
  960. {
  961. unsigned tmp = end;
  962. start = end;
  963. end = tmp;
  964. }
  965. if(end > this->end)
  966. {
  967. end = this->end;
  968. }
  969. delta = (end - start) + 1;
  970. /* move any text which is left */
  971. memmove(&this->buffer[start],
  972. &this->buffer[start+delta],
  973. this->end - end);
  974. /* now adjust the indexs */
  975. if(this->point >= start)
  976. {
  977. if(this->point > end)
  978. {
  979. /* move the insertion point back appropriately */
  980. this->point -= delta;
  981. }
  982. else
  983. {
  984. /* move the insertion point to the start */
  985. this->point = start;
  986. }
  987. }
  988. if(this->end > end)
  989. {
  990. this->end -= delta;
  991. }
  992. else
  993. {
  994. this->end = start;
  995. }
  996. /* put a terminator at the end of the buffer */
  997. this->buffer[this->end] ='\0';
  998. }
  999. /*----------------------------------------------------------------------- */
  1000. bool_t
  1001. tinyrl_bind_key(tinyrl_t *this,
  1002. int key,
  1003. tinyrl_key_func_t *fn)
  1004. {
  1005. bool_t result = BOOL_FALSE;
  1006. if((key >= 0) && (key < 256))
  1007. {
  1008. /* set the key handling function */
  1009. this->handlers[key] = fn;
  1010. result = BOOL_TRUE;
  1011. }
  1012. return result;
  1013. }
  1014. /*-------------------------------------------------------- */
  1015. /*
  1016. * Returns an array of strings which is a list of completions for text.
  1017. * If there are no completions, returns NULL. The first entry in the
  1018. * returned array is the substitution for text. The remaining entries
  1019. * are the possible completions. The array is terminated with a NULL pointer.
  1020. *
  1021. * entry_func is a function of two args, and returns a char *.
  1022. * The first argument is text. The second is a state argument;
  1023. * it is zero on the first call, and non-zero on subsequent calls.
  1024. * entry_func returns a NULL pointer to the caller when there are no
  1025. * more matches.
  1026. */
  1027. char **
  1028. tinyrl_completion(tinyrl_t *this,
  1029. const char *line,
  1030. unsigned start,
  1031. unsigned end,
  1032. tinyrl_compentry_func_t *entry_func)
  1033. {
  1034. unsigned state = 0;
  1035. size_t size = 1;
  1036. unsigned offset = 1; /* need at least one entry for the substitution */
  1037. char **matches = NULL;
  1038. char *match;
  1039. /* duplicate the string upto the insertion point */
  1040. char *text = lub_string_dupn(line,end);
  1041. /* now try and find possible completions */
  1042. while( (match = entry_func(this,text,start,state++)) )
  1043. {
  1044. if(size == offset)
  1045. {
  1046. /* resize the buffer if needed - the +1 is for the NULL terminator */
  1047. size += 10;
  1048. matches = realloc(matches,(sizeof(char *) * (size + 1)));
  1049. }
  1050. if (NULL == matches)
  1051. {
  1052. /* not much we can do... */
  1053. break;
  1054. }
  1055. matches[offset] = match;
  1056. /*
  1057. * augment the substitute string with this entry
  1058. */
  1059. if(1 == offset)
  1060. {
  1061. /* let's be optimistic */
  1062. matches[0] = lub_string_dup(match);
  1063. }
  1064. else
  1065. {
  1066. char *p = matches[0];
  1067. size_t match_len = strlen(p);
  1068. /* identify the common prefix */
  1069. while((tolower(*p) == tolower(*match)) && match_len--)
  1070. {
  1071. p++,match++;
  1072. }
  1073. /* terminate the prefix string */
  1074. *p = '\0';
  1075. }
  1076. offset++;
  1077. }
  1078. /* be a good memory citizen */
  1079. lub_string_free(text);
  1080. if(matches)
  1081. {
  1082. matches[offset] = NULL;
  1083. }
  1084. return matches;
  1085. }
  1086. /*-------------------------------------------------------- */
  1087. void
  1088. tinyrl_delete_matches(char **this)
  1089. {
  1090. char **matches = this;
  1091. while(*matches)
  1092. {
  1093. /* release the memory for each contained string */
  1094. free(*matches++);
  1095. }
  1096. /* release the memory for the array */
  1097. free(this);
  1098. }
  1099. /*-------------------------------------------------------- */
  1100. void
  1101. tinyrl_crlf(const tinyrl_t *this)
  1102. {
  1103. tinyrl_vt100_printf(this->term,"\n");
  1104. }
  1105. /*-------------------------------------------------------- */
  1106. /*
  1107. * Ring the terminal bell, obeying the setting of bell-style.
  1108. */
  1109. void
  1110. tinyrl_ding(const tinyrl_t *this)
  1111. {
  1112. tinyrl_vt100_ding(this->term);
  1113. }
  1114. /*-------------------------------------------------------- */
  1115. void
  1116. tinyrl_reset_line_state(tinyrl_t *this)
  1117. {
  1118. /* start from scratch */
  1119. lub_string_free(this->last_buffer);
  1120. this->last_buffer = NULL;
  1121. tinyrl_redisplay(this);
  1122. }
  1123. /*-------------------------------------------------------- */
  1124. void
  1125. tinyrl_replace_line(tinyrl_t *this,
  1126. const char *text,
  1127. int clear_undo)
  1128. {
  1129. size_t new_len = strlen(text);
  1130. /* ignored for now */
  1131. clear_undo = clear_undo;
  1132. /* ensure there is sufficient space */
  1133. if (BOOL_TRUE == tinyrl_extend_line_buffer(this,new_len))
  1134. {
  1135. /* overwrite the current contents of the buffer */
  1136. strcpy(this->buffer,text);
  1137. /* set the insert point and end point */
  1138. this->point = this->end = new_len;
  1139. }
  1140. tinyrl_redisplay(this);
  1141. }
  1142. /*-------------------------------------------------------- */
  1143. static tinyrl_match_e
  1144. tinyrl_do_complete(tinyrl_t *this,
  1145. bool_t with_extensions)
  1146. {
  1147. tinyrl_match_e result = TINYRL_NO_MATCH;
  1148. char **matches = NULL;
  1149. unsigned start,end;
  1150. bool_t completion = BOOL_FALSE;
  1151. bool_t prefix = BOOL_FALSE;
  1152. /* find the start and end of the current word */
  1153. start = end = this->point;
  1154. while(start && !isspace(this->line[start-1]))
  1155. {
  1156. start--;
  1157. }
  1158. if(this->attempted_completion_function)
  1159. {
  1160. this->completion_over = BOOL_FALSE;
  1161. this->completion_error_over = BOOL_FALSE;
  1162. /* try and complete the current line buffer */
  1163. matches = this->attempted_completion_function(this,
  1164. this->line,
  1165. start,
  1166. end);
  1167. }
  1168. if((NULL == matches)
  1169. && (BOOL_FALSE == this->completion_over))
  1170. {
  1171. /* insert default completion call here... */
  1172. }
  1173. if(matches)
  1174. {
  1175. /* identify and insert a common prefix if there is one */
  1176. if(0 != strncmp(matches[0],&this->line[start],strlen(matches[0])))
  1177. {
  1178. /*
  1179. * delete the original text not including
  1180. * the current insertion point character
  1181. */
  1182. if(this->end != end)
  1183. {
  1184. end--;
  1185. }
  1186. tinyrl_delete_text(this,start,end);
  1187. if (BOOL_FALSE == tinyrl_insert_text(this,matches[0]))
  1188. {
  1189. return TINYRL_NO_MATCH;
  1190. }
  1191. completion = BOOL_TRUE;
  1192. }
  1193. if(0 == lub_string_nocasecmp(matches[0],matches[1]))
  1194. {
  1195. /* this is just a prefix string */
  1196. prefix = BOOL_TRUE;
  1197. }
  1198. /* is there more than one completion? */
  1199. if(matches[2] != NULL)
  1200. {
  1201. char **tmp = matches;
  1202. unsigned max,len;
  1203. max = len = 0;
  1204. while(*tmp)
  1205. {
  1206. size_t size = strlen(*tmp++);
  1207. len++;
  1208. if(size > max)
  1209. {
  1210. max = size;
  1211. }
  1212. }
  1213. if(BOOL_TRUE == completion)
  1214. {
  1215. result = TINYRL_COMPLETED_AMBIGUOUS;
  1216. }
  1217. else if(BOOL_TRUE == prefix)
  1218. {
  1219. result = TINYRL_MATCH_WITH_EXTENSIONS;
  1220. }
  1221. else
  1222. {
  1223. result = TINYRL_AMBIGUOUS;
  1224. }
  1225. if((BOOL_TRUE == with_extensions)
  1226. || (BOOL_FALSE == prefix))
  1227. {
  1228. /* Either we always want to show extensions or
  1229. * we haven't been able to complete the current line
  1230. * and there is just a prefix, so let the user see the options
  1231. */
  1232. tinyrl_crlf(this);
  1233. tinyrl_display_matches(this,matches,len,max);
  1234. tinyrl_reset_line_state(this);
  1235. }
  1236. }
  1237. else
  1238. {
  1239. result = completion ? TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  1240. }
  1241. /* free the memory */
  1242. tinyrl_delete_matches(matches);
  1243. /* redisplay the line */
  1244. tinyrl_redisplay(this);
  1245. }
  1246. return result;
  1247. }
  1248. /*-------------------------------------------------------- */
  1249. tinyrl_match_e
  1250. tinyrl_complete_with_extensions(tinyrl_t *this)
  1251. {
  1252. return tinyrl_do_complete(this,BOOL_TRUE);
  1253. }
  1254. /*-------------------------------------------------------- */
  1255. tinyrl_match_e
  1256. tinyrl_complete(tinyrl_t *this)
  1257. {
  1258. return tinyrl_do_complete(this,BOOL_FALSE);
  1259. }
  1260. /*-------------------------------------------------------- */
  1261. void *
  1262. tinyrl__get_context(const tinyrl_t *this)
  1263. {
  1264. return this->context;
  1265. }
  1266. /*--------------------------------------------------------- */
  1267. const char *
  1268. tinyrl__get_line(const tinyrl_t *this)
  1269. {
  1270. return this->line;
  1271. }
  1272. /*--------------------------------------------------------- */
  1273. tinyrl_history_t *
  1274. tinyrl__get_history(const tinyrl_t *this)
  1275. {
  1276. return this->history;
  1277. }
  1278. /*--------------------------------------------------------- */
  1279. void
  1280. tinyrl_completion_over(tinyrl_t *this)
  1281. {
  1282. this->completion_over = BOOL_TRUE;
  1283. }
  1284. /*--------------------------------------------------------- */
  1285. void
  1286. tinyrl_completion_error_over(tinyrl_t *this)
  1287. {
  1288. this->completion_error_over = BOOL_TRUE;
  1289. }
  1290. /*--------------------------------------------------------- */
  1291. bool_t
  1292. tinyrl_is_completion_error_over(const tinyrl_t *this)
  1293. {
  1294. return this->completion_error_over;
  1295. }
  1296. /*--------------------------------------------------------- */
  1297. void
  1298. tinyrl_done(tinyrl_t *this)
  1299. {
  1300. this->done = BOOL_TRUE;
  1301. }
  1302. /*--------------------------------------------------------- */
  1303. void
  1304. tinyrl_enable_echo(tinyrl_t *this)
  1305. {
  1306. this->echo_enabled = BOOL_TRUE;
  1307. }
  1308. /*--------------------------------------------------------- */
  1309. void
  1310. tinyrl_disable_echo(tinyrl_t *this,
  1311. char echo_char)
  1312. {
  1313. this->echo_enabled = BOOL_FALSE;
  1314. this->echo_char = echo_char;
  1315. }
  1316. /*--------------------------------------------------------- */
  1317. void
  1318. tinyrl__set_istream(tinyrl_t *this,
  1319. FILE *istream)
  1320. {
  1321. tinyrl_vt100__set_istream(this->term,istream);
  1322. this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  1323. }
  1324. /*-------------------------------------------------------- */
  1325. bool_t
  1326. tinyrl__get_isatty(const tinyrl_t *this)
  1327. {
  1328. return this->isatty;
  1329. }
  1330. /*-------------------------------------------------------- */
  1331. FILE *
  1332. tinyrl__get_istream(const tinyrl_t *this)
  1333. {
  1334. return tinyrl_vt100__get_istream(this->term);
  1335. }
  1336. /*-------------------------------------------------------- */
  1337. FILE *
  1338. tinyrl__get_ostream(const tinyrl_t *this)
  1339. {
  1340. return tinyrl_vt100__get_ostream(this->term);
  1341. }
  1342. /*-------------------------------------------------------- */
  1343. const char *
  1344. tinyrl__get_prompt(const tinyrl_t *this)
  1345. {
  1346. return this->prompt;
  1347. }
  1348. /*-------------------------------------------------------- */
  1349. bool_t
  1350. tinyrl_is_quoting(const tinyrl_t *this)
  1351. {
  1352. bool_t result = BOOL_FALSE;
  1353. /* count the quotes upto the current insertion point */
  1354. unsigned i = 0;
  1355. while(i < this->point)
  1356. {
  1357. if(this->line[i++] == '"')
  1358. {
  1359. result = result ? BOOL_FALSE : BOOL_TRUE;
  1360. }
  1361. }
  1362. return result;
  1363. }
  1364. /*--------------------------------------------------------- */
  1365. void
  1366. tinyrl_limit_line_length(tinyrl_t *this, unsigned length)
  1367. {
  1368. this->max_line_length = length;
  1369. }
  1370. /*--------------------------------------------------------- */