regex.c 166 KB

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