1
0

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