regex.c 166 KB

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