2
0

bestline.c 139 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837
  1. /*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-│
  2. │ vi: set et ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi │
  3. ╞══════════════════════════════════════════════════════════════════════════════╡
  4. │ │
  5. │ Bestline ── Library for interactive pseudoteletypewriter command │
  6. │ sessions using ANSI Standard X3.64 control sequences │
  7. │ │
  8. │ OVERVIEW │
  9. │ │
  10. │ Bestline is a fork of linenoise (a popular readline alternative) │
  11. │ that fixes its bugs and adds the missing features while reducing │
  12. │ binary footprint (surprisingly) by removing bloated dependencies │
  13. │ which means you can finally have a permissively-licensed command │
  14. │ prompt w/ a 30kb footprint that's nearly as good as gnu readline │
  15. │ │
  16. │ EXAMPLE │
  17. │ │
  18. │ main() { │
  19. │ char *line; │
  20. │ while ((line = bestlineWithHistory("IN> ", "foo"))) { │
  21. │ fputs("OUT> ", stdout); │
  22. │ fputs(line, stdout); │
  23. │ fputs("\n", stdout); │
  24. │ free(line); │
  25. │ } │
  26. │ } │
  27. │ │
  28. │ CHANGES │
  29. │ │
  30. │ - Remove bell │
  31. │ - Add kill ring │
  32. │ - Fix flickering │
  33. │ - Add UTF-8 editing │
  34. │ - Add CTRL-R search │
  35. │ - Support unlimited lines │
  36. │ - Add parentheses awareness │
  37. │ - React to terminal resizing │
  38. │ - Don't generate .data section │
  39. │ - Support terminal flow control │
  40. │ - Make history loading 10x faster │
  41. │ - Make multiline mode the only mode │
  42. │ - Accommodate O_NONBLOCK file descriptors │
  43. │ - Restore raw mode on process foregrounding │
  44. │ - Make source code compatible with C++ compilers │
  45. │ - Fix corruption issues by using generalized parsing │
  46. │ - Implement nearly all GNU readline editing shortcuts │
  47. │ - Remove heavyweight dependencies like printf/sprintf │
  48. │ - Remove ISIG→^C→EAGAIN hack and use ephemeral handlers │
  49. │ - Support running on Windows in MinTTY or CMD.EXE on Win10+ │
  50. │ - Support diacratics, русский, Ελληνικά, 漢字, 仮名, 한글 │
  51. │ │
  52. │ SHORTCUTS │
  53. │ │
  54. │ CTRL-E END │
  55. │ CTRL-A START │
  56. │ CTRL-B BACK │
  57. │ CTRL-F FORWARD │
  58. │ CTRL-L CLEAR │
  59. │ CTRL-H BACKSPACE │
  60. │ CTRL-D DELETE │
  61. │ CTRL-Y YANK │
  62. │ CTRL-D EOF (IF EMPTY) │
  63. │ CTRL-N NEXT HISTORY │
  64. │ CTRL-P PREVIOUS HISTORY │
  65. │ CTRL-R SEARCH HISTORY │
  66. │ CTRL-G CANCEL SEARCH │
  67. │ CTRL-J INSERT NEWLINE │
  68. │ ALT-< BEGINNING OF HISTORY │
  69. │ ALT-> END OF HISTORY │
  70. │ ALT-F FORWARD WORD │
  71. │ ALT-B BACKWARD WORD │
  72. │ CTRL-ALT-F FORWARD EXPR │
  73. │ CTRL-ALT-B BACKWARD EXPR │
  74. │ ALT-RIGHT FORWARD EXPR │
  75. │ ALT-LEFT BACKWARD EXPR │
  76. │ ALT-SHIFT-B BARF EXPR │
  77. │ ALT-SHIFT-S SLURP EXPR │
  78. │ ALT-SHIFT-R RAISE EXPR │
  79. │ CTRL-K KILL LINE FORWARDS │
  80. │ CTRL-U KILL LINE BACKWARDS │
  81. │ ALT-H KILL WORD BACKWARDS │
  82. │ CTRL-W KILL WORD BACKWARDS │
  83. │ CTRL-ALT-H KILL WORD BACKWARDS │
  84. │ ALT-D KILL WORD FORWARDS │
  85. │ ALT-Y ROTATE KILL RING AND YANK AGAIN │
  86. │ ALT-\ SQUEEZE ADJACENT WHITESPACE │
  87. │ CTRL-T TRANSPOSE │
  88. │ ALT-T TRANSPOSE WORD │
  89. │ ALT-U UPPERCASE WORD │
  90. │ ALT-L LOWERCASE WORD │
  91. │ ALT-C CAPITALIZE WORD │
  92. │ CTRL-C CTRL-C INTERRUPT PROCESS │
  93. │ CTRL-Z SUSPEND PROCESS │
  94. │ CTRL-\ QUIT PROCESS │
  95. │ CTRL-S PAUSE OUTPUT │
  96. │ CTRL-Q UNPAUSE OUTPUT (IF PAUSED) │
  97. │ CTRL-Q ESCAPED INSERT │
  98. │ CTRL-SPACE SET MARK │
  99. │ CTRL-X CTRL-X GOTO MARK │
  100. │ PROTIP REMAP CAPS LOCK TO CTRL │
  101. │ │
  102. ╞══════════════════════════════════════════════════════════════════════════════╡
  103. │ │
  104. │ Copyright 2018-2021 Justine Tunney <jtunney@gmail.com> │
  105. │ Copyright 2010-2016 Salvatore Sanfilippo <antirez@gmail.com> │
  106. │ Copyright 2010-2013 Pieter Noordhuis <pcnoordhuis@gmail.com> │
  107. │ │
  108. │ All rights reserved. │
  109. │ │
  110. │ Redistribution and use in source and binary forms, with or without │
  111. │ modification, are permitted provided that the following conditions are │
  112. │ met: │
  113. │ │
  114. │ * Redistributions of source code must retain the above copyright │
  115. │ notice, this list of conditions and the following disclaimer. │
  116. │ │
  117. │ * Redistributions in binary form must reproduce the above copyright │
  118. │ notice, this list of conditions and the following disclaimer in the │
  119. │ documentation and/or other materials provided with the distribution. │
  120. │ │
  121. │ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS │
  122. │ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT │
  123. │ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR │
  124. │ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT │
  125. │ HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, │
  126. │ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT │
  127. │ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, │
  128. │ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY │
  129. │ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT │
  130. │ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE │
  131. │ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. │
  132. │ │
  133. ╚─────────────────────────────────────────────────────────────────────────────*/
  134. #include "bestline.h"
  135. #define _POSIX_C_SOURCE 1 /* so GCC builds in ANSI mode */
  136. #define _XOPEN_SOURCE 700 /* so GCC builds in ANSI mode */
  137. #define _DARWIN_C_SOURCE 1 /* so SIGWINCH / IUTF8 on XNU */
  138. #include <assert.h>
  139. #include <ctype.h>
  140. #include <errno.h>
  141. #include <fcntl.h>
  142. #include <limits.h>
  143. #include <poll.h>
  144. #include <setjmp.h>
  145. #include <signal.h>
  146. #include <stdio.h>
  147. #include <stdlib.h>
  148. #include <string.h>
  149. #include <sys/ioctl.h>
  150. #include <sys/mman.h>
  151. #include <sys/stat.h>
  152. #include <sys/time.h>
  153. #include <sys/types.h>
  154. #include <termios.h>
  155. #include <unistd.h>
  156. #ifndef SIGWINCH
  157. #define SIGWINCH 28 /* GNU/Systemd + XNU + FreeBSD + NetBSD + OpenBSD */
  158. #endif
  159. #ifndef IUTF8
  160. #define IUTF8 0
  161. #endif
  162. __asm__(".ident\t\"\\n\\n\
  163. Bestline (BSD-2)\\n\
  164. Copyright 2018-2020 Justine Tunney <jtunney@gmail.com>\\n\
  165. Copyright 2010-2016 Salvatore Sanfilippo <antirez@gmail.com>\\n\
  166. Copyright 2010-2013 Pieter Noordhuis <pcnoordhuis@gmail.com>\"");
  167. #ifndef BESTLINE_MAX_RING
  168. #define BESTLINE_MAX_RING 8
  169. #endif
  170. #ifndef BESTLINE_MAX_HISTORY
  171. #define BESTLINE_MAX_HISTORY 1024
  172. #endif
  173. #define BESTLINE_HISTORY_PREV +1
  174. #define BESTLINE_HISTORY_NEXT -1
  175. #define Ctrl(C) ((C) ^ 0100)
  176. #define Min(X, Y) ((Y) > (X) ? (X) : (Y))
  177. #define Max(X, Y) ((Y) < (X) ? (X) : (Y))
  178. #define Case(X, Y) \
  179. case X: \
  180. Y; \
  181. break
  182. #define Read16le(X) ((255 & (X)[0]) << 000 | (255 & (X)[1]) << 010)
  183. #define Read32le(X) \
  184. ((unsigned)(255 & (X)[0]) << 000 | (unsigned)(255 & (X)[1]) << 010 | \
  185. (unsigned)(255 & (X)[2]) << 020 | (unsigned)(255 & (X)[3]) << 030)
  186. struct abuf {
  187. char *b;
  188. unsigned len;
  189. unsigned cap;
  190. };
  191. struct rune {
  192. unsigned c;
  193. unsigned n;
  194. };
  195. struct bestlineRing {
  196. unsigned i;
  197. char *p[BESTLINE_MAX_RING];
  198. };
  199. /* The bestlineState structure represents the state during line editing.
  200. * We pass this state to functions implementing specific editing
  201. * functionalities. */
  202. struct bestlineState {
  203. int ifd; /* terminal stdin file descriptor */
  204. int ofd; /* terminal stdout file descriptor */
  205. struct winsize ws; /* rows and columns in terminal */
  206. char *buf; /* edited line buffer */
  207. const char *prompt; /* prompt to display */
  208. int hindex; /* history index */
  209. int rows; /* rows being used */
  210. int oldpos; /* previous refresh cursor position */
  211. unsigned buflen; /* edited line buffer size */
  212. unsigned pos; /* current buffer index */
  213. unsigned len; /* current edited line length */
  214. unsigned mark; /* saved cursor position */
  215. unsigned yi, yj; /* boundaries of last yank */
  216. char seq[2][16]; /* keystroke history for yanking code */
  217. char final; /* set to true on last update */
  218. char dirty; /* if an update was squashed */
  219. struct abuf full; /* used for multiline mode */
  220. };
  221. static const char *const kUnsupported[] = {"dumb", "cons25", "emacs"};
  222. static int gotint;
  223. static int gotcont;
  224. static int gotwinch;
  225. static signed char rawmode;
  226. static char maskmode;
  227. static char emacsmode;
  228. static char llamamode;
  229. static char balancemode;
  230. static char ispaused;
  231. static char iscapital;
  232. static unsigned historylen;
  233. static struct bestlineRing ring;
  234. static struct sigaction orig_cont;
  235. static struct sigaction orig_winch;
  236. static struct termios orig_termios;
  237. static char *history[BESTLINE_MAX_HISTORY];
  238. static bestlineXlatCallback *xlatCallback;
  239. static bestlineHintsCallback *hintsCallback;
  240. static bestlineFreeHintsCallback *freeHintsCallback;
  241. static bestlineCompletionCallback *completionCallback;
  242. static void bestlineAtExit(void);
  243. static void bestlineRefreshLine(struct bestlineState *);
  244. static void bestlineOnInt(int sig) {
  245. gotint = sig;
  246. }
  247. static void bestlineOnCont(int sig) {
  248. gotcont = sig;
  249. }
  250. static void bestlineOnWinch(int sig) {
  251. gotwinch = sig;
  252. }
  253. static char IsControl(unsigned c) {
  254. return c <= 0x1F || (0x7F <= c && c <= 0x9F);
  255. }
  256. static int GetMonospaceCharacterWidth(unsigned c) {
  257. return !IsControl(c) +
  258. (c >= 0x1100 && (c <= 0x115f || c == 0x2329 || c == 0x232a ||
  259. (c >= 0x2e80 && c <= 0xa4cf && c != 0x303f) ||
  260. (c >= 0xac00 && c <= 0xd7a3) || (c >= 0xf900 && c <= 0xfaff) ||
  261. (c >= 0xfe10 && c <= 0xfe19) || (c >= 0xfe30 && c <= 0xfe6f) ||
  262. (c >= 0xff00 && c <= 0xff60) || (c >= 0xffe0 && c <= 0xffe6) ||
  263. (c >= 0x20000 && c <= 0x2fffd) || (c >= 0x30000 && c <= 0x3fffd)));
  264. }
  265. /**
  266. * Returns nonzero if 𝑐 isn't alphanumeric.
  267. *
  268. * Line reading interfaces generally define this operation as UNICODE
  269. * characters that aren't in the letter category (Lu, Ll, Lt, Lm, Lo)
  270. * and aren't in the number categorie (Nd, Nl, No). We also add a few
  271. * other things like blocks and emoji (So).
  272. */
  273. char bestlineIsSeparator(unsigned c) {
  274. int m, l, r, n;
  275. if (c < 0200) {
  276. return !(('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'));
  277. }
  278. if (c <= 0xffff) {
  279. static const unsigned short kGlyphs[][2] = {
  280. {0x00aa, 0x00aa}, /* 1x English */
  281. {0x00b2, 0x00b3}, /* 2x English Arabic */
  282. {0x00b5, 0x00b5}, /* 1x Greek */
  283. {0x00b9, 0x00ba}, /* 2x English Arabic */
  284. {0x00bc, 0x00be}, /* 3x Vulgar English Arabic */
  285. {0x00c0, 0x00d6}, /* 23x Watin */
  286. {0x00d8, 0x00f6}, /* 31x Watin */
  287. {0x0100, 0x02c1}, /* 450x Watin-AB,IPA,Spacemod */
  288. {0x02c6, 0x02d1}, /* 12x Spacemod */
  289. {0x02e0, 0x02e4}, /* 5x Spacemod */
  290. {0x02ec, 0x02ec}, /* 1x Spacemod */
  291. {0x02ee, 0x02ee}, /* 1x Spacemod */
  292. {0x0370, 0x0374}, /* 5x Greek */
  293. {0x0376, 0x0377}, /* 2x Greek */
  294. {0x037a, 0x037d}, /* 4x Greek */
  295. {0x037f, 0x037f}, /* 1x Greek */
  296. {0x0386, 0x0386}, /* 1x Greek */
  297. {0x0388, 0x038a}, /* 3x Greek */
  298. {0x038c, 0x038c}, /* 1x Greek */
  299. {0x038e, 0x03a1}, /* 20x Greek */
  300. {0x03a3, 0x03f5}, /* 83x Greek */
  301. {0x03f7, 0x0481}, /* 139x Greek */
  302. {0x048a, 0x052f}, /* 166x Cyrillic */
  303. {0x0531, 0x0556}, /* 38x Armenian */
  304. {0x0560, 0x0588}, /* 41x Armenian */
  305. {0x05d0, 0x05ea}, /* 27x Hebrew */
  306. {0x0620, 0x064a}, /* 43x Arabic */
  307. {0x0660, 0x0669}, /* 10x Arabic */
  308. {0x0671, 0x06d3}, /* 99x Arabic */
  309. {0x06ee, 0x06fc}, /* 15x Arabic */
  310. {0x0712, 0x072f}, /* 30x Syriac */
  311. {0x074d, 0x07a5}, /* 89x Syriac,Arabic2,Thaana */
  312. {0x07c0, 0x07ea}, /* 43x NKo */
  313. {0x0800, 0x0815}, /* 22x Samaritan */
  314. {0x0840, 0x0858}, /* 25x Mandaic */
  315. {0x0904, 0x0939}, /* 54x Devanagari */
  316. {0x0993, 0x09a8}, /* 22x Bengali */
  317. {0x09e6, 0x09f1}, /* 12x Bengali */
  318. {0x0a13, 0x0a28}, /* 22x Gurmukhi */
  319. {0x0a66, 0x0a6f}, /* 10x Gurmukhi */
  320. {0x0a93, 0x0aa8}, /* 22x Gujarati */
  321. {0x0b13, 0x0b28}, /* 22x Oriya */
  322. {0x0c92, 0x0ca8}, /* 23x Kannada */
  323. {0x0caa, 0x0cb3}, /* 10x Kannada */
  324. {0x0ce6, 0x0cef}, /* 10x Kannada */
  325. {0x0d12, 0x0d3a}, /* 41x Malayalam */
  326. {0x0d85, 0x0d96}, /* 18x Sinhala */
  327. {0x0d9a, 0x0db1}, /* 24x Sinhala */
  328. {0x0de6, 0x0def}, /* 10x Sinhala */
  329. {0x0e01, 0x0e30}, /* 48x Thai */
  330. {0x0e8c, 0x0ea3}, /* 24x Lao */
  331. {0x0f20, 0x0f33}, /* 20x Tibetan */
  332. {0x0f49, 0x0f6c}, /* 36x Tibetan */
  333. {0x109e, 0x10c5}, /* 40x Myanmar,Georgian */
  334. {0x10d0, 0x10fa}, /* 43x Georgian */
  335. {0x10fc, 0x1248}, /* 333x Georgian,Hangul,Ethiopic */
  336. {0x13a0, 0x13f5}, /* 86x Cherokee */
  337. {0x1401, 0x166d}, /* 621x Aboriginal */
  338. {0x16a0, 0x16ea}, /* 75x Runic */
  339. {0x1700, 0x170c}, /* 13x Tagalog */
  340. {0x1780, 0x17b3}, /* 52x Khmer */
  341. {0x1820, 0x1878}, /* 89x Mongolian */
  342. {0x1a00, 0x1a16}, /* 23x Buginese */
  343. {0x1a20, 0x1a54}, /* 53x Tai Tham */
  344. {0x1a80, 0x1a89}, /* 10x Tai Tham */
  345. {0x1a90, 0x1a99}, /* 10x Tai Tham */
  346. {0x1b05, 0x1b33}, /* 47x Balinese */
  347. {0x1b50, 0x1b59}, /* 10x Balinese */
  348. {0x1b83, 0x1ba0}, /* 30x Sundanese */
  349. {0x1bae, 0x1be5}, /* 56x Sundanese */
  350. {0x1c90, 0x1cba}, /* 43x Georgian2 */
  351. {0x1cbd, 0x1cbf}, /* 3x Georgian2 */
  352. {0x1e00, 0x1f15}, /* 278x Watin-C,Greek2 */
  353. {0x2070, 0x2071}, /* 2x Supersub */
  354. {0x2074, 0x2079}, /* 6x Supersub */
  355. {0x207f, 0x2089}, /* 11x Supersub */
  356. {0x2090, 0x209c}, /* 13x Supersub */
  357. {0x2100, 0x2117}, /* 24x Letterlike */
  358. {0x2119, 0x213f}, /* 39x Letterlike */
  359. {0x2145, 0x214a}, /* 6x Letterlike */
  360. {0x214c, 0x218b}, /* 64x Letterlike,Numbery */
  361. {0x21af, 0x21cd}, /* 31x Arrows */
  362. {0x21d5, 0x21f3}, /* 31x Arrows */
  363. {0x230c, 0x231f}, /* 20x Technical */
  364. {0x232b, 0x237b}, /* 81x Technical */
  365. {0x237d, 0x239a}, /* 30x Technical */
  366. {0x23b4, 0x23db}, /* 40x Technical */
  367. {0x23e2, 0x2426}, /* 69x Technical,ControlPictures */
  368. {0x2460, 0x25b6}, /* 343x Enclosed,Boxes,Blocks,Shapes */
  369. {0x25c2, 0x25f7}, /* 54x Shapes */
  370. {0x2600, 0x266e}, /* 111x Symbols */
  371. {0x2670, 0x2767}, /* 248x Symbols,Dingbats */
  372. {0x2776, 0x27bf}, /* 74x Dingbats */
  373. {0x2800, 0x28ff}, /* 256x Braille */
  374. {0x2c00, 0x2c2e}, /* 47x Glagolitic */
  375. {0x2c30, 0x2c5e}, /* 47x Glagolitic */
  376. {0x2c60, 0x2ce4}, /* 133x Watin-D */
  377. {0x2d00, 0x2d25}, /* 38x Georgian2 */
  378. {0x2d30, 0x2d67}, /* 56x Tifinagh */
  379. {0x2d80, 0x2d96}, /* 23x Ethiopic2 */
  380. {0x2e2f, 0x2e2f}, /* 1x Punctuation2 */
  381. {0x3005, 0x3007}, /* 3x CJK Symbols & Punctuation */
  382. {0x3021, 0x3029}, /* 9x CJK Symbols & Punctuation */
  383. {0x3031, 0x3035}, /* 5x CJK Symbols & Punctuation */
  384. {0x3038, 0x303c}, /* 5x CJK Symbols & Punctuation */
  385. {0x3041, 0x3096}, /* 86x Hiragana */
  386. {0x30a1, 0x30fa}, /* 90x Katakana */
  387. {0x3105, 0x312f}, /* 43x Bopomofo */
  388. {0x3131, 0x318e}, /* 94x Hangul Compatibility Jamo */
  389. {0x31a0, 0x31ba}, /* 27x Bopomofo Extended */
  390. {0x31f0, 0x31ff}, /* 16x Katakana Phonetic Extensions */
  391. {0x3220, 0x3229}, /* 10x Enclosed CJK Letters & Months */
  392. {0x3248, 0x324f}, /* 8x Enclosed CJK Letters & Months */
  393. {0x3251, 0x325f}, /* 15x Enclosed CJK Letters & Months */
  394. {0x3280, 0x3289}, /* 10x Enclosed CJK Letters & Months */
  395. {0x32b1, 0x32bf}, /* 15x Enclosed CJK Letters & Months */
  396. {0x3400, 0x4db5}, /* 6582x CJK Unified Ideographs Extension A */
  397. {0x4dc0, 0x9fef}, /* 21040x Yijing Hexagram, CJK Unified Ideographs */
  398. {0xa000, 0xa48c}, /* 1165x Yi Syllables */
  399. {0xa4d0, 0xa4fd}, /* 46x Lisu */
  400. {0xa500, 0xa60c}, /* 269x Vai */
  401. {0xa610, 0xa62b}, /* 28x Vai */
  402. {0xa6a0, 0xa6ef}, /* 80x Bamum */
  403. {0xa80c, 0xa822}, /* 23x Syloti Nagri */
  404. {0xa840, 0xa873}, /* 52x Phags-pa */
  405. {0xa882, 0xa8b3}, /* 50x Saurashtra */
  406. {0xa8d0, 0xa8d9}, /* 10x Saurashtra */
  407. {0xa900, 0xa925}, /* 38x Kayah Li */
  408. {0xa930, 0xa946}, /* 23x Rejang */
  409. {0xa960, 0xa97c}, /* 29x Hangul Jamo Extended-A */
  410. {0xa984, 0xa9b2}, /* 47x Javanese */
  411. {0xa9cf, 0xa9d9}, /* 11x Javanese */
  412. {0xaa00, 0xaa28}, /* 41x Cham */
  413. {0xaa50, 0xaa59}, /* 10x Cham */
  414. {0xabf0, 0xabf9}, /* 10x Meetei Mayek */
  415. {0xac00, 0xd7a3}, /* 11172x Hangul Syllables */
  416. {0xf900, 0xfa6d}, /* 366x CJK Compatibility Ideographs */
  417. {0xfa70, 0xfad9}, /* 106x CJK Compatibility Ideographs */
  418. {0xfb1f, 0xfb28}, /* 10x Alphabetic Presentation Forms */
  419. {0xfb2a, 0xfb36}, /* 13x Alphabetic Presentation Forms */
  420. {0xfb46, 0xfbb1}, /* 108x Alphabetic Presentation Forms */
  421. {0xfbd3, 0xfd3d}, /* 363x Arabic Presentation Forms-A */
  422. {0xfe76, 0xfefc}, /* 135x Arabic Presentation Forms-B */
  423. {0xff10, 0xff19}, /* 10x Dubs */
  424. {0xff21, 0xff3a}, /* 26x Dubs */
  425. {0xff41, 0xff5a}, /* 26x Dubs */
  426. {0xff66, 0xffbe}, /* 89x Dubs */
  427. {0xffc2, 0xffc7}, /* 6x Dubs */
  428. {0xffca, 0xffcf}, /* 6x Dubs */
  429. {0xffd2, 0xffd7}, /* 6x Dubs */
  430. {0xffda, 0xffdc}, /* 3x Dubs */
  431. };
  432. l = 0;
  433. r = n = sizeof(kGlyphs) / sizeof(kGlyphs[0]);
  434. while (l < r) {
  435. m = (l + r) >> 1;
  436. if (kGlyphs[m][1] < c) {
  437. l = m + 1;
  438. } else {
  439. r = m;
  440. }
  441. }
  442. return !(l < n && kGlyphs[l][0] <= c && c <= kGlyphs[l][1]);
  443. } else {
  444. static const unsigned kAstralGlyphs[][2] = {
  445. {0x10107, 0x10133}, /* 45x Aegean */
  446. {0x10140, 0x10178}, /* 57x Ancient Greek Numbers */
  447. {0x1018a, 0x1018b}, /* 2x Ancient Greek Numbers */
  448. {0x10280, 0x1029c}, /* 29x Lycian */
  449. {0x102a0, 0x102d0}, /* 49x Carian */
  450. {0x102e1, 0x102fb}, /* 27x Coptic Epact Numbers */
  451. {0x10300, 0x10323}, /* 36x Old Italic */
  452. {0x1032d, 0x1034a}, /* 30x Old Italic, Gothic */
  453. {0x10350, 0x10375}, /* 38x Old Permic */
  454. {0x10380, 0x1039d}, /* 30x Ugaritic */
  455. {0x103a0, 0x103c3}, /* 36x Old Persian */
  456. {0x103c8, 0x103cf}, /* 8x Old Persian */
  457. {0x103d1, 0x103d5}, /* 5x Old Persian */
  458. {0x10400, 0x1049d}, /* 158x Deseret, Shavian, Osmanya */
  459. {0x104b0, 0x104d3}, /* 36x Osage */
  460. {0x104d8, 0x104fb}, /* 36x Osage */
  461. {0x10500, 0x10527}, /* 40x Elbasan */
  462. {0x10530, 0x10563}, /* 52x Caucasian Albanian */
  463. {0x10600, 0x10736}, /* 311x Linear A */
  464. {0x10800, 0x10805}, /* 6x Cypriot Syllabary */
  465. {0x1080a, 0x10835}, /* 44x Cypriot Syllabary */
  466. {0x10837, 0x10838}, /* 2x Cypriot Syllabary */
  467. {0x1083f, 0x1089e}, /* 86x Cypriot,ImperialAramaic,Palmyrene,Nabataean */
  468. {0x108e0, 0x108f2}, /* 19x Hatran */
  469. {0x108f4, 0x108f5}, /* 2x Hatran */
  470. {0x108fb, 0x1091b}, /* 33x Hatran */
  471. {0x10920, 0x10939}, /* 26x Lydian */
  472. {0x10980, 0x109b7}, /* 56x Meroitic Hieroglyphs */
  473. {0x109bc, 0x109cf}, /* 20x Meroitic Cursive */
  474. {0x109d2, 0x10a00}, /* 47x Meroitic Cursive */
  475. {0x10a10, 0x10a13}, /* 4x Kharoshthi */
  476. {0x10a15, 0x10a17}, /* 3x Kharoshthi */
  477. {0x10a19, 0x10a35}, /* 29x Kharoshthi */
  478. {0x10a40, 0x10a48}, /* 9x Kharoshthi */
  479. {0x10a60, 0x10a7e}, /* 31x Old South Arabian */
  480. {0x10a80, 0x10a9f}, /* 32x Old North Arabian */
  481. {0x10ac0, 0x10ac7}, /* 8x Manichaean */
  482. {0x10ac9, 0x10ae4}, /* 28x Manichaean */
  483. {0x10aeb, 0x10aef}, /* 5x Manichaean */
  484. {0x10b00, 0x10b35}, /* 54x Avestan */
  485. {0x10b40, 0x10b55}, /* 22x Inscriptional Parthian */
  486. {0x10b58, 0x10b72}, /* 27x Inscriptional Parthian and Pahlavi */
  487. {0x10b78, 0x10b91}, /* 26x Inscriptional Pahlavi, Psalter Pahlavi */
  488. {0x10c00, 0x10c48}, /* 73x Old Turkic */
  489. {0x10c80, 0x10cb2}, /* 51x Old Hungarian */
  490. {0x10cc0, 0x10cf2}, /* 51x Old Hungarian */
  491. {0x10cfa, 0x10d23}, /* 42x Old Hungarian, Hanifi Rohingya */
  492. {0x10d30, 0x10d39}, /* 10x Hanifi Rohingya */
  493. {0x10e60, 0x10e7e}, /* 31x Rumi Numeral Symbols */
  494. {0x10f00, 0x10f27}, /* 40x Old Sogdian */
  495. {0x10f30, 0x10f45}, /* 22x Sogdian */
  496. {0x10f51, 0x10f54}, /* 4x Sogdian */
  497. {0x10fe0, 0x10ff6}, /* 23x Elymaic */
  498. {0x11003, 0x11037}, /* 53x Brahmi */
  499. {0x11052, 0x1106f}, /* 30x Brahmi */
  500. {0x11083, 0x110af}, /* 45x Kaithi */
  501. {0x110d0, 0x110e8}, /* 25x Sora Sompeng */
  502. {0x110f0, 0x110f9}, /* 10x Sora Sompeng */
  503. {0x11103, 0x11126}, /* 36x Chakma */
  504. {0x11136, 0x1113f}, /* 10x Chakma */
  505. {0x11144, 0x11144}, /* 1x Chakma */
  506. {0x11150, 0x11172}, /* 35x Mahajani */
  507. {0x11176, 0x11176}, /* 1x Mahajani */
  508. {0x11183, 0x111b2}, /* 48x Sharada */
  509. {0x111c1, 0x111c4}, /* 4x Sharada */
  510. {0x111d0, 0x111da}, /* 11x Sharada */
  511. {0x111dc, 0x111dc}, /* 1x Sharada */
  512. {0x111e1, 0x111f4}, /* 20x Sinhala Archaic Numbers */
  513. {0x11200, 0x11211}, /* 18x Khojki */
  514. {0x11213, 0x1122b}, /* 25x Khojki */
  515. {0x11280, 0x11286}, /* 7x Multani */
  516. {0x11288, 0x11288}, /* 1x Multani */
  517. {0x1128a, 0x1128d}, /* 4x Multani */
  518. {0x1128f, 0x1129d}, /* 15x Multani */
  519. {0x1129f, 0x112a8}, /* 10x Multani */
  520. {0x112b0, 0x112de}, /* 47x Khudawadi */
  521. {0x112f0, 0x112f9}, /* 10x Khudawadi */
  522. {0x11305, 0x1130c}, /* 8x Grantha */
  523. {0x1130f, 0x11310}, /* 2x Grantha */
  524. {0x11313, 0x11328}, /* 22x Grantha */
  525. {0x1132a, 0x11330}, /* 7x Grantha */
  526. {0x11332, 0x11333}, /* 2x Grantha */
  527. {0x11335, 0x11339}, /* 5x Grantha */
  528. {0x1133d, 0x1133d}, /* 1x Grantha */
  529. {0x11350, 0x11350}, /* 1x Grantha */
  530. {0x1135d, 0x11361}, /* 5x Grantha */
  531. {0x11400, 0x11434}, /* 53x Newa */
  532. {0x11447, 0x1144a}, /* 4x Newa */
  533. {0x11450, 0x11459}, /* 10x Newa */
  534. {0x1145f, 0x1145f}, /* 1x Newa */
  535. {0x11480, 0x114af}, /* 48x Tirhuta */
  536. {0x114c4, 0x114c5}, /* 2x Tirhuta */
  537. {0x114c7, 0x114c7}, /* 1x Tirhuta */
  538. {0x114d0, 0x114d9}, /* 10x Tirhuta */
  539. {0x11580, 0x115ae}, /* 47x Siddham */
  540. {0x115d8, 0x115db}, /* 4x Siddham */
  541. {0x11600, 0x1162f}, /* 48x Modi */
  542. {0x11644, 0x11644}, /* 1x Modi */
  543. {0x11650, 0x11659}, /* 10x Modi */
  544. {0x11680, 0x116aa}, /* 43x Takri */
  545. {0x116b8, 0x116b8}, /* 1x Takri */
  546. {0x116c0, 0x116c9}, /* 10x Takri */
  547. {0x11700, 0x1171a}, /* 27x Ahom */
  548. {0x11730, 0x1173b}, /* 12x Ahom */
  549. {0x11800, 0x1182b}, /* 44x Dogra */
  550. {0x118a0, 0x118f2}, /* 83x Warang Citi */
  551. {0x118ff, 0x118ff}, /* 1x Warang Citi */
  552. {0x119a0, 0x119a7}, /* 8x Nandinagari */
  553. {0x119aa, 0x119d0}, /* 39x Nandinagari */
  554. {0x119e1, 0x119e1}, /* 1x Nandinagari */
  555. {0x119e3, 0x119e3}, /* 1x Nandinagari */
  556. {0x11a00, 0x11a00}, /* 1x Zanabazar Square */
  557. {0x11a0b, 0x11a32}, /* 40x Zanabazar Square */
  558. {0x11a3a, 0x11a3a}, /* 1x Zanabazar Square */
  559. {0x11a50, 0x11a50}, /* 1x Soyombo */
  560. {0x11a5c, 0x11a89}, /* 46x Soyombo */
  561. {0x11a9d, 0x11a9d}, /* 1x Soyombo */
  562. {0x11ac0, 0x11af8}, /* 57x Pau Cin Hau */
  563. {0x11c00, 0x11c08}, /* 9x Bhaiksuki */
  564. {0x11c0a, 0x11c2e}, /* 37x Bhaiksuki */
  565. {0x11c40, 0x11c40}, /* 1x Bhaiksuki */
  566. {0x11c50, 0x11c6c}, /* 29x Bhaiksuki */
  567. {0x11c72, 0x11c8f}, /* 30x Marchen */
  568. {0x11d00, 0x11d06}, /* 7x Masaram Gondi */
  569. {0x11d08, 0x11d09}, /* 2x Masaram Gondi */
  570. {0x11d0b, 0x11d30}, /* 38x Masaram Gondi */
  571. {0x11d46, 0x11d46}, /* 1x Masaram Gondi */
  572. {0x11d50, 0x11d59}, /* 10x Masaram Gondi */
  573. {0x11d60, 0x11d65}, /* 6x Gunjala Gondi */
  574. {0x11d67, 0x11d68}, /* 2x Gunjala Gondi */
  575. {0x11d6a, 0x11d89}, /* 32x Gunjala Gondi */
  576. {0x11d98, 0x11d98}, /* 1x Gunjala Gondi */
  577. {0x11da0, 0x11da9}, /* 10x Gunjala Gondi */
  578. {0x11ee0, 0x11ef2}, /* 19x Makasar */
  579. {0x11fc0, 0x11fd4}, /* 21x Tamil Supplement */
  580. {0x12000, 0x12399}, /* 922x Cuneiform */
  581. {0x12400, 0x1246e}, /* 111x Cuneiform Numbers & Punctuation */
  582. {0x12480, 0x12543}, /* 196x Early Dynastic Cuneiform */
  583. {0x13000, 0x1342e}, /* 1071x Egyptian Hieroglyphs */
  584. {0x14400, 0x14646}, /* 583x Anatolian Hieroglyphs */
  585. {0x16800, 0x16a38}, /* 569x Bamum Supplement */
  586. {0x16a40, 0x16a5e}, /* 31x Mro */
  587. {0x16a60, 0x16a69}, /* 10x Mro */
  588. {0x16ad0, 0x16aed}, /* 30x Bassa Vah */
  589. {0x16b00, 0x16b2f}, /* 48x Pahawh Hmong */
  590. {0x16b40, 0x16b43}, /* 4x Pahawh Hmong */
  591. {0x16b50, 0x16b59}, /* 10x Pahawh Hmong */
  592. {0x16b5b, 0x16b61}, /* 7x Pahawh Hmong */
  593. {0x16b63, 0x16b77}, /* 21x Pahawh Hmong */
  594. {0x16b7d, 0x16b8f}, /* 19x Pahawh Hmong */
  595. {0x16e40, 0x16e96}, /* 87x Medefaidrin */
  596. {0x16f00, 0x16f4a}, /* 75x Miao */
  597. {0x16f50, 0x16f50}, /* 1x Miao */
  598. {0x16f93, 0x16f9f}, /* 13x Miao */
  599. {0x16fe0, 0x16fe1}, /* 2x Ideographic Symbols & Punctuation */
  600. {0x16fe3, 0x16fe3}, /* 1x Ideographic Symbols & Punctuation */
  601. {0x17000, 0x187f7}, /* 6136x Tangut */
  602. {0x18800, 0x18af2}, /* 755x Tangut Components */
  603. {0x1b000, 0x1b11e}, /* 287x Kana Supplement */
  604. {0x1b150, 0x1b152}, /* 3x Small Kana Extension */
  605. {0x1b164, 0x1b167}, /* 4x Small Kana Extension */
  606. {0x1b170, 0x1b2fb}, /* 396x Nushu */
  607. {0x1bc00, 0x1bc6a}, /* 107x Duployan */
  608. {0x1bc70, 0x1bc7c}, /* 13x Duployan */
  609. {0x1bc80, 0x1bc88}, /* 9x Duployan */
  610. {0x1bc90, 0x1bc99}, /* 10x Duployan */
  611. {0x1d2e0, 0x1d2f3}, /* 20x Mayan Numerals */
  612. {0x1d360, 0x1d378}, /* 25x Counting Rod Numerals */
  613. {0x1d400, 0x1d454}, /* 85x 𝐀..𝑔 Math */
  614. {0x1d456, 0x1d49c}, /* 71x 𝑖..𝒜 Math */
  615. {0x1d49e, 0x1d49f}, /* 2x 𝒞..𝒟 Math */
  616. {0x1d4a2, 0x1d4a2}, /* 1x 𝒢..𝒢 Math */
  617. {0x1d4a5, 0x1d4a6}, /* 2x 𝒥..𝒦 Math */
  618. {0x1d4a9, 0x1d4ac}, /* 4x 𝒩..𝒬 Math */
  619. {0x1d4ae, 0x1d4b9}, /* 12x 𝒮..𝒹 Math */
  620. {0x1d4bb, 0x1d4bb}, /* 1x 𝒻..𝒻 Math */
  621. {0x1d4bd, 0x1d4c3}, /* 7x 𝒽..𝓃 Math */
  622. {0x1d4c5, 0x1d505}, /* 65x 𝓅..𝔅 Math */
  623. {0x1d507, 0x1d50a}, /* 4x 𝔇..𝔊 Math */
  624. {0x1d50d, 0x1d514}, /* 8x 𝔍..𝔔 Math */
  625. {0x1d516, 0x1d51c}, /* 7x 𝔖..𝔜 Math */
  626. {0x1d51e, 0x1d539}, /* 28x 𝔞..𝔹 Math */
  627. {0x1d53b, 0x1d53e}, /* 4x 𝔻..𝔾 Math */
  628. {0x1d540, 0x1d544}, /* 5x 𝕀..𝕄 Math */
  629. {0x1d546, 0x1d546}, /* 1x 𝕆..𝕆 Math */
  630. {0x1d54a, 0x1d550}, /* 7x 𝕊..𝕐 Math */
  631. {0x1d552, 0x1d6a5}, /* 340x 𝕒..𝚥 Math */
  632. {0x1d6a8, 0x1d6c0}, /* 25x 𝚨..𝛀 Math */
  633. {0x1d6c2, 0x1d6da}, /* 25x 𝛂..𝛚 Math */
  634. {0x1d6dc, 0x1d6fa}, /* 31x 𝛜..𝛺 Math */
  635. {0x1d6fc, 0x1d714}, /* 25x 𝛼..𝜔 Math */
  636. {0x1d716, 0x1d734}, /* 31x 𝜖..𝜴 Math */
  637. {0x1d736, 0x1d74e}, /* 25x 𝜶..𝝎 Math */
  638. {0x1d750, 0x1d76e}, /* 31x 𝝐..𝝮 Math */
  639. {0x1d770, 0x1d788}, /* 25x 𝝰..𝞈 Math */
  640. {0x1d78a, 0x1d7a8}, /* 31x 𝞊..𝞨 Math */
  641. {0x1d7aa, 0x1d7c2}, /* 25x 𝞪..𝟂 Math */
  642. {0x1d7c4, 0x1d7cb}, /* 8x 𝟄..𝟋 Math */
  643. {0x1d7ce, 0x1d9ff}, /* 562x Math, Sutton SignWriting */
  644. {0x1f100, 0x1f10c}, /* 13x Enclosed Alphanumeric Supplement */
  645. {0x20000, 0x2a6d6}, /* 42711x CJK Unified Ideographs Extension B */
  646. {0x2a700, 0x2b734}, /* 4149x CJK Unified Ideographs Extension C */
  647. {0x2b740, 0x2b81d}, /* 222x CJK Unified Ideographs Extension D */
  648. {0x2b820, 0x2cea1}, /* 5762x CJK Unified Ideographs Extension E */
  649. {0x2ceb0, 0x2ebe0}, /* 7473x CJK Unified Ideographs Extension F */
  650. {0x2f800, 0x2fa1d}, /* 542x CJK Compatibility Ideographs Supplement */
  651. };
  652. l = 0;
  653. r = n = sizeof(kAstralGlyphs) / sizeof(kAstralGlyphs[0]);
  654. while (l < r) {
  655. m = (l + r) >> 1;
  656. if (kAstralGlyphs[m][1] < c) {
  657. l = m + 1;
  658. } else {
  659. r = m;
  660. }
  661. }
  662. return !(l < n && kAstralGlyphs[l][0] <= c && c <= kAstralGlyphs[l][1]);
  663. }
  664. }
  665. unsigned bestlineLowercase(unsigned c) {
  666. int m, l, r, n;
  667. if (c < 0200) {
  668. if ('A' <= c && c <= 'Z') {
  669. return c + 32;
  670. } else {
  671. return c;
  672. }
  673. } else if (c <= 0xffff) {
  674. if ((0x0100 <= c && c <= 0x0176) || /* 60x Ā..ā → ā..ŵ Watin-A */
  675. (0x01de <= c && c <= 0x01ee) || /* 9x Ǟ..Ǯ → ǟ..ǯ Watin-B */
  676. (0x01f8 <= c && c <= 0x021e) || /* 20x Ǹ..Ȟ → ǹ..ȟ Watin-B */
  677. (0x0222 <= c && c <= 0x0232) || /* 9x Ȣ..Ȳ → ȣ..ȳ Watin-B */
  678. (0x1e00 <= c && c <= 0x1eff)) { /*256x Ḁ..Ỿ → ḁ..ỿ Watin-C */
  679. if (c == 0x0130)
  680. return c - 199;
  681. if (c == 0x1e9e)
  682. return c;
  683. return c + (~c & 1);
  684. } else if (0x01cf <= c && c <= 0x01db) {
  685. return c + (c & 1); /* 7x Ǐ..Ǜ → ǐ..ǜ Watin-B */
  686. } else if (0x13a0 <= c && c <= 0x13ef) {
  687. return c + 38864; /* 80x Ꭰ ..Ꮿ → ꭰ ..ꮿ Cherokee */
  688. } else {
  689. static const struct {
  690. unsigned short a;
  691. unsigned short b;
  692. short d;
  693. } kLower[] = {
  694. {0x00c0, 0x00d6, +32}, /* 23x À ..Ö → à ..ö Watin */
  695. {0x00d8, 0x00de, +32}, /* 7x Ø ..Þ → ø ..þ Watin */
  696. {0x0178, 0x0178, -121}, /* 1x Ÿ ..Ÿ → ÿ ..ÿ Watin-A */
  697. {0x0179, 0x0179, +1}, /* 1x Ź ..Ź → ź ..ź Watin-A */
  698. {0x017b, 0x017b, +1}, /* 1x Ż ..Ż → ż ..ż Watin-A */
  699. {0x017d, 0x017d, +1}, /* 1x Ž ..Ž → ž ..ž Watin-A */
  700. {0x0181, 0x0181, +210}, /* 1x Ɓ ..Ɓ → ɓ ..ɓ Watin-B */
  701. {0x0182, 0x0182, +1}, /* 1x Ƃ ..Ƃ → ƃ ..ƃ Watin-B */
  702. {0x0184, 0x0184, +1}, /* 1x Ƅ ..Ƅ → ƅ ..ƅ Watin-B */
  703. {0x0186, 0x0186, +206}, /* 1x Ɔ ..Ɔ → ɔ ..ɔ Watin-B */
  704. {0x0187, 0x0187, +1}, /* 1x Ƈ ..Ƈ → ƈ ..ƈ Watin-B */
  705. {0x0189, 0x018a, +205}, /* 2x Ɖ ..Ɗ → ɖ ..ɗ Watin-B */
  706. {0x018b, 0x018b, +1}, /* 1x Ƌ ..Ƌ → ƌ ..ƌ Watin-B */
  707. {0x018e, 0x018e, +79}, /* 1x Ǝ ..Ǝ → ǝ ..ǝ Watin-B */
  708. {0x018f, 0x018f, +202}, /* 1x Ə ..Ə → ə ..ə Watin-B */
  709. {0x0190, 0x0190, +203}, /* 1x Ɛ ..Ɛ → ɛ ..ɛ Watin-B */
  710. {0x0191, 0x0191, +1}, /* 1x Ƒ ..Ƒ → ƒ ..ƒ Watin-B */
  711. {0x0193, 0x0193, +205}, /* 1x Ɠ ..Ɠ → ɠ ..ɠ Watin-B */
  712. {0x0194, 0x0194, +207}, /* 1x Ɣ ..Ɣ → ɣ ..ɣ Watin-B */
  713. {0x0196, 0x0196, +211}, /* 1x Ɩ ..Ɩ → ɩ ..ɩ Watin-B */
  714. {0x0197, 0x0197, +209}, /* 1x Ɨ ..Ɨ → ɨ ..ɨ Watin-B */
  715. {0x0198, 0x0198, +1}, /* 1x Ƙ ..Ƙ → ƙ ..ƙ Watin-B */
  716. {0x019c, 0x019c, +211}, /* 1x Ɯ ..Ɯ → ɯ ..ɯ Watin-B */
  717. {0x019d, 0x019d, +213}, /* 1x Ɲ ..Ɲ → ɲ ..ɲ Watin-B */
  718. {0x019f, 0x019f, +214}, /* 1x Ɵ ..Ɵ → ɵ ..ɵ Watin-B */
  719. {0x01a0, 0x01a0, +1}, /* 1x Ơ ..Ơ → ơ ..ơ Watin-B */
  720. {0x01a2, 0x01a2, +1}, /* 1x Ƣ ..Ƣ → ƣ ..ƣ Watin-B */
  721. {0x01a4, 0x01a4, +1}, /* 1x Ƥ ..Ƥ → ƥ ..ƥ Watin-B */
  722. {0x01a6, 0x01a6, +218}, /* 1x Ʀ ..Ʀ → ʀ ..ʀ Watin-B */
  723. {0x01a7, 0x01a7, +1}, /* 1x Ƨ ..Ƨ → ƨ ..ƨ Watin-B */
  724. {0x01a9, 0x01a9, +218}, /* 1x Ʃ ..Ʃ → ʃ ..ʃ Watin-B */
  725. {0x01ac, 0x01ac, +1}, /* 1x Ƭ ..Ƭ → ƭ ..ƭ Watin-B */
  726. {0x01ae, 0x01ae, +218}, /* 1x Ʈ ..Ʈ → ʈ ..ʈ Watin-B */
  727. {0x01af, 0x01af, +1}, /* 1x Ư ..Ư → ư ..ư Watin-B */
  728. {0x01b1, 0x01b2, +217}, /* 2x Ʊ ..Ʋ → ʊ ..ʋ Watin-B */
  729. {0x01b3, 0x01b3, +1}, /* 1x Ƴ ..Ƴ → ƴ ..ƴ Watin-B */
  730. {0x01b5, 0x01b5, +1}, /* 1x Ƶ ..Ƶ → ƶ ..ƶ Watin-B */
  731. {0x01b7, 0x01b7, +219}, /* 1x Ʒ ..Ʒ → ʒ ..ʒ Watin-B */
  732. {0x01b8, 0x01b8, +1}, /* 1x Ƹ ..Ƹ → ƹ ..ƹ Watin-B */
  733. {0x01bc, 0x01bc, +1}, /* 1x Ƽ ..Ƽ → ƽ ..ƽ Watin-B */
  734. {0x01c4, 0x01c4, +2}, /* 1x DŽ ..DŽ → dž ..dž Watin-B */
  735. {0x01c5, 0x01c5, +1}, /* 1x Dž ..Dž → dž ..dž Watin-B */
  736. {0x01c7, 0x01c7, +2}, /* 1x LJ ..LJ → lj ..lj Watin-B */
  737. {0x01c8, 0x01c8, +1}, /* 1x Lj ..Lj → lj ..lj Watin-B */
  738. {0x01ca, 0x01ca, +2}, /* 1x NJ ..NJ → nj ..nj Watin-B */
  739. {0x01cb, 0x01cb, +1}, /* 1x Nj ..Nj → nj ..nj Watin-B */
  740. {0x01cd, 0x01cd, +1}, /* 1x Ǎ ..Ǎ → ǎ ..ǎ Watin-B */
  741. {0x01f1, 0x01f1, +2}, /* 1x DZ ..DZ → dz ..dz Watin-B */
  742. {0x01f2, 0x01f2, +1}, /* 1x Dz ..Dz → dz ..dz Watin-B */
  743. {0x01f4, 0x01f4, +1}, /* 1x Ǵ ..Ǵ → ǵ ..ǵ Watin-B */
  744. {0x01f6, 0x01f6, -97}, /* 1x Ƕ ..Ƕ → ƕ ..ƕ Watin-B */
  745. {0x01f7, 0x01f7, -56}, /* 1x Ƿ ..Ƿ → ƿ ..ƿ Watin-B */
  746. {0x0220, 0x0220, -130}, /* 1x Ƞ ..Ƞ → ƞ ..ƞ Watin-B */
  747. {0x023b, 0x023b, +1}, /* 1x Ȼ ..Ȼ → ȼ ..ȼ Watin-B */
  748. {0x023d, 0x023d, -163}, /* 1x Ƚ ..Ƚ → ƚ ..ƚ Watin-B */
  749. {0x0241, 0x0241, +1}, /* 1x Ɂ ..Ɂ → ɂ ..ɂ Watin-B */
  750. {0x0243, 0x0243, -195}, /* 1x Ƀ ..Ƀ → ƀ ..ƀ Watin-B */
  751. {0x0244, 0x0244, +69}, /* 1x Ʉ ..Ʉ → ʉ ..ʉ Watin-B */
  752. {0x0245, 0x0245, +71}, /* 1x Ʌ ..Ʌ → ʌ ..ʌ Watin-B */
  753. {0x0246, 0x0246, +1}, /* 1x Ɇ ..Ɇ → ɇ ..ɇ Watin-B */
  754. {0x0248, 0x0248, +1}, /* 1x Ɉ ..Ɉ → ɉ ..ɉ Watin-B */
  755. {0x024a, 0x024a, +1}, /* 1x Ɋ ..Ɋ → ɋ ..ɋ Watin-B */
  756. {0x024c, 0x024c, +1}, /* 1x Ɍ ..Ɍ → ɍ ..ɍ Watin-B */
  757. {0x024e, 0x024e, +1}, /* 1x Ɏ ..Ɏ → ɏ ..ɏ Watin-B */
  758. {0x0386, 0x0386, +38}, /* 1x Ά ..Ά → ά ..ά Greek */
  759. {0x0388, 0x038a, +37}, /* 3x Έ ..Ί → έ ..ί Greek */
  760. {0x038c, 0x038c, +64}, /* 1x Ό ..Ό → ό ..ό Greek */
  761. {0x038e, 0x038f, +63}, /* 2x Ύ ..Ώ → ύ ..ώ Greek */
  762. {0x0391, 0x03a1, +32}, /* 17x Α ..Ρ → α ..ρ Greek */
  763. {0x03a3, 0x03ab, +32}, /* 9x Σ ..Ϋ → σ ..ϋ Greek */
  764. {0x03dc, 0x03dc, +1}, /* 1x Ϝ ..Ϝ → ϝ ..ϝ Greek */
  765. {0x03f4, 0x03f4, -60}, /* 1x ϴ ..ϴ → θ ..θ Greek */
  766. {0x0400, 0x040f, +80}, /* 16x Ѐ ..Џ → ѐ ..џ Cyrillic */
  767. {0x0410, 0x042f, +32}, /* 32x А ..Я → а ..я Cyrillic */
  768. {0x0460, 0x0460, +1}, /* 1x Ѡ ..Ѡ → ѡ ..ѡ Cyrillic */
  769. {0x0462, 0x0462, +1}, /* 1x Ѣ ..Ѣ → ѣ ..ѣ Cyrillic */
  770. {0x0464, 0x0464, +1}, /* 1x Ѥ ..Ѥ → ѥ ..ѥ Cyrillic */
  771. {0x0472, 0x0472, +1}, /* 1x Ѳ ..Ѳ → ѳ ..ѳ Cyrillic */
  772. {0x0490, 0x0490, +1}, /* 1x Ґ ..Ґ → ґ ..ґ Cyrillic */
  773. {0x0498, 0x0498, +1}, /* 1x Ҙ ..Ҙ → ҙ ..ҙ Cyrillic */
  774. {0x049a, 0x049a, +1}, /* 1x Қ ..Қ → қ ..қ Cyrillic */
  775. {0x0531, 0x0556, +48}, /* 38x Ա ..Ֆ → ա ..ֆ Armenian */
  776. {0x10a0, 0x10c5, +7264}, /* 38x Ⴀ ..Ⴥ → ⴀ ..ⴥ Georgian */
  777. {0x10c7, 0x10c7, +7264}, /* 1x Ⴧ ..Ⴧ → ⴧ ..ⴧ Georgian */
  778. {0x10cd, 0x10cd, +7264}, /* 1x Ⴭ ..Ⴭ → ⴭ ..ⴭ Georgian */
  779. {0x13f0, 0x13f5, +8}, /* 6x Ᏸ ..Ᏽ → ᏸ ..ᏽ Cherokee */
  780. {0x1c90, 0x1cba, -3008}, /* 43x Ა ..Ჺ → ა ..ჺ Georgian2 */
  781. {0x1cbd, 0x1cbf, -3008}, /* 3x Ჽ ..Ჿ → ჽ ..ჿ Georgian2 */
  782. {0x1f08, 0x1f0f, -8}, /* 8x Ἀ ..Ἇ → ἀ ..ἇ Greek2 */
  783. {0x1f18, 0x1f1d, -8}, /* 6x Ἐ ..Ἕ → ἐ ..ἕ Greek2 */
  784. {0x1f28, 0x1f2f, -8}, /* 8x Ἠ ..Ἧ → ἠ ..ἧ Greek2 */
  785. {0x1f38, 0x1f3f, -8}, /* 8x Ἰ ..Ἷ → ἰ ..ἷ Greek2 */
  786. {0x1f48, 0x1f4d, -8}, /* 6x Ὀ ..Ὅ → ὀ ..ὅ Greek2 */
  787. {0x1f59, 0x1f59, -8}, /* 1x Ὑ ..Ὑ → ὑ ..ὑ Greek2 */
  788. {0x1f5b, 0x1f5b, -8}, /* 1x Ὓ ..Ὓ → ὓ ..ὓ Greek2 */
  789. {0x1f5d, 0x1f5d, -8}, /* 1x Ὕ ..Ὕ → ὕ ..ὕ Greek2 */
  790. {0x1f5f, 0x1f5f, -8}, /* 1x Ὗ ..Ὗ → ὗ ..ὗ Greek2 */
  791. {0x1f68, 0x1f6f, -8}, /* 8x Ὠ ..Ὧ → ὠ ..ὧ Greek2 */
  792. {0x1f88, 0x1f8f, -8}, /* 8x ᾈ ..ᾏ → ᾀ ..ᾇ Greek2 */
  793. {0x1f98, 0x1f9f, -8}, /* 8x ᾘ ..ᾟ → ᾐ ..ᾗ Greek2 */
  794. {0x1fa8, 0x1faf, -8}, /* 8x ᾨ ..ᾯ → ᾠ ..ᾧ Greek2 */
  795. {0x1fb8, 0x1fb9, -8}, /* 2x Ᾰ ..Ᾱ → ᾰ ..ᾱ Greek2 */
  796. {0x1fba, 0x1fbb, -74}, /* 2x Ὰ ..Ά → ὰ ..ά Greek2 */
  797. {0x1fbc, 0x1fbc, -9}, /* 1x ᾼ ..ᾼ → ᾳ ..ᾳ Greek2 */
  798. {0x1fc8, 0x1fcb, -86}, /* 4x Ὲ ..Ή → ὲ ..ή Greek2 */
  799. {0x1fcc, 0x1fcc, -9}, /* 1x ῌ ..ῌ → ῃ ..ῃ Greek2 */
  800. {0x1fd8, 0x1fd9, -8}, /* 2x Ῐ ..Ῑ → ῐ ..ῑ Greek2 */
  801. {0x1fda, 0x1fdb, -100}, /* 2x Ὶ ..Ί → ὶ ..ί Greek2 */
  802. {0x1fe8, 0x1fe9, -8}, /* 2x Ῠ ..Ῡ → ῠ ..ῡ Greek2 */
  803. {0x1fea, 0x1feb, -112}, /* 2x Ὺ ..Ύ → ὺ ..ύ Greek2 */
  804. {0x1fec, 0x1fec, -7}, /* 1x Ῥ ..Ῥ → ῥ ..ῥ Greek2 */
  805. {0x1ff8, 0x1ff9, -128}, /* 2x Ὸ ..Ό → ὸ ..ό Greek2 */
  806. {0x1ffa, 0x1ffb, -126}, /* 2x Ὼ ..Ώ → ὼ ..ώ Greek2 */
  807. {0x1ffc, 0x1ffc, -9}, /* 1x ῼ ..ῼ → ῳ ..ῳ Greek2 */
  808. {0x2126, 0x2126, -7517}, /* 1x Ω ..Ω → ω ..ω Letterlike */
  809. {0x212a, 0x212a, -8383}, /* 1x K ..K → k ..k Letterlike */
  810. {0x212b, 0x212b, -8262}, /* 1x Å ..Å → å ..å Letterlike */
  811. {0x2132, 0x2132, +28}, /* 1x Ⅎ ..Ⅎ → ⅎ ..ⅎ Letterlike */
  812. {0x2160, 0x216f, +16}, /* 16x Ⅰ ..Ⅿ → ⅰ ..ⅿ Numbery */
  813. {0x2183, 0x2183, +1}, /* 1x Ↄ ..Ↄ → ↄ ..ↄ Numbery */
  814. {0x24b6, 0x24cf, +26}, /* 26x Ⓐ ..Ⓩ → ⓐ ..ⓩ Enclosed */
  815. {0x2c00, 0x2c2e, +48}, /* 47x Ⰰ ..Ⱞ → ⰰ ..ⱞ Glagolitic */
  816. {0xff21, 0xff3a, +32}, /* 26x A..Z → a..z Dubs */
  817. };
  818. l = 0;
  819. r = n = sizeof(kLower) / sizeof(kLower[0]);
  820. while (l < r) {
  821. m = (l + r) >> 1;
  822. if (kLower[m].b < c) {
  823. l = m + 1;
  824. } else {
  825. r = m;
  826. }
  827. }
  828. if (l < n && kLower[l].a <= c && c <= kLower[l].b) {
  829. return c + kLower[l].d;
  830. } else {
  831. return c;
  832. }
  833. }
  834. } else {
  835. static struct {
  836. unsigned a;
  837. unsigned b;
  838. short d;
  839. } kAstralLower[] = {
  840. {0x10400, 0x10427, +40}, /* 40x 𐐀 ..𐐧 → 𐐨 ..𐑏 Deseret */
  841. {0x104b0, 0x104d3, +40}, /* 36x 𐒰 ..𐓓 → 𐓘 ..𐓻 Osage */
  842. {0x1d400, 0x1d419, +26}, /* 26x 𝐀 ..𝐙 → 𝐚 ..𝐳 Math */
  843. {0x1d43c, 0x1d44d, +26}, /* 18x 𝐼 ..𝑍 → 𝑖 ..𝑧 Math */
  844. {0x1d468, 0x1d481, +26}, /* 26x 𝑨 ..𝒁 → 𝒂 ..𝒛 Math */
  845. {0x1d4ae, 0x1d4b5, +26}, /* 8x 𝒮 ..𝒵 → 𝓈 ..𝓏 Math */
  846. {0x1d4d0, 0x1d4e9, +26}, /* 26x 𝓐 ..𝓩 → 𝓪 ..𝔃 Math */
  847. {0x1d50d, 0x1d514, +26}, /* 8x 𝔍 ..𝔔 → 𝔧 ..𝔮 Math */
  848. {0x1d56c, 0x1d585, +26}, /* 26x 𝕬 ..𝖅 → 𝖆 ..𝖟 Math */
  849. {0x1d5a0, 0x1d5b9, +26}, /* 26x 𝖠 ..𝖹 → 𝖺 ..𝗓 Math */
  850. {0x1d5d4, 0x1d5ed, +26}, /* 26x 𝗔 ..𝗭 → 𝗮 ..𝘇 Math */
  851. {0x1d608, 0x1d621, +26}, /* 26x 𝘈 ..𝘡 → 𝘢 ..𝘻 Math */
  852. {0x1d63c, 0x1d655, -442}, /* 26x 𝘼 ..𝙕 → 𝒂 ..𝒛 Math */
  853. {0x1d670, 0x1d689, +26}, /* 26x 𝙰 ..𝚉 → 𝚊 ..𝚣 Math */
  854. {0x1d6a8, 0x1d6b8, +26}, /* 17x 𝚨 ..𝚸 → 𝛂 ..𝛒 Math */
  855. {0x1d6e2, 0x1d6f2, +26}, /* 17x 𝛢 ..𝛲 → 𝛼 ..𝜌 Math */
  856. {0x1d71c, 0x1d72c, +26}, /* 17x 𝜜 ..𝜬 → 𝜶 ..𝝆 Math */
  857. {0x1d756, 0x1d766, +26}, /* 17x 𝝖 ..𝝦 → 𝝰 ..𝞀 Math */
  858. {0x1d790, 0x1d7a0, -90}, /* 17x 𝞐 ..𝞠 → 𝜶 ..𝝆 Math */
  859. };
  860. l = 0;
  861. r = n = sizeof(kAstralLower) / sizeof(kAstralLower[0]);
  862. while (l < r) {
  863. m = (l + r) >> 1;
  864. if (kAstralLower[m].b < c) {
  865. l = m + 1;
  866. } else {
  867. r = m;
  868. }
  869. }
  870. if (l < n && kAstralLower[l].a <= c && c <= kAstralLower[l].b) {
  871. return c + kAstralLower[l].d;
  872. } else {
  873. return c;
  874. }
  875. }
  876. }
  877. unsigned bestlineUppercase(unsigned c) {
  878. int m, l, r, n;
  879. if (c < 0200) {
  880. if ('a' <= c && c <= 'z') {
  881. return c - 32;
  882. } else {
  883. return c;
  884. }
  885. } else if (c <= 0xffff) {
  886. if ((0x0101 <= c && c <= 0x0177) || /* 60x ā..ŵ → Ā..ā Watin-A */
  887. (0x01df <= c && c <= 0x01ef) || /* 9x ǟ..ǯ → Ǟ..Ǯ Watin-B */
  888. (0x01f8 <= c && c <= 0x021e) || /* 20x ǹ..ȟ → Ǹ..Ȟ Watin-B */
  889. (0x0222 <= c && c <= 0x0232) || /* 9x ȣ..ȳ → Ȣ..Ȳ Watin-B */
  890. (0x1e01 <= c && c <= 0x1eff)) { /*256x ḁ..ỿ → Ḁ..Ỿ Watin-C */
  891. if (c == 0x0131)
  892. return c + 232;
  893. if (c == 0x1e9e)
  894. return c;
  895. return c - (c & 1);
  896. } else if (0x01d0 <= c && c <= 0x01dc) {
  897. return c - (~c & 1); /* 7x ǐ..ǜ → Ǐ..Ǜ Watin-B */
  898. } else if (0xab70 <= c && c <= 0xabbf) {
  899. return c - 38864; /* 80x ꭰ ..ꮿ → Ꭰ ..Ꮿ Cherokee Supplement */
  900. } else {
  901. static const struct {
  902. unsigned short a;
  903. unsigned short b;
  904. short d;
  905. } kUpper[] = {
  906. {0x00b5, 0x00b5, +743}, /* 1x µ ..µ → Μ ..Μ Watin */
  907. {0x00e0, 0x00f6, -32}, /* 23x à ..ö → À ..Ö Watin */
  908. {0x00f8, 0x00fe, -32}, /* 7x ø ..þ → Ø ..Þ Watin */
  909. {0x00ff, 0x00ff, +121}, /* 1x ÿ ..ÿ → Ÿ ..Ÿ Watin */
  910. {0x017a, 0x017a, -1}, /* 1x ź ..ź → Ź ..Ź Watin-A */
  911. {0x017c, 0x017c, -1}, /* 1x ż ..ż → Ż ..Ż Watin-A */
  912. {0x017e, 0x017e, -1}, /* 1x ž ..ž → Ž ..Ž Watin-A */
  913. {0x017f, 0x017f, -300}, /* 1x ſ ..ſ → S ..S Watin-A */
  914. {0x0180, 0x0180, +195}, /* 1x ƀ ..ƀ → Ƀ ..Ƀ Watin-B */
  915. {0x0183, 0x0183, -1}, /* 1x ƃ ..ƃ → Ƃ ..Ƃ Watin-B */
  916. {0x0185, 0x0185, -1}, /* 1x ƅ ..ƅ → Ƅ ..Ƅ Watin-B */
  917. {0x0188, 0x0188, -1}, /* 1x ƈ ..ƈ → Ƈ ..Ƈ Watin-B */
  918. {0x018c, 0x018c, -1}, /* 1x ƌ ..ƌ → Ƌ ..Ƌ Watin-B */
  919. {0x0192, 0x0192, -1}, /* 1x ƒ ..ƒ → Ƒ ..Ƒ Watin-B */
  920. {0x0195, 0x0195, +97}, /* 1x ƕ ..ƕ → Ƕ ..Ƕ Watin-B */
  921. {0x0199, 0x0199, -1}, /* 1x ƙ ..ƙ → Ƙ ..Ƙ Watin-B */
  922. {0x019a, 0x019a, +163}, /* 1x ƚ ..ƚ → Ƚ ..Ƚ Watin-B */
  923. {0x019e, 0x019e, +130}, /* 1x ƞ ..ƞ → Ƞ ..Ƞ Watin-B */
  924. {0x01a1, 0x01a1, -1}, /* 1x ơ ..ơ → Ơ ..Ơ Watin-B */
  925. {0x01a3, 0x01a3, -1}, /* 1x ƣ ..ƣ → Ƣ ..Ƣ Watin-B */
  926. {0x01a5, 0x01a5, -1}, /* 1x ƥ ..ƥ → Ƥ ..Ƥ Watin-B */
  927. {0x01a8, 0x01a8, -1}, /* 1x ƨ ..ƨ → Ƨ ..Ƨ Watin-B */
  928. {0x01ad, 0x01ad, -1}, /* 1x ƭ ..ƭ → Ƭ ..Ƭ Watin-B */
  929. {0x01b0, 0x01b0, -1}, /* 1x ư ..ư → Ư ..Ư Watin-B */
  930. {0x01b4, 0x01b4, -1}, /* 1x ƴ ..ƴ → Ƴ ..Ƴ Watin-B */
  931. {0x01b6, 0x01b6, -1}, /* 1x ƶ ..ƶ → Ƶ ..Ƶ Watin-B */
  932. {0x01b9, 0x01b9, -1}, /* 1x ƹ ..ƹ → Ƹ ..Ƹ Watin-B */
  933. {0x01bd, 0x01bd, -1}, /* 1x ƽ ..ƽ → Ƽ ..Ƽ Watin-B */
  934. {0x01bf, 0x01bf, +56}, /* 1x ƿ ..ƿ → Ƿ ..Ƿ Watin-B */
  935. {0x01c5, 0x01c5, -1}, /* 1x Dž ..Dž → DŽ ..DŽ Watin-B */
  936. {0x01c6, 0x01c6, -2}, /* 1x dž ..dž → DŽ ..DŽ Watin-B */
  937. {0x01c8, 0x01c8, -1}, /* 1x Lj ..Lj → LJ ..LJ Watin-B */
  938. {0x01c9, 0x01c9, -2}, /* 1x lj ..lj → LJ ..LJ Watin-B */
  939. {0x01cb, 0x01cb, -1}, /* 1x Nj ..Nj → NJ ..NJ Watin-B */
  940. {0x01cc, 0x01cc, -2}, /* 1x nj ..nj → NJ ..NJ Watin-B */
  941. {0x01ce, 0x01ce, -1}, /* 1x ǎ ..ǎ → Ǎ ..Ǎ Watin-B */
  942. {0x01dd, 0x01dd, -79}, /* 1x ǝ ..ǝ → Ǝ ..Ǝ Watin-B */
  943. {0x01f2, 0x01f2, -1}, /* 1x Dz ..Dz → DZ ..DZ Watin-B */
  944. {0x01f3, 0x01f3, -2}, /* 1x dz ..dz → DZ ..DZ Watin-B */
  945. {0x01f5, 0x01f5, -1}, /* 1x ǵ ..ǵ → Ǵ ..Ǵ Watin-B */
  946. {0x023c, 0x023c, -1}, /* 1x ȼ ..ȼ → Ȼ ..Ȼ Watin-B */
  947. {0x023f, 0x0240, +10815}, /* 2x ȿ ..ɀ → Ȿ ..Ɀ Watin-B */
  948. {0x0242, 0x0242, -1}, /* 1x ɂ ..ɂ → Ɂ ..Ɂ Watin-B */
  949. {0x0247, 0x0247, -1}, /* 1x ɇ ..ɇ → Ɇ ..Ɇ Watin-B */
  950. {0x0249, 0x0249, -1}, /* 1x ɉ ..ɉ → Ɉ ..Ɉ Watin-B */
  951. {0x024b, 0x024b, -1}, /* 1x ɋ ..ɋ → Ɋ ..Ɋ Watin-B */
  952. {0x024d, 0x024d, -1}, /* 1x ɍ ..ɍ → Ɍ ..Ɍ Watin-B */
  953. {0x024f, 0x024f, -1}, /* 1x ɏ ..ɏ → Ɏ ..Ɏ Watin-B */
  954. {0x037b, 0x037d, +130}, /* 3x ͻ ..ͽ → Ͻ ..Ͽ Greek */
  955. {0x03ac, 0x03ac, -38}, /* 1x ά ..ά → Ά ..Ά Greek */
  956. {0x03ad, 0x03af, -37}, /* 3x έ ..ί → Έ ..Ί Greek */
  957. {0x03b1, 0x03c1, -32}, /* 17x α ..ρ → Α ..Ρ Greek */
  958. {0x03c2, 0x03c2, -31}, /* 1x ς ..ς → Σ ..Σ Greek */
  959. {0x03c3, 0x03cb, -32}, /* 9x σ ..ϋ → Σ ..Ϋ Greek */
  960. {0x03cc, 0x03cc, -64}, /* 1x ό ..ό → Ό ..Ό Greek */
  961. {0x03cd, 0x03ce, -63}, /* 2x ύ ..ώ → Ύ ..Ώ Greek */
  962. {0x03d0, 0x03d0, -62}, /* 1x ϐ ..ϐ → Β ..Β Greek */
  963. {0x03d1, 0x03d1, -57}, /* 1x ϑ ..ϑ → Θ ..Θ Greek */
  964. {0x03d5, 0x03d5, -47}, /* 1x ϕ ..ϕ → Φ ..Φ Greek */
  965. {0x03d6, 0x03d6, -54}, /* 1x ϖ ..ϖ → Π ..Π Greek */
  966. {0x03dd, 0x03dd, -1}, /* 1x ϝ ..ϝ → Ϝ ..Ϝ Greek */
  967. {0x03f0, 0x03f0, -86}, /* 1x ϰ ..ϰ → Κ ..Κ Greek */
  968. {0x03f1, 0x03f1, -80}, /* 1x ϱ ..ϱ → Ρ ..Ρ Greek */
  969. {0x03f5, 0x03f5, -96}, /* 1x ϵ ..ϵ → Ε ..Ε Greek */
  970. {0x0430, 0x044f, -32}, /* 32x а ..я → А ..Я Cyrillic */
  971. {0x0450, 0x045f, -80}, /* 16x ѐ ..џ → Ѐ ..Џ Cyrillic */
  972. {0x0461, 0x0461, -1}, /* 1x ѡ ..ѡ → Ѡ ..Ѡ Cyrillic */
  973. {0x0463, 0x0463, -1}, /* 1x ѣ ..ѣ → Ѣ ..Ѣ Cyrillic */
  974. {0x0465, 0x0465, -1}, /* 1x ѥ ..ѥ → Ѥ ..Ѥ Cyrillic */
  975. {0x0473, 0x0473, -1}, /* 1x ѳ ..ѳ → Ѳ ..Ѳ Cyrillic */
  976. {0x0491, 0x0491, -1}, /* 1x ґ ..ґ → Ґ ..Ґ Cyrillic */
  977. {0x0499, 0x0499, -1}, /* 1x ҙ ..ҙ → Ҙ ..Ҙ Cyrillic */
  978. {0x049b, 0x049b, -1}, /* 1x қ ..қ → Қ ..Қ Cyrillic */
  979. {0x0561, 0x0586, -48}, /* 38x ա ..ֆ → Ա ..Ֆ Armenian */
  980. {0x10d0, 0x10fa, +3008}, /* 43x ა ..ჺ → Ა ..Ჺ Georgian */
  981. {0x10fd, 0x10ff, +3008}, /* 3x ჽ ..ჿ → Ჽ ..Ჿ Georgian */
  982. {0x13f8, 0x13fd, -8}, /* 6x ᏸ ..ᏽ → Ᏸ ..Ᏽ Cherokee */
  983. {0x214e, 0x214e, -28}, /* 1x ⅎ ..ⅎ → Ⅎ ..Ⅎ Letterlike */
  984. {0x2170, 0x217f, -16}, /* 16x ⅰ ..ⅿ → Ⅰ ..Ⅿ Numbery */
  985. {0x2184, 0x2184, -1}, /* 1x ↄ ..ↄ → Ↄ ..Ↄ Numbery */
  986. {0x24d0, 0x24e9, -26}, /* 26x ⓐ ..ⓩ → Ⓐ ..Ⓩ Enclosed */
  987. {0x2c30, 0x2c5e, -48}, /* 47x ⰰ ..ⱞ → Ⰰ ..Ⱞ Glagolitic */
  988. {0x2d00, 0x2d25, -7264}, /* 38x ⴀ ..ⴥ → Ⴀ ..Ⴥ Georgian2 */
  989. {0x2d27, 0x2d27, -7264}, /* 1x ⴧ ..ⴧ → Ⴧ ..Ⴧ Georgian2 */
  990. {0x2d2d, 0x2d2d, -7264}, /* 1x ⴭ ..ⴭ → Ⴭ ..Ⴭ Georgian2 */
  991. {0xff41, 0xff5a, -32}, /* 26x a..z → A..Z Dubs */
  992. };
  993. l = 0;
  994. r = n = sizeof(kUpper) / sizeof(kUpper[0]);
  995. while (l < r) {
  996. m = (l + r) >> 1;
  997. if (kUpper[m].b < c) {
  998. l = m + 1;
  999. } else {
  1000. r = m;
  1001. }
  1002. }
  1003. if (l < n && kUpper[l].a <= c && c <= kUpper[l].b) {
  1004. return c + kUpper[l].d;
  1005. } else {
  1006. return c;
  1007. }
  1008. }
  1009. } else {
  1010. static const struct {
  1011. unsigned a;
  1012. unsigned b;
  1013. short d;
  1014. } kAstralUpper[] = {
  1015. {0x10428, 0x1044f, -40}, /* 40x 𐐨..𐑏 → 𐐀..𐐧 Deseret */
  1016. {0x104d8, 0x104fb, -40}, /* 36x 𐓘..𐓻 → 𐒰..𐓓 Osage */
  1017. {0x1d41a, 0x1d433, -26}, /* 26x 𝐚..𝐳 → 𝐀..𝐙 Math */
  1018. {0x1d456, 0x1d467, -26}, /* 18x 𝑖..𝑧 → 𝐼..𝑍 Math */
  1019. {0x1d482, 0x1d49b, -26}, /* 26x 𝒂..𝒛 → 𝑨..𝒁 Math */
  1020. {0x1d4c8, 0x1d4cf, -26}, /* 8x 𝓈..𝓏 → 𝒮..𝒵 Math */
  1021. {0x1d4ea, 0x1d503, -26}, /* 26x 𝓪..𝔃 → 𝓐..𝓩 Math */
  1022. {0x1d527, 0x1d52e, -26}, /* 8x 𝔧..𝔮 → 𝔍..𝔔 Math */
  1023. {0x1d586, 0x1d59f, -26}, /* 26x 𝖆..𝖟 → 𝕬..𝖅 Math */
  1024. {0x1d5ba, 0x1d5d3, -26}, /* 26x 𝖺..𝗓 → 𝖠..𝖹 Math */
  1025. {0x1d5ee, 0x1d607, -26}, /* 26x 𝗮..𝘇 → 𝗔..𝗭 Math */
  1026. {0x1d622, 0x1d63b, -26}, /* 26x 𝘢..𝘻 → 𝘈..𝘡 Math */
  1027. {0x1d68a, 0x1d6a3, +442}, /* 26x 𝒂..𝒛 → 𝘼..𝙕 Math */
  1028. {0x1d6c2, 0x1d6d2, -26}, /* 26x 𝚊..𝚣 → 𝙰..𝚉 Math */
  1029. {0x1d6fc, 0x1d70c, -26}, /* 17x 𝛂..𝛒 → 𝚨..𝚸 Math */
  1030. {0x1d736, 0x1d746, -26}, /* 17x 𝛼..𝜌 → 𝛢..𝛲 Math */
  1031. {0x1d770, 0x1d780, -26}, /* 17x 𝜶..𝝆 → 𝜜..𝜬 Math */
  1032. {0x1d770, 0x1d756, -26}, /* 17x 𝝰..𝞀 → 𝝖..𝝦 Math */
  1033. {0x1d736, 0x1d790, -90}, /* 17x 𝜶..𝝆 → 𝞐..𝞠 Math */
  1034. };
  1035. l = 0;
  1036. r = n = sizeof(kAstralUpper) / sizeof(kAstralUpper[0]);
  1037. while (l < r) {
  1038. m = (l + r) >> 1;
  1039. if (kAstralUpper[m].b < c) {
  1040. l = m + 1;
  1041. } else {
  1042. r = m;
  1043. }
  1044. }
  1045. if (l < n && kAstralUpper[l].a <= c && c <= kAstralUpper[l].b) {
  1046. return c + kAstralUpper[l].d;
  1047. } else {
  1048. return c;
  1049. }
  1050. }
  1051. }
  1052. char bestlineNotSeparator(unsigned c) {
  1053. return !bestlineIsSeparator(c);
  1054. }
  1055. static unsigned GetMirror(const unsigned short A[][2], size_t n, unsigned c) {
  1056. int l, m, r;
  1057. l = 0;
  1058. r = n - 1;
  1059. while (l <= r) {
  1060. m = (l + r) >> 1;
  1061. if (A[m][0] < c) {
  1062. l = m + 1;
  1063. } else if (A[m][0] > c) {
  1064. r = m - 1;
  1065. } else {
  1066. return A[m][1];
  1067. }
  1068. }
  1069. return 0;
  1070. }
  1071. unsigned bestlineMirrorLeft(unsigned c) {
  1072. static const unsigned short kMirrorRight[][2] = {
  1073. {L')', L'('}, {L']', L'['}, {L'}', L'{'}, {L'⁆', L'⁅'}, {L'⁾', L'⁽'},
  1074. {L'₎', L'₍'}, {L'⌉', L'⌈'}, {L'⌋', L'⌊'}, {L'〉', L'〈'}, {L'❩', L'❨'},
  1075. {L'❫', L'❪'}, {L'❭', L'❬'}, {L'❯', L'❮'}, {L'❱', L'❰'}, {L'❳', L'❲'},
  1076. {L'❵', L'❴'}, {L'⟆', L'⟅'}, {L'⟧', L'⟦'}, {L'⟩', L'⟨'}, {L'⟫', L'⟪'},
  1077. {L'⟭', L'⟬'}, {L'⟯', L'⟮'}, {L'⦄', L'⦃'}, {L'⦆', L'⦅'}, {L'⦈', L'⦇'},
  1078. {L'⦊', L'⦉'}, {L'⦌', L'⦋'}, {L'⦎', L'⦏'}, {L'⦐', L'⦍'}, {L'⦒', L'⦑'},
  1079. {L'⦔', L'⦓'}, {L'⦘', L'⦗'}, {L'⧙', L'⧘'}, {L'⧛', L'⧚'}, {L'⧽', L'⧼'},
  1080. {L'﹚', L'﹙'}, {L'﹜', L'﹛'}, {L'﹞', L'﹝'}, {L')', L'('}, {L']', L'['},
  1081. {L'}', L'{'}, {L'」', L'「'},
  1082. };
  1083. return GetMirror(kMirrorRight, sizeof(kMirrorRight) / sizeof(kMirrorRight[0]), c);
  1084. }
  1085. unsigned bestlineMirrorRight(unsigned c) {
  1086. static const unsigned short kMirrorLeft[][2] = {
  1087. {L'(', L')'}, {L'[', L']'}, {L'{', L'}'}, {L'⁅', L'⁆'}, {L'⁽', L'⁾'},
  1088. {L'₍', L'₎'}, {L'⌈', L'⌉'}, {L'⌊', L'⌋'}, {L'〈', L'〉'}, {L'❨', L'❩'},
  1089. {L'❪', L'❫'}, {L'❬', L'❭'}, {L'❮', L'❯'}, {L'❰', L'❱'}, {L'❲', L'❳'},
  1090. {L'❴', L'❵'}, {L'⟅', L'⟆'}, {L'⟦', L'⟧'}, {L'⟨', L'⟩'}, {L'⟪', L'⟫'},
  1091. {L'⟬', L'⟭'}, {L'⟮', L'⟯'}, {L'⦃', L'⦄'}, {L'⦅', L'⦆'}, {L'⦇', L'⦈'},
  1092. {L'⦉', L'⦊'}, {L'⦋', L'⦌'}, {L'⦍', L'⦐'}, {L'⦏', L'⦎'}, {L'⦑', L'⦒'},
  1093. {L'⦓', L'⦔'}, {L'⦗', L'⦘'}, {L'⧘', L'⧙'}, {L'⧚', L'⧛'}, {L'⧼', L'⧽'},
  1094. {L'﹙', L'﹚'}, {L'﹛', L'﹜'}, {L'﹝', L'﹞'}, {L'(', L')'}, {L'[', L']'},
  1095. {L'{', L'}'}, {L'「', L'」'},
  1096. };
  1097. return GetMirror(kMirrorLeft, sizeof(kMirrorLeft) / sizeof(kMirrorLeft[0]), c);
  1098. }
  1099. static char StartsWith(const char *s, const char *prefix) {
  1100. for (;;) {
  1101. if (!*prefix)
  1102. return 1;
  1103. if (!*s)
  1104. return 0;
  1105. if (*s++ != *prefix++)
  1106. return 0;
  1107. }
  1108. }
  1109. static char EndsWith(const char *s, const char *suffix) {
  1110. size_t n, m;
  1111. n = strlen(s);
  1112. m = strlen(suffix);
  1113. if (m > n)
  1114. return 0;
  1115. return !memcmp(s + n - m, suffix, m);
  1116. }
  1117. char bestlineIsXeparator(unsigned c) {
  1118. return (bestlineIsSeparator(c) && !bestlineMirrorLeft(c) && !bestlineMirrorRight(c));
  1119. }
  1120. static unsigned Capitalize(unsigned c) {
  1121. if (!iscapital) {
  1122. c = bestlineUppercase(c);
  1123. iscapital = 1;
  1124. }
  1125. return c;
  1126. }
  1127. static inline int Bsr(unsigned long long x) {
  1128. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  1129. int b;
  1130. b = __builtin_clzll(x);
  1131. b ^= sizeof(unsigned long long) * CHAR_BIT - 1;
  1132. return b;
  1133. #else
  1134. static const char kDebruijn[64] = {
  1135. 0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61, 54, 58, 35, 52, 50, 42,
  1136. 21, 44, 38, 32, 29, 23, 17, 11, 4, 62, 46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43,
  1137. 31, 22, 10, 45, 25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63,
  1138. };
  1139. x |= x >> 1;
  1140. x |= x >> 2;
  1141. x |= x >> 4;
  1142. x |= x >> 8;
  1143. x |= x >> 16;
  1144. x |= x >> 32;
  1145. return kDebruijn[(x * 0x03f79d71b4cb0a89) >> 58];
  1146. #endif
  1147. }
  1148. static struct rune DecodeUtf8(int c) {
  1149. struct rune r;
  1150. if (c < 252) {
  1151. r.n = Bsr(255 & ~c);
  1152. r.c = c & (((1 << r.n) - 1) | 3);
  1153. r.n = 6 - r.n;
  1154. } else {
  1155. r.c = c & 3;
  1156. r.n = 5;
  1157. }
  1158. return r;
  1159. }
  1160. static unsigned long long EncodeUtf8(unsigned c) {
  1161. static const unsigned short kTpEnc[32 - 7] = {
  1162. 1 | 0300 << 8, 1 | 0300 << 8, 1 | 0300 << 8, 1 | 0300 << 8, 2 | 0340 << 8,
  1163. 2 | 0340 << 8, 2 | 0340 << 8, 2 | 0340 << 8, 2 | 0340 << 8, 3 | 0360 << 8,
  1164. 3 | 0360 << 8, 3 | 0360 << 8, 3 | 0360 << 8, 3 | 0360 << 8, 4 | 0370 << 8,
  1165. 4 | 0370 << 8, 4 | 0370 << 8, 4 | 0370 << 8, 4 | 0370 << 8, 5 | 0374 << 8,
  1166. 5 | 0374 << 8, 5 | 0374 << 8, 5 | 0374 << 8, 5 | 0374 << 8, 5 | 0374 << 8,
  1167. };
  1168. int e, n;
  1169. unsigned long long w;
  1170. if (c < 0200)
  1171. return c;
  1172. e = kTpEnc[Bsr(c) - 7];
  1173. n = e & 0xff;
  1174. w = 0;
  1175. do {
  1176. w |= 0200 | (c & 077);
  1177. w <<= 8;
  1178. c >>= 6;
  1179. } while (--n);
  1180. return c | w | e >> 8;
  1181. }
  1182. static struct rune GetUtf8(const char *p, size_t n) {
  1183. struct rune r;
  1184. if ((r.n = r.c = 0) < n && (r.c = p[r.n++] & 255) >= 0300) {
  1185. r.c = DecodeUtf8(r.c).c;
  1186. while (r.n < n && (p[r.n] & 0300) == 0200) {
  1187. r.c = r.c << 6 | (p[r.n++] & 077);
  1188. }
  1189. }
  1190. return r;
  1191. }
  1192. static char *FormatUnsigned(char *p, unsigned x) {
  1193. char t;
  1194. size_t i, a, b;
  1195. i = 0;
  1196. do {
  1197. p[i++] = x % 10 + '0';
  1198. x = x / 10;
  1199. } while (x > 0);
  1200. p[i] = '\0';
  1201. if (i) {
  1202. for (a = 0, b = i - 1; a < b; ++a, --b) {
  1203. t = p[a];
  1204. p[a] = p[b];
  1205. p[b] = t;
  1206. }
  1207. }
  1208. return p + i;
  1209. }
  1210. static void abInit(struct abuf *a) {
  1211. a->len = 0;
  1212. a->cap = 16;
  1213. a->b = (char *)malloc(a->cap);
  1214. a->b[0] = 0;
  1215. }
  1216. static char abGrow(struct abuf *a, int need) {
  1217. int cap;
  1218. char *b;
  1219. cap = a->cap;
  1220. do
  1221. cap += cap / 2;
  1222. while (cap < need);
  1223. if (!(b = (char *)realloc(a->b, cap * sizeof(*a->b))))
  1224. return 0;
  1225. a->cap = cap;
  1226. a->b = b;
  1227. return 1;
  1228. }
  1229. static void abAppendw(struct abuf *a, unsigned long long w) {
  1230. char *p;
  1231. if (a->len + 8 > a->cap && !abGrow(a, a->len + 8))
  1232. return;
  1233. p = a->b + a->len;
  1234. p[0] = (0x00000000000000FF & w) >> 000;
  1235. p[1] = (0x000000000000FF00 & w) >> 010;
  1236. p[2] = (0x0000000000FF0000 & w) >> 020;
  1237. p[3] = (0x00000000FF000000 & w) >> 030;
  1238. p[4] = (0x000000FF00000000 & w) >> 040;
  1239. p[5] = (0x0000FF0000000000 & w) >> 050;
  1240. p[6] = (0x00FF000000000000 & w) >> 060;
  1241. p[7] = (0xFF00000000000000 & w) >> 070;
  1242. a->len += w ? (Bsr(w) >> 3) + 1 : 1;
  1243. }
  1244. static void abAppend(struct abuf *a, const char *s, int len) {
  1245. if (a->len + len + 1 > a->cap && !abGrow(a, a->len + len + 1))
  1246. return;
  1247. memcpy(a->b + a->len, s, len);
  1248. a->b[a->len + len] = 0;
  1249. a->len += len;
  1250. }
  1251. static void abAppends(struct abuf *a, const char *s) {
  1252. abAppend(a, s, strlen(s));
  1253. }
  1254. static void abAppendu(struct abuf *a, unsigned u) {
  1255. char b[11];
  1256. abAppend(a, b, FormatUnsigned(b, u) - b);
  1257. }
  1258. static void abFree(struct abuf *a) {
  1259. free(a->b);
  1260. a->b = 0;
  1261. }
  1262. static size_t GetFdSize(int fd) {
  1263. struct stat st;
  1264. st.st_size = 0;
  1265. fstat(fd, &st);
  1266. return st.st_size;
  1267. }
  1268. static char IsCharDev(int fd) {
  1269. struct stat st;
  1270. st.st_mode = 0;
  1271. fstat(fd, &st);
  1272. return (st.st_mode & S_IFMT) == S_IFCHR;
  1273. }
  1274. static int MyRead(int fd, void *c, int);
  1275. static int MyWrite(int fd, const void *c, int);
  1276. static int MyPoll(int fd, int events, int to);
  1277. static int (*_MyRead)(int fd, void *c, int n) = MyRead;
  1278. static int (*_MyWrite)(int fd, const void *c, int n) = MyWrite;
  1279. static int (*_MyPoll)(int fd, int events, int to) = MyPoll;
  1280. static int WaitUntilReady(int fd, int events) {
  1281. return _MyPoll(fd, events, -1);
  1282. }
  1283. static char HasPendingInput(int fd) {
  1284. return _MyPoll(fd, POLLIN, 0) == 1;
  1285. }
  1286. static char *GetLineBlock(FILE *f) {
  1287. ssize_t rc;
  1288. char *p = 0;
  1289. size_t n, c = 0;
  1290. if ((rc = getdelim(&p, &c, '\n', f)) != EOF) {
  1291. for (n = rc; n; --n) {
  1292. if (p[n - 1] == '\r' || p[n - 1] == '\n') {
  1293. p[n - 1] = 0;
  1294. } else {
  1295. break;
  1296. }
  1297. }
  1298. return p;
  1299. } else {
  1300. free(p);
  1301. return 0;
  1302. }
  1303. }
  1304. long bestlineReadCharacter(int fd, char *p, unsigned long n) {
  1305. int e;
  1306. size_t i;
  1307. ssize_t rc;
  1308. struct rune r;
  1309. unsigned char c;
  1310. enum { kAscii, kUtf8, kEsc, kCsi1, kCsi2, kSs, kNf, kStr, kStr2, kDone } t;
  1311. i = 0;
  1312. r.c = 0;
  1313. r.n = 0;
  1314. e = errno;
  1315. t = kAscii;
  1316. if (n)
  1317. p[0] = 0;
  1318. do {
  1319. for (;;) {
  1320. if (gotint) {
  1321. errno = EINTR;
  1322. return -1;
  1323. }
  1324. if (n) {
  1325. rc = _MyRead(fd, &c, 1);
  1326. } else {
  1327. rc = _MyRead(fd, 0, 0);
  1328. }
  1329. if (rc == -1 && errno == EINTR) {
  1330. if (!i) {
  1331. return -1;
  1332. }
  1333. } else if (rc == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  1334. if (WaitUntilReady(fd, POLLIN) == -1) {
  1335. if (rc == -1 && errno == EINTR) {
  1336. if (!i) {
  1337. return -1;
  1338. }
  1339. } else {
  1340. return -1;
  1341. }
  1342. }
  1343. } else if (rc == -1) {
  1344. return -1;
  1345. } else if (!rc) {
  1346. if (!i) {
  1347. errno = e;
  1348. return 0;
  1349. } else {
  1350. errno = EILSEQ;
  1351. return -1;
  1352. }
  1353. } else {
  1354. break;
  1355. }
  1356. }
  1357. if (i + 1 < n) {
  1358. p[i] = c;
  1359. p[i + 1] = 0;
  1360. } else if (i < n) {
  1361. p[i] = 0;
  1362. }
  1363. ++i;
  1364. switch (t) {
  1365. Whoopsie:
  1366. if (n)
  1367. p[0] = c;
  1368. t = kAscii;
  1369. i = 1;
  1370. /* fallthrough */
  1371. case kAscii:
  1372. if (c < 0200) {
  1373. if (c == 033) {
  1374. t = kEsc;
  1375. } else {
  1376. t = kDone;
  1377. }
  1378. } else if (c >= 0300) {
  1379. t = kUtf8;
  1380. r = DecodeUtf8(c);
  1381. } else {
  1382. /* ignore overlong sequences */
  1383. }
  1384. break;
  1385. case kUtf8:
  1386. if ((c & 0300) == 0200) {
  1387. r.c <<= 6;
  1388. r.c |= c & 077;
  1389. if (!--r.n) {
  1390. switch (r.c) {
  1391. case 033:
  1392. t = kEsc; /* parsed but not canonicalized */
  1393. break;
  1394. case 0x9b:
  1395. t = kCsi1; /* unusual but legal */
  1396. break;
  1397. case 0x8e: /* SS2 (Single Shift Two) */
  1398. case 0x8f: /* SS3 (Single Shift Three) */
  1399. t = kSs;
  1400. break;
  1401. case 0x90: /* DCS (Device Control String) */
  1402. case 0x98: /* SOS (Start of String) */
  1403. case 0x9d: /* OSC (Operating System Command) */
  1404. case 0x9e: /* PM (Privacy Message) */
  1405. case 0x9f: /* APC (Application Program Command) */
  1406. t = kStr;
  1407. break;
  1408. default:
  1409. t = kDone;
  1410. break;
  1411. }
  1412. }
  1413. } else {
  1414. goto Whoopsie; /* ignore underlong sequences if not eof */
  1415. }
  1416. break;
  1417. case kEsc:
  1418. if (0x20 <= c && c <= 0x2f) { /* Nf */
  1419. /*
  1420. * Almost no one uses ANSI Nf sequences
  1421. * They overlaps with alt+graphic keystrokes
  1422. * We care more about being able to type alt-/
  1423. */
  1424. if (c == ' ' || c == '#') {
  1425. t = kNf;
  1426. } else {
  1427. t = kDone;
  1428. }
  1429. } else if (0x30 <= c && c <= 0x3f) { /* Fp */
  1430. t = kDone;
  1431. } else if (0x20 <= c && c <= 0x5F) { /* Fe */
  1432. switch (c) {
  1433. case '[':
  1434. t = kCsi1;
  1435. break;
  1436. case 'N': /* SS2 (Single Shift Two) */
  1437. case 'O': /* SS3 (Single Shift Three) */
  1438. t = kSs;
  1439. break;
  1440. case 'P': /* DCS (Device Control String) */
  1441. case 'X': /* SOS (Start of String) */
  1442. case ']': /* OSC (Operating System Command) */
  1443. case '^': /* PM (Privacy Message) */
  1444. case '_': /* APC (Application Program Command) */
  1445. t = kStr;
  1446. break;
  1447. default:
  1448. t = kDone;
  1449. break;
  1450. }
  1451. } else if (0x60 <= c && c <= 0x7e) { /* Fs */
  1452. t = kDone;
  1453. } else if (c == 033) {
  1454. if (i < 3) {
  1455. /* alt chording */
  1456. } else {
  1457. t = kDone; /* esc mashing */
  1458. i = 1;
  1459. }
  1460. } else {
  1461. t = kDone;
  1462. }
  1463. break;
  1464. case kSs:
  1465. t = kDone;
  1466. break;
  1467. case kNf:
  1468. if (0x30 <= c && c <= 0x7e) {
  1469. t = kDone;
  1470. } else if (!(0x20 <= c && c <= 0x2f)) {
  1471. goto Whoopsie;
  1472. }
  1473. break;
  1474. case kCsi1:
  1475. if (0x20 <= c && c <= 0x2f) {
  1476. t = kCsi2;
  1477. } else if (c == '[' && ((i == 3) || (i == 4 && p[1] == 033))) {
  1478. /* linux function keys */
  1479. } else if (0x40 <= c && c <= 0x7e) {
  1480. t = kDone;
  1481. } else if (!(0x30 <= c && c <= 0x3f)) {
  1482. goto Whoopsie;
  1483. }
  1484. break;
  1485. case kCsi2:
  1486. if (0x40 <= c && c <= 0x7e) {
  1487. t = kDone;
  1488. } else if (!(0x20 <= c && c <= 0x2f)) {
  1489. goto Whoopsie;
  1490. }
  1491. break;
  1492. case kStr:
  1493. switch (c) {
  1494. case '\a':
  1495. t = kDone;
  1496. break;
  1497. case 0033: /* ESC */
  1498. case 0302: /* C1 (UTF-8) */
  1499. t = kStr2;
  1500. break;
  1501. default:
  1502. break;
  1503. }
  1504. break;
  1505. case kStr2:
  1506. switch (c) {
  1507. case '\a':
  1508. case '\\': /* ST (ASCII) */
  1509. case 0234: /* ST (UTF-8) */
  1510. t = kDone;
  1511. break;
  1512. default:
  1513. t = kStr;
  1514. break;
  1515. }
  1516. break;
  1517. default:
  1518. assert(0);
  1519. }
  1520. } while (t != kDone);
  1521. errno = e;
  1522. return i;
  1523. }
  1524. static char *GetLineChar(int fin, int fout) {
  1525. size_t got;
  1526. ssize_t rc;
  1527. char seq[16];
  1528. struct abuf a;
  1529. struct sigaction sa[3];
  1530. abInit(&a);
  1531. gotint = 0;
  1532. sigemptyset(&sa->sa_mask);
  1533. sa->sa_flags = 0;
  1534. sa->sa_handler = bestlineOnInt;
  1535. sigaction(SIGINT, sa, sa + 1);
  1536. sigaction(SIGQUIT, sa, sa + 2);
  1537. for (;;) {
  1538. if (gotint) {
  1539. rc = -1;
  1540. break;
  1541. }
  1542. if ((rc = bestlineReadCharacter(fin, seq, sizeof(seq))) == -1) {
  1543. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  1544. if (WaitUntilReady(fin, POLLIN) > 0) {
  1545. continue;
  1546. }
  1547. }
  1548. if (errno == EINTR) {
  1549. continue;
  1550. } else {
  1551. break;
  1552. }
  1553. }
  1554. if (!(got = rc)) {
  1555. if (a.len) {
  1556. break;
  1557. } else {
  1558. rc = -1;
  1559. break;
  1560. }
  1561. }
  1562. if (seq[0] == '\r') {
  1563. if (HasPendingInput(fin)) {
  1564. if ((rc = bestlineReadCharacter(fin, seq + 1, sizeof(seq) - 1)) > 0) {
  1565. if (seq[0] == '\n') {
  1566. break;
  1567. }
  1568. } else {
  1569. rc = -1;
  1570. break;
  1571. }
  1572. } else {
  1573. _MyWrite(fout, "\n", 1);
  1574. break;
  1575. }
  1576. } else if (seq[0] == Ctrl('D')) {
  1577. break;
  1578. } else if (seq[0] == '\n') {
  1579. break;
  1580. } else if (seq[0] == '\b') {
  1581. while (a.len && (a.b[a.len - 1] & 0300) == 0200)
  1582. --a.len;
  1583. if (a.len)
  1584. --a.len;
  1585. }
  1586. if (!IsControl(seq[0])) {
  1587. abAppend(&a, seq, got);
  1588. }
  1589. }
  1590. sigaction(SIGQUIT, sa + 2, 0);
  1591. sigaction(SIGINT, sa + 1, 0);
  1592. if (gotint) {
  1593. abFree(&a);
  1594. raise(gotint);
  1595. errno = EINTR;
  1596. rc = -1;
  1597. }
  1598. if (rc != -1) {
  1599. return a.b;
  1600. } else {
  1601. abFree(&a);
  1602. return 0;
  1603. }
  1604. }
  1605. static char *GetLine(FILE *in, FILE *out) {
  1606. if (!IsCharDev(fileno(in))) {
  1607. return GetLineBlock(in);
  1608. } else {
  1609. return GetLineChar(fileno(in), fileno(out));
  1610. }
  1611. }
  1612. static char *Copy(char *d, const char *s, size_t n) {
  1613. memcpy(d, s, n);
  1614. return d + n;
  1615. }
  1616. static int CompareStrings(const char *a, const char *b) {
  1617. size_t i;
  1618. int x, y, c;
  1619. for (i = 0;; ++i) {
  1620. x = bestlineLowercase(a[i] & 255);
  1621. y = bestlineLowercase(b[i] & 255);
  1622. if ((c = x - y) || !x) {
  1623. return c;
  1624. }
  1625. }
  1626. }
  1627. static const char *FindSubstringReverse(const char *p, size_t n, const char *q, size_t m) {
  1628. size_t i;
  1629. if (m <= n) {
  1630. n -= m;
  1631. do {
  1632. for (i = 0; i < m; ++i) {
  1633. if (p[n + i] != q[i]) {
  1634. break;
  1635. }
  1636. }
  1637. if (i == m) {
  1638. return p + n;
  1639. }
  1640. } while (n--);
  1641. }
  1642. return 0;
  1643. }
  1644. static int ParseUnsigned(const char *s, void *e) {
  1645. int c, x;
  1646. for (x = 0; (c = *s++);) {
  1647. if ('0' <= c && c <= '9') {
  1648. x = Min(c - '0' + x * 10, 32767);
  1649. } else {
  1650. break;
  1651. }
  1652. }
  1653. if (e)
  1654. *(const char **)e = s;
  1655. return x;
  1656. }
  1657. /**
  1658. * Returns UNICODE CJK Monospace Width of string.
  1659. *
  1660. * Control codes and ANSI sequences have a width of zero. We only parse
  1661. * a limited subset of ANSI here since we don't store ANSI codes in the
  1662. * linenoiseState::buf, but we do encourage CSI color codes in prompts.
  1663. */
  1664. static size_t GetMonospaceWidth(const char *p, size_t n, char *out_haswides) {
  1665. int c, d;
  1666. size_t i, w;
  1667. struct rune r;
  1668. char haswides;
  1669. enum { kAscii, kUtf8, kEsc, kCsi1, kCsi2 } t;
  1670. for (haswides = r.c = r.n = w = i = 0, t = kAscii; i < n; ++i) {
  1671. c = p[i] & 255;
  1672. switch (t) {
  1673. Whoopsie:
  1674. t = kAscii;
  1675. /* fallthrough */
  1676. case kAscii:
  1677. if (c < 0200) {
  1678. if (c == 033) {
  1679. t = kEsc;
  1680. } else {
  1681. ++w;
  1682. }
  1683. } else if (c >= 0300) {
  1684. t = kUtf8;
  1685. r = DecodeUtf8(c);
  1686. }
  1687. break;
  1688. case kUtf8:
  1689. if ((c & 0300) == 0200) {
  1690. r.c <<= 6;
  1691. r.c |= c & 077;
  1692. if (!--r.n) {
  1693. d = GetMonospaceCharacterWidth(r.c);
  1694. d = Max(0, d);
  1695. w += d;
  1696. haswides |= d > 1;
  1697. t = kAscii;
  1698. break;
  1699. }
  1700. } else {
  1701. goto Whoopsie;
  1702. }
  1703. break;
  1704. case kEsc:
  1705. if (c == '[') {
  1706. t = kCsi1;
  1707. } else {
  1708. t = kAscii;
  1709. }
  1710. break;
  1711. case kCsi1:
  1712. if (0x20 <= c && c <= 0x2f) {
  1713. t = kCsi2;
  1714. } else if (0x40 <= c && c <= 0x7e) {
  1715. t = kAscii;
  1716. } else if (!(0x30 <= c && c <= 0x3f)) {
  1717. goto Whoopsie;
  1718. }
  1719. break;
  1720. case kCsi2:
  1721. if (0x40 <= c && c <= 0x7e) {
  1722. t = kAscii;
  1723. } else if (!(0x20 <= c && c <= 0x2f)) {
  1724. goto Whoopsie;
  1725. }
  1726. break;
  1727. default:
  1728. assert(0);
  1729. }
  1730. }
  1731. if (out_haswides) {
  1732. *out_haswides = haswides;
  1733. }
  1734. return w;
  1735. }
  1736. static int bestlineIsUnsupportedTerm(void) {
  1737. size_t i;
  1738. char *term;
  1739. static char once, res;
  1740. if (!once) {
  1741. if ((term = getenv("TERM"))) {
  1742. for (i = 0; i < sizeof(kUnsupported) / sizeof(*kUnsupported); i++) {
  1743. if (!CompareStrings(term, kUnsupported[i])) {
  1744. res = 1;
  1745. break;
  1746. }
  1747. }
  1748. }
  1749. once = 1;
  1750. }
  1751. return res;
  1752. }
  1753. static int enableRawMode(int fd) {
  1754. struct termios raw;
  1755. struct sigaction sa;
  1756. if (tcgetattr(fd, &orig_termios) != -1) {
  1757. raw = orig_termios;
  1758. raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
  1759. raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
  1760. raw.c_iflag |= IUTF8;
  1761. raw.c_cflag |= CS8;
  1762. raw.c_cc[VMIN] = 1;
  1763. raw.c_cc[VTIME] = 0;
  1764. if (tcsetattr(fd, TCSANOW, &raw) != -1) {
  1765. sa.sa_flags = 0;
  1766. sa.sa_handler = bestlineOnCont;
  1767. sigemptyset(&sa.sa_mask);
  1768. sigaction(SIGCONT, &sa, &orig_cont);
  1769. sa.sa_handler = bestlineOnWinch;
  1770. sigaction(SIGWINCH, &sa, &orig_winch);
  1771. rawmode = fd;
  1772. gotwinch = 0;
  1773. gotcont = 0;
  1774. return 0;
  1775. }
  1776. }
  1777. errno = ENOTTY;
  1778. return -1;
  1779. }
  1780. static void bestlineUnpause(int fd) {
  1781. if (ispaused) {
  1782. tcflow(fd, TCOON);
  1783. ispaused = 0;
  1784. }
  1785. }
  1786. void bestlineDisableRawMode(void) {
  1787. if (rawmode != -1) {
  1788. bestlineUnpause(rawmode);
  1789. sigaction(SIGCONT, &orig_cont, 0);
  1790. sigaction(SIGWINCH, &orig_winch, 0);
  1791. tcsetattr(rawmode, TCSANOW, &orig_termios);
  1792. rawmode = -1;
  1793. }
  1794. }
  1795. static int bestlineWrite(int fd, const void *p, size_t n) {
  1796. ssize_t rc;
  1797. size_t wrote;
  1798. do {
  1799. for (;;) {
  1800. if (gotint) {
  1801. errno = EINTR;
  1802. return -1;
  1803. }
  1804. if (ispaused) {
  1805. return 0;
  1806. }
  1807. rc = _MyWrite(fd, p, n);
  1808. if (rc == -1 && errno == EINTR) {
  1809. continue;
  1810. } else if (rc == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  1811. if (WaitUntilReady(fd, POLLOUT) == -1) {
  1812. if (errno == EINTR) {
  1813. continue;
  1814. } else {
  1815. return -1;
  1816. }
  1817. }
  1818. } else {
  1819. break;
  1820. }
  1821. }
  1822. if (rc != -1) {
  1823. wrote = rc;
  1824. n -= wrote;
  1825. p = (char *)p + wrote;
  1826. } else {
  1827. return -1;
  1828. }
  1829. } while (n);
  1830. return 0;
  1831. }
  1832. static int bestlineWriteStr(int fd, const char *p) {
  1833. return bestlineWrite(fd, p, strlen(p));
  1834. }
  1835. static ssize_t bestlineRead(int fd, char *buf, size_t size, struct bestlineState *l) {
  1836. size_t got;
  1837. ssize_t rc;
  1838. int refreshme;
  1839. do {
  1840. refreshme = 0;
  1841. if (gotint) {
  1842. errno = EINTR;
  1843. return -1;
  1844. }
  1845. if (gotcont && rawmode != -1) {
  1846. enableRawMode(rawmode);
  1847. if (l)
  1848. refreshme = 1;
  1849. }
  1850. if (gotwinch && l) {
  1851. refreshme = 1;
  1852. }
  1853. if (refreshme)
  1854. bestlineRefreshLine(l);
  1855. rc = bestlineReadCharacter(fd, buf, size);
  1856. } while (rc == -1 && errno == EINTR);
  1857. if (rc != -1) {
  1858. got = rc;
  1859. if (got > 0 && l) {
  1860. memcpy(l->seq[1], l->seq[0], sizeof(l->seq[0]));
  1861. memset(l->seq[0], 0, sizeof(l->seq[0]));
  1862. memcpy(l->seq[0], buf, Min(Min(size, got), sizeof(l->seq[0]) - 1));
  1863. }
  1864. }
  1865. return rc;
  1866. }
  1867. /**
  1868. * Returns number of columns in current terminal.
  1869. *
  1870. * 1. Checks COLUMNS environment variable (set by Emacs)
  1871. * 2. Tries asking termios (works for pseudoteletypewriters)
  1872. * 3. Falls back to inband signalling (works w/ pipe or serial)
  1873. * 4. Otherwise we conservatively assume 80 columns
  1874. *
  1875. * @param ws should be initialized by caller to zero before first call
  1876. * @param ifd is input file descriptor
  1877. * @param ofd is output file descriptor
  1878. * @return window size
  1879. */
  1880. static struct winsize GetTerminalSize(struct winsize ws, int ifd, int ofd) {
  1881. int x;
  1882. ssize_t n;
  1883. char *p, *s, b[16];
  1884. ioctl(ofd, TIOCGWINSZ, &ws);
  1885. if ((!ws.ws_row && (s = getenv("ROWS")) && (x = ParseUnsigned(s, 0)))) {
  1886. ws.ws_row = x;
  1887. }
  1888. if ((!ws.ws_col && (s = getenv("COLUMNS")) && (x = ParseUnsigned(s, 0)))) {
  1889. ws.ws_col = x;
  1890. }
  1891. if (((!ws.ws_col || !ws.ws_row) && bestlineRead(ifd, 0, 0, 0) != -1 &&
  1892. bestlineWriteStr(ofd, "\0337" /* save position */
  1893. "\033[9979;9979H" /* move cursor to bottom right corner */
  1894. "\033[6n" /* report position */
  1895. "\0338") != -1 && /* restore position */
  1896. (n = bestlineRead(ifd, b, sizeof(b), 0)) != -1 &&
  1897. n && b[0] == 033 && b[1] == '[' && b[n - 1] == 'R')) {
  1898. p = b + 2;
  1899. if ((x = ParseUnsigned(p, &p)))
  1900. ws.ws_row = x;
  1901. if (*p++ == ';' && (x = ParseUnsigned(p, 0)))
  1902. ws.ws_col = x;
  1903. }
  1904. if (!ws.ws_col)
  1905. ws.ws_col = 80;
  1906. if (!ws.ws_row)
  1907. ws.ws_row = 24;
  1908. return ws;
  1909. }
  1910. /* Clear the screen. Used to handle ctrl+l */
  1911. void bestlineClearScreen(int fd) {
  1912. bestlineWriteStr(fd, "\033[H" /* move cursor to top left corner */
  1913. "\033[2J"); /* erase display */
  1914. }
  1915. static void bestlineBeep(void) {
  1916. /* THE TERMINAL BELL IS DEAD - HISTORY HAS KILLED IT */
  1917. }
  1918. static char bestlineGrow(struct bestlineState *ls, size_t n) {
  1919. char *p;
  1920. size_t m;
  1921. m = ls->buflen;
  1922. if (m >= n)
  1923. return 1;
  1924. do
  1925. m += m >> 1;
  1926. while (m < n);
  1927. if (!(p = (char *)realloc(ls->buf, m * sizeof(*ls->buf))))
  1928. return 0;
  1929. ls->buf = p;
  1930. ls->buflen = m;
  1931. return 1;
  1932. }
  1933. /* This is an helper function for bestlineEdit() and is called when the
  1934. * user types the <tab> key in order to complete the string currently in the
  1935. * input.
  1936. *
  1937. * The state of the editing is encapsulated into the pointed bestlineState
  1938. * structure as described in the structure definition. */
  1939. static ssize_t bestlineCompleteLine(struct bestlineState *ls, char *seq, int size) {
  1940. ssize_t nread;
  1941. size_t i, n, stop;
  1942. bestlineCompletions lc;
  1943. struct bestlineState original, saved;
  1944. nread = 0;
  1945. memset(&lc, 0, sizeof(lc));
  1946. completionCallback(ls->buf, ls->pos, &lc);
  1947. if (!lc.len) {
  1948. bestlineBeep();
  1949. } else {
  1950. i = 0;
  1951. stop = 0;
  1952. original = *ls;
  1953. while (!stop) {
  1954. /* Show completion or original buffer */
  1955. if (i < lc.len) {
  1956. saved = *ls;
  1957. ls->len = strlen(lc.cvec[i]);
  1958. ls->pos = original.pos + ls->len - original.len;
  1959. ls->buf = lc.cvec[i];
  1960. bestlineRefreshLine(ls);
  1961. ls->len = saved.len;
  1962. ls->pos = saved.pos;
  1963. ls->buf = saved.buf;
  1964. if (lc.len == 1) {
  1965. nread = 0;
  1966. goto FinishQuickly;
  1967. }
  1968. } else {
  1969. bestlineRefreshLine(ls);
  1970. }
  1971. if ((nread = bestlineRead(ls->ifd, seq, size, ls)) <= 0) {
  1972. bestlineFreeCompletions(&lc);
  1973. return -1;
  1974. }
  1975. switch (seq[0]) {
  1976. case '\t':
  1977. i = (i + 1) % (lc.len + 1);
  1978. if (i == lc.len) {
  1979. bestlineBeep();
  1980. }
  1981. break;
  1982. default:
  1983. if (i < lc.len) {
  1984. FinishQuickly:
  1985. n = strlen(lc.cvec[i]);
  1986. if (bestlineGrow(ls, n + 1)) {
  1987. memcpy(ls->buf, lc.cvec[i], n + 1);
  1988. ls->len = n;
  1989. ls->pos = original.pos + n - original.len;
  1990. }
  1991. }
  1992. stop = 1;
  1993. break;
  1994. }
  1995. }
  1996. }
  1997. bestlineFreeCompletions(&lc);
  1998. return nread;
  1999. }
  2000. static void bestlineEditHistoryGoto(struct bestlineState *l, unsigned i) {
  2001. size_t n;
  2002. if (historylen <= 1)
  2003. return;
  2004. if (i > historylen - 1)
  2005. return;
  2006. i = Max(Min(i, historylen - 1), 0);
  2007. free(history[historylen - 1 - l->hindex]);
  2008. history[historylen - 1 - l->hindex] = strdup(l->buf);
  2009. l->hindex = i;
  2010. n = strlen(history[historylen - 1 - l->hindex]);
  2011. bestlineGrow(l, n + 1);
  2012. n = Min(n, l->buflen - 1);
  2013. memcpy(l->buf, history[historylen - 1 - l->hindex], n);
  2014. l->buf[n] = 0;
  2015. l->len = l->pos = n;
  2016. bestlineRefreshLine(l);
  2017. }
  2018. static void bestlineEditHistoryMove(struct bestlineState *l, int dx) {
  2019. bestlineEditHistoryGoto(l, l->hindex + dx);
  2020. }
  2021. static char *bestlineMakeSearchPrompt(struct abuf *ab, int fail, const char *s, int n) {
  2022. ab->len = 0;
  2023. abAppendw(ab, '(');
  2024. if (fail)
  2025. abAppends(ab, "failed ");
  2026. abAppends(ab, "reverse-i-search `\033[4m");
  2027. abAppend(ab, s, n);
  2028. abAppends(ab, "\033[24m");
  2029. abAppends(ab, s + n);
  2030. abAppendw(ab, Read32le("') "));
  2031. return ab->b;
  2032. }
  2033. static int bestlineSearch(struct bestlineState *l, char *seq, int size) {
  2034. char *p;
  2035. char isstale;
  2036. struct abuf ab;
  2037. struct abuf prompt;
  2038. unsigned i, j, k, matlen;
  2039. const char *oldprompt, *q;
  2040. int rc, fail, added, oldpos, oldindex;
  2041. if (historylen <= 1)
  2042. return 0;
  2043. abInit(&ab);
  2044. abInit(&prompt);
  2045. oldpos = l->pos;
  2046. oldprompt = l->prompt;
  2047. oldindex = l->hindex;
  2048. for (fail = matlen = 0;;) {
  2049. l->prompt = bestlineMakeSearchPrompt(&prompt, fail, ab.b, matlen);
  2050. bestlineRefreshLine(l);
  2051. fail = 1;
  2052. added = 0;
  2053. j = l->pos;
  2054. i = l->hindex;
  2055. rc = bestlineRead(l->ifd, seq, size, l);
  2056. if (rc > 0) {
  2057. if (seq[0] == Ctrl('?') || seq[0] == Ctrl('H')) {
  2058. if (ab.len) {
  2059. --ab.len;
  2060. matlen = Min(matlen, ab.len);
  2061. }
  2062. } else if (seq[0] == Ctrl('R')) {
  2063. if (j) {
  2064. --j;
  2065. } else if (i + 1 < historylen) {
  2066. ++i;
  2067. j = strlen(history[historylen - 1 - i]);
  2068. }
  2069. } else if (seq[0] == Ctrl('G')) {
  2070. bestlineEditHistoryGoto(l, oldindex);
  2071. l->pos = oldpos;
  2072. rc = 0;
  2073. break;
  2074. } else if (IsControl(seq[0])) { /* only sees canonical c0 */
  2075. break;
  2076. } else {
  2077. abAppend(&ab, seq, rc);
  2078. added = rc;
  2079. }
  2080. } else {
  2081. break;
  2082. }
  2083. isstale = 0;
  2084. while (i < historylen) {
  2085. p = history[historylen - 1 - i];
  2086. k = strlen(p);
  2087. if (!isstale) {
  2088. j = Min(k, j + ab.len);
  2089. } else {
  2090. isstale = 0;
  2091. j = k;
  2092. }
  2093. if ((q = FindSubstringReverse(p, j, ab.b, ab.len))) {
  2094. bestlineEditHistoryGoto(l, i);
  2095. l->pos = q - p;
  2096. fail = 0;
  2097. if (added) {
  2098. matlen += added;
  2099. added = 0;
  2100. }
  2101. break;
  2102. } else {
  2103. isstale = 1;
  2104. ++i;
  2105. }
  2106. }
  2107. }
  2108. l->prompt = oldprompt;
  2109. bestlineRefreshLine(l);
  2110. abFree(&prompt);
  2111. abFree(&ab);
  2112. bestlineRefreshLine(l);
  2113. return rc;
  2114. }
  2115. static void bestlineRingFree(void) {
  2116. size_t i;
  2117. for (i = 0; i < BESTLINE_MAX_RING; ++i) {
  2118. if (ring.p[i]) {
  2119. free(ring.p[i]);
  2120. ring.p[i] = 0;
  2121. }
  2122. }
  2123. }
  2124. static void bestlineRingPush(const char *p, size_t n) {
  2125. char *q;
  2126. if (!n)
  2127. return;
  2128. if (!(q = (char *)malloc(n + 1)))
  2129. return;
  2130. ring.i = (ring.i + 1) % BESTLINE_MAX_RING;
  2131. free(ring.p[ring.i]);
  2132. ring.p[ring.i] = (char *)memcpy(q, p, n);
  2133. ring.p[ring.i][n] = 0;
  2134. }
  2135. static void bestlineRingRotate(void) {
  2136. size_t i;
  2137. for (i = 0; i < BESTLINE_MAX_RING; ++i) {
  2138. ring.i = (ring.i - 1) % BESTLINE_MAX_RING;
  2139. if (ring.p[ring.i])
  2140. break;
  2141. }
  2142. }
  2143. static char *bestlineRefreshHints(struct bestlineState *l) {
  2144. char *hint;
  2145. struct abuf ab;
  2146. const char *ansi1 = "\033[90m", *ansi2 = "\033[39m";
  2147. if (!hintsCallback)
  2148. return 0;
  2149. if (!(hint = hintsCallback(l->buf, &ansi1, &ansi2)))
  2150. return 0;
  2151. abInit(&ab);
  2152. if (ansi1)
  2153. abAppends(&ab, ansi1);
  2154. abAppends(&ab, hint);
  2155. if (ansi2)
  2156. abAppends(&ab, ansi2);
  2157. if (freeHintsCallback)
  2158. freeHintsCallback(hint);
  2159. return ab.b;
  2160. }
  2161. static size_t Backward(struct bestlineState *l, size_t pos) {
  2162. if (pos) {
  2163. do
  2164. --pos;
  2165. while (pos && (l->buf[pos] & 0300) == 0200);
  2166. }
  2167. return pos;
  2168. }
  2169. static int bestlineEditMirrorLeft(struct bestlineState *l, int res[2]) {
  2170. unsigned c, pos, left, right, depth, index;
  2171. if ((pos = Backward(l, l->pos))) {
  2172. right = GetUtf8(l->buf + pos, l->len - pos).c;
  2173. if ((left = bestlineMirrorLeft(right))) {
  2174. depth = 0;
  2175. index = pos;
  2176. do {
  2177. pos = Backward(l, pos);
  2178. c = GetUtf8(l->buf + pos, l->len - pos).c;
  2179. if (c == right) {
  2180. ++depth;
  2181. } else if (c == left) {
  2182. if (depth) {
  2183. --depth;
  2184. } else {
  2185. res[0] = pos;
  2186. res[1] = index;
  2187. return 0;
  2188. }
  2189. }
  2190. } while (pos);
  2191. }
  2192. }
  2193. return -1;
  2194. }
  2195. static int bestlineEditMirrorRight(struct bestlineState *l, int res[2]) {
  2196. struct rune rune;
  2197. unsigned pos, left, right, depth, index;
  2198. pos = l->pos;
  2199. rune = GetUtf8(l->buf + pos, l->len - pos);
  2200. left = rune.c;
  2201. if ((right = bestlineMirrorRight(left))) {
  2202. depth = 0;
  2203. index = pos;
  2204. do {
  2205. pos += rune.n;
  2206. rune = GetUtf8(l->buf + pos, l->len - pos);
  2207. if (rune.c == left) {
  2208. ++depth;
  2209. } else if (rune.c == right) {
  2210. if (depth) {
  2211. --depth;
  2212. } else {
  2213. res[0] = index;
  2214. res[1] = pos;
  2215. return 0;
  2216. }
  2217. }
  2218. } while (pos + rune.n < l->len);
  2219. }
  2220. return -1;
  2221. }
  2222. static int bestlineEditMirror(struct bestlineState *l, int res[2]) {
  2223. int rc;
  2224. rc = bestlineEditMirrorLeft(l, res);
  2225. if (rc == -1)
  2226. rc = bestlineEditMirrorRight(l, res);
  2227. return rc;
  2228. }
  2229. static void bestlineRefreshLineImpl(struct bestlineState *l, int force) {
  2230. char *hint;
  2231. char flipit;
  2232. char hasflip;
  2233. char haswides;
  2234. struct abuf ab;
  2235. const char *buf;
  2236. struct rune rune;
  2237. struct winsize oldsize;
  2238. int fd, plen, rows, len, pos;
  2239. unsigned x, xn, yn, width, pwidth;
  2240. int i, t, cx, cy, tn, resized, flip[2];
  2241. /*
  2242. * synchonize the i/o state
  2243. */
  2244. if (ispaused) {
  2245. if (force) {
  2246. bestlineUnpause(l->ofd);
  2247. } else {
  2248. return;
  2249. }
  2250. }
  2251. if (!force && HasPendingInput(l->ifd)) {
  2252. l->dirty = 1;
  2253. return;
  2254. }
  2255. oldsize = l->ws;
  2256. if ((resized = gotwinch) && rawmode != -1) {
  2257. gotwinch = 0;
  2258. l->ws = GetTerminalSize(l->ws, l->ifd, l->ofd);
  2259. }
  2260. hasflip = !l->final && !bestlineEditMirror(l, flip);
  2261. StartOver:
  2262. fd = l->ofd;
  2263. buf = l->buf;
  2264. pos = l->pos;
  2265. len = l->len;
  2266. xn = l->ws.ws_col;
  2267. yn = l->ws.ws_row;
  2268. plen = strlen(l->prompt);
  2269. pwidth = GetMonospaceWidth(l->prompt, plen, 0);
  2270. width = GetMonospaceWidth(buf, len, &haswides);
  2271. /*
  2272. * handle the case where the line is larger than the whole display
  2273. * gnu readline actually isn't able to deal with this situation!!!
  2274. * we kludge xn to address the edge case of wide chars on the edge
  2275. */
  2276. for (tn = xn - haswides * 2;;) {
  2277. if (pwidth + width + 1 < tn * yn)
  2278. break; /* we're fine */
  2279. if (!len || width < 2)
  2280. break; /* we can't do anything */
  2281. if (pwidth + 2 > tn * yn)
  2282. break; /* we can't do anything */
  2283. if (pos > len / 2) {
  2284. /* hide content on the left if we're editing on the right */
  2285. rune = GetUtf8(buf, len);
  2286. buf += rune.n;
  2287. len -= rune.n;
  2288. pos -= rune.n;
  2289. } else {
  2290. /* hide content on the right if we're editing on left */
  2291. t = len;
  2292. while (len && (buf[len - 1] & 0300) == 0200)
  2293. --len;
  2294. if (len)
  2295. --len;
  2296. rune = GetUtf8(buf + len, t - len);
  2297. }
  2298. if ((t = GetMonospaceCharacterWidth(rune.c)) > 0) {
  2299. width -= t;
  2300. }
  2301. }
  2302. pos = Max(0, Min(pos, len));
  2303. /*
  2304. * now generate the terminal codes to update the line
  2305. *
  2306. * since we support unlimited lines it's important that we don't
  2307. * clear the screen before we draw the screen. doing that causes
  2308. * flickering. the key with terminals is to overwrite cells, and
  2309. * then use \e[K and \e[J to clear everything else.
  2310. *
  2311. * we make the assumption that prompts and hints may contain ansi
  2312. * sequences, but the buffer does not.
  2313. *
  2314. * we need to handle the edge case where a wide character like 度
  2315. * might be at the edge of the window, when there's one cell left.
  2316. * so we can't use division based on string width to compute the
  2317. * coordinates and have to track it as we go.
  2318. */
  2319. cy = -1;
  2320. cx = -1;
  2321. rows = 1;
  2322. abInit(&ab);
  2323. abAppendw(&ab, '\r'); /* start of line */
  2324. if (l->rows - l->oldpos - 1 > 0) {
  2325. abAppends(&ab, "\033[");
  2326. abAppendu(&ab, l->rows - l->oldpos - 1);
  2327. abAppendw(&ab, 'A'); /* cursor up clamped */
  2328. }
  2329. abAppends(&ab, l->prompt);
  2330. x = pwidth;
  2331. for (i = 0; i < len; i += rune.n) {
  2332. rune = GetUtf8(buf + i, len - i);
  2333. if (x && x + rune.n > xn) {
  2334. if (cy >= 0)
  2335. ++cy;
  2336. if (x < xn) {
  2337. abAppends(&ab, "\033[K"); /* clear line forward */
  2338. }
  2339. abAppends(&ab, "\r" /* start of line */
  2340. "\n"); /* cursor down unclamped */
  2341. ++rows;
  2342. x = 0;
  2343. }
  2344. if (i == pos) {
  2345. cy = 0;
  2346. cx = x;
  2347. }
  2348. if (maskmode) {
  2349. abAppendw(&ab, '*');
  2350. } else {
  2351. flipit = hasflip && (i == flip[0] || i == flip[1]);
  2352. if (flipit)
  2353. abAppends(&ab, "\033[1m");
  2354. abAppendw(&ab, EncodeUtf8(rune.c));
  2355. if (flipit)
  2356. abAppends(&ab, "\033[22m");
  2357. }
  2358. t = GetMonospaceCharacterWidth(rune.c);
  2359. t = Max(0, t);
  2360. x += t;
  2361. }
  2362. if (!l->final && (hint = bestlineRefreshHints(l))) {
  2363. if (GetMonospaceWidth(hint, strlen(hint), 0) < xn - x) {
  2364. if (cx < 0) {
  2365. cx = x;
  2366. }
  2367. abAppends(&ab, hint);
  2368. }
  2369. free(hint);
  2370. }
  2371. abAppendw(&ab, Read32le("\033[J")); /* erase display forwards */
  2372. /*
  2373. * if we are at the very end of the screen with our prompt, we need
  2374. * to emit a newline and move the prompt to the first column.
  2375. */
  2376. if (pos && pos == len && x >= xn) {
  2377. abAppendw(&ab, Read32le("\n\r\0"));
  2378. ++rows;
  2379. }
  2380. /*
  2381. * move cursor to right position
  2382. */
  2383. if (cy > 0) {
  2384. abAppends(&ab, "\033[");
  2385. abAppendu(&ab, cy);
  2386. abAppendw(&ab, 'A'); /* cursor up */
  2387. }
  2388. if (cx > 0) {
  2389. abAppendw(&ab, Read32le("\r\033["));
  2390. abAppendu(&ab, cx);
  2391. abAppendw(&ab, 'C'); /* cursor right */
  2392. } else if (!cx) {
  2393. abAppendw(&ab, '\r'); /* start */
  2394. }
  2395. /*
  2396. * now get ready to progress state
  2397. * we use a mostly correct kludge when the tty resizes
  2398. */
  2399. l->rows = rows;
  2400. if (resized && oldsize.ws_col > l->ws.ws_col) {
  2401. resized = 0;
  2402. abFree(&ab);
  2403. goto StartOver;
  2404. }
  2405. l->dirty = 0;
  2406. l->oldpos = Max(0, cy);
  2407. /*
  2408. * send codes to terminal
  2409. */
  2410. bestlineWrite(fd, ab.b, ab.len);
  2411. abFree(&ab);
  2412. }
  2413. static void bestlineRefreshLine(struct bestlineState *l) {
  2414. bestlineRefreshLineImpl(l, 0);
  2415. }
  2416. static void bestlineRefreshLineForce(struct bestlineState *l) {
  2417. bestlineRefreshLineImpl(l, 1);
  2418. }
  2419. static void bestlineEditInsert(struct bestlineState *l, const char *p, size_t n) {
  2420. if (!bestlineGrow(l, l->len + n + 1))
  2421. return;
  2422. memmove(l->buf + l->pos + n, l->buf + l->pos, l->len - l->pos);
  2423. memcpy(l->buf + l->pos, p, n);
  2424. l->pos += n;
  2425. l->len += n;
  2426. l->buf[l->len] = 0;
  2427. bestlineRefreshLine(l);
  2428. }
  2429. static void bestlineEditHome(struct bestlineState *l) {
  2430. l->pos = 0;
  2431. bestlineRefreshLine(l);
  2432. }
  2433. static void bestlineEditEnd(struct bestlineState *l) {
  2434. l->pos = l->len;
  2435. bestlineRefreshLine(l);
  2436. }
  2437. static void bestlineEditUp(struct bestlineState *l) {
  2438. bestlineEditHistoryMove(l, BESTLINE_HISTORY_PREV);
  2439. }
  2440. static void bestlineEditDown(struct bestlineState *l) {
  2441. bestlineEditHistoryMove(l, BESTLINE_HISTORY_NEXT);
  2442. }
  2443. static void bestlineEditBof(struct bestlineState *l) {
  2444. bestlineEditHistoryGoto(l, historylen - 1);
  2445. }
  2446. static void bestlineEditEof(struct bestlineState *l) {
  2447. bestlineEditHistoryGoto(l, 0);
  2448. }
  2449. static void bestlineEditRefresh(struct bestlineState *l) {
  2450. bestlineClearScreen(l->ofd);
  2451. bestlineRefreshLine(l);
  2452. }
  2453. static size_t Forward(struct bestlineState *l, size_t pos) {
  2454. return pos + GetUtf8(l->buf + pos, l->len - pos).n;
  2455. }
  2456. static size_t Backwards(struct bestlineState *l, size_t pos, char pred(unsigned)) {
  2457. size_t i;
  2458. struct rune r;
  2459. while (pos) {
  2460. i = Backward(l, pos);
  2461. r = GetUtf8(l->buf + i, l->len - i);
  2462. if (pred(r.c)) {
  2463. pos = i;
  2464. } else {
  2465. break;
  2466. }
  2467. }
  2468. return pos;
  2469. }
  2470. static size_t Forwards(struct bestlineState *l, size_t pos, char pred(unsigned)) {
  2471. struct rune r;
  2472. while (pos < l->len) {
  2473. r = GetUtf8(l->buf + pos, l->len - pos);
  2474. if (pred(r.c)) {
  2475. pos += r.n;
  2476. } else {
  2477. break;
  2478. }
  2479. }
  2480. return pos;
  2481. }
  2482. static size_t ForwardWord(struct bestlineState *l, size_t pos) {
  2483. pos = Forwards(l, pos, bestlineIsSeparator);
  2484. pos = Forwards(l, pos, bestlineNotSeparator);
  2485. return pos;
  2486. }
  2487. static size_t BackwardWord(struct bestlineState *l, size_t pos) {
  2488. pos = Backwards(l, pos, bestlineIsSeparator);
  2489. pos = Backwards(l, pos, bestlineNotSeparator);
  2490. return pos;
  2491. }
  2492. static size_t EscapeWord(struct bestlineState *l, size_t i) {
  2493. size_t j;
  2494. struct rune r;
  2495. for (; i && i < l->len; i += r.n) {
  2496. if (i < l->len) {
  2497. r = GetUtf8(l->buf + i, l->len - i);
  2498. if (bestlineIsSeparator(r.c))
  2499. break;
  2500. }
  2501. if ((j = i)) {
  2502. do
  2503. --j;
  2504. while (j && (l->buf[j] & 0300) == 0200);
  2505. r = GetUtf8(l->buf + j, l->len - j);
  2506. if (bestlineIsSeparator(r.c))
  2507. break;
  2508. }
  2509. }
  2510. return i;
  2511. }
  2512. static void bestlineEditLeft(struct bestlineState *l) {
  2513. l->pos = Backward(l, l->pos);
  2514. bestlineRefreshLine(l);
  2515. }
  2516. static void bestlineEditRight(struct bestlineState *l) {
  2517. if (l->pos == l->len)
  2518. return;
  2519. do
  2520. l->pos++;
  2521. while (l->pos < l->len && (l->buf[l->pos] & 0300) == 0200);
  2522. bestlineRefreshLine(l);
  2523. }
  2524. static void bestlineEditLeftWord(struct bestlineState *l) {
  2525. l->pos = BackwardWord(l, l->pos);
  2526. bestlineRefreshLine(l);
  2527. }
  2528. static void bestlineEditRightWord(struct bestlineState *l) {
  2529. l->pos = ForwardWord(l, l->pos);
  2530. bestlineRefreshLine(l);
  2531. }
  2532. static void bestlineEditLeftExpr(struct bestlineState *l) {
  2533. int mark[2];
  2534. l->pos = Backwards(l, l->pos, bestlineIsXeparator);
  2535. if (!bestlineEditMirrorLeft(l, mark)) {
  2536. l->pos = mark[0];
  2537. } else {
  2538. l->pos = Backwards(l, l->pos, bestlineNotSeparator);
  2539. }
  2540. bestlineRefreshLine(l);
  2541. }
  2542. static void bestlineEditRightExpr(struct bestlineState *l) {
  2543. int mark[2];
  2544. l->pos = Forwards(l, l->pos, bestlineIsXeparator);
  2545. if (!bestlineEditMirrorRight(l, mark)) {
  2546. l->pos = Forward(l, mark[1]);
  2547. } else {
  2548. l->pos = Forwards(l, l->pos, bestlineNotSeparator);
  2549. }
  2550. bestlineRefreshLine(l);
  2551. }
  2552. static void bestlineEditDelete(struct bestlineState *l) {
  2553. size_t i;
  2554. if (l->pos == l->len)
  2555. return;
  2556. i = Forward(l, l->pos);
  2557. memmove(l->buf + l->pos, l->buf + i, l->len - i + 1);
  2558. l->len -= i - l->pos;
  2559. bestlineRefreshLine(l);
  2560. }
  2561. static void bestlineEditRubout(struct bestlineState *l) {
  2562. size_t i;
  2563. if (!l->pos)
  2564. return;
  2565. i = Backward(l, l->pos);
  2566. memmove(l->buf + i, l->buf + l->pos, l->len - l->pos + 1);
  2567. l->len -= l->pos - i;
  2568. l->pos = i;
  2569. bestlineRefreshLine(l);
  2570. }
  2571. static void bestlineEditDeleteWord(struct bestlineState *l) {
  2572. size_t i;
  2573. if (l->pos == l->len)
  2574. return;
  2575. i = ForwardWord(l, l->pos);
  2576. bestlineRingPush(l->buf + l->pos, i - l->pos);
  2577. memmove(l->buf + l->pos, l->buf + i, l->len - i + 1);
  2578. l->len -= i - l->pos;
  2579. bestlineRefreshLine(l);
  2580. }
  2581. static void bestlineEditRuboutWord(struct bestlineState *l) {
  2582. size_t i;
  2583. if (!l->pos)
  2584. return;
  2585. i = BackwardWord(l, l->pos);
  2586. bestlineRingPush(l->buf + i, l->pos - i);
  2587. memmove(l->buf + i, l->buf + l->pos, l->len - l->pos + 1);
  2588. l->len -= l->pos - i;
  2589. l->pos = i;
  2590. bestlineRefreshLine(l);
  2591. }
  2592. static void bestlineEditXlatWord(struct bestlineState *l, unsigned xlat(unsigned)) {
  2593. unsigned c;
  2594. size_t i, j;
  2595. struct rune r;
  2596. struct abuf ab;
  2597. abInit(&ab);
  2598. i = Forwards(l, l->pos, bestlineIsSeparator);
  2599. for (j = i; j < l->len; j += r.n) {
  2600. r = GetUtf8(l->buf + j, l->len - j);
  2601. if (bestlineIsSeparator(r.c))
  2602. break;
  2603. if ((c = xlat(r.c)) != r.c) {
  2604. abAppendw(&ab, EncodeUtf8(c));
  2605. } else { /* avoid canonicalization */
  2606. abAppend(&ab, l->buf + j, r.n);
  2607. }
  2608. }
  2609. if (ab.len && bestlineGrow(l, i + ab.len + l->len - j + 1)) {
  2610. l->pos = i + ab.len;
  2611. abAppend(&ab, l->buf + j, l->len - j);
  2612. l->len = i + ab.len;
  2613. memcpy(l->buf + i, ab.b, ab.len + 1);
  2614. bestlineRefreshLine(l);
  2615. }
  2616. abFree(&ab);
  2617. }
  2618. static void bestlineEditLowercaseWord(struct bestlineState *l) {
  2619. bestlineEditXlatWord(l, bestlineLowercase);
  2620. }
  2621. static void bestlineEditUppercaseWord(struct bestlineState *l) {
  2622. bestlineEditXlatWord(l, bestlineUppercase);
  2623. }
  2624. static void bestlineEditCapitalizeWord(struct bestlineState *l) {
  2625. iscapital = 0;
  2626. bestlineEditXlatWord(l, Capitalize);
  2627. }
  2628. static void bestlineEditKillLeft(struct bestlineState *l) {
  2629. size_t diff, old_pos;
  2630. bestlineRingPush(l->buf, l->pos);
  2631. old_pos = l->pos;
  2632. l->pos = 0;
  2633. diff = old_pos - l->pos;
  2634. memmove(l->buf + l->pos, l->buf + old_pos, l->len - old_pos + 1);
  2635. l->len -= diff;
  2636. bestlineRefreshLine(l);
  2637. }
  2638. static void bestlineEditKillRight(struct bestlineState *l) {
  2639. bestlineRingPush(l->buf + l->pos, l->len - l->pos);
  2640. l->buf[l->pos] = '\0';
  2641. l->len = l->pos;
  2642. bestlineRefreshLine(l);
  2643. }
  2644. static void bestlineEditYank(struct bestlineState *l) {
  2645. char *p;
  2646. size_t n;
  2647. if (!ring.p[ring.i])
  2648. return;
  2649. n = strlen(ring.p[ring.i]);
  2650. if (!bestlineGrow(l, l->len + n + 1))
  2651. return;
  2652. if (!(p = (char *)malloc(l->len - l->pos + 1)))
  2653. return;
  2654. memcpy(p, l->buf + l->pos, l->len - l->pos + 1);
  2655. memcpy(l->buf + l->pos, ring.p[ring.i], n);
  2656. memcpy(l->buf + l->pos + n, p, l->len - l->pos + 1);
  2657. free(p);
  2658. l->yi = l->pos;
  2659. l->yj = l->pos + n;
  2660. l->pos += n;
  2661. l->len += n;
  2662. bestlineRefreshLine(l);
  2663. }
  2664. static void bestlineEditRotate(struct bestlineState *l) {
  2665. if ((l->seq[1][0] == Ctrl('Y') || (l->seq[1][0] == 033 && l->seq[1][1] == 'y'))) {
  2666. if (l->yi < l->len && l->yj <= l->len) {
  2667. memmove(l->buf + l->yi, l->buf + l->yj, l->len - l->yj + 1);
  2668. l->len -= l->yj - l->yi;
  2669. l->pos -= l->yj - l->yi;
  2670. }
  2671. bestlineRingRotate();
  2672. bestlineEditYank(l);
  2673. }
  2674. }
  2675. static void bestlineEditTranspose(struct bestlineState *l) {
  2676. char *q, *p;
  2677. size_t a, b, c;
  2678. b = l->pos;
  2679. if (b == l->len)
  2680. --b;
  2681. a = Backward(l, b);
  2682. c = Forward(l, b);
  2683. if (!(a < b && b < c))
  2684. return;
  2685. p = q = (char *)malloc(c - a);
  2686. p = Copy(p, l->buf + b, c - b);
  2687. p = Copy(p, l->buf + a, b - a);
  2688. assert((size_t)(p - q) == c - a);
  2689. memcpy(l->buf + a, q, p - q);
  2690. l->pos = c;
  2691. free(q);
  2692. bestlineRefreshLine(l);
  2693. }
  2694. static void bestlineEditTransposeWords(struct bestlineState *l) {
  2695. char *q, *p;
  2696. size_t i, pi, xi, xj, yi, yj;
  2697. i = l->pos;
  2698. if (i == l->len) {
  2699. i = Backwards(l, i, bestlineIsSeparator);
  2700. i = Backwards(l, i, bestlineNotSeparator);
  2701. }
  2702. pi = EscapeWord(l, i);
  2703. xj = Backwards(l, pi, bestlineIsSeparator);
  2704. xi = Backwards(l, xj, bestlineNotSeparator);
  2705. yi = Forwards(l, pi, bestlineIsSeparator);
  2706. yj = Forwards(l, yi, bestlineNotSeparator);
  2707. if (!(xi < xj && xj < yi && yi < yj))
  2708. return;
  2709. p = q = (char *)malloc(yj - xi);
  2710. p = Copy(p, l->buf + yi, yj - yi);
  2711. p = Copy(p, l->buf + xj, yi - xj);
  2712. p = Copy(p, l->buf + xi, xj - xi);
  2713. assert((size_t)(p - q) == yj - xi);
  2714. memcpy(l->buf + xi, q, p - q);
  2715. l->pos = yj;
  2716. free(q);
  2717. bestlineRefreshLine(l);
  2718. }
  2719. static void bestlineEditSqueeze(struct bestlineState *l) {
  2720. size_t i, j;
  2721. i = Backwards(l, l->pos, bestlineIsSeparator);
  2722. j = Forwards(l, l->pos, bestlineIsSeparator);
  2723. if (!(i < j))
  2724. return;
  2725. memmove(l->buf + i, l->buf + j, l->len - j + 1);
  2726. l->len -= j - i;
  2727. l->pos = i;
  2728. bestlineRefreshLine(l);
  2729. }
  2730. static void bestlineEditMark(struct bestlineState *l) {
  2731. l->mark = l->pos;
  2732. }
  2733. static void bestlineEditGoto(struct bestlineState *l) {
  2734. if (l->mark > l->len)
  2735. return;
  2736. l->pos = Min(l->mark, l->len);
  2737. bestlineRefreshLine(l);
  2738. }
  2739. static size_t bestlineEscape(char *d, const char *s, size_t n) {
  2740. char *p;
  2741. size_t i;
  2742. unsigned c, w, l;
  2743. for (p = d, l = i = 0; i < n; ++i) {
  2744. switch ((c = s[i] & 255)) {
  2745. Case('\a', w = Read16le("\\a"));
  2746. Case('\b', w = Read16le("\\b"));
  2747. Case('\t', w = Read16le("\\t"));
  2748. Case('\n', w = Read16le("\\n"));
  2749. Case('\v', w = Read16le("\\v"));
  2750. Case('\f', w = Read16le("\\f"));
  2751. Case('\r', w = Read16le("\\r"));
  2752. Case('"', w = Read16le("\\\""));
  2753. Case('\'', w = Read16le("\\\'"));
  2754. Case('\\', w = Read16le("\\\\"));
  2755. default:
  2756. if (c <= 0x1F || c == 0x7F || (c == '?' && l == '?')) {
  2757. w = Read16le("\\x");
  2758. w |= "0123456789abcdef"[(c & 0xF0) >> 4] << 020;
  2759. w |= "0123456789abcdef"[(c & 0x0F) >> 0] << 030;
  2760. } else {
  2761. w = c;
  2762. }
  2763. break;
  2764. }
  2765. p[0] = (w & 0x000000ff) >> 000;
  2766. p[1] = (w & 0x0000ff00) >> 010;
  2767. p[2] = (w & 0x00ff0000) >> 020;
  2768. p[3] = (w & 0xff000000) >> 030;
  2769. p += (Bsr(w) >> 3) + 1;
  2770. l = w;
  2771. }
  2772. return p - d;
  2773. }
  2774. static void bestlineEditInsertEscape(struct bestlineState *l) {
  2775. size_t m;
  2776. ssize_t n;
  2777. char seq[16];
  2778. char esc[sizeof(seq) * 4];
  2779. if ((n = bestlineRead(l->ifd, seq, sizeof(seq), l)) > 0) {
  2780. m = bestlineEscape(esc, seq, n);
  2781. bestlineEditInsert(l, esc, m);
  2782. }
  2783. }
  2784. static void bestlineEditInterrupt(void) {
  2785. gotint = SIGINT;
  2786. }
  2787. static void bestlineEditQuit(void) {
  2788. gotint = SIGQUIT;
  2789. }
  2790. static void bestlineEditSuspend(void) {
  2791. raise(SIGSTOP);
  2792. }
  2793. static void bestlineEditPause(struct bestlineState *l) {
  2794. tcflow(l->ofd, TCOOFF);
  2795. ispaused = 1;
  2796. }
  2797. static void bestlineEditCtrlq(struct bestlineState *l) {
  2798. if (ispaused) {
  2799. bestlineUnpause(l->ofd);
  2800. bestlineRefreshLineForce(l);
  2801. } else {
  2802. bestlineEditInsertEscape(l);
  2803. }
  2804. }
  2805. /**
  2806. * Moves last item inside current s-expression to outside, e.g.
  2807. *
  2808. * (a| b c)
  2809. * (a| b) c
  2810. *
  2811. * The cursor position changes only if a paren is moved before it:
  2812. *
  2813. * (a b c |)
  2814. * (a b) c |
  2815. *
  2816. * To accommodate non-LISP languages we connect unspaced outer symbols:
  2817. *
  2818. * f(a,| b, g())
  2819. * f(a,| b), g()
  2820. *
  2821. * Our standard keybinding is ALT-SHIFT-B.
  2822. */
  2823. static void bestlineEditBarf(struct bestlineState *l) {
  2824. struct rune r;
  2825. unsigned long w;
  2826. size_t i, pos, depth = 0;
  2827. unsigned lhs, rhs, end, *stack = 0;
  2828. /* go as far right within current s-expr as possible */
  2829. for (pos = l->pos;; pos += r.n) {
  2830. if (pos == l->len)
  2831. goto Finish;
  2832. r = GetUtf8(l->buf + pos, l->len - pos);
  2833. if (depth) {
  2834. if (r.c == stack[depth - 1]) {
  2835. --depth;
  2836. }
  2837. } else {
  2838. if ((rhs = bestlineMirrorRight(r.c))) {
  2839. stack = (unsigned *)realloc(stack, ++depth * sizeof(*stack));
  2840. stack[depth - 1] = rhs;
  2841. } else if (bestlineMirrorLeft(r.c)) {
  2842. end = pos;
  2843. break;
  2844. }
  2845. }
  2846. }
  2847. /* go back one item */
  2848. pos = Backwards(l, pos, bestlineIsXeparator);
  2849. for (;; pos = i) {
  2850. if (!pos)
  2851. goto Finish;
  2852. i = Backward(l, pos);
  2853. r = GetUtf8(l->buf + i, l->len - i);
  2854. if (depth) {
  2855. if (r.c == stack[depth - 1]) {
  2856. --depth;
  2857. }
  2858. } else {
  2859. if ((lhs = bestlineMirrorLeft(r.c))) {
  2860. stack = (unsigned *)realloc(stack, ++depth * sizeof(*stack));
  2861. stack[depth - 1] = lhs;
  2862. } else if (bestlineIsSeparator(r.c)) {
  2863. break;
  2864. }
  2865. }
  2866. }
  2867. pos = Backwards(l, pos, bestlineIsXeparator);
  2868. /* now move the text */
  2869. r = GetUtf8(l->buf + end, l->len - end);
  2870. memmove(l->buf + pos + r.n, l->buf + pos, end - pos);
  2871. w = EncodeUtf8(r.c);
  2872. for (i = 0; i < r.n; ++i) {
  2873. l->buf[pos + i] = w;
  2874. w >>= 8;
  2875. }
  2876. if (l->pos > pos) {
  2877. l->pos += r.n;
  2878. }
  2879. bestlineRefreshLine(l);
  2880. Finish:
  2881. free(stack);
  2882. }
  2883. /**
  2884. * Moves first item outside current s-expression to inside, e.g.
  2885. *
  2886. * (a| b) c d
  2887. * (a| b c) d
  2888. *
  2889. * To accommodate non-LISP languages we connect unspaced outer symbols:
  2890. *
  2891. * f(a,| b), g()
  2892. * f(a,| b, g())
  2893. *
  2894. * Our standard keybinding is ALT-SHIFT-S.
  2895. */
  2896. static void bestlineEditSlurp(struct bestlineState *l) {
  2897. char rp[6];
  2898. struct rune r;
  2899. size_t pos, depth = 0;
  2900. unsigned rhs, point = 0, start = 0, *stack = 0;
  2901. /* go to outside edge of current s-expr */
  2902. for (pos = l->pos; pos < l->len; pos += r.n) {
  2903. r = GetUtf8(l->buf + pos, l->len - pos);
  2904. if (depth) {
  2905. if (r.c == stack[depth - 1]) {
  2906. --depth;
  2907. }
  2908. } else {
  2909. if ((rhs = bestlineMirrorRight(r.c))) {
  2910. stack = (unsigned *)realloc(stack, ++depth * sizeof(*stack));
  2911. stack[depth - 1] = rhs;
  2912. } else if (bestlineMirrorLeft(r.c)) {
  2913. point = pos;
  2914. pos += r.n;
  2915. start = pos;
  2916. break;
  2917. }
  2918. }
  2919. }
  2920. /* go forward one item */
  2921. pos = Forwards(l, pos, bestlineIsXeparator);
  2922. for (; pos < l->len; pos += r.n) {
  2923. r = GetUtf8(l->buf + pos, l->len - pos);
  2924. if (depth) {
  2925. if (r.c == stack[depth - 1]) {
  2926. --depth;
  2927. }
  2928. } else {
  2929. if ((rhs = bestlineMirrorRight(r.c))) {
  2930. stack = (unsigned *)realloc(stack, ++depth * sizeof(*stack));
  2931. stack[depth - 1] = rhs;
  2932. } else if (bestlineIsSeparator(r.c)) {
  2933. break;
  2934. }
  2935. }
  2936. }
  2937. /* now move the text */
  2938. memcpy(rp, l->buf + point, start - point);
  2939. memmove(l->buf + point, l->buf + start, pos - start);
  2940. memcpy(l->buf + pos - (start - point), rp, start - point);
  2941. bestlineRefreshLine(l);
  2942. free(stack);
  2943. }
  2944. static void bestlineEditRaise(struct bestlineState *l) {
  2945. (void)l;
  2946. }
  2947. static char IsBalanced(struct abuf *buf) {
  2948. unsigned i, d;
  2949. for (d = i = 0; i < buf->len; ++i) {
  2950. if (buf->b[i] == '(')
  2951. ++d;
  2952. else if (d > 0 && buf->b[i] == ')')
  2953. --d;
  2954. }
  2955. return d == 0;
  2956. }
  2957. /**
  2958. * Runs bestline engine.
  2959. *
  2960. * This function is the core of the line editing capability of bestline.
  2961. * It expects 'fd' to be already in "raw mode" so that every key pressed
  2962. * will be returned ASAP to read().
  2963. *
  2964. * The resulting string is put into 'buf' when the user type enter, or
  2965. * when ctrl+d is typed.
  2966. *
  2967. * Returns chomped character count in buf >=0 or -1 on eof / error
  2968. */
  2969. static ssize_t bestlineEdit(int stdin_fd, int stdout_fd, const char *prompt, const char *init,
  2970. char **obuf) {
  2971. ssize_t rc;
  2972. char seq[16];
  2973. const char *promptnotnull, *promptlastnl;
  2974. size_t nread;
  2975. int pastemode;
  2976. struct rune rune;
  2977. unsigned long long w;
  2978. struct bestlineState l;
  2979. pastemode = 0;
  2980. memset(&l, 0, sizeof(l));
  2981. if (!(l.buf = (char *)malloc((l.buflen = 32))))
  2982. return -1;
  2983. l.buf[0] = 0;
  2984. l.ifd = stdin_fd;
  2985. l.ofd = stdout_fd;
  2986. promptnotnull = prompt ? prompt : "";
  2987. promptlastnl = strrchr(promptnotnull, '\n');
  2988. l.prompt = promptlastnl ? promptlastnl + 1 : promptnotnull;
  2989. l.ws = GetTerminalSize(l.ws, l.ifd, l.ofd);
  2990. abInit(&l.full);
  2991. bestlineHistoryAdd("");
  2992. bestlineWriteStr(l.ofd, promptnotnull);
  2993. init = init ? init : "";
  2994. bestlineEditInsert(&l, init, strlen(init));
  2995. while (1) {
  2996. if (l.dirty)
  2997. bestlineRefreshLineForce(&l);
  2998. rc = bestlineRead(l.ifd, seq, sizeof(seq), &l);
  2999. if (rc > 0) {
  3000. if (seq[0] == Ctrl('R')) {
  3001. rc = bestlineSearch(&l, seq, sizeof(seq));
  3002. if (!rc)
  3003. continue;
  3004. } else if (seq[0] == '\t' && completionCallback) {
  3005. rc = bestlineCompleteLine(&l, seq, sizeof(seq));
  3006. if (!rc)
  3007. continue;
  3008. }
  3009. }
  3010. if (rc > 0) {
  3011. nread = rc;
  3012. } else if (!rc && l.len) {
  3013. nread = 1;
  3014. seq[0] = '\r';
  3015. seq[1] = 0;
  3016. } else {
  3017. if (historylen) {
  3018. free(history[--historylen]);
  3019. history[historylen] = 0;
  3020. }
  3021. free(l.buf);
  3022. abFree(&l.full);
  3023. return -1;
  3024. }
  3025. switch (seq[0]) {
  3026. Case(Ctrl('P'), bestlineEditUp(&l));
  3027. Case(Ctrl('E'), bestlineEditEnd(&l));
  3028. Case(Ctrl('N'), bestlineEditDown(&l));
  3029. Case(Ctrl('A'), bestlineEditHome(&l));
  3030. Case(Ctrl('B'), bestlineEditLeft(&l));
  3031. Case(Ctrl('@'), bestlineEditMark(&l));
  3032. Case(Ctrl('Y'), bestlineEditYank(&l));
  3033. Case(Ctrl('Q'), bestlineEditCtrlq(&l));
  3034. Case(Ctrl('F'), bestlineEditRight(&l));
  3035. Case(Ctrl('\\'), bestlineEditQuit());
  3036. Case(Ctrl('S'), bestlineEditPause(&l));
  3037. Case(Ctrl('?'), bestlineEditRubout(&l));
  3038. Case(Ctrl('H'), bestlineEditRubout(&l));
  3039. Case(Ctrl('L'), bestlineEditRefresh(&l));
  3040. Case(Ctrl('Z'), bestlineEditSuspend());
  3041. Case(Ctrl('U'), bestlineEditKillLeft(&l));
  3042. Case(Ctrl('T'), bestlineEditTranspose(&l));
  3043. Case(Ctrl('K'), bestlineEditKillRight(&l));
  3044. Case(Ctrl('W'), bestlineEditRuboutWord(&l));
  3045. case Ctrl('C'):
  3046. if (emacsmode) {
  3047. if (bestlineRead(l.ifd, seq, sizeof(seq), &l) != 1)
  3048. break;
  3049. switch (seq[0]) {
  3050. Case(Ctrl('C'), bestlineEditInterrupt());
  3051. Case(Ctrl('B'), bestlineEditBarf(&l));
  3052. Case(Ctrl('S'), bestlineEditSlurp(&l));
  3053. Case(Ctrl('R'), bestlineEditRaise(&l));
  3054. default:
  3055. break;
  3056. }
  3057. } else {
  3058. bestlineEditInterrupt();
  3059. }
  3060. break;
  3061. case Ctrl('X'):
  3062. if (l.seq[1][0] == Ctrl('X')) {
  3063. bestlineEditGoto(&l);
  3064. }
  3065. break;
  3066. case Ctrl('D'):
  3067. if (l.len) {
  3068. bestlineEditDelete(&l);
  3069. } else {
  3070. if (historylen) {
  3071. free(history[--historylen]);
  3072. history[historylen] = 0;
  3073. }
  3074. free(l.buf);
  3075. abFree(&l.full);
  3076. return -1;
  3077. }
  3078. break;
  3079. case '\n':
  3080. l.final = 1;
  3081. bestlineEditEnd(&l);
  3082. bestlineRefreshLineForce(&l);
  3083. l.final = 0;
  3084. abAppend(&l.full, l.buf, l.len);
  3085. l.prompt = "... ";
  3086. abAppends(&l.full, "\n");
  3087. l.len = 0;
  3088. l.pos = 0;
  3089. bestlineWriteStr(stdout_fd, "\r\n");
  3090. bestlineRefreshLineForce(&l);
  3091. break;
  3092. case '\r': {
  3093. char is_finished = 1;
  3094. char needs_strip = 0;
  3095. if (historylen) {
  3096. free(history[--historylen]);
  3097. history[historylen] = 0;
  3098. }
  3099. l.final = 1;
  3100. bestlineEditEnd(&l);
  3101. bestlineRefreshLineForce(&l);
  3102. l.final = 0;
  3103. abAppend(&l.full, l.buf, l.len);
  3104. if (pastemode)
  3105. is_finished = 0;
  3106. if (balancemode)
  3107. if (!IsBalanced(&l.full))
  3108. is_finished = 0;
  3109. if (llamamode)
  3110. if (StartsWith(l.full.b, "\"\"\""))
  3111. needs_strip = is_finished = l.full.len > 6 && EndsWith(l.full.b, "\"\"\"");
  3112. if (is_finished) {
  3113. if (needs_strip) {
  3114. int len = l.full.len - 6;
  3115. *obuf = strndup(l.full.b + 3, len);
  3116. abFree(&l.full);
  3117. free(l.buf);
  3118. return len;
  3119. } else {
  3120. *obuf = l.full.b;
  3121. free(l.buf);
  3122. return l.full.len;
  3123. }
  3124. } else {
  3125. l.prompt = "... ";
  3126. abAppends(&l.full, "\n");
  3127. l.len = 0;
  3128. l.pos = 0;
  3129. bestlineWriteStr(stdout_fd, "\r\n");
  3130. bestlineRefreshLineForce(&l);
  3131. }
  3132. break;
  3133. }
  3134. case 033:
  3135. if (nread < 2)
  3136. break;
  3137. switch (seq[1]) {
  3138. Case('<', bestlineEditBof(&l));
  3139. Case('>', bestlineEditEof(&l));
  3140. Case('B', bestlineEditBarf(&l));
  3141. Case('S', bestlineEditSlurp(&l));
  3142. Case('R', bestlineEditRaise(&l));
  3143. Case('y', bestlineEditRotate(&l));
  3144. Case('\\', bestlineEditSqueeze(&l));
  3145. Case('b', bestlineEditLeftWord(&l));
  3146. Case('f', bestlineEditRightWord(&l));
  3147. Case('h', bestlineEditRuboutWord(&l));
  3148. Case('d', bestlineEditDeleteWord(&l));
  3149. Case('l', bestlineEditLowercaseWord(&l));
  3150. Case('u', bestlineEditUppercaseWord(&l));
  3151. Case('c', bestlineEditCapitalizeWord(&l));
  3152. Case('t', bestlineEditTransposeWords(&l));
  3153. Case(Ctrl('B'), bestlineEditLeftExpr(&l));
  3154. Case(Ctrl('F'), bestlineEditRightExpr(&l));
  3155. Case(Ctrl('H'), bestlineEditRuboutWord(&l));
  3156. case '[':
  3157. if (nread == 6 && !memcmp(seq, "\033[200~", 6)) {
  3158. pastemode = 1;
  3159. break;
  3160. }
  3161. if (nread == 6 && !memcmp(seq, "\033[201~", 6)) {
  3162. pastemode = 0;
  3163. break;
  3164. }
  3165. if (nread < 3)
  3166. break;
  3167. if (seq[2] >= '0' && seq[2] <= '9') {
  3168. if (nread < 4)
  3169. break;
  3170. if (seq[3] == '~') {
  3171. switch (seq[2]) {
  3172. Case('1', bestlineEditHome(&l)); /* \e[1~ */
  3173. Case('3', bestlineEditDelete(&l)); /* \e[3~ */
  3174. Case('4', bestlineEditEnd(&l)); /* \e[4~ */
  3175. default:
  3176. break;
  3177. }
  3178. }
  3179. } else {
  3180. switch (seq[2]) {
  3181. Case('A', bestlineEditUp(&l));
  3182. Case('B', bestlineEditDown(&l));
  3183. Case('C', bestlineEditRight(&l));
  3184. Case('D', bestlineEditLeft(&l));
  3185. Case('H', bestlineEditHome(&l));
  3186. Case('F', bestlineEditEnd(&l));
  3187. default:
  3188. break;
  3189. }
  3190. }
  3191. break;
  3192. case 'O':
  3193. if (nread < 3)
  3194. break;
  3195. switch (seq[2]) {
  3196. Case('A', bestlineEditUp(&l));
  3197. Case('B', bestlineEditDown(&l));
  3198. Case('C', bestlineEditRight(&l));
  3199. Case('D', bestlineEditLeft(&l));
  3200. Case('H', bestlineEditHome(&l));
  3201. Case('F', bestlineEditEnd(&l));
  3202. default:
  3203. break;
  3204. }
  3205. break;
  3206. case 033:
  3207. if (nread < 3)
  3208. break;
  3209. switch (seq[2]) {
  3210. case '[':
  3211. if (nread < 4)
  3212. break;
  3213. switch (seq[3]) {
  3214. Case('C', bestlineEditRightExpr(&l)); /* \e\e[C alt-right */
  3215. Case('D', bestlineEditLeftExpr(&l)); /* \e\e[D alt-left */
  3216. default:
  3217. break;
  3218. }
  3219. break;
  3220. case 'O':
  3221. if (nread < 4)
  3222. break;
  3223. switch (seq[3]) {
  3224. Case('C', bestlineEditRightExpr(&l)); /* \e\eOC alt-right */
  3225. Case('D', bestlineEditLeftExpr(&l)); /* \e\eOD alt-left */
  3226. default:
  3227. break;
  3228. }
  3229. break;
  3230. default:
  3231. break;
  3232. }
  3233. break;
  3234. default:
  3235. break;
  3236. }
  3237. break;
  3238. default:
  3239. if (!IsControl(seq[0])) { /* only sees canonical c0 */
  3240. if (xlatCallback) {
  3241. rune = GetUtf8(seq, nread);
  3242. w = EncodeUtf8(xlatCallback(rune.c));
  3243. nread = 0;
  3244. do {
  3245. seq[nread++] = w;
  3246. } while ((w >>= 8));
  3247. }
  3248. bestlineEditInsert(&l, seq, nread);
  3249. }
  3250. break;
  3251. }
  3252. }
  3253. }
  3254. void bestlineFree(void *ptr) {
  3255. free(ptr);
  3256. }
  3257. void bestlineHistoryFree(void) {
  3258. size_t i;
  3259. for (i = 0; i < BESTLINE_MAX_HISTORY; i++) {
  3260. if (history[i]) {
  3261. free(history[i]);
  3262. history[i] = 0;
  3263. }
  3264. }
  3265. historylen = 0;
  3266. }
  3267. static void bestlineAtExit(void) {
  3268. bestlineDisableRawMode();
  3269. bestlineHistoryFree();
  3270. bestlineRingFree();
  3271. }
  3272. int bestlineHistoryAdd(const char *line) {
  3273. char *linecopy;
  3274. if (!BESTLINE_MAX_HISTORY)
  3275. return 0;
  3276. if (historylen && !strcmp(history[historylen - 1], line))
  3277. return 0;
  3278. if (!(linecopy = strdup(line)))
  3279. return 0;
  3280. if (historylen == BESTLINE_MAX_HISTORY) {
  3281. free(history[0]);
  3282. memmove(history, history + 1, sizeof(char *) * (BESTLINE_MAX_HISTORY - 1));
  3283. historylen--;
  3284. }
  3285. history[historylen++] = linecopy;
  3286. return 1;
  3287. }
  3288. /**
  3289. * Saves line editing history to file.
  3290. *
  3291. * @return 0 on success, or -1 w/ errno
  3292. */
  3293. int bestlineHistorySave(const char *filename) {
  3294. FILE *fp;
  3295. unsigned j;
  3296. mode_t old_umask;
  3297. old_umask = umask(S_IXUSR | S_IRWXG | S_IRWXO);
  3298. fp = fopen(filename, "w");
  3299. umask(old_umask);
  3300. if (!fp)
  3301. return -1;
  3302. chmod(filename, S_IRUSR | S_IWUSR);
  3303. for (j = 0; j < historylen; j++) {
  3304. fputs(history[j], fp);
  3305. fputc('\n', fp);
  3306. }
  3307. fclose(fp);
  3308. return 0;
  3309. }
  3310. /**
  3311. * Loads history from the specified file.
  3312. *
  3313. * If the file doesn't exist, zero is returned and this will do nothing.
  3314. * If the file does exists and the operation succeeded zero is returned
  3315. * otherwise on error -1 is returned.
  3316. *
  3317. * @return 0 on success, or -1 w/ errno
  3318. */
  3319. int bestlineHistoryLoad(const char *filename) {
  3320. char **h;
  3321. int rc, fd, err;
  3322. size_t i, j, k, n, t;
  3323. char *m, *e, *p, *q, *f, *s;
  3324. err = errno, rc = 0;
  3325. if (!BESTLINE_MAX_HISTORY)
  3326. return 0;
  3327. if (!(h = (char **)calloc(2 * BESTLINE_MAX_HISTORY, sizeof(char *))))
  3328. return -1;
  3329. if ((fd = open(filename, O_RDONLY)) != -1) {
  3330. if ((n = GetFdSize(fd))) {
  3331. if ((m = (char *)mmap(0, n, PROT_READ, MAP_SHARED, fd, 0)) != MAP_FAILED) {
  3332. for (i = 0, e = (p = m) + n; p < e; p = f + 1) {
  3333. if (!(q = (char *)memchr(p, '\n', e - p)))
  3334. q = e;
  3335. for (f = q; q > p; --q) {
  3336. if (q[-1] != '\n' && q[-1] != '\r')
  3337. break;
  3338. }
  3339. if (q > p) {
  3340. h[i * 2 + 0] = p;
  3341. h[i * 2 + 1] = q;
  3342. i = (i + 1) % BESTLINE_MAX_HISTORY;
  3343. }
  3344. }
  3345. bestlineHistoryFree();
  3346. for (j = 0; j < BESTLINE_MAX_HISTORY; ++j) {
  3347. if (h[(k = (i + j) % BESTLINE_MAX_HISTORY) * 2]) {
  3348. if ((s = (char *)malloc((t = h[k * 2 + 1] - h[k * 2]) + 1))) {
  3349. memcpy(s, h[k * 2], t), s[t] = 0;
  3350. history[historylen++] = s;
  3351. }
  3352. }
  3353. }
  3354. munmap(m, n);
  3355. } else {
  3356. rc = -1;
  3357. }
  3358. }
  3359. close(fd);
  3360. } else if (errno == ENOENT) {
  3361. errno = err;
  3362. } else {
  3363. rc = -1;
  3364. }
  3365. free(h);
  3366. return rc;
  3367. }
  3368. /**
  3369. * Like bestlineRaw, but with the additional parameter init used as the buffer
  3370. * initial value.
  3371. */
  3372. char *bestlineRawInit(const char *prompt, const char *init, int infd, int outfd) {
  3373. char *buf;
  3374. ssize_t rc;
  3375. static char once;
  3376. struct sigaction sa[3];
  3377. if (!once)
  3378. atexit(bestlineAtExit), once = 1;
  3379. if (enableRawMode(infd) == -1)
  3380. return 0;
  3381. buf = 0;
  3382. gotint = 0;
  3383. sigemptyset(&sa->sa_mask);
  3384. sa->sa_flags = 0;
  3385. sa->sa_handler = bestlineOnInt;
  3386. sigaction(SIGINT, sa, sa + 1);
  3387. sigaction(SIGQUIT, sa, sa + 2);
  3388. bestlineWriteStr(outfd, "\033[?2004h"); // enable bracketed paste mode
  3389. rc = bestlineEdit(infd, outfd, prompt, init, &buf);
  3390. bestlineWriteStr(outfd, "\033[?2004l"); // disable bracketed paste mode
  3391. bestlineDisableRawMode();
  3392. sigaction(SIGQUIT, sa + 2, 0);
  3393. sigaction(SIGINT, sa + 1, 0);
  3394. if (gotint) {
  3395. free(buf);
  3396. buf = 0;
  3397. raise(gotint);
  3398. errno = EINTR;
  3399. rc = -1;
  3400. }
  3401. bestlineWriteStr(outfd, "\r\n");
  3402. if (rc != -1) {
  3403. return buf;
  3404. } else {
  3405. free(buf);
  3406. return 0;
  3407. }
  3408. }
  3409. /**
  3410. * Reads line interactively.
  3411. *
  3412. * This function can be used instead of bestline() in cases where we
  3413. * know for certain we're dealing with a terminal, which means we can
  3414. * avoid linking any stdio code.
  3415. *
  3416. * @return chomped allocated string of read line or null on eof/error
  3417. */
  3418. char *bestlineRaw(const char *prompt, int infd, int outfd) {
  3419. return bestlineRawInit(prompt, "", infd, outfd);
  3420. }
  3421. /**
  3422. * Like bestline, but with the additional parameter init used as the buffer
  3423. * initial value. The init parameter is only used if the terminal has basic
  3424. * capabilites.
  3425. */
  3426. char *bestlineInit(const char *prompt, const char *init) {
  3427. if (prompt && *prompt && (strchr(prompt, '\t') || strchr(prompt + 1, '\r'))) {
  3428. errno = EINVAL;
  3429. return 0;
  3430. }
  3431. if ((!isatty(fileno(stdin)) || !isatty(fileno(stdout)))) {
  3432. if (prompt && *prompt && (IsCharDev(fileno(stdin)) && IsCharDev(fileno(stdout)))) {
  3433. fputs(prompt, stdout);
  3434. fflush(stdout);
  3435. }
  3436. return GetLine(stdin, stdout);
  3437. } else if (bestlineIsUnsupportedTerm()) {
  3438. if (prompt && *prompt) {
  3439. fputs(prompt, stdout);
  3440. fflush(stdout);
  3441. }
  3442. return GetLine(stdin, stdout);
  3443. } else {
  3444. fflush(stdout);
  3445. return bestlineRawInit(prompt, init, fileno(stdin), fileno(stdout));
  3446. }
  3447. }
  3448. /**
  3449. * Reads line intelligently.
  3450. *
  3451. * The high level function that is the main API of the bestline library.
  3452. * This function checks if the terminal has basic capabilities, just checking
  3453. * for a blacklist of inarticulate terminals, and later either calls the line
  3454. * editing function or uses dummy fgets() so that you will be able to type
  3455. * something even in the most desperate of the conditions.
  3456. *
  3457. * @param prompt is printed before asking for input if we have a term
  3458. * and this may be set to empty or null to disable and prompt may
  3459. * contain ansi escape sequences, color, utf8, etc.
  3460. * @return chomped allocated string of read line or null on eof/error
  3461. */
  3462. char *bestline(const char *prompt) {
  3463. return bestlineInit(prompt, "");
  3464. }
  3465. /**
  3466. * Reads line intelligently w/ history, e.g.
  3467. *
  3468. * // see ~/.foo_history
  3469. * main() {
  3470. * char *line;
  3471. * while ((line = bestlineWithHistory("IN> ", "foo"))) {
  3472. * printf("OUT> %s\n", line);
  3473. * free(line);
  3474. * }
  3475. * }
  3476. *
  3477. * @param prompt is printed before asking for input if we have a term
  3478. * and this may be set to empty or null to disable and prompt may
  3479. * contain ansi escape sequences, color, utf8, etc.
  3480. * @param prog is name of your app, used to generate history filename
  3481. * however if it contains a slash / dot then we'll assume prog is
  3482. * the history filename which as determined by the caller
  3483. * @return chomped allocated string of read line or null on eof/error
  3484. */
  3485. char *bestlineWithHistory(const char *prompt, const char *prog) {
  3486. char *line;
  3487. struct abuf path;
  3488. const char *a, *b;
  3489. abInit(&path);
  3490. if (prog) {
  3491. if (strchr(prog, '/') || strchr(prog, '.')) {
  3492. abAppends(&path, prog);
  3493. } else {
  3494. b = "";
  3495. if (!(a = getenv("HOME"))) {
  3496. if (!(a = getenv("HOMEDRIVE")) || !(b = getenv("HOMEPATH"))) {
  3497. a = "";
  3498. }
  3499. }
  3500. if (*a) {
  3501. abAppends(&path, a);
  3502. abAppends(&path, b);
  3503. abAppendw(&path, '/');
  3504. }
  3505. abAppendw(&path, '.');
  3506. abAppends(&path, prog);
  3507. abAppends(&path, "_history");
  3508. }
  3509. }
  3510. if (path.len) {
  3511. bestlineHistoryLoad(path.b);
  3512. }
  3513. line = bestline(prompt);
  3514. if (path.len && line && *line) {
  3515. /* history here is inefficient but helpful when the user has multiple
  3516. * repls open at the same time, so history propagates between them */
  3517. bestlineHistoryLoad(path.b);
  3518. bestlineHistoryAdd(line);
  3519. bestlineHistorySave(path.b);
  3520. }
  3521. abFree(&path);
  3522. return line;
  3523. }
  3524. /**
  3525. * Registers tab completion callback.
  3526. */
  3527. void bestlineSetCompletionCallback(bestlineCompletionCallback *fn) {
  3528. completionCallback = fn;
  3529. }
  3530. /**
  3531. * Registers hints callback.
  3532. *
  3533. * Register a hits function to be called to show hits to the user at the
  3534. * right of the prompt.
  3535. */
  3536. void bestlineSetHintsCallback(bestlineHintsCallback *fn) {
  3537. hintsCallback = fn;
  3538. }
  3539. /**
  3540. * Sets free hints callback.
  3541. *
  3542. * This registers a function to free the hints returned by the hints
  3543. * callback registered with bestlineSetHintsCallback().
  3544. */
  3545. void bestlineSetFreeHintsCallback(bestlineFreeHintsCallback *fn) {
  3546. freeHintsCallback = fn;
  3547. }
  3548. /**
  3549. * Sets character translation callback.
  3550. */
  3551. void bestlineSetXlatCallback(bestlineXlatCallback *fn) {
  3552. xlatCallback = fn;
  3553. }
  3554. /**
  3555. * Adds completion.
  3556. *
  3557. * This function is used by the callback function registered by the user
  3558. * in order to add completion options given the input string when the
  3559. * user typed <tab>. See the example.c source code for a very easy to
  3560. * understand example.
  3561. */
  3562. void bestlineAddCompletion(bestlineCompletions *lc, const char *str) {
  3563. size_t len;
  3564. char *copy, **cvec;
  3565. if ((copy = (char *)malloc((len = strlen(str)) + 1))) {
  3566. memcpy(copy, str, len + 1);
  3567. if ((cvec = (char **)realloc(lc->cvec, (lc->len + 1) * sizeof(*lc->cvec)))) {
  3568. lc->cvec = cvec;
  3569. lc->cvec[lc->len++] = copy;
  3570. } else {
  3571. free(copy);
  3572. }
  3573. }
  3574. }
  3575. /**
  3576. * Frees list of completion option populated by bestlineAddCompletion().
  3577. */
  3578. void bestlineFreeCompletions(bestlineCompletions *lc) {
  3579. size_t i;
  3580. for (i = 0; i < lc->len; i++)
  3581. free(lc->cvec[i]);
  3582. if (lc->cvec)
  3583. free(lc->cvec);
  3584. }
  3585. /**
  3586. * Enables "mask mode".
  3587. *
  3588. * When it is enabled, instead of the input that the user is typing, the
  3589. * terminal will just display a corresponding number of asterisks, like
  3590. * "****". This is useful for passwords and other secrets that should
  3591. * not be displayed.
  3592. *
  3593. * @see bestlineMaskModeDisable()
  3594. */
  3595. void bestlineMaskModeEnable(void) {
  3596. maskmode = 1;
  3597. }
  3598. /**
  3599. * Disables "mask mode".
  3600. */
  3601. void bestlineMaskModeDisable(void) {
  3602. maskmode = 0;
  3603. }
  3604. /**
  3605. * Enables or disables "balance mode".
  3606. *
  3607. * When it is enabled, bestline() will block until parentheses are
  3608. * balanced. This is useful for code but not for free text.
  3609. */
  3610. void bestlineBalanceMode(char mode) {
  3611. balancemode = mode;
  3612. }
  3613. /**
  3614. * Enables or disables "ollama mode".
  3615. *
  3616. * This enables you to type multiline input by putting triple quotes at
  3617. * the beginning and end. For example:
  3618. *
  3619. * >>> """
  3620. * ... second line
  3621. * ... third line
  3622. * ... """
  3623. *
  3624. * Would yield the string `"\nsecond line\nthird line\n"`.
  3625. *
  3626. * @param mode is 1 to enable, or 0 to disable
  3627. */
  3628. void bestlineLlamaMode(char mode) {
  3629. llamamode = mode;
  3630. }
  3631. /**
  3632. * Enables Emacs mode.
  3633. *
  3634. * This mode remaps CTRL-C so you can use additional shortcuts, like C-c
  3635. * C-s for slurp. By default, CTRL-C raises SIGINT for exiting programs.
  3636. */
  3637. void bestlineEmacsMode(char mode) {
  3638. emacsmode = mode;
  3639. }
  3640. /**
  3641. * Allows implementation of user functions for read, write, and poll
  3642. * with the intention of polling for background I/O.
  3643. */
  3644. static int MyRead(int fd, void *c, int n) {
  3645. return read(fd, c, n);
  3646. }
  3647. static int MyWrite(int fd, const void *c, int n) {
  3648. return write(fd, c, n);
  3649. }
  3650. static int MyPoll(int fd, int events, int to) {
  3651. struct pollfd p[1];
  3652. p[0].fd = fd;
  3653. p[0].events = events;
  3654. return poll(p, 1, to);
  3655. }
  3656. void bestlineUserIO(int (*userReadFn)(int, void *, int), int (*userWriteFn)(int, const void *, int),
  3657. int (*userPollFn)(int, int, int)) {
  3658. if (userReadFn)
  3659. _MyRead = userReadFn;
  3660. else
  3661. _MyRead = MyRead;
  3662. if (userWriteFn)
  3663. _MyWrite = userWriteFn;
  3664. else
  3665. _MyWrite = MyWrite;
  3666. if (userPollFn)
  3667. _MyPoll = userPollFn;
  3668. else
  3669. _MyPoll = MyPoll;
  3670. }