tinyrl.c 34 KB

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