tinyrl.c 35 KB

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