tinyrl.c 34 KB

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