1
0

tinyrl.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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. /*--------------------------------------------------------- */