regex_old.c 256 KB

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