regex.c 166 KB

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