regex.c 185 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896
  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, 94, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA. */
  18. /* AIX requires this to be the first thing in the file. */
  19. #if defined _AIX && !defined REGEX_MALLOC
  20. #pragma alloca
  21. #endif
  22. #undef _GNU_SOURCE
  23. #define _GNU_SOURCE
  24. #ifdef HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #ifdef _WIN32
  28. /* Windows does not provide unistd.h, which is required for abort() */
  29. #include <process.h>
  30. #endif /* _WIN32 */
  31. #ifndef PARAMS
  32. # if defined __GNUC__ || (defined __STDC__ && __STDC__)
  33. # define PARAMS(args) args
  34. # else
  35. # define PARAMS(args) ()
  36. # endif /* GCC. */
  37. #endif /* Not PARAMS. */
  38. #if defined STDC_HEADERS && !defined emacs
  39. # include <stddef.h>
  40. #else
  41. /* We need this for `regex.h', and perhaps for the Emacs include files. */
  42. # include <sys/types.h>
  43. #endif
  44. #define WIDE_CHAR_SUPPORT (HAVE_WCTYPE_H && HAVE_WCHAR_H && HAVE_BTOWC)
  45. /* For platform which support the ISO C amendement 1 functionality we
  46. support user defined character classes. */
  47. #if defined _LIBC || WIDE_CHAR_SUPPORT
  48. /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
  49. # include <wchar.h>
  50. # include <wctype.h>
  51. #endif
  52. #ifdef _LIBC
  53. /* We have to keep the namespace clean. */
  54. # define regfree(preg) __regfree (preg)
  55. # define regexec(pr, st, nm, pm, ef) __regexec (pr, st, nm, pm, ef)
  56. # define regcomp(preg, pattern, cflags) __regcomp (preg, pattern, cflags)
  57. # define regerror(errcode, preg, errbuf, errbuf_size) \
  58. __regerror(errcode, preg, errbuf, errbuf_size)
  59. # define re_set_registers(bu, re, nu, st, en) \
  60. __re_set_registers (bu, re, nu, st, en)
  61. # define re_match_2(bufp, string1, size1, string2, size2, pos, regs, stop) \
  62. __re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  63. # define re_match(bufp, string, size, pos, regs) \
  64. __re_match (bufp, string, size, pos, regs)
  65. # define re_search(bufp, string, size, startpos, range, regs) \
  66. __re_search (bufp, string, size, startpos, range, regs)
  67. # define re_compile_pattern(pattern, length, bufp) \
  68. __re_compile_pattern (pattern, length, bufp)
  69. # define re_set_syntax(syntax) __re_set_syntax (syntax)
  70. # define re_search_2(bufp, st1, s1, st2, s2, startpos, range, regs, stop) \
  71. __re_search_2 (bufp, st1, s1, st2, s2, startpos, range, regs, stop)
  72. # define re_compile_fastmap(bufp) __re_compile_fastmap (bufp)
  73. #define btowc __btowc
  74. #endif
  75. /* This is for other GNU distributions with internationalized messages. */
  76. #if HAVE_LIBINTL_H || defined _LIBC
  77. # include <libintl.h>
  78. #else
  79. # define gettext(msgid) (msgid)
  80. #endif
  81. #ifndef gettext_noop
  82. /* This define is so xgettext can find the internationalizable
  83. strings. */
  84. # define gettext_noop(String) String
  85. #endif
  86. /* The `emacs' switch turns on certain matching commands
  87. that make sense only in Emacs. */
  88. #ifdef emacs
  89. # include "lisp.h"
  90. # include "buffer.h"
  91. # include "syntax.h"
  92. #else /* not emacs */
  93. /* If we are not linking with Emacs proper,
  94. we can't use the relocating allocator
  95. even if config.h says that we can. */
  96. # undef REL_ALLOC
  97. # if defined STDC_HEADERS || defined _LIBC
  98. # include <stdlib.h>
  99. # else
  100. char *malloc ();
  101. char *realloc ();
  102. # endif
  103. /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
  104. If nothing else has been done, use the method below. */
  105. # ifdef INHIBIT_STRING_HEADER
  106. # if !(defined HAVE_BZERO && defined HAVE_BCOPY)
  107. # if !defined bzero && !defined bcopy
  108. # undef INHIBIT_STRING_HEADER
  109. # endif
  110. # endif
  111. # endif
  112. /* This is the normal way of making sure we have a bcopy and a bzero.
  113. This is used in most programs--a few other programs avoid this
  114. by defining INHIBIT_STRING_HEADER. */
  115. # ifndef INHIBIT_STRING_HEADER
  116. # if defined HAVE_STRING_H || defined STDC_HEADERS || defined _LIBC
  117. # include <string.h>
  118. # ifndef bzero
  119. # ifndef _LIBC
  120. # define bzero(s, n) (memset (s, '\0', n), (s))
  121. # else
  122. # define bzero(s, n) __bzero (s, n)
  123. # endif
  124. # endif
  125. # else
  126. # include <strings.h>
  127. # ifndef memcmp
  128. # define memcmp(s1, s2, n) bcmp (s1, s2, n)
  129. # endif
  130. # ifndef memcpy
  131. # define memcpy(d, s, n) (bcopy (s, d, n), (d))
  132. # endif
  133. # endif
  134. # endif
  135. /* Define the syntax stuff for \<, \>, etc. */
  136. /* This must be nonzero for the wordchar and notwordchar pattern
  137. commands in re_match_2. */
  138. # ifndef Sword
  139. # define Sword 1
  140. # endif
  141. # ifdef SWITCH_ENUM_BUG
  142. # define SWITCH_ENUM_CAST(x) ((int)(x))
  143. # else
  144. # define SWITCH_ENUM_CAST(x) (x)
  145. # endif
  146. /* How many characters in the character set. */
  147. # define CHAR_SET_SIZE 256
  148. # ifdef SYNTAX_TABLE
  149. extern char *re_syntax_table;
  150. # else /* not SYNTAX_TABLE */
  151. static char re_syntax_table[CHAR_SET_SIZE];
  152. static void
  153. init_syntax_once ()
  154. {
  155. register int c;
  156. static int done;
  157. if (done)
  158. return;
  159. memset (re_syntax_table, 0, sizeof re_syntax_table);
  160. for (c = 'a'; c <= 'z'; c++)
  161. re_syntax_table[c] = Sword;
  162. for (c = 'A'; c <= 'Z'; c++)
  163. re_syntax_table[c] = Sword;
  164. for (c = '0'; c <= '9'; c++)
  165. re_syntax_table[c] = Sword;
  166. re_syntax_table['_'] = Sword;
  167. done = 1;
  168. }
  169. # endif /* not SYNTAX_TABLE */
  170. # define SYNTAX(c) re_syntax_table[c]
  171. #endif /* not emacs */
  172. /* Get the interface, including the syntax bits. */
  173. #include <regex-gnu.h>
  174. /* isalpha etc. are used for the character classes. */
  175. #include <ctype.h>
  176. /* Jim Meyering writes:
  177. "... Some ctype macros are valid only for character codes that
  178. isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  179. using /bin/cc or gcc but without giving an ansi option). So, all
  180. ctype uses should be through macros like ISPRINT... If
  181. STDC_HEADERS is defined, then autoconf has verified that the ctype
  182. macros don't need to be guarded with references to isascii. ...
  183. Defining isascii to 1 should let any compiler worth its salt
  184. eliminate the && through constant folding."
  185. Solaris defines some of these symbols so we must undefine them first. */
  186. #undef ISASCII
  187. #if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
  188. # define ISASCII(c) 1
  189. #else
  190. # define ISASCII(c) isascii(c)
  191. #endif
  192. #ifdef isblank
  193. # define ISBLANK(c) (ISASCII (c) && isblank (c))
  194. #else
  195. # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  196. #endif
  197. #ifdef isgraph
  198. # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  199. #else
  200. # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  201. #endif
  202. #undef ISPRINT
  203. #define ISPRINT(c) (ISASCII (c) && isprint (c))
  204. #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
  205. #define ISALNUM(c) (ISASCII (c) && isalnum (c))
  206. #define ISALPHA(c) (ISASCII (c) && isalpha (c))
  207. #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
  208. #define ISLOWER(c) (ISASCII (c) && islower (c))
  209. #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
  210. #define ISSPACE(c) (ISASCII (c) && isspace (c))
  211. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  212. #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
  213. #ifdef _tolower
  214. # define TOLOWER(c) _tolower(c)
  215. #else
  216. # define TOLOWER(c) tolower(c)
  217. #endif
  218. #ifndef NULL
  219. # define NULL (void *)0
  220. #endif
  221. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  222. since ours (we hope) works properly with all combinations of
  223. machines, compilers, `char' and `unsigned char' argument types.
  224. (Per Bothner suggested the basic approach.) */
  225. #undef SIGN_EXTEND_CHAR
  226. #if __STDC__
  227. # define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  228. #else /* not __STDC__ */
  229. /* As in Harbison and Steele. */
  230. # define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  231. #endif
  232. /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
  233. use `alloca' instead of `malloc'. This is because using malloc in
  234. re_search* or re_match* could cause memory leaks when C-g is used in
  235. Emacs; also, malloc is slower and causes storage fragmentation. On
  236. the other hand, malloc is more portable, and easier to debug.
  237. Because we sometimes use alloca, some routines have to be macros,
  238. not functions -- `alloca'-allocated space disappears at the end of the
  239. function it is called in. */
  240. #ifdef REGEX_MALLOC
  241. # define REGEX_ALLOCATE malloc
  242. # define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  243. # define REGEX_FREE free
  244. #else /* not REGEX_MALLOC */
  245. /* Emacs already defines alloca, sometimes. */
  246. # ifndef alloca
  247. /* Make alloca work the best possible way. */
  248. # ifdef __GNUC__
  249. # define alloca __builtin_alloca
  250. # else /* not __GNUC__ */
  251. # if HAVE_ALLOCA_H
  252. # include <alloca.h>
  253. # endif /* HAVE_ALLOCA_H */
  254. # endif /* not __GNUC__ */
  255. # endif /* not alloca */
  256. # define REGEX_ALLOCATE alloca
  257. /* Assumes a `char *destination' variable. */
  258. # define REGEX_REALLOCATE(source, osize, nsize) \
  259. (destination = (char *) alloca (nsize), \
  260. memcpy (destination, source, osize))
  261. /* No need to do anything to free, after alloca. */
  262. # define REGEX_FREE(arg) ((void)0) /* Do nothing! But inhibit gcc warning. */
  263. #endif /* not REGEX_MALLOC */
  264. /* Define how to allocate the failure stack. */
  265. #if defined REL_ALLOC && defined REGEX_MALLOC
  266. # define REGEX_ALLOCATE_STACK(size) \
  267. r_alloc (&failure_stack_ptr, (size))
  268. # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  269. r_re_alloc (&failure_stack_ptr, (nsize))
  270. # define REGEX_FREE_STACK(ptr) \
  271. r_alloc_free (&failure_stack_ptr)
  272. #else /* not using relocating allocator */
  273. # ifdef REGEX_MALLOC
  274. # define REGEX_ALLOCATE_STACK malloc
  275. # define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
  276. # define REGEX_FREE_STACK free
  277. # else /* not REGEX_MALLOC */
  278. # define REGEX_ALLOCATE_STACK alloca
  279. # define REGEX_REALLOCATE_STACK(source, osize, nsize) \
  280. REGEX_REALLOCATE (source, osize, nsize)
  281. /* No need to explicitly free anything. */
  282. # define REGEX_FREE_STACK(arg)
  283. # endif /* not REGEX_MALLOC */
  284. #endif /* not using relocating allocator */
  285. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  286. `string1' or just past its end. This works if PTR is NULL, which is
  287. a good thing. */
  288. #define FIRST_STRING_P(ptr) \
  289. (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  290. /* (Re)Allocate N items of type T using malloc, or fail. */
  291. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  292. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  293. #define RETALLOC_IF(addr, n, t) \
  294. if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
  295. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  296. #define BYTEWIDTH 8 /* In bits. */
  297. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  298. #undef MAX
  299. #undef MIN
  300. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  301. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  302. typedef char boolean;
  303. #define false 0
  304. #define true 1
  305. static int re_match_2_internal PARAMS ((struct re_pattern_buffer *bufp,
  306. const char *string1, int size1,
  307. const char *string2, int size2,
  308. int pos,
  309. struct re_registers *regs,
  310. int stop));
  311. /* These are the command codes that appear in compiled regular
  312. expressions. Some opcodes are followed by argument bytes. A
  313. command code can specify any interpretation whatsoever for its
  314. arguments. Zero bytes may appear in the compiled regular expression. */
  315. typedef enum
  316. {
  317. no_op = 0,
  318. /* Succeed right away--no more backtracking. */
  319. succeed,
  320. /* Followed by one byte giving n, then by n literal bytes. */
  321. exactn,
  322. /* Matches any (more or less) character. */
  323. anychar,
  324. /* Matches any one char belonging to specified set. First
  325. following byte is number of bitmap bytes. Then come bytes
  326. for a bitmap saying which chars are in. Bits in each byte
  327. are ordered low-bit-first. A character is in the set if its
  328. bit is 1. A character too large to have a bit in the map is
  329. automatically not in the set. */
  330. charset,
  331. /* Same parameters as charset, but match any character that is
  332. not one of those specified. */
  333. charset_not,
  334. /* Start remembering the text that is matched, for storing in a
  335. register. Followed by one byte with the register number, in
  336. the range 0 to one less than the pattern buffer's re_nsub
  337. field. Then followed by one byte with the number of groups
  338. inner to this one. (This last has to be part of the
  339. start_memory only because we need it in the on_failure_jump
  340. of re_match_2.) */
  341. start_memory,
  342. /* Stop remembering the text that is matched and store it in a
  343. memory register. Followed by one byte with the register
  344. number, in the range 0 to one less than `re_nsub' in the
  345. pattern buffer, and one byte with the number of inner groups,
  346. just like `start_memory'. (We need the number of inner
  347. groups here because we don't have any easy way of finding the
  348. corresponding start_memory when we're at a stop_memory.) */
  349. stop_memory,
  350. /* Match a duplicate of something remembered. Followed by one
  351. byte containing the register number. */
  352. duplicate,
  353. /* Fail unless at beginning of line. */
  354. begline,
  355. /* Fail unless at end of line. */
  356. endline,
  357. /* Succeeds if at beginning of buffer (if emacs) or at beginning
  358. of string to be matched (if not). */
  359. begbuf,
  360. /* Analogously, for end of buffer/string. */
  361. endbuf,
  362. /* Followed by two byte relative address to which to jump. */
  363. jump,
  364. /* Same as jump, but marks the end of an alternative. */
  365. jump_past_alt,
  366. /* Followed by two-byte relative address of place to resume at
  367. in case of failure. */
  368. on_failure_jump,
  369. /* Like on_failure_jump, but pushes a placeholder instead of the
  370. current string position when executed. */
  371. on_failure_keep_string_jump,
  372. /* Throw away latest failure point and then jump to following
  373. two-byte relative address. */
  374. pop_failure_jump,
  375. /* Change to pop_failure_jump if know won't have to backtrack to
  376. match; otherwise change to jump. This is used to jump
  377. back to the beginning of a repeat. If what follows this jump
  378. clearly won't match what the repeat does, such that we can be
  379. sure that there is no use backtracking out of repetitions
  380. already matched, then we change it to a pop_failure_jump.
  381. Followed by two-byte address. */
  382. maybe_pop_jump,
  383. /* Jump to following two-byte address, and push a dummy failure
  384. point. This failure point will be thrown away if an attempt
  385. is made to use it for a failure. A `+' construct makes this
  386. before the first repeat. Also used as an intermediary kind
  387. of jump when compiling an alternative. */
  388. dummy_failure_jump,
  389. /* Push a dummy failure point and continue. Used at the end of
  390. alternatives. */
  391. push_dummy_failure,
  392. /* Followed by two-byte relative address and two-byte number n.
  393. After matching N times, jump to the address upon failure. */
  394. succeed_n,
  395. /* Followed by two-byte relative address, and two-byte number n.
  396. Jump to the address N times, then fail. */
  397. jump_n,
  398. /* Set the following two-byte relative address to the
  399. subsequent two-byte number. The address *includes* the two
  400. bytes of number. */
  401. set_number_at,
  402. wordchar, /* Matches any word-constituent character. */
  403. notwordchar, /* Matches any char that is not a word-constituent. */
  404. wordbeg, /* Succeeds if at word beginning. */
  405. wordend, /* Succeeds if at word end. */
  406. wordbound, /* Succeeds if at a word boundary. */
  407. notwordbound /* Succeeds if not at a word boundary. */
  408. #ifdef emacs
  409. ,before_dot, /* Succeeds if before point. */
  410. at_dot, /* Succeeds if at point. */
  411. after_dot, /* Succeeds if after point. */
  412. /* Matches any character whose syntax is specified. Followed by
  413. a byte which contains a syntax code, e.g., Sword. */
  414. syntaxspec,
  415. /* Matches any character whose syntax is not that specified. */
  416. notsyntaxspec
  417. #endif /* emacs */
  418. } re_opcode_t;
  419. /* Common operations on the compiled pattern. */
  420. /* Store NUMBER in two contiguous bytes starting at DESTINATION. */
  421. #define STORE_NUMBER(destination, number) \
  422. do { \
  423. (destination)[0] = (number) & 0377; \
  424. (destination)[1] = (number) >> 8; \
  425. } while (0)
  426. /* Same as STORE_NUMBER, except increment DESTINATION to
  427. the byte after where the number is stored. Therefore, DESTINATION
  428. must be an lvalue. */
  429. #define STORE_NUMBER_AND_INCR(destination, number) \
  430. do { \
  431. STORE_NUMBER (destination, number); \
  432. (destination) += 2; \
  433. } while (0)
  434. /* Put into DESTINATION a number stored in two contiguous bytes starting
  435. at SOURCE. */
  436. #define EXTRACT_NUMBER(destination, source) \
  437. do { \
  438. (destination) = *(source) & 0377; \
  439. (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8; \
  440. } while (0)
  441. #ifdef DEBUG
  442. static void extract_number _RE_ARGS ((int *dest, unsigned char *source));
  443. static void
  444. extract_number (dest, source)
  445. int *dest;
  446. unsigned char *source;
  447. {
  448. int temp = SIGN_EXTEND_CHAR (*(source + 1));
  449. *dest = *source & 0377;
  450. *dest += temp << 8;
  451. }
  452. # ifndef EXTRACT_MACROS /* To debug the macros. */
  453. # undef EXTRACT_NUMBER
  454. # define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  455. # endif /* not EXTRACT_MACROS */
  456. #endif /* DEBUG */
  457. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  458. SOURCE must be an lvalue. */
  459. #define EXTRACT_NUMBER_AND_INCR(destination, source) \
  460. do { \
  461. EXTRACT_NUMBER (destination, source); \
  462. (source) += 2; \
  463. } while (0)
  464. #ifdef DEBUG
  465. static void extract_number_and_incr _RE_ARGS ((int *destination,
  466. unsigned char **source));
  467. static void
  468. extract_number_and_incr (destination, source)
  469. int *destination;
  470. unsigned char **source;
  471. {
  472. extract_number (destination, *source);
  473. *source += 2;
  474. }
  475. # ifndef EXTRACT_MACROS
  476. # undef EXTRACT_NUMBER_AND_INCR
  477. # define EXTRACT_NUMBER_AND_INCR(dest, src) \
  478. extract_number_and_incr (&dest, &src)
  479. # endif /* not EXTRACT_MACROS */
  480. #endif /* DEBUG */
  481. /* If DEBUG is defined, Regex prints many voluminous messages about what
  482. it is doing (if the variable `debug' is nonzero). If linked with the
  483. main program in `iregex.c', you can enter patterns and strings
  484. interactively. And if linked with the main program in `main.c' and
  485. the other test files, you can run the already-written tests. */
  486. #ifdef DEBUG
  487. /* We use standard I/O for debugging. */
  488. # include <stdio.h>
  489. /* It is useful to test things that ``must'' be true when debugging. */
  490. # include "zassert.h"
  491. static int debug;
  492. # define DEBUG_STATEMENT(e) e
  493. # define DEBUG_PRINT1(x) if (debug) printf (x)
  494. # define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  495. # define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  496. # define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  497. # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e) \
  498. if (debug) print_partial_compiled_pattern (s, e)
  499. # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2) \
  500. if (debug) print_double_string (w, s1, sz1, s2, sz2)
  501. /* Print the fastmap in human-readable form. */
  502. void
  503. print_fastmap (fastmap)
  504. char *fastmap;
  505. {
  506. unsigned was_a_range = 0;
  507. unsigned i = 0;
  508. while (i < (1 << BYTEWIDTH))
  509. {
  510. if (fastmap[i++])
  511. {
  512. was_a_range = 0;
  513. putchar (i - 1);
  514. while (i < (1 << BYTEWIDTH) && fastmap[i])
  515. {
  516. was_a_range = 1;
  517. i++;
  518. }
  519. if (was_a_range)
  520. {
  521. printf ("-");
  522. putchar (i - 1);
  523. }
  524. }
  525. }
  526. putchar ('\n');
  527. }
  528. /* Print a compiled pattern string in human-readable form, starting at
  529. the START pointer into it and ending just before the pointer END. */
  530. void
  531. print_partial_compiled_pattern (start, end)
  532. unsigned char *start;
  533. unsigned char *end;
  534. {
  535. int mcnt, mcnt2;
  536. unsigned char *p1;
  537. unsigned char *p = start;
  538. unsigned char *pend = end;
  539. if (start == NULL)
  540. {
  541. printf ("(null)\n");
  542. return;
  543. }
  544. /* Loop over pattern commands. */
  545. while (p < pend)
  546. {
  547. printf ("%d:\t", p - start);
  548. switch ((re_opcode_t) *p++)
  549. {
  550. case no_op:
  551. printf ("/no_op");
  552. break;
  553. case exactn:
  554. mcnt = *p++;
  555. printf ("/exactn/%d", mcnt);
  556. do
  557. {
  558. putchar ('/');
  559. putchar (*p++);
  560. }
  561. while (--mcnt);
  562. break;
  563. case start_memory:
  564. mcnt = *p++;
  565. printf ("/start_memory/%d/%d", mcnt, *p++);
  566. break;
  567. case stop_memory:
  568. mcnt = *p++;
  569. printf ("/stop_memory/%d/%d", mcnt, *p++);
  570. break;
  571. case duplicate:
  572. printf ("/duplicate/%d", *p++);
  573. break;
  574. case anychar:
  575. printf ("/anychar");
  576. break;
  577. case charset:
  578. case charset_not:
  579. {
  580. register int c, last = -100;
  581. register int in_range = 0;
  582. printf ("/charset [%s",
  583. (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  584. assert (p + *p < pend);
  585. for (c = 0; c < 256; c++)
  586. if (c / 8 < *p
  587. && (p[1 + (c/8)] & (1 << (c % 8))))
  588. {
  589. /* Are we starting a range? */
  590. if (last + 1 == c && ! in_range)
  591. {
  592. putchar ('-');
  593. in_range = 1;
  594. }
  595. /* Have we broken a range? */
  596. else if (last + 1 != c && in_range)
  597. {
  598. putchar (last);
  599. in_range = 0;
  600. }
  601. if (! in_range)
  602. putchar (c);
  603. last = c;
  604. }
  605. if (in_range)
  606. putchar (last);
  607. putchar (']');
  608. p += 1 + *p;
  609. }
  610. break;
  611. case begline:
  612. printf ("/begline");
  613. break;
  614. case endline:
  615. printf ("/endline");
  616. break;
  617. case on_failure_jump:
  618. extract_number_and_incr (&mcnt, &p);
  619. printf ("/on_failure_jump to %d", p + mcnt - start);
  620. break;
  621. case on_failure_keep_string_jump:
  622. extract_number_and_incr (&mcnt, &p);
  623. printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
  624. break;
  625. case dummy_failure_jump:
  626. extract_number_and_incr (&mcnt, &p);
  627. printf ("/dummy_failure_jump to %d", p + mcnt - start);
  628. break;
  629. case push_dummy_failure:
  630. printf ("/push_dummy_failure");
  631. break;
  632. case maybe_pop_jump:
  633. extract_number_and_incr (&mcnt, &p);
  634. printf ("/maybe_pop_jump to %d", p + mcnt - start);
  635. break;
  636. case pop_failure_jump:
  637. extract_number_and_incr (&mcnt, &p);
  638. printf ("/pop_failure_jump to %d", p + mcnt - start);
  639. break;
  640. case jump_past_alt:
  641. extract_number_and_incr (&mcnt, &p);
  642. printf ("/jump_past_alt to %d", p + mcnt - start);
  643. break;
  644. case jump:
  645. extract_number_and_incr (&mcnt, &p);
  646. printf ("/jump to %d", p + mcnt - start);
  647. break;
  648. case succeed_n:
  649. extract_number_and_incr (&mcnt, &p);
  650. p1 = p + mcnt;
  651. extract_number_and_incr (&mcnt2, &p);
  652. printf ("/succeed_n to %d, %d times", p1 - start, mcnt2);
  653. break;
  654. case jump_n:
  655. extract_number_and_incr (&mcnt, &p);
  656. p1 = p + mcnt;
  657. extract_number_and_incr (&mcnt2, &p);
  658. printf ("/jump_n to %d, %d times", p1 - start, mcnt2);
  659. break;
  660. case set_number_at:
  661. extract_number_and_incr (&mcnt, &p);
  662. p1 = p + mcnt;
  663. extract_number_and_incr (&mcnt2, &p);
  664. printf ("/set_number_at location %d to %d", p1 - start, mcnt2);
  665. break;
  666. case wordbound:
  667. printf ("/wordbound");
  668. break;
  669. case notwordbound:
  670. printf ("/notwordbound");
  671. break;
  672. case wordbeg:
  673. printf ("/wordbeg");
  674. break;
  675. case wordend:
  676. printf ("/wordend");
  677. # ifdef emacs
  678. case before_dot:
  679. printf ("/before_dot");
  680. break;
  681. case at_dot:
  682. printf ("/at_dot");
  683. break;
  684. case after_dot:
  685. printf ("/after_dot");
  686. break;
  687. case syntaxspec:
  688. printf ("/syntaxspec");
  689. mcnt = *p++;
  690. printf ("/%d", mcnt);
  691. break;
  692. case notsyntaxspec:
  693. printf ("/notsyntaxspec");
  694. mcnt = *p++;
  695. printf ("/%d", mcnt);
  696. break;
  697. # endif /* emacs */
  698. case wordchar:
  699. printf ("/wordchar");
  700. break;
  701. case notwordchar:
  702. printf ("/notwordchar");
  703. break;
  704. case begbuf:
  705. printf ("/begbuf");
  706. break;
  707. case endbuf:
  708. printf ("/endbuf");
  709. break;
  710. default:
  711. printf ("?%d", *(p-1));
  712. }
  713. putchar ('\n');
  714. }
  715. printf ("%d:\tend of pattern.\n", p - start);
  716. }
  717. void
  718. print_compiled_pattern (bufp)
  719. struct re_pattern_buffer *bufp;
  720. {
  721. unsigned char *buffer = bufp->buffer;
  722. print_partial_compiled_pattern (buffer, buffer + bufp->used);
  723. printf ("%ld bytes used/%ld bytes allocated.\n",
  724. bufp->used, bufp->allocated);
  725. if (bufp->fastmap_accurate && bufp->fastmap)
  726. {
  727. printf ("fastmap: ");
  728. print_fastmap (bufp->fastmap);
  729. }
  730. printf ("re_nsub: %d\t", bufp->re_nsub);
  731. printf ("regs_alloc: %d\t", bufp->regs_allocated);
  732. printf ("can_be_null: %d\t", bufp->can_be_null);
  733. printf ("newline_anchor: %d\n", bufp->newline_anchor);
  734. printf ("no_sub: %d\t", bufp->no_sub);
  735. printf ("not_bol: %d\t", bufp->not_bol);
  736. printf ("not_eol: %d\t", bufp->not_eol);
  737. printf ("syntax: %lx\n", bufp->syntax);
  738. /* Perhaps we should print the translate table? */
  739. }
  740. void
  741. print_double_string (where, string1, size1, string2, size2)
  742. const char *where;
  743. const char *string1;
  744. const char *string2;
  745. int size1;
  746. int size2;
  747. {
  748. int this_char;
  749. if (where == NULL)
  750. printf ("(null)");
  751. else
  752. {
  753. if (FIRST_STRING_P (where))
  754. {
  755. for (this_char = where - string1; this_char < size1; this_char++)
  756. putchar (string1[this_char]);
  757. where = string2;
  758. }
  759. for (this_char = where - string2; this_char < size2; this_char++)
  760. putchar (string2[this_char]);
  761. }
  762. }
  763. void
  764. printchar (c)
  765. int c;
  766. {
  767. putc (c, stderr);
  768. }
  769. #else /* not DEBUG */
  770. # undef assert
  771. # define assert(e)
  772. # define DEBUG_STATEMENT(e)
  773. # define DEBUG_PRINT1(x)
  774. # define DEBUG_PRINT2(x1, x2)
  775. # define DEBUG_PRINT3(x1, x2, x3)
  776. # define DEBUG_PRINT4(x1, x2, x3, x4)
  777. # define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  778. # define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  779. #endif /* not DEBUG */
  780. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  781. also be assigned to arbitrarily: each pattern buffer stores its own
  782. syntax, so it can be changed between regex compilations. */
  783. /* This has no initializer because initialized variables in Emacs
  784. become read-only after dumping. */
  785. reg_syntax_t re_syntax_options;
  786. /* Specify the precise syntax of regexps for compilation. This provides
  787. for compatibility for various utilities which historically have
  788. different, incompatible syntaxes.
  789. The argument SYNTAX is a bit mask comprised of the various bits
  790. defined in regex.h. We return the old syntax. */
  791. reg_syntax_t
  792. re_set_syntax (syntax)
  793. reg_syntax_t syntax;
  794. {
  795. reg_syntax_t ret = re_syntax_options;
  796. re_syntax_options = syntax;
  797. #ifdef DEBUG
  798. if (syntax & RE_DEBUG)
  799. debug = 1;
  800. else if (debug) /* was on but now is not */
  801. debug = 0;
  802. #endif /* DEBUG */
  803. return ret;
  804. }
  805. #ifdef _LIBC
  806. weak_alias (__re_set_syntax, re_set_syntax)
  807. #endif
  808. /* This table gives an error message for each of the error codes listed
  809. in regex.h. Obviously the order here has to be same as there.
  810. POSIX doesn't require that we do anything for REG_NOERROR,
  811. but why not be nice? */
  812. static const char re_error_msgid[] =
  813. {
  814. #define REG_NOERROR_IDX 0
  815. gettext_noop ("Success") /* REG_NOERROR */
  816. "\0"
  817. #define REG_NOMATCH_IDX (REG_NOERROR_IDX + sizeof "Success")
  818. gettext_noop ("No match") /* REG_NOMATCH */
  819. "\0"
  820. #define REG_BADPAT_IDX (REG_NOMATCH_IDX + sizeof "No match")
  821. gettext_noop ("Invalid regular expression") /* REG_BADPAT */
  822. "\0"
  823. #define REG_ECOLLATE_IDX (REG_BADPAT_IDX + sizeof "Invalid regular expression")
  824. gettext_noop ("Invalid collation character") /* REG_ECOLLATE */
  825. "\0"
  826. #define REG_ECTYPE_IDX (REG_ECOLLATE_IDX + sizeof "Invalid collation character")
  827. gettext_noop ("Invalid character class name") /* REG_ECTYPE */
  828. "\0"
  829. #define REG_EESCAPE_IDX (REG_ECTYPE_IDX + sizeof "Invalid character class name")
  830. gettext_noop ("Trailing backslash") /* REG_EESCAPE */
  831. "\0"
  832. #define REG_ESUBREG_IDX (REG_EESCAPE_IDX + sizeof "Trailing backslash")
  833. gettext_noop ("Invalid back reference") /* REG_ESUBREG */
  834. "\0"
  835. #define REG_EBRACK_IDX (REG_ESUBREG_IDX + sizeof "Invalid back reference")
  836. gettext_noop ("Unmatched [ or [^") /* REG_EBRACK */
  837. "\0"
  838. #define REG_EPAREN_IDX (REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
  839. gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
  840. "\0"
  841. #define REG_EBRACE_IDX (REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
  842. gettext_noop ("Unmatched \\{") /* REG_EBRACE */
  843. "\0"
  844. #define REG_BADBR_IDX (REG_EBRACE_IDX + sizeof "Unmatched \\{")
  845. gettext_noop ("Invalid content of \\{\\}") /* REG_BADBR */
  846. "\0"
  847. #define REG_ERANGE_IDX (REG_BADBR_IDX + sizeof "Invalid content of \\{\\}")
  848. gettext_noop ("Invalid range end") /* REG_ERANGE */
  849. "\0"
  850. #define REG_ESPACE_IDX (REG_ERANGE_IDX + sizeof "Invalid range end")
  851. gettext_noop ("Memory exhausted") /* REG_ESPACE */
  852. "\0"
  853. #define REG_BADRPT_IDX (REG_ESPACE_IDX + sizeof "Memory exhausted")
  854. gettext_noop ("Invalid preceding regular expression") /* REG_BADRPT */
  855. "\0"
  856. #define REG_EEND_IDX (REG_BADRPT_IDX + sizeof "Invalid preceding regular expression")
  857. gettext_noop ("Premature end of regular expression") /* REG_EEND */
  858. "\0"
  859. #define REG_ESIZE_IDX (REG_EEND_IDX + sizeof "Premature end of regular expression")
  860. gettext_noop ("Regular expression too big") /* REG_ESIZE */
  861. "\0"
  862. #define REG_ERPAREN_IDX (REG_ESIZE_IDX + sizeof "Regular expression too big")
  863. gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
  864. };
  865. static const size_t re_error_msgid_idx[] =
  866. {
  867. REG_NOERROR_IDX,
  868. REG_NOMATCH_IDX,
  869. REG_BADPAT_IDX,
  870. REG_ECOLLATE_IDX,
  871. REG_ECTYPE_IDX,
  872. REG_EESCAPE_IDX,
  873. REG_ESUBREG_IDX,
  874. REG_EBRACK_IDX,
  875. REG_EPAREN_IDX,
  876. REG_EBRACE_IDX,
  877. REG_BADBR_IDX,
  878. REG_ERANGE_IDX,
  879. REG_ESPACE_IDX,
  880. REG_BADRPT_IDX,
  881. REG_EEND_IDX,
  882. REG_ESIZE_IDX,
  883. REG_ERPAREN_IDX
  884. };
  885. /* Avoiding alloca during matching, to placate r_alloc. */
  886. /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
  887. searching and matching functions should not call alloca. On some
  888. systems, alloca is implemented in terms of malloc, and if we're
  889. using the relocating allocator routines, then malloc could cause a
  890. relocation, which might (if the strings being searched are in the
  891. ralloc heap) shift the data out from underneath the regexp
  892. routines.
  893. Here's another reason to avoid allocation: Emacs
  894. processes input from X in a signal handler; processing X input may
  895. call malloc; if input arrives while a matching routine is calling
  896. malloc, then we're scrod. But Emacs can't just block input while
  897. calling matching routines; then we don't notice interrupts when
  898. they come in. So, Emacs blocks input around all regexp calls
  899. except the matching calls, which it leaves unprotected, in the
  900. faith that they will not malloc. */
  901. /* Normally, this is fine. */
  902. #define MATCH_MAY_ALLOCATE
  903. /* When using GNU C, we are not REALLY using the C alloca, no matter
  904. what config.h may say. So don't take precautions for it. */
  905. #ifdef __GNUC__
  906. # undef C_ALLOCA
  907. #endif
  908. /* The match routines may not allocate if (1) they would do it with malloc
  909. and (2) it's not safe for them to use malloc.
  910. Note that if REL_ALLOC is defined, matching would not use malloc for the
  911. failure stack, but we would still use it for the register vectors;
  912. so REL_ALLOC should not affect this. */
  913. #if (defined C_ALLOCA || defined REGEX_MALLOC) && defined emacs
  914. # undef MATCH_MAY_ALLOCATE
  915. #endif
  916. /* Failure stack declarations and macros; both re_compile_fastmap and
  917. re_match_2 use a failure stack. These have to be macros because of
  918. REGEX_ALLOCATE_STACK. */
  919. /* Number of failure points for which to initially allocate space
  920. when matching. If this number is exceeded, we allocate more
  921. space, so it is not a hard limit. */
  922. #ifndef INIT_FAILURE_ALLOC
  923. # define INIT_FAILURE_ALLOC 5
  924. #endif
  925. /* Roughly the maximum number of failure points on the stack. Would be
  926. exactly that if always used MAX_FAILURE_ITEMS items each time we failed.
  927. This is a variable only so users of regex can assign to it; we never
  928. change it ourselves. */
  929. #ifdef INT_IS_16BIT
  930. # if defined MATCH_MAY_ALLOCATE
  931. /* 4400 was enough to cause a crash on Alpha OSF/1,
  932. whose default stack limit is 2mb. */
  933. long int re_max_failures = 4000;
  934. # else
  935. long int re_max_failures = 2000;
  936. # endif
  937. union fail_stack_elt
  938. {
  939. unsigned char *pointer;
  940. long int integer;
  941. };
  942. typedef union fail_stack_elt fail_stack_elt_t;
  943. typedef struct
  944. {
  945. fail_stack_elt_t *stack;
  946. unsigned long int size;
  947. unsigned long int avail; /* Offset of next open position. */
  948. } fail_stack_type;
  949. #else /* not INT_IS_16BIT */
  950. # if defined MATCH_MAY_ALLOCATE
  951. /* 4400 was enough to cause a crash on Alpha OSF/1,
  952. whose default stack limit is 2mb. */
  953. int re_max_failures = 20000;
  954. # else
  955. int re_max_failures = 2000;
  956. # endif
  957. union fail_stack_elt
  958. {
  959. unsigned char *pointer;
  960. int integer;
  961. };
  962. typedef union fail_stack_elt fail_stack_elt_t;
  963. typedef struct
  964. {
  965. fail_stack_elt_t *stack;
  966. unsigned size;
  967. unsigned avail; /* Offset of next open position. */
  968. } fail_stack_type;
  969. #endif /* INT_IS_16BIT */
  970. #define FAIL_STACK_EMPTY() (fail_stack.avail == 0)
  971. #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  972. #define FAIL_STACK_FULL() (fail_stack.avail == fail_stack.size)
  973. /* Define macros to initialize and free the failure stack.
  974. Do `return -2' if the alloc fails. */
  975. #ifdef MATCH_MAY_ALLOCATE
  976. # define INIT_FAIL_STACK() \
  977. do { \
  978. fail_stack.stack = (fail_stack_elt_t *) \
  979. REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t)); \
  980. \
  981. if (fail_stack.stack == NULL) \
  982. return -2; \
  983. \
  984. fail_stack.size = INIT_FAILURE_ALLOC; \
  985. fail_stack.avail = 0; \
  986. } while (0)
  987. # define RESET_FAIL_STACK() REGEX_FREE_STACK (fail_stack.stack)
  988. #else
  989. # define INIT_FAIL_STACK() \
  990. do { \
  991. fail_stack.avail = 0; \
  992. } while (0)
  993. # define RESET_FAIL_STACK()
  994. #endif
  995. /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
  996. Return 1 if succeeds, and 0 if either ran out of memory
  997. allocating space for it or it was already too large.
  998. REGEX_REALLOCATE_STACK requires `destination' be declared. */
  999. #define DOUBLE_FAIL_STACK(fail_stack) \
  1000. ((fail_stack).size > (unsigned) (re_max_failures * MAX_FAILURE_ITEMS) \
  1001. ? 0 \
  1002. : ((fail_stack).stack = (fail_stack_elt_t *) \
  1003. REGEX_REALLOCATE_STACK ((fail_stack).stack, \
  1004. (fail_stack).size * sizeof (fail_stack_elt_t), \
  1005. ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)), \
  1006. \
  1007. (fail_stack).stack == NULL \
  1008. ? 0 \
  1009. : ((fail_stack).size <<= 1, \
  1010. 1)))
  1011. /* Push pointer POINTER on FAIL_STACK.
  1012. Return 1 if was able to do so and 0 if ran out of memory allocating
  1013. space to do so. */
  1014. #define PUSH_PATTERN_OP(POINTER, FAIL_STACK) \
  1015. ((FAIL_STACK_FULL () \
  1016. && !DOUBLE_FAIL_STACK (FAIL_STACK)) \
  1017. ? 0 \
  1018. : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER, \
  1019. 1))
  1020. /* Push a pointer value onto the failure stack.
  1021. Assumes the variable `fail_stack'. Probably should only
  1022. be called from within `PUSH_FAILURE_POINT'. */
  1023. #define PUSH_FAILURE_POINTER(item) \
  1024. fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
  1025. /* This pushes an integer-valued item onto the failure stack.
  1026. Assumes the variable `fail_stack'. Probably should only
  1027. be called from within `PUSH_FAILURE_POINT'. */
  1028. #define PUSH_FAILURE_INT(item) \
  1029. fail_stack.stack[fail_stack.avail++].integer = (item)
  1030. /* Push a fail_stack_elt_t value onto the failure stack.
  1031. Assumes the variable `fail_stack'. Probably should only
  1032. be called from within `PUSH_FAILURE_POINT'. */
  1033. #define PUSH_FAILURE_ELT(item) \
  1034. fail_stack.stack[fail_stack.avail++] = (item)
  1035. /* These three POP... operations complement the three PUSH... operations.
  1036. All assume that `fail_stack' is nonempty. */
  1037. #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
  1038. #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
  1039. #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
  1040. /* Used to omit pushing failure point id's when we're not debugging. */
  1041. #ifdef DEBUG
  1042. # define DEBUG_PUSH PUSH_FAILURE_INT
  1043. # define DEBUG_POP(item_addr) *(item_addr) = POP_FAILURE_INT ()
  1044. #else
  1045. # define DEBUG_PUSH(item)
  1046. # define DEBUG_POP(item_addr)
  1047. #endif
  1048. /* Push the information about the state we will need
  1049. if we ever fail back to it.
  1050. Requires variables fail_stack, regstart, regend, reg_info, and
  1051. num_regs_pushed be declared. DOUBLE_FAIL_STACK requires `destination'
  1052. be declared.
  1053. Does `return FAILURE_CODE' if runs out of memory. */
  1054. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code) \
  1055. do { \
  1056. char *destination; \
  1057. /* Must be int, so when we don't save any registers, the arithmetic \
  1058. of 0 + -1 isn't done as unsigned. */ \
  1059. /* Can't be int, since there is not a shred of a guarantee that int \
  1060. is wide enough to hold a value of something to which pointer can \
  1061. be assigned */ \
  1062. active_reg_t this_reg; \
  1063. \
  1064. DEBUG_STATEMENT (failure_id++); \
  1065. DEBUG_STATEMENT (nfailure_points_pushed++); \
  1066. DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id); \
  1067. DEBUG_PRINT2 (" Before push, next avail: %d\n", (fail_stack).avail);\
  1068. DEBUG_PRINT2 (" size: %d\n", (fail_stack).size);\
  1069. \
  1070. DEBUG_PRINT2 (" slots needed: %ld\n", NUM_FAILURE_ITEMS); \
  1071. DEBUG_PRINT2 (" available: %d\n", REMAINING_AVAIL_SLOTS); \
  1072. \
  1073. /* Ensure we have enough space allocated for what we will push. */ \
  1074. while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS) \
  1075. { \
  1076. if (!DOUBLE_FAIL_STACK (fail_stack)) \
  1077. return failure_code; \
  1078. \
  1079. DEBUG_PRINT2 ("\n Doubled stack; size now: %d\n", \
  1080. (fail_stack).size); \
  1081. DEBUG_PRINT2 (" slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  1082. } \
  1083. \
  1084. /* Push the info, starting with the registers. */ \
  1085. DEBUG_PRINT1 ("\n"); \
  1086. \
  1087. if (1) \
  1088. for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
  1089. this_reg++) \
  1090. { \
  1091. DEBUG_PRINT2 (" Pushing reg: %lu\n", this_reg); \
  1092. DEBUG_STATEMENT (num_regs_pushed++); \
  1093. \
  1094. DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
  1095. PUSH_FAILURE_POINTER (regstart[this_reg]); \
  1096. \
  1097. DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
  1098. PUSH_FAILURE_POINTER (regend[this_reg]); \
  1099. \
  1100. DEBUG_PRINT2 (" info: %p\n ", \
  1101. reg_info[this_reg].word.pointer); \
  1102. DEBUG_PRINT2 (" match_null=%d", \
  1103. REG_MATCH_NULL_STRING_P (reg_info[this_reg])); \
  1104. DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg])); \
  1105. DEBUG_PRINT2 (" matched_something=%d", \
  1106. MATCHED_SOMETHING (reg_info[this_reg])); \
  1107. DEBUG_PRINT2 (" ever_matched=%d", \
  1108. EVER_MATCHED_SOMETHING (reg_info[this_reg])); \
  1109. DEBUG_PRINT1 ("\n"); \
  1110. PUSH_FAILURE_ELT (reg_info[this_reg].word); \
  1111. } \
  1112. \
  1113. DEBUG_PRINT2 (" Pushing low active reg: %ld\n", lowest_active_reg);\
  1114. PUSH_FAILURE_INT (lowest_active_reg); \
  1115. \
  1116. DEBUG_PRINT2 (" Pushing high active reg: %ld\n", highest_active_reg);\
  1117. PUSH_FAILURE_INT (highest_active_reg); \
  1118. \
  1119. DEBUG_PRINT2 (" Pushing pattern %p:\n", pattern_place); \
  1120. DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend); \
  1121. PUSH_FAILURE_POINTER (pattern_place); \
  1122. \
  1123. DEBUG_PRINT2 (" Pushing string %p: `", string_place); \
  1124. DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2, \
  1125. size2); \
  1126. DEBUG_PRINT1 ("'\n"); \
  1127. PUSH_FAILURE_POINTER (string_place); \
  1128. \
  1129. DEBUG_PRINT2 (" Pushing failure id: %u\n", failure_id); \
  1130. DEBUG_PUSH (failure_id); \
  1131. } while (0)
  1132. /* This is the number of items that are pushed and popped on the stack
  1133. for each register. */
  1134. #define NUM_REG_ITEMS 3
  1135. /* Individual items aside from the registers. */
  1136. #ifdef DEBUG
  1137. # define NUM_NONREG_ITEMS 5 /* Includes failure point id. */
  1138. #else
  1139. # define NUM_NONREG_ITEMS 4
  1140. #endif
  1141. /* We push at most this many items on the stack. */
  1142. /* We used to use (num_regs - 1), which is the number of registers
  1143. this regexp will save; but that was changed to 5
  1144. to avoid stack overflow for a regexp with lots of parens. */
  1145. #define MAX_FAILURE_ITEMS (5 * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  1146. /* We actually push this many items. */
  1147. #define NUM_FAILURE_ITEMS \
  1148. (((0 \
  1149. ? 0 : highest_active_reg - lowest_active_reg + 1) \
  1150. * NUM_REG_ITEMS) \
  1151. + NUM_NONREG_ITEMS)
  1152. /* How many items can still be added to the stack without overflowing it. */
  1153. #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  1154. /* Pops what PUSH_FAIL_STACK pushes.
  1155. We restore into the parameters, all of which should be lvalues:
  1156. STR -- the saved data position.
  1157. PAT -- the saved pattern position.
  1158. LOW_REG, HIGH_REG -- the highest and lowest active registers.
  1159. REGSTART, REGEND -- arrays of string positions.
  1160. REG_INFO -- array of information about each subexpression.
  1161. Also assumes the variables `fail_stack' and (if debugging), `bufp',
  1162. `pend', `string1', `size1', `string2', and `size2'. */
  1163. #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  1164. { \
  1165. DEBUG_STATEMENT (unsigned failure_id;) \
  1166. active_reg_t this_reg; \
  1167. const unsigned char *string_temp; \
  1168. \
  1169. assert (!FAIL_STACK_EMPTY ()); \
  1170. \
  1171. /* Remove failure points and point to how many regs pushed. */ \
  1172. DEBUG_PRINT1 ("POP_FAILURE_POINT:\n"); \
  1173. DEBUG_PRINT2 (" Before pop, next avail: %d\n", fail_stack.avail); \
  1174. DEBUG_PRINT2 (" size: %d\n", fail_stack.size); \
  1175. \
  1176. assert (fail_stack.avail >= NUM_NONREG_ITEMS); \
  1177. \
  1178. DEBUG_POP (&failure_id); \
  1179. DEBUG_PRINT2 (" Popping failure id: %u\n", failure_id); \
  1180. \
  1181. /* If the saved string location is NULL, it came from an \
  1182. on_failure_keep_string_jump opcode, and we want to throw away the \
  1183. saved NULL, thus retaining our current position in the string. */ \
  1184. string_temp = POP_FAILURE_POINTER (); \
  1185. if (string_temp != NULL) \
  1186. str = (const char *) string_temp; \
  1187. \
  1188. DEBUG_PRINT2 (" Popping string %p: `", str); \
  1189. DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2); \
  1190. DEBUG_PRINT1 ("'\n"); \
  1191. \
  1192. pat = (unsigned char *) POP_FAILURE_POINTER (); \
  1193. DEBUG_PRINT2 (" Popping pattern %p:\n", pat); \
  1194. DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend); \
  1195. \
  1196. /* Restore register info. */ \
  1197. high_reg = (active_reg_t) POP_FAILURE_INT (); \
  1198. DEBUG_PRINT2 (" Popping high active reg: %ld\n", high_reg); \
  1199. \
  1200. low_reg = (active_reg_t) POP_FAILURE_INT (); \
  1201. DEBUG_PRINT2 (" Popping low active reg: %ld\n", low_reg); \
  1202. \
  1203. if (1) \
  1204. for (this_reg = high_reg; this_reg >= low_reg; this_reg--) \
  1205. { \
  1206. DEBUG_PRINT2 (" Popping reg: %ld\n", this_reg); \
  1207. \
  1208. reg_info[this_reg].word = POP_FAILURE_ELT (); \
  1209. DEBUG_PRINT2 (" info: %p\n", \
  1210. reg_info[this_reg].word.pointer); \
  1211. \
  1212. regend[this_reg] = (const char *) POP_FAILURE_POINTER (); \
  1213. DEBUG_PRINT2 (" end: %p\n", regend[this_reg]); \
  1214. \
  1215. regstart[this_reg] = (const char *) POP_FAILURE_POINTER (); \
  1216. DEBUG_PRINT2 (" start: %p\n", regstart[this_reg]); \
  1217. } \
  1218. else \
  1219. { \
  1220. for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
  1221. { \
  1222. reg_info[this_reg].word.integer = 0; \
  1223. regend[this_reg] = 0; \
  1224. regstart[this_reg] = 0; \
  1225. } \
  1226. highest_active_reg = high_reg; \
  1227. } \
  1228. \
  1229. set_regs_matched_done = 0; \
  1230. DEBUG_STATEMENT (nfailure_points_popped++); \
  1231. } /* POP_FAILURE_POINT */
  1232. /* Structure for per-register (a.k.a. per-group) information.
  1233. Other register information, such as the
  1234. starting and ending positions (which are addresses), and the list of
  1235. inner groups (which is a bits list) are maintained in separate
  1236. variables.
  1237. We are making a (strictly speaking) nonportable assumption here: that
  1238. the compiler will pack our bit fields into something that fits into
  1239. the type of `word', i.e., is something that fits into one item on the
  1240. failure stack. */
  1241. /* Declarations and macros for re_match_2. */
  1242. typedef union
  1243. {
  1244. fail_stack_elt_t word;
  1245. struct
  1246. {
  1247. /* This field is one if this group can match the empty string,
  1248. zero if not. If not yet determined, `MATCH_NULL_UNSET_VALUE'. */
  1249. #define MATCH_NULL_UNSET_VALUE 3
  1250. unsigned match_null_string_p : 2;
  1251. unsigned is_active : 1;
  1252. unsigned matched_something : 1;
  1253. unsigned ever_matched_something : 1;
  1254. } bits;
  1255. } register_info_type;
  1256. #define REG_MATCH_NULL_STRING_P(R) ((R).bits.match_null_string_p)
  1257. #define IS_ACTIVE(R) ((R).bits.is_active)
  1258. #define MATCHED_SOMETHING(R) ((R).bits.matched_something)
  1259. #define EVER_MATCHED_SOMETHING(R) ((R).bits.ever_matched_something)
  1260. /* Call this when have matched a real character; it sets `matched' flags
  1261. for the subexpressions which we are currently inside. Also records
  1262. that those subexprs have matched. */
  1263. #define SET_REGS_MATCHED() \
  1264. do \
  1265. { \
  1266. if (!set_regs_matched_done) \
  1267. { \
  1268. active_reg_t r; \
  1269. set_regs_matched_done = 1; \
  1270. for (r = lowest_active_reg; r <= highest_active_reg; r++) \
  1271. { \
  1272. MATCHED_SOMETHING (reg_info[r]) \
  1273. = EVER_MATCHED_SOMETHING (reg_info[r]) \
  1274. = 1; \
  1275. } \
  1276. } \
  1277. } \
  1278. while (0)
  1279. /* Registers are set to a sentinel when they haven't yet matched. */
  1280. static char reg_unset_dummy;
  1281. #define REG_UNSET_VALUE (&reg_unset_dummy)
  1282. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  1283. /* Subroutine declarations and macros for regex_compile. */
  1284. static reg_errcode_t regex_compile _RE_ARGS ((const char *pattern, size_t size,
  1285. reg_syntax_t syntax,
  1286. struct re_pattern_buffer *bufp));
  1287. static void store_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc, int arg));
  1288. static void store_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
  1289. int arg1, int arg2));
  1290. static void insert_op1 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
  1291. int arg, unsigned char *end));
  1292. static void insert_op2 _RE_ARGS ((re_opcode_t op, unsigned char *loc,
  1293. int arg1, int arg2, unsigned char *end));
  1294. static boolean at_begline_loc_p _RE_ARGS ((const char *pattern, const char *p,
  1295. reg_syntax_t syntax));
  1296. static boolean at_endline_loc_p _RE_ARGS ((const char *p, const char *pend,
  1297. reg_syntax_t syntax));
  1298. static reg_errcode_t compile_range _RE_ARGS ((const char **p_ptr,
  1299. const char *pend,
  1300. char *translate,
  1301. reg_syntax_t syntax,
  1302. unsigned char *b));
  1303. /* Fetch the next character in the uncompiled pattern---translating it
  1304. if necessary. Also cast from a signed character in the constant
  1305. string passed to us by the user to an unsigned char that we can use
  1306. as an array index (in, e.g., `translate'). */
  1307. #ifndef PATFETCH
  1308. # define PATFETCH(c) \
  1309. do {if (p == pend) return REG_EEND; \
  1310. c = (unsigned char) *p++; \
  1311. if (translate) c = (unsigned char) translate[c]; \
  1312. } while (0)
  1313. #endif
  1314. /* Fetch the next character in the uncompiled pattern, with no
  1315. translation. */
  1316. #define PATFETCH_RAW(c) \
  1317. do {if (p == pend) return REG_EEND; \
  1318. c = (unsigned char) *p++; \
  1319. } while (0)
  1320. /* Go backwards one character in the pattern. */
  1321. #define PATUNFETCH p--
  1322. /* If `translate' is non-null, return translate[D], else just D. We
  1323. cast the subscript to translate because some data is declared as
  1324. `char *', to avoid warnings when a string constant is passed. But
  1325. when we use a character as a subscript we must make it unsigned. */
  1326. #ifndef TRANSLATE
  1327. # define TRANSLATE(d) \
  1328. (translate ? (char) translate[(unsigned char) (d)] : (d))
  1329. #endif
  1330. /* Macros for outputting the compiled pattern into `buffer'. */
  1331. /* If the buffer isn't allocated when it comes in, use this. */
  1332. #define INIT_BUF_SIZE 32
  1333. /* Make sure we have at least N more bytes of space in buffer. */
  1334. #define GET_BUFFER_SPACE(n) \
  1335. while ((unsigned long) (b - bufp->buffer + (n)) > bufp->allocated) \
  1336. EXTEND_BUFFER ()
  1337. /* Make sure we have one more byte of buffer space and then add C to it. */
  1338. #define BUF_PUSH(c) \
  1339. do { \
  1340. GET_BUFFER_SPACE (1); \
  1341. *b++ = (unsigned char) (c); \
  1342. } while (0)
  1343. /* Ensure we have two more bytes of buffer space and then append C1 and C2. */
  1344. #define BUF_PUSH_2(c1, c2) \
  1345. do { \
  1346. GET_BUFFER_SPACE (2); \
  1347. *b++ = (unsigned char) (c1); \
  1348. *b++ = (unsigned char) (c2); \
  1349. } while (0)
  1350. /* As with BUF_PUSH_2, except for three bytes. */
  1351. #define BUF_PUSH_3(c1, c2, c3) \
  1352. do { \
  1353. GET_BUFFER_SPACE (3); \
  1354. *b++ = (unsigned char) (c1); \
  1355. *b++ = (unsigned char) (c2); \
  1356. *b++ = (unsigned char) (c3); \
  1357. } while (0)
  1358. /* Store a jump with opcode OP at LOC to location TO. We store a
  1359. relative address offset by the three bytes the jump itself occupies. */
  1360. #define STORE_JUMP(op, loc, to) \
  1361. store_op1 (op, loc, (int) ((to) - (loc) - 3))
  1362. /* Likewise, for a two-argument jump. */
  1363. #define STORE_JUMP2(op, loc, to, arg) \
  1364. store_op2 (op, loc, (int) ((to) - (loc) - 3), arg)
  1365. /* Like `STORE_JUMP', but for inserting. Assume `b' is the buffer end. */
  1366. #define INSERT_JUMP(op, loc, to) \
  1367. insert_op1 (op, loc, (int) ((to) - (loc) - 3), b)
  1368. /* Like `STORE_JUMP2', but for inserting. Assume `b' is the buffer end. */
  1369. #define INSERT_JUMP2(op, loc, to, arg) \
  1370. insert_op2 (op, loc, (int) ((to) - (loc) - 3), arg, b)
  1371. /* This is not an arbitrary limit: the arguments which represent offsets
  1372. into the pattern are two bytes long. So if 2^16 bytes turns out to
  1373. be too small, many things would have to change. */
  1374. /* Any other compiler which, like MSC, has allocation limit below 2^16
  1375. bytes will have to use approach similar to what was done below for
  1376. MSC and drop MAX_BUF_SIZE a bit. Otherwise you may end up
  1377. reallocating to 0 bytes. Such thing is not going to work too well.
  1378. You have been warned!! */
  1379. #if defined _MSC_VER && !defined _WIN32
  1380. /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes.
  1381. The REALLOC define eliminates a flurry of conversion warnings,
  1382. but is not required. */
  1383. # define MAX_BUF_SIZE 65500L
  1384. # define REALLOC(p,s) realloc ((p), (size_t) (s))
  1385. #else
  1386. # define MAX_BUF_SIZE (1L << 16)
  1387. # define REALLOC(p,s) realloc ((p), (s))
  1388. #endif
  1389. /* Extend the buffer by twice its current size via realloc and
  1390. reset the pointers that pointed into the old block to point to the
  1391. correct places in the new one. If extending the buffer results in it
  1392. being larger than MAX_BUF_SIZE, then flag memory exhausted. */
  1393. #define EXTEND_BUFFER() \
  1394. do { \
  1395. unsigned char *old_buffer = bufp->buffer; \
  1396. if (bufp->allocated == MAX_BUF_SIZE) \
  1397. return REG_ESIZE; \
  1398. bufp->allocated <<= 1; \
  1399. if (bufp->allocated > MAX_BUF_SIZE) \
  1400. bufp->allocated = MAX_BUF_SIZE; \
  1401. bufp->buffer = (unsigned char *) REALLOC (bufp->buffer, bufp->allocated);\
  1402. if (bufp->buffer == NULL) \
  1403. return REG_ESPACE; \
  1404. /* If the buffer moved, move all the pointers into it. */ \
  1405. if (old_buffer != bufp->buffer) \
  1406. { \
  1407. b = (b - old_buffer) + bufp->buffer; \
  1408. begalt = (begalt - old_buffer) + bufp->buffer; \
  1409. if (fixup_alt_jump) \
  1410. fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  1411. if (laststart) \
  1412. laststart = (laststart - old_buffer) + bufp->buffer; \
  1413. if (pending_exact) \
  1414. pending_exact = (pending_exact - old_buffer) + bufp->buffer; \
  1415. } \
  1416. } while (0)
  1417. /* Since we have one byte reserved for the register number argument to
  1418. {start,stop}_memory, the maximum number of groups we can report
  1419. things about is what fits in that byte. */
  1420. #define MAX_REGNUM 255
  1421. /* But patterns can have more than `MAX_REGNUM' registers. We just
  1422. ignore the excess. */
  1423. typedef unsigned regnum_t;
  1424. /* Macros for the compile stack. */
  1425. /* Since offsets can go either forwards or backwards, this type needs to
  1426. be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1. */
  1427. /* int may be not enough when sizeof(int) == 2. */
  1428. typedef long pattern_offset_t;
  1429. typedef struct
  1430. {
  1431. pattern_offset_t begalt_offset;
  1432. pattern_offset_t fixup_alt_jump;
  1433. pattern_offset_t inner_group_offset;
  1434. pattern_offset_t laststart_offset;
  1435. regnum_t regnum;
  1436. } compile_stack_elt_t;
  1437. typedef struct
  1438. {
  1439. compile_stack_elt_t *stack;
  1440. unsigned size;
  1441. unsigned avail; /* Offset of next open position. */
  1442. } compile_stack_type;
  1443. #define INIT_COMPILE_STACK_SIZE 32
  1444. #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
  1445. #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
  1446. /* The next available element. */
  1447. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1448. /* Set the bit for character C in a list. */
  1449. #define SET_LIST_BIT(c) \
  1450. (b[((unsigned char) (c)) / BYTEWIDTH] \
  1451. |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1452. /* Get the next unsigned number in the uncompiled pattern. */
  1453. #define GET_UNSIGNED_NUMBER(num) \
  1454. { if (p != pend) \
  1455. { \
  1456. PATFETCH (c); \
  1457. while (ISDIGIT (c)) \
  1458. { \
  1459. if (num < 0) \
  1460. num = 0; \
  1461. num = num * 10 + c - '0'; \
  1462. if (p == pend) \
  1463. break; \
  1464. PATFETCH (c); \
  1465. } \
  1466. } \
  1467. }
  1468. #if defined _LIBC || WIDE_CHAR_SUPPORT
  1469. /* The GNU C library provides support for user-defined character classes
  1470. and the functions from ISO C amendement 1. */
  1471. # ifdef CHARCLASS_NAME_MAX
  1472. # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
  1473. # else
  1474. /* This shouldn't happen but some implementation might still have this
  1475. problem. Use a reasonable default value. */
  1476. # define CHAR_CLASS_MAX_LENGTH 256
  1477. # endif
  1478. # ifdef _LIBC
  1479. # define IS_CHAR_CLASS(string) __wctype (string)
  1480. # else
  1481. # define IS_CHAR_CLASS(string) wctype (string)
  1482. # endif
  1483. #else
  1484. # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  1485. # define IS_CHAR_CLASS(string) \
  1486. (STREQ (string, "alpha") || STREQ (string, "upper") \
  1487. || STREQ (string, "lower") || STREQ (string, "digit") \
  1488. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  1489. || STREQ (string, "space") || STREQ (string, "print") \
  1490. || STREQ (string, "punct") || STREQ (string, "graph") \
  1491. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1492. #endif
  1493. #ifndef MATCH_MAY_ALLOCATE
  1494. /* If we cannot allocate large objects within re_match_2_internal,
  1495. we make the fail stack and register vectors global.
  1496. The fail stack, we grow to the maximum size when a regexp
  1497. is compiled.
  1498. The register vectors, we adjust in size each time we
  1499. compile a regexp, according to the number of registers it needs. */
  1500. static fail_stack_type fail_stack;
  1501. /* Size with which the following vectors are currently allocated.
  1502. That is so we can make them bigger as needed,
  1503. but never make them smaller. */
  1504. static int regs_allocated_size;
  1505. static const char ** regstart, ** regend;
  1506. static const char ** old_regstart, ** old_regend;
  1507. static const char **best_regstart, **best_regend;
  1508. static register_info_type *reg_info;
  1509. static const char **reg_dummy;
  1510. static register_info_type *reg_info_dummy;
  1511. /* Make the register vectors big enough for NUM_REGS registers,
  1512. but don't make them smaller. */
  1513. static
  1514. regex_grow_registers (num_regs)
  1515. int num_regs;
  1516. {
  1517. if (num_regs > regs_allocated_size)
  1518. {
  1519. RETALLOC_IF (regstart, num_regs, const char *);
  1520. RETALLOC_IF (regend, num_regs, const char *);
  1521. RETALLOC_IF (old_regstart, num_regs, const char *);
  1522. RETALLOC_IF (old_regend, num_regs, const char *);
  1523. RETALLOC_IF (best_regstart, num_regs, const char *);
  1524. RETALLOC_IF (best_regend, num_regs, const char *);
  1525. RETALLOC_IF (reg_info, num_regs, register_info_type);
  1526. RETALLOC_IF (reg_dummy, num_regs, const char *);
  1527. RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
  1528. regs_allocated_size = num_regs;
  1529. }
  1530. }
  1531. #endif /* not MATCH_MAY_ALLOCATE */
  1532. static boolean group_in_compile_stack _RE_ARGS ((compile_stack_type
  1533. compile_stack,
  1534. regnum_t regnum));
  1535. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1536. Returns one of error codes defined in `regex.h', or zero for success.
  1537. Assumes the `allocated' (and perhaps `buffer') and `translate'
  1538. fields are set in BUFP on entry.
  1539. If it succeeds, results are put in BUFP (if it returns an error, the
  1540. contents of BUFP are undefined):
  1541. `buffer' is the compiled pattern;
  1542. `syntax' is set to SYNTAX;
  1543. `used' is set to the length of the compiled pattern;
  1544. `fastmap_accurate' is zero;
  1545. `re_nsub' is the number of subexpressions in PATTERN;
  1546. `not_bol' and `not_eol' are zero;
  1547. The `fastmap' and `newline_anchor' fields are neither
  1548. examined nor set. */
  1549. /* Return, freeing storage we allocated. */
  1550. #define FREE_STACK_RETURN(value) \
  1551. return (free (compile_stack.stack), value)
  1552. static reg_errcode_t
  1553. regex_compile (pattern, size, syntax, bufp)
  1554. const char *pattern;
  1555. size_t size;
  1556. reg_syntax_t syntax;
  1557. struct re_pattern_buffer *bufp;
  1558. {
  1559. /* We fetch characters from PATTERN here. Even though PATTERN is
  1560. `char *' (i.e., signed), we declare these variables as unsigned, so
  1561. they can be reliably used as array indices. */
  1562. register unsigned char c, c1;
  1563. /* A random temporary spot in PATTERN. */
  1564. const char *p1;
  1565. /* Points to the end of the buffer, where we should append. */
  1566. register unsigned char *b;
  1567. /* Keeps track of unclosed groups. */
  1568. compile_stack_type compile_stack;
  1569. /* Points to the current (ending) position in the pattern. */
  1570. const char *p = pattern;
  1571. const char *pend = pattern + size;
  1572. /* How to translate the characters in the pattern. */
  1573. RE_TRANSLATE_TYPE translate = bufp->translate;
  1574. /* Address of the count-byte of the most recently inserted `exactn'
  1575. command. This makes it possible to tell if a new exact-match
  1576. character can be added to that command or if the character requires
  1577. a new `exactn' command. */
  1578. unsigned char *pending_exact = 0;
  1579. /* Address of start of the most recently finished expression.
  1580. This tells, e.g., postfix * where to find the start of its
  1581. operand. Reset at the beginning of groups and alternatives. */
  1582. unsigned char *laststart = 0;
  1583. /* Address of beginning of regexp, or inside of last group. */
  1584. unsigned char *begalt;
  1585. /* Place in the uncompiled pattern (i.e., the {) to
  1586. which to go back if the interval is invalid. */
  1587. const char *beg_interval;
  1588. /* Address of the place where a forward jump should go to the end of
  1589. the containing expression. Each alternative of an `or' -- except the
  1590. last -- ends with a forward jump of this sort. */
  1591. unsigned char *fixup_alt_jump = 0;
  1592. /* Counts open-groups as they are encountered. Remembered for the
  1593. matching close-group on the compile stack, so the same register
  1594. number is put in the stop_memory as the start_memory. */
  1595. regnum_t regnum = 0;
  1596. #ifdef DEBUG
  1597. DEBUG_PRINT1 ("\nCompiling pattern: ");
  1598. if (debug)
  1599. {
  1600. unsigned debug_count;
  1601. for (debug_count = 0; debug_count < size; debug_count++)
  1602. putchar (pattern[debug_count]);
  1603. putchar ('\n');
  1604. }
  1605. #endif /* DEBUG */
  1606. /* Initialize the compile stack. */
  1607. compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1608. if (compile_stack.stack == NULL)
  1609. return REG_ESPACE;
  1610. compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1611. compile_stack.avail = 0;
  1612. /* Initialize the pattern buffer. */
  1613. bufp->syntax = syntax;
  1614. bufp->fastmap_accurate = 0;
  1615. bufp->not_bol = bufp->not_eol = 0;
  1616. /* Set `used' to zero, so that if we return an error, the pattern
  1617. printer (for debugging) will think there's no pattern. We reset it
  1618. at the end. */
  1619. bufp->used = 0;
  1620. /* Always count groups, whether or not bufp->no_sub is set. */
  1621. bufp->re_nsub = 0;
  1622. #if !defined emacs && !defined SYNTAX_TABLE
  1623. /* Initialize the syntax table. */
  1624. init_syntax_once ();
  1625. #endif
  1626. if (bufp->allocated == 0)
  1627. {
  1628. if (bufp->buffer)
  1629. { /* If zero allocated, but buffer is non-null, try to realloc
  1630. enough space. This loses if buffer's address is bogus, but
  1631. that is the user's responsibility. */
  1632. RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1633. }
  1634. else
  1635. { /* Caller did not allocate a buffer. Do it for them. */
  1636. bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1637. }
  1638. if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
  1639. bufp->allocated = INIT_BUF_SIZE;
  1640. }
  1641. begalt = b = bufp->buffer;
  1642. /* Loop through the uncompiled pattern until we're at the end. */
  1643. while (p != pend)
  1644. {
  1645. PATFETCH (c);
  1646. switch (c)
  1647. {
  1648. case '^':
  1649. {
  1650. if ( /* If at start of pattern, it's an operator. */
  1651. p == pattern + 1
  1652. /* If context independent, it's an operator. */
  1653. || syntax & RE_CONTEXT_INDEP_ANCHORS
  1654. /* Otherwise, depends on what's come before. */
  1655. || at_begline_loc_p (pattern, p, syntax))
  1656. BUF_PUSH (begline);
  1657. else
  1658. goto normal_char;
  1659. }
  1660. break;
  1661. case '$':
  1662. {
  1663. if ( /* If at end of pattern, it's an operator. */
  1664. p == pend
  1665. /* If context independent, it's an operator. */
  1666. || syntax & RE_CONTEXT_INDEP_ANCHORS
  1667. /* Otherwise, depends on what's next. */
  1668. || at_endline_loc_p (p, pend, syntax))
  1669. BUF_PUSH (endline);
  1670. else
  1671. goto normal_char;
  1672. }
  1673. break;
  1674. case '+':
  1675. case '?':
  1676. if ((syntax & RE_BK_PLUS_QM)
  1677. || (syntax & RE_LIMITED_OPS))
  1678. goto normal_char;
  1679. handle_plus:
  1680. case '*':
  1681. /* If there is no previous pattern... */
  1682. if (!laststart)
  1683. {
  1684. if (syntax & RE_CONTEXT_INVALID_OPS)
  1685. FREE_STACK_RETURN (REG_BADRPT);
  1686. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1687. goto normal_char;
  1688. }
  1689. {
  1690. /* Are we optimizing this jump? */
  1691. boolean keep_string_p = false;
  1692. /* 1 means zero (many) matches is allowed. */
  1693. char zero_times_ok = 0, many_times_ok = 0;
  1694. /* If there is a sequence of repetition chars, collapse it
  1695. down to just one (the right one). We can't combine
  1696. interval operators with these because of, e.g., `a{2}*',
  1697. which should only match an even number of `a's. */
  1698. for (;;)
  1699. {
  1700. zero_times_ok |= c != '+';
  1701. many_times_ok |= c != '?';
  1702. if (p == pend)
  1703. break;
  1704. PATFETCH (c);
  1705. if (c == '*'
  1706. || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1707. ;
  1708. else if (syntax & RE_BK_PLUS_QM && c == '\\')
  1709. {
  1710. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1711. PATFETCH (c1);
  1712. if (!(c1 == '+' || c1 == '?'))
  1713. {
  1714. PATUNFETCH;
  1715. PATUNFETCH;
  1716. break;
  1717. }
  1718. c = c1;
  1719. }
  1720. else
  1721. {
  1722. PATUNFETCH;
  1723. break;
  1724. }
  1725. /* If we get here, we found another repeat character. */
  1726. }
  1727. /* Star, etc. applied to an empty pattern is equivalent
  1728. to an empty pattern. */
  1729. if (!laststart)
  1730. break;
  1731. /* Now we know whether or not zero matches is allowed
  1732. and also whether or not two or more matches is allowed. */
  1733. if (many_times_ok)
  1734. { /* More than one repetition is allowed, so put in at the
  1735. end a backward relative jump from `b' to before the next
  1736. jump we're going to put in below (which jumps from
  1737. laststart to after this jump).
  1738. But if we are at the `*' in the exact sequence `.*\n',
  1739. insert an unconditional jump backwards to the .,
  1740. instead of the beginning of the loop. This way we only
  1741. push a failure point once, instead of every time
  1742. through the loop. */
  1743. assert (p - 1 > pattern);
  1744. /* Allocate the space for the jump. */
  1745. GET_BUFFER_SPACE (3);
  1746. /* We know we are not at the first character of the pattern,
  1747. because laststart was nonzero. And we've already
  1748. incremented `p', by the way, to be the character after
  1749. the `*'. Do we have to do something analogous here
  1750. for null bytes, because of RE_DOT_NOT_NULL? */
  1751. if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  1752. && zero_times_ok
  1753. && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  1754. && !(syntax & RE_DOT_NEWLINE))
  1755. { /* We have .*\n. */
  1756. STORE_JUMP (jump, b, laststart);
  1757. keep_string_p = true;
  1758. }
  1759. else
  1760. /* Anything else. */
  1761. STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1762. /* We've added more stuff to the buffer. */
  1763. b += 3;
  1764. }
  1765. /* On failure, jump from laststart to b + 3, which will be the
  1766. end of the buffer after this jump is inserted. */
  1767. GET_BUFFER_SPACE (3);
  1768. INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1769. : on_failure_jump,
  1770. laststart, b + 3);
  1771. pending_exact = 0;
  1772. b += 3;
  1773. if (!zero_times_ok)
  1774. {
  1775. /* At least one repetition is required, so insert a
  1776. `dummy_failure_jump' before the initial
  1777. `on_failure_jump' instruction of the loop. This
  1778. effects a skip over that instruction the first time
  1779. we hit that loop. */
  1780. GET_BUFFER_SPACE (3);
  1781. INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  1782. b += 3;
  1783. }
  1784. }
  1785. break;
  1786. case '.':
  1787. laststart = b;
  1788. BUF_PUSH (anychar);
  1789. break;
  1790. case '[':
  1791. {
  1792. boolean had_char_class = false;
  1793. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1794. /* Ensure that we have enough space to push a charset: the
  1795. opcode, the length count, and the bitset; 34 bytes in all. */
  1796. GET_BUFFER_SPACE (34);
  1797. laststart = b;
  1798. /* We test `*p == '^' twice, instead of using an if
  1799. statement, so we only need one BUF_PUSH. */
  1800. BUF_PUSH (*p == '^' ? charset_not : charset);
  1801. if (*p == '^')
  1802. p++;
  1803. /* Remember the first position in the bracket expression. */
  1804. p1 = p;
  1805. /* Push the number of bytes in the bitmap. */
  1806. BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  1807. /* Clear the whole map. */
  1808. memset (b, 0, (1 << BYTEWIDTH) / BYTEWIDTH);
  1809. /* charset_not matches newline according to a syntax bit. */
  1810. if ((re_opcode_t) b[-2] == charset_not
  1811. && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  1812. SET_LIST_BIT ('\n');
  1813. /* Read in characters and ranges, setting map bits. */
  1814. for (;;)
  1815. {
  1816. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1817. PATFETCH (c);
  1818. /* \ might escape characters inside [...] and [^...]. */
  1819. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  1820. {
  1821. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1822. PATFETCH (c1);
  1823. SET_LIST_BIT (c1);
  1824. continue;
  1825. }
  1826. /* Could be the end of the bracket expression. If it's
  1827. not (i.e., when the bracket expression is `[]' so
  1828. far), the ']' character bit gets set way below. */
  1829. if (c == ']' && p != p1 + 1)
  1830. break;
  1831. /* Look ahead to see if it's a range when the last thing
  1832. was a character class. */
  1833. if (had_char_class && c == '-' && *p != ']')
  1834. FREE_STACK_RETURN (REG_ERANGE);
  1835. /* Look ahead to see if it's a range when the last thing
  1836. was a character: if this is a hyphen not at the
  1837. beginning or the end of a list, then it's the range
  1838. operator. */
  1839. if (c == '-'
  1840. && !(p - 2 >= pattern && p[-2] == '[')
  1841. && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  1842. && *p != ']')
  1843. {
  1844. reg_errcode_t ret
  1845. = compile_range (&p, pend, translate, syntax, b);
  1846. if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  1847. }
  1848. else if (p[0] == '-' && p[1] != ']')
  1849. { /* This handles ranges made up of characters only. */
  1850. reg_errcode_t ret;
  1851. /* Move past the `-'. */
  1852. PATFETCH (c1);
  1853. ret = compile_range (&p, pend, translate, syntax, b);
  1854. if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  1855. }
  1856. /* See if we're at the beginning of a possible character
  1857. class. */
  1858. else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  1859. { /* Leave room for the null. */
  1860. char str[CHAR_CLASS_MAX_LENGTH + 1];
  1861. PATFETCH (c);
  1862. c1 = 0;
  1863. /* If pattern is `[[:'. */
  1864. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1865. for (;;)
  1866. {
  1867. PATFETCH (c);
  1868. if ((c == ':' && *p == ']') || p == pend)
  1869. break;
  1870. if (c1 < CHAR_CLASS_MAX_LENGTH)
  1871. str[c1++] = c;
  1872. else
  1873. /* This is in any case an invalid class name. */
  1874. str[0] = '\0';
  1875. }
  1876. str[c1] = '\0';
  1877. /* If isn't a word bracketed by `[:' and `:]':
  1878. undo the ending character, the letters, and leave
  1879. the leading `:' and `[' (but set bits for them). */
  1880. if (c == ':' && *p == ']')
  1881. {
  1882. #if defined _LIBC || WIDE_CHAR_SUPPORT
  1883. boolean is_lower = STREQ (str, "lower");
  1884. boolean is_upper = STREQ (str, "upper");
  1885. wctype_t wt;
  1886. int ch;
  1887. wt = IS_CHAR_CLASS (str);
  1888. if (wt == 0)
  1889. FREE_STACK_RETURN (REG_ECTYPE);
  1890. /* Throw away the ] at the end of the character
  1891. class. */
  1892. PATFETCH (c);
  1893. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1894. for (ch = 0; ch < 1 << BYTEWIDTH; ++ch)
  1895. {
  1896. # ifdef _LIBC
  1897. if (__iswctype (__btowc (ch), wt))
  1898. SET_LIST_BIT (ch);
  1899. # else
  1900. if (iswctype (btowc (ch), wt))
  1901. SET_LIST_BIT (ch);
  1902. # endif
  1903. if (translate && (is_upper || is_lower)
  1904. && (ISUPPER (ch) || ISLOWER (ch)))
  1905. SET_LIST_BIT (ch);
  1906. }
  1907. had_char_class = true;
  1908. #else
  1909. int ch;
  1910. boolean is_alnum = STREQ (str, "alnum");
  1911. boolean is_alpha = STREQ (str, "alpha");
  1912. boolean is_blank = STREQ (str, "blank");
  1913. boolean is_cntrl = STREQ (str, "cntrl");
  1914. boolean is_digit = STREQ (str, "digit");
  1915. boolean is_graph = STREQ (str, "graph");
  1916. boolean is_lower = STREQ (str, "lower");
  1917. boolean is_print = STREQ (str, "print");
  1918. boolean is_punct = STREQ (str, "punct");
  1919. boolean is_space = STREQ (str, "space");
  1920. boolean is_upper = STREQ (str, "upper");
  1921. boolean is_xdigit = STREQ (str, "xdigit");
  1922. if (!IS_CHAR_CLASS (str))
  1923. FREE_STACK_RETURN (REG_ECTYPE);
  1924. /* Throw away the ] at the end of the character
  1925. class. */
  1926. PATFETCH (c);
  1927. if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  1928. for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  1929. {
  1930. /* This was split into 3 if's to
  1931. avoid an arbitrary limit in some compiler. */
  1932. if ( (is_alnum && ISALNUM (ch))
  1933. || (is_alpha && ISALPHA (ch))
  1934. || (is_blank && ISBLANK (ch))
  1935. || (is_cntrl && ISCNTRL (ch)))
  1936. SET_LIST_BIT (ch);
  1937. if ( (is_digit && ISDIGIT (ch))
  1938. || (is_graph && ISGRAPH (ch))
  1939. || (is_lower && ISLOWER (ch))
  1940. || (is_print && ISPRINT (ch)))
  1941. SET_LIST_BIT (ch);
  1942. if ( (is_punct && ISPUNCT (ch))
  1943. || (is_space && ISSPACE (ch))
  1944. || (is_upper && ISUPPER (ch))
  1945. || (is_xdigit && ISXDIGIT (ch)))
  1946. SET_LIST_BIT (ch);
  1947. if ( translate && (is_upper || is_lower)
  1948. && (ISUPPER (ch) || ISLOWER (ch)))
  1949. SET_LIST_BIT (ch);
  1950. }
  1951. had_char_class = true;
  1952. #endif /* libc || wctype.h */
  1953. }
  1954. else
  1955. {
  1956. c1++;
  1957. while (c1--)
  1958. PATUNFETCH;
  1959. SET_LIST_BIT ('[');
  1960. SET_LIST_BIT (':');
  1961. had_char_class = false;
  1962. }
  1963. }
  1964. else
  1965. {
  1966. had_char_class = false;
  1967. SET_LIST_BIT (c);
  1968. }
  1969. }
  1970. /* Discard any (non)matching list bytes that are all 0 at the
  1971. end of the map. Decrease the map-length byte too. */
  1972. while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  1973. b[-1]--;
  1974. b += b[-1];
  1975. }
  1976. break;
  1977. case '(':
  1978. if (syntax & RE_NO_BK_PARENS)
  1979. goto handle_open;
  1980. else
  1981. goto normal_char;
  1982. case ')':
  1983. if (syntax & RE_NO_BK_PARENS)
  1984. goto handle_close;
  1985. else
  1986. goto normal_char;
  1987. case '\n':
  1988. if (syntax & RE_NEWLINE_ALT)
  1989. goto handle_alt;
  1990. else
  1991. goto normal_char;
  1992. case '|':
  1993. if (syntax & RE_NO_BK_VBAR)
  1994. goto handle_alt;
  1995. else
  1996. goto normal_char;
  1997. case '{':
  1998. if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  1999. goto handle_interval;
  2000. else
  2001. goto normal_char;
  2002. case '\\':
  2003. if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2004. /* Do not translate the character after the \, so that we can
  2005. distinguish, e.g., \B from \b, even if we normally would
  2006. translate, e.g., B to b. */
  2007. PATFETCH_RAW (c);
  2008. switch (c)
  2009. {
  2010. case '(':
  2011. if (syntax & RE_NO_BK_PARENS)
  2012. goto normal_backslash;
  2013. handle_open:
  2014. bufp->re_nsub++;
  2015. regnum++;
  2016. if (COMPILE_STACK_FULL)
  2017. {
  2018. RETALLOC (compile_stack.stack, compile_stack.size << 1,
  2019. compile_stack_elt_t);
  2020. if (compile_stack.stack == NULL) return REG_ESPACE;
  2021. compile_stack.size <<= 1;
  2022. }
  2023. /* These are the values to restore when we hit end of this
  2024. group. They are all relative offsets, so that if the
  2025. whole pattern moves because of realloc, they will still
  2026. be valid. */
  2027. COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  2028. COMPILE_STACK_TOP.fixup_alt_jump
  2029. = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  2030. COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  2031. COMPILE_STACK_TOP.regnum = regnum;
  2032. /* We will eventually replace the 0 with the number of
  2033. groups inner to this one. But do not push a
  2034. start_memory for groups beyond the last one we can
  2035. represent in the compiled pattern. */
  2036. if (regnum <= MAX_REGNUM)
  2037. {
  2038. COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  2039. BUF_PUSH_3 (start_memory, regnum, 0);
  2040. }
  2041. compile_stack.avail++;
  2042. fixup_alt_jump = 0;
  2043. laststart = 0;
  2044. begalt = b;
  2045. /* If we've reached MAX_REGNUM groups, then this open
  2046. won't actually generate any code, so we'll have to
  2047. clear pending_exact explicitly. */
  2048. pending_exact = 0;
  2049. break;
  2050. case ')':
  2051. if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  2052. if (COMPILE_STACK_EMPTY)
  2053. {
  2054. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2055. goto normal_backslash;
  2056. else
  2057. FREE_STACK_RETURN (REG_ERPAREN);
  2058. }
  2059. handle_close:
  2060. if (fixup_alt_jump)
  2061. { /* Push a dummy failure point at the end of the
  2062. alternative for a possible future
  2063. `pop_failure_jump' to pop. See comments at
  2064. `push_dummy_failure' in `re_match_2'. */
  2065. BUF_PUSH (push_dummy_failure);
  2066. /* We allocated space for this jump when we assigned
  2067. to `fixup_alt_jump', in the `handle_alt' case below. */
  2068. STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  2069. }
  2070. /* See similar code for backslashed left paren above. */
  2071. if (COMPILE_STACK_EMPTY)
  2072. {
  2073. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2074. goto normal_char;
  2075. else
  2076. FREE_STACK_RETURN (REG_ERPAREN);
  2077. }
  2078. /* Since we just checked for an empty stack above, this
  2079. ``can't happen''. */
  2080. assert (compile_stack.avail != 0);
  2081. {
  2082. /* We don't just want to restore into `regnum', because
  2083. later groups should continue to be numbered higher,
  2084. as in `(ab)c(de)' -- the second group is #2. */
  2085. regnum_t this_group_regnum;
  2086. compile_stack.avail--;
  2087. begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  2088. fixup_alt_jump
  2089. = COMPILE_STACK_TOP.fixup_alt_jump
  2090. ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
  2091. : 0;
  2092. laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  2093. this_group_regnum = COMPILE_STACK_TOP.regnum;
  2094. /* If we've reached MAX_REGNUM groups, then this open
  2095. won't actually generate any code, so we'll have to
  2096. clear pending_exact explicitly. */
  2097. pending_exact = 0;
  2098. /* We're at the end of the group, so now we know how many
  2099. groups were inside this one. */
  2100. if (this_group_regnum <= MAX_REGNUM)
  2101. {
  2102. unsigned char *inner_group_loc
  2103. = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  2104. *inner_group_loc = regnum - this_group_regnum;
  2105. BUF_PUSH_3 (stop_memory, this_group_regnum,
  2106. regnum - this_group_regnum);
  2107. }
  2108. }
  2109. break;
  2110. case '|': /* `\|'. */
  2111. if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  2112. goto normal_backslash;
  2113. handle_alt:
  2114. if (syntax & RE_LIMITED_OPS)
  2115. goto normal_char;
  2116. /* Insert before the previous alternative a jump which
  2117. jumps to this alternative if the former fails. */
  2118. GET_BUFFER_SPACE (3);
  2119. INSERT_JUMP (on_failure_jump, begalt, b + 6);
  2120. pending_exact = 0;
  2121. b += 3;
  2122. /* The alternative before this one has a jump after it
  2123. which gets executed if it gets matched. Adjust that
  2124. jump so it will jump to this alternative's analogous
  2125. jump (put in below, which in turn will jump to the next
  2126. (if any) alternative's such jump, etc.). The last such
  2127. jump jumps to the correct final destination. A picture:
  2128. _____ _____
  2129. | | | |
  2130. | v | v
  2131. a | b | c
  2132. If we are at `b', then fixup_alt_jump right now points to a
  2133. three-byte space after `a'. We'll put in the jump, set
  2134. fixup_alt_jump to right after `b', and leave behind three
  2135. bytes which we'll fill in when we get to after `c'. */
  2136. if (fixup_alt_jump)
  2137. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2138. /* Mark and leave space for a jump after this alternative,
  2139. to be filled in later either by next alternative or
  2140. when know we're at the end of a series of alternatives. */
  2141. fixup_alt_jump = b;
  2142. GET_BUFFER_SPACE (3);
  2143. b += 3;
  2144. laststart = 0;
  2145. begalt = b;
  2146. break;
  2147. case '{':
  2148. /* If \{ is a literal. */
  2149. if (!(syntax & RE_INTERVALS)
  2150. /* If we're at `\{' and it's not the open-interval
  2151. operator. */
  2152. || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2153. || (p - 2 == pattern && p == pend))
  2154. goto normal_backslash;
  2155. handle_interval:
  2156. {
  2157. /* If got here, then the syntax allows intervals. */
  2158. /* At least (most) this many matches must be made. */
  2159. int lower_bound = -1, upper_bound = -1;
  2160. beg_interval = p - 1;
  2161. if (p == pend)
  2162. {
  2163. if (syntax & RE_NO_BK_BRACES)
  2164. goto unfetch_interval;
  2165. else
  2166. FREE_STACK_RETURN (REG_EBRACE);
  2167. }
  2168. GET_UNSIGNED_NUMBER (lower_bound);
  2169. if (c == ',')
  2170. {
  2171. GET_UNSIGNED_NUMBER (upper_bound);
  2172. if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  2173. }
  2174. else
  2175. /* Interval such as `{1}' => match exactly once. */
  2176. upper_bound = lower_bound;
  2177. if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  2178. || lower_bound > upper_bound)
  2179. {
  2180. if (syntax & RE_NO_BK_BRACES)
  2181. goto unfetch_interval;
  2182. else
  2183. FREE_STACK_RETURN (REG_BADBR);
  2184. }
  2185. if (!(syntax & RE_NO_BK_BRACES))
  2186. {
  2187. if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
  2188. PATFETCH (c);
  2189. }
  2190. if (c != '}')
  2191. {
  2192. if (syntax & RE_NO_BK_BRACES)
  2193. goto unfetch_interval;
  2194. else
  2195. FREE_STACK_RETURN (REG_BADBR);
  2196. }
  2197. /* We just parsed a valid interval. */
  2198. /* If it's invalid to have no preceding re. */
  2199. if (!laststart)
  2200. {
  2201. if (syntax & RE_CONTEXT_INVALID_OPS)
  2202. FREE_STACK_RETURN (REG_BADRPT);
  2203. else if (syntax & RE_CONTEXT_INDEP_OPS)
  2204. laststart = b;
  2205. else
  2206. goto unfetch_interval;
  2207. }
  2208. /* If the upper bound is zero, don't want to succeed at
  2209. all; jump from `laststart' to `b + 3', which will be
  2210. the end of the buffer after we insert the jump. */
  2211. if (upper_bound == 0)
  2212. {
  2213. GET_BUFFER_SPACE (3);
  2214. INSERT_JUMP (jump, laststart, b + 3);
  2215. b += 3;
  2216. }
  2217. /* Otherwise, we have a nontrivial interval. When
  2218. we're all done, the pattern will look like:
  2219. set_number_at <jump count> <upper bound>
  2220. set_number_at <succeed_n count> <lower bound>
  2221. succeed_n <after jump addr> <succeed_n count>
  2222. <body of loop>
  2223. jump_n <succeed_n addr> <jump count>
  2224. (The upper bound and `jump_n' are omitted if
  2225. `upper_bound' is 1, though.) */
  2226. else
  2227. { /* If the upper bound is > 1, we need to insert
  2228. more at the end of the loop. */
  2229. unsigned nbytes = 10 + (upper_bound > 1) * 10;
  2230. GET_BUFFER_SPACE (nbytes);
  2231. /* Initialize lower bound of the `succeed_n', even
  2232. though it will be set during matching by its
  2233. attendant `set_number_at' (inserted next),
  2234. because `re_compile_fastmap' needs to know.
  2235. Jump to the `jump_n' we might insert below. */
  2236. INSERT_JUMP2 (succeed_n, laststart,
  2237. b + 5 + (upper_bound > 1) * 5,
  2238. lower_bound);
  2239. b += 5;
  2240. /* Code to initialize the lower bound. Insert
  2241. before the `succeed_n'. The `5' is the last two
  2242. bytes of this `set_number_at', plus 3 bytes of
  2243. the following `succeed_n'. */
  2244. insert_op2 (set_number_at, laststart, 5, lower_bound, b);
  2245. b += 5;
  2246. if (upper_bound > 1)
  2247. { /* More than one repetition is allowed, so
  2248. append a backward jump to the `succeed_n'
  2249. that starts this interval.
  2250. When we've reached this during matching,
  2251. we'll have matched the interval once, so
  2252. jump back only `upper_bound - 1' times. */
  2253. STORE_JUMP2 (jump_n, b, laststart + 5,
  2254. upper_bound - 1);
  2255. b += 5;
  2256. /* The location we want to set is the second
  2257. parameter of the `jump_n'; that is `b-2' as
  2258. an absolute address. `laststart' will be
  2259. the `set_number_at' we're about to insert;
  2260. `laststart+3' the number to set, the source
  2261. for the relative address. But we are
  2262. inserting into the middle of the pattern --
  2263. so everything is getting moved up by 5.
  2264. Conclusion: (b - 2) - (laststart + 3) + 5,
  2265. i.e., b - laststart.
  2266. We insert this at the beginning of the loop
  2267. so that if we fail during matching, we'll
  2268. reinitialize the bounds. */
  2269. insert_op2 (set_number_at, laststart, b - laststart,
  2270. upper_bound - 1, b);
  2271. b += 5;
  2272. }
  2273. }
  2274. pending_exact = 0;
  2275. beg_interval = NULL;
  2276. }
  2277. break;
  2278. unfetch_interval:
  2279. /* If an invalid interval, match the characters as literals. */
  2280. assert (beg_interval);
  2281. p = beg_interval;
  2282. beg_interval = NULL;
  2283. /* normal_char and normal_backslash need `c'. */
  2284. PATFETCH (c);
  2285. if (!(syntax & RE_NO_BK_BRACES))
  2286. {
  2287. if (p > pattern && p[-1] == '\\')
  2288. goto normal_backslash;
  2289. }
  2290. goto normal_char;
  2291. #ifdef emacs
  2292. /* There is no way to specify the before_dot and after_dot
  2293. operators. rms says this is ok. --karl */
  2294. case '=':
  2295. BUF_PUSH (at_dot);
  2296. break;
  2297. case 's':
  2298. laststart = b;
  2299. PATFETCH (c);
  2300. BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  2301. break;
  2302. case 'S':
  2303. laststart = b;
  2304. PATFETCH (c);
  2305. BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  2306. break;
  2307. #endif /* emacs */
  2308. case 'w':
  2309. if (syntax & RE_NO_GNU_OPS)
  2310. goto normal_char;
  2311. laststart = b;
  2312. BUF_PUSH (wordchar);
  2313. break;
  2314. case 'W':
  2315. if (syntax & RE_NO_GNU_OPS)
  2316. goto normal_char;
  2317. laststart = b;
  2318. BUF_PUSH (notwordchar);
  2319. break;
  2320. case '<':
  2321. if (syntax & RE_NO_GNU_OPS)
  2322. goto normal_char;
  2323. BUF_PUSH (wordbeg);
  2324. break;
  2325. case '>':
  2326. if (syntax & RE_NO_GNU_OPS)
  2327. goto normal_char;
  2328. BUF_PUSH (wordend);
  2329. break;
  2330. case 'b':
  2331. if (syntax & RE_NO_GNU_OPS)
  2332. goto normal_char;
  2333. BUF_PUSH (wordbound);
  2334. break;
  2335. case 'B':
  2336. if (syntax & RE_NO_GNU_OPS)
  2337. goto normal_char;
  2338. BUF_PUSH (notwordbound);
  2339. break;
  2340. case '`':
  2341. if (syntax & RE_NO_GNU_OPS)
  2342. goto normal_char;
  2343. BUF_PUSH (begbuf);
  2344. break;
  2345. case '\'':
  2346. if (syntax & RE_NO_GNU_OPS)
  2347. goto normal_char;
  2348. BUF_PUSH (endbuf);
  2349. break;
  2350. case '1': case '2': case '3': case '4': case '5':
  2351. case '6': case '7': case '8': case '9':
  2352. if (syntax & RE_NO_BK_REFS)
  2353. goto normal_char;
  2354. c1 = c - '0';
  2355. if (c1 > regnum)
  2356. FREE_STACK_RETURN (REG_ESUBREG);
  2357. /* Can't back reference to a subexpression if inside of it. */
  2358. if (group_in_compile_stack (compile_stack, (regnum_t) c1))
  2359. goto normal_char;
  2360. laststart = b;
  2361. BUF_PUSH_2 (duplicate, c1);
  2362. break;
  2363. case '+':
  2364. case '?':
  2365. if (syntax & RE_BK_PLUS_QM)
  2366. goto handle_plus;
  2367. else
  2368. goto normal_backslash;
  2369. default:
  2370. normal_backslash:
  2371. /* You might think it would be useful for \ to mean
  2372. not to translate; but if we don't translate it
  2373. it will never match anything. */
  2374. c = TRANSLATE (c);
  2375. goto normal_char;
  2376. }
  2377. break;
  2378. default:
  2379. /* Expects the character in `c'. */
  2380. normal_char:
  2381. /* If no exactn currently being built. */
  2382. if (!pending_exact
  2383. /* If last exactn not at current position. */
  2384. || pending_exact + *pending_exact + 1 != b
  2385. /* We have only one byte following the exactn for the count. */
  2386. || *pending_exact == (1 << BYTEWIDTH) - 1
  2387. /* If followed by a repetition operator. */
  2388. || *p == '*' || *p == '^'
  2389. || ((syntax & RE_BK_PLUS_QM)
  2390. ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  2391. : (*p == '+' || *p == '?'))
  2392. || ((syntax & RE_INTERVALS)
  2393. && ((syntax & RE_NO_BK_BRACES)
  2394. ? *p == '{'
  2395. : (p[0] == '\\' && p[1] == '{'))))
  2396. {
  2397. /* Start building a new exactn. */
  2398. laststart = b;
  2399. BUF_PUSH_2 (exactn, 0);
  2400. pending_exact = b - 1;
  2401. }
  2402. BUF_PUSH (c);
  2403. (*pending_exact)++;
  2404. break;
  2405. } /* switch (c) */
  2406. } /* while p != pend */
  2407. /* Through the pattern now. */
  2408. if (fixup_alt_jump)
  2409. STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2410. if (!COMPILE_STACK_EMPTY)
  2411. FREE_STACK_RETURN (REG_EPAREN);
  2412. /* If we don't want backtracking, force success
  2413. the first time we reach the end of the compiled pattern. */
  2414. if (syntax & RE_NO_POSIX_BACKTRACKING)
  2415. BUF_PUSH (succeed);
  2416. free (compile_stack.stack);
  2417. /* We have succeeded; set the length of the buffer. */
  2418. bufp->used = b - bufp->buffer;
  2419. #ifdef DEBUG
  2420. if (debug)
  2421. {
  2422. DEBUG_PRINT1 ("\nCompiled pattern: \n");
  2423. print_compiled_pattern (bufp);
  2424. }
  2425. #endif /* DEBUG */
  2426. #ifndef MATCH_MAY_ALLOCATE
  2427. /* Initialize the failure stack to the largest possible stack. This
  2428. isn't necessary unless we're trying to avoid calling alloca in
  2429. the search and match routines. */
  2430. {
  2431. int num_regs = bufp->re_nsub + 1;
  2432. /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
  2433. is strictly greater than re_max_failures, the largest possible stack
  2434. is 2 * re_max_failures failure points. */
  2435. if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
  2436. {
  2437. fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
  2438. # ifdef emacs
  2439. if (! fail_stack.stack)
  2440. fail_stack.stack
  2441. = (fail_stack_elt_t *) xmalloc (fail_stack.size
  2442. * sizeof (fail_stack_elt_t));
  2443. else
  2444. fail_stack.stack
  2445. = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
  2446. (fail_stack.size
  2447. * sizeof (fail_stack_elt_t)));
  2448. # else /* not emacs */
  2449. if (! fail_stack.stack)
  2450. fail_stack.stack
  2451. = (fail_stack_elt_t *) malloc (fail_stack.size
  2452. * sizeof (fail_stack_elt_t));
  2453. else
  2454. fail_stack.stack
  2455. = (fail_stack_elt_t *) realloc (fail_stack.stack,
  2456. (fail_stack.size
  2457. * sizeof (fail_stack_elt_t)));
  2458. # endif /* not emacs */
  2459. }
  2460. regex_grow_registers (num_regs);
  2461. }
  2462. #endif /* not MATCH_MAY_ALLOCATE */
  2463. return REG_NOERROR;
  2464. } /* regex_compile */
  2465. /* Subroutines for `regex_compile'. */
  2466. /* Store OP at LOC followed by two-byte integer parameter ARG. */
  2467. static void
  2468. store_op1 (op, loc, arg)
  2469. re_opcode_t op;
  2470. unsigned char *loc;
  2471. int arg;
  2472. {
  2473. *loc = (unsigned char) op;
  2474. STORE_NUMBER (loc + 1, arg);
  2475. }
  2476. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2. */
  2477. static void
  2478. store_op2 (op, loc, arg1, arg2)
  2479. re_opcode_t op;
  2480. unsigned char *loc;
  2481. int arg1, arg2;
  2482. {
  2483. *loc = (unsigned char) op;
  2484. STORE_NUMBER (loc + 1, arg1);
  2485. STORE_NUMBER (loc + 3, arg2);
  2486. }
  2487. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  2488. for OP followed by two-byte integer parameter ARG. */
  2489. static void
  2490. insert_op1 (op, loc, arg, end)
  2491. re_opcode_t op;
  2492. unsigned char *loc;
  2493. int arg;
  2494. unsigned char *end;
  2495. {
  2496. register unsigned char *pfrom = end;
  2497. register unsigned char *pto = end + 3;
  2498. while (pfrom != loc)
  2499. *--pto = *--pfrom;
  2500. store_op1 (op, loc, arg);
  2501. }
  2502. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2. */
  2503. static void
  2504. insert_op2 (op, loc, arg1, arg2, end)
  2505. re_opcode_t op;
  2506. unsigned char *loc;
  2507. int arg1, arg2;
  2508. unsigned char *end;
  2509. {
  2510. register unsigned char *pfrom = end;
  2511. register unsigned char *pto = end + 5;
  2512. while (pfrom != loc)
  2513. *--pto = *--pfrom;
  2514. store_op2 (op, loc, arg1, arg2);
  2515. }
  2516. /* P points to just after a ^ in PATTERN. Return true if that ^ comes
  2517. after an alternative or a begin-subexpression. We assume there is at
  2518. least one character before the ^. */
  2519. static boolean
  2520. at_begline_loc_p (pattern, p, syntax)
  2521. const char *pattern, *p;
  2522. reg_syntax_t syntax;
  2523. {
  2524. const char *prev = p - 2;
  2525. boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  2526. return
  2527. /* After a subexpression? */
  2528. (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  2529. /* After an alternative? */
  2530. || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  2531. }
  2532. /* The dual of at_begline_loc_p. This one is for $. We assume there is
  2533. at least one character after the $, i.e., `P < PEND'. */
  2534. static boolean
  2535. at_endline_loc_p (p, pend, syntax)
  2536. const char *p, *pend;
  2537. reg_syntax_t syntax;
  2538. {
  2539. const char *next = p;
  2540. boolean next_backslash = *next == '\\';
  2541. const char *next_next = p + 1 < pend ? p + 1 : 0;
  2542. return
  2543. /* Before a subexpression? */
  2544. (syntax & RE_NO_BK_PARENS ? *next == ')'
  2545. : next_backslash && next_next && *next_next == ')')
  2546. /* Before an alternative? */
  2547. || (syntax & RE_NO_BK_VBAR ? *next == '|'
  2548. : next_backslash && next_next && *next_next == '|');
  2549. }
  2550. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
  2551. false if it's not. */
  2552. static boolean
  2553. group_in_compile_stack (compile_stack, regnum)
  2554. compile_stack_type compile_stack;
  2555. regnum_t regnum;
  2556. {
  2557. int this_element;
  2558. for (this_element = compile_stack.avail - 1;
  2559. this_element >= 0;
  2560. this_element--)
  2561. if (compile_stack.stack[this_element].regnum == regnum)
  2562. return true;
  2563. return false;
  2564. }
  2565. /* Read the ending character of a range (in a bracket expression) from the
  2566. uncompiled pattern *P_PTR (which ends at PEND). We assume the
  2567. starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
  2568. Then we set the translation of all bits between the starting and
  2569. ending characters (inclusive) in the compiled pattern B.
  2570. Return an error code.
  2571. We use these short variable names so we can use the same macros as
  2572. `regex_compile' itself. */
  2573. static reg_errcode_t
  2574. compile_range (p_ptr, pend, translate, syntax, b)
  2575. const char **p_ptr, *pend;
  2576. RE_TRANSLATE_TYPE translate;
  2577. reg_syntax_t syntax;
  2578. unsigned char *b;
  2579. {
  2580. unsigned this_char;
  2581. const char *p = *p_ptr;
  2582. unsigned int range_start, range_end;
  2583. if (p == pend)
  2584. return REG_ERANGE;
  2585. /* Even though the pattern is a signed `char *', we need to fetch
  2586. with unsigned char *'s; if the high bit of the pattern character
  2587. is set, the range endpoints will be negative if we fetch using a
  2588. signed char *.
  2589. We also want to fetch the endpoints without translating them; the
  2590. appropriate translation is done in the bit-setting loop below. */
  2591. /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *. */
  2592. range_start = ((const unsigned char *) p)[-2];
  2593. range_end = ((const unsigned char *) p)[0];
  2594. /* Have to increment the pointer into the pattern string, so the
  2595. caller isn't still at the ending character. */
  2596. (*p_ptr)++;
  2597. /* If the start is after the end, the range is empty. */
  2598. if (range_start > range_end)
  2599. return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  2600. /* Here we see why `this_char' has to be larger than an `unsigned
  2601. char' -- the range is inclusive, so if `range_end' == 0xff
  2602. (assuming 8-bit characters), we would otherwise go into an infinite
  2603. loop, since all characters <= 0xff. */
  2604. for (this_char = range_start; this_char <= range_end; this_char++)
  2605. {
  2606. SET_LIST_BIT (TRANSLATE (this_char));
  2607. }
  2608. return REG_NOERROR;
  2609. }
  2610. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2611. BUFP. A fastmap records which of the (1 << BYTEWIDTH) possible
  2612. characters can start a string that matches the pattern. This fastmap
  2613. is used by re_search to skip quickly over impossible starting points.
  2614. The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2615. area as BUFP->fastmap.
  2616. We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  2617. the pattern buffer.
  2618. Returns 0 if we succeed, -2 if an internal error. */
  2619. int
  2620. re_compile_fastmap (bufp)
  2621. struct re_pattern_buffer *bufp;
  2622. {
  2623. int j, k;
  2624. #ifdef MATCH_MAY_ALLOCATE
  2625. fail_stack_type fail_stack;
  2626. #endif
  2627. #ifndef REGEX_MALLOC
  2628. char *destination;
  2629. #endif
  2630. register char *fastmap = bufp->fastmap;
  2631. unsigned char *pattern = bufp->buffer;
  2632. unsigned char *p = pattern;
  2633. register unsigned char *pend = pattern + bufp->used;
  2634. #ifdef REL_ALLOC
  2635. /* This holds the pointer to the failure stack, when
  2636. it is allocated relocatably. */
  2637. fail_stack_elt_t *failure_stack_ptr;
  2638. #endif
  2639. /* Assume that each path through the pattern can be null until
  2640. proven otherwise. We set this false at the bottom of switch
  2641. statement, to which we get only if a particular path doesn't
  2642. match the empty string. */
  2643. boolean path_can_be_null = true;
  2644. /* We aren't doing a `succeed_n' to begin with. */
  2645. boolean succeed_n_p = false;
  2646. assert (fastmap != NULL && p != NULL);
  2647. INIT_FAIL_STACK ();
  2648. memset (fastmap, 0, 1 << BYTEWIDTH); /* Assume nothing's valid. */
  2649. bufp->fastmap_accurate = 1; /* It will be when we're done. */
  2650. bufp->can_be_null = 0;
  2651. while (1)
  2652. {
  2653. if (p == pend || *p == succeed)
  2654. {
  2655. /* We have reached the (effective) end of pattern. */
  2656. if (!FAIL_STACK_EMPTY ())
  2657. {
  2658. bufp->can_be_null |= path_can_be_null;
  2659. /* Reset for next path. */
  2660. path_can_be_null = true;
  2661. p = fail_stack.stack[--fail_stack.avail].pointer;
  2662. continue;
  2663. }
  2664. else
  2665. break;
  2666. }
  2667. /* We should never be about to go beyond the end of the pattern. */
  2668. assert (p < pend);
  2669. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  2670. {
  2671. /* I guess the idea here is to simply not bother with a fastmap
  2672. if a backreference is used, since it's too hard to figure out
  2673. the fastmap for the corresponding group. Setting
  2674. `can_be_null' stops `re_search_2' from using the fastmap, so
  2675. that is all we do. */
  2676. case duplicate:
  2677. bufp->can_be_null = 1;
  2678. goto done;
  2679. /* Following are the cases which match a character. These end
  2680. with `break'. */
  2681. case exactn:
  2682. fastmap[p[1]] = 1;
  2683. break;
  2684. case charset:
  2685. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2686. if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  2687. fastmap[j] = 1;
  2688. break;
  2689. case charset_not:
  2690. /* Chars beyond end of map must be allowed. */
  2691. for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  2692. fastmap[j] = 1;
  2693. for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  2694. if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  2695. fastmap[j] = 1;
  2696. break;
  2697. case wordchar:
  2698. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2699. if (SYNTAX (j) == Sword)
  2700. fastmap[j] = 1;
  2701. break;
  2702. case notwordchar:
  2703. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2704. if (SYNTAX (j) != Sword)
  2705. fastmap[j] = 1;
  2706. break;
  2707. case anychar:
  2708. {
  2709. int fastmap_newline = fastmap['\n'];
  2710. /* `.' matches anything ... */
  2711. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2712. fastmap[j] = 1;
  2713. /* ... except perhaps newline. */
  2714. if (!(bufp->syntax & RE_DOT_NEWLINE))
  2715. fastmap['\n'] = fastmap_newline;
  2716. /* Return if we have already set `can_be_null'; if we have,
  2717. then the fastmap is irrelevant. Something's wrong here. */
  2718. else if (bufp->can_be_null)
  2719. goto done;
  2720. /* Otherwise, have to check alternative paths. */
  2721. break;
  2722. }
  2723. #ifdef emacs
  2724. case syntaxspec:
  2725. k = *p++;
  2726. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2727. if (SYNTAX (j) == (enum syntaxcode) k)
  2728. fastmap[j] = 1;
  2729. break;
  2730. case notsyntaxspec:
  2731. k = *p++;
  2732. for (j = 0; j < (1 << BYTEWIDTH); j++)
  2733. if (SYNTAX (j) != (enum syntaxcode) k)
  2734. fastmap[j] = 1;
  2735. break;
  2736. /* All cases after this match the empty string. These end with
  2737. `continue'. */
  2738. case before_dot:
  2739. case at_dot:
  2740. case after_dot:
  2741. continue;
  2742. #endif /* emacs */
  2743. case no_op:
  2744. case begline:
  2745. case endline:
  2746. case begbuf:
  2747. case endbuf:
  2748. case wordbound:
  2749. case notwordbound:
  2750. case wordbeg:
  2751. case wordend:
  2752. case push_dummy_failure:
  2753. continue;
  2754. case jump_n:
  2755. case pop_failure_jump:
  2756. case maybe_pop_jump:
  2757. case jump:
  2758. case jump_past_alt:
  2759. case dummy_failure_jump:
  2760. EXTRACT_NUMBER_AND_INCR (j, p);
  2761. p += j;
  2762. if (j > 0)
  2763. continue;
  2764. /* Jump backward implies we just went through the body of a
  2765. loop and matched nothing. Opcode jumped to should be
  2766. `on_failure_jump' or `succeed_n'. Just treat it like an
  2767. ordinary jump. For a * loop, it has pushed its failure
  2768. point already; if so, discard that as redundant. */
  2769. if ((re_opcode_t) *p != on_failure_jump
  2770. && (re_opcode_t) *p != succeed_n)
  2771. continue;
  2772. p++;
  2773. EXTRACT_NUMBER_AND_INCR (j, p);
  2774. p += j;
  2775. /* If what's on the stack is where we are now, pop it. */
  2776. if (!FAIL_STACK_EMPTY ()
  2777. && fail_stack.stack[fail_stack.avail - 1].pointer == p)
  2778. fail_stack.avail--;
  2779. continue;
  2780. case on_failure_jump:
  2781. case on_failure_keep_string_jump:
  2782. handle_on_failure_jump:
  2783. EXTRACT_NUMBER_AND_INCR (j, p);
  2784. /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  2785. end of the pattern. We don't want to push such a point,
  2786. since when we restore it above, entering the switch will
  2787. increment `p' past the end of the pattern. We don't need
  2788. to push such a point since we obviously won't find any more
  2789. fastmap entries beyond `pend'. Such a pattern can match
  2790. the null string, though. */
  2791. if (p + j < pend)
  2792. {
  2793. if (!PUSH_PATTERN_OP (p + j, fail_stack))
  2794. {
  2795. RESET_FAIL_STACK ();
  2796. return -2;
  2797. }
  2798. }
  2799. else
  2800. bufp->can_be_null = 1;
  2801. if (succeed_n_p)
  2802. {
  2803. EXTRACT_NUMBER_AND_INCR (k, p); /* Skip the n. */
  2804. succeed_n_p = false;
  2805. }
  2806. continue;
  2807. case succeed_n:
  2808. /* Get to the number of times to succeed. */
  2809. p += 2;
  2810. /* Increment p past the n for when k != 0. */
  2811. EXTRACT_NUMBER_AND_INCR (k, p);
  2812. if (k == 0)
  2813. {
  2814. p -= 4;
  2815. succeed_n_p = true; /* Spaghetti code alert. */
  2816. goto handle_on_failure_jump;
  2817. }
  2818. continue;
  2819. case set_number_at:
  2820. p += 4;
  2821. continue;
  2822. case start_memory:
  2823. case stop_memory:
  2824. p += 2;
  2825. continue;
  2826. default:
  2827. abort (); /* We have listed all the cases. */
  2828. } /* switch *p++ */
  2829. /* Getting here means we have found the possible starting
  2830. characters for one path of the pattern -- and that the empty
  2831. string does not match. We need not follow this path further.
  2832. Instead, look at the next alternative (remembered on the
  2833. stack), or quit if no more. The test at the top of the loop
  2834. does these things. */
  2835. path_can_be_null = false;
  2836. p = pend;
  2837. } /* while p */
  2838. /* Set `can_be_null' for the last path (also the first path, if the
  2839. pattern is empty). */
  2840. bufp->can_be_null |= path_can_be_null;
  2841. done:
  2842. RESET_FAIL_STACK ();
  2843. return 0;
  2844. } /* re_compile_fastmap */
  2845. #ifdef _LIBC
  2846. weak_alias (__re_compile_fastmap, re_compile_fastmap)
  2847. #endif
  2848. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  2849. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  2850. this memory for recording register information. STARTS and ENDS
  2851. must be allocated using the malloc library routine, and must each
  2852. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  2853. If NUM_REGS == 0, then subsequent matches should allocate their own
  2854. register data.
  2855. Unless this function is called, the first search or match using
  2856. PATTERN_BUFFER will allocate its own register data, without
  2857. freeing the old data. */
  2858. void
  2859. re_set_registers (bufp, regs, num_regs, starts, ends)
  2860. struct re_pattern_buffer *bufp;
  2861. struct re_registers *regs;
  2862. unsigned num_regs;
  2863. regoff_t *starts, *ends;
  2864. {
  2865. if (num_regs)
  2866. {
  2867. bufp->regs_allocated = REGS_REALLOCATE;
  2868. regs->num_regs = num_regs;
  2869. regs->start = starts;
  2870. regs->end = ends;
  2871. }
  2872. else
  2873. {
  2874. bufp->regs_allocated = REGS_UNALLOCATED;
  2875. regs->num_regs = 0;
  2876. regs->start = regs->end = (regoff_t *) 0;
  2877. }
  2878. }
  2879. #ifdef _LIBC
  2880. weak_alias (__re_set_registers, re_set_registers)
  2881. #endif
  2882. /* Searching routines. */
  2883. /* Like re_search_2, below, but only one string is specified, and
  2884. doesn't let you say where to stop matching. */
  2885. int
  2886. re_search (bufp, string, size, startpos, range, regs)
  2887. struct re_pattern_buffer *bufp;
  2888. const char *string;
  2889. int size, startpos, range;
  2890. struct re_registers *regs;
  2891. {
  2892. return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
  2893. regs, size);
  2894. }
  2895. #ifdef _LIBC
  2896. weak_alias (__re_search, re_search)
  2897. #endif
  2898. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  2899. virtual concatenation of STRING1 and STRING2, starting first at index
  2900. STARTPOS, then at STARTPOS + 1, and so on.
  2901. STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  2902. RANGE is how far to scan while trying to match. RANGE = 0 means try
  2903. only at STARTPOS; in general, the last start tried is STARTPOS +
  2904. RANGE.
  2905. In REGS, return the indices of the virtual concatenation of STRING1
  2906. and STRING2 that matched the entire BUFP->buffer and its contained
  2907. subexpressions.
  2908. Do not consider matching one past the index STOP in the virtual
  2909. concatenation of STRING1 and STRING2.
  2910. We return either the position in the strings at which the match was
  2911. found, -1 if no match, or -2 if error (such as failure
  2912. stack overflow). */
  2913. int
  2914. re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
  2915. struct re_pattern_buffer *bufp;
  2916. const char *string1, *string2;
  2917. int size1, size2;
  2918. int startpos;
  2919. int range;
  2920. struct re_registers *regs;
  2921. int stop;
  2922. {
  2923. int val;
  2924. register char *fastmap = bufp->fastmap;
  2925. register RE_TRANSLATE_TYPE translate = bufp->translate;
  2926. int total_size = size1 + size2;
  2927. int endpos = startpos + range;
  2928. /* Check for out-of-range STARTPOS. */
  2929. if (startpos < 0 || startpos > total_size)
  2930. return -1;
  2931. /* Fix up RANGE if it might eventually take us outside
  2932. the virtual concatenation of STRING1 and STRING2.
  2933. Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE. */
  2934. if (endpos < 0)
  2935. range = 0 - startpos;
  2936. else if (endpos > total_size)
  2937. range = total_size - startpos;
  2938. /* If the search isn't to be a backwards one, don't waste time in a
  2939. search for a pattern that must be anchored. */
  2940. if (bufp->used > 0 && range > 0
  2941. && ((re_opcode_t) bufp->buffer[0] == begbuf
  2942. /* `begline' is like `begbuf' if it cannot match at newlines. */
  2943. || ((re_opcode_t) bufp->buffer[0] == begline
  2944. && !bufp->newline_anchor)))
  2945. {
  2946. if (startpos > 0)
  2947. return -1;
  2948. else
  2949. range = 1;
  2950. }
  2951. #ifdef emacs
  2952. /* In a forward search for something that starts with \=.
  2953. don't keep searching past point. */
  2954. if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
  2955. {
  2956. range = PT - startpos;
  2957. if (range <= 0)
  2958. return -1;
  2959. }
  2960. #endif /* emacs */
  2961. /* Update the fastmap now if not correct already. */
  2962. if (fastmap && !bufp->fastmap_accurate)
  2963. if (re_compile_fastmap (bufp) == -2)
  2964. return -2;
  2965. /* Loop through the string, looking for a place to start matching. */
  2966. for (;;)
  2967. {
  2968. /* If a fastmap is supplied, skip quickly over characters that
  2969. cannot be the start of a match. If the pattern can match the
  2970. null string, however, we don't need to skip characters; we want
  2971. the first null string. */
  2972. if (fastmap && startpos < total_size && !bufp->can_be_null)
  2973. {
  2974. if (range > 0) /* Searching forwards. */
  2975. {
  2976. register const char *d;
  2977. register int lim = 0;
  2978. int irange = range;
  2979. if (startpos < size1 && startpos + range >= size1)
  2980. lim = range - (size1 - startpos);
  2981. d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
  2982. /* Written out as an if-else to avoid testing `translate'
  2983. inside the loop. */
  2984. if (translate)
  2985. while (range > lim
  2986. && !fastmap[(unsigned char)
  2987. translate[(unsigned char) *d++]])
  2988. range--;
  2989. else
  2990. while (range > lim && !fastmap[(unsigned char) *d++])
  2991. range--;
  2992. startpos += irange - range;
  2993. }
  2994. else /* Searching backwards. */
  2995. {
  2996. register char c = (size1 == 0 || startpos >= size1
  2997. ? string2[startpos - size1]
  2998. : string1[startpos]);
  2999. if (!fastmap[(unsigned char) TRANSLATE (c)])
  3000. goto advance;
  3001. }
  3002. }
  3003. /* If can't match the null string, and that's all we have left, fail. */
  3004. if (range >= 0 && startpos == total_size && fastmap
  3005. && !bufp->can_be_null)
  3006. return -1;
  3007. val = re_match_2_internal (bufp, string1, size1, string2, size2,
  3008. startpos, regs, stop);
  3009. #ifndef REGEX_MALLOC
  3010. # ifdef C_ALLOCA
  3011. alloca (0);
  3012. # endif
  3013. #endif
  3014. if (val >= 0)
  3015. return startpos;
  3016. if (val == -2)
  3017. return -2;
  3018. advance:
  3019. if (!range)
  3020. break;
  3021. else if (range > 0)
  3022. {
  3023. range--;
  3024. startpos++;
  3025. }
  3026. else
  3027. {
  3028. range++;
  3029. startpos--;
  3030. }
  3031. }
  3032. return -1;
  3033. } /* re_search_2 */
  3034. #ifdef _LIBC
  3035. weak_alias (__re_search_2, re_search_2)
  3036. #endif
  3037. /* This converts PTR, a pointer into one of the search strings `string1'
  3038. and `string2' into an offset from the beginning of that string. */
  3039. #define POINTER_TO_OFFSET(ptr) \
  3040. (FIRST_STRING_P (ptr) \
  3041. ? ((regoff_t) ((ptr) - string1)) \
  3042. : ((regoff_t) ((ptr) - string2 + size1)))
  3043. /* Macros for dealing with the split strings in re_match_2. */
  3044. #define MATCHING_IN_FIRST_STRING (dend == end_match_1)
  3045. /* Call before fetching a character with *d. This switches over to
  3046. string2 if necessary. */
  3047. #define PREFETCH() \
  3048. while (d == dend) \
  3049. { \
  3050. /* End of string2 => fail. */ \
  3051. if (dend == end_match_2) \
  3052. goto fail; \
  3053. /* End of string1 => advance to string2. */ \
  3054. d = string2; \
  3055. dend = end_match_2; \
  3056. }
  3057. /* Test if at very beginning or at very end of the virtual concatenation
  3058. of `string1' and `string2'. If only one string, it's `string2'. */
  3059. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  3060. #define AT_STRINGS_END(d) ((d) == end2)
  3061. /* Test if D points to a character which is word-constituent. We have
  3062. two special cases to check for: if past the end of string1, look at
  3063. the first character in string2; and if before the beginning of
  3064. string2, look at the last character in string1. */
  3065. #define WORDCHAR_P(d) \
  3066. (SYNTAX ((d) == end1 ? *string2 \
  3067. : (d) == string2 - 1 ? *(end1 - 1) : *(d)) \
  3068. == Sword)
  3069. /* Disabled due to a compiler bug -- see comment at case wordbound */
  3070. #if 0
  3071. /* Test if the character before D and the one at D differ with respect
  3072. to being word-constituent. */
  3073. #define AT_WORD_BOUNDARY(d) \
  3074. (AT_STRINGS_BEG (d) || AT_STRINGS_END (d) \
  3075. || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  3076. #endif
  3077. /* Free everything we malloc. */
  3078. #ifdef MATCH_MAY_ALLOCATE
  3079. # define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
  3080. # define FREE_VARIABLES() \
  3081. do { \
  3082. REGEX_FREE_STACK (fail_stack.stack); \
  3083. FREE_VAR (regstart); \
  3084. FREE_VAR (regend); \
  3085. FREE_VAR (old_regstart); \
  3086. FREE_VAR (old_regend); \
  3087. FREE_VAR (best_regstart); \
  3088. FREE_VAR (best_regend); \
  3089. FREE_VAR (reg_info); \
  3090. FREE_VAR (reg_dummy); \
  3091. FREE_VAR (reg_info_dummy); \
  3092. } while (0)
  3093. #else
  3094. # define FREE_VARIABLES() ((void)0) /* Do nothing! But inhibit gcc warning. */
  3095. #endif /* not MATCH_MAY_ALLOCATE */
  3096. /* These values must meet several constraints. They must not be valid
  3097. register values; since we have a limit of 255 registers (because
  3098. we use only one byte in the pattern for the register number), we can
  3099. use numbers larger than 255. They must differ by 1, because of
  3100. NUM_FAILURE_ITEMS above. And the value for the lowest register must
  3101. be larger than the value for the highest register, so we do not try
  3102. to actually save any registers when none are active. */
  3103. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  3104. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  3105. /* Matching routines. */
  3106. #ifndef emacs /* Emacs never uses this. */
  3107. /* re_match is like re_match_2 except it takes only a single string. */
  3108. int
  3109. re_match (bufp, string, size, pos, regs)
  3110. struct re_pattern_buffer *bufp;
  3111. const char *string;
  3112. int size, pos;
  3113. struct re_registers *regs;
  3114. {
  3115. int result = re_match_2_internal (bufp, NULL, 0, string, size,
  3116. pos, regs, size);
  3117. # ifndef REGEX_MALLOC
  3118. # ifdef C_ALLOCA
  3119. alloca (0);
  3120. # endif
  3121. # endif
  3122. return result;
  3123. }
  3124. # ifdef _LIBC
  3125. weak_alias (__re_match, re_match)
  3126. # endif
  3127. #endif /* not emacs */
  3128. static boolean group_match_null_string_p _RE_ARGS ((unsigned char **p,
  3129. unsigned char *end,
  3130. register_info_type *reg_info));
  3131. static boolean alt_match_null_string_p _RE_ARGS ((unsigned char *p,
  3132. unsigned char *end,
  3133. register_info_type *reg_info));
  3134. static boolean common_op_match_null_string_p _RE_ARGS ((unsigned char **p,
  3135. unsigned char *end,
  3136. register_info_type *reg_info));
  3137. static int bcmp_translate _RE_ARGS ((const char *s1, const char *s2,
  3138. int len, char *translate));
  3139. /* re_match_2 matches the compiled pattern in BUFP against the
  3140. the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  3141. and SIZE2, respectively). We start matching at POS, and stop
  3142. matching at STOP.
  3143. If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  3144. store offsets for the substring each group matched in REGS. See the
  3145. documentation for exactly how many groups we fill.
  3146. We return -1 if no match, -2 if an internal error (such as the
  3147. failure stack overflowing). Otherwise, we return the length of the
  3148. matched substring. */
  3149. int
  3150. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  3151. struct re_pattern_buffer *bufp;
  3152. const char *string1, *string2;
  3153. int size1, size2;
  3154. int pos;
  3155. struct re_registers *regs;
  3156. int stop;
  3157. {
  3158. int result = re_match_2_internal (bufp, string1, size1, string2, size2,
  3159. pos, regs, stop);
  3160. #ifndef REGEX_MALLOC
  3161. # ifdef C_ALLOCA
  3162. alloca (0);
  3163. # endif
  3164. #endif
  3165. return result;
  3166. }
  3167. #ifdef _LIBC
  3168. weak_alias (__re_match_2, re_match_2)
  3169. #endif
  3170. /* This is a separate function so that we can force an alloca cleanup
  3171. afterwards. */
  3172. static int
  3173. re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
  3174. struct re_pattern_buffer *bufp;
  3175. const char *string1, *string2;
  3176. int size1, size2;
  3177. int pos;
  3178. struct re_registers *regs;
  3179. int stop;
  3180. {
  3181. /* General temporaries. */
  3182. int mcnt;
  3183. unsigned char *p1;
  3184. /* Just past the end of the corresponding string. */
  3185. const char *end1, *end2;
  3186. /* Pointers into string1 and string2, just past the last characters in
  3187. each to consider matching. */
  3188. const char *end_match_1, *end_match_2;
  3189. /* Where we are in the data, and the end of the current string. */
  3190. const char *d, *dend;
  3191. /* Where we are in the pattern, and the end of the pattern. */
  3192. unsigned char *p = bufp->buffer;
  3193. register unsigned char *pend = p + bufp->used;
  3194. /* Mark the opcode just after a start_memory, so we can test for an
  3195. empty subpattern when we get to the stop_memory. */
  3196. unsigned char *just_past_start_mem = 0;
  3197. /* We use this to map every character in the string. */
  3198. RE_TRANSLATE_TYPE translate = bufp->translate;
  3199. /* Failure point stack. Each place that can handle a failure further
  3200. down the line pushes a failure point on this stack. It consists of
  3201. restart, regend, and reg_info for all registers corresponding to
  3202. the subexpressions we're currently inside, plus the number of such
  3203. registers, and, finally, two char *'s. The first char * is where
  3204. to resume scanning the pattern; the second one is where to resume
  3205. scanning the strings. If the latter is zero, the failure point is
  3206. a ``dummy''; if a failure happens and the failure point is a dummy,
  3207. it gets discarded and the next next one is tried. */
  3208. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  3209. fail_stack_type fail_stack;
  3210. #endif
  3211. #ifdef DEBUG
  3212. static unsigned failure_id;
  3213. unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  3214. #endif
  3215. #ifdef REL_ALLOC
  3216. /* This holds the pointer to the failure stack, when
  3217. it is allocated relocatably. */
  3218. fail_stack_elt_t *failure_stack_ptr;
  3219. #endif
  3220. /* We fill all the registers internally, independent of what we
  3221. return, for use in backreferences. The number here includes
  3222. an element for register zero. */
  3223. size_t num_regs = bufp->re_nsub + 1;
  3224. /* The currently active registers. */
  3225. active_reg_t lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3226. active_reg_t highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3227. /* Information on the contents of registers. These are pointers into
  3228. the input strings; they record just what was matched (on this
  3229. attempt) by a subexpression part of the pattern, that is, the
  3230. regnum-th regstart pointer points to where in the pattern we began
  3231. matching and the regnum-th regend points to right after where we
  3232. stopped matching the regnum-th subexpression. (The zeroth register
  3233. keeps track of what the whole pattern matches.) */
  3234. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3235. const char **regstart, **regend;
  3236. #endif
  3237. /* If a group that's operated upon by a repetition operator fails to
  3238. match anything, then the register for its start will need to be
  3239. restored because it will have been set to wherever in the string we
  3240. are when we last see its open-group operator. Similarly for a
  3241. register's end. */
  3242. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3243. const char **old_regstart, **old_regend;
  3244. #endif
  3245. /* The is_active field of reg_info helps us keep track of which (possibly
  3246. nested) subexpressions we are currently in. The matched_something
  3247. field of reg_info[reg_num] helps us tell whether or not we have
  3248. matched any of the pattern so far this time through the reg_num-th
  3249. subexpression. These two fields get reset each time through any
  3250. loop their register is in. */
  3251. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global. */
  3252. register_info_type *reg_info;
  3253. #endif
  3254. /* The following record the register info as found in the above
  3255. variables when we find a match better than any we've seen before.
  3256. This happens as we backtrack through the failure points, which in
  3257. turn happens only if we have not yet matched the entire string. */
  3258. unsigned best_regs_set = false;
  3259. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3260. const char **best_regstart, **best_regend;
  3261. #endif
  3262. /* Logically, this is `best_regend[0]'. But we don't want to have to
  3263. allocate space for that if we're not allocating space for anything
  3264. else (see below). Also, we never need info about register 0 for
  3265. any of the other register vectors, and it seems rather a kludge to
  3266. treat `best_regend' differently than the rest. So we keep track of
  3267. the end of the best match so far in a separate variable. We
  3268. initialize this to NULL so that when we backtrack the first time
  3269. and need to test it, it's not garbage. */
  3270. const char *match_end = NULL;
  3271. /* This helps SET_REGS_MATCHED avoid doing redundant work. */
  3272. int set_regs_matched_done = 0;
  3273. /* Used when we pop values we don't care about. */
  3274. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */
  3275. const char **reg_dummy;
  3276. register_info_type *reg_info_dummy;
  3277. #endif
  3278. #ifdef DEBUG
  3279. /* Counts the total number of registers pushed. */
  3280. unsigned num_regs_pushed = 0;
  3281. #endif
  3282. DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3283. INIT_FAIL_STACK ();
  3284. #ifdef MATCH_MAY_ALLOCATE
  3285. /* Do not bother to initialize all the register variables if there are
  3286. no groups in the pattern, as it takes a fair amount of time. If
  3287. there are groups, we include space for register 0 (the whole
  3288. pattern), even though we never use it, since it simplifies the
  3289. array indexing. We should fix this. */
  3290. if (bufp->re_nsub)
  3291. {
  3292. regstart = REGEX_TALLOC (num_regs, const char *);
  3293. regend = REGEX_TALLOC (num_regs, const char *);
  3294. old_regstart = REGEX_TALLOC (num_regs, const char *);
  3295. old_regend = REGEX_TALLOC (num_regs, const char *);
  3296. best_regstart = REGEX_TALLOC (num_regs, const char *);
  3297. best_regend = REGEX_TALLOC (num_regs, const char *);
  3298. reg_info = REGEX_TALLOC (num_regs, register_info_type);
  3299. reg_dummy = REGEX_TALLOC (num_regs, const char *);
  3300. reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
  3301. if (!(regstart && regend && old_regstart && old_regend && reg_info
  3302. && best_regstart && best_regend && reg_dummy && reg_info_dummy))
  3303. {
  3304. FREE_VARIABLES ();
  3305. return -2;
  3306. }
  3307. }
  3308. else
  3309. {
  3310. /* We must initialize all our variables to NULL, so that
  3311. `FREE_VARIABLES' doesn't try to free them. */
  3312. regstart = regend = old_regstart = old_regend = best_regstart
  3313. = best_regend = reg_dummy = NULL;
  3314. reg_info = reg_info_dummy = (register_info_type *) NULL;
  3315. }
  3316. #endif /* MATCH_MAY_ALLOCATE */
  3317. /* The starting position is bogus. */
  3318. if (pos < 0 || pos > size1 + size2)
  3319. {
  3320. FREE_VARIABLES ();
  3321. return -1;
  3322. }
  3323. /* Initialize subexpression text positions to -1 to mark ones that no
  3324. start_memory/stop_memory has been seen for. Also initialize the
  3325. register information struct. */
  3326. for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
  3327. {
  3328. regstart[mcnt] = regend[mcnt]
  3329. = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3330. REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  3331. IS_ACTIVE (reg_info[mcnt]) = 0;
  3332. MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3333. EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3334. }
  3335. /* We move `string1' into `string2' if the latter's empty -- but not if
  3336. `string1' is null. */
  3337. if (size2 == 0 && string1 != NULL)
  3338. {
  3339. string2 = string1;
  3340. size2 = size1;
  3341. string1 = 0;
  3342. size1 = 0;
  3343. }
  3344. end1 = string1 + size1;
  3345. end2 = string2 + size2;
  3346. /* Compute where to stop matching, within the two strings. */
  3347. if (stop <= size1)
  3348. {
  3349. end_match_1 = string1 + stop;
  3350. end_match_2 = string2;
  3351. }
  3352. else
  3353. {
  3354. end_match_1 = end1;
  3355. end_match_2 = string2 + stop - size1;
  3356. }
  3357. /* `p' scans through the pattern as `d' scans through the data.
  3358. `dend' is the end of the input string that `d' points within. `d'
  3359. is advanced into the following input string whenever necessary, but
  3360. this happens before fetching; therefore, at the beginning of the
  3361. loop, `d' can be pointing at the end of a string, but it cannot
  3362. equal `string2'. */
  3363. if (size1 > 0 && pos <= size1)
  3364. {
  3365. d = string1 + pos;
  3366. dend = end_match_1;
  3367. }
  3368. else
  3369. {
  3370. d = string2 + pos - size1;
  3371. dend = end_match_2;
  3372. }
  3373. DEBUG_PRINT1 ("The compiled pattern is:\n");
  3374. DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  3375. DEBUG_PRINT1 ("The string to match is: `");
  3376. DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  3377. DEBUG_PRINT1 ("'\n");
  3378. /* This loops over pattern commands. It exits by returning from the
  3379. function if the match is complete, or it drops through if the match
  3380. fails at this starting point in the input data. */
  3381. for (;;)
  3382. {
  3383. #ifdef _LIBC
  3384. DEBUG_PRINT2 ("\n%p: ", p);
  3385. #else
  3386. DEBUG_PRINT2 ("\n0x%x: ", p);
  3387. #endif
  3388. if (p == pend)
  3389. { /* End of pattern means we might have succeeded. */
  3390. DEBUG_PRINT1 ("end of pattern ... ");
  3391. /* If we haven't matched the entire string, and we want the
  3392. longest match, try backtracking. */
  3393. if (d != end_match_2)
  3394. {
  3395. /* 1 if this match ends in the same string (string1 or string2)
  3396. as the best previous match. */
  3397. boolean same_str_p = (FIRST_STRING_P (match_end)
  3398. == MATCHING_IN_FIRST_STRING);
  3399. /* 1 if this match is the best seen so far. */
  3400. boolean best_match_p;
  3401. /* AIX compiler got confused when this was combined
  3402. with the previous declaration. */
  3403. if (same_str_p)
  3404. best_match_p = d > match_end;
  3405. else
  3406. best_match_p = !MATCHING_IN_FIRST_STRING;
  3407. DEBUG_PRINT1 ("backtracking.\n");
  3408. if (!FAIL_STACK_EMPTY ())
  3409. { /* More failure points to try. */
  3410. /* If exceeds best match so far, save it. */
  3411. if (!best_regs_set || best_match_p)
  3412. {
  3413. best_regs_set = true;
  3414. match_end = d;
  3415. DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  3416. for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
  3417. {
  3418. best_regstart[mcnt] = regstart[mcnt];
  3419. best_regend[mcnt] = regend[mcnt];
  3420. }
  3421. }
  3422. goto fail;
  3423. }
  3424. /* If no failure points, don't restore garbage. And if
  3425. last match is real best match, don't restore second
  3426. best one. */
  3427. else if (best_regs_set && !best_match_p)
  3428. {
  3429. restore_best_regs:
  3430. /* Restore best match. It may happen that `dend ==
  3431. end_match_1' while the restored d is in string2.
  3432. For example, the pattern `x.*y.*z' against the
  3433. strings `x-' and `y-z-', if the two strings are
  3434. not consecutive in memory. */
  3435. DEBUG_PRINT1 ("Restoring best registers.\n");
  3436. d = match_end;
  3437. dend = ((d >= string1 && d <= end1)
  3438. ? end_match_1 : end_match_2);
  3439. for (mcnt = 1; (unsigned) mcnt < num_regs; mcnt++)
  3440. {
  3441. regstart[mcnt] = best_regstart[mcnt];
  3442. regend[mcnt] = best_regend[mcnt];
  3443. }
  3444. }
  3445. } /* d != end_match_2 */
  3446. succeed_label:
  3447. DEBUG_PRINT1 ("Accepting match.\n");
  3448. /* If caller wants register contents data back, do it. */
  3449. if (regs && !bufp->no_sub)
  3450. {
  3451. /* Have the register data arrays been allocated? */
  3452. if (bufp->regs_allocated == REGS_UNALLOCATED)
  3453. { /* No. So allocate them with malloc. We need one
  3454. extra element beyond `num_regs' for the `-1' marker
  3455. GNU code uses. */
  3456. regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3457. regs->start = TALLOC (regs->num_regs, regoff_t);
  3458. regs->end = TALLOC (regs->num_regs, regoff_t);
  3459. if (regs->start == NULL || regs->end == NULL)
  3460. {
  3461. FREE_VARIABLES ();
  3462. return -2;
  3463. }
  3464. bufp->regs_allocated = REGS_REALLOCATE;
  3465. }
  3466. else if (bufp->regs_allocated == REGS_REALLOCATE)
  3467. { /* Yes. If we need more elements than were already
  3468. allocated, reallocate them. If we need fewer, just
  3469. leave it alone. */
  3470. if (regs->num_regs < num_regs + 1)
  3471. {
  3472. regs->num_regs = num_regs + 1;
  3473. RETALLOC (regs->start, regs->num_regs, regoff_t);
  3474. RETALLOC (regs->end, regs->num_regs, regoff_t);
  3475. if (regs->start == NULL || regs->end == NULL)
  3476. {
  3477. FREE_VARIABLES ();
  3478. return -2;
  3479. }
  3480. }
  3481. }
  3482. else
  3483. {
  3484. /* These braces fend off a "empty body in an else-statement"
  3485. warning under GCC when assert expands to nothing. */
  3486. assert (bufp->regs_allocated == REGS_FIXED);
  3487. }
  3488. /* Convert the pointer data in `regstart' and `regend' to
  3489. indices. Register zero has to be set differently,
  3490. since we haven't kept track of any info for it. */
  3491. if (regs->num_regs > 0)
  3492. {
  3493. regs->start[0] = pos;
  3494. regs->end[0] = (MATCHING_IN_FIRST_STRING
  3495. ? ((regoff_t) (d - string1))
  3496. : ((regoff_t) (d - string2 + size1)));
  3497. }
  3498. /* Go through the first `min (num_regs, regs->num_regs)'
  3499. registers, since that is all we initialized. */
  3500. for (mcnt = 1; (unsigned) mcnt < MIN (num_regs, regs->num_regs);
  3501. mcnt++)
  3502. {
  3503. if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  3504. regs->start[mcnt] = regs->end[mcnt] = -1;
  3505. else
  3506. {
  3507. regs->start[mcnt]
  3508. = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
  3509. regs->end[mcnt]
  3510. = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
  3511. }
  3512. }
  3513. /* If the regs structure we return has more elements than
  3514. were in the pattern, set the extra elements to -1. If
  3515. we (re)allocated the registers, this is the case,
  3516. because we always allocate enough to have at least one
  3517. -1 at the end. */
  3518. for (mcnt = num_regs; (unsigned) mcnt < regs->num_regs; mcnt++)
  3519. regs->start[mcnt] = regs->end[mcnt] = -1;
  3520. } /* regs && !bufp->no_sub */
  3521. DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  3522. nfailure_points_pushed, nfailure_points_popped,
  3523. nfailure_points_pushed - nfailure_points_popped);
  3524. DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  3525. mcnt = d - pos - (MATCHING_IN_FIRST_STRING
  3526. ? string1
  3527. : string2 - size1);
  3528. DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  3529. FREE_VARIABLES ();
  3530. return mcnt;
  3531. }
  3532. /* Otherwise match next pattern command. */
  3533. switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  3534. {
  3535. /* Ignore these. Used to ignore the n of succeed_n's which
  3536. currently have n == 0. */
  3537. case no_op:
  3538. DEBUG_PRINT1 ("EXECUTING no_op.\n");
  3539. break;
  3540. case succeed:
  3541. DEBUG_PRINT1 ("EXECUTING succeed.\n");
  3542. goto succeed_label;
  3543. /* Match the next n pattern characters exactly. The following
  3544. byte in the pattern defines n, and the n bytes after that
  3545. are the characters to match. */
  3546. case exactn:
  3547. mcnt = *p++;
  3548. DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  3549. /* This is written out as an if-else so we don't waste time
  3550. testing `translate' inside the loop. */
  3551. if (translate)
  3552. {
  3553. do
  3554. {
  3555. PREFETCH ();
  3556. if ((unsigned char) translate[(unsigned char) *d++]
  3557. != (unsigned char) *p++)
  3558. goto fail;
  3559. }
  3560. while (--mcnt);
  3561. }
  3562. else
  3563. {
  3564. do
  3565. {
  3566. PREFETCH ();
  3567. if (*d++ != (char) *p++) goto fail;
  3568. }
  3569. while (--mcnt);
  3570. }
  3571. SET_REGS_MATCHED ();
  3572. break;
  3573. /* Match any character except possibly a newline or a null. */
  3574. case anychar:
  3575. DEBUG_PRINT1 ("EXECUTING anychar.\n");
  3576. PREFETCH ();
  3577. if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
  3578. || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
  3579. goto fail;
  3580. SET_REGS_MATCHED ();
  3581. DEBUG_PRINT2 (" Matched `%d'.\n", *d);
  3582. d++;
  3583. break;
  3584. case charset:
  3585. case charset_not:
  3586. {
  3587. register unsigned char c;
  3588. boolean not = (re_opcode_t) *(p - 1) == charset_not;
  3589. DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  3590. PREFETCH ();
  3591. c = TRANSLATE (*d); /* The character to match. */
  3592. /* Cast to `unsigned' instead of `unsigned char' in case the
  3593. bit list is a full 32 bytes long. */
  3594. if (c < (unsigned) (*p * BYTEWIDTH)
  3595. && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3596. not = !not;
  3597. p += 1 + *p;
  3598. if (!not) goto fail;
  3599. SET_REGS_MATCHED ();
  3600. d++;
  3601. break;
  3602. }
  3603. /* The beginning of a group is represented by start_memory.
  3604. The arguments are the register number in the next byte, and the
  3605. number of groups inner to this one in the next. The text
  3606. matched within the group is recorded (in the internal
  3607. registers data structure) under the register number. */
  3608. case start_memory:
  3609. DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  3610. /* Find out if this group can match the empty string. */
  3611. p1 = p; /* To send to group_match_null_string_p. */
  3612. if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  3613. REG_MATCH_NULL_STRING_P (reg_info[*p])
  3614. = group_match_null_string_p (&p1, pend, reg_info);
  3615. /* Save the position in the string where we were the last time
  3616. we were at this open-group operator in case the group is
  3617. operated upon by a repetition operator, e.g., with `(a*)*b'
  3618. against `ab'; then we want to ignore where we are now in
  3619. the string in case this attempt to match fails. */
  3620. old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3621. ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  3622. : regstart[*p];
  3623. DEBUG_PRINT2 (" old_regstart: %d\n",
  3624. POINTER_TO_OFFSET (old_regstart[*p]));
  3625. regstart[*p] = d;
  3626. DEBUG_PRINT2 (" regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  3627. IS_ACTIVE (reg_info[*p]) = 1;
  3628. MATCHED_SOMETHING (reg_info[*p]) = 0;
  3629. /* Clear this whenever we change the register activity status. */
  3630. set_regs_matched_done = 0;
  3631. /* This is the new highest active register. */
  3632. highest_active_reg = *p;
  3633. /* If nothing was active before, this is the new lowest active
  3634. register. */
  3635. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3636. lowest_active_reg = *p;
  3637. /* Move past the register number and inner group count. */
  3638. p += 2;
  3639. just_past_start_mem = p;
  3640. break;
  3641. /* The stop_memory opcode represents the end of a group. Its
  3642. arguments are the same as start_memory's: the register
  3643. number, and the number of inner groups. */
  3644. case stop_memory:
  3645. DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  3646. /* We need to save the string position the last time we were at
  3647. this close-group operator in case the group is operated
  3648. upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  3649. against `aba'; then we want to ignore where we are now in
  3650. the string in case this attempt to match fails. */
  3651. old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  3652. ? REG_UNSET (regend[*p]) ? d : regend[*p]
  3653. : regend[*p];
  3654. DEBUG_PRINT2 (" old_regend: %d\n",
  3655. POINTER_TO_OFFSET (old_regend[*p]));
  3656. regend[*p] = d;
  3657. DEBUG_PRINT2 (" regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  3658. /* This register isn't active anymore. */
  3659. IS_ACTIVE (reg_info[*p]) = 0;
  3660. /* Clear this whenever we change the register activity status. */
  3661. set_regs_matched_done = 0;
  3662. /* If this was the only register active, nothing is active
  3663. anymore. */
  3664. if (lowest_active_reg == highest_active_reg)
  3665. {
  3666. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3667. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3668. }
  3669. else
  3670. { /* We must scan for the new highest active register, since
  3671. it isn't necessarily one less than now: consider
  3672. (a(b)c(d(e)f)g). When group 3 ends, after the f), the
  3673. new highest active register is 1. */
  3674. unsigned char r = *p - 1;
  3675. while (r > 0 && !IS_ACTIVE (reg_info[r]))
  3676. r--;
  3677. /* If we end up at register zero, that means that we saved
  3678. the registers as the result of an `on_failure_jump', not
  3679. a `start_memory', and we jumped to past the innermost
  3680. `stop_memory'. For example, in ((.)*) we save
  3681. registers 1 and 2 as a result of the *, but when we pop
  3682. back to the second ), we are at the stop_memory 1.
  3683. Thus, nothing is active. */
  3684. if (r == 0)
  3685. {
  3686. lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3687. highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3688. }
  3689. else
  3690. highest_active_reg = r;
  3691. }
  3692. /* If just failed to match something this time around with a
  3693. group that's operated on by a repetition operator, try to
  3694. force exit from the ``loop'', and restore the register
  3695. information for this group that we had before trying this
  3696. last match. */
  3697. if ((!MATCHED_SOMETHING (reg_info[*p])
  3698. || just_past_start_mem == p - 1)
  3699. && (p + 2) < pend)
  3700. {
  3701. boolean is_a_jump_n = false;
  3702. p1 = p + 2;
  3703. mcnt = 0;
  3704. switch ((re_opcode_t) *p1++)
  3705. {
  3706. case jump_n:
  3707. is_a_jump_n = true;
  3708. case pop_failure_jump:
  3709. case maybe_pop_jump:
  3710. case jump:
  3711. case dummy_failure_jump:
  3712. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3713. if (is_a_jump_n)
  3714. p1 += 2;
  3715. break;
  3716. default:
  3717. /* do nothing */ ;
  3718. }
  3719. p1 += mcnt;
  3720. /* If the next operation is a jump backwards in the pattern
  3721. to an on_failure_jump right before the start_memory
  3722. corresponding to this stop_memory, exit from the loop
  3723. by forcing a failure after pushing on the stack the
  3724. on_failure_jump's jump in the pattern, and d. */
  3725. if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  3726. && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  3727. {
  3728. /* If this group ever matched anything, then restore
  3729. what its registers were before trying this last
  3730. failed match, e.g., with `(a*)*b' against `ab' for
  3731. regstart[1], and, e.g., with `((a*)*(b*)*)*'
  3732. against `aba' for regend[3].
  3733. Also restore the registers for inner groups for,
  3734. e.g., `((a*)(b*))*' against `aba' (register 3 would
  3735. otherwise get trashed). */
  3736. if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  3737. {
  3738. unsigned r;
  3739. EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  3740. /* Restore this and inner groups' (if any) registers. */
  3741. for (r = *p; r < (unsigned) *p + (unsigned) *(p + 1);
  3742. r++)
  3743. {
  3744. regstart[r] = old_regstart[r];
  3745. /* xx why this test? */
  3746. if (old_regend[r] >= regstart[r])
  3747. regend[r] = old_regend[r];
  3748. }
  3749. }
  3750. p1++;
  3751. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  3752. PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  3753. goto fail;
  3754. }
  3755. }
  3756. /* Move past the register number and the inner group count. */
  3757. p += 2;
  3758. break;
  3759. /* \<digit> has been turned into a `duplicate' command which is
  3760. followed by the numeric value of <digit> as the register number. */
  3761. case duplicate:
  3762. {
  3763. register const char *d2, *dend2;
  3764. int regno = *p++; /* Get which register to match against. */
  3765. DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  3766. /* Can't back reference a group which we've never matched. */
  3767. if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  3768. goto fail;
  3769. /* Where in input to try to start matching. */
  3770. d2 = regstart[regno];
  3771. /* Where to stop matching; if both the place to start and
  3772. the place to stop matching are in the same string, then
  3773. set to the place to stop, otherwise, for now have to use
  3774. the end of the first string. */
  3775. dend2 = ((FIRST_STRING_P (regstart[regno])
  3776. == FIRST_STRING_P (regend[regno]))
  3777. ? regend[regno] : end_match_1);
  3778. for (;;)
  3779. {
  3780. /* If necessary, advance to next segment in register
  3781. contents. */
  3782. while (d2 == dend2)
  3783. {
  3784. if (dend2 == end_match_2) break;
  3785. if (dend2 == regend[regno]) break;
  3786. /* End of string1 => advance to string2. */
  3787. d2 = string2;
  3788. dend2 = regend[regno];
  3789. }
  3790. /* At end of register contents => success */
  3791. if (d2 == dend2) break;
  3792. /* If necessary, advance to next segment in data. */
  3793. PREFETCH ();
  3794. /* How many characters left in this segment to match. */
  3795. mcnt = dend - d;
  3796. /* Want how many consecutive characters we can match in
  3797. one shot, so, if necessary, adjust the count. */
  3798. if (mcnt > dend2 - d2)
  3799. mcnt = dend2 - d2;
  3800. /* Compare that many; failure if mismatch, else move
  3801. past them. */
  3802. if (translate
  3803. ? bcmp_translate (d, d2, mcnt, translate)
  3804. : memcmp (d, d2, mcnt))
  3805. goto fail;
  3806. d += mcnt, d2 += mcnt;
  3807. /* Do this because we've match some characters. */
  3808. SET_REGS_MATCHED ();
  3809. }
  3810. }
  3811. break;
  3812. /* begline matches the empty string at the beginning of the string
  3813. (unless `not_bol' is set in `bufp'), and, if
  3814. `newline_anchor' is set, after newlines. */
  3815. case begline:
  3816. DEBUG_PRINT1 ("EXECUTING begline.\n");
  3817. if (AT_STRINGS_BEG (d))
  3818. {
  3819. if (!bufp->not_bol) break;
  3820. }
  3821. else if (d[-1] == '\n' && bufp->newline_anchor)
  3822. {
  3823. break;
  3824. }
  3825. /* In all other cases, we fail. */
  3826. goto fail;
  3827. /* endline is the dual of begline. */
  3828. case endline:
  3829. DEBUG_PRINT1 ("EXECUTING endline.\n");
  3830. if (AT_STRINGS_END (d))
  3831. {
  3832. if (!bufp->not_eol) break;
  3833. }
  3834. /* We have to ``prefetch'' the next character. */
  3835. else if ((d == end1 ? *string2 : *d) == '\n'
  3836. && bufp->newline_anchor)
  3837. {
  3838. break;
  3839. }
  3840. goto fail;
  3841. /* Match at the very beginning of the data. */
  3842. case begbuf:
  3843. DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  3844. if (AT_STRINGS_BEG (d))
  3845. break;
  3846. goto fail;
  3847. /* Match at the very end of the data. */
  3848. case endbuf:
  3849. DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  3850. if (AT_STRINGS_END (d))
  3851. break;
  3852. goto fail;
  3853. /* on_failure_keep_string_jump is used to optimize `.*\n'. It
  3854. pushes NULL as the value for the string on the stack. Then
  3855. `pop_failure_point' will keep the current value for the
  3856. string, instead of restoring it. To see why, consider
  3857. matching `foo\nbar' against `.*\n'. The .* matches the foo;
  3858. then the . fails against the \n. But the next thing we want
  3859. to do is match the \n against the \n; if we restored the
  3860. string value, we would be back at the foo.
  3861. Because this is used only in specific cases, we don't need to
  3862. check all the things that `on_failure_jump' does, to make
  3863. sure the right things get saved on the stack. Hence we don't
  3864. share its code. The only reason to push anything on the
  3865. stack at all is that otherwise we would have to change
  3866. `anychar's code to do something besides goto fail in this
  3867. case; that seems worse than this. */
  3868. case on_failure_keep_string_jump:
  3869. DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  3870. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3871. #ifdef _LIBC
  3872. DEBUG_PRINT3 (" %d (to %p):\n", mcnt, p + mcnt);
  3873. #else
  3874. DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  3875. #endif
  3876. PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  3877. break;
  3878. /* Uses of on_failure_jump:
  3879. Each alternative starts with an on_failure_jump that points
  3880. to the beginning of the next alternative. Each alternative
  3881. except the last ends with a jump that in effect jumps past
  3882. the rest of the alternatives. (They really jump to the
  3883. ending jump of the following alternative, because tensioning
  3884. these jumps is a hassle.)
  3885. Repeats start with an on_failure_jump that points past both
  3886. the repetition text and either the following jump or
  3887. pop_failure_jump back to this on_failure_jump. */
  3888. case on_failure_jump:
  3889. on_failure:
  3890. DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  3891. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3892. #ifdef _LIBC
  3893. DEBUG_PRINT3 (" %d (to %p)", mcnt, p + mcnt);
  3894. #else
  3895. DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  3896. #endif
  3897. /* If this on_failure_jump comes right before a group (i.e.,
  3898. the original * applied to a group), save the information
  3899. for that group and all inner ones, so that if we fail back
  3900. to this point, the group's information will be correct.
  3901. For example, in \(a*\)*\1, we need the preceding group,
  3902. and in \(zz\(a*\)b*\)\2, we need the inner group. */
  3903. /* We can't use `p' to check ahead because we push
  3904. a failure point to `p + mcnt' after we do this. */
  3905. p1 = p;
  3906. /* We need to skip no_op's before we look for the
  3907. start_memory in case this on_failure_jump is happening as
  3908. the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  3909. against aba. */
  3910. while (p1 < pend && (re_opcode_t) *p1 == no_op)
  3911. p1++;
  3912. if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  3913. {
  3914. /* We have a new highest active register now. This will
  3915. get reset at the start_memory we are about to get to,
  3916. but we will have saved all the registers relevant to
  3917. this repetition op, as described above. */
  3918. highest_active_reg = *(p1 + 1) + *(p1 + 2);
  3919. if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  3920. lowest_active_reg = *(p1 + 1);
  3921. }
  3922. DEBUG_PRINT1 (":\n");
  3923. PUSH_FAILURE_POINT (p + mcnt, d, -2);
  3924. break;
  3925. /* A smart repeat ends with `maybe_pop_jump'.
  3926. We change it to either `pop_failure_jump' or `jump'. */
  3927. case maybe_pop_jump:
  3928. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  3929. DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  3930. {
  3931. register unsigned char *p2 = p;
  3932. /* Compare the beginning of the repeat with what in the
  3933. pattern follows its end. If we can establish that there
  3934. is nothing that they would both match, i.e., that we
  3935. would have to backtrack because of (as in, e.g., `a*a')
  3936. then we can change to pop_failure_jump, because we'll
  3937. never have to backtrack.
  3938. This is not true in the case of alternatives: in
  3939. `(a|ab)*' we do need to backtrack to the `ab' alternative
  3940. (e.g., if the string was `ab'). But instead of trying to
  3941. detect that here, the alternative has put on a dummy
  3942. failure point which is what we will end up popping. */
  3943. /* Skip over open/close-group commands.
  3944. If what follows this loop is a ...+ construct,
  3945. look at what begins its body, since we will have to
  3946. match at least one of that. */
  3947. while (1)
  3948. {
  3949. if (p2 + 2 < pend
  3950. && ((re_opcode_t) *p2 == stop_memory
  3951. || (re_opcode_t) *p2 == start_memory))
  3952. p2 += 3;
  3953. else if (p2 + 6 < pend
  3954. && (re_opcode_t) *p2 == dummy_failure_jump)
  3955. p2 += 6;
  3956. else
  3957. break;
  3958. }
  3959. p1 = p + mcnt;
  3960. /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  3961. to the `maybe_finalize_jump' of this case. Examine what
  3962. follows. */
  3963. /* If we're at the end of the pattern, we can change. */
  3964. if (p2 == pend)
  3965. {
  3966. /* Consider what happens when matching ":\(.*\)"
  3967. against ":/". I don't really understand this code
  3968. yet. */
  3969. p[-3] = (unsigned char) pop_failure_jump;
  3970. DEBUG_PRINT1
  3971. (" End of pattern: change to `pop_failure_jump'.\n");
  3972. }
  3973. else if ((re_opcode_t) *p2 == exactn
  3974. || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  3975. {
  3976. register unsigned char c
  3977. = *p2 == (unsigned char) endline ? '\n' : p2[2];
  3978. if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
  3979. {
  3980. p[-3] = (unsigned char) pop_failure_jump;
  3981. DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
  3982. c, p1[5]);
  3983. }
  3984. else if ((re_opcode_t) p1[3] == charset
  3985. || (re_opcode_t) p1[3] == charset_not)
  3986. {
  3987. int not = (re_opcode_t) p1[3] == charset_not;
  3988. if (c < (unsigned char) (p1[4] * BYTEWIDTH)
  3989. && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  3990. not = !not;
  3991. /* `not' is equal to 1 if c would match, which means
  3992. that we can't change to pop_failure_jump. */
  3993. if (!not)
  3994. {
  3995. p[-3] = (unsigned char) pop_failure_jump;
  3996. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  3997. }
  3998. }
  3999. }
  4000. else if ((re_opcode_t) *p2 == charset)
  4001. {
  4002. #ifdef DEBUG
  4003. register unsigned char c
  4004. = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4005. #endif
  4006. #if 0
  4007. if ((re_opcode_t) p1[3] == exactn
  4008. && ! ((int) p2[1] * BYTEWIDTH > (int) p1[5]
  4009. && (p2[2 + p1[5] / BYTEWIDTH]
  4010. & (1 << (p1[5] % BYTEWIDTH)))))
  4011. #else
  4012. if ((re_opcode_t) p1[3] == exactn
  4013. && ! ((int) p2[1] * BYTEWIDTH > (int) p1[4]
  4014. && (p2[2 + p1[4] / BYTEWIDTH]
  4015. & (1 << (p1[4] % BYTEWIDTH)))))
  4016. #endif
  4017. {
  4018. p[-3] = (unsigned char) pop_failure_jump;
  4019. DEBUG_PRINT3 (" %c != %c => pop_failure_jump.\n",
  4020. c, p1[5]);
  4021. }
  4022. else if ((re_opcode_t) p1[3] == charset_not)
  4023. {
  4024. int idx;
  4025. /* We win if the charset_not inside the loop
  4026. lists every character listed in the charset after. */
  4027. for (idx = 0; idx < (int) p2[1]; idx++)
  4028. if (! (p2[2 + idx] == 0
  4029. || (idx < (int) p1[4]
  4030. && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
  4031. break;
  4032. if (idx == p2[1])
  4033. {
  4034. p[-3] = (unsigned char) pop_failure_jump;
  4035. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4036. }
  4037. }
  4038. else if ((re_opcode_t) p1[3] == charset)
  4039. {
  4040. int idx;
  4041. /* We win if the charset inside the loop
  4042. has no overlap with the one after the loop. */
  4043. for (idx = 0;
  4044. idx < (int) p2[1] && idx < (int) p1[4];
  4045. idx++)
  4046. if ((p2[2 + idx] & p1[5 + idx]) != 0)
  4047. break;
  4048. if (idx == p2[1] || idx == p1[4])
  4049. {
  4050. p[-3] = (unsigned char) pop_failure_jump;
  4051. DEBUG_PRINT1 (" No match => pop_failure_jump.\n");
  4052. }
  4053. }
  4054. }
  4055. }
  4056. p -= 2; /* Point at relative address again. */
  4057. if ((re_opcode_t) p[-1] != pop_failure_jump)
  4058. {
  4059. p[-1] = (unsigned char) jump;
  4060. DEBUG_PRINT1 (" Match => jump.\n");
  4061. goto unconditional_jump;
  4062. }
  4063. /* Note fall through. */
  4064. /* The end of a simple repeat has a pop_failure_jump back to
  4065. its matching on_failure_jump, where the latter will push a
  4066. failure point. The pop_failure_jump takes off failure
  4067. points put on by this pop_failure_jump's matching
  4068. on_failure_jump; we got through the pattern to here from the
  4069. matching on_failure_jump, so didn't fail. */
  4070. case pop_failure_jump:
  4071. {
  4072. /* We need to pass separate storage for the lowest and
  4073. highest registers, even though we don't care about the
  4074. actual values. Otherwise, we will restore only one
  4075. register from the stack, since lowest will == highest in
  4076. `pop_failure_point'. */
  4077. active_reg_t dummy_low_reg, dummy_high_reg;
  4078. unsigned char *pdummy;
  4079. const char *sdummy;
  4080. DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  4081. POP_FAILURE_POINT (sdummy, pdummy,
  4082. dummy_low_reg, dummy_high_reg,
  4083. reg_dummy, reg_dummy, reg_info_dummy);
  4084. }
  4085. /* Note fall through. */
  4086. unconditional_jump:
  4087. #ifdef _LIBC
  4088. DEBUG_PRINT2 ("\n%p: ", p);
  4089. #else
  4090. DEBUG_PRINT2 ("\n0x%x: ", p);
  4091. #endif
  4092. /* Note fall through. */
  4093. /* Unconditionally jump (without popping any failure points). */
  4094. case jump:
  4095. EXTRACT_NUMBER_AND_INCR (mcnt, p); /* Get the amount to jump. */
  4096. DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  4097. p += mcnt; /* Do the jump. */
  4098. #ifdef _LIBC
  4099. DEBUG_PRINT2 ("(to %p).\n", p);
  4100. #else
  4101. DEBUG_PRINT2 ("(to 0x%x).\n", p);
  4102. #endif
  4103. break;
  4104. /* We need this opcode so we can detect where alternatives end
  4105. in `group_match_null_string_p' et al. */
  4106. case jump_past_alt:
  4107. DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  4108. goto unconditional_jump;
  4109. /* Normally, the on_failure_jump pushes a failure point, which
  4110. then gets popped at pop_failure_jump. We will end up at
  4111. pop_failure_jump, also, and with a pattern of, say, `a+', we
  4112. are skipping over the on_failure_jump, so we have to push
  4113. something meaningless for pop_failure_jump to pop. */
  4114. case dummy_failure_jump:
  4115. DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  4116. /* It doesn't matter what we push for the string here. What
  4117. the code at `fail' tests is the value for the pattern. */
  4118. PUSH_FAILURE_POINT (NULL, NULL, -2);
  4119. goto unconditional_jump;
  4120. /* At the end of an alternative, we need to push a dummy failure
  4121. point in case we are followed by a `pop_failure_jump', because
  4122. we don't want the failure point for the alternative to be
  4123. popped. For example, matching `(a|ab)*' against `aab'
  4124. requires that we match the `ab' alternative. */
  4125. case push_dummy_failure:
  4126. DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  4127. /* See comments just above at `dummy_failure_jump' about the
  4128. two zeroes. */
  4129. PUSH_FAILURE_POINT (NULL, NULL, -2);
  4130. break;
  4131. /* Have to succeed matching what follows at least n times.
  4132. After that, handle like `on_failure_jump'. */
  4133. case succeed_n:
  4134. EXTRACT_NUMBER (mcnt, p + 2);
  4135. DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  4136. assert (mcnt >= 0);
  4137. /* Originally, this is how many times we HAVE to succeed. */
  4138. if (mcnt > 0)
  4139. {
  4140. mcnt--;
  4141. p += 2;
  4142. STORE_NUMBER_AND_INCR (p, mcnt);
  4143. #ifdef _LIBC
  4144. DEBUG_PRINT3 (" Setting %p to %d.\n", p - 2, mcnt);
  4145. #else
  4146. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p - 2, mcnt);
  4147. #endif
  4148. }
  4149. else if (mcnt == 0)
  4150. {
  4151. #ifdef _LIBC
  4152. DEBUG_PRINT2 (" Setting two bytes from %p to no_op.\n", p+2);
  4153. #else
  4154. DEBUG_PRINT2 (" Setting two bytes from 0x%x to no_op.\n", p+2);
  4155. #endif
  4156. p[2] = (unsigned char) no_op;
  4157. p[3] = (unsigned char) no_op;
  4158. goto on_failure;
  4159. }
  4160. break;
  4161. case jump_n:
  4162. EXTRACT_NUMBER (mcnt, p + 2);
  4163. DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  4164. /* Originally, this is how many times we CAN jump. */
  4165. if (mcnt)
  4166. {
  4167. mcnt--;
  4168. STORE_NUMBER (p + 2, mcnt);
  4169. #ifdef _LIBC
  4170. DEBUG_PRINT3 (" Setting %p to %d.\n", p + 2, mcnt);
  4171. #else
  4172. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p + 2, mcnt);
  4173. #endif
  4174. goto unconditional_jump;
  4175. }
  4176. /* If don't have to jump any more, skip over the rest of command. */
  4177. else
  4178. p += 4;
  4179. break;
  4180. case set_number_at:
  4181. {
  4182. DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  4183. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4184. p1 = p + mcnt;
  4185. EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4186. #ifdef _LIBC
  4187. DEBUG_PRINT3 (" Setting %p to %d.\n", p1, mcnt);
  4188. #else
  4189. DEBUG_PRINT3 (" Setting 0x%x to %d.\n", p1, mcnt);
  4190. #endif
  4191. STORE_NUMBER (p1, mcnt);
  4192. break;
  4193. }
  4194. #if 0
  4195. /* The DEC Alpha C compiler 3.x generates incorrect code for the
  4196. test WORDCHAR_P (d - 1) != WORDCHAR_P (d) in the expansion of
  4197. AT_WORD_BOUNDARY, so this code is disabled. Expanding the
  4198. macro and introducing temporary variables works around the bug. */
  4199. case wordbound:
  4200. DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4201. if (AT_WORD_BOUNDARY (d))
  4202. break;
  4203. goto fail;
  4204. case notwordbound:
  4205. DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4206. if (AT_WORD_BOUNDARY (d))
  4207. goto fail;
  4208. break;
  4209. #else
  4210. case wordbound:
  4211. {
  4212. boolean prevchar, thischar;
  4213. DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4214. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  4215. break;
  4216. prevchar = WORDCHAR_P (d - 1);
  4217. thischar = WORDCHAR_P (d);
  4218. if (prevchar != thischar)
  4219. break;
  4220. goto fail;
  4221. }
  4222. case notwordbound:
  4223. {
  4224. boolean prevchar, thischar;
  4225. DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4226. if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d))
  4227. goto fail;
  4228. prevchar = WORDCHAR_P (d - 1);
  4229. thischar = WORDCHAR_P (d);
  4230. if (prevchar != thischar)
  4231. goto fail;
  4232. break;
  4233. }
  4234. #endif
  4235. case wordbeg:
  4236. DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  4237. if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
  4238. break;
  4239. goto fail;
  4240. case wordend:
  4241. DEBUG_PRINT1 ("EXECUTING wordend.\n");
  4242. if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
  4243. && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
  4244. break;
  4245. goto fail;
  4246. #ifdef emacs
  4247. case before_dot:
  4248. DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  4249. if (PTR_CHAR_POS ((unsigned char *) d) >= point)
  4250. goto fail;
  4251. break;
  4252. case at_dot:
  4253. DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4254. if (PTR_CHAR_POS ((unsigned char *) d) != point)
  4255. goto fail;
  4256. break;
  4257. case after_dot:
  4258. DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  4259. if (PTR_CHAR_POS ((unsigned char *) d) <= point)
  4260. goto fail;
  4261. break;
  4262. case syntaxspec:
  4263. DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  4264. mcnt = *p++;
  4265. goto matchsyntax;
  4266. case wordchar:
  4267. DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  4268. mcnt = (int) Sword;
  4269. matchsyntax:
  4270. PREFETCH ();
  4271. /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
  4272. d++;
  4273. if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt)
  4274. goto fail;
  4275. SET_REGS_MATCHED ();
  4276. break;
  4277. case notsyntaxspec:
  4278. DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  4279. mcnt = *p++;
  4280. goto matchnotsyntax;
  4281. case notwordchar:
  4282. DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  4283. mcnt = (int) Sword;
  4284. matchnotsyntax:
  4285. PREFETCH ();
  4286. /* Can't use *d++ here; SYNTAX may be an unsafe macro. */
  4287. d++;
  4288. if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt)
  4289. goto fail;
  4290. SET_REGS_MATCHED ();
  4291. break;
  4292. #else /* not emacs */
  4293. case wordchar:
  4294. DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  4295. PREFETCH ();
  4296. if (!WORDCHAR_P (d))
  4297. goto fail;
  4298. SET_REGS_MATCHED ();
  4299. d++;
  4300. break;
  4301. case notwordchar:
  4302. DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  4303. PREFETCH ();
  4304. if (WORDCHAR_P (d))
  4305. goto fail;
  4306. SET_REGS_MATCHED ();
  4307. d++;
  4308. break;
  4309. #endif /* not emacs */
  4310. default:
  4311. abort ();
  4312. }
  4313. continue; /* Successfully executed one pattern command; keep going. */
  4314. /* We goto here if a matching operation fails. */
  4315. fail:
  4316. if (!FAIL_STACK_EMPTY ())
  4317. { /* A restart point is known. Restore to that state. */
  4318. DEBUG_PRINT1 ("\nFAIL:\n");
  4319. POP_FAILURE_POINT (d, p,
  4320. lowest_active_reg, highest_active_reg,
  4321. regstart, regend, reg_info);
  4322. /* If this failure point is a dummy, try the next one. */
  4323. if (!p)
  4324. goto fail;
  4325. /* If we failed to the end of the pattern, don't examine *p. */
  4326. assert (p <= pend);
  4327. if (p < pend)
  4328. {
  4329. boolean is_a_jump_n = false;
  4330. /* If failed to a backwards jump that's part of a repetition
  4331. loop, need to pop this failure point and use the next one. */
  4332. switch ((re_opcode_t) *p)
  4333. {
  4334. case jump_n:
  4335. is_a_jump_n = true;
  4336. case maybe_pop_jump:
  4337. case pop_failure_jump:
  4338. case jump:
  4339. p1 = p + 1;
  4340. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4341. p1 += mcnt;
  4342. if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4343. || (!is_a_jump_n
  4344. && (re_opcode_t) *p1 == on_failure_jump))
  4345. goto fail;
  4346. break;
  4347. default:
  4348. /* do nothing */ ;
  4349. }
  4350. }
  4351. if (d >= string1 && d <= end1)
  4352. dend = end_match_1;
  4353. }
  4354. else
  4355. break; /* Matching at this starting point really fails. */
  4356. } /* for (;;) */
  4357. if (best_regs_set)
  4358. goto restore_best_regs;
  4359. FREE_VARIABLES ();
  4360. return -1; /* Failure to match. */
  4361. } /* re_match_2 */
  4362. /* Subroutine definitions for re_match_2. */
  4363. /* We are passed P pointing to a register number after a start_memory.
  4364. Return true if the pattern up to the corresponding stop_memory can
  4365. match the empty string, and false otherwise.
  4366. If we find the matching stop_memory, sets P to point to one past its number.
  4367. Otherwise, sets P to an undefined byte less than or equal to END.
  4368. We don't handle duplicates properly (yet). */
  4369. static boolean
  4370. group_match_null_string_p (p, end, reg_info)
  4371. unsigned char **p, *end;
  4372. register_info_type *reg_info;
  4373. {
  4374. int mcnt;
  4375. /* Point to after the args to the start_memory. */
  4376. unsigned char *p1 = *p + 2;
  4377. while (p1 < end)
  4378. {
  4379. /* Skip over opcodes that can match nothing, and return true or
  4380. false, as appropriate, when we get to one that can't, or to the
  4381. matching stop_memory. */
  4382. switch ((re_opcode_t) *p1)
  4383. {
  4384. /* Could be either a loop or a series of alternatives. */
  4385. case on_failure_jump:
  4386. p1++;
  4387. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4388. /* If the next operation is not a jump backwards in the
  4389. pattern. */
  4390. if (mcnt >= 0)
  4391. {
  4392. /* Go through the on_failure_jumps of the alternatives,
  4393. seeing if any of the alternatives cannot match nothing.
  4394. The last alternative starts with only a jump,
  4395. whereas the rest start with on_failure_jump and end
  4396. with a jump, e.g., here is the pattern for `a|b|c':
  4397. /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  4398. /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  4399. /exactn/1/c
  4400. So, we have to first go through the first (n-1)
  4401. alternatives and then deal with the last one separately. */
  4402. /* Deal with the first (n-1) alternatives, which start
  4403. with an on_failure_jump (see above) that jumps to right
  4404. past a jump_past_alt. */
  4405. while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
  4406. {
  4407. /* `mcnt' holds how many bytes long the alternative
  4408. is, including the ending `jump_past_alt' and
  4409. its number. */
  4410. if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
  4411. reg_info))
  4412. return false;
  4413. /* Move to right after this alternative, including the
  4414. jump_past_alt. */
  4415. p1 += mcnt;
  4416. /* Break if it's the beginning of an n-th alternative
  4417. that doesn't begin with an on_failure_jump. */
  4418. if ((re_opcode_t) *p1 != on_failure_jump)
  4419. break;
  4420. /* Still have to check that it's not an n-th
  4421. alternative that starts with an on_failure_jump. */
  4422. p1++;
  4423. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4424. if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
  4425. {
  4426. /* Get to the beginning of the n-th alternative. */
  4427. p1 -= 3;
  4428. break;
  4429. }
  4430. }
  4431. /* Deal with the last alternative: go back and get number
  4432. of the `jump_past_alt' just before it. `mcnt' contains
  4433. the length of the alternative. */
  4434. EXTRACT_NUMBER (mcnt, p1 - 2);
  4435. if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  4436. return false;
  4437. p1 += mcnt; /* Get past the n-th alternative. */
  4438. } /* if mcnt > 0 */
  4439. break;
  4440. case stop_memory:
  4441. assert (p1[1] == **p);
  4442. *p = p1 + 2;
  4443. return true;
  4444. default:
  4445. if (!common_op_match_null_string_p (&p1, end, reg_info))
  4446. return false;
  4447. }
  4448. } /* while p1 < end */
  4449. return false;
  4450. } /* group_match_null_string_p */
  4451. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  4452. It expects P to be the first byte of a single alternative and END one
  4453. byte past the last. The alternative can contain groups. */
  4454. static boolean
  4455. alt_match_null_string_p (p, end, reg_info)
  4456. unsigned char *p, *end;
  4457. register_info_type *reg_info;
  4458. {
  4459. int mcnt;
  4460. unsigned char *p1 = p;
  4461. while (p1 < end)
  4462. {
  4463. /* Skip over opcodes that can match nothing, and break when we get
  4464. to one that can't. */
  4465. switch ((re_opcode_t) *p1)
  4466. {
  4467. /* It's a loop. */
  4468. case on_failure_jump:
  4469. p1++;
  4470. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4471. p1 += mcnt;
  4472. break;
  4473. default:
  4474. if (!common_op_match_null_string_p (&p1, end, reg_info))
  4475. return false;
  4476. }
  4477. } /* while p1 < end */
  4478. return true;
  4479. } /* alt_match_null_string_p */
  4480. /* Deals with the ops common to group_match_null_string_p and
  4481. alt_match_null_string_p.
  4482. Sets P to one after the op and its arguments, if any. */
  4483. static boolean
  4484. common_op_match_null_string_p (p, end, reg_info)
  4485. unsigned char **p, *end;
  4486. register_info_type *reg_info;
  4487. {
  4488. int mcnt;
  4489. boolean ret;
  4490. int reg_no;
  4491. unsigned char *p1 = *p;
  4492. switch ((re_opcode_t) *p1++)
  4493. {
  4494. case no_op:
  4495. case begline:
  4496. case endline:
  4497. case begbuf:
  4498. case endbuf:
  4499. case wordbeg:
  4500. case wordend:
  4501. case wordbound:
  4502. case notwordbound:
  4503. #ifdef emacs
  4504. case before_dot:
  4505. case at_dot:
  4506. case after_dot:
  4507. #endif
  4508. break;
  4509. case start_memory:
  4510. reg_no = *p1;
  4511. assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  4512. ret = group_match_null_string_p (&p1, end, reg_info);
  4513. /* Have to set this here in case we're checking a group which
  4514. contains a group and a back reference to it. */
  4515. if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  4516. REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  4517. if (!ret)
  4518. return false;
  4519. break;
  4520. /* If this is an optimized succeed_n for zero times, make the jump. */
  4521. case jump:
  4522. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4523. if (mcnt >= 0)
  4524. p1 += mcnt;
  4525. else
  4526. return false;
  4527. break;
  4528. case succeed_n:
  4529. /* Get to the number of times to succeed. */
  4530. p1 += 2;
  4531. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4532. if (mcnt == 0)
  4533. {
  4534. p1 -= 4;
  4535. EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4536. p1 += mcnt;
  4537. }
  4538. else
  4539. return false;
  4540. break;
  4541. case duplicate:
  4542. if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  4543. return false;
  4544. break;
  4545. case set_number_at:
  4546. p1 += 4;
  4547. default:
  4548. /* All other opcodes mean we cannot match the empty string. */
  4549. return false;
  4550. }
  4551. *p = p1;
  4552. return true;
  4553. } /* common_op_match_null_string_p */
  4554. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  4555. bytes; nonzero otherwise. */
  4556. static int
  4557. bcmp_translate (s1, s2, len, translate)
  4558. const char *s1, *s2;
  4559. register int len;
  4560. RE_TRANSLATE_TYPE translate;
  4561. {
  4562. register const unsigned char *p1 = (const unsigned char *) s1;
  4563. register const unsigned char *p2 = (const unsigned char *) s2;
  4564. while (len)
  4565. {
  4566. if (translate[*p1++] != translate[*p2++]) return 1;
  4567. len--;
  4568. }
  4569. return 0;
  4570. }
  4571. /* Entry points for GNU code. */
  4572. /* re_compile_pattern is the GNU regular expression compiler: it
  4573. compiles PATTERN (of length SIZE) and puts the result in BUFP.
  4574. Returns 0 if the pattern was valid, otherwise an error string.
  4575. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  4576. are set in BUFP on entry.
  4577. We call regex_compile to do the actual compilation. */
  4578. const char *
  4579. re_compile_pattern (pattern, length, bufp)
  4580. const char *pattern;
  4581. size_t length;
  4582. struct re_pattern_buffer *bufp;
  4583. {
  4584. reg_errcode_t ret;
  4585. /* GNU code is written to assume at least RE_NREGS registers will be set
  4586. (and at least one extra will be -1). */
  4587. bufp->regs_allocated = REGS_UNALLOCATED;
  4588. /* And GNU code determines whether or not to get register information
  4589. by passing null for the REGS argument to re_match, etc., not by
  4590. setting no_sub. */
  4591. bufp->no_sub = 0;
  4592. /* Match anchors at newline. */
  4593. bufp->newline_anchor = 1;
  4594. ret = regex_compile (pattern, length, re_syntax_options, bufp);
  4595. if (!ret)
  4596. return NULL;
  4597. return gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
  4598. }
  4599. #ifdef _LIBC
  4600. weak_alias (__re_compile_pattern, re_compile_pattern)
  4601. #endif
  4602. /* Entry points compatible with 4.2 BSD regex library. We don't define
  4603. them unless specifically requested. */
  4604. #if defined _REGEX_RE_COMP || defined _LIBC
  4605. /* BSD has one and only one pattern buffer. */
  4606. static struct re_pattern_buffer re_comp_buf;
  4607. char *
  4608. #ifdef _LIBC
  4609. /* Make these definitions weak in libc, so POSIX programs can redefine
  4610. these names if they don't use our functions, and still use
  4611. regcomp/regexec below without link errors. */
  4612. weak_function
  4613. #endif
  4614. re_comp (s)
  4615. const char *s;
  4616. {
  4617. reg_errcode_t ret;
  4618. if (!s)
  4619. {
  4620. if (!re_comp_buf.buffer)
  4621. return gettext ("No previous regular expression");
  4622. return 0;
  4623. }
  4624. if (!re_comp_buf.buffer)
  4625. {
  4626. re_comp_buf.buffer = (unsigned char *) malloc (200);
  4627. if (re_comp_buf.buffer == NULL)
  4628. return (char *) gettext (re_error_msgid
  4629. + re_error_msgid_idx[(int) REG_ESPACE]);
  4630. re_comp_buf.allocated = 200;
  4631. re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  4632. if (re_comp_buf.fastmap == NULL)
  4633. return (char *) gettext (re_error_msgid
  4634. + re_error_msgid_idx[(int) REG_ESPACE]);
  4635. }
  4636. /* Since `re_exec' always passes NULL for the `regs' argument, we
  4637. don't need to initialize the pattern buffer fields which affect it. */
  4638. /* Match anchors at newlines. */
  4639. re_comp_buf.newline_anchor = 1;
  4640. ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  4641. if (!ret)
  4642. return NULL;
  4643. /* Yes, we're discarding `const' here if !HAVE_LIBINTL. */
  4644. return (char *) gettext (re_error_msgid + re_error_msgid_idx[(int) ret]);
  4645. }
  4646. int
  4647. #ifdef _LIBC
  4648. weak_function
  4649. #endif
  4650. re_exec (s)
  4651. const char *s;
  4652. {
  4653. const int len = strlen (s);
  4654. return
  4655. 0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  4656. }
  4657. #endif /* _REGEX_RE_COMP */
  4658. /* POSIX.2 functions. Don't define these for Emacs. */
  4659. #ifndef emacs
  4660. /* regcomp takes a regular expression as a string and compiles it.
  4661. PREG is a regex_t *. We do not expect any fields to be initialized,
  4662. since POSIX says we shouldn't. Thus, we set
  4663. `buffer' to the compiled pattern;
  4664. `used' to the length of the compiled pattern;
  4665. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  4666. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  4667. RE_SYNTAX_POSIX_BASIC;
  4668. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  4669. `fastmap' to an allocated space for the fastmap;
  4670. `fastmap_accurate' to zero;
  4671. `re_nsub' to the number of subexpressions in PATTERN.
  4672. PATTERN is the address of the pattern string.
  4673. CFLAGS is a series of bits which affect compilation.
  4674. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  4675. use POSIX basic syntax.
  4676. If REG_NEWLINE is set, then . and [^...] don't match newline.
  4677. Also, regexec will try a match beginning after every newline.
  4678. If REG_ICASE is set, then we considers upper- and lowercase
  4679. versions of letters to be equivalent when matching.
  4680. If REG_NOSUB is set, then when PREG is passed to regexec, that
  4681. routine will report only success or failure, and nothing about the
  4682. registers.
  4683. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  4684. the return codes and their meanings.) */
  4685. int
  4686. regcomp (preg, pattern, cflags)
  4687. regex_t *preg;
  4688. const char *pattern;
  4689. int cflags;
  4690. {
  4691. reg_errcode_t ret;
  4692. reg_syntax_t syntax
  4693. = (cflags & REG_EXTENDED) ?
  4694. RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  4695. /* regex_compile will allocate the space for the compiled pattern. */
  4696. preg->buffer = 0;
  4697. preg->allocated = 0;
  4698. preg->used = 0;
  4699. /* Try to allocate space for the fastmap. */
  4700. preg->fastmap = (char *) malloc (1 << BYTEWIDTH);
  4701. if (cflags & REG_ICASE)
  4702. {
  4703. unsigned i;
  4704. preg->translate
  4705. = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
  4706. * sizeof (*(RE_TRANSLATE_TYPE)0));
  4707. if (preg->translate == NULL)
  4708. return (int) REG_ESPACE;
  4709. /* Map uppercase characters to corresponding lowercase ones. */
  4710. for (i = 0; i < CHAR_SET_SIZE; i++)
  4711. preg->translate[i] = ISUPPER (i) ? TOLOWER (i) : i;
  4712. }
  4713. else
  4714. preg->translate = NULL;
  4715. /* If REG_NEWLINE is set, newlines are treated differently. */
  4716. if (cflags & REG_NEWLINE)
  4717. { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  4718. syntax &= ~RE_DOT_NEWLINE;
  4719. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  4720. /* It also changes the matching behavior. */
  4721. preg->newline_anchor = 1;
  4722. }
  4723. else
  4724. preg->newline_anchor = 0;
  4725. preg->no_sub = !!(cflags & REG_NOSUB);
  4726. /* POSIX says a null character in the pattern terminates it, so we
  4727. can use strlen here in compiling the pattern. */
  4728. ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  4729. /* POSIX doesn't distinguish between an unmatched open-group and an
  4730. unmatched close-group: both are REG_EPAREN. */
  4731. if (ret == REG_ERPAREN) ret = REG_EPAREN;
  4732. if (ret == REG_NOERROR && preg->fastmap)
  4733. {
  4734. /* Compute the fastmap now, since regexec cannot modify the pattern
  4735. buffer. */
  4736. if (re_compile_fastmap (preg) == -2)
  4737. {
  4738. /* Some error occurred while computing the fastmap, just forget
  4739. about it. */
  4740. free (preg->fastmap);
  4741. preg->fastmap = NULL;
  4742. }
  4743. }
  4744. return (int) ret;
  4745. }
  4746. #ifdef _LIBC
  4747. weak_alias (__regcomp, regcomp)
  4748. #endif
  4749. /* regexec searches for a given pattern, specified by PREG, in the
  4750. string STRING.
  4751. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  4752. `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  4753. least NMATCH elements, and we set them to the offsets of the
  4754. corresponding matched substrings.
  4755. EFLAGS specifies `execution flags' which affect matching: if
  4756. REG_NOTBOL is set, then ^ does not match at the beginning of the
  4757. string; if REG_NOTEOL is set, then $ does not match at the end.
  4758. We return 0 if we find a match and REG_NOMATCH if not. */
  4759. int
  4760. regexec (preg, string, nmatch, pmatch, eflags)
  4761. const regex_t *preg;
  4762. const char *string;
  4763. size_t nmatch;
  4764. regmatch_t pmatch[];
  4765. int eflags;
  4766. {
  4767. int ret;
  4768. struct re_registers regs;
  4769. regex_t private_preg;
  4770. int len = strlen (string);
  4771. boolean want_reg_info = !preg->no_sub && nmatch > 0;
  4772. private_preg = *preg;
  4773. private_preg.not_bol = !!(eflags & REG_NOTBOL);
  4774. private_preg.not_eol = !!(eflags & REG_NOTEOL);
  4775. /* The user has told us exactly how many registers to return
  4776. information about, via `nmatch'. We have to pass that on to the
  4777. matching routines. */
  4778. private_preg.regs_allocated = REGS_FIXED;
  4779. if (want_reg_info)
  4780. {
  4781. regs.num_regs = nmatch;
  4782. regs.start = TALLOC (nmatch * 2, regoff_t);
  4783. if (regs.start == NULL)
  4784. return (int) REG_NOMATCH;
  4785. regs.end = regs.start + nmatch;
  4786. }
  4787. /* Perform the searching operation. */
  4788. ret = re_search (&private_preg, string, len,
  4789. /* start: */ 0, /* range: */ len,
  4790. want_reg_info ? &regs : (struct re_registers *) 0);
  4791. /* Copy the register information to the POSIX structure. */
  4792. if (want_reg_info)
  4793. {
  4794. if (ret >= 0)
  4795. {
  4796. unsigned r;
  4797. for (r = 0; r < nmatch; r++)
  4798. {
  4799. pmatch[r].rm_so = regs.start[r];
  4800. pmatch[r].rm_eo = regs.end[r];
  4801. }
  4802. }
  4803. /* If we needed the temporary register info, free the space now. */
  4804. free (regs.start);
  4805. }
  4806. /* We want zero return to mean success, unlike `re_search'. */
  4807. return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  4808. }
  4809. #ifdef _LIBC
  4810. weak_alias (__regexec, regexec)
  4811. #endif
  4812. /* Returns a message corresponding to an error code, ERRCODE, returned
  4813. from either regcomp or regexec. We don't use PREG here. */
  4814. size_t
  4815. regerror (err, preg, errbuf, errbuf_size)
  4816. int err;
  4817. const regex_t *preg;
  4818. char *errbuf;
  4819. size_t errbuf_size;
  4820. {
  4821. const char *msg;
  4822. size_t msg_size;
  4823. if (err < 0
  4824. || err >= (int) (sizeof (re_error_msgid_idx)
  4825. / sizeof (re_error_msgid_idx[0])))
  4826. /* Only error codes returned by the rest of the code should be passed
  4827. to this routine. If we are given anything else, or if other regex
  4828. code generates an invalid error code, then the program has a bug.
  4829. Dump core so we can fix it. */
  4830. abort ();
  4831. msg = gettext (re_error_msgid + re_error_msgid_idx[err]);
  4832. msg_size = strlen (msg) + 1; /* Includes the null. */
  4833. if (errbuf_size != 0)
  4834. {
  4835. if (msg_size > errbuf_size)
  4836. {
  4837. #if defined HAVE_MEMPCPY || defined _LIBC
  4838. *((char *) __mempcpy (errbuf, msg, errbuf_size - 1)) = '\0';
  4839. #else
  4840. memcpy (errbuf, msg, errbuf_size - 1);
  4841. errbuf[errbuf_size - 1] = 0;
  4842. #endif
  4843. }
  4844. else
  4845. memcpy (errbuf, msg, msg_size);
  4846. }
  4847. return msg_size;
  4848. }
  4849. #ifdef _LIBC
  4850. weak_alias (__regerror, regerror)
  4851. #endif
  4852. /* Free dynamically allocated space used by PREG. */
  4853. void
  4854. regfree (preg)
  4855. regex_t *preg;
  4856. {
  4857. if (preg->buffer != NULL)
  4858. free (preg->buffer);
  4859. preg->buffer = NULL;
  4860. preg->allocated = 0;
  4861. preg->used = 0;
  4862. if (preg->fastmap != NULL)
  4863. free (preg->fastmap);
  4864. preg->fastmap = NULL;
  4865. preg->fastmap_accurate = 0;
  4866. if (preg->translate != NULL)
  4867. free (preg->translate);
  4868. preg->translate = NULL;
  4869. }
  4870. #ifdef _LIBC
  4871. weak_alias (__regfree, regfree)
  4872. #endif
  4873. #endif /* not emacs */