regex_old.c 251 KB

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