regex.c 255 KB

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