regex_old.c 255 KB

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