1
0

tinyrl.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  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. static void 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. static bool_t tinyrl_escape_seq(tinyrl_t *this, const char *esc_seq)
  468. {
  469. int key = 0;
  470. bool_t result = BOOL_FALSE;
  471. switch (tinyrl_vt100_escape_decode(this->term, esc_seq)) {
  472. case tinyrl_vt100_CURSOR_UP:
  473. result = tinyrl_key_up(this, key);
  474. break;
  475. case tinyrl_vt100_CURSOR_DOWN:
  476. result = tinyrl_key_down(this, key);
  477. break;
  478. case tinyrl_vt100_CURSOR_LEFT:
  479. result = tinyrl_key_left(this, key);
  480. break;
  481. case tinyrl_vt100_CURSOR_RIGHT:
  482. result = tinyrl_key_right(this, key);
  483. break;
  484. case tinyrl_vt100_HOME:
  485. result = tinyrl_key_start_of_line(this,key);
  486. break;
  487. case tinyrl_vt100_END:
  488. result = tinyrl_key_end_of_line(this,key);
  489. break;
  490. case tinyrl_vt100_DELETE:
  491. result = tinyrl_key_delete(this,key);
  492. break;
  493. case tinyrl_vt100_INSERT:
  494. case tinyrl_vt100_PGDOWN:
  495. case tinyrl_vt100_PGUP:
  496. case tinyrl_vt100_UNKNOWN:
  497. break;
  498. }
  499. return result;
  500. }
  501. /*-------------------------------------------------------- */
  502. static bool_t tinyrl_key_tab(tinyrl_t * this, int key)
  503. {
  504. bool_t result = BOOL_FALSE;
  505. tinyrl_match_e status = tinyrl_complete_with_extensions(this);
  506. switch (status) {
  507. case TINYRL_COMPLETED_MATCH:
  508. case TINYRL_MATCH:
  509. /* everything is OK with the world... */
  510. result = tinyrl_insert_text(this, " ");
  511. break;
  512. case TINYRL_NO_MATCH:
  513. case TINYRL_MATCH_WITH_EXTENSIONS:
  514. case TINYRL_AMBIGUOUS:
  515. case TINYRL_COMPLETED_AMBIGUOUS:
  516. /* oops don't change the result and let the bell ring */
  517. break;
  518. }
  519. /* keep the compiler happy */
  520. key = key;
  521. return result;
  522. }
  523. /*-------------------------------------------------------- */
  524. static void tinyrl_fini(tinyrl_t * this)
  525. {
  526. /* delete the history session */
  527. tinyrl_history_delete(this->history);
  528. /* delete the terminal session */
  529. tinyrl_vt100_delete(this->term);
  530. /* free up any dynamic strings */
  531. lub_string_free(this->buffer);
  532. lub_string_free(this->kill_string);
  533. lub_string_free(this->last_buffer);
  534. lub_string_free(this->prompt);
  535. }
  536. /*-------------------------------------------------------- */
  537. static void tinyrl_init(tinyrl_t * this, FILE * istream, FILE * ostream,
  538. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  539. {
  540. int i;
  541. for (i = 0; i < NUM_HANDLERS; i++) {
  542. this->handlers[i] = tinyrl_key_default;
  543. }
  544. /* Default handlers */
  545. this->handlers[KEY_CR] = tinyrl_key_crlf;
  546. this->handlers[KEY_LF] = tinyrl_key_crlf;
  547. this->handlers[KEY_ETX] = tinyrl_key_interrupt;
  548. this->handlers[KEY_DEL] = tinyrl_key_backspace;
  549. this->handlers[KEY_BS] = tinyrl_key_backspace;
  550. this->handlers[KEY_EOT] = tinyrl_key_delete;
  551. this->handlers[KEY_FF] = tinyrl_key_clear_screen;
  552. this->handlers[KEY_NAK] = tinyrl_key_erase_line;
  553. this->handlers[KEY_SOH] = tinyrl_key_start_of_line;
  554. this->handlers[KEY_ENQ] = tinyrl_key_end_of_line;
  555. this->handlers[KEY_VT] = tinyrl_key_kill;
  556. this->handlers[KEY_EM] = tinyrl_key_yank;
  557. this->handlers[KEY_HT] = tinyrl_key_tab;
  558. this->handlers[KEY_ETB] = tinyrl_key_backword;
  559. this->line = NULL;
  560. this->max_line_length = 0;
  561. this->prompt = NULL;
  562. this->prompt_size = 0;
  563. this->buffer = NULL;
  564. this->buffer_size = 0;
  565. this->done = BOOL_FALSE;
  566. this->completion_over = BOOL_FALSE;
  567. this->point = 0;
  568. this->end = 0;
  569. this->attempted_completion_function = complete_fn;
  570. this->timeout_fn = tinyrl_timeout_default;
  571. this->keypress_fn = NULL;
  572. this->hotkey_fn = NULL;
  573. this->state = 0;
  574. this->kill_string = NULL;
  575. this->echo_char = '\0';
  576. this->echo_enabled = BOOL_TRUE;
  577. this->last_buffer = NULL;
  578. this->last_point = 0;
  579. this->last_line_size = 0;
  580. this->utf8 = BOOL_FALSE;
  581. /* create the vt100 terminal */
  582. this->term = tinyrl_vt100_new(NULL, ostream);
  583. tinyrl__set_istream(this, istream);
  584. this->last_width = tinyrl_vt100__get_width(this->term);
  585. /* create the history */
  586. this->history = tinyrl_history_new(stifle);
  587. }
  588. /*-------------------------------------------------------- */
  589. int tinyrl_printf(const tinyrl_t * this, const char *fmt, ...)
  590. {
  591. va_list args;
  592. int len;
  593. va_start(args, fmt);
  594. len = tinyrl_vt100_vprintf(this->term, fmt, args);
  595. va_end(args);
  596. return len;
  597. }
  598. /*-------------------------------------------------------- */
  599. void tinyrl_delete(tinyrl_t * this)
  600. {
  601. assert(this);
  602. if (this) {
  603. /* let the object tidy itself up */
  604. tinyrl_fini(this);
  605. /* release the memory associate with this instance */
  606. free(this);
  607. }
  608. }
  609. /*-------------------------------------------------------- */
  610. /*#####################################
  611. * EXPORTED INTERFACE
  612. *##################################### */
  613. /*----------------------------------------------------------------------- */
  614. int tinyrl_getchar(const tinyrl_t * this)
  615. {
  616. return tinyrl_vt100_getchar(this->term);
  617. }
  618. /*----------------------------------------------------------------------- */
  619. static void tinyrl_internal_print(const tinyrl_t * this, const char *text)
  620. {
  621. if (this->echo_enabled) {
  622. /* simply echo the line */
  623. tinyrl_vt100_printf(this->term, "%s", text);
  624. } else {
  625. /* replace the line with echo char if defined */
  626. if (this->echo_char) {
  627. unsigned int i = strlen(text);
  628. while (i--) {
  629. tinyrl_vt100_printf(this->term, "%c",
  630. this->echo_char);
  631. }
  632. }
  633. }
  634. }
  635. /*----------------------------------------------------------------------- */
  636. static void tinyrl_internal_position(const tinyrl_t *this, int prompt_len,
  637. int line_len, unsigned int width)
  638. {
  639. int rows, cols;
  640. rows = ((line_len + prompt_len) / width) - (prompt_len / width);
  641. cols = ((line_len + prompt_len) % width) - (prompt_len % width);
  642. if (cols > 0)
  643. tinyrl_vt100_cursor_back(this->term, cols);
  644. else if (cols < 0)
  645. tinyrl_vt100_cursor_forward(this->term, -cols);
  646. if (rows > 0)
  647. tinyrl_vt100_cursor_up(this->term, rows);
  648. else if (rows < 0)
  649. tinyrl_vt100_cursor_down(this->term, -rows);
  650. }
  651. /*-------------------------------------------------------- */
  652. /* Jump to first free line after current multiline input */
  653. void tinyrl_multi_crlf(const tinyrl_t * this)
  654. {
  655. unsigned int line_size = strlen(this->last_buffer);
  656. unsigned int line_len = utf8_nsyms(this, this->last_buffer, line_size);
  657. unsigned int count = utf8_nsyms(this, this->last_buffer, this->last_point);
  658. tinyrl_internal_position(this, this->prompt_len + line_len,
  659. - (line_len - count), this->last_width);
  660. tinyrl_crlf(this);
  661. }
  662. /*----------------------------------------------------------------------- */
  663. void tinyrl_redisplay(tinyrl_t * this)
  664. {
  665. unsigned int line_size = strlen(this->line);
  666. unsigned int line_len = utf8_nsyms(this, this->line, line_size);
  667. unsigned int width = tinyrl_vt100__get_width(this->term);
  668. unsigned int count, eq_chars = 0;
  669. int cols;
  670. /* Prepare print position */
  671. if (this->last_buffer && (width == this->last_width)) {
  672. unsigned int eq_len = 0;
  673. /* If line and last line have the equal chars at begining */
  674. eq_chars = lub_string_equal_part(this->line, this->last_buffer,
  675. this->utf8);
  676. eq_len = utf8_nsyms(this, this->last_buffer, eq_chars);
  677. count = utf8_nsyms(this, this->last_buffer, this->last_point);
  678. tinyrl_internal_position(this, this->prompt_len + eq_len,
  679. count - eq_len, width);
  680. } else {
  681. /* Prepare to resize */
  682. if (width != this->last_width) {
  683. tinyrl_vt100_next_line(this->term);
  684. tinyrl_vt100_erase_down(this->term);
  685. }
  686. tinyrl_vt100_printf(this->term, "%s", this->prompt);
  687. }
  688. /* Print current line */
  689. tinyrl_internal_print(this, this->line + eq_chars);
  690. cols = (this->prompt_len + line_len) % width;
  691. if (!cols && (line_size - eq_chars))
  692. tinyrl_vt100_next_line(this->term);
  693. /* Erase down if current line is shorter than previous one */
  694. if (this->last_line_size > line_size)
  695. tinyrl_vt100_erase_down(this->term);
  696. /* Move the cursor to the insertion point */
  697. if (this->point < line_size) {
  698. unsigned int pre_len = utf8_nsyms(this,
  699. this->line, this->point);
  700. count = utf8_nsyms(this, this->line + this->point,
  701. line_size - this->point);
  702. tinyrl_internal_position(this, this->prompt_len + pre_len,
  703. count, width);
  704. }
  705. /* Update the display */
  706. (void)tinyrl_vt100_oflush(this->term);
  707. /* Save the last line buffer */
  708. lub_string_free(this->last_buffer);
  709. this->last_buffer = lub_string_dup(this->line);
  710. this->last_point = this->point;
  711. this->last_width = width;
  712. this->last_line_size = line_size;
  713. }
  714. /*----------------------------------------------------------------------- */
  715. tinyrl_t *tinyrl_new(FILE * istream, FILE * ostream,
  716. unsigned int stifle, tinyrl_completion_func_t * complete_fn)
  717. {
  718. tinyrl_t *this = NULL;
  719. this = malloc(sizeof(tinyrl_t));
  720. if (this)
  721. tinyrl_init(this, istream, ostream, stifle, complete_fn);
  722. return this;
  723. }
  724. /*----------------------------------------------------------------------- */
  725. static char *internal_insertline(tinyrl_t * this, char *buffer)
  726. {
  727. char *p;
  728. char *s = buffer;
  729. /* strip any spurious '\r' or '\n' */
  730. if ((p = strchr(buffer, '\r')))
  731. *p = '\0';
  732. if ((p = strchr(buffer, '\n')))
  733. *p = '\0';
  734. /* skip any whitespace at the beginning of the line */
  735. if (0 == this->point) {
  736. while (*s && isspace(*s))
  737. s++;
  738. }
  739. if (*s) {
  740. /* append this string to the input buffer */
  741. (void)tinyrl_insert_text(this, s);
  742. }
  743. /* echo the command to the output stream */
  744. tinyrl_redisplay(this);
  745. return s;
  746. }
  747. /*----------------------------------------------------------------------- */
  748. static char *internal_readline(tinyrl_t * this,
  749. void *context, const char *str)
  750. {
  751. FILE *istream = tinyrl_vt100__get_istream(this->term);
  752. char *result = NULL;
  753. int lerrno = 0;
  754. this->done = BOOL_FALSE;
  755. this->point = 0;
  756. this->end = 0;
  757. this->buffer = lub_string_dup("");
  758. this->buffer_size = strlen(this->buffer);
  759. this->line = this->buffer;
  760. this->context = context;
  761. /* Interactive session */
  762. if (this->isatty && !str) {
  763. unsigned int utf8_cont = 0; /* UTF-8 continue bytes */
  764. unsigned int esc_cont = 0; /* Escape sequence continues */
  765. char esc_seq[10]; /* Buffer for ESC sequence */
  766. char *esc_p = esc_seq;
  767. /* Set the terminal into raw mode */
  768. tty_set_raw_mode(this);
  769. tinyrl_reset_line_state(this);
  770. while (!this->done) {
  771. int key;
  772. key = tinyrl_getchar(this);
  773. /* Error || EOF || Timeout */
  774. if (key < 0) {
  775. if ((VT100_TIMEOUT == key) &&
  776. !this->timeout_fn(this))
  777. continue;
  778. /* It's time to finish the session */
  779. this->done = BOOL_TRUE;
  780. this->line = NULL;
  781. lerrno = ENOENT;
  782. continue;
  783. }
  784. /* Real key pressed */
  785. /* Common callback for any key */
  786. if (this->keypress_fn)
  787. this->keypress_fn(this, key);
  788. /* Check for ESC sequence. It's a special case. */
  789. if (!esc_cont && (key == KEY_ESC)) {
  790. esc_cont = 1; /* Start ESC sequence */
  791. esc_p = esc_seq;
  792. continue;
  793. }
  794. if (esc_cont) {
  795. /* Broken sequence */
  796. if (esc_p >= (esc_seq + sizeof(esc_seq) - 1)) {
  797. esc_cont = 0;
  798. continue;
  799. }
  800. /* Dump the control sequence into sequence buffer
  801. ANSI standard control sequences will end
  802. with a character between 64 - 126 */
  803. *esc_p = key & 0xff;
  804. esc_p++;
  805. /* This is an ANSI control sequence terminator code */
  806. if ((key != '[') && (key > 63)) {
  807. *esc_p = '\0';
  808. tinyrl_escape_seq(this, esc_seq);
  809. esc_cont = 0;
  810. tinyrl_redisplay(this);
  811. }
  812. continue;
  813. }
  814. /* Call the handler for this key */
  815. if (!this->handlers[key](this, key))
  816. tinyrl_ding(this);
  817. if (this->done) /* Some handler set the done flag */
  818. continue; /* It will break the loop */
  819. if (this->utf8) {
  820. if (!(UTF8_7BIT_MASK & key)) /* ASCII char */
  821. utf8_cont = 0;
  822. else if (utf8_cont && (UTF8_10 == (key & UTF8_MASK))) /* Continue byte */
  823. utf8_cont--;
  824. else if (UTF8_11 == (key & UTF8_MASK)) { /* First byte of multibyte char */
  825. /* Find out number of char's bytes */
  826. int b = key;
  827. utf8_cont = 0;
  828. while ((utf8_cont < 6) && (UTF8_10 != (b & UTF8_MASK))) {
  829. utf8_cont++;
  830. b = b << 1;
  831. }
  832. }
  833. }
  834. /* For non UTF-8 encoding the utf8_cont is always 0.
  835. For UTF-8 it's 0 when one-byte symbol or we get
  836. all bytes for the current multibyte character. */
  837. if (!utf8_cont)
  838. tinyrl_redisplay(this);
  839. }
  840. /* If the last character in the line (other than NULL)
  841. is a space remove it. */
  842. if (this->end && this->line && isspace(this->line[this->end - 1]))
  843. tinyrl_delete_text(this, this->end - 1, this->end);
  844. /* Restores the terminal mode */
  845. tty_restore_mode(this);
  846. /* Non-interactive session */
  847. } else {
  848. char *s = NULL, buffer[80];
  849. size_t len = sizeof(buffer);
  850. char *tmp = NULL;
  851. /* manually reset the line state without redisplaying */
  852. lub_string_free(this->last_buffer);
  853. this->last_buffer = NULL;
  854. if (str) {
  855. tmp = lub_string_dup(str);
  856. internal_insertline(this, tmp);
  857. } else {
  858. while (istream && (sizeof(buffer) == len) &&
  859. (s = fgets(buffer, sizeof(buffer), istream))) {
  860. s = internal_insertline(this, buffer);
  861. len = strlen(buffer) + 1; /* account for the '\0' */
  862. }
  863. if (!s || ((this->line[0] == '\0') && feof(istream))) {
  864. /* time to finish the session */
  865. this->line = NULL;
  866. lerrno = ENOENT;
  867. }
  868. }
  869. /*
  870. * check against fgets returning null as either error or end of file.
  871. * This is a measure to stop potential task spin on encountering an
  872. * error from fgets.
  873. */
  874. if (this->line && !this->handlers[KEY_LF](this, KEY_LF)) {
  875. /* an issue has occured */
  876. this->line = NULL;
  877. lerrno = ENOEXEC;
  878. }
  879. if (str)
  880. lub_string_free(tmp);
  881. }
  882. /*
  883. * duplicate the string for return to the client
  884. * we have to duplicate as we may be referencing a
  885. * history entry or our internal buffer
  886. */
  887. result = this->line ? lub_string_dup(this->line) : NULL;
  888. /* free our internal buffer */
  889. free(this->buffer);
  890. this->buffer = NULL;
  891. if (!result)
  892. errno = lerrno; /* get saved errno */
  893. return result;
  894. }
  895. /*----------------------------------------------------------------------- */
  896. char *tinyrl_readline(tinyrl_t * this, void *context)
  897. {
  898. return internal_readline(this, context, NULL);
  899. }
  900. /*----------------------------------------------------------------------- */
  901. char *tinyrl_forceline(tinyrl_t * this, void *context, const char *line)
  902. {
  903. return internal_readline(this, context, line);
  904. }
  905. /*----------------------------------------------------------------------- */
  906. /*
  907. * Ensure that buffer has enough space to hold len characters,
  908. * possibly reallocating it if necessary. The function returns BOOL_TRUE
  909. * if the line is successfully extended, BOOL_FALSE if not.
  910. */
  911. bool_t tinyrl_extend_line_buffer(tinyrl_t * this, unsigned int len)
  912. {
  913. bool_t result = BOOL_TRUE;
  914. char *new_buffer;
  915. size_t new_len = len;
  916. if (this->buffer_size >= len)
  917. return result;
  918. /*
  919. * What we do depends on whether we are limited by
  920. * memory or a user imposed limit.
  921. */
  922. if (this->max_line_length == 0) {
  923. /* make sure we don't realloc too often */
  924. if (new_len < this->buffer_size + 10)
  925. new_len = this->buffer_size + 10;
  926. /* leave space for terminator */
  927. new_buffer = realloc(this->buffer, new_len + 1);
  928. if (!new_buffer) {
  929. tinyrl_ding(this);
  930. result = BOOL_FALSE;
  931. } else {
  932. this->buffer_size = new_len;
  933. this->line = this->buffer = new_buffer;
  934. }
  935. } else {
  936. if (new_len < this->max_line_length) {
  937. /* Just reallocate once to the max size */
  938. new_buffer = realloc(this->buffer,
  939. this->max_line_length);
  940. if (!new_buffer) {
  941. tinyrl_ding(this);
  942. result = BOOL_FALSE;
  943. } else {
  944. this->buffer_size =
  945. this->max_line_length - 1;
  946. this->line = this->buffer = new_buffer;
  947. }
  948. } else {
  949. tinyrl_ding(this);
  950. result = BOOL_FALSE;
  951. }
  952. }
  953. return result;
  954. }
  955. /*----------------------------------------------------------------------- */
  956. /*
  957. * Insert text into the line at the current cursor position.
  958. */
  959. bool_t tinyrl_insert_text(tinyrl_t * this, const char *text)
  960. {
  961. unsigned int delta = strlen(text);
  962. /*
  963. * If the client wants to change the line ensure that the line and buffer
  964. * references are in sync
  965. */
  966. changed_line(this);
  967. if ((delta + this->end) > (this->buffer_size)) {
  968. /* extend the current buffer */
  969. if (BOOL_FALSE ==
  970. tinyrl_extend_line_buffer(this, this->end + delta))
  971. return BOOL_FALSE;
  972. }
  973. if (this->point < this->end) {
  974. /* move the current text to the right (including the terminator) */
  975. memmove(&this->buffer[this->point + delta],
  976. &this->buffer[this->point],
  977. (this->end - this->point) + 1);
  978. } else {
  979. /* terminate the string */
  980. this->buffer[this->end + delta] = '\0';
  981. }
  982. /* insert the new text */
  983. strncpy(&this->buffer[this->point], text, delta);
  984. /* now update the indexes */
  985. this->point += delta;
  986. this->end += delta;
  987. return BOOL_TRUE;
  988. }
  989. /*----------------------------------------------------------------------- */
  990. /*
  991. * A convenience function for displaying a list of strings in columnar
  992. * format on Readline's output stream. matches is the list of strings,
  993. * in argv format, such as a list of completion matches. len is the number
  994. * of strings in matches, and max is the length of the longest string in matches.
  995. * This function uses the setting of print-completions-horizontally to select
  996. * how the matches are displayed
  997. */
  998. void tinyrl_display_matches(const tinyrl_t *this,
  999. char *const *matches, unsigned int len, size_t max)
  1000. {
  1001. unsigned int width = tinyrl_vt100__get_width(this->term);
  1002. unsigned int cols, rows;
  1003. /* Find out column and rows number */
  1004. if (max < width)
  1005. cols = (width + 1) / (max + 1); /* allow for a space between words */
  1006. else
  1007. cols = 1;
  1008. rows = len / cols + 1;
  1009. assert(matches);
  1010. if (matches) {
  1011. unsigned int r, c;
  1012. len--, matches++; /* skip the subtitution string */
  1013. /* Print out a table of completions */
  1014. for (r = 0; r < rows && len; r++) {
  1015. for (c = 0; c < cols && len; c++) {
  1016. const char *match = *matches++;
  1017. len--;
  1018. if ((c + 1) == cols) /* Last str in row */
  1019. tinyrl_vt100_printf(this->term, "%s",
  1020. match);
  1021. else
  1022. tinyrl_vt100_printf(this->term, "%-*s ",
  1023. max, match);
  1024. }
  1025. tinyrl_crlf(this);
  1026. }
  1027. }
  1028. }
  1029. /*----------------------------------------------------------------------- */
  1030. /*
  1031. * Delete the text between start and end in the current line. (inclusive)
  1032. * This adjusts the rl_point and rl_end indexes appropriately.
  1033. */
  1034. void tinyrl_delete_text(tinyrl_t * this, unsigned int start, unsigned int end)
  1035. {
  1036. unsigned int delta;
  1037. /*
  1038. * If the client wants to change the line ensure that the line and buffer
  1039. * references are in sync
  1040. */
  1041. changed_line(this);
  1042. /* make sure we play it safe */
  1043. if (start > end) {
  1044. unsigned int tmp = end;
  1045. start = end;
  1046. end = tmp;
  1047. }
  1048. if (end > this->end)
  1049. end = this->end;
  1050. delta = (end - start) + 1;
  1051. /* move any text which is left */
  1052. memmove(&this->buffer[start],
  1053. &this->buffer[start + delta], this->end - end);
  1054. /* now adjust the indexs */
  1055. if (this->point >= start) {
  1056. if (this->point > end) {
  1057. /* move the insertion point back appropriately */
  1058. this->point -= delta;
  1059. } else {
  1060. /* move the insertion point to the start */
  1061. this->point = start;
  1062. }
  1063. }
  1064. if (this->end > end)
  1065. this->end -= delta;
  1066. else
  1067. this->end = start;
  1068. /* put a terminator at the end of the buffer */
  1069. this->buffer[this->end] = '\0';
  1070. }
  1071. /*----------------------------------------------------------------------- */
  1072. bool_t tinyrl_bind_key(tinyrl_t * this, int key, tinyrl_key_func_t * fn)
  1073. {
  1074. bool_t result = BOOL_FALSE;
  1075. if ((key >= 0) && (key < 256)) {
  1076. /* set the key handling function */
  1077. this->handlers[key] = fn;
  1078. result = BOOL_TRUE;
  1079. }
  1080. return result;
  1081. }
  1082. /*-------------------------------------------------------- */
  1083. /*
  1084. * Returns an array of strings which is a list of completions for text.
  1085. * If there are no completions, returns NULL. The first entry in the
  1086. * returned array is the substitution for text. The remaining entries
  1087. * are the possible completions. The array is terminated with a NULL pointer.
  1088. *
  1089. * entry_func is a function of two args, and returns a char *.
  1090. * The first argument is text. The second is a state argument;
  1091. * it is zero on the first call, and non-zero on subsequent calls.
  1092. * entry_func returns a NULL pointer to the caller when there are no
  1093. * more matches.
  1094. */
  1095. char **tinyrl_completion(tinyrl_t * this,
  1096. const char *line, unsigned int start, unsigned int end,
  1097. tinyrl_compentry_func_t * entry_func)
  1098. {
  1099. unsigned int state = 0;
  1100. size_t size = 1;
  1101. unsigned int offset = 1; /* Need at least one entry for the substitution */
  1102. char **matches = NULL;
  1103. char *match;
  1104. /* duplicate the string upto the insertion point */
  1105. char *text = lub_string_dupn(line, end);
  1106. /* now try and find possible completions */
  1107. while ((match = entry_func(this, text, start, state++))) {
  1108. if (size == offset) {
  1109. /* resize the buffer if needed - the +1 is for the NULL terminator */
  1110. size += 10;
  1111. matches =
  1112. realloc(matches, (sizeof(char *) * (size + 1)));
  1113. }
  1114. /* not much we can do... */
  1115. if (!matches)
  1116. break;
  1117. matches[offset] = match;
  1118. /*
  1119. * augment the substitute string with this entry
  1120. */
  1121. if (1 == offset) {
  1122. /* let's be optimistic */
  1123. matches[0] = lub_string_dup(match);
  1124. } else {
  1125. char *p = matches[0];
  1126. size_t match_len = strlen(p);
  1127. /* identify the common prefix */
  1128. while ((tolower(*p) == tolower(*match)) && match_len--) {
  1129. p++, match++;
  1130. }
  1131. /* terminate the prefix string */
  1132. *p = '\0';
  1133. }
  1134. offset++;
  1135. }
  1136. /* be a good memory citizen */
  1137. lub_string_free(text);
  1138. if (matches)
  1139. matches[offset] = NULL;
  1140. return matches;
  1141. }
  1142. /*-------------------------------------------------------- */
  1143. void tinyrl_delete_matches(char **this)
  1144. {
  1145. char **matches = this;
  1146. while (*matches) {
  1147. /* release the memory for each contained string */
  1148. free(*matches++);
  1149. }
  1150. /* release the memory for the array */
  1151. free(this);
  1152. }
  1153. /*-------------------------------------------------------- */
  1154. void tinyrl_crlf(const tinyrl_t * this)
  1155. {
  1156. tinyrl_vt100_printf(this->term, "\n");
  1157. }
  1158. /*-------------------------------------------------------- */
  1159. /*
  1160. * Ring the terminal bell, obeying the setting of bell-style.
  1161. */
  1162. void tinyrl_ding(const tinyrl_t * this)
  1163. {
  1164. tinyrl_vt100_ding(this->term);
  1165. }
  1166. /*-------------------------------------------------------- */
  1167. void tinyrl_reset_line_state(tinyrl_t * this)
  1168. {
  1169. lub_string_free(this->last_buffer);
  1170. this->last_buffer = NULL;
  1171. this->last_line_size = 0;
  1172. tinyrl_redisplay(this);
  1173. }
  1174. /*-------------------------------------------------------- */
  1175. void tinyrl_replace_line(tinyrl_t * this, const char *text, int clear_undo)
  1176. {
  1177. size_t new_len = strlen(text);
  1178. /* ignored for now */
  1179. clear_undo = clear_undo;
  1180. /* ensure there is sufficient space */
  1181. if (tinyrl_extend_line_buffer(this, new_len)) {
  1182. /* overwrite the current contents of the buffer */
  1183. strcpy(this->buffer, text);
  1184. /* set the insert point and end point */
  1185. this->point = this->end = new_len;
  1186. }
  1187. tinyrl_redisplay(this);
  1188. }
  1189. /*-------------------------------------------------------- */
  1190. static tinyrl_match_e
  1191. tinyrl_do_complete(tinyrl_t * this, bool_t with_extensions)
  1192. {
  1193. tinyrl_match_e result = TINYRL_NO_MATCH;
  1194. char **matches = NULL;
  1195. unsigned int start, end;
  1196. bool_t completion = BOOL_FALSE;
  1197. bool_t prefix = BOOL_FALSE;
  1198. int i = 0;
  1199. /* find the start and end of the current word */
  1200. start = end = this->point;
  1201. while (start && !isspace(this->line[start - 1]))
  1202. start--;
  1203. if (this->attempted_completion_function) {
  1204. this->completion_over = BOOL_FALSE;
  1205. this->completion_error_over = BOOL_FALSE;
  1206. /* try and complete the current line buffer */
  1207. matches = this->attempted_completion_function(this,
  1208. this->line, start, end);
  1209. }
  1210. if (!matches && (BOOL_FALSE == this->completion_over)) {
  1211. /* insert default completion call here... */
  1212. }
  1213. if (!matches)
  1214. return result;
  1215. /* identify and insert a common prefix if there is one */
  1216. if (0 != strncmp(matches[0], &this->line[start],
  1217. strlen(matches[0]))) {
  1218. /*
  1219. * delete the original text not including
  1220. * the current insertion point character
  1221. */
  1222. if (this->end != end)
  1223. end--;
  1224. tinyrl_delete_text(this, start, end);
  1225. if (BOOL_FALSE == tinyrl_insert_text(this, matches[0]))
  1226. return TINYRL_NO_MATCH;
  1227. completion = BOOL_TRUE;
  1228. }
  1229. for (i = 1; matches[i]; i++) {
  1230. /* this is just a prefix string */
  1231. if (0 == lub_string_nocasecmp(matches[0], matches[i]))
  1232. prefix = BOOL_TRUE;
  1233. }
  1234. /* is there more than one completion? */
  1235. if (matches[2]) {
  1236. char **tmp = matches;
  1237. unsigned int max, len;
  1238. max = len = 0;
  1239. while (*tmp) {
  1240. size_t size = strlen(*tmp++);
  1241. len++;
  1242. if (size > max)
  1243. max = size;
  1244. }
  1245. if (completion)
  1246. result = TINYRL_COMPLETED_AMBIGUOUS;
  1247. else if (prefix)
  1248. result = TINYRL_MATCH_WITH_EXTENSIONS;
  1249. else
  1250. result = TINYRL_AMBIGUOUS;
  1251. if (with_extensions || !prefix) {
  1252. /* Either we always want to show extensions or
  1253. * we haven't been able to complete the current line
  1254. * and there is just a prefix, so let the user see the options
  1255. */
  1256. tinyrl_crlf(this);
  1257. tinyrl_display_matches(this, matches, len, max);
  1258. tinyrl_reset_line_state(this);
  1259. }
  1260. } else {
  1261. result = completion ?
  1262. TINYRL_COMPLETED_MATCH : TINYRL_MATCH;
  1263. }
  1264. /* free the memory */
  1265. tinyrl_delete_matches(matches);
  1266. /* redisplay the line */
  1267. tinyrl_redisplay(this);
  1268. return result;
  1269. }
  1270. /*-------------------------------------------------------- */
  1271. tinyrl_match_e tinyrl_complete_with_extensions(tinyrl_t * this)
  1272. {
  1273. return tinyrl_do_complete(this, BOOL_TRUE);
  1274. }
  1275. /*-------------------------------------------------------- */
  1276. tinyrl_match_e tinyrl_complete(tinyrl_t * this)
  1277. {
  1278. return tinyrl_do_complete(this, BOOL_FALSE);
  1279. }
  1280. /*-------------------------------------------------------- */
  1281. void *tinyrl__get_context(const tinyrl_t * this)
  1282. {
  1283. return this->context;
  1284. }
  1285. /*--------------------------------------------------------- */
  1286. const char *tinyrl__get_line(const tinyrl_t * this)
  1287. {
  1288. return this->line;
  1289. }
  1290. /*--------------------------------------------------------- */
  1291. tinyrl_history_t *tinyrl__get_history(const tinyrl_t * this)
  1292. {
  1293. return this->history;
  1294. }
  1295. /*--------------------------------------------------------- */
  1296. void tinyrl_completion_over(tinyrl_t * this)
  1297. {
  1298. this->completion_over = BOOL_TRUE;
  1299. }
  1300. /*--------------------------------------------------------- */
  1301. void tinyrl_completion_error_over(tinyrl_t * this)
  1302. {
  1303. this->completion_error_over = BOOL_TRUE;
  1304. }
  1305. /*--------------------------------------------------------- */
  1306. bool_t tinyrl_is_completion_error_over(const tinyrl_t * this)
  1307. {
  1308. return this->completion_error_over;
  1309. }
  1310. /*--------------------------------------------------------- */
  1311. void tinyrl_done(tinyrl_t * this)
  1312. {
  1313. this->done = BOOL_TRUE;
  1314. }
  1315. /*--------------------------------------------------------- */
  1316. void tinyrl_enable_echo(tinyrl_t * this)
  1317. {
  1318. this->echo_enabled = BOOL_TRUE;
  1319. }
  1320. /*--------------------------------------------------------- */
  1321. void tinyrl_disable_echo(tinyrl_t * this, char echo_char)
  1322. {
  1323. this->echo_enabled = BOOL_FALSE;
  1324. this->echo_char = echo_char;
  1325. }
  1326. /*--------------------------------------------------------- */
  1327. void tinyrl__set_istream(tinyrl_t * this, FILE * istream)
  1328. {
  1329. tinyrl_vt100__set_istream(this->term, istream);
  1330. if (istream) {
  1331. int fd;
  1332. this->isatty = isatty(fileno(istream)) ? BOOL_TRUE : BOOL_FALSE;
  1333. /* Save terminal settings to restore on exit */
  1334. fd = fileno(istream);
  1335. tcgetattr(fd, &this->default_termios);
  1336. } else
  1337. this->isatty = BOOL_FALSE;
  1338. }
  1339. /*-------------------------------------------------------- */
  1340. bool_t tinyrl__get_isatty(const tinyrl_t * this)
  1341. {
  1342. return this->isatty;
  1343. }
  1344. /*-------------------------------------------------------- */
  1345. FILE *tinyrl__get_istream(const tinyrl_t * this)
  1346. {
  1347. return tinyrl_vt100__get_istream(this->term);
  1348. }
  1349. /*-------------------------------------------------------- */
  1350. FILE *tinyrl__get_ostream(const tinyrl_t * this)
  1351. {
  1352. return tinyrl_vt100__get_ostream(this->term);
  1353. }
  1354. /*-------------------------------------------------------- */
  1355. const char *tinyrl__get_prompt(const tinyrl_t * this)
  1356. {
  1357. return this->prompt;
  1358. }
  1359. /*-------------------------------------------------------- */
  1360. void tinyrl__set_prompt(tinyrl_t *this, const char *prompt)
  1361. {
  1362. if (this->prompt) {
  1363. lub_string_free(this->prompt);
  1364. this->prompt_size = 0;
  1365. this->prompt_len = 0;
  1366. }
  1367. this->prompt = lub_string_dup(prompt);
  1368. if (this->prompt) {
  1369. this->prompt_size = strlen(this->prompt);
  1370. this->prompt_len = utf8_nsyms(this, this->prompt,
  1371. this->prompt_size);
  1372. }
  1373. }
  1374. /*-------------------------------------------------------- */
  1375. bool_t tinyrl__get_utf8(const tinyrl_t * this)
  1376. {
  1377. return this->utf8;
  1378. }
  1379. /*-------------------------------------------------------- */
  1380. void tinyrl__set_utf8(tinyrl_t * this, bool_t utf8)
  1381. {
  1382. this->utf8 = utf8;
  1383. }
  1384. /*-------------------------------------------------------- */
  1385. void tinyrl__set_timeout(tinyrl_t *this, int timeout)
  1386. {
  1387. tinyrl_vt100__set_timeout(this->term, timeout);
  1388. }
  1389. /*-------------------------------------------------------- */
  1390. void tinyrl__set_timeout_fn(tinyrl_t *this,
  1391. tinyrl_timeout_fn_t *fn)
  1392. {
  1393. this->timeout_fn = fn;
  1394. }
  1395. /*-------------------------------------------------------- */
  1396. void tinyrl__set_keypress_fn(tinyrl_t *this,
  1397. tinyrl_keypress_fn_t *fn)
  1398. {
  1399. this->keypress_fn = fn;
  1400. }
  1401. /*-------------------------------------------------------- */
  1402. void tinyrl__set_hotkey_fn(tinyrl_t *this,
  1403. tinyrl_key_func_t *fn)
  1404. {
  1405. this->hotkey_fn = fn;
  1406. }
  1407. /*-------------------------------------------------------- */
  1408. bool_t tinyrl_is_quoting(const tinyrl_t * this)
  1409. {
  1410. bool_t result = BOOL_FALSE;
  1411. /* count the quotes upto the current insertion point */
  1412. unsigned int i = 0;
  1413. while (i < this->point) {
  1414. if (result && (this->line[i] == '\\')) {
  1415. i++;
  1416. if (i >= this->point)
  1417. break;
  1418. i++;
  1419. continue;
  1420. }
  1421. if (this->line[i++] == '"') {
  1422. result = result ? BOOL_FALSE : BOOL_TRUE;
  1423. }
  1424. }
  1425. return result;
  1426. }
  1427. /*-------------------------------------------------------- */
  1428. bool_t tinyrl_is_empty(const tinyrl_t *this)
  1429. {
  1430. return (this->point == 0) ? BOOL_TRUE : BOOL_FALSE;
  1431. }
  1432. /*--------------------------------------------------------- */
  1433. void tinyrl_limit_line_length(tinyrl_t * this, unsigned int length)
  1434. {
  1435. this->max_line_length = length;
  1436. }
  1437. /*--------------------------------------------------------- */
  1438. extern unsigned int tinyrl__get_width(const tinyrl_t *this)
  1439. {
  1440. return tinyrl_vt100__get_width(this->term);
  1441. }
  1442. /*--------------------------------------------------------- */
  1443. extern unsigned int tinyrl__get_height(const tinyrl_t *this)
  1444. {
  1445. return tinyrl_vt100__get_height(this->term);
  1446. }
  1447. /*----------------------------------------------------------*/
  1448. int tinyrl__save_history(const tinyrl_t *this, const char *fname)
  1449. {
  1450. return tinyrl_history_save(this->history, fname);
  1451. }
  1452. /*----------------------------------------------------------*/
  1453. int tinyrl__restore_history(tinyrl_t *this, const char *fname)
  1454. {
  1455. return tinyrl_history_restore(this->history, fname);
  1456. }
  1457. /*----------------------------------------------------------*/
  1458. void tinyrl__stifle_history(tinyrl_t *this, unsigned int stifle)
  1459. {
  1460. tinyrl_history_stifle(this->history, stifle);
  1461. }
  1462. /*--------------------------------------------------------- */