regex_old.c 256 KB

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