tinyrl.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * tinyrl.c
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <faux/faux.h>
  12. #include <faux/str.h>
  13. #include "private.h"
  14. #define LINE_CHUNK 80
  15. tinyrl_t *tinyrl_new(FILE *istream, FILE *ostream,
  16. const char *hist_fname, size_t hist_stifle)
  17. {
  18. tinyrl_t *tinyrl = NULL;
  19. int i = 0;
  20. tinyrl = faux_zmalloc(sizeof(tinyrl_t));
  21. if (!tinyrl)
  22. return NULL;
  23. // Line
  24. faux_bzero(&tinyrl->line, sizeof(tinyrl->line));
  25. tinyrl_line_extend(tinyrl, LINE_CHUNK);
  26. // Input processing vars
  27. tinyrl->utf8_cont = 0;
  28. tinyrl->esc_cont = BOOL_FALSE;
  29. tinyrl->esc_seq[0] = '\0';
  30. tinyrl->esc_p = tinyrl->esc_seq;
  31. // Key handlers
  32. for (i = 0; i < NUM_HANDLERS; i++) {
  33. tinyrl->handlers[i] = tinyrl_key_default;
  34. }
  35. tinyrl->handlers[KEY_CR] = tinyrl_key_crlf;
  36. tinyrl->handlers[KEY_LF] = tinyrl_key_crlf;
  37. tinyrl->handlers[KEY_ETX] = tinyrl_key_interrupt;
  38. tinyrl->handlers[KEY_DEL] = tinyrl_key_backspace;
  39. tinyrl->handlers[KEY_BS] = tinyrl_key_backspace;
  40. tinyrl->handlers[KEY_EOT] = tinyrl_key_delete;
  41. tinyrl->handlers[KEY_FF] = tinyrl_key_clear_screen;
  42. tinyrl->handlers[KEY_NAK] = tinyrl_key_erase_line;
  43. tinyrl->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  44. tinyrl->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  45. tinyrl->handlers[KEY_VT] = tinyrl_key_kill;
  46. tinyrl->handlers[KEY_EM] = tinyrl_key_yank;
  47. tinyrl->handlers[KEY_HT] = tinyrl_key_tab;
  48. tinyrl->handlers[KEY_ETB] = tinyrl_key_backword;
  49. tinyrl->max_line_length = 0;
  50. tinyrl->prompt = NULL;
  51. tinyrl->prompt_size = 0;
  52. tinyrl->buffer = NULL;
  53. tinyrl->buffer_size = 0;
  54. tinyrl->done = BOOL_FALSE;
  55. tinyrl->completion_over = BOOL_FALSE;
  56. tinyrl->attempted_completion_function = NULL;
  57. tinyrl->hotkey_fn = NULL;
  58. tinyrl->state = 0;
  59. tinyrl->kill_string = NULL;
  60. tinyrl->echo_char = '\0';
  61. tinyrl->echo_enabled = BOOL_TRUE;
  62. tinyrl->last_buffer = NULL;
  63. tinyrl->last_point = 0;
  64. tinyrl->last_line_size = 0;
  65. tinyrl->utf8 = BOOL_TRUE;
  66. // VT100 terminal
  67. tinyrl->term = vt100_new(istream, ostream);
  68. // To save terminal settings
  69. tinyrl_set_istream(tinyrl, istream);
  70. tinyrl->width = vt100_width(tinyrl->term);
  71. // History object
  72. tinyrl->hist = hist_new(hist_fname, hist_stifle);
  73. tinyrl_hist_restore(tinyrl);
  74. tty_raw_mode(tinyrl);
  75. return tinyrl;
  76. }
  77. void tinyrl_free(tinyrl_t *tinyrl)
  78. {
  79. assert(tinyrl);
  80. if (!tinyrl)
  81. return;
  82. tty_restore_mode(tinyrl);
  83. hist_free(tinyrl->hist);
  84. vt100_free(tinyrl->term);
  85. faux_str_free(tinyrl->buffer);
  86. faux_str_free(tinyrl->kill_string);
  87. faux_str_free(tinyrl->last_buffer);
  88. faux_str_free(tinyrl->prompt);
  89. faux_free(tinyrl);
  90. }
  91. void tty_raw_mode(tinyrl_t *tinyrl)
  92. {
  93. struct termios new_termios = {};
  94. FILE *istream = NULL;
  95. int fd = -1;
  96. if (!tinyrl)
  97. return;
  98. istream = vt100_istream(tinyrl->term);
  99. if (!istream)
  100. return;
  101. fd = fileno(istream);
  102. if (tcgetattr(fd, &new_termios) < 0)
  103. return;
  104. new_termios.c_iflag = 0;
  105. new_termios.c_oflag = OPOST | ONLCR;
  106. new_termios.c_lflag = 0;
  107. new_termios.c_cc[VMIN] = 1;
  108. new_termios.c_cc[VTIME] = 0;
  109. // Mode switch
  110. tcsetattr(fd, TCSADRAIN, &new_termios);
  111. }
  112. void tty_restore_mode(tinyrl_t *tinyrl)
  113. {
  114. FILE *istream = NULL;
  115. int fd = -1;
  116. istream = vt100_istream(tinyrl->term);
  117. if (!istream)
  118. return;
  119. fd = fileno(istream);
  120. // Do the mode switch
  121. tcsetattr(fd, TCSADRAIN, &tinyrl->default_termios);
  122. }
  123. bool_t tinyrl_bind_key(tinyrl_t *tinyrl, int key, tinyrl_key_func_t *fn)
  124. {
  125. assert(tinyrl);
  126. if (!tinyrl)
  127. return BOOL_FALSE;
  128. if ((key < 0) || (key > 255))
  129. return BOOL_FALSE;
  130. tinyrl->handlers[key] = fn;
  131. return BOOL_TRUE;
  132. }
  133. void tinyrl_set_hotkey_fn(tinyrl_t *tinyrl, tinyrl_key_func_t *fn)
  134. {
  135. tinyrl->hotkey_fn = fn;
  136. }
  137. void tinyrl_set_istream(tinyrl_t *tinyrl, FILE *istream)
  138. {
  139. assert(tinyrl);
  140. if (!tinyrl)
  141. return;
  142. vt100_set_istream(tinyrl->term, istream);
  143. // Save terminal settings to restore on exit
  144. if (istream)
  145. tcgetattr(fileno(istream), &tinyrl->default_termios);
  146. }
  147. FILE *tinyrl_istream(const tinyrl_t *tinyrl)
  148. {
  149. return vt100_istream(tinyrl->term);
  150. }
  151. void tinyrl_set_ostream(tinyrl_t *tinyrl, FILE *ostream)
  152. {
  153. assert(tinyrl);
  154. if (!tinyrl)
  155. return;
  156. vt100_set_ostream(tinyrl->term, ostream);
  157. }
  158. FILE *tinyrl_ostream(const tinyrl_t *tinyrl)
  159. {
  160. return vt100_ostream(tinyrl->term);
  161. }
  162. bool_t tinyrl_utf8(const tinyrl_t *tinyrl)
  163. {
  164. assert(tinyrl);
  165. if (!tinyrl)
  166. return BOOL_TRUE;
  167. return tinyrl->utf8;
  168. }
  169. void tinyrl_set_utf8(tinyrl_t *tinyrl, bool_t utf8)
  170. {
  171. assert(tinyrl);
  172. if (!tinyrl)
  173. return;
  174. tinyrl->utf8 = utf8;
  175. }
  176. bool_t tinyrl_hist_save(const tinyrl_t *tinyrl)
  177. {
  178. assert(tinyrl);
  179. if (!tinyrl)
  180. return BOOL_FALSE;
  181. return hist_save(tinyrl->hist);
  182. }
  183. bool_t tinyrl_hist_restore(tinyrl_t *tinyrl)
  184. {
  185. assert(tinyrl);
  186. if (!tinyrl)
  187. return BOOL_FALSE;
  188. return hist_restore(tinyrl->hist);
  189. }
  190. static bool_t process_char(tinyrl_t *tinyrl, char key)
  191. {
  192. // Begin of ESC sequence
  193. if (!tinyrl->esc_cont && (KEY_ESC == key)) {
  194. tinyrl->esc_cont = BOOL_TRUE; // Start ESC sequence
  195. tinyrl->esc_p = tinyrl->esc_seq;
  196. // Note: Don't put ESC symbol itself to buffer
  197. return BOOL_TRUE;
  198. }
  199. // Continue ESC sequence
  200. if (tinyrl->esc_cont) {
  201. // Broken sequence. Too long
  202. if ((tinyrl->esc_p - tinyrl->esc_seq) >= (sizeof(tinyrl->esc_seq) - 1)) {
  203. tinyrl->esc_cont = BOOL_FALSE;
  204. return BOOL_FALSE;
  205. }
  206. // Save the curren char to sequence buffer
  207. *tinyrl->esc_p = key;
  208. tinyrl->esc_p++;
  209. // ANSI standard control sequences will end
  210. // with a character between 64 - 126
  211. if ((key != '[') && (key > 63)) {
  212. *tinyrl->esc_p = '\0';
  213. tinyrl_esc_seq(tinyrl, tinyrl->esc_seq);
  214. tinyrl->esc_cont = BOOL_FALSE;
  215. //tinyrl_redisplay(tinyrl);
  216. }
  217. return BOOL_TRUE;
  218. }
  219. // Call the handler for key
  220. // Handler (that has no special meaning) will put new char to line buffer
  221. if (!tinyrl->handlers[(unsigned char)key](tinyrl, key))
  222. vt100_ding(tinyrl->term);
  223. // if (tinyrl->done) // Some handler set the done flag
  224. // continue; // It will break the loop
  225. if (tinyrl->utf8) {
  226. // ASCII char (one byte)
  227. if (!(UTF8_7BIT_MASK & key)) {
  228. tinyrl->utf8_cont = 0;
  229. // First byte of multibyte symbol
  230. } else if (UTF8_11 == (key & UTF8_MASK)) {
  231. // Find out number of symbol's bytes
  232. unsigned int b = (unsigned int)key;
  233. tinyrl->utf8_cont = 0;
  234. while ((tinyrl->utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  235. tinyrl->utf8_cont++;
  236. b = b << 1;
  237. }
  238. // Continue of multibyte symbol
  239. } else if ((tinyrl->utf8_cont > 0) && (UTF8_10 == (key & UTF8_MASK))) {
  240. tinyrl->utf8_cont--;
  241. }
  242. }
  243. // For non UTF-8 encoding the utf8_cont is always 0.
  244. // For UTF-8 it's 0 when one-byte symbol or we get
  245. // all bytes for the current multibyte character
  246. // if (!utf8_cont)
  247. // tinyrl_redisplay(tinyrl);
  248. printf("%s\n", tinyrl->line.str);
  249. return BOOL_TRUE;
  250. }
  251. int tinyrl_read(tinyrl_t *tinyrl)
  252. {
  253. int rc = 0;
  254. char key = 0;
  255. int count = 0;
  256. assert(tinyrl);
  257. while ((rc = vt100_getchar(tinyrl->term, &key)) > 0) {
  258. count++;
  259. process_char(tinyrl, key);
  260. }
  261. if ((rc < 0) && (EAGAIN == errno))
  262. return count;
  263. return rc;
  264. }
  265. /*
  266. * Ensure that buffer has enough space to hold len characters,
  267. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  268. * if the line is successfully extended, BOOL_FALSE if not.
  269. */
  270. bool_t tinyrl_line_extend(tinyrl_t *tinyrl, size_t len)
  271. {
  272. char *new_buf = NULL;
  273. size_t new_size = 0;
  274. size_t chunk_num = 0;
  275. if (tinyrl->line.len >= len)
  276. return BOOL_TRUE;
  277. chunk_num = len / LINE_CHUNK;
  278. if ((len % LINE_CHUNK) > 0)
  279. chunk_num++;
  280. new_size = chunk_num * LINE_CHUNK;
  281. // First initialization
  282. if (tinyrl->line.str == NULL) {
  283. tinyrl->line.str = faux_zmalloc(new_size);
  284. if (!tinyrl->line.str)
  285. return BOOL_FALSE;
  286. tinyrl->line.size = new_size;
  287. return BOOL_TRUE;
  288. }
  289. new_buf = realloc(tinyrl->line.str, new_size);
  290. if (!new_buf)
  291. return BOOL_FALSE;
  292. tinyrl->line.str = new_buf;
  293. tinyrl->line.size = new_size;
  294. return BOOL_TRUE;
  295. }
  296. bool_t tinyrl_esc_seq(tinyrl_t *tinyrl, const char *esc_seq)
  297. {
  298. bool_t result = BOOL_FALSE;
  299. switch (vt100_esc_decode(tinyrl->term, esc_seq)) {
  300. case VT100_CURSOR_UP:
  301. result = tinyrl_key_up(tinyrl, 0);
  302. break;
  303. case VT100_CURSOR_DOWN:
  304. result = tinyrl_key_down(tinyrl, 0);
  305. break;
  306. case VT100_CURSOR_LEFT:
  307. result = tinyrl_key_left(tinyrl, 0);
  308. break;
  309. case VT100_CURSOR_RIGHT:
  310. result = tinyrl_key_right(tinyrl, 0);
  311. break;
  312. case VT100_HOME:
  313. result = tinyrl_key_start_of_line(tinyrl, 0);
  314. break;
  315. case VT100_END:
  316. result = tinyrl_key_end_of_line(tinyrl, 0);
  317. break;
  318. case VT100_DELETE:
  319. result = tinyrl_key_delete(tinyrl, 0);
  320. break;
  321. case VT100_INSERT:
  322. case VT100_PGDOWN:
  323. case VT100_PGUP:
  324. case VT100_UNKNOWN:
  325. break;
  326. }
  327. return result;
  328. }
  329. bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len)
  330. {
  331. size_t new_size = tinyrl->line.len + len + 1;
  332. if (len == 0)
  333. return BOOL_TRUE;
  334. tinyrl_line_extend(tinyrl, new_size);
  335. if (tinyrl->line.pos < tinyrl->line.len) {
  336. memmove(tinyrl->line.str + tinyrl->line.pos + len,
  337. tinyrl->line.str + tinyrl->line.pos,
  338. len);
  339. }
  340. memcpy(tinyrl->line.str + tinyrl->line.pos, text, len);
  341. tinyrl->line.pos += len;
  342. tinyrl->line.len += len;
  343. tinyrl->line.str[tinyrl->line.len] = '\0';
  344. return BOOL_TRUE;
  345. }
  346. #if 0
  347. /*----------------------------------------------------------------------- */
  348. /*
  349. tinyrl is called whenever a line is edited in any way.
  350. It signals that if we are currently viewing a history line we should transfer it
  351. to the current buffer
  352. */
  353. static void changed_line(tinyrl_t * tinyrl)
  354. {
  355. /* if the current line is not our buffer then make it so */
  356. if (tinyrl->line != tinyrl->buffer) {
  357. /* replace the current buffer with the new details */
  358. free(tinyrl->buffer);
  359. tinyrl->line = tinyrl->buffer = lub_string_dup(tinyrl->line);
  360. tinyrl->buffer_size = strlen(tinyrl->buffer);
  361. assert(tinyrl->line);
  362. }
  363. }
  364. /*-------------------------------------------------------- */
  365. int tinyrl_printf(const tinyrl_t * tinyrl, const char *fmt, ...)
  366. {
  367. va_list args;
  368. int len;
  369. va_start(args, fmt);
  370. len = tinyrl_vt100_vprintf(tinyrl->term, fmt, args);
  371. va_end(args);
  372. return len;
  373. }
  374. /*----------------------------------------------------------------------- */
  375. static void tinyrl_internal_print(const tinyrl_t * tinyrl, const char *text)
  376. {
  377. if (tinyrl->echo_enabled) {
  378. /* simply echo the line */
  379. tinyrl_vt100_printf(tinyrl->term, "%s", text);
  380. } else {
  381. /* replace the line with echo char if defined */
  382. if (tinyrl->echo_char) {
  383. unsigned int i = strlen(text);
  384. while (i--) {
  385. tinyrl_vt100_printf(tinyrl->term, "%c",
  386. tinyrl->echo_char);
  387. }
  388. }
  389. }
  390. }
  391. /*----------------------------------------------------------------------- */
  392. static void tinyrl_internal_position(const tinyrl_t *tinyrl, int prompt_len,
  393. int line_len, unsigned int width)
  394. {
  395. int rows, cols;
  396. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  397. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  398. if (cols > 0)
  399. tinyrl_vt100_cursor_back(tinyrl->term, cols);
  400. else if (cols < 0)
  401. tinyrl_vt100_cursor_forward(tinyrl->term, -cols);
  402. if (rows > 0)
  403. tinyrl_vt100_cursor_up(tinyrl->term, rows);
  404. else if (rows < 0)
  405. tinyrl_vt100_cursor_down(tinyrl->term, -rows);
  406. }
  407. /*-------------------------------------------------------- */
  408. /* Jump to first free line after current multiline input */
  409. void tinyrl_multi_crlf(const tinyrl_t * tinyrl)
  410. {
  411. unsigned int line_size = strlen(tinyrl->last_buffer);
  412. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, line_size);
  413. unsigned int count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  414. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + line_len,
  415. - (line_len - count), tinyrl->width);
  416. tinyrl_crlf(tinyrl);
  417. tinyrl_vt100_oflush(tinyrl->term);
  418. }
  419. /*----------------------------------------------------------------------- */
  420. void tinyrl_redisplay(tinyrl_t * tinyrl)
  421. {
  422. unsigned int line_size = strlen(tinyrl->line);
  423. unsigned int line_len = utf8_nsyms(tinyrl, tinyrl->line, line_size);
  424. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  425. unsigned int count, eq_chars = 0;
  426. int cols;
  427. /* Prepare print position */
  428. if (tinyrl->last_buffer && (width == tinyrl->width)) {
  429. unsigned int eq_len = 0;
  430. /* If line and last line have the equal chars at begining */
  431. eq_chars = lub_string_equal_part(tinyrl->line, tinyrl->last_buffer,
  432. tinyrl->utf8);
  433. eq_len = utf8_nsyms(tinyrl, tinyrl->last_buffer, eq_chars);
  434. count = utf8_nsyms(tinyrl, tinyrl->last_buffer, tinyrl->last_point);
  435. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + eq_len,
  436. count - eq_len, width);
  437. } else {
  438. /* Prepare to resize */
  439. if (width != tinyrl->width) {
  440. tinyrl_vt100_next_line(tinyrl->term);
  441. tinyrl_vt100_erase_down(tinyrl->term);
  442. }
  443. tinyrl_vt100_printf(tinyrl->term, "%s", tinyrl->prompt);
  444. }
  445. /* Print current line */
  446. tinyrl_internal_print(tinyrl, tinyrl->line + eq_chars);
  447. cols = (tinyrl->prompt_len + line_len) % width;
  448. if (!cols && (line_size - eq_chars))
  449. tinyrl_vt100_next_line(tinyrl->term);
  450. /* Erase down if current line is shorter than previous one */
  451. if (tinyrl->last_line_size > line_size)
  452. tinyrl_vt100_erase_down(tinyrl->term);
  453. /* Move the cursor to the insertion point */
  454. if (tinyrl->point < line_size) {
  455. unsigned int pre_len = utf8_nsyms(tinyrl,
  456. tinyrl->line, tinyrl->point);
  457. count = utf8_nsyms(tinyrl, tinyrl->line + tinyrl->point,
  458. line_size - tinyrl->point);
  459. tinyrl_internal_position(tinyrl, tinyrl->prompt_len + pre_len,
  460. count, width);
  461. }
  462. /* Update the display */
  463. tinyrl_vt100_oflush(tinyrl->term);
  464. /* Save the last line buffer */
  465. lub_string_free(tinyrl->last_buffer);
  466. tinyrl->last_buffer = lub_string_dup(tinyrl->line);
  467. tinyrl->last_point = tinyrl->point;
  468. tinyrl->width = width;
  469. tinyrl->last_line_size = line_size;
  470. }
  471. static char *internal_insertline(tinyrl_t * tinyrl, char *buffer)
  472. {
  473. char *p;
  474. char *s = buffer;
  475. /* strip any spurious '\r' or '\n' */
  476. if ((p = strchr(buffer, '\r')))
  477. *p = '\0';
  478. if ((p = strchr(buffer, '\n')))
  479. *p = '\0';
  480. /* skip any whitespace at the beginning of the line */
  481. if (0 == tinyrl->point) {
  482. while (*s && isspace(*s))
  483. s++;
  484. }
  485. if (*s) {
  486. /* append tinyrl string to the input buffer */
  487. (void)tinyrl_insert_text(tinyrl, s);
  488. }
  489. /* echo the command to the output stream */
  490. tinyrl_redisplay(tinyrl);
  491. return s;
  492. }
  493. /*----------------------------------------------------------------------- */
  494. static char *internal_readline(tinyrl_t * tinyrl,
  495. void *context, const char *str)
  496. {
  497. FILE *istream = tinyrl_vt100__get_istream(tinyrl->term);
  498. char *result = NULL;
  499. int lerrno = 0;
  500. tinyrl->done = BOOL_FALSE;
  501. tinyrl->point = 0;
  502. tinyrl->end = 0;
  503. tinyrl->buffer = lub_string_dup("");
  504. tinyrl->buffer_size = strlen(tinyrl->buffer);
  505. tinyrl->line = tinyrl->buffer;
  506. tinyrl->context = context;
  507. /* Interactive session */
  508. if (isatty(fileno(tinyrl->istream)) && !str) {
  509. unsigned int utf8_cont = 0; /* UTF-8 continue bytes */
  510. unsigned int esc_cont = 0; /* Escape sequence continues */
  511. char esc_seq[10]; /* Buffer for ESC sequence */
  512. char *esc_p = esc_seq;
  513. /* Set the terminal into raw mode */
  514. tty_raw_mode(tinyrl);
  515. tinyrl_reset_line_state(tinyrl);
  516. while (!tinyrl->done) {
  517. int key;
  518. key = tinyrl_getchar(tinyrl);
  519. /* Error || EOF || Timeout */
  520. if (key < 0) {
  521. if ((VT100_TIMEOUT == key) &&
  522. !tinyrl->timeout_fn(tinyrl))
  523. continue;
  524. /* It's time to finish the session */
  525. tinyrl->done = BOOL_TRUE;
  526. tinyrl->line = NULL;
  527. lerrno = ENOENT;
  528. continue;
  529. }
  530. /* Real key pressed */
  531. /* Common callback for any key */
  532. if (tinyrl->keypress_fn)
  533. tinyrl->keypress_fn(tinyrl, key);
  534. /* Check for ESC sequence. It's a special case. */
  535. if (!esc_cont && (key == KEY_ESC)) {
  536. esc_cont = 1; /* Start ESC sequence */
  537. esc_p = esc_seq;
  538. continue;
  539. }
  540. if (esc_cont) {
  541. /* Broken sequence */
  542. if (esc_p >= (esc_seq + sizeof(esc_seq) - 1)) {
  543. esc_cont = 0;
  544. continue;
  545. }
  546. /* Dump the control sequence into sequence buffer
  547. ANSI standard control sequences will end
  548. with a character between 64 - 126 */
  549. *esc_p = key & 0xff;
  550. esc_p++;
  551. /* tinyrl is an ANSI control sequence terminator code */
  552. if ((key != '[') && (key > 63)) {
  553. *esc_p = '\0';
  554. tinyrl_escape_seq(tinyrl, esc_seq);
  555. esc_cont = 0;
  556. tinyrl_redisplay(tinyrl);
  557. }
  558. continue;
  559. }
  560. /* Call the handler for tinyrl key */
  561. if (!tinyrl->handlers[key](tinyrl, key))
  562. tinyrl_ding(tinyrl);
  563. if (tinyrl->done) /* Some handler set the done flag */
  564. continue; /* It will break the loop */
  565. if (tinyrl->utf8) {
  566. if (!(UTF8_7BIT_MASK & key)) /* ASCII char */
  567. utf8_cont = 0;
  568. else if (utf8_cont && (UTF8_10 == (key & UTF8_MASK))) /* Continue byte */
  569. utf8_cont--;
  570. else if (UTF8_11 == (key & UTF8_MASK)) { /* First byte of multibyte char */
  571. /* Find out number of char's bytes */
  572. int b = key;
  573. utf8_cont = 0;
  574. while ((utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  575. utf8_cont++;
  576. b = b << 1;
  577. }
  578. }
  579. }
  580. /* For non UTF-8 encoding the utf8_cont is always 0.
  581. For UTF-8 it's 0 when one-byte symbol or we get
  582. all bytes for the current multibyte character. */
  583. if (!utf8_cont)
  584. tinyrl_redisplay(tinyrl);
  585. }
  586. /* If the last character in the line (other than NULL)
  587. is a space remove it. */
  588. if (tinyrl->end && tinyrl->line && isspace(tinyrl->line[tinyrl->end - 1]))
  589. tinyrl_delete_text(tinyrl, tinyrl->end - 1, tinyrl->end);
  590. /* Restores the terminal mode */
  591. tty_restore_mode(tinyrl);
  592. /* Non-interactive session */
  593. } else {
  594. char *s = NULL, buffer[80];
  595. size_t len = sizeof(buffer);
  596. char *tmp = NULL;
  597. /* manually reset the line state without redisplaying */
  598. lub_string_free(tinyrl->last_buffer);
  599. tinyrl->last_buffer = NULL;
  600. if (str) {
  601. tmp = lub_string_dup(str);
  602. internal_insertline(tinyrl, tmp);
  603. } else {
  604. while (istream && (sizeof(buffer) == len) &&
  605. (s = fgets(buffer, sizeof(buffer), istream))) {
  606. s = internal_insertline(tinyrl, buffer);
  607. len = strlen(buffer) + 1; /* account for the '\0' */
  608. }
  609. if (!s || ((tinyrl->line[0] == '\0') && feof(istream))) {
  610. /* time to finish the session */
  611. tinyrl->line = NULL;
  612. lerrno = ENOENT;
  613. }
  614. }
  615. /*
  616. * check against fgets returning null as either error or end of file.
  617. * tinyrl is a measure to stop potential task spin on encountering an
  618. * error from fgets.
  619. */
  620. if (tinyrl->line && !tinyrl->handlers[KEY_LF](tinyrl, KEY_LF)) {
  621. /* an issue has occured */
  622. tinyrl->line = NULL;
  623. lerrno = ENOEXEC;
  624. }
  625. if (str)
  626. lub_string_free(tmp);
  627. }
  628. /*
  629. * duplicate the string for return to the client
  630. * we have to duplicate as we may be referencing a
  631. * history entry or our internal buffer
  632. */
  633. result = tinyrl->line ? lub_string_dup(tinyrl->line) : NULL;
  634. /* free our internal buffer */
  635. free(tinyrl->buffer);
  636. tinyrl->buffer = NULL;
  637. if (!result)
  638. errno = lerrno; /* get saved errno */
  639. return result;
  640. }
  641. /*----------------------------------------------------------------------- */
  642. char *tinyrl_readline(tinyrl_t * tinyrl, void *context)
  643. {
  644. return internal_readline(tinyrl, context, NULL);
  645. }
  646. /*----------------------------------------------------------------------- */
  647. char *tinyrl_forceline(tinyrl_t * tinyrl, void *context, const char *line)
  648. {
  649. return internal_readline(tinyrl, context, line);
  650. }
  651. /*----------------------------------------------------------------------- */
  652. /*
  653. * A convenience function for displaying a list of strings in columnar
  654. * format on Readline's output stream. matches is the list of strings,
  655. * in argv format, such as a list of completion matches. len is the number
  656. * of strings in matches, and max is the length of the longest string in matches.
  657. * tinyrl function uses the setting of print-completions-horizontally to select
  658. * how the matches are displayed
  659. */
  660. void tinyrl_display_matches(const tinyrl_t *tinyrl,
  661. char *const *matches, unsigned int len, size_t max)
  662. {
  663. unsigned int width = tinyrl_vt100__get_width(tinyrl->term);
  664. unsigned int cols, rows;
  665. /* Find out column and rows number */
  666. if (max < width)
  667. cols = (width + 1) / (max + 1); /* allow for a space between words */
  668. else
  669. cols = 1;
  670. rows = len / cols + 1;
  671. assert(matches);
  672. if (matches) {
  673. unsigned int r, c;
  674. len--, matches++; /* skip the subtitution string */
  675. /* Print out a table of completions */
  676. for (r = 0; r < rows && len; r++) {
  677. for (c = 0; c < cols && len; c++) {
  678. const char *match = *matches++;
  679. len--;
  680. if ((c + 1) == cols) /* Last str in row */
  681. tinyrl_vt100_printf(tinyrl->term, "%s",
  682. match);
  683. else
  684. tinyrl_vt100_printf(tinyrl->term, "%-*s ",
  685. max, match);
  686. }
  687. tinyrl_crlf(tinyrl);
  688. }
  689. }
  690. }
  691. /*----------------------------------------------------------------------- */
  692. /*
  693. * Delete the text between start and end in the current line. (inclusive)
  694. * tinyrl adjusts the rl_point and rl_end indexes appropriately.
  695. */
  696. void tinyrl_delete_text(tinyrl_t * tinyrl, unsigned int start, unsigned int end)
  697. {
  698. unsigned int delta;
  699. /*
  700. * If the client wants to change the line ensure that the line and buffer
  701. * references are in sync
  702. */
  703. changed_line(tinyrl);
  704. /* make sure we play it safe */
  705. if (start > end) {
  706. unsigned int tmp = end;
  707. start = end;
  708. end = tmp;
  709. }
  710. if (end > tinyrl->end)
  711. end = tinyrl->end;
  712. delta = (end - start) + 1;
  713. /* move any text which is left */
  714. memmove(&tinyrl->buffer[start],
  715. &tinyrl->buffer[start + delta], tinyrl->end - end);
  716. /* now adjust the indexs */
  717. if (tinyrl->point >= start) {
  718. if (tinyrl->point > end) {
  719. /* move the insertion point back appropriately */
  720. tinyrl->point -= delta;
  721. } else {
  722. /* move the insertion point to the start */
  723. tinyrl->point = start;
  724. }
  725. }
  726. if (tinyrl->end > end)
  727. tinyrl->end -= delta;
  728. else
  729. tinyrl->end = start;
  730. /* put a terminator at the end of the buffer */
  731. tinyrl->buffer[tinyrl->end] = '\0';
  732. }
  733. /*-------------------------------------------------------- */
  734. /*
  735. * Returns an array of strings which is a list of completions for text.
  736. * If there are no completions, returns NULL. The first entry in the
  737. * returned array is the substitution for text. The remaining entries
  738. * are the possible completions. The array is terminated with a NULL pointer.
  739. *
  740. * entry_func is a function of two args, and returns a char *.
  741. * The first argument is text. The second is a state argument;
  742. * it is zero on the first call, and non-zero on subsequent calls.
  743. * entry_func returns a NULL pointer to the caller when there are no
  744. * more matches.
  745. */
  746. char **tinyrl_completion(tinyrl_t * tinyrl,
  747. const char *line, unsigned int start, unsigned int end,
  748. tinyrl_compentry_func_t * entry_func)
  749. {
  750. unsigned int state = 0;
  751. size_t size = 1;
  752. unsigned int offset = 1; /* Need at least one entry for the substitution */
  753. char **matches = NULL;
  754. char *match;
  755. /* duplicate the string upto the insertion point */
  756. char *text = lub_string_dupn(line, end);
  757. /* now try and find possible completions */
  758. while ((match = entry_func(tinyrl, text, start, state++))) {
  759. if (size == offset) {
  760. /* resize the buffer if needed - the +1 is for the NULL terminator */
  761. size += 10;
  762. matches =
  763. realloc(matches, (sizeof(char *) * (size + 1)));
  764. }
  765. /* not much we can do... */
  766. if (!matches)
  767. break;
  768. matches[offset] = match;
  769. /*
  770. * augment the substitute string with tinyrl entry
  771. */
  772. if (1 == offset) {
  773. /* let's be optimistic */
  774. matches[0] = lub_string_dup(match);
  775. } else {
  776. char *p = matches[0];
  777. size_t match_len = strlen(p);
  778. /* identify the common prefix */
  779. while ((tolower(*p) == tolower(*match)) && match_len--) {
  780. p++, match++;
  781. }
  782. /* terminate the prefix string */
  783. *p = '\0';
  784. }
  785. offset++;
  786. }
  787. /* be a good memory citizen */
  788. lub_string_free(text);
  789. if (matches)
  790. matches[offset] = NULL;
  791. return matches;
  792. }
  793. /*-------------------------------------------------------- */
  794. void tinyrl_delete_matches(char **tinyrl)
  795. {
  796. char **matches = tinyrl;
  797. while (*matches) {
  798. /* release the memory for each contained string */
  799. free(*matches++);
  800. }
  801. /* release the memory for the array */
  802. free(tinyrl);
  803. }
  804. /*-------------------------------------------------------- */
  805. void tinyrl_crlf(const tinyrl_t * tinyrl)
  806. {
  807. tinyrl_vt100_printf(tinyrl->term, "\n");
  808. }
  809. /*-------------------------------------------------------- */
  810. /*
  811. * Ring the terminal bell, obeying the setting of bell-style.
  812. */
  813. void tinyrl_ding(const tinyrl_t * tinyrl)
  814. {
  815. tinyrl_vt100_ding(tinyrl->term);
  816. }
  817. /*-------------------------------------------------------- */
  818. void tinyrl_reset_line_state(tinyrl_t * tinyrl)
  819. {
  820. lub_string_free(tinyrl->last_buffer);
  821. tinyrl->last_buffer = NULL;
  822. tinyrl->last_line_size = 0;
  823. tinyrl_redisplay(tinyrl);
  824. }
  825. /*-------------------------------------------------------- */
  826. void tinyrl_replace_line(tinyrl_t * tinyrl, const char *text, int clear_undo)
  827. {
  828. size_t new_len = strlen(text);
  829. /* ignored for now */
  830. clear_undo = clear_undo;
  831. /* ensure there is sufficient space */
  832. if (tinyrl_extend_line_buffer(tinyrl, new_len)) {
  833. /* overwrite the current contents of the buffer */
  834. strcpy(tinyrl->buffer, text);
  835. /* set the insert point and end point */
  836. tinyrl->point = tinyrl->end = new_len;
  837. }
  838. tinyrl_redisplay(tinyrl);
  839. }
  840. /*-------------------------------------------------------- */
  841. static tinyrl_match_e
  842. tinyrl_do_complete(tinyrl_t * tinyrl, bool_t with_extensions)
  843. {
  844. tinyrl_match_e result = TINYRL_NO_MATCH;
  845. char **matches = NULL;
  846. unsigned int start, end;
  847. bool_t completion = BOOL_FALSE;
  848. bool_t prefix = BOOL_FALSE;
  849. int i = 0;
  850. /* find the start and end of the current word */
  851. start = end = tinyrl->point;
  852. while (start && !isspace(tinyrl->line[start - 1]))
  853. start--;
  854. if (tinyrl->attempted_completion_function) {
  855. tinyrl->completion_over = BOOL_FALSE;
  856. tinyrl->completion_error_over = BOOL_FALSE;
  857. /* try and complete the current line buffer */
  858. matches = tinyrl->attempted_completion_function(tinyrl,
  859. tinyrl->line, start, end);
  860. }
  861. if (!matches && (BOOL_FALSE == tinyrl->completion_over)) {
  862. /* insert default completion call here... */
  863. }
  864. if (!matches)
  865. return result;
  866. /* identify and insert a common prefix if there is one */
  867. if (0 != strncmp(matches[0], &tinyrl->line[start],
  868. strlen(matches[0]))) {
  869. /*
  870. * delete the original text not including
  871. * the current insertion point character
  872. */
  873. if (tinyrl->end != end)
  874. end--;
  875. tinyrl_delete_text(tinyrl, start, end);
  876. if (BOOL_FALSE == tinyrl_insert_text(tinyrl, matches[0]))
  877. return TINYRL_NO_MATCH;
  878. completion = BOOL_TRUE;
  879. }
  880. for (i = 1; matches[i]; i++) {
  881. /* tinyrl is just a prefix string */
  882. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  883. prefix = BOOL_TRUE;
  884. }
  885. /* is there more than one completion? */
  886. if (matches[2]) {
  887. char **tmp = matches;
  888. unsigned int max, len;
  889. max = len = 0;
  890. while (*tmp) {
  891. size_t size = strlen(*tmp++);
  892. len++;
  893. if (size > max)
  894. max = size;
  895. }
  896. if (completion)
  897. result = TINYRL_COMPLETED_AMBIGUOUS;
  898. else if (prefix)
  899. result = TINYRL_MATCH_WITH_EXTENSIONS;
  900. else
  901. result = TINYRL_AMBIGUOUS;
  902. if (with_extensions || !prefix) {
  903. /* Either we always want to show extensions or
  904. * we haven't been able to complete the current line
  905. * and there is just a prefix, so let the user see the options
  906. */
  907. tinyrl_crlf(tinyrl);
  908. tinyrl_display_matches(tinyrl, matches, len, max);
  909. tinyrl_reset_line_state(tinyrl);
  910. }
  911. } else {
  912. result = completion ?
  913. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  914. }
  915. /* free the memory */
  916. tinyrl_delete_matches(matches);
  917. /* redisplay the line */
  918. tinyrl_redisplay(tinyrl);
  919. return result;
  920. }
  921. /*-------------------------------------------------------- */
  922. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * tinyrl)
  923. {
  924. return tinyrl_do_complete(tinyrl, BOOL_TRUE);
  925. }
  926. /*-------------------------------------------------------- */
  927. tinyrl_match_e tinyrl_complete(tinyrl_t * tinyrl)
  928. {
  929. return tinyrl_do_complete(tinyrl, BOOL_FALSE);
  930. }
  931. /*-------------------------------------------------------- */
  932. void *tinyrl__get_context(const tinyrl_t * tinyrl)
  933. {
  934. return tinyrl->context;
  935. }
  936. /*--------------------------------------------------------- */
  937. const char *tinyrl__get_line(const tinyrl_t * tinyrl)
  938. {
  939. return tinyrl->line;
  940. }
  941. /*--------------------------------------------------------- */
  942. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * tinyrl)
  943. {
  944. return tinyrl->history;
  945. }
  946. /*--------------------------------------------------------- */
  947. void tinyrl_completion_over(tinyrl_t * tinyrl)
  948. {
  949. tinyrl->completion_over = BOOL_TRUE;
  950. }
  951. /*--------------------------------------------------------- */
  952. void tinyrl_completion_error_over(tinyrl_t * tinyrl)
  953. {
  954. tinyrl->completion_error_over = BOOL_TRUE;
  955. }
  956. /*--------------------------------------------------------- */
  957. bool_t tinyrl_is_completion_error_over(const tinyrl_t * tinyrl)
  958. {
  959. return tinyrl->completion_error_over;
  960. }
  961. /*--------------------------------------------------------- */
  962. void tinyrl_done(tinyrl_t * tinyrl)
  963. {
  964. tinyrl->done = BOOL_TRUE;
  965. }
  966. /*--------------------------------------------------------- */
  967. void tinyrl_enable_echo(tinyrl_t * tinyrl)
  968. {
  969. tinyrl->echo_enabled = BOOL_TRUE;
  970. }
  971. /*--------------------------------------------------------- */
  972. void tinyrl_disable_echo(tinyrl_t * tinyrl, char echo_char)
  973. {
  974. tinyrl->echo_enabled = BOOL_FALSE;
  975. tinyrl->echo_char = echo_char;
  976. }
  977. /*-------------------------------------------------------- */
  978. const char *tinyrl__get_prompt(const tinyrl_t * tinyrl)
  979. {
  980. return tinyrl->prompt;
  981. }
  982. /*-------------------------------------------------------- */
  983. void tinyrl__set_prompt(tinyrl_t *tinyrl, const char *prompt)
  984. {
  985. if (tinyrl->prompt) {
  986. lub_string_free(tinyrl->prompt);
  987. tinyrl->prompt_size = 0;
  988. tinyrl->prompt_len = 0;
  989. }
  990. tinyrl->prompt = lub_string_dup(prompt);
  991. if (tinyrl->prompt) {
  992. tinyrl->prompt_size = strlen(tinyrl->prompt);
  993. tinyrl->prompt_len = utf8_nsyms(tinyrl, tinyrl->prompt,
  994. tinyrl->prompt_size);
  995. }
  996. }
  997. /*-------------------------------------------------------- */
  998. bool_t tinyrl_is_quoting(const tinyrl_t * tinyrl)
  999. {
  1000. bool_t result = BOOL_FALSE;
  1001. /* count the quotes upto the current insertion point */
  1002. unsigned int i = 0;
  1003. while (i < tinyrl->point) {
  1004. if (result && (tinyrl->line[i] == '\\')) {
  1005. i++;
  1006. if (i >= tinyrl->point)
  1007. break;
  1008. i++;
  1009. continue;
  1010. }
  1011. if (tinyrl->line[i++] == '"') {
  1012. result = result ? BOOL_FALSE : BOOL_TRUE;
  1013. }
  1014. }
  1015. return result;
  1016. }
  1017. /*-------------------------------------------------------- */
  1018. bool_t tinyrl_is_empty(const tinyrl_t *tinyrl)
  1019. {
  1020. return (tinyrl->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1021. }
  1022. /*--------------------------------------------------------- */
  1023. void tinyrl_limit_line_length(tinyrl_t * tinyrl, unsigned int length)
  1024. {
  1025. tinyrl->max_line_length = length;
  1026. }
  1027. #endif