regex_old.c 251 KB

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