tinyrl.c 35 KB

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