1
0

linenoise.cpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. #ifndef _WIN32
  2. /*
  3. * You can find the latest source code at:
  4. *
  5. * http://github.com/ericcurtin/linenoise.cpp
  6. *
  7. * Does a number of crazy assumptions that happen to be true in 99.9999% of
  8. * the 2010 UNIX computers around.
  9. *
  10. * ------------------------------------------------------------------------
  11. *
  12. * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
  13. * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
  14. * Copyright (c) 2025, Eric Curtin <ericcurtin17 at gmail dot com>
  15. *
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions are
  20. * met:
  21. *
  22. * * Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * * Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * ------------------------------------------------------------------------
  42. *
  43. * References:
  44. * - http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
  45. * - http://www.3waylabs.com/nw/WWW/products/wizcon/vt220.html
  46. *
  47. * Todo list:
  48. * - Filter bogus Ctrl+<char> combinations.
  49. * - Win32 support
  50. *
  51. * Bloat:
  52. * - History search like Ctrl+r in readline?
  53. *
  54. * List of escape sequences used by this program, we do everything just
  55. * with three sequences. In order to be so cheap we may have some
  56. * flickering effect with some slow terminal, but the lesser sequences
  57. * the more compatible.
  58. *
  59. * EL (Erase Line)
  60. * Sequence: ESC [ n K
  61. * Effect: if n is 0 or missing, clear from cursor to end of line
  62. * Effect: if n is 1, clear from beginning of line to cursor
  63. * Effect: if n is 2, clear entire line
  64. *
  65. * CUF (CUrsor Forward)
  66. * Sequence: ESC [ n C
  67. * Effect: moves cursor forward n chars
  68. *
  69. * CUB (CUrsor Backward)
  70. * Sequence: ESC [ n D
  71. * Effect: moves cursor backward n chars
  72. *
  73. * The following is used to get the terminal width if getting
  74. * the width with the TIOCGWINSZ ioctl fails
  75. *
  76. * DSR (Device Status Report)
  77. * Sequence: ESC [ 6 n
  78. * Effect: reports the current cursor position as ESC [ n ; m R
  79. * where n is the row and m is the column
  80. *
  81. * When multi line mode is enabled, we also use an additional escape
  82. * sequence. However multi line editing is disabled by default.
  83. *
  84. * CUU (Cursor Up)
  85. * Sequence: ESC [ n A
  86. * Effect: moves cursor up of n chars.
  87. *
  88. * CUD (Cursor Down)
  89. * Sequence: ESC [ n B
  90. * Effect: moves cursor down of n chars.
  91. *
  92. * When linenoiseClearScreen() is called, two additional escape sequences
  93. * are used in order to clear the screen and position the cursor at home
  94. * position.
  95. *
  96. * CUP (Cursor position)
  97. * Sequence: ESC [ H
  98. * Effect: moves the cursor to upper left corner
  99. *
  100. * ED (Erase display)
  101. * Sequence: ESC [ 2 J
  102. * Effect: clear the whole screen
  103. *
  104. */
  105. # include "linenoise.h"
  106. # include <ctype.h>
  107. # include <errno.h>
  108. # include <poll.h>
  109. # include <stdio.h>
  110. # include <string.h>
  111. # include <sys/file.h>
  112. # include <sys/ioctl.h>
  113. # include <sys/stat.h>
  114. # include <sys/types.h>
  115. # include <termios.h>
  116. # include <unistd.h>
  117. # include <memory>
  118. # include <string>
  119. # include <vector>
  120. # define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100
  121. # define LINENOISE_MAX_LINE 4096
  122. static std::vector<const char *> unsupported_term = { "dumb", "cons25", "emacs" };
  123. static linenoiseCompletionCallback *completionCallback = NULL;
  124. static linenoiseHintsCallback *hintsCallback = NULL;
  125. static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
  126. static char *linenoiseNoTTY(void);
  127. static void refreshLineWithCompletion(struct linenoiseState *ls, linenoiseCompletions *lc, int flags);
  128. static void refreshLineWithFlags(struct linenoiseState *l, int flags);
  129. static struct termios orig_termios; /* In order to restore at exit.*/
  130. static int maskmode = 0; /* Show "***" instead of input. For passwords. */
  131. static int rawmode = 0; /* For atexit() function to check if restore is needed*/
  132. static int mlmode = 0; /* Multi line mode. Default is single line. */
  133. static int atexit_registered = 0; /* Register atexit just 1 time. */
  134. static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
  135. static int history_len = 0;
  136. static char **history = NULL;
  137. enum KEY_ACTION{
  138. KEY_NULL = 0, /* NULL */
  139. CTRL_A = 1, /* Ctrl+a */
  140. CTRL_B = 2, /* Ctrl-b */
  141. CTRL_C = 3, /* Ctrl-c */
  142. CTRL_D = 4, /* Ctrl-d */
  143. CTRL_E = 5, /* Ctrl-e */
  144. CTRL_F = 6, /* Ctrl-f */
  145. CTRL_H = 8, /* Ctrl-h */
  146. TAB = 9, /* Tab */
  147. CTRL_K = 11, /* Ctrl+k */
  148. CTRL_L = 12, /* Ctrl+l */
  149. ENTER = 13, /* Enter */
  150. CTRL_N = 14, /* Ctrl-n */
  151. CTRL_P = 16, /* Ctrl-p */
  152. CTRL_T = 20, /* Ctrl-t */
  153. CTRL_U = 21, /* Ctrl+u */
  154. CTRL_W = 23, /* Ctrl+w */
  155. ESC = 27, /* Escape */
  156. BACKSPACE = 127 /* Backspace */
  157. };
  158. static void linenoiseAtExit(void);
  159. int linenoiseHistoryAdd(const char *line);
  160. #define REFRESH_CLEAN (1<<0) // Clean the old prompt from the screen
  161. #define REFRESH_WRITE (1<<1) // Rewrite the prompt on the screen.
  162. #define REFRESH_ALL (REFRESH_CLEAN|REFRESH_WRITE) // Do both.
  163. static void refreshLine(struct linenoiseState *l);
  164. class File {
  165. public:
  166. FILE * file = nullptr;
  167. FILE * open(const std::string & filename, const char * mode) {
  168. file = fopen(filename.c_str(), mode);
  169. return file;
  170. }
  171. int lock() {
  172. if (file) {
  173. fd = fileno(file);
  174. if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
  175. fd = -1;
  176. return 1;
  177. }
  178. }
  179. return 0;
  180. }
  181. ~File() {
  182. if (fd >= 0) {
  183. flock(fd, LOCK_UN);
  184. }
  185. if (file) {
  186. fclose(file);
  187. }
  188. }
  189. private:
  190. int fd = -1;
  191. };
  192. #if 0
  193. /* Debugging function. */
  194. __attribute__((format(printf, 1, 2)))
  195. static void lndebug(const char *fmt, ...) {
  196. static File file;
  197. if (file.file == nullptr) {
  198. file.open("/tmp/lndebug.txt", "a");
  199. }
  200. if (file.file != nullptr) {
  201. va_list args;
  202. va_start(args, fmt);
  203. vfprintf(file.file, fmt, args);
  204. va_end(args);
  205. fflush(file.file);
  206. }
  207. }
  208. #endif
  209. /* ========================== Encoding functions ============================= */
  210. /* Get length of previous UTF8 codepoint */
  211. static size_t prevUtf8CodePointLen(const char * buf, int pos) {
  212. int end = pos--;
  213. while (pos >= 0 && ((unsigned char) buf[pos] & 0xC0) == 0x80) {
  214. pos--;
  215. }
  216. return end - pos;
  217. }
  218. /* Convert UTF8 to Unicode code point */
  219. static size_t utf8BytesToCodePoint(const char * buf, size_t len, int * cp) {
  220. if (len) {
  221. unsigned char byte = buf[0];
  222. if ((byte & 0x80) == 0) {
  223. *cp = byte;
  224. return 1;
  225. } else if ((byte & 0xE0) == 0xC0) {
  226. if (len >= 2) {
  227. *cp = (((unsigned long) (buf[0] & 0x1F)) << 6) | ((unsigned long) (buf[1] & 0x3F));
  228. return 2;
  229. }
  230. } else if ((byte & 0xF0) == 0xE0) {
  231. if (len >= 3) {
  232. *cp = (((unsigned long) (buf[0] & 0x0F)) << 12) | (((unsigned long) (buf[1] & 0x3F)) << 6) |
  233. ((unsigned long) (buf[2] & 0x3F));
  234. return 3;
  235. }
  236. } else if ((byte & 0xF8) == 0xF0) {
  237. if (len >= 4) {
  238. *cp = (((unsigned long) (buf[0] & 0x07)) << 18) | (((unsigned long) (buf[1] & 0x3F)) << 12) |
  239. (((unsigned long) (buf[2] & 0x3F)) << 6) | ((unsigned long) (buf[3] & 0x3F));
  240. return 4;
  241. }
  242. }
  243. }
  244. return 0;
  245. }
  246. /* Check if the code is a wide character */
  247. static const unsigned long wideCharTable[][2] = {
  248. /* BEGIN: WIDE CHAR TABLE */
  249. { 0x1100, 0x115F },
  250. { 0x231A, 0x231B },
  251. { 0x2329, 0x232A },
  252. { 0x23E9, 0x23EC },
  253. { 0x23F0, 0x23F0 },
  254. { 0x23F3, 0x23F3 },
  255. { 0x25FD, 0x25FE },
  256. { 0x2614, 0x2615 },
  257. { 0x2630, 0x2637 },
  258. { 0x2648, 0x2653 },
  259. { 0x267F, 0x267F },
  260. { 0x268A, 0x268F },
  261. { 0x2693, 0x2693 },
  262. { 0x26A1, 0x26A1 },
  263. { 0x26AA, 0x26AB },
  264. { 0x26BD, 0x26BE },
  265. { 0x26C4, 0x26C5 },
  266. { 0x26CE, 0x26CE },
  267. { 0x26D4, 0x26D4 },
  268. { 0x26EA, 0x26EA },
  269. { 0x26F2, 0x26F3 },
  270. { 0x26F5, 0x26F5 },
  271. { 0x26FA, 0x26FA },
  272. { 0x26FD, 0x26FD },
  273. { 0x2705, 0x2705 },
  274. { 0x270A, 0x270B },
  275. { 0x2728, 0x2728 },
  276. { 0x274C, 0x274C },
  277. { 0x274E, 0x274E },
  278. { 0x2753, 0x2755 },
  279. { 0x2757, 0x2757 },
  280. { 0x2795, 0x2797 },
  281. { 0x27B0, 0x27B0 },
  282. { 0x27BF, 0x27BF },
  283. { 0x2B1B, 0x2B1C },
  284. { 0x2B50, 0x2B50 },
  285. { 0x2B55, 0x2B55 },
  286. { 0x2E80, 0x2E99 },
  287. { 0x2E9B, 0x2EF3 },
  288. { 0x2F00, 0x2FD5 },
  289. { 0x2FF0, 0x303E },
  290. { 0x3041, 0x3096 },
  291. { 0x3099, 0x30FF },
  292. { 0x3105, 0x312F },
  293. { 0x3131, 0x318E },
  294. { 0x3190, 0x31E5 },
  295. { 0x31EF, 0x321E },
  296. { 0x3220, 0x3247 },
  297. { 0x3250, 0xA48C },
  298. { 0xA490, 0xA4C6 },
  299. { 0xA960, 0xA97C },
  300. { 0xAC00, 0xD7A3 },
  301. { 0xF900, 0xFAFF },
  302. { 0xFE10, 0xFE19 },
  303. { 0xFE30, 0xFE52 },
  304. { 0xFE54, 0xFE66 },
  305. { 0xFE68, 0xFE6B },
  306. { 0xFF01, 0xFF60 },
  307. { 0xFFE0, 0xFFE6 },
  308. { 0x16FE0, 0x16FE4 },
  309. { 0x16FF0, 0x16FF1 },
  310. { 0x17000, 0x187F7 },
  311. { 0x18800, 0x18CD5 },
  312. { 0x18CFF, 0x18D08 },
  313. { 0x1AFF0, 0x1AFF3 },
  314. { 0x1AFF5, 0x1AFFB },
  315. { 0x1AFFD, 0x1AFFE },
  316. { 0x1B000, 0x1B122 },
  317. { 0x1B132, 0x1B132 },
  318. { 0x1B150, 0x1B152 },
  319. { 0x1B155, 0x1B155 },
  320. { 0x1B164, 0x1B167 },
  321. { 0x1B170, 0x1B2FB },
  322. { 0x1D300, 0x1D356 },
  323. { 0x1D360, 0x1D376 },
  324. { 0x1F004, 0x1F004 },
  325. { 0x1F0CF, 0x1F0CF },
  326. { 0x1F18E, 0x1F18E },
  327. { 0x1F191, 0x1F19A },
  328. { 0x1F200, 0x1F202 },
  329. { 0x1F210, 0x1F23B },
  330. { 0x1F240, 0x1F248 },
  331. { 0x1F250, 0x1F251 },
  332. { 0x1F260, 0x1F265 },
  333. { 0x1F300, 0x1F320 },
  334. { 0x1F32D, 0x1F335 },
  335. { 0x1F337, 0x1F37C },
  336. { 0x1F37E, 0x1F393 },
  337. { 0x1F3A0, 0x1F3CA },
  338. { 0x1F3CF, 0x1F3D3 },
  339. { 0x1F3E0, 0x1F3F0 },
  340. { 0x1F3F4, 0x1F3F4 },
  341. { 0x1F3F8, 0x1F43E },
  342. { 0x1F440, 0x1F440 },
  343. { 0x1F442, 0x1F4FC },
  344. { 0x1F4FF, 0x1F53D },
  345. { 0x1F54B, 0x1F54E },
  346. { 0x1F550, 0x1F567 },
  347. { 0x1F57A, 0x1F57A },
  348. { 0x1F595, 0x1F596 },
  349. { 0x1F5A4, 0x1F5A4 },
  350. { 0x1F5FB, 0x1F64F },
  351. { 0x1F680, 0x1F6C5 },
  352. { 0x1F6CC, 0x1F6CC },
  353. { 0x1F6D0, 0x1F6D2 },
  354. { 0x1F6D5, 0x1F6D7 },
  355. { 0x1F6DC, 0x1F6DF },
  356. { 0x1F6EB, 0x1F6EC },
  357. { 0x1F6F4, 0x1F6FC },
  358. { 0x1F7E0, 0x1F7EB },
  359. { 0x1F7F0, 0x1F7F0 },
  360. { 0x1F90C, 0x1F93A },
  361. { 0x1F93C, 0x1F945 },
  362. { 0x1F947, 0x1F9FF },
  363. { 0x1FA70, 0x1FA7C },
  364. { 0x1FA80, 0x1FA89 },
  365. { 0x1FA8F, 0x1FAC6 },
  366. { 0x1FACE, 0x1FADC },
  367. { 0x1FADF, 0x1FAE9 },
  368. { 0x1FAF0, 0x1FAF8 },
  369. { 0x20000, 0x2FFFD },
  370. { 0x30000, 0x3FFFD }
  371. /* END: WIDE CHAR TABLE */
  372. };
  373. static const size_t wideCharTableSize = sizeof(wideCharTable) / sizeof(wideCharTable[0]);
  374. static bool isWideChar(unsigned long cp) {
  375. for (size_t i = 0; i < wideCharTableSize; i++) {
  376. auto first_code = wideCharTable[i][0];
  377. auto last_code = wideCharTable[i][1];
  378. if (first_code > cp) {
  379. return false;
  380. }
  381. if (first_code <= cp && cp <= last_code) {
  382. return true;
  383. }
  384. }
  385. return false;
  386. }
  387. /* Check if the code is a combining character */
  388. static const unsigned long combiningCharTable[] = {
  389. /* BEGIN: COMBINING CHAR TABLE */
  390. 0x0300, 0x0301, 0x0302, 0x0303, 0x0304, 0x0305, 0x0306, 0x0307, 0x0308, 0x0309, 0x030A, 0x030B, 0x030C,
  391. 0x030D, 0x030E, 0x030F, 0x0310, 0x0311, 0x0312, 0x0313, 0x0314, 0x0315, 0x0316, 0x0317, 0x0318, 0x0319,
  392. 0x031A, 0x031B, 0x031C, 0x031D, 0x031E, 0x031F, 0x0320, 0x0321, 0x0322, 0x0323, 0x0324, 0x0325, 0x0326,
  393. 0x0327, 0x0328, 0x0329, 0x032A, 0x032B, 0x032C, 0x032D, 0x032E, 0x032F, 0x0330, 0x0331, 0x0332, 0x0333,
  394. 0x0334, 0x0335, 0x0336, 0x0337, 0x0338, 0x0339, 0x033A, 0x033B, 0x033C, 0x033D, 0x033E, 0x033F, 0x0340,
  395. 0x0341, 0x0342, 0x0343, 0x0344, 0x0345, 0x0346, 0x0347, 0x0348, 0x0349, 0x034A, 0x034B, 0x034C, 0x034D,
  396. 0x034E, 0x034F, 0x0350, 0x0351, 0x0352, 0x0353, 0x0354, 0x0355, 0x0356, 0x0357, 0x0358, 0x0359, 0x035A,
  397. 0x035B, 0x035C, 0x035D, 0x035E, 0x035F, 0x0360, 0x0361, 0x0362, 0x0363, 0x0364, 0x0365, 0x0366, 0x0367,
  398. 0x0368, 0x0369, 0x036A, 0x036B, 0x036C, 0x036D, 0x036E, 0x036F, 0x0483, 0x0484, 0x0485, 0x0486, 0x0487,
  399. 0x0591, 0x0592, 0x0593, 0x0594, 0x0595, 0x0596, 0x0597, 0x0598, 0x0599, 0x059A, 0x059B, 0x059C, 0x059D,
  400. 0x059E, 0x059F, 0x05A0, 0x05A1, 0x05A2, 0x05A3, 0x05A4, 0x05A5, 0x05A6, 0x05A7, 0x05A8, 0x05A9, 0x05AA,
  401. 0x05AB, 0x05AC, 0x05AD, 0x05AE, 0x05AF, 0x05B0, 0x05B1, 0x05B2, 0x05B3, 0x05B4, 0x05B5, 0x05B6, 0x05B7,
  402. 0x05B8, 0x05B9, 0x05BA, 0x05BB, 0x05BC, 0x05BD, 0x05BF, 0x05C1, 0x05C2, 0x05C4, 0x05C5, 0x05C7, 0x0610,
  403. 0x0611, 0x0612, 0x0613, 0x0614, 0x0615, 0x0616, 0x0617, 0x0618, 0x0619, 0x061A, 0x064B, 0x064C, 0x064D,
  404. 0x064E, 0x064F, 0x0650, 0x0651, 0x0652, 0x0653, 0x0654, 0x0655, 0x0656, 0x0657, 0x0658, 0x0659, 0x065A,
  405. 0x065B, 0x065C, 0x065D, 0x065E, 0x065F, 0x0670, 0x06D6, 0x06D7, 0x06D8, 0x06D9, 0x06DA, 0x06DB, 0x06DC,
  406. 0x06DF, 0x06E0, 0x06E1, 0x06E2, 0x06E3, 0x06E4, 0x06E7, 0x06E8, 0x06EA, 0x06EB, 0x06EC, 0x06ED, 0x0711,
  407. 0x0730, 0x0731, 0x0732, 0x0733, 0x0734, 0x0735, 0x0736, 0x0737, 0x0738, 0x0739, 0x073A, 0x073B, 0x073C,
  408. 0x073D, 0x073E, 0x073F, 0x0740, 0x0741, 0x0742, 0x0743, 0x0744, 0x0745, 0x0746, 0x0747, 0x0748, 0x0749,
  409. 0x074A, 0x07A6, 0x07A7, 0x07A8, 0x07A9, 0x07AA, 0x07AB, 0x07AC, 0x07AD, 0x07AE, 0x07AF, 0x07B0, 0x07EB,
  410. 0x07EC, 0x07ED, 0x07EE, 0x07EF, 0x07F0, 0x07F1, 0x07F2, 0x07F3, 0x07FD, 0x0816, 0x0817, 0x0818, 0x0819,
  411. 0x081B, 0x081C, 0x081D, 0x081E, 0x081F, 0x0820, 0x0821, 0x0822, 0x0823, 0x0825, 0x0826, 0x0827, 0x0829,
  412. 0x082A, 0x082B, 0x082C, 0x082D, 0x0859, 0x085A, 0x085B, 0x0897, 0x0898, 0x0899, 0x089A, 0x089B, 0x089C,
  413. 0x089D, 0x089E, 0x089F, 0x08CA, 0x08CB, 0x08CC, 0x08CD, 0x08CE, 0x08CF, 0x08D0, 0x08D1, 0x08D2, 0x08D3,
  414. 0x08D4, 0x08D5, 0x08D6, 0x08D7, 0x08D8, 0x08D9, 0x08DA, 0x08DB, 0x08DC, 0x08DD, 0x08DE, 0x08DF, 0x08E0,
  415. 0x08E1, 0x08E3, 0x08E4, 0x08E5, 0x08E6, 0x08E7, 0x08E8, 0x08E9, 0x08EA, 0x08EB, 0x08EC, 0x08ED, 0x08EE,
  416. 0x08EF, 0x08F0, 0x08F1, 0x08F2, 0x08F3, 0x08F4, 0x08F5, 0x08F6, 0x08F7, 0x08F8, 0x08F9, 0x08FA, 0x08FB,
  417. 0x08FC, 0x08FD, 0x08FE, 0x08FF, 0x0900, 0x0901, 0x0902, 0x093A, 0x093C, 0x0941, 0x0942, 0x0943, 0x0944,
  418. 0x0945, 0x0946, 0x0947, 0x0948, 0x094D, 0x0951, 0x0952, 0x0953, 0x0954, 0x0955, 0x0956, 0x0957, 0x0962,
  419. 0x0963, 0x0981, 0x09BC, 0x09C1, 0x09C2, 0x09C3, 0x09C4, 0x09CD, 0x09E2, 0x09E3, 0x09FE, 0x0A01, 0x0A02,
  420. 0x0A3C, 0x0A41, 0x0A42, 0x0A47, 0x0A48, 0x0A4B, 0x0A4C, 0x0A4D, 0x0A51, 0x0A70, 0x0A71, 0x0A75, 0x0A81,
  421. 0x0A82, 0x0ABC, 0x0AC1, 0x0AC2, 0x0AC3, 0x0AC4, 0x0AC5, 0x0AC7, 0x0AC8, 0x0ACD, 0x0AE2, 0x0AE3, 0x0AFA,
  422. 0x0AFB, 0x0AFC, 0x0AFD, 0x0AFE, 0x0AFF, 0x0B01, 0x0B3C, 0x0B3F, 0x0B41, 0x0B42, 0x0B43, 0x0B44, 0x0B4D,
  423. 0x0B55, 0x0B56, 0x0B62, 0x0B63, 0x0B82, 0x0BC0, 0x0BCD, 0x0C00, 0x0C04, 0x0C3C, 0x0C3E, 0x0C3F, 0x0C40,
  424. 0x0C46, 0x0C47, 0x0C48, 0x0C4A, 0x0C4B, 0x0C4C, 0x0C4D, 0x0C55, 0x0C56, 0x0C62, 0x0C63, 0x0C81, 0x0CBC,
  425. 0x0CBF, 0x0CC6, 0x0CCC, 0x0CCD, 0x0CE2, 0x0CE3, 0x0D00, 0x0D01, 0x0D3B, 0x0D3C, 0x0D41, 0x0D42, 0x0D43,
  426. 0x0D44, 0x0D4D, 0x0D62, 0x0D63, 0x0D81, 0x0DCA, 0x0DD2, 0x0DD3, 0x0DD4, 0x0DD6, 0x0E31, 0x0E34, 0x0E35,
  427. 0x0E36, 0x0E37, 0x0E38, 0x0E39, 0x0E3A, 0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D, 0x0E4E,
  428. 0x0EB1, 0x0EB4, 0x0EB5, 0x0EB6, 0x0EB7, 0x0EB8, 0x0EB9, 0x0EBA, 0x0EBB, 0x0EBC, 0x0EC8, 0x0EC9, 0x0ECA,
  429. 0x0ECB, 0x0ECC, 0x0ECD, 0x0ECE, 0x0F18, 0x0F19, 0x0F35, 0x0F37, 0x0F39, 0x0F71, 0x0F72, 0x0F73, 0x0F74,
  430. 0x0F75, 0x0F76, 0x0F77, 0x0F78, 0x0F79, 0x0F7A, 0x0F7B, 0x0F7C, 0x0F7D, 0x0F7E, 0x0F80, 0x0F81, 0x0F82,
  431. 0x0F83, 0x0F84, 0x0F86, 0x0F87, 0x0F8D, 0x0F8E, 0x0F8F, 0x0F90, 0x0F91, 0x0F92, 0x0F93, 0x0F94, 0x0F95,
  432. 0x0F96, 0x0F97, 0x0F99, 0x0F9A, 0x0F9B, 0x0F9C, 0x0F9D, 0x0F9E, 0x0F9F, 0x0FA0, 0x0FA1, 0x0FA2, 0x0FA3,
  433. 0x0FA4, 0x0FA5, 0x0FA6, 0x0FA7, 0x0FA8, 0x0FA9, 0x0FAA, 0x0FAB, 0x0FAC, 0x0FAD, 0x0FAE, 0x0FAF, 0x0FB0,
  434. 0x0FB1, 0x0FB2, 0x0FB3, 0x0FB4, 0x0FB5, 0x0FB6, 0x0FB7, 0x0FB8, 0x0FB9, 0x0FBA, 0x0FBB, 0x0FBC, 0x0FC6,
  435. 0x102D, 0x102E, 0x102F, 0x1030, 0x1032, 0x1033, 0x1034, 0x1035, 0x1036, 0x1037, 0x1039, 0x103A, 0x103D,
  436. 0x103E, 0x1058, 0x1059, 0x105E, 0x105F, 0x1060, 0x1071, 0x1072, 0x1073, 0x1074, 0x1082, 0x1085, 0x1086,
  437. 0x108D, 0x109D, 0x135D, 0x135E, 0x135F, 0x1712, 0x1713, 0x1714, 0x1732, 0x1733, 0x1752, 0x1753, 0x1772,
  438. 0x1773, 0x17B4, 0x17B5, 0x17B7, 0x17B8, 0x17B9, 0x17BA, 0x17BB, 0x17BC, 0x17BD, 0x17C6, 0x17C9, 0x17CA,
  439. 0x17CB, 0x17CC, 0x17CD, 0x17CE, 0x17CF, 0x17D0, 0x17D1, 0x17D2, 0x17D3, 0x17DD, 0x180B, 0x180C, 0x180D,
  440. 0x180F, 0x1885, 0x1886, 0x18A9, 0x1920, 0x1921, 0x1922, 0x1927, 0x1928, 0x1932, 0x1939, 0x193A, 0x193B,
  441. 0x1A17, 0x1A18, 0x1A1B, 0x1A56, 0x1A58, 0x1A59, 0x1A5A, 0x1A5B, 0x1A5C, 0x1A5D, 0x1A5E, 0x1A60, 0x1A62,
  442. 0x1A65, 0x1A66, 0x1A67, 0x1A68, 0x1A69, 0x1A6A, 0x1A6B, 0x1A6C, 0x1A73, 0x1A74, 0x1A75, 0x1A76, 0x1A77,
  443. 0x1A78, 0x1A79, 0x1A7A, 0x1A7B, 0x1A7C, 0x1A7F, 0x1AB0, 0x1AB1, 0x1AB2, 0x1AB3, 0x1AB4, 0x1AB5, 0x1AB6,
  444. 0x1AB7, 0x1AB8, 0x1AB9, 0x1ABA, 0x1ABB, 0x1ABC, 0x1ABD, 0x1ABF, 0x1AC0, 0x1AC1, 0x1AC2, 0x1AC3, 0x1AC4,
  445. 0x1AC5, 0x1AC6, 0x1AC7, 0x1AC8, 0x1AC9, 0x1ACA, 0x1ACB, 0x1ACC, 0x1ACD, 0x1ACE, 0x1B00, 0x1B01, 0x1B02,
  446. 0x1B03, 0x1B34, 0x1B36, 0x1B37, 0x1B38, 0x1B39, 0x1B3A, 0x1B3C, 0x1B42, 0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E,
  447. 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73, 0x1B80, 0x1B81, 0x1BA2, 0x1BA3, 0x1BA4, 0x1BA5, 0x1BA8, 0x1BA9,
  448. 0x1BAB, 0x1BAC, 0x1BAD, 0x1BE6, 0x1BE8, 0x1BE9, 0x1BED, 0x1BEF, 0x1BF0, 0x1BF1, 0x1C2C, 0x1C2D, 0x1C2E,
  449. 0x1C2F, 0x1C30, 0x1C31, 0x1C32, 0x1C33, 0x1C36, 0x1C37, 0x1CD0, 0x1CD1, 0x1CD2, 0x1CD4, 0x1CD5, 0x1CD6,
  450. 0x1CD7, 0x1CD8, 0x1CD9, 0x1CDA, 0x1CDB, 0x1CDC, 0x1CDD, 0x1CDE, 0x1CDF, 0x1CE0, 0x1CE2, 0x1CE3, 0x1CE4,
  451. 0x1CE5, 0x1CE6, 0x1CE7, 0x1CE8, 0x1CED, 0x1CF4, 0x1CF8, 0x1CF9, 0x1DC0, 0x1DC1, 0x1DC2, 0x1DC3, 0x1DC4,
  452. 0x1DC5, 0x1DC6, 0x1DC7, 0x1DC8, 0x1DC9, 0x1DCA, 0x1DCB, 0x1DCC, 0x1DCD, 0x1DCE, 0x1DCF, 0x1DD0, 0x1DD1,
  453. 0x1DD2, 0x1DD3, 0x1DD4, 0x1DD5, 0x1DD6, 0x1DD7, 0x1DD8, 0x1DD9, 0x1DDA, 0x1DDB, 0x1DDC, 0x1DDD, 0x1DDE,
  454. 0x1DDF, 0x1DE0, 0x1DE1, 0x1DE2, 0x1DE3, 0x1DE4, 0x1DE5, 0x1DE6, 0x1DE7, 0x1DE8, 0x1DE9, 0x1DEA, 0x1DEB,
  455. 0x1DEC, 0x1DED, 0x1DEE, 0x1DEF, 0x1DF0, 0x1DF1, 0x1DF2, 0x1DF3, 0x1DF4, 0x1DF5, 0x1DF6, 0x1DF7, 0x1DF8,
  456. 0x1DF9, 0x1DFA, 0x1DFB, 0x1DFC, 0x1DFD, 0x1DFE, 0x1DFF, 0x20D0, 0x20D1, 0x20D2, 0x20D3, 0x20D4, 0x20D5,
  457. 0x20D6, 0x20D7, 0x20D8, 0x20D9, 0x20DA, 0x20DB, 0x20DC, 0x20E1, 0x20E5, 0x20E6, 0x20E7, 0x20E8, 0x20E9,
  458. 0x20EA, 0x20EB, 0x20EC, 0x20ED, 0x20EE, 0x20EF, 0x20F0, 0x2CEF, 0x2CF0, 0x2CF1, 0x2D7F, 0x2DE0, 0x2DE1,
  459. 0x2DE2, 0x2DE3, 0x2DE4, 0x2DE5, 0x2DE6, 0x2DE7, 0x2DE8, 0x2DE9, 0x2DEA, 0x2DEB, 0x2DEC, 0x2DED, 0x2DEE,
  460. 0x2DEF, 0x2DF0, 0x2DF1, 0x2DF2, 0x2DF3, 0x2DF4, 0x2DF5, 0x2DF6, 0x2DF7, 0x2DF8, 0x2DF9, 0x2DFA, 0x2DFB,
  461. 0x2DFC, 0x2DFD, 0x2DFE, 0x2DFF, 0x302A, 0x302B, 0x302C, 0x302D, 0x3099, 0x309A, 0xA66F, 0xA674, 0xA675,
  462. 0xA676, 0xA677, 0xA678, 0xA679, 0xA67A, 0xA67B, 0xA67C, 0xA67D, 0xA69E, 0xA69F, 0xA6F0, 0xA6F1, 0xA802,
  463. 0xA806, 0xA80B, 0xA825, 0xA826, 0xA82C, 0xA8C4, 0xA8C5, 0xA8E0, 0xA8E1, 0xA8E2, 0xA8E3, 0xA8E4, 0xA8E5,
  464. 0xA8E6, 0xA8E7, 0xA8E8, 0xA8E9, 0xA8EA, 0xA8EB, 0xA8EC, 0xA8ED, 0xA8EE, 0xA8EF, 0xA8F0, 0xA8F1, 0xA8FF,
  465. 0xA926, 0xA927, 0xA928, 0xA929, 0xA92A, 0xA92B, 0xA92C, 0xA92D, 0xA947, 0xA948, 0xA949, 0xA94A, 0xA94B,
  466. 0xA94C, 0xA94D, 0xA94E, 0xA94F, 0xA950, 0xA951, 0xA980, 0xA981, 0xA982, 0xA9B3, 0xA9B6, 0xA9B7, 0xA9B8,
  467. 0xA9B9, 0xA9BC, 0xA9BD, 0xA9E5, 0xAA29, 0xAA2A, 0xAA2B, 0xAA2C, 0xAA2D, 0xAA2E, 0xAA31, 0xAA32, 0xAA35,
  468. 0xAA36, 0xAA43, 0xAA4C, 0xAA7C, 0xAAB0, 0xAAB2, 0xAAB3, 0xAAB4, 0xAAB7, 0xAAB8, 0xAABE, 0xAABF, 0xAAC1,
  469. 0xAAEC, 0xAAED, 0xAAF6, 0xABE5, 0xABE8, 0xABED, 0xFB1E, 0xFE00, 0xFE01, 0xFE02, 0xFE03, 0xFE04, 0xFE05,
  470. 0xFE06, 0xFE07, 0xFE08, 0xFE09, 0xFE0A, 0xFE0B, 0xFE0C, 0xFE0D, 0xFE0E, 0xFE0F, 0xFE20, 0xFE21, 0xFE22,
  471. 0xFE23, 0xFE24, 0xFE25, 0xFE26, 0xFE27, 0xFE28, 0xFE29, 0xFE2A, 0xFE2B, 0xFE2C, 0xFE2D, 0xFE2E, 0xFE2F,
  472. 0x101FD, 0x102E0, 0x10376, 0x10377, 0x10378, 0x10379, 0x1037A, 0x10A01, 0x10A02, 0x10A03, 0x10A05, 0x10A06, 0x10A0C,
  473. 0x10A0D, 0x10A0E, 0x10A0F, 0x10A38, 0x10A39, 0x10A3A, 0x10A3F, 0x10AE5, 0x10AE6, 0x10D24, 0x10D25, 0x10D26, 0x10D27,
  474. 0x10D69, 0x10D6A, 0x10D6B, 0x10D6C, 0x10D6D, 0x10EAB, 0x10EAC, 0x10EFC, 0x10EFD, 0x10EFE, 0x10EFF, 0x10F46, 0x10F47,
  475. 0x10F48, 0x10F49, 0x10F4A, 0x10F4B, 0x10F4C, 0x10F4D, 0x10F4E, 0x10F4F, 0x10F50, 0x10F82, 0x10F83, 0x10F84, 0x10F85,
  476. 0x11001, 0x11038, 0x11039, 0x1103A, 0x1103B, 0x1103C, 0x1103D, 0x1103E, 0x1103F, 0x11040, 0x11041, 0x11042, 0x11043,
  477. 0x11044, 0x11045, 0x11046, 0x11070, 0x11073, 0x11074, 0x1107F, 0x11080, 0x11081, 0x110B3, 0x110B4, 0x110B5, 0x110B6,
  478. 0x110B9, 0x110BA, 0x110C2, 0x11100, 0x11101, 0x11102, 0x11127, 0x11128, 0x11129, 0x1112A, 0x1112B, 0x1112D, 0x1112E,
  479. 0x1112F, 0x11130, 0x11131, 0x11132, 0x11133, 0x11134, 0x11173, 0x11180, 0x11181, 0x111B6, 0x111B7, 0x111B8, 0x111B9,
  480. 0x111BA, 0x111BB, 0x111BC, 0x111BD, 0x111BE, 0x111C9, 0x111CA, 0x111CB, 0x111CC, 0x111CF, 0x1122F, 0x11230, 0x11231,
  481. 0x11234, 0x11236, 0x11237, 0x1123E, 0x11241, 0x112DF, 0x112E3, 0x112E4, 0x112E5, 0x112E6, 0x112E7, 0x112E8, 0x112E9,
  482. 0x112EA, 0x11300, 0x11301, 0x1133B, 0x1133C, 0x11340, 0x11366, 0x11367, 0x11368, 0x11369, 0x1136A, 0x1136B, 0x1136C,
  483. 0x11370, 0x11371, 0x11372, 0x11373, 0x11374, 0x113BB, 0x113BC, 0x113BD, 0x113BE, 0x113BF, 0x113C0, 0x113CE, 0x113D0,
  484. 0x113D2, 0x113E1, 0x113E2, 0x11438, 0x11439, 0x1143A, 0x1143B, 0x1143C, 0x1143D, 0x1143E, 0x1143F, 0x11442, 0x11443,
  485. 0x11444, 0x11446, 0x1145E, 0x114B3, 0x114B4, 0x114B5, 0x114B6, 0x114B7, 0x114B8, 0x114BA, 0x114BF, 0x114C0, 0x114C2,
  486. 0x114C3, 0x115B2, 0x115B3, 0x115B4, 0x115B5, 0x115BC, 0x115BD, 0x115BF, 0x115C0, 0x115DC, 0x115DD, 0x11633, 0x11634,
  487. 0x11635, 0x11636, 0x11637, 0x11638, 0x11639, 0x1163A, 0x1163D, 0x1163F, 0x11640, 0x116AB, 0x116AD, 0x116B0, 0x116B1,
  488. 0x116B2, 0x116B3, 0x116B4, 0x116B5, 0x116B7, 0x1171D, 0x1171F, 0x11722, 0x11723, 0x11724, 0x11725, 0x11727, 0x11728,
  489. 0x11729, 0x1172A, 0x1172B, 0x1182F, 0x11830, 0x11831, 0x11832, 0x11833, 0x11834, 0x11835, 0x11836, 0x11837, 0x11839,
  490. 0x1183A, 0x1193B, 0x1193C, 0x1193E, 0x11943, 0x119D4, 0x119D5, 0x119D6, 0x119D7, 0x119DA, 0x119DB, 0x119E0, 0x11A01,
  491. 0x11A02, 0x11A03, 0x11A04, 0x11A05, 0x11A06, 0x11A07, 0x11A08, 0x11A09, 0x11A0A, 0x11A33, 0x11A34, 0x11A35, 0x11A36,
  492. 0x11A37, 0x11A38, 0x11A3B, 0x11A3C, 0x11A3D, 0x11A3E, 0x11A47, 0x11A51, 0x11A52, 0x11A53, 0x11A54, 0x11A55, 0x11A56,
  493. 0x11A59, 0x11A5A, 0x11A5B, 0x11A8A, 0x11A8B, 0x11A8C, 0x11A8D, 0x11A8E, 0x11A8F, 0x11A90, 0x11A91, 0x11A92, 0x11A93,
  494. 0x11A94, 0x11A95, 0x11A96, 0x11A98, 0x11A99, 0x11C30, 0x11C31, 0x11C32, 0x11C33, 0x11C34, 0x11C35, 0x11C36, 0x11C38,
  495. 0x11C39, 0x11C3A, 0x11C3B, 0x11C3C, 0x11C3D, 0x11C3F, 0x11C92, 0x11C93, 0x11C94, 0x11C95, 0x11C96, 0x11C97, 0x11C98,
  496. 0x11C99, 0x11C9A, 0x11C9B, 0x11C9C, 0x11C9D, 0x11C9E, 0x11C9F, 0x11CA0, 0x11CA1, 0x11CA2, 0x11CA3, 0x11CA4, 0x11CA5,
  497. 0x11CA6, 0x11CA7, 0x11CAA, 0x11CAB, 0x11CAC, 0x11CAD, 0x11CAE, 0x11CAF, 0x11CB0, 0x11CB2, 0x11CB3, 0x11CB5, 0x11CB6,
  498. 0x11D31, 0x11D32, 0x11D33, 0x11D34, 0x11D35, 0x11D36, 0x11D3A, 0x11D3C, 0x11D3D, 0x11D3F, 0x11D40, 0x11D41, 0x11D42,
  499. 0x11D43, 0x11D44, 0x11D45, 0x11D47, 0x11D90, 0x11D91, 0x11D95, 0x11D97, 0x11EF3, 0x11EF4, 0x11F00, 0x11F01, 0x11F36,
  500. 0x11F37, 0x11F38, 0x11F39, 0x11F3A, 0x11F40, 0x11F42, 0x11F5A, 0x13440, 0x13447, 0x13448, 0x13449, 0x1344A, 0x1344B,
  501. 0x1344C, 0x1344D, 0x1344E, 0x1344F, 0x13450, 0x13451, 0x13452, 0x13453, 0x13454, 0x13455, 0x1611E, 0x1611F, 0x16120,
  502. 0x16121, 0x16122, 0x16123, 0x16124, 0x16125, 0x16126, 0x16127, 0x16128, 0x16129, 0x1612D, 0x1612E, 0x1612F, 0x16AF0,
  503. 0x16AF1, 0x16AF2, 0x16AF3, 0x16AF4, 0x16B30, 0x16B31, 0x16B32, 0x16B33, 0x16B34, 0x16B35, 0x16B36, 0x16F4F, 0x16F8F,
  504. 0x16F90, 0x16F91, 0x16F92, 0x16FE4, 0x1BC9D, 0x1BC9E, 0x1CF00, 0x1CF01, 0x1CF02, 0x1CF03, 0x1CF04, 0x1CF05, 0x1CF06,
  505. 0x1CF07, 0x1CF08, 0x1CF09, 0x1CF0A, 0x1CF0B, 0x1CF0C, 0x1CF0D, 0x1CF0E, 0x1CF0F, 0x1CF10, 0x1CF11, 0x1CF12, 0x1CF13,
  506. 0x1CF14, 0x1CF15, 0x1CF16, 0x1CF17, 0x1CF18, 0x1CF19, 0x1CF1A, 0x1CF1B, 0x1CF1C, 0x1CF1D, 0x1CF1E, 0x1CF1F, 0x1CF20,
  507. 0x1CF21, 0x1CF22, 0x1CF23, 0x1CF24, 0x1CF25, 0x1CF26, 0x1CF27, 0x1CF28, 0x1CF29, 0x1CF2A, 0x1CF2B, 0x1CF2C, 0x1CF2D,
  508. 0x1CF30, 0x1CF31, 0x1CF32, 0x1CF33, 0x1CF34, 0x1CF35, 0x1CF36, 0x1CF37, 0x1CF38, 0x1CF39, 0x1CF3A, 0x1CF3B, 0x1CF3C,
  509. 0x1CF3D, 0x1CF3E, 0x1CF3F, 0x1CF40, 0x1CF41, 0x1CF42, 0x1CF43, 0x1CF44, 0x1CF45, 0x1CF46, 0x1D167, 0x1D168, 0x1D169,
  510. 0x1D17B, 0x1D17C, 0x1D17D, 0x1D17E, 0x1D17F, 0x1D180, 0x1D181, 0x1D182, 0x1D185, 0x1D186, 0x1D187, 0x1D188, 0x1D189,
  511. 0x1D18A, 0x1D18B, 0x1D1AA, 0x1D1AB, 0x1D1AC, 0x1D1AD, 0x1D242, 0x1D243, 0x1D244, 0x1DA00, 0x1DA01, 0x1DA02, 0x1DA03,
  512. 0x1DA04, 0x1DA05, 0x1DA06, 0x1DA07, 0x1DA08, 0x1DA09, 0x1DA0A, 0x1DA0B, 0x1DA0C, 0x1DA0D, 0x1DA0E, 0x1DA0F, 0x1DA10,
  513. 0x1DA11, 0x1DA12, 0x1DA13, 0x1DA14, 0x1DA15, 0x1DA16, 0x1DA17, 0x1DA18, 0x1DA19, 0x1DA1A, 0x1DA1B, 0x1DA1C, 0x1DA1D,
  514. 0x1DA1E, 0x1DA1F, 0x1DA20, 0x1DA21, 0x1DA22, 0x1DA23, 0x1DA24, 0x1DA25, 0x1DA26, 0x1DA27, 0x1DA28, 0x1DA29, 0x1DA2A,
  515. 0x1DA2B, 0x1DA2C, 0x1DA2D, 0x1DA2E, 0x1DA2F, 0x1DA30, 0x1DA31, 0x1DA32, 0x1DA33, 0x1DA34, 0x1DA35, 0x1DA36, 0x1DA3B,
  516. 0x1DA3C, 0x1DA3D, 0x1DA3E, 0x1DA3F, 0x1DA40, 0x1DA41, 0x1DA42, 0x1DA43, 0x1DA44, 0x1DA45, 0x1DA46, 0x1DA47, 0x1DA48,
  517. 0x1DA49, 0x1DA4A, 0x1DA4B, 0x1DA4C, 0x1DA4D, 0x1DA4E, 0x1DA4F, 0x1DA50, 0x1DA51, 0x1DA52, 0x1DA53, 0x1DA54, 0x1DA55,
  518. 0x1DA56, 0x1DA57, 0x1DA58, 0x1DA59, 0x1DA5A, 0x1DA5B, 0x1DA5C, 0x1DA5D, 0x1DA5E, 0x1DA5F, 0x1DA60, 0x1DA61, 0x1DA62,
  519. 0x1DA63, 0x1DA64, 0x1DA65, 0x1DA66, 0x1DA67, 0x1DA68, 0x1DA69, 0x1DA6A, 0x1DA6B, 0x1DA6C, 0x1DA75, 0x1DA84, 0x1DA9B,
  520. 0x1DA9C, 0x1DA9D, 0x1DA9E, 0x1DA9F, 0x1DAA1, 0x1DAA2, 0x1DAA3, 0x1DAA4, 0x1DAA5, 0x1DAA6, 0x1DAA7, 0x1DAA8, 0x1DAA9,
  521. 0x1DAAA, 0x1DAAB, 0x1DAAC, 0x1DAAD, 0x1DAAE, 0x1DAAF, 0x1E000, 0x1E001, 0x1E002, 0x1E003, 0x1E004, 0x1E005, 0x1E006,
  522. 0x1E008, 0x1E009, 0x1E00A, 0x1E00B, 0x1E00C, 0x1E00D, 0x1E00E, 0x1E00F, 0x1E010, 0x1E011, 0x1E012, 0x1E013, 0x1E014,
  523. 0x1E015, 0x1E016, 0x1E017, 0x1E018, 0x1E01B, 0x1E01C, 0x1E01D, 0x1E01E, 0x1E01F, 0x1E020, 0x1E021, 0x1E023, 0x1E024,
  524. 0x1E026, 0x1E027, 0x1E028, 0x1E029, 0x1E02A, 0x1E08F, 0x1E130, 0x1E131, 0x1E132, 0x1E133, 0x1E134, 0x1E135, 0x1E136,
  525. 0x1E2AE, 0x1E2EC, 0x1E2ED, 0x1E2EE, 0x1E2EF, 0x1E4EC, 0x1E4ED, 0x1E4EE, 0x1E4EF, 0x1E5EE, 0x1E5EF, 0x1E8D0, 0x1E8D1,
  526. 0x1E8D2, 0x1E8D3, 0x1E8D4, 0x1E8D5, 0x1E8D6, 0x1E944, 0x1E945, 0x1E946, 0x1E947, 0x1E948, 0x1E949, 0x1E94A, 0xE0100,
  527. 0xE0101, 0xE0102, 0xE0103, 0xE0104, 0xE0105, 0xE0106, 0xE0107, 0xE0108, 0xE0109, 0xE010A, 0xE010B, 0xE010C, 0xE010D,
  528. 0xE010E, 0xE010F, 0xE0110, 0xE0111, 0xE0112, 0xE0113, 0xE0114, 0xE0115, 0xE0116, 0xE0117, 0xE0118, 0xE0119, 0xE011A,
  529. 0xE011B, 0xE011C, 0xE011D, 0xE011E, 0xE011F, 0xE0120, 0xE0121, 0xE0122, 0xE0123, 0xE0124, 0xE0125, 0xE0126, 0xE0127,
  530. 0xE0128, 0xE0129, 0xE012A, 0xE012B, 0xE012C, 0xE012D, 0xE012E, 0xE012F, 0xE0130, 0xE0131, 0xE0132, 0xE0133, 0xE0134,
  531. 0xE0135, 0xE0136, 0xE0137, 0xE0138, 0xE0139, 0xE013A, 0xE013B, 0xE013C, 0xE013D, 0xE013E, 0xE013F, 0xE0140, 0xE0141,
  532. 0xE0142, 0xE0143, 0xE0144, 0xE0145, 0xE0146, 0xE0147, 0xE0148, 0xE0149, 0xE014A, 0xE014B, 0xE014C, 0xE014D, 0xE014E,
  533. 0xE014F, 0xE0150, 0xE0151, 0xE0152, 0xE0153, 0xE0154, 0xE0155, 0xE0156, 0xE0157, 0xE0158, 0xE0159, 0xE015A, 0xE015B,
  534. 0xE015C, 0xE015D, 0xE015E, 0xE015F, 0xE0160, 0xE0161, 0xE0162, 0xE0163, 0xE0164, 0xE0165, 0xE0166, 0xE0167, 0xE0168,
  535. 0xE0169, 0xE016A, 0xE016B, 0xE016C, 0xE016D, 0xE016E, 0xE016F, 0xE0170, 0xE0171, 0xE0172, 0xE0173, 0xE0174, 0xE0175,
  536. 0xE0176, 0xE0177, 0xE0178, 0xE0179, 0xE017A, 0xE017B, 0xE017C, 0xE017D, 0xE017E, 0xE017F, 0xE0180, 0xE0181, 0xE0182,
  537. 0xE0183, 0xE0184, 0xE0185, 0xE0186, 0xE0187, 0xE0188, 0xE0189, 0xE018A, 0xE018B, 0xE018C, 0xE018D, 0xE018E, 0xE018F,
  538. 0xE0190, 0xE0191, 0xE0192, 0xE0193, 0xE0194, 0xE0195, 0xE0196, 0xE0197, 0xE0198, 0xE0199, 0xE019A, 0xE019B, 0xE019C,
  539. 0xE019D, 0xE019E, 0xE019F, 0xE01A0, 0xE01A1, 0xE01A2, 0xE01A3, 0xE01A4, 0xE01A5, 0xE01A6, 0xE01A7, 0xE01A8, 0xE01A9,
  540. 0xE01AA, 0xE01AB, 0xE01AC, 0xE01AD, 0xE01AE, 0xE01AF, 0xE01B0, 0xE01B1, 0xE01B2, 0xE01B3, 0xE01B4, 0xE01B5, 0xE01B6,
  541. 0xE01B7, 0xE01B8, 0xE01B9, 0xE01BA, 0xE01BB, 0xE01BC, 0xE01BD, 0xE01BE, 0xE01BF, 0xE01C0, 0xE01C1, 0xE01C2, 0xE01C3,
  542. 0xE01C4, 0xE01C5, 0xE01C6, 0xE01C7, 0xE01C8, 0xE01C9, 0xE01CA, 0xE01CB, 0xE01CC, 0xE01CD, 0xE01CE, 0xE01CF, 0xE01D0,
  543. 0xE01D1, 0xE01D2, 0xE01D3, 0xE01D4, 0xE01D5, 0xE01D6, 0xE01D7, 0xE01D8, 0xE01D9, 0xE01DA, 0xE01DB, 0xE01DC, 0xE01DD,
  544. 0xE01DE, 0xE01DF, 0xE01E0, 0xE01E1, 0xE01E2, 0xE01E3, 0xE01E4, 0xE01E5, 0xE01E6, 0xE01E7, 0xE01E8, 0xE01E9, 0xE01EA,
  545. 0xE01EB, 0xE01EC, 0xE01ED, 0xE01EE, 0xE01EF
  546. /* END: COMBINING CHAR TABLE */
  547. };
  548. static const unsigned long combiningCharTableSize = sizeof(combiningCharTable) / sizeof(combiningCharTable[0]);
  549. static bool isCombiningChar(unsigned long cp) {
  550. for (size_t i = 0; i < combiningCharTableSize; i++) {
  551. auto code = combiningCharTable[i];
  552. if (code > cp) {
  553. return false;
  554. }
  555. if (code == cp) {
  556. return true;
  557. }
  558. }
  559. return false;
  560. }
  561. /* Get length of previous grapheme */
  562. static size_t defaultPrevCharLen(const char * buf, size_t /*buf_len*/, size_t pos, size_t * col_len) {
  563. size_t end = pos;
  564. while (pos > 0) {
  565. size_t len = prevUtf8CodePointLen(buf, pos);
  566. pos -= len;
  567. int cp;
  568. utf8BytesToCodePoint(buf + pos, len, &cp);
  569. if (!isCombiningChar(cp)) {
  570. if (col_len != NULL) {
  571. *col_len = isWideChar(cp) ? 2 : 1;
  572. }
  573. return end - pos;
  574. }
  575. }
  576. /* NOTREACHED */
  577. return 0;
  578. }
  579. /* Get length of next grapheme */
  580. static size_t defaultNextCharLen(const char * buf, size_t buf_len, size_t pos, size_t * col_len) {
  581. size_t beg = pos;
  582. int cp;
  583. size_t len = utf8BytesToCodePoint(buf + pos, buf_len - pos, &cp);
  584. if (isCombiningChar(cp)) {
  585. /* NOTREACHED */
  586. return 0;
  587. }
  588. if (col_len != NULL) {
  589. *col_len = isWideChar(cp) ? 2 : 1;
  590. }
  591. pos += len;
  592. while (pos < buf_len) {
  593. int cp;
  594. len = utf8BytesToCodePoint(buf + pos, buf_len - pos, &cp);
  595. if (!isCombiningChar(cp)) {
  596. return pos - beg;
  597. }
  598. pos += len;
  599. }
  600. return pos - beg;
  601. }
  602. /* Read a Unicode from file. */
  603. static size_t defaultReadCode(int fd, char * buf, size_t buf_len, int * cp) {
  604. if (buf_len < 1) {
  605. return -1;
  606. }
  607. size_t nread = read(fd, &buf[0], 1);
  608. if (nread <= 0) {
  609. return nread;
  610. }
  611. unsigned char byte = buf[0];
  612. if ((byte & 0x80) == 0) {
  613. ;
  614. } else if ((byte & 0xE0) == 0xC0) {
  615. if (buf_len < 2) {
  616. return -1;
  617. }
  618. nread = read(fd, &buf[1], 1);
  619. if (nread <= 0) {
  620. return nread;
  621. }
  622. } else if ((byte & 0xF0) == 0xE0) {
  623. if (buf_len < 3) {
  624. return -1;
  625. }
  626. nread = read(fd, &buf[1], 2);
  627. if (nread <= 0) {
  628. return nread;
  629. }
  630. } else if ((byte & 0xF8) == 0xF0) {
  631. if (buf_len < 3) {
  632. return -1;
  633. }
  634. nread = read(fd, &buf[1], 3);
  635. if (nread <= 0) {
  636. return nread;
  637. }
  638. } else {
  639. return -1;
  640. }
  641. return utf8BytesToCodePoint(buf, buf_len, cp);
  642. }
  643. /* Set default encoding functions */
  644. static linenoisePrevCharLen * prevCharLen = defaultPrevCharLen;
  645. static linenoiseNextCharLen * nextCharLen = defaultNextCharLen;
  646. static linenoiseReadCode * readCode = defaultReadCode;
  647. /* Set used defined encoding functions */
  648. void linenoiseSetEncodingFunctions(linenoisePrevCharLen * prevCharLenFunc, linenoiseNextCharLen * nextCharLenFunc,
  649. linenoiseReadCode * readCodeFunc) {
  650. prevCharLen = prevCharLenFunc;
  651. nextCharLen = nextCharLenFunc;
  652. readCode = readCodeFunc;
  653. }
  654. /* ======================= Low level terminal handling ====================== */
  655. /* Enable "mask mode". When it is enabled, instead of the input that
  656. * the user is typing, the terminal will just display a corresponding
  657. * number of asterisks, like "****". This is useful for passwords and other
  658. * secrets that should not be displayed. */
  659. void linenoiseMaskModeEnable(void) {
  660. maskmode = 1;
  661. }
  662. /* Disable mask mode. */
  663. void linenoiseMaskModeDisable(void) {
  664. maskmode = 0;
  665. }
  666. /* Set if to use or not the multi line mode. */
  667. void linenoiseSetMultiLine(int ml) {
  668. mlmode = ml;
  669. }
  670. /* Return true if the terminal name is in the list of terminals we know are
  671. * not able to understand basic escape sequences. */
  672. static int isUnsupportedTerm(void) {
  673. char *term = getenv("TERM");
  674. if (term == NULL) return 0;
  675. for (size_t j = 0; j < unsupported_term.size(); ++j) {
  676. if (!strcasecmp(term, unsupported_term[j])) {
  677. return 1;
  678. }
  679. }
  680. return 0;
  681. }
  682. /* Raw mode: 1960 magic shit. */
  683. static int enableRawMode(int fd) {
  684. struct termios raw;
  685. if (!isatty(STDIN_FILENO)) goto fatal;
  686. if (!atexit_registered) {
  687. atexit(linenoiseAtExit);
  688. atexit_registered = 1;
  689. }
  690. if (tcgetattr(fd,&orig_termios) == -1) goto fatal;
  691. raw = orig_termios; /* modify the original mode */
  692. /* input modes: no break, no CR to NL, no parity check, no strip char,
  693. * no start/stop output control. */
  694. raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  695. /* output modes - disable post processing */
  696. raw.c_oflag &= ~(OPOST);
  697. /* control modes - set 8 bit chars */
  698. raw.c_cflag |= (CS8);
  699. /* local modes - choing off, canonical off, no extended functions,
  700. * no signal chars (^Z,^C) */
  701. raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  702. /* control chars - set return condition: min number of bytes and timer.
  703. * We want read to return every single byte, without timeout. */
  704. raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
  705. /* put terminal in raw mode after flushing */
  706. if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal;
  707. rawmode = 1;
  708. return 0;
  709. fatal:
  710. errno = ENOTTY;
  711. return -1;
  712. }
  713. static void disableRawMode(int fd) {
  714. /* Don't even check the return value as it's too late. */
  715. if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1)
  716. rawmode = 0;
  717. }
  718. /* Use the ESC [6n escape sequence to query the horizontal cursor position
  719. * and return it. On error -1 is returned, on success the position of the
  720. * cursor. */
  721. static int getCursorPosition(int ifd, int ofd) {
  722. char buf[32];
  723. int cols, rows;
  724. unsigned int i = 0;
  725. /* Report cursor location */
  726. if (write(ofd, "\x1b[6n", 4) != 4) return -1;
  727. /* Read the response: ESC [ rows ; cols R */
  728. while (i < sizeof(buf)-1) {
  729. if (read(ifd,buf+i,1) != 1) break;
  730. if (buf[i] == 'R') break;
  731. i++;
  732. }
  733. buf[i] = '\0';
  734. /* Parse it. */
  735. if (buf[0] != ESC || buf[1] != '[') return -1;
  736. if (sscanf(buf+2,"%d;%d",&rows,&cols) != 2) return -1;
  737. return cols;
  738. }
  739. /* Try to get the number of columns in the current terminal, or assume 80
  740. * if it fails. */
  741. static int getColumns(int ifd, int ofd) {
  742. struct winsize ws;
  743. if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
  744. /* ioctl() failed. Try to query the terminal itself. */
  745. int start, cols;
  746. /* Get the initial position so we can restore it later. */
  747. start = getCursorPosition(ifd,ofd);
  748. if (start == -1) goto failed;
  749. /* Go to right margin and get position. */
  750. if (write(ofd,"\x1b[999C",6) != 6) goto failed;
  751. cols = getCursorPosition(ifd,ofd);
  752. if (cols == -1) goto failed;
  753. /* Restore position. */
  754. if (cols > start) {
  755. char seq[32];
  756. snprintf(seq,32,"\x1b[%dD",cols-start);
  757. if (write(ofd,seq,strlen(seq)) == -1) {
  758. /* Can't recover... */
  759. }
  760. }
  761. return cols;
  762. } else {
  763. return ws.ws_col;
  764. }
  765. failed:
  766. return 80;
  767. }
  768. /* Clear the screen. Used to handle ctrl+l */
  769. void linenoiseClearScreen(void) {
  770. if (write(STDOUT_FILENO,"\x1b[H\x1b[2J",7) <= 0) {
  771. /* nothing to do, just to avoid warning. */
  772. }
  773. }
  774. /* Beep, used for completion when there is nothing to complete or when all
  775. * the choices were already shown. */
  776. static void linenoiseBeep(void) {
  777. fprintf(stderr, "\x7");
  778. fflush(stderr);
  779. }
  780. /* Called by completeLine() and linenoiseShow() to render the current
  781. * edited line with the proposed completion. If the current completion table
  782. * is already available, it is passed as second argument, otherwise the
  783. * function will use the callback to obtain it.
  784. *
  785. * Flags are the same as refreshLine*(), that is REFRESH_* macros. */
  786. static void refreshLineWithCompletion(struct linenoiseState *ls, linenoiseCompletions *lc, int flags) {
  787. /* Obtain the table of completions if the caller didn't provide one. */
  788. linenoiseCompletions ctable;
  789. if (lc == NULL) {
  790. completionCallback(ls->buf, &ctable);
  791. lc = &ctable;
  792. }
  793. /* Show the edited line with completion if possible, or just refresh. */
  794. if (ls->completion_idx < lc->len) {
  795. struct linenoiseState saved = *ls;
  796. ls->len = ls->pos = strlen(lc->cvec[ls->completion_idx]);
  797. ls->buf = lc->cvec[ls->completion_idx];
  798. refreshLineWithFlags(ls, flags);
  799. ls->len = saved.len;
  800. ls->pos = saved.pos;
  801. ls->buf = saved.buf;
  802. } else {
  803. refreshLineWithFlags(ls, flags);
  804. }
  805. if (lc == &ctable) {
  806. ctable.to_free = false;
  807. }
  808. }
  809. enum ESC_TYPE { ESC_NULL = 0, ESC_DELETE, ESC_UP, ESC_DOWN, ESC_RIGHT, ESC_LEFT, ESC_HOME, ESC_END };
  810. static ESC_TYPE readEscapeSequence(struct linenoiseState * l) {
  811. /* Check if the file input has additional data. */
  812. struct pollfd pfd;
  813. pfd.fd = l->ifd;
  814. pfd.events = POLLIN;
  815. auto ret = poll(&pfd, 1, 1); // 1 millisecond timeout
  816. if (ret <= 0) { // -1: error, 0: timeout
  817. return ESC_NULL;
  818. }
  819. /* Read the next two bytes representing the escape sequence.
  820. * Use two calls to handle slow terminals returning the two
  821. * chars at different times. */
  822. char seq[3];
  823. if (read(l->ifd, seq, 1) == -1) {
  824. return ESC_NULL;
  825. }
  826. if (read(l->ifd, seq + 1, 1) == -1) {
  827. return ESC_NULL;
  828. }
  829. /* ESC [ sequences. */
  830. if (seq[0] == '[') {
  831. if (seq[1] >= '0' && seq[1] <= '9') {
  832. /* Extended escape, read additional byte. */
  833. if (read(l->ifd, seq + 2, 1) == -1) {
  834. return ESC_NULL;
  835. }
  836. if (seq[2] == '~') {
  837. switch (seq[1]) {
  838. case '3':
  839. return ESC_DELETE;
  840. }
  841. }
  842. } else {
  843. switch (seq[1]) {
  844. case 'A':
  845. return ESC_UP;
  846. case 'B':
  847. return ESC_DOWN;
  848. case 'C':
  849. return ESC_RIGHT;
  850. case 'D':
  851. return ESC_LEFT;
  852. case 'H':
  853. return ESC_HOME;
  854. case 'F':
  855. return ESC_END;
  856. }
  857. }
  858. }
  859. /* ESC O sequences. */
  860. else if (seq[0] == 'O') {
  861. switch (seq[1]) {
  862. case 'H':
  863. return ESC_HOME;
  864. case 'F':
  865. return ESC_END;
  866. }
  867. }
  868. return ESC_NULL;
  869. }
  870. /* This is an helper function for linenoiseEdit*() and is called when the
  871. * user types the <tab> key in order to complete the string currently in the
  872. * input.
  873. *
  874. * The state of the editing is encapsulated into the pointed linenoiseState
  875. * structure as described in the structure definition.
  876. *
  877. * If the function returns non-zero, the caller should handle the
  878. * returned value as a byte read from the standard input, and process
  879. * it as usually: this basically means that the function may return a byte
  880. * read from the terminal but not processed. Otherwise, if zero is returned,
  881. * the input was consumed by the completeLine() function to navigate the
  882. * possible completions, and the caller should read for the next characters
  883. * from stdin. */
  884. static int completeLine(struct linenoiseState * ls, int keypressed, ESC_TYPE esc_type) {
  885. linenoiseCompletions lc;
  886. int nwritten;
  887. char c = keypressed;
  888. completionCallback(ls->buf, &lc);
  889. if (lc.len == 0) {
  890. linenoiseBeep();
  891. ls->in_completion = 0;
  892. } else {
  893. if (c == TAB) {
  894. if (ls->in_completion == 0) {
  895. ls->in_completion = 1;
  896. ls->completion_idx = 0;
  897. } else {
  898. ls->completion_idx = (ls->completion_idx + 1) % (lc.len + 1);
  899. if (ls->completion_idx == lc.len) {
  900. linenoiseBeep();
  901. }
  902. }
  903. c = 0;
  904. } else if (c == ESC && esc_type == ESC_NULL) {
  905. /* Re-show original buffer */
  906. if (ls->completion_idx < lc.len) {
  907. refreshLine(ls);
  908. }
  909. ls->in_completion = 0;
  910. c = 0;
  911. } else {
  912. /* Update buffer and return */
  913. if (ls->completion_idx < lc.len) {
  914. nwritten = snprintf(ls->buf, ls->buflen, "%s", lc.cvec[ls->completion_idx]);
  915. ls->len = ls->pos = nwritten;
  916. }
  917. ls->in_completion = 0;
  918. }
  919. /* Show completion or original buffer */
  920. if (ls->in_completion && ls->completion_idx < lc.len) {
  921. refreshLineWithCompletion(ls, &lc, REFRESH_ALL);
  922. } else {
  923. refreshLine(ls);
  924. }
  925. }
  926. return c; /* Return last read character */
  927. }
  928. /* Register a callback function to be called for tab-completion. */
  929. void linenoiseSetCompletionCallback(linenoiseCompletionCallback *fn) {
  930. completionCallback = fn;
  931. }
  932. /* Register a hits function to be called to show hits to the user at the
  933. * right of the prompt. */
  934. void linenoiseSetHintsCallback(linenoiseHintsCallback *fn) {
  935. hintsCallback = fn;
  936. }
  937. /* Register a function to free the hints returned by the hints callback
  938. * registered with linenoiseSetHintsCallback(). */
  939. void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *fn) {
  940. freeHintsCallback = fn;
  941. }
  942. /* This function is used by the callback function registered by the user
  943. * in order to add completion options given the input string when the
  944. * user typed <tab>. See the example.c source code for a very easy to
  945. * understand example. */
  946. void linenoiseAddCompletion(linenoiseCompletions *lc, const char *str) {
  947. const size_t len = strlen(str);
  948. auto copy = std::make_unique<char[]>(len + 1);
  949. if (!copy) {
  950. return;
  951. }
  952. memcpy(copy.get(), str, len + 1);
  953. char ** cvec = static_cast<char **>(std::realloc(lc->cvec, sizeof(char *) * (lc->len + 1)));
  954. if (cvec == nullptr) {
  955. return;
  956. }
  957. lc->cvec = cvec;
  958. lc->cvec[lc->len++] = copy.release();
  959. }
  960. /* Get column length from begining of buffer to current byte position */
  961. static size_t columnPos(const char * buf, size_t buf_len, size_t pos) {
  962. size_t ret = 0;
  963. size_t off = 0;
  964. while (off < pos) {
  965. size_t col_len;
  966. size_t len = nextCharLen(buf, buf_len, off, &col_len);
  967. off += len;
  968. ret += col_len;
  969. }
  970. return ret;
  971. }
  972. /* Helper of refreshSingleLine() and refreshMultiLine() to show hints
  973. * to the right of the prompt. */
  974. static void refreshShowHints(std::string & ab, struct linenoiseState * l, int pcollen) {
  975. char seq[64];
  976. size_t collen = pcollen + columnPos(l->buf, l->len, l->len);
  977. if (hintsCallback && collen < l->cols) {
  978. int color = -1, bold = 0;
  979. const char *hint = hintsCallback(l->buf,&color,&bold);
  980. if (hint) {
  981. int hintlen = strlen(hint);
  982. int hintmaxlen = l->cols - collen;
  983. if (hintlen > hintmaxlen) hintlen = hintmaxlen;
  984. if (bold == 1 && color == -1) color = 37;
  985. if (color != -1 || bold != 0)
  986. snprintf(seq,64,"\033[%d;%d;49m",bold,color);
  987. else
  988. seq[0] = '\0';
  989. ab.append(seq);
  990. ab.append(hint, hintlen);
  991. if (color != -1 || bold != 0)
  992. ab.append("\033[0m");
  993. /* Call the function to free the hint returned. */
  994. if (freeHintsCallback) freeHintsCallback(hint);
  995. }
  996. }
  997. }
  998. /* Check if text is an ANSI escape sequence */
  999. static int isAnsiEscape(const char * buf, size_t buf_len, size_t * len) {
  1000. if (buf_len > 2 && !memcmp("\033[", buf, 2)) {
  1001. size_t off = 2;
  1002. while (off < buf_len) {
  1003. switch (buf[off++]) {
  1004. case 'A':
  1005. case 'B':
  1006. case 'C':
  1007. case 'D':
  1008. case 'E':
  1009. case 'F':
  1010. case 'G':
  1011. case 'H':
  1012. case 'J':
  1013. case 'K':
  1014. case 'S':
  1015. case 'T':
  1016. case 'f':
  1017. case 'm':
  1018. *len = off;
  1019. return 1;
  1020. }
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. /* Get column length of prompt text */
  1026. static size_t promptTextColumnLen(const char * prompt, size_t plen) {
  1027. char buf[LINENOISE_MAX_LINE];
  1028. size_t buf_len = 0;
  1029. size_t off = 0;
  1030. while (off < plen) {
  1031. size_t len;
  1032. if (isAnsiEscape(prompt + off, plen - off, &len)) {
  1033. off += len;
  1034. continue;
  1035. }
  1036. buf[buf_len++] = prompt[off++];
  1037. }
  1038. return columnPos(buf, buf_len, buf_len);
  1039. }
  1040. /* Single line low level line refresh.
  1041. *
  1042. * Rewrite the currently edited line accordingly to the buffer content,
  1043. * cursor position, and number of columns of the terminal.
  1044. *
  1045. * Flags is REFRESH_* macros. The function can just remove the old
  1046. * prompt, just write it, or both. */
  1047. static void refreshSingleLine(struct linenoiseState *l, int flags) {
  1048. char seq[64];
  1049. size_t pcollen = promptTextColumnLen(l->prompt, strlen(l->prompt));
  1050. int fd = l->ofd;
  1051. char *buf = l->buf;
  1052. size_t len = l->len;
  1053. size_t pos = l->pos;
  1054. std::string ab;
  1055. while ((pcollen + columnPos(buf, len, pos)) >= l->cols) {
  1056. int chlen = nextCharLen(buf, len, 0, NULL);
  1057. buf += chlen;
  1058. len -= chlen;
  1059. pos -= chlen;
  1060. }
  1061. while (pcollen + columnPos(buf, len, len) > l->cols) {
  1062. len -= prevCharLen(buf, len, len, NULL);
  1063. }
  1064. /* Cursor to left edge */
  1065. snprintf(seq,sizeof(seq),"\r");
  1066. ab.append(seq);
  1067. if (flags & REFRESH_WRITE) {
  1068. /* Write the prompt and the current buffer content */
  1069. ab.append(l->prompt);
  1070. if (maskmode == 1) {
  1071. while (len--) {
  1072. ab.append("*");
  1073. }
  1074. } else {
  1075. ab.append(buf, len);
  1076. }
  1077. /* Show hits if any. */
  1078. refreshShowHints(ab, l, pcollen);
  1079. }
  1080. /* Erase to right */
  1081. snprintf(seq,sizeof(seq),"\x1b[0K");
  1082. ab.append(seq);
  1083. if (flags & REFRESH_WRITE) {
  1084. /* Move cursor to original position. */
  1085. snprintf(seq, sizeof(seq), "\r\x1b[%dC", (int) (columnPos(buf, len, pos) + pcollen));
  1086. ab.append(seq);
  1087. }
  1088. (void) !write(fd, ab.c_str(), ab.size()); /* Can't recover from write error. */
  1089. }
  1090. /* Get column length from begining of buffer to current byte position for multiline mode*/
  1091. static size_t columnPosForMultiLine(const char * buf, size_t buf_len, size_t pos, size_t cols, size_t ini_pos) {
  1092. size_t ret = 0;
  1093. size_t colwid = ini_pos;
  1094. size_t off = 0;
  1095. while (off < buf_len) {
  1096. size_t col_len;
  1097. size_t len = nextCharLen(buf, buf_len, off, &col_len);
  1098. int dif = (int) (colwid + col_len) - (int) cols;
  1099. if (dif > 0) {
  1100. ret += dif;
  1101. colwid = col_len;
  1102. } else if (dif == 0) {
  1103. colwid = 0;
  1104. } else {
  1105. colwid += col_len;
  1106. }
  1107. if (off >= pos) {
  1108. break;
  1109. }
  1110. off += len;
  1111. ret += col_len;
  1112. }
  1113. return ret;
  1114. }
  1115. /* Multi line low level line refresh.
  1116. *
  1117. * Rewrite the currently edited line accordingly to the buffer content,
  1118. * cursor position, and number of columns of the terminal.
  1119. *
  1120. * Flags is REFRESH_* macros. The function can just remove the old
  1121. * prompt, just write it, or both. */
  1122. static void refreshMultiLine(struct linenoiseState *l, int flags) {
  1123. char seq[64];
  1124. size_t pcollen = promptTextColumnLen(l->prompt, strlen(l->prompt));
  1125. int colpos = columnPosForMultiLine(l->buf, l->len, l->len, l->cols, pcollen);
  1126. int colpos2; /* cursor column position. */
  1127. int rows = (pcollen + colpos + l->cols - 1) / l->cols; /* rows used by current buf. */
  1128. int rpos = (pcollen + l->oldcolpos + l->cols) / l->cols; /* cursor relative row. */
  1129. int rpos2; /* rpos after refresh. */
  1130. int col; /* column position, zero-based. */
  1131. int old_rows = l->oldrows;
  1132. int fd = l->ofd, j;
  1133. std::string ab;
  1134. l->oldrows = rows;
  1135. /* First step: clear all the lines used before. To do so start by
  1136. * going to the last row. */
  1137. if (flags & REFRESH_CLEAN) {
  1138. if (old_rows - rpos > 0) {
  1139. snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
  1140. ab.append(seq);
  1141. }
  1142. /* Now for every row clear it, go up. */
  1143. for (j = 0; j < old_rows - 1; j++) {
  1144. snprintf(seq,64,"\r\x1b[0K\x1b[1A");
  1145. ab.append(seq);
  1146. }
  1147. }
  1148. if (flags & REFRESH_ALL) {
  1149. /* Clean the top line. */
  1150. snprintf(seq,64,"\r\x1b[0K");
  1151. ab.append(seq);
  1152. }
  1153. /* Get column length to cursor position */
  1154. colpos2 = columnPosForMultiLine(l->buf, l->len, l->pos, l->cols, pcollen);
  1155. if (flags & REFRESH_WRITE) {
  1156. /* Write the prompt and the current buffer content */
  1157. ab.append(l->prompt);
  1158. if (maskmode == 1) {
  1159. for (unsigned int i = 0; i < l->len; ++i) {
  1160. ab.append("*");
  1161. }
  1162. } else {
  1163. ab.append(l->buf, l->len);
  1164. }
  1165. /* Show hits if any. */
  1166. refreshShowHints(ab, l, pcollen);
  1167. /* If we are at the very end of the screen with our prompt, we need to
  1168. * emit a newline and move the prompt to the first column. */
  1169. if (l->pos && l->pos == l->len && (colpos2 + pcollen) % l->cols == 0) {
  1170. ab.append("\n");
  1171. snprintf(seq,64,"\r");
  1172. ab.append(seq);
  1173. rows++;
  1174. if (rows > (int)l->oldrows) l->oldrows = rows;
  1175. }
  1176. /* Move cursor to right position. */
  1177. rpos2 = (pcollen + colpos2 + l->cols) / l->cols; /* Current cursor relative row */
  1178. /* Go up till we reach the expected position. */
  1179. if (rows - rpos2 > 0) {
  1180. snprintf(seq,64,"\x1b[%dA", rows-rpos2);
  1181. ab.append(seq);
  1182. }
  1183. /* Set column. */
  1184. col = (pcollen + colpos2) % l->cols;
  1185. if (col)
  1186. snprintf(seq,64,"\r\x1b[%dC", col);
  1187. else
  1188. snprintf(seq,64,"\r");
  1189. ab.append(seq);
  1190. }
  1191. l->oldcolpos = colpos2;
  1192. (void) !write(fd, ab.c_str(), ab.size()); /* Can't recover from write error. */
  1193. }
  1194. /* Calls the two low level functions refreshSingleLine() or
  1195. * refreshMultiLine() according to the selected mode. */
  1196. static void refreshLineWithFlags(struct linenoiseState *l, int flags) {
  1197. if (mlmode)
  1198. refreshMultiLine(l,flags);
  1199. else
  1200. refreshSingleLine(l,flags);
  1201. }
  1202. /* Utility function to avoid specifying REFRESH_ALL all the times. */
  1203. static void refreshLine(struct linenoiseState *l) {
  1204. refreshLineWithFlags(l,REFRESH_ALL);
  1205. }
  1206. /* Hide the current line, when using the multiplexing API. */
  1207. void linenoiseHide(struct linenoiseState *l) {
  1208. if (mlmode)
  1209. refreshMultiLine(l,REFRESH_CLEAN);
  1210. else
  1211. refreshSingleLine(l,REFRESH_CLEAN);
  1212. }
  1213. /* Show the current line, when using the multiplexing API. */
  1214. void linenoiseShow(struct linenoiseState *l) {
  1215. if (l->in_completion) {
  1216. refreshLineWithCompletion(l,NULL,REFRESH_WRITE);
  1217. } else {
  1218. refreshLineWithFlags(l,REFRESH_WRITE);
  1219. }
  1220. }
  1221. /* Insert the character 'c' at cursor current position.
  1222. *
  1223. * On error writing to the terminal -1 is returned, otherwise 0. */
  1224. static int linenoiseEditInsert(struct linenoiseState * l, const char * cbuf, int clen) {
  1225. if (l->len + clen <= l->buflen) {
  1226. if (l->len == l->pos) {
  1227. memcpy(&l->buf[l->pos], cbuf, clen);
  1228. l->pos += clen;
  1229. l->len += clen;
  1230. ;
  1231. l->buf[l->len] = '\0';
  1232. if ((!mlmode && promptTextColumnLen(l->prompt, l->plen) + columnPos(l->buf, l->len, l->len) < l->cols &&
  1233. !hintsCallback)) {
  1234. /* Avoid a full update of the line in the
  1235. * trivial case. */
  1236. if (maskmode == 1) {
  1237. static const char d = '*';
  1238. if (write(l->ofd, &d, 1) == -1) {
  1239. return -1;
  1240. }
  1241. } else {
  1242. if (write(l->ofd, cbuf, clen) == -1) {
  1243. return -1;
  1244. }
  1245. }
  1246. } else {
  1247. refreshLine(l);
  1248. }
  1249. } else {
  1250. memmove(l->buf + l->pos + clen, l->buf + l->pos, l->len - l->pos);
  1251. memcpy(&l->buf[l->pos], cbuf, clen);
  1252. l->pos += clen;
  1253. l->len += clen;
  1254. l->buf[l->len] = '\0';
  1255. refreshLine(l);
  1256. }
  1257. }
  1258. return 0;
  1259. }
  1260. /* Move cursor on the left. */
  1261. static void linenoiseEditMoveLeft(struct linenoiseState * l) {
  1262. if (l->pos > 0) {
  1263. l->pos -= prevCharLen(l->buf, l->len, l->pos, NULL);
  1264. refreshLine(l);
  1265. }
  1266. }
  1267. /* Move cursor on the right. */
  1268. static void linenoiseEditMoveRight(struct linenoiseState * l) {
  1269. if (l->pos != l->len) {
  1270. l->pos += nextCharLen(l->buf, l->len, l->pos, NULL);
  1271. refreshLine(l);
  1272. }
  1273. }
  1274. /* Move cursor to the start of the line. */
  1275. static void linenoiseEditMoveHome(struct linenoiseState * l) {
  1276. if (l->pos != 0) {
  1277. l->pos = 0;
  1278. refreshLine(l);
  1279. }
  1280. }
  1281. /* Move cursor to the end of the line. */
  1282. static void linenoiseEditMoveEnd(struct linenoiseState * l) {
  1283. if (l->pos != l->len) {
  1284. l->pos = l->len;
  1285. refreshLine(l);
  1286. }
  1287. }
  1288. /* Substitute the currently edited line with the next or previous history
  1289. * entry as specified by 'dir'. */
  1290. #define LINENOISE_HISTORY_NEXT 0
  1291. #define LINENOISE_HISTORY_PREV 1
  1292. static void linenoiseEditHistoryNext(struct linenoiseState * l, int dir) {
  1293. if (history_len > 1) {
  1294. /* Update the current history entry before to
  1295. * overwrite it with the next one. */
  1296. free(history[history_len - 1 - l->history_index]);
  1297. history[history_len - 1 - l->history_index] = strdup(l->buf);
  1298. /* Show the new entry */
  1299. l->history_index += (dir == LINENOISE_HISTORY_PREV) ? 1 : -1;
  1300. if (l->history_index < 0) {
  1301. l->history_index = 0;
  1302. return;
  1303. } else if (l->history_index >= history_len) {
  1304. l->history_index = history_len-1;
  1305. return;
  1306. }
  1307. strncpy(l->buf,history[history_len - 1 - l->history_index],l->buflen);
  1308. l->buf[l->buflen-1] = '\0';
  1309. l->len = l->pos = strlen(l->buf);
  1310. refreshLine(l);
  1311. }
  1312. }
  1313. /* Delete the character at the right of the cursor without altering the cursor
  1314. * position. Basically this is what happens with the "Delete" keyboard key. */
  1315. static void linenoiseEditDelete(struct linenoiseState * l) {
  1316. if (l->len > 0 && l->pos < l->len) {
  1317. int chlen = nextCharLen(l->buf, l->len, l->pos, NULL);
  1318. memmove(l->buf + l->pos, l->buf + l->pos + chlen, l->len - l->pos - chlen);
  1319. l->len -= chlen;
  1320. l->buf[l->len] = '\0';
  1321. refreshLine(l);
  1322. }
  1323. }
  1324. /* Backspace implementation. */
  1325. static void linenoiseEditBackspace(struct linenoiseState * l) {
  1326. if (l->pos > 0 && l->len > 0) {
  1327. int chlen = prevCharLen(l->buf, l->len, l->pos, NULL);
  1328. memmove(l->buf + l->pos - chlen, l->buf + l->pos, l->len - l->pos);
  1329. l->pos -= chlen;
  1330. l->len -= chlen;
  1331. l->buf[l->len] = '\0';
  1332. refreshLine(l);
  1333. }
  1334. }
  1335. /* Delete the previous word, maintaining the cursor at the start of the
  1336. * current word. */
  1337. static void linenoiseEditDeletePrevWord(struct linenoiseState * l) {
  1338. size_t old_pos = l->pos;
  1339. size_t diff;
  1340. while (l->pos > 0 && l->buf[l->pos-1] == ' ')
  1341. l->pos--;
  1342. while (l->pos > 0 && l->buf[l->pos-1] != ' ')
  1343. l->pos--;
  1344. diff = old_pos - l->pos;
  1345. memmove(l->buf+l->pos,l->buf+old_pos,l->len-old_pos+1);
  1346. l->len -= diff;
  1347. refreshLine(l);
  1348. }
  1349. /* This function is part of the multiplexed API of Linenoise, that is used
  1350. * in order to implement the blocking variant of the API but can also be
  1351. * called by the user directly in an event driven program. It will:
  1352. *
  1353. * 1. Initialize the linenoise state passed by the user.
  1354. * 2. Put the terminal in RAW mode.
  1355. * 3. Show the prompt.
  1356. * 4. Return control to the user, that will have to call linenoiseEditFeed()
  1357. * each time there is some data arriving in the standard input.
  1358. *
  1359. * The user can also call linenoiseEditHide() and linenoiseEditShow() if it
  1360. * is required to show some input arriving asynchronously, without mixing
  1361. * it with the currently edited line.
  1362. *
  1363. * When linenoiseEditFeed() returns non-NULL, the user finished with the
  1364. * line editing session (pressed enter CTRL-D/C): in this case the caller
  1365. * needs to call linenoiseEditStop() to put back the terminal in normal
  1366. * mode. This will not destroy the buffer, as long as the linenoiseState
  1367. * is still valid in the context of the caller.
  1368. *
  1369. * The function returns 0 on success, or -1 if writing to standard output
  1370. * fails. If stdin_fd or stdout_fd are set to -1, the default is to use
  1371. * STDIN_FILENO and STDOUT_FILENO.
  1372. */
  1373. int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) {
  1374. /* Populate the linenoise state that we pass to functions implementing
  1375. * specific editing functionalities. */
  1376. l->in_completion = 0;
  1377. l->ifd = stdin_fd != -1 ? stdin_fd : STDIN_FILENO;
  1378. l->ofd = stdout_fd != -1 ? stdout_fd : STDOUT_FILENO;
  1379. l->buf = buf;
  1380. l->buflen = buflen;
  1381. l->prompt = prompt;
  1382. l->plen = strlen(prompt);
  1383. l->oldcolpos = l->pos = 0;
  1384. l->len = 0;
  1385. /* Enter raw mode. */
  1386. if (enableRawMode(l->ifd) == -1) return -1;
  1387. l->cols = getColumns(stdin_fd, stdout_fd);
  1388. l->oldrows = 0;
  1389. l->history_index = 0;
  1390. /* Buffer starts empty. */
  1391. l->buf[0] = '\0';
  1392. l->buflen--; /* Make sure there is always space for the nullterm */
  1393. /* If stdin is not a tty, stop here with the initialization. We
  1394. * will actually just read a line from standard input in blocking
  1395. * mode later, in linenoiseEditFeed(). */
  1396. if (!isatty(l->ifd)) return 0;
  1397. /* The latest history entry is always our current buffer, that
  1398. * initially is just an empty string. */
  1399. linenoiseHistoryAdd("");
  1400. if (write(l->ofd,prompt,l->plen) == -1) return -1;
  1401. return 0;
  1402. }
  1403. const char* linenoiseEditMore = "If you see this, you are misusing the API: when linenoiseEditFeed() is called, if it returns linenoiseEditMore the user is yet editing the line. See the README file for more information.";
  1404. static const char * handleEnterKey(struct linenoiseState * l) {
  1405. --history_len;
  1406. free(history[history_len]);
  1407. if (mlmode) {
  1408. linenoiseEditMoveEnd(l);
  1409. }
  1410. if (hintsCallback) {
  1411. /* Force a refresh without hints to leave the previous
  1412. * line as the user typed it after a newline. */
  1413. linenoiseHintsCallback * hc = hintsCallback;
  1414. hintsCallback = NULL;
  1415. refreshLine(l);
  1416. hintsCallback = hc;
  1417. }
  1418. return strdup(l->buf);
  1419. }
  1420. static const char * handleCtrlCKey() {
  1421. errno = EAGAIN;
  1422. return NULL;
  1423. }
  1424. static const char * handleCtrlDKey(struct linenoiseState * l) {
  1425. if (l->len > 0) {
  1426. linenoiseEditDelete(l);
  1427. return linenoiseEditMore;
  1428. }
  1429. --history_len;
  1430. free(history[history_len]);
  1431. errno = ENOENT;
  1432. return NULL;
  1433. }
  1434. static void handleCtrlTKey(struct linenoiseState * l) {
  1435. if (l->pos > 0 && l->pos < l->len) {
  1436. auto prev_chlen = prevCharLen(l->buf, l->len, l->pos, NULL);
  1437. auto curr_chlen = nextCharLen(l->buf, l->len, l->pos, NULL);
  1438. std::string prev_char(prev_chlen, 0);
  1439. memcpy(prev_char.data(), l->buf + l->pos - prev_chlen, prev_chlen);
  1440. memmove(l->buf + l->pos - prev_chlen, l->buf + l->pos, curr_chlen);
  1441. memmove(l->buf + l->pos - prev_chlen + curr_chlen, prev_char.data(), prev_chlen);
  1442. l->pos = l->pos - prev_chlen + curr_chlen;
  1443. if (l->pos + prev_chlen != l->len) {
  1444. l->pos += prev_chlen;
  1445. }
  1446. refreshLine(l);
  1447. }
  1448. }
  1449. static void handleEscapeSequence(struct linenoiseState * l, int esc_type) {
  1450. switch (esc_type) {
  1451. case ESC_NULL:
  1452. break;
  1453. case ESC_DELETE:
  1454. linenoiseEditDelete(l);
  1455. break;
  1456. case ESC_UP:
  1457. linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV);
  1458. break;
  1459. case ESC_DOWN:
  1460. linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT);
  1461. break;
  1462. case ESC_RIGHT:
  1463. linenoiseEditMoveRight(l);
  1464. break;
  1465. case ESC_LEFT:
  1466. linenoiseEditMoveLeft(l);
  1467. break;
  1468. case ESC_HOME:
  1469. linenoiseEditMoveHome(l);
  1470. break;
  1471. case ESC_END:
  1472. linenoiseEditMoveEnd(l);
  1473. break;
  1474. }
  1475. }
  1476. static void handleCtrlUKey(struct linenoiseState * l) {
  1477. l->buf[0] = '\0';
  1478. l->pos = l->len = 0;
  1479. refreshLine(l);
  1480. }
  1481. static void handleCtrlKKey(struct linenoiseState * l) {
  1482. l->buf[l->pos] = '\0';
  1483. l->len = l->pos;
  1484. refreshLine(l);
  1485. }
  1486. static const char * processInputCharacter(struct linenoiseState * l, int c, char * cbuf, int nread, int esc_type) {
  1487. switch (c) {
  1488. case ENTER:
  1489. return handleEnterKey(l);
  1490. case CTRL_C:
  1491. return handleCtrlCKey();
  1492. case BACKSPACE:
  1493. case CTRL_H:
  1494. linenoiseEditBackspace(l);
  1495. break;
  1496. case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the
  1497. line is empty, act as end-of-file. */
  1498. return handleCtrlDKey(l);
  1499. case CTRL_T:
  1500. handleCtrlTKey(l);
  1501. break;
  1502. case CTRL_B:
  1503. linenoiseEditMoveLeft(l);
  1504. break;
  1505. case CTRL_F:
  1506. linenoiseEditMoveRight(l);
  1507. break;
  1508. case CTRL_P:
  1509. linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV);
  1510. break;
  1511. case CTRL_N:
  1512. linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT);
  1513. break;
  1514. case ESC:
  1515. handleEscapeSequence(l, esc_type);
  1516. break;
  1517. default:
  1518. if (linenoiseEditInsert(l, cbuf, nread)) {
  1519. return NULL;
  1520. }
  1521. break;
  1522. case CTRL_U: /* Ctrl+u, delete the whole line. */
  1523. handleCtrlUKey(l);
  1524. break;
  1525. case CTRL_K: /* Ctrl+k, delete from current to end of line. */
  1526. handleCtrlKKey(l);
  1527. break;
  1528. case CTRL_A: /* Ctrl+a, go to the start of the line */
  1529. linenoiseEditMoveHome(l);
  1530. break;
  1531. case CTRL_E: /* ctrl+e, go to the end of the line */
  1532. linenoiseEditMoveEnd(l);
  1533. break;
  1534. case CTRL_L: /* ctrl+l, clear screen */
  1535. linenoiseClearScreen();
  1536. refreshLine(l);
  1537. break;
  1538. case CTRL_W: /* ctrl+w, delete previous word */
  1539. linenoiseEditDeletePrevWord(l);
  1540. break;
  1541. }
  1542. return linenoiseEditMore;
  1543. }
  1544. /* This function is part of the multiplexed API of linenoise, see the top
  1545. * comment on linenoiseEditStart() for more information. Call this function
  1546. * each time there is some data to read from the standard input file
  1547. * descriptor. In the case of blocking operations, this function can just be
  1548. * called in a loop, and block.
  1549. *
  1550. * The function returns linenoiseEditMore to signal that line editing is still
  1551. * in progress, that is, the user didn't yet pressed enter / CTRL-D. Otherwise
  1552. * the function returns the pointer to the heap-allocated buffer with the
  1553. * edited line, that the user should free with linenoiseFree().
  1554. *
  1555. * On special conditions, NULL is returned and errno is populated:
  1556. *
  1557. * EAGAIN if the user pressed Ctrl-C
  1558. * ENOENT if the user pressed Ctrl-D
  1559. *
  1560. * Some other errno: I/O error.
  1561. */
  1562. const char * linenoiseEditFeed(struct linenoiseState * l) {
  1563. /* Not a TTY, pass control to line reading without character count
  1564. * limits. */
  1565. if (!isatty(l->ifd)) return linenoiseNoTTY();
  1566. int c;
  1567. int nread;
  1568. char cbuf[32];
  1569. nread = readCode(l->ifd, cbuf, sizeof(cbuf), &c);
  1570. if (nread <= 0) return NULL;
  1571. auto esc_type = ESC_NULL;
  1572. if (c == ESC) {
  1573. esc_type = readEscapeSequence(l);
  1574. }
  1575. /* Only autocomplete when the callback is set. It returns < 0 when
  1576. * there was an error reading from fd. Otherwise it will return the
  1577. * character that should be handled next. */
  1578. if ((l->in_completion || c == 9) && completionCallback != NULL) {
  1579. c = completeLine(l, c, esc_type);
  1580. /* Read next character when 0 */
  1581. if (c == 0) return linenoiseEditMore;
  1582. }
  1583. return processInputCharacter(l, c, cbuf, nread, esc_type);
  1584. }
  1585. /* This is part of the multiplexed linenoise API. See linenoiseEditStart()
  1586. * for more information. This function is called when linenoiseEditFeed()
  1587. * returns something different than NULL. At this point the user input
  1588. * is in the buffer, and we can restore the terminal in normal mode. */
  1589. void linenoiseEditStop(struct linenoiseState *l) {
  1590. if (!isatty(l->ifd)) return;
  1591. disableRawMode(l->ifd);
  1592. printf("\n");
  1593. }
  1594. /* This just implements a blocking loop for the multiplexed API.
  1595. * In many applications that are not event-driven, we can just call
  1596. * the blocking linenoise API, wait for the user to complete the editing
  1597. * and return the buffer. */
  1598. static const char *linenoiseBlockingEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt)
  1599. {
  1600. struct linenoiseState l;
  1601. /* Editing without a buffer is invalid. */
  1602. if (buflen == 0) {
  1603. errno = EINVAL;
  1604. return NULL;
  1605. }
  1606. linenoiseEditStart(&l,stdin_fd,stdout_fd,buf,buflen,prompt);
  1607. const char *res;
  1608. while((res = linenoiseEditFeed(&l)) == linenoiseEditMore);
  1609. linenoiseEditStop(&l);
  1610. return res;
  1611. }
  1612. /* This special mode is used by linenoise in order to print scan codes
  1613. * on screen for debugging / development purposes. It is implemented
  1614. * by the linenoise_example program using the --keycodes option. */
  1615. void linenoisePrintKeyCodes(void) {
  1616. char quit[4];
  1617. printf("Linenoise key codes debugging mode.\n"
  1618. "Press keys to see scan codes. Type 'quit' at any time to exit.\n");
  1619. if (enableRawMode(STDIN_FILENO) == -1) return;
  1620. memset(quit,' ',4);
  1621. while(1) {
  1622. char c;
  1623. int nread;
  1624. nread = read(STDIN_FILENO,&c,1);
  1625. if (nread <= 0) continue;
  1626. memmove(quit,quit+1,sizeof(quit)-1); /* shift string to left. */
  1627. quit[sizeof(quit)-1] = c; /* Insert current char on the right. */
  1628. if (memcmp(quit,"quit",sizeof(quit)) == 0) break;
  1629. printf("'%c' %02x (%d) (type quit to exit)\n", isprint((int) c) ? c : '?', (int) c, (int) c);
  1630. printf("\r"); /* Go left edge manually, we are in raw mode. */
  1631. fflush(stdout);
  1632. }
  1633. disableRawMode(STDIN_FILENO);
  1634. }
  1635. /* This function is called when linenoise() is called with the standard
  1636. * input file descriptor not attached to a TTY. So for example when the
  1637. * program using linenoise is called in pipe or with a file redirected
  1638. * to its standard input. In this case, we want to be able to return the
  1639. * line regardless of its length (by default we are limited to 4k). */
  1640. static char *linenoiseNoTTY(void) {
  1641. char *line = NULL;
  1642. size_t len = 0, maxlen = 0;
  1643. while(1) {
  1644. if (len == maxlen) {
  1645. if (maxlen == 0) maxlen = 16;
  1646. maxlen *= 2;
  1647. char *oldval = line;
  1648. line = (char*) realloc(line,maxlen);
  1649. if (line == NULL) {
  1650. if (oldval) free(oldval);
  1651. return NULL;
  1652. }
  1653. }
  1654. int c = fgetc(stdin);
  1655. if (c == EOF || c == '\n') {
  1656. if (c == EOF && len == 0) {
  1657. free(line);
  1658. return NULL;
  1659. } else {
  1660. line[len] = '\0';
  1661. return line;
  1662. }
  1663. } else {
  1664. line[len] = c;
  1665. len++;
  1666. }
  1667. }
  1668. }
  1669. /* The high level function that is the main API of the linenoise library.
  1670. * This function checks if the terminal has basic capabilities, just checking
  1671. * for a blacklist of stupid terminals, and later either calls the line
  1672. * editing function or uses dummy fgets() so that you will be able to type
  1673. * something even in the most desperate of the conditions. */
  1674. const char *linenoise(const char *prompt) {
  1675. char buf[LINENOISE_MAX_LINE];
  1676. if (!isatty(STDIN_FILENO)) {
  1677. /* Not a tty: read from file / pipe. In this mode we don't want any
  1678. * limit to the line size, so we call a function to handle that. */
  1679. return linenoiseNoTTY();
  1680. } else if (isUnsupportedTerm()) {
  1681. size_t len;
  1682. printf("%s",prompt);
  1683. fflush(stdout);
  1684. if (fgets(buf,LINENOISE_MAX_LINE,stdin) == NULL) return NULL;
  1685. len = strlen(buf);
  1686. while(len && (buf[len-1] == '\n' || buf[len-1] == '\r')) {
  1687. len--;
  1688. buf[len] = '\0';
  1689. }
  1690. return strdup(buf);
  1691. } else {
  1692. const char *retval = linenoiseBlockingEdit(STDIN_FILENO,STDOUT_FILENO,buf,LINENOISE_MAX_LINE,prompt);
  1693. return retval;
  1694. }
  1695. }
  1696. /* This is just a wrapper the user may want to call in order to make sure
  1697. * the linenoise returned buffer is freed with the same allocator it was
  1698. * created with. Useful when the main program is using an alternative
  1699. * allocator. */
  1700. void linenoiseFree(void *ptr) {
  1701. if (ptr == linenoiseEditMore) return; // Protect from API misuse.
  1702. free(ptr);
  1703. }
  1704. /* ================================ History ================================= */
  1705. /* Free the history, but does not reset it. Only used when we have to
  1706. * exit() to avoid memory leaks are reported by valgrind & co. */
  1707. static void freeHistory(void) {
  1708. if (history) {
  1709. int j;
  1710. for (j = 0; j < history_len; j++)
  1711. free(history[j]);
  1712. free(history);
  1713. }
  1714. }
  1715. /* At exit we'll try to fix the terminal to the initial conditions. */
  1716. static void linenoiseAtExit(void) {
  1717. disableRawMode(STDIN_FILENO);
  1718. freeHistory();
  1719. }
  1720. /* This is the API call to add a new entry in the linenoise history.
  1721. * It uses a fixed array of char pointers that are shifted (memmoved)
  1722. * when the history max length is reached in order to remove the older
  1723. * entry and make room for the new one, so it is not exactly suitable for huge
  1724. * histories, but will work well for a few hundred of entries.
  1725. *
  1726. * Using a circular buffer is smarter, but a bit more complex to handle. */
  1727. int linenoiseHistoryAdd(const char *line) {
  1728. char *linecopy;
  1729. if (history_max_len == 0) return 0;
  1730. /* Initialization on first call. */
  1731. if (history == NULL) {
  1732. history = (char**) malloc(sizeof(char*)*history_max_len);
  1733. if (history == NULL) return 0;
  1734. memset(history,0,(sizeof(char*)*history_max_len));
  1735. }
  1736. /* Don't add duplicated lines. */
  1737. if (history_len && !strcmp(history[history_len-1], line)) return 0;
  1738. /* Add an heap allocated copy of the line in the history.
  1739. * If we reached the max length, remove the older line. */
  1740. linecopy = strdup(line);
  1741. if (!linecopy) return 0;
  1742. if (history_len == history_max_len) {
  1743. free(history[0]);
  1744. memmove(history,history+1,sizeof(char*)*(history_max_len-1));
  1745. history_len--;
  1746. }
  1747. history[history_len] = linecopy;
  1748. history_len++;
  1749. return 1;
  1750. }
  1751. /* Set the maximum length for the history. This function can be called even
  1752. * if there is already some history, the function will make sure to retain
  1753. * just the latest 'len' elements if the new history length value is smaller
  1754. * than the amount of items already inside the history. */
  1755. int linenoiseHistorySetMaxLen(int len) {
  1756. char **new_ptr;
  1757. if (len < 1) return 0;
  1758. if (history) {
  1759. int tocopy = history_len;
  1760. new_ptr = (char**) malloc(sizeof(char*)*len);
  1761. if (new_ptr == NULL) return 0;
  1762. /* If we can't copy everything, free the elements we'll not use. */
  1763. if (len < tocopy) {
  1764. int j;
  1765. for (j = 0; j < tocopy-len; j++) free(history[j]);
  1766. tocopy = len;
  1767. }
  1768. memset(new_ptr,0,sizeof(char*)*len);
  1769. memcpy(new_ptr,history+(history_len-tocopy), sizeof(char*)*tocopy);
  1770. free(history);
  1771. history = new_ptr;
  1772. }
  1773. history_max_len = len;
  1774. if (history_len > history_max_len)
  1775. history_len = history_max_len;
  1776. return 1;
  1777. }
  1778. /* Save the history in the specified file. On success 0 is returned
  1779. * otherwise -1 is returned. */
  1780. int linenoiseHistorySave(const char *filename) {
  1781. mode_t old_umask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
  1782. File file;
  1783. file.open(filename, "w");
  1784. umask(old_umask);
  1785. if (file.file == NULL) {
  1786. return -1;
  1787. }
  1788. chmod(filename,S_IRUSR|S_IWUSR);
  1789. for (int j = 0; j < history_len; ++j) {
  1790. fprintf(file.file, "%s\n", history[j]);
  1791. }
  1792. return 0;
  1793. }
  1794. /* Load the history from the specified file. If the file does not exist
  1795. * zero is returned and no operation is performed.
  1796. *
  1797. * If the file exists and the operation succeeded 0 is returned, otherwise
  1798. * on error -1 is returned. */
  1799. int linenoiseHistoryLoad(const char *filename) {
  1800. File file;
  1801. file.open(filename, "r");
  1802. char buf[LINENOISE_MAX_LINE];
  1803. if (file.file == NULL) {
  1804. return -1;
  1805. }
  1806. while (fgets(buf, LINENOISE_MAX_LINE, file.file) != NULL) {
  1807. char *p;
  1808. p = strchr(buf,'\r');
  1809. if (!p) p = strchr(buf,'\n');
  1810. if (p) *p = '\0';
  1811. linenoiseHistoryAdd(buf);
  1812. }
  1813. return 0;
  1814. }
  1815. #endif