regex.c 182 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374
  1. /* Extended regular expression matching and search library, version
  2. 0.12. (Implements POSIX draft P10003.2/D11.2, except for
  3. internationalization features.)
  4. Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. USA. */
  17. /* AIX requires this to be the first thing in the file. */
  18. #if defined (_AIX) && !defined (REGEX_MALLOC)
  19. #pragma alloca
  20. #endif
  21. #undef _GNU_SOURCE
  22. #define _GNU_SOURCE
  23. #ifdef emacs
  24. /* Converts the pointer to the char to BEG-based offset from the start. */
  25. #define PTR_TO_OFFSET(d) \
  26. POS_AS_IN_BUFFER (MATCHING_IN_FIRST_STRING \
  27. ? (d) - string1 : (d) - (string2 - size1))
  28. #define POS_AS_IN_BUFFER(p) ((p) + (NILP (re_match_object) || BUFFERP (re_match_object)))
  29. #else
  30. #define PTR_TO_OFFSET(d) 0
  31. #endif
  32. #include "config.h"
  33. /* We need this for `regex.h', and perhaps for the Emacs include files. */
  34. #include <sys/types.h>
  35. /* This is for other GNU distributions with internationalized messages. */
  36. #if HAVE_LIBINTL_H || defined (_LIBC)
  37. # include <libintl.h>
  38. #else
  39. # define gettext(msgid) (msgid)
  40. #endif
  41. #ifndef gettext_noop
  42. /* This define is so xgettext can find the internationalizable
  43. strings. */
  44. #define gettext_noop(String) String
  45. #endif
  46. /* The `emacs' switch turns on certain matching commands
  47. that make sense only in Emacs. */
  48. #ifdef emacs
  49. #include "lisp.h"
  50. #include "buffer.h"
  51. /* Make syntax table lookup grant data in gl_state. */
  52. #define SYNTAX_ENTRY_VIA_PROPERTY
  53. #include "syntax.h"
  54. #include "charset.h"
  55. #include "category.h"
  56. #define malloc xmalloc
  57. #define realloc xrealloc
  58. #define free xfree
  59. #else /* not emacs */
  60. /* If we are not linking with Emacs proper,
  61. we can't use the relocating allocator
  62. even if config.h says that we can. */
  63. #undef REL_ALLOC
  64. #if defined (STDC_HEADERS) || defined (_LIBC)
  65. #include <stdlib.h>
  66. #else
  67. char *malloc ();
  68. char *realloc ();
  69. #endif
  70. /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
  71. If nothing else has been done, use the method below. */
  72. #ifdef INHIBIT_STRING_HEADER
  73. #if !(defined (HAVE_BZERO) && defined (HAVE_BCOPY))
  74. #if !defined (bzero) && !defined (bcopy)
  75. #undef INHIBIT_STRING_HEADER
  76. #endif
  77. #endif
  78. #endif
  79. /* This is the normal way of making sure we have a bcopy and a bzero.
  80. This is used in most programs--a few other programs avoid this
  81. by defining INHIBIT_STRING_HEADER. */
  82. #ifndef INHIBIT_STRING_HEADER
  83. #if defined (HAVE_STRING_H) || defined (STDC_HEADERS) || defined (_LIBC)
  84. #include <string.h>
  85. #ifndef bcmp
  86. #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
  87. #endif
  88. #ifndef bcopy
  89. #define bcopy(s, d, n) memcpy ((d), (s), (n))
  90. #endif
  91. #ifndef bzero
  92. #define bzero(s, n) memset ((s), 0, (n))
  93. #endif
  94. #else
  95. #include <strings.h>
  96. #endif
  97. #endif
  98. /* Define the syntax stuff for \<, \>, etc. */
  99. /* This must be nonzero for the wordchar and notwordchar pattern
  100. commands in re_match_2. */
  101. #ifndef Sword
  102. #define Sword 1
  103. #endif
  104. #ifdef SWITCH_ENUM_BUG
  105. #define SWITCH_ENUM_CAST(x) ((int)(x))
  106. #else
  107. #define SWITCH_ENUM_CAST(x) (x)
  108. #endif
  109. #ifdef SYNTAX_TABLE
  110. extern char *re_syntax_table;
  111. #else /* not SYNTAX_TABLE */
  112. /* How many characters in the character set. */
  113. #define CHAR_SET_SIZE 256
  114. static char re_syntax_table[CHAR_SET_SIZE];
  115. static void
  116. init_syntax_once ()
  117. {
  118. register int c;
  119. static int done = 0;
  120. if (done)
  121. return;
  122. bzero (re_syntax_table, sizeof re_syntax_table);
  123. for (c = 'a'; c <= 'z'; c++)
  124. re_syntax_table[c] = Sword;
  125. for (c = 'A'; c <= 'Z'; c++)
  126. re_syntax_table[c] = Sword;
  127. for (c = '0'; c <= '9'; c++)
  128. re_syntax_table[c] = Sword;
  129. re_syntax_table['_'] = Sword;
  130. done = 1;
  131. }
  132. #endif /* not SYNTAX_TABLE */
  133. #define SYNTAX(c) re_syntax_table[c]
  134. /* Dummy macros for non-Emacs environments. */
  135. #define BASE_LEADING_CODE_P(c) (0)
  136. #define WORD_BOUNDARY_P(c1, c2) (0)
  137. #define CHAR_HEAD_P(p) (1)
  138. #define SINGLE_BYTE_CHAR_P(c) (1)
  139. #define SAME_CHARSET_P(c1, c2) (1)
  140. #define MULTIBYTE_FORM_LENGTH(p, s) (1)
  141. #define STRING_CHAR(p, s) (*(p))
  142. #define STRING_CHAR_AND_LENGTH(p, s, actual_len) ((actual_len) = 1, *(p))
  143. #define GET_CHAR_AFTER_2(c, p, str1, end1, str2, end2) \
  144. (c = ((p) == (end1) ? *(str2) : *(p)))
  145. #define GET_CHAR_BEFORE_2(c, p, str1, end1, str2, end2) \
  146. (c = ((p) == (str2) ? *((end1) - 1) : *((p) - 1)))
  147. #endif /* not emacs */
  148. /* Get the interface, including the syntax bits. */
  149. #include "regex.h"
  150. /* isalpha etc. are used for the character classes. */
  151. #include <ctype.h>
  152. /* Jim Meyering writes:
  153. "... Some ctype macros are valid only for character codes that
  154. isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  155. using /bin/cc or gcc but without giving an ansi option). So, all
  156. ctype uses should be through macros like ISPRINT... If
  157. STDC_HEADERS is defined, then autoconf has verified that the ctype
  158. macros don't need to be guarded with references to isascii. ...
  159. Defining isascii to 1 should let any compiler worth its salt
  160. eliminate the && through constant folding." */
  161. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  162. #define ISASCII(c) 1
  163. #else
  164. #define ISASCII(c) isascii(c)
  165. #endif
  166. #ifdef isblank
  167. #define ISBLANK(c) (ISASCII (c) && isblank (c))
  168. #else
  169. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  170. #endif
  171. #ifdef isgraph
  172. #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  173. #else
  174. #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  175. #endif
  176. #define ISPRINT(c) (ISASCII (c) && isprint (c))
  177. #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
  178. #define ISALNUM(c) (ISASCII (c) && isalnum (c))
  179. #define ISALPHA(c) (ISASCII (c) && isalpha (c))
  180. #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
  181. #define ISLOWER(c) (ISASCII (c) && islower (c))
  182. #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
  183. #define ISSPACE(c) (ISASCII (c) && isspace (c))
  184. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  185. #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
  186. #ifndef NULL
  187. #define NULL (void *)0
  188. #endif
  189. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  190. since ours (we hope) works properly with all combinations of
  191. machines, compilers, `char' and `unsigned char' argument types.
  192. (Per Bothner suggested the basic approach.) */
  193. #undef SIGN_EXTEND_CHAR
  194. #if __STDC__
  195. #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  196. #else /* not __STDC__ */
  197. /* As in Harbison and Steele. */
  198. #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  199. #endif
  200. /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
  201. use `alloca' instead of `malloc'. This is because using malloc in
  202. re_search* or re_match* could cause memory leaks when C-g is used in
  203. Emacs; also, malloc is slower and causes storage fragmentation. On
  204. the other hand, malloc is more portable, and easier to debug.
  205. Because we sometimes use alloca, some routines have to be macros,
  206. not functions -- `alloca'-allocated space disappears at the end of the
  207. function it is called in. */
  208. #ifdef REGEX_MALLOC
  209. #define REGEX_ALLOCATE malloc
  210. #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  211. #define REGEX_FREE free
  212. #else /* not REGEX_MALLOC */
  213. /* Emacs already defines alloca, sometimes. */
  214. #ifndef alloca
  215. /* Make alloca work the best possible way. */
  216. #ifdef __GNUC__
  217. #define alloca __builtin_alloca
  218. #else /* not __GNUC__ */
  219. #if HAVE_ALLOCA_H
  220. #include <alloca.h>
  221. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  222. #if 0 /* It is a bad idea to declare alloca. We always cast the result. */
  223. #ifndef _AIX /* Already did AIX, up at the top. */
  224. char *alloca ();
  225. #endif /* not _AIX */
  226. #endif
  227. #endif /* not HAVE_ALLOCA_H */
  228. #endif /* not __GNUC__ */
  229. #endif /* not alloca */
  230. #define REGEX_ALLOCATE alloca
  231. /* Assumes a `char *destination' variable. */
  232. #define REGEX_REALLOCATE(source, osize, nsize) \
  233. (destination = (char *) alloca (nsize), \
  234. bcopy (source, destination, osize), \
  235. destination)
  236. /* No need to do anything to free, after alloca. */
  237. #define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
  238. #endif /* not REGEX_MALLOC */
  239. /* Define how to allocate the failure stack. */
  240. #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
  241. #define REGEX_ALLOCATE_STACK(size) \
  242. r_alloc (&failure_stack_ptr, (size))
  243. #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  244. r_re_alloc (&failure_stack_ptr, (nsize))
  245. #define REGEX_FREE_STACK(ptr) \
  246. r_alloc_free (&failure_stack_ptr)
  247. #else /* not using relocating allocator */
  248. #ifdef REGEX_MALLOC
  249. #define REGEX_ALLOCATE_STACK malloc
  250. #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
  251. #define REGEX_FREE_STACK free
  252. #else /* not REGEX_MALLOC */
  253. #define REGEX_ALLOCATE_STACK alloca
  254. #define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  255. REGEX_REALLOCATE (source, osize, nsize)
  256. /* No need to explicitly free anything. */
  257. #define REGEX_FREE_STACK(arg)
  258. #endif /* not REGEX_MALLOC */
  259. #endif /* not using relocating allocator */
  260. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  261. `string1' or just past its end. This works if PTR is NULL, which is
  262. a good thing. */
  263. #define FIRST_STRING_P(ptr) \
  264. (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  265. /* (Re)Allocate N items of type T using malloc, or fail. */
  266. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  267. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  268. #define RETALLOC_IF(addr, n, t) \
  269. if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
  270. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  271. #define BYTEWIDTH 8 /* In bits. */
  272. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  273. #undef MAX
  274. #undef MIN
  275. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  276. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  277. typedef char boolean;
  278. #define false 0
  279. #define true 1
  280. static int re_match_2_internal ();
  281. /* These are the command codes that appear in compiled regular
  282. expressions. Some opcodes are followed by argument bytes. A
  283. command code can specify any interpretation whatsoever for its
  284. arguments. Zero bytes may appear in the compiled regular expression. */
  285. typedef enum
  286. {
  287. no_op = 0,
  288. /* Succeed right away--no more backtracking. */
  289. succeed,
  290. /* Followed by one byte giving n, then by n literal bytes. */
  291. exactn,
  292. /* Matches any (more or less) character. */
  293. anychar,
  294. /* Matches any one char belonging to specified set. First
  295. following byte is number of bitmap bytes. Then come bytes
  296. for a bitmap saying which chars are in. Bits in each byte
  297. are ordered low-bit-first. A character is in the set if its
  298. bit is 1. A character too large to have a bit in the map is
  299. automatically not in the set. */
  300. charset,
  301. /* Same parameters as charset, but match any character that is
  302. not one of those specified. */
  303. charset_not,
  304. /* Start remembering the text that is matched, for storing in a
  305. register. Followed by one byte with the register number, in
  306. the range 0 to one less than the pattern buffer's re_nsub
  307. field. Then followed by one byte with the number of groups
  308. inner to this one. (This last has to be part of the
  309. start_memory only because we need it in the on_failure_jump
  310. of re_match_2.) */
  311. start_memory,
  312. /* Stop remembering the text that is matched and store it in a
  313. memory register. Followed by one byte with the register
  314. number, in the range 0 to one less than `re_nsub' in the
  315. pattern buffer, and one byte with the number of inner groups,
  316. just like `start_memory'. (We need the number of inner
  317. groups here because we don't have any easy way of finding the
  318. corresponding start_memory when we're at a stop_memory.) */
  319. stop_memory,
  320. /* Match a duplicate of something remembered. Followed by one
  321. byte containing the register number. */
  322. duplicate,
  323. /* Fail unless at beginning of line. */
  324. begline,
  325. /* Fail unless at end of line. */
  326. endline,
  327. /* Succeeds if at beginning of buffer (if emacs) or at beginning
  328. of string to be matched (if not). */
  329. begbuf,
  330. /* Analogously, for end of buffer/string. */
  331. endbuf,
  332. /* Followed by two byte relative address to which to jump. */
  333. jump,
  334. /* Same as jump, but marks the end of an alternative. */
  335. jump_past_alt,
  336. /* Followed by two-byte relative address of place to resume at
  337. in case of failure. */
  338. on_failure_jump,
  339. /* Like on_failure_jump, but pushes a placeholder instead of the
  340. current string position when executed. */
  341. on_failure_keep_string_jump,
  342. /* Throw away latest failure point and then jump to following
  343. two-byte relative address. */
  344. pop_failure_jump,
  345. /* Change to pop_failure_jump if know won't have to backtrack to
  346. match; otherwise change to jump. This is used to jump
  347. back to the beginning of a repeat. If what follows this jump
  348. clearly won't match what the repeat does, such that we can be
  349. sure that there is no use backtracking out of repetitions
  350. already matched, then we change it to a pop_failure_jump.
  351. Followed by two-byte address. */
  352. maybe_pop_jump,
  353. /* Jump to following two-byte address, and push a dummy failure
  354. point. This failure point will be thrown away if an attempt
  355. is made to use it for a failure. A `+' construct makes this
  356. before the first repeat. Also used as an intermediary kind
  357. of jump when compiling an alternative. */
  358. dummy_failure_jump,
  359. /* Push a dummy failure point and continue. Used at the end of
  360. alternatives. */
  361. push_dummy_failure,
  362. /* Followed by two-byte relative address and two-byte number n.
  363. After matching N times, jump to the address upon failure. */
  364. succeed_n,
  365. /* Followed by two-byte relative address, and two-byte number n.
  366. Jump to the address N times, then fail. */
  367. jump_n,
  368. /* Set the following two-byte relative address to the
  369. subsequent two-byte number. The address *includes* the two
  370. bytes of number. */
  371. set_number_at,
  372. wordchar, /* Matches any word-constituent character. */
  373. notwordchar, /* Matches any char that is not a word-constituent. */
  374. wordbeg, /* Succeeds if at word beginning. */
  375. wordend, /* Succeeds if at word end. */
  376. wordbound, /* Succeeds if at a word boundary. */
  377. notwordbound /* Succeeds if not at a word boundary. */
  378. #ifdef emacs
  379. ,before_dot, /* Succeeds if before point. */
  380. at_dot, /* Succeeds if at point. */
  381. after_dot, /* Succeeds if after point. */
  382. /* Matches any character whose syntax is specified. Followed by
  383. a byte which contains a syntax code, e.g., Sword. */
  384. syntaxspec,
  385. /* Matches any character whose syntax is not that specified. */
  386. notsyntaxspec,
  387. /* Matches any character whose category-set contains the specified
  388. category. The operator is followed by a byte which contains a
  389. category code (mnemonic ASCII character). */
  390. categoryspec,
  391. /* Matches any character whose category-set does not contain the
  392. specified category. The operator is followed by a byte which
  393. contains the category code (mnemonic ASCII character). */
  394. notcategoryspec
  395. #endif /* emacs */
  396. } re_opcode_t;
  397. /* Common operations on the compiled pattern. */
  398. /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
  399. #define STORE_NUMBER(destination, number) \
  400. do { \
  401. (destination)[0] = (number) & 0377; \
  402. (destination)[1] = (number) >> 8; \
  403. } while (0)
  404. /* Same as STORE_NUMBER, except increment DESTINATION to
  405. the byte after where the number is stored. Therefore, DESTINATION
  406. must be an lvalue. */
  407. #define STORE_NUMBER_AND_INCR(destination, number) \
  408. do { \
  409. STORE_NUMBER (destination, number); \
  410. (destination) += 2; \
  411. } while (0)
  412. /* Put into DESTINATION a number stored in two contiguous bytes starting
  413. at SOURCE. */
  414. #define EXTRACT_NUMBER(destination, source) \
  415. do { \
  416. (destination) = *(source) & 0377; \
  417. (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
  418. } while (0)
  419. #ifdef DEBUG
  420. static void
  421. extract_number (dest, source)
  422. int *dest;
  423. unsigned char *source;
  424. {
  425. int temp = SIGN_EXTEND_CHAR (*(source + 1));
  426. *dest = *source & 0377;
  427. *dest += temp << 8;
  428. }
  429. #ifndef EXTRACT_MACROS /* To debug the macros. */
  430. #undef EXTRACT_NUMBER
  431. #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  432. #endif /* not EXTRACT_MACROS */
  433. #endif /* DEBUG */
  434. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  435. SOURCE must be an lvalue. */
  436. #define EXTRACT_NUMBER_AND_INCR(destination, source) \
  437. do { \
  438. EXTRACT_NUMBER (destination, source); \
  439. (source) += 2; \
  440. } while (0)
  441. #ifdef DEBUG
  442. static void
  443. extract_number_and_incr (destination, source)
  444. int *destination;
  445. unsigned char **source;
  446. {
  447. extract_number (destination, *source);
  448. *source += 2;
  449. }
  450. #ifndef EXTRACT_MACROS
  451. #undef EXTRACT_NUMBER_AND_INCR
  452. #define EXTRACT_NUMBER_AND_INCR(dest, src) \
  453. extract_number_and_incr (&dest, &src)
  454. #endif /* not EXTRACT_MACROS */
  455. #endif /* DEBUG */
  456. /* Store a multibyte character in three contiguous bytes starting
  457. DESTINATION, and increment DESTINATION to the byte after where the
  458. character is stored. Therefore, DESTINATION must be an lvalue. */
  459. #define STORE_CHARACTER_AND_INCR(destination, character) \
  460. do { \
  461. (destination)[0] = (character) & 0377; \
  462. (destination)[1] = ((character) >> 8) & 0377; \
  463. (destination)[2] = (character) >> 16; \
  464. (destination) += 3; \
  465. } while (0)
  466. /* Put into DESTINATION a character stored in three contiguous bytes
  467. starting at SOURCE. */
  468. #define EXTRACT_CHARACTER(destination, source) \
  469. do { \
  470. (destination) = ((source)[0] \
  471. | ((source)[1] << 8) \
  472. | ((source)[2] << 16)); \
  473. } while (0)
  474. /* Macros for charset. */
  475. /* Size of bitmap of charset P in bytes. P is a start of charset,
  476. i.e. *P is (re_opcode_t) charset or (re_opcode_t) charset_not. */
  477. #define CHARSET_BITMAP_SIZE(p) ((p)[1] & 0x7F)
  478. /* Nonzero if charset P has range table. */
  479. #define CHARSET_RANGE_TABLE_EXISTS_P(p) ((p)[1] & 0x80)
  480. /* Return the address of range table of charset P. But not the start
  481. of table itself, but the before where the number of ranges is
  482. stored. `2 +' means to skip re_opcode_t and size of bitmap. */
  483. #define CHARSET_RANGE_TABLE(p) (&(p)[2 + CHARSET_BITMAP_SIZE (p)])
  484. /* Test if C is listed in the bitmap of charset P. */
  485. #define CHARSET_LOOKUP_BITMAP(p, c) \
  486. ((c) < CHARSET_BITMAP_SIZE (p) * BYTEWIDTH \
  487. && (p)[2 + (c) / BYTEWIDTH] & (1 << ((c) % BYTEWIDTH)))
  488. /* Return the address of end of RANGE_TABLE. COUNT is number of
  489. ranges (which is a pair of (start, end)) in the RANGE_TABLE. `* 2'
  490. is start of range and end of range. `* 3' is size of each start
  491. and end. */
  492. #define CHARSET_RANGE_TABLE_END(range_table, count) \
  493. ((range_table) + (count) * 2 * 3)
  494. /* Test if C is in RANGE_TABLE. A flag NOT is negated if C is in.
  495. COUNT is number of ranges in RANGE_TABLE. */
  496. #define CHARSET_LOOKUP_RANGE_TABLE_RAW(not, c, range_table, count) \
  497. do \
  498. { \
  499. int range_start, range_end; \
  500. unsigned char *p; \
  501. unsigned char *range_table_end \
  502. = CHARSET_RANGE_TABLE_END ((range_table), (count)); \
  503. \
  504. for (p = (range_table); p < range_table_end; p += 2 * 3) \
  505. { \
  506. EXTRACT_CHARACTER (range_start, p); \
  507. EXTRACT_CHARACTER (range_end, p + 3); \
  508. \
  509. if (range_start <= (c) && (c) <= range_end) \
  510. { \
  511. (not) = !(not); \
  512. break; \
  513. } \
  514. } \
  515. } \
  516. while (0)
  517. /* Test if C is in range table of CHARSET. The flag NOT is negated if
  518. C is listed in it. */
  519. #define CHARSET_LOOKUP_RANGE_TABLE(not, c, charset) \
  520. do \
  521. { \
  522. /* Number of ranges in range table. */ \
  523. int count; \
  524. unsigned char *range_table = CHARSET_RANGE_TABLE (charset); \
  525. \
  526. EXTRACT_NUMBER_AND_INCR (count, range_table); \
  527. CHARSET_LOOKUP_RANGE_TABLE_RAW ((not), (c), range_table, count); \
  528. } \
  529. while (0)
  530. /* If DEBUG is defined, Regex prints many voluminous messages about what
  531. it is doing (if the variable `debug' is nonzero). If linked with the
  532. main program in `iregex.c', you can enter patterns and strings
  533. interactively. And if linked with the main program in `main.c' and
  534. the other test files, you can run the already-written tests. */
  535. #ifdef DEBUG
  536. /* We use standard I/O for debugging. */
  537. #include <stdio.h>
  538. /* It is useful to test things that ``must'' be true when debugging. */
  539. #include <assert.h>
  540. static int debug = 0;
  541. #define DEBUG_STATEMENT(e) e
  542. #define DEBUG_PRINT1(x) if (debug) printf (x)
  543. #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  544. #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  545. #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  546. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
  547. if (debug) print_partial_compiled_pattern (s, e)
  548. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
  549. if (debug) print_double_string (w, s1, sz1, s2, sz2)
  550. /* Print the fastmap in human-readable form. */
  551. void
  552. print_fastmap (fastmap)
  553. char *fastmap;
  554. {
  555. unsigned was_a_range = 0;
  556. unsigned i = 0;
  557. while (i < (1 << BYTEWIDTH))
  558. {
  559. if (fastmap[i++])
  560. {
  561. was_a_range = 0;
  562. putchar (i - 1);
  563. while (i < (1 << BYTEWIDTH) && fastmap[i])
  564. {
  565. was_a_range = 1;
  566. i++;
  567. }
  568. if (was_a_range)
  569. {
  570. printf ("-");
  571. putchar (i - 1);
  572. }
  573. }
  574. }
  575. putchar ('\n');
  576. }
  577. /* Print a compiled pattern string in human-readable form, starting at
  578. the START pointer into it and ending just before the pointer END. */
  579. void
  580. print_partial_compiled_pattern (start, end)
  581. unsigned char *start;
  582. unsigned char *end;
  583. {
  584. int mcnt, mcnt2;
  585. unsigned char *p = start;
  586. unsigned char *pend = end;
  587. if (start == NULL)
  588. {
  589. printf ("(null)\n");
  590. return;
  591. }
  592. /* Loop over pattern commands. */
  593. while (p < pend)
  594. {
  595. printf ("%d:\t", p - start);
  596. switch ((re_opcode_t) *p++)
  597. {
  598. case no_op:
  599. printf ("/no_op");
  600. break;
  601. case exactn:
  602. mcnt = *p++;
  603. printf ("/exactn/%d", mcnt);
  604. do
  605. {
  606. putchar ('/');
  607. putchar (*p++);
  608. }
  609. while (--mcnt);
  610. break;
  611. case start_memory:
  612. mcnt = *p++;
  613. printf ("/start_memory/%d/%d", mcnt, *p++);
  614. break;
  615. case stop_memory:
  616. mcnt = *p++;
  617. printf ("/stop_memory/%d/%d", mcnt, *p++);
  618. break;
  619. case duplicate:
  620. printf ("/duplicate/%d", *p++);
  621. break;
  622. case anychar:
  623. printf ("/anychar");
  624. break;
  625. case charset:
  626. case charset_not:
  627. {
  628. register int c, last = -100;
  629. register int in_range = 0;
  630. printf ("/charset [%s",
  631. (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  632. assert (p + *p < pend);
  633. for (c = 0; c < 256; c++)
  634. if (c / 8 < *p
  635. && (p[1 + (c/8)] & (1 << (c % 8))))
  636. {
  637. /* Are we starting a range? */
  638. if (last + 1 == c && ! in_range)
  639. {
  640. putchar ('-');
  641. in_range = 1;
  642. }
  643. /* Have we broken a range? */
  644. else if (last + 1 != c && in_range)
  645. {
  646. putchar (last);
  647. in_range = 0;
  648. }
  649. if (! in_range)
  650. putchar (c);
  651. last = c;
  652. }
  653. if (in_range)
  654. putchar (last);
  655. putchar (']');
  656. p += 1 + *p;
  657. }
  658. break;
  659. case begline:
  660. printf ("/begline");
  661. break;
  662. case endline:
  663. printf ("/endline");
  664. break;
  665. case on_failure_jump:
  666. extract_number_and_incr (&mcnt, &p);
  667. printf ("/on_failure_jump to %d", p + mcnt - start);
  668. break;
  669. case on_failure_keep_string_jump:
  670. extract_number_and_incr (&mcnt, &p);
  671. printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
  672. break;
  673. case dummy_failure_jump:
  674. extract_number_and_incr (&mcnt, &p);
  675. printf ("/dummy_failure_jump to %d", p + mcnt - start);
  676. break;
  677. case push_dummy_failure:
  678. printf ("/push_dummy_failure");
  679. break;
  680. case maybe_pop_jump:
  681. extract_number_and_incr (&mcnt, &p);
  682. printf ("/maybe_pop_jump to %d", p + mcnt - start);
  683. break;
  684. case pop_failure_jump:
  685. extract_number_and_incr (&mcnt, &p);
  686. printf ("/pop_failure_jump to %d", p + mcnt - start);
  687. break;
  688. case jump_past_alt:
  689. extract_number_and_incr (&mcnt, &p);
  690. printf ("/jump_past_alt to %d", p + mcnt - start);
  691. break;
  692. case jump:
  693. extract_number_and_incr (&mcnt, &p);
  694. printf ("/jump to %d", p + mcnt - start);
  695. break;
  696. case succeed_n:
  697. extract_number_and_incr (&mcnt, &p);
  698. extract_number_and_incr (&mcnt2, &p);
  699. printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
  700. break;
  701. case jump_n:
  702. extract_number_and_incr (&mcnt, &p);
  703. extract_number_and_incr (&mcnt2, &p);
  704. printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
  705. break;
  706. case set_number_at:
  707. extract_number_and_incr (&mcnt, &p);
  708. extract_number_and_incr (&mcnt2, &p);
  709. printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
  710. break;
  711. case wordbound:
  712. printf ("/wordbound");
  713. break;
  714. case notwordbound:
  715. printf ("/notwordbound");
  716. break;
  717. case wordbeg:
  718. printf ("/wordbeg");
  719. break;
  720. case wordend:
  721. printf ("/wordend");
  722. #ifdef emacs
  723. case before_dot:
  724. printf ("/before_dot");
  725. break;
  726. case at_dot:
  727. printf ("/at_dot");
  728. break;
  729. case after_dot:
  730. printf ("/after_dot");
  731. break;
  732. case syntaxspec:
  733. printf ("/syntaxspec");
  734. mcnt = *p++;
  735. printf ("/%d", mcnt);
  736. break;
  737. case notsyntaxspec:
  738. printf ("/notsyntaxspec");
  739. mcnt = *p++;
  740. printf ("/%d", mcnt);
  741. break;
  742. #endif /* emacs */
  743. case wordchar:
  744. printf ("/wordchar");
  745. break;
  746. case notwordchar:
  747. printf ("/notwordchar");
  748. break;
  749. case begbuf:
  750. printf ("/begbuf");
  751. break;
  752. case endbuf:
  753. printf ("/endbuf");
  754. break;
  755. default:
  756. printf ("?%d", *(p-1));
  757. }
  758. putchar ('\n');
  759. }
  760. printf ("%d:\tend of pattern.\n", p - start);
  761. }
  762. void
  763. print_compiled_pattern (bufp)
  764. struct re_pattern_buffer *bufp;
  765. {
  766. unsigned char *buffer = bufp->buffer;
  767. print_partial_compiled_pattern (buffer, buffer + bufp->used);
  768. printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
  769. if (bufp->fastmap_accurate && bufp->fastmap)
  770. {
  771. printf ("fastmap: ");
  772. print_fastmap (bufp->fastmap);
  773. }
  774. printf ("re_nsub: %d\t", bufp->re_nsub);
  775. printf ("regs_alloc: %d\t", bufp->regs_allocated);
  776. printf ("can_be_null: %d\t", bufp->can_be_null);
  777. printf ("newline_anchor: %d\n", bufp->newline_anchor);
  778. printf ("no_sub: %d\t", bufp->no_sub);
  779. printf ("not_bol: %d\t", bufp->not_bol);
  780. printf ("not_eol: %d\t", bufp->not_eol);
  781. printf ("syntax: %d\n", bufp->syntax);
  782. /* Perhaps we should print the translate table? */
  783. }
  784. void
  785. print_double_string (where, string1, size1, string2, size2)
  786. const char *where;
  787. const char *string1;
  788. const char *string2;
  789. int size1;
  790. int size2;
  791. {
  792. unsigned this_char;
  793. if (where == NULL)
  794. printf ("(null)");
  795. else
  796. {
  797. if (FIRST_STRING_P (where))
  798. {
  799. for (this_char = where - string1; this_char < size1; this_char++)
  800. putchar (string1[this_char]);
  801. where = string2;
  802. }
  803. for (this_char = where - string2; this_char < size2; this_char++)
  804. putchar (string2[this_char]);
  805. }
  806. }
  807. #else /* not DEBUG */
  808. #undef assert
  809. #define assert(e)
  810. #define DEBUG_STATEMENT(e)
  811. #define DEBUG_PRINT1(x)
  812. #define DEBUG_PRINT2(x1, x2)
  813. #define DEBUG_PRINT3(x1, x2, x3)
  814. #define DEBUG_PRINT4(x1, x2, x3, x4)
  815. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  816. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  817. #endif /* not DEBUG */
  818. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  819. also be assigned to arbitrarily: each pattern buffer stores its own
  820. syntax, so it can be changed between regex compilations. */
  821. /* This has no initializer because initialized variables in Emacs
  822. become read-only after dumping. */
  823. reg_syntax_t re_syntax_options;
  824. /* Specify the precise syntax of regexps for compilation. This provides
  825. for compatibility for various utilities which historically have
  826. different, incompatible syntaxes.
  827. The argument SYNTAX is a bit mask comprised of the various bits
  828. defined in regex.h. We return the old syntax. */
  829. reg_syntax_t
  830. re_set_syntax (syntax)
  831. reg_syntax_t syntax;
  832. {
  833. reg_syntax_t ret = re_syntax_options;
  834. re_syntax_options = syntax;
  835. return ret;
  836. }
  837. /* This table gives an error message for each of the error codes listed
  838. in regex.h. Obviously the order here has to be same as there.
  839. POSIX doesn't require that we do anything for REG_NOERROR,
  840. but why not be nice? */
  841. static const char *re_error_msgid[] =
  842. {
  843. gettext_noop ("Success"), /* REG_NOERROR */
  844. gettext_noop ("No match"), /* REG_NOMATCH */
  845. gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
  846. gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
  847. gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
  848. gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
  849. gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
  850. gettext_noop ("Unmatched [ or [^"), /* REG_EBRACK */
  851. gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
  852. gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
  853. gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
  854. gettext_noop ("Invalid range end"), /* REG_ERANGE */
  855. gettext_noop ("Memory exhausted"), /* REG_ESPACE */
  856. gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
  857. gettext_noop ("Premature end of regular expression"), /* REG_EEND */
  858. gettext_noop ("Regular expression too big"), /* REG_ESIZE */
  859. gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
  860. };
  861. /* Avoiding alloca during matching, to placate r_alloc. */
  862. /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
  863. searching and matching functions should not call alloca. On some
  864. systems, alloca is implemented in terms of malloc, and if we're
  865. using the relocating allocator routines, then malloc could cause a
  866. relocation, which might (if the strings being searched are in the
  867. ralloc heap) shift the data out from underneath the regexp
  868. routines.
  869. Here's another reason to avoid allocation: Emacs
  870. processes input from X in a signal handler; processing X input may
  871. call malloc; if input arrives while a matching routine is calling
  872. malloc, then we're scrod. But Emacs can't just block input while
  873. calling matching routines; then we don't notice interrupts when
  874. they come in. So, Emacs blocks input around all regexp calls
  875. except the matching calls, which it leaves unprotected, in the
  876. faith that they will not malloc. */
  877. /* Normally, this is fine. */
  878. #define MATCH_MAY_ALLOCATE
  879. /* When using GNU C, we are not REALLY using the C alloca, no matter
  880. what config.h may say. So don't take precautions for it. */
  881. #ifdef __GNUC__
  882. #undef C_ALLOCA
  883. #endif
  884. /* The match routines may not allocate if (1) they would do it with malloc
  885. and (2) it's not safe for them to use malloc.
  886. Note that if REL_ALLOC is defined, matching would not use malloc for the
  887. failure stack, but we would still use it for the register vectors;
  888. so REL_ALLOC should not affect this. */
  889. #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
  890. #undef MATCH_MAY_ALLOCATE
  891. #endif
  892. /* Failure stack declarations and macros; both re_compile_fastmap and
  893. re_match_2 use a failure stack. These have to be macros because of
  894. REGEX_ALLOCATE_STACK. */
  895. /* Approximate number of failure points for which to initially allocate space
  896. when matching. If this number is exceeded, we allocate more
  897. space, so it is not a hard limit. */
  898. #ifndef INIT_FAILURE_ALLOC
  899. #define INIT_FAILURE_ALLOC 20
  900. #endif
  901. /* Roughly the maximum number of failure points on the stack. Would be
  902. exactly that if always used TYPICAL_FAILURE_SIZE items each time we failed.
  903. This is a variable only so users of regex can assign to it; we never
  904. change it ourselves. */
  905. #if defined (MATCH_MAY_ALLOCATE)
  906. /* Note that 4400 is enough to cause a crash on Alpha OSF/1,
  907. whose default stack limit is 2mb. In order for a larger
  908. value to work reliably, you have to try to make it accord
  909. with the process stack limit. */
  910. int re_max_failures = 40000;
  911. #else
  912. int re_max_failures = 4000;
  913. #endif
  914. union fail_stack_elt
  915. {
  916. unsigned char *pointer;
  917. int integer;
  918. };
  919. typedef union fail_stack_elt fail_stack_elt_t;
  920. typedef struct
  921. {
  922. fail_stack_elt_t *stack;
  923. unsigned size;
  924. unsigned avail; /* Offset of next open position. */
  925. } fail_stack_type;
  926. #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
  927. #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  928. #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
  929. /* Define macros to initialize and free the failure stack.
  930. Do `return -2' if the alloc fails. */
  931. #ifdef MATCH_MAY_ALLOCATE
  932. #define INIT_FAIL_STACK() \
  933. do { \
  934. fail_stack.stack = (fail_stack_elt_t *) \
  935. REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * TYPICAL_FAILURE_SIZE \
  936. * sizeof (fail_stack_elt_t)); \
  937. \
  938. if (fail_stack.stack == NULL) \
  939. return -2; \
  940. \
  941. fail_stack.size = INIT_FAILURE_ALLOC; \
  942. fail_stack.avail = 0; \
  943. } while (0)
  944. #define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
  945. #else
  946. #define INIT_FAIL_STACK() \
  947. do { \
  948. fail_stack.avail = 0; \
  949. } while (0)
  950. #define RESET_FAIL_STACK()
  951. #endif
  952. /* Double the size of FAIL_STACK, up to a limit
  953. which allows approximately `re_max_failures' items.
  954. Return 1 if succeeds, and 0 if either ran out of memory
  955. allocating space for it or it was already too large.
  956. REGEX_REALLOCATE_STACK requires `destination' be declared. */
  957. /* Factor to increase the failure stack size by
  958. when we increase it.
  959. This used to be 2, but 2 was too wasteful
  960. because the old discarded stacks added up to as much space
  961. were as ultimate, maximum-size stack. */
  962. #define FAIL_STACK_GROWTH_FACTOR 4
  963. #define GROW_FAIL_STACK(fail_stack) \
  964. (((fail_stack).size * sizeof (fail_stack_elt_t) \
  965. >= re_max_failures * TYPICAL_FAILURE_SIZE) \
  966. ? 0 \
  967. : ((fail_stack).stack \
  968. = (fail_stack_elt_t *) \
  969. REGEX_REALLOCATE_STACK ((fail_stack).stack, \
  970. (fail_stack).size * sizeof (fail_stack_elt_t), \
  971. MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
  972. ((fail_stack).size * sizeof (fail_stack_elt_t) \
  973. * FAIL_STACK_GROWTH_FACTOR))), \
  974. \
  975. (fail_stack).stack == NULL \
  976. ? 0 \
  977. : ((fail_stack).size \
  978. = (MIN (re_max_failures * TYPICAL_FAILURE_SIZE, \
  979. ((fail_stack).size * sizeof (fail_stack_elt_t) \
  980. * FAIL_STACK_GROWTH_FACTOR)) \
  981. / sizeof (fail_stack_elt_t)), \
  982. 1)))
  983. /* Push pointer POINTER on FAIL_STACK.
  984. Return 1 if was able to do so and 0 if ran out of memory allocating
  985. space to do so. */
  986. #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
  987. ((FAIL_STACK_FULL () \
  988. && !GROW_FAIL_STACK (FAIL_STACK)) \
  989. ? 0 \
  990. : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
  991. 1))
  992. /* Push a pointer value onto the failure stack.
  993. Assumes the variable `fail_stack'. Probably should only
  994. be called from within `PUSH_FAILURE_POINT'. */
  995. #define PUSH_FAILURE_POINTER(item) \
  996. fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
  997. /* This pushes an integer-valued item onto the failure stack.
  998. Assumes the variable `fail_stack'. Probably should only
  999. be called from within `PUSH_FAILURE_POINT'. */
  1000. #define PUSH_FAILURE_INT(item) \
  1001. fail_stack.stack[fail_stack.avail++].integer = (item)
  1002. /* Push a fail_stack_elt_t value onto the failure stack.
  1003. Assumes the variable `fail_stack'. Probably should only
  1004. be called from within `PUSH_FAILURE_POINT'. */
  1005. #define PUSH_FAILURE_ELT(item) \
  1006. fail_stack.stack[fail_stack.avail++] = (item)
  1007. /* These three POP... operations complement the three PUSH... operations.
  1008. All assume that `fail_stack' is nonempty. */
  1009. #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
  1010. #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
  1011. #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
  1012. /* Used to omit pushing failure point id's when we're not debugging. */
  1013. #ifdef DEBUG
  1014. #define DEBUG_PUSH PUSH_FAILURE_INT
  1015. #define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
  1016. #else
  1017. #define DEBUG_PUSH(item)
  1018. #define DEBUG_POP(item_addr)
  1019. #endif
  1020. /* Push the information about the state we will need
  1021. if we ever fail back to it.
  1022. Requires variables fail_stack, regstart, regend, reg_info, and
  1023. num_regs be declared. GROW_FAIL_STACK requires `destination' be
  1024. declared.
  1025. Does `return FAILURE_CODE' if runs out of memory. */
  1026. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
  1027. do { \
  1028. char *destination; \
  1029. /* Must be int, so when we don't save any registers, the arithmetic \
  1030. of 0 + -1 isn't done as unsigned. */ \
  1031. int this_reg; \
  1032. \
  1033. DEBUG_STATEMENT (failure_id++); \
  1034. DEBUG_STATEMENT (nfailure_points_pushed++); \
  1035. DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
  1036. DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
  1037. DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
  1038. \
  1039. DEBUG_PRINT2 (" slots needed: %d\n", NUM_FAILURE_ITEMS); \
  1040. DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
  1041. \
  1042. /* Ensure we have enough space allocated for what we will push. */ \
  1043. while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
  1044. { \
  1045. if (!GROW_FAIL_STACK (fail_stack)) \
  1046. return failure_code; \
  1047. \
  1048. DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
  1049. (fail_stack).size); \
  1050. DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  1051. } \
  1052. \
  1053. /* Push the info, starting with the registers. */ \
  1054. DEBUG_PRINT1 ("\n"); \
  1055. \
  1056. if (1) \
  1057. for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
  1058. this_reg++) \
  1059. { \
  1060. DEBUG_PRINT2 (" Pushing reg: %d\n", this_reg); \
  1061. DEBUG_STATEMENT (num_regs_pushed++); \
  1062. \
  1063. DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
  1064. PUSH_FAILURE_POINTER (regstart[this_reg]); \
  1065. \
  1066. DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
  1067. PUSH_FAILURE_POINTER (regend[this_reg]); \
  1068. \
  1069. DEBUG_PRINT2 (" info: 0x%x\n ", reg_info[this_reg]); \
  1070. DEBUG_PRINT2 (" match_null=%d", \
  1071. REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
  1072. DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
  1073. DEBUG_PRINT2 (" matched_something=%d", \
  1074. MATCHED_SOMETHING (reg_info[this_reg])); \
  1075. DEBUG_PRINT2 (" ever_matched=%d", \
  1076. EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
  1077. DEBUG_PRINT1 ("\n"); \
  1078. PUSH_FAILURE_ELT (reg_info[this_reg].word); \
  1079. } \
  1080. \
  1081. DEBUG_PRINT2 (" Pushing low active reg: %d\n", lowest_active_reg);\
  1082. PUSH_FAILURE_INT (lowest_active_reg); \
  1083. \
  1084. DEBUG_PRINT2 (" Pushing high active reg: %d\n", highest_active_reg);\
  1085. PUSH_FAILURE_INT (highest_active_reg); \
  1086. \
  1087. DEBUG_PRINT2 (" Pushing pattern 0x%x: ", pattern_place); \
  1088. DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
  1089. PUSH_FAILURE_POINTER (pattern_place); \
  1090. \
  1091. DEBUG_PRINT2 (" Pushing string 0x%x: `", string_place); \
  1092. DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
  1093. size2); \
  1094. DEBUG_PRINT1 ("'\n"); \
  1095. PUSH_FAILURE_POINTER (string_place); \
  1096. \
  1097. DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
  1098. DEBUG_PUSH (failure_id); \
  1099. } while (0)
  1100. /* This is the number of items that are pushed and popped on the stack
  1101. for each register. */
  1102. #define NUM_REG_ITEMS 3
  1103. /* Individual items aside from the registers. */
  1104. #ifdef DEBUG
  1105. #define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
  1106. #else
  1107. #define NUM_NONREG_ITEMS 4
  1108. #endif
  1109. /* Estimate the size of data pushed by a typical failure stack entry.
  1110. An estimate is all we need, because all we use this for
  1111. is to choose a limit for how big to make the failure stack. */
  1112. #define TYPICAL_FAILURE_SIZE 20
  1113. /* This is how many items we actually use for a failure point.
  1114. It depends on the regexp. */
  1115. #define NUM_FAILURE_ITEMS \
  1116. (((0 \
  1117. ? 0 : highest_active_reg - lowest_active_reg + 1) \
  1118. * NUM_REG_ITEMS) \
  1119. + NUM_NONREG_ITEMS)
  1120. /* How many items can still be added to the stack without overflowing it. */
  1121. #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  1122. /* Pops what PUSH_FAIL_STACK pushes.
  1123. We restore into the parameters, all of which should be lvalues:
  1124. STR -- the saved data position.
  1125. PAT -- the saved pattern position.
  1126. LOW_REG, HIGH_REG -- the highest and lowest active registers.
  1127. REGSTART, REGEND -- arrays of string positions.
  1128. REG_INFO -- array of information about each subexpression.
  1129. Also assumes the variables `fail_stack' and (if debugging), `bufp',
  1130. `pend', `string1', `size1', `string2', and `size2'. */
  1131. #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  1132. { \
  1133. DEBUG_STATEMENT (fail_stack_elt_t failure_id;) \
  1134. int this_reg; \
  1135. const unsigned char *string_temp; \
  1136. \
  1137. assert (!FAIL_STACK_EMPTY ()); \
  1138. \
  1139. /* Remove failure points and point to how many regs pushed. */ \
  1140. DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
  1141. DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
  1142. DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
  1143. \
  1144. assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
  1145. \
  1146. DEBUG_POP (&failure_id); \
  1147. DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
  1148. \
  1149. /* If the saved string location is NULL, it came from an \
  1150. on_failure_keep_string_jump opcode, and we want to throw away the \
  1151. saved NULL, thus retaining our current position in the string. */ \
  1152. string_temp = POP_FAILURE_POINTER (); \
  1153. if (string_temp != NULL) \
  1154. str = (const char *) string_temp; \
  1155. \
  1156. DEBUG_PRINT2 (" Popping string 0x%x: `", str); \
  1157. DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
  1158. DEBUG_PRINT1 ("'\n"); \
  1159. \
  1160. pat = (unsigned char *) POP_FAILURE_POINTER (); \
  1161. DEBUG_PRINT2 (" Popping pattern 0x%x: ", pat); \
  1162. DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
  1163. \
  1164. /* Restore register info. */ \
  1165. high_reg = (unsigned) POP_FAILURE_INT (); \
  1166. DEBUG_PRINT2 (" Popping high active reg: %d\n", high_reg); \
  1167. \
  1168. low_reg = (unsigned) POP_FAILURE_INT (); \
  1169. DEBUG_PRINT2 (" Popping low active reg: %d\n", low_reg); \
  1170. \
  1171. if (1) \
  1172. for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
  1173. { \
  1174. DEBUG_PRINT2 (" Popping reg: %d\n", this_reg); \
  1175. \
  1176. reg_info[this_reg].word = POP_FAILURE_ELT (); \
  1177. DEBUG_PRINT2 (" info: 0x%x\n", reg_info[this_reg]); \
  1178. \
  1179. regend[this_reg] = (const char *) POP_FAILURE_POINTER (); \
  1180. DEBUG_PRINT2 (" end: 0x%x\n", regend[this_reg]); \
  1181. \
  1182. regstart[this_reg] = (const char *) POP_FAILURE_POINTER (); \
  1183. DEBUG_PRINT2 (" start: 0x%x\n", regstart[this_reg]); \
  1184. } \
  1185. else \
  1186. { \
  1187. for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
  1188. { \
  1189. reg_info[this_reg].word.integer = 0; \
  1190. regend[this_reg] = 0; \
  1191. regstart[this_reg] = 0; \
  1192. } \
  1193. highest_active_reg = high_reg; \
  1194. } \
  1195. \
  1196. set_regs_matched_done = 0; \
  1197. DEBUG_STATEMENT (nfailure_points_popped++); \
  1198. } /* POP_FAILURE_POINT */
  1199. /* Structure for per-register (a.k.a. per-group) information.
  1200. Other register information, such as the
  1201. starting and ending positions (which are addresses), and the list of
  1202. inner groups (which is a bits list) are maintained in separate
  1203. variables.
  1204. We are making a (strictly speaking) nonportable assumption here: that
  1205. the compiler will pack our bit fields into something that fits into
  1206. the type of `word', i.e., is something that fits into one item on the
  1207. failure stack. */
  1208. typedef union
  1209. {
  1210. fail_stack_elt_t word;
  1211. struct
  1212. {
  1213. /* This field is one if this group can match the empty string,
  1214. zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
  1215. #define MATCH_NULL_UNSET_VALUE 3
  1216. unsigned match_null_string_p : 2;
  1217. unsigned is_active : 1;
  1218. unsigned matched_something : 1;
  1219. unsigned ever_matched_something : 1;
  1220. } bits;
  1221. } register_info_type;
  1222. #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
  1223. #define IS_ACTIVE(R) ((R).bits.is_active)
  1224. #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
  1225. #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
  1226. /* Call this when have matched a real character; it sets `matched' flags
  1227. for the subexpressions which we are currently inside. Also records
  1228. that those subexprs have matched. */
  1229. #define SET_REGS_MATCHED() \
  1230. do \
  1231. { \
  1232. if (!set_regs_matched_done) \
  1233. { \
  1234. unsigned r; \
  1235. set_regs_matched_done = 1; \
  1236. for (r = lowest_active_reg; r <= highest_active_reg; r++) \
  1237. { \
  1238. MATCHED_SOMETHING (reg_info[r]) \
  1239. = EVER_MATCHED_SOMETHING (reg_info[r]) \
  1240. = 1; \
  1241. } \
  1242. } \
  1243. } \
  1244. while (0)
  1245. /* Registers are set to a sentinel when they haven't yet matched. */
  1246. static char reg_unset_dummy;
  1247. #define REG_UNSET_VALUE (&reg_unset_dummy)
  1248. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  1249. /* Subroutine declarations and macros for regex_compile. */
  1250. static void store_op1 (), store_op2 ();
  1251. static void insert_op1 (), insert_op2 ();
  1252. static boolean at_begline_loc_p (), at_endline_loc_p ();
  1253. static boolean group_in_compile_stack ();
  1254. static reg_errcode_t compile_range ();
  1255. /* Fetch the next character in the uncompiled pattern---translating it
  1256. if necessary. Also cast from a signed character in the constant
  1257. string passed to us by the user to an unsigned char that we can use
  1258. as an array index (in, e.g., `translate'). */
  1259. #ifndef PATFETCH
  1260. #define PATFETCH(c) \
  1261. do {if (p == pend) return REG_EEND; \
  1262. c = (unsigned char) *p++; \
  1263. if (RE_TRANSLATE_P (translate)) c = RE_TRANSLATE (translate, c); \
  1264. } while (0)
  1265. #endif
  1266. /* Fetch the next character in the uncompiled pattern, with no
  1267. translation. */
  1268. #define PATFETCH_RAW(c) \
  1269. do {if (p == pend) return REG_EEND; \
  1270. c = (unsigned char) *p++; \
  1271. } while (0)
  1272. /* Go backwards one character in the pattern. */
  1273. #define PATUNFETCH p--
  1274. /* If `translate' is non-null, return translate[D], else just D. We
  1275. cast the subscript to translate because some data is declared as
  1276. `char *', to avoid warnings when a string constant is passed. But
  1277. when we use a character as a subscript we must make it unsigned. */
  1278. #ifndef TRANSLATE
  1279. #define TRANSLATE(d) \
  1280. (RE_TRANSLATE_P (translate) \
  1281. ? (unsigned) RE_TRANSLATE (translate, (unsigned) (d)) : (d))
  1282. #endif
  1283. /* Macros for outputting the compiled pattern into `buffer'. */
  1284. /* If the buffer isn't allocated when it comes in, use this. */
  1285. #define INIT_BUF_SIZE 32
  1286. /* Make sure we have at least N more bytes of space in buffer. */
  1287. #define GET_BUFFER_SPACE(n) \
  1288. while (b - bufp->buffer + (n) > bufp->allocated) \
  1289. EXTEND_BUFFER ()
  1290. /* Make sure we have one more byte of buffer space and then add C to it. */
  1291. #define BUF_PUSH(c) \
  1292. do { \
  1293. GET_BUFFER_SPACE (1); \
  1294. *b++ = (unsigned char) (c); \
  1295. } while (0)
  1296. /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
  1297. #define BUF_PUSH_2(c1, c2) \
  1298. do { \
  1299. GET_BUFFER_SPACE (2); \
  1300. *b++ = (unsigned char) (c1); \
  1301. *b++ = (unsigned char) (c2); \
  1302. } while (0)
  1303. /* As with BUF_PUSH_2, except for three bytes. */
  1304. #define BUF_PUSH_3(c1, c2, c3) \
  1305. do { \
  1306. GET_BUFFER_SPACE (3); \
  1307. *b++ = (unsigned char) (c1); \
  1308. *b++ = (unsigned char) (c2); \
  1309. *b++ = (unsigned char) (c3); \
  1310. } while (0)
  1311. /* Store a jump with opcode OP at LOC to location TO. We store a
  1312. relative address offset by the three bytes the jump itself occupies. */
  1313. #define STORE_JUMP(op, loc, to) \
  1314. store_op1 (op, loc, (to) - (loc) - 3)
  1315. /* Likewise, for a two-argument jump. */
  1316. #define STORE_JUMP2(op, loc, to, arg) \
  1317. store_op2 (op, loc, (to) - (loc) - 3, arg)
  1318. /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
  1319. #define INSERT_JUMP(op, loc, to) \
  1320. insert_op1 (op, loc, (to) - (loc) - 3, b)
  1321. /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
  1322. #define INSERT_JUMP2(op, loc, to, arg) \
  1323. insert_op2 (op, loc, (to) - (loc) - 3, arg, b)
  1324. /* This is not an arbitrary limit: the arguments which represent offsets
  1325. into the pattern are two bytes long. So if 2^16 bytes turns out to
  1326. be too small, many things would have to change. */
  1327. #define MAX_BUF_SIZE (1L << 16)
  1328. /* Extend the buffer by twice its current size via realloc and
  1329. reset the pointers that pointed into the old block to point to the
  1330. correct places in the new one. If extending the buffer results in it
  1331. being larger than MAX_BUF_SIZE, then flag memory exhausted. */
  1332. #define EXTEND_BUFFER() \
  1333. do { \
  1334. unsigned char *old_buffer = bufp->buffer; \
  1335. if (bufp->allocated == MAX_BUF_SIZE) \
  1336. return REG_ESIZE; \
  1337. bufp->allocated <<= 1; \
  1338. if (bufp->allocated > MAX_BUF_SIZE) \
  1339. bufp->allocated = MAX_BUF_SIZE; \
  1340. bufp->buffer = (unsigned char *) realloc (bufp->buffer, bufp->allocated);\
  1341. if (bufp->buffer == NULL) \
  1342. return REG_ESPACE; \
  1343. /* If the buffer moved, move all the pointers into it. */ \
  1344. if (old_buffer != bufp->buffer) \
  1345. { \
  1346. b = (b - old_buffer) + bufp->buffer; \
  1347. begalt = (begalt - old_buffer) + bufp->buffer; \
  1348. if (fixup_alt_jump) \
  1349. fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  1350. if (laststart) \
  1351. laststart = (laststart - old_buffer) + bufp->buffer; \
  1352. if (pending_exact) \
  1353. pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
  1354. } \
  1355. } while (0)
  1356. /* Since we have one byte reserved for the register number argument to
  1357. {start,stop}_memory, the maximum number of groups we can report
  1358. things about is what fits in that byte. */
  1359. #define MAX_REGNUM 255
  1360. /* But patterns can have more than `MAX_REGNUM' registers. We just
  1361. ignore the excess. */
  1362. typedef unsigned regnum_t;
  1363. /* Macros for the compile stack. */
  1364. /* Since offsets can go either forwards or backwards, this type needs to
  1365. be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
  1366. typedef int pattern_offset_t;
  1367. typedef struct
  1368. {
  1369. pattern_offset_t begalt_offset;
  1370. pattern_offset_t fixup_alt_jump;
  1371. pattern_offset_t inner_group_offset;
  1372. pattern_offset_t laststart_offset;
  1373. regnum_t regnum;
  1374. } compile_stack_elt_t;
  1375. typedef struct
  1376. {
  1377. compile_stack_elt_t *stack;
  1378. unsigned size;
  1379. unsigned avail; /* Offset of next open position. */
  1380. } compile_stack_type;
  1381. #define INIT_COMPILE_STACK_SIZE 32
  1382. #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
  1383. #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
  1384. /* The next available element. */
  1385. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1386. /* Structure to manage work area for range table. */
  1387. struct range_table_work_area
  1388. {
  1389. int *table; /* actual work area. */
  1390. int allocated; /* allocated size for work area in bytes. */
  1391. int used; /* actually used size in words. */
  1392. };
  1393. /* Make sure that WORK_AREA can hold more N multibyte characters. */
  1394. #define EXTEND_RANGE_TABLE_WORK_AREA(work_area, n) \
  1395. do { \
  1396. if (((work_area).used + (n)) * sizeof (int) > (work_area).allocated) \
  1397. { \
  1398. (work_area).allocated += 16 * sizeof (int); \
  1399. if ((work_area).table) \
  1400. (work_area).table \
  1401. = (int *) realloc ((work_area).table, (work_area).allocated); \
  1402. else \
  1403. (work_area).table \
  1404. = (int *) malloc ((work_area).allocated); \
  1405. if ((work_area).table == 0) \
  1406. FREE_STACK_RETURN (REG_ESPACE); \
  1407. } \
  1408. } while (0)
  1409. /* Set a range (RANGE_START, RANGE_END) to WORK_AREA. */
  1410. #define SET_RANGE_TABLE_WORK_AREA(work_area, range_start, range_end) \
  1411. do { \
  1412. EXTEND_RANGE_TABLE_WORK_AREA ((work_area), 2); \
  1413. (work_area).table[(work_area).used++] = (range_start); \
  1414. (work_area).table[(work_area).used++] = (range_end); \
  1415. } while (0)
  1416. /* Free allocated memory for WORK_AREA. */
  1417. #define FREE_RANGE_TABLE_WORK_AREA(work_area) \
  1418. do { \
  1419. if ((work_area).table) \
  1420. free ((work_area).table); \
  1421. } while (0)
  1422. #define CLEAR_RANGE_TABLE_WORK_USED(work_area) ((work_area).used = 0)
  1423. #define RANGE_TABLE_WORK_USED(work_area) ((work_area).used)
  1424. #define RANGE_TABLE_WORK_ELT(work_area, i) ((work_area).table[i])
  1425. /* Set the bit for character C in a list. */
  1426. #define SET_LIST_BIT(c) \
  1427. (b[((unsigned char) (c)) / BYTEWIDTH] \
  1428. |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1429. /* Get the next unsigned number in the uncompiled pattern. */
  1430. #define GET_UNSIGNED_NUMBER(num) \
  1431. { if (p != pend) \
  1432. { \
  1433. PATFETCH (c); \
  1434. while (ISDIGIT (c)) \
  1435. { \
  1436. if (num < 0) \
  1437. num = 0; \
  1438. num = num * 10 + c - '0'; \
  1439. if (p == pend) \
  1440. break; \
  1441. PATFETCH (c); \
  1442. } \
  1443. } \
  1444. }
  1445. #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  1446. #define IS_CHAR_CLASS(string) \
  1447. (STREQ (string, "alpha") || STREQ (string, "upper") \
  1448. || STREQ (string, "lower") || STREQ (string, "digit") \
  1449. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  1450. || STREQ (string, "space") || STREQ (string, "print") \
  1451. || STREQ (string, "punct") || STREQ (string, "graph") \
  1452. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1453. #ifndef MATCH_MAY_ALLOCATE
  1454. /* If we cannot allocate large objects within re_match_2_internal,
  1455. we make the fail stack and register vectors global.
  1456. The fail stack, we grow to the maximum size when a regexp
  1457. is compiled.
  1458. The register vectors, we adjust in size each time we
  1459. compile a regexp, according to the number of registers it needs. */
  1460. static fail_stack_type fail_stack;
  1461. /* Size with which the following vectors are currently allocated.
  1462. That is so we can make them bigger as needed,
  1463. but never make them smaller. */
  1464. static int regs_allocated_size;
  1465. static const char ** regstart, ** regend;
  1466. static const char ** old_regstart, ** old_regend;
  1467. static const char **best_regstart, **best_regend;
  1468. static register_info_type *reg_info;
  1469. static const char **reg_dummy;
  1470. static register_info_type *reg_info_dummy;
  1471. /* Make the register vectors big enough for NUM_REGS registers,
  1472. but don't make them smaller. */
  1473. static
  1474. regex_grow_registers (num_regs)
  1475. int num_regs;
  1476. {
  1477. if (num_regs > regs_allocated_size)
  1478. {
  1479. RETALLOC_IF (regstart, num_regs, const char *);
  1480. RETALLOC_IF (regend, num_regs, const char *);
  1481. RETALLOC_IF (old_regstart, num_regs, const char *);
  1482. RETALLOC_IF (old_regend, num_regs, const char *);
  1483. RETALLOC_IF (best_regstart, num_regs, const char *);
  1484. RETALLOC_IF (best_regend, num_regs, const char *);
  1485. RETALLOC_IF (reg_info, num_regs, register_info_type);
  1486. RETALLOC_IF (reg_dummy, num_regs, const char *);
  1487. RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
  1488. regs_allocated_size = num_regs;
  1489. }
  1490. }
  1491. #endif /* not MATCH_MAY_ALLOCATE */
  1492. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1493. Returns one of error codes defined in `regex.h', or zero for success.
  1494. Assumes the `allocated' (and perhaps `buffer') and `translate'
  1495. fields are set in BUFP on entry.
  1496. If it succeeds, results are put in BUFP (if it returns an error, the
  1497. contents of BUFP are undefined):
  1498. `buffer' is the compiled pattern;
  1499. `syntax' is set to SYNTAX;
  1500. `used' is set to the length of the compiled pattern;
  1501. `fastmap_accurate' is zero;
  1502. `re_nsub' is the number of subexpressions in PATTERN;
  1503. `not_bol' and `not_eol' are zero;
  1504. The `fastmap' and `newline_anchor' fields are neither
  1505. examined nor set. */
  1506. /* Return, freeing storage we allocated. */
  1507. #define FREE_STACK_RETURN(value) \
  1508. do { \
  1509. FREE_RANGE_TABLE_WORK_AREA (range_table_work); \
  1510. free (compile_stack.stack); \
  1511. return value; \
  1512. } while (0)
  1513. static reg_errcode_t
  1514. regex_compile (pattern, size, syntax, bufp)
  1515. const char *pattern;
  1516. int size;
  1517. reg_syntax_t syntax;
  1518. struct re_pattern_buffer *bufp;
  1519. {
  1520. /* We fetch characters from PATTERN here. Even though PATTERN is
  1521. `char *' (i.e., signed), we declare these variables as unsigned, so
  1522. they can be reliably used as array indices. */
  1523. register unsigned int c, c1;
  1524. /* A random temporary spot in PATTERN. */
  1525. const char *p1;
  1526. /* Points to the end of the buffer, where we should append. */
  1527. register unsigned char *b;
  1528. /* Keeps track of unclosed groups. */
  1529. compile_stack_type compile_stack;
  1530. /* Points to the current (ending) position in the pattern. */
  1531. #ifdef AIX
  1532. /* `const' makes AIX compiler fail. */
  1533. char *p = pattern;
  1534. #else
  1535. const char *p = pattern;
  1536. #endif
  1537. const char *pend = pattern + size;
  1538. /* How to translate the characters in the pattern. */
  1539. RE_TRANSLATE_TYPE translate = bufp->translate;
  1540. /* Address of the count-byte of the most recently inserted `exactn'
  1541. command. This makes it possible to tell if a new exact-match
  1542. character can be added to that command or if the character requires
  1543. a new `exactn' command. */
  1544. unsigned char *pending_exact = 0;
  1545. /* Address of start of the most recently finished expression.
  1546. This tells, e.g., postfix * where to find the start of its
  1547. operand. Reset at the beginning of groups and alternatives. */
  1548. unsigned char *laststart = 0;
  1549. /* Address of beginning of regexp, or inside of last group. */
  1550. unsigned char *begalt;
  1551. /* Place in the uncompiled pattern (i.e., the {) to
  1552. which to go back if the interval is invalid. */
  1553. const char *beg_interval;
  1554. /* Address of the place where a forward jump should go to the end of
  1555. the containing expression. Each alternative of an `or' -- except the
  1556. last -- ends with a forward jump of this sort. */
  1557. unsigned char *fixup_alt_jump = 0;
  1558. /* Counts open-groups as they are encountered. Remembered for the
  1559. matching close-group on the compile stack, so the same register
  1560. number is put in the stop_memory as the start_memory. */
  1561. regnum_t regnum = 0;
  1562. /* Work area for range table of charset. */
  1563. struct range_table_work_area range_table_work;
  1564. #ifdef DEBUG
  1565. DEBUG_PRINT1 ("\nCompiling pattern: ");
  1566. if (debug)
  1567. {
  1568. unsigned debug_count;
  1569. for (debug_count = 0; debug_count < size; debug_count++)
  1570. putchar (pattern[debug_count]);
  1571. putchar ('\n');
  1572. }
  1573. #endif /* DEBUG */
  1574. /* Initialize the compile stack. */
  1575. compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1576. if (compile_stack.stack == NULL)
  1577. return REG_ESPACE;
  1578. compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1579. compile_stack.avail = 0;
  1580. range_table_work.table = 0;
  1581. range_table_work.allocated = 0;
  1582. /* Initialize the pattern buffer. */
  1583. bufp->syntax = syntax;
  1584. bufp->fastmap_accurate = 0;
  1585. bufp->not_bol = bufp->not_eol = 0;
  1586. /* Set `used' to zero, so that if we return an error, the pattern
  1587. printer (for debugging) will think there's no pattern. We reset it
  1588. at the end. */
  1589. bufp->used = 0;
  1590. /* Always count groups, whether or not bufp->no_sub is set. */
  1591. bufp->re_nsub = 0;
  1592. #ifdef emacs
  1593. /* bufp->multibyte is set before regex_compile is called, so don't alter
  1594. it. */
  1595. #else /* not emacs */
  1596. /* Nothing is recognized as a multibyte character. */
  1597. bufp->multibyte = 0;
  1598. #endif
  1599. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1600. /* Initialize the syntax table. */
  1601. init_syntax_once ();
  1602. #endif
  1603. if (bufp->allocated == 0)
  1604. {
  1605. if (bufp->buffer)
  1606. { /* If zero allocated, but buffer is non-null, try to realloc
  1607. enough space. This loses if buffer's address is bogus, but
  1608. that is the user's responsibility. */
  1609. RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1610. }
  1611. else
  1612. { /* Caller did not allocate a buffer. Do it for them. */
  1613. bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1614. }
  1615. if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
  1616. bufp->allocated = INIT_BUF_SIZE;
  1617. }
  1618. begalt = b = bufp->buffer;
  1619. /* Loop through the uncompiled pattern until we're at the end. */
  1620. while (p != pend)
  1621. {
  1622. PATFETCH (c);
  1623. switch (c)
  1624. {
  1625. case '^':
  1626. {
  1627. if ( /* If at start of pattern, it's an operator. */
  1628. p == pattern + 1
  1629. /* If context independent, it's an operator. */
  1630. || syntax & RE_CONTEXT_INDEP_ANCHORS
  1631. /* Otherwise, depends on what's come before. */
  1632. || at_begline_loc_p (pattern, p, syntax))
  1633. BUF_PUSH (begline);
  1634. else
  1635. goto normal_char;
  1636. }
  1637. break;
  1638. case '$':
  1639. {
  1640. if ( /* If at end of pattern, it's an operator. */
  1641. p == pend
  1642. /* If context independent, it's an operator. */
  1643. || syntax & RE_CONTEXT_INDEP_ANCHORS
  1644. /* Otherwise, depends on what's next. */
  1645. || at_endline_loc_p (p, pend, syntax))
  1646. BUF_PUSH (endline);
  1647. else
  1648. goto normal_char;
  1649. }
  1650. break;
  1651. case '+':
  1652. case '?':
  1653. if ((syntax & RE_BK_PLUS_QM)
  1654. || (syntax & RE_LIMITED_OPS))
  1655. goto normal_char;
  1656. handle_plus:
  1657. case '*':
  1658. /* If there is no previous pattern... */
  1659. if (!laststart)
  1660. {
  1661. if (syntax & RE_CONTEXT_INVALID_OPS)
  1662. FREE_STACK_RETURN (REG_BADRPT);
  1663. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1664. goto normal_char;
  1665. }
  1666. {
  1667. /* Are we optimizing this jump? */
  1668. boolean keep_string_p = false;
  1669. /* 1 means zero (many) matches is allowed. */
  1670. char zero_times_ok = 0, many_times_ok = 0;
  1671. /* If there is a sequence of repetition chars, collapse it
  1672. down to just one (the right one). We can't combine
  1673. interval operators with these because of, e.g., `a{2}*',
  1674. which should only match an even number of `a's. */
  1675. for (;;)
  1676. {
  1677. zero_times_ok |= c != '+';
  1678. many_times_ok |= c != '?';
  1679. if (p == pend)
  1680. break;
  1681. PATFETCH (c);
  1682. if (c == '*'
  1683. || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1684. ;
  1685. else if (syntax & RE_BK_PLUS_QM && c == '\\')
  1686. {
  1687. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1688. PATFETCH (c1);
  1689. if (!(c1 == '+' || c1 == '?'))
  1690. {
  1691. PATUNFETCH;
  1692. PATUNFETCH;
  1693. break;
  1694. }
  1695. c = c1;
  1696. }
  1697. else
  1698. {
  1699. PATUNFETCH;
  1700. break;
  1701. }
  1702. /* If we get here, we found another repeat character. */
  1703. }
  1704. /* Star, etc. applied to an empty pattern is equivalent
  1705. to an empty pattern. */
  1706. if (!laststart)
  1707. break;
  1708. /* Now we know whether or not zero matches is allowed
  1709. and also whether or not two or more matches is allowed. */
  1710. if (many_times_ok)
  1711. { /* More than one repetition is allowed, so put in at the
  1712. end a backward relative jump from `b' to before the next
  1713. jump we're going to put in below (which jumps from
  1714. laststart to after this jump).
  1715. But if we are at the `*' in the exact sequence `.*\n',
  1716. insert an unconditional jump backwards to the .,
  1717. instead of the beginning of the loop. This way we only
  1718. push a failure point once, instead of every time
  1719. through the loop. */
  1720. assert (p - 1 > pattern);
  1721. /* Allocate the space for the jump. */
  1722. GET_BUFFER_SPACE (3);
  1723. /* We know we are not at the first character of the pattern,
  1724. because laststart was nonzero. And we've already
  1725. incremented `p', by the way, to be the character after
  1726. the `*'. Do we have to do something analogous here
  1727. for null bytes, because of RE_DOT_NOT_NULL? */
  1728. if (TRANSLATE ((unsigned char)*(p - 2)) == TRANSLATE ('.')
  1729. && zero_times_ok
  1730. && p < pend
  1731. && TRANSLATE ((unsigned char)*p) == TRANSLATE ('\n')
  1732. && !(syntax & RE_DOT_NEWLINE))
  1733. { /* We have .*\n. */
  1734. STORE_JUMP (jump, b, laststart);
  1735. keep_string_p = true;
  1736. }
  1737. else
  1738. /* Anything else. */
  1739. STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1740. /* We've added more stuff to the buffer. */
  1741. b += 3;
  1742. }
  1743. /* On failure, jump from laststart to b + 3, which will be the
  1744. end of the buffer after this jump is inserted. */
  1745. GET_BUFFER_SPACE (3);
  1746. INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1747. : on_failure_jump,
  1748. laststart, b + 3);
  1749. pending_exact = 0;
  1750. b += 3;
  1751. if (!zero_times_ok)
  1752. {
  1753. /* At least one repetition is required, so insert a
  1754. `dummy_failure_jump' before the initial
  1755. `on_failure_jump' instruction of the loop. This
  1756. effects a skip over that instruction the first time
  1757. we hit that loop. */
  1758. GET_BUFFER_SPACE (3);
  1759. INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  1760. b += 3;
  1761. }
  1762. }
  1763. break;
  1764. case '.':
  1765. laststart = b;
  1766. BUF_PUSH (anychar);
  1767. break;
  1768. case '[':
  1769. {
  1770. CLEAR_RANGE_TABLE_WORK_USED (range_table_work);
  1771. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1772. /* Ensure that we have enough space to push a charset: the
  1773. opcode, the length count, and the bitset; 34 bytes in all. */
  1774. GET_BUFFER_SPACE (34);
  1775. laststart = b;
  1776. /* We test `*p == '^' twice, instead of using an if
  1777. statement, so we only need one BUF_PUSH. */
  1778. BUF_PUSH (*p == '^' ? charset_not : charset);
  1779. if (*p == '^')
  1780. p++;
  1781. /* Remember the first position in the bracket expression. */
  1782. p1 = p;
  1783. /* Push the number of bytes in the bitmap. */
  1784. BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1785. /* Clear the whole map. */
  1786. bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  1787. /* charset_not matches newline according to a syntax bit. */
  1788. if ((re_opcode_t) b[-2] == charset_not
  1789. && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1790. SET_LIST_BIT ('\n');
  1791. /* Read in characters and ranges, setting map bits. */
  1792. for (;;)
  1793. {
  1794. int len;
  1795. boolean escaped_char = false;
  1796. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1797. PATFETCH (c);
  1798. /* \ might escape characters inside [...] and [^...]. */
  1799. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1800. {
  1801. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1802. PATFETCH (c);
  1803. escaped_char = true;
  1804. }
  1805. else
  1806. {
  1807. /* Could be the end of the bracket expression. If it's
  1808. not (i.e., when the bracket expression is `[]' so
  1809. far), the ']' character bit gets set way below. */
  1810. if (c == ']' && p != p1 + 1)
  1811. break;
  1812. }
  1813. /* If C indicates start of multibyte char, get the
  1814. actual character code in C, and set the pattern
  1815. pointer P to the next character boundary. */
  1816. if (bufp->multibyte && BASE_LEADING_CODE_P (c))
  1817. {
  1818. PATUNFETCH;
  1819. c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
  1820. p += len;
  1821. }
  1822. /* What should we do for the character which is
  1823. greater than 0x7F, but not BASE_LEADING_CODE_P?
  1824. XXX */
  1825. /* See if we're at the beginning of a possible character
  1826. class. */
  1827. else if (!escaped_char &&
  1828. syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1829. {
  1830. /* Leave room for the null. */
  1831. char str[CHAR_CLASS_MAX_LENGTH + 1];
  1832. PATFETCH (c);
  1833. c1 = 0;
  1834. /* If pattern is `[[:'. */
  1835. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1836. for (;;)
  1837. {
  1838. PATFETCH (c);
  1839. if (c == ':' || c == ']' || p == pend
  1840. || c1 == CHAR_CLASS_MAX_LENGTH)
  1841. break;
  1842. str[c1++] = c;
  1843. }
  1844. str[c1] = '\0';
  1845. /* If isn't a word bracketed by `[:' and `:]':
  1846. undo the ending character, the letters, and
  1847. leave the leading `:' and `[' (but set bits for
  1848. them). */
  1849. if (c == ':' && *p == ']')
  1850. {
  1851. int ch;
  1852. boolean is_alnum = STREQ (str, "alnum");
  1853. boolean is_alpha = STREQ (str, "alpha");
  1854. boolean is_blank = STREQ (str, "blank");
  1855. boolean is_cntrl = STREQ (str, "cntrl");
  1856. boolean is_digit = STREQ (str, "digit");
  1857. boolean is_graph = STREQ (str, "graph");
  1858. boolean is_lower = STREQ (str, "lower");
  1859. boolean is_print = STREQ (str, "print");
  1860. boolean is_punct = STREQ (str, "punct");
  1861. boolean is_space = STREQ (str, "space");
  1862. boolean is_upper = STREQ (str, "upper");
  1863. boolean is_xdigit = STREQ (str, "xdigit");
  1864. if (!IS_CHAR_CLASS (str))
  1865. FREE_STACK_RETURN (REG_ECTYPE);
  1866. /* Throw away the ] at the end of the character
  1867. class. */
  1868. PATFETCH (c);
  1869. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1870. for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1871. {
  1872. int translated = TRANSLATE (ch);
  1873. /* This was split into 3 if's to
  1874. avoid an arbitrary limit in some compiler. */
  1875. if ( (is_alnum && ISALNUM (ch))
  1876. || (is_alpha && ISALPHA (ch))
  1877. || (is_blank && ISBLANK (ch))
  1878. || (is_cntrl && ISCNTRL (ch)))
  1879. SET_LIST_BIT (translated);
  1880. if ( (is_digit && ISDIGIT (ch))
  1881. || (is_graph && ISGRAPH (ch))
  1882. || (is_lower && ISLOWER (ch))
  1883. || (is_print && ISPRINT (ch)))
  1884. SET_LIST_BIT (translated);
  1885. if ( (is_punct && ISPUNCT (ch))
  1886. || (is_space && ISSPACE (ch))
  1887. || (is_upper && ISUPPER (ch))
  1888. || (is_xdigit && ISXDIGIT (ch)))
  1889. SET_LIST_BIT (translated);
  1890. }
  1891. /* Repeat the loop. */
  1892. continue;
  1893. }
  1894. else
  1895. {
  1896. c1++;
  1897. while (c1--)
  1898. PATUNFETCH;
  1899. SET_LIST_BIT ('[');
  1900. /* Because the `:' may starts the range, we
  1901. can't simply set bit and repeat the loop.
  1902. Instead, just set it to C and handle below. */
  1903. c = ':';
  1904. }
  1905. }
  1906. if (p < pend && p[0] == '-' && p[1] != ']')
  1907. {
  1908. /* Discard the `-'. */
  1909. PATFETCH (c1);
  1910. /* Fetch the character which ends the range. */
  1911. PATFETCH (c1);
  1912. if (bufp->multibyte && BASE_LEADING_CODE_P (c1))
  1913. {
  1914. PATUNFETCH;
  1915. c1 = STRING_CHAR_AND_LENGTH (p, pend - p, len);
  1916. p += len;
  1917. }
  1918. if (SINGLE_BYTE_CHAR_P (c)
  1919. && ! SINGLE_BYTE_CHAR_P (c1))
  1920. {
  1921. /* Handle a range such as \177-\377 in multibyte mode.
  1922. Split that into two ranges,,
  1923. the low one ending at 0237, and the high one
  1924. starting at ...040. */
  1925. int c1_base = (c1 & ~0177) | 040;
  1926. SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
  1927. c1 = 0237;
  1928. }
  1929. else if (!SAME_CHARSET_P (c, c1))
  1930. FREE_STACK_RETURN (REG_ERANGE);
  1931. }
  1932. else
  1933. /* Range from C to C. */
  1934. c1 = c;
  1935. /* Set the range ... */
  1936. if (SINGLE_BYTE_CHAR_P (c))
  1937. /* ... into bitmap. */
  1938. {
  1939. unsigned this_char;
  1940. int range_start = c, range_end = c1;
  1941. /* If the start is after the end, the range is empty. */
  1942. if (range_start > range_end)
  1943. {
  1944. if (syntax & RE_NO_EMPTY_RANGES)
  1945. FREE_STACK_RETURN (REG_ERANGE);
  1946. /* Else, repeat the loop. */
  1947. }
  1948. else
  1949. {
  1950. for (this_char = range_start; this_char <= range_end;
  1951. this_char++)
  1952. SET_LIST_BIT (TRANSLATE (this_char));
  1953. }
  1954. }
  1955. else
  1956. /* ... into range table. */
  1957. SET_RANGE_TABLE_WORK_AREA (range_table_work, c, c1);
  1958. }
  1959. /* Discard any (non)matching list bytes that are all 0 at the
  1960. end of the map. Decrease the map-length byte too. */
  1961. while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  1962. b[-1]--;
  1963. b += b[-1];
  1964. /* Build real range table from work area. */
  1965. if (RANGE_TABLE_WORK_USED (range_table_work))
  1966. {
  1967. int i;
  1968. int used = RANGE_TABLE_WORK_USED (range_table_work);
  1969. /* Allocate space for COUNT + RANGE_TABLE. Needs two
  1970. bytes for COUNT and three bytes for each character. */
  1971. GET_BUFFER_SPACE (2 + used * 3);
  1972. /* Indicate the existence of range table. */
  1973. laststart[1] |= 0x80;
  1974. STORE_NUMBER_AND_INCR (b, used / 2);
  1975. for (i = 0; i < used; i++)
  1976. STORE_CHARACTER_AND_INCR
  1977. (b, RANGE_TABLE_WORK_ELT (range_table_work, i));
  1978. }
  1979. }
  1980. break;
  1981. case '(':
  1982. if (syntax & RE_NO_BK_PARENS)
  1983. goto handle_open;
  1984. else
  1985. goto normal_char;
  1986. case ')':
  1987. if (syntax & RE_NO_BK_PARENS)
  1988. goto handle_close;
  1989. else
  1990. goto normal_char;
  1991. case '\n':
  1992. if (syntax & RE_NEWLINE_ALT)
  1993. goto handle_alt;
  1994. else
  1995. goto normal_char;
  1996. case '|':
  1997. if (syntax & RE_NO_BK_VBAR)
  1998. goto handle_alt;
  1999. else
  2000. goto normal_char;
  2001. case '{':
  2002. if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  2003. goto handle_interval;
  2004. else
  2005. goto normal_char;
  2006. case '\\':
  2007. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2008. /* Do not translate the character after the \, so that we can
  2009. distinguish, e.g., \B from \b, even if we normally would
  2010. translate, e.g., B to b. */
  2011. PATFETCH_RAW (c);
  2012. switch (c)
  2013. {
  2014. case '(':
  2015. if (syntax & RE_NO_BK_PARENS)
  2016. goto normal_backslash;
  2017. handle_open:
  2018. bufp->re_nsub++;
  2019. regnum++;
  2020. if (COMPILE_STACK_FULL)
  2021. {
  2022. RETALLOC (compile_stack.stack, compile_stack.size << 1,
  2023. compile_stack_elt_t);
  2024. if (compile_stack.stack == NULL) return REG_ESPACE;
  2025. compile_stack.size <<= 1;
  2026. }
  2027. /* These are the values to restore when we hit end of this
  2028. group. They are all relative offsets, so that if the
  2029. whole pattern moves because of realloc, they will still
  2030. be valid. */
  2031. COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  2032. COMPILE_STACK_TOP.fixup_alt_jump
  2033. = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  2034. COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  2035. COMPILE_STACK_TOP.regnum = regnum;
  2036. /* We will eventually replace the 0 with the number of
  2037. groups inner to this one. But do not push a
  2038. start_memory for groups beyond the last one we can
  2039. represent in the compiled pattern. */
  2040. if (regnum <= MAX_REGNUM)
  2041. {
  2042. COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  2043. BUF_PUSH_3 (start_memory, regnum, 0);
  2044. }
  2045. compile_stack.avail++;
  2046. fixup_alt_jump = 0;
  2047. laststart = 0;
  2048. begalt = b;
  2049. /* If we've reached MAX_REGNUM groups, then this open
  2050. won't actually generate any code, so we'll have to
  2051. clear pending_exact explicitly. */
  2052. pending_exact = 0;
  2053. break;
  2054. case ')':
  2055. if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  2056. if (COMPILE_STACK_EMPTY)
  2057. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2058. goto normal_backslash;
  2059. else
  2060. FREE_STACK_RETURN (REG_ERPAREN);
  2061. handle_close:
  2062. if (fixup_alt_jump)
  2063. { /* Push a dummy failure point at the end of the
  2064. alternative for a possible future
  2065. `pop_failure_jump' to pop. See comments at
  2066. `push_dummy_failure' in `re_match_2'. */
  2067. BUF_PUSH (push_dummy_failure);
  2068. /* We allocated space for this jump when we assigned
  2069. to `fixup_alt_jump', in the `handle_alt' case below. */
  2070. STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  2071. }
  2072. /* See similar code for backslashed left paren above. */
  2073. if (COMPILE_STACK_EMPTY)
  2074. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2075. goto normal_char;
  2076. else
  2077. FREE_STACK_RETURN (REG_ERPAREN);
  2078. /* Since we just checked for an empty stack above, this
  2079. ``can't happen''. */
  2080. assert (compile_stack.avail != 0);
  2081. {
  2082. /* We don't just want to restore into `regnum', because
  2083. later groups should continue to be numbered higher,
  2084. as in `(ab)c(de)' -- the second group is #2. */
  2085. regnum_t this_group_regnum;
  2086. compile_stack.avail--;
  2087. begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  2088. fixup_alt_jump
  2089. = COMPILE_STACK_TOP.fixup_alt_jump
  2090. ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
  2091. : 0;
  2092. laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  2093. this_group_regnum = COMPILE_STACK_TOP.regnum;
  2094. /* If we've reached MAX_REGNUM groups, then this open
  2095. won't actually generate any code, so we'll have to
  2096. clear pending_exact explicitly. */
  2097. pending_exact = 0;
  2098. /* We're at the end of the group, so now we know how many
  2099. groups were inside this one. */
  2100. if (this_group_regnum <= MAX_REGNUM)
  2101. {
  2102. unsigned char *inner_group_loc
  2103. = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  2104. *inner_group_loc = regnum - this_group_regnum;
  2105. BUF_PUSH_3 (stop_memory, this_group_regnum,
  2106. regnum - this_group_regnum);
  2107. }
  2108. }
  2109. break;
  2110. case '|': /* `\|'. */
  2111. if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  2112. goto normal_backslash;
  2113. handle_alt:
  2114. if (syntax & RE_LIMITED_OPS)
  2115. goto normal_char;
  2116. /* Insert before the previous alternative a jump which
  2117. jumps to this alternative if the former fails. */
  2118. GET_BUFFER_SPACE (3);
  2119. INSERT_JUMP (on_failure_jump, begalt, b + 6);
  2120. pending_exact = 0;
  2121. b += 3;
  2122. /* The alternative before this one has a jump after it
  2123. which gets executed if it gets matched. Adjust that
  2124. jump so it will jump to this alternative's analogous
  2125. jump (put in below, which in turn will jump to the next
  2126. (if any) alternative's such jump, etc.). The last such
  2127. jump jumps to the correct final destination. A picture:
  2128. _____ _____
  2129. | | | |
  2130. | v | v
  2131. a | b | c
  2132. If we are at `b', then fixup_alt_jump right now points to a
  2133. three-byte space after `a'. We'll put in the jump, set
  2134. fixup_alt_jump to right after `b', and leave behind three
  2135. bytes which we'll fill in when we get to after `c'. */
  2136. if (fixup_alt_jump)
  2137. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2138. /* Mark and leave space for a jump after this alternative,
  2139. to be filled in later either by next alternative or
  2140. when know we're at the end of a series of alternatives. */
  2141. fixup_alt_jump = b;
  2142. GET_BUFFER_SPACE (3);
  2143. b += 3;
  2144. laststart = 0;
  2145. begalt = b;
  2146. break;
  2147. case '{':
  2148. /* If \{ is a literal. */
  2149. if (!(syntax & RE_INTERVALS)
  2150. /* If we're at `\{' and it's not the open-interval
  2151. operator. */
  2152. || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2153. || (p - 2 == pattern && p == pend))
  2154. goto normal_backslash;
  2155. handle_interval:
  2156. {
  2157. /* If got here, then the syntax allows intervals. */
  2158. /* At least (most) this many matches must be made. */
  2159. int lower_bound = -1, upper_bound = -1;
  2160. beg_interval = p - 1;
  2161. if (p == pend)
  2162. {
  2163. if (syntax & RE_NO_BK_BRACES)
  2164. goto unfetch_interval;
  2165. else
  2166. FREE_STACK_RETURN (REG_EBRACE);
  2167. }
  2168. GET_UNSIGNED_NUMBER (lower_bound);
  2169. if (c == ',')
  2170. {
  2171. GET_UNSIGNED_NUMBER (upper_bound);
  2172. if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  2173. }
  2174. else
  2175. /* Interval such as `{1}' => match exactly once. */
  2176. upper_bound = lower_bound;
  2177. if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  2178. || lower_bound > upper_bound)
  2179. {
  2180. if (syntax & RE_NO_BK_BRACES)
  2181. goto unfetch_interval;
  2182. else
  2183. FREE_STACK_RETURN (REG_BADBR);
  2184. }
  2185. if (!(syntax & RE_NO_BK_BRACES))
  2186. {
  2187. if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
  2188. PATFETCH (c);
  2189. }
  2190. if (c != '}')
  2191. {
  2192. if (syntax & RE_NO_BK_BRACES)
  2193. goto unfetch_interval;
  2194. else
  2195. FREE_STACK_RETURN (REG_BADBR);
  2196. }
  2197. /* We just parsed a valid interval. */
  2198. /* If it's invalid to have no preceding re. */
  2199. if (!laststart)
  2200. {
  2201. if (syntax & RE_CONTEXT_INVALID_OPS)
  2202. FREE_STACK_RETURN (REG_BADRPT);
  2203. else if (syntax & RE_CONTEXT_INDEP_OPS)
  2204. laststart = b;
  2205. else
  2206. goto unfetch_interval;
  2207. }
  2208. /* If the upper bound is zero, don't want to succeed at
  2209. all; jump from `laststart' to `b + 3', which will be
  2210. the end of the buffer after we insert the jump. */
  2211. if (upper_bound == 0)
  2212. {
  2213. GET_BUFFER_SPACE (3);
  2214. INSERT_JUMP (jump, laststart, b + 3);
  2215. b += 3;
  2216. }
  2217. /* Otherwise, we have a nontrivial interval. When
  2218. we're all done, the pattern will look like:
  2219. set_number_at <jump count> <upper bound>
  2220. set_number_at <succeed_n count> <lower bound>
  2221. succeed_n <after jump addr> <succeed_n count>
  2222. <body of loop>
  2223. jump_n <succeed_n addr> <jump count>
  2224. (The upper bound and `jump_n' are omitted if
  2225. `upper_bound' is 1, though.) */
  2226. else
  2227. { /* If the upper bound is > 1, we need to insert
  2228. more at the end of the loop. */
  2229. unsigned nbytes = 10 + (upper_bound > 1) * 10;
  2230. GET_BUFFER_SPACE (nbytes);
  2231. /* Initialize lower bound of the `succeed_n', even
  2232. though it will be set during matching by its
  2233. attendant `set_number_at' (inserted next),
  2234. because `re_compile_fastmap' needs to know.
  2235. Jump to the `jump_n' we might insert below. */
  2236. INSERT_JUMP2 (succeed_n, laststart,
  2237. b + 5 + (upper_bound > 1) * 5,
  2238. lower_bound);
  2239. b += 5;
  2240. /* Code to initialize the lower bound. Insert
  2241. before the `succeed_n'. The `5' is the last two
  2242. bytes of this `set_number_at', plus 3 bytes of
  2243. the following `succeed_n'. */
  2244. insert_op2 (set_number_at, laststart, 5, lower_bound, b);
  2245. b += 5;
  2246. if (upper_bound > 1)
  2247. { /* More than one repetition is allowed, so
  2248. append a backward jump to the `succeed_n'
  2249. that starts this interval.
  2250. When we've reached this during matching,
  2251. we'll have matched the interval once, so
  2252. jump back only `upper_bound - 1' times. */
  2253. STORE_JUMP2 (jump_n, b, laststart + 5,
  2254. upper_bound - 1);
  2255. b += 5;
  2256. /* The location we want to set is the second
  2257. parameter of the `jump_n'; that is `b-2' as
  2258. an absolute address. `laststart' will be
  2259. the `set_number_at' we're about to insert;
  2260. `laststart+3' the number to set, the source
  2261. for the relative address. But we are
  2262. inserting into the middle of the pattern --
  2263. so everything is getting moved up by 5.
  2264. Conclusion: (b - 2) - (laststart + 3) + 5,
  2265. i.e., b - laststart.
  2266. We insert this at the beginning of the loop
  2267. so that if we fail during matching, we'll
  2268. reinitialize the bounds. */
  2269. insert_op2 (set_number_at, laststart, b - laststart,
  2270. upper_bound - 1, b);
  2271. b += 5;
  2272. }
  2273. }
  2274. pending_exact = 0;
  2275. beg_interval = NULL;
  2276. }
  2277. break;
  2278. unfetch_interval:
  2279. /* If an invalid interval, match the characters as literals. */
  2280. assert (beg_interval);
  2281. p = beg_interval;
  2282. beg_interval = NULL;
  2283. /* normal_char and normal_backslash need `c'. */
  2284. PATFETCH (c);
  2285. if (!(syntax & RE_NO_BK_BRACES))
  2286. {
  2287. if (p > pattern && p[-1] == '\\')
  2288. goto normal_backslash;
  2289. }
  2290. goto normal_char;
  2291. #ifdef emacs
  2292. /* There is no way to specify the before_dot and after_dot
  2293. operators. rms says this is ok. --karl */
  2294. case '=':
  2295. BUF_PUSH (at_dot);
  2296. break;
  2297. case 's':
  2298. laststart = b;
  2299. PATFETCH (c);
  2300. BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  2301. break;
  2302. case 'S':
  2303. laststart = b;
  2304. PATFETCH (c);
  2305. BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  2306. break;
  2307. case 'c':
  2308. laststart = b;
  2309. PATFETCH_RAW (c);
  2310. BUF_PUSH_2 (categoryspec, c);
  2311. break;
  2312. case 'C':
  2313. laststart = b;
  2314. PATFETCH_RAW (c);
  2315. BUF_PUSH_2 (notcategoryspec, c);
  2316. break;
  2317. #endif /* emacs */
  2318. case 'w':
  2319. laststart = b;
  2320. BUF_PUSH (wordchar);
  2321. break;
  2322. case 'W':
  2323. laststart = b;
  2324. BUF_PUSH (notwordchar);
  2325. break;
  2326. case '<':
  2327. BUF_PUSH (wordbeg);
  2328. break;
  2329. case '>':
  2330. BUF_PUSH (wordend);
  2331. break;
  2332. case 'b':
  2333. BUF_PUSH (wordbound);
  2334. break;
  2335. case 'B':
  2336. BUF_PUSH (notwordbound);
  2337. break;
  2338. case '`':
  2339. BUF_PUSH (begbuf);
  2340. break;
  2341. case '\'':
  2342. BUF_PUSH (endbuf);
  2343. break;
  2344. case '1': case '2': case '3': case '4': case '5':
  2345. case '6': case '7': case '8': case '9':
  2346. if (syntax & RE_NO_BK_REFS)
  2347. goto normal_char;
  2348. c1 = c - '0';
  2349. if (c1 > regnum)
  2350. FREE_STACK_RETURN (REG_ESUBREG);
  2351. /* Can't back reference to a subexpression if inside of it. */
  2352. if (group_in_compile_stack (compile_stack, c1))
  2353. goto normal_char;
  2354. laststart = b;
  2355. BUF_PUSH_2 (duplicate, c1);
  2356. break;
  2357. case '+':
  2358. case '?':
  2359. if (syntax & RE_BK_PLUS_QM)
  2360. goto handle_plus;
  2361. else
  2362. goto normal_backslash;
  2363. default:
  2364. normal_backslash:
  2365. /* You might think it would be useful for \ to mean
  2366. not to translate; but if we don't translate it
  2367. it will never match anything. */
  2368. c = TRANSLATE (c);
  2369. goto normal_char;
  2370. }
  2371. break;
  2372. default:
  2373. /* Expects the character in `c'. */
  2374. normal_char:
  2375. p1 = p - 1; /* P1 points the head of C. */
  2376. #ifdef emacs
  2377. if (bufp->multibyte)
  2378. {
  2379. c = STRING_CHAR (p1, pend - p1);
  2380. c = TRANSLATE (c);
  2381. /* Set P to the next character boundary. */
  2382. p += MULTIBYTE_FORM_LENGTH (p1, pend - p1) - 1;
  2383. }
  2384. #endif
  2385. /* If no exactn currently being built. */
  2386. if (!pending_exact
  2387. /* If last exactn not at current position. */
  2388. || pending_exact + *pending_exact + 1 != b
  2389. /* We have only one byte following the exactn for the count. */
  2390. || *pending_exact >= (1 << BYTEWIDTH) - (p - p1)
  2391. /* If followed by a repetition operator. */
  2392. || (p != pend && (*p == '*' || *p == '^'))
  2393. || ((syntax & RE_BK_PLUS_QM)
  2394. ? p + 1 < pend && *p == '\\' && (p[1] == '+' || p[1] == '?')
  2395. : p != pend && (*p == '+' || *p == '?'))
  2396. || ((syntax & RE_INTERVALS)
  2397. && ((syntax & RE_NO_BK_BRACES)
  2398. ? p != pend && *p == '{'
  2399. : p + 1 < pend && p[0] == '\\' && p[1] == '{')))
  2400. {
  2401. /* Start building a new exactn. */
  2402. laststart = b;
  2403. BUF_PUSH_2 (exactn, 0);
  2404. pending_exact = b - 1;
  2405. }
  2406. #ifdef emacs
  2407. if (! SINGLE_BYTE_CHAR_P (c))
  2408. {
  2409. unsigned char work[4], *str;
  2410. int i = CHAR_STRING (c, work, str);
  2411. int j;
  2412. for (j = 0; j < i; j++)
  2413. {
  2414. BUF_PUSH (str[j]);
  2415. (*pending_exact)++;
  2416. }
  2417. }
  2418. else
  2419. #endif
  2420. {
  2421. BUF_PUSH (c);
  2422. (*pending_exact)++;
  2423. }
  2424. break;
  2425. } /* switch (c) */
  2426. } /* while p != pend */
  2427. /* Through the pattern now. */
  2428. if (fixup_alt_jump)
  2429. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2430. if (!COMPILE_STACK_EMPTY)
  2431. FREE_STACK_RETURN (REG_EPAREN);
  2432. /* If we don't want backtracking, force success
  2433. the first time we reach the end of the compiled pattern. */
  2434. if (syntax & RE_NO_POSIX_BACKTRACKING)
  2435. BUF_PUSH (succeed);
  2436. free (compile_stack.stack);
  2437. /* We have succeeded; set the length of the buffer. */
  2438. bufp->used = b - bufp->buffer;
  2439. #ifdef DEBUG
  2440. if (debug)
  2441. {
  2442. DEBUG_PRINT1 ("\nCompiled pattern: \n");
  2443. print_compiled_pattern (bufp);
  2444. }
  2445. #endif /* DEBUG */
  2446. #ifndef MATCH_MAY_ALLOCATE
  2447. /* Initialize the failure stack to the largest possible stack. This
  2448. isn't necessary unless we're trying to avoid calling alloca in
  2449. the search and match routines. */
  2450. {
  2451. int num_regs = bufp->re_nsub + 1;
  2452. if (fail_stack.size < re_max_failures * TYPICAL_FAILURE_SIZE)
  2453. {
  2454. fail_stack.size = re_max_failures * TYPICAL_FAILURE_SIZE;
  2455. #ifdef emacs
  2456. if (! fail_stack.stack)
  2457. fail_stack.stack
  2458. = (fail_stack_elt_t *) xmalloc (fail_stack.size
  2459. * sizeof (fail_stack_elt_t));
  2460. else
  2461. fail_stack.stack
  2462. = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
  2463. (fail_stack.size
  2464. * sizeof (fail_stack_elt_t)));
  2465. #else /* not emacs */
  2466. if (! fail_stack.stack)
  2467. fail_stack.stack
  2468. = (fail_stack_elt_t *) malloc (fail_stack.size
  2469. * sizeof (fail_stack_elt_t));
  2470. else
  2471. fail_stack.stack
  2472. = (fail_stack_elt_t *) realloc (fail_stack.stack,
  2473. (fail_stack.size
  2474. * sizeof (fail_stack_elt_t)));
  2475. #endif /* not emacs */
  2476. }
  2477. regex_grow_registers (num_regs);
  2478. }
  2479. #endif /* not MATCH_MAY_ALLOCATE */
  2480. return REG_NOERROR;
  2481. } /* regex_compile */
  2482. /* Subroutines for `regex_compile'. */
  2483. /* Store OP at LOC followed by two-byte integer parameter ARG. */
  2484. static void
  2485. store_op1 (op, loc, arg)
  2486. re_opcode_t op;
  2487. unsigned char *loc;
  2488. int arg;
  2489. {
  2490. *loc = (unsigned char) op;
  2491. STORE_NUMBER (loc + 1, arg);
  2492. }
  2493. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
  2494. static void
  2495. store_op2 (op, loc, arg1, arg2)
  2496. re_opcode_t op;
  2497. unsigned char *loc;
  2498. int arg1, arg2;
  2499. {
  2500. *loc = (unsigned char) op;
  2501. STORE_NUMBER (loc + 1, arg1);
  2502. STORE_NUMBER (loc + 3, arg2);
  2503. }
  2504. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  2505. for OP followed by two-byte integer parameter ARG. */
  2506. static void
  2507. insert_op1 (op, loc, arg, end)
  2508. re_opcode_t op;
  2509. unsigned char *loc;
  2510. int arg;
  2511. unsigned char *end;
  2512. {
  2513. register unsigned char *pfrom = end;
  2514. register unsigned char *pto = end + 3;
  2515. while (pfrom != loc)
  2516. *--pto = *--pfrom;
  2517. store_op1 (op, loc, arg);
  2518. }
  2519. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
  2520. static void
  2521. insert_op2 (op, loc, arg1, arg2, end)
  2522. re_opcode_t op;
  2523. unsigned char *loc;
  2524. int arg1, arg2;
  2525. unsigned char *end;
  2526. {
  2527. register unsigned char *pfrom = end;
  2528. register unsigned char *pto = end + 5;
  2529. while (pfrom != loc)
  2530. *--pto = *--pfrom;
  2531. store_op2 (op, loc, arg1, arg2);
  2532. }
  2533. /* P points to just after a ^ in PATTERN. Return true if that ^ comes
  2534. after an alternative or a begin-subexpression. We assume there is at
  2535. least one character before the ^. */
  2536. static boolean
  2537. at_begline_loc_p (pattern, p, syntax)
  2538. const char *pattern, *p;
  2539. reg_syntax_t syntax;
  2540. {
  2541. const char *prev = p - 2;
  2542. boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  2543. return
  2544. /* After a subexpression? */
  2545. (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  2546. /* After an alternative? */
  2547. || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  2548. }
  2549. /* The dual of at_begline_loc_p. This one is for $. We assume there is
  2550. at least one character after the $, i.e., `P < PEND'. */
  2551. static boolean
  2552. at_endline_loc_p (p, pend, syntax)
  2553. const char *p, *pend;
  2554. int syntax;
  2555. {
  2556. const char *next = p;
  2557. boolean next_backslash = *next == '\\';
  2558. const char *next_next = p + 1 < pend ? p + 1 : 0;
  2559. return
  2560. /* Before a subexpression? */
  2561. (syntax & RE_NO_BK_PARENS ? *next == ')'
  2562. : next_backslash && next_next && *next_next == ')')
  2563. /* Before an alternative? */
  2564. || (syntax & RE_NO_BK_VBAR ? *next == '|'
  2565. : next_backslash && next_next && *next_next == '|');
  2566. }
  2567. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
  2568. false if it's not. */
  2569. static boolean
  2570. group_in_compile_stack (compile_stack, regnum)
  2571. compile_stack_type compile_stack;
  2572. regnum_t regnum;
  2573. {
  2574. int this_element;
  2575. for (this_element = compile_stack.avail - 1;
  2576. this_element >= 0;
  2577. this_element--)
  2578. if (compile_stack.stack[this_element].regnum == regnum)
  2579. return true;
  2580. return false;
  2581. }
  2582. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2583. BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
  2584. characters can start a string that matches the pattern. This fastmap
  2585. is used by re_search to skip quickly over impossible starting points.
  2586. The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2587. area as BUFP->fastmap.
  2588. We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  2589. the pattern buffer.
  2590. Returns 0 if we succeed, -2 if an internal error. */
  2591. int
  2592. re_compile_fastmap (bufp)
  2593. struct re_pattern_buffer *bufp;
  2594. {
  2595. int i, j, k;
  2596. #ifdef MATCH_MAY_ALLOCATE
  2597. fail_stack_type fail_stack;
  2598. #endif
  2599. #ifndef REGEX_MALLOC
  2600. char *destination;
  2601. #endif
  2602. /* We don't push any register information onto the failure stack. */
  2603. unsigned num_regs = 0;
  2604. register char *fastmap = bufp->fastmap;
  2605. unsigned char *pattern = bufp->buffer;
  2606. unsigned long size = bufp->used;
  2607. unsigned char *p = pattern;
  2608. register unsigned char *pend = pattern + size;
  2609. /* This holds the pointer to the failure stack, when
  2610. it is allocated relocatably. */
  2611. fail_stack_elt_t *failure_stack_ptr;
  2612. /* Assume that each path through the pattern can be null until
  2613. proven otherwise. We set this false at the bottom of switch
  2614. statement, to which we get only if a particular path doesn't
  2615. match the empty string. */
  2616. boolean path_can_be_null = true;
  2617. /* We aren't doing a `succeed_n' to begin with. */
  2618. boolean succeed_n_p = false;
  2619. /* If all elements for base leading-codes in fastmap is set, this
  2620. flag is set true. */
  2621. boolean match_any_multibyte_characters = false;
  2622. /* Maximum code of simple (single byte) character. */
  2623. int simple_char_max;
  2624. assert (fastmap != NULL && p != NULL);
  2625. INIT_FAIL_STACK ();
  2626. bzero (fastmap, 1 << BYTEWIDTH); /* Assume nothing's valid. */
  2627. bufp->fastmap_accurate = 1; /* It will be when we're done. */
  2628. bufp->can_be_null = 0;
  2629. while (1)
  2630. {
  2631. if (p == pend || *p == succeed)
  2632. {
  2633. /* We have reached the (effective) end of pattern. */
  2634. if (!FAIL_STACK_EMPTY ())
  2635. {
  2636. bufp->can_be_null |= path_can_be_null;
  2637. /* Reset for next path. */
  2638. path_can_be_null = true;
  2639. p = fail_stack.stack[--fail_stack.avail].pointer;
  2640. continue;
  2641. }
  2642. else
  2643. break;
  2644. }
  2645. /* We should never be about to go beyond the end of the pattern. */
  2646. assert (p < pend);
  2647. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  2648. {
  2649. /* I guess the idea here is to simply not bother with a fastmap
  2650. if a backreference is used, since it's too hard to figure out
  2651. the fastmap for the corresponding group. Setting
  2652. `can_be_null' stops `re_search_2' from using the fastmap, so
  2653. that is all we do. */
  2654. case duplicate:
  2655. bufp->can_be_null = 1;
  2656. goto done;
  2657. /* Following are the cases which match a character. These end
  2658. with `break'. */
  2659. case exactn:
  2660. fastmap[p[1]] = 1;
  2661. break;
  2662. #ifndef emacs
  2663. case charset:
  2664. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2665. if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2666. fastmap[j] = 1;
  2667. break;
  2668. case charset_not:
  2669. /* Chars beyond end of map must be allowed. */
  2670. for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  2671. fastmap[j] = 1;
  2672. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2673. if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2674. fastmap[j] = 1;
  2675. break;
  2676. case wordchar:
  2677. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2678. if (SYNTAX (j) == Sword)
  2679. fastmap[j] = 1;
  2680. break;
  2681. case notwordchar:
  2682. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2683. if (SYNTAX (j) != Sword)
  2684. fastmap[j] = 1;
  2685. break;
  2686. #else /* emacs */
  2687. case charset:
  2688. for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
  2689. j >= 0; j--)
  2690. if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2691. fastmap[j] = 1;
  2692. if (CHARSET_RANGE_TABLE_EXISTS_P (&p[-2])
  2693. && match_any_multibyte_characters == false)
  2694. {
  2695. /* Set fastmap[I] 1 where I is a base leading code of each
  2696. multibyte character in the range table. */
  2697. int c, count;
  2698. /* Make P points the range table. */
  2699. p += CHARSET_BITMAP_SIZE (&p[-2]);
  2700. /* Extract the number of ranges in range table into
  2701. COUNT. */
  2702. EXTRACT_NUMBER_AND_INCR (count, p);
  2703. for (; count > 0; count--, p += 2 * 3) /* XXX */
  2704. {
  2705. /* Extract the start of each range. */
  2706. EXTRACT_CHARACTER (c, p);
  2707. j = CHAR_CHARSET (c);
  2708. fastmap[CHARSET_LEADING_CODE_BASE (j)] = 1;
  2709. }
  2710. }
  2711. break;
  2712. case charset_not:
  2713. /* Chars beyond end of bitmap are possible matches.
  2714. All the single-byte codes can occur in multibyte buffers.
  2715. So any that are not listed in the charset
  2716. are possible matches, even in multibyte buffers. */
  2717. simple_char_max = (1 << BYTEWIDTH);
  2718. for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH;
  2719. j < simple_char_max; j++)
  2720. fastmap[j] = 1;
  2721. for (j = CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH - 1, p++;
  2722. j >= 0; j--)
  2723. if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2724. fastmap[j] = 1;
  2725. if (bufp->multibyte)
  2726. /* Any character set can possibly contain a character
  2727. which doesn't match the specified set of characters. */
  2728. {
  2729. set_fastmap_for_multibyte_characters:
  2730. if (match_any_multibyte_characters == false)
  2731. {
  2732. for (j = 0x80; j < 0xA0; j++) /* XXX */
  2733. if (BASE_LEADING_CODE_P (j))
  2734. fastmap[j] = 1;
  2735. match_any_multibyte_characters = true;
  2736. }
  2737. }
  2738. break;
  2739. case wordchar:
  2740. /* All the single-byte codes can occur in multibyte buffers,
  2741. and they may have word syntax. So do consider them. */
  2742. simple_char_max = (1 << BYTEWIDTH);
  2743. for (j = 0; j < simple_char_max; j++)
  2744. if (SYNTAX (j) == Sword)
  2745. fastmap[j] = 1;
  2746. if (bufp->multibyte)
  2747. /* Any character set can possibly contain a character
  2748. whose syntax is `Sword'. */
  2749. goto set_fastmap_for_multibyte_characters;
  2750. break;
  2751. case notwordchar:
  2752. /* All the single-byte codes can occur in multibyte buffers,
  2753. and they may not have word syntax. So do consider them. */
  2754. simple_char_max = (1 << BYTEWIDTH);
  2755. for (j = 0; j < simple_char_max; j++)
  2756. if (SYNTAX (j) != Sword)
  2757. fastmap[j] = 1;
  2758. if (bufp->multibyte)
  2759. /* Any character set can possibly contain a character
  2760. whose syntax is not `Sword'. */
  2761. goto set_fastmap_for_multibyte_characters;
  2762. break;
  2763. #endif
  2764. case anychar:
  2765. {
  2766. int fastmap_newline = fastmap['\n'];
  2767. /* `.' matches anything, except perhaps newline.
  2768. Even in a multibyte buffer, it should match any
  2769. conceivable byte value for the fastmap. */
  2770. if (bufp->multibyte)
  2771. match_any_multibyte_characters = true;
  2772. simple_char_max = (1 << BYTEWIDTH);
  2773. for (j = 0; j < simple_char_max; j++)
  2774. fastmap[j] = 1;
  2775. /* ... except perhaps newline. */
  2776. if (!(bufp->syntax & RE_DOT_NEWLINE))
  2777. fastmap['\n'] = fastmap_newline;
  2778. /* Return if we have already set `can_be_null'; if we have,
  2779. then the fastmap is irrelevant. Something's wrong here. */
  2780. else if (bufp->can_be_null)
  2781. goto done;
  2782. /* Otherwise, have to check alternative paths. */
  2783. break;
  2784. }
  2785. #ifdef emacs
  2786. case wordbound:
  2787. case notwordbound:
  2788. case wordbeg:
  2789. case wordend:
  2790. case notsyntaxspec:
  2791. case syntaxspec:
  2792. /* This match depends on text properties. These end with
  2793. aborting optimizations. */
  2794. bufp->can_be_null = 1;
  2795. goto done;
  2796. #if 0
  2797. k = *p++;
  2798. simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
  2799. for (j = 0; j < simple_char_max; j++)
  2800. if (SYNTAX (j) == (enum syntaxcode) k)
  2801. fastmap[j] = 1;
  2802. if (bufp->multibyte)
  2803. /* Any character set can possibly contain a character
  2804. whose syntax is K. */
  2805. goto set_fastmap_for_multibyte_characters;
  2806. break;
  2807. case notsyntaxspec:
  2808. k = *p++;
  2809. simple_char_max = bufp->multibyte ? 0x80 : (1 << BYTEWIDTH);
  2810. for (j = 0; j < simple_char_max; j++)
  2811. if (SYNTAX (j) != (enum syntaxcode) k)
  2812. fastmap[j] = 1;
  2813. if (bufp->multibyte)
  2814. /* Any character set can possibly contain a character
  2815. whose syntax is not K. */
  2816. goto set_fastmap_for_multibyte_characters;
  2817. break;
  2818. #endif
  2819. case categoryspec:
  2820. k = *p++;
  2821. simple_char_max = (1 << BYTEWIDTH);
  2822. for (j = 0; j < simple_char_max; j++)
  2823. if (CHAR_HAS_CATEGORY (j, k))
  2824. fastmap[j] = 1;
  2825. if (bufp->multibyte)
  2826. /* Any character set can possibly contain a character
  2827. whose category is K. */
  2828. goto set_fastmap_for_multibyte_characters;
  2829. break;
  2830. case notcategoryspec:
  2831. k = *p++;
  2832. simple_char_max = (1 << BYTEWIDTH);
  2833. for (j = 0; j < simple_char_max; j++)
  2834. if (!CHAR_HAS_CATEGORY (j, k))
  2835. fastmap[j] = 1;
  2836. if (bufp->multibyte)
  2837. /* Any character set can possibly contain a character
  2838. whose category is not K. */
  2839. goto set_fastmap_for_multibyte_characters;
  2840. break;
  2841. /* All cases after this match the empty string. These end with
  2842. `continue'. */
  2843. case before_dot:
  2844. case at_dot:
  2845. case after_dot:
  2846. continue;
  2847. #endif /* emacs */
  2848. case no_op:
  2849. case begline:
  2850. case endline:
  2851. case begbuf:
  2852. case endbuf:
  2853. #ifndef emacs
  2854. case wordbound:
  2855. case notwordbound:
  2856. case wordbeg:
  2857. case wordend:
  2858. #endif
  2859. case push_dummy_failure:
  2860. continue;
  2861. case jump_n:
  2862. case pop_failure_jump:
  2863. case maybe_pop_jump:
  2864. case jump:
  2865. case jump_past_alt:
  2866. case dummy_failure_jump:
  2867. EXTRACT_NUMBER_AND_INCR (j, p);
  2868. p += j;
  2869. if (j > 0)
  2870. continue;
  2871. /* Jump backward implies we just went through the body of a
  2872. loop and matched nothing. Opcode jumped to should be
  2873. `on_failure_jump' or `succeed_n'. Just treat it like an
  2874. ordinary jump. For a * loop, it has pushed its failure
  2875. point already; if so, discard that as redundant. */
  2876. if ((re_opcode_t) *p != on_failure_jump
  2877. && (re_opcode_t) *p != succeed_n)
  2878. continue;
  2879. p++;
  2880. EXTRACT_NUMBER_AND_INCR (j, p);
  2881. p += j;
  2882. /* If what's on the stack is where we are now, pop it. */
  2883. if (!FAIL_STACK_EMPTY ()
  2884. && fail_stack.stack[fail_stack.avail - 1].pointer == p)
  2885. fail_stack.avail--;
  2886. continue;
  2887. case on_failure_jump:
  2888. case on_failure_keep_string_jump:
  2889. handle_on_failure_jump:
  2890. EXTRACT_NUMBER_AND_INCR (j, p);
  2891. /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  2892. end of the pattern. We don't want to push such a point,
  2893. since when we restore it above, entering the switch will
  2894. increment `p' past the end of the pattern. We don't need
  2895. to push such a point since we obviously won't find any more
  2896. fastmap entries beyond `pend'. Such a pattern can match
  2897. the null string, though. */
  2898. if (p + j < pend)
  2899. {
  2900. if (!PUSH_PATTERN_OP (p + j, fail_stack))
  2901. {
  2902. RESET_FAIL_STACK ();
  2903. return -2;
  2904. }
  2905. }
  2906. else
  2907. bufp->can_be_null = 1;
  2908. if (succeed_n_p)
  2909. {
  2910. EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
  2911. succeed_n_p = false;
  2912. }
  2913. continue;
  2914. case succeed_n:
  2915. /* Get to the number of times to succeed. */
  2916. p += 2;
  2917. /* Increment p past the n for when k != 0. */
  2918. EXTRACT_NUMBER_AND_INCR (k, p);
  2919. if (k == 0)
  2920. {
  2921. p -= 4;
  2922. succeed_n_p = true; /* Spaghetti code alert. */
  2923. goto handle_on_failure_jump;
  2924. }
  2925. continue;
  2926. case set_number_at:
  2927. p += 4;
  2928. continue;
  2929. case start_memory:
  2930. case stop_memory:
  2931. p += 2;
  2932. continue;
  2933. default:
  2934. abort (); /* We have listed all the cases. */
  2935. } /* switch *p++ */
  2936. /* Getting here means we have found the possible starting
  2937. characters for one path of the pattern -- and that the empty
  2938. string does not match. We need not follow this path further.
  2939. Instead, look at the next alternative (remembered on the
  2940. stack), or quit if no more. The test at the top of the loop
  2941. does these things. */
  2942. path_can_be_null = false;
  2943. p = pend;
  2944. } /* while p */
  2945. /* Set `can_be_null' for the last path (also the first path, if the
  2946. pattern is empty). */
  2947. bufp->can_be_null |= path_can_be_null;
  2948. done:
  2949. RESET_FAIL_STACK ();
  2950. return 0;
  2951. } /* re_compile_fastmap */
  2952. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  2953. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  2954. this memory for recording register information. STARTS and ENDS
  2955. must be allocated using the malloc library routine, and must each
  2956. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  2957. If NUM_REGS == 0, then subsequent matches should allocate their own
  2958. register data.
  2959. Unless this function is called, the first search or match using
  2960. PATTERN_BUFFER will allocate its own register data, without
  2961. freeing the old data. */
  2962. void
  2963. re_set_registers (bufp, regs, num_regs, starts, ends)
  2964. struct re_pattern_buffer *bufp;
  2965. struct re_registers *regs;
  2966. unsigned num_regs;
  2967. regoff_t *starts, *ends;
  2968. {
  2969. if (num_regs)
  2970. {
  2971. bufp->regs_allocated = REGS_REALLOCATE;
  2972. regs->num_regs = num_regs;
  2973. regs->start = starts;
  2974. regs->end = ends;
  2975. }
  2976. else
  2977. {
  2978. bufp->regs_allocated = REGS_UNALLOCATED;
  2979. regs->num_regs = 0;
  2980. regs->start = regs->end = (regoff_t *) 0;
  2981. }
  2982. }
  2983. /* Searching routines. */
  2984. /* Like re_search_2, below, but only one string is specified, and
  2985. doesn't let you say where to stop matching. */
  2986. int
  2987. re_search (bufp, string, size, startpos, range, regs)
  2988. struct re_pattern_buffer *bufp;
  2989. const char *string;
  2990. int size, startpos, range;
  2991. struct re_registers *regs;
  2992. {
  2993. return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
  2994. regs, size);
  2995. }
  2996. /* End address of virtual concatenation of string. */
  2997. #define STOP_ADDR_VSTRING(P) \
  2998. (((P) >= size1 ? string2 + size2 : string1 + size1))
  2999. /* Address of POS in the concatenation of virtual string. */
  3000. #define POS_ADDR_VSTRING(POS) \
  3001. (((POS) >= size1 ? string2 - size1 : string1) + (POS))
  3002. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  3003. virtual concatenation of STRING1 and STRING2, starting first at index
  3004. STARTPOS, then at STARTPOS + 1, and so on.
  3005. STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  3006. RANGE is how far to scan while trying to match. RANGE = 0 means try
  3007. only at STARTPOS; in general, the last start tried is STARTPOS +
  3008. RANGE.
  3009. In REGS, return the indices of the virtual concatenation of STRING1
  3010. and STRING2 that matched the entire BUFP->buffer and its contained
  3011. subexpressions.
  3012. Do not consider matching one past the index STOP in the virtual
  3013. concatenation of STRING1 and STRING2.
  3014. We return either the position in the strings at which the match was
  3015. found, -1 if no match, or -2 if error (such as failure
  3016. stack overflow). */
  3017. int
  3018. re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
  3019. struct re_pattern_buffer *bufp;
  3020. const char *string1, *string2;
  3021. int size1, size2;
  3022. int startpos;
  3023. int range;
  3024. struct re_registers *regs;
  3025. int stop;
  3026. {
  3027. int val;
  3028. register char *fastmap = bufp->fastmap;
  3029. register RE_TRANSLATE_TYPE translate = bufp->translate;
  3030. int total_size = size1 + size2;
  3031. int endpos = startpos + range;
  3032. int anchored_start = 0;
  3033. /* Nonzero if we have to concern multibyte character. */
  3034. int multibyte = bufp->multibyte;
  3035. /* Check for out-of-range STARTPOS. */
  3036. if (startpos < 0 || startpos > total_size)
  3037. return -1;
  3038. /* Fix up RANGE if it might eventually take us outside
  3039. the virtual concatenation of STRING1 and STRING2.
  3040. Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
  3041. if (endpos < 0)
  3042. range = 0 - startpos;
  3043. else if (endpos > total_size)
  3044. range = total_size - startpos;
  3045. /* If the search isn't to be a backwards one, don't waste time in a
  3046. search for a pattern anchored at beginning of buffer. */
  3047. if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
  3048. {
  3049. if (startpos > 0)
  3050. return -1;
  3051. else
  3052. range = 0;
  3053. }
  3054. #ifdef emacs
  3055. /* In a forward search for something that starts with \=.
  3056. don't keep searching past point. */
  3057. if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
  3058. {
  3059. range = PT_BYTE - BEGV_BYTE - startpos;
  3060. if (range < 0)
  3061. return -1;
  3062. }
  3063. #endif /* emacs */
  3064. /* Update the fastmap now if not correct already. */
  3065. if (fastmap && !bufp->fastmap_accurate)
  3066. if (re_compile_fastmap (bufp) == -2)
  3067. return -2;
  3068. /* See whether the pattern is anchored. */
  3069. if (bufp->buffer[0] == begline)
  3070. anchored_start = 1;
  3071. #ifdef emacs
  3072. gl_state.object = re_match_object;
  3073. {
  3074. int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
  3075. int charpos = SYNTAX_TABLE_BYTE_TO_CHAR (startpos + adjpos);
  3076. SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
  3077. }
  3078. #endif
  3079. /* Loop through the string, looking for a place to start matching. */
  3080. for (;;)
  3081. {
  3082. /* If the pattern is anchored,
  3083. skip quickly past places we cannot match.
  3084. We don't bother to treat startpos == 0 specially
  3085. because that case doesn't repeat. */
  3086. if (anchored_start && startpos > 0)
  3087. {
  3088. if (! (bufp->newline_anchor
  3089. && ((startpos <= size1 ? string1[startpos - 1]
  3090. : string2[startpos - size1 - 1])
  3091. == '\n')))
  3092. goto advance;
  3093. }
  3094. /* If a fastmap is supplied, skip quickly over characters that
  3095. cannot be the start of a match. If the pattern can match the
  3096. null string, however, we don't need to skip characters; we want
  3097. the first null string. */
  3098. if (fastmap && startpos < total_size && !bufp->can_be_null)
  3099. {
  3100. register const char *d;
  3101. register unsigned int buf_ch;
  3102. d = POS_ADDR_VSTRING (startpos);
  3103. if (range > 0) /* Searching forwards. */
  3104. {
  3105. register int lim = 0;
  3106. int irange = range;
  3107. if (startpos < size1 && startpos + range >= size1)
  3108. lim = range - (size1 - startpos);
  3109. /* Written out as an if-else to avoid testing `translate'
  3110. inside the loop. */
  3111. if (RE_TRANSLATE_P (translate))
  3112. {
  3113. if (multibyte)
  3114. while (range > lim)
  3115. {
  3116. int buf_charlen;
  3117. buf_ch = STRING_CHAR_AND_LENGTH (d, range - lim,
  3118. buf_charlen);
  3119. buf_ch = RE_TRANSLATE (translate, buf_ch);
  3120. if (buf_ch >= 0400
  3121. || fastmap[buf_ch])
  3122. break;
  3123. range -= buf_charlen;
  3124. d += buf_charlen;
  3125. }
  3126. else
  3127. while (range > lim
  3128. && !fastmap[(unsigned char)
  3129. RE_TRANSLATE (translate, (unsigned char) *d)])
  3130. {
  3131. d++;
  3132. range--;
  3133. }
  3134. }
  3135. else
  3136. while (range > lim && !fastmap[(unsigned char) *d])
  3137. {
  3138. d++;
  3139. range--;
  3140. }
  3141. startpos += irange - range;
  3142. }
  3143. else /* Searching backwards. */
  3144. {
  3145. int room = (size1 == 0 || startpos >= size1
  3146. ? size2 + size1 - startpos
  3147. : size1 - startpos);
  3148. buf_ch = STRING_CHAR (d, room);
  3149. if (RE_TRANSLATE_P (translate))
  3150. buf_ch = RE_TRANSLATE (translate, buf_ch);
  3151. if (! (buf_ch >= 0400
  3152. || fastmap[buf_ch]))
  3153. goto advance;
  3154. }
  3155. }
  3156. /* If can't match the null string, and that's all we have left, fail. */
  3157. if (range >= 0 && startpos == total_size && fastmap
  3158. && !bufp->can_be_null)
  3159. return -1;
  3160. val = re_match_2_internal (bufp, string1, size1, string2, size2,
  3161. startpos, regs, stop);
  3162. #ifndef REGEX_MALLOC
  3163. #ifdef C_ALLOCA
  3164. alloca (0);
  3165. #endif
  3166. #endif
  3167. if (val >= 0)
  3168. return startpos;
  3169. if (val == -2)
  3170. return -2;
  3171. advance:
  3172. if (!range)
  3173. break;
  3174. else if (range > 0)
  3175. {
  3176. /* Update STARTPOS to the next character boundary. */
  3177. if (multibyte)
  3178. {
  3179. const unsigned char *p
  3180. = (const unsigned char *) POS_ADDR_VSTRING (startpos);
  3181. const unsigned char *pend
  3182. = (const unsigned char *) STOP_ADDR_VSTRING (startpos);
  3183. int len = MULTIBYTE_FORM_LENGTH (p, pend - p);
  3184. range -= len;
  3185. if (range < 0)
  3186. break;
  3187. startpos += len;
  3188. }
  3189. else
  3190. {
  3191. range--;
  3192. startpos++;
  3193. }
  3194. }
  3195. else
  3196. {
  3197. range++;
  3198. startpos--;
  3199. /* Update STARTPOS to the previous character boundary. */
  3200. if (multibyte)
  3201. {
  3202. const unsigned char *p
  3203. = (const unsigned char *) POS_ADDR_VSTRING (startpos);
  3204. int len = 0;
  3205. /* Find the head of multibyte form. */
  3206. while (!CHAR_HEAD_P (*p))
  3207. p--, len++;
  3208. /* Adjust it. */
  3209. #if 0 /* XXX */
  3210. if (MULTIBYTE_FORM_LENGTH (p, len + 1) != (len + 1))
  3211. ;
  3212. else
  3213. #endif
  3214. {
  3215. range += len;
  3216. if (range > 0)
  3217. break;
  3218. startpos -= len;
  3219. }
  3220. }
  3221. }
  3222. }
  3223. return -1;
  3224. } /* re_search_2 */
  3225. /* Declarations and macros for re_match_2. */
  3226. static int bcmp_translate ();
  3227. static boolean alt_match_null_string_p (),
  3228. common_op_match_null_string_p (),
  3229. group_match_null_string_p ();
  3230. /* This converts PTR, a pointer into one of the search strings `string1'
  3231. and `string2' into an offset from the beginning of that string. */
  3232. #define POINTER_TO_OFFSET(ptr) \
  3233. (FIRST_STRING_P (ptr) \
  3234. ? ((regoff_t) ((ptr) - string1)) \
  3235. : ((regoff_t) ((ptr) - string2 + size1)))
  3236. /* Macros for dealing with the split strings in re_match_2. */
  3237. #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
  3238. /* Call before fetching a character with *d. This switches over to
  3239. string2 if necessary. */
  3240. #define PREFETCH() \
  3241. while (d == dend) \
  3242. { \
  3243. /* End of string2 => fail. */ \
  3244. if (dend == end_match_2) \
  3245. goto fail; \
  3246. /* End of string1 => advance to string2. */ \
  3247. d = string2; \
  3248. dend = end_match_2; \
  3249. }
  3250. /* Test if at very beginning or at very end of the virtual concatenation
  3251. of `string1' and `string2'. If only one string, it's `string2'. */
  3252. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  3253. #define AT_STRINGS_END(d) ((d) == end2)
  3254. /* Test if D points to a character which is word-constituent. We have
  3255. two special cases to check for: if past the end of string1, look at
  3256. the first character in string2; and if before the beginning of
  3257. string2, look at the last character in string1. */
  3258. #define WORDCHAR_P(d) \
  3259. (SYNTAX ((d) == end1 ? *string2 \
  3260. : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
  3261. == Sword)
  3262. /* Disabled due to a compiler bug -- see comment at case wordbound */
  3263. /* The comment at case wordbound is following one, but we don't use
  3264. AT_WORD_BOUNDARY anymore to support multibyte form.
  3265. The DEC Alpha C compiler 3.x generates incorrect code for the
  3266. test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
  3267. AT_WORD_BOUNDARY, so this code is disabled. Expanding the
  3268. macro and introducing temporary variables works around the bug. */
  3269. #if 0
  3270. /* Test if the character before D and the one at D differ with respect
  3271. to being word-constituent. */
  3272. #define AT_WORD_BOUNDARY(d) \
  3273. (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
  3274. || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  3275. #endif
  3276. /* Free everything we malloc. */
  3277. #ifdef MATCH_MAY_ALLOCATE
  3278. #define FREE_VAR(var) if (var) { REGEX_FREE (var); var = NULL; } else
  3279. #define FREE_VARIABLES() \
  3280. do { \
  3281. REGEX_FREE_STACK (fail_stack.stack); \
  3282. FREE_VAR (regstart); \
  3283. FREE_VAR (regend); \
  3284. FREE_VAR (old_regstart); \
  3285. FREE_VAR (old_regend); \
  3286. FREE_VAR (best_regstart); \
  3287. FREE_VAR (best_regend); \
  3288. FREE_VAR (reg_info); \
  3289. FREE_VAR (reg_dummy); \
  3290. FREE_VAR (reg_info_dummy); \
  3291. } while (0)
  3292. #else
  3293. #define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
  3294. #endif /* not MATCH_MAY_ALLOCATE */
  3295. /* These values must meet several constraints. They must not be valid
  3296. register values; since we have a limit of 255 registers (because
  3297. we use only one byte in the pattern for the register number), we can
  3298. use numbers larger than 255. They must differ by 1, because of
  3299. NUM_FAILURE_ITEMS above. And the value for the lowest register must
  3300. be larger than the value for the highest register, so we do not try
  3301. to actually save any registers when none are active. */
  3302. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  3303. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  3304. /* Matching routines. */
  3305. #ifndef emacs /* Emacs never uses this. */
  3306. /* re_match is like re_match_2 except it takes only a single string. */
  3307. int
  3308. re_match (bufp, string, size, pos, regs)
  3309. struct re_pattern_buffer *bufp;
  3310. const char *string;
  3311. int size, pos;
  3312. struct re_registers *regs;
  3313. {
  3314. int result = re_match_2_internal (bufp, NULL, 0, string, size,
  3315. pos, regs, size);
  3316. #ifndef REGEX_MALLOC /* CVS */
  3317. #ifdef C_ALLOCA /* CVS */
  3318. alloca (0);
  3319. #endif /* CVS */
  3320. #endif /* CVS */
  3321. return result;
  3322. }
  3323. #endif /* not emacs */
  3324. #ifdef emacs
  3325. /* In Emacs, this is the string or buffer in which we
  3326. are matching. It is used for looking up syntax properties. */
  3327. Lisp_Object re_match_object;
  3328. #endif
  3329. /* re_match_2 matches the compiled pattern in BUFP against the
  3330. the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  3331. and SIZE2, respectively). We start matching at POS, and stop
  3332. matching at STOP.
  3333. If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  3334. store offsets for the substring each group matched in REGS. See the
  3335. documentation for exactly how many groups we fill.
  3336. We return -1 if no match, -2 if an internal error (such as the
  3337. failure stack overflowing). Otherwise, we return the length of the
  3338. matched substring. */
  3339. int
  3340. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  3341. struct re_pattern_buffer *bufp;
  3342. const char *string1, *string2;
  3343. int size1, size2;
  3344. int pos;
  3345. struct re_registers *regs;
  3346. int stop;
  3347. {
  3348. int result;
  3349. #ifdef emacs
  3350. int charpos;
  3351. int adjpos = NILP (re_match_object) || BUFFERP (re_match_object);
  3352. gl_state.object = re_match_object;
  3353. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos + adjpos);
  3354. SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, charpos, 1);
  3355. #endif
  3356. result = re_match_2_internal (bufp, string1, size1, string2, size2,
  3357. pos, regs, stop);
  3358. #ifndef REGEX_MALLOC /* CVS */
  3359. #ifdef C_ALLOCA /* CVS */
  3360. alloca (0);
  3361. #endif /* CVS */
  3362. #endif /* CVS */
  3363. return result;
  3364. }
  3365. /* This is a separate function so that we can force an alloca cleanup
  3366. afterwards. */
  3367. static int
  3368. re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
  3369. struct re_pattern_buffer *bufp;
  3370. const char *string1, *string2;
  3371. int size1, size2;
  3372. int pos;
  3373. struct re_registers *regs;
  3374. int stop;
  3375. {
  3376. /* General temporaries. */
  3377. int mcnt;
  3378. unsigned char *p1;
  3379. /* Just past the end of the corresponding string. */
  3380. const char *end1, *end2;
  3381. /* Pointers into string1 and string2, just past the last characters in
  3382. each to consider matching. */
  3383. const char *end_match_1, *end_match_2;
  3384. /* Where we are in the data, and the end of the current string. */
  3385. const char *d, *dend;
  3386. /* Where we are in the pattern, and the end of the pattern. */
  3387. unsigned char *p = bufp->buffer;
  3388. register unsigned char *pend = p + bufp->used;
  3389. /* Mark the opcode just after a start_memory, so we can test for an
  3390. empty subpattern when we get to the stop_memory. */
  3391. unsigned char *just_past_start_mem = 0;
  3392. /* We use this to map every character in the string. */
  3393. RE_TRANSLATE_TYPE translate = bufp->translate;
  3394. /* Nonzero if we have to concern multibyte character. */
  3395. int multibyte = bufp->multibyte;
  3396. /* Failure point stack. Each place that can handle a failure further
  3397. down the line pushes a failure point on this stack. It consists of
  3398. restart, regend, and reg_info for all registers corresponding to
  3399. the subexpressions we're currently inside, plus the number of such
  3400. registers, and, finally, two char *'s. The first char * is where
  3401. to resume scanning the pattern; the second one is where to resume
  3402. scanning the strings. If the latter is zero, the failure point is
  3403. a ``dummy''; if a failure happens and the failure point is a dummy,
  3404. it gets discarded and the next next one is tried. */
  3405. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  3406. fail_stack_type fail_stack;
  3407. #endif
  3408. #ifdef DEBUG
  3409. static unsigned failure_id = 0;
  3410. unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  3411. #endif
  3412. /* This holds the pointer to the failure stack, when
  3413. it is allocated relocatably. */
  3414. fail_stack_elt_t *failure_stack_ptr;
  3415. /* We fill all the registers internally, independent of what we
  3416. return, for use in backreferences. The number here includes
  3417. an element for register zero. */
  3418. unsigned num_regs = bufp->re_nsub + 1;
  3419. /* The currently active registers. */
  3420. unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3421. unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3422. /* Information on the contents of registers. These are pointers into
  3423. the input strings; they record just what was matched (on this
  3424. attempt) by a subexpression part of the pattern, that is, the
  3425. regnum-th regstart pointer points to where in the pattern we began
  3426. matching and the regnum-th regend points to right after where we
  3427. stopped matching the regnum-th subexpression. (The zeroth register
  3428. keeps track of what the whole pattern matches.) */
  3429. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3430. const char **regstart, **regend;
  3431. #endif
  3432. /* If a group that's operated upon by a repetition operator fails to
  3433. match anything, then the register for its start will need to be
  3434. restored because it will have been set to wherever in the string we
  3435. are when we last see its open-group operator. Similarly for a
  3436. register's end. */
  3437. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3438. const char **old_regstart, **old_regend;
  3439. #endif
  3440. /* The is_active field of reg_info helps us keep track of which (possibly
  3441. nested) subexpressions we are currently in. The matched_something
  3442. field of reg_info[reg_num] helps us tell whether or not we have
  3443. matched any of the pattern so far this time through the reg_num-th
  3444. subexpression. These two fields get reset each time through any
  3445. loop their register is in. */
  3446. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  3447. register_info_type *reg_info;
  3448. #endif
  3449. /* The following record the register info as found in the above
  3450. variables when we find a match better than any we've seen before.
  3451. This happens as we backtrack through the failure points, which in
  3452. turn happens only if we have not yet matched the entire string. */
  3453. unsigned best_regs_set = false;
  3454. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3455. const char **best_regstart, **best_regend;
  3456. #endif
  3457. /* Logically, this is `best_regend[0]'. But we don't want to have to
  3458. allocate space for that if we're not allocating space for anything
  3459. else (see below). Also, we never need info about register 0 for
  3460. any of the other register vectors, and it seems rather a kludge to
  3461. treat `best_regend' differently than the rest. So we keep track of
  3462. the end of the best match so far in a separate variable. We
  3463. initialize this to NULL so that when we backtrack the first time
  3464. and need to test it, it's not garbage. */
  3465. const char *match_end = NULL;
  3466. /* This helps SET_REGS_MATCHED avoid doing redundant work. */
  3467. int set_regs_matched_done = 0;
  3468. /* Used when we pop values we don't care about. */
  3469. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3470. const char **reg_dummy;
  3471. register_info_type *reg_info_dummy;
  3472. #endif
  3473. #ifdef DEBUG
  3474. /* Counts the total number of registers pushed. */
  3475. unsigned num_regs_pushed = 0;
  3476. #endif
  3477. DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3478. INIT_FAIL_STACK ();
  3479. #ifdef MATCH_MAY_ALLOCATE
  3480. /* Do not bother to initialize all the register variables if there are
  3481. no groups in the pattern, as it takes a fair amount of time. If
  3482. there are groups, we include space for register 0 (the whole
  3483. pattern), even though we never use it, since it simplifies the
  3484. array indexing. We should fix this. */
  3485. if (bufp->re_nsub)
  3486. {
  3487. regstart = REGEX_TALLOC (num_regs, const char *);
  3488. regend = REGEX_TALLOC (num_regs, const char *);
  3489. old_regstart = REGEX_TALLOC (num_regs, const char *);
  3490. old_regend = REGEX_TALLOC (num_regs, const char *);
  3491. best_regstart = REGEX_TALLOC (num_regs, const char *);
  3492. best_regend = REGEX_TALLOC (num_regs, const char *);
  3493. reg_info = REGEX_TALLOC (num_regs, register_info_type);
  3494. reg_dummy = REGEX_TALLOC (num_regs, const char *);
  3495. reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
  3496. if (!(regstart && regend && old_regstart && old_regend && reg_info
  3497. && best_regstart && best_regend && reg_dummy && reg_info_dummy))
  3498. {
  3499. FREE_VARIABLES ();
  3500. return -2;
  3501. }
  3502. }
  3503. else
  3504. {
  3505. /* We must initialize all our variables to NULL, so that
  3506. `FREE_VARIABLES' doesn't try to free them. */
  3507. regstart = regend = old_regstart = old_regend = best_regstart
  3508. = best_regend = reg_dummy = NULL;
  3509. reg_info = reg_info_dummy = (register_info_type *) NULL;
  3510. }
  3511. #endif /* MATCH_MAY_ALLOCATE */
  3512. /* The starting position is bogus. */
  3513. if (pos < 0 || pos > size1 + size2)
  3514. {
  3515. FREE_VARIABLES ();
  3516. return -1;
  3517. }
  3518. /* Initialize subexpression text positions to -1 to mark ones that no
  3519. start_memory/stop_memory has been seen for. Also initialize the
  3520. register information struct. */
  3521. for (mcnt = 1; mcnt < num_regs; mcnt++)
  3522. {
  3523. regstart[mcnt] = regend[mcnt]
  3524. = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3525. REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  3526. IS_ACTIVE (reg_info[mcnt]) = 0;
  3527. MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3528. EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3529. }
  3530. /* We move `string1' into `string2' if the latter's empty -- but not if
  3531. `string1' is null. */
  3532. if (size2 == 0 && string1 != NULL)
  3533. {
  3534. string2 = string1;
  3535. size2 = size1;
  3536. string1 = 0;
  3537. size1 = 0;
  3538. }
  3539. end1 = string1 + size1;
  3540. end2 = string2 + size2;
  3541. /* Compute where to stop matching, within the two strings. */
  3542. if (stop <= size1)
  3543. {
  3544. end_match_1 = string1 + stop;
  3545. end_match_2 = string2;
  3546. }
  3547. else
  3548. {
  3549. end_match_1 = end1;
  3550. end_match_2 = string2 + stop - size1;
  3551. }
  3552. /* `p' scans through the pattern as `d' scans through the data.
  3553. `dend' is the end of the input string that `d' points within. `d'
  3554. is advanced into the following input string whenever necessary, but
  3555. this happens before fetching; therefore, at the beginning of the
  3556. loop, `d' can be pointing at the end of a string, but it cannot
  3557. equal `string2'. */
  3558. if (size1 > 0 && pos <= size1)
  3559. {
  3560. d = string1 + pos;
  3561. dend = end_match_1;
  3562. }
  3563. else
  3564. {
  3565. d = string2 + pos - size1;
  3566. dend = end_match_2;
  3567. }
  3568. DEBUG_PRINT1 ("The compiled pattern is: ");
  3569. DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  3570. DEBUG_PRINT1 ("The string to match is: `");
  3571. DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  3572. DEBUG_PRINT1 ("'\n");
  3573. /* This loops over pattern commands. It exits by returning from the
  3574. function if the match is complete, or it drops through if the match
  3575. fails at this starting point in the input data. */
  3576. for (;;)
  3577. {
  3578. DEBUG_PRINT2 ("\n0x%x: ", p);
  3579. if (p == pend)
  3580. { /* End of pattern means we might have succeeded. */
  3581. DEBUG_PRINT1 ("end of pattern ... ");
  3582. /* If we haven't matched the entire string, and we want the
  3583. longest match, try backtracking. */
  3584. if (d != end_match_2)
  3585. {
  3586. /* 1 if this match ends in the same string (string1 or string2)
  3587. as the best previous match. */
  3588. boolean same_str_p = (FIRST_STRING_P (match_end)
  3589. == MATCHING_IN_FIRST_STRING);
  3590. /* 1 if this match is the best seen so far. */
  3591. boolean best_match_p;
  3592. /* AIX compiler got confused when this was combined
  3593. with the previous declaration. */
  3594. if (same_str_p)
  3595. best_match_p = d > match_end;
  3596. else
  3597. best_match_p = !MATCHING_IN_FIRST_STRING;
  3598. DEBUG_PRINT1 ("backtracking.\n");
  3599. if (!FAIL_STACK_EMPTY ())
  3600. { /* More failure points to try. */
  3601. /* If exceeds best match so far, save it. */
  3602. if (!best_regs_set || best_match_p)
  3603. {
  3604. best_regs_set = true;
  3605. match_end = d;
  3606. DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  3607. for (mcnt = 1; mcnt < num_regs; mcnt++)
  3608. {
  3609. best_regstart[mcnt] = regstart[mcnt];
  3610. best_regend[mcnt] = regend[mcnt];
  3611. }
  3612. }
  3613. goto fail;
  3614. }
  3615. /* If no failure points, don't restore garbage. And if
  3616. last match is real best match, don't restore second
  3617. best one. */
  3618. else if (best_regs_set && !best_match_p)
  3619. {
  3620. restore_best_regs:
  3621. /* Restore best match. It may happen that `dend ==
  3622. end_match_1' while the restored d is in string2.
  3623. For example, the pattern `x.*y.*z' against the
  3624. strings `x-' and `y-z-', if the two strings are
  3625. not consecutive in memory. */
  3626. DEBUG_PRINT1 ("Restoring best registers.\n");
  3627. d = match_end;
  3628. dend = ((d >= string1 && d <= end1)
  3629. ? end_match_1 : end_match_2);
  3630. for (mcnt = 1; mcnt < num_regs; mcnt++)
  3631. {
  3632. regstart[mcnt] = best_regstart[mcnt];
  3633. regend[mcnt] = best_regend[mcnt];
  3634. }
  3635. }
  3636. } /* d != end_match_2 */
  3637. succeed_label:
  3638. DEBUG_PRINT1 ("Accepting match.\n");
  3639. /* If caller wants register contents data back, do it. */
  3640. if (regs && !bufp->no_sub)
  3641. {
  3642. /* Have the register data arrays been allocated? */
  3643. if (bufp->regs_allocated == REGS_UNALLOCATED)
  3644. { /* No. So allocate them with malloc. We need one
  3645. extra element beyond `num_regs' for the `-1' marker
  3646. GNU code uses. */
  3647. regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3648. regs->start = TALLOC (regs->num_regs, regoff_t);
  3649. regs->end = TALLOC (regs->num_regs, regoff_t);
  3650. if (regs->start == NULL || regs->end == NULL)
  3651. {
  3652. FREE_VARIABLES ();
  3653. return -2;
  3654. }
  3655. bufp->regs_allocated = REGS_REALLOCATE;
  3656. }
  3657. else if (bufp->regs_allocated == REGS_REALLOCATE)
  3658. { /* Yes. If we need more elements than were already
  3659. allocated, reallocate them. If we need fewer, just
  3660. leave it alone. */
  3661. if (regs->num_regs < num_regs + 1)
  3662. {
  3663. regs->num_regs = num_regs + 1;
  3664. RETALLOC (regs->start, regs->num_regs, regoff_t);
  3665. RETALLOC (regs->end, regs->num_regs, regoff_t);
  3666. if (regs->start == NULL || regs->end == NULL)
  3667. {
  3668. FREE_VARIABLES ();
  3669. return -2;
  3670. }
  3671. }
  3672. }
  3673. else
  3674. {
  3675. /* These braces fend off a "empty body in an else-statement"
  3676. warning under GCC when assert expands to nothing. */
  3677. assert (bufp->regs_allocated == REGS_FIXED);
  3678. }
  3679. /* Convert the pointer data in `regstart' and `regend' to
  3680. indices. Register zero has to be set differently,
  3681. since we haven't kept track of any info for it. */
  3682. if (regs->num_regs > 0)
  3683. {
  3684. regs->start[0] = pos;
  3685. regs->end[0] = (MATCHING_IN_FIRST_STRING
  3686. ? ((regoff_t) (d - string1))
  3687. : ((regoff_t) (d - string2 + size1)));
  3688. }
  3689. /* Go through the first `min (num_regs, regs->num_regs)'
  3690. registers, since that is all we initialized. */
  3691. for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
  3692. {
  3693. if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  3694. regs->start[mcnt] = regs->end[mcnt] = -1;
  3695. else
  3696. {
  3697. regs->start[mcnt]
  3698. = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
  3699. regs->end[mcnt]
  3700. = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
  3701. }
  3702. }
  3703. /* If the regs structure we return has more elements than
  3704. were in the pattern, set the extra elements to -1. If
  3705. we (re)allocated the registers, this is the case,
  3706. because we always allocate enough to have at least one
  3707. -1 at the end. */
  3708. for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
  3709. regs->start[mcnt] = regs->end[mcnt] = -1;
  3710. } /* regs && !bufp->no_sub */
  3711. DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  3712. nfailure_points_pushed, nfailure_points_popped,
  3713. nfailure_points_pushed - nfailure_points_popped);
  3714. DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  3715. mcnt = d - pos - (MATCHING_IN_FIRST_STRING
  3716. ? string1
  3717. : string2 - size1);
  3718. DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  3719. FREE_VARIABLES ();
  3720. return mcnt;
  3721. }
  3722. /* Otherwise match next pattern command. */
  3723. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  3724. {
  3725. /* Ignore these. Used to ignore the n of succeed_n's which
  3726. currently have n == 0. */
  3727. case no_op:
  3728. DEBUG_PRINT1 ("EXECUTING no_op.\n");
  3729. break;
  3730. case succeed:
  3731. DEBUG_PRINT1 ("EXECUTING succeed.\n");
  3732. goto succeed_label;
  3733. /* Match the next n pattern characters exactly. The following
  3734. byte in the pattern defines n, and the n bytes after that
  3735. are the characters to match. */
  3736. case exactn:
  3737. mcnt = *p++;
  3738. DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  3739. /* This is written out as an if-else so we don't waste time
  3740. testing `translate' inside the loop. */
  3741. if (RE_TRANSLATE_P (translate))
  3742. {
  3743. #ifdef emacs
  3744. if (multibyte)
  3745. do
  3746. {
  3747. int pat_charlen, buf_charlen;
  3748. unsigned int pat_ch, buf_ch;
  3749. PREFETCH ();
  3750. pat_ch = STRING_CHAR_AND_LENGTH (p, pend - p, pat_charlen);
  3751. buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
  3752. if (RE_TRANSLATE (translate, buf_ch)
  3753. != pat_ch)
  3754. goto fail;
  3755. p += pat_charlen;
  3756. d += buf_charlen;
  3757. mcnt -= pat_charlen;
  3758. }
  3759. while (mcnt > 0);
  3760. else
  3761. #endif /* not emacs */
  3762. do
  3763. {
  3764. PREFETCH ();
  3765. if ((unsigned char) RE_TRANSLATE (translate, (unsigned char) *d)
  3766. != (unsigned char) *p++)
  3767. goto fail;
  3768. d++;
  3769. }
  3770. while (--mcnt);
  3771. }
  3772. else
  3773. {
  3774. do
  3775. {
  3776. PREFETCH ();
  3777. if (*d++ != (char) *p++) goto fail;
  3778. }
  3779. while (--mcnt);
  3780. }
  3781. SET_REGS_MATCHED ();
  3782. break;
  3783. /* Match any character except possibly a newline or a null. */
  3784. case anychar:
  3785. {
  3786. int buf_charlen;
  3787. unsigned int buf_ch;
  3788. DEBUG_PRINT1 ("EXECUTING anychar.\n");
  3789. PREFETCH ();
  3790. #ifdef emacs
  3791. if (multibyte)
  3792. buf_ch = STRING_CHAR_AND_LENGTH (d, dend - d, buf_charlen);
  3793. else
  3794. #endif /* not emacs */
  3795. {
  3796. buf_ch = (unsigned char) *d;
  3797. buf_charlen = 1;
  3798. }
  3799. buf_ch = TRANSLATE (buf_ch);
  3800. if ((!(bufp->syntax & RE_DOT_NEWLINE)
  3801. && buf_ch == '\n')
  3802. || ((bufp->syntax & RE_DOT_NOT_NULL)
  3803. && buf_ch == '\000'))
  3804. goto fail;
  3805. SET_REGS_MATCHED ();
  3806. DEBUG_PRINT2 (" Matched `%d'.\n", *d);
  3807. d += buf_charlen;
  3808. }
  3809. break;
  3810. case charset:
  3811. case charset_not:
  3812. {
  3813. register unsigned int c;
  3814. boolean not = (re_opcode_t) *(p - 1) == charset_not;
  3815. int len;
  3816. /* Start of actual range_table, or end of bitmap if there is no
  3817. range table. */
  3818. unsigned char *range_table;
  3819. /* Nonzero if there is range table. */
  3820. int range_table_exists;
  3821. /* Number of ranges of range table. Not in bytes. */
  3822. int count;
  3823. DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  3824. PREFETCH ();
  3825. c = (unsigned char) *d;
  3826. range_table = CHARSET_RANGE_TABLE (&p[-1]); /* Past the bitmap. */
  3827. range_table_exists = CHARSET_RANGE_TABLE_EXISTS_P (&p[-1]);
  3828. if (range_table_exists)
  3829. EXTRACT_NUMBER_AND_INCR (count, range_table);
  3830. else
  3831. count = 0;
  3832. if (multibyte && BASE_LEADING_CODE_P (c))
  3833. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  3834. if (SINGLE_BYTE_CHAR_P (c))
  3835. { /* Lookup bitmap. */
  3836. c = TRANSLATE (c); /* The character to match. */
  3837. len = 1;
  3838. /* Cast to `unsigned' instead of `unsigned char' in
  3839. case the bit list is a full 32 bytes long. */
  3840. if (c < (unsigned) (CHARSET_BITMAP_SIZE (&p[-1]) * BYTEWIDTH)
  3841. && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3842. not = !not;
  3843. }
  3844. else if (range_table_exists)
  3845. CHARSET_LOOKUP_RANGE_TABLE_RAW (not, c, range_table, count);
  3846. p = CHARSET_RANGE_TABLE_END (range_table, count);
  3847. if (!not) goto fail;
  3848. SET_REGS_MATCHED ();
  3849. d += len;
  3850. break;
  3851. }
  3852. /* The beginning of a group is represented by start_memory.
  3853. The arguments are the register number in the next byte, and the
  3854. number of groups inner to this one in the next. The text
  3855. matched within the group is recorded (in the internal
  3856. registers data structure) under the register number. */
  3857. case start_memory:
  3858. DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  3859. /* Find out if this group can match the empty string. */
  3860. p1 = p; /* To send to group_match_null_string_p. */
  3861. if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  3862. REG_MATCH_NULL_STRING_P (reg_info[*p])
  3863. = group_match_null_string_p (&p1, pend, reg_info);
  3864. /* Save the position in the string where we were the last time
  3865. we were at this open-group operator in case the group is
  3866. operated upon by a repetition operator, e.g., with `(a*)*b'
  3867. against `ab'; then we want to ignore where we are now in
  3868. the string in case this attempt to match fails. */
  3869. old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3870. ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  3871. : regstart[*p];
  3872. DEBUG_PRINT2 (" old_regstart: %d\n",
  3873. POINTER_TO_OFFSET (old_regstart[*p]));
  3874. regstart[*p] = d;
  3875. DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  3876. IS_ACTIVE (reg_info[*p]) = 1;
  3877. MATCHED_SOMETHING (reg_info[*p]) = 0;
  3878. /* Clear this whenever we change the register activity status. */
  3879. set_regs_matched_done = 0;
  3880. /* This is the new highest active register. */
  3881. highest_active_reg = *p;
  3882. /* If nothing was active before, this is the new lowest active
  3883. register. */
  3884. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3885. lowest_active_reg = *p;
  3886. /* Move past the register number and inner group count. */
  3887. p += 2;
  3888. just_past_start_mem = p;
  3889. break;
  3890. /* The stop_memory opcode represents the end of a group. Its
  3891. arguments are the same as start_memory's: the register
  3892. number, and the number of inner groups. */
  3893. case stop_memory:
  3894. DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  3895. /* We need to save the string position the last time we were at
  3896. this close-group operator in case the group is operated
  3897. upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  3898. against `aba'; then we want to ignore where we are now in
  3899. the string in case this attempt to match fails. */
  3900. old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3901. ? REG_UNSET (regend[*p]) ? d : regend[*p]
  3902. : regend[*p];
  3903. DEBUG_PRINT2 (" old_regend: %d\n",
  3904. POINTER_TO_OFFSET (old_regend[*p]));
  3905. regend[*p] = d;
  3906. DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  3907. /* This register isn't active anymore. */
  3908. IS_ACTIVE (reg_info[*p]) = 0;
  3909. /* Clear this whenever we change the register activity status. */
  3910. set_regs_matched_done = 0;
  3911. /* If this was the only register active, nothing is active
  3912. anymore. */
  3913. if (lowest_active_reg == highest_active_reg)
  3914. {
  3915. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3916. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3917. }
  3918. else
  3919. { /* We must scan for the new highest active register, since
  3920. it isn't necessarily one less than now: consider
  3921. (a(b)c(d(e)f)g). When group 3 ends, after the f), the
  3922. new highest active register is 1. */
  3923. unsigned char r = *p - 1;
  3924. while (r > 0 && !IS_ACTIVE (reg_info[r]))
  3925. r--;
  3926. /* If we end up at register zero, that means that we saved
  3927. the registers as the result of an `on_failure_jump', not
  3928. a `start_memory', and we jumped to past the innermost
  3929. `stop_memory'. For example, in ((.)*) we save
  3930. registers 1 and 2 as a result of the *, but when we pop
  3931. back to the second ), we are at the stop_memory 1.
  3932. Thus, nothing is active. */
  3933. if (r == 0)
  3934. {
  3935. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3936. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3937. }
  3938. else
  3939. highest_active_reg = r;
  3940. }
  3941. /* If just failed to match something this time around with a
  3942. group that's operated on by a repetition operator, try to
  3943. force exit from the ``loop'', and restore the register
  3944. information for this group that we had before trying this
  3945. last match. */
  3946. if ((!MATCHED_SOMETHING (reg_info[*p])
  3947. || just_past_start_mem == p - 1)
  3948. && (p + 2) < pend)
  3949. {
  3950. boolean is_a_jump_n = false;
  3951. p1 = p + 2;
  3952. mcnt = 0;
  3953. switch ((re_opcode_t) *p1++)
  3954. {
  3955. case jump_n:
  3956. is_a_jump_n = true;
  3957. case pop_failure_jump:
  3958. case maybe_pop_jump:
  3959. case jump:
  3960. case dummy_failure_jump:
  3961. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3962. if (is_a_jump_n)
  3963. p1 += 2;
  3964. break;
  3965. default:
  3966. /* do nothing */ ;
  3967. }
  3968. p1 += mcnt;
  3969. /* If the next operation is a jump backwards in the pattern
  3970. to an on_failure_jump right before the start_memory
  3971. corresponding to this stop_memory, exit from the loop
  3972. by forcing a failure after pushing on the stack the
  3973. on_failure_jump's jump in the pattern, and d. */
  3974. if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  3975. && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  3976. {
  3977. /* If this group ever matched anything, then restore
  3978. what its registers were before trying this last
  3979. failed match, e.g., with `(a*)*b' against `ab' for
  3980. regstart[1], and, e.g., with `((a*)*(b*)*)*'
  3981. against `aba' for regend[3].
  3982. Also restore the registers for inner groups for,
  3983. e.g., `((a*)(b*))*' against `aba' (register 3 would
  3984. otherwise get trashed). */
  3985. if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  3986. {
  3987. unsigned r;
  3988. EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  3989. /* Restore this and inner groups' (if any) registers. */
  3990. for (r = *p; r < *p + *(p + 1); r++)
  3991. {
  3992. regstart[r] = old_regstart[r];
  3993. /* xx why this test? */
  3994. if (old_regend[r] >= regstart[r])
  3995. regend[r] = old_regend[r];
  3996. }
  3997. }
  3998. p1++;
  3999. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4000. PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  4001. goto fail;
  4002. }
  4003. }
  4004. /* Move past the register number and the inner group count. */
  4005. p += 2;
  4006. break;
  4007. /* \<digit> has been turned into a `duplicate' command which is
  4008. followed by the numeric value of <digit> as the register number. */
  4009. case duplicate:
  4010. {
  4011. register const char *d2, *dend2;
  4012. int regno = *p++; /* Get which register to match against. */
  4013. DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  4014. /* Can't back reference a group which we've never matched. */
  4015. if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  4016. goto fail;
  4017. /* Where in input to try to start matching. */
  4018. d2 = regstart[regno];
  4019. /* Where to stop matching; if both the place to start and
  4020. the place to stop matching are in the same string, then
  4021. set to the place to stop, otherwise, for now have to use
  4022. the end of the first string. */
  4023. dend2 = ((FIRST_STRING_P (regstart[regno])
  4024. == FIRST_STRING_P (regend[regno]))
  4025. ? regend[regno] : end_match_1);
  4026. for (;;)
  4027. {
  4028. /* If necessary, advance to next segment in register
  4029. contents. */
  4030. while (d2 == dend2)
  4031. {
  4032. if (dend2 == end_match_2) break;
  4033. if (dend2 == regend[regno]) break;
  4034. /* End of string1 => advance to string2. */
  4035. d2 = string2;
  4036. dend2 = regend[regno];
  4037. }
  4038. /* At end of register contents => success */
  4039. if (d2 == dend2) break;
  4040. /* If necessary, advance to next segment in data. */
  4041. PREFETCH ();
  4042. /* How many characters left in this segment to match. */
  4043. mcnt = dend - d;
  4044. /* Want how many consecutive characters we can match in
  4045. one shot, so, if necessary, adjust the count. */
  4046. if (mcnt > dend2 - d2)
  4047. mcnt = dend2 - d2;
  4048. /* Compare that many; failure if mismatch, else move
  4049. past them. */
  4050. if (RE_TRANSLATE_P (translate)
  4051. ? bcmp_translate (d, d2, mcnt, translate)
  4052. : bcmp (d, d2, mcnt))
  4053. goto fail;
  4054. d += mcnt, d2 += mcnt;
  4055. /* Do this because we've match some characters. */
  4056. SET_REGS_MATCHED ();
  4057. }
  4058. }
  4059. break;
  4060. /* begline matches the empty string at the beginning of the string
  4061. (unless `not_bol' is set in `bufp'), and, if
  4062. `newline_anchor' is set, after newlines. */
  4063. case begline:
  4064. DEBUG_PRINT1 ("EXECUTING begline.\n");
  4065. if (AT_STRINGS_BEG (d))
  4066. {
  4067. if (!bufp->not_bol) break;
  4068. }
  4069. else if (d[-1] == '\n' && bufp->newline_anchor)
  4070. {
  4071. break;
  4072. }
  4073. /* In all other cases, we fail. */
  4074. goto fail;
  4075. /* endline is the dual of begline. */
  4076. case endline:
  4077. DEBUG_PRINT1 ("EXECUTING endline.\n");
  4078. if (AT_STRINGS_END (d))
  4079. {
  4080. if (!bufp->not_eol) break;
  4081. }
  4082. /* We have to ``prefetch'' the next character. */
  4083. else if ((d == end1 ? *string2 : *d) == '\n'
  4084. && bufp->newline_anchor)
  4085. {
  4086. break;
  4087. }
  4088. goto fail;
  4089. /* Match at the very beginning of the data. */
  4090. case begbuf:
  4091. DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  4092. if (AT_STRINGS_BEG (d))
  4093. break;
  4094. goto fail;
  4095. /* Match at the very end of the data. */
  4096. case endbuf:
  4097. DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  4098. if (AT_STRINGS_END (d))
  4099. break;
  4100. goto fail;
  4101. /* on_failure_keep_string_jump is used to optimize `.*\n'. It
  4102. pushes NULL as the value for the string on the stack. Then
  4103. `pop_failure_point' will keep the current value for the
  4104. string, instead of restoring it. To see why, consider
  4105. matching `foo\nbar' against `.*\n'. The .* matches the foo;
  4106. then the . fails against the \n. But the next thing we want
  4107. to do is match the \n against the \n; if we restored the
  4108. string value, we would be back at the foo.
  4109. Because this is used only in specific cases, we don't need to
  4110. check all the things that `on_failure_jump' does, to make
  4111. sure the right things get saved on the stack. Hence we don't
  4112. share its code. The only reason to push anything on the
  4113. stack at all is that otherwise we would have to change
  4114. `anychar's code to do something besides goto fail in this
  4115. case; that seems worse than this. */
  4116. case on_failure_keep_string_jump:
  4117. DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  4118. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4119. DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  4120. PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  4121. break;
  4122. /* Uses of on_failure_jump:
  4123. Each alternative starts with an on_failure_jump that points
  4124. to the beginning of the next alternative. Each alternative
  4125. except the last ends with a jump that in effect jumps past
  4126. the rest of the alternatives. (They really jump to the
  4127. ending jump of the following alternative, because tensioning
  4128. these jumps is a hassle.)
  4129. Repeats start with an on_failure_jump that points past both
  4130. the repetition text and either the following jump or
  4131. pop_failure_jump back to this on_failure_jump. */
  4132. case on_failure_jump:
  4133. on_failure:
  4134. DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  4135. #if defined (WINDOWSNT) && defined (emacs)
  4136. QUIT;
  4137. #endif
  4138. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4139. DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  4140. /* If this on_failure_jump comes right before a group (i.e.,
  4141. the original * applied to a group), save the information
  4142. for that group and all inner ones, so that if we fail back
  4143. to this point, the group's information will be correct.
  4144. For example, in \(a*\)*\1, we need the preceding group,
  4145. and in \(zz\(a*\)b*\)\2, we need the inner group. */
  4146. /* We can't use `p' to check ahead because we push
  4147. a failure point to `p + mcnt' after we do this. */
  4148. p1 = p;
  4149. /* We need to skip no_op's before we look for the
  4150. start_memory in case this on_failure_jump is happening as
  4151. the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  4152. against aba. */
  4153. while (p1 < pend && (re_opcode_t) *p1 == no_op)
  4154. p1++;
  4155. if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  4156. {
  4157. /* We have a new highest active register now. This will
  4158. get reset at the start_memory we are about to get to,
  4159. but we will have saved all the registers relevant to
  4160. this repetition op, as described above. */
  4161. highest_active_reg = *(p1 + 1) + *(p1 + 2);
  4162. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  4163. lowest_active_reg = *(p1 + 1);
  4164. }
  4165. DEBUG_PRINT1 (":\n");
  4166. PUSH_FAILURE_POINT (p + mcnt, d, -2);
  4167. break;
  4168. /* A smart repeat ends with `maybe_pop_jump'.
  4169. We change it to either `pop_failure_jump' or `jump'. */
  4170. case maybe_pop_jump:
  4171. #if defined (WINDOWSNT) && defined (emacs)
  4172. QUIT;
  4173. #endif
  4174. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4175. DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  4176. {
  4177. register unsigned char *p2 = p;
  4178. /* Compare the beginning of the repeat with what in the
  4179. pattern follows its end. If we can establish that there
  4180. is nothing that they would both match, i.e., that we
  4181. would have to backtrack because of (as in, e.g., `a*a')
  4182. then we can change to pop_failure_jump, because we'll
  4183. never have to backtrack.
  4184. This is not true in the case of alternatives: in
  4185. `(a|ab)*' we do need to backtrack to the `ab' alternative
  4186. (e.g., if the string was `ab'). But instead of trying to
  4187. detect that here, the alternative has put on a dummy
  4188. failure point which is what we will end up popping. */
  4189. /* Skip over open/close-group commands.
  4190. If what follows this loop is a ...+ construct,
  4191. look at what begins its body, since we will have to
  4192. match at least one of that. */
  4193. while (1)
  4194. {
  4195. if (p2 + 2 < pend
  4196. && ((re_opcode_t) *p2 == stop_memory
  4197. || (re_opcode_t) *p2 == start_memory))
  4198. p2 += 3;
  4199. else if (p2 + 6 < pend
  4200. && (re_opcode_t) *p2 == dummy_failure_jump)
  4201. p2 += 6;
  4202. else
  4203. break;
  4204. }
  4205. p1 = p + mcnt;
  4206. /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  4207. to the `maybe_finalize_jump' of this case. Examine what
  4208. follows. */
  4209. /* If we're at the end of the pattern, we can change. */
  4210. if (p2 == pend)
  4211. {
  4212. /* Consider what happens when matching ":\(.*\)"
  4213. against ":/". I don't really understand this code
  4214. yet. */
  4215. p[-3] = (unsigned char) pop_failure_jump;
  4216. DEBUG_PRINT1
  4217. (" End of pattern: change to `pop_failure_jump'.\n");
  4218. }
  4219. else if ((re_opcode_t) *p2 == exactn
  4220. || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  4221. {
  4222. register unsigned int c
  4223. = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4224. if ((re_opcode_t) p1[3] == exactn)
  4225. {
  4226. if (!(multibyte /* && (c != '\n') */
  4227. && BASE_LEADING_CODE_P (c))
  4228. ? c != p1[5]
  4229. : (STRING_CHAR (&p2[2], pend - &p2[2])
  4230. != STRING_CHAR (&p1[5], pend - &p1[5])))
  4231. {
  4232. p[-3] = (unsigned char) pop_failure_jump;
  4233. DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
  4234. c, p1[5]);
  4235. }
  4236. }
  4237. else if ((re_opcode_t) p1[3] == charset
  4238. || (re_opcode_t) p1[3] == charset_not)
  4239. {
  4240. int not = (re_opcode_t) p1[3] == charset_not;
  4241. if (multibyte /* && (c != '\n') */
  4242. && BASE_LEADING_CODE_P (c))
  4243. c = STRING_CHAR (&p2[2], pend - &p2[2]);
  4244. /* Test if C is listed in charset (or charset_not)
  4245. at `&p1[3]'. */
  4246. if (SINGLE_BYTE_CHAR_P (c))
  4247. {
  4248. if (c < CHARSET_BITMAP_SIZE (&p1[3]) * BYTEWIDTH
  4249. && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4250. not = !not;
  4251. }
  4252. else if (CHARSET_RANGE_TABLE_EXISTS_P (&p1[3]))
  4253. CHARSET_LOOKUP_RANGE_TABLE (not, c, &p1[3]);
  4254. /* `not' is equal to 1 if c would match, which means
  4255. that we can't change to pop_failure_jump. */
  4256. if (!not)
  4257. {
  4258. p[-3] = (unsigned char) pop_failure_jump;
  4259. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4260. }
  4261. }
  4262. }
  4263. else if ((re_opcode_t) *p2 == charset)
  4264. {
  4265. if ((re_opcode_t) p1[3] == exactn)
  4266. {
  4267. register unsigned int c = p1[5];
  4268. int not = 0;
  4269. if (multibyte && BASE_LEADING_CODE_P (c))
  4270. c = STRING_CHAR (&p1[5], pend - &p1[5]);
  4271. /* Test if C is listed in charset at `p2'. */
  4272. if (SINGLE_BYTE_CHAR_P (c))
  4273. {
  4274. if (c < CHARSET_BITMAP_SIZE (p2) * BYTEWIDTH
  4275. && (p2[2 + c / BYTEWIDTH]
  4276. & (1 << (c % BYTEWIDTH))))
  4277. not = !not;
  4278. }
  4279. else if (CHARSET_RANGE_TABLE_EXISTS_P (p2))
  4280. CHARSET_LOOKUP_RANGE_TABLE (not, c, p2);
  4281. if (!not)
  4282. {
  4283. p[-3] = (unsigned char) pop_failure_jump;
  4284. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4285. }
  4286. }
  4287. /* It is hard to list up all the character in charset
  4288. P2 if it includes multibyte character. Give up in
  4289. such case. */
  4290. else if (!multibyte || !CHARSET_RANGE_TABLE_EXISTS_P (p2))
  4291. {
  4292. /* Now, we are sure that P2 has no range table.
  4293. So, for the size of bitmap in P2, `p2[1]' is
  4294. enough. But P1 may have range table, so the
  4295. size of bitmap table of P1 is extracted by
  4296. using macro `CHARSET_BITMAP_SIZE'.
  4297. Since we know that all the character listed in
  4298. P2 is ASCII, it is enough to test only bitmap
  4299. table of P1. */
  4300. if ((re_opcode_t) p1[3] == charset_not)
  4301. {
  4302. int idx;
  4303. /* We win if the charset_not inside the loop lists
  4304. every character listed in the charset after. */
  4305. for (idx = 0; idx < (int) p2[1]; idx++)
  4306. if (! (p2[2 + idx] == 0
  4307. || (idx < CHARSET_BITMAP_SIZE (&p1[3])
  4308. && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
  4309. break;
  4310. if (idx == p2[1])
  4311. {
  4312. p[-3] = (unsigned char) pop_failure_jump;
  4313. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4314. }
  4315. }
  4316. else if ((re_opcode_t) p1[3] == charset)
  4317. {
  4318. int idx;
  4319. /* We win if the charset inside the loop
  4320. has no overlap with the one after the loop. */
  4321. for (idx = 0;
  4322. (idx < (int) p2[1]
  4323. && idx < CHARSET_BITMAP_SIZE (&p1[3]));
  4324. idx++)
  4325. if ((p2[2 + idx] & p1[5 + idx]) != 0)
  4326. break;
  4327. if (idx == p2[1]
  4328. || idx == CHARSET_BITMAP_SIZE (&p1[3]))
  4329. {
  4330. p[-3] = (unsigned char) pop_failure_jump;
  4331. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4332. }
  4333. }
  4334. }
  4335. }
  4336. }
  4337. p -= 2; /* Point at relative address again. */
  4338. if ((re_opcode_t) p[-1] != pop_failure_jump)
  4339. {
  4340. p[-1] = (unsigned char) jump;
  4341. DEBUG_PRINT1 (" Match => jump.\n");
  4342. goto unconditional_jump;
  4343. }
  4344. /* Note fall through. */
  4345. /* The end of a simple repeat has a pop_failure_jump back to
  4346. its matching on_failure_jump, where the latter will push a
  4347. failure point. The pop_failure_jump takes off failure
  4348. points put on by this pop_failure_jump's matching
  4349. on_failure_jump; we got through the pattern to here from the
  4350. matching on_failure_jump, so didn't fail. */
  4351. case pop_failure_jump:
  4352. {
  4353. /* We need to pass separate storage for the lowest and
  4354. highest registers, even though we don't care about the
  4355. actual values. Otherwise, we will restore only one
  4356. register from the stack, since lowest will == highest in
  4357. `pop_failure_point'. */
  4358. unsigned dummy_low_reg, dummy_high_reg;
  4359. unsigned char *pdummy;
  4360. const char *sdummy;
  4361. DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  4362. POP_FAILURE_POINT (sdummy, pdummy,
  4363. dummy_low_reg, dummy_high_reg,
  4364. reg_dummy, reg_dummy, reg_info_dummy);
  4365. }
  4366. /* Note fall through. */
  4367. /* Unconditionally jump (without popping any failure points). */
  4368. case jump:
  4369. unconditional_jump:
  4370. #if defined (WINDOWSNT) && defined (emacs)
  4371. QUIT;
  4372. #endif
  4373. EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
  4374. DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  4375. p += mcnt; /* Do the jump. */
  4376. DEBUG_PRINT2 ("(to 0x%x).\n", p);
  4377. break;
  4378. /* We need this opcode so we can detect where alternatives end
  4379. in `group_match_null_string_p' et al. */
  4380. case jump_past_alt:
  4381. DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  4382. goto unconditional_jump;
  4383. /* Normally, the on_failure_jump pushes a failure point, which
  4384. then gets popped at pop_failure_jump. We will end up at
  4385. pop_failure_jump, also, and with a pattern of, say, `a+', we
  4386. are skipping over the on_failure_jump, so we have to push
  4387. something meaningless for pop_failure_jump to pop. */
  4388. case dummy_failure_jump:
  4389. DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  4390. /* It doesn't matter what we push for the string here. What
  4391. the code at `fail' tests is the value for the pattern. */
  4392. PUSH_FAILURE_POINT (0, 0, -2);
  4393. goto unconditional_jump;
  4394. /* At the end of an alternative, we need to push a dummy failure
  4395. point in case we are followed by a `pop_failure_jump', because
  4396. we don't want the failure point for the alternative to be
  4397. popped. For example, matching `(a|ab)*' against `aab'
  4398. requires that we match the `ab' alternative. */
  4399. case push_dummy_failure:
  4400. DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  4401. /* See comments just above at `dummy_failure_jump' about the
  4402. two zeroes. */
  4403. PUSH_FAILURE_POINT (0, 0, -2);
  4404. break;
  4405. /* Have to succeed matching what follows at least n times.
  4406. After that, handle like `on_failure_jump'. */
  4407. case succeed_n:
  4408. EXTRACT_NUMBER (mcnt, p + 2);
  4409. DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  4410. assert (mcnt >= 0);
  4411. /* Originally, this is how many times we HAVE to succeed. */
  4412. if (mcnt > 0)
  4413. {
  4414. mcnt--;
  4415. p += 2;
  4416. STORE_NUMBER_AND_INCR (p, mcnt);
  4417. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p, mcnt);
  4418. }
  4419. else if (mcnt == 0)
  4420. {
  4421. DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p+2);
  4422. p[2] = (unsigned char) no_op;
  4423. p[3] = (unsigned char) no_op;
  4424. goto on_failure;
  4425. }
  4426. break;
  4427. case jump_n:
  4428. EXTRACT_NUMBER (mcnt, p + 2);
  4429. DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  4430. /* Originally, this is how many times we CAN jump. */
  4431. if (mcnt)
  4432. {
  4433. mcnt--;
  4434. STORE_NUMBER (p + 2, mcnt);
  4435. goto unconditional_jump;
  4436. }
  4437. /* If don't have to jump any more, skip over the rest of command. */
  4438. else
  4439. p += 4;
  4440. break;
  4441. case set_number_at:
  4442. {
  4443. DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  4444. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4445. p1 = p + mcnt;
  4446. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4447. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
  4448. STORE_NUMBER (p1, mcnt);
  4449. break;
  4450. }
  4451. case wordbound:
  4452. DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4453. /* We SUCCEED in one of the following cases: */
  4454. /* Case 1: D is at the beginning or the end of string. */
  4455. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  4456. break;
  4457. else
  4458. {
  4459. /* C1 is the character before D, S1 is the syntax of C1, C2
  4460. is the character at D, and S2 is the syntax of C2. */
  4461. int c1, c2, s1, s2;
  4462. int pos1 = PTR_TO_OFFSET (d - 1);
  4463. int charpos;
  4464. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4465. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4466. #ifdef emacs
  4467. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
  4468. UPDATE_SYNTAX_TABLE (charpos);
  4469. #endif
  4470. s1 = SYNTAX (c1);
  4471. #ifdef emacs
  4472. UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
  4473. #endif
  4474. s2 = SYNTAX (c2);
  4475. if (/* Case 2: Only one of S1 and S2 is Sword. */
  4476. ((s1 == Sword) != (s2 == Sword))
  4477. /* Case 3: Both of S1 and S2 are Sword, and macro
  4478. WORD_BOUNDARY_P (C1, C2) returns nonzero. */
  4479. || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
  4480. break;
  4481. }
  4482. goto fail;
  4483. case notwordbound:
  4484. DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4485. /* We FAIL in one of the following cases: */
  4486. /* Case 1: D is at the beginning or the end of string. */
  4487. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  4488. goto fail;
  4489. else
  4490. {
  4491. /* C1 is the character before D, S1 is the syntax of C1, C2
  4492. is the character at D, and S2 is the syntax of C2. */
  4493. int c1, c2, s1, s2;
  4494. int pos1 = PTR_TO_OFFSET (d - 1);
  4495. int charpos;
  4496. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4497. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4498. #ifdef emacs
  4499. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
  4500. UPDATE_SYNTAX_TABLE (charpos);
  4501. #endif
  4502. s1 = SYNTAX (c1);
  4503. #ifdef emacs
  4504. UPDATE_SYNTAX_TABLE_FORWARD (charpos + 1);
  4505. #endif
  4506. s2 = SYNTAX (c2);
  4507. if (/* Case 2: Only one of S1 and S2 is Sword. */
  4508. ((s1 == Sword) != (s2 == Sword))
  4509. /* Case 3: Both of S1 and S2 are Sword, and macro
  4510. WORD_BOUNDARY_P (C1, C2) returns nonzero. */
  4511. || ((s1 == Sword) && WORD_BOUNDARY_P (c1, c2)))
  4512. goto fail;
  4513. }
  4514. break;
  4515. case wordbeg:
  4516. DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  4517. /* We FAIL in one of the following cases: */
  4518. /* Case 1: D is at the end of string. */
  4519. if (AT_STRINGS_END (d))
  4520. goto fail;
  4521. else
  4522. {
  4523. /* C1 is the character before D, S1 is the syntax of C1, C2
  4524. is the character at D, and S2 is the syntax of C2. */
  4525. int c1, c2, s1, s2;
  4526. int pos1 = PTR_TO_OFFSET (d);
  4527. int charpos;
  4528. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4529. #ifdef emacs
  4530. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1);
  4531. UPDATE_SYNTAX_TABLE (charpos);
  4532. #endif
  4533. s2 = SYNTAX (c2);
  4534. /* Case 2: S2 is not Sword. */
  4535. if (s2 != Sword)
  4536. goto fail;
  4537. /* Case 3: D is not at the beginning of string ... */
  4538. if (!AT_STRINGS_BEG (d))
  4539. {
  4540. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4541. #ifdef emacs
  4542. UPDATE_SYNTAX_TABLE_BACKWARD (charpos - 1);
  4543. #endif
  4544. s1 = SYNTAX (c1);
  4545. /* ... and S1 is Sword, and WORD_BOUNDARY_P (C1, C2)
  4546. returns 0. */
  4547. if ((s1 == Sword) && !WORD_BOUNDARY_P (c1, c2))
  4548. goto fail;
  4549. }
  4550. }
  4551. break;
  4552. case wordend:
  4553. DEBUG_PRINT1 ("EXECUTING wordend.\n");
  4554. /* We FAIL in one of the following cases: */
  4555. /* Case 1: D is at the beginning of string. */
  4556. if (AT_STRINGS_BEG (d))
  4557. goto fail;
  4558. else
  4559. {
  4560. /* C1 is the character before D, S1 is the syntax of C1, C2
  4561. is the character at D, and S2 is the syntax of C2. */
  4562. int c1, c2, s1, s2;
  4563. int pos1 = PTR_TO_OFFSET (d);
  4564. int charpos;
  4565. GET_CHAR_BEFORE_2 (c1, d, string1, end1, string2, end2);
  4566. #ifdef emacs
  4567. charpos = SYNTAX_TABLE_BYTE_TO_CHAR (pos1 - 1);
  4568. UPDATE_SYNTAX_TABLE (charpos);
  4569. #endif
  4570. s1 = SYNTAX (c1);
  4571. /* Case 2: S1 is not Sword. */
  4572. if (s1 != Sword)
  4573. goto fail;
  4574. /* Case 3: D is not at the end of string ... */
  4575. if (!AT_STRINGS_END (d))
  4576. {
  4577. GET_CHAR_AFTER_2 (c2, d, string1, end1, string2, end2);
  4578. #ifdef emacs
  4579. UPDATE_SYNTAX_TABLE_FORWARD (charpos);
  4580. #endif
  4581. s2 = SYNTAX (c2);
  4582. /* ... and S2 is Sword, and WORD_BOUNDARY_P (C1, C2)
  4583. returns 0. */
  4584. if ((s2 == Sword) && !WORD_BOUNDARY_P (c1, c2))
  4585. goto fail;
  4586. }
  4587. }
  4588. break;
  4589. #ifdef emacs
  4590. case before_dot:
  4591. DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  4592. if (PTR_BYTE_POS ((unsigned char *) d) >= PT_BYTE)
  4593. goto fail;
  4594. break;
  4595. case at_dot:
  4596. DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4597. if (PTR_BYTE_POS ((unsigned char *) d) != PT_BYTE)
  4598. goto fail;
  4599. break;
  4600. case after_dot:
  4601. DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  4602. if (PTR_BYTE_POS ((unsigned char *) d) <= PT_BYTE)
  4603. goto fail;
  4604. break;
  4605. case syntaxspec:
  4606. DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  4607. mcnt = *p++;
  4608. goto matchsyntax;
  4609. case wordchar:
  4610. DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  4611. mcnt = (int) Sword;
  4612. matchsyntax:
  4613. PREFETCH ();
  4614. #ifdef emacs
  4615. {
  4616. int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
  4617. UPDATE_SYNTAX_TABLE (pos1);
  4618. }
  4619. #endif
  4620. {
  4621. int c, len;
  4622. if (multibyte)
  4623. /* we must concern about multibyte form, ... */
  4624. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4625. else
  4626. /* everything should be handled as ASCII, even though it
  4627. looks like multibyte form. */
  4628. c = *d, len = 1;
  4629. if (SYNTAX (c) != (enum syntaxcode) mcnt)
  4630. goto fail;
  4631. d += len;
  4632. }
  4633. SET_REGS_MATCHED ();
  4634. break;
  4635. case notsyntaxspec:
  4636. DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  4637. mcnt = *p++;
  4638. goto matchnotsyntax;
  4639. case notwordchar:
  4640. DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  4641. mcnt = (int) Sword;
  4642. matchnotsyntax:
  4643. PREFETCH ();
  4644. #ifdef emacs
  4645. {
  4646. int pos1 = SYNTAX_TABLE_BYTE_TO_CHAR (PTR_TO_OFFSET (d));
  4647. UPDATE_SYNTAX_TABLE (pos1);
  4648. }
  4649. #endif
  4650. {
  4651. int c, len;
  4652. if (multibyte)
  4653. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4654. else
  4655. c = *d, len = 1;
  4656. if (SYNTAX (c) == (enum syntaxcode) mcnt)
  4657. goto fail;
  4658. d += len;
  4659. }
  4660. SET_REGS_MATCHED ();
  4661. break;
  4662. case categoryspec:
  4663. DEBUG_PRINT2 ("EXECUTING categoryspec %d.\n", *p);
  4664. mcnt = *p++;
  4665. PREFETCH ();
  4666. {
  4667. int c, len;
  4668. if (multibyte)
  4669. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4670. else
  4671. c = *d, len = 1;
  4672. if (!CHAR_HAS_CATEGORY (c, mcnt))
  4673. goto fail;
  4674. d += len;
  4675. }
  4676. SET_REGS_MATCHED ();
  4677. break;
  4678. case notcategoryspec:
  4679. DEBUG_PRINT2 ("EXECUTING notcategoryspec %d.\n", *p);
  4680. mcnt = *p++;
  4681. PREFETCH ();
  4682. {
  4683. int c, len;
  4684. if (multibyte)
  4685. c = STRING_CHAR_AND_LENGTH (d, dend - d, len);
  4686. else
  4687. c = *d, len = 1;
  4688. if (CHAR_HAS_CATEGORY (c, mcnt))
  4689. goto fail;
  4690. d += len;
  4691. }
  4692. SET_REGS_MATCHED ();
  4693. break;
  4694. #else /* not emacs */
  4695. case wordchar:
  4696. DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  4697. PREFETCH ();
  4698. if (!WORDCHAR_P (d))
  4699. goto fail;
  4700. SET_REGS_MATCHED ();
  4701. d++;
  4702. break;
  4703. case notwordchar:
  4704. DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  4705. PREFETCH ();
  4706. if (WORDCHAR_P (d))
  4707. goto fail;
  4708. SET_REGS_MATCHED ();
  4709. d++;
  4710. break;
  4711. #endif /* not emacs */
  4712. default:
  4713. abort ();
  4714. }
  4715. continue; /* Successfully executed one pattern command; keep going. */
  4716. /* We goto here if a matching operation fails. */
  4717. fail:
  4718. #if defined (WINDOWSNT) && defined (emacs)
  4719. QUIT;
  4720. #endif
  4721. if (!FAIL_STACK_EMPTY ())
  4722. { /* A restart point is known. Restore to that state. */
  4723. DEBUG_PRINT1 ("\nFAIL:\n");
  4724. POP_FAILURE_POINT (d, p,
  4725. lowest_active_reg, highest_active_reg,
  4726. regstart, regend, reg_info);
  4727. /* If this failure point is a dummy, try the next one. */
  4728. if (!p)
  4729. goto fail;
  4730. /* If we failed to the end of the pattern, don't examine *p. */
  4731. assert (p <= pend);
  4732. if (p < pend)
  4733. {
  4734. boolean is_a_jump_n = false;
  4735. /* If failed to a backwards jump that's part of a repetition
  4736. loop, need to pop this failure point and use the next one. */
  4737. switch ((re_opcode_t) *p)
  4738. {
  4739. case jump_n:
  4740. is_a_jump_n = true;
  4741. case maybe_pop_jump:
  4742. case pop_failure_jump:
  4743. case jump:
  4744. p1 = p + 1;
  4745. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4746. p1 += mcnt;
  4747. if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4748. || (!is_a_jump_n
  4749. && (re_opcode_t) *p1 == on_failure_jump))
  4750. goto fail;
  4751. break;
  4752. default:
  4753. /* do nothing */ ;
  4754. }
  4755. }
  4756. if (d >= string1 && d <= end1)
  4757. dend = end_match_1;
  4758. }
  4759. else
  4760. break; /* Matching at this starting point really fails. */
  4761. } /* for (;;) */
  4762. if (best_regs_set)
  4763. goto restore_best_regs;
  4764. FREE_VARIABLES ();
  4765. return -1; /* Failure to match. */
  4766. } /* re_match_2 */
  4767. /* Subroutine definitions for re_match_2. */
  4768. /* We are passed P pointing to a register number after a start_memory.
  4769. Return true if the pattern up to the corresponding stop_memory can
  4770. match the empty string, and false otherwise.
  4771. If we find the matching stop_memory, sets P to point to one past its number.
  4772. Otherwise, sets P to an undefined byte less than or equal to END.
  4773. We don't handle duplicates properly (yet). */
  4774. static boolean
  4775. group_match_null_string_p (p, end, reg_info)
  4776. unsigned char **p, *end;
  4777. register_info_type *reg_info;
  4778. {
  4779. int mcnt;
  4780. /* Point to after the args to the start_memory. */
  4781. unsigned char *p1 = *p + 2;
  4782. while (p1 < end)
  4783. {
  4784. /* Skip over opcodes that can match nothing, and return true or
  4785. false, as appropriate, when we get to one that can't, or to the
  4786. matching stop_memory. */
  4787. switch ((re_opcode_t) *p1)
  4788. {
  4789. /* Could be either a loop or a series of alternatives. */
  4790. case on_failure_jump:
  4791. p1++;
  4792. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4793. /* If the next operation is not a jump backwards in the
  4794. pattern. */
  4795. if (mcnt >= 0)
  4796. {
  4797. /* Go through the on_failure_jumps of the alternatives,
  4798. seeing if any of the alternatives cannot match nothing.
  4799. The last alternative starts with only a jump,
  4800. whereas the rest start with on_failure_jump and end
  4801. with a jump, e.g., here is the pattern for `a|b|c':
  4802. /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  4803. /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  4804. /exactn/1/c
  4805. So, we have to first go through the first (n-1)
  4806. alternatives and then deal with the last one separately. */
  4807. /* Deal with the first (n-1) alternatives, which start
  4808. with an on_failure_jump (see above) that jumps to right
  4809. past a jump_past_alt. */
  4810. while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
  4811. {
  4812. /* `mcnt' holds how many bytes long the alternative
  4813. is, including the ending `jump_past_alt' and
  4814. its number. */
  4815. if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
  4816. reg_info))
  4817. return false;
  4818. /* Move to right after this alternative, including the
  4819. jump_past_alt. */
  4820. p1 += mcnt;
  4821. /* Break if it's the beginning of an n-th alternative
  4822. that doesn't begin with an on_failure_jump. */
  4823. if ((re_opcode_t) *p1 != on_failure_jump)
  4824. break;
  4825. /* Still have to check that it's not an n-th
  4826. alternative that starts with an on_failure_jump. */
  4827. p1++;
  4828. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4829. if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
  4830. {
  4831. /* Get to the beginning of the n-th alternative. */
  4832. p1 -= 3;
  4833. break;
  4834. }
  4835. }
  4836. /* Deal with the last alternative: go back and get number
  4837. of the `jump_past_alt' just before it. `mcnt' contains
  4838. the length of the alternative. */
  4839. EXTRACT_NUMBER (mcnt, p1 - 2);
  4840. if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  4841. return false;
  4842. p1 += mcnt; /* Get past the n-th alternative. */
  4843. } /* if mcnt > 0 */
  4844. break;
  4845. case stop_memory:
  4846. assert (p1[1] == **p);
  4847. *p = p1 + 2;
  4848. return true;
  4849. default:
  4850. if (!common_op_match_null_string_p (&p1, end, reg_info))
  4851. return false;
  4852. }
  4853. } /* while p1 < end */
  4854. return false;
  4855. } /* group_match_null_string_p */
  4856. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  4857. It expects P to be the first byte of a single alternative and END one
  4858. byte past the last. The alternative can contain groups. */
  4859. static boolean
  4860. alt_match_null_string_p (p, end, reg_info)
  4861. unsigned char *p, *end;
  4862. register_info_type *reg_info;
  4863. {
  4864. int mcnt;
  4865. unsigned char *p1 = p;
  4866. while (p1 < end)
  4867. {
  4868. /* Skip over opcodes that can match nothing, and break when we get
  4869. to one that can't. */
  4870. switch ((re_opcode_t) *p1)
  4871. {
  4872. /* It's a loop. */
  4873. case on_failure_jump:
  4874. p1++;
  4875. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4876. p1 += mcnt;
  4877. break;
  4878. default:
  4879. if (!common_op_match_null_string_p (&p1, end, reg_info))
  4880. return false;
  4881. }
  4882. } /* while p1 < end */
  4883. return true;
  4884. } /* alt_match_null_string_p */
  4885. /* Deals with the ops common to group_match_null_string_p and
  4886. alt_match_null_string_p.
  4887. Sets P to one after the op and its arguments, if any. */
  4888. static boolean
  4889. common_op_match_null_string_p (p, end, reg_info)
  4890. unsigned char **p, *end;
  4891. register_info_type *reg_info;
  4892. {
  4893. int mcnt;
  4894. boolean ret;
  4895. int reg_no;
  4896. unsigned char *p1 = *p;
  4897. switch ((re_opcode_t) *p1++)
  4898. {
  4899. case no_op:
  4900. case begline:
  4901. case endline:
  4902. case begbuf:
  4903. case endbuf:
  4904. case wordbeg:
  4905. case wordend:
  4906. case wordbound:
  4907. case notwordbound:
  4908. #ifdef emacs
  4909. case before_dot:
  4910. case at_dot:
  4911. case after_dot:
  4912. #endif
  4913. break;
  4914. case start_memory:
  4915. reg_no = *p1;
  4916. assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  4917. ret = group_match_null_string_p (&p1, end, reg_info);
  4918. /* Have to set this here in case we're checking a group which
  4919. contains a group and a back reference to it. */
  4920. if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  4921. REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  4922. if (!ret)
  4923. return false;
  4924. break;
  4925. /* If this is an optimized succeed_n for zero times, make the jump. */
  4926. case jump:
  4927. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4928. if (mcnt >= 0)
  4929. p1 += mcnt;
  4930. else
  4931. return false;
  4932. break;
  4933. case succeed_n:
  4934. /* Get to the number of times to succeed. */
  4935. p1 += 2;
  4936. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4937. if (mcnt == 0)
  4938. {
  4939. p1 -= 4;
  4940. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4941. p1 += mcnt;
  4942. }
  4943. else
  4944. return false;
  4945. break;
  4946. case duplicate:
  4947. if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  4948. return false;
  4949. break;
  4950. case set_number_at:
  4951. p1 += 4;
  4952. default:
  4953. /* All other opcodes mean we cannot match the empty string. */
  4954. return false;
  4955. }
  4956. *p = p1;
  4957. return true;
  4958. } /* common_op_match_null_string_p */
  4959. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4960. bytes; nonzero otherwise. */
  4961. static int
  4962. bcmp_translate (s1, s2, len, translate)
  4963. unsigned char *s1, *s2;
  4964. register int len;
  4965. RE_TRANSLATE_TYPE translate;
  4966. {
  4967. register unsigned char *p1 = s1, *p2 = s2;
  4968. unsigned char *p1_end = s1 + len;
  4969. unsigned char *p2_end = s2 + len;
  4970. while (p1 != p1_end && p2 != p2_end)
  4971. {
  4972. int p1_charlen, p2_charlen;
  4973. int p1_ch, p2_ch;
  4974. p1_ch = STRING_CHAR_AND_LENGTH (p1, p1_end - p1, p1_charlen);
  4975. p2_ch = STRING_CHAR_AND_LENGTH (p2, p2_end - p2, p2_charlen);
  4976. if (RE_TRANSLATE (translate, p1_ch)
  4977. != RE_TRANSLATE (translate, p2_ch))
  4978. return 1;
  4979. p1 += p1_charlen, p2 += p2_charlen;
  4980. }
  4981. if (p1 != p1_end || p2 != p2_end)
  4982. return 1;
  4983. return 0;
  4984. }
  4985. /* Entry points for GNU code. */
  4986. /* re_compile_pattern is the GNU regular expression compiler: it
  4987. compiles PATTERN (of length SIZE) and puts the result in BUFP.
  4988. Returns 0 if the pattern was valid, otherwise an error string.
  4989. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  4990. are set in BUFP on entry.
  4991. We call regex_compile to do the actual compilation. */
  4992. const char *
  4993. re_compile_pattern (pattern, length, bufp)
  4994. const char *pattern;
  4995. int length;
  4996. struct re_pattern_buffer *bufp;
  4997. {
  4998. reg_errcode_t ret;
  4999. /* GNU code is written to assume at least RE_NREGS registers will be set
  5000. (and at least one extra will be -1). */
  5001. bufp->regs_allocated = REGS_UNALLOCATED;
  5002. /* And GNU code determines whether or not to get register information
  5003. by passing null for the REGS argument to re_match, etc., not by
  5004. setting no_sub. */
  5005. bufp->no_sub = 0;
  5006. /* Match anchors at newline. */
  5007. bufp->newline_anchor = 1;
  5008. ret = regex_compile (pattern, length, re_syntax_options, bufp);
  5009. if (!ret)
  5010. return NULL;
  5011. return gettext (re_error_msgid[(int) ret]);
  5012. }
  5013. /* Entry points compatible with 4.2 BSD regex library. We don't define
  5014. them unless specifically requested. */
  5015. #if defined (_REGEX_RE_COMP) || defined (_LIBC)
  5016. /* BSD has one and only one pattern buffer. */
  5017. static struct re_pattern_buffer re_comp_buf;
  5018. char *
  5019. #ifdef _LIBC
  5020. /* Make these definitions weak in libc, so POSIX programs can redefine
  5021. these names if they don't use our functions, and still use
  5022. regcomp/regexec below without link errors. */
  5023. weak_function
  5024. #endif
  5025. re_comp (s)
  5026. const char *s;
  5027. {
  5028. reg_errcode_t ret;
  5029. if (!s)
  5030. {
  5031. if (!re_comp_buf.buffer)
  5032. return gettext ("No previous regular expression");
  5033. return 0;
  5034. }
  5035. if (!re_comp_buf.buffer)
  5036. {
  5037. re_comp_buf.buffer = (unsigned char *) malloc (200);
  5038. if (re_comp_buf.buffer == NULL)
  5039. /* CVS: Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  5040. return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
  5041. re_comp_buf.allocated = 200;
  5042. re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  5043. if (re_comp_buf.fastmap == NULL)
  5044. /* CVS: Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  5045. return (char *) gettext (re_error_msgid[(int) REG_ESPACE]);
  5046. }
  5047. /* Since `re_exec' always passes NULL for the `regs' argument, we
  5048. don't need to initialize the pattern buffer fields which affect it. */
  5049. /* Match anchors at newlines. */
  5050. re_comp_buf.newline_anchor = 1;
  5051. ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  5052. if (!ret)
  5053. return NULL;
  5054. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  5055. return (char *) gettext (re_error_msgid[(int) ret]);
  5056. }
  5057. int
  5058. #ifdef _LIBC
  5059. weak_function
  5060. #endif
  5061. re_exec (s)
  5062. const char *s;
  5063. {
  5064. const int len = strlen (s);
  5065. return
  5066. 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  5067. }
  5068. #endif /* _REGEX_RE_COMP */
  5069. /* POSIX.2 functions. Don't define these for Emacs. */
  5070. #ifndef emacs
  5071. /* regcomp takes a regular expression as a string and compiles it.
  5072. PREG is a regex_t *. We do not expect any fields to be initialized,
  5073. since POSIX says we shouldn't. Thus, we set
  5074. `buffer' to the compiled pattern;
  5075. `used' to the length of the compiled pattern;
  5076. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  5077. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  5078. RE_SYNTAX_POSIX_BASIC;
  5079. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  5080. `fastmap' and `fastmap_accurate' to zero;
  5081. `re_nsub' to the number of subexpressions in PATTERN.
  5082. PATTERN is the address of the pattern string.
  5083. CFLAGS is a series of bits which affect compilation.
  5084. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  5085. use POSIX basic syntax.
  5086. If REG_NEWLINE is set, then . and [^...] don't match newline.
  5087. Also, regexec will try a match beginning after every newline.
  5088. If REG_ICASE is set, then we considers upper- and lowercase
  5089. versions of letters to be equivalent when matching.
  5090. If REG_NOSUB is set, then when PREG is passed to regexec, that
  5091. routine will report only success or failure, and nothing about the
  5092. registers.
  5093. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  5094. the return codes and their meanings.) */
  5095. int
  5096. regcomp (preg, pattern, cflags)
  5097. regex_t *preg;
  5098. const char *pattern;
  5099. int cflags;
  5100. {
  5101. reg_errcode_t ret;
  5102. unsigned syntax
  5103. = (cflags & REG_EXTENDED) ?
  5104. RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  5105. /* regex_compile will allocate the space for the compiled pattern. */
  5106. preg->buffer = 0;
  5107. preg->allocated = 0;
  5108. preg->used = 0;
  5109. /* Don't bother to use a fastmap when searching. This simplifies the
  5110. REG_NEWLINE case: if we used a fastmap, we'd have to put all the
  5111. characters after newlines into the fastmap. This way, we just try
  5112. every character. */
  5113. preg->fastmap = 0;
  5114. if (cflags & REG_ICASE)
  5115. {
  5116. unsigned i;
  5117. preg->translate
  5118. = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
  5119. * sizeof (*(RE_TRANSLATE_TYPE)0));
  5120. if (preg->translate == NULL)
  5121. return (int) REG_ESPACE;
  5122. /* Map uppercase characters to corresponding lowercase ones. */
  5123. for (i = 0; i < CHAR_SET_SIZE; i++)
  5124. preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
  5125. }
  5126. else
  5127. preg->translate = NULL;
  5128. /* If REG_NEWLINE is set, newlines are treated differently. */
  5129. if (cflags & REG_NEWLINE)
  5130. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  5131. syntax &= ~RE_DOT_NEWLINE;
  5132. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  5133. /* It also changes the matching behavior. */
  5134. preg->newline_anchor = 1;
  5135. }
  5136. else
  5137. preg->newline_anchor = 0;
  5138. preg->no_sub = !!(cflags & REG_NOSUB);
  5139. /* POSIX says a null character in the pattern terminates it, so we
  5140. can use strlen here in compiling the pattern. */
  5141. ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  5142. /* POSIX doesn't distinguish between an unmatched open-group and an
  5143. unmatched close-group: both are REG_EPAREN. */
  5144. if (ret == REG_ERPAREN) ret = REG_EPAREN;
  5145. return (int) ret;
  5146. }
  5147. /* regexec searches for a given pattern, specified by PREG, in the
  5148. string STRING.
  5149. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  5150. `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  5151. least NMATCH elements, and we set them to the offsets of the
  5152. corresponding matched substrings.
  5153. EFLAGS specifies `execution flags' which affect matching: if
  5154. REG_NOTBOL is set, then ^ does not match at the beginning of the
  5155. string; if REG_NOTEOL is set, then $ does not match at the end.
  5156. We return 0 if we find a match and REG_NOMATCH if not. */
  5157. int
  5158. regexec (preg, string, nmatch, pmatch, eflags)
  5159. const regex_t *preg;
  5160. const char *string;
  5161. size_t nmatch;
  5162. regmatch_t pmatch[];
  5163. int eflags;
  5164. {
  5165. int ret;
  5166. struct re_registers regs;
  5167. regex_t private_preg;
  5168. int len = strlen (string);
  5169. boolean want_reg_info = !preg->no_sub && nmatch > 0;
  5170. private_preg = *preg;
  5171. private_preg.not_bol = !!(eflags & REG_NOTBOL);
  5172. private_preg.not_eol = !!(eflags & REG_NOTEOL);
  5173. /* The user has told us exactly how many registers to return
  5174. information about, via `nmatch'. We have to pass that on to the
  5175. matching routines. */
  5176. private_preg.regs_allocated = REGS_FIXED;
  5177. if (want_reg_info)
  5178. {
  5179. regs.num_regs = nmatch;
  5180. regs.start = TALLOC (nmatch, regoff_t);
  5181. regs.end = TALLOC (nmatch, regoff_t);
  5182. if (regs.start == NULL || regs.end == NULL)
  5183. return (int) REG_NOMATCH;
  5184. }
  5185. /* Perform the searching operation. */
  5186. ret = re_search (&private_preg, string, len,
  5187. /* start: */ 0, /* range: */ len,
  5188. want_reg_info ? &regs : (struct re_registers *) 0);
  5189. /* Copy the register information to the POSIX structure. */
  5190. if (want_reg_info)
  5191. {
  5192. if (ret >= 0)
  5193. {
  5194. unsigned r;
  5195. for (r = 0; r < nmatch; r++)
  5196. {
  5197. pmatch[r].rm_so = regs.start[r];
  5198. pmatch[r].rm_eo = regs.end[r];
  5199. }
  5200. }
  5201. /* If we needed the temporary register info, free the space now. */
  5202. free (regs.start);
  5203. free (regs.end);
  5204. }
  5205. /* We want zero return to mean success, unlike `re_search'. */
  5206. return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  5207. }
  5208. /* Returns a message corresponding to an error code, ERRCODE, returned
  5209. from either regcomp or regexec. We don't use PREG here. */
  5210. size_t
  5211. regerror (errcode, preg, errbuf, errbuf_size)
  5212. int errcode;
  5213. const regex_t *preg;
  5214. char *errbuf;
  5215. size_t errbuf_size;
  5216. {
  5217. const char *msg;
  5218. size_t msg_size;
  5219. if (errcode < 0
  5220. || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
  5221. /* Only error codes returned by the rest of the code should be passed
  5222. to this routine. If we are given anything else, or if other regex
  5223. code generates an invalid error code, then the program has a bug.
  5224. Dump core so we can fix it. */
  5225. abort ();
  5226. msg = gettext (re_error_msgid[errcode]);
  5227. msg_size = strlen (msg) + 1; /* Includes the null. */
  5228. if (errbuf_size != 0)
  5229. {
  5230. if (msg_size > errbuf_size)
  5231. {
  5232. strncpy (errbuf, msg, errbuf_size - 1);
  5233. errbuf[errbuf_size - 1] = 0;
  5234. }
  5235. else
  5236. strcpy (errbuf, msg);
  5237. }
  5238. return msg_size;
  5239. }
  5240. /* Free dynamically allocated space used by PREG. */
  5241. void
  5242. regfree (preg)
  5243. regex_t *preg;
  5244. {
  5245. if (preg->buffer != NULL)
  5246. free (preg->buffer);
  5247. preg->buffer = NULL;
  5248. preg->allocated = 0;
  5249. preg->used = 0;
  5250. if (preg->fastmap != NULL)
  5251. free (preg->fastmap);
  5252. preg->fastmap = NULL;
  5253. preg->fastmap_accurate = 0;
  5254. if (preg->translate != NULL)
  5255. free (preg->translate);
  5256. preg->translate = NULL;
  5257. }
  5258. #endif /* not emacs */