tinyrl.c 35 KB

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