tinyrl.c 35 KB

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