regex_old.c 255 KB

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