tinyrl.c 34 KB

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