tinyrl.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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 int utf8_wchar(const char *sp, unsigned long *sym_out)
  19. {
  20. int i = 0;
  21. int l = 0; /* Number of 0x10 UTF sequence bytes */
  22. unsigned long sym = 0;
  23. const unsigned char *p = (const unsigned char *)sp;
  24. if (sym_out)
  25. *sym_out = *p;
  26. if (!*p)
  27. return 0;
  28. /* Check for first byte of UTF-8 */
  29. if (!(*p & 0xc0))
  30. return 1;
  31. /* Analyze first byte */
  32. if ((*p & 0xe0) == 0xc0) {
  33. l = 1;
  34. sym = (*p & 0x1f);
  35. } else if ((*p & 0xf0) == 0xe0) {
  36. l = 2;
  37. sym = (*p & 0xf);
  38. } else if ((*p & 0xf8) == 0xf0) {
  39. l = 3;
  40. sym = (*p & 7);
  41. } else if ((*p & 0xfc) == 0xf8) {
  42. l = 4;
  43. sym = (*p & 3);
  44. } else if ((*p & 0xfe) == 0xfc) {
  45. l = 5;
  46. sym = (*p & 1);
  47. } else {
  48. return 1;
  49. }
  50. p++;
  51. /* Analyze next UTF-8 bytes */
  52. for (i = 0; i < l; i++) {
  53. sym <<= 6;
  54. /* Check if it's really UTF-8 bytes */
  55. if ((*p & 0xc0) != 0x80)
  56. return 1;
  57. sym |= (*p & 0x3f);
  58. p++;
  59. }
  60. if (sym_out)
  61. *sym_out = sym;
  62. return (l + 1);
  63. }
  64. /*-------------------------------------------------------- */
  65. static int utf8_is_cjk(unsigned long sym)
  66. {
  67. if (sym < 0x1100) /* Speed up for non-CJK chars */
  68. return 0;
  69. if (sym >= 0x1100 && sym <= 0x11FF) /* Hangul Jamo */
  70. return 1;
  71. #if 0
  72. if (sym >=0x2E80 && sym <= 0x2EFF) /* CJK Radicals Supplement */
  73. return 1;
  74. if (sym >=0x2F00 && sym <= 0x2FDF) /* Kangxi Radicals */
  75. return 1;
  76. if (sym >= 0x2FF0 && sym <= 0x2FFF) /* Ideographic Description Characters */
  77. return 1;
  78. if (sym >= 0x3000 && sym < 0x303F) /* CJK Symbols and Punctuation. The U+303f is half space */
  79. return 1;
  80. if (sym >= 0x3040 && sym <= 0x309F) /* Hiragana */
  81. return 1;
  82. if (sym >= 0x30A0 && sym <=0x30FF) /* Katakana */
  83. return 1;
  84. if (sym >= 0x3100 && sym <=0x312F) /* Bopomofo */
  85. return 1;
  86. if (sym >= 0x3130 && sym <= 0x318F) /* Hangul Compatibility Jamo */
  87. return 1;
  88. if (sym >= 0x3190 && sym <= 0x319F) /* Kanbun */
  89. return 1;
  90. if (sym >= 0x31A0 && sym <= 0x31BF) /* Bopomofo Extended */
  91. return 1;
  92. if (sym >= 0x31C0 && sym <= 0x31EF) /* CJK strokes */
  93. return 1;
  94. if (sym >= 0x31F0 && sym <= 0x31FF) /* Katakana Phonetic Extensions */
  95. return 1;
  96. if (sym >= 0x3200 && sym <= 0x32FF) /* Enclosed CJK Letters and Months */
  97. return 1;
  98. if (sym >= 0x3300 && sym <= 0x33FF) /* CJK Compatibility */
  99. return 1;
  100. if (sym >= 0x3400 && sym <= 0x4DBF) /* CJK Unified Ideographs Extension A */
  101. return 1;
  102. if (sym >= 0x4DC0 && sym <= 0x4DFF) /* Yijing Hexagram Symbols */
  103. return 1;
  104. if (sym >= 0x4E00 && sym <= 0x9FFF) /* CJK Unified Ideographs */
  105. return 1;
  106. if (sym >= 0xA000 && sym <= 0xA48F) /* Yi Syllables */
  107. return 1;
  108. if (sym >= 0xA490 && sym <= 0xA4CF) /* Yi Radicals */
  109. return 1;
  110. #endif
  111. /* Speed up previous block */
  112. if (sym >= 0x2E80 && sym <= 0xA4CF && sym != 0x303F)
  113. return 1;
  114. if (sym >= 0xAC00 && sym <= 0xD7AF) /* Hangul Syllables */
  115. return 1;
  116. if (sym >= 0xF900 && sym <= 0xFAFF) /* CJK Compatibility Ideographs */
  117. return 1;
  118. if (sym >= 0xFE10 && sym <= 0xFE1F) /* Vertical Forms */
  119. return 1;
  120. #if 0
  121. if (sym >= 0xFE30 && sym <= 0xFE4F) /* CJK Compatibility Forms */
  122. return 1;
  123. if (sym >= 0xFE50 && sym <= 0xFE6F) /* Small Form Variants */
  124. return 1;
  125. #endif
  126. /* Speed up previous block */
  127. if (sym >= 0xFE30 && sym <= 0xFE6F)
  128. return 1;
  129. if ((sym >= 0xFF00 && sym <= 0xFF60) ||
  130. (sym >= 0xFFE0 && sym <= 0xFFE6)) /* Fullwidth Forms */
  131. return 1;
  132. if (sym >= 0x1D300 && sym <= 0x1D35F) /* Tai Xuan Jing Symbols */
  133. return 1;
  134. if (sym >= 0x20000 && sym <= 0x2B81F) /* CJK Unified Ideographs Extensions B, C, D */
  135. return 1;
  136. if (sym >= 0x2F800 && sym <= 0x2FA1F) /* CJK Compatibility Ideographs Supplement */
  137. return 1;
  138. return 0;
  139. }
  140. /*-------------------------------------------------------- */
  141. static void utf8_point_left(tinyrl_t * this)
  142. {
  143. if (!this->utf8)
  144. return;
  145. while (this->point &&
  146. (UTF8_10 == (this->line[this->point] & UTF8_MASK)))
  147. this->point--;
  148. }
  149. /*-------------------------------------------------------- */
  150. static void utf8_point_right(tinyrl_t * this)
  151. {
  152. if (!this->utf8)
  153. return;
  154. while ((this->point < this->end) &&
  155. (UTF8_10 == (this->line[this->point] & UTF8_MASK)))
  156. this->point++;
  157. }
  158. /*-------------------------------------------------------- */
  159. static unsigned int utf8_nsyms(const tinyrl_t *this, const char *str,
  160. unsigned int num)
  161. {
  162. unsigned int nsym = 0;
  163. unsigned long sym = 0;
  164. unsigned int i = 0;
  165. if (!this->utf8)
  166. return num;
  167. while (i < num) {
  168. if ('\0' == str[i])
  169. break;
  170. /* ASCII char */
  171. if (!(UTF8_7BIT_MASK & str[i])) {
  172. i++;
  173. nsym++;
  174. continue;
  175. }
  176. /* Multibyte */
  177. i += utf8_wchar(&str[i], &sym);
  178. if (utf8_is_cjk(sym)) /* CJK chars have double-width */
  179. nsym += 2;
  180. else
  181. nsym += 1;
  182. }
  183. return nsym;
  184. }
  185. /*----------------------------------------------------------------------- */
  186. static void tty_set_raw_mode(tinyrl_t * this)
  187. {
  188. struct termios new_termios;
  189. int fd;
  190. if (!tinyrl_vt100__get_istream(this->term))
  191. return;
  192. fd = fileno(tinyrl_vt100__get_istream(this->term));
  193. if (tcgetattr(fd, &new_termios) < 0)
  194. return;
  195. new_termios.c_iflag = 0;
  196. new_termios.c_oflag = OPOST | ONLCR;
  197. new_termios.c_lflag = 0;
  198. new_termios.c_cc[VMIN] = 1;
  199. new_termios.c_cc[VTIME] = 0;
  200. /* Do the mode switch */
  201. (void)tcsetattr(fd, TCSADRAIN, &new_termios);
  202. }
  203. /*----------------------------------------------------------------------- */
  204. static void tty_restore_mode(const tinyrl_t * this)
  205. {
  206. int fd;
  207. if (!tinyrl_vt100__get_istream(this->term))
  208. return;
  209. fd = fileno(tinyrl_vt100__get_istream(this->term));
  210. /* Do the mode switch */
  211. (void)tcsetattr(fd, TCSADRAIN, &this->default_termios);
  212. }
  213. /*----------------------------------------------------------------------- */
  214. /*
  215. This is called whenever a line is edited in any way.
  216. It signals that if we are currently viewing a history line we should transfer it
  217. to the current buffer
  218. */
  219. void tinyrl_changed_line(tinyrl_t * this)
  220. {
  221. /* if the current line is not our buffer then make it so */
  222. if (this->line != this->buffer) {
  223. /* replace the current buffer with the new details */
  224. free(this->buffer);
  225. this->line = this->buffer = lub_string_dup(this->line);
  226. this->buffer_size = strlen(this->buffer);
  227. assert(this->line);
  228. }
  229. }
  230. /*----------------------------------------------------------------------- */
  231. static int tinyrl_timeout_default(tinyrl_t *this)
  232. {
  233. this = this; /* Happy compiler */
  234. /* Return -1 to close session on timeout */
  235. return -1;
  236. }
  237. /*----------------------------------------------------------------------- */
  238. static bool_t tinyrl_key_default(tinyrl_t * this, int key)
  239. {
  240. bool_t result = BOOL_FALSE;
  241. if (key > 31) {
  242. char tmp[2];
  243. tmp[0] = (key & 0xFF), tmp[1] = '\0';
  244. /* inject this text into the buffer */
  245. result = tinyrl_insert_text(this, tmp);
  246. } else {
  247. /* Call the external hotkey analyzer */
  248. if (this->hotkey_fn)
  249. this->hotkey_fn(this, key);
  250. }
  251. return result;
  252. }
  253. /*-------------------------------------------------------- */
  254. static bool_t tinyrl_key_interrupt(tinyrl_t * this, int key)
  255. {
  256. tinyrl_crlf(this);
  257. tinyrl_delete_text(this, 0, this->end);
  258. this->done = BOOL_TRUE;
  259. /* keep the compiler happy */
  260. key = key;
  261. return BOOL_TRUE;
  262. }
  263. /*-------------------------------------------------------- */
  264. static bool_t tinyrl_key_start_of_line(tinyrl_t * this, int key)
  265. {
  266. /* set the insertion point to the start of the line */
  267. this->point = 0;
  268. /* keep the compiler happy */
  269. key = key;
  270. return BOOL_TRUE;
  271. }
  272. /*-------------------------------------------------------- */
  273. static bool_t tinyrl_key_end_of_line(tinyrl_t * this, int key)
  274. {
  275. /* set the insertion point to the end of the line */
  276. this->point = this->end;
  277. /* keep the compiler happy */
  278. key = key;
  279. return BOOL_TRUE;
  280. }
  281. /*-------------------------------------------------------- */
  282. static bool_t tinyrl_key_kill(tinyrl_t * this, int key)
  283. {
  284. /* release any old kill string */
  285. lub_string_free(this->kill_string);
  286. /* store the killed string */
  287. this->kill_string = lub_string_dup(&this->buffer[this->point]);
  288. /* delete the text to the end of the line */
  289. tinyrl_delete_text(this, this->point, this->end);
  290. /* keep the compiler happy */
  291. key = key;
  292. return BOOL_TRUE;
  293. }
  294. /*-------------------------------------------------------- */
  295. static bool_t tinyrl_key_yank(tinyrl_t * this, int key)
  296. {
  297. bool_t result = BOOL_FALSE;
  298. if (this->kill_string) {
  299. /* insert the kill string at the current insertion point */
  300. result = tinyrl_insert_text(this, this->kill_string);
  301. }
  302. /* keep the compiler happy */
  303. key = key;
  304. return result;
  305. }
  306. /*-------------------------------------------------------- */
  307. static bool_t tinyrl_key_crlf(tinyrl_t * this, int key)
  308. {
  309. tinyrl_crlf(this);
  310. this->done = BOOL_TRUE;
  311. /* keep the compiler happy */
  312. key = key;
  313. return BOOL_TRUE;
  314. }
  315. /*-------------------------------------------------------- */
  316. static bool_t tinyrl_key_up(tinyrl_t * this, int key)
  317. {
  318. bool_t result = BOOL_FALSE;
  319. tinyrl_history_entry_t *entry = NULL;
  320. if (this->line == this->buffer) {
  321. /* go to the last history entry */
  322. entry = tinyrl_history_getlast(this->history, &this->hist_iter);
  323. } else {
  324. /* already traversing the history list so get previous */
  325. entry = tinyrl_history_getprevious(&this->hist_iter);
  326. }
  327. if (entry) {
  328. /* display the entry moving the insertion point
  329. * to the end of the line
  330. */
  331. this->line = tinyrl_history_entry__get_line(entry);
  332. this->point = this->end = strlen(this->line);
  333. result = BOOL_TRUE;
  334. }
  335. /* keep the compiler happy */
  336. key = key;
  337. return result;
  338. }
  339. /*-------------------------------------------------------- */
  340. static bool_t tinyrl_key_down(tinyrl_t * this, int key)
  341. {
  342. bool_t result = BOOL_FALSE;
  343. if (this->line != this->buffer) {
  344. /* we are not already at the bottom */
  345. /* the iterator will have been set up by the key_up() function */
  346. tinyrl_history_entry_t *entry =
  347. tinyrl_history_getnext(&this->hist_iter);
  348. if (!entry) {
  349. /* nothing more in the history list */
  350. this->line = this->buffer;
  351. } else {
  352. this->line = tinyrl_history_entry__get_line(entry);
  353. }
  354. /* display the entry moving the insertion point
  355. * to the end of the line
  356. */
  357. this->point = this->end = strlen(this->line);
  358. result = BOOL_TRUE;
  359. }
  360. /* keep the compiler happy */
  361. key = key;
  362. return result;
  363. }
  364. /*-------------------------------------------------------- */
  365. static bool_t tinyrl_key_left(tinyrl_t * this, int key)
  366. {
  367. bool_t result = BOOL_FALSE;
  368. if (this->point > 0) {
  369. this->point--;
  370. utf8_point_left(this);
  371. result = BOOL_TRUE;
  372. }
  373. /* keep the compiler happy */
  374. key = key;
  375. return result;
  376. }
  377. /*-------------------------------------------------------- */
  378. static bool_t tinyrl_key_right(tinyrl_t * this, int key)
  379. {
  380. bool_t result = BOOL_FALSE;
  381. if (this->point < this->end) {
  382. this->point++;
  383. utf8_point_right(this);
  384. result = BOOL_TRUE;
  385. }
  386. /* keep the compiler happy */
  387. key = key;
  388. return result;
  389. }
  390. /*-------------------------------------------------------- */
  391. static bool_t tinyrl_key_backspace(tinyrl_t *this, int key)
  392. {
  393. bool_t result = BOOL_FALSE;
  394. if (this->point) {
  395. unsigned int end = --this->point;
  396. utf8_point_left(this);
  397. tinyrl_delete_text(this, this->point, end);
  398. result = BOOL_TRUE;
  399. }
  400. /* keep the compiler happy */
  401. key = key;
  402. return result;
  403. }
  404. /*-------------------------------------------------------- */
  405. static bool_t tinyrl_key_backword(tinyrl_t *this, int key)
  406. {
  407. bool_t result = BOOL_FALSE;
  408. /* remove current whitespace before cursor */
  409. while (this->point > 0 && isspace(this->line[this->point - 1]))
  410. tinyrl_key_backspace(this, KEY_BS);
  411. /* delete word before cusor */
  412. while (this->point > 0 && !isspace(this->line[this->point - 1]))
  413. tinyrl_key_backspace(this, KEY_BS);
  414. result = BOOL_TRUE;
  415. /* keep the compiler happy */
  416. key = key;
  417. return result;
  418. }
  419. /*-------------------------------------------------------- */
  420. static bool_t tinyrl_key_delete(tinyrl_t * this, int key)
  421. {
  422. bool_t result = BOOL_FALSE;
  423. if (this->point < this->end) {
  424. unsigned int begin = this->point++;
  425. utf8_point_right(this);
  426. tinyrl_delete_text(this, begin, this->point - 1);
  427. result = BOOL_TRUE;
  428. }
  429. /* keep the compiler happy */
  430. key = key;
  431. return result;
  432. }
  433. /*-------------------------------------------------------- */
  434. static bool_t tinyrl_key_clear_screen(tinyrl_t * this, int key)
  435. {
  436. tinyrl_vt100_clear_screen(this->term);
  437. tinyrl_vt100_cursor_home(this->term);
  438. tinyrl_reset_line_state(this);
  439. /* keep the compiler happy */
  440. key = key;
  441. this = this;
  442. return BOOL_TRUE;
  443. }
  444. /*-------------------------------------------------------- */
  445. static bool_t tinyrl_key_erase_line(tinyrl_t * this, int key)
  446. {
  447. unsigned int end;
  448. /* release any old kill string */
  449. lub_string_free(this->kill_string);
  450. if (!this->point) {
  451. this->kill_string = NULL;
  452. return BOOL_TRUE;
  453. }
  454. end = this->point - 1;
  455. /* store the killed string */
  456. this->kill_string = malloc(this->point + 1);
  457. memcpy(this->kill_string, this->buffer, this->point);
  458. this->kill_string[this->point] = '\0';
  459. /* delete the text from the start of the line */
  460. tinyrl_delete_text(this, 0, end);
  461. this->point = 0;
  462. /* keep the compiler happy */
  463. key = key;
  464. this = this;
  465. return BOOL_TRUE;
  466. }
  467. /*-------------------------------------------------------- */
  468. /* Toggle INSert flag. When INS flag is on then pressing "?" you will get
  469. * the "?" itself. When INS flag is off then pressing "?" you will get help.
  470. * On each interactive line the INS flag wil be reset. May be it will be used
  471. * for another symbols (and hotkeys) later.
  472. */
  473. static bool_t tinyrl_toggle_ins_flag(tinyrl_t *this)
  474. {
  475. if (!this)
  476. return BOOL_FALSE;
  477. if (this->ins_flag)
  478. this->ins_flag = BOOL_FALSE;
  479. else
  480. this->ins_flag = BOOL_TRUE;
  481. return BOOL_TRUE;
  482. }
  483. /*-------------------------------------------------------- */
  484. static bool_t tinyrl_reset_ins_flag(tinyrl_t *this)
  485. {
  486. if (!this)
  487. return BOOL_FALSE;
  488. this->ins_flag = BOOL_FALSE;
  489. return BOOL_TRUE;
  490. }
  491. /*-------------------------------------------------------- */
  492. bool_t tinyrl_get_ins_flag(tinyrl_t *this)
  493. {
  494. if (!this)
  495. return BOOL_FALSE;
  496. return this->ins_flag;
  497. }
  498. /*-------------------------------------------------------- */
  499. /* It's a dirty hack. The function to print help is external but we need
  500. * to show help on some escape sequences. So suppose raw help key is
  501. * Ctrl+_ (Control_underscore 0x1f KEY_US) execute external function for this
  502. * key here.
  503. */
  504. /*
  505. static bool_t tinyrl_key_help(tinyrl_t *this, int key)
  506. {
  507. if (!this || !this->handlers[KEY_US])
  508. return BOOL_FALSE;
  509. key = key; // Happy compiler
  510. return this->handlers[KEY_US](this, key);
  511. }
  512. */
  513. /*-------------------------------------------------------- */
  514. static bool_t tinyrl_escape_seq(tinyrl_t *this, const char *esc_seq)
  515. {
  516. int key = 0;
  517. bool_t result = BOOL_FALSE;
  518. switch (tinyrl_vt100_escape_decode(this->term, esc_seq)) {
  519. case tinyrl_vt100_CURSOR_UP:
  520. result = tinyrl_key_up(this, key);
  521. break;
  522. case tinyrl_vt100_CURSOR_DOWN:
  523. result = tinyrl_key_down(this, key);
  524. break;
  525. case tinyrl_vt100_CURSOR_LEFT:
  526. result = tinyrl_key_left(this, key);
  527. break;
  528. case tinyrl_vt100_CURSOR_RIGHT:
  529. result = tinyrl_key_right(this, key);
  530. break;
  531. case tinyrl_vt100_HOME:
  532. result = tinyrl_key_start_of_line(this,key);
  533. break;
  534. case tinyrl_vt100_END:
  535. result = tinyrl_key_end_of_line(this,key);
  536. break;
  537. case tinyrl_vt100_DELETE:
  538. result = tinyrl_key_delete(this,key);
  539. break;
  540. case tinyrl_vt100_INSERT:
  541. result = tinyrl_toggle_ins_flag(this);
  542. break;
  543. case tinyrl_vt100_PGDOWN:
  544. case tinyrl_vt100_PGUP:
  545. case tinyrl_vt100_UNKNOWN:
  546. break;
  547. }
  548. return result;
  549. }
  550. /*-------------------------------------------------------- */
  551. static bool_t tinyrl_key_tab(tinyrl_t * this, int key)
  552. {
  553. bool_t result = BOOL_FALSE;
  554. tinyrl_match_e status = tinyrl_complete_with_extensions(this);
  555. switch (status) {
  556. case TINYRL_COMPLETED_MATCH:
  557. case TINYRL_MATCH:
  558. /* everything is OK with the world... */
  559. result = tinyrl_insert_text(this, " ");
  560. break;
  561. case TINYRL_NO_MATCH:
  562. case TINYRL_MATCH_WITH_EXTENSIONS:
  563. case TINYRL_AMBIGUOUS:
  564. case TINYRL_COMPLETED_AMBIGUOUS:
  565. /* oops don't change the result and let the bell ring */
  566. break;
  567. }
  568. /* keep the compiler happy */
  569. key = key;
  570. return result;
  571. }
  572. /*-------------------------------------------------------- */
  573. static void tinyrl_fini(tinyrl_t * this)
  574. {
  575. /* delete the history session */
  576. tinyrl_history_delete(this->history);
  577. /* delete the terminal session */
  578. tinyrl_vt100_delete(this->term);
  579. /* free up any dynamic strings */
  580. lub_string_free(this->buffer);
  581. lub_string_free(this->kill_string);
  582. lub_string_free(this->last_buffer);
  583. lub_string_free(this->prompt);
  584. }
  585. /*-------------------------------------------------------- */
  586. static void tinyrl_init(tinyrl_t * this, FILE * istream, FILE * ostream,
  587. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  588. {
  589. int i;
  590. for (i = 0; i < NUM_HANDLERS; i++) {
  591. this->handlers[i] = tinyrl_key_default;
  592. }
  593. /* Default handlers */
  594. this->handlers[KEY_CR] = tinyrl_key_crlf;
  595. this->handlers[KEY_LF] = tinyrl_key_crlf;
  596. this->handlers[KEY_ETX] = tinyrl_key_interrupt;
  597. this->handlers[KEY_DEL] = tinyrl_key_backspace;
  598. this->handlers[KEY_BS] = tinyrl_key_backspace;
  599. this->handlers[KEY_EOT] = tinyrl_key_delete;
  600. this->handlers[KEY_FF] = tinyrl_key_clear_screen;
  601. this->handlers[KEY_NAK] = tinyrl_key_erase_line;
  602. this->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  603. this->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  604. this->handlers[KEY_VT] = tinyrl_key_kill;
  605. this->handlers[KEY_EM] = tinyrl_key_yank;
  606. this->handlers[KEY_HT] = tinyrl_key_tab;
  607. this->handlers[KEY_ETB] = tinyrl_key_backword;
  608. this->line = NULL;
  609. this->max_line_length = 0;
  610. this->prompt = NULL;
  611. this->prompt_size = 0;
  612. this->buffer = NULL;
  613. this->buffer_size = 0;
  614. this->done = BOOL_FALSE;
  615. this->completion_over = BOOL_FALSE;
  616. this->point = 0;
  617. this->end = 0;
  618. this->attempted_completion_function = complete_fn;
  619. this->timeout_fn = tinyrl_timeout_default;
  620. this->keypress_fn = NULL;
  621. this->hotkey_fn = NULL;
  622. this->state = 0;
  623. this->kill_string = NULL;
  624. this->echo_char = '\0';
  625. this->echo_enabled = BOOL_TRUE;
  626. this->last_buffer = NULL;
  627. this->last_point = 0;
  628. this->last_line_size = 0;
  629. this->utf8 = BOOL_FALSE;
  630. this->machine_interface = BOOL_FALSE;
  631. this->ins_flag = BOOL_FALSE;
  632. /* create the vt100 terminal */
  633. this->term = tinyrl_vt100_new(NULL, ostream);
  634. tinyrl__set_istream(this, istream);
  635. this->last_width = tinyrl_vt100__get_width(this->term);
  636. /* create the history */
  637. this->history = tinyrl_history_new(stifle);
  638. }
  639. /*-------------------------------------------------------- */
  640. int tinyrl_printf(const tinyrl_t * this, const char *fmt, ...)
  641. {
  642. va_list args;
  643. int len;
  644. va_start(args, fmt);
  645. len = tinyrl_vt100_vprintf(this->term, fmt, args);
  646. va_end(args);
  647. return len;
  648. }
  649. /*-------------------------------------------------------- */
  650. void tinyrl_delete(tinyrl_t * this)
  651. {
  652. assert(this);
  653. if (this) {
  654. /* let the object tidy itself up */
  655. tinyrl_fini(this);
  656. /* release the memory associate with this instance */
  657. free(this);
  658. }
  659. }
  660. /*-------------------------------------------------------- */
  661. /*#####################################
  662. * EXPORTED INTERFACE
  663. *##################################### */
  664. /*----------------------------------------------------------------------- */
  665. int tinyrl_getchar(const tinyrl_t * this)
  666. {
  667. return tinyrl_vt100_getchar(this->term);
  668. }
  669. /*----------------------------------------------------------------------- */
  670. static void tinyrl_internal_print(const tinyrl_t * this, const char *text)
  671. {
  672. if (this->echo_enabled) {
  673. /* simply echo the line */
  674. tinyrl_vt100_printf(this->term, "%s", text);
  675. } else {
  676. /* replace the line with echo char if defined */
  677. if (this->echo_char) {
  678. unsigned int i = strlen(text);
  679. while (i--) {
  680. tinyrl_vt100_printf(this->term, "%c",
  681. this->echo_char);
  682. }
  683. }
  684. }
  685. }
  686. /*----------------------------------------------------------------------- */
  687. static void tinyrl_internal_position(const tinyrl_t *this, int prompt_len,
  688. int line_len, unsigned int width)
  689. {
  690. int rows, cols;
  691. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  692. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  693. if (cols > 0)
  694. tinyrl_vt100_cursor_back(this->term, cols);
  695. else if (cols < 0)
  696. tinyrl_vt100_cursor_forward(this->term, -cols);
  697. if (rows > 0)
  698. tinyrl_vt100_cursor_up(this->term, rows);
  699. else if (rows < 0)
  700. tinyrl_vt100_cursor_down(this->term, -rows);
  701. }
  702. /*-------------------------------------------------------- */
  703. /* Jump to first free line after current multiline input */
  704. void tinyrl_multi_crlf(const tinyrl_t * this)
  705. {
  706. unsigned int line_size = 0;
  707. unsigned int line_len = 0;
  708. unsigned int count = 0;
  709. assert(this);
  710. if (!this)
  711. return;
  712. if (tinyrl_is_machine_interface(this))
  713. return;
  714. line_size = strlen(this->last_buffer);
  715. line_len = utf8_nsyms(this, this->last_buffer, line_size);
  716. count = utf8_nsyms(this, this->last_buffer, this->last_point);
  717. tinyrl_internal_position(this, this->prompt_len + line_len,
  718. - (line_len - count), this->last_width);
  719. tinyrl_crlf(this);
  720. tinyrl_vt100_oflush(this->term);
  721. }
  722. /*----------------------------------------------------------------------- */
  723. void tinyrl_redisplay(tinyrl_t * this)
  724. {
  725. unsigned int line_size = 0;
  726. unsigned int line_len = 0;
  727. unsigned int width = 0;
  728. unsigned int count = 0;
  729. unsigned int eq_chars = 0;
  730. int cols = 0;
  731. // Don't redisplay for non-interactive machine interface
  732. if (tinyrl_is_machine_interface(this))
  733. return;
  734. line_size = strlen(this->line);
  735. line_len = utf8_nsyms(this, this->line, line_size);
  736. width = tinyrl_vt100__get_width(this->term);
  737. /* Prepare print position */
  738. if (this->last_buffer && (width == this->last_width)) {
  739. unsigned int eq_len = 0;
  740. /* If line and last line have the equal chars at begining */
  741. eq_chars = lub_string_equal_part(this->line, this->last_buffer,
  742. this->utf8);
  743. eq_len = utf8_nsyms(this, this->last_buffer, eq_chars);
  744. count = utf8_nsyms(this, this->last_buffer, this->last_point);
  745. tinyrl_internal_position(this, this->prompt_len + eq_len,
  746. count - eq_len, width);
  747. } else {
  748. /* Prepare to resize */
  749. if (width != this->last_width) {
  750. tinyrl_vt100_next_line(this->term);
  751. tinyrl_vt100_erase_down(this->term);
  752. }
  753. tinyrl_vt100_printf(this->term, "%s", this->prompt);
  754. }
  755. /* Print current line */
  756. tinyrl_internal_print(this, this->line + eq_chars);
  757. cols = (this->prompt_len + line_len) % width;
  758. if (!cols && (line_size - eq_chars))
  759. tinyrl_vt100_next_line(this->term);
  760. /* Erase down if current line is shorter than previous one */
  761. if (this->last_line_size > line_size)
  762. tinyrl_vt100_erase_down(this->term);
  763. /* Move the cursor to the insertion point */
  764. if (this->point < line_size) {
  765. unsigned int pre_len = utf8_nsyms(this,
  766. this->line, this->point);
  767. count = utf8_nsyms(this, this->line + this->point,
  768. line_size - this->point);
  769. tinyrl_internal_position(this, this->prompt_len + pre_len,
  770. count, width);
  771. }
  772. /* Update the display */
  773. tinyrl_vt100_oflush(this->term);
  774. /* Save the last line buffer */
  775. lub_string_free(this->last_buffer);
  776. this->last_buffer = lub_string_dup(this->line);
  777. this->last_point = this->point;
  778. this->last_width = width;
  779. this->last_line_size = line_size;
  780. }
  781. /*----------------------------------------------------------------------- */
  782. tinyrl_t *tinyrl_new(FILE * istream, FILE * ostream,
  783. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  784. {
  785. tinyrl_t *this = NULL;
  786. this = malloc(sizeof(tinyrl_t));
  787. if (this)
  788. tinyrl_init(this, istream, ostream, stifle, complete_fn);
  789. return this;
  790. }
  791. /*----------------------------------------------------------------------- */
  792. static char *internal_insertline(tinyrl_t * this, char *buffer)
  793. {
  794. char *p;
  795. char *s = buffer;
  796. /* strip any spurious '\r' or '\n' */
  797. if ((p = strchr(buffer, '\r')))
  798. *p = '\0';
  799. if ((p = strchr(buffer, '\n')))
  800. *p = '\0';
  801. /* skip any whitespace at the beginning of the line */
  802. if (0 == this->point) {
  803. while (*s && isspace(*s))
  804. s++;
  805. }
  806. if (*s) {
  807. /* append this string to the input buffer */
  808. (void)tinyrl_insert_text(this, s);
  809. }
  810. /* echo the command to the output stream */
  811. tinyrl_redisplay(this);
  812. return s;
  813. }
  814. /*----------------------------------------------------------------------- */
  815. static char *internal_readline(tinyrl_t * this,
  816. void *context, const char *str)
  817. {
  818. FILE *istream = tinyrl_vt100__get_istream(this->term);
  819. char *result = NULL;
  820. int lerrno = 0;
  821. this->done = BOOL_FALSE;
  822. this->point = 0;
  823. this->end = 0;
  824. this->buffer = lub_string_dup("");
  825. this->buffer_size = strlen(this->buffer);
  826. this->line = this->buffer;
  827. this->context = context;
  828. tinyrl_reset_ins_flag(this);
  829. /* Interactive session */
  830. if (tinyrl__get_isatty(this) && !str) {
  831. unsigned int utf8_cont = 0; /* UTF-8 continue bytes */
  832. unsigned int esc_cont = 0; /* Escape sequence continues */
  833. char esc_seq[10]; /* Buffer for ESC sequence */
  834. char *esc_p = esc_seq;
  835. /* Set the terminal into raw mode */
  836. tty_set_raw_mode(this);
  837. tinyrl_reset_line_state(this);
  838. while (!this->done) {
  839. int key;
  840. key = tinyrl_getchar(this);
  841. /* Error || EOF || Timeout */
  842. if (key < 0) {
  843. if ((VT100_TIMEOUT == key) &&
  844. !this->timeout_fn(this))
  845. continue;
  846. /* It's time to finish the session */
  847. this->done = BOOL_TRUE;
  848. this->line = NULL;
  849. lerrno = ENOENT;
  850. continue;
  851. }
  852. /* Real key pressed */
  853. /* Common callback for any key */
  854. if (this->keypress_fn)
  855. this->keypress_fn(this, key);
  856. /* Check for ESC sequence. It's a special case. */
  857. if (!esc_cont && (key == KEY_ESC)) {
  858. esc_cont = 1; /* Start ESC sequence */
  859. esc_p = esc_seq;
  860. continue;
  861. }
  862. if (esc_cont) {
  863. /* Broken sequence */
  864. if (esc_p >= (esc_seq + sizeof(esc_seq) - 1)) {
  865. esc_cont = 0;
  866. continue;
  867. }
  868. /* Dump the control sequence into sequence buffer
  869. ANSI standard control sequences will end
  870. with a character between 64 - 126 */
  871. *esc_p = key & 0xff;
  872. esc_p++;
  873. /* This is an ANSI control sequence terminator code */
  874. if ((key != '[') && (key > 63)) {
  875. *esc_p = '\0';
  876. tinyrl_escape_seq(this, esc_seq);
  877. esc_cont = 0;
  878. tinyrl_redisplay(this);
  879. }
  880. continue;
  881. }
  882. /* Call the handler for this key */
  883. if (!this->handlers[key](this, key))
  884. tinyrl_ding(this);
  885. if (this->done) /* Some handler set the done flag */
  886. continue; /* It will break the loop */
  887. if (this->utf8) {
  888. if (!(UTF8_7BIT_MASK & key)) /* ASCII char */
  889. utf8_cont = 0;
  890. else if (utf8_cont && (UTF8_10 == (key & UTF8_MASK))) /* Continue byte */
  891. utf8_cont--;
  892. else if (UTF8_11 == (key & UTF8_MASK)) { /* First byte of multibyte char */
  893. /* Find out number of char's bytes */
  894. int b = key;
  895. utf8_cont = 0;
  896. while ((utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  897. utf8_cont++;
  898. b = b << 1;
  899. }
  900. }
  901. }
  902. /* For non UTF-8 encoding the utf8_cont is always 0.
  903. For UTF-8 it's 0 when one-byte symbol or we get
  904. all bytes for the current multibyte character. */
  905. if (!utf8_cont)
  906. tinyrl_redisplay(this);
  907. }
  908. /* If the last character in the line (other than NULL)
  909. is a space remove it. */
  910. if (this->end && this->line && isspace(this->line[this->end - 1]))
  911. tinyrl_delete_text(this, this->end - 1, this->end);
  912. /* Restores the terminal mode */
  913. tty_restore_mode(this);
  914. /* Non-interactive session */
  915. } else {
  916. char *s = NULL, buffer[80];
  917. size_t len = sizeof(buffer);
  918. char *tmp = NULL;
  919. /* manually reset the line state without redisplaying */
  920. lub_string_free(this->last_buffer);
  921. this->last_buffer = NULL;
  922. if (str) {
  923. tmp = lub_string_dup(str);
  924. internal_insertline(this, tmp);
  925. } else {
  926. while (istream && (sizeof(buffer) == len) &&
  927. (s = fgets(buffer, sizeof(buffer), istream))) {
  928. s = internal_insertline(this, buffer);
  929. len = strlen(buffer) + 1; /* account for the '\0' */
  930. }
  931. if (!s || ((this->line[0] == '\0') && feof(istream))) {
  932. /* time to finish the session */
  933. this->line = NULL;
  934. lerrno = ENOENT;
  935. }
  936. }
  937. /*
  938. * check against fgets returning null as either error or end of file.
  939. * This is a measure to stop potential task spin on encountering an
  940. * error from fgets.
  941. */
  942. if (this->line && !this->handlers[KEY_LF](this, KEY_LF)) {
  943. /* an issue has occured */
  944. this->line = NULL;
  945. lerrno = ENOEXEC;
  946. }
  947. if (str)
  948. lub_string_free(tmp);
  949. }
  950. /*
  951. * duplicate the string for return to the client
  952. * we have to duplicate as we may be referencing a
  953. * history entry or our internal buffer
  954. */
  955. result = this->line ? lub_string_dup(this->line) : NULL;
  956. /* free our internal buffer */
  957. free(this->buffer);
  958. this->buffer = NULL;
  959. if (!result)
  960. errno = lerrno; /* get saved errno */
  961. return result;
  962. }
  963. /*----------------------------------------------------------------------- */
  964. char *tinyrl_readline(tinyrl_t * this, void *context)
  965. {
  966. return internal_readline(this, context, NULL);
  967. }
  968. /*----------------------------------------------------------------------- */
  969. char *tinyrl_forceline(tinyrl_t * this, void *context, const char *line)
  970. {
  971. return internal_readline(this, context, line);
  972. }
  973. /*----------------------------------------------------------------------- */
  974. /*
  975. * Ensure that buffer has enough space to hold len characters,
  976. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  977. * if the line is successfully extended, BOOL_FALSE if not.
  978. */
  979. bool_t tinyrl_extend_line_buffer(tinyrl_t * this, unsigned int len)
  980. {
  981. bool_t result = BOOL_TRUE;
  982. char *new_buffer;
  983. size_t new_len = len;
  984. if (this->buffer_size >= len)
  985. return result;
  986. /*
  987. * What we do depends on whether we are limited by
  988. * memory or a user imposed limit.
  989. */
  990. if (this->max_line_length == 0) {
  991. /* make sure we don't realloc too often */
  992. if (new_len < this->buffer_size + 10)
  993. new_len = this->buffer_size + 10;
  994. /* leave space for terminator */
  995. new_buffer = realloc(this->buffer, new_len + 1);
  996. if (!new_buffer) {
  997. tinyrl_ding(this);
  998. result = BOOL_FALSE;
  999. } else {
  1000. this->buffer_size = new_len;
  1001. this->line = this->buffer = new_buffer;
  1002. }
  1003. } else {
  1004. if (new_len < this->max_line_length) {
  1005. /* Just reallocate once to the max size */
  1006. new_buffer = realloc(this->buffer,
  1007. this->max_line_length);
  1008. if (!new_buffer) {
  1009. tinyrl_ding(this);
  1010. result = BOOL_FALSE;
  1011. } else {
  1012. this->buffer_size =
  1013. this->max_line_length - 1;
  1014. this->line = this->buffer = new_buffer;
  1015. }
  1016. } else {
  1017. tinyrl_ding(this);
  1018. result = BOOL_FALSE;
  1019. }
  1020. }
  1021. return result;
  1022. }
  1023. /*----------------------------------------------------------------------- */
  1024. /*
  1025. * Insert text into the line at the current cursor position.
  1026. */
  1027. bool_t tinyrl_insert_text(tinyrl_t * this, const char *text)
  1028. {
  1029. unsigned int delta = strlen(text);
  1030. /*
  1031. * If the client wants to change the line ensure that the line and buffer
  1032. * references are in sync
  1033. */
  1034. tinyrl_changed_line(this);
  1035. if ((delta + this->end) > (this->buffer_size)) {
  1036. /* extend the current buffer */
  1037. if (BOOL_FALSE ==
  1038. tinyrl_extend_line_buffer(this, this->end + delta))
  1039. return BOOL_FALSE;
  1040. }
  1041. if (this->point < this->end) {
  1042. /* move the current text to the right (including the terminator) */
  1043. memmove(&this->buffer[this->point + delta],
  1044. &this->buffer[this->point],
  1045. (this->end - this->point) + 1);
  1046. } else {
  1047. /* terminate the string */
  1048. this->buffer[this->end + delta] = '\0';
  1049. }
  1050. /* insert the new text */
  1051. strncpy(&this->buffer[this->point], text, delta);
  1052. /* now update the indexes */
  1053. this->point += delta;
  1054. this->end += delta;
  1055. return BOOL_TRUE;
  1056. }
  1057. /*----------------------------------------------------------------------- */
  1058. /*
  1059. * A convenience function for displaying a list of strings in columnar
  1060. * format on Readline's output stream. matches is the list of strings,
  1061. * in argv format, such as a list of completion matches. len is the number
  1062. * of strings in matches, and max is the length of the longest string in matches.
  1063. * This function uses the setting of print-completions-horizontally to select
  1064. * how the matches are displayed
  1065. */
  1066. void tinyrl_display_matches(const tinyrl_t *this,
  1067. char *const *matches, unsigned int len, size_t max)
  1068. {
  1069. unsigned int width = tinyrl_vt100__get_width(this->term);
  1070. unsigned int cols, rows;
  1071. /* Find out column and rows number */
  1072. if (max < width)
  1073. cols = (width + 1) / (max + 1); /* allow for a space between words */
  1074. else
  1075. cols = 1;
  1076. rows = len / cols + 1;
  1077. assert(matches);
  1078. if (matches) {
  1079. unsigned int r, c;
  1080. len--, matches++; /* skip the subtitution string */
  1081. /* Print out a table of completions */
  1082. for (r = 0; r < rows && len; r++) {
  1083. for (c = 0; c < cols && len; c++) {
  1084. const char *match = *matches++;
  1085. len--;
  1086. if ((c + 1) == cols) /* Last str in row */
  1087. tinyrl_vt100_printf(this->term, "%s",
  1088. match);
  1089. else
  1090. tinyrl_vt100_printf(this->term, "%-*s ",
  1091. max, match);
  1092. }
  1093. tinyrl_crlf(this);
  1094. }
  1095. }
  1096. }
  1097. /*----------------------------------------------------------------------- */
  1098. /*
  1099. * Delete the text between start and end in the current line. (inclusive)
  1100. * This adjusts the rl_point and rl_end indexes appropriately.
  1101. */
  1102. void tinyrl_delete_text(tinyrl_t * this, unsigned int start, unsigned int end)
  1103. {
  1104. unsigned int delta;
  1105. /*
  1106. * If the client wants to change the line ensure that the line and buffer
  1107. * references are in sync
  1108. */
  1109. tinyrl_changed_line(this);
  1110. /* make sure we play it safe */
  1111. if (start > end) {
  1112. unsigned int tmp = end;
  1113. start = end;
  1114. end = tmp;
  1115. }
  1116. if (end > this->end)
  1117. end = this->end;
  1118. delta = (end - start) + 1;
  1119. /* move any text which is left */
  1120. memmove(&this->buffer[start],
  1121. &this->buffer[start + delta], this->end - end);
  1122. /* now adjust the indexs */
  1123. if (this->point >= start) {
  1124. if (this->point > end) {
  1125. /* move the insertion point back appropriately */
  1126. this->point -= delta;
  1127. } else {
  1128. /* move the insertion point to the start */
  1129. this->point = start;
  1130. }
  1131. }
  1132. if (this->end > end)
  1133. this->end -= delta;
  1134. else
  1135. this->end = start;
  1136. /* put a terminator at the end of the buffer */
  1137. this->buffer[this->end] = '\0';
  1138. }
  1139. /*----------------------------------------------------------------------- */
  1140. bool_t tinyrl_bind_key(tinyrl_t * this, int key, tinyrl_key_func_t * fn)
  1141. {
  1142. bool_t result = BOOL_FALSE;
  1143. if ((key >= 0) && (key < 256)) {
  1144. /* set the key handling function */
  1145. this->handlers[key] = fn;
  1146. result = BOOL_TRUE;
  1147. }
  1148. return result;
  1149. }
  1150. /*-------------------------------------------------------- */
  1151. /*
  1152. * Returns an array of strings which is a list of completions for text.
  1153. * If there are no completions, returns NULL. The first entry in the
  1154. * returned array is the substitution for text. The remaining entries
  1155. * are the possible completions. The array is terminated with a NULL pointer.
  1156. *
  1157. * entry_func is a function of two args, and returns a char *.
  1158. * The first argument is text. The second is a state argument;
  1159. * it is zero on the first call, and non-zero on subsequent calls.
  1160. * entry_func returns a NULL pointer to the caller when there are no
  1161. * more matches.
  1162. */
  1163. char **tinyrl_completion(tinyrl_t * this,
  1164. const char *line, unsigned int start, unsigned int end,
  1165. tinyrl_compentry_func_t * entry_func)
  1166. {
  1167. unsigned int state = 0;
  1168. size_t size = 1;
  1169. unsigned int offset = 1; /* Need at least one entry for the substitution */
  1170. char **matches = NULL;
  1171. char *match;
  1172. /* duplicate the string upto the insertion point */
  1173. char *text = lub_string_dupn(line, end);
  1174. /* now try and find possible completions */
  1175. while ((match = entry_func(this, text, start, state++))) {
  1176. if (size == offset) {
  1177. /* resize the buffer if needed - the +1 is for the NULL terminator */
  1178. size += 10;
  1179. matches =
  1180. realloc(matches, (sizeof(char *) * (size + 1)));
  1181. }
  1182. /* not much we can do... */
  1183. if (!matches)
  1184. break;
  1185. matches[offset] = match;
  1186. /*
  1187. * augment the substitute string with this entry
  1188. */
  1189. if (1 == offset) {
  1190. /* let's be optimistic */
  1191. matches[0] = lub_string_dup(match);
  1192. } else {
  1193. char *p = matches[0];
  1194. size_t match_len = strlen(p);
  1195. /* identify the common prefix */
  1196. while ((tolower(*p) == tolower(*match)) && match_len--) {
  1197. p++, match++;
  1198. }
  1199. /* terminate the prefix string */
  1200. *p = '\0';
  1201. }
  1202. offset++;
  1203. }
  1204. /* be a good memory citizen */
  1205. lub_string_free(text);
  1206. if (matches)
  1207. matches[offset] = NULL;
  1208. return matches;
  1209. }
  1210. /*-------------------------------------------------------- */
  1211. void tinyrl_delete_matches(char **this)
  1212. {
  1213. char **matches = this;
  1214. while (*matches) {
  1215. /* release the memory for each contained string */
  1216. free(*matches++);
  1217. }
  1218. /* release the memory for the array */
  1219. free(this);
  1220. }
  1221. /*-------------------------------------------------------- */
  1222. void tinyrl_crlf(const tinyrl_t * this)
  1223. {
  1224. tinyrl_vt100_printf(this->term, "\n");
  1225. }
  1226. /*-------------------------------------------------------- */
  1227. /*
  1228. * Ring the terminal bell, obeying the setting of bell-style.
  1229. */
  1230. void tinyrl_ding(const tinyrl_t * this)
  1231. {
  1232. tinyrl_vt100_ding(this->term);
  1233. }
  1234. /*-------------------------------------------------------- */
  1235. void tinyrl_reset_line_state(tinyrl_t * this)
  1236. {
  1237. lub_string_free(this->last_buffer);
  1238. this->last_buffer = NULL;
  1239. this->last_line_size = 0;
  1240. tinyrl_redisplay(this);
  1241. }
  1242. /*-------------------------------------------------------- */
  1243. void tinyrl_replace_line(tinyrl_t * this, const char *text, int clear_undo)
  1244. {
  1245. size_t new_len = strlen(text);
  1246. /* ignored for now */
  1247. clear_undo = clear_undo;
  1248. /* ensure there is sufficient space */
  1249. if (tinyrl_extend_line_buffer(this, new_len)) {
  1250. /* overwrite the current contents of the buffer */
  1251. strcpy(this->buffer, text);
  1252. /* set the insert point and end point */
  1253. this->point = this->end = new_len;
  1254. }
  1255. tinyrl_redisplay(this);
  1256. }
  1257. /*-------------------------------------------------------- */
  1258. static tinyrl_match_e
  1259. tinyrl_do_complete(tinyrl_t * this, bool_t with_extensions)
  1260. {
  1261. tinyrl_match_e result = TINYRL_NO_MATCH;
  1262. char **matches = NULL;
  1263. unsigned int start, end;
  1264. bool_t completion = BOOL_FALSE;
  1265. bool_t prefix = BOOL_FALSE;
  1266. int i = 0;
  1267. /* find the start and end of the current word */
  1268. start = end = this->point;
  1269. while (start && !isspace(this->line[start - 1]))
  1270. start--;
  1271. if (this->attempted_completion_function) {
  1272. this->completion_over = BOOL_FALSE;
  1273. this->completion_error_over = BOOL_FALSE;
  1274. /* try and complete the current line buffer */
  1275. matches = this->attempted_completion_function(this,
  1276. this->line, start, end);
  1277. }
  1278. if (!matches && (BOOL_FALSE == this->completion_over)) {
  1279. /* insert default completion call here... */
  1280. }
  1281. if (!matches)
  1282. return result;
  1283. /* identify and insert a common prefix if there is one */
  1284. if (0 != strncmp(matches[0], &this->line[start],
  1285. strlen(matches[0]))) {
  1286. /*
  1287. * delete the original text not including
  1288. * the current insertion point character
  1289. */
  1290. if (this->end != end)
  1291. end--;
  1292. tinyrl_delete_text(this, start, end);
  1293. if (BOOL_FALSE == tinyrl_insert_text(this, matches[0]))
  1294. return TINYRL_NO_MATCH;
  1295. completion = BOOL_TRUE;
  1296. }
  1297. for (i = 1; matches[i]; i++) {
  1298. /* this is just a prefix string */
  1299. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  1300. prefix = BOOL_TRUE;
  1301. }
  1302. /* is there more than one completion? */
  1303. if (matches[2]) {
  1304. char **tmp = matches;
  1305. unsigned int max, len;
  1306. max = len = 0;
  1307. while (*tmp) {
  1308. size_t size = strlen(*tmp++);
  1309. len++;
  1310. if (size > max)
  1311. max = size;
  1312. }
  1313. if (completion)
  1314. result = TINYRL_COMPLETED_AMBIGUOUS;
  1315. else if (prefix)
  1316. result = TINYRL_MATCH_WITH_EXTENSIONS;
  1317. else
  1318. result = TINYRL_AMBIGUOUS;
  1319. if (with_extensions || !prefix) {
  1320. /* Either we always want to show extensions or
  1321. * we haven't been able to complete the current line
  1322. * and there is just a prefix, so let the user see the options
  1323. */
  1324. tinyrl_crlf(this);
  1325. tinyrl_display_matches(this, matches, len, max);
  1326. tinyrl_reset_line_state(this);
  1327. }
  1328. } else {
  1329. result = completion ?
  1330. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  1331. }
  1332. /* free the memory */
  1333. tinyrl_delete_matches(matches);
  1334. /* redisplay the line */
  1335. tinyrl_redisplay(this);
  1336. return result;
  1337. }
  1338. /*-------------------------------------------------------- */
  1339. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * this)
  1340. {
  1341. return tinyrl_do_complete(this, BOOL_TRUE);
  1342. }
  1343. /*-------------------------------------------------------- */
  1344. tinyrl_match_e tinyrl_complete(tinyrl_t * this)
  1345. {
  1346. return tinyrl_do_complete(this, BOOL_FALSE);
  1347. }
  1348. /*-------------------------------------------------------- */
  1349. void *tinyrl__get_context(const tinyrl_t * this)
  1350. {
  1351. return this->context;
  1352. }
  1353. /*--------------------------------------------------------- */
  1354. const char *tinyrl__get_line(const tinyrl_t * this)
  1355. {
  1356. return this->line;
  1357. }
  1358. /*--------------------------------------------------------- */
  1359. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * this)
  1360. {
  1361. return this->history;
  1362. }
  1363. /*--------------------------------------------------------- */
  1364. void tinyrl_completion_over(tinyrl_t * this)
  1365. {
  1366. this->completion_over = BOOL_TRUE;
  1367. }
  1368. /*--------------------------------------------------------- */
  1369. void tinyrl_completion_error_over(tinyrl_t * this)
  1370. {
  1371. this->completion_error_over = BOOL_TRUE;
  1372. }
  1373. /*--------------------------------------------------------- */
  1374. bool_t tinyrl_is_completion_error_over(const tinyrl_t * this)
  1375. {
  1376. return this->completion_error_over;
  1377. }
  1378. /*--------------------------------------------------------- */
  1379. void tinyrl_done(tinyrl_t * this)
  1380. {
  1381. this->done = BOOL_TRUE;
  1382. }
  1383. /*--------------------------------------------------------- */
  1384. void tinyrl_enable_echo(tinyrl_t * this)
  1385. {
  1386. this->echo_enabled = BOOL_TRUE;
  1387. }
  1388. /*--------------------------------------------------------- */
  1389. void tinyrl_disable_echo(tinyrl_t * this, char echo_char)
  1390. {
  1391. this->echo_enabled = BOOL_FALSE;
  1392. this->echo_char = echo_char;
  1393. }
  1394. /*--------------------------------------------------------- */
  1395. void tinyrl__set_istream(tinyrl_t * this, FILE * istream)
  1396. {
  1397. tinyrl_vt100__set_istream(this->term, istream);
  1398. if (istream) {
  1399. int fd;
  1400. this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  1401. /* Save terminal settings to restore on exit */
  1402. fd = fileno(istream);
  1403. tcgetattr(fd, &this->default_termios);
  1404. } else
  1405. this->isatty = BOOL_FALSE;
  1406. }
  1407. /*-------------------------------------------------------- */
  1408. bool_t tinyrl__get_isatty(const tinyrl_t * this)
  1409. {
  1410. assert(this);
  1411. if (!this)
  1412. return BOOL_FALSE;
  1413. // The machine interface can be forced. It means the interaction is not
  1414. // interactive and stdin/stdout considered as not terminal but just as
  1415. // a stream. So return fake value.
  1416. if (tinyrl_is_machine_interface(this))
  1417. return BOOL_FALSE;
  1418. return this->isatty;
  1419. }
  1420. /*-------------------------------------------------------- */
  1421. FILE *tinyrl__get_istream(const tinyrl_t * this)
  1422. {
  1423. return tinyrl_vt100__get_istream(this->term);
  1424. }
  1425. /*-------------------------------------------------------- */
  1426. FILE *tinyrl__get_ostream(const tinyrl_t * this)
  1427. {
  1428. return tinyrl_vt100__get_ostream(this->term);
  1429. }
  1430. /*-------------------------------------------------------- */
  1431. const char *tinyrl__get_prompt(const tinyrl_t * this)
  1432. {
  1433. return this->prompt;
  1434. }
  1435. /*-------------------------------------------------------- */
  1436. void tinyrl__set_prompt(tinyrl_t *this, const char *prompt)
  1437. {
  1438. if (this->prompt) {
  1439. lub_string_free(this->prompt);
  1440. this->prompt_size = 0;
  1441. this->prompt_len = 0;
  1442. }
  1443. this->prompt = lub_string_dup(prompt);
  1444. if (this->prompt) {
  1445. this->prompt_size = strlen(this->prompt);
  1446. this->prompt_len = utf8_nsyms(this, this->prompt,
  1447. this->prompt_size);
  1448. }
  1449. }
  1450. /*-------------------------------------------------------- */
  1451. bool_t tinyrl__get_utf8(const tinyrl_t * this)
  1452. {
  1453. return this->utf8;
  1454. }
  1455. /*-------------------------------------------------------- */
  1456. void tinyrl__set_utf8(tinyrl_t * this, bool_t utf8)
  1457. {
  1458. this->utf8 = utf8;
  1459. }
  1460. /*-------------------------------------------------------- */
  1461. void tinyrl__set_timeout(tinyrl_t *this, int timeout)
  1462. {
  1463. tinyrl_vt100__set_timeout(this->term, timeout);
  1464. }
  1465. /*-------------------------------------------------------- */
  1466. void tinyrl__set_timeout_fn(tinyrl_t *this,
  1467. tinyrl_timeout_fn_t *fn)
  1468. {
  1469. this->timeout_fn = fn;
  1470. }
  1471. /*-------------------------------------------------------- */
  1472. void tinyrl__set_keypress_fn(tinyrl_t *this,
  1473. tinyrl_keypress_fn_t *fn)
  1474. {
  1475. this->keypress_fn = fn;
  1476. }
  1477. /*-------------------------------------------------------- */
  1478. void tinyrl__set_hotkey_fn(tinyrl_t *this,
  1479. tinyrl_key_func_t *fn)
  1480. {
  1481. this->hotkey_fn = fn;
  1482. }
  1483. /*-------------------------------------------------------- */
  1484. bool_t tinyrl_is_quoting(const tinyrl_t * this)
  1485. {
  1486. bool_t result = BOOL_FALSE;
  1487. /* count the quotes upto the current insertion point */
  1488. unsigned int i = 0;
  1489. while (i < this->point) {
  1490. if (result && (this->line[i] == '\\')) {
  1491. i++;
  1492. if (i >= this->point)
  1493. break;
  1494. i++;
  1495. continue;
  1496. }
  1497. if (this->line[i++] == '"') {
  1498. result = result ? BOOL_FALSE : BOOL_TRUE;
  1499. }
  1500. }
  1501. return result;
  1502. }
  1503. /*-------------------------------------------------------- */
  1504. bool_t tinyrl_is_empty(const tinyrl_t *this)
  1505. {
  1506. return (this->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1507. }
  1508. /*--------------------------------------------------------- */
  1509. void tinyrl_limit_line_length(tinyrl_t * this, unsigned int length)
  1510. {
  1511. this->max_line_length = length;
  1512. }
  1513. /*--------------------------------------------------------- */
  1514. extern unsigned int tinyrl__get_width(const tinyrl_t *this)
  1515. {
  1516. return tinyrl_vt100__get_width(this->term);
  1517. }
  1518. /*--------------------------------------------------------- */
  1519. extern unsigned int tinyrl__get_height(const tinyrl_t *this)
  1520. {
  1521. return tinyrl_vt100__get_height(this->term);
  1522. }
  1523. /*----------------------------------------------------------*/
  1524. int tinyrl__save_history(const tinyrl_t *this, const char *fname)
  1525. {
  1526. return tinyrl_history_save(this->history, fname);
  1527. }
  1528. /*----------------------------------------------------------*/
  1529. int tinyrl__restore_history(tinyrl_t *this, const char *fname)
  1530. {
  1531. return tinyrl_history_restore(this->history, fname);
  1532. }
  1533. /*----------------------------------------------------------*/
  1534. void tinyrl__stifle_history(tinyrl_t *this, unsigned int stifle)
  1535. {
  1536. tinyrl_history_stifle(this->history, stifle);
  1537. }
  1538. /*--------------------------------------------------------- */
  1539. void tinyrl_set_machine_interface(tinyrl_t *this)
  1540. {
  1541. assert(this);
  1542. if (!this)
  1543. return;
  1544. this->machine_interface = BOOL_TRUE;
  1545. }
  1546. /*--------------------------------------------------------- */
  1547. void tinyrl_set_human_interface(tinyrl_t *this)
  1548. {
  1549. assert(this);
  1550. if (!this)
  1551. return;
  1552. this->machine_interface = BOOL_FALSE;
  1553. }
  1554. /*--------------------------------------------------------- */
  1555. bool_t tinyrl_is_machine_interface(const tinyrl_t *this)
  1556. {
  1557. assert(this);
  1558. if (!this)
  1559. return BOOL_FALSE;
  1560. return this->machine_interface;
  1561. }