tinyrl.c 34 KB

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