tinyrl.c 35 KB

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