regex.c 253 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211
  1. /* Extended regular expression matching and search library,
  2. version 0.12.
  3. (Implements POSIX draft P1003.2/D11.2, except for some of the
  4. internationalization features.)
  5. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  6. This file is part of the GNU C Library.
  7. The GNU C Library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. The GNU C Library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with the GNU C Library; if not, write to the Free
  17. Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. 02110-1301 USA. */
  19. /* This file has been modified for usage in libiberty. It includes "xregex.h"
  20. instead of <regex.h>. The "xregex.h" header file renames all external
  21. routines with an "x" prefix so they do not collide with the native regex
  22. routines or with other components regex routines. */
  23. /* AIX requires this to be the first thing in the file. */
  24. #if defined _AIX && !defined __GNUC__ && !defined REGEX_MALLOC
  25. #pragma alloca
  26. #endif
  27. #if __GNUC__ >= 12
  28. # pragma GCC diagnostic ignored "-Wuse-after-free"
  29. #endif
  30. #undef _GNU_SOURCE
  31. #define _GNU_SOURCE
  32. #ifndef INSIDE_RECURSION
  33. # ifdef HAVE_CONFIG_H
  34. # include <config.h>
  35. # endif
  36. #endif
  37. #include <ansidecl.h>
  38. #ifndef INSIDE_RECURSION
  39. # if defined STDC_HEADERS && !defined emacs
  40. # include <stddef.h>
  41. # define PTR_INT_TYPE ptrdiff_t
  42. # else
  43. /* We need this for `regex.h', and perhaps for the Emacs include files. */
  44. # include <sys/types.h>
  45. # define PTR_INT_TYPE long
  46. # endif
  47. # define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
  48. /* For platform which support the ISO C amendement 1 functionality we
  49. support user defined character classes. */
  50. # if defined _LIBC || WIDE_CHAR_SUPPORT
  51. /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
  52. # include <wchar.h>
  53. # include <wctype.h>
  54. # endif
  55. # ifdef _LIBC
  56. /* We have to keep the namespace clean. */
  57. # define regfree(preg) __regfree (preg)
  58. # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
  59. # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
  60. # define regerror(errcode, preg, errbuf, errbuf_size) \
  61. __regerror(errcode, preg, errbuf, errbuf_size)
  62. # define re_set_registers(bu, re, nu, st, en) \
  63. __re_set_registers (bu, re, nu, st, en)
  64. # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
  65. __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  66. # define re_match(bufp, string, size, pos, regs) \
  67. __re_match (bufp, string, size, pos, regs)
  68. # define re_search(bufp, string, size, startpos, range, regs) \
  69. __re_search (bufp, string, size, startpos, range, regs)
  70. # define re_compile_pattern(pattern, length, bufp) \
  71. __re_compile_pattern (pattern, length, bufp)
  72. # define re_set_syntax(syntax) __re_set_syntax (syntax)
  73. # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
  74. __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
  75. # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
  76. # define btowc __btowc
  77. /* We are also using some library internals. */
  78. # include <locale/localeinfo.h>
  79. # include <locale/elem-hash.h>
  80. # include <langinfo.h>
  81. # include <locale/coll-lookup.h>
  82. # endif
  83. /* This is for other GNU distributions with internationalized messages. */
  84. # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
  85. # include <libintl.h>
  86. # ifdef _LIBC
  87. # undef gettext
  88. # define gettext(msgid) __dcgettext ("libc", msgid, LC_MESSAGES)
  89. # endif
  90. # else
  91. # define gettext(msgid) (msgid)
  92. # endif
  93. # ifndef gettext_noop
  94. /* This define is so xgettext can find the internationalizable
  95. strings. */
  96. # define gettext_noop(String) String
  97. # endif
  98. /* The `emacs' switch turns on certain matching commands
  99. that make sense only in Emacs. */
  100. # ifdef emacs
  101. # include "lisp.h"
  102. # include "buffer.h"
  103. # include "syntax.h"
  104. # else /* not emacs */
  105. /* If we are not linking with Emacs proper,
  106. we can't use the relocating allocator
  107. even if config.h says that we can. */
  108. # undef REL_ALLOC
  109. # if defined STDC_HEADERS || defined _LIBC
  110. # include <stdlib.h>
  111. # else
  112. char *malloc ();
  113. char *realloc ();
  114. # endif
  115. /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
  116. If nothing else has been done, use the method below. */
  117. # ifdef INHIBIT_STRING_HEADER
  118. # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
  119. # if !defined bzero && !defined bcopy
  120. # undef INHIBIT_STRING_HEADER
  121. # endif
  122. # endif
  123. # endif
  124. /* This is the normal way of making sure we have a bcopy and a bzero.
  125. This is used in most programs--a few other programs avoid this
  126. by defining INHIBIT_STRING_HEADER. */
  127. # ifndef INHIBIT_STRING_HEADER
  128. # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
  129. # include <string.h>
  130. # ifndef bzero
  131. # ifndef _LIBC
  132. # define bzero(s, n) ((void) memset (s, '\0', n))
  133. # else
  134. # define bzero(s, n) __bzero (s, n)
  135. # endif
  136. # endif
  137. # else
  138. # include <strings.h>
  139. # ifndef memcmp
  140. # define memcmp(s1, s2, n) bcmp (s1, s2, n)
  141. # endif
  142. # ifndef memcpy
  143. # define memcpy(d, s, n) (bcopy (s, d, n), (d))
  144. # endif
  145. # endif
  146. # endif
  147. /* Define the syntax stuff for \<, \>, etc. */
  148. /* This must be nonzero for the wordchar and notwordchar pattern
  149. commands in re_match_2. */
  150. # ifndef Sword
  151. # define Sword 1
  152. # endif
  153. # ifdef SWITCH_ENUM_BUG
  154. # define SWITCH_ENUM_CAST(x) ((int)(x))
  155. # else
  156. # define SWITCH_ENUM_CAST(x) (x)
  157. # endif
  158. # endif /* not emacs */
  159. # if defined _LIBC || HAVE_LIMITS_H
  160. # include <limits.h>
  161. # endif
  162. # ifndef MB_LEN_MAX
  163. # define MB_LEN_MAX 1
  164. # endif
  165. /* Get the interface, including the syntax bits. */
  166. # include "xregex.h" /* change for libiberty */
  167. /* isalpha etc. are used for the character classes. */
  168. # include <ctype.h>
  169. /* Jim Meyering writes:
  170. "... Some ctype macros are valid only for character codes that
  171. isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  172. using /bin/cc or gcc but without giving an ansi option). So, all
  173. ctype uses should be through macros like ISPRINT... If
  174. STDC_HEADERS is defined, then autoconf has verified that the ctype
  175. macros don't need to be guarded with references to isascii. ...
  176. Defining isascii to 1 should let any compiler worth its salt
  177. eliminate the && through constant folding."
  178. Solaris defines some of these symbols so we must undefine them first. */
  179. # undef ISASCII
  180. # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
  181. # define ISASCII(c) 1
  182. # else
  183. # define ISASCII(c) isascii(c)
  184. # endif
  185. # ifdef isblank
  186. # define ISBLANK(c) (ISASCII (c) && isblank (c))
  187. # else
  188. # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  189. # endif
  190. # ifdef isgraph
  191. # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  192. # else
  193. # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  194. # endif
  195. # undef ISPRINT
  196. # define ISPRINT(c) (ISASCII (c) && isprint (c))
  197. # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
  198. # define ISALNUM(c) (ISASCII (c) && isalnum (c))
  199. # define ISALPHA(c) (ISASCII (c) && isalpha (c))
  200. # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
  201. # define ISLOWER(c) (ISASCII (c) && islower (c))
  202. # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
  203. # define ISSPACE(c) (ISASCII (c) && isspace (c))
  204. # define ISUPPER(c) (ISASCII (c) && isupper (c))
  205. # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
  206. # ifdef _tolower
  207. # define TOLOWER(c) _tolower(c)
  208. # else
  209. # define TOLOWER(c) tolower(c)
  210. # endif
  211. # ifndef NULL
  212. # define NULL (void *)0
  213. # endif
  214. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  215. since ours (we hope) works properly with all combinations of
  216. machines, compilers, `char' and `unsigned char' argument types.
  217. (Per Bothner suggested the basic approach.) */
  218. # undef SIGN_EXTEND_CHAR
  219. # if __STDC__
  220. # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  221. # else /* not __STDC__ */
  222. /* As in Harbison and Steele. */
  223. # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  224. # endif
  225. # ifndef emacs
  226. /* How many characters in the character set. */
  227. # define CHAR_SET_SIZE 256
  228. # ifdef SYNTAX_TABLE
  229. extern char *re_syntax_table;
  230. # else /* not SYNTAX_TABLE */
  231. static char re_syntax_table[CHAR_SET_SIZE];
  232. static void init_syntax_once (void);
  233. static void
  234. init_syntax_once (void)
  235. {
  236. register int c;
  237. static int done = 0;
  238. if (done)
  239. return;
  240. bzero (re_syntax_table, sizeof re_syntax_table);
  241. for (c = 0; c < CHAR_SET_SIZE; ++c)
  242. if (ISALNUM (c))
  243. re_syntax_table[c] = Sword;
  244. re_syntax_table['_'] = Sword;
  245. done = 1;
  246. }
  247. # endif /* not SYNTAX_TABLE */
  248. # define SYNTAX(c) re_syntax_table[(unsigned char) (c)]
  249. # endif /* emacs */
  250. /* Integer type for pointers. */
  251. # if !defined _LIBC && !defined HAVE_UINTPTR_T
  252. typedef unsigned long int uintptr_t;
  253. # endif
  254. /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
  255. use `alloca' instead of `malloc'. This is because using malloc in
  256. re_search* or re_match* could cause memory leaks when C-g is used in
  257. Emacs; also, malloc is slower and causes storage fragmentation. On
  258. the other hand, malloc is more portable, and easier to debug.
  259. Because we sometimes use alloca, some routines have to be macros,
  260. not functions -- `alloca'-allocated space disappears at the end of the
  261. function it is called in. */
  262. # ifdef REGEX_MALLOC
  263. # define REGEX_ALLOCATE malloc
  264. # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  265. # define REGEX_FREE free
  266. # else /* not REGEX_MALLOC */
  267. /* Emacs already defines alloca, sometimes. */
  268. # ifndef alloca
  269. /* Make alloca work the best possible way. */
  270. # ifdef __GNUC__
  271. # define alloca __builtin_alloca
  272. # else /* not __GNUC__ */
  273. # if HAVE_ALLOCA_H
  274. # include <alloca.h>
  275. # endif /* HAVE_ALLOCA_H */
  276. # endif /* not __GNUC__ */
  277. # endif /* not alloca */
  278. # define REGEX_ALLOCATE alloca
  279. /* Assumes a `char *destination' variable. */
  280. # define REGEX_REALLOCATE(source, osize, nsize) \
  281. (destination = (char *) alloca (nsize), \
  282. memcpy (destination, source, osize))
  283. /* No need to do anything to free, after alloca. */
  284. # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
  285. # endif /* not REGEX_MALLOC */
  286. /* Define how to allocate the failure stack. */
  287. # if defined REL_ALLOC && defined REGEX_MALLOC
  288. # define REGEX_ALLOCATE_STACK(size) \
  289. r_alloc (&failure_stack_ptr, (size))
  290. # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  291. r_re_alloc (&failure_stack_ptr, (nsize))
  292. # define REGEX_FREE_STACK(ptr) \
  293. r_alloc_free (&failure_stack_ptr)
  294. # else /* not using relocating allocator */
  295. # ifdef REGEX_MALLOC
  296. # define REGEX_ALLOCATE_STACK malloc
  297. # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
  298. # define REGEX_FREE_STACK free
  299. # else /* not REGEX_MALLOC */
  300. # define REGEX_ALLOCATE_STACK alloca
  301. # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  302. REGEX_REALLOCATE (source, osize, nsize)
  303. /* No need to explicitly free anything. */
  304. # define REGEX_FREE_STACK(arg)
  305. # endif /* not REGEX_MALLOC */
  306. # endif /* not using relocating allocator */
  307. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  308. `string1' or just past its end. This works if PTR is NULL, which is
  309. a good thing. */
  310. # define FIRST_STRING_P(ptr) \
  311. (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  312. /* (Re)Allocate N items of type T using malloc, or fail. */
  313. # define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  314. # define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  315. # define RETALLOC_IF(addr, n, t) \
  316. if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
  317. # define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  318. # define BYTEWIDTH 8 /* In bits. */
  319. # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  320. # undef MAX
  321. # undef MIN
  322. # define MAX(a, b) ((a) > (b) ? (a) : (b))
  323. # define MIN(a, b) ((a) < (b) ? (a) : (b))
  324. typedef char boolean;
  325. # define false 0
  326. # define true 1
  327. static reg_errcode_t byte_regex_compile (const char *pattern, size_t size,
  328. reg_syntax_t syntax,
  329. struct re_pattern_buffer *bufp);
  330. static int byte_re_match_2_internal (struct re_pattern_buffer *bufp,
  331. const char *string1, int size1,
  332. const char *string2, int size2,
  333. int pos,
  334. struct re_registers *regs,
  335. int stop);
  336. static int byte_re_search_2 (struct re_pattern_buffer *bufp,
  337. const char *string1, int size1,
  338. const char *string2, int size2,
  339. int startpos, int range,
  340. struct re_registers *regs, int stop);
  341. static int byte_re_compile_fastmap (struct re_pattern_buffer *bufp);
  342. #ifdef MBS_SUPPORT
  343. static reg_errcode_t wcs_regex_compile (const char *pattern, size_t size,
  344. reg_syntax_t syntax,
  345. struct re_pattern_buffer *bufp);
  346. static int wcs_re_match_2_internal (struct re_pattern_buffer *bufp,
  347. const char *cstring1, int csize1,
  348. const char *cstring2, int csize2,
  349. int pos,
  350. struct re_registers *regs,
  351. int stop,
  352. wchar_t *string1, int size1,
  353. wchar_t *string2, int size2,
  354. int *mbs_offset1, int *mbs_offset2);
  355. static int wcs_re_search_2 (struct re_pattern_buffer *bufp,
  356. const char *string1, int size1,
  357. const char *string2, int size2,
  358. int startpos, int range,
  359. struct re_registers *regs, int stop);
  360. static int wcs_re_compile_fastmap (struct re_pattern_buffer *bufp);
  361. #endif
  362. /* These are the command codes that appear in compiled regular
  363. expressions. Some opcodes are followed by argument bytes. A
  364. command code can specify any interpretation whatsoever for its
  365. arguments. Zero bytes may appear in the compiled regular expression. */
  366. typedef enum
  367. {
  368. no_op = 0,
  369. /* Succeed right away--no more backtracking. */
  370. succeed,
  371. /* Followed by one byte giving n, then by n literal bytes. */
  372. exactn,
  373. # ifdef MBS_SUPPORT
  374. /* Same as exactn, but contains binary data. */
  375. exactn_bin,
  376. # endif
  377. /* Matches any (more or less) character. */
  378. anychar,
  379. /* Matches any one char belonging to specified set. First
  380. following byte is number of bitmap bytes. Then come bytes
  381. for a bitmap saying which chars are in. Bits in each byte
  382. are ordered low-bit-first. A character is in the set if its
  383. bit is 1. A character too large to have a bit in the map is
  384. automatically not in the set. */
  385. /* ifdef MBS_SUPPORT, following element is length of character
  386. classes, length of collating symbols, length of equivalence
  387. classes, length of character ranges, and length of characters.
  388. Next, character class element, collating symbols elements,
  389. equivalence class elements, range elements, and character
  390. elements follow.
  391. See regex_compile function. */
  392. charset,
  393. /* Same parameters as charset, but match any character that is
  394. not one of those specified. */
  395. charset_not,
  396. /* Start remembering the text that is matched, for storing in a
  397. register. Followed by one byte with the register number, in
  398. the range 0 to one less than the pattern buffer's re_nsub
  399. field. Then followed by one byte with the number of groups
  400. inner to this one. (This last has to be part of the
  401. start_memory only because we need it in the on_failure_jump
  402. of re_match_2.) */
  403. start_memory,
  404. /* Stop remembering the text that is matched and store it in a
  405. memory register. Followed by one byte with the register
  406. number, in the range 0 to one less than `re_nsub' in the
  407. pattern buffer, and one byte with the number of inner groups,
  408. just like `start_memory'. (We need the number of inner
  409. groups here because we don't have any easy way of finding the
  410. corresponding start_memory when we're at a stop_memory.) */
  411. stop_memory,
  412. /* Match a duplicate of something remembered. Followed by one
  413. byte containing the register number. */
  414. duplicate,
  415. /* Fail unless at beginning of line. */
  416. begline,
  417. /* Fail unless at end of line. */
  418. endline,
  419. /* Succeeds if at beginning of buffer (if emacs) or at beginning
  420. of string to be matched (if not). */
  421. begbuf,
  422. /* Analogously, for end of buffer/string. */
  423. endbuf,
  424. /* Followed by two byte relative address to which to jump. */
  425. jump,
  426. /* Same as jump, but marks the end of an alternative. */
  427. jump_past_alt,
  428. /* Followed by two-byte relative address of place to resume at
  429. in case of failure. */
  430. /* ifdef MBS_SUPPORT, the size of address is 1. */
  431. on_failure_jump,
  432. /* Like on_failure_jump, but pushes a placeholder instead of the
  433. current string position when executed. */
  434. on_failure_keep_string_jump,
  435. /* Throw away latest failure point and then jump to following
  436. two-byte relative address. */
  437. /* ifdef MBS_SUPPORT, the size of address is 1. */
  438. pop_failure_jump,
  439. /* Change to pop_failure_jump if know won't have to backtrack to
  440. match; otherwise change to jump. This is used to jump
  441. back to the beginning of a repeat. If what follows this jump
  442. clearly won't match what the repeat does, such that we can be
  443. sure that there is no use backtracking out of repetitions
  444. already matched, then we change it to a pop_failure_jump.
  445. Followed by two-byte address. */
  446. /* ifdef MBS_SUPPORT, the size of address is 1. */
  447. maybe_pop_jump,
  448. /* Jump to following two-byte address, and push a dummy failure
  449. point. This failure point will be thrown away if an attempt
  450. is made to use it for a failure. A `+' construct makes this
  451. before the first repeat. Also used as an intermediary kind
  452. of jump when compiling an alternative. */
  453. /* ifdef MBS_SUPPORT, the size of address is 1. */
  454. dummy_failure_jump,
  455. /* Push a dummy failure point and continue. Used at the end of
  456. alternatives. */
  457. push_dummy_failure,
  458. /* Followed by two-byte relative address and two-byte number n.
  459. After matching N times, jump to the address upon failure. */
  460. /* ifdef MBS_SUPPORT, the size of address is 1. */
  461. succeed_n,
  462. /* Followed by two-byte relative address, and two-byte number n.
  463. Jump to the address N times, then fail. */
  464. /* ifdef MBS_SUPPORT, the size of address is 1. */
  465. jump_n,
  466. /* Set the following two-byte relative address to the
  467. subsequent two-byte number. The address *includes* the two
  468. bytes of number. */
  469. /* ifdef MBS_SUPPORT, the size of address is 1. */
  470. set_number_at,
  471. wordchar, /* Matches any word-constituent character. */
  472. notwordchar, /* Matches any char that is not a word-constituent. */
  473. wordbeg, /* Succeeds if at word beginning. */
  474. wordend, /* Succeeds if at word end. */
  475. wordbound, /* Succeeds if at a word boundary. */
  476. notwordbound /* Succeeds if not at a word boundary. */
  477. # ifdef emacs
  478. ,before_dot, /* Succeeds if before point. */
  479. at_dot, /* Succeeds if at point. */
  480. after_dot, /* Succeeds if after point. */
  481. /* Matches any character whose syntax is specified. Followed by
  482. a byte which contains a syntax code, e.g., Sword. */
  483. syntaxspec,
  484. /* Matches any character whose syntax is not that specified. */
  485. notsyntaxspec
  486. # endif /* emacs */
  487. } re_opcode_t;
  488. #endif /* not INSIDE_RECURSION */
  489. #ifdef BYTE
  490. # define CHAR_T char
  491. # define UCHAR_T unsigned char
  492. # define COMPILED_BUFFER_VAR bufp->buffer
  493. # define OFFSET_ADDRESS_SIZE 2
  494. # define PREFIX(name) byte_##name
  495. # define ARG_PREFIX(name) name
  496. # define PUT_CHAR(c) putchar (c)
  497. #else
  498. # ifdef WCHAR
  499. # define CHAR_T wchar_t
  500. # define UCHAR_T wchar_t
  501. # define COMPILED_BUFFER_VAR wc_buffer
  502. # define OFFSET_ADDRESS_SIZE 1 /* the size which STORE_NUMBER macro use */
  503. # define CHAR_CLASS_SIZE ((__alignof__(wctype_t)+sizeof(wctype_t))/sizeof(CHAR_T)+1)
  504. # define PREFIX(name) wcs_##name
  505. # define ARG_PREFIX(name) c##name
  506. /* Should we use wide stream?? */
  507. # define PUT_CHAR(c) printf ("%C", c);
  508. # define TRUE 1
  509. # define FALSE 0
  510. # else
  511. # ifdef MBS_SUPPORT
  512. # define WCHAR
  513. # define INSIDE_RECURSION
  514. # include "regex.c"
  515. # undef INSIDE_RECURSION
  516. # endif
  517. # define BYTE
  518. # define INSIDE_RECURSION
  519. # include "regex.c"
  520. # undef INSIDE_RECURSION
  521. # endif
  522. #endif
  523. #ifdef INSIDE_RECURSION
  524. /* Common operations on the compiled pattern. */
  525. /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
  526. /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
  527. # ifdef WCHAR
  528. # define STORE_NUMBER(destination, number) \
  529. do { \
  530. *(destination) = (UCHAR_T)(number); \
  531. } while (0)
  532. # else /* BYTE */
  533. # define STORE_NUMBER(destination, number) \
  534. do { \
  535. (destination)[0] = (number) & 0377; \
  536. (destination)[1] = (number) >> 8; \
  537. } while (0)
  538. # endif /* WCHAR */
  539. /* Same as STORE_NUMBER, except increment DESTINATION to
  540. the byte after where the number is stored. Therefore, DESTINATION
  541. must be an lvalue. */
  542. /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
  543. # define STORE_NUMBER_AND_INCR(destination, number) \
  544. do { \
  545. STORE_NUMBER (destination, number); \
  546. (destination) += OFFSET_ADDRESS_SIZE; \
  547. } while (0)
  548. /* Put into DESTINATION a number stored in two contiguous bytes starting
  549. at SOURCE. */
  550. /* ifdef MBS_SUPPORT, we store NUMBER in 1 element. */
  551. # ifdef WCHAR
  552. # define EXTRACT_NUMBER(destination, source) \
  553. do { \
  554. (destination) = *(source); \
  555. } while (0)
  556. # else /* BYTE */
  557. # define EXTRACT_NUMBER(destination, source) \
  558. do { \
  559. (destination) = *(source) & 0377; \
  560. (destination) += ((unsigned) SIGN_EXTEND_CHAR (*((source) + 1))) << 8; \
  561. } while (0)
  562. # endif
  563. # ifdef DEBUG
  564. static void PREFIX(extract_number) (int *dest, UCHAR_T *source);
  565. static void
  566. PREFIX(extract_number) (int *dest, UCHAR_T *source)
  567. {
  568. # ifdef WCHAR
  569. *dest = *source;
  570. # else /* BYTE */
  571. int temp = SIGN_EXTEND_CHAR (*(source + 1));
  572. *dest = *source & 0377;
  573. *dest += temp << 8;
  574. # endif
  575. }
  576. # ifndef EXTRACT_MACROS /* To debug the macros. */
  577. # undef EXTRACT_NUMBER
  578. # define EXTRACT_NUMBER(dest, src) PREFIX(extract_number) (&dest, src)
  579. # endif /* not EXTRACT_MACROS */
  580. # endif /* DEBUG */
  581. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  582. SOURCE must be an lvalue. */
  583. # define EXTRACT_NUMBER_AND_INCR(destination, source) \
  584. do { \
  585. EXTRACT_NUMBER (destination, source); \
  586. (source) += OFFSET_ADDRESS_SIZE; \
  587. } while (0)
  588. # ifdef DEBUG
  589. static void PREFIX(extract_number_and_incr) (int *destination,
  590. UCHAR_T **source);
  591. static void
  592. PREFIX(extract_number_and_incr) (int *destination, UCHAR_T **source)
  593. {
  594. PREFIX(extract_number) (destination, *source);
  595. *source += OFFSET_ADDRESS_SIZE;
  596. }
  597. # ifndef EXTRACT_MACROS
  598. # undef EXTRACT_NUMBER_AND_INCR
  599. # define EXTRACT_NUMBER_AND_INCR(dest, src) \
  600. PREFIX(extract_number_and_incr) (&dest, &src)
  601. # endif /* not EXTRACT_MACROS */
  602. # endif /* DEBUG */
  603. /* If DEBUG is defined, Regex prints many voluminous messages about what
  604. it is doing (if the variable `debug' is nonzero). If linked with the
  605. main program in `iregex.c', you can enter patterns and strings
  606. interactively. And if linked with the main program in `main.c' and
  607. the other test files, you can run the already-written tests. */
  608. # ifdef DEBUG
  609. # ifndef DEFINED_ONCE
  610. /* We use standard I/O for debugging. */
  611. # include <stdio.h>
  612. /* It is useful to test things that ``must'' be true when debugging. */
  613. # include <assert.h>
  614. static int debug;
  615. # define DEBUG_STATEMENT(e) e
  616. # define DEBUG_PRINT1(x) if (debug) printf (x)
  617. # define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  618. # define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  619. # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  620. # endif /* not DEFINED_ONCE */
  621. # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
  622. if (debug) PREFIX(print_partial_compiled_pattern) (s, e)
  623. # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
  624. if (debug) PREFIX(print_double_string) (w, s1, sz1, s2, sz2)
  625. /* Print the fastmap in human-readable form. */
  626. # ifndef DEFINED_ONCE
  627. void
  628. print_fastmap (char *fastmap)
  629. {
  630. unsigned was_a_range = 0;
  631. unsigned i = 0;
  632. while (i < (1 << BYTEWIDTH))
  633. {
  634. if (fastmap[i++])
  635. {
  636. was_a_range = 0;
  637. putchar (i - 1);
  638. while (i < (1 << BYTEWIDTH) && fastmap[i])
  639. {
  640. was_a_range = 1;
  641. i++;
  642. }
  643. if (was_a_range)
  644. {
  645. printf ("-");
  646. putchar (i - 1);
  647. }
  648. }
  649. }
  650. putchar ('\n');
  651. }
  652. # endif /* not DEFINED_ONCE */
  653. /* Print a compiled pattern string in human-readable form, starting at
  654. the START pointer into it and ending just before the pointer END. */
  655. void
  656. PREFIX(print_partial_compiled_pattern) (UCHAR_T *start, UCHAR_T *end)
  657. {
  658. int mcnt, mcnt2;
  659. UCHAR_T *p1;
  660. UCHAR_T *p = start;
  661. UCHAR_T *pend = end;
  662. if (start == NULL)
  663. {
  664. printf ("(null)\n");
  665. return;
  666. }
  667. /* Loop over pattern commands. */
  668. while (p < pend)
  669. {
  670. # ifdef _LIBC
  671. printf ("%td:\t", p - start);
  672. # else
  673. printf ("%ld:\t", (long int) (p - start));
  674. # endif
  675. switch ((re_opcode_t) *p++)
  676. {
  677. case no_op:
  678. printf ("/no_op");
  679. break;
  680. case exactn:
  681. mcnt = *p++;
  682. printf ("/exactn/%d", mcnt);
  683. do
  684. {
  685. putchar ('/');
  686. PUT_CHAR (*p++);
  687. }
  688. while (--mcnt);
  689. break;
  690. # ifdef MBS_SUPPORT
  691. case exactn_bin:
  692. mcnt = *p++;
  693. printf ("/exactn_bin/%d", mcnt);
  694. do
  695. {
  696. printf("/%lx", (long int) *p++);
  697. }
  698. while (--mcnt);
  699. break;
  700. # endif /* MBS_SUPPORT */
  701. case start_memory:
  702. mcnt = *p++;
  703. printf ("/start_memory/%d/%ld", mcnt, (long int) *p++);
  704. break;
  705. case stop_memory:
  706. mcnt = *p++;
  707. printf ("/stop_memory/%d/%ld", mcnt, (long int) *p++);
  708. break;
  709. case duplicate:
  710. printf ("/duplicate/%ld", (long int) *p++);
  711. break;
  712. case anychar:
  713. printf ("/anychar");
  714. break;
  715. case charset:
  716. case charset_not:
  717. {
  718. # ifdef WCHAR
  719. int i, length;
  720. wchar_t *workp = p;
  721. printf ("/charset [%s",
  722. (re_opcode_t) *(workp - 1) == charset_not ? "^" : "");
  723. p += 5;
  724. length = *workp++; /* the length of char_classes */
  725. for (i=0 ; i<length ; i++)
  726. printf("[:%lx:]", (long int) *p++);
  727. length = *workp++; /* the length of collating_symbol */
  728. for (i=0 ; i<length ;)
  729. {
  730. printf("[.");
  731. while(*p != 0)
  732. PUT_CHAR((i++,*p++));
  733. i++,p++;
  734. printf(".]");
  735. }
  736. length = *workp++; /* the length of equivalence_class */
  737. for (i=0 ; i<length ;)
  738. {
  739. printf("[=");
  740. while(*p != 0)
  741. PUT_CHAR((i++,*p++));
  742. i++,p++;
  743. printf("=]");
  744. }
  745. length = *workp++; /* the length of char_range */
  746. for (i=0 ; i<length ; i++)
  747. {
  748. wchar_t range_start = *p++;
  749. wchar_t range_end = *p++;
  750. printf("%C-%C", range_start, range_end);
  751. }
  752. length = *workp++; /* the length of char */
  753. for (i=0 ; i<length ; i++)
  754. printf("%C", *p++);
  755. putchar (']');
  756. # else
  757. register int c, last = -100;
  758. register int in_range = 0;
  759. printf ("/charset [%s",
  760. (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  761. assert (p + *p < pend);
  762. for (c = 0; c < 256; c++)
  763. if (c / 8 < *p
  764. && (p[1 + (c/8)] & (1 << (c % 8))))
  765. {
  766. /* Are we starting a range? */
  767. if (last + 1 == c && ! in_range)
  768. {
  769. putchar ('-');
  770. in_range = 1;
  771. }
  772. /* Have we broken a range? */
  773. else if (last + 1 != c && in_range)
  774. {
  775. putchar (last);
  776. in_range = 0;
  777. }
  778. if (! in_range)
  779. putchar (c);
  780. last = c;
  781. }
  782. if (in_range)
  783. putchar (last);
  784. putchar (']');
  785. p += 1 + *p;
  786. # endif /* WCHAR */
  787. }
  788. break;
  789. case begline:
  790. printf ("/begline");
  791. break;
  792. case endline:
  793. printf ("/endline");
  794. break;
  795. case on_failure_jump:
  796. PREFIX(extract_number_and_incr) (&mcnt, &p);
  797. # ifdef _LIBC
  798. printf ("/on_failure_jump to %td", p + mcnt - start);
  799. # else
  800. printf ("/on_failure_jump to %ld", (long int) (p + mcnt - start));
  801. # endif
  802. break;
  803. case on_failure_keep_string_jump:
  804. PREFIX(extract_number_and_incr) (&mcnt, &p);
  805. # ifdef _LIBC
  806. printf ("/on_failure_keep_string_jump to %td", p + mcnt - start);
  807. # else
  808. printf ("/on_failure_keep_string_jump to %ld",
  809. (long int) (p + mcnt - start));
  810. # endif
  811. break;
  812. case dummy_failure_jump:
  813. PREFIX(extract_number_and_incr) (&mcnt, &p);
  814. # ifdef _LIBC
  815. printf ("/dummy_failure_jump to %td", p + mcnt - start);
  816. # else
  817. printf ("/dummy_failure_jump to %ld", (long int) (p + mcnt - start));
  818. # endif
  819. break;
  820. case push_dummy_failure:
  821. printf ("/push_dummy_failure");
  822. break;
  823. case maybe_pop_jump:
  824. PREFIX(extract_number_and_incr) (&mcnt, &p);
  825. # ifdef _LIBC
  826. printf ("/maybe_pop_jump to %td", p + mcnt - start);
  827. # else
  828. printf ("/maybe_pop_jump to %ld", (long int) (p + mcnt - start));
  829. # endif
  830. break;
  831. case pop_failure_jump:
  832. PREFIX(extract_number_and_incr) (&mcnt, &p);
  833. # ifdef _LIBC
  834. printf ("/pop_failure_jump to %td", p + mcnt - start);
  835. # else
  836. printf ("/pop_failure_jump to %ld", (long int) (p + mcnt - start));
  837. # endif
  838. break;
  839. case jump_past_alt:
  840. PREFIX(extract_number_and_incr) (&mcnt, &p);
  841. # ifdef _LIBC
  842. printf ("/jump_past_alt to %td", p + mcnt - start);
  843. # else
  844. printf ("/jump_past_alt to %ld", (long int) (p + mcnt - start));
  845. # endif
  846. break;
  847. case jump:
  848. PREFIX(extract_number_and_incr) (&mcnt, &p);
  849. # ifdef _LIBC
  850. printf ("/jump to %td", p + mcnt - start);
  851. # else
  852. printf ("/jump to %ld", (long int) (p + mcnt - start));
  853. # endif
  854. break;
  855. case succeed_n:
  856. PREFIX(extract_number_and_incr) (&mcnt, &p);
  857. p1 = p + mcnt;
  858. PREFIX(extract_number_and_incr) (&mcnt2, &p);
  859. # ifdef _LIBC
  860. printf ("/succeed_n to %td, %d times", p1 - start, mcnt2);
  861. # else
  862. printf ("/succeed_n to %ld, %d times",
  863. (long int) (p1 - start), mcnt2);
  864. # endif
  865. break;
  866. case jump_n:
  867. PREFIX(extract_number_and_incr) (&mcnt, &p);
  868. p1 = p + mcnt;
  869. PREFIX(extract_number_and_incr) (&mcnt2, &p);
  870. printf ("/jump_n to %d, %d times", p1 - start, mcnt2);
  871. break;
  872. case set_number_at:
  873. PREFIX(extract_number_and_incr) (&mcnt, &p);
  874. p1 = p + mcnt;
  875. PREFIX(extract_number_and_incr) (&mcnt2, &p);
  876. # ifdef _LIBC
  877. printf ("/set_number_at location %td to %d", p1 - start, mcnt2);
  878. # else
  879. printf ("/set_number_at location %ld to %d",
  880. (long int) (p1 - start), mcnt2);
  881. # endif
  882. break;
  883. case wordbound:
  884. printf ("/wordbound");
  885. break;
  886. case notwordbound:
  887. printf ("/notwordbound");
  888. break;
  889. case wordbeg:
  890. printf ("/wordbeg");
  891. break;
  892. case wordend:
  893. printf ("/wordend");
  894. break;
  895. # ifdef emacs
  896. case before_dot:
  897. printf ("/before_dot");
  898. break;
  899. case at_dot:
  900. printf ("/at_dot");
  901. break;
  902. case after_dot:
  903. printf ("/after_dot");
  904. break;
  905. case syntaxspec:
  906. printf ("/syntaxspec");
  907. mcnt = *p++;
  908. printf ("/%d", mcnt);
  909. break;
  910. case notsyntaxspec:
  911. printf ("/notsyntaxspec");
  912. mcnt = *p++;
  913. printf ("/%d", mcnt);
  914. break;
  915. # endif /* emacs */
  916. case wordchar:
  917. printf ("/wordchar");
  918. break;
  919. case notwordchar:
  920. printf ("/notwordchar");
  921. break;
  922. case begbuf:
  923. printf ("/begbuf");
  924. break;
  925. case endbuf:
  926. printf ("/endbuf");
  927. break;
  928. default:
  929. printf ("?%ld", (long int) *(p-1));
  930. }
  931. putchar ('\n');
  932. }
  933. # ifdef _LIBC
  934. printf ("%td:\tend of pattern.\n", p - start);
  935. # else
  936. printf ("%ld:\tend of pattern.\n", (long int) (p - start));
  937. # endif
  938. }
  939. void
  940. PREFIX(print_compiled_pattern) (struct re_pattern_buffer *bufp)
  941. {
  942. UCHAR_T *buffer = (UCHAR_T*) bufp->buffer;
  943. PREFIX(print_partial_compiled_pattern) (buffer, buffer
  944. + bufp->used / sizeof(UCHAR_T));
  945. printf ("%ld bytes used/%ld bytes allocated.\n",
  946. bufp->used, bufp->allocated);
  947. if (bufp->fastmap_accurate && bufp->fastmap)
  948. {
  949. printf ("fastmap: ");
  950. print_fastmap (bufp->fastmap);
  951. }
  952. # ifdef _LIBC
  953. printf ("re_nsub: %Zd\t", bufp->re_nsub);
  954. # else
  955. printf ("re_nsub: %ld\t", (long int) bufp->re_nsub);
  956. # endif
  957. printf ("regs_alloc: %d\t", bufp->regs_allocated);
  958. printf ("can_be_null: %d\t", bufp->can_be_null);
  959. printf ("newline_anchor: %d\n", bufp->newline_anchor);
  960. printf ("no_sub: %d\t", bufp->no_sub);
  961. printf ("not_bol: %d\t", bufp->not_bol);
  962. printf ("not_eol: %d\t", bufp->not_eol);
  963. printf ("syntax: %lx\n", bufp->syntax);
  964. /* Perhaps we should print the translate table? */
  965. }
  966. void
  967. PREFIX(print_double_string) (const CHAR_T *where, const CHAR_T *string1,
  968. int size1, const CHAR_T *string2, int size2)
  969. {
  970. int this_char;
  971. if (where == NULL)
  972. printf ("(null)");
  973. else
  974. {
  975. int cnt;
  976. if (FIRST_STRING_P (where))
  977. {
  978. for (this_char = where - string1; this_char < size1; this_char++)
  979. PUT_CHAR (string1[this_char]);
  980. where = string2;
  981. }
  982. cnt = 0;
  983. for (this_char = where - string2; this_char < size2; this_char++)
  984. {
  985. PUT_CHAR (string2[this_char]);
  986. if (++cnt > 100)
  987. {
  988. fputs ("...", stdout);
  989. break;
  990. }
  991. }
  992. }
  993. }
  994. # ifndef DEFINED_ONCE
  995. void
  996. printchar (int c)
  997. {
  998. putc (c, stderr);
  999. }
  1000. # endif
  1001. # else /* not DEBUG */
  1002. # ifndef DEFINED_ONCE
  1003. # undef assert
  1004. # define assert(e)
  1005. # define DEBUG_STATEMENT(e)
  1006. # define DEBUG_PRINT1(x)
  1007. # define DEBUG_PRINT2(x1, x2)
  1008. # define DEBUG_PRINT3(x1, x2, x3)
  1009. # define DEBUG_PRINT4(x1, x2, x3, x4)
  1010. # endif /* not DEFINED_ONCE */
  1011. # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  1012. # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  1013. # endif /* not DEBUG */
  1014. # ifdef WCHAR
  1015. /* This convert a multibyte string to a wide character string.
  1016. And write their correspondances to offset_buffer(see below)
  1017. and write whether each wchar_t is binary data to is_binary.
  1018. This assume invalid multibyte sequences as binary data.
  1019. We assume offset_buffer and is_binary is already allocated
  1020. enough space. */
  1021. static size_t convert_mbs_to_wcs (CHAR_T *dest, const unsigned char* src,
  1022. size_t len, int *offset_buffer,
  1023. char *is_binary);
  1024. static size_t
  1025. convert_mbs_to_wcs (CHAR_T *dest, const unsigned char*src, size_t len,
  1026. int *offset_buffer, char *is_binary)
  1027. /* It hold correspondances between src(char string) and
  1028. dest(wchar_t string) for optimization.
  1029. e.g. src = "xxxyzz"
  1030. dest = {'X', 'Y', 'Z'}
  1031. (each "xxx", "y" and "zz" represent one multibyte character
  1032. corresponding to 'X', 'Y' and 'Z'.)
  1033. offset_buffer = {0, 0+3("xxx"), 0+3+1("y"), 0+3+1+2("zz")}
  1034. = {0, 3, 4, 6}
  1035. */
  1036. {
  1037. wchar_t *pdest = dest;
  1038. const unsigned char *psrc = src;
  1039. size_t wc_count = 0;
  1040. mbstate_t mbs;
  1041. int i, consumed;
  1042. size_t mb_remain = len;
  1043. size_t mb_count = 0;
  1044. /* Initialize the conversion state. */
  1045. memset (&mbs, 0, sizeof (mbstate_t));
  1046. offset_buffer[0] = 0;
  1047. for( ; mb_remain > 0 ; ++wc_count, ++pdest, mb_remain -= consumed,
  1048. psrc += consumed)
  1049. {
  1050. #ifdef _LIBC
  1051. consumed = __mbrtowc (pdest, psrc, mb_remain, &mbs);
  1052. #else
  1053. consumed = mbrtowc (pdest, psrc, mb_remain, &mbs);
  1054. #endif
  1055. if (consumed <= 0)
  1056. /* failed to convert. maybe src contains binary data.
  1057. So we consume 1 byte manualy. */
  1058. {
  1059. *pdest = *psrc;
  1060. consumed = 1;
  1061. is_binary[wc_count] = TRUE;
  1062. }
  1063. else
  1064. is_binary[wc_count] = FALSE;
  1065. /* In sjis encoding, we use yen sign as escape character in
  1066. place of reverse solidus. So we convert 0x5c(yen sign in
  1067. sjis) to not 0xa5(yen sign in UCS2) but 0x5c(reverse
  1068. solidus in UCS2). */
  1069. if (consumed == 1 && (int) *psrc == 0x5c && (int) *pdest == 0xa5)
  1070. *pdest = (wchar_t) *psrc;
  1071. offset_buffer[wc_count + 1] = mb_count += consumed;
  1072. }
  1073. /* Fill remain of the buffer with sentinel. */
  1074. for (i = wc_count + 1 ; i <= len ; i++)
  1075. offset_buffer[i] = mb_count + 1;
  1076. return wc_count;
  1077. }
  1078. # endif /* WCHAR */
  1079. #else /* not INSIDE_RECURSION */
  1080. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  1081. also be assigned to arbitrarily: each pattern buffer stores its own
  1082. syntax, so it can be changed between regex compilations. */
  1083. /* This has no initializer because initialized variables in Emacs
  1084. become read-only after dumping. */
  1085. reg_syntax_t re_syntax_options;
  1086. /* Specify the precise syntax of regexps for compilation. This provides
  1087. for compatibility for various utilities which historically have
  1088. different, incompatible syntaxes.
  1089. The argument SYNTAX is a bit mask comprised of the various bits
  1090. defined in regex.h. We return the old syntax. */
  1091. reg_syntax_t
  1092. re_set_syntax (reg_syntax_t syntax)
  1093. {
  1094. reg_syntax_t ret = re_syntax_options;
  1095. re_syntax_options = syntax;
  1096. # ifdef DEBUG
  1097. if (syntax & RE_DEBUG)
  1098. debug = 1;
  1099. else if (debug) /* was on but now is not */
  1100. debug = 0;
  1101. # endif /* DEBUG */
  1102. return ret;
  1103. }
  1104. # ifdef _LIBC
  1105. weak_alias (__re_set_syntax, re_set_syntax)
  1106. # endif
  1107. /* This table gives an error message for each of the error codes listed
  1108. in regex.h. Obviously the order here has to be same as there.
  1109. POSIX doesn't require that we do anything for REG_NOERROR,
  1110. but why not be nice? */
  1111. static const char *re_error_msgid[] =
  1112. {
  1113. gettext_noop ("Success"), /* REG_NOERROR */
  1114. gettext_noop ("No match"), /* REG_NOMATCH */
  1115. gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
  1116. gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
  1117. gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
  1118. gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
  1119. gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
  1120. gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
  1121. gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
  1122. gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
  1123. gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
  1124. gettext_noop ("Invalid range end"), /* REG_ERANGE */
  1125. gettext_noop ("Memory exhausted"), /* REG_ESPACE */
  1126. gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
  1127. gettext_noop ("Premature end of regular expression"), /* REG_EEND */
  1128. gettext_noop ("Regular expression too big"), /* REG_ESIZE */
  1129. gettext_noop ("Unmatched ) or \\)") /* REG_ERPAREN */
  1130. };
  1131. #endif /* INSIDE_RECURSION */
  1132. #ifndef DEFINED_ONCE
  1133. /* Avoiding alloca during matching, to placate r_alloc. */
  1134. /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
  1135. searching and matching functions should not call alloca. On some
  1136. systems, alloca is implemented in terms of malloc, and if we're
  1137. using the relocating allocator routines, then malloc could cause a
  1138. relocation, which might (if the strings being searched are in the
  1139. ralloc heap) shift the data out from underneath the regexp
  1140. routines.
  1141. Here's another reason to avoid allocation: Emacs
  1142. processes input from X in a signal handler; processing X input may
  1143. call malloc; if input arrives while a matching routine is calling
  1144. malloc, then we're scrod. But Emacs can't just block input while
  1145. calling matching routines; then we don't notice interrupts when
  1146. they come in. So, Emacs blocks input around all regexp calls
  1147. except the matching calls, which it leaves unprotected, in the
  1148. faith that they will not malloc. */
  1149. /* Normally, this is fine. */
  1150. # define MATCH_MAY_ALLOCATE
  1151. /* When using GNU C, we are not REALLY using the C alloca, no matter
  1152. what config.h may say. So don't take precautions for it. */
  1153. # ifdef __GNUC__
  1154. # undef C_ALLOCA
  1155. # endif
  1156. /* The match routines may not allocate if (1) they would do it with malloc
  1157. and (2) it's not safe for them to use malloc.
  1158. Note that if REL_ALLOC is defined, matching would not use malloc for the
  1159. failure stack, but we would still use it for the register vectors;
  1160. so REL_ALLOC should not affect this. */
  1161. # if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
  1162. # undef MATCH_MAY_ALLOCATE
  1163. # endif
  1164. #endif /* not DEFINED_ONCE */
  1165. #ifdef INSIDE_RECURSION
  1166. /* Failure stack declarations and macros; both re_compile_fastmap and
  1167. re_match_2 use a failure stack. These have to be macros because of
  1168. REGEX_ALLOCATE_STACK. */
  1169. /* Number of failure points for which to initially allocate space
  1170. when matching. If this number is exceeded, we allocate more
  1171. space, so it is not a hard limit. */
  1172. # ifndef INIT_FAILURE_ALLOC
  1173. # define INIT_FAILURE_ALLOC 5
  1174. # endif
  1175. /* Roughly the maximum number of failure points on the stack. Would be
  1176. exactly that if always used MAX_FAILURE_ITEMS items each time we failed.
  1177. This is a variable only so users of regex can assign to it; we never
  1178. change it ourselves. */
  1179. # ifdef INT_IS_16BIT
  1180. # ifndef DEFINED_ONCE
  1181. # if defined MATCH_MAY_ALLOCATE
  1182. /* 4400 was enough to cause a crash on Alpha OSF/1,
  1183. whose default stack limit is 2mb. */
  1184. long int re_max_failures = 4000;
  1185. # else
  1186. long int re_max_failures = 2000;
  1187. # endif
  1188. # endif
  1189. union PREFIX(fail_stack_elt)
  1190. {
  1191. UCHAR_T *pointer;
  1192. long int integer;
  1193. };
  1194. typedef union PREFIX(fail_stack_elt) PREFIX(fail_stack_elt_t);
  1195. typedef struct
  1196. {
  1197. PREFIX(fail_stack_elt_t) *stack;
  1198. unsigned long int size;
  1199. unsigned long int avail; /* Offset of next open position. */
  1200. } PREFIX(fail_stack_type);
  1201. # else /* not INT_IS_16BIT */
  1202. # ifndef DEFINED_ONCE
  1203. # if defined MATCH_MAY_ALLOCATE
  1204. /* 4400 was enough to cause a crash on Alpha OSF/1,
  1205. whose default stack limit is 2mb. */
  1206. int re_max_failures = 4000;
  1207. # else
  1208. int re_max_failures = 2000;
  1209. # endif
  1210. # endif
  1211. union PREFIX(fail_stack_elt)
  1212. {
  1213. UCHAR_T *pointer;
  1214. int integer;
  1215. };
  1216. typedef union PREFIX(fail_stack_elt) PREFIX(fail_stack_elt_t);
  1217. typedef struct
  1218. {
  1219. PREFIX(fail_stack_elt_t) *stack;
  1220. unsigned size;
  1221. unsigned avail; /* Offset of next open position. */
  1222. } PREFIX(fail_stack_type);
  1223. # endif /* INT_IS_16BIT */
  1224. # ifndef DEFINED_ONCE
  1225. # define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
  1226. # define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  1227. # define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
  1228. # endif
  1229. /* Define macros to initialize and free the failure stack.
  1230. Do `return -2' if the alloc fails. */
  1231. # ifdef MATCH_MAY_ALLOCATE
  1232. # define INIT_FAIL_STACK() \
  1233. do { \
  1234. fail_stack.stack = (PREFIX(fail_stack_elt_t) *) \
  1235. REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (PREFIX(fail_stack_elt_t))); \
  1236. \
  1237. if (fail_stack.stack == NULL) \
  1238. return -2; \
  1239. \
  1240. fail_stack.size = INIT_FAILURE_ALLOC; \
  1241. fail_stack.avail = 0; \
  1242. } while (0)
  1243. # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
  1244. # else
  1245. # define INIT_FAIL_STACK() \
  1246. do { \
  1247. fail_stack.avail = 0; \
  1248. } while (0)
  1249. # define RESET_FAIL_STACK()
  1250. # endif
  1251. /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
  1252. Return 1 if succeeds, and 0 if either ran out of memory
  1253. allocating space for it or it was already too large.
  1254. REGEX_REALLOCATE_STACK requires `destination' be declared. */
  1255. # define DOUBLE_FAIL_STACK(fail_stack) \
  1256. ((fail_stack).size > (unsigned) (re_max_failures * MAX_FAILURE_ITEMS) \
  1257. ? 0 \
  1258. : ((fail_stack).stack = (PREFIX(fail_stack_elt_t) *) \
  1259. REGEX_REALLOCATE_STACK ((fail_stack).stack, \
  1260. (fail_stack).size * sizeof (PREFIX(fail_stack_elt_t)), \
  1261. ((fail_stack).size << 1) * sizeof (PREFIX(fail_stack_elt_t))),\
  1262. \
  1263. (fail_stack).stack == NULL \
  1264. ? 0 \
  1265. : ((fail_stack).size <<= 1, \
  1266. 1)))
  1267. /* Push pointer POINTER on FAIL_STACK.
  1268. Return 1 if was able to do so and 0 if ran out of memory allocating
  1269. space to do so. */
  1270. # define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
  1271. ((FAIL_STACK_FULL () \
  1272. && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
  1273. ? 0 \
  1274. : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
  1275. 1))
  1276. /* Push a pointer value onto the failure stack.
  1277. Assumes the variable `fail_stack'. Probably should only
  1278. be called from within `PUSH_FAILURE_POINT'. */
  1279. # define PUSH_FAILURE_POINTER(item) \
  1280. fail_stack.stack[fail_stack.avail++].pointer = (UCHAR_T *) (item)
  1281. /* This pushes an integer-valued item onto the failure stack.
  1282. Assumes the variable `fail_stack'. Probably should only
  1283. be called from within `PUSH_FAILURE_POINT'. */
  1284. # define PUSH_FAILURE_INT(item) \
  1285. fail_stack.stack[fail_stack.avail++].integer = (item)
  1286. /* Push a fail_stack_elt_t value onto the failure stack.
  1287. Assumes the variable `fail_stack'. Probably should only
  1288. be called from within `PUSH_FAILURE_POINT'. */
  1289. # define PUSH_FAILURE_ELT(item) \
  1290. fail_stack.stack[fail_stack.avail++] = (item)
  1291. /* These three POP... operations complement the three PUSH... operations.
  1292. All assume that `fail_stack' is nonempty. */
  1293. # define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
  1294. # define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
  1295. # define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
  1296. /* Used to omit pushing failure point id's when we're not debugging. */
  1297. # ifdef DEBUG
  1298. # define DEBUG_PUSH PUSH_FAILURE_INT
  1299. # define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
  1300. # else
  1301. # define DEBUG_PUSH(item)
  1302. # define DEBUG_POP(item_addr)
  1303. # endif
  1304. /* Push the information about the state we will need
  1305. if we ever fail back to it.
  1306. Requires variables fail_stack, regstart, regend, reg_info, and
  1307. num_regs_pushed be declared. DOUBLE_FAIL_STACK requires `destination'
  1308. be declared.
  1309. Does `return FAILURE_CODE' if runs out of memory. */
  1310. # define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
  1311. do { \
  1312. char *destination; \
  1313. /* Must be int, so when we don't save any registers, the arithmetic \
  1314. of 0 + -1 isn't done as unsigned. */ \
  1315. /* Can't be int, since there is not a shred of a guarantee that int \
  1316. is wide enough to hold a value of something to which pointer can \
  1317. be assigned */ \
  1318. active_reg_t this_reg; \
  1319. \
  1320. DEBUG_STATEMENT (failure_id++); \
  1321. DEBUG_STATEMENT (nfailure_points_pushed++); \
  1322. DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
  1323. DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
  1324. DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
  1325. \
  1326. DEBUG_PRINT2 (" slots needed: %ld\n", NUM_FAILURE_ITEMS); \
  1327. DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
  1328. \
  1329. /* Ensure we have enough space allocated for what we will push. */ \
  1330. while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
  1331. { \
  1332. if (!DOUBLE_FAIL_STACK (fail_stack)) \
  1333. return failure_code; \
  1334. \
  1335. DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
  1336. (fail_stack).size); \
  1337. DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  1338. } \
  1339. \
  1340. /* Push the info, starting with the registers. */ \
  1341. DEBUG_PRINT1 ("\n"); \
  1342. \
  1343. if (1) \
  1344. for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
  1345. this_reg++) \
  1346. { \
  1347. DEBUG_PRINT2 (" Pushing reg: %lu\n", this_reg); \
  1348. DEBUG_STATEMENT (num_regs_pushed++); \
  1349. \
  1350. DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
  1351. PUSH_FAILURE_POINTER (regstart[this_reg]); \
  1352. \
  1353. DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
  1354. PUSH_FAILURE_POINTER (regend[this_reg]); \
  1355. \
  1356. DEBUG_PRINT2 (" info: %p\n ", \
  1357. reg_info[this_reg].word.pointer); \
  1358. DEBUG_PRINT2 (" match_null=%d", \
  1359. REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
  1360. DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
  1361. DEBUG_PRINT2 (" matched_something=%d", \
  1362. MATCHED_SOMETHING (reg_info[this_reg])); \
  1363. DEBUG_PRINT2 (" ever_matched=%d", \
  1364. EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
  1365. DEBUG_PRINT1 ("\n"); \
  1366. PUSH_FAILURE_ELT (reg_info[this_reg].word); \
  1367. } \
  1368. \
  1369. DEBUG_PRINT2 (" Pushing low active reg: %ld\n", lowest_active_reg);\
  1370. PUSH_FAILURE_INT (lowest_active_reg); \
  1371. \
  1372. DEBUG_PRINT2 (" Pushing high active reg: %ld\n", highest_active_reg);\
  1373. PUSH_FAILURE_INT (highest_active_reg); \
  1374. \
  1375. DEBUG_PRINT2 (" Pushing pattern %p:\n", pattern_place); \
  1376. DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
  1377. PUSH_FAILURE_POINTER (pattern_place); \
  1378. \
  1379. DEBUG_PRINT2 (" Pushing string %p: `", string_place); \
  1380. DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
  1381. size2); \
  1382. DEBUG_PRINT1 ("'\n"); \
  1383. PUSH_FAILURE_POINTER (string_place); \
  1384. \
  1385. DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
  1386. DEBUG_PUSH (failure_id); \
  1387. } while (0)
  1388. # ifndef DEFINED_ONCE
  1389. /* This is the number of items that are pushed and popped on the stack
  1390. for each register. */
  1391. # define NUM_REG_ITEMS 3
  1392. /* Individual items aside from the registers. */
  1393. # ifdef DEBUG
  1394. # define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
  1395. # else
  1396. # define NUM_NONREG_ITEMS 4
  1397. # endif
  1398. /* We push at most this many items on the stack. */
  1399. /* We used to use (num_regs - 1), which is the number of registers
  1400. this regexp will save; but that was changed to 5
  1401. to avoid stack overflow for a regexp with lots of parens. */
  1402. # define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  1403. /* We actually push this many items. */
  1404. # define NUM_FAILURE_ITEMS \
  1405. (((0 \
  1406. ? 0 : highest_active_reg - lowest_active_reg + 1) \
  1407. * NUM_REG_ITEMS) \
  1408. + NUM_NONREG_ITEMS)
  1409. /* How many items can still be added to the stack without overflowing it. */
  1410. # define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  1411. # endif /* not DEFINED_ONCE */
  1412. /* Pops what PUSH_FAIL_STACK pushes.
  1413. We restore into the parameters, all of which should be lvalues:
  1414. STR -- the saved data position.
  1415. PAT -- the saved pattern position.
  1416. LOW_REG, HIGH_REG -- the highest and lowest active registers.
  1417. REGSTART, REGEND -- arrays of string positions.
  1418. REG_INFO -- array of information about each subexpression.
  1419. Also assumes the variables `fail_stack' and (if debugging), `bufp',
  1420. `pend', `string1', `size1', `string2', and `size2'. */
  1421. # define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  1422. { \
  1423. DEBUG_STATEMENT (unsigned failure_id;) \
  1424. active_reg_t this_reg; \
  1425. const UCHAR_T *string_temp; \
  1426. \
  1427. assert (!FAIL_STACK_EMPTY ()); \
  1428. \
  1429. /* Remove failure points and point to how many regs pushed. */ \
  1430. DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
  1431. DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
  1432. DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
  1433. \
  1434. assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
  1435. \
  1436. DEBUG_POP (&failure_id); \
  1437. DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
  1438. \
  1439. /* If the saved string location is NULL, it came from an \
  1440. on_failure_keep_string_jump opcode, and we want to throw away the \
  1441. saved NULL, thus retaining our current position in the string. */ \
  1442. string_temp = POP_FAILURE_POINTER (); \
  1443. if (string_temp != NULL) \
  1444. str = (const CHAR_T *) string_temp; \
  1445. \
  1446. DEBUG_PRINT2 (" Popping string %p: `", str); \
  1447. DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
  1448. DEBUG_PRINT1 ("'\n"); \
  1449. \
  1450. pat = (UCHAR_T *) POP_FAILURE_POINTER (); \
  1451. DEBUG_PRINT2 (" Popping pattern %p:\n", pat); \
  1452. DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
  1453. \
  1454. /* Restore register info. */ \
  1455. high_reg = (active_reg_t) POP_FAILURE_INT (); \
  1456. DEBUG_PRINT2 (" Popping high active reg: %ld\n", high_reg); \
  1457. \
  1458. low_reg = (active_reg_t) POP_FAILURE_INT (); \
  1459. DEBUG_PRINT2 (" Popping low active reg: %ld\n", low_reg); \
  1460. \
  1461. if (1) \
  1462. for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
  1463. { \
  1464. DEBUG_PRINT2 (" Popping reg: %ld\n", this_reg); \
  1465. \
  1466. reg_info[this_reg].word = POP_FAILURE_ELT (); \
  1467. DEBUG_PRINT2 (" info: %p\n", \
  1468. reg_info[this_reg].word.pointer); \
  1469. \
  1470. regend[this_reg] = (const CHAR_T *) POP_FAILURE_POINTER (); \
  1471. DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
  1472. \
  1473. regstart[this_reg] = (const CHAR_T *) POP_FAILURE_POINTER (); \
  1474. DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
  1475. } \
  1476. else \
  1477. { \
  1478. for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
  1479. { \
  1480. reg_info[this_reg].word.integer = 0; \
  1481. regend[this_reg] = 0; \
  1482. regstart[this_reg] = 0; \
  1483. } \
  1484. highest_active_reg = high_reg; \
  1485. } \
  1486. \
  1487. set_regs_matched_done = 0; \
  1488. DEBUG_STATEMENT (nfailure_points_popped++); \
  1489. } /* POP_FAILURE_POINT */
  1490. /* Structure for per-register (a.k.a. per-group) information.
  1491. Other register information, such as the
  1492. starting and ending positions (which are addresses), and the list of
  1493. inner groups (which is a bits list) are maintained in separate
  1494. variables.
  1495. We are making a (strictly speaking) nonportable assumption here: that
  1496. the compiler will pack our bit fields into something that fits into
  1497. the type of `word', i.e., is something that fits into one item on the
  1498. failure stack. */
  1499. /* Declarations and macros for re_match_2. */
  1500. typedef union
  1501. {
  1502. PREFIX(fail_stack_elt_t) word;
  1503. struct
  1504. {
  1505. /* This field is one if this group can match the empty string,
  1506. zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
  1507. # define MATCH_NULL_UNSET_VALUE 3
  1508. unsigned match_null_string_p : 2;
  1509. unsigned is_active : 1;
  1510. unsigned matched_something : 1;
  1511. unsigned ever_matched_something : 1;
  1512. } bits;
  1513. } PREFIX(register_info_type);
  1514. # ifndef DEFINED_ONCE
  1515. # define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
  1516. # define IS_ACTIVE(R) ((R).bits.is_active)
  1517. # define MATCHED_SOMETHING(R) ((R).bits.matched_something)
  1518. # define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
  1519. /* Call this when have matched a real character; it sets `matched' flags
  1520. for the subexpressions which we are currently inside. Also records
  1521. that those subexprs have matched. */
  1522. # define SET_REGS_MATCHED() \
  1523. do \
  1524. { \
  1525. if (!set_regs_matched_done) \
  1526. { \
  1527. active_reg_t r; \
  1528. set_regs_matched_done = 1; \
  1529. for (r = lowest_active_reg; r <= highest_active_reg; r++) \
  1530. { \
  1531. MATCHED_SOMETHING (reg_info[r]) \
  1532. = EVER_MATCHED_SOMETHING (reg_info[r]) \
  1533. = 1; \
  1534. } \
  1535. } \
  1536. } \
  1537. while (0)
  1538. # endif /* not DEFINED_ONCE */
  1539. /* Registers are set to a sentinel when they haven't yet matched. */
  1540. static CHAR_T PREFIX(reg_unset_dummy);
  1541. # define REG_UNSET_VALUE (&PREFIX(reg_unset_dummy))
  1542. # define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  1543. /* Subroutine declarations and macros for regex_compile. */
  1544. static void PREFIX(store_op1) (re_opcode_t op, UCHAR_T *loc, int arg);
  1545. static void PREFIX(store_op2) (re_opcode_t op, UCHAR_T *loc,
  1546. int arg1, int arg2);
  1547. static void PREFIX(insert_op1) (re_opcode_t op, UCHAR_T *loc,
  1548. int arg, UCHAR_T *end);
  1549. static void PREFIX(insert_op2) (re_opcode_t op, UCHAR_T *loc,
  1550. int arg1, int arg2, UCHAR_T *end);
  1551. static boolean PREFIX(at_begline_loc_p) (const CHAR_T *pattern,
  1552. const CHAR_T *p,
  1553. reg_syntax_t syntax);
  1554. static boolean PREFIX(at_endline_loc_p) (const CHAR_T *p,
  1555. const CHAR_T *pend,
  1556. reg_syntax_t syntax);
  1557. # ifdef WCHAR
  1558. static reg_errcode_t wcs_compile_range (CHAR_T range_start,
  1559. const CHAR_T **p_ptr,
  1560. const CHAR_T *pend,
  1561. char *translate,
  1562. reg_syntax_t syntax,
  1563. UCHAR_T *b,
  1564. CHAR_T *char_set);
  1565. static void insert_space (int num, CHAR_T *loc, CHAR_T *end);
  1566. # else /* BYTE */
  1567. static reg_errcode_t byte_compile_range (unsigned int range_start,
  1568. const char **p_ptr,
  1569. const char *pend,
  1570. char *translate,
  1571. reg_syntax_t syntax,
  1572. unsigned char *b);
  1573. # endif /* WCHAR */
  1574. /* Fetch the next character in the uncompiled pattern---translating it
  1575. if necessary. Also cast from a signed character in the constant
  1576. string passed to us by the user to an unsigned char that we can use
  1577. as an array index (in, e.g., `translate'). */
  1578. /* ifdef MBS_SUPPORT, we translate only if character <= 0xff,
  1579. because it is impossible to allocate 4GB array for some encodings
  1580. which have 4 byte character_set like UCS4. */
  1581. # ifndef PATFETCH
  1582. # ifdef WCHAR
  1583. # define PATFETCH(c) \
  1584. do {if (p == pend) return REG_EEND; \
  1585. c = (UCHAR_T) *p++; \
  1586. if (translate && (c <= 0xff)) c = (UCHAR_T) translate[c]; \
  1587. } while (0)
  1588. # else /* BYTE */
  1589. # define PATFETCH(c) \
  1590. do {if (p == pend) return REG_EEND; \
  1591. c = (unsigned char) *p++; \
  1592. if (translate) c = (unsigned char) translate[c]; \
  1593. } while (0)
  1594. # endif /* WCHAR */
  1595. # endif
  1596. /* Fetch the next character in the uncompiled pattern, with no
  1597. translation. */
  1598. # define PATFETCH_RAW(c) \
  1599. do {if (p == pend) return REG_EEND; \
  1600. c = (UCHAR_T) *p++; \
  1601. } while (0)
  1602. /* Go backwards one character in the pattern. */
  1603. # define PATUNFETCH p--
  1604. /* If `translate' is non-null, return translate[D], else just D. We
  1605. cast the subscript to translate because some data is declared as
  1606. `char *', to avoid warnings when a string constant is passed. But
  1607. when we use a character as a subscript we must make it unsigned. */
  1608. /* ifdef MBS_SUPPORT, we translate only if character <= 0xff,
  1609. because it is impossible to allocate 4GB array for some encodings
  1610. which have 4 byte character_set like UCS4. */
  1611. # ifndef TRANSLATE
  1612. # ifdef WCHAR
  1613. # define TRANSLATE(d) \
  1614. ((translate && ((UCHAR_T) (d)) <= 0xff) \
  1615. ? (char) translate[(unsigned char) (d)] : (d))
  1616. # else /* BYTE */
  1617. # define TRANSLATE(d) \
  1618. (translate ? (char) translate[(unsigned char) (d)] : (char) (d))
  1619. # endif /* WCHAR */
  1620. # endif
  1621. /* Macros for outputting the compiled pattern into `buffer'. */
  1622. /* If the buffer isn't allocated when it comes in, use this. */
  1623. # define INIT_BUF_SIZE (32 * sizeof(UCHAR_T))
  1624. /* Make sure we have at least N more bytes of space in buffer. */
  1625. # ifdef WCHAR
  1626. # define GET_BUFFER_SPACE(n) \
  1627. while (((unsigned long)b - (unsigned long)COMPILED_BUFFER_VAR \
  1628. + (n)*sizeof(CHAR_T)) > bufp->allocated) \
  1629. EXTEND_BUFFER ()
  1630. # else /* BYTE */
  1631. # define GET_BUFFER_SPACE(n) \
  1632. while ((unsigned long) (b - bufp->buffer + (n)) > bufp->allocated) \
  1633. EXTEND_BUFFER ()
  1634. # endif /* WCHAR */
  1635. /* Make sure we have one more byte of buffer space and then add C to it. */
  1636. # define BUF_PUSH(c) \
  1637. do { \
  1638. GET_BUFFER_SPACE (1); \
  1639. *b++ = (UCHAR_T) (c); \
  1640. } while (0)
  1641. /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
  1642. # define BUF_PUSH_2(c1, c2) \
  1643. do { \
  1644. GET_BUFFER_SPACE (2); \
  1645. *b++ = (UCHAR_T) (c1); \
  1646. *b++ = (UCHAR_T) (c2); \
  1647. } while (0)
  1648. /* As with BUF_PUSH_2, except for three bytes. */
  1649. # define BUF_PUSH_3(c1, c2, c3) \
  1650. do { \
  1651. GET_BUFFER_SPACE (3); \
  1652. *b++ = (UCHAR_T) (c1); \
  1653. *b++ = (UCHAR_T) (c2); \
  1654. *b++ = (UCHAR_T) (c3); \
  1655. } while (0)
  1656. /* Store a jump with opcode OP at LOC to location TO. We store a
  1657. relative address offset by the three bytes the jump itself occupies. */
  1658. # define STORE_JUMP(op, loc, to) \
  1659. PREFIX(store_op1) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)))
  1660. /* Likewise, for a two-argument jump. */
  1661. # define STORE_JUMP2(op, loc, to, arg) \
  1662. PREFIX(store_op2) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)), arg)
  1663. /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
  1664. # define INSERT_JUMP(op, loc, to) \
  1665. PREFIX(insert_op1) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)), b)
  1666. /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
  1667. # define INSERT_JUMP2(op, loc, to, arg) \
  1668. PREFIX(insert_op2) (op, loc, (int) ((to) - (loc) - (1 + OFFSET_ADDRESS_SIZE)),\
  1669. arg, b)
  1670. /* This is not an arbitrary limit: the arguments which represent offsets
  1671. into the pattern are two bytes long. So if 2^16 bytes turns out to
  1672. be too small, many things would have to change. */
  1673. /* Any other compiler which, like MSC, has allocation limit below 2^16
  1674. bytes will have to use approach similar to what was done below for
  1675. MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
  1676. reallocating to 0 bytes. Such thing is not going to work too well.
  1677. You have been warned!! */
  1678. # ifndef DEFINED_ONCE
  1679. # if defined _MSC_VER && !defined WIN32
  1680. /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes.
  1681. The REALLOC define eliminates a flurry of conversion warnings,
  1682. but is not required. */
  1683. # define MAX_BUF_SIZE 65500L
  1684. # define REALLOC(p,s) realloc ((p), (size_t) (s))
  1685. # else
  1686. # define MAX_BUF_SIZE (1L << 16)
  1687. # define REALLOC(p,s) realloc ((p), (s))
  1688. # endif
  1689. /* Extend the buffer by twice its current size via realloc and
  1690. reset the pointers that pointed into the old block to point to the
  1691. correct places in the new one. If extending the buffer results in it
  1692. being larger than MAX_BUF_SIZE, then flag memory exhausted. */
  1693. # if __BOUNDED_POINTERS__
  1694. # define SET_HIGH_BOUND(P) (__ptrhigh (P) = __ptrlow (P) + bufp->allocated)
  1695. # define MOVE_BUFFER_POINTER(P) \
  1696. (__ptrlow (P) += incr, SET_HIGH_BOUND (P), __ptrvalue (P) += incr)
  1697. # define ELSE_EXTEND_BUFFER_HIGH_BOUND \
  1698. else \
  1699. { \
  1700. SET_HIGH_BOUND (b); \
  1701. SET_HIGH_BOUND (begalt); \
  1702. if (fixup_alt_jump) \
  1703. SET_HIGH_BOUND (fixup_alt_jump); \
  1704. if (laststart) \
  1705. SET_HIGH_BOUND (laststart); \
  1706. if (pending_exact) \
  1707. SET_HIGH_BOUND (pending_exact); \
  1708. }
  1709. # else
  1710. # define MOVE_BUFFER_POINTER(P) (P) += incr
  1711. # define ELSE_EXTEND_BUFFER_HIGH_BOUND
  1712. # endif
  1713. # endif /* not DEFINED_ONCE */
  1714. # ifdef WCHAR
  1715. # define EXTEND_BUFFER() \
  1716. do { \
  1717. UCHAR_T *old_buffer = COMPILED_BUFFER_VAR; \
  1718. int wchar_count; \
  1719. if (bufp->allocated + sizeof(UCHAR_T) > MAX_BUF_SIZE) \
  1720. return REG_ESIZE; \
  1721. bufp->allocated <<= 1; \
  1722. if (bufp->allocated > MAX_BUF_SIZE) \
  1723. bufp->allocated = MAX_BUF_SIZE; \
  1724. /* How many characters the new buffer can have? */ \
  1725. wchar_count = bufp->allocated / sizeof(UCHAR_T); \
  1726. if (wchar_count == 0) wchar_count = 1; \
  1727. /* Truncate the buffer to CHAR_T align. */ \
  1728. bufp->allocated = wchar_count * sizeof(UCHAR_T); \
  1729. RETALLOC (COMPILED_BUFFER_VAR, wchar_count, UCHAR_T); \
  1730. bufp->buffer = (char*)COMPILED_BUFFER_VAR; \
  1731. if (COMPILED_BUFFER_VAR == NULL) \
  1732. return REG_ESPACE; \
  1733. /* If the buffer moved, move all the pointers into it. */ \
  1734. if (old_buffer != COMPILED_BUFFER_VAR) \
  1735. { \
  1736. PTR_INT_TYPE incr = COMPILED_BUFFER_VAR - old_buffer; \
  1737. MOVE_BUFFER_POINTER (b); \
  1738. MOVE_BUFFER_POINTER (begalt); \
  1739. if (fixup_alt_jump) \
  1740. MOVE_BUFFER_POINTER (fixup_alt_jump); \
  1741. if (laststart) \
  1742. MOVE_BUFFER_POINTER (laststart); \
  1743. if (pending_exact) \
  1744. MOVE_BUFFER_POINTER (pending_exact); \
  1745. } \
  1746. ELSE_EXTEND_BUFFER_HIGH_BOUND \
  1747. } while (0)
  1748. # else /* BYTE */
  1749. # define EXTEND_BUFFER() \
  1750. do { \
  1751. UCHAR_T *old_buffer = COMPILED_BUFFER_VAR; \
  1752. if (bufp->allocated == MAX_BUF_SIZE) \
  1753. return REG_ESIZE; \
  1754. bufp->allocated <<= 1; \
  1755. if (bufp->allocated > MAX_BUF_SIZE) \
  1756. bufp->allocated = MAX_BUF_SIZE; \
  1757. bufp->buffer = (UCHAR_T *) REALLOC (COMPILED_BUFFER_VAR, \
  1758. bufp->allocated); \
  1759. if (COMPILED_BUFFER_VAR == NULL) \
  1760. return REG_ESPACE; \
  1761. /* If the buffer moved, move all the pointers into it. */ \
  1762. if (old_buffer != COMPILED_BUFFER_VAR) \
  1763. { \
  1764. PTR_INT_TYPE incr = COMPILED_BUFFER_VAR - old_buffer; \
  1765. MOVE_BUFFER_POINTER (b); \
  1766. MOVE_BUFFER_POINTER (begalt); \
  1767. if (fixup_alt_jump) \
  1768. MOVE_BUFFER_POINTER (fixup_alt_jump); \
  1769. if (laststart) \
  1770. MOVE_BUFFER_POINTER (laststart); \
  1771. if (pending_exact) \
  1772. MOVE_BUFFER_POINTER (pending_exact); \
  1773. } \
  1774. ELSE_EXTEND_BUFFER_HIGH_BOUND \
  1775. } while (0)
  1776. # endif /* WCHAR */
  1777. # ifndef DEFINED_ONCE
  1778. /* Since we have one byte reserved for the register number argument to
  1779. {start,stop}_memory, the maximum number of groups we can report
  1780. things about is what fits in that byte. */
  1781. # define MAX_REGNUM 255
  1782. /* But patterns can have more than `MAX_REGNUM' registers. We just
  1783. ignore the excess. */
  1784. typedef unsigned regnum_t;
  1785. /* Macros for the compile stack. */
  1786. /* Since offsets can go either forwards or backwards, this type needs to
  1787. be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
  1788. /* int may be not enough when sizeof(int) == 2. */
  1789. typedef long pattern_offset_t;
  1790. typedef struct
  1791. {
  1792. pattern_offset_t begalt_offset;
  1793. pattern_offset_t fixup_alt_jump;
  1794. pattern_offset_t inner_group_offset;
  1795. pattern_offset_t laststart_offset;
  1796. regnum_t regnum;
  1797. } compile_stack_elt_t;
  1798. typedef struct
  1799. {
  1800. compile_stack_elt_t *stack;
  1801. unsigned size;
  1802. unsigned avail; /* Offset of next open position. */
  1803. } compile_stack_type;
  1804. # define INIT_COMPILE_STACK_SIZE 32
  1805. # define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
  1806. # define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
  1807. /* The next available element. */
  1808. # define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1809. # endif /* not DEFINED_ONCE */
  1810. /* Set the bit for character C in a list. */
  1811. # ifndef DEFINED_ONCE
  1812. # define SET_LIST_BIT(c) \
  1813. (b[((unsigned char) (c)) / BYTEWIDTH] \
  1814. |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1815. # endif /* DEFINED_ONCE */
  1816. /* Get the next unsigned number in the uncompiled pattern. */
  1817. # define GET_UNSIGNED_NUMBER(num) \
  1818. { \
  1819. while (p != pend) \
  1820. { \
  1821. PATFETCH (c); \
  1822. if (c < '0' || c > '9') \
  1823. break; \
  1824. if (num <= RE_DUP_MAX) \
  1825. { \
  1826. if (num < 0) \
  1827. num = 0; \
  1828. num = num * 10 + c - '0'; \
  1829. } \
  1830. } \
  1831. }
  1832. # ifndef DEFINED_ONCE
  1833. # if defined _LIBC || WIDE_CHAR_SUPPORT
  1834. /* The GNU C library provides support for user-defined character classes
  1835. and the functions from ISO C amendement 1. */
  1836. # ifdef CHARCLASS_NAME_MAX
  1837. # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
  1838. # else
  1839. /* This shouldn't happen but some implementation might still have this
  1840. problem. Use a reasonable default value. */
  1841. # define CHAR_CLASS_MAX_LENGTH 256
  1842. # endif
  1843. # ifdef _LIBC
  1844. # define IS_CHAR_CLASS(string) __wctype (string)
  1845. # else
  1846. # define IS_CHAR_CLASS(string) wctype (string)
  1847. # endif
  1848. # else
  1849. # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  1850. # define IS_CHAR_CLASS(string) \
  1851. (STREQ (string, "alpha") || STREQ (string, "upper") \
  1852. || STREQ (string, "lower") || STREQ (string, "digit") \
  1853. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  1854. || STREQ (string, "space") || STREQ (string, "print") \
  1855. || STREQ (string, "punct") || STREQ (string, "graph") \
  1856. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1857. # endif
  1858. # endif /* DEFINED_ONCE */
  1859. # ifndef MATCH_MAY_ALLOCATE
  1860. /* If we cannot allocate large objects within re_match_2_internal,
  1861. we make the fail stack and register vectors global.
  1862. The fail stack, we grow to the maximum size when a regexp
  1863. is compiled.
  1864. The register vectors, we adjust in size each time we
  1865. compile a regexp, according to the number of registers it needs. */
  1866. static PREFIX(fail_stack_type) fail_stack;
  1867. /* Size with which the following vectors are currently allocated.
  1868. That is so we can make them bigger as needed,
  1869. but never make them smaller. */
  1870. # ifdef DEFINED_ONCE
  1871. static int regs_allocated_size;
  1872. static const char ** regstart, ** regend;
  1873. static const char ** old_regstart, ** old_regend;
  1874. static const char **best_regstart, **best_regend;
  1875. static const char **reg_dummy;
  1876. # endif /* DEFINED_ONCE */
  1877. static PREFIX(register_info_type) *PREFIX(reg_info);
  1878. static PREFIX(register_info_type) *PREFIX(reg_info_dummy);
  1879. /* Make the register vectors big enough for NUM_REGS registers,
  1880. but don't make them smaller. */
  1881. static void
  1882. PREFIX(regex_grow_registers) (int num_regs)
  1883. {
  1884. if (num_regs > regs_allocated_size)
  1885. {
  1886. RETALLOC_IF (regstart, num_regs, const char *);
  1887. RETALLOC_IF (regend, num_regs, const char *);
  1888. RETALLOC_IF (old_regstart, num_regs, const char *);
  1889. RETALLOC_IF (old_regend, num_regs, const char *);
  1890. RETALLOC_IF (best_regstart, num_regs, const char *);
  1891. RETALLOC_IF (best_regend, num_regs, const char *);
  1892. RETALLOC_IF (PREFIX(reg_info), num_regs, PREFIX(register_info_type));
  1893. RETALLOC_IF (reg_dummy, num_regs, const char *);
  1894. RETALLOC_IF (PREFIX(reg_info_dummy), num_regs, PREFIX(register_info_type));
  1895. regs_allocated_size = num_regs;
  1896. }
  1897. }
  1898. # endif /* not MATCH_MAY_ALLOCATE */
  1899. # ifndef DEFINED_ONCE
  1900. static boolean group_in_compile_stack (compile_stack_type compile_stack,
  1901. regnum_t regnum);
  1902. # endif /* not DEFINED_ONCE */
  1903. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1904. Returns one of error codes defined in `regex.h', or zero for success.
  1905. Assumes the `allocated' (and perhaps `buffer') and `translate'
  1906. fields are set in BUFP on entry.
  1907. If it succeeds, results are put in BUFP (if it returns an error, the
  1908. contents of BUFP are undefined):
  1909. `buffer' is the compiled pattern;
  1910. `syntax' is set to SYNTAX;
  1911. `used' is set to the length of the compiled pattern;
  1912. `fastmap_accurate' is zero;
  1913. `re_nsub' is the number of subexpressions in PATTERN;
  1914. `not_bol' and `not_eol' are zero;
  1915. The `fastmap' and `newline_anchor' fields are neither
  1916. examined nor set. */
  1917. /* Return, freeing storage we allocated. */
  1918. # ifdef WCHAR
  1919. # define FREE_STACK_RETURN(value) \
  1920. return (free(pattern), free(mbs_offset), free(is_binary), free (compile_stack.stack), value)
  1921. # else
  1922. # define FREE_STACK_RETURN(value) \
  1923. return (free (compile_stack.stack), value)
  1924. # endif /* WCHAR */
  1925. static reg_errcode_t
  1926. PREFIX(regex_compile) (const char *ARG_PREFIX(pattern),
  1927. size_t ARG_PREFIX(size), reg_syntax_t syntax,
  1928. struct re_pattern_buffer *bufp)
  1929. {
  1930. /* We fetch characters from PATTERN here. Even though PATTERN is
  1931. `char *' (i.e., signed), we declare these variables as unsigned, so
  1932. they can be reliably used as array indices. */
  1933. register UCHAR_T c, c1;
  1934. #ifdef WCHAR
  1935. /* A temporary space to keep wchar_t pattern and compiled pattern. */
  1936. CHAR_T *pattern, *COMPILED_BUFFER_VAR;
  1937. size_t size;
  1938. /* offset buffer for optimization. See convert_mbs_to_wc. */
  1939. int *mbs_offset = NULL;
  1940. /* It hold whether each wchar_t is binary data or not. */
  1941. char *is_binary = NULL;
  1942. /* A flag whether exactn is handling binary data or not. */
  1943. char is_exactn_bin = FALSE;
  1944. #endif /* WCHAR */
  1945. /* A random temporary spot in PATTERN. */
  1946. const CHAR_T *p1;
  1947. /* Points to the end of the buffer, where we should append. */
  1948. register UCHAR_T *b;
  1949. /* Keeps track of unclosed groups. */
  1950. compile_stack_type compile_stack;
  1951. /* Points to the current (ending) position in the pattern. */
  1952. #ifdef WCHAR
  1953. const CHAR_T *p;
  1954. const CHAR_T *pend;
  1955. #else /* BYTE */
  1956. const CHAR_T *p = pattern;
  1957. const CHAR_T *pend = pattern + size;
  1958. #endif /* WCHAR */
  1959. /* How to translate the characters in the pattern. */
  1960. RE_TRANSLATE_TYPE translate = bufp->translate;
  1961. /* Address of the count-byte of the most recently inserted `exactn'
  1962. command. This makes it possible to tell if a new exact-match
  1963. character can be added to that command or if the character requires
  1964. a new `exactn' command. */
  1965. UCHAR_T *pending_exact = 0;
  1966. /* Address of start of the most recently finished expression.
  1967. This tells, e.g., postfix * where to find the start of its
  1968. operand. Reset at the beginning of groups and alternatives. */
  1969. UCHAR_T *laststart = 0;
  1970. /* Address of beginning of regexp, or inside of last group. */
  1971. UCHAR_T *begalt;
  1972. /* Address of the place where a forward jump should go to the end of
  1973. the containing expression. Each alternative of an `or' -- except the
  1974. last -- ends with a forward jump of this sort. */
  1975. UCHAR_T *fixup_alt_jump = 0;
  1976. /* Counts open-groups as they are encountered. Remembered for the
  1977. matching close-group on the compile stack, so the same register
  1978. number is put in the stop_memory as the start_memory. */
  1979. regnum_t regnum = 0;
  1980. #ifdef WCHAR
  1981. /* Initialize the wchar_t PATTERN and offset_buffer. */
  1982. p = pend = pattern = TALLOC(csize + 1, CHAR_T);
  1983. mbs_offset = TALLOC(csize + 1, int);
  1984. is_binary = TALLOC(csize + 1, char);
  1985. if (pattern == NULL || mbs_offset == NULL || is_binary == NULL)
  1986. {
  1987. free(pattern);
  1988. free(mbs_offset);
  1989. free(is_binary);
  1990. return REG_ESPACE;
  1991. }
  1992. pattern[csize] = L'\0'; /* sentinel */
  1993. size = convert_mbs_to_wcs(pattern, cpattern, csize, mbs_offset, is_binary);
  1994. pend = p + size;
  1995. if (size < 0)
  1996. {
  1997. free(pattern);
  1998. free(mbs_offset);
  1999. free(is_binary);
  2000. return REG_BADPAT;
  2001. }
  2002. #endif
  2003. #ifdef DEBUG
  2004. DEBUG_PRINT1 ("\nCompiling pattern: ");
  2005. if (debug)
  2006. {
  2007. unsigned debug_count;
  2008. for (debug_count = 0; debug_count < size; debug_count++)
  2009. PUT_CHAR (pattern[debug_count]);
  2010. putchar ('\n');
  2011. }
  2012. #endif /* DEBUG */
  2013. /* Initialize the compile stack. */
  2014. compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  2015. if (compile_stack.stack == NULL)
  2016. {
  2017. #ifdef WCHAR
  2018. free(pattern);
  2019. free(mbs_offset);
  2020. free(is_binary);
  2021. #endif
  2022. return REG_ESPACE;
  2023. }
  2024. compile_stack.size = INIT_COMPILE_STACK_SIZE;
  2025. compile_stack.avail = 0;
  2026. /* Initialize the pattern buffer. */
  2027. bufp->syntax = syntax;
  2028. bufp->fastmap_accurate = 0;
  2029. bufp->not_bol = bufp->not_eol = 0;
  2030. /* Set `used' to zero, so that if we return an error, the pattern
  2031. printer (for debugging) will think there's no pattern. We reset it
  2032. at the end. */
  2033. bufp->used = 0;
  2034. /* Always count groups, whether or not bufp->no_sub is set. */
  2035. bufp->re_nsub = 0;
  2036. #if !defined emacs && !defined SYNTAX_TABLE
  2037. /* Initialize the syntax table. */
  2038. init_syntax_once ();
  2039. #endif
  2040. if (bufp->allocated == 0)
  2041. {
  2042. if (bufp->buffer)
  2043. { /* If zero allocated, but buffer is non-null, try to realloc
  2044. enough space. This loses if buffer's address is bogus, but
  2045. that is the user's responsibility. */
  2046. #ifdef WCHAR
  2047. /* Free bufp->buffer and allocate an array for wchar_t pattern
  2048. buffer. */
  2049. free(bufp->buffer);
  2050. COMPILED_BUFFER_VAR = TALLOC (INIT_BUF_SIZE/sizeof(UCHAR_T),
  2051. UCHAR_T);
  2052. #else
  2053. RETALLOC (COMPILED_BUFFER_VAR, INIT_BUF_SIZE, UCHAR_T);
  2054. #endif /* WCHAR */
  2055. }
  2056. else
  2057. { /* Caller did not allocate a buffer. Do it for them. */
  2058. COMPILED_BUFFER_VAR = TALLOC (INIT_BUF_SIZE / sizeof(UCHAR_T),
  2059. UCHAR_T);
  2060. }
  2061. if (!COMPILED_BUFFER_VAR) FREE_STACK_RETURN (REG_ESPACE);
  2062. #ifdef WCHAR
  2063. bufp->buffer = (char*)COMPILED_BUFFER_VAR;
  2064. #endif /* WCHAR */
  2065. bufp->allocated = INIT_BUF_SIZE;
  2066. }
  2067. #ifdef WCHAR
  2068. else
  2069. COMPILED_BUFFER_VAR = (UCHAR_T*) bufp->buffer;
  2070. #endif
  2071. begalt = b = COMPILED_BUFFER_VAR;
  2072. /* Loop through the uncompiled pattern until we're at the end. */
  2073. while (p != pend)
  2074. {
  2075. PATFETCH (c);
  2076. switch (c)
  2077. {
  2078. case '^':
  2079. {
  2080. if ( /* If at start of pattern, it's an operator. */
  2081. p == pattern + 1
  2082. /* If context independent, it's an operator. */
  2083. || syntax & RE_CONTEXT_INDEP_ANCHORS
  2084. /* Otherwise, depends on what's come before. */
  2085. || PREFIX(at_begline_loc_p) (pattern, p, syntax))
  2086. BUF_PUSH (begline);
  2087. else
  2088. goto normal_char;
  2089. }
  2090. break;
  2091. case '$':
  2092. {
  2093. if ( /* If at end of pattern, it's an operator. */
  2094. p == pend
  2095. /* If context independent, it's an operator. */
  2096. || syntax & RE_CONTEXT_INDEP_ANCHORS
  2097. /* Otherwise, depends on what's next. */
  2098. || PREFIX(at_endline_loc_p) (p, pend, syntax))
  2099. BUF_PUSH (endline);
  2100. else
  2101. goto normal_char;
  2102. }
  2103. break;
  2104. case '+':
  2105. case '?':
  2106. if ((syntax & RE_BK_PLUS_QM)
  2107. || (syntax & RE_LIMITED_OPS))
  2108. goto normal_char;
  2109. /* Fall through. */
  2110. handle_plus:
  2111. case '*':
  2112. /* If there is no previous pattern... */
  2113. if (!laststart)
  2114. {
  2115. if (syntax & RE_CONTEXT_INVALID_OPS)
  2116. FREE_STACK_RETURN (REG_BADRPT);
  2117. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  2118. goto normal_char;
  2119. }
  2120. {
  2121. /* Are we optimizing this jump? */
  2122. boolean keep_string_p = false;
  2123. /* 1 means zero (many) matches is allowed. */
  2124. char zero_times_ok = 0, many_times_ok = 0;
  2125. /* If there is a sequence of repetition chars, collapse it
  2126. down to just one (the right one). We can't combine
  2127. interval operators with these because of, e.g., `a{2}*',
  2128. which should only match an even number of `a's. */
  2129. for (;;)
  2130. {
  2131. zero_times_ok |= c != '+';
  2132. many_times_ok |= c != '?';
  2133. if (p == pend)
  2134. break;
  2135. PATFETCH (c);
  2136. if (c == '*'
  2137. || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  2138. ;
  2139. else if (syntax & RE_BK_PLUS_QM && c == '\\')
  2140. {
  2141. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2142. PATFETCH (c1);
  2143. if (!(c1 == '+' || c1 == '?'))
  2144. {
  2145. PATUNFETCH;
  2146. PATUNFETCH;
  2147. break;
  2148. }
  2149. c = c1;
  2150. }
  2151. else
  2152. {
  2153. PATUNFETCH;
  2154. break;
  2155. }
  2156. /* If we get here, we found another repeat character. */
  2157. }
  2158. /* Star, etc. applied to an empty pattern is equivalent
  2159. to an empty pattern. */
  2160. if (!laststart)
  2161. break;
  2162. /* Now we know whether or not zero matches is allowed
  2163. and also whether or not two or more matches is allowed. */
  2164. if (many_times_ok)
  2165. { /* More than one repetition is allowed, so put in at the
  2166. end a backward relative jump from `b' to before the next
  2167. jump we're going to put in below (which jumps from
  2168. laststart to after this jump).
  2169. But if we are at the `*' in the exact sequence `.*\n',
  2170. insert an unconditional jump backwards to the .,
  2171. instead of the beginning of the loop. This way we only
  2172. push a failure point once, instead of every time
  2173. through the loop. */
  2174. assert (p - 1 > pattern);
  2175. /* Allocate the space for the jump. */
  2176. GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
  2177. /* We know we are not at the first character of the pattern,
  2178. because laststart was nonzero. And we've already
  2179. incremented `p', by the way, to be the character after
  2180. the `*'. Do we have to do something analogous here
  2181. for null bytes, because of RE_DOT_NOT_NULL? */
  2182. if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  2183. && zero_times_ok
  2184. && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  2185. && !(syntax & RE_DOT_NEWLINE))
  2186. { /* We have .*\n. */
  2187. STORE_JUMP (jump, b, laststart);
  2188. keep_string_p = true;
  2189. }
  2190. else
  2191. /* Anything else. */
  2192. STORE_JUMP (maybe_pop_jump, b, laststart -
  2193. (1 + OFFSET_ADDRESS_SIZE));
  2194. /* We've added more stuff to the buffer. */
  2195. b += 1 + OFFSET_ADDRESS_SIZE;
  2196. }
  2197. /* On failure, jump from laststart to b + 3, which will be the
  2198. end of the buffer after this jump is inserted. */
  2199. /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE' instead of
  2200. 'b + 3'. */
  2201. GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
  2202. INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  2203. : on_failure_jump,
  2204. laststart, b + 1 + OFFSET_ADDRESS_SIZE);
  2205. pending_exact = 0;
  2206. b += 1 + OFFSET_ADDRESS_SIZE;
  2207. if (!zero_times_ok)
  2208. {
  2209. /* At least one repetition is required, so insert a
  2210. `dummy_failure_jump' before the initial
  2211. `on_failure_jump' instruction of the loop. This
  2212. effects a skip over that instruction the first time
  2213. we hit that loop. */
  2214. GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
  2215. INSERT_JUMP (dummy_failure_jump, laststart, laststart +
  2216. 2 + 2 * OFFSET_ADDRESS_SIZE);
  2217. b += 1 + OFFSET_ADDRESS_SIZE;
  2218. }
  2219. }
  2220. break;
  2221. case '.':
  2222. laststart = b;
  2223. BUF_PUSH (anychar);
  2224. break;
  2225. case '[':
  2226. {
  2227. boolean had_char_class = false;
  2228. #ifdef WCHAR
  2229. CHAR_T range_start = 0xffffffff;
  2230. #else
  2231. unsigned int range_start = 0xffffffff;
  2232. #endif
  2233. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2234. #ifdef WCHAR
  2235. /* We assume a charset(_not) structure as a wchar_t array.
  2236. charset[0] = (re_opcode_t) charset(_not)
  2237. charset[1] = l (= length of char_classes)
  2238. charset[2] = m (= length of collating_symbols)
  2239. charset[3] = n (= length of equivalence_classes)
  2240. charset[4] = o (= length of char_ranges)
  2241. charset[5] = p (= length of chars)
  2242. charset[6] = char_class (wctype_t)
  2243. charset[6+CHAR_CLASS_SIZE] = char_class (wctype_t)
  2244. ...
  2245. charset[l+5] = char_class (wctype_t)
  2246. charset[l+6] = collating_symbol (wchar_t)
  2247. ...
  2248. charset[l+m+5] = collating_symbol (wchar_t)
  2249. ifdef _LIBC we use the index if
  2250. _NL_COLLATE_SYMB_EXTRAMB instead of
  2251. wchar_t string.
  2252. charset[l+m+6] = equivalence_classes (wchar_t)
  2253. ...
  2254. charset[l+m+n+5] = equivalence_classes (wchar_t)
  2255. ifdef _LIBC we use the index in
  2256. _NL_COLLATE_WEIGHT instead of
  2257. wchar_t string.
  2258. charset[l+m+n+6] = range_start
  2259. charset[l+m+n+7] = range_end
  2260. ...
  2261. charset[l+m+n+2o+4] = range_start
  2262. charset[l+m+n+2o+5] = range_end
  2263. ifdef _LIBC we use the value looked up
  2264. in _NL_COLLATE_COLLSEQ instead of
  2265. wchar_t character.
  2266. charset[l+m+n+2o+6] = char
  2267. ...
  2268. charset[l+m+n+2o+p+5] = char
  2269. */
  2270. /* We need at least 6 spaces: the opcode, the length of
  2271. char_classes, the length of collating_symbols, the length of
  2272. equivalence_classes, the length of char_ranges, the length of
  2273. chars. */
  2274. GET_BUFFER_SPACE (6);
  2275. /* Save b as laststart. And We use laststart as the pointer
  2276. to the first element of the charset here.
  2277. In other words, laststart[i] indicates charset[i]. */
  2278. laststart = b;
  2279. /* We test `*p == '^' twice, instead of using an if
  2280. statement, so we only need one BUF_PUSH. */
  2281. BUF_PUSH (*p == '^' ? charset_not : charset);
  2282. if (*p == '^')
  2283. p++;
  2284. /* Push the length of char_classes, the length of
  2285. collating_symbols, the length of equivalence_classes, the
  2286. length of char_ranges and the length of chars. */
  2287. BUF_PUSH_3 (0, 0, 0);
  2288. BUF_PUSH_2 (0, 0);
  2289. /* Remember the first position in the bracket expression. */
  2290. p1 = p;
  2291. /* charset_not matches newline according to a syntax bit. */
  2292. if ((re_opcode_t) b[-6] == charset_not
  2293. && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  2294. {
  2295. BUF_PUSH('\n');
  2296. laststart[5]++; /* Update the length of characters */
  2297. }
  2298. /* Read in characters and ranges, setting map bits. */
  2299. for (;;)
  2300. {
  2301. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2302. PATFETCH (c);
  2303. /* \ might escape characters inside [...] and [^...]. */
  2304. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  2305. {
  2306. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2307. PATFETCH (c1);
  2308. BUF_PUSH(c1);
  2309. laststart[5]++; /* Update the length of chars */
  2310. range_start = c1;
  2311. continue;
  2312. }
  2313. /* Could be the end of the bracket expression. If it's
  2314. not (i.e., when the bracket expression is `[]' so
  2315. far), the ']' character bit gets set way below. */
  2316. if (c == ']' && p != p1 + 1)
  2317. break;
  2318. /* Look ahead to see if it's a range when the last thing
  2319. was a character class. */
  2320. if (had_char_class && c == '-' && *p != ']')
  2321. FREE_STACK_RETURN (REG_ERANGE);
  2322. /* Look ahead to see if it's a range when the last thing
  2323. was a character: if this is a hyphen not at the
  2324. beginning or the end of a list, then it's the range
  2325. operator. */
  2326. if (c == '-'
  2327. && !(p - 2 >= pattern && p[-2] == '[')
  2328. && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  2329. && *p != ']')
  2330. {
  2331. reg_errcode_t ret;
  2332. /* Allocate the space for range_start and range_end. */
  2333. GET_BUFFER_SPACE (2);
  2334. /* Update the pointer to indicate end of buffer. */
  2335. b += 2;
  2336. ret = wcs_compile_range (range_start, &p, pend, translate,
  2337. syntax, b, laststart);
  2338. if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  2339. range_start = 0xffffffff;
  2340. }
  2341. else if (p[0] == '-' && p[1] != ']')
  2342. { /* This handles ranges made up of characters only. */
  2343. reg_errcode_t ret;
  2344. /* Move past the `-'. */
  2345. PATFETCH (c1);
  2346. /* Allocate the space for range_start and range_end. */
  2347. GET_BUFFER_SPACE (2);
  2348. /* Update the pointer to indicate end of buffer. */
  2349. b += 2;
  2350. ret = wcs_compile_range (c, &p, pend, translate, syntax, b,
  2351. laststart);
  2352. if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  2353. range_start = 0xffffffff;
  2354. }
  2355. /* See if we're at the beginning of a possible character
  2356. class. */
  2357. else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  2358. { /* Leave room for the null. */
  2359. char str[CHAR_CLASS_MAX_LENGTH + 1];
  2360. PATFETCH (c);
  2361. c1 = 0;
  2362. /* If pattern is `[[:'. */
  2363. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2364. for (;;)
  2365. {
  2366. PATFETCH (c);
  2367. if ((c == ':' && *p == ']') || p == pend)
  2368. break;
  2369. if (c1 < CHAR_CLASS_MAX_LENGTH)
  2370. str[c1++] = c;
  2371. else
  2372. /* This is in any case an invalid class name. */
  2373. str[0] = '\0';
  2374. }
  2375. str[c1] = '\0';
  2376. /* If isn't a word bracketed by `[:' and `:]':
  2377. undo the ending character, the letters, and leave
  2378. the leading `:' and `[' (but store them as character). */
  2379. if (c == ':' && *p == ']')
  2380. {
  2381. wctype_t wt;
  2382. uintptr_t alignedp;
  2383. /* Query the character class as wctype_t. */
  2384. wt = IS_CHAR_CLASS (str);
  2385. if (wt == 0)
  2386. FREE_STACK_RETURN (REG_ECTYPE);
  2387. /* Throw away the ] at the end of the character
  2388. class. */
  2389. PATFETCH (c);
  2390. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2391. /* Allocate the space for character class. */
  2392. GET_BUFFER_SPACE(CHAR_CLASS_SIZE);
  2393. /* Update the pointer to indicate end of buffer. */
  2394. b += CHAR_CLASS_SIZE;
  2395. /* Move data which follow character classes
  2396. not to violate the data. */
  2397. insert_space(CHAR_CLASS_SIZE,
  2398. laststart + 6 + laststart[1],
  2399. b - 1);
  2400. alignedp = ((uintptr_t)(laststart + 6 + laststart[1])
  2401. + __alignof__(wctype_t) - 1)
  2402. & ~(uintptr_t)(__alignof__(wctype_t) - 1);
  2403. /* Store the character class. */
  2404. *((wctype_t*)alignedp) = wt;
  2405. /* Update length of char_classes */
  2406. laststart[1] += CHAR_CLASS_SIZE;
  2407. had_char_class = true;
  2408. }
  2409. else
  2410. {
  2411. c1++;
  2412. while (c1--)
  2413. PATUNFETCH;
  2414. BUF_PUSH ('[');
  2415. BUF_PUSH (':');
  2416. laststart[5] += 2; /* Update the length of characters */
  2417. range_start = ':';
  2418. had_char_class = false;
  2419. }
  2420. }
  2421. else if (syntax & RE_CHAR_CLASSES && c == '[' && (*p == '='
  2422. || *p == '.'))
  2423. {
  2424. CHAR_T str[128]; /* Should be large enough. */
  2425. CHAR_T delim = *p; /* '=' or '.' */
  2426. # ifdef _LIBC
  2427. uint32_t nrules =
  2428. _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  2429. # endif
  2430. PATFETCH (c);
  2431. c1 = 0;
  2432. /* If pattern is `[[=' or '[[.'. */
  2433. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2434. for (;;)
  2435. {
  2436. PATFETCH (c);
  2437. if ((c == delim && *p == ']') || p == pend)
  2438. break;
  2439. if (c1 < sizeof (str) - 1)
  2440. str[c1++] = c;
  2441. else
  2442. /* This is in any case an invalid class name. */
  2443. str[0] = '\0';
  2444. }
  2445. str[c1] = '\0';
  2446. if (c == delim && *p == ']' && str[0] != '\0')
  2447. {
  2448. unsigned int i, offset;
  2449. /* If we have no collation data we use the default
  2450. collation in which each character is in a class
  2451. by itself. It also means that ASCII is the
  2452. character set and therefore we cannot have character
  2453. with more than one byte in the multibyte
  2454. representation. */
  2455. /* If not defined _LIBC, we push the name and
  2456. `\0' for the sake of matching performance. */
  2457. int datasize = c1 + 1;
  2458. # ifdef _LIBC
  2459. int32_t idx = 0;
  2460. if (nrules == 0)
  2461. # endif
  2462. {
  2463. if (c1 != 1)
  2464. FREE_STACK_RETURN (REG_ECOLLATE);
  2465. }
  2466. # ifdef _LIBC
  2467. else
  2468. {
  2469. const int32_t *table;
  2470. const int32_t *weights;
  2471. const int32_t *extra;
  2472. const int32_t *indirect;
  2473. wint_t *cp;
  2474. /* This #include defines a local function! */
  2475. # include <locale/weightwc.h>
  2476. if(delim == '=')
  2477. {
  2478. /* We push the index for equivalence class. */
  2479. cp = (wint_t*)str;
  2480. table = (const int32_t *)
  2481. _NL_CURRENT (LC_COLLATE,
  2482. _NL_COLLATE_TABLEWC);
  2483. weights = (const int32_t *)
  2484. _NL_CURRENT (LC_COLLATE,
  2485. _NL_COLLATE_WEIGHTWC);
  2486. extra = (const int32_t *)
  2487. _NL_CURRENT (LC_COLLATE,
  2488. _NL_COLLATE_EXTRAWC);
  2489. indirect = (const int32_t *)
  2490. _NL_CURRENT (LC_COLLATE,
  2491. _NL_COLLATE_INDIRECTWC);
  2492. idx = findidx ((const wint_t**)&cp);
  2493. if (idx == 0 || cp < (wint_t*) str + c1)
  2494. /* This is no valid character. */
  2495. FREE_STACK_RETURN (REG_ECOLLATE);
  2496. str[0] = (wchar_t)idx;
  2497. }
  2498. else /* delim == '.' */
  2499. {
  2500. /* We push collation sequence value
  2501. for collating symbol. */
  2502. int32_t table_size;
  2503. const int32_t *symb_table;
  2504. const unsigned char *extra;
  2505. int32_t idx;
  2506. int32_t elem;
  2507. int32_t second;
  2508. int32_t hash;
  2509. char char_str[c1];
  2510. /* We have to convert the name to a single-byte
  2511. string. This is possible since the names
  2512. consist of ASCII characters and the internal
  2513. representation is UCS4. */
  2514. for (i = 0; i < c1; ++i)
  2515. char_str[i] = str[i];
  2516. table_size =
  2517. _NL_CURRENT_WORD (LC_COLLATE,
  2518. _NL_COLLATE_SYMB_HASH_SIZEMB);
  2519. symb_table = (const int32_t *)
  2520. _NL_CURRENT (LC_COLLATE,
  2521. _NL_COLLATE_SYMB_TABLEMB);
  2522. extra = (const unsigned char *)
  2523. _NL_CURRENT (LC_COLLATE,
  2524. _NL_COLLATE_SYMB_EXTRAMB);
  2525. /* Locate the character in the hashing table. */
  2526. hash = elem_hash (char_str, c1);
  2527. idx = 0;
  2528. elem = hash % table_size;
  2529. second = hash % (table_size - 2);
  2530. while (symb_table[2 * elem] != 0)
  2531. {
  2532. /* First compare the hashing value. */
  2533. if (symb_table[2 * elem] == hash
  2534. && c1 == extra[symb_table[2 * elem + 1]]
  2535. && memcmp (char_str,
  2536. &extra[symb_table[2 * elem + 1]
  2537. + 1], c1) == 0)
  2538. {
  2539. /* Yep, this is the entry. */
  2540. idx = symb_table[2 * elem + 1];
  2541. idx += 1 + extra[idx];
  2542. break;
  2543. }
  2544. /* Next entry. */
  2545. elem += second;
  2546. }
  2547. if (symb_table[2 * elem] != 0)
  2548. {
  2549. /* Compute the index of the byte sequence
  2550. in the table. */
  2551. idx += 1 + extra[idx];
  2552. /* Adjust for the alignment. */
  2553. idx = (idx + 3) & ~3;
  2554. str[0] = (wchar_t) idx + 4;
  2555. }
  2556. else if (symb_table[2 * elem] == 0 && c1 == 1)
  2557. {
  2558. /* No valid character. Match it as a
  2559. single byte character. */
  2560. had_char_class = false;
  2561. BUF_PUSH(str[0]);
  2562. /* Update the length of characters */
  2563. laststart[5]++;
  2564. range_start = str[0];
  2565. /* Throw away the ] at the end of the
  2566. collating symbol. */
  2567. PATFETCH (c);
  2568. /* exit from the switch block. */
  2569. continue;
  2570. }
  2571. else
  2572. FREE_STACK_RETURN (REG_ECOLLATE);
  2573. }
  2574. datasize = 1;
  2575. }
  2576. # endif
  2577. /* Throw away the ] at the end of the equivalence
  2578. class (or collating symbol). */
  2579. PATFETCH (c);
  2580. /* Allocate the space for the equivalence class
  2581. (or collating symbol) (and '\0' if needed). */
  2582. GET_BUFFER_SPACE(datasize);
  2583. /* Update the pointer to indicate end of buffer. */
  2584. b += datasize;
  2585. if (delim == '=')
  2586. { /* equivalence class */
  2587. /* Calculate the offset of char_ranges,
  2588. which is next to equivalence_classes. */
  2589. offset = laststart[1] + laststart[2]
  2590. + laststart[3] +6;
  2591. /* Insert space. */
  2592. insert_space(datasize, laststart + offset, b - 1);
  2593. /* Write the equivalence_class and \0. */
  2594. for (i = 0 ; i < datasize ; i++)
  2595. laststart[offset + i] = str[i];
  2596. /* Update the length of equivalence_classes. */
  2597. laststart[3] += datasize;
  2598. had_char_class = true;
  2599. }
  2600. else /* delim == '.' */
  2601. { /* collating symbol */
  2602. /* Calculate the offset of the equivalence_classes,
  2603. which is next to collating_symbols. */
  2604. offset = laststart[1] + laststart[2] + 6;
  2605. /* Insert space and write the collationg_symbol
  2606. and \0. */
  2607. insert_space(datasize, laststart + offset, b-1);
  2608. for (i = 0 ; i < datasize ; i++)
  2609. laststart[offset + i] = str[i];
  2610. /* In re_match_2_internal if range_start < -1, we
  2611. assume -range_start is the offset of the
  2612. collating symbol which is specified as
  2613. the character of the range start. So we assign
  2614. -(laststart[1] + laststart[2] + 6) to
  2615. range_start. */
  2616. range_start = -(laststart[1] + laststart[2] + 6);
  2617. /* Update the length of collating_symbol. */
  2618. laststart[2] += datasize;
  2619. had_char_class = false;
  2620. }
  2621. }
  2622. else
  2623. {
  2624. c1++;
  2625. while (c1--)
  2626. PATUNFETCH;
  2627. BUF_PUSH ('[');
  2628. BUF_PUSH (delim);
  2629. laststart[5] += 2; /* Update the length of characters */
  2630. range_start = delim;
  2631. had_char_class = false;
  2632. }
  2633. }
  2634. else
  2635. {
  2636. had_char_class = false;
  2637. BUF_PUSH(c);
  2638. laststart[5]++; /* Update the length of characters */
  2639. range_start = c;
  2640. }
  2641. }
  2642. #else /* BYTE */
  2643. /* Ensure that we have enough space to push a charset: the
  2644. opcode, the length count, and the bitset; 34 bytes in all. */
  2645. GET_BUFFER_SPACE (34);
  2646. laststart = b;
  2647. /* We test `*p == '^' twice, instead of using an if
  2648. statement, so we only need one BUF_PUSH. */
  2649. BUF_PUSH (*p == '^' ? charset_not : charset);
  2650. if (*p == '^')
  2651. p++;
  2652. /* Remember the first position in the bracket expression. */
  2653. p1 = p;
  2654. /* Push the number of bytes in the bitmap. */
  2655. BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  2656. /* Clear the whole map. */
  2657. bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  2658. /* charset_not matches newline according to a syntax bit. */
  2659. if ((re_opcode_t) b[-2] == charset_not
  2660. && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  2661. SET_LIST_BIT ('\n');
  2662. /* Read in characters and ranges, setting map bits. */
  2663. for (;;)
  2664. {
  2665. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2666. PATFETCH (c);
  2667. /* \ might escape characters inside [...] and [^...]. */
  2668. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  2669. {
  2670. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2671. PATFETCH (c1);
  2672. SET_LIST_BIT (c1);
  2673. range_start = c1;
  2674. continue;
  2675. }
  2676. /* Could be the end of the bracket expression. If it's
  2677. not (i.e., when the bracket expression is `[]' so
  2678. far), the ']' character bit gets set way below. */
  2679. if (c == ']' && p != p1 + 1)
  2680. break;
  2681. /* Look ahead to see if it's a range when the last thing
  2682. was a character class. */
  2683. if (had_char_class && c == '-' && *p != ']')
  2684. FREE_STACK_RETURN (REG_ERANGE);
  2685. /* Look ahead to see if it's a range when the last thing
  2686. was a character: if this is a hyphen not at the
  2687. beginning or the end of a list, then it's the range
  2688. operator. */
  2689. if (c == '-'
  2690. && !(p - 2 >= pattern && p[-2] == '[')
  2691. && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  2692. && *p != ']')
  2693. {
  2694. reg_errcode_t ret
  2695. = byte_compile_range (range_start, &p, pend, translate,
  2696. syntax, b);
  2697. if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  2698. range_start = 0xffffffff;
  2699. }
  2700. else if (p[0] == '-' && p[1] != ']')
  2701. { /* This handles ranges made up of characters only. */
  2702. reg_errcode_t ret;
  2703. /* Move past the `-'. */
  2704. PATFETCH (c1);
  2705. ret = byte_compile_range (c, &p, pend, translate, syntax, b);
  2706. if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  2707. range_start = 0xffffffff;
  2708. }
  2709. /* See if we're at the beginning of a possible character
  2710. class. */
  2711. else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  2712. { /* Leave room for the null. */
  2713. char str[CHAR_CLASS_MAX_LENGTH + 1];
  2714. PATFETCH (c);
  2715. c1 = 0;
  2716. /* If pattern is `[[:'. */
  2717. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2718. for (;;)
  2719. {
  2720. PATFETCH (c);
  2721. if ((c == ':' && *p == ']') || p == pend)
  2722. break;
  2723. if (c1 < CHAR_CLASS_MAX_LENGTH)
  2724. str[c1++] = c;
  2725. else
  2726. /* This is in any case an invalid class name. */
  2727. str[0] = '\0';
  2728. }
  2729. str[c1] = '\0';
  2730. /* If isn't a word bracketed by `[:' and `:]':
  2731. undo the ending character, the letters, and leave
  2732. the leading `:' and `[' (but set bits for them). */
  2733. if (c == ':' && *p == ']')
  2734. {
  2735. # if defined _LIBC || WIDE_CHAR_SUPPORT
  2736. boolean is_lower = STREQ (str, "lower");
  2737. boolean is_upper = STREQ (str, "upper");
  2738. wctype_t wt;
  2739. int ch;
  2740. wt = IS_CHAR_CLASS (str);
  2741. if (wt == 0)
  2742. FREE_STACK_RETURN (REG_ECTYPE);
  2743. /* Throw away the ] at the end of the character
  2744. class. */
  2745. PATFETCH (c);
  2746. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2747. for (ch = 0; ch < 1 << BYTEWIDTH; ++ch)
  2748. {
  2749. # ifdef _LIBC
  2750. if (__iswctype (__btowc (ch), wt))
  2751. SET_LIST_BIT (ch);
  2752. # else
  2753. if (iswctype (btowc (ch), wt))
  2754. SET_LIST_BIT (ch);
  2755. # endif
  2756. if (translate && (is_upper || is_lower)
  2757. && (ISUPPER (ch) || ISLOWER (ch)))
  2758. SET_LIST_BIT (ch);
  2759. }
  2760. had_char_class = true;
  2761. # else
  2762. int ch;
  2763. boolean is_alnum = STREQ (str, "alnum");
  2764. boolean is_alpha = STREQ (str, "alpha");
  2765. boolean is_blank = STREQ (str, "blank");
  2766. boolean is_cntrl = STREQ (str, "cntrl");
  2767. boolean is_digit = STREQ (str, "digit");
  2768. boolean is_graph = STREQ (str, "graph");
  2769. boolean is_lower = STREQ (str, "lower");
  2770. boolean is_print = STREQ (str, "print");
  2771. boolean is_punct = STREQ (str, "punct");
  2772. boolean is_space = STREQ (str, "space");
  2773. boolean is_upper = STREQ (str, "upper");
  2774. boolean is_xdigit = STREQ (str, "xdigit");
  2775. if (!IS_CHAR_CLASS (str))
  2776. FREE_STACK_RETURN (REG_ECTYPE);
  2777. /* Throw away the ] at the end of the character
  2778. class. */
  2779. PATFETCH (c);
  2780. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2781. for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  2782. {
  2783. /* This was split into 3 if's to
  2784. avoid an arbitrary limit in some compiler. */
  2785. if ( (is_alnum && ISALNUM (ch))
  2786. || (is_alpha && ISALPHA (ch))
  2787. || (is_blank && ISBLANK (ch))
  2788. || (is_cntrl && ISCNTRL (ch)))
  2789. SET_LIST_BIT (ch);
  2790. if ( (is_digit && ISDIGIT (ch))
  2791. || (is_graph && ISGRAPH (ch))
  2792. || (is_lower && ISLOWER (ch))
  2793. || (is_print && ISPRINT (ch)))
  2794. SET_LIST_BIT (ch);
  2795. if ( (is_punct && ISPUNCT (ch))
  2796. || (is_space && ISSPACE (ch))
  2797. || (is_upper && ISUPPER (ch))
  2798. || (is_xdigit && ISXDIGIT (ch)))
  2799. SET_LIST_BIT (ch);
  2800. if ( translate && (is_upper || is_lower)
  2801. && (ISUPPER (ch) || ISLOWER (ch)))
  2802. SET_LIST_BIT (ch);
  2803. }
  2804. had_char_class = true;
  2805. # endif /* libc || wctype.h */
  2806. }
  2807. else
  2808. {
  2809. c1++;
  2810. while (c1--)
  2811. PATUNFETCH;
  2812. SET_LIST_BIT ('[');
  2813. SET_LIST_BIT (':');
  2814. range_start = ':';
  2815. had_char_class = false;
  2816. }
  2817. }
  2818. else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '=')
  2819. {
  2820. unsigned char str[MB_LEN_MAX + 1];
  2821. # ifdef _LIBC
  2822. uint32_t nrules =
  2823. _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  2824. # endif
  2825. PATFETCH (c);
  2826. c1 = 0;
  2827. /* If pattern is `[[='. */
  2828. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2829. for (;;)
  2830. {
  2831. PATFETCH (c);
  2832. if ((c == '=' && *p == ']') || p == pend)
  2833. break;
  2834. if (c1 < MB_LEN_MAX)
  2835. str[c1++] = c;
  2836. else
  2837. /* This is in any case an invalid class name. */
  2838. str[0] = '\0';
  2839. }
  2840. str[c1] = '\0';
  2841. if (c == '=' && *p == ']' && str[0] != '\0')
  2842. {
  2843. /* If we have no collation data we use the default
  2844. collation in which each character is in a class
  2845. by itself. It also means that ASCII is the
  2846. character set and therefore we cannot have character
  2847. with more than one byte in the multibyte
  2848. representation. */
  2849. # ifdef _LIBC
  2850. if (nrules == 0)
  2851. # endif
  2852. {
  2853. if (c1 != 1)
  2854. FREE_STACK_RETURN (REG_ECOLLATE);
  2855. /* Throw away the ] at the end of the equivalence
  2856. class. */
  2857. PATFETCH (c);
  2858. /* Set the bit for the character. */
  2859. SET_LIST_BIT (str[0]);
  2860. }
  2861. # ifdef _LIBC
  2862. else
  2863. {
  2864. /* Try to match the byte sequence in `str' against
  2865. those known to the collate implementation.
  2866. First find out whether the bytes in `str' are
  2867. actually from exactly one character. */
  2868. const int32_t *table;
  2869. const unsigned char *weights;
  2870. const unsigned char *extra;
  2871. const int32_t *indirect;
  2872. int32_t idx;
  2873. const unsigned char *cp = str;
  2874. int ch;
  2875. /* This #include defines a local function! */
  2876. # include <locale/weight.h>
  2877. table = (const int32_t *)
  2878. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  2879. weights = (const unsigned char *)
  2880. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
  2881. extra = (const unsigned char *)
  2882. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  2883. indirect = (const int32_t *)
  2884. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
  2885. idx = findidx (&cp);
  2886. if (idx == 0 || cp < str + c1)
  2887. /* This is no valid character. */
  2888. FREE_STACK_RETURN (REG_ECOLLATE);
  2889. /* Throw away the ] at the end of the equivalence
  2890. class. */
  2891. PATFETCH (c);
  2892. /* Now we have to go through the whole table
  2893. and find all characters which have the same
  2894. first level weight.
  2895. XXX Note that this is not entirely correct.
  2896. we would have to match multibyte sequences
  2897. but this is not possible with the current
  2898. implementation. */
  2899. for (ch = 1; ch < 256; ++ch)
  2900. /* XXX This test would have to be changed if we
  2901. would allow matching multibyte sequences. */
  2902. if (table[ch] > 0)
  2903. {
  2904. int32_t idx2 = table[ch];
  2905. size_t len = weights[idx2];
  2906. /* Test whether the lenghts match. */
  2907. if (weights[idx] == len)
  2908. {
  2909. /* They do. New compare the bytes of
  2910. the weight. */
  2911. size_t cnt = 0;
  2912. while (cnt < len
  2913. && (weights[idx + 1 + cnt]
  2914. == weights[idx2 + 1 + cnt]))
  2915. ++cnt;
  2916. if (cnt == len)
  2917. /* They match. Mark the character as
  2918. acceptable. */
  2919. SET_LIST_BIT (ch);
  2920. }
  2921. }
  2922. }
  2923. # endif
  2924. had_char_class = true;
  2925. }
  2926. else
  2927. {
  2928. c1++;
  2929. while (c1--)
  2930. PATUNFETCH;
  2931. SET_LIST_BIT ('[');
  2932. SET_LIST_BIT ('=');
  2933. range_start = '=';
  2934. had_char_class = false;
  2935. }
  2936. }
  2937. else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == '.')
  2938. {
  2939. unsigned char str[128]; /* Should be large enough. */
  2940. # ifdef _LIBC
  2941. uint32_t nrules =
  2942. _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  2943. # endif
  2944. PATFETCH (c);
  2945. c1 = 0;
  2946. /* If pattern is `[[.'. */
  2947. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2948. for (;;)
  2949. {
  2950. PATFETCH (c);
  2951. if ((c == '.' && *p == ']') || p == pend)
  2952. break;
  2953. if (c1 < sizeof (str))
  2954. str[c1++] = c;
  2955. else
  2956. /* This is in any case an invalid class name. */
  2957. str[0] = '\0';
  2958. }
  2959. str[c1] = '\0';
  2960. if (c == '.' && *p == ']' && str[0] != '\0')
  2961. {
  2962. /* If we have no collation data we use the default
  2963. collation in which each character is the name
  2964. for its own class which contains only the one
  2965. character. It also means that ASCII is the
  2966. character set and therefore we cannot have character
  2967. with more than one byte in the multibyte
  2968. representation. */
  2969. # ifdef _LIBC
  2970. if (nrules == 0)
  2971. # endif
  2972. {
  2973. if (c1 != 1)
  2974. FREE_STACK_RETURN (REG_ECOLLATE);
  2975. /* Throw away the ] at the end of the equivalence
  2976. class. */
  2977. PATFETCH (c);
  2978. /* Set the bit for the character. */
  2979. SET_LIST_BIT (str[0]);
  2980. range_start = ((const unsigned char *) str)[0];
  2981. }
  2982. # ifdef _LIBC
  2983. else
  2984. {
  2985. /* Try to match the byte sequence in `str' against
  2986. those known to the collate implementation.
  2987. First find out whether the bytes in `str' are
  2988. actually from exactly one character. */
  2989. int32_t table_size;
  2990. const int32_t *symb_table;
  2991. const unsigned char *extra;
  2992. int32_t idx;
  2993. int32_t elem;
  2994. int32_t second;
  2995. int32_t hash;
  2996. table_size =
  2997. _NL_CURRENT_WORD (LC_COLLATE,
  2998. _NL_COLLATE_SYMB_HASH_SIZEMB);
  2999. symb_table = (const int32_t *)
  3000. _NL_CURRENT (LC_COLLATE,
  3001. _NL_COLLATE_SYMB_TABLEMB);
  3002. extra = (const unsigned char *)
  3003. _NL_CURRENT (LC_COLLATE,
  3004. _NL_COLLATE_SYMB_EXTRAMB);
  3005. /* Locate the character in the hashing table. */
  3006. hash = elem_hash (str, c1);
  3007. idx = 0;
  3008. elem = hash % table_size;
  3009. second = hash % (table_size - 2);
  3010. while (symb_table[2 * elem] != 0)
  3011. {
  3012. /* First compare the hashing value. */
  3013. if (symb_table[2 * elem] == hash
  3014. && c1 == extra[symb_table[2 * elem + 1]]
  3015. && memcmp (str,
  3016. &extra[symb_table[2 * elem + 1]
  3017. + 1],
  3018. c1) == 0)
  3019. {
  3020. /* Yep, this is the entry. */
  3021. idx = symb_table[2 * elem + 1];
  3022. idx += 1 + extra[idx];
  3023. break;
  3024. }
  3025. /* Next entry. */
  3026. elem += second;
  3027. }
  3028. if (symb_table[2 * elem] == 0)
  3029. /* This is no valid character. */
  3030. FREE_STACK_RETURN (REG_ECOLLATE);
  3031. /* Throw away the ] at the end of the equivalence
  3032. class. */
  3033. PATFETCH (c);
  3034. /* Now add the multibyte character(s) we found
  3035. to the accept list.
  3036. XXX Note that this is not entirely correct.
  3037. we would have to match multibyte sequences
  3038. but this is not possible with the current
  3039. implementation. Also, we have to match
  3040. collating symbols, which expand to more than
  3041. one file, as a whole and not allow the
  3042. individual bytes. */
  3043. c1 = extra[idx++];
  3044. if (c1 == 1)
  3045. range_start = extra[idx];
  3046. while (c1-- > 0)
  3047. {
  3048. SET_LIST_BIT (extra[idx]);
  3049. ++idx;
  3050. }
  3051. }
  3052. # endif
  3053. had_char_class = false;
  3054. }
  3055. else
  3056. {
  3057. c1++;
  3058. while (c1--)
  3059. PATUNFETCH;
  3060. SET_LIST_BIT ('[');
  3061. SET_LIST_BIT ('.');
  3062. range_start = '.';
  3063. had_char_class = false;
  3064. }
  3065. }
  3066. else
  3067. {
  3068. had_char_class = false;
  3069. SET_LIST_BIT (c);
  3070. range_start = c;
  3071. }
  3072. }
  3073. /* Discard any (non)matching list bytes that are all 0 at the
  3074. end of the map. Decrease the map-length byte too. */
  3075. while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  3076. b[-1]--;
  3077. b += b[-1];
  3078. #endif /* WCHAR */
  3079. }
  3080. break;
  3081. case '(':
  3082. if (syntax & RE_NO_BK_PARENS)
  3083. goto handle_open;
  3084. else
  3085. goto normal_char;
  3086. case ')':
  3087. if (syntax & RE_NO_BK_PARENS)
  3088. goto handle_close;
  3089. else
  3090. goto normal_char;
  3091. case '\n':
  3092. if (syntax & RE_NEWLINE_ALT)
  3093. goto handle_alt;
  3094. else
  3095. goto normal_char;
  3096. case '|':
  3097. if (syntax & RE_NO_BK_VBAR)
  3098. goto handle_alt;
  3099. else
  3100. goto normal_char;
  3101. case '{':
  3102. if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  3103. goto handle_interval;
  3104. else
  3105. goto normal_char;
  3106. case '\\':
  3107. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  3108. /* Do not translate the character after the \, so that we can
  3109. distinguish, e.g., \B from \b, even if we normally would
  3110. translate, e.g., B to b. */
  3111. PATFETCH_RAW (c);
  3112. switch (c)
  3113. {
  3114. case '(':
  3115. if (syntax & RE_NO_BK_PARENS)
  3116. goto normal_backslash;
  3117. handle_open:
  3118. bufp->re_nsub++;
  3119. regnum++;
  3120. if (COMPILE_STACK_FULL)
  3121. {
  3122. RETALLOC (compile_stack.stack, compile_stack.size << 1,
  3123. compile_stack_elt_t);
  3124. if (compile_stack.stack == NULL) return REG_ESPACE;
  3125. compile_stack.size <<= 1;
  3126. }
  3127. /* These are the values to restore when we hit end of this
  3128. group. They are all relative offsets, so that if the
  3129. whole pattern moves because of realloc, they will still
  3130. be valid. */
  3131. COMPILE_STACK_TOP.begalt_offset = begalt - COMPILED_BUFFER_VAR;
  3132. COMPILE_STACK_TOP.fixup_alt_jump
  3133. = fixup_alt_jump ? fixup_alt_jump - COMPILED_BUFFER_VAR + 1 : 0;
  3134. COMPILE_STACK_TOP.laststart_offset = b - COMPILED_BUFFER_VAR;
  3135. COMPILE_STACK_TOP.regnum = regnum;
  3136. /* We will eventually replace the 0 with the number of
  3137. groups inner to this one. But do not push a
  3138. start_memory for groups beyond the last one we can
  3139. represent in the compiled pattern. */
  3140. if (regnum <= MAX_REGNUM)
  3141. {
  3142. COMPILE_STACK_TOP.inner_group_offset = b
  3143. - COMPILED_BUFFER_VAR + 2;
  3144. BUF_PUSH_3 (start_memory, regnum, 0);
  3145. }
  3146. compile_stack.avail++;
  3147. fixup_alt_jump = 0;
  3148. laststart = 0;
  3149. begalt = b;
  3150. /* If we've reached MAX_REGNUM groups, then this open
  3151. won't actually generate any code, so we'll have to
  3152. clear pending_exact explicitly. */
  3153. pending_exact = 0;
  3154. break;
  3155. case ')':
  3156. if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  3157. if (COMPILE_STACK_EMPTY)
  3158. {
  3159. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  3160. goto normal_backslash;
  3161. else
  3162. FREE_STACK_RETURN (REG_ERPAREN);
  3163. }
  3164. handle_close:
  3165. if (fixup_alt_jump)
  3166. { /* Push a dummy failure point at the end of the
  3167. alternative for a possible future
  3168. `pop_failure_jump' to pop. See comments at
  3169. `push_dummy_failure' in `re_match_2'. */
  3170. BUF_PUSH (push_dummy_failure);
  3171. /* We allocated space for this jump when we assigned
  3172. to `fixup_alt_jump', in the `handle_alt' case below. */
  3173. STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  3174. }
  3175. /* See similar code for backslashed left paren above. */
  3176. if (COMPILE_STACK_EMPTY)
  3177. {
  3178. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  3179. goto normal_char;
  3180. else
  3181. FREE_STACK_RETURN (REG_ERPAREN);
  3182. }
  3183. /* Since we just checked for an empty stack above, this
  3184. ``can't happen''. */
  3185. assert (compile_stack.avail != 0);
  3186. {
  3187. /* We don't just want to restore into `regnum', because
  3188. later groups should continue to be numbered higher,
  3189. as in `(ab)c(de)' -- the second group is #2. */
  3190. regnum_t this_group_regnum;
  3191. compile_stack.avail--;
  3192. begalt = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.begalt_offset;
  3193. fixup_alt_jump
  3194. = COMPILE_STACK_TOP.fixup_alt_jump
  3195. ? COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.fixup_alt_jump - 1
  3196. : 0;
  3197. laststart = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.laststart_offset;
  3198. this_group_regnum = COMPILE_STACK_TOP.regnum;
  3199. /* If we've reached MAX_REGNUM groups, then this open
  3200. won't actually generate any code, so we'll have to
  3201. clear pending_exact explicitly. */
  3202. pending_exact = 0;
  3203. /* We're at the end of the group, so now we know how many
  3204. groups were inside this one. */
  3205. if (this_group_regnum <= MAX_REGNUM)
  3206. {
  3207. UCHAR_T *inner_group_loc
  3208. = COMPILED_BUFFER_VAR + COMPILE_STACK_TOP.inner_group_offset;
  3209. *inner_group_loc = regnum - this_group_regnum;
  3210. BUF_PUSH_3 (stop_memory, this_group_regnum,
  3211. regnum - this_group_regnum);
  3212. }
  3213. }
  3214. break;
  3215. case '|': /* `\|'. */
  3216. if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  3217. goto normal_backslash;
  3218. handle_alt:
  3219. if (syntax & RE_LIMITED_OPS)
  3220. goto normal_char;
  3221. /* Insert before the previous alternative a jump which
  3222. jumps to this alternative if the former fails. */
  3223. GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
  3224. INSERT_JUMP (on_failure_jump, begalt,
  3225. b + 2 + 2 * OFFSET_ADDRESS_SIZE);
  3226. pending_exact = 0;
  3227. b += 1 + OFFSET_ADDRESS_SIZE;
  3228. /* The alternative before this one has a jump after it
  3229. which gets executed if it gets matched. Adjust that
  3230. jump so it will jump to this alternative's analogous
  3231. jump (put in below, which in turn will jump to the next
  3232. (if any) alternative's such jump, etc.). The last such
  3233. jump jumps to the correct final destination. A picture:
  3234. _____ _____
  3235. | | | |
  3236. | v | v
  3237. a | b | c
  3238. If we are at `b', then fixup_alt_jump right now points to a
  3239. three-byte space after `a'. We'll put in the jump, set
  3240. fixup_alt_jump to right after `b', and leave behind three
  3241. bytes which we'll fill in when we get to after `c'. */
  3242. if (fixup_alt_jump)
  3243. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  3244. /* Mark and leave space for a jump after this alternative,
  3245. to be filled in later either by next alternative or
  3246. when know we're at the end of a series of alternatives. */
  3247. fixup_alt_jump = b;
  3248. GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
  3249. b += 1 + OFFSET_ADDRESS_SIZE;
  3250. laststart = 0;
  3251. begalt = b;
  3252. break;
  3253. case '{':
  3254. /* If \{ is a literal. */
  3255. if (!(syntax & RE_INTERVALS)
  3256. /* If we're at `\{' and it's not the open-interval
  3257. operator. */
  3258. || (syntax & RE_NO_BK_BRACES))
  3259. goto normal_backslash;
  3260. handle_interval:
  3261. {
  3262. /* If got here, then the syntax allows intervals. */
  3263. /* At least (most) this many matches must be made. */
  3264. int lower_bound = -1, upper_bound = -1;
  3265. /* Place in the uncompiled pattern (i.e., just after
  3266. the '{') to go back to if the interval is invalid. */
  3267. const CHAR_T *beg_interval = p;
  3268. if (p == pend)
  3269. goto invalid_interval;
  3270. GET_UNSIGNED_NUMBER (lower_bound);
  3271. if (c == ',')
  3272. {
  3273. GET_UNSIGNED_NUMBER (upper_bound);
  3274. if (upper_bound < 0)
  3275. upper_bound = RE_DUP_MAX;
  3276. }
  3277. else
  3278. /* Interval such as `{1}' => match exactly once. */
  3279. upper_bound = lower_bound;
  3280. if (! (0 <= lower_bound && lower_bound <= upper_bound))
  3281. goto invalid_interval;
  3282. if (!(syntax & RE_NO_BK_BRACES))
  3283. {
  3284. if (c != '\\' || p == pend)
  3285. goto invalid_interval;
  3286. PATFETCH (c);
  3287. }
  3288. if (c != '}')
  3289. goto invalid_interval;
  3290. /* If it's invalid to have no preceding re. */
  3291. if (!laststart)
  3292. {
  3293. if (syntax & RE_CONTEXT_INVALID_OPS
  3294. && !(syntax & RE_INVALID_INTERVAL_ORD))
  3295. FREE_STACK_RETURN (REG_BADRPT);
  3296. else if (syntax & RE_CONTEXT_INDEP_OPS)
  3297. laststart = b;
  3298. else
  3299. goto unfetch_interval;
  3300. }
  3301. /* We just parsed a valid interval. */
  3302. if (RE_DUP_MAX < upper_bound)
  3303. FREE_STACK_RETURN (REG_BADBR);
  3304. /* If the upper bound is zero, don't want to succeed at
  3305. all; jump from `laststart' to `b + 3', which will be
  3306. the end of the buffer after we insert the jump. */
  3307. /* ifdef WCHAR, 'b + 1 + OFFSET_ADDRESS_SIZE'
  3308. instead of 'b + 3'. */
  3309. if (upper_bound == 0)
  3310. {
  3311. GET_BUFFER_SPACE (1 + OFFSET_ADDRESS_SIZE);
  3312. INSERT_JUMP (jump, laststart, b + 1
  3313. + OFFSET_ADDRESS_SIZE);
  3314. b += 1 + OFFSET_ADDRESS_SIZE;
  3315. }
  3316. /* Otherwise, we have a nontrivial interval. When
  3317. we're all done, the pattern will look like:
  3318. set_number_at <jump count> <upper bound>
  3319. set_number_at <succeed_n count> <lower bound>
  3320. succeed_n <after jump addr> <succeed_n count>
  3321. <body of loop>
  3322. jump_n <succeed_n addr> <jump count>
  3323. (The upper bound and `jump_n' are omitted if
  3324. `upper_bound' is 1, though.) */
  3325. else
  3326. { /* If the upper bound is > 1, we need to insert
  3327. more at the end of the loop. */
  3328. unsigned nbytes = 2 + 4 * OFFSET_ADDRESS_SIZE +
  3329. (upper_bound > 1) * (2 + 4 * OFFSET_ADDRESS_SIZE);
  3330. GET_BUFFER_SPACE (nbytes);
  3331. /* Initialize lower bound of the `succeed_n', even
  3332. though it will be set during matching by its
  3333. attendant `set_number_at' (inserted next),
  3334. because `re_compile_fastmap' needs to know.
  3335. Jump to the `jump_n' we might insert below. */
  3336. INSERT_JUMP2 (succeed_n, laststart,
  3337. b + 1 + 2 * OFFSET_ADDRESS_SIZE
  3338. + (upper_bound > 1) * (1 + 2 * OFFSET_ADDRESS_SIZE)
  3339. , lower_bound);
  3340. b += 1 + 2 * OFFSET_ADDRESS_SIZE;
  3341. /* Code to initialize the lower bound. Insert
  3342. before the `succeed_n'. The `5' is the last two
  3343. bytes of this `set_number_at', plus 3 bytes of
  3344. the following `succeed_n'. */
  3345. /* ifdef WCHAR, The '1+2*OFFSET_ADDRESS_SIZE'
  3346. is the 'set_number_at', plus '1+OFFSET_ADDRESS_SIZE'
  3347. of the following `succeed_n'. */
  3348. PREFIX(insert_op2) (set_number_at, laststart, 1
  3349. + 2 * OFFSET_ADDRESS_SIZE, lower_bound, b);
  3350. b += 1 + 2 * OFFSET_ADDRESS_SIZE;
  3351. if (upper_bound > 1)
  3352. { /* More than one repetition is allowed, so
  3353. append a backward jump to the `succeed_n'
  3354. that starts this interval.
  3355. When we've reached this during matching,
  3356. we'll have matched the interval once, so
  3357. jump back only `upper_bound - 1' times. */
  3358. STORE_JUMP2 (jump_n, b, laststart
  3359. + 2 * OFFSET_ADDRESS_SIZE + 1,
  3360. upper_bound - 1);
  3361. b += 1 + 2 * OFFSET_ADDRESS_SIZE;
  3362. /* The location we want to set is the second
  3363. parameter of the `jump_n'; that is `b-2' as
  3364. an absolute address. `laststart' will be
  3365. the `set_number_at' we're about to insert;
  3366. `laststart+3' the number to set, the source
  3367. for the relative address. But we are
  3368. inserting into the middle of the pattern --
  3369. so everything is getting moved up by 5.
  3370. Conclusion: (b - 2) - (laststart + 3) + 5,
  3371. i.e., b - laststart.
  3372. We insert this at the beginning of the loop
  3373. so that if we fail during matching, we'll
  3374. reinitialize the bounds. */
  3375. PREFIX(insert_op2) (set_number_at, laststart,
  3376. b - laststart,
  3377. upper_bound - 1, b);
  3378. b += 1 + 2 * OFFSET_ADDRESS_SIZE;
  3379. }
  3380. }
  3381. pending_exact = 0;
  3382. break;
  3383. invalid_interval:
  3384. if (!(syntax & RE_INVALID_INTERVAL_ORD))
  3385. FREE_STACK_RETURN (p == pend ? REG_EBRACE : REG_BADBR);
  3386. unfetch_interval:
  3387. /* Match the characters as literals. */
  3388. p = beg_interval;
  3389. c = '{';
  3390. if (syntax & RE_NO_BK_BRACES)
  3391. goto normal_char;
  3392. else
  3393. goto normal_backslash;
  3394. }
  3395. #ifdef emacs
  3396. /* There is no way to specify the before_dot and after_dot
  3397. operators. rms says this is ok. --karl */
  3398. case '=':
  3399. BUF_PUSH (at_dot);
  3400. break;
  3401. case 's':
  3402. laststart = b;
  3403. PATFETCH (c);
  3404. BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  3405. break;
  3406. case 'S':
  3407. laststart = b;
  3408. PATFETCH (c);
  3409. BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  3410. break;
  3411. #endif /* emacs */
  3412. case 'w':
  3413. if (syntax & RE_NO_GNU_OPS)
  3414. goto normal_char;
  3415. laststart = b;
  3416. BUF_PUSH (wordchar);
  3417. break;
  3418. case 'W':
  3419. if (syntax & RE_NO_GNU_OPS)
  3420. goto normal_char;
  3421. laststart = b;
  3422. BUF_PUSH (notwordchar);
  3423. break;
  3424. case '<':
  3425. if (syntax & RE_NO_GNU_OPS)
  3426. goto normal_char;
  3427. BUF_PUSH (wordbeg);
  3428. break;
  3429. case '>':
  3430. if (syntax & RE_NO_GNU_OPS)
  3431. goto normal_char;
  3432. BUF_PUSH (wordend);
  3433. break;
  3434. case 'b':
  3435. if (syntax & RE_NO_GNU_OPS)
  3436. goto normal_char;
  3437. BUF_PUSH (wordbound);
  3438. break;
  3439. case 'B':
  3440. if (syntax & RE_NO_GNU_OPS)
  3441. goto normal_char;
  3442. BUF_PUSH (notwordbound);
  3443. break;
  3444. case '`':
  3445. if (syntax & RE_NO_GNU_OPS)
  3446. goto normal_char;
  3447. BUF_PUSH (begbuf);
  3448. break;
  3449. case '\'':
  3450. if (syntax & RE_NO_GNU_OPS)
  3451. goto normal_char;
  3452. BUF_PUSH (endbuf);
  3453. break;
  3454. case '1': case '2': case '3': case '4': case '5':
  3455. case '6': case '7': case '8': case '9':
  3456. if (syntax & RE_NO_BK_REFS)
  3457. goto normal_char;
  3458. c1 = c - '0';
  3459. if (c1 > regnum)
  3460. FREE_STACK_RETURN (REG_ESUBREG);
  3461. /* Can't back reference to a subexpression if inside of it. */
  3462. if (group_in_compile_stack (compile_stack, (regnum_t) c1))
  3463. goto normal_char;
  3464. laststart = b;
  3465. BUF_PUSH_2 (duplicate, c1);
  3466. break;
  3467. case '+':
  3468. case '?':
  3469. if (syntax & RE_BK_PLUS_QM)
  3470. goto handle_plus;
  3471. else
  3472. goto normal_backslash;
  3473. default:
  3474. normal_backslash:
  3475. /* You might think it would be useful for \ to mean
  3476. not to translate; but if we don't translate it
  3477. it will never match anything. */
  3478. c = TRANSLATE (c);
  3479. goto normal_char;
  3480. }
  3481. break;
  3482. default:
  3483. /* Expects the character in `c'. */
  3484. normal_char:
  3485. /* If no exactn currently being built. */
  3486. if (!pending_exact
  3487. #ifdef WCHAR
  3488. /* If last exactn handle binary(or character) and
  3489. new exactn handle character(or binary). */
  3490. || is_exactn_bin != is_binary[p - 1 - pattern]
  3491. #endif /* WCHAR */
  3492. /* If last exactn not at current position. */
  3493. || pending_exact + *pending_exact + 1 != b
  3494. /* We have only one byte following the exactn for the count. */
  3495. || *pending_exact == (1 << BYTEWIDTH) - 1
  3496. /* If followed by a repetition operator. */
  3497. || *p == '*' || *p == '^'
  3498. || ((syntax & RE_BK_PLUS_QM)
  3499. ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  3500. : (*p == '+' || *p == '?'))
  3501. || ((syntax & RE_INTERVALS)
  3502. && ((syntax & RE_NO_BK_BRACES)
  3503. ? *p == '{'
  3504. : (p[0] == '\\' && p[1] == '{'))))
  3505. {
  3506. /* Start building a new exactn. */
  3507. laststart = b;
  3508. #ifdef WCHAR
  3509. /* Is this exactn binary data or character? */
  3510. is_exactn_bin = is_binary[p - 1 - pattern];
  3511. if (is_exactn_bin)
  3512. BUF_PUSH_2 (exactn_bin, 0);
  3513. else
  3514. BUF_PUSH_2 (exactn, 0);
  3515. #else
  3516. BUF_PUSH_2 (exactn, 0);
  3517. #endif /* WCHAR */
  3518. pending_exact = b - 1;
  3519. }
  3520. BUF_PUSH (c);
  3521. (*pending_exact)++;
  3522. break;
  3523. } /* switch (c) */
  3524. } /* while p != pend */
  3525. /* Through the pattern now. */
  3526. if (fixup_alt_jump)
  3527. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  3528. if (!COMPILE_STACK_EMPTY)
  3529. FREE_STACK_RETURN (REG_EPAREN);
  3530. /* If we don't want backtracking, force success
  3531. the first time we reach the end of the compiled pattern. */
  3532. if (syntax & RE_NO_POSIX_BACKTRACKING)
  3533. BUF_PUSH (succeed);
  3534. #ifdef WCHAR
  3535. free (pattern);
  3536. free (mbs_offset);
  3537. free (is_binary);
  3538. #endif
  3539. free (compile_stack.stack);
  3540. /* We have succeeded; set the length of the buffer. */
  3541. #ifdef WCHAR
  3542. bufp->used = (uintptr_t) b - (uintptr_t) COMPILED_BUFFER_VAR;
  3543. #else
  3544. bufp->used = b - bufp->buffer;
  3545. #endif
  3546. #ifdef DEBUG
  3547. if (debug)
  3548. {
  3549. DEBUG_PRINT1 ("\nCompiled pattern: \n");
  3550. PREFIX(print_compiled_pattern) (bufp);
  3551. }
  3552. #endif /* DEBUG */
  3553. #ifndef MATCH_MAY_ALLOCATE
  3554. /* Initialize the failure stack to the largest possible stack. This
  3555. isn't necessary unless we're trying to avoid calling alloca in
  3556. the search and match routines. */
  3557. {
  3558. int num_regs = bufp->re_nsub + 1;
  3559. /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
  3560. is strictly greater than re_max_failures, the largest possible stack
  3561. is 2 * re_max_failures failure points. */
  3562. if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
  3563. {
  3564. fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
  3565. # ifdef emacs
  3566. if (! fail_stack.stack)
  3567. fail_stack.stack
  3568. = (PREFIX(fail_stack_elt_t) *) xmalloc (fail_stack.size
  3569. * sizeof (PREFIX(fail_stack_elt_t)));
  3570. else
  3571. fail_stack.stack
  3572. = (PREFIX(fail_stack_elt_t) *) xrealloc (fail_stack.stack,
  3573. (fail_stack.size
  3574. * sizeof (PREFIX(fail_stack_elt_t))));
  3575. # else /* not emacs */
  3576. if (! fail_stack.stack)
  3577. fail_stack.stack
  3578. = (PREFIX(fail_stack_elt_t) *) malloc (fail_stack.size
  3579. * sizeof (PREFIX(fail_stack_elt_t)));
  3580. else
  3581. fail_stack.stack
  3582. = (PREFIX(fail_stack_elt_t) *) realloc (fail_stack.stack,
  3583. (fail_stack.size
  3584. * sizeof (PREFIX(fail_stack_elt_t))));
  3585. # endif /* not emacs */
  3586. }
  3587. PREFIX(regex_grow_registers) (num_regs);
  3588. }
  3589. #endif /* not MATCH_MAY_ALLOCATE */
  3590. return REG_NOERROR;
  3591. } /* regex_compile */
  3592. /* Subroutines for `regex_compile'. */
  3593. /* Store OP at LOC followed by two-byte integer parameter ARG. */
  3594. /* ifdef WCHAR, integer parameter is 1 wchar_t. */
  3595. static void
  3596. PREFIX(store_op1) (re_opcode_t op, UCHAR_T *loc, int arg)
  3597. {
  3598. *loc = (UCHAR_T) op;
  3599. STORE_NUMBER (loc + 1, arg);
  3600. }
  3601. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
  3602. /* ifdef WCHAR, integer parameter is 1 wchar_t. */
  3603. static void
  3604. PREFIX(store_op2) (re_opcode_t op, UCHAR_T *loc, int arg1, int arg2)
  3605. {
  3606. *loc = (UCHAR_T) op;
  3607. STORE_NUMBER (loc + 1, arg1);
  3608. STORE_NUMBER (loc + 1 + OFFSET_ADDRESS_SIZE, arg2);
  3609. }
  3610. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  3611. for OP followed by two-byte integer parameter ARG. */
  3612. /* ifdef WCHAR, integer parameter is 1 wchar_t. */
  3613. static void
  3614. PREFIX(insert_op1) (re_opcode_t op, UCHAR_T *loc, int arg, UCHAR_T *end)
  3615. {
  3616. register UCHAR_T *pfrom = end;
  3617. register UCHAR_T *pto = end + 1 + OFFSET_ADDRESS_SIZE;
  3618. while (pfrom != loc)
  3619. *--pto = *--pfrom;
  3620. PREFIX(store_op1) (op, loc, arg);
  3621. }
  3622. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
  3623. /* ifdef WCHAR, integer parameter is 1 wchar_t. */
  3624. static void
  3625. PREFIX(insert_op2) (re_opcode_t op, UCHAR_T *loc, int arg1,
  3626. int arg2, UCHAR_T *end)
  3627. {
  3628. register UCHAR_T *pfrom = end;
  3629. register UCHAR_T *pto = end + 1 + 2 * OFFSET_ADDRESS_SIZE;
  3630. while (pfrom != loc)
  3631. *--pto = *--pfrom;
  3632. PREFIX(store_op2) (op, loc, arg1, arg2);
  3633. }
  3634. /* P points to just after a ^ in PATTERN. Return true if that ^ comes
  3635. after an alternative or a begin-subexpression. We assume there is at
  3636. least one character before the ^. */
  3637. static boolean
  3638. PREFIX(at_begline_loc_p) (const CHAR_T *pattern, const CHAR_T *p,
  3639. reg_syntax_t syntax)
  3640. {
  3641. const CHAR_T *prev = p - 2;
  3642. boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  3643. return
  3644. /* After a subexpression? */
  3645. (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  3646. /* After an alternative? */
  3647. || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  3648. }
  3649. /* The dual of at_begline_loc_p. This one is for $. We assume there is
  3650. at least one character after the $, i.e., `P < PEND'. */
  3651. static boolean
  3652. PREFIX(at_endline_loc_p) (const CHAR_T *p, const CHAR_T *pend,
  3653. reg_syntax_t syntax)
  3654. {
  3655. const CHAR_T *next = p;
  3656. boolean next_backslash = *next == '\\';
  3657. const CHAR_T *next_next = p + 1 < pend ? p + 1 : 0;
  3658. return
  3659. /* Before a subexpression? */
  3660. (syntax & RE_NO_BK_PARENS ? *next == ')'
  3661. : next_backslash && next_next && *next_next == ')')
  3662. /* Before an alternative? */
  3663. || (syntax & RE_NO_BK_VBAR ? *next == '|'
  3664. : next_backslash && next_next && *next_next == '|');
  3665. }
  3666. #else /* not INSIDE_RECURSION */
  3667. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
  3668. false if it's not. */
  3669. static boolean
  3670. group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum)
  3671. {
  3672. int this_element;
  3673. for (this_element = compile_stack.avail - 1;
  3674. this_element >= 0;
  3675. this_element--)
  3676. if (compile_stack.stack[this_element].regnum == regnum)
  3677. return true;
  3678. return false;
  3679. }
  3680. #endif /* not INSIDE_RECURSION */
  3681. #ifdef INSIDE_RECURSION
  3682. #ifdef WCHAR
  3683. /* This insert space, which size is "num", into the pattern at "loc".
  3684. "end" must point the end of the allocated buffer. */
  3685. static void
  3686. insert_space (int num, CHAR_T *loc, CHAR_T *end)
  3687. {
  3688. register CHAR_T *pto = end;
  3689. register CHAR_T *pfrom = end - num;
  3690. while (pfrom >= loc)
  3691. *pto-- = *pfrom--;
  3692. }
  3693. #endif /* WCHAR */
  3694. #ifdef WCHAR
  3695. static reg_errcode_t
  3696. wcs_compile_range (CHAR_T range_start_char, const CHAR_T **p_ptr,
  3697. const CHAR_T *pend, RE_TRANSLATE_TYPE translate,
  3698. reg_syntax_t syntax, CHAR_T *b, CHAR_T *char_set)
  3699. {
  3700. const CHAR_T *p = *p_ptr;
  3701. CHAR_T range_start, range_end;
  3702. reg_errcode_t ret;
  3703. # ifdef _LIBC
  3704. uint32_t nrules;
  3705. uint32_t start_val, end_val;
  3706. # endif
  3707. if (p == pend)
  3708. return REG_ERANGE;
  3709. # ifdef _LIBC
  3710. nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  3711. if (nrules != 0)
  3712. {
  3713. const char *collseq = (const char *) _NL_CURRENT(LC_COLLATE,
  3714. _NL_COLLATE_COLLSEQWC);
  3715. const unsigned char *extra = (const unsigned char *)
  3716. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
  3717. if (range_start_char < -1)
  3718. {
  3719. /* range_start is a collating symbol. */
  3720. int32_t *wextra;
  3721. /* Retreive the index and get collation sequence value. */
  3722. wextra = (int32_t*)(extra + char_set[-range_start_char]);
  3723. start_val = wextra[1 + *wextra];
  3724. }
  3725. else
  3726. start_val = collseq_table_lookup(collseq, TRANSLATE(range_start_char));
  3727. end_val = collseq_table_lookup (collseq, TRANSLATE (p[0]));
  3728. /* Report an error if the range is empty and the syntax prohibits
  3729. this. */
  3730. ret = ((syntax & RE_NO_EMPTY_RANGES)
  3731. && (start_val > end_val))? REG_ERANGE : REG_NOERROR;
  3732. /* Insert space to the end of the char_ranges. */
  3733. insert_space(2, b - char_set[5] - 2, b - 1);
  3734. *(b - char_set[5] - 2) = (wchar_t)start_val;
  3735. *(b - char_set[5] - 1) = (wchar_t)end_val;
  3736. char_set[4]++; /* ranges_index */
  3737. }
  3738. else
  3739. # endif
  3740. {
  3741. range_start = (range_start_char >= 0)? TRANSLATE (range_start_char):
  3742. range_start_char;
  3743. range_end = TRANSLATE (p[0]);
  3744. /* Report an error if the range is empty and the syntax prohibits
  3745. this. */
  3746. ret = ((syntax & RE_NO_EMPTY_RANGES)
  3747. && (range_start > range_end))? REG_ERANGE : REG_NOERROR;
  3748. /* Insert space to the end of the char_ranges. */
  3749. insert_space(2, b - char_set[5] - 2, b - 1);
  3750. *(b - char_set[5] - 2) = range_start;
  3751. *(b - char_set[5] - 1) = range_end;
  3752. char_set[4]++; /* ranges_index */
  3753. }
  3754. /* Have to increment the pointer into the pattern string, so the
  3755. caller isn't still at the ending character. */
  3756. (*p_ptr)++;
  3757. return ret;
  3758. }
  3759. #else /* BYTE */
  3760. /* Read the ending character of a range (in a bracket expression) from the
  3761. uncompiled pattern *P_PTR (which ends at PEND). We assume the
  3762. starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
  3763. Then we set the translation of all bits between the starting and
  3764. ending characters (inclusive) in the compiled pattern B.
  3765. Return an error code.
  3766. We use these short variable names so we can use the same macros as
  3767. `regex_compile' itself. */
  3768. static reg_errcode_t
  3769. byte_compile_range (unsigned int range_start_char, const char **p_ptr,
  3770. const char *pend, RE_TRANSLATE_TYPE translate,
  3771. reg_syntax_t syntax, unsigned char *b)
  3772. {
  3773. unsigned this_char;
  3774. const char *p = *p_ptr;
  3775. reg_errcode_t ret;
  3776. # if _LIBC
  3777. const unsigned char *collseq;
  3778. unsigned int start_colseq;
  3779. unsigned int end_colseq;
  3780. # else
  3781. unsigned end_char;
  3782. # endif
  3783. if (p == pend)
  3784. return REG_ERANGE;
  3785. /* Have to increment the pointer into the pattern string, so the
  3786. caller isn't still at the ending character. */
  3787. (*p_ptr)++;
  3788. /* Report an error if the range is empty and the syntax prohibits this. */
  3789. ret = syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  3790. # if _LIBC
  3791. collseq = (const unsigned char *) _NL_CURRENT (LC_COLLATE,
  3792. _NL_COLLATE_COLLSEQMB);
  3793. start_colseq = collseq[(unsigned char) TRANSLATE (range_start_char)];
  3794. end_colseq = collseq[(unsigned char) TRANSLATE (p[0])];
  3795. for (this_char = 0; this_char <= (unsigned char) -1; ++this_char)
  3796. {
  3797. unsigned int this_colseq = collseq[(unsigned char) TRANSLATE (this_char)];
  3798. if (start_colseq <= this_colseq && this_colseq <= end_colseq)
  3799. {
  3800. SET_LIST_BIT (TRANSLATE (this_char));
  3801. ret = REG_NOERROR;
  3802. }
  3803. }
  3804. # else
  3805. /* Here we see why `this_char' has to be larger than an `unsigned
  3806. char' -- we would otherwise go into an infinite loop, since all
  3807. characters <= 0xff. */
  3808. range_start_char = TRANSLATE (range_start_char);
  3809. /* TRANSLATE(p[0]) is casted to char (not unsigned char) in TRANSLATE,
  3810. and some compilers cast it to int implicitly, so following for_loop
  3811. may fall to (almost) infinite loop.
  3812. e.g. If translate[p[0]] = 0xff, end_char may equals to 0xffffffff.
  3813. To avoid this, we cast p[0] to unsigned int and truncate it. */
  3814. end_char = ((unsigned)TRANSLATE(p[0]) & ((1 << BYTEWIDTH) - 1));
  3815. for (this_char = range_start_char; this_char <= end_char; ++this_char)
  3816. {
  3817. SET_LIST_BIT (TRANSLATE (this_char));
  3818. ret = REG_NOERROR;
  3819. }
  3820. # endif
  3821. return ret;
  3822. }
  3823. #endif /* WCHAR */
  3824. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  3825. BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
  3826. characters can start a string that matches the pattern. This fastmap
  3827. is used by re_search to skip quickly over impossible starting points.
  3828. The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  3829. area as BUFP->fastmap.
  3830. We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  3831. the pattern buffer.
  3832. Returns 0 if we succeed, -2 if an internal error. */
  3833. #ifdef WCHAR
  3834. /* local function for re_compile_fastmap.
  3835. truncate wchar_t character to char. */
  3836. static unsigned char truncate_wchar (CHAR_T c);
  3837. static unsigned char
  3838. truncate_wchar (CHAR_T c)
  3839. {
  3840. unsigned char buf[MB_CUR_MAX];
  3841. mbstate_t state;
  3842. int retval;
  3843. memset (&state, '\0', sizeof (state));
  3844. # ifdef _LIBC
  3845. retval = __wcrtomb (buf, c, &state);
  3846. # else
  3847. retval = wcrtomb (buf, c, &state);
  3848. # endif
  3849. return retval > 0 ? buf[0] : (unsigned char) c;
  3850. }
  3851. #endif /* WCHAR */
  3852. static int
  3853. PREFIX(re_compile_fastmap) (struct re_pattern_buffer *bufp)
  3854. {
  3855. int j, k;
  3856. #ifdef MATCH_MAY_ALLOCATE
  3857. PREFIX(fail_stack_type) fail_stack;
  3858. #endif
  3859. #ifndef REGEX_MALLOC
  3860. char *destination;
  3861. #endif
  3862. register char *fastmap = bufp->fastmap;
  3863. #ifdef WCHAR
  3864. /* We need to cast pattern to (wchar_t*), because we casted this compiled
  3865. pattern to (char*) in regex_compile. */
  3866. UCHAR_T *pattern = (UCHAR_T*)bufp->buffer;
  3867. register UCHAR_T *pend = (UCHAR_T*) (bufp->buffer + bufp->used);
  3868. #else /* BYTE */
  3869. UCHAR_T *pattern = bufp->buffer;
  3870. register UCHAR_T *pend = pattern + bufp->used;
  3871. #endif /* WCHAR */
  3872. UCHAR_T *p = pattern;
  3873. #ifdef REL_ALLOC
  3874. /* This holds the pointer to the failure stack, when
  3875. it is allocated relocatably. */
  3876. fail_stack_elt_t *failure_stack_ptr;
  3877. #endif
  3878. /* Assume that each path through the pattern can be null until
  3879. proven otherwise. We set this false at the bottom of switch
  3880. statement, to which we get only if a particular path doesn't
  3881. match the empty string. */
  3882. boolean path_can_be_null = true;
  3883. /* We aren't doing a `succeed_n' to begin with. */
  3884. boolean succeed_n_p = false;
  3885. assert (fastmap != NULL && p != NULL);
  3886. INIT_FAIL_STACK ();
  3887. bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
  3888. bufp->fastmap_accurate = 1; /* It will be when we're done. */
  3889. bufp->can_be_null = 0;
  3890. while (1)
  3891. {
  3892. if (p == pend || *p == (UCHAR_T) succeed)
  3893. {
  3894. /* We have reached the (effective) end of pattern. */
  3895. if (!FAIL_STACK_EMPTY ())
  3896. {
  3897. bufp->can_be_null |= path_can_be_null;
  3898. /* Reset for next path. */
  3899. path_can_be_null = true;
  3900. p = fail_stack.stack[--fail_stack.avail].pointer;
  3901. continue;
  3902. }
  3903. else
  3904. break;
  3905. }
  3906. /* We should never be about to go beyond the end of the pattern. */
  3907. assert (p < pend);
  3908. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  3909. {
  3910. /* I guess the idea here is to simply not bother with a fastmap
  3911. if a backreference is used, since it's too hard to figure out
  3912. the fastmap for the corresponding group. Setting
  3913. `can_be_null' stops `re_search_2' from using the fastmap, so
  3914. that is all we do. */
  3915. case duplicate:
  3916. bufp->can_be_null = 1;
  3917. goto done;
  3918. /* Following are the cases which match a character. These end
  3919. with `break'. */
  3920. #ifdef WCHAR
  3921. case exactn:
  3922. fastmap[truncate_wchar(p[1])] = 1;
  3923. break;
  3924. #else /* BYTE */
  3925. case exactn:
  3926. fastmap[p[1]] = 1;
  3927. break;
  3928. #endif /* WCHAR */
  3929. #ifdef MBS_SUPPORT
  3930. case exactn_bin:
  3931. fastmap[p[1]] = 1;
  3932. break;
  3933. #endif
  3934. #ifdef WCHAR
  3935. /* It is hard to distinguish fastmap from (multi byte) characters
  3936. which depends on current locale. */
  3937. case charset:
  3938. case charset_not:
  3939. case wordchar:
  3940. case notwordchar:
  3941. bufp->can_be_null = 1;
  3942. goto done;
  3943. #else /* BYTE */
  3944. case charset:
  3945. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  3946. if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  3947. fastmap[j] = 1;
  3948. break;
  3949. case charset_not:
  3950. /* Chars beyond end of map must be allowed. */
  3951. for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  3952. fastmap[j] = 1;
  3953. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  3954. if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  3955. fastmap[j] = 1;
  3956. break;
  3957. case wordchar:
  3958. for (j = 0; j < (1 << BYTEWIDTH); j++)
  3959. if (SYNTAX (j) == Sword)
  3960. fastmap[j] = 1;
  3961. break;
  3962. case notwordchar:
  3963. for (j = 0; j < (1 << BYTEWIDTH); j++)
  3964. if (SYNTAX (j) != Sword)
  3965. fastmap[j] = 1;
  3966. break;
  3967. #endif /* WCHAR */
  3968. case anychar:
  3969. {
  3970. int fastmap_newline = fastmap['\n'];
  3971. /* `.' matches anything ... */
  3972. for (j = 0; j < (1 << BYTEWIDTH); j++)
  3973. fastmap[j] = 1;
  3974. /* ... except perhaps newline. */
  3975. if (!(bufp->syntax & RE_DOT_NEWLINE))
  3976. fastmap['\n'] = fastmap_newline;
  3977. /* Return if we have already set `can_be_null'; if we have,
  3978. then the fastmap is irrelevant. Something's wrong here. */
  3979. else if (bufp->can_be_null)
  3980. goto done;
  3981. /* Otherwise, have to check alternative paths. */
  3982. break;
  3983. }
  3984. #ifdef emacs
  3985. case syntaxspec:
  3986. k = *p++;
  3987. for (j = 0; j < (1 << BYTEWIDTH); j++)
  3988. if (SYNTAX (j) == (enum syntaxcode) k)
  3989. fastmap[j] = 1;
  3990. break;
  3991. case notsyntaxspec:
  3992. k = *p++;
  3993. for (j = 0; j < (1 << BYTEWIDTH); j++)
  3994. if (SYNTAX (j) != (enum syntaxcode) k)
  3995. fastmap[j] = 1;
  3996. break;
  3997. /* All cases after this match the empty string. These end with
  3998. `continue'. */
  3999. case before_dot:
  4000. case at_dot:
  4001. case after_dot:
  4002. continue;
  4003. #endif /* emacs */
  4004. case no_op:
  4005. case begline:
  4006. case endline:
  4007. case begbuf:
  4008. case endbuf:
  4009. case wordbound:
  4010. case notwordbound:
  4011. case wordbeg:
  4012. case wordend:
  4013. case push_dummy_failure:
  4014. continue;
  4015. case jump_n:
  4016. case pop_failure_jump:
  4017. case maybe_pop_jump:
  4018. case jump:
  4019. case jump_past_alt:
  4020. case dummy_failure_jump:
  4021. EXTRACT_NUMBER_AND_INCR (j, p);
  4022. p += j;
  4023. if (j > 0)
  4024. continue;
  4025. /* Jump backward implies we just went through the body of a
  4026. loop and matched nothing. Opcode jumped to should be
  4027. `on_failure_jump' or `succeed_n'. Just treat it like an
  4028. ordinary jump. For a * loop, it has pushed its failure
  4029. point already; if so, discard that as redundant. */
  4030. if ((re_opcode_t) *p != on_failure_jump
  4031. && (re_opcode_t) *p != succeed_n)
  4032. continue;
  4033. p++;
  4034. EXTRACT_NUMBER_AND_INCR (j, p);
  4035. p += j;
  4036. /* If what's on the stack is where we are now, pop it. */
  4037. if (!FAIL_STACK_EMPTY ()
  4038. && fail_stack.stack[fail_stack.avail - 1].pointer == p)
  4039. fail_stack.avail--;
  4040. continue;
  4041. case on_failure_jump:
  4042. case on_failure_keep_string_jump:
  4043. handle_on_failure_jump:
  4044. EXTRACT_NUMBER_AND_INCR (j, p);
  4045. /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  4046. end of the pattern. We don't want to push such a point,
  4047. since when we restore it above, entering the switch will
  4048. increment `p' past the end of the pattern. We don't need
  4049. to push such a point since we obviously won't find any more
  4050. fastmap entries beyond `pend'. Such a pattern can match
  4051. the null string, though. */
  4052. if (p + j < pend)
  4053. {
  4054. if (!PUSH_PATTERN_OP (p + j, fail_stack))
  4055. {
  4056. RESET_FAIL_STACK ();
  4057. return -2;
  4058. }
  4059. }
  4060. else
  4061. bufp->can_be_null = 1;
  4062. if (succeed_n_p)
  4063. {
  4064. EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
  4065. succeed_n_p = false;
  4066. }
  4067. continue;
  4068. case succeed_n:
  4069. /* Get to the number of times to succeed. */
  4070. p += OFFSET_ADDRESS_SIZE;
  4071. /* Increment p past the n for when k != 0. */
  4072. EXTRACT_NUMBER_AND_INCR (k, p);
  4073. if (k == 0)
  4074. {
  4075. p -= 2 * OFFSET_ADDRESS_SIZE;
  4076. succeed_n_p = true; /* Spaghetti code alert. */
  4077. goto handle_on_failure_jump;
  4078. }
  4079. continue;
  4080. case set_number_at:
  4081. p += 2 * OFFSET_ADDRESS_SIZE;
  4082. continue;
  4083. case start_memory:
  4084. case stop_memory:
  4085. p += 2;
  4086. continue;
  4087. default:
  4088. abort (); /* We have listed all the cases. */
  4089. } /* switch *p++ */
  4090. /* Getting here means we have found the possible starting
  4091. characters for one path of the pattern -- and that the empty
  4092. string does not match. We need not follow this path further.
  4093. Instead, look at the next alternative (remembered on the
  4094. stack), or quit if no more. The test at the top of the loop
  4095. does these things. */
  4096. path_can_be_null = false;
  4097. p = pend;
  4098. } /* while p */
  4099. /* Set `can_be_null' for the last path (also the first path, if the
  4100. pattern is empty). */
  4101. bufp->can_be_null |= path_can_be_null;
  4102. done:
  4103. RESET_FAIL_STACK ();
  4104. return 0;
  4105. }
  4106. #else /* not INSIDE_RECURSION */
  4107. int
  4108. re_compile_fastmap (struct re_pattern_buffer *bufp)
  4109. {
  4110. # ifdef MBS_SUPPORT
  4111. if (MB_CUR_MAX != 1)
  4112. return wcs_re_compile_fastmap(bufp);
  4113. else
  4114. # endif
  4115. return byte_re_compile_fastmap(bufp);
  4116. } /* re_compile_fastmap */
  4117. #ifdef _LIBC
  4118. weak_alias (__re_compile_fastmap, re_compile_fastmap)
  4119. #endif
  4120. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  4121. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  4122. this memory for recording register information. STARTS and ENDS
  4123. must be allocated using the malloc library routine, and must each
  4124. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  4125. If NUM_REGS == 0, then subsequent matches should allocate their own
  4126. register data.
  4127. Unless this function is called, the first search or match using
  4128. PATTERN_BUFFER will allocate its own register data, without
  4129. freeing the old data. */
  4130. void
  4131. re_set_registers (struct re_pattern_buffer *bufp,
  4132. struct re_registers *regs, unsigned num_regs,
  4133. regoff_t *starts, regoff_t *ends)
  4134. {
  4135. if (num_regs)
  4136. {
  4137. bufp->regs_allocated = REGS_REALLOCATE;
  4138. regs->num_regs = num_regs;
  4139. regs->start = starts;
  4140. regs->end = ends;
  4141. }
  4142. else
  4143. {
  4144. bufp->regs_allocated = REGS_UNALLOCATED;
  4145. regs->num_regs = 0;
  4146. regs->start = regs->end = (regoff_t *) 0;
  4147. }
  4148. }
  4149. #ifdef _LIBC
  4150. weak_alias (__re_set_registers, re_set_registers)
  4151. #endif
  4152. /* Searching routines. */
  4153. /* Like re_search_2, below, but only one string is specified, and
  4154. doesn't let you say where to stop matching. */
  4155. int
  4156. re_search (struct re_pattern_buffer *bufp, const char *string, int size,
  4157. int startpos, int range, struct re_registers *regs)
  4158. {
  4159. return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
  4160. regs, size);
  4161. }
  4162. #ifdef _LIBC
  4163. weak_alias (__re_search, re_search)
  4164. #endif
  4165. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  4166. virtual concatenation of STRING1 and STRING2, starting first at index
  4167. STARTPOS, then at STARTPOS + 1, and so on.
  4168. STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  4169. RANGE is how far to scan while trying to match. RANGE = 0 means try
  4170. only at STARTPOS; in general, the last start tried is STARTPOS +
  4171. RANGE.
  4172. In REGS, return the indices of the virtual concatenation of STRING1
  4173. and STRING2 that matched the entire BUFP->buffer and its contained
  4174. subexpressions.
  4175. Do not consider matching one past the index STOP in the virtual
  4176. concatenation of STRING1 and STRING2.
  4177. We return either the position in the strings at which the match was
  4178. found, -1 if no match, or -2 if error (such as failure
  4179. stack overflow). */
  4180. int
  4181. re_search_2 (struct re_pattern_buffer *bufp, const char *string1, int size1,
  4182. const char *string2, int size2, int startpos, int range,
  4183. struct re_registers *regs, int stop)
  4184. {
  4185. # ifdef MBS_SUPPORT
  4186. if (MB_CUR_MAX != 1)
  4187. return wcs_re_search_2 (bufp, string1, size1, string2, size2, startpos,
  4188. range, regs, stop);
  4189. else
  4190. # endif
  4191. return byte_re_search_2 (bufp, string1, size1, string2, size2, startpos,
  4192. range, regs, stop);
  4193. } /* re_search_2 */
  4194. #ifdef _LIBC
  4195. weak_alias (__re_search_2, re_search_2)
  4196. #endif
  4197. #endif /* not INSIDE_RECURSION */
  4198. #ifdef INSIDE_RECURSION
  4199. #ifdef MATCH_MAY_ALLOCATE
  4200. # define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
  4201. #else
  4202. # define FREE_VAR(var) free (var); var = NULL
  4203. #endif
  4204. #ifdef WCHAR
  4205. # define MAX_ALLOCA_SIZE 2000
  4206. # define FREE_WCS_BUFFERS() \
  4207. do { \
  4208. if (size1 > MAX_ALLOCA_SIZE) \
  4209. { \
  4210. free (wcs_string1); \
  4211. free (mbs_offset1); \
  4212. } \
  4213. else \
  4214. { \
  4215. FREE_VAR (wcs_string1); \
  4216. FREE_VAR (mbs_offset1); \
  4217. } \
  4218. if (size2 > MAX_ALLOCA_SIZE) \
  4219. { \
  4220. free (wcs_string2); \
  4221. free (mbs_offset2); \
  4222. } \
  4223. else \
  4224. { \
  4225. FREE_VAR (wcs_string2); \
  4226. FREE_VAR (mbs_offset2); \
  4227. } \
  4228. } while (0)
  4229. #endif
  4230. static int
  4231. PREFIX(re_search_2) (struct re_pattern_buffer *bufp, const char *string1,
  4232. int size1, const char *string2, int size2,
  4233. int startpos, int range,
  4234. struct re_registers *regs, int stop)
  4235. {
  4236. int val;
  4237. register char *fastmap = bufp->fastmap;
  4238. register RE_TRANSLATE_TYPE translate = bufp->translate;
  4239. int total_size = size1 + size2;
  4240. int endpos = startpos + range;
  4241. #ifdef WCHAR
  4242. /* We need wchar_t* buffers correspond to cstring1, cstring2. */
  4243. wchar_t *wcs_string1 = NULL, *wcs_string2 = NULL;
  4244. /* We need the size of wchar_t buffers correspond to csize1, csize2. */
  4245. int wcs_size1 = 0, wcs_size2 = 0;
  4246. /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
  4247. int *mbs_offset1 = NULL, *mbs_offset2 = NULL;
  4248. /* They hold whether each wchar_t is binary data or not. */
  4249. char *is_binary = NULL;
  4250. #endif /* WCHAR */
  4251. /* Check for out-of-range STARTPOS. */
  4252. if (startpos < 0 || startpos > total_size)
  4253. return -1;
  4254. /* Fix up RANGE if it might eventually take us outside
  4255. the virtual concatenation of STRING1 and STRING2.
  4256. Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
  4257. if (endpos < 0)
  4258. range = 0 - startpos;
  4259. else if (endpos > total_size)
  4260. range = total_size - startpos;
  4261. /* If the search isn't to be a backwards one, don't waste time in a
  4262. search for a pattern that must be anchored. */
  4263. if (bufp->used > 0 && range > 0
  4264. && ((re_opcode_t) bufp->buffer[0] == begbuf
  4265. /* `begline' is like `begbuf' if it cannot match at newlines. */
  4266. || ((re_opcode_t) bufp->buffer[0] == begline
  4267. && !bufp->newline_anchor)))
  4268. {
  4269. if (startpos > 0)
  4270. return -1;
  4271. else
  4272. range = 1;
  4273. }
  4274. #ifdef emacs
  4275. /* In a forward search for something that starts with \=.
  4276. don't keep searching past point. */
  4277. if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
  4278. {
  4279. range = PT - startpos;
  4280. if (range <= 0)
  4281. return -1;
  4282. }
  4283. #endif /* emacs */
  4284. /* Update the fastmap now if not correct already. */
  4285. if (fastmap && !bufp->fastmap_accurate)
  4286. if (re_compile_fastmap (bufp) == -2)
  4287. return -2;
  4288. #ifdef WCHAR
  4289. /* Allocate wchar_t array for wcs_string1 and wcs_string2 and
  4290. fill them with converted string. */
  4291. if (size1 != 0)
  4292. {
  4293. if (size1 > MAX_ALLOCA_SIZE)
  4294. {
  4295. wcs_string1 = TALLOC (size1 + 1, CHAR_T);
  4296. mbs_offset1 = TALLOC (size1 + 1, int);
  4297. is_binary = TALLOC (size1 + 1, char);
  4298. }
  4299. else
  4300. {
  4301. wcs_string1 = REGEX_TALLOC (size1 + 1, CHAR_T);
  4302. mbs_offset1 = REGEX_TALLOC (size1 + 1, int);
  4303. is_binary = REGEX_TALLOC (size1 + 1, char);
  4304. }
  4305. if (!wcs_string1 || !mbs_offset1 || !is_binary)
  4306. {
  4307. if (size1 > MAX_ALLOCA_SIZE)
  4308. {
  4309. free (wcs_string1);
  4310. free (mbs_offset1);
  4311. free (is_binary);
  4312. }
  4313. else
  4314. {
  4315. FREE_VAR (wcs_string1);
  4316. FREE_VAR (mbs_offset1);
  4317. FREE_VAR (is_binary);
  4318. }
  4319. return -2;
  4320. }
  4321. wcs_size1 = convert_mbs_to_wcs(wcs_string1, string1, size1,
  4322. mbs_offset1, is_binary);
  4323. wcs_string1[wcs_size1] = L'\0'; /* for a sentinel */
  4324. if (size1 > MAX_ALLOCA_SIZE)
  4325. free (is_binary);
  4326. else
  4327. FREE_VAR (is_binary);
  4328. }
  4329. if (size2 != 0)
  4330. {
  4331. if (size2 > MAX_ALLOCA_SIZE)
  4332. {
  4333. wcs_string2 = TALLOC (size2 + 1, CHAR_T);
  4334. mbs_offset2 = TALLOC (size2 + 1, int);
  4335. is_binary = TALLOC (size2 + 1, char);
  4336. }
  4337. else
  4338. {
  4339. wcs_string2 = REGEX_TALLOC (size2 + 1, CHAR_T);
  4340. mbs_offset2 = REGEX_TALLOC (size2 + 1, int);
  4341. is_binary = REGEX_TALLOC (size2 + 1, char);
  4342. }
  4343. if (!wcs_string2 || !mbs_offset2 || !is_binary)
  4344. {
  4345. FREE_WCS_BUFFERS ();
  4346. if (size2 > MAX_ALLOCA_SIZE)
  4347. free (is_binary);
  4348. else
  4349. FREE_VAR (is_binary);
  4350. return -2;
  4351. }
  4352. wcs_size2 = convert_mbs_to_wcs(wcs_string2, string2, size2,
  4353. mbs_offset2, is_binary);
  4354. wcs_string2[wcs_size2] = L'\0'; /* for a sentinel */
  4355. if (size2 > MAX_ALLOCA_SIZE)
  4356. free (is_binary);
  4357. else
  4358. FREE_VAR (is_binary);
  4359. }
  4360. #endif /* WCHAR */
  4361. /* Loop through the string, looking for a place to start matching. */
  4362. for (;;)
  4363. {
  4364. /* If a fastmap is supplied, skip quickly over characters that
  4365. cannot be the start of a match. If the pattern can match the
  4366. null string, however, we don't need to skip characters; we want
  4367. the first null string. */
  4368. if (fastmap && startpos < total_size && !bufp->can_be_null)
  4369. {
  4370. if (range > 0) /* Searching forwards. */
  4371. {
  4372. register const char *d;
  4373. register int lim = 0;
  4374. int irange = range;
  4375. if (startpos < size1 && startpos + range >= size1)
  4376. lim = range - (size1 - startpos);
  4377. d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
  4378. /* Written out as an if-else to avoid testing `translate'
  4379. inside the loop. */
  4380. if (translate)
  4381. while (range > lim
  4382. && !fastmap[(unsigned char)
  4383. translate[(unsigned char) *d++]])
  4384. range--;
  4385. else
  4386. while (range > lim && !fastmap[(unsigned char) *d++])
  4387. range--;
  4388. startpos += irange - range;
  4389. }
  4390. else /* Searching backwards. */
  4391. {
  4392. register CHAR_T c = (size1 == 0 || startpos >= size1
  4393. ? string2[startpos - size1]
  4394. : string1[startpos]);
  4395. if (!fastmap[(unsigned char) TRANSLATE (c)])
  4396. goto advance;
  4397. }
  4398. }
  4399. /* If can't match the null string, and that's all we have left, fail. */
  4400. if (range >= 0 && startpos == total_size && fastmap
  4401. && !bufp->can_be_null)
  4402. {
  4403. #ifdef WCHAR
  4404. FREE_WCS_BUFFERS ();
  4405. #endif
  4406. return -1;
  4407. }
  4408. #ifdef WCHAR
  4409. val = wcs_re_match_2_internal (bufp, string1, size1, string2,
  4410. size2, startpos, regs, stop,
  4411. wcs_string1, wcs_size1,
  4412. wcs_string2, wcs_size2,
  4413. mbs_offset1, mbs_offset2);
  4414. #else /* BYTE */
  4415. val = byte_re_match_2_internal (bufp, string1, size1, string2,
  4416. size2, startpos, regs, stop);
  4417. #endif /* BYTE */
  4418. #ifndef REGEX_MALLOC
  4419. # ifdef C_ALLOCA
  4420. alloca (0);
  4421. # endif
  4422. #endif
  4423. if (val >= 0)
  4424. {
  4425. #ifdef WCHAR
  4426. FREE_WCS_BUFFERS ();
  4427. #endif
  4428. return startpos;
  4429. }
  4430. if (val == -2)
  4431. {
  4432. #ifdef WCHAR
  4433. FREE_WCS_BUFFERS ();
  4434. #endif
  4435. return -2;
  4436. }
  4437. advance:
  4438. if (!range)
  4439. break;
  4440. else if (range > 0)
  4441. {
  4442. range--;
  4443. startpos++;
  4444. }
  4445. else
  4446. {
  4447. range++;
  4448. startpos--;
  4449. }
  4450. }
  4451. #ifdef WCHAR
  4452. FREE_WCS_BUFFERS ();
  4453. #endif
  4454. return -1;
  4455. }
  4456. #ifdef WCHAR
  4457. /* This converts PTR, a pointer into one of the search wchar_t strings
  4458. `string1' and `string2' into an multibyte string offset from the
  4459. beginning of that string. We use mbs_offset to optimize.
  4460. See convert_mbs_to_wcs. */
  4461. # define POINTER_TO_OFFSET(ptr) \
  4462. (FIRST_STRING_P (ptr) \
  4463. ? ((regoff_t)(mbs_offset1 != NULL? mbs_offset1[(ptr)-string1] : 0)) \
  4464. : ((regoff_t)((mbs_offset2 != NULL? mbs_offset2[(ptr)-string2] : 0) \
  4465. + csize1)))
  4466. #else /* BYTE */
  4467. /* This converts PTR, a pointer into one of the search strings `string1'
  4468. and `string2' into an offset from the beginning of that string. */
  4469. # define POINTER_TO_OFFSET(ptr) \
  4470. (FIRST_STRING_P (ptr) \
  4471. ? ((regoff_t) ((ptr) - string1)) \
  4472. : ((regoff_t) ((ptr) - string2 + size1)))
  4473. #endif /* WCHAR */
  4474. /* Macros for dealing with the split strings in re_match_2. */
  4475. #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
  4476. /* Call before fetching a character with *d. This switches over to
  4477. string2 if necessary. */
  4478. #define PREFETCH() \
  4479. while (d == dend) \
  4480. { \
  4481. /* End of string2 => fail. */ \
  4482. if (dend == end_match_2) \
  4483. goto fail; \
  4484. /* End of string1 => advance to string2. */ \
  4485. d = string2; \
  4486. dend = end_match_2; \
  4487. }
  4488. /* Test if at very beginning or at very end of the virtual concatenation
  4489. of `string1' and `string2'. If only one string, it's `string2'. */
  4490. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  4491. #define AT_STRINGS_END(d) ((d) == end2)
  4492. /* Test if D points to a character which is word-constituent. We have
  4493. two special cases to check for: if past the end of string1, look at
  4494. the first character in string2; and if before the beginning of
  4495. string2, look at the last character in string1. */
  4496. #ifdef WCHAR
  4497. /* Use internationalized API instead of SYNTAX. */
  4498. # define WORDCHAR_P(d) \
  4499. (iswalnum ((wint_t)((d) == end1 ? *string2 \
  4500. : (d) == string2 - 1 ? *(end1 - 1) : *(d))) != 0 \
  4501. || ((d) == end1 ? *string2 \
  4502. : (d) == string2 - 1 ? *(end1 - 1) : *(d)) == L'_')
  4503. #else /* BYTE */
  4504. # define WORDCHAR_P(d) \
  4505. (SYNTAX ((d) == end1 ? *string2 \
  4506. : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
  4507. == Sword)
  4508. #endif /* WCHAR */
  4509. /* Disabled due to a compiler bug -- see comment at case wordbound */
  4510. #if 0
  4511. /* Test if the character before D and the one at D differ with respect
  4512. to being word-constituent. */
  4513. #define AT_WORD_BOUNDARY(d) \
  4514. (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
  4515. || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  4516. #endif
  4517. /* Free everything we malloc. */
  4518. #ifdef MATCH_MAY_ALLOCATE
  4519. # ifdef WCHAR
  4520. # define FREE_VARIABLES() \
  4521. do { \
  4522. REGEX_FREE_STACK (fail_stack.stack); \
  4523. FREE_VAR (regstart); \
  4524. FREE_VAR (regend); \
  4525. FREE_VAR (old_regstart); \
  4526. FREE_VAR (old_regend); \
  4527. FREE_VAR (best_regstart); \
  4528. FREE_VAR (best_regend); \
  4529. FREE_VAR (reg_info); \
  4530. FREE_VAR (reg_dummy); \
  4531. FREE_VAR (reg_info_dummy); \
  4532. if (!cant_free_wcs_buf) \
  4533. { \
  4534. FREE_VAR (string1); \
  4535. FREE_VAR (string2); \
  4536. FREE_VAR (mbs_offset1); \
  4537. FREE_VAR (mbs_offset2); \
  4538. } \
  4539. } while (0)
  4540. # else /* BYTE */
  4541. # define FREE_VARIABLES() \
  4542. do { \
  4543. REGEX_FREE_STACK (fail_stack.stack); \
  4544. FREE_VAR (regstart); \
  4545. FREE_VAR (regend); \
  4546. FREE_VAR (old_regstart); \
  4547. FREE_VAR (old_regend); \
  4548. FREE_VAR (best_regstart); \
  4549. FREE_VAR (best_regend); \
  4550. FREE_VAR (reg_info); \
  4551. FREE_VAR (reg_dummy); \
  4552. FREE_VAR (reg_info_dummy); \
  4553. } while (0)
  4554. # endif /* WCHAR */
  4555. #else
  4556. # ifdef WCHAR
  4557. # define FREE_VARIABLES() \
  4558. do { \
  4559. if (!cant_free_wcs_buf) \
  4560. { \
  4561. FREE_VAR (string1); \
  4562. FREE_VAR (string2); \
  4563. FREE_VAR (mbs_offset1); \
  4564. FREE_VAR (mbs_offset2); \
  4565. } \
  4566. } while (0)
  4567. # else /* BYTE */
  4568. # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
  4569. # endif /* WCHAR */
  4570. #endif /* not MATCH_MAY_ALLOCATE */
  4571. /* These values must meet several constraints. They must not be valid
  4572. register values; since we have a limit of 255 registers (because
  4573. we use only one byte in the pattern for the register number), we can
  4574. use numbers larger than 255. They must differ by 1, because of
  4575. NUM_FAILURE_ITEMS above. And the value for the lowest register must
  4576. be larger than the value for the highest register, so we do not try
  4577. to actually save any registers when none are active. */
  4578. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  4579. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  4580. #else /* not INSIDE_RECURSION */
  4581. /* Matching routines. */
  4582. #ifndef emacs /* Emacs never uses this. */
  4583. /* re_match is like re_match_2 except it takes only a single string. */
  4584. int
  4585. re_match (struct re_pattern_buffer *bufp, const char *string,
  4586. int size, int pos, struct re_registers *regs)
  4587. {
  4588. int result;
  4589. # ifdef MBS_SUPPORT
  4590. if (MB_CUR_MAX != 1)
  4591. result = wcs_re_match_2_internal (bufp, NULL, 0, string, size,
  4592. pos, regs, size,
  4593. NULL, 0, NULL, 0, NULL, NULL);
  4594. else
  4595. # endif
  4596. result = byte_re_match_2_internal (bufp, NULL, 0, string, size,
  4597. pos, regs, size);
  4598. # ifndef REGEX_MALLOC
  4599. # ifdef C_ALLOCA
  4600. alloca (0);
  4601. # endif
  4602. # endif
  4603. return result;
  4604. }
  4605. # ifdef _LIBC
  4606. weak_alias (__re_match, re_match)
  4607. # endif
  4608. #endif /* not emacs */
  4609. #endif /* not INSIDE_RECURSION */
  4610. #ifdef INSIDE_RECURSION
  4611. static boolean PREFIX(group_match_null_string_p) (UCHAR_T **p,
  4612. UCHAR_T *end,
  4613. PREFIX(register_info_type) *reg_info);
  4614. static boolean PREFIX(alt_match_null_string_p) (UCHAR_T *p,
  4615. UCHAR_T *end,
  4616. PREFIX(register_info_type) *reg_info);
  4617. static boolean PREFIX(common_op_match_null_string_p) (UCHAR_T **p,
  4618. UCHAR_T *end,
  4619. PREFIX(register_info_type) *reg_info);
  4620. static int PREFIX(bcmp_translate) (const CHAR_T *s1, const CHAR_T *s2,
  4621. int len, char *translate);
  4622. #else /* not INSIDE_RECURSION */
  4623. /* re_match_2 matches the compiled pattern in BUFP against the
  4624. the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  4625. and SIZE2, respectively). We start matching at POS, and stop
  4626. matching at STOP.
  4627. If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  4628. store offsets for the substring each group matched in REGS. See the
  4629. documentation for exactly how many groups we fill.
  4630. We return -1 if no match, -2 if an internal error (such as the
  4631. failure stack overflowing). Otherwise, we return the length of the
  4632. matched substring. */
  4633. int
  4634. re_match_2 (struct re_pattern_buffer *bufp, const char *string1, int size1,
  4635. const char *string2, int size2, int pos,
  4636. struct re_registers *regs, int stop)
  4637. {
  4638. int result;
  4639. # ifdef MBS_SUPPORT
  4640. if (MB_CUR_MAX != 1)
  4641. result = wcs_re_match_2_internal (bufp, string1, size1, string2, size2,
  4642. pos, regs, stop,
  4643. NULL, 0, NULL, 0, NULL, NULL);
  4644. else
  4645. # endif
  4646. result = byte_re_match_2_internal (bufp, string1, size1, string2, size2,
  4647. pos, regs, stop);
  4648. #ifndef REGEX_MALLOC
  4649. # ifdef C_ALLOCA
  4650. alloca (0);
  4651. # endif
  4652. #endif
  4653. return result;
  4654. }
  4655. #ifdef _LIBC
  4656. weak_alias (__re_match_2, re_match_2)
  4657. #endif
  4658. #endif /* not INSIDE_RECURSION */
  4659. #ifdef INSIDE_RECURSION
  4660. #ifdef WCHAR
  4661. static int count_mbs_length (int *, int);
  4662. /* This check the substring (from 0, to length) of the multibyte string,
  4663. to which offset_buffer correspond. And count how many wchar_t_characters
  4664. the substring occupy. We use offset_buffer to optimization.
  4665. See convert_mbs_to_wcs. */
  4666. static int
  4667. count_mbs_length(int *offset_buffer, int length)
  4668. {
  4669. int upper, lower;
  4670. /* Check whether the size is valid. */
  4671. if (length < 0)
  4672. return -1;
  4673. if (offset_buffer == NULL)
  4674. return 0;
  4675. /* If there are no multibyte character, offset_buffer[i] == i.
  4676. Optmize for this case. */
  4677. if (offset_buffer[length] == length)
  4678. return length;
  4679. /* Set up upper with length. (because for all i, offset_buffer[i] >= i) */
  4680. upper = length;
  4681. lower = 0;
  4682. while (true)
  4683. {
  4684. int middle = (lower + upper) / 2;
  4685. if (middle == lower || middle == upper)
  4686. break;
  4687. if (offset_buffer[middle] > length)
  4688. upper = middle;
  4689. else if (offset_buffer[middle] < length)
  4690. lower = middle;
  4691. else
  4692. return middle;
  4693. }
  4694. return -1;
  4695. }
  4696. #endif /* WCHAR */
  4697. /* This is a separate function so that we can force an alloca cleanup
  4698. afterwards. */
  4699. #ifdef WCHAR
  4700. static int
  4701. wcs_re_match_2_internal (struct re_pattern_buffer *bufp,
  4702. const char *cstring1, int csize1,
  4703. const char *cstring2, int csize2,
  4704. int pos,
  4705. struct re_registers *regs,
  4706. int stop,
  4707. /* string1 == string2 == NULL means string1/2, size1/2 and
  4708. mbs_offset1/2 need seting up in this function. */
  4709. /* We need wchar_t* buffers correspond to cstring1, cstring2. */
  4710. wchar_t *string1, int size1,
  4711. wchar_t *string2, int size2,
  4712. /* offset buffer for optimizatoin. See convert_mbs_to_wc. */
  4713. int *mbs_offset1, int *mbs_offset2)
  4714. #else /* BYTE */
  4715. static int
  4716. byte_re_match_2_internal (struct re_pattern_buffer *bufp,
  4717. const char *string1, int size1,
  4718. const char *string2, int size2,
  4719. int pos,
  4720. struct re_registers *regs, int stop)
  4721. #endif /* BYTE */
  4722. {
  4723. /* General temporaries. */
  4724. int mcnt;
  4725. UCHAR_T *p1;
  4726. #ifdef WCHAR
  4727. /* They hold whether each wchar_t is binary data or not. */
  4728. char *is_binary = NULL;
  4729. /* If true, we can't free string1/2, mbs_offset1/2. */
  4730. int cant_free_wcs_buf = 1;
  4731. #endif /* WCHAR */
  4732. /* Just past the end of the corresponding string. */
  4733. const CHAR_T *end1, *end2;
  4734. /* Pointers into string1 and string2, just past the last characters in
  4735. each to consider matching. */
  4736. const CHAR_T *end_match_1, *end_match_2;
  4737. /* Where we are in the data, and the end of the current string. */
  4738. const CHAR_T *d, *dend;
  4739. /* Where we are in the pattern, and the end of the pattern. */
  4740. #ifdef WCHAR
  4741. UCHAR_T *pattern, *p;
  4742. register UCHAR_T *pend;
  4743. #else /* BYTE */
  4744. UCHAR_T *p = bufp->buffer;
  4745. register UCHAR_T *pend = p + bufp->used;
  4746. #endif /* WCHAR */
  4747. /* Mark the opcode just after a start_memory, so we can test for an
  4748. empty subpattern when we get to the stop_memory. */
  4749. UCHAR_T *just_past_start_mem = 0;
  4750. /* We use this to map every character in the string. */
  4751. RE_TRANSLATE_TYPE translate = bufp->translate;
  4752. /* Failure point stack. Each place that can handle a failure further
  4753. down the line pushes a failure point on this stack. It consists of
  4754. restart, regend, and reg_info for all registers corresponding to
  4755. the subexpressions we're currently inside, plus the number of such
  4756. registers, and, finally, two char *'s. The first char * is where
  4757. to resume scanning the pattern; the second one is where to resume
  4758. scanning the strings. If the latter is zero, the failure point is
  4759. a ``dummy''; if a failure happens and the failure point is a dummy,
  4760. it gets discarded and the next next one is tried. */
  4761. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  4762. PREFIX(fail_stack_type) fail_stack;
  4763. #endif
  4764. #ifdef DEBUG
  4765. static unsigned failure_id;
  4766. unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  4767. #endif
  4768. #ifdef REL_ALLOC
  4769. /* This holds the pointer to the failure stack, when
  4770. it is allocated relocatably. */
  4771. fail_stack_elt_t *failure_stack_ptr;
  4772. #endif
  4773. /* We fill all the registers internally, independent of what we
  4774. return, for use in backreferences. The number here includes
  4775. an element for register zero. */
  4776. size_t num_regs = bufp->re_nsub + 1;
  4777. /* The currently active registers. */
  4778. active_reg_t lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  4779. active_reg_t highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  4780. /* Information on the contents of registers. These are pointers into
  4781. the input strings; they record just what was matched (on this
  4782. attempt) by a subexpression part of the pattern, that is, the
  4783. regnum-th regstart pointer points to where in the pattern we began
  4784. matching and the regnum-th regend points to right after where we
  4785. stopped matching the regnum-th subexpression. (The zeroth register
  4786. keeps track of what the whole pattern matches.) */
  4787. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  4788. const CHAR_T **regstart, **regend;
  4789. #endif
  4790. /* If a group that's operated upon by a repetition operator fails to
  4791. match anything, then the register for its start will need to be
  4792. restored because it will have been set to wherever in the string we
  4793. are when we last see its open-group operator. Similarly for a
  4794. register's end. */
  4795. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  4796. const CHAR_T **old_regstart, **old_regend;
  4797. #endif
  4798. /* The is_active field of reg_info helps us keep track of which (possibly
  4799. nested) subexpressions we are currently in. The matched_something
  4800. field of reg_info[reg_num] helps us tell whether or not we have
  4801. matched any of the pattern so far this time through the reg_num-th
  4802. subexpression. These two fields get reset each time through any
  4803. loop their register is in. */
  4804. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  4805. PREFIX(register_info_type) *reg_info;
  4806. #endif
  4807. /* The following record the register info as found in the above
  4808. variables when we find a match better than any we've seen before.
  4809. This happens as we backtrack through the failure points, which in
  4810. turn happens only if we have not yet matched the entire string. */
  4811. unsigned best_regs_set = false;
  4812. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  4813. const CHAR_T **best_regstart, **best_regend;
  4814. #endif
  4815. /* Logically, this is `best_regend[0]'. But we don't want to have to
  4816. allocate space for that if we're not allocating space for anything
  4817. else (see below). Also, we never need info about register 0 for
  4818. any of the other register vectors, and it seems rather a kludge to
  4819. treat `best_regend' differently than the rest. So we keep track of
  4820. the end of the best match so far in a separate variable. We
  4821. initialize this to NULL so that when we backtrack the first time
  4822. and need to test it, it's not garbage. */
  4823. const CHAR_T *match_end = NULL;
  4824. /* This helps SET_REGS_MATCHED avoid doing redundant work. */
  4825. int set_regs_matched_done = 0;
  4826. /* Used when we pop values we don't care about. */
  4827. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  4828. const CHAR_T **reg_dummy;
  4829. PREFIX(register_info_type) *reg_info_dummy;
  4830. #endif
  4831. #ifdef DEBUG
  4832. /* Counts the total number of registers pushed. */
  4833. unsigned num_regs_pushed = 0;
  4834. #endif
  4835. DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  4836. INIT_FAIL_STACK ();
  4837. #ifdef MATCH_MAY_ALLOCATE
  4838. /* Do not bother to initialize all the register variables if there are
  4839. no groups in the pattern, as it takes a fair amount of time. If
  4840. there are groups, we include space for register 0 (the whole
  4841. pattern), even though we never use it, since it simplifies the
  4842. array indexing. We should fix this. */
  4843. if (bufp->re_nsub)
  4844. {
  4845. regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
  4846. regend = REGEX_TALLOC (num_regs, const CHAR_T *);
  4847. old_regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
  4848. old_regend = REGEX_TALLOC (num_regs, const CHAR_T *);
  4849. best_regstart = REGEX_TALLOC (num_regs, const CHAR_T *);
  4850. best_regend = REGEX_TALLOC (num_regs, const CHAR_T *);
  4851. reg_info = REGEX_TALLOC (num_regs, PREFIX(register_info_type));
  4852. reg_dummy = REGEX_TALLOC (num_regs, const CHAR_T *);
  4853. reg_info_dummy = REGEX_TALLOC (num_regs, PREFIX(register_info_type));
  4854. if (!(regstart && regend && old_regstart && old_regend && reg_info
  4855. && best_regstart && best_regend && reg_dummy && reg_info_dummy))
  4856. {
  4857. FREE_VARIABLES ();
  4858. return -2;
  4859. }
  4860. }
  4861. else
  4862. {
  4863. /* We must initialize all our variables to NULL, so that
  4864. `FREE_VARIABLES' doesn't try to free them. */
  4865. regstart = regend = old_regstart = old_regend = best_regstart
  4866. = best_regend = reg_dummy = NULL;
  4867. reg_info = reg_info_dummy = (PREFIX(register_info_type) *) NULL;
  4868. }
  4869. #endif /* MATCH_MAY_ALLOCATE */
  4870. /* The starting position is bogus. */
  4871. #ifdef WCHAR
  4872. if (pos < 0 || pos > csize1 + csize2)
  4873. #else /* BYTE */
  4874. if (pos < 0 || pos > size1 + size2)
  4875. #endif
  4876. {
  4877. FREE_VARIABLES ();
  4878. return -1;
  4879. }
  4880. #ifdef WCHAR
  4881. /* Allocate wchar_t array for string1 and string2 and
  4882. fill them with converted string. */
  4883. if (string1 == NULL && string2 == NULL)
  4884. {
  4885. /* We need seting up buffers here. */
  4886. /* We must free wcs buffers in this function. */
  4887. cant_free_wcs_buf = 0;
  4888. if (csize1 != 0)
  4889. {
  4890. string1 = REGEX_TALLOC (csize1 + 1, CHAR_T);
  4891. mbs_offset1 = REGEX_TALLOC (csize1 + 1, int);
  4892. is_binary = REGEX_TALLOC (csize1 + 1, char);
  4893. if (!string1 || !mbs_offset1 || !is_binary)
  4894. {
  4895. FREE_VAR (string1);
  4896. FREE_VAR (mbs_offset1);
  4897. FREE_VAR (is_binary);
  4898. return -2;
  4899. }
  4900. }
  4901. if (csize2 != 0)
  4902. {
  4903. string2 = REGEX_TALLOC (csize2 + 1, CHAR_T);
  4904. mbs_offset2 = REGEX_TALLOC (csize2 + 1, int);
  4905. is_binary = REGEX_TALLOC (csize2 + 1, char);
  4906. if (!string2 || !mbs_offset2 || !is_binary)
  4907. {
  4908. FREE_VAR (string1);
  4909. FREE_VAR (mbs_offset1);
  4910. FREE_VAR (string2);
  4911. FREE_VAR (mbs_offset2);
  4912. FREE_VAR (is_binary);
  4913. return -2;
  4914. }
  4915. size2 = convert_mbs_to_wcs(string2, cstring2, csize2,
  4916. mbs_offset2, is_binary);
  4917. string2[size2] = L'\0'; /* for a sentinel */
  4918. FREE_VAR (is_binary);
  4919. }
  4920. }
  4921. /* We need to cast pattern to (wchar_t*), because we casted this compiled
  4922. pattern to (char*) in regex_compile. */
  4923. p = pattern = (CHAR_T*)bufp->buffer;
  4924. pend = (CHAR_T*)(bufp->buffer + bufp->used);
  4925. #endif /* WCHAR */
  4926. /* Initialize subexpression text positions to -1 to mark ones that no
  4927. start_memory/stop_memory has been seen for. Also initialize the
  4928. register information struct. */
  4929. for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
  4930. {
  4931. regstart[mcnt] = regend[mcnt]
  4932. = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  4933. REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  4934. IS_ACTIVE (reg_info[mcnt]) = 0;
  4935. MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  4936. EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  4937. }
  4938. /* We move `string1' into `string2' if the latter's empty -- but not if
  4939. `string1' is null. */
  4940. if (size2 == 0 && string1 != NULL)
  4941. {
  4942. string2 = string1;
  4943. size2 = size1;
  4944. string1 = 0;
  4945. size1 = 0;
  4946. #ifdef WCHAR
  4947. mbs_offset2 = mbs_offset1;
  4948. csize2 = csize1;
  4949. mbs_offset1 = NULL;
  4950. csize1 = 0;
  4951. #endif
  4952. }
  4953. end1 = string1 + size1;
  4954. end2 = string2 + size2;
  4955. /* Compute where to stop matching, within the two strings. */
  4956. #ifdef WCHAR
  4957. if (stop <= csize1)
  4958. {
  4959. mcnt = count_mbs_length(mbs_offset1, stop);
  4960. end_match_1 = string1 + mcnt;
  4961. end_match_2 = string2;
  4962. }
  4963. else
  4964. {
  4965. if (stop > csize1 + csize2)
  4966. stop = csize1 + csize2;
  4967. end_match_1 = end1;
  4968. mcnt = count_mbs_length(mbs_offset2, stop-csize1);
  4969. end_match_2 = string2 + mcnt;
  4970. }
  4971. if (mcnt < 0)
  4972. { /* count_mbs_length return error. */
  4973. FREE_VARIABLES ();
  4974. return -1;
  4975. }
  4976. #else
  4977. if (stop <= size1)
  4978. {
  4979. end_match_1 = string1 + stop;
  4980. end_match_2 = string2;
  4981. }
  4982. else
  4983. {
  4984. end_match_1 = end1;
  4985. end_match_2 = string2 + stop - size1;
  4986. }
  4987. #endif /* WCHAR */
  4988. /* `p' scans through the pattern as `d' scans through the data.
  4989. `dend' is the end of the input string that `d' points within. `d'
  4990. is advanced into the following input string whenever necessary, but
  4991. this happens before fetching; therefore, at the beginning of the
  4992. loop, `d' can be pointing at the end of a string, but it cannot
  4993. equal `string2'. */
  4994. #ifdef WCHAR
  4995. if (size1 > 0 && pos <= csize1)
  4996. {
  4997. mcnt = count_mbs_length(mbs_offset1, pos);
  4998. d = string1 + mcnt;
  4999. dend = end_match_1;
  5000. }
  5001. else
  5002. {
  5003. mcnt = count_mbs_length(mbs_offset2, pos-csize1);
  5004. d = string2 + mcnt;
  5005. dend = end_match_2;
  5006. }
  5007. if (mcnt < 0)
  5008. { /* count_mbs_length return error. */
  5009. FREE_VARIABLES ();
  5010. return -1;
  5011. }
  5012. #else
  5013. if (size1 > 0 && pos <= size1)
  5014. {
  5015. d = string1 + pos;
  5016. dend = end_match_1;
  5017. }
  5018. else
  5019. {
  5020. d = string2 + pos - size1;
  5021. dend = end_match_2;
  5022. }
  5023. #endif /* WCHAR */
  5024. DEBUG_PRINT1 ("The compiled pattern is:\n");
  5025. DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  5026. DEBUG_PRINT1 ("The string to match is: `");
  5027. DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  5028. DEBUG_PRINT1 ("'\n");
  5029. /* This loops over pattern commands. It exits by returning from the
  5030. function if the match is complete, or it drops through if the match
  5031. fails at this starting point in the input data. */
  5032. for (;;)
  5033. {
  5034. #ifdef _LIBC
  5035. DEBUG_PRINT2 ("\n%p: ", p);
  5036. #else
  5037. DEBUG_PRINT2 ("\n0x%x: ", p);
  5038. #endif
  5039. if (p == pend)
  5040. { /* End of pattern means we might have succeeded. */
  5041. DEBUG_PRINT1 ("end of pattern ... ");
  5042. /* If we haven't matched the entire string, and we want the
  5043. longest match, try backtracking. */
  5044. if (d != end_match_2)
  5045. {
  5046. /* 1 if this match ends in the same string (string1 or string2)
  5047. as the best previous match. */
  5048. boolean same_str_p;
  5049. /* 1 if this match is the best seen so far. */
  5050. boolean best_match_p;
  5051. same_str_p = (FIRST_STRING_P (match_end)
  5052. == MATCHING_IN_FIRST_STRING);
  5053. /* AIX compiler got confused when this was combined
  5054. with the previous declaration. */
  5055. if (same_str_p)
  5056. best_match_p = d > match_end;
  5057. else
  5058. best_match_p = !MATCHING_IN_FIRST_STRING;
  5059. DEBUG_PRINT1 ("backtracking.\n");
  5060. if (!FAIL_STACK_EMPTY ())
  5061. { /* More failure points to try. */
  5062. /* If exceeds best match so far, save it. */
  5063. if (!best_regs_set || best_match_p)
  5064. {
  5065. best_regs_set = true;
  5066. match_end = d;
  5067. DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  5068. for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
  5069. {
  5070. best_regstart[mcnt] = regstart[mcnt];
  5071. best_regend[mcnt] = regend[mcnt];
  5072. }
  5073. }
  5074. goto fail;
  5075. }
  5076. /* If no failure points, don't restore garbage. And if
  5077. last match is real best match, don't restore second
  5078. best one. */
  5079. else if (best_regs_set && !best_match_p)
  5080. {
  5081. restore_best_regs:
  5082. /* Restore best match. It may happen that `dend ==
  5083. end_match_1' while the restored d is in string2.
  5084. For example, the pattern `x.*y.*z' against the
  5085. strings `x-' and `y-z-', if the two strings are
  5086. not consecutive in memory. */
  5087. DEBUG_PRINT1 ("Restoring best registers.\n");
  5088. d = match_end;
  5089. dend = ((d >= string1 && d <= end1)
  5090. ? end_match_1 : end_match_2);
  5091. for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
  5092. {
  5093. regstart[mcnt] = best_regstart[mcnt];
  5094. regend[mcnt] = best_regend[mcnt];
  5095. }
  5096. }
  5097. } /* d != end_match_2 */
  5098. succeed_label:
  5099. DEBUG_PRINT1 ("Accepting match.\n");
  5100. /* If caller wants register contents data back, do it. */
  5101. if (regs && !bufp->no_sub)
  5102. {
  5103. /* Have the register data arrays been allocated? */
  5104. if (bufp->regs_allocated == REGS_UNALLOCATED)
  5105. { /* No. So allocate them with malloc. We need one
  5106. extra element beyond `num_regs' for the `-1' marker
  5107. GNU code uses. */
  5108. regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  5109. regs->start = TALLOC (regs->num_regs, regoff_t);
  5110. regs->end = TALLOC (regs->num_regs, regoff_t);
  5111. if (regs->start == NULL || regs->end == NULL)
  5112. {
  5113. FREE_VARIABLES ();
  5114. return -2;
  5115. }
  5116. bufp->regs_allocated = REGS_REALLOCATE;
  5117. }
  5118. else if (bufp->regs_allocated == REGS_REALLOCATE)
  5119. { /* Yes. If we need more elements than were already
  5120. allocated, reallocate them. If we need fewer, just
  5121. leave it alone. */
  5122. if (regs->num_regs < num_regs + 1)
  5123. {
  5124. regs->num_regs = num_regs + 1;
  5125. RETALLOC (regs->start, regs->num_regs, regoff_t);
  5126. RETALLOC (regs->end, regs->num_regs, regoff_t);
  5127. if (regs->start == NULL || regs->end == NULL)
  5128. {
  5129. FREE_VARIABLES ();
  5130. return -2;
  5131. }
  5132. }
  5133. }
  5134. else
  5135. {
  5136. /* These braces fend off a "empty body in an else-statement"
  5137. warning under GCC when assert expands to nothing. */
  5138. assert (bufp->regs_allocated == REGS_FIXED);
  5139. }
  5140. /* Convert the pointer data in `regstart' and `regend' to
  5141. indices. Register zero has to be set differently,
  5142. since we haven't kept track of any info for it. */
  5143. if (regs->num_regs > 0)
  5144. {
  5145. regs->start[0] = pos;
  5146. #ifdef WCHAR
  5147. if (MATCHING_IN_FIRST_STRING)
  5148. regs->end[0] = mbs_offset1 != NULL ?
  5149. mbs_offset1[d-string1] : 0;
  5150. else
  5151. regs->end[0] = csize1 + (mbs_offset2 != NULL ?
  5152. mbs_offset2[d-string2] : 0);
  5153. #else
  5154. regs->end[0] = (MATCHING_IN_FIRST_STRING
  5155. ? ((regoff_t) (d - string1))
  5156. : ((regoff_t) (d - string2 + size1)));
  5157. #endif /* WCHAR */
  5158. }
  5159. /* Go through the first `min (num_regs, regs->num_regs)'
  5160. registers, since that is all we initialized. */
  5161. for (mcnt = 1; (unsigned) mcnt < MIN (num_regs, regs->num_regs);
  5162. mcnt++)
  5163. {
  5164. if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  5165. regs->start[mcnt] = regs->end[mcnt] = -1;
  5166. else
  5167. {
  5168. regs->start[mcnt]
  5169. = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
  5170. regs->end[mcnt]
  5171. = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
  5172. }
  5173. }
  5174. /* If the regs structure we return has more elements than
  5175. were in the pattern, set the extra elements to -1. If
  5176. we (re)allocated the registers, this is the case,
  5177. because we always allocate enough to have at least one
  5178. -1 at the end. */
  5179. for (mcnt = num_regs; (unsigned) mcnt < regs->num_regs; mcnt++)
  5180. regs->start[mcnt] = regs->end[mcnt] = -1;
  5181. } /* regs && !bufp->no_sub */
  5182. DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  5183. nfailure_points_pushed, nfailure_points_popped,
  5184. nfailure_points_pushed - nfailure_points_popped);
  5185. DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  5186. #ifdef WCHAR
  5187. if (MATCHING_IN_FIRST_STRING)
  5188. mcnt = mbs_offset1 != NULL ? mbs_offset1[d-string1] : 0;
  5189. else
  5190. mcnt = (mbs_offset2 != NULL ? mbs_offset2[d-string2] : 0) +
  5191. csize1;
  5192. mcnt -= pos;
  5193. #else
  5194. mcnt = d - pos - (MATCHING_IN_FIRST_STRING
  5195. ? string1
  5196. : string2 - size1);
  5197. #endif /* WCHAR */
  5198. DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  5199. FREE_VARIABLES ();
  5200. return mcnt;
  5201. }
  5202. /* Otherwise match next pattern command. */
  5203. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  5204. {
  5205. /* Ignore these. Used to ignore the n of succeed_n's which
  5206. currently have n == 0. */
  5207. case no_op:
  5208. DEBUG_PRINT1 ("EXECUTING no_op.\n");
  5209. break;
  5210. case succeed:
  5211. DEBUG_PRINT1 ("EXECUTING succeed.\n");
  5212. goto succeed_label;
  5213. /* Match the next n pattern characters exactly. The following
  5214. byte in the pattern defines n, and the n bytes after that
  5215. are the characters to match. */
  5216. case exactn:
  5217. #ifdef MBS_SUPPORT
  5218. case exactn_bin:
  5219. #endif
  5220. mcnt = *p++;
  5221. DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  5222. /* This is written out as an if-else so we don't waste time
  5223. testing `translate' inside the loop. */
  5224. if (translate)
  5225. {
  5226. do
  5227. {
  5228. PREFETCH ();
  5229. #ifdef WCHAR
  5230. if (*d <= 0xff)
  5231. {
  5232. if ((UCHAR_T) translate[(unsigned char) *d++]
  5233. != (UCHAR_T) *p++)
  5234. goto fail;
  5235. }
  5236. else
  5237. {
  5238. if (*d++ != (CHAR_T) *p++)
  5239. goto fail;
  5240. }
  5241. #else
  5242. if ((UCHAR_T) translate[(unsigned char) *d++]
  5243. != (UCHAR_T) *p++)
  5244. goto fail;
  5245. #endif /* WCHAR */
  5246. }
  5247. while (--mcnt);
  5248. }
  5249. else
  5250. {
  5251. do
  5252. {
  5253. PREFETCH ();
  5254. if (*d++ != (CHAR_T) *p++) goto fail;
  5255. }
  5256. while (--mcnt);
  5257. }
  5258. SET_REGS_MATCHED ();
  5259. break;
  5260. /* Match any character except possibly a newline or a null. */
  5261. case anychar:
  5262. DEBUG_PRINT1 ("EXECUTING anychar.\n");
  5263. PREFETCH ();
  5264. if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
  5265. || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
  5266. goto fail;
  5267. SET_REGS_MATCHED ();
  5268. DEBUG_PRINT2 (" Matched `%ld'.\n", (long int) *d);
  5269. d++;
  5270. break;
  5271. case charset:
  5272. case charset_not:
  5273. {
  5274. register UCHAR_T c;
  5275. #ifdef WCHAR
  5276. unsigned int i, char_class_length, coll_symbol_length,
  5277. equiv_class_length, ranges_length, chars_length, length;
  5278. CHAR_T *workp, *workp2, *charset_top;
  5279. #define WORK_BUFFER_SIZE 128
  5280. CHAR_T str_buf[WORK_BUFFER_SIZE];
  5281. # ifdef _LIBC
  5282. uint32_t nrules;
  5283. # endif /* _LIBC */
  5284. #endif /* WCHAR */
  5285. boolean negate = (re_opcode_t) *(p - 1) == charset_not;
  5286. DEBUG_PRINT2 ("EXECUTING charset%s.\n", negate ? "_not" : "");
  5287. PREFETCH ();
  5288. c = TRANSLATE (*d); /* The character to match. */
  5289. #ifdef WCHAR
  5290. # ifdef _LIBC
  5291. nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  5292. # endif /* _LIBC */
  5293. charset_top = p - 1;
  5294. char_class_length = *p++;
  5295. coll_symbol_length = *p++;
  5296. equiv_class_length = *p++;
  5297. ranges_length = *p++;
  5298. chars_length = *p++;
  5299. /* p points charset[6], so the address of the next instruction
  5300. (charset[l+m+n+2o+k+p']) equals p[l+m+n+2*o+p'],
  5301. where l=length of char_classes, m=length of collating_symbol,
  5302. n=equivalence_class, o=length of char_range,
  5303. p'=length of character. */
  5304. workp = p;
  5305. /* Update p to indicate the next instruction. */
  5306. p += char_class_length + coll_symbol_length+ equiv_class_length +
  5307. 2*ranges_length + chars_length;
  5308. /* match with char_class? */
  5309. for (i = 0; i < char_class_length ; i += CHAR_CLASS_SIZE)
  5310. {
  5311. wctype_t wctype;
  5312. uintptr_t alignedp = ((uintptr_t)workp
  5313. + __alignof__(wctype_t) - 1)
  5314. & ~(uintptr_t)(__alignof__(wctype_t) - 1);
  5315. wctype = *((wctype_t*)alignedp);
  5316. workp += CHAR_CLASS_SIZE;
  5317. # ifdef _LIBC
  5318. if (__iswctype((wint_t)c, wctype))
  5319. goto char_set_matched;
  5320. # else
  5321. if (iswctype((wint_t)c, wctype))
  5322. goto char_set_matched;
  5323. # endif
  5324. }
  5325. /* match with collating_symbol? */
  5326. # ifdef _LIBC
  5327. if (nrules != 0)
  5328. {
  5329. const unsigned char *extra = (const unsigned char *)
  5330. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_SYMB_EXTRAMB);
  5331. for (workp2 = workp + coll_symbol_length ; workp < workp2 ;
  5332. workp++)
  5333. {
  5334. int32_t *wextra;
  5335. wextra = (int32_t*)(extra + *workp++);
  5336. for (i = 0; i < *wextra; ++i)
  5337. if (TRANSLATE(d[i]) != wextra[1 + i])
  5338. break;
  5339. if (i == *wextra)
  5340. {
  5341. /* Update d, however d will be incremented at
  5342. char_set_matched:, we decrement d here. */
  5343. d += i - 1;
  5344. goto char_set_matched;
  5345. }
  5346. }
  5347. }
  5348. else /* (nrules == 0) */
  5349. # endif
  5350. /* If we can't look up collation data, we use wcscoll
  5351. instead. */
  5352. {
  5353. for (workp2 = workp + coll_symbol_length ; workp < workp2 ;)
  5354. {
  5355. const CHAR_T *backup_d = d, *backup_dend = dend;
  5356. # ifdef _LIBC
  5357. length = __wcslen (workp);
  5358. # else
  5359. length = wcslen (workp);
  5360. # endif
  5361. /* If wcscoll(the collating symbol, whole string) > 0,
  5362. any substring of the string never match with the
  5363. collating symbol. */
  5364. # ifdef _LIBC
  5365. if (__wcscoll (workp, d) > 0)
  5366. # else
  5367. if (wcscoll (workp, d) > 0)
  5368. # endif
  5369. {
  5370. workp += length + 1;
  5371. continue;
  5372. }
  5373. /* First, we compare the collating symbol with
  5374. the first character of the string.
  5375. If it don't match, we add the next character to
  5376. the compare buffer in turn. */
  5377. for (i = 0 ; i < WORK_BUFFER_SIZE-1 ; i++, d++)
  5378. {
  5379. int match;
  5380. if (d == dend)
  5381. {
  5382. if (dend == end_match_2)
  5383. break;
  5384. d = string2;
  5385. dend = end_match_2;
  5386. }
  5387. /* add next character to the compare buffer. */
  5388. str_buf[i] = TRANSLATE(*d);
  5389. str_buf[i+1] = '\0';
  5390. # ifdef _LIBC
  5391. match = __wcscoll (workp, str_buf);
  5392. # else
  5393. match = wcscoll (workp, str_buf);
  5394. # endif
  5395. if (match == 0)
  5396. goto char_set_matched;
  5397. if (match < 0)
  5398. /* (str_buf > workp) indicate (str_buf + X > workp),
  5399. because for all X (str_buf + X > str_buf).
  5400. So we don't need continue this loop. */
  5401. break;
  5402. /* Otherwise(str_buf < workp),
  5403. (str_buf+next_character) may equals (workp).
  5404. So we continue this loop. */
  5405. }
  5406. /* not matched */
  5407. d = backup_d;
  5408. dend = backup_dend;
  5409. workp += length + 1;
  5410. }
  5411. }
  5412. /* match with equivalence_class? */
  5413. # ifdef _LIBC
  5414. if (nrules != 0)
  5415. {
  5416. const CHAR_T *backup_d = d, *backup_dend = dend;
  5417. /* Try to match the equivalence class against
  5418. those known to the collate implementation. */
  5419. const int32_t *table;
  5420. const int32_t *weights;
  5421. const int32_t *extra;
  5422. const int32_t *indirect;
  5423. int32_t idx, idx2;
  5424. wint_t *cp;
  5425. size_t len;
  5426. /* This #include defines a local function! */
  5427. # include <locale/weightwc.h>
  5428. table = (const int32_t *)
  5429. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
  5430. weights = (const wint_t *)
  5431. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
  5432. extra = (const wint_t *)
  5433. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
  5434. indirect = (const int32_t *)
  5435. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
  5436. /* Write 1 collating element to str_buf, and
  5437. get its index. */
  5438. idx2 = 0;
  5439. for (i = 0 ; idx2 == 0 && i < WORK_BUFFER_SIZE - 1; i++)
  5440. {
  5441. cp = (wint_t*)str_buf;
  5442. if (d == dend)
  5443. {
  5444. if (dend == end_match_2)
  5445. break;
  5446. d = string2;
  5447. dend = end_match_2;
  5448. }
  5449. str_buf[i] = TRANSLATE(*(d+i));
  5450. str_buf[i+1] = '\0'; /* sentinel */
  5451. idx2 = findidx ((const wint_t**)&cp);
  5452. }
  5453. /* Update d, however d will be incremented at
  5454. char_set_matched:, we decrement d here. */
  5455. d = backup_d + ((wchar_t*)cp - (wchar_t*)str_buf - 1);
  5456. if (d >= dend)
  5457. {
  5458. if (dend == end_match_2)
  5459. d = dend;
  5460. else
  5461. {
  5462. d = string2;
  5463. dend = end_match_2;
  5464. }
  5465. }
  5466. len = weights[idx2];
  5467. for (workp2 = workp + equiv_class_length ; workp < workp2 ;
  5468. workp++)
  5469. {
  5470. idx = (int32_t)*workp;
  5471. /* We already checked idx != 0 in regex_compile. */
  5472. if (idx2 != 0 && len == weights[idx])
  5473. {
  5474. int cnt = 0;
  5475. while (cnt < len && (weights[idx + 1 + cnt]
  5476. == weights[idx2 + 1 + cnt]))
  5477. ++cnt;
  5478. if (cnt == len)
  5479. goto char_set_matched;
  5480. }
  5481. }
  5482. /* not matched */
  5483. d = backup_d;
  5484. dend = backup_dend;
  5485. }
  5486. else /* (nrules == 0) */
  5487. # endif
  5488. /* If we can't look up collation data, we use wcscoll
  5489. instead. */
  5490. {
  5491. for (workp2 = workp + equiv_class_length ; workp < workp2 ;)
  5492. {
  5493. const CHAR_T *backup_d = d, *backup_dend = dend;
  5494. # ifdef _LIBC
  5495. length = __wcslen (workp);
  5496. # else
  5497. length = wcslen (workp);
  5498. # endif
  5499. /* If wcscoll(the collating symbol, whole string) > 0,
  5500. any substring of the string never match with the
  5501. collating symbol. */
  5502. # ifdef _LIBC
  5503. if (__wcscoll (workp, d) > 0)
  5504. # else
  5505. if (wcscoll (workp, d) > 0)
  5506. # endif
  5507. {
  5508. workp += length + 1;
  5509. break;
  5510. }
  5511. /* First, we compare the equivalence class with
  5512. the first character of the string.
  5513. If it don't match, we add the next character to
  5514. the compare buffer in turn. */
  5515. for (i = 0 ; i < WORK_BUFFER_SIZE - 1 ; i++, d++)
  5516. {
  5517. int match;
  5518. if (d == dend)
  5519. {
  5520. if (dend == end_match_2)
  5521. break;
  5522. d = string2;
  5523. dend = end_match_2;
  5524. }
  5525. /* add next character to the compare buffer. */
  5526. str_buf[i] = TRANSLATE(*d);
  5527. str_buf[i+1] = '\0';
  5528. # ifdef _LIBC
  5529. match = __wcscoll (workp, str_buf);
  5530. # else
  5531. match = wcscoll (workp, str_buf);
  5532. # endif
  5533. if (match == 0)
  5534. goto char_set_matched;
  5535. if (match < 0)
  5536. /* (str_buf > workp) indicate (str_buf + X > workp),
  5537. because for all X (str_buf + X > str_buf).
  5538. So we don't need continue this loop. */
  5539. break;
  5540. /* Otherwise(str_buf < workp),
  5541. (str_buf+next_character) may equals (workp).
  5542. So we continue this loop. */
  5543. }
  5544. /* not matched */
  5545. d = backup_d;
  5546. dend = backup_dend;
  5547. workp += length + 1;
  5548. }
  5549. }
  5550. /* match with char_range? */
  5551. # ifdef _LIBC
  5552. if (nrules != 0)
  5553. {
  5554. uint32_t collseqval;
  5555. const char *collseq = (const char *)
  5556. _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC);
  5557. collseqval = collseq_table_lookup (collseq, c);
  5558. for (; workp < p - chars_length ;)
  5559. {
  5560. uint32_t start_val, end_val;
  5561. /* We already compute the collation sequence value
  5562. of the characters (or collating symbols). */
  5563. start_val = (uint32_t) *workp++; /* range_start */
  5564. end_val = (uint32_t) *workp++; /* range_end */
  5565. if (start_val <= collseqval && collseqval <= end_val)
  5566. goto char_set_matched;
  5567. }
  5568. }
  5569. else
  5570. # endif
  5571. {
  5572. /* We set range_start_char at str_buf[0], range_end_char
  5573. at str_buf[4], and compared char at str_buf[2]. */
  5574. str_buf[1] = 0;
  5575. str_buf[2] = c;
  5576. str_buf[3] = 0;
  5577. str_buf[5] = 0;
  5578. for (; workp < p - chars_length ;)
  5579. {
  5580. wchar_t *range_start_char, *range_end_char;
  5581. /* match if (range_start_char <= c <= range_end_char). */
  5582. /* If range_start(or end) < 0, we assume -range_start(end)
  5583. is the offset of the collating symbol which is specified
  5584. as the character of the range start(end). */
  5585. /* range_start */
  5586. if (*workp < 0)
  5587. range_start_char = charset_top - (*workp++);
  5588. else
  5589. {
  5590. str_buf[0] = *workp++;
  5591. range_start_char = str_buf;
  5592. }
  5593. /* range_end */
  5594. if (*workp < 0)
  5595. range_end_char = charset_top - (*workp++);
  5596. else
  5597. {
  5598. str_buf[4] = *workp++;
  5599. range_end_char = str_buf + 4;
  5600. }
  5601. # ifdef _LIBC
  5602. if (__wcscoll (range_start_char, str_buf+2) <= 0
  5603. && __wcscoll (str_buf+2, range_end_char) <= 0)
  5604. # else
  5605. if (wcscoll (range_start_char, str_buf+2) <= 0
  5606. && wcscoll (str_buf+2, range_end_char) <= 0)
  5607. # endif
  5608. goto char_set_matched;
  5609. }
  5610. }
  5611. /* match with char? */
  5612. for (; workp < p ; workp++)
  5613. if (c == *workp)
  5614. goto char_set_matched;
  5615. negate = !negate;
  5616. char_set_matched:
  5617. if (negate) goto fail;
  5618. #else
  5619. /* Cast to `unsigned' instead of `unsigned char' in case the
  5620. bit list is a full 32 bytes long. */
  5621. if (c < (unsigned) (*p * BYTEWIDTH)
  5622. && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  5623. negate = !negate;
  5624. p += 1 + *p;
  5625. if (!negate) goto fail;
  5626. #undef WORK_BUFFER_SIZE
  5627. #endif /* WCHAR */
  5628. SET_REGS_MATCHED ();
  5629. d++;
  5630. break;
  5631. }
  5632. /* The beginning of a group is represented by start_memory.
  5633. The arguments are the register number in the next byte, and the
  5634. number of groups inner to this one in the next. The text
  5635. matched within the group is recorded (in the internal
  5636. registers data structure) under the register number. */
  5637. case start_memory:
  5638. DEBUG_PRINT3 ("EXECUTING start_memory %ld (%ld):\n",
  5639. (long int) *p, (long int) p[1]);
  5640. /* Find out if this group can match the empty string. */
  5641. p1 = p; /* To send to group_match_null_string_p. */
  5642. if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  5643. REG_MATCH_NULL_STRING_P (reg_info[*p])
  5644. = PREFIX(group_match_null_string_p) (&p1, pend, reg_info);
  5645. /* Save the position in the string where we were the last time
  5646. we were at this open-group operator in case the group is
  5647. operated upon by a repetition operator, e.g., with `(a*)*b'
  5648. against `ab'; then we want to ignore where we are now in
  5649. the string in case this attempt to match fails. */
  5650. old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  5651. ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  5652. : regstart[*p];
  5653. DEBUG_PRINT2 (" old_regstart: %d\n",
  5654. POINTER_TO_OFFSET (old_regstart[*p]));
  5655. regstart[*p] = d;
  5656. DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  5657. IS_ACTIVE (reg_info[*p]) = 1;
  5658. MATCHED_SOMETHING (reg_info[*p]) = 0;
  5659. /* Clear this whenever we change the register activity status. */
  5660. set_regs_matched_done = 0;
  5661. /* This is the new highest active register. */
  5662. highest_active_reg = *p;
  5663. /* If nothing was active before, this is the new lowest active
  5664. register. */
  5665. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  5666. lowest_active_reg = *p;
  5667. /* Move past the register number and inner group count. */
  5668. p += 2;
  5669. just_past_start_mem = p;
  5670. break;
  5671. /* The stop_memory opcode represents the end of a group. Its
  5672. arguments are the same as start_memory's: the register
  5673. number, and the number of inner groups. */
  5674. case stop_memory:
  5675. DEBUG_PRINT3 ("EXECUTING stop_memory %ld (%ld):\n",
  5676. (long int) *p, (long int) p[1]);
  5677. /* We need to save the string position the last time we were at
  5678. this close-group operator in case the group is operated
  5679. upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  5680. against `aba'; then we want to ignore where we are now in
  5681. the string in case this attempt to match fails. */
  5682. old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  5683. ? REG_UNSET (regend[*p]) ? d : regend[*p]
  5684. : regend[*p];
  5685. DEBUG_PRINT2 (" old_regend: %d\n",
  5686. POINTER_TO_OFFSET (old_regend[*p]));
  5687. regend[*p] = d;
  5688. DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  5689. /* This register isn't active anymore. */
  5690. IS_ACTIVE (reg_info[*p]) = 0;
  5691. /* Clear this whenever we change the register activity status. */
  5692. set_regs_matched_done = 0;
  5693. /* If this was the only register active, nothing is active
  5694. anymore. */
  5695. if (lowest_active_reg == highest_active_reg)
  5696. {
  5697. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  5698. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  5699. }
  5700. else
  5701. { /* We must scan for the new highest active register, since
  5702. it isn't necessarily one less than now: consider
  5703. (a(b)c(d(e)f)g). When group 3 ends, after the f), the
  5704. new highest active register is 1. */
  5705. UCHAR_T r = *p - 1;
  5706. while (r > 0 && !IS_ACTIVE (reg_info[r]))
  5707. r--;
  5708. /* If we end up at register zero, that means that we saved
  5709. the registers as the result of an `on_failure_jump', not
  5710. a `start_memory', and we jumped to past the innermost
  5711. `stop_memory'. For example, in ((.)*) we save
  5712. registers 1 and 2 as a result of the *, but when we pop
  5713. back to the second ), we are at the stop_memory 1.
  5714. Thus, nothing is active. */
  5715. if (r == 0)
  5716. {
  5717. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  5718. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  5719. }
  5720. else
  5721. highest_active_reg = r;
  5722. }
  5723. /* If just failed to match something this time around with a
  5724. group that's operated on by a repetition operator, try to
  5725. force exit from the ``loop'', and restore the register
  5726. information for this group that we had before trying this
  5727. last match. */
  5728. if ((!MATCHED_SOMETHING (reg_info[*p])
  5729. || just_past_start_mem == p - 1)
  5730. && (p + 2) < pend)
  5731. {
  5732. boolean is_a_jump_n = false;
  5733. p1 = p + 2;
  5734. mcnt = 0;
  5735. switch ((re_opcode_t) *p1++)
  5736. {
  5737. case jump_n:
  5738. is_a_jump_n = true;
  5739. /* Fall through. */
  5740. case pop_failure_jump:
  5741. case maybe_pop_jump:
  5742. case jump:
  5743. case dummy_failure_jump:
  5744. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5745. if (is_a_jump_n)
  5746. p1 += OFFSET_ADDRESS_SIZE;
  5747. break;
  5748. default:
  5749. /* do nothing */ ;
  5750. }
  5751. p1 += mcnt;
  5752. /* If the next operation is a jump backwards in the pattern
  5753. to an on_failure_jump right before the start_memory
  5754. corresponding to this stop_memory, exit from the loop
  5755. by forcing a failure after pushing on the stack the
  5756. on_failure_jump's jump in the pattern, and d. */
  5757. if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  5758. && (re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == start_memory
  5759. && p1[2+OFFSET_ADDRESS_SIZE] == *p)
  5760. {
  5761. /* If this group ever matched anything, then restore
  5762. what its registers were before trying this last
  5763. failed match, e.g., with `(a*)*b' against `ab' for
  5764. regstart[1], and, e.g., with `((a*)*(b*)*)*'
  5765. against `aba' for regend[3].
  5766. Also restore the registers for inner groups for,
  5767. e.g., `((a*)(b*))*' against `aba' (register 3 would
  5768. otherwise get trashed). */
  5769. if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  5770. {
  5771. unsigned r;
  5772. EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  5773. /* Restore this and inner groups' (if any) registers. */
  5774. for (r = *p; r < (unsigned) *p + (unsigned) *(p + 1);
  5775. r++)
  5776. {
  5777. regstart[r] = old_regstart[r];
  5778. /* xx why this test? */
  5779. if (old_regend[r] >= regstart[r])
  5780. regend[r] = old_regend[r];
  5781. }
  5782. }
  5783. p1++;
  5784. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5785. PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  5786. goto fail;
  5787. }
  5788. }
  5789. /* Move past the register number and the inner group count. */
  5790. p += 2;
  5791. break;
  5792. /* \<digit> has been turned into a `duplicate' command which is
  5793. followed by the numeric value of <digit> as the register number. */
  5794. case duplicate:
  5795. {
  5796. register const CHAR_T *d2, *dend2;
  5797. int regno = *p++; /* Get which register to match against. */
  5798. DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  5799. /* Can't back reference a group which we've never matched. */
  5800. if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  5801. goto fail;
  5802. /* Where in input to try to start matching. */
  5803. d2 = regstart[regno];
  5804. /* Where to stop matching; if both the place to start and
  5805. the place to stop matching are in the same string, then
  5806. set to the place to stop, otherwise, for now have to use
  5807. the end of the first string. */
  5808. dend2 = ((FIRST_STRING_P (regstart[regno])
  5809. == FIRST_STRING_P (regend[regno]))
  5810. ? regend[regno] : end_match_1);
  5811. for (;;)
  5812. {
  5813. /* If necessary, advance to next segment in register
  5814. contents. */
  5815. while (d2 == dend2)
  5816. {
  5817. if (dend2 == end_match_2) break;
  5818. if (dend2 == regend[regno]) break;
  5819. /* End of string1 => advance to string2. */
  5820. d2 = string2;
  5821. dend2 = regend[regno];
  5822. }
  5823. /* At end of register contents => success */
  5824. if (d2 == dend2) break;
  5825. /* If necessary, advance to next segment in data. */
  5826. PREFETCH ();
  5827. /* How many characters left in this segment to match. */
  5828. mcnt = dend - d;
  5829. /* Want how many consecutive characters we can match in
  5830. one shot, so, if necessary, adjust the count. */
  5831. if (mcnt > dend2 - d2)
  5832. mcnt = dend2 - d2;
  5833. /* Compare that many; failure if mismatch, else move
  5834. past them. */
  5835. if (translate
  5836. ? PREFIX(bcmp_translate) (d, d2, mcnt, translate)
  5837. : memcmp (d, d2, mcnt*sizeof(UCHAR_T)))
  5838. goto fail;
  5839. d += mcnt, d2 += mcnt;
  5840. /* Do this because we've match some characters. */
  5841. SET_REGS_MATCHED ();
  5842. }
  5843. }
  5844. break;
  5845. /* begline matches the empty string at the beginning of the string
  5846. (unless `not_bol' is set in `bufp'), and, if
  5847. `newline_anchor' is set, after newlines. */
  5848. case begline:
  5849. DEBUG_PRINT1 ("EXECUTING begline.\n");
  5850. if (AT_STRINGS_BEG (d))
  5851. {
  5852. if (!bufp->not_bol) break;
  5853. }
  5854. else if (d[-1] == '\n' && bufp->newline_anchor)
  5855. {
  5856. break;
  5857. }
  5858. /* In all other cases, we fail. */
  5859. goto fail;
  5860. /* endline is the dual of begline. */
  5861. case endline:
  5862. DEBUG_PRINT1 ("EXECUTING endline.\n");
  5863. if (AT_STRINGS_END (d))
  5864. {
  5865. if (!bufp->not_eol) break;
  5866. }
  5867. /* We have to ``prefetch'' the next character. */
  5868. else if ((d == end1 ? *string2 : *d) == '\n'
  5869. && bufp->newline_anchor)
  5870. {
  5871. break;
  5872. }
  5873. goto fail;
  5874. /* Match at the very beginning of the data. */
  5875. case begbuf:
  5876. DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  5877. if (AT_STRINGS_BEG (d))
  5878. break;
  5879. goto fail;
  5880. /* Match at the very end of the data. */
  5881. case endbuf:
  5882. DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  5883. if (AT_STRINGS_END (d))
  5884. break;
  5885. goto fail;
  5886. /* on_failure_keep_string_jump is used to optimize `.*\n'. It
  5887. pushes NULL as the value for the string on the stack. Then
  5888. `pop_failure_point' will keep the current value for the
  5889. string, instead of restoring it. To see why, consider
  5890. matching `foo\nbar' against `.*\n'. The .* matches the foo;
  5891. then the . fails against the \n. But the next thing we want
  5892. to do is match the \n against the \n; if we restored the
  5893. string value, we would be back at the foo.
  5894. Because this is used only in specific cases, we don't need to
  5895. check all the things that `on_failure_jump' does, to make
  5896. sure the right things get saved on the stack. Hence we don't
  5897. share its code. The only reason to push anything on the
  5898. stack at all is that otherwise we would have to change
  5899. `anychar's code to do something besides goto fail in this
  5900. case; that seems worse than this. */
  5901. case on_failure_keep_string_jump:
  5902. DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  5903. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  5904. #ifdef _LIBC
  5905. DEBUG_PRINT3 (" %d (to %p):\n", mcnt, p + mcnt);
  5906. #else
  5907. DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  5908. #endif
  5909. PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  5910. break;
  5911. /* Uses of on_failure_jump:
  5912. Each alternative starts with an on_failure_jump that points
  5913. to the beginning of the next alternative. Each alternative
  5914. except the last ends with a jump that in effect jumps past
  5915. the rest of the alternatives. (They really jump to the
  5916. ending jump of the following alternative, because tensioning
  5917. these jumps is a hassle.)
  5918. Repeats start with an on_failure_jump that points past both
  5919. the repetition text and either the following jump or
  5920. pop_failure_jump back to this on_failure_jump. */
  5921. case on_failure_jump:
  5922. on_failure:
  5923. DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  5924. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  5925. #ifdef _LIBC
  5926. DEBUG_PRINT3 (" %d (to %p)", mcnt, p + mcnt);
  5927. #else
  5928. DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  5929. #endif
  5930. /* If this on_failure_jump comes right before a group (i.e.,
  5931. the original * applied to a group), save the information
  5932. for that group and all inner ones, so that if we fail back
  5933. to this point, the group's information will be correct.
  5934. For example, in \(a*\)*\1, we need the preceding group,
  5935. and in \(zz\(a*\)b*\)\2, we need the inner group. */
  5936. /* We can't use `p' to check ahead because we push
  5937. a failure point to `p + mcnt' after we do this. */
  5938. p1 = p;
  5939. /* We need to skip no_op's before we look for the
  5940. start_memory in case this on_failure_jump is happening as
  5941. the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  5942. against aba. */
  5943. while (p1 < pend && (re_opcode_t) *p1 == no_op)
  5944. p1++;
  5945. if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  5946. {
  5947. /* We have a new highest active register now. This will
  5948. get reset at the start_memory we are about to get to,
  5949. but we will have saved all the registers relevant to
  5950. this repetition op, as described above. */
  5951. highest_active_reg = *(p1 + 1) + *(p1 + 2);
  5952. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  5953. lowest_active_reg = *(p1 + 1);
  5954. }
  5955. DEBUG_PRINT1 (":\n");
  5956. PUSH_FAILURE_POINT (p + mcnt, d, -2);
  5957. break;
  5958. /* A smart repeat ends with `maybe_pop_jump'.
  5959. We change it to either `pop_failure_jump' or `jump'. */
  5960. case maybe_pop_jump:
  5961. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  5962. DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  5963. {
  5964. register UCHAR_T *p2 = p;
  5965. /* Compare the beginning of the repeat with what in the
  5966. pattern follows its end. If we can establish that there
  5967. is nothing that they would both match, i.e., that we
  5968. would have to backtrack because of (as in, e.g., `a*a')
  5969. then we can change to pop_failure_jump, because we'll
  5970. never have to backtrack.
  5971. This is not true in the case of alternatives: in
  5972. `(a|ab)*' we do need to backtrack to the `ab' alternative
  5973. (e.g., if the string was `ab'). But instead of trying to
  5974. detect that here, the alternative has put on a dummy
  5975. failure point which is what we will end up popping. */
  5976. /* Skip over open/close-group commands.
  5977. If what follows this loop is a ...+ construct,
  5978. look at what begins its body, since we will have to
  5979. match at least one of that. */
  5980. while (1)
  5981. {
  5982. if (p2 + 2 < pend
  5983. && ((re_opcode_t) *p2 == stop_memory
  5984. || (re_opcode_t) *p2 == start_memory))
  5985. p2 += 3;
  5986. else if (p2 + 2 + 2 * OFFSET_ADDRESS_SIZE < pend
  5987. && (re_opcode_t) *p2 == dummy_failure_jump)
  5988. p2 += 2 + 2 * OFFSET_ADDRESS_SIZE;
  5989. else
  5990. break;
  5991. }
  5992. p1 = p + mcnt;
  5993. /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  5994. to the `maybe_finalize_jump' of this case. Examine what
  5995. follows. */
  5996. /* If we're at the end of the pattern, we can change. */
  5997. if (p2 == pend)
  5998. {
  5999. /* Consider what happens when matching ":\(.*\)"
  6000. against ":/". I don't really understand this code
  6001. yet. */
  6002. p[-(1+OFFSET_ADDRESS_SIZE)] = (UCHAR_T)
  6003. pop_failure_jump;
  6004. DEBUG_PRINT1
  6005. (" End of pattern: change to `pop_failure_jump'.\n");
  6006. }
  6007. else if ((re_opcode_t) *p2 == exactn
  6008. #ifdef MBS_SUPPORT
  6009. || (re_opcode_t) *p2 == exactn_bin
  6010. #endif
  6011. || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  6012. {
  6013. register UCHAR_T c
  6014. = *p2 == (UCHAR_T) endline ? '\n' : p2[2];
  6015. if (((re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == exactn
  6016. #ifdef MBS_SUPPORT
  6017. || (re_opcode_t) p1[1+OFFSET_ADDRESS_SIZE] == exactn_bin
  6018. #endif
  6019. ) && p1[3+OFFSET_ADDRESS_SIZE] != c)
  6020. {
  6021. p[-(1+OFFSET_ADDRESS_SIZE)] = (UCHAR_T)
  6022. pop_failure_jump;
  6023. #ifdef WCHAR
  6024. DEBUG_PRINT3 (" %C != %C => pop_failure_jump.\n",
  6025. (wint_t) c,
  6026. (wint_t) p1[3+OFFSET_ADDRESS_SIZE]);
  6027. #else
  6028. DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
  6029. (char) c,
  6030. (char) p1[3+OFFSET_ADDRESS_SIZE]);
  6031. #endif
  6032. }
  6033. #ifndef WCHAR
  6034. else if ((re_opcode_t) p1[3] == charset
  6035. || (re_opcode_t) p1[3] == charset_not)
  6036. {
  6037. int negate = (re_opcode_t) p1[3] == charset_not;
  6038. if (c < (unsigned) (p1[4] * BYTEWIDTH)
  6039. && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  6040. negate = !negate;
  6041. /* `negate' is equal to 1 if c would match, which means
  6042. that we can't change to pop_failure_jump. */
  6043. if (!negate)
  6044. {
  6045. p[-3] = (unsigned char) pop_failure_jump;
  6046. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  6047. }
  6048. }
  6049. #endif /* not WCHAR */
  6050. }
  6051. #ifndef WCHAR
  6052. else if ((re_opcode_t) *p2 == charset)
  6053. {
  6054. /* We win if the first character of the loop is not part
  6055. of the charset. */
  6056. if ((re_opcode_t) p1[3] == exactn
  6057. && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
  6058. && (p2[2 + p1[5] / BYTEWIDTH]
  6059. & (1 << (p1[5] % BYTEWIDTH)))))
  6060. {
  6061. p[-3] = (unsigned char) pop_failure_jump;
  6062. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  6063. }
  6064. else if ((re_opcode_t) p1[3] == charset_not)
  6065. {
  6066. int idx;
  6067. /* We win if the charset_not inside the loop
  6068. lists every character listed in the charset after. */
  6069. for (idx = 0; idx < (int) p2[1]; idx++)
  6070. if (! (p2[2 + idx] == 0
  6071. || (idx < (int) p1[4]
  6072. && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
  6073. break;
  6074. if (idx == p2[1])
  6075. {
  6076. p[-3] = (unsigned char) pop_failure_jump;
  6077. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  6078. }
  6079. }
  6080. else if ((re_opcode_t) p1[3] == charset)
  6081. {
  6082. int idx;
  6083. /* We win if the charset inside the loop
  6084. has no overlap with the one after the loop. */
  6085. for (idx = 0;
  6086. idx < (int) p2[1] && idx < (int) p1[4];
  6087. idx++)
  6088. if ((p2[2 + idx] & p1[5 + idx]) != 0)
  6089. break;
  6090. if (idx == p2[1] || idx == p1[4])
  6091. {
  6092. p[-3] = (unsigned char) pop_failure_jump;
  6093. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  6094. }
  6095. }
  6096. }
  6097. #endif /* not WCHAR */
  6098. }
  6099. p -= OFFSET_ADDRESS_SIZE; /* Point at relative address again. */
  6100. if ((re_opcode_t) p[-1] != pop_failure_jump)
  6101. {
  6102. p[-1] = (UCHAR_T) jump;
  6103. DEBUG_PRINT1 (" Match => jump.\n");
  6104. goto unconditional_jump;
  6105. }
  6106. /* Fall through. */
  6107. /* The end of a simple repeat has a pop_failure_jump back to
  6108. its matching on_failure_jump, where the latter will push a
  6109. failure point. The pop_failure_jump takes off failure
  6110. points put on by this pop_failure_jump's matching
  6111. on_failure_jump; we got through the pattern to here from the
  6112. matching on_failure_jump, so didn't fail. */
  6113. case pop_failure_jump:
  6114. {
  6115. /* We need to pass separate storage for the lowest and
  6116. highest registers, even though we don't care about the
  6117. actual values. Otherwise, we will restore only one
  6118. register from the stack, since lowest will == highest in
  6119. `pop_failure_point'. */
  6120. active_reg_t dummy_low_reg, dummy_high_reg;
  6121. UCHAR_T *pdummy ATTRIBUTE_UNUSED = NULL;
  6122. const CHAR_T *sdummy ATTRIBUTE_UNUSED = NULL;
  6123. DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  6124. POP_FAILURE_POINT (sdummy, pdummy,
  6125. dummy_low_reg, dummy_high_reg,
  6126. reg_dummy, reg_dummy, reg_info_dummy);
  6127. }
  6128. /* Fall through. */
  6129. unconditional_jump:
  6130. #ifdef _LIBC
  6131. DEBUG_PRINT2 ("\n%p: ", p);
  6132. #else
  6133. DEBUG_PRINT2 ("\n0x%x: ", p);
  6134. #endif
  6135. /* Note fall through. */
  6136. /* Unconditionally jump (without popping any failure points). */
  6137. case jump:
  6138. EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
  6139. DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  6140. p += mcnt; /* Do the jump. */
  6141. #ifdef _LIBC
  6142. DEBUG_PRINT2 ("(to %p).\n", p);
  6143. #else
  6144. DEBUG_PRINT2 ("(to 0x%x).\n", p);
  6145. #endif
  6146. break;
  6147. /* We need this opcode so we can detect where alternatives end
  6148. in `group_match_null_string_p' et al. */
  6149. case jump_past_alt:
  6150. DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  6151. goto unconditional_jump;
  6152. /* Normally, the on_failure_jump pushes a failure point, which
  6153. then gets popped at pop_failure_jump. We will end up at
  6154. pop_failure_jump, also, and with a pattern of, say, `a+', we
  6155. are skipping over the on_failure_jump, so we have to push
  6156. something meaningless for pop_failure_jump to pop. */
  6157. case dummy_failure_jump:
  6158. DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  6159. /* It doesn't matter what we push for the string here. What
  6160. the code at `fail' tests is the value for the pattern. */
  6161. PUSH_FAILURE_POINT (NULL, NULL, -2);
  6162. goto unconditional_jump;
  6163. /* At the end of an alternative, we need to push a dummy failure
  6164. point in case we are followed by a `pop_failure_jump', because
  6165. we don't want the failure point for the alternative to be
  6166. popped. For example, matching `(a|ab)*' against `aab'
  6167. requires that we match the `ab' alternative. */
  6168. case push_dummy_failure:
  6169. DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  6170. /* See comments just above at `dummy_failure_jump' about the
  6171. two zeroes. */
  6172. PUSH_FAILURE_POINT (NULL, NULL, -2);
  6173. break;
  6174. /* Have to succeed matching what follows at least n times.
  6175. After that, handle like `on_failure_jump'. */
  6176. case succeed_n:
  6177. EXTRACT_NUMBER (mcnt, p + OFFSET_ADDRESS_SIZE);
  6178. DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  6179. assert (mcnt >= 0);
  6180. /* Originally, this is how many times we HAVE to succeed. */
  6181. if (mcnt > 0)
  6182. {
  6183. mcnt--;
  6184. p += OFFSET_ADDRESS_SIZE;
  6185. STORE_NUMBER_AND_INCR (p, mcnt);
  6186. #ifdef _LIBC
  6187. DEBUG_PRINT3 (" Setting %p to %d.\n", p - OFFSET_ADDRESS_SIZE
  6188. , mcnt);
  6189. #else
  6190. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p - OFFSET_ADDRESS_SIZE
  6191. , mcnt);
  6192. #endif
  6193. }
  6194. else if (mcnt == 0)
  6195. {
  6196. #ifdef _LIBC
  6197. DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n",
  6198. p + OFFSET_ADDRESS_SIZE);
  6199. #else
  6200. DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n",
  6201. p + OFFSET_ADDRESS_SIZE);
  6202. #endif /* _LIBC */
  6203. #ifdef WCHAR
  6204. p[1] = (UCHAR_T) no_op;
  6205. #else
  6206. p[2] = (UCHAR_T) no_op;
  6207. p[3] = (UCHAR_T) no_op;
  6208. #endif /* WCHAR */
  6209. goto on_failure;
  6210. }
  6211. break;
  6212. case jump_n:
  6213. EXTRACT_NUMBER (mcnt, p + OFFSET_ADDRESS_SIZE);
  6214. DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  6215. /* Originally, this is how many times we CAN jump. */
  6216. if (mcnt)
  6217. {
  6218. mcnt--;
  6219. STORE_NUMBER (p + OFFSET_ADDRESS_SIZE, mcnt);
  6220. #ifdef _LIBC
  6221. DEBUG_PRINT3 (" Setting %p to %d.\n", p + OFFSET_ADDRESS_SIZE,
  6222. mcnt);
  6223. #else
  6224. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p + OFFSET_ADDRESS_SIZE,
  6225. mcnt);
  6226. #endif /* _LIBC */
  6227. goto unconditional_jump;
  6228. }
  6229. /* If don't have to jump any more, skip over the rest of command. */
  6230. else
  6231. p += 2 * OFFSET_ADDRESS_SIZE;
  6232. break;
  6233. case set_number_at:
  6234. {
  6235. DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  6236. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  6237. p1 = p + mcnt;
  6238. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  6239. #ifdef _LIBC
  6240. DEBUG_PRINT3 (" Setting %p to %d.\n", p1, mcnt);
  6241. #else
  6242. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
  6243. #endif
  6244. STORE_NUMBER (p1, mcnt);
  6245. break;
  6246. }
  6247. #if 0
  6248. /* The DEC Alpha C compiler 3.x generates incorrect code for the
  6249. test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
  6250. AT_WORD_BOUNDARY, so this code is disabled. Expanding the
  6251. macro and introducing temporary variables works around the bug. */
  6252. case wordbound:
  6253. DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  6254. if (AT_WORD_BOUNDARY (d))
  6255. break;
  6256. goto fail;
  6257. case notwordbound:
  6258. DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  6259. if (AT_WORD_BOUNDARY (d))
  6260. goto fail;
  6261. break;
  6262. #else
  6263. case wordbound:
  6264. {
  6265. boolean prevchar, thischar;
  6266. DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  6267. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  6268. break;
  6269. prevchar = WORDCHAR_P (d - 1);
  6270. thischar = WORDCHAR_P (d);
  6271. if (prevchar != thischar)
  6272. break;
  6273. goto fail;
  6274. }
  6275. case notwordbound:
  6276. {
  6277. boolean prevchar, thischar;
  6278. DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  6279. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  6280. goto fail;
  6281. prevchar = WORDCHAR_P (d - 1);
  6282. thischar = WORDCHAR_P (d);
  6283. if (prevchar != thischar)
  6284. goto fail;
  6285. break;
  6286. }
  6287. #endif
  6288. case wordbeg:
  6289. DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  6290. if (!AT_STRINGS_END (d) && WORDCHAR_P (d)
  6291. && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
  6292. break;
  6293. goto fail;
  6294. case wordend:
  6295. DEBUG_PRINT1 ("EXECUTING wordend.\n");
  6296. if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
  6297. && (AT_STRINGS_END (d) || !WORDCHAR_P (d)))
  6298. break;
  6299. goto fail;
  6300. #ifdef emacs
  6301. case before_dot:
  6302. DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  6303. if (PTR_CHAR_POS ((unsigned char *) d) >= point)
  6304. goto fail;
  6305. break;
  6306. case at_dot:
  6307. DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  6308. if (PTR_CHAR_POS ((unsigned char *) d) != point)
  6309. goto fail;
  6310. break;
  6311. case after_dot:
  6312. DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  6313. if (PTR_CHAR_POS ((unsigned char *) d) <= point)
  6314. goto fail;
  6315. break;
  6316. case syntaxspec:
  6317. DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  6318. mcnt = *p++;
  6319. goto matchsyntax;
  6320. case wordchar:
  6321. DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  6322. mcnt = (int) Sword;
  6323. matchsyntax:
  6324. PREFETCH ();
  6325. /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
  6326. d++;
  6327. if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt)
  6328. goto fail;
  6329. SET_REGS_MATCHED ();
  6330. break;
  6331. case notsyntaxspec:
  6332. DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  6333. mcnt = *p++;
  6334. goto matchnotsyntax;
  6335. case notwordchar:
  6336. DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  6337. mcnt = (int) Sword;
  6338. matchnotsyntax:
  6339. PREFETCH ();
  6340. /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
  6341. d++;
  6342. if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt)
  6343. goto fail;
  6344. SET_REGS_MATCHED ();
  6345. break;
  6346. #else /* not emacs */
  6347. case wordchar:
  6348. DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  6349. PREFETCH ();
  6350. if (!WORDCHAR_P (d))
  6351. goto fail;
  6352. SET_REGS_MATCHED ();
  6353. d++;
  6354. break;
  6355. case notwordchar:
  6356. DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  6357. PREFETCH ();
  6358. if (WORDCHAR_P (d))
  6359. goto fail;
  6360. SET_REGS_MATCHED ();
  6361. d++;
  6362. break;
  6363. #endif /* not emacs */
  6364. default:
  6365. abort ();
  6366. }
  6367. continue; /* Successfully executed one pattern command; keep going. */
  6368. /* We goto here if a matching operation fails. */
  6369. fail:
  6370. if (!FAIL_STACK_EMPTY ())
  6371. { /* A restart point is known. Restore to that state. */
  6372. DEBUG_PRINT1 ("\nFAIL:\n");
  6373. POP_FAILURE_POINT (d, p,
  6374. lowest_active_reg, highest_active_reg,
  6375. regstart, regend, reg_info);
  6376. /* If this failure point is a dummy, try the next one. */
  6377. if (!p)
  6378. goto fail;
  6379. /* If we failed to the end of the pattern, don't examine *p. */
  6380. assert (p <= pend);
  6381. if (p < pend)
  6382. {
  6383. boolean is_a_jump_n = false;
  6384. /* If failed to a backwards jump that's part of a repetition
  6385. loop, need to pop this failure point and use the next one. */
  6386. switch ((re_opcode_t) *p)
  6387. {
  6388. case jump_n:
  6389. is_a_jump_n = true;
  6390. /* Fall through. */
  6391. case maybe_pop_jump:
  6392. case pop_failure_jump:
  6393. case jump:
  6394. p1 = p + 1;
  6395. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6396. p1 += mcnt;
  6397. if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  6398. || (!is_a_jump_n
  6399. && (re_opcode_t) *p1 == on_failure_jump))
  6400. goto fail;
  6401. break;
  6402. default:
  6403. /* do nothing */ ;
  6404. }
  6405. }
  6406. if (d >= string1 && d <= end1)
  6407. dend = end_match_1;
  6408. }
  6409. else
  6410. break; /* Matching at this starting point really fails. */
  6411. } /* for (;;) */
  6412. if (best_regs_set)
  6413. goto restore_best_regs;
  6414. FREE_VARIABLES ();
  6415. return -1; /* Failure to match. */
  6416. } /* re_match_2 */
  6417. /* Subroutine definitions for re_match_2. */
  6418. /* We are passed P pointing to a register number after a start_memory.
  6419. Return true if the pattern up to the corresponding stop_memory can
  6420. match the empty string, and false otherwise.
  6421. If we find the matching stop_memory, sets P to point to one past its number.
  6422. Otherwise, sets P to an undefined byte less than or equal to END.
  6423. We don't handle duplicates properly (yet). */
  6424. static boolean
  6425. PREFIX(group_match_null_string_p) (UCHAR_T **p, UCHAR_T *end,
  6426. PREFIX(register_info_type) *reg_info)
  6427. {
  6428. int mcnt;
  6429. /* Point to after the args to the start_memory. */
  6430. UCHAR_T *p1 = *p + 2;
  6431. while (p1 < end)
  6432. {
  6433. /* Skip over opcodes that can match nothing, and return true or
  6434. false, as appropriate, when we get to one that can't, or to the
  6435. matching stop_memory. */
  6436. switch ((re_opcode_t) *p1)
  6437. {
  6438. /* Could be either a loop or a series of alternatives. */
  6439. case on_failure_jump:
  6440. p1++;
  6441. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6442. /* If the next operation is not a jump backwards in the
  6443. pattern. */
  6444. if (mcnt >= 0)
  6445. {
  6446. /* Go through the on_failure_jumps of the alternatives,
  6447. seeing if any of the alternatives cannot match nothing.
  6448. The last alternative starts with only a jump,
  6449. whereas the rest start with on_failure_jump and end
  6450. with a jump, e.g., here is the pattern for `a|b|c':
  6451. /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  6452. /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  6453. /exactn/1/c
  6454. So, we have to first go through the first (n-1)
  6455. alternatives and then deal with the last one separately. */
  6456. /* Deal with the first (n-1) alternatives, which start
  6457. with an on_failure_jump (see above) that jumps to right
  6458. past a jump_past_alt. */
  6459. while ((re_opcode_t) p1[mcnt-(1+OFFSET_ADDRESS_SIZE)] ==
  6460. jump_past_alt)
  6461. {
  6462. /* `mcnt' holds how many bytes long the alternative
  6463. is, including the ending `jump_past_alt' and
  6464. its number. */
  6465. if (!PREFIX(alt_match_null_string_p) (p1, p1 + mcnt -
  6466. (1 + OFFSET_ADDRESS_SIZE),
  6467. reg_info))
  6468. return false;
  6469. /* Move to right after this alternative, including the
  6470. jump_past_alt. */
  6471. p1 += mcnt;
  6472. /* Break if it's the beginning of an n-th alternative
  6473. that doesn't begin with an on_failure_jump. */
  6474. if ((re_opcode_t) *p1 != on_failure_jump)
  6475. break;
  6476. /* Still have to check that it's not an n-th
  6477. alternative that starts with an on_failure_jump. */
  6478. p1++;
  6479. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6480. if ((re_opcode_t) p1[mcnt-(1+OFFSET_ADDRESS_SIZE)] !=
  6481. jump_past_alt)
  6482. {
  6483. /* Get to the beginning of the n-th alternative. */
  6484. p1 -= 1 + OFFSET_ADDRESS_SIZE;
  6485. break;
  6486. }
  6487. }
  6488. /* Deal with the last alternative: go back and get number
  6489. of the `jump_past_alt' just before it. `mcnt' contains
  6490. the length of the alternative. */
  6491. EXTRACT_NUMBER (mcnt, p1 - OFFSET_ADDRESS_SIZE);
  6492. if (!PREFIX(alt_match_null_string_p) (p1, p1 + mcnt, reg_info))
  6493. return false;
  6494. p1 += mcnt; /* Get past the n-th alternative. */
  6495. } /* if mcnt > 0 */
  6496. break;
  6497. case stop_memory:
  6498. assert (p1[1] == **p);
  6499. *p = p1 + 2;
  6500. return true;
  6501. default:
  6502. if (!PREFIX(common_op_match_null_string_p) (&p1, end, reg_info))
  6503. return false;
  6504. }
  6505. } /* while p1 < end */
  6506. return false;
  6507. } /* group_match_null_string_p */
  6508. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  6509. It expects P to be the first byte of a single alternative and END one
  6510. byte past the last. The alternative can contain groups. */
  6511. static boolean
  6512. PREFIX(alt_match_null_string_p) (UCHAR_T *p, UCHAR_T *end,
  6513. PREFIX(register_info_type) *reg_info)
  6514. {
  6515. int mcnt;
  6516. UCHAR_T *p1 = p;
  6517. while (p1 < end)
  6518. {
  6519. /* Skip over opcodes that can match nothing, and break when we get
  6520. to one that can't. */
  6521. switch ((re_opcode_t) *p1)
  6522. {
  6523. /* It's a loop. */
  6524. case on_failure_jump:
  6525. p1++;
  6526. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6527. p1 += mcnt;
  6528. break;
  6529. default:
  6530. if (!PREFIX(common_op_match_null_string_p) (&p1, end, reg_info))
  6531. return false;
  6532. }
  6533. } /* while p1 < end */
  6534. return true;
  6535. } /* alt_match_null_string_p */
  6536. /* Deals with the ops common to group_match_null_string_p and
  6537. alt_match_null_string_p.
  6538. Sets P to one after the op and its arguments, if any. */
  6539. static boolean
  6540. PREFIX(common_op_match_null_string_p) (UCHAR_T **p, UCHAR_T *end,
  6541. PREFIX(register_info_type) *reg_info)
  6542. {
  6543. int mcnt;
  6544. boolean ret;
  6545. int reg_no;
  6546. UCHAR_T *p1 = *p;
  6547. switch ((re_opcode_t) *p1++)
  6548. {
  6549. case no_op:
  6550. case begline:
  6551. case endline:
  6552. case begbuf:
  6553. case endbuf:
  6554. case wordbeg:
  6555. case wordend:
  6556. case wordbound:
  6557. case notwordbound:
  6558. #ifdef emacs
  6559. case before_dot:
  6560. case at_dot:
  6561. case after_dot:
  6562. #endif
  6563. break;
  6564. case start_memory:
  6565. reg_no = *p1;
  6566. assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  6567. ret = PREFIX(group_match_null_string_p) (&p1, end, reg_info);
  6568. /* Have to set this here in case we're checking a group which
  6569. contains a group and a back reference to it. */
  6570. if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  6571. REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  6572. if (!ret)
  6573. return false;
  6574. break;
  6575. /* If this is an optimized succeed_n for zero times, make the jump. */
  6576. case jump:
  6577. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6578. if (mcnt >= 0)
  6579. p1 += mcnt;
  6580. else
  6581. return false;
  6582. break;
  6583. case succeed_n:
  6584. /* Get to the number of times to succeed. */
  6585. p1 += OFFSET_ADDRESS_SIZE;
  6586. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6587. if (mcnt == 0)
  6588. {
  6589. p1 -= 2 * OFFSET_ADDRESS_SIZE;
  6590. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  6591. p1 += mcnt;
  6592. }
  6593. else
  6594. return false;
  6595. break;
  6596. case duplicate:
  6597. if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  6598. return false;
  6599. break;
  6600. case set_number_at:
  6601. p1 += 2 * OFFSET_ADDRESS_SIZE;
  6602. return false;
  6603. default:
  6604. /* All other opcodes mean we cannot match the empty string. */
  6605. return false;
  6606. }
  6607. *p = p1;
  6608. return true;
  6609. } /* common_op_match_null_string_p */
  6610. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  6611. bytes; nonzero otherwise. */
  6612. static int
  6613. PREFIX(bcmp_translate) (const CHAR_T *s1, const CHAR_T *s2, register int len,
  6614. RE_TRANSLATE_TYPE translate)
  6615. {
  6616. register const UCHAR_T *p1 = (const UCHAR_T *) s1;
  6617. register const UCHAR_T *p2 = (const UCHAR_T *) s2;
  6618. while (len)
  6619. {
  6620. #ifdef WCHAR
  6621. if (((*p1<=0xff)?translate[*p1++]:*p1++)
  6622. != ((*p2<=0xff)?translate[*p2++]:*p2++))
  6623. return 1;
  6624. #else /* BYTE */
  6625. if (translate[*p1++] != translate[*p2++]) return 1;
  6626. #endif /* WCHAR */
  6627. len--;
  6628. }
  6629. return 0;
  6630. }
  6631. #else /* not INSIDE_RECURSION */
  6632. /* Entry points for GNU code. */
  6633. /* re_compile_pattern is the GNU regular expression compiler: it
  6634. compiles PATTERN (of length SIZE) and puts the result in BUFP.
  6635. Returns 0 if the pattern was valid, otherwise an error string.
  6636. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  6637. are set in BUFP on entry.
  6638. We call regex_compile to do the actual compilation. */
  6639. const char *
  6640. re_compile_pattern (const char *pattern, size_t length,
  6641. struct re_pattern_buffer *bufp)
  6642. {
  6643. reg_errcode_t ret;
  6644. /* GNU code is written to assume at least RE_NREGS registers will be set
  6645. (and at least one extra will be -1). */
  6646. bufp->regs_allocated = REGS_UNALLOCATED;
  6647. /* And GNU code determines whether or not to get register information
  6648. by passing null for the REGS argument to re_match, etc., not by
  6649. setting no_sub. */
  6650. bufp->no_sub = 0;
  6651. /* Match anchors at newline. */
  6652. bufp->newline_anchor = 1;
  6653. # ifdef MBS_SUPPORT
  6654. if (MB_CUR_MAX != 1)
  6655. ret = wcs_regex_compile (pattern, length, re_syntax_options, bufp);
  6656. else
  6657. # endif
  6658. ret = byte_regex_compile (pattern, length, re_syntax_options, bufp);
  6659. if (!ret)
  6660. return NULL;
  6661. return gettext (re_error_msgid[(int) ret]);
  6662. }
  6663. #ifdef _LIBC
  6664. weak_alias (__re_compile_pattern, re_compile_pattern)
  6665. #endif
  6666. /* Entry points compatible with 4.2 BSD regex library. We don't define
  6667. them unless specifically requested. */
  6668. #if defined _REGEX_RE_COMP || defined _LIBC
  6669. /* BSD has one and only one pattern buffer. */
  6670. static struct re_pattern_buffer re_comp_buf;
  6671. char *
  6672. #ifdef _LIBC
  6673. /* Make these definitions weak in libc, so POSIX programs can redefine
  6674. these names if they don't use our functions, and still use
  6675. regcomp/regexec below without link errors. */
  6676. weak_function
  6677. #endif
  6678. re_comp (const char *s)
  6679. {
  6680. reg_errcode_t ret;
  6681. if (!s)
  6682. {
  6683. if (!re_comp_buf.buffer)
  6684. return (char *) gettext ("No previous regular expression");
  6685. return 0;
  6686. }
  6687. if (!re_comp_buf.buffer)
  6688. {
  6689. re_comp_buf.buffer = (unsigned char *) malloc (200);
  6690. if (re_comp_buf.buffer == NULL)
  6691. return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
  6692. re_comp_buf.allocated = 200;
  6693. re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  6694. if (re_comp_buf.fastmap == NULL)
  6695. return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
  6696. }
  6697. /* Since `re_exec' always passes NULL for the `regs' argument, we
  6698. don't need to initialize the pattern buffer fields which affect it. */
  6699. /* Match anchors at newlines. */
  6700. re_comp_buf.newline_anchor = 1;
  6701. # ifdef MBS_SUPPORT
  6702. if (MB_CUR_MAX != 1)
  6703. ret = wcs_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  6704. else
  6705. # endif
  6706. ret = byte_regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  6707. if (!ret)
  6708. return NULL;
  6709. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  6710. return (char *) gettext (re_error_msgid[(int) ret]);
  6711. }
  6712. int
  6713. #ifdef _LIBC
  6714. weak_function
  6715. #endif
  6716. re_exec (const char *s)
  6717. {
  6718. const int len = strlen (s);
  6719. return
  6720. 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  6721. }
  6722. #endif /* _REGEX_RE_COMP */
  6723. /* POSIX.2 functions. Don't define these for Emacs. */
  6724. #ifndef emacs
  6725. /* regcomp takes a regular expression as a string and compiles it.
  6726. PREG is a regex_t *. We do not expect any fields to be initialized,
  6727. since POSIX says we shouldn't. Thus, we set
  6728. `buffer' to the compiled pattern;
  6729. `used' to the length of the compiled pattern;
  6730. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  6731. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  6732. RE_SYNTAX_POSIX_BASIC;
  6733. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  6734. `fastmap' to an allocated space for the fastmap;
  6735. `fastmap_accurate' to zero;
  6736. `re_nsub' to the number of subexpressions in PATTERN.
  6737. PATTERN is the address of the pattern string.
  6738. CFLAGS is a series of bits which affect compilation.
  6739. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  6740. use POSIX basic syntax.
  6741. If REG_NEWLINE is set, then . and [^...] don't match newline.
  6742. Also, regexec will try a match beginning after every newline.
  6743. If REG_ICASE is set, then we considers upper- and lowercase
  6744. versions of letters to be equivalent when matching.
  6745. If REG_NOSUB is set, then when PREG is passed to regexec, that
  6746. routine will report only success or failure, and nothing about the
  6747. registers.
  6748. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  6749. the return codes and their meanings.) */
  6750. int
  6751. regcomp (regex_t *preg, const char *pattern, int cflags)
  6752. {
  6753. reg_errcode_t ret;
  6754. reg_syntax_t syntax
  6755. = (cflags & REG_EXTENDED) ?
  6756. RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  6757. /* regex_compile will allocate the space for the compiled pattern. */
  6758. preg->buffer = 0;
  6759. preg->allocated = 0;
  6760. preg->used = 0;
  6761. /* Try to allocate space for the fastmap. */
  6762. preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
  6763. if (cflags & REG_ICASE)
  6764. {
  6765. int i;
  6766. preg->translate
  6767. = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
  6768. * sizeof (*(RE_TRANSLATE_TYPE)0));
  6769. if (preg->translate == NULL)
  6770. return (int) REG_ESPACE;
  6771. /* Map uppercase characters to corresponding lowercase ones. */
  6772. for (i = 0; i < CHAR_SET_SIZE; i++)
  6773. preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
  6774. }
  6775. else
  6776. preg->translate = NULL;
  6777. /* If REG_NEWLINE is set, newlines are treated differently. */
  6778. if (cflags & REG_NEWLINE)
  6779. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  6780. syntax &= ~RE_DOT_NEWLINE;
  6781. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  6782. /* It also changes the matching behavior. */
  6783. preg->newline_anchor = 1;
  6784. }
  6785. else
  6786. preg->newline_anchor = 0;
  6787. preg->no_sub = !!(cflags & REG_NOSUB);
  6788. /* POSIX says a null character in the pattern terminates it, so we
  6789. can use strlen here in compiling the pattern. */
  6790. # ifdef MBS_SUPPORT
  6791. if (MB_CUR_MAX != 1)
  6792. ret = wcs_regex_compile (pattern, strlen (pattern), syntax, preg);
  6793. else
  6794. # endif
  6795. ret = byte_regex_compile (pattern, strlen (pattern), syntax, preg);
  6796. /* POSIX doesn't distinguish between an unmatched open-group and an
  6797. unmatched close-group: both are REG_EPAREN. */
  6798. if (ret == REG_ERPAREN) ret = REG_EPAREN;
  6799. if (ret == REG_NOERROR && preg->fastmap)
  6800. {
  6801. /* Compute the fastmap now, since regexec cannot modify the pattern
  6802. buffer. */
  6803. if (re_compile_fastmap (preg) == -2)
  6804. {
  6805. /* Some error occurred while computing the fastmap, just forget
  6806. about it. */
  6807. free (preg->fastmap);
  6808. preg->fastmap = NULL;
  6809. }
  6810. }
  6811. return (int) ret;
  6812. }
  6813. #ifdef _LIBC
  6814. weak_alias (__regcomp, regcomp)
  6815. #endif
  6816. /* regexec searches for a given pattern, specified by PREG, in the
  6817. string STRING.
  6818. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  6819. `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  6820. least NMATCH elements, and we set them to the offsets of the
  6821. corresponding matched substrings.
  6822. EFLAGS specifies `execution flags' which affect matching: if
  6823. REG_NOTBOL is set, then ^ does not match at the beginning of the
  6824. string; if REG_NOTEOL is set, then $ does not match at the end.
  6825. We return 0 if we find a match and REG_NOMATCH if not. */
  6826. int
  6827. regexec (const regex_t *preg, const char *string, size_t nmatch,
  6828. regmatch_t pmatch[], int eflags)
  6829. {
  6830. int ret;
  6831. struct re_registers regs;
  6832. regex_t private_preg;
  6833. int len = strlen (string);
  6834. boolean want_reg_info = !preg->no_sub && nmatch > 0;
  6835. private_preg = *preg;
  6836. private_preg.not_bol = !!(eflags & REG_NOTBOL);
  6837. private_preg.not_eol = !!(eflags & REG_NOTEOL);
  6838. /* The user has told us exactly how many registers to return
  6839. information about, via `nmatch'. We have to pass that on to the
  6840. matching routines. */
  6841. private_preg.regs_allocated = REGS_FIXED;
  6842. if (want_reg_info)
  6843. {
  6844. regs.num_regs = nmatch;
  6845. regs.start = TALLOC (nmatch * 2, regoff_t);
  6846. if (regs.start == NULL)
  6847. return (int) REG_NOMATCH;
  6848. regs.end = regs.start + nmatch;
  6849. }
  6850. /* Perform the searching operation. */
  6851. ret = re_search (&private_preg, string, len,
  6852. /* start: */ 0, /* range: */ len,
  6853. want_reg_info ? &regs : (struct re_registers *) 0);
  6854. /* Copy the register information to the POSIX structure. */
  6855. if (want_reg_info)
  6856. {
  6857. if (ret >= 0)
  6858. {
  6859. unsigned r;
  6860. for (r = 0; r < nmatch; r++)
  6861. {
  6862. pmatch[r].rm_so = regs.start[r];
  6863. pmatch[r].rm_eo = regs.end[r];
  6864. }
  6865. }
  6866. /* If we needed the temporary register info, free the space now. */
  6867. free (regs.start);
  6868. }
  6869. /* We want zero return to mean success, unlike `re_search'. */
  6870. return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  6871. }
  6872. #ifdef _LIBC
  6873. weak_alias (__regexec, regexec)
  6874. #endif
  6875. /* Returns a message corresponding to an error code, ERRCODE, returned
  6876. from either regcomp or regexec. We don't use PREG here. */
  6877. size_t
  6878. regerror (int errcode, const regex_t *preg ATTRIBUTE_UNUSED,
  6879. char *errbuf, size_t errbuf_size)
  6880. {
  6881. const char *msg;
  6882. size_t msg_size;
  6883. if (errcode < 0
  6884. || errcode >= (int) (sizeof (re_error_msgid)
  6885. / sizeof (re_error_msgid[0])))
  6886. /* Only error codes returned by the rest of the code should be passed
  6887. to this routine. If we are given anything else, or if other regex
  6888. code generates an invalid error code, then the program has a bug.
  6889. Dump core so we can fix it. */
  6890. abort ();
  6891. msg = gettext (re_error_msgid[errcode]);
  6892. msg_size = strlen (msg) + 1; /* Includes the null. */
  6893. if (errbuf_size != 0)
  6894. {
  6895. if (msg_size > errbuf_size)
  6896. {
  6897. #if defined HAVE_MEMPCPY || defined _LIBC
  6898. *((char *) mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
  6899. #else
  6900. (void) memcpy (errbuf, msg, errbuf_size - 1);
  6901. errbuf[errbuf_size - 1] = 0;
  6902. #endif
  6903. }
  6904. else
  6905. (void) memcpy (errbuf, msg, msg_size);
  6906. }
  6907. return msg_size;
  6908. }
  6909. #ifdef _LIBC
  6910. weak_alias (__regerror, regerror)
  6911. #endif
  6912. /* Free dynamically allocated space used by PREG. */
  6913. void
  6914. regfree (regex_t *preg)
  6915. {
  6916. free (preg->buffer);
  6917. preg->buffer = NULL;
  6918. preg->allocated = 0;
  6919. preg->used = 0;
  6920. free (preg->fastmap);
  6921. preg->fastmap = NULL;
  6922. preg->fastmap_accurate = 0;
  6923. free (preg->translate);
  6924. preg->translate = NULL;
  6925. }
  6926. #ifdef _LIBC
  6927. weak_alias (__regfree, regfree)
  6928. #endif
  6929. #endif /* not emacs */
  6930. #endif /* not INSIDE_RECURSION */
  6931. #undef STORE_NUMBER
  6932. #undef STORE_NUMBER_AND_INCR
  6933. #undef EXTRACT_NUMBER
  6934. #undef EXTRACT_NUMBER_AND_INCR
  6935. #undef DEBUG_PRINT_COMPILED_PATTERN
  6936. #undef DEBUG_PRINT_DOUBLE_STRING
  6937. #undef INIT_FAIL_STACK
  6938. #undef RESET_FAIL_STACK
  6939. #undef DOUBLE_FAIL_STACK
  6940. #undef PUSH_PATTERN_OP
  6941. #undef PUSH_FAILURE_POINTER
  6942. #undef PUSH_FAILURE_INT
  6943. #undef PUSH_FAILURE_ELT
  6944. #undef POP_FAILURE_POINTER
  6945. #undef POP_FAILURE_INT
  6946. #undef POP_FAILURE_ELT
  6947. #undef DEBUG_PUSH
  6948. #undef DEBUG_POP
  6949. #undef PUSH_FAILURE_POINT
  6950. #undef POP_FAILURE_POINT
  6951. #undef REG_UNSET_VALUE
  6952. #undef REG_UNSET
  6953. #undef PATFETCH
  6954. #undef PATFETCH_RAW
  6955. #undef PATUNFETCH
  6956. #undef TRANSLATE
  6957. #undef INIT_BUF_SIZE
  6958. #undef GET_BUFFER_SPACE
  6959. #undef BUF_PUSH
  6960. #undef BUF_PUSH_2
  6961. #undef BUF_PUSH_3
  6962. #undef STORE_JUMP
  6963. #undef STORE_JUMP2
  6964. #undef INSERT_JUMP
  6965. #undef INSERT_JUMP2
  6966. #undef EXTEND_BUFFER
  6967. #undef GET_UNSIGNED_NUMBER
  6968. #undef FREE_STACK_RETURN
  6969. # undef POINTER_TO_OFFSET
  6970. # undef MATCHING_IN_FRST_STRING
  6971. # undef PREFETCH
  6972. # undef AT_STRINGS_BEG
  6973. # undef AT_STRINGS_END
  6974. # undef WORDCHAR_P
  6975. # undef FREE_VAR
  6976. # undef FREE_VARIABLES
  6977. # undef NO_HIGHEST_ACTIVE_REG
  6978. # undef NO_LOWEST_ACTIVE_REG
  6979. # undef CHAR_T
  6980. # undef UCHAR_T
  6981. # undef COMPILED_BUFFER_VAR
  6982. # undef OFFSET_ADDRESS_SIZE
  6983. # undef CHAR_CLASS_SIZE
  6984. # undef PREFIX
  6985. # undef ARG_PREFIX
  6986. # undef PUT_CHAR
  6987. # undef BYTE
  6988. # undef WCHAR
  6989. # define DEFINED_ONCE