regex.c 253 KB

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