tinyrl.c 35 KB

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