regex_old.c 256 KB

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