regex_old.c 255 KB

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