tinyrl.c 35 KB

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