tinyrl.c 43 KB

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