tinyrl.c 35 KB

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