1
0

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