tinyrl.c 36 KB

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