1
0

tinyrl.c 43 KB

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