regex_old.c 251 KB

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