tinyrl.c 36 KB

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