regex_old.c 253 KB

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