bestline.c 133 KB

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