regex_old.c 255 KB

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