1
0

tinyrl.c 43 KB

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