tinyrl.c 43 KB

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