regex_old.c 256 KB

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