tinyrl.c 45 KB

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