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