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