tinyrl.c 35 KB

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