regex.c 255 KB

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