regex_old.c 254 KB

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