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