regex_old.c 251 KB

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