rx.c 177 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273
  1. /* Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  2. This file is part of the librx library.
  3. Librx is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU Library General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. Librx is distributed in the hope that it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  10. for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this software; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA
  14. 02139, USA. */
  15. /* NOTE!!! AIX is so losing it requires this to be the first thing in the
  16. * file.
  17. * Do not put ANYTHING before it!
  18. */
  19. #if !defined (__GNUC__) && defined (_AIX)
  20. #pragma alloca
  21. #endif
  22. /* To make linux happy? */
  23. #ifndef _GNU_SOURCE
  24. #define _GNU_SOURCE
  25. #endif
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #ifndef isgraph
  31. #define isgraph(c) (isprint (c) && !isspace (c))
  32. #endif
  33. #ifndef isblank
  34. #define isblank(c) ((c) == ' ' || (c) == '\t')
  35. #endif
  36. #include <sys/types.h>
  37. #undef MAX
  38. #undef MIN
  39. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  40. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  41. typedef char boolean;
  42. #define false 0
  43. #define true 1
  44. #ifndef __GCC__
  45. #undef __inline__
  46. #define __inline__
  47. #endif
  48. /* Emacs already defines alloca, sometimes. */
  49. #ifndef alloca
  50. /* Make alloca work the best possible way. */
  51. #ifdef __GNUC__
  52. #define alloca __builtin_alloca
  53. #else /* not __GNUC__ */
  54. #if HAVE_ALLOCA_H
  55. #include <alloca.h>
  56. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  57. #ifndef _AIX /* Already did AIX, up at the top. */
  58. char *alloca();
  59. #endif /* not _AIX */
  60. #endif /* not HAVE_ALLOCA_H */
  61. #endif /* not __GNUC__ */
  62. #endif /* not alloca */
  63. /* Memory management and stuff for emacs. */
  64. #define CHARBITS 8
  65. #define remalloc(M, S) (M ? realloc (M, S) : malloc (S))
  66. /* Should we use malloc or alloca? If REGEX_MALLOC is not defined, we
  67. * use `alloca' instead of `malloc' for the backtracking stack.
  68. *
  69. * Emacs will die miserably if we don't do this.
  70. */
  71. #ifdef REGEX_MALLOC
  72. #define REGEX_ALLOCATE malloc
  73. #else /* not REGEX_MALLOC */
  74. #define REGEX_ALLOCATE alloca
  75. #endif /* not REGEX_MALLOC */
  76. #ifdef RX_WANT_RX_DEFS
  77. #define RX_DECL extern
  78. #define RX_DEF_QUAL
  79. #else
  80. #define RX_WANT_RX_DEFS
  81. #define RX_DECL static
  82. #define RX_DEF_QUAL static
  83. #endif
  84. #include <regex.h>
  85. #undef RX_DECL
  86. #define RX_DECL RX_DEF_QUAL
  87. /*
  88. * Prototypes.
  89. */
  90. #ifdef __STDC__
  91. RX_DECL struct rx_hash_item
  92. *rx_hash_find(struct rx_hash *, unsigned long,
  93. void *, struct rx_hash_rules *);
  94. RX_DECL struct rx_hash_item
  95. *rx_hash_find(struct rx_hash *, unsigned long,
  96. void *, struct rx_hash_rules *);
  97. RX_DECL struct rx_hash_item
  98. *rx_hash_store(struct rx_hash *, unsigned long,
  99. void *, struct rx_hash_rules *);
  100. RX_DECL void rx_hash_free(struct rx_hash_item *, struct rx_hash_rules *);
  101. RX_DECL void rx_free_hash_table(struct rx_hash *, rx_hash_freefn,
  102. struct rx_hash_rules *);
  103. RX_DECL rx_Bitset rx_cset(struct rx *);
  104. RX_DECL rx_Bitset rx_copy_cset(struct rx *, rx_Bitset);
  105. RX_DECL void rx_free_cset(struct rx *, rx_Bitset);
  106. static struct rx_hash_item
  107. *compiler_hash_item_alloc(struct rx_hash_rules *, void *);
  108. static struct rx_hash
  109. *compiler_hash_alloc(struct rx_hash_rules *);
  110. static void compiler_free_hash(struct rx_hash *, struct rx_hash_rules *);
  111. static void compiler_free_hash_item(struct rx_hash_item *,
  112. struct rx_hash_rules *);
  113. RX_DECL struct rexp_node
  114. *rexp_node(struct rx *, enum rexp_node_type);
  115. RX_DECL struct rexp_node
  116. *rx_mk_r_cset(struct rx *, rx_Bitset);
  117. RX_DECL struct rexp_node
  118. *rx_mk_r_concat(struct rx *, struct rexp_node *, struct rexp_node *);
  119. RX_DECL struct rexp_node
  120. *rx_mk_r_alternate(struct rx *, struct rexp_node *, struct rexp_node *);
  121. RX_DECL struct rexp_node
  122. *rx_mk_r_alternate(struct rx *, struct rexp_node *, struct rexp_node *);
  123. RX_DECL struct rexp_node
  124. *rx_mk_r_opt(struct rx *, struct rexp_node *);
  125. RX_DECL struct rexp_node
  126. *rx_mk_r_star(struct rx *, struct rexp_node *);
  127. RX_DECL struct rexp_node
  128. *rx_mk_r_2phase_star(struct rx *, struct rexp_node *, struct rexp_node *);
  129. RX_DECL struct rexp_node
  130. *rx_mk_r_side_effect(struct rx *, rx_side_effect);
  131. //RX_DECL struct rexp_node
  132. // *rx_mk_r_data (struct rx *, void *);
  133. RX_DECL void rx_free_rexp(struct rx *, struct rexp_node *);
  134. RX_DECL struct rexp_node
  135. *rx_copy_rexp(struct rx *, struct rexp_node *);
  136. RX_DECL struct rx_nfa_state
  137. *rx_nfa_state(struct rx *);
  138. RX_DECL void rx_free_nfa_state(struct rx_nfa_state *);
  139. RX_DECL struct rx_nfa_state
  140. *rx_id_to_nfa_state(struct rx *, int);
  141. RX_DECL struct rx_nfa_edge
  142. *rx_nfa_edge(struct rx *, enum rx_nfa_etype,
  143. struct rx_nfa_state *, struct rx_nfa_state *);
  144. RX_DECL void rx_free_nfa_edge(struct rx_nfa_edge *);
  145. static struct rx_possible_future
  146. *rx_possible_future(struct rx *, struct rx_se_list *);
  147. static void rx_free_possible_future(struct rx_possible_future *);
  148. RX_DECL void rx_free_nfa(struct rx *);
  149. RX_DECL int rx_build_nfa(struct rx *, struct rexp_node *,
  150. struct rx_nfa_state **, struct rx_nfa_state **);
  151. RX_DECL void rx_name_nfa_states(struct rx *);
  152. static int se_list_cmp(void *, void *);
  153. static int se_list_equal(void *, void *);
  154. static struct rx_se_list
  155. *hash_cons_se_prog(struct rx *, struct rx_hash *,
  156. void *, struct rx_se_list *);
  157. static struct rx_se_list
  158. *hash_se_prog(struct rx *, struct rx_hash *, struct rx_se_list *);
  159. static int nfa_set_cmp(void *, void *);
  160. static int nfa_set_equal(void *, void *);
  161. static struct rx_nfa_state_set
  162. *nfa_set_cons(struct rx *, struct rx_hash *,
  163. struct rx_nfa_state *, struct rx_nfa_state_set *);
  164. static struct rx_nfa_state_set
  165. *nfa_set_enjoin(struct rx *, struct rx_hash *,
  166. struct rx_nfa_state *, struct rx_nfa_state_set *);
  167. #endif
  168. #ifndef emacs
  169. #ifdef SYNTAX_TABLE
  170. extern char *re_syntax_table;
  171. #else /* not SYNTAX_TABLE */
  172. #ifndef RX_WANT_RX_DEFS
  173. RX_DECL char re_syntax_table[CHAR_SET_SIZE];
  174. #endif
  175. #ifdef __STDC__
  176. static void init_syntax_once(void)
  177. #else
  178. static void init_syntax_once()
  179. #endif
  180. {
  181. register int c;
  182. static int done = 0;
  183. if (done)
  184. return;
  185. bzero(re_syntax_table, sizeof re_syntax_table);
  186. for (c = 'a'; c <= 'z'; c++)
  187. re_syntax_table[c] = Sword;
  188. for (c = 'A'; c <= 'Z'; c++)
  189. re_syntax_table[c] = Sword;
  190. for (c = '0'; c <= '9'; c++)
  191. re_syntax_table[c] = Sword;
  192. re_syntax_table['_'] = Sword;
  193. done = 1;
  194. }
  195. #endif /* not SYNTAX_TABLE */
  196. #endif /* not emacs */
  197. /* Compile with `-DRX_DEBUG' and use the following flags.
  198. *
  199. * Debugging flags:
  200. * rx_debug - print information as a regexp is compiled
  201. * rx_debug_trace - print information as a regexp is executed
  202. */
  203. #ifdef RX_DEBUG
  204. int rx_debug_compile = 0;
  205. int rx_debug_trace = 0;
  206. static struct re_pattern_buffer *dbug_rxb = 0;
  207. /*
  208. * More Prototypes
  209. */
  210. #ifdef __STDC__
  211. typedef void (*side_effect_printer) (struct rx *, void *, FILE *);
  212. static void print_cset(struct rx *, rx_Bitset, FILE *);
  213. static void print_rexp(struct rx *, struct rexp_node *, int,
  214. side_effect_printer, FILE *);
  215. static void print_nfa(struct rx *, struct rx_nfa_state *,
  216. side_effect_printer, FILE *);
  217. static void re_seprint(struct rx *, void *, FILE *);
  218. void print_compiled_pattern(struct re_pattern_buffer *);
  219. void print_fastmap(char *);
  220. #else
  221. typedef void (*side_effect_printer) ();
  222. static void print_cset();
  223. #endif
  224. #ifdef __STDC__
  225. static void
  226. print_rexp(struct rx *rx,
  227. struct rexp_node *node, int depth,
  228. side_effect_printer seprint, FILE * fp)
  229. #else
  230. static void print_rexp(rx, node, depth, seprint, fp)
  231. struct rx *rx;
  232. struct rexp_node *node;
  233. int depth;
  234. side_effect_printer seprint;
  235. FILE *fp;
  236. #endif
  237. {
  238. if (!node)
  239. return;
  240. else {
  241. switch (node->type) {
  242. case r_cset:
  243. {
  244. fprintf(fp, "%*s", depth, "");
  245. print_cset(rx, node->params.cset, fp);
  246. fputc('\n', fp);
  247. break;
  248. }
  249. case r_opt:
  250. case r_star:
  251. fprintf(fp, "%*s%s\n", depth, "",
  252. node->type == r_opt ? "opt" : "star");
  253. print_rexp(rx, node->params.pair.left, depth + 3, seprint, fp);
  254. break;
  255. case r_2phase_star:
  256. fprintf(fp, "%*s2phase star\n", depth, "");
  257. print_rexp(rx, node->params.pair.right, depth + 3, seprint,
  258. fp);
  259. print_rexp(rx, node->params.pair.left, depth + 3, seprint, fp);
  260. break;
  261. case r_alternate:
  262. case r_concat:
  263. fprintf(fp, "%*s%s\n", depth, "",
  264. node->type == r_alternate ? "alt" : "concat");
  265. print_rexp(rx, node->params.pair.left, depth + 3, seprint, fp);
  266. print_rexp(rx, node->params.pair.right, depth + 3, seprint,
  267. fp);
  268. break;
  269. case r_side_effect:
  270. fprintf(fp, "%*sSide effect: ", depth, "");
  271. seprint(rx, node->params.side_effect, fp);
  272. fputc('\n', fp);
  273. }
  274. }
  275. }
  276. #ifdef __STDC__
  277. static void
  278. print_nfa(struct rx *rx,
  279. struct rx_nfa_state *n, side_effect_printer seprint, FILE * fp)
  280. #else
  281. static void print_nfa(rx, n, seprint, fp)
  282. struct rx *rx;
  283. struct rx_nfa_state *n;
  284. side_effect_printer seprint;
  285. FILE *fp;
  286. #endif
  287. {
  288. while (n) {
  289. struct rx_nfa_edge *e = n->edges;
  290. struct rx_possible_future *ec = n->futures;
  291. fprintf(fp, "node %d %s\n", n->id,
  292. n->is_final ? "final" : (n->is_start ? "start" : ""));
  293. while (e) {
  294. fprintf(fp, " edge to %d, ", e->dest->id);
  295. switch (e->type) {
  296. case ne_epsilon:
  297. fprintf(fp, "epsilon\n");
  298. break;
  299. case ne_side_effect:
  300. fprintf(fp, "side effect ");
  301. seprint(rx, e->params.side_effect, fp);
  302. fputc('\n', fp);
  303. break;
  304. case ne_cset:
  305. fprintf(fp, "cset ");
  306. print_cset(rx, e->params.cset, fp);
  307. fputc('\n', fp);
  308. break;
  309. }
  310. e = e->next;
  311. }
  312. while (ec) {
  313. int x;
  314. struct rx_nfa_state_set *s;
  315. struct rx_se_list *l;
  316. fprintf(fp, " eclosure to {");
  317. for (s = ec->destset; s; s = s->cdr)
  318. fprintf(fp, "%d ", s->car->id);
  319. fprintf(fp, "} (");
  320. for (l = ec->effects; l; l = l->cdr) {
  321. seprint(rx, l->car, fp);
  322. fputc(' ', fp);
  323. }
  324. fprintf(fp, ")\n");
  325. ec = ec->next;
  326. }
  327. n = n->next;
  328. }
  329. }
  330. static char *efnames[] = {
  331. "bogon",
  332. "re_se_try",
  333. "re_se_pushback",
  334. "re_se_push0",
  335. "re_se_pushpos",
  336. "re_se_chkpos",
  337. "re_se_poppos",
  338. "re_se_at_dot",
  339. "re_se_syntax",
  340. "re_se_not_syntax",
  341. "re_se_begbuf",
  342. "re_se_hat",
  343. "re_se_wordbeg",
  344. "re_se_wordbound",
  345. "re_se_notwordbound",
  346. "re_se_wordend",
  347. "re_se_endbuf",
  348. "re_se_dollar",
  349. "re_se_fail",
  350. };
  351. static char *efnames2[] = {
  352. "re_se_win",
  353. "re_se_lparen",
  354. "re_se_rparen",
  355. "re_se_backref",
  356. "re_se_iter",
  357. "re_se_end_iter",
  358. "re_se_tv"
  359. };
  360. static char *inx_names[] = {
  361. "rx_backtrack_point",
  362. "rx_do_side_effects",
  363. "rx_cache_miss",
  364. "rx_next_char",
  365. "rx_backtrack",
  366. "rx_error_inx",
  367. "rx_num_instructions"
  368. };
  369. #ifdef __STDC__
  370. static void re_seprint(struct rx *rx, void *effect, FILE * fp)
  371. #else
  372. static void re_seprint(rx, effect, fp)
  373. struct rx *rx;
  374. void *effect;
  375. FILE *fp;
  376. #endif
  377. {
  378. if ((int) effect < 0)
  379. fputs(efnames[-(int) effect], fp);
  380. else if (dbug_rxb) {
  381. struct re_se_params *p = &dbug_rxb->se_params[(int) effect];
  382. fprintf(fp, "%s(%d,%d)", efnames2[p->se], p->op1, p->op2);
  383. } else
  384. fprintf(fp, "[complex op # %d]", (int) effect);
  385. }
  386. /* These are so the regex.c regression tests will compile. */
  387. void print_compiled_pattern(rxb)
  388. struct re_pattern_buffer *rxb;
  389. {
  390. }
  391. void print_fastmap(fm)
  392. char *fm;
  393. {
  394. }
  395. #endif /* RX_DEBUG */
  396. /* This page: Bitsets. Completely unintersting. */
  397. //RX_DECL int rx_bitset_is_equal (int, rx_Bitset, rx_Bitset);
  398. RX_DECL int rx_bitset_is_subset(int, rx_Bitset, rx_Bitset);
  399. //RX_DECL int rx_bitset_empty (int, rx_Bitset);
  400. RX_DECL void rx_bitset_null(int, rx_Bitset);
  401. RX_DECL void rx_bitset_complement(int, rx_Bitset);
  402. RX_DECL void rx_bitset_complement(int, rx_Bitset);
  403. RX_DECL void rx_bitset_assign(int, rx_Bitset, rx_Bitset);
  404. RX_DECL void rx_bitset_union(int, rx_Bitset, rx_Bitset);
  405. RX_DECL void rx_bitset_intersection(int, rx_Bitset, rx_Bitset);
  406. RX_DECL void rx_bitset_difference(int, rx_Bitset, rx_Bitset);
  407. //RX_DECL void rx_bitset_revdifference (int, rx_Bitset, rx_Bitset);
  408. #ifdef emacs
  409. RX_DECL void rx_bitset_xor(int, rx_Bitset, rx_Bitset);
  410. #endif
  411. RX_DECL unsigned long rx_bitset_hash(int, rx_Bitset);
  412. #if 0
  413. #ifdef __STDC__
  414. RX_DECL int rx_bitset_is_equal(int size, rx_Bitset a, rx_Bitset b)
  415. #else
  416. RX_DECL int rx_bitset_is_equal(size, a, b)
  417. int size;
  418. rx_Bitset a;
  419. rx_Bitset b;
  420. #endif
  421. {
  422. int x;
  423. RX_subset s = b[0];
  424. b[0] = ~a[0];
  425. for (x = rx_bitset_numb_subsets(size) - 1; a[x] == b[x]; --x);
  426. b[0] = s;
  427. return !x && s == a[0];
  428. }
  429. #endif
  430. #ifdef __STDC__
  431. RX_DECL int rx_bitset_is_subset(int size, rx_Bitset a, rx_Bitset b)
  432. #else
  433. RX_DECL int rx_bitset_is_subset(size, a, b)
  434. int size;
  435. rx_Bitset a;
  436. rx_Bitset b;
  437. #endif
  438. {
  439. int x = rx_bitset_numb_subsets(size) - 1;
  440. while (x-- && (a[x] & b[x]) == a[x]);
  441. return x == -1;
  442. }
  443. #if 0
  444. #ifdef __STDC__
  445. RX_DECL int rx_bitset_empty(int size, rx_Bitset set)
  446. #else
  447. RX_DECL int rx_bitset_empty(size, set)
  448. int size;
  449. rx_Bitset set;
  450. #endif
  451. {
  452. int x;
  453. RX_subset s = set[0];
  454. set[0] = 1;
  455. for (x = rx_bitset_numb_subsets(size) - 1; !set[x]; --x);
  456. set[0] = s;
  457. return !s;
  458. }
  459. #endif
  460. #ifdef __STDC__
  461. RX_DECL void rx_bitset_null(int size, rx_Bitset b)
  462. #else
  463. RX_DECL void rx_bitset_null(size, b)
  464. int size;
  465. rx_Bitset b;
  466. #endif
  467. {
  468. bzero(b, rx_sizeof_bitset(size));
  469. }
  470. #ifdef __STDC__
  471. RX_DECL void rx_bitset_universe(int size, rx_Bitset b)
  472. #else
  473. RX_DECL void rx_bitset_universe(size, b)
  474. int size;
  475. rx_Bitset b;
  476. #endif
  477. {
  478. int x = rx_bitset_numb_subsets(size);
  479. while (x--)
  480. *b++ = ~(RX_subset) 0;
  481. }
  482. #ifdef __STDC__
  483. RX_DECL void rx_bitset_complement(int size, rx_Bitset b)
  484. #else
  485. RX_DECL void rx_bitset_complement(size, b)
  486. int size;
  487. rx_Bitset b;
  488. #endif
  489. {
  490. int x = rx_bitset_numb_subsets(size);
  491. while (x--) {
  492. *b = ~*b;
  493. ++b;
  494. }
  495. }
  496. #ifdef __STDC__
  497. RX_DECL void rx_bitset_assign(int size, rx_Bitset a, rx_Bitset b)
  498. #else
  499. RX_DECL void rx_bitset_assign(size, a, b)
  500. int size;
  501. rx_Bitset a;
  502. rx_Bitset b;
  503. #endif
  504. {
  505. int x;
  506. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  507. a[x] = b[x];
  508. }
  509. #ifdef __STDC__
  510. RX_DECL void rx_bitset_union(int size, rx_Bitset a, rx_Bitset b)
  511. #else
  512. RX_DECL void rx_bitset_union(size, a, b)
  513. int size;
  514. rx_Bitset a;
  515. rx_Bitset b;
  516. #endif
  517. {
  518. int x;
  519. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  520. a[x] |= b[x];
  521. }
  522. #ifdef __STDC__
  523. RX_DECL void rx_bitset_intersection(int size, rx_Bitset a, rx_Bitset b)
  524. #else
  525. RX_DECL void rx_bitset_intersection(size, a, b)
  526. int size;
  527. rx_Bitset a;
  528. rx_Bitset b;
  529. #endif
  530. {
  531. int x;
  532. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  533. a[x] &= b[x];
  534. }
  535. #ifdef __STDC__
  536. RX_DECL void rx_bitset_difference(int size, rx_Bitset a, rx_Bitset b)
  537. #else
  538. RX_DECL void rx_bitset_difference(size, a, b)
  539. int size;
  540. rx_Bitset a;
  541. rx_Bitset b;
  542. #endif
  543. {
  544. int x;
  545. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  546. a[x] &= ~b[x];
  547. }
  548. #if 0
  549. #ifdef __STDC__
  550. RX_DECL void rx_bitset_revdifference(int size, rx_Bitset a, rx_Bitset b)
  551. #else
  552. RX_DECL void rx_bitset_revdifference(size, a, b)
  553. int size;
  554. rx_Bitset a;
  555. rx_Bitset b;
  556. #endif
  557. {
  558. int x;
  559. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  560. a[x] = ~a[x] & b[x];
  561. }
  562. #endif
  563. #ifdef emacs
  564. #ifdef __STDC__
  565. RX_DECL void rx_bitset_xor(int size, rx_Bitset a, rx_Bitset b)
  566. #else
  567. RX_DECL void rx_bitset_xor(size, a, b)
  568. int size;
  569. rx_Bitset a;
  570. rx_Bitset b;
  571. #endif
  572. {
  573. int x;
  574. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  575. a[x] ^= b[x];
  576. }
  577. #endif
  578. #ifdef __STDC__
  579. RX_DECL unsigned long rx_bitset_hash(int size, rx_Bitset b)
  580. #else
  581. RX_DECL unsigned long rx_bitset_hash(size, b)
  582. int size;
  583. rx_Bitset b;
  584. #endif
  585. {
  586. int x;
  587. unsigned long hash = (unsigned long) rx_bitset_hash;
  588. for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x)
  589. hash ^= rx_bitset_subset_val(b, x);
  590. return hash;
  591. }
  592. RX_DECL RX_subset rx_subset_singletons[RX_subset_bits] = {
  593. 0x1,
  594. 0x2,
  595. 0x4,
  596. 0x8,
  597. 0x10,
  598. 0x20,
  599. 0x40,
  600. 0x80,
  601. 0x100,
  602. 0x200,
  603. 0x400,
  604. 0x800,
  605. 0x1000,
  606. 0x2000,
  607. 0x4000,
  608. 0x8000,
  609. 0x10000,
  610. 0x20000,
  611. 0x40000,
  612. 0x80000,
  613. 0x100000,
  614. 0x200000,
  615. 0x400000,
  616. 0x800000,
  617. 0x1000000,
  618. 0x2000000,
  619. 0x4000000,
  620. 0x8000000,
  621. 0x10000000,
  622. 0x20000000,
  623. 0x40000000,
  624. 0x80000000
  625. };
  626. #ifdef RX_DEBUG
  627. #ifdef __STDC__
  628. static void print_cset(struct rx *rx, rx_Bitset cset, FILE * fp)
  629. #else
  630. static void print_cset(rx, cset, fp)
  631. struct rx *rx;
  632. rx_Bitset cset;
  633. FILE *fp;
  634. #endif
  635. {
  636. int x;
  637. fputc('[', fp);
  638. for (x = 0; x < rx->local_cset_size; ++x)
  639. if (RX_bitset_member(cset, x)) {
  640. if (isprint(x))
  641. fputc(x, fp);
  642. else
  643. fprintf(fp, "\\0%o ", x);
  644. }
  645. fputc(']', fp);
  646. }
  647. #endif /* RX_DEBUG */
  648. static unsigned long rx_hash_masks[4] = {
  649. 0x12488421,
  650. 0x96699669,
  651. 0xbe7dd7eb,
  652. 0xffffffff
  653. };
  654. /* Hash tables */
  655. #ifdef __STDC__
  656. RX_DECL struct rx_hash_item *rx_hash_find(struct rx_hash *table,
  657. unsigned long hash,
  658. void *value,
  659. struct rx_hash_rules *rules)
  660. #else
  661. RX_DECL struct rx_hash_item *rx_hash_find(table, hash, value, rules)
  662. struct rx_hash *table;
  663. unsigned long hash;
  664. void *value;
  665. struct rx_hash_rules *rules;
  666. #endif
  667. {
  668. rx_hash_eq eq = rules->eq;
  669. int maskc = 0;
  670. long mask = rx_hash_masks[0];
  671. int bucket = (hash & mask) % 13;
  672. while (table->children[bucket]) {
  673. table = table->children[bucket];
  674. ++maskc;
  675. mask = rx_hash_masks[maskc];
  676. bucket = (hash & mask) % 13;
  677. }
  678. {
  679. struct rx_hash_item *it = table->buckets[bucket];
  680. while (it)
  681. if (eq(it->data, value))
  682. return it;
  683. else
  684. it = it->next_same_hash;
  685. }
  686. return 0;
  687. }
  688. #ifdef __STDC__
  689. RX_DECL struct rx_hash_item *rx_hash_store(struct rx_hash *table,
  690. unsigned long hash,
  691. void *value,
  692. struct rx_hash_rules *rules)
  693. #else
  694. RX_DECL struct rx_hash_item *rx_hash_store(table, hash, value, rules)
  695. struct rx_hash *table;
  696. unsigned long hash;
  697. void *value;
  698. struct rx_hash_rules *rules;
  699. #endif
  700. {
  701. rx_hash_eq eq = rules->eq;
  702. int maskc = 0;
  703. long mask = rx_hash_masks[0];
  704. int bucket = (hash & mask) % 13;
  705. int depth = 0;
  706. while (table->children[bucket]) {
  707. table = table->children[bucket];
  708. ++maskc;
  709. mask = rx_hash_masks[maskc];
  710. bucket = (hash & mask) % 13;
  711. ++depth;
  712. }
  713. {
  714. struct rx_hash_item *it = table->buckets[bucket];
  715. while (it)
  716. if (eq(it->data, value))
  717. return it;
  718. else
  719. it = it->next_same_hash;
  720. }
  721. {
  722. if ((depth < 3)
  723. && (table->bucket_size[bucket] >= 4)) {
  724. struct rx_hash *newtab = ((struct rx_hash *)
  725. rules->hash_alloc(rules));
  726. if (!newtab)
  727. goto add_to_bucket;
  728. bzero(newtab, sizeof(*newtab));
  729. newtab->parent = table;
  730. {
  731. struct rx_hash_item *them = table->buckets[bucket];
  732. unsigned long newmask = rx_hash_masks[maskc + 1];
  733. while (them) {
  734. struct rx_hash_item *save = them->next_same_hash;
  735. int new_buck = (them->hash & newmask) % 13;
  736. them->next_same_hash = newtab->buckets[new_buck];
  737. newtab->buckets[new_buck] = them;
  738. them->table = newtab;
  739. them = save;
  740. ++newtab->bucket_size[new_buck];
  741. ++newtab->refs;
  742. }
  743. table->refs =
  744. (table->refs - table->bucket_size[bucket] + 1);
  745. table->bucket_size[bucket] = 0;
  746. table->buckets[bucket] = 0;
  747. table->children[bucket] = newtab;
  748. table = newtab;
  749. bucket = (hash & newmask) % 13;
  750. }
  751. }
  752. }
  753. add_to_bucket:
  754. {
  755. struct rx_hash_item *it = ((struct rx_hash_item *)
  756. rules->hash_item_alloc(rules, value));
  757. if (!it)
  758. return 0;
  759. it->hash = hash;
  760. it->table = table;
  761. /* DATA and BINDING are to be set in hash_item_alloc */
  762. it->next_same_hash = table->buckets[bucket];
  763. table->buckets[bucket] = it;
  764. ++table->bucket_size[bucket];
  765. ++table->refs;
  766. return it;
  767. }
  768. }
  769. #ifdef __STDC__
  770. RX_DECL void
  771. rx_hash_free(struct rx_hash_item *it, struct rx_hash_rules *rules)
  772. #else
  773. RX_DECL void rx_hash_free(it, rules)
  774. struct rx_hash_item *it;
  775. struct rx_hash_rules *rules;
  776. #endif
  777. {
  778. if (it) {
  779. struct rx_hash *table = it->table;
  780. unsigned long hash = it->hash;
  781. int depth = (table->parent
  782. ? (table->parent->parent
  783. ? (table->parent->parent->parent ? 3 : 2)
  784. : 1)
  785. : 0);
  786. int bucket = (hash & rx_hash_masks[depth]) % 13;
  787. struct rx_hash_item **pos = &table->buckets[bucket];
  788. while (*pos != it)
  789. pos = &(*pos)->next_same_hash;
  790. *pos = it->next_same_hash;
  791. rules->free_hash_item(it, rules);
  792. --table->bucket_size[bucket];
  793. --table->refs;
  794. while (!table->refs && depth) {
  795. struct rx_hash *save = table;
  796. table = table->parent;
  797. --depth;
  798. bucket = (hash & rx_hash_masks[depth]) % 13;
  799. --table->refs;
  800. table->children[bucket] = 0;
  801. rules->free_hash(save, rules);
  802. }
  803. }
  804. }
  805. #ifdef __STDC__
  806. RX_DECL void
  807. rx_free_hash_table(struct rx_hash *tab, rx_hash_freefn freefn,
  808. struct rx_hash_rules *rules)
  809. #else
  810. RX_DECL void rx_free_hash_table(tab, freefn, rules)
  811. struct rx_hash *tab;
  812. rx_hash_freefn freefn;
  813. struct rx_hash_rules *rules;
  814. #endif
  815. {
  816. int x;
  817. for (x = 0; x < 13; ++x)
  818. if (tab->children[x]) {
  819. rx_free_hash_table(tab->children[x], freefn, rules);
  820. rules->free_hash(tab->children[x], rules);
  821. } else {
  822. struct rx_hash_item *them = tab->buckets[x];
  823. while (them) {
  824. struct rx_hash_item *that = them;
  825. them = that->next_same_hash;
  826. freefn(that);
  827. rules->free_hash_item(that, rules);
  828. }
  829. }
  830. }
  831. /* Utilities for manipulating bitset represntations of characters sets. */
  832. #ifdef __STDC__
  833. RX_DECL rx_Bitset rx_cset(struct rx *rx)
  834. #else
  835. RX_DECL rx_Bitset rx_cset(rx)
  836. struct rx *rx;
  837. #endif
  838. {
  839. rx_Bitset b =
  840. (rx_Bitset) malloc(rx_sizeof_bitset(rx->local_cset_size));
  841. if (b)
  842. rx_bitset_null(rx->local_cset_size, b);
  843. return b;
  844. }
  845. #ifdef __STDC__
  846. RX_DECL rx_Bitset rx_copy_cset(struct rx * rx, rx_Bitset a)
  847. #else
  848. RX_DECL rx_Bitset rx_copy_cset(rx, a)
  849. struct rx *rx;
  850. rx_Bitset a;
  851. #endif
  852. {
  853. rx_Bitset cs = rx_cset(rx);
  854. if (cs)
  855. rx_bitset_union(rx->local_cset_size, cs, a);
  856. return cs;
  857. }
  858. #ifdef __STDC__
  859. RX_DECL void rx_free_cset(struct rx *rx, rx_Bitset c)
  860. #else
  861. RX_DECL void rx_free_cset(rx, c)
  862. struct rx *rx;
  863. rx_Bitset c;
  864. #endif
  865. {
  866. if (c)
  867. free((char *) c);
  868. }
  869. /* Hash table memory allocation policy for the regexp compiler */
  870. #ifdef __STDC__
  871. static struct rx_hash *compiler_hash_alloc(struct rx_hash_rules *rules)
  872. #else
  873. static struct rx_hash *compiler_hash_alloc(rules)
  874. struct rx_hash_rules *rules;
  875. #endif
  876. {
  877. return (struct rx_hash *) malloc(sizeof(struct rx_hash));
  878. }
  879. #ifdef __STDC__
  880. static struct rx_hash_item *compiler_hash_item_alloc(struct rx_hash_rules
  881. *rules, void *value)
  882. #else
  883. static struct rx_hash_item *compiler_hash_item_alloc(rules, value)
  884. struct rx_hash_rules *rules;
  885. void *value;
  886. #endif
  887. {
  888. struct rx_hash_item *it;
  889. it = (struct rx_hash_item *) malloc(sizeof(*it));
  890. if (it) {
  891. it->data = value;
  892. it->binding = 0;
  893. }
  894. return it;
  895. }
  896. #ifdef __STDC__
  897. static void
  898. compiler_free_hash(struct rx_hash *tab, struct rx_hash_rules *rules)
  899. #else
  900. static void compiler_free_hash(tab, rules)
  901. struct rx_hash *tab;
  902. struct rx_hash_rules *rules;
  903. #endif
  904. {
  905. free((char *) tab);
  906. }
  907. #ifdef __STDC__
  908. static void
  909. compiler_free_hash_item(struct rx_hash_item *item,
  910. struct rx_hash_rules *rules)
  911. #else
  912. static void compiler_free_hash_item(item, rules)
  913. struct rx_hash_item *item;
  914. struct rx_hash_rules *rules;
  915. #endif
  916. {
  917. free((char *) item);
  918. }
  919. /* This page: REXP_NODE (expression tree) structures. */
  920. #ifdef __STDC__
  921. RX_DECL struct rexp_node *rexp_node(struct rx *rx,
  922. enum rexp_node_type type)
  923. #else
  924. RX_DECL struct rexp_node *rexp_node(rx, type)
  925. struct rx *rx;
  926. enum rexp_node_type type;
  927. #endif
  928. {
  929. struct rexp_node *n;
  930. n = (struct rexp_node *) malloc(sizeof(*n));
  931. if (n) {
  932. bzero(n, sizeof(*n));
  933. n->type = type;
  934. }
  935. return n;
  936. }
  937. /* free_rexp_node assumes that the bitset passed to rx_mk_r_cset
  938. * can be freed using rx_free_cset.
  939. */
  940. #ifdef __STDC__
  941. RX_DECL struct rexp_node *rx_mk_r_cset(struct rx *rx, rx_Bitset b)
  942. #else
  943. RX_DECL struct rexp_node *rx_mk_r_cset(rx, b)
  944. struct rx *rx;
  945. rx_Bitset b;
  946. #endif
  947. {
  948. struct rexp_node *n = rexp_node(rx, r_cset);
  949. if (n)
  950. n->params.cset = b;
  951. return n;
  952. }
  953. #ifdef __STDC__
  954. RX_DECL struct rexp_node *rx_mk_r_concat(struct rx *rx,
  955. struct rexp_node *a,
  956. struct rexp_node *b)
  957. #else
  958. RX_DECL struct rexp_node *rx_mk_r_concat(rx, a, b)
  959. struct rx *rx;
  960. struct rexp_node *a;
  961. struct rexp_node *b;
  962. #endif
  963. {
  964. struct rexp_node *n = rexp_node(rx, r_concat);
  965. if (n) {
  966. n->params.pair.left = a;
  967. n->params.pair.right = b;
  968. }
  969. return n;
  970. }
  971. #ifdef __STDC__
  972. RX_DECL struct rexp_node *rx_mk_r_alternate(struct rx *rx,
  973. struct rexp_node *a,
  974. struct rexp_node *b)
  975. #else
  976. RX_DECL struct rexp_node *rx_mk_r_alternate(rx, a, b)
  977. struct rx *rx;
  978. struct rexp_node *a;
  979. struct rexp_node *b;
  980. #endif
  981. {
  982. struct rexp_node *n = rexp_node(rx, r_alternate);
  983. if (n) {
  984. n->params.pair.left = a;
  985. n->params.pair.right = b;
  986. }
  987. return n;
  988. }
  989. #ifdef __STDC__
  990. RX_DECL struct rexp_node *rx_mk_r_opt(struct rx *rx, struct rexp_node *a)
  991. #else
  992. RX_DECL struct rexp_node *rx_mk_r_opt(rx, a)
  993. struct rx *rx;
  994. struct rexp_node *a;
  995. #endif
  996. {
  997. struct rexp_node *n = rexp_node(rx, r_opt);
  998. if (n) {
  999. n->params.pair.left = a;
  1000. n->params.pair.right = 0;
  1001. }
  1002. return n;
  1003. }
  1004. #ifdef __STDC__
  1005. RX_DECL struct rexp_node *rx_mk_r_star(struct rx *rx, struct rexp_node *a)
  1006. #else
  1007. RX_DECL struct rexp_node *rx_mk_r_star(rx, a)
  1008. struct rx *rx;
  1009. struct rexp_node *a;
  1010. #endif
  1011. {
  1012. struct rexp_node *n = rexp_node(rx, r_star);
  1013. if (n) {
  1014. n->params.pair.left = a;
  1015. n->params.pair.right = 0;
  1016. }
  1017. return n;
  1018. }
  1019. #ifdef __STDC__
  1020. RX_DECL struct rexp_node *rx_mk_r_2phase_star(struct rx *rx,
  1021. struct rexp_node *a,
  1022. struct rexp_node *b)
  1023. #else
  1024. RX_DECL struct rexp_node *rx_mk_r_2phase_star(rx, a, b)
  1025. struct rx *rx;
  1026. struct rexp_node *a;
  1027. struct rexp_node *b;
  1028. #endif
  1029. {
  1030. struct rexp_node *n = rexp_node(rx, r_2phase_star);
  1031. if (n) {
  1032. n->params.pair.left = a;
  1033. n->params.pair.right = b;
  1034. }
  1035. return n;
  1036. }
  1037. #ifdef __STDC__
  1038. RX_DECL struct rexp_node *rx_mk_r_side_effect(struct rx *rx,
  1039. rx_side_effect a)
  1040. #else
  1041. RX_DECL struct rexp_node *rx_mk_r_side_effect(rx, a)
  1042. struct rx *rx;
  1043. rx_side_effect a;
  1044. #endif
  1045. {
  1046. struct rexp_node *n = rexp_node(rx, r_side_effect);
  1047. if (n) {
  1048. n->params.side_effect = a;
  1049. n->params.pair.right = 0;
  1050. }
  1051. return n;
  1052. }
  1053. #if 0
  1054. #ifdef __STDC__
  1055. RX_DECL struct rexp_node *rx_mk_r_data(struct rx *rx, void *a)
  1056. #else
  1057. RX_DECL struct rexp_node *rx_mk_r_data(rx, a)
  1058. struct rx *rx;
  1059. void *a;
  1060. #endif
  1061. {
  1062. struct rexp_node *n = rexp_node(rx, r_data);
  1063. if (n) {
  1064. n->params.pair.left = a;
  1065. n->params.pair.right = 0;
  1066. }
  1067. return n;
  1068. }
  1069. #endif
  1070. #ifdef __STDC__
  1071. RX_DECL void rx_free_rexp(struct rx *rx, struct rexp_node *node)
  1072. #else
  1073. RX_DECL void rx_free_rexp(rx, node)
  1074. struct rx *rx;
  1075. struct rexp_node *node;
  1076. #endif
  1077. {
  1078. if (node) {
  1079. switch (node->type) {
  1080. case r_cset:
  1081. if (node->params.cset)
  1082. rx_free_cset(rx, node->params.cset);
  1083. case r_side_effect:
  1084. break;
  1085. case r_concat:
  1086. case r_alternate:
  1087. case r_2phase_star:
  1088. case r_opt:
  1089. case r_star:
  1090. rx_free_rexp(rx, node->params.pair.left);
  1091. rx_free_rexp(rx, node->params.pair.right);
  1092. break;
  1093. case r_data:
  1094. /* This shouldn't occur. */
  1095. break;
  1096. }
  1097. free((char *) node);
  1098. }
  1099. }
  1100. #ifdef __STDC__
  1101. RX_DECL struct rexp_node *rx_copy_rexp(struct rx *rx,
  1102. struct rexp_node *node)
  1103. #else
  1104. RX_DECL struct rexp_node *rx_copy_rexp(rx, node)
  1105. struct rx *rx;
  1106. struct rexp_node *node;
  1107. #endif
  1108. {
  1109. if (!node)
  1110. return 0;
  1111. else {
  1112. struct rexp_node *n = rexp_node(rx, node->type);
  1113. if (!n)
  1114. return 0;
  1115. switch (node->type) {
  1116. case r_cset:
  1117. n->params.cset = rx_copy_cset(rx, node->params.cset);
  1118. if (!n->params.cset) {
  1119. rx_free_rexp(rx, n);
  1120. return 0;
  1121. }
  1122. break;
  1123. case r_side_effect:
  1124. n->params.side_effect = node->params.side_effect;
  1125. break;
  1126. case r_concat:
  1127. case r_alternate:
  1128. case r_opt:
  1129. case r_2phase_star:
  1130. case r_star:
  1131. n->params.pair.left = rx_copy_rexp(rx, node->params.pair.left);
  1132. n->params.pair.right =
  1133. rx_copy_rexp(rx, node->params.pair.right);
  1134. if ((node->params.pair.left && !n->params.pair.left)
  1135. || (node->params.pair.right && !n->params.pair.right)) {
  1136. rx_free_rexp(rx, n);
  1137. return 0;
  1138. }
  1139. break;
  1140. case r_data:
  1141. /* shouldn't happen */
  1142. break;
  1143. }
  1144. return n;
  1145. }
  1146. }
  1147. /* This page: functions to build and destroy graphs that describe nfa's */
  1148. /* Constructs a new nfa node. */
  1149. #ifdef __STDC__
  1150. RX_DECL struct rx_nfa_state *rx_nfa_state(struct rx *rx)
  1151. #else
  1152. RX_DECL struct rx_nfa_state *rx_nfa_state(rx)
  1153. struct rx *rx;
  1154. #endif
  1155. {
  1156. struct rx_nfa_state *n = (struct rx_nfa_state *) malloc(sizeof(*n));
  1157. if (!n)
  1158. return 0;
  1159. bzero(n, sizeof(*n));
  1160. n->next = rx->nfa_states;
  1161. rx->nfa_states = n;
  1162. return n;
  1163. }
  1164. #ifdef __STDC__
  1165. RX_DECL void rx_free_nfa_state(struct rx_nfa_state *n)
  1166. #else
  1167. RX_DECL void rx_free_nfa_state(n)
  1168. struct rx_nfa_state *n;
  1169. #endif
  1170. {
  1171. free((char *) n);
  1172. }
  1173. /* This looks up an nfa node, given a numeric id. Numeric id's are
  1174. * assigned after the nfa has been built.
  1175. */
  1176. #ifdef __STDC__
  1177. RX_DECL struct rx_nfa_state *rx_id_to_nfa_state(struct rx *rx, int id)
  1178. #else
  1179. RX_DECL struct rx_nfa_state *rx_id_to_nfa_state(rx, id)
  1180. struct rx *rx;
  1181. int id;
  1182. #endif
  1183. {
  1184. struct rx_nfa_state *n;
  1185. for (n = rx->nfa_states; n; n = n->next)
  1186. if (n->id == id)
  1187. return n;
  1188. return 0;
  1189. }
  1190. /* This adds an edge between two nodes, but doesn't initialize the
  1191. * edge label.
  1192. */
  1193. #ifdef __STDC__
  1194. RX_DECL struct rx_nfa_edge *rx_nfa_edge(struct rx *rx,
  1195. enum rx_nfa_etype type,
  1196. struct rx_nfa_state *start,
  1197. struct rx_nfa_state *dest)
  1198. #else
  1199. RX_DECL struct rx_nfa_edge *rx_nfa_edge(rx, type, start, dest)
  1200. struct rx *rx;
  1201. enum rx_nfa_etype type;
  1202. struct rx_nfa_state *start;
  1203. struct rx_nfa_state *dest;
  1204. #endif
  1205. {
  1206. struct rx_nfa_edge *e;
  1207. e = (struct rx_nfa_edge *) malloc(sizeof(*e));
  1208. if (!e)
  1209. return 0;
  1210. e->next = start->edges;
  1211. start->edges = e;
  1212. e->type = type;
  1213. e->dest = dest;
  1214. return e;
  1215. }
  1216. #ifdef __STDC__
  1217. RX_DECL void rx_free_nfa_edge(struct rx_nfa_edge *e)
  1218. #else
  1219. RX_DECL void rx_free_nfa_edge(e)
  1220. struct rx_nfa_edge *e;
  1221. #endif
  1222. {
  1223. free((char *) e);
  1224. }
  1225. /* This constructs a POSSIBLE_FUTURE, which is a kind epsilon-closure
  1226. * of an NFA. These are added to an nfa automaticly by eclose_nfa.
  1227. */
  1228. #ifdef __STDC__
  1229. static struct rx_possible_future *rx_possible_future(struct rx *rx, struct rx_se_list
  1230. *effects)
  1231. #else
  1232. static struct rx_possible_future *rx_possible_future(rx, effects)
  1233. struct rx *rx;
  1234. struct rx_se_list *effects;
  1235. #endif
  1236. {
  1237. struct rx_possible_future *ec;
  1238. ec = (struct rx_possible_future *) malloc(sizeof(*ec));
  1239. if (!ec)
  1240. return 0;
  1241. ec->destset = 0;
  1242. ec->next = 0;
  1243. ec->effects = effects;
  1244. return ec;
  1245. }
  1246. #ifdef __STDC__
  1247. static void rx_free_possible_future(struct rx_possible_future *pf)
  1248. #else
  1249. static void rx_free_possible_future(pf)
  1250. struct rx_possible_future *pf;
  1251. #endif
  1252. {
  1253. free((char *) pf);
  1254. }
  1255. #ifdef __STDC__
  1256. RX_DECL void rx_free_nfa(struct rx *rx)
  1257. #else
  1258. RX_DECL void rx_free_nfa(rx)
  1259. struct rx *rx;
  1260. #endif
  1261. {
  1262. while (rx->nfa_states) {
  1263. while (rx->nfa_states->edges) {
  1264. switch (rx->nfa_states->edges->type) {
  1265. case ne_cset:
  1266. rx_free_cset(rx, rx->nfa_states->edges->params.cset);
  1267. break;
  1268. default:
  1269. break;
  1270. }
  1271. {
  1272. struct rx_nfa_edge *e;
  1273. e = rx->nfa_states->edges;
  1274. rx->nfa_states->edges = rx->nfa_states->edges->next;
  1275. rx_free_nfa_edge(e);
  1276. }
  1277. } /* while (rx->nfa_states->edges) */
  1278. {
  1279. /* Iterate over the partial epsilon closures of rx->nfa_states */
  1280. struct rx_possible_future *pf = rx->nfa_states->futures;
  1281. while (pf) {
  1282. struct rx_possible_future *pft = pf;
  1283. pf = pf->next;
  1284. rx_free_possible_future(pft);
  1285. }
  1286. }
  1287. {
  1288. struct rx_nfa_state *n;
  1289. n = rx->nfa_states;
  1290. rx->nfa_states = rx->nfa_states->next;
  1291. rx_free_nfa_state(n);
  1292. }
  1293. }
  1294. }
  1295. /* This page: translating a pattern expression into an nfa and doing the
  1296. * static part of the nfa->super-nfa translation.
  1297. */
  1298. /* This is the thompson regexp->nfa algorithm.
  1299. * It is modified to allow for `side-effect epsilons.' Those are
  1300. * edges that are taken whenever a similar epsilon edge would be,
  1301. * but which imply that some side effect occurs when the edge
  1302. * is taken.
  1303. *
  1304. * Side effects are used to model parts of the pattern langauge
  1305. * that are not regular (in the formal sense).
  1306. */
  1307. #ifdef __STDC__
  1308. RX_DECL int
  1309. rx_build_nfa(struct rx *rx,
  1310. struct rexp_node *rexp,
  1311. struct rx_nfa_state **start, struct rx_nfa_state **end)
  1312. #else
  1313. RX_DECL int rx_build_nfa(rx, rexp, start, end)
  1314. struct rx *rx;
  1315. struct rexp_node *rexp;
  1316. struct rx_nfa_state **start;
  1317. struct rx_nfa_state **end;
  1318. #endif
  1319. {
  1320. struct rx_nfa_edge *edge;
  1321. /* Start & end nodes may have been allocated by the caller. */
  1322. *start = *start ? *start : rx_nfa_state(rx);
  1323. if (!*start)
  1324. return 0;
  1325. if (!rexp) {
  1326. *end = *start;
  1327. return 1;
  1328. }
  1329. *end = *end ? *end : rx_nfa_state(rx);
  1330. if (!*end) {
  1331. rx_free_nfa_state(*start);
  1332. return 0;
  1333. }
  1334. switch (rexp->type) {
  1335. case r_data:
  1336. return 0;
  1337. case r_cset:
  1338. edge = rx_nfa_edge(rx, ne_cset, *start, *end);
  1339. if (!edge)
  1340. return 0;
  1341. edge->params.cset = rx_copy_cset(rx, rexp->params.cset);
  1342. if (!edge->params.cset) {
  1343. rx_free_nfa_edge(edge);
  1344. return 0;
  1345. }
  1346. return 1;
  1347. case r_opt:
  1348. return (rx_build_nfa(rx, rexp->params.pair.left, start, end)
  1349. && rx_nfa_edge(rx, ne_epsilon, *start, *end));
  1350. case r_star:
  1351. {
  1352. struct rx_nfa_state *star_start = 0;
  1353. struct rx_nfa_state *star_end = 0;
  1354. return (rx_build_nfa(rx, rexp->params.pair.left,
  1355. &star_start, &star_end)
  1356. && star_start
  1357. && star_end
  1358. && rx_nfa_edge(rx, ne_epsilon, star_start, star_end)
  1359. && rx_nfa_edge(rx, ne_epsilon, *start, star_start)
  1360. && rx_nfa_edge(rx, ne_epsilon, star_end, *end)
  1361. && rx_nfa_edge(rx, ne_epsilon, star_end, star_start));
  1362. }
  1363. case r_2phase_star:
  1364. {
  1365. struct rx_nfa_state *star_start = 0;
  1366. struct rx_nfa_state *star_end = 0;
  1367. struct rx_nfa_state *loop_exp_start = 0;
  1368. struct rx_nfa_state *loop_exp_end = 0;
  1369. return (rx_build_nfa(rx, rexp->params.pair.left,
  1370. &star_start, &star_end)
  1371. && rx_build_nfa(rx, rexp->params.pair.right,
  1372. &loop_exp_start, &loop_exp_end)
  1373. && star_start
  1374. && star_end
  1375. && loop_exp_end
  1376. && loop_exp_start
  1377. && rx_nfa_edge(rx, ne_epsilon, star_start, *end)
  1378. && rx_nfa_edge(rx, ne_epsilon, *start, star_start)
  1379. && rx_nfa_edge(rx, ne_epsilon, star_end, *end)
  1380. && rx_nfa_edge(rx, ne_epsilon, star_end, loop_exp_start)
  1381. && rx_nfa_edge(rx, ne_epsilon, loop_exp_end, star_start));
  1382. }
  1383. case r_concat:
  1384. {
  1385. struct rx_nfa_state *shared = 0;
  1386. return (rx_build_nfa(rx, rexp->params.pair.left, start, &shared)
  1387. && rx_build_nfa(rx, rexp->params.pair.right, &shared,
  1388. end));
  1389. }
  1390. case r_alternate:
  1391. {
  1392. struct rx_nfa_state *ls = 0;
  1393. struct rx_nfa_state *le = 0;
  1394. struct rx_nfa_state *rs = 0;
  1395. struct rx_nfa_state *re = 0;
  1396. return (rx_build_nfa(rx, rexp->params.pair.left, &ls, &le)
  1397. && rx_build_nfa(rx, rexp->params.pair.right, &rs, &re)
  1398. && rx_nfa_edge(rx, ne_epsilon, *start, ls)
  1399. && rx_nfa_edge(rx, ne_epsilon, *start, rs)
  1400. && rx_nfa_edge(rx, ne_epsilon, le, *end)
  1401. && rx_nfa_edge(rx, ne_epsilon, re, *end));
  1402. }
  1403. case r_side_effect:
  1404. edge = rx_nfa_edge(rx, ne_side_effect, *start, *end);
  1405. if (!edge)
  1406. return 0;
  1407. edge->params.side_effect = rexp->params.side_effect;
  1408. return 1;
  1409. }
  1410. /* this should never happen */
  1411. return 0;
  1412. }
  1413. /* RX_NAME_NFA_STATES identifies all nodes with outgoing non-epsilon
  1414. * transitions. Only these nodes can occur in super-states.
  1415. * All nodes are given an integer id.
  1416. * The id is non-negative if the node has non-epsilon out-transitions, negative
  1417. * otherwise (this is because we want the non-negative ids to be used as
  1418. * array indexes in a few places).
  1419. */
  1420. #ifdef __STDC__
  1421. RX_DECL void rx_name_nfa_states(struct rx *rx)
  1422. #else
  1423. RX_DECL void rx_name_nfa_states(rx)
  1424. struct rx *rx;
  1425. #endif
  1426. {
  1427. struct rx_nfa_state *n = rx->nfa_states;
  1428. rx->nodec = 0;
  1429. rx->epsnodec = -1;
  1430. while (n) {
  1431. struct rx_nfa_edge *e = n->edges;
  1432. if (n->is_start)
  1433. n->eclosure_needed = 1;
  1434. while (e) {
  1435. switch (e->type) {
  1436. case ne_epsilon:
  1437. case ne_side_effect:
  1438. break;
  1439. case ne_cset:
  1440. n->id = rx->nodec++;
  1441. {
  1442. struct rx_nfa_edge *from_n = n->edges;
  1443. while (from_n) {
  1444. from_n->dest->eclosure_needed = 1;
  1445. from_n = from_n->next;
  1446. }
  1447. }
  1448. goto cont;
  1449. }
  1450. e = e->next;
  1451. }
  1452. n->id = rx->epsnodec--;
  1453. cont:
  1454. n = n->next;
  1455. }
  1456. rx->epsnodec = -rx->epsnodec;
  1457. }
  1458. /* This page: data structures for the static part of the nfa->supernfa
  1459. * translation.
  1460. *
  1461. * There are side effect lists -- lists of side effects occuring
  1462. * along an uninterrupted, acyclic path of side-effect epsilon edges.
  1463. * Such paths are collapsed to single edges in the course of computing
  1464. * epsilon closures. Such single edges are labled with a list of all
  1465. * the side effects entailed in crossing them. Like lists of side
  1466. * effects are made == by the constructors below.
  1467. *
  1468. * There are also nfa state sets. These are used to hold a list of all
  1469. * states reachable from a starting state for a given type of transition
  1470. * and side effect list. These are also hash-consed.
  1471. */
  1472. /* The next several functions compare, construct, etc. lists of side
  1473. * effects. See ECLOSE_NFA (below) for details.
  1474. */
  1475. /* Ordering of rx_se_list
  1476. * (-1, 0, 1 return value convention).
  1477. */
  1478. #ifdef __STDC__
  1479. static int se_list_cmp(void *va, void *vb)
  1480. #else
  1481. static int se_list_cmp(va, vb)
  1482. void *va;
  1483. void *vb;
  1484. #endif
  1485. {
  1486. struct rx_se_list *a = (struct rx_se_list *) va;
  1487. struct rx_se_list *b = (struct rx_se_list *) vb;
  1488. return ((va == vb)
  1489. ? 0
  1490. : (!va
  1491. ? -1
  1492. : (!vb
  1493. ? 1
  1494. : ((long) a->car < (long) b->car
  1495. ? 1
  1496. : ((long) a->car > (long) b->car
  1497. ? -1
  1498. : se_list_cmp((void *) a->cdr,
  1499. (void *) b->cdr))))));
  1500. }
  1501. #ifdef __STDC__
  1502. static int se_list_equal(void *va, void *vb)
  1503. #else
  1504. static int se_list_equal(va, vb)
  1505. void *va;
  1506. void *vb;
  1507. #endif
  1508. {
  1509. return !(se_list_cmp(va, vb));
  1510. }
  1511. static struct rx_hash_rules se_list_hash_rules = {
  1512. se_list_equal,
  1513. compiler_hash_alloc,
  1514. compiler_free_hash,
  1515. compiler_hash_item_alloc,
  1516. compiler_free_hash_item
  1517. };
  1518. #ifdef __STDC__
  1519. static struct rx_se_list *side_effect_cons(struct rx *rx,
  1520. void *se,
  1521. struct rx_se_list *list)
  1522. #else
  1523. static struct rx_se_list *side_effect_cons(rx, se, list)
  1524. struct rx *rx;
  1525. void *se;
  1526. struct rx_se_list *list;
  1527. #endif
  1528. {
  1529. struct rx_se_list *l;
  1530. l = ((struct rx_se_list *) malloc(sizeof(*l)));
  1531. if (!l)
  1532. return 0;
  1533. l->car = se;
  1534. l->cdr = list;
  1535. return l;
  1536. }
  1537. #ifdef __STDC__
  1538. static struct rx_se_list *hash_cons_se_prog(struct rx *rx,
  1539. struct rx_hash *memo,
  1540. void *car,
  1541. struct rx_se_list *cdr)
  1542. #else
  1543. static struct rx_se_list *hash_cons_se_prog(rx, memo, car, cdr)
  1544. struct rx *rx;
  1545. struct rx_hash *memo;
  1546. void *car;
  1547. struct rx_se_list *cdr;
  1548. #endif
  1549. {
  1550. long hash = (long) car ^ (long) cdr;
  1551. struct rx_se_list template;
  1552. template.car = car;
  1553. template.cdr = cdr;
  1554. {
  1555. struct rx_hash_item *it = rx_hash_store(memo, hash,
  1556. (void *) &template,
  1557. &se_list_hash_rules);
  1558. if (!it)
  1559. return 0;
  1560. if (it->data == (void *) &template) {
  1561. struct rx_se_list *consed;
  1562. consed = (struct rx_se_list *) malloc(sizeof(*consed));
  1563. if (!consed) {
  1564. free((char *) it);
  1565. return 0;
  1566. }
  1567. *consed = template;
  1568. it->data = (void *) consed;
  1569. }
  1570. return (struct rx_se_list *) it->data;
  1571. }
  1572. }
  1573. #ifdef __STDC__
  1574. static struct rx_se_list *hash_se_prog(struct rx *rx, struct rx_hash *memo,
  1575. struct rx_se_list *prog)
  1576. #else
  1577. static struct rx_se_list *hash_se_prog(rx, memo, prog)
  1578. struct rx *rx;
  1579. struct rx_hash *memo;
  1580. struct rx_se_list *prog;
  1581. #endif
  1582. {
  1583. struct rx_se_list *answer = 0;
  1584. while (prog) {
  1585. answer = hash_cons_se_prog(rx, memo, prog->car, answer);
  1586. if (!answer)
  1587. return 0;
  1588. prog = prog->cdr;
  1589. }
  1590. return answer;
  1591. }
  1592. #ifdef __STDC__
  1593. static int nfa_set_cmp(void *va, void *vb)
  1594. #else
  1595. static int nfa_set_cmp(va, vb)
  1596. void *va;
  1597. void *vb;
  1598. #endif
  1599. {
  1600. struct rx_nfa_state_set *a = (struct rx_nfa_state_set *) va;
  1601. struct rx_nfa_state_set *b = (struct rx_nfa_state_set *) vb;
  1602. return ((va == vb)
  1603. ? 0
  1604. : (!va
  1605. ? -1
  1606. : (!vb
  1607. ? 1
  1608. : (a->car->id < b->car->id
  1609. ? 1
  1610. : (a->car->id > b->car->id
  1611. ? -1
  1612. : nfa_set_cmp((void *) a->cdr,
  1613. (void *) b->cdr))))));
  1614. }
  1615. #ifdef __STDC__
  1616. static int nfa_set_equal(void *va, void *vb)
  1617. #else
  1618. static int nfa_set_equal(va, vb)
  1619. void *va;
  1620. void *vb;
  1621. #endif
  1622. {
  1623. return !nfa_set_cmp(va, vb);
  1624. }
  1625. static struct rx_hash_rules nfa_set_hash_rules = {
  1626. nfa_set_equal,
  1627. compiler_hash_alloc,
  1628. compiler_free_hash,
  1629. compiler_hash_item_alloc,
  1630. compiler_free_hash_item
  1631. };
  1632. #ifdef __STDC__
  1633. static struct rx_nfa_state_set *nfa_set_cons(struct rx *rx,
  1634. struct rx_hash *memo,
  1635. struct rx_nfa_state *state,
  1636. struct rx_nfa_state_set *set)
  1637. #else
  1638. static struct rx_nfa_state_set *nfa_set_cons(rx, memo, state, set)
  1639. struct rx *rx;
  1640. struct rx_hash *memo;
  1641. struct rx_nfa_state *state;
  1642. struct rx_nfa_state_set *set;
  1643. #endif
  1644. {
  1645. struct rx_nfa_state_set template;
  1646. struct rx_hash_item *node;
  1647. template.car = state;
  1648. template.cdr = set;
  1649. node = rx_hash_store(memo,
  1650. (((long) state) >> 8) ^ (long) set,
  1651. &template, &nfa_set_hash_rules);
  1652. if (!node)
  1653. return 0;
  1654. if (node->data == &template) {
  1655. struct rx_nfa_state_set *l;
  1656. l = (struct rx_nfa_state_set *) malloc(sizeof(*l));
  1657. node->data = (void *) l;
  1658. if (!l)
  1659. return 0;
  1660. *l = template;
  1661. }
  1662. return (struct rx_nfa_state_set *) node->data;
  1663. }
  1664. #ifdef __STDC__
  1665. static struct rx_nfa_state_set *nfa_set_enjoin(struct rx *rx,
  1666. struct rx_hash *memo,
  1667. struct rx_nfa_state *state,
  1668. struct rx_nfa_state_set
  1669. *set)
  1670. #else
  1671. static struct rx_nfa_state_set *nfa_set_enjoin(rx, memo, state, set)
  1672. struct rx *rx;
  1673. struct rx_hash *memo;
  1674. struct rx_nfa_state *state;
  1675. struct rx_nfa_state_set *set;
  1676. #endif
  1677. {
  1678. if (!set || state->id < set->car->id)
  1679. return nfa_set_cons(rx, memo, state, set);
  1680. if (state->id == set->car->id)
  1681. return set;
  1682. else {
  1683. struct rx_nfa_state_set *newcdr
  1684. = nfa_set_enjoin(rx, memo, state, set->cdr);
  1685. if (newcdr != set->cdr)
  1686. set = nfa_set_cons(rx, memo, set->car, newcdr);
  1687. return set;
  1688. }
  1689. }
  1690. /* This page: computing epsilon closures. The closures aren't total.
  1691. * Each node's closures are partitioned according to the side effects entailed
  1692. * along the epsilon edges. Return true on success.
  1693. */
  1694. struct eclose_frame {
  1695. struct rx_se_list *prog_backwards;
  1696. };
  1697. static int eclose_node(struct rx *, struct rx_nfa_state *,
  1698. struct rx_nfa_state *, struct eclose_frame *);
  1699. RX_DECL int rx_eclose_nfa(struct rx *);
  1700. RX_DECL void rx_delete_epsilon_transitions(struct rx *);
  1701. static int nfacmp(void *, void *);
  1702. static int count_hash_nodes(struct rx_hash *);
  1703. static void nfa_set_freer(struct rx_hash_item *);
  1704. RX_DECL int rx_compactify_nfa(struct rx *, void **, unsigned long *);
  1705. static char *rx_cache_malloc(struct rx_cache *, int);
  1706. static void rx_cache_free(struct rx_cache *,
  1707. struct rx_freelist **, char *);
  1708. static void install_transition(struct rx_superstate *,
  1709. struct rx_inx *, rx_Bitset);
  1710. static int qlen(struct rx_superstate *);
  1711. static void check_cache(struct rx_cache *);
  1712. static void semifree_superstate(struct rx_cache *);
  1713. static void refresh_semifree_superstate
  1714. (struct rx_cache *, struct rx_superstate *);
  1715. static void rx_refresh_this_superstate
  1716. (struct rx_cache *, struct rx_superstate *);
  1717. static void release_superset_low(struct rx_cache *, struct rx_superset *);
  1718. RX_DECL void rx_release_superset(struct rx *, struct rx_superset *);
  1719. static int rx_really_free_superstate(struct rx_cache *);
  1720. static char *rx_cache_get(struct rx_cache *, struct rx_freelist **);
  1721. static char *rx_cache_malloc_or_get(struct rx_cache *,
  1722. struct rx_freelist **, int);
  1723. static char *rx_cache_get_superstate(struct rx_cache *);
  1724. static int supersetcmp(void *, void *);
  1725. static struct rx_hash_item
  1726. *superset_allocator(struct rx_hash_rules *, void *);
  1727. static struct rx_hash
  1728. *super_hash_allocator(struct rx_hash_rules *);
  1729. static void super_hash_liberator(struct rx_hash *, struct rx_hash_rules *);
  1730. static void superset_hash_item_liberator
  1731. (struct rx_hash_item *, struct rx_hash_rules *);
  1732. static int bytes_for_cache_size(int, int);
  1733. static void rx_morecore(struct rx_cache *);
  1734. RX_DECL struct rx_superset
  1735. *rx_superset_cons(struct rx *, struct rx_nfa_state *,
  1736. struct rx_superset *);
  1737. RX_DECL struct rx_superset
  1738. *rx_superstate_eclosure_union
  1739. (struct rx *, struct rx_superset *, struct rx_nfa_state_set *);
  1740. static struct rx_distinct_future
  1741. *include_futures(struct rx *,
  1742. struct rx_distinct_future *,
  1743. struct rx_nfa_state *, struct rx_superstate *);
  1744. RX_DECL struct rx_superstate
  1745. *rx_superstate(struct rx *, struct rx_superset *);
  1746. static int solve_destination(struct rx *, struct rx_distinct_future *);
  1747. static int compute_super_edge(struct rx *,
  1748. struct rx_distinct_future **,
  1749. rx_Bitset, struct rx_superstate *,
  1750. unsigned char);
  1751. static struct rx_super_edge
  1752. *rx_super_edge(struct rx *, struct rx_superstate *,
  1753. rx_Bitset, struct rx_distinct_future *);
  1754. static void install_partial_transition
  1755. (struct rx_superstate *, struct rx_inx *, RX_subset, int);
  1756. RX_DECL struct rx_inx
  1757. *rx_handle_cache_miss(struct rx *, struct rx_superstate *,
  1758. unsigned char, void *);
  1759. static boolean
  1760. at_begline_loc_p(__const__ char *, __const__ char *, reg_syntax_t);
  1761. static boolean at_endline_loc_p(__const__ char *, __const__ char *, int);
  1762. static rx_Bitset
  1763. inverse_translation(struct re_pattern_buffer *, char *,
  1764. rx_Bitset, unsigned char *, int);
  1765. #ifdef __STDC__
  1766. static int
  1767. eclose_node(struct rx *rx, struct rx_nfa_state *outnode,
  1768. struct rx_nfa_state *node, struct eclose_frame *frame)
  1769. #else
  1770. static int eclose_node(rx, outnode, node, frame)
  1771. struct rx *rx;
  1772. struct rx_nfa_state *outnode;
  1773. struct rx_nfa_state *node;
  1774. struct eclose_frame *frame;
  1775. #endif
  1776. {
  1777. struct rx_nfa_edge *e = node->edges;
  1778. /* For each node, we follow all epsilon paths to build the closure.
  1779. * The closure omits nodes that have only epsilon edges.
  1780. * The closure is split into partial closures -- all the states in
  1781. * a partial closure are reached by crossing the same list of
  1782. * of side effects (though not necessarily the same path).
  1783. */
  1784. if (node->mark)
  1785. return 1;
  1786. node->mark = 1;
  1787. if (node->id >= 0 || node->is_final) {
  1788. struct rx_possible_future **ec;
  1789. struct rx_se_list *prog_in_order
  1790. = ((struct rx_se_list *) hash_se_prog(rx,
  1791. &rx->se_list_memo,
  1792. frame->prog_backwards));
  1793. int cmp;
  1794. ec = &outnode->futures;
  1795. while (*ec) {
  1796. cmp =
  1797. se_list_cmp((void *) (*ec)->effects,
  1798. (void *) prog_in_order);
  1799. if (cmp <= 0)
  1800. break;
  1801. ec = &(*ec)->next;
  1802. }
  1803. if (!*ec || (cmp < 0)) {
  1804. struct rx_possible_future *saved = *ec;
  1805. *ec = rx_possible_future(rx, prog_in_order);
  1806. (*ec)->next = saved;
  1807. if (!*ec)
  1808. return 0;
  1809. }
  1810. if (node->id >= 0) {
  1811. (*ec)->destset = nfa_set_enjoin(rx, &rx->set_list_memo,
  1812. node, (*ec)->destset);
  1813. if (!(*ec)->destset)
  1814. return 0;
  1815. }
  1816. }
  1817. while (e) {
  1818. switch (e->type) {
  1819. case ne_epsilon:
  1820. if (!eclose_node(rx, outnode, e->dest, frame))
  1821. return 0;
  1822. break;
  1823. case ne_side_effect:
  1824. {
  1825. frame->prog_backwards = side_effect_cons(rx,
  1826. e->params.side_effect,
  1827. frame->prog_backwards);
  1828. if (!frame->prog_backwards)
  1829. return 0;
  1830. if (!eclose_node(rx, outnode, e->dest, frame))
  1831. return 0;
  1832. {
  1833. struct rx_se_list *dying = frame->prog_backwards;
  1834. frame->prog_backwards = frame->prog_backwards->cdr;
  1835. free((char *) dying);
  1836. }
  1837. break;
  1838. }
  1839. default:
  1840. break;
  1841. }
  1842. e = e->next;
  1843. }
  1844. node->mark = 0;
  1845. return 1;
  1846. }
  1847. #ifdef __STDC__
  1848. RX_DECL int rx_eclose_nfa(struct rx *rx)
  1849. #else
  1850. RX_DECL int rx_eclose_nfa(rx)
  1851. struct rx *rx;
  1852. #endif
  1853. {
  1854. struct rx_nfa_state *n = rx->nfa_states;
  1855. struct eclose_frame frame;
  1856. static int rx_id = 0;
  1857. frame.prog_backwards = 0;
  1858. rx->rx_id = rx_id++;
  1859. bzero(&rx->se_list_memo, sizeof(rx->se_list_memo));
  1860. bzero(&rx->set_list_memo, sizeof(rx->set_list_memo));
  1861. while (n) {
  1862. n->futures = 0;
  1863. if (n->eclosure_needed && !eclose_node(rx, n, n, &frame))
  1864. return 0;
  1865. /* clear_marks (rx); */
  1866. n = n->next;
  1867. }
  1868. return 1;
  1869. }
  1870. /* This deletes epsilon edges from an NFA. After running eclose_node,
  1871. * we have no more need for these edges. They are removed to simplify
  1872. * further operations on the NFA.
  1873. */
  1874. #ifdef __STDC__
  1875. RX_DECL void rx_delete_epsilon_transitions(struct rx *rx)
  1876. #else
  1877. RX_DECL void rx_delete_epsilon_transitions(rx)
  1878. struct rx *rx;
  1879. #endif
  1880. {
  1881. struct rx_nfa_state *n = rx->nfa_states;
  1882. struct rx_nfa_edge **e;
  1883. while (n) {
  1884. e = &n->edges;
  1885. while (*e) {
  1886. struct rx_nfa_edge *t;
  1887. switch ((*e)->type) {
  1888. case ne_epsilon:
  1889. case ne_side_effect:
  1890. t = *e;
  1891. *e = t->next;
  1892. rx_free_nfa_edge(t);
  1893. break;
  1894. default:
  1895. e = &(*e)->next;
  1896. break;
  1897. }
  1898. }
  1899. n = n->next;
  1900. }
  1901. }
  1902. /* This page: storing the nfa in a contiguous region of memory for
  1903. * subsequent conversion to a super-nfa.
  1904. */
  1905. /* This is for qsort on an array of nfa_states. The order
  1906. * is based on state ids and goes
  1907. * [0...MAX][MIN..-1] where (MAX>=0) and (MIN<0)
  1908. * This way, positive ids double as array indices.
  1909. */
  1910. #ifdef __STDC__
  1911. static int nfacmp(void *va, void *vb)
  1912. #else
  1913. static int nfacmp(va, vb)
  1914. void *va;
  1915. void *vb;
  1916. #endif
  1917. {
  1918. struct rx_nfa_state **a = (struct rx_nfa_state **) va;
  1919. struct rx_nfa_state **b = (struct rx_nfa_state **) vb;
  1920. return (*a == *b /* &&&& 3.18 */
  1921. ? 0 : (((*a)->id < 0) == ((*b)->id < 0)
  1922. ? (((*a)->id < (*b)->id) ? -1 : 1)
  1923. : (((*a)->id < 0)
  1924. ? 1 : -1)));
  1925. }
  1926. #ifdef __STDC__
  1927. static int count_hash_nodes(struct rx_hash *st)
  1928. #else
  1929. static int count_hash_nodes(st)
  1930. struct rx_hash *st;
  1931. #endif
  1932. {
  1933. int x;
  1934. int count = 0;
  1935. for (x = 0; x < 13; ++x)
  1936. count += ((st->children[x])
  1937. ? count_hash_nodes(st->children[x])
  1938. : st->bucket_size[x]);
  1939. return count;
  1940. }
  1941. #ifdef __STDC__
  1942. static void se_memo_freer(struct rx_hash_item *node)
  1943. #else
  1944. static void se_memo_freer(node)
  1945. struct rx_hash_item *node;
  1946. #endif
  1947. {
  1948. free((char *) node->data);
  1949. }
  1950. #ifdef __STDC__
  1951. static void nfa_set_freer(struct rx_hash_item *node)
  1952. #else
  1953. static void nfa_set_freer(node)
  1954. struct rx_hash_item *node;
  1955. #endif
  1956. {
  1957. free((char *) node->data);
  1958. }
  1959. /* This copies an entire NFA into a single malloced block of memory.
  1960. * Mostly this is for compatability with regex.c, though it is convenient
  1961. * to have the nfa nodes in an array.
  1962. */
  1963. #ifdef __STDC__
  1964. RX_DECL int
  1965. rx_compactify_nfa(struct rx *rx, void **mem, unsigned long *size)
  1966. #else
  1967. RX_DECL int rx_compactify_nfa(rx, mem, size)
  1968. struct rx *rx;
  1969. void **mem;
  1970. unsigned long *size;
  1971. #endif
  1972. {
  1973. int total_nodec;
  1974. struct rx_nfa_state *n;
  1975. int edgec = 0;
  1976. int eclosec = 0;
  1977. int se_list_consc = count_hash_nodes(&rx->se_list_memo);
  1978. int nfa_setc = count_hash_nodes(&rx->set_list_memo);
  1979. unsigned long total_size;
  1980. /* This takes place in two stages. First, the total size of the
  1981. * nfa is computed, then structures are copied.
  1982. */
  1983. n = rx->nfa_states;
  1984. total_nodec = 0;
  1985. while (n) {
  1986. struct rx_nfa_edge *e = n->edges;
  1987. struct rx_possible_future *ec = n->futures;
  1988. ++total_nodec;
  1989. while (e) {
  1990. ++edgec;
  1991. e = e->next;
  1992. }
  1993. while (ec) {
  1994. ++eclosec;
  1995. ec = ec->next;
  1996. }
  1997. n = n->next;
  1998. }
  1999. total_size = (total_nodec * sizeof(struct rx_nfa_state)
  2000. + edgec * rx_sizeof_bitset(rx->local_cset_size)
  2001. + edgec * sizeof(struct rx_nfa_edge)
  2002. + nfa_setc * sizeof(struct rx_nfa_state_set)
  2003. + eclosec * sizeof(struct rx_possible_future)
  2004. + se_list_consc * sizeof(struct rx_se_list)
  2005. + rx->reserved);
  2006. if (total_size > *size) {
  2007. *mem = remalloc(*mem, total_size);
  2008. if (*mem)
  2009. *size = total_size;
  2010. else
  2011. return 0;
  2012. }
  2013. /* Now we've allocated the memory; this copies the NFA. */
  2014. {
  2015. static struct rx_nfa_state **scratch = 0;
  2016. static int scratch_alloc = 0;
  2017. struct rx_nfa_state *state_base = (struct rx_nfa_state *) *mem;
  2018. struct rx_nfa_state *new_state = state_base;
  2019. struct rx_nfa_edge *new_edge = (struct rx_nfa_edge *)
  2020. ((char *) state_base + total_nodec * sizeof(struct rx_nfa_state));
  2021. struct rx_se_list *new_se_list = (struct rx_se_list *)
  2022. ((char *) new_edge + edgec * sizeof(struct rx_nfa_edge));
  2023. struct rx_possible_future *new_close =
  2024. ((struct rx_possible_future *)
  2025. ((char *) new_se_list
  2026. + se_list_consc * sizeof(struct rx_se_list)));
  2027. struct rx_nfa_state_set *new_nfa_set = ((struct rx_nfa_state_set *)
  2028. ((char *) new_close +
  2029. eclosec *
  2030. sizeof(struct
  2031. rx_possible_future)));
  2032. char *new_bitset =
  2033. ((char *) new_nfa_set +
  2034. nfa_setc * sizeof(struct rx_nfa_state_set));
  2035. int x;
  2036. struct rx_nfa_state *n;
  2037. if (scratch_alloc < total_nodec) {
  2038. scratch = ((struct rx_nfa_state **)
  2039. remalloc(scratch, total_nodec * sizeof(*scratch)));
  2040. if (scratch)
  2041. scratch_alloc = total_nodec;
  2042. else {
  2043. scratch_alloc = 0;
  2044. return 0;
  2045. }
  2046. }
  2047. for (x = 0, n = rx->nfa_states; n; n = n->next)
  2048. scratch[x++] = n;
  2049. qsort(scratch, total_nodec, sizeof(struct rx_nfa_state *),
  2050. (__compar_fn_t) nfacmp);
  2051. for (x = 0; x < total_nodec; ++x) {
  2052. struct rx_possible_future *eclose = scratch[x]->futures;
  2053. struct rx_nfa_edge *edge = scratch[x]->edges;
  2054. struct rx_nfa_state *cn = new_state++;
  2055. cn->futures = 0;
  2056. cn->edges = 0;
  2057. cn->next = (x == total_nodec - 1) ? 0 : (cn + 1);
  2058. cn->id = scratch[x]->id;
  2059. cn->is_final = scratch[x]->is_final;
  2060. cn->is_start = scratch[x]->is_start;
  2061. cn->mark = 0;
  2062. while (edge) {
  2063. int indx = (edge->dest->id < 0
  2064. ? (total_nodec + edge->dest->id)
  2065. : edge->dest->id);
  2066. struct rx_nfa_edge *e = new_edge++;
  2067. rx_Bitset cset = (rx_Bitset) new_bitset;
  2068. new_bitset += rx_sizeof_bitset(rx->local_cset_size);
  2069. rx_bitset_null(rx->local_cset_size, cset);
  2070. rx_bitset_union(rx->local_cset_size, cset,
  2071. edge->params.cset);
  2072. e->next = cn->edges;
  2073. cn->edges = e;
  2074. e->type = edge->type;
  2075. e->dest = state_base + indx;
  2076. e->params.cset = cset;
  2077. edge = edge->next;
  2078. }
  2079. while (eclose) {
  2080. struct rx_possible_future *ec = new_close++;
  2081. struct rx_hash_item *sp;
  2082. struct rx_se_list **sepos;
  2083. struct rx_se_list *sesrc;
  2084. struct rx_nfa_state_set *destlst;
  2085. struct rx_nfa_state_set **destpos;
  2086. ec->next = cn->futures;
  2087. cn->futures = ec;
  2088. for (sepos = &ec->effects, sesrc = eclose->effects;
  2089. sesrc; sesrc = sesrc->cdr, sepos = &(*sepos)->cdr) {
  2090. sp = rx_hash_find(&rx->se_list_memo,
  2091. (long) sesrc->
  2092. car ^ (long) sesrc->cdr, sesrc,
  2093. &se_list_hash_rules);
  2094. if (sp->binding) {
  2095. sesrc = (struct rx_se_list *) sp->binding;
  2096. break;
  2097. }
  2098. *new_se_list = *sesrc;
  2099. sp->binding = (void *) new_se_list;
  2100. *sepos = new_se_list;
  2101. ++new_se_list;
  2102. }
  2103. *sepos = sesrc;
  2104. for (destpos = &ec->destset, destlst = eclose->destset;
  2105. destlst;
  2106. destpos = &(*destpos)->cdr, destlst = destlst->cdr) {
  2107. sp = rx_hash_find(&rx->set_list_memo,
  2108. ((((long) destlst->car) >> 8)
  2109. ^ (long) destlst->cdr),
  2110. destlst, &nfa_set_hash_rules);
  2111. if (sp->binding) {
  2112. destlst = (struct rx_nfa_state_set *) sp->binding;
  2113. break;
  2114. }
  2115. *new_nfa_set = *destlst;
  2116. new_nfa_set->car = state_base + destlst->car->id;
  2117. sp->binding = (void *) new_nfa_set;
  2118. *destpos = new_nfa_set;
  2119. ++new_nfa_set;
  2120. }
  2121. *destpos = destlst;
  2122. eclose = eclose->next;
  2123. }
  2124. }
  2125. }
  2126. rx_free_hash_table(&rx->se_list_memo, se_memo_freer,
  2127. &se_list_hash_rules);
  2128. bzero(&rx->se_list_memo, sizeof(rx->se_list_memo));
  2129. rx_free_hash_table(&rx->set_list_memo, nfa_set_freer,
  2130. &nfa_set_hash_rules);
  2131. bzero(&rx->set_list_memo, sizeof(rx->set_list_memo));
  2132. rx_free_nfa(rx);
  2133. rx->nfa_states = (struct rx_nfa_state *) *mem;
  2134. return 1;
  2135. }
  2136. /* The functions in the next several pages define the lazy-NFA-conversion used
  2137. * by matchers. The input to this construction is an NFA such as
  2138. * is built by compactify_nfa (rx.c). The output is the superNFA.
  2139. */
  2140. /* Match engines can use arbitrary values for opcodes. So, the parse tree
  2141. * is built using instructions names (enum rx_opcode), but the superstate
  2142. * nfa is populated with mystery opcodes (void *).
  2143. *
  2144. * For convenience, here is an id table. The opcodes are == to their inxs
  2145. *
  2146. * The lables in re_search_2 would make good values for instructions.
  2147. */
  2148. void *rx_id_instruction_table[rx_num_instructions] = {
  2149. (void *) rx_backtrack_point,
  2150. (void *) rx_do_side_effects,
  2151. (void *) rx_cache_miss,
  2152. (void *) rx_next_char,
  2153. (void *) rx_backtrack,
  2154. (void *) rx_error_inx
  2155. };
  2156. /* Memory mgt. for superstate graphs. */
  2157. #ifdef __STDC__
  2158. static char *rx_cache_malloc(struct rx_cache *cache, int bytes)
  2159. #else
  2160. static char *rx_cache_malloc(cache, bytes)
  2161. struct rx_cache *cache;
  2162. int bytes;
  2163. #endif
  2164. {
  2165. while (cache->bytes_left < bytes) {
  2166. if (cache->memory_pos)
  2167. cache->memory_pos = cache->memory_pos->next;
  2168. if (!cache->memory_pos) {
  2169. cache->morecore(cache);
  2170. if (!cache->memory_pos)
  2171. return 0;
  2172. }
  2173. cache->bytes_left = cache->memory_pos->bytes;
  2174. cache->memory_addr = ((char *) cache->memory_pos
  2175. + sizeof(struct rx_blocklist));
  2176. }
  2177. cache->bytes_left -= bytes;
  2178. {
  2179. char *addr = cache->memory_addr;
  2180. cache->memory_addr += bytes;
  2181. return addr;
  2182. }
  2183. }
  2184. #ifdef __STDC__
  2185. static void
  2186. rx_cache_free(struct rx_cache *cache,
  2187. struct rx_freelist **freelist, char *mem)
  2188. #else
  2189. static void rx_cache_free(cache, freelist, mem)
  2190. struct rx_cache *cache;
  2191. struct rx_freelist **freelist;
  2192. char *mem;
  2193. #endif
  2194. {
  2195. struct rx_freelist *it = (struct rx_freelist *) mem;
  2196. it->next = *freelist;
  2197. *freelist = it;
  2198. }
  2199. /* The partially instantiated superstate graph has a transition
  2200. * table at every node. There is one entry for every character.
  2201. * This fills in the transition for a set.
  2202. */
  2203. #ifdef __STDC__
  2204. static void
  2205. install_transition(struct rx_superstate *super,
  2206. struct rx_inx *answer, rx_Bitset trcset)
  2207. #else
  2208. static void install_transition(super, answer, trcset)
  2209. struct rx_superstate *super;
  2210. struct rx_inx *answer;
  2211. rx_Bitset trcset;
  2212. #endif
  2213. {
  2214. struct rx_inx *transitions = super->transitions;
  2215. int chr;
  2216. for (chr = 0; chr < 256;)
  2217. if (!*trcset) {
  2218. ++trcset;
  2219. chr += 32;
  2220. } else {
  2221. RX_subset sub = *trcset;
  2222. RX_subset mask = 1;
  2223. int bound = chr + 32;
  2224. while (chr < bound) {
  2225. if (sub & mask)
  2226. transitions[chr] = *answer;
  2227. ++chr;
  2228. mask <<= 1;
  2229. }
  2230. ++trcset;
  2231. }
  2232. }
  2233. #ifdef __STDC__
  2234. static int qlen(struct rx_superstate *q)
  2235. #else
  2236. static int qlen(q)
  2237. struct rx_superstate *q;
  2238. #endif
  2239. {
  2240. int count = 1;
  2241. struct rx_superstate *it;
  2242. if (!q)
  2243. return 0;
  2244. for (it = q->next_recyclable; it != q; it = it->next_recyclable)
  2245. ++count;
  2246. return count;
  2247. }
  2248. #ifdef __STDC__
  2249. static void check_cache(struct rx_cache *cache)
  2250. #else
  2251. static void check_cache(cache)
  2252. struct rx_cache *cache;
  2253. #endif
  2254. {
  2255. struct rx_cache *you_fucked_up = 0;
  2256. int total = cache->superstates;
  2257. int semi = cache->semifree_superstates;
  2258. if (semi != qlen(cache->semifree_superstate))
  2259. check_cache(you_fucked_up);
  2260. if ((total - semi) != qlen(cache->lru_superstate))
  2261. check_cache(you_fucked_up);
  2262. }
  2263. /* When a superstate is old and neglected, it can enter a
  2264. * semi-free state. A semi-free state is slated to die.
  2265. * Incoming transitions to a semi-free state are re-written
  2266. * to cause an (interpreted) fault when they are taken.
  2267. * The fault handler revives the semi-free state, patches
  2268. * incoming transitions back to normal, and continues.
  2269. *
  2270. * The idea is basicly to free in two stages, aborting
  2271. * between the two if the state turns out to be useful again.
  2272. * When a free is aborted, the rescued superstate is placed
  2273. * in the most-favored slot to maximize the time until it
  2274. * is next semi-freed.
  2275. */
  2276. #ifdef __STDC__
  2277. static void semifree_superstate(struct rx_cache *cache)
  2278. #else
  2279. static void semifree_superstate(cache)
  2280. struct rx_cache *cache;
  2281. #endif
  2282. {
  2283. int disqualified = cache->semifree_superstates;
  2284. if (disqualified == cache->superstates)
  2285. return;
  2286. while (cache->lru_superstate->locks) {
  2287. cache->lru_superstate = cache->lru_superstate->next_recyclable;
  2288. ++disqualified;
  2289. if (disqualified == cache->superstates)
  2290. return;
  2291. }
  2292. {
  2293. struct rx_superstate *it = cache->lru_superstate;
  2294. it->next_recyclable->prev_recyclable = it->prev_recyclable;
  2295. it->prev_recyclable->next_recyclable = it->next_recyclable;
  2296. cache->lru_superstate = (it == it->next_recyclable
  2297. ? 0 : it->next_recyclable);
  2298. if (!cache->semifree_superstate) {
  2299. cache->semifree_superstate = it;
  2300. it->next_recyclable = it;
  2301. it->prev_recyclable = it;
  2302. } else {
  2303. it->prev_recyclable =
  2304. cache->semifree_superstate->prev_recyclable;
  2305. it->next_recyclable = cache->semifree_superstate;
  2306. it->prev_recyclable->next_recyclable = it;
  2307. it->next_recyclable->prev_recyclable = it;
  2308. }
  2309. {
  2310. struct rx_distinct_future *df;
  2311. it->is_semifree = 1;
  2312. ++cache->semifree_superstates;
  2313. df = it->transition_refs;
  2314. if (df) {
  2315. df->prev_same_dest->next_same_dest = 0;
  2316. for (df = it->transition_refs; df; df = df->next_same_dest) {
  2317. df->future_frame.inx =
  2318. cache->instruction_table[rx_cache_miss];
  2319. df->future_frame.data = 0;
  2320. df->future_frame.data_2 = (void *) df;
  2321. /* If there are any NEXT-CHAR instruction frames that
  2322. * refer to this state, we convert them to CACHE-MISS frames.
  2323. */
  2324. if (!df->effects
  2325. && (df->edge->options->next_same_super_edge[0]
  2326. == df->edge->options))
  2327. install_transition(df->present, &df->future_frame,
  2328. df->edge->cset);
  2329. }
  2330. df = it->transition_refs;
  2331. df->prev_same_dest->next_same_dest = df;
  2332. }
  2333. }
  2334. }
  2335. }
  2336. #ifdef __STDC__
  2337. static void
  2338. refresh_semifree_superstate(struct rx_cache *cache,
  2339. struct rx_superstate *super)
  2340. #else
  2341. static void refresh_semifree_superstate(cache, super)
  2342. struct rx_cache *cache;
  2343. struct rx_superstate *super;
  2344. #endif
  2345. {
  2346. struct rx_distinct_future *df;
  2347. if (super->transition_refs) {
  2348. super->transition_refs->prev_same_dest->next_same_dest = 0;
  2349. for (df = super->transition_refs; df; df = df->next_same_dest) {
  2350. df->future_frame.inx = cache->instruction_table[rx_next_char];
  2351. df->future_frame.data = (void *) super->transitions;
  2352. /* CACHE-MISS instruction frames that refer to this state,
  2353. * must be converted to NEXT-CHAR frames.
  2354. */
  2355. if (!df->effects && (df->edge->options->next_same_super_edge[0]
  2356. == df->edge->options))
  2357. install_transition(df->present, &df->future_frame,
  2358. df->edge->cset);
  2359. }
  2360. super->transition_refs->prev_same_dest->next_same_dest
  2361. = super->transition_refs;
  2362. }
  2363. if (cache->semifree_superstate == super)
  2364. cache->semifree_superstate = (super->prev_recyclable == super
  2365. ? 0 : super->prev_recyclable);
  2366. super->next_recyclable->prev_recyclable = super->prev_recyclable;
  2367. super->prev_recyclable->next_recyclable = super->next_recyclable;
  2368. if (!cache->lru_superstate)
  2369. (cache->lru_superstate
  2370. = super->next_recyclable = super->prev_recyclable = super);
  2371. else {
  2372. super->next_recyclable = cache->lru_superstate;
  2373. super->prev_recyclable = cache->lru_superstate->prev_recyclable;
  2374. super->next_recyclable->prev_recyclable = super;
  2375. super->prev_recyclable->next_recyclable = super;
  2376. }
  2377. super->is_semifree = 0;
  2378. --cache->semifree_superstates;
  2379. }
  2380. #ifdef __STDC__
  2381. static void
  2382. rx_refresh_this_superstate(struct rx_cache *cache,
  2383. struct rx_superstate *superstate)
  2384. #else
  2385. static void rx_refresh_this_superstate(cache, superstate)
  2386. struct rx_cache *cache;
  2387. struct rx_superstate *superstate;
  2388. #endif
  2389. {
  2390. if (superstate->is_semifree)
  2391. refresh_semifree_superstate(cache, superstate);
  2392. else if (cache->lru_superstate == superstate)
  2393. cache->lru_superstate = superstate->next_recyclable;
  2394. else if (superstate != cache->lru_superstate->prev_recyclable) {
  2395. superstate->next_recyclable->prev_recyclable
  2396. = superstate->prev_recyclable;
  2397. superstate->prev_recyclable->next_recyclable
  2398. = superstate->next_recyclable;
  2399. superstate->next_recyclable = cache->lru_superstate;
  2400. superstate->prev_recyclable =
  2401. cache->lru_superstate->prev_recyclable;
  2402. superstate->next_recyclable->prev_recyclable = superstate;
  2403. superstate->prev_recyclable->next_recyclable = superstate;
  2404. }
  2405. }
  2406. #ifdef __STDC__
  2407. static void
  2408. release_superset_low(struct rx_cache *cache, struct rx_superset *set)
  2409. #else
  2410. static void release_superset_low(cache, set)
  2411. struct rx_cache *cache;
  2412. struct rx_superset *set;
  2413. #endif
  2414. {
  2415. if (!--set->refs) {
  2416. if (set->cdr)
  2417. release_superset_low(cache, set->cdr);
  2418. set->starts_for = 0;
  2419. rx_hash_free
  2420. (rx_hash_find
  2421. (&cache->superset_table,
  2422. (unsigned long) set->car ^ set->
  2423. id ^ (unsigned long) set->cdr, (void *) set,
  2424. &cache->superset_hash_rules), &cache->superset_hash_rules);
  2425. rx_cache_free(cache, &cache->free_supersets, (char *) set);
  2426. }
  2427. }
  2428. #ifdef __STDC__
  2429. RX_DECL void rx_release_superset(struct rx *rx, struct rx_superset *set)
  2430. #else
  2431. RX_DECL void rx_release_superset(rx, set)
  2432. struct rx *rx;
  2433. struct rx_superset *set;
  2434. #endif
  2435. {
  2436. release_superset_low(rx->cache, set);
  2437. }
  2438. /* This tries to add a new superstate to the superstate freelist.
  2439. * It might, as a result, free some edge pieces or hash tables.
  2440. * If nothing can be freed because too many locks are being held, fail.
  2441. */
  2442. #ifdef __STDC__
  2443. static int rx_really_free_superstate(struct rx_cache *cache)
  2444. #else
  2445. static int rx_really_free_superstate(cache)
  2446. struct rx_cache *cache;
  2447. #endif
  2448. {
  2449. int locked_superstates = 0;
  2450. struct rx_superstate *it;
  2451. if (!cache->superstates)
  2452. return 0;
  2453. {
  2454. /* This is a total guess. The idea is that we should expect as
  2455. * many misses as we've recently experienced. I.e., cache->misses
  2456. * should be the same as cache->semifree_superstates.
  2457. */
  2458. while ((cache->hits + cache->misses) > cache->superstates_allowed) {
  2459. cache->hits >>= 1;
  2460. cache->misses >>= 1;
  2461. }
  2462. if (((cache->hits + cache->misses) * cache->semifree_superstates)
  2463. < (cache->superstates * cache->misses)) {
  2464. semifree_superstate(cache);
  2465. semifree_superstate(cache);
  2466. }
  2467. }
  2468. while (cache->semifree_superstate && cache->semifree_superstate->locks) {
  2469. refresh_semifree_superstate(cache, cache->semifree_superstate);
  2470. ++locked_superstates;
  2471. if (locked_superstates == cache->superstates)
  2472. return 0;
  2473. }
  2474. if (cache->semifree_superstate) {
  2475. it = cache->semifree_superstate;
  2476. it->next_recyclable->prev_recyclable = it->prev_recyclable;
  2477. it->prev_recyclable->next_recyclable = it->next_recyclable;
  2478. cache->semifree_superstate = ((it == it->next_recyclable)
  2479. ? 0 : it->next_recyclable);
  2480. --cache->semifree_superstates;
  2481. } else {
  2482. while (cache->lru_superstate->locks) {
  2483. cache->lru_superstate = cache->lru_superstate->next_recyclable;
  2484. ++locked_superstates;
  2485. if (locked_superstates == cache->superstates)
  2486. return 0;
  2487. }
  2488. it = cache->lru_superstate;
  2489. it->next_recyclable->prev_recyclable = it->prev_recyclable;
  2490. it->prev_recyclable->next_recyclable = it->next_recyclable;
  2491. cache->lru_superstate = ((it == it->next_recyclable)
  2492. ? 0 : it->next_recyclable);
  2493. }
  2494. if (it->transition_refs) {
  2495. struct rx_distinct_future *df;
  2496. for (df = it->transition_refs,
  2497. df->prev_same_dest->next_same_dest = 0;
  2498. df; df = df->next_same_dest) {
  2499. df->future_frame.inx = cache->instruction_table[rx_cache_miss];
  2500. df->future_frame.data = 0;
  2501. df->future_frame.data_2 = (void *) df;
  2502. df->future = 0;
  2503. }
  2504. it->transition_refs->prev_same_dest->next_same_dest =
  2505. it->transition_refs;
  2506. }
  2507. {
  2508. struct rx_super_edge *tc = it->edges;
  2509. while (tc) {
  2510. struct rx_distinct_future *df;
  2511. struct rx_super_edge *tct = tc->next;
  2512. df = tc->options;
  2513. df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  2514. while (df) {
  2515. struct rx_distinct_future *dft = df;
  2516. df = df->next_same_super_edge[0];
  2517. if (dft->future && dft->future->transition_refs == dft) {
  2518. dft->future->transition_refs = dft->next_same_dest;
  2519. if (dft->future->transition_refs == dft)
  2520. dft->future->transition_refs = 0;
  2521. }
  2522. dft->next_same_dest->prev_same_dest = dft->prev_same_dest;
  2523. dft->prev_same_dest->next_same_dest = dft->next_same_dest;
  2524. rx_cache_free(cache, &cache->free_discernable_futures,
  2525. (char *) dft);
  2526. }
  2527. rx_cache_free(cache, &cache->free_transition_classes,
  2528. (char *) tc);
  2529. tc = tct;
  2530. }
  2531. }
  2532. if (it->contents->superstate == it)
  2533. it->contents->superstate = 0;
  2534. release_superset_low(cache, it->contents);
  2535. rx_cache_free(cache, &cache->free_superstates, (char *) it);
  2536. --cache->superstates;
  2537. return 1;
  2538. }
  2539. #ifdef __STDC__
  2540. static char *rx_cache_get(struct rx_cache *cache,
  2541. struct rx_freelist **freelist)
  2542. #else
  2543. static char *rx_cache_get(cache, freelist)
  2544. struct rx_cache *cache;
  2545. struct rx_freelist **freelist;
  2546. #endif
  2547. {
  2548. while (!*freelist && rx_really_free_superstate(cache));
  2549. if (!*freelist)
  2550. return 0;
  2551. {
  2552. struct rx_freelist *it = *freelist;
  2553. *freelist = it->next;
  2554. return (char *) it;
  2555. }
  2556. }
  2557. #ifdef __STDC__
  2558. static char *rx_cache_malloc_or_get(struct rx_cache *cache,
  2559. struct rx_freelist **freelist,
  2560. int bytes)
  2561. #else
  2562. static char *rx_cache_malloc_or_get(cache, freelist, bytes)
  2563. struct rx_cache *cache;
  2564. struct rx_freelist **freelist;
  2565. int bytes;
  2566. #endif
  2567. {
  2568. if (!*freelist) {
  2569. char *answer = rx_cache_malloc(cache, bytes);
  2570. if (answer)
  2571. return answer;
  2572. }
  2573. return rx_cache_get(cache, freelist);
  2574. }
  2575. #ifdef __STDC__
  2576. static char *rx_cache_get_superstate(struct rx_cache *cache)
  2577. #else
  2578. static char *rx_cache_get_superstate(cache)
  2579. struct rx_cache *cache;
  2580. #endif
  2581. {
  2582. char *answer;
  2583. int bytes = (sizeof(struct rx_superstate)
  2584. + cache->local_cset_size * sizeof(struct rx_inx));
  2585. if (!cache->free_superstates
  2586. && (cache->superstates < cache->superstates_allowed)) {
  2587. answer = rx_cache_malloc(cache, bytes);
  2588. if (answer) {
  2589. ++cache->superstates;
  2590. return answer;
  2591. }
  2592. }
  2593. answer = rx_cache_get(cache, &cache->free_superstates);
  2594. if (!answer) {
  2595. answer = rx_cache_malloc(cache, bytes);
  2596. if (answer)
  2597. ++cache->superstates_allowed;
  2598. }
  2599. ++cache->superstates;
  2600. return answer;
  2601. }
  2602. #ifdef __STDC__
  2603. static int supersetcmp(void *va, void *vb)
  2604. #else
  2605. static int supersetcmp(va, vb)
  2606. void *va;
  2607. void *vb;
  2608. #endif
  2609. {
  2610. struct rx_superset *a = (struct rx_superset *) va;
  2611. struct rx_superset *b = (struct rx_superset *) vb;
  2612. return ((a == b)
  2613. || (a && b && (a->car == b->car) && (a->cdr == b->cdr)));
  2614. }
  2615. #ifdef __STDC__
  2616. static struct rx_hash_item *superset_allocator(struct rx_hash_rules *rules,
  2617. void *val)
  2618. #else
  2619. static struct rx_hash_item *superset_allocator(rules, val)
  2620. struct rx_hash_rules *rules;
  2621. void *val;
  2622. #endif
  2623. {
  2624. struct rx_cache *cache = ((struct rx_cache *)
  2625. ((char *) rules
  2626. -
  2627. (unsigned
  2628. long) (&((struct rx_cache *)
  2629. 0)->superset_hash_rules)));
  2630. struct rx_superset *template = (struct rx_superset *) val;
  2631. struct rx_superset *newset
  2632. = ((struct rx_superset *) rx_cache_malloc_or_get(cache,
  2633. &cache->free_supersets,
  2634. sizeof
  2635. (*template)));
  2636. if (!newset)
  2637. return 0;
  2638. newset->refs = 0;
  2639. newset->car = template->car;
  2640. newset->id = template->car->id;
  2641. newset->cdr = template->cdr;
  2642. newset->superstate = 0;
  2643. rx_protect_superset(rx, template->cdr);
  2644. newset->hash_item.data = (void *) newset;
  2645. newset->hash_item.binding = 0;
  2646. return &newset->hash_item;
  2647. }
  2648. #ifdef __STDC__
  2649. static struct rx_hash *super_hash_allocator(struct rx_hash_rules *rules)
  2650. #else
  2651. static struct rx_hash *super_hash_allocator(rules)
  2652. struct rx_hash_rules *rules;
  2653. #endif
  2654. {
  2655. struct rx_cache *cache = ((struct rx_cache *)
  2656. ((char *) rules
  2657. -
  2658. (unsigned
  2659. long) (&((struct rx_cache *)
  2660. 0)->superset_hash_rules)));
  2661. return ((struct rx_hash *)
  2662. rx_cache_malloc_or_get(cache, &cache->free_hash,
  2663. sizeof(struct rx_hash)));
  2664. }
  2665. #ifdef __STDC__
  2666. static void
  2667. super_hash_liberator(struct rx_hash *hash, struct rx_hash_rules *rules)
  2668. #else
  2669. static void super_hash_liberator(hash, rules)
  2670. struct rx_hash *hash;
  2671. struct rx_hash_rules *rules;
  2672. #endif
  2673. {
  2674. struct rx_cache *cache = ((struct rx_cache *)
  2675. (char *) rules -
  2676. (long) (&
  2677. ((struct rx_cache *)
  2678. 0)->superset_hash_rules));
  2679. rx_cache_free(cache, &cache->free_hash, (char *) hash);
  2680. }
  2681. #ifdef __STDC__
  2682. static void
  2683. superset_hash_item_liberator(struct rx_hash_item *it,
  2684. struct rx_hash_rules *rules)
  2685. #else
  2686. static void superset_hash_item_liberator(it, rules) /* Well, it does ya know. */
  2687. struct rx_hash_item *it;
  2688. struct rx_hash_rules *rules;
  2689. #endif
  2690. {
  2691. }
  2692. int rx_cache_bound = 128;
  2693. static int rx_default_cache_got = 0;
  2694. #ifdef __STDC__
  2695. static int bytes_for_cache_size(int supers, int cset_size)
  2696. #else
  2697. static int bytes_for_cache_size(supers, cset_size)
  2698. int supers;
  2699. int cset_size;
  2700. #endif
  2701. {
  2702. /* What the hell is this? !!! */
  2703. return (int)
  2704. ((float) supers * ((1.03 * (float) (rx_sizeof_bitset(cset_size)
  2705. +
  2706. sizeof(struct rx_super_edge)))
  2707. +
  2708. (1.80 *
  2709. (float) sizeof(struct rx_possible_future)) +
  2710. (float) (sizeof(struct rx_superstate)
  2711. + cset_size * sizeof(struct rx_inx))));
  2712. }
  2713. #ifdef __STDC__
  2714. static void rx_morecore(struct rx_cache *cache)
  2715. #else
  2716. static void rx_morecore(cache)
  2717. struct rx_cache *cache;
  2718. #endif
  2719. {
  2720. if (rx_default_cache_got >= rx_cache_bound)
  2721. return;
  2722. rx_default_cache_got += 16;
  2723. cache->superstates_allowed = rx_cache_bound;
  2724. {
  2725. struct rx_blocklist **pos = &cache->memory;
  2726. int size = bytes_for_cache_size(16, cache->local_cset_size);
  2727. while (*pos)
  2728. pos = &(*pos)->next;
  2729. *pos = ((struct rx_blocklist *)
  2730. malloc(size + sizeof(struct rx_blocklist)));
  2731. if (!*pos)
  2732. return;
  2733. (*pos)->next = 0;
  2734. (*pos)->bytes = size;
  2735. cache->memory_pos = *pos;
  2736. cache->memory_addr = (char *) *pos + sizeof(**pos);
  2737. cache->bytes_left = size;
  2738. }
  2739. }
  2740. static struct rx_cache default_cache = {
  2741. {
  2742. supersetcmp,
  2743. super_hash_allocator,
  2744. super_hash_liberator,
  2745. superset_allocator,
  2746. superset_hash_item_liberator,
  2747. },
  2748. 0,
  2749. 0,
  2750. 0,
  2751. 0,
  2752. rx_morecore,
  2753. 0,
  2754. 0,
  2755. 0,
  2756. 0,
  2757. 0,
  2758. 0,
  2759. 0,
  2760. 0,
  2761. 0,
  2762. 0,
  2763. 0,
  2764. 0,
  2765. 128,
  2766. 256,
  2767. rx_id_instruction_table,
  2768. {
  2769. 0,
  2770. 0,
  2771. {0},
  2772. {0},
  2773. {0}
  2774. }
  2775. };
  2776. /* This adds an element to a superstate set. These sets are lists, such
  2777. * that lists with == elements are ==. The empty set is returned by
  2778. * superset_cons (rx, 0, 0) and is NOT equivelent to
  2779. * (struct rx_superset)0.
  2780. */
  2781. #ifdef __STDC__
  2782. RX_DECL struct rx_superset *rx_superset_cons(struct rx *rx,
  2783. struct rx_nfa_state *car,
  2784. struct rx_superset *cdr)
  2785. #else
  2786. RX_DECL struct rx_superset *rx_superset_cons(rx, car, cdr)
  2787. struct rx *rx;
  2788. struct rx_nfa_state *car;
  2789. struct rx_superset *cdr;
  2790. #endif
  2791. {
  2792. struct rx_cache *cache = rx->cache;
  2793. if (!car && !cdr) {
  2794. if (!cache->empty_superset) {
  2795. cache->empty_superset = ((struct rx_superset *)
  2796. rx_cache_malloc_or_get(cache,
  2797. &cache->free_supersets,
  2798. sizeof(struct
  2799. rx_superset)));
  2800. if (!cache->empty_superset)
  2801. return 0;
  2802. bzero(cache->empty_superset, sizeof(struct rx_superset));
  2803. cache->empty_superset->refs = 1000;
  2804. }
  2805. return cache->empty_superset;
  2806. }
  2807. {
  2808. struct rx_superset template;
  2809. struct rx_hash_item *hit;
  2810. template.car = car;
  2811. template.cdr = cdr;
  2812. template.id = car->id;
  2813. /* While hash_store will protect cdr itself it might first allocate hash
  2814. tables and stuff which might cause it to be garbage collected before
  2815. it's protected -- [gsstark:19961026.2155EST] */
  2816. rx_protect_superset(rx, cdr);
  2817. hit = rx_hash_store(&cache->superset_table,
  2818. (unsigned long) car ^ car->id ^ (unsigned long)
  2819. cdr, (void *) &template,
  2820. &cache->superset_hash_rules);
  2821. rx_release_superset(rx, cdr);
  2822. return (hit ? (struct rx_superset *) hit->data : 0);
  2823. }
  2824. }
  2825. /* This computes a union of two NFA state sets. The sets do not have the
  2826. * same representation though. One is a RX_SUPERSET structure (part
  2827. * of the superstate NFA) and the other is an NFA_STATE_SET (part of the NFA).
  2828. */
  2829. #ifdef __STDC__
  2830. RX_DECL struct rx_superset *rx_superstate_eclosure_union
  2831. (struct rx *rx, struct rx_superset *set, struct rx_nfa_state_set *ecl)
  2832. #else
  2833. RX_DECL struct rx_superset *rx_superstate_eclosure_union(rx, set, ecl)
  2834. struct rx *rx;
  2835. struct rx_superset *set;
  2836. struct rx_nfa_state_set *ecl;
  2837. #endif
  2838. {
  2839. if (!ecl)
  2840. return set;
  2841. if (!set->car)
  2842. return rx_superset_cons(rx, ecl->car,
  2843. rx_superstate_eclosure_union(rx, set,
  2844. ecl->cdr));
  2845. if (set->car == ecl->car)
  2846. return rx_superstate_eclosure_union(rx, set, ecl->cdr);
  2847. {
  2848. struct rx_superset *tail;
  2849. struct rx_nfa_state *first;
  2850. if (set->car > ecl->car) {
  2851. tail = rx_superstate_eclosure_union(rx, set->cdr, ecl);
  2852. first = set->car;
  2853. } else {
  2854. tail = rx_superstate_eclosure_union(rx, set, ecl->cdr);
  2855. first = ecl->car;
  2856. }
  2857. if (!tail)
  2858. return 0;
  2859. else {
  2860. struct rx_superset *answer;
  2861. answer = rx_superset_cons(rx, first, tail);
  2862. if (!answer) {
  2863. rx_protect_superset(rx, tail);
  2864. rx_release_superset(rx, tail);
  2865. return 0;
  2866. } else
  2867. return answer;
  2868. }
  2869. }
  2870. }
  2871. /*
  2872. * This makes sure that a list of rx_distinct_futures contains
  2873. * a future for each possible set of side effects in the eclosure
  2874. * of a given state. This is some of the work of filling in a
  2875. * superstate transition.
  2876. */
  2877. #ifdef __STDC__
  2878. static struct rx_distinct_future *include_futures(struct rx *rx, struct rx_distinct_future
  2879. *df, struct rx_nfa_state
  2880. *state, struct rx_superstate
  2881. *superstate)
  2882. #else
  2883. static struct rx_distinct_future *include_futures(rx, df, state,
  2884. superstate)
  2885. struct rx *rx;
  2886. struct rx_distinct_future *df;
  2887. struct rx_nfa_state *state;
  2888. struct rx_superstate *superstate;
  2889. #endif
  2890. {
  2891. struct rx_possible_future *future;
  2892. struct rx_cache *cache = rx->cache;
  2893. for (future = state->futures; future; future = future->next) {
  2894. struct rx_distinct_future *dfp;
  2895. struct rx_distinct_future *insert_before = 0;
  2896. if (df)
  2897. df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  2898. for (dfp = df; dfp; dfp = dfp->next_same_super_edge[0])
  2899. if (dfp->effects == future->effects)
  2900. break;
  2901. else {
  2902. int order =
  2903. rx->se_list_cmp(rx, dfp->effects, future->effects);
  2904. if (order > 0) {
  2905. insert_before = dfp;
  2906. dfp = 0;
  2907. break;
  2908. }
  2909. }
  2910. if (df)
  2911. df->next_same_super_edge[1]->next_same_super_edge[0] = df;
  2912. if (!dfp) {
  2913. dfp = ((struct rx_distinct_future *)
  2914. rx_cache_malloc_or_get(cache,
  2915. &cache->free_discernable_futures,
  2916. sizeof(struct
  2917. rx_distinct_future)));
  2918. if (!dfp)
  2919. return 0;
  2920. if (!df) {
  2921. df = insert_before = dfp;
  2922. df->next_same_super_edge[0] = df->next_same_super_edge[1] =
  2923. df;
  2924. } else if (!insert_before)
  2925. insert_before = df;
  2926. else if (insert_before == df)
  2927. df = dfp;
  2928. dfp->next_same_super_edge[0] = insert_before;
  2929. dfp->next_same_super_edge[1]
  2930. = insert_before->next_same_super_edge[1];
  2931. dfp->next_same_super_edge[1]->next_same_super_edge[0] = dfp;
  2932. dfp->next_same_super_edge[0]->next_same_super_edge[1] = dfp;
  2933. dfp->next_same_dest = dfp->prev_same_dest = dfp;
  2934. dfp->future = 0;
  2935. dfp->present = superstate;
  2936. dfp->future_frame.inx = rx->instruction_table[rx_cache_miss];
  2937. dfp->future_frame.data = 0;
  2938. dfp->future_frame.data_2 = (void *) dfp;
  2939. dfp->side_effects_frame.inx
  2940. = rx->instruction_table[rx_do_side_effects];
  2941. dfp->side_effects_frame.data = 0;
  2942. dfp->side_effects_frame.data_2 = (void *) dfp;
  2943. dfp->effects = future->effects;
  2944. }
  2945. }
  2946. return df;
  2947. }
  2948. /* This constructs a new superstate from its state set. The only
  2949. * complexity here is memory management.
  2950. */
  2951. #ifdef __STDC__
  2952. RX_DECL struct rx_superstate *rx_superstate(struct rx *rx,
  2953. struct rx_superset *set)
  2954. #else
  2955. RX_DECL struct rx_superstate *rx_superstate(rx, set)
  2956. struct rx *rx;
  2957. struct rx_superset *set;
  2958. #endif
  2959. {
  2960. struct rx_cache *cache = rx->cache;
  2961. struct rx_superstate *superstate = 0;
  2962. /* Does the superstate already exist in the cache? */
  2963. if (set->superstate) {
  2964. if (set->superstate->rx_id != rx->rx_id) {
  2965. /* Aha. It is in the cache, but belongs to a superstate
  2966. * that refers to an NFA that no longer exists.
  2967. * (We know it no longer exists because it was evidently
  2968. * stored in the same region of memory as the current nfa
  2969. * yet it has a different id.)
  2970. */
  2971. superstate = set->superstate;
  2972. if (!superstate->is_semifree) {
  2973. if (cache->lru_superstate == superstate) {
  2974. cache->lru_superstate = superstate->next_recyclable;
  2975. if (cache->lru_superstate == superstate)
  2976. cache->lru_superstate = 0;
  2977. }
  2978. {
  2979. superstate->next_recyclable->prev_recyclable
  2980. = superstate->prev_recyclable;
  2981. superstate->prev_recyclable->next_recyclable
  2982. = superstate->next_recyclable;
  2983. if (!cache->semifree_superstate) {
  2984. (cache->semifree_superstate
  2985. = superstate->next_recyclable
  2986. = superstate->prev_recyclable = superstate);
  2987. } else {
  2988. superstate->next_recyclable =
  2989. cache->semifree_superstate;
  2990. superstate->prev_recyclable =
  2991. cache->semifree_superstate->prev_recyclable;
  2992. superstate->next_recyclable->prev_recyclable =
  2993. superstate;
  2994. superstate->prev_recyclable->next_recyclable =
  2995. superstate;
  2996. cache->semifree_superstate = superstate;
  2997. }
  2998. ++cache->semifree_superstates;
  2999. }
  3000. }
  3001. set->superstate = 0;
  3002. goto handle_cache_miss;
  3003. }
  3004. ++cache->hits;
  3005. superstate = set->superstate;
  3006. rx_refresh_this_superstate(cache, superstate);
  3007. return superstate;
  3008. }
  3009. handle_cache_miss:
  3010. /* This point reached only for cache misses. */
  3011. ++cache->misses;
  3012. #if RX_DEBUG
  3013. if (rx_debug_trace > 1) {
  3014. struct rx_superset *setp = set;
  3015. fprintf(stderr, "Building a superstet %d(%d): ", rx->rx_id, set);
  3016. while (setp) {
  3017. fprintf(stderr, "%d ", setp->id);
  3018. setp = setp->cdr;
  3019. }
  3020. fprintf(stderr, "(%d)\n", set);
  3021. }
  3022. #endif
  3023. superstate = (struct rx_superstate *) rx_cache_get_superstate(cache);
  3024. if (!superstate)
  3025. return 0;
  3026. if (!cache->lru_superstate)
  3027. (cache->lru_superstate
  3028. = superstate->next_recyclable
  3029. = superstate->prev_recyclable = superstate);
  3030. else {
  3031. superstate->next_recyclable = cache->lru_superstate;
  3032. superstate->prev_recyclable =
  3033. cache->lru_superstate->prev_recyclable;
  3034. (superstate->prev_recyclable->next_recyclable =
  3035. superstate->next_recyclable->prev_recyclable = superstate);
  3036. }
  3037. superstate->rx_id = rx->rx_id;
  3038. superstate->transition_refs = 0;
  3039. superstate->locks = 0;
  3040. superstate->is_semifree = 0;
  3041. set->superstate = superstate;
  3042. superstate->contents = set;
  3043. rx_protect_superset(rx, set);
  3044. superstate->edges = 0;
  3045. {
  3046. int x;
  3047. /* None of the transitions from this superstate are known yet. */
  3048. for (x = 0; x < rx->local_cset_size; ++x) { /* &&&&& 3.8 % */
  3049. struct rx_inx *ifr = &superstate->transitions[x];
  3050. ifr->inx = rx->instruction_table[rx_cache_miss];
  3051. ifr->data = ifr->data_2 = 0;
  3052. }
  3053. }
  3054. return superstate;
  3055. }
  3056. /* This computes the destination set of one edge of the superstate NFA.
  3057. * Note that a RX_DISTINCT_FUTURE is a superstate edge.
  3058. * Returns 0 on an allocation failure.
  3059. */
  3060. #ifdef __STDC__
  3061. static int solve_destination(struct rx *rx, struct rx_distinct_future *df)
  3062. #else
  3063. static int solve_destination(rx, df)
  3064. struct rx *rx;
  3065. struct rx_distinct_future *df;
  3066. #endif
  3067. {
  3068. struct rx_super_edge *tc = df->edge;
  3069. struct rx_superset *nfa_state;
  3070. struct rx_superset *nil_set = rx_superset_cons(rx, 0, 0);
  3071. struct rx_superset *solution = nil_set;
  3072. struct rx_superstate *dest;
  3073. rx_protect_superset(rx, solution);
  3074. /* Iterate over all NFA states in the state set of this superstate. */
  3075. for (nfa_state = df->present->contents;
  3076. nfa_state->car; nfa_state = nfa_state->cdr) {
  3077. struct rx_nfa_edge *e;
  3078. /* Iterate over all edges of each NFA state. */
  3079. for (e = nfa_state->car->edges; e; e = e->next)
  3080. /* If we find an edge that is labeled with
  3081. * the characters we are solving for.....
  3082. */
  3083. if (rx_bitset_is_subset(rx->local_cset_size,
  3084. tc->cset, e->params.cset)) {
  3085. struct rx_nfa_state *n = e->dest;
  3086. struct rx_possible_future *pf;
  3087. /* ....search the partial epsilon closures of the destination
  3088. * of that edge for a path that involves the same set of
  3089. * side effects we are solving for.
  3090. * If we find such a RX_POSSIBLE_FUTURE, we add members to the
  3091. * stateset we are computing.
  3092. */
  3093. for (pf = n->futures; pf; pf = pf->next)
  3094. if (pf->effects == df->effects) {
  3095. struct rx_superset *old_sol;
  3096. old_sol = solution;
  3097. solution =
  3098. rx_superstate_eclosure_union(rx, solution,
  3099. pf->destset);
  3100. if (!solution)
  3101. return 0;
  3102. rx_protect_superset(rx, solution);
  3103. rx_release_superset(rx, old_sol);
  3104. }
  3105. }
  3106. }
  3107. /* It is possible that the RX_DISTINCT_FUTURE we are working on has
  3108. * the empty set of NFA states as its definition. In that case, this
  3109. * is a failure point.
  3110. */
  3111. if (solution == nil_set) {
  3112. df->future_frame.inx = (void *) rx_backtrack;
  3113. df->future_frame.data = 0;
  3114. df->future_frame.data_2 = 0;
  3115. return 1;
  3116. }
  3117. dest = rx_superstate(rx, solution);
  3118. rx_release_superset(rx, solution);
  3119. if (!dest)
  3120. return 0;
  3121. {
  3122. struct rx_distinct_future *dft;
  3123. dft = df;
  3124. df->prev_same_dest->next_same_dest = 0;
  3125. while (dft) {
  3126. dft->future = dest;
  3127. dft->future_frame.inx = rx->instruction_table[rx_next_char];
  3128. dft->future_frame.data = (void *) dest->transitions;
  3129. dft = dft->next_same_dest;
  3130. }
  3131. df->prev_same_dest->next_same_dest = df;
  3132. }
  3133. if (!dest->transition_refs)
  3134. dest->transition_refs = df;
  3135. else {
  3136. struct rx_distinct_future *dft =
  3137. dest->transition_refs->next_same_dest;
  3138. dest->transition_refs->next_same_dest = df->next_same_dest;
  3139. df->next_same_dest->prev_same_dest = dest->transition_refs;
  3140. df->next_same_dest = dft;
  3141. dft->prev_same_dest = df;
  3142. }
  3143. return 1;
  3144. }
  3145. /* This takes a superstate and a character, and computes some edges
  3146. * from the superstate NFA. In particular, this computes all edges
  3147. * that lead from SUPERSTATE given CHR. This function also
  3148. * computes the set of characters that share this edge set.
  3149. * This returns 0 on allocation error.
  3150. * The character set and list of edges are returned through
  3151. * the paramters CSETOUT and DFOUT.
  3152. } */
  3153. #ifdef __STDC__
  3154. static int
  3155. compute_super_edge(struct rx *rx, struct rx_distinct_future **dfout,
  3156. rx_Bitset csetout, struct rx_superstate *superstate,
  3157. unsigned char chr)
  3158. #else
  3159. static int compute_super_edge(rx, dfout, csetout, superstate, chr)
  3160. struct rx *rx;
  3161. struct rx_distinct_future **dfout;
  3162. rx_Bitset csetout;
  3163. struct rx_superstate *superstate;
  3164. unsigned char chr;
  3165. #endif
  3166. {
  3167. struct rx_superset *stateset = superstate->contents;
  3168. /* To compute the set of characters that share edges with CHR,
  3169. * we start with the full character set, and subtract.
  3170. */
  3171. rx_bitset_universe(rx->local_cset_size, csetout);
  3172. *dfout = 0;
  3173. /* Iterate over the NFA states in the superstate state-set. */
  3174. while (stateset->car) {
  3175. struct rx_nfa_edge *e;
  3176. for (e = stateset->car->edges; e; e = e->next)
  3177. if (RX_bitset_member(e->params.cset, chr)) {
  3178. /* If we find an NFA edge that applies, we make sure there
  3179. * are corresponding edges in the superstate NFA.
  3180. */
  3181. {
  3182. struct rx_distinct_future *saved;
  3183. saved = *dfout;
  3184. *dfout =
  3185. include_futures(rx, *dfout, e->dest, superstate);
  3186. if (!*dfout) {
  3187. struct rx_distinct_future *df;
  3188. df = saved;
  3189. if (df)
  3190. df->
  3191. next_same_super_edge
  3192. [1]->next_same_super_edge[0] = 0;
  3193. while (df) {
  3194. struct rx_distinct_future *dft;
  3195. dft = df;
  3196. df = df->next_same_super_edge[0];
  3197. if (dft->future
  3198. && dft->future->transition_refs == dft) {
  3199. dft->future->transition_refs =
  3200. dft->next_same_dest;
  3201. if (dft->future->transition_refs == dft)
  3202. dft->future->transition_refs = 0;
  3203. }
  3204. dft->next_same_dest->prev_same_dest =
  3205. dft->prev_same_dest;
  3206. dft->prev_same_dest->next_same_dest =
  3207. dft->next_same_dest;
  3208. rx_cache_free(rx->cache,
  3209. &rx->
  3210. cache->free_discernable_futures,
  3211. (char *) dft);
  3212. }
  3213. return 0;
  3214. }
  3215. }
  3216. /* We also trim the character set a bit. */
  3217. rx_bitset_intersection(rx->local_cset_size,
  3218. csetout, e->params.cset);
  3219. } else
  3220. /* An edge that doesn't apply at least tells us some characters
  3221. * that don't share the same edge set as CHR.
  3222. */
  3223. rx_bitset_difference(rx->local_cset_size, csetout,
  3224. e->params.cset);
  3225. stateset = stateset->cdr;
  3226. }
  3227. return 1;
  3228. }
  3229. /* This is a constructor for RX_SUPER_EDGE structures. These are
  3230. * wrappers for lists of superstate NFA edges that share character sets labels.
  3231. * If a transition class contains more than one rx_distinct_future (superstate
  3232. * edge), then it represents a non-determinism in the superstate NFA.
  3233. */
  3234. #ifdef __STDC__
  3235. static struct rx_super_edge *rx_super_edge(struct rx *rx,
  3236. struct rx_superstate *super,
  3237. rx_Bitset cset,
  3238. struct rx_distinct_future *df)
  3239. #else
  3240. static struct rx_super_edge *rx_super_edge(rx, super, cset, df)
  3241. struct rx *rx;
  3242. struct rx_superstate *super;
  3243. rx_Bitset cset;
  3244. struct rx_distinct_future *df;
  3245. #endif
  3246. {
  3247. struct rx_super_edge *tc =
  3248. (struct rx_super_edge *) rx_cache_malloc_or_get
  3249. (rx->cache, &rx->cache->free_transition_classes,
  3250. sizeof(struct rx_super_edge) +
  3251. rx_sizeof_bitset(rx->local_cset_size));
  3252. if (!tc)
  3253. return 0;
  3254. tc->next = super->edges;
  3255. super->edges = tc;
  3256. tc->rx_backtrack_frame.inx = rx->instruction_table[rx_backtrack_point];
  3257. tc->rx_backtrack_frame.data = 0;
  3258. tc->rx_backtrack_frame.data_2 = (void *) tc;
  3259. tc->options = df;
  3260. tc->cset = (rx_Bitset) ((char *) tc + sizeof(*tc));
  3261. rx_bitset_assign(rx->local_cset_size, tc->cset, cset);
  3262. if (df) {
  3263. struct rx_distinct_future *dfp = df;
  3264. df->next_same_super_edge[1]->next_same_super_edge[0] = 0;
  3265. while (dfp) {
  3266. dfp->edge = tc;
  3267. dfp = dfp->next_same_super_edge[0];
  3268. }
  3269. df->next_same_super_edge[1]->next_same_super_edge[0] = df;
  3270. }
  3271. return tc;
  3272. }
  3273. /* There are three kinds of cache miss. The first occurs when a
  3274. * transition is taken that has never been computed during the
  3275. * lifetime of the source superstate. That cache miss is handled by
  3276. * calling COMPUTE_SUPER_EDGE. The second kind of cache miss
  3277. * occurs when the destination superstate of a transition doesn't
  3278. * exist. SOLVE_DESTINATION is used to construct the destination superstate.
  3279. * Finally, the third kind of cache miss occurs when the destination
  3280. * superstate of a transition is in a `semi-free state'. That case is
  3281. * handled by UNFREE_SUPERSTATE.
  3282. *
  3283. * The function of HANDLE_CACHE_MISS is to figure out which of these
  3284. * cases applies.
  3285. */
  3286. #ifdef __STDC__
  3287. static void
  3288. install_partial_transition(struct rx_superstate *super,
  3289. struct rx_inx *answer,
  3290. RX_subset set, int offset)
  3291. #else
  3292. static void install_partial_transition(super, answer, set, offset)
  3293. struct rx_superstate *super;
  3294. struct rx_inx *answer;
  3295. RX_subset set;
  3296. int offset;
  3297. #endif
  3298. {
  3299. int start = offset;
  3300. int end = start + 32;
  3301. RX_subset pos = 1;
  3302. struct rx_inx *transitions = super->transitions;
  3303. while (start < end) {
  3304. if (set & pos)
  3305. transitions[start] = *answer;
  3306. pos <<= 1;
  3307. ++start;
  3308. }
  3309. }
  3310. #ifdef __STDC__
  3311. RX_DECL struct rx_inx *rx_handle_cache_miss
  3312. (struct rx *rx, struct rx_superstate *super, unsigned char chr,
  3313. void *data)
  3314. #else
  3315. RX_DECL struct rx_inx *rx_handle_cache_miss(rx, super, chr, data)
  3316. struct rx *rx;
  3317. struct rx_superstate *super;
  3318. unsigned char chr;
  3319. void *data;
  3320. #endif
  3321. {
  3322. int offset = chr / RX_subset_bits;
  3323. struct rx_distinct_future *df = data;
  3324. if (!df) { /* must be the shared_cache_miss_frame */
  3325. /* Perhaps this is just a transition waiting to be filled. */
  3326. struct rx_super_edge *tc;
  3327. RX_subset mask = rx_subset_singletons[chr % RX_subset_bits];
  3328. for (tc = super->edges; tc; tc = tc->next)
  3329. if (tc->cset[offset] & mask) {
  3330. struct rx_inx *answer;
  3331. df = tc->options;
  3332. answer =
  3333. ((tc->options->next_same_super_edge[0] !=
  3334. tc->options) ? &tc->
  3335. rx_backtrack_frame : (df->effects ?
  3336. &df->side_effects_frame :
  3337. &df->future_frame));
  3338. install_partial_transition(super, answer, tc->cset[offset],
  3339. offset * 32);
  3340. return answer;
  3341. }
  3342. /* Otherwise, it's a flushed or newly encountered edge. */
  3343. {
  3344. char cset_space[1024]; /* this limit is far from unreasonable */
  3345. rx_Bitset trcset;
  3346. struct rx_inx *answer;
  3347. if (rx_sizeof_bitset(rx->local_cset_size) > sizeof(cset_space))
  3348. return 0; /* If the arbitrary limit is hit, always fail */
  3349. /* cleanly. */
  3350. trcset = (rx_Bitset) cset_space;
  3351. rx_lock_superstate(rx, super);
  3352. if (!compute_super_edge(rx, &df, trcset, super, chr)) {
  3353. rx_unlock_superstate(rx, super);
  3354. return 0;
  3355. }
  3356. if (!df) { /* We just computed the fail transition. */
  3357. static struct rx_inx
  3358. shared_fail_frame = { 0, 0, (void *) rx_backtrack, 0 };
  3359. answer = &shared_fail_frame;
  3360. } else {
  3361. tc = rx_super_edge(rx, super, trcset, df);
  3362. if (!tc) {
  3363. rx_unlock_superstate(rx, super);
  3364. return 0;
  3365. }
  3366. answer =
  3367. ((tc->options->next_same_super_edge[0] !=
  3368. tc->options) ? &tc->
  3369. rx_backtrack_frame : (df->effects ?
  3370. &df->side_effects_frame :
  3371. &df->future_frame));
  3372. }
  3373. install_partial_transition(super, answer,
  3374. trcset[offset], offset * 32);
  3375. rx_unlock_superstate(rx, super);
  3376. return answer;
  3377. }
  3378. } else if (df->future) { /* A cache miss on an edge with a future? Must be
  3379. * a semi-free destination. */
  3380. if (df->future->is_semifree)
  3381. refresh_semifree_superstate(rx->cache, df->future);
  3382. return &df->future_frame;
  3383. } else
  3384. /* no future superstate on an existing edge */
  3385. {
  3386. rx_lock_superstate(rx, super);
  3387. if (!solve_destination(rx, df)) {
  3388. rx_unlock_superstate(rx, super);
  3389. return 0;
  3390. }
  3391. if (!df->effects
  3392. && (df->edge->options->next_same_super_edge[0] ==
  3393. df->edge->options)) install_partial_transition(super,
  3394. &df->future_frame,
  3395. df->
  3396. edge->cset
  3397. [offset],
  3398. offset *
  3399. 32);
  3400. rx_unlock_superstate(rx, super);
  3401. return &df->future_frame;
  3402. }
  3403. }
  3404. /* The rest of the code provides a regex.c compatable interface. */
  3405. __const__ char *re_error_msg[] = {
  3406. 0, /* REG_NOUT */
  3407. "No match", /* REG_NOMATCH */
  3408. "Invalid regular expression", /* REG_BADPAT */
  3409. "Invalid collation character", /* REG_ECOLLATE */
  3410. "Invalid character class name", /* REG_ECTYPE */
  3411. "Trailing backslash", /* REG_EESCAPE */
  3412. "Invalid back reference", /* REG_ESUBREG */
  3413. "Unmatched [ or [^", /* REG_EBRACK */
  3414. "Unmatched ( or \\(", /* REG_EPAREN */
  3415. "Unmatched \\{", /* REG_EBRACE */
  3416. "Invalid content of \\{\\}", /* REG_BADBR */
  3417. "Invalid range end", /* REG_ERANGE */
  3418. "Memory exhausted", /* REG_ESPACE */
  3419. "Invalid preceding regular expression", /* REG_BADRPT */
  3420. "Premature end of regular expression", /* REG_EEND */
  3421. "Regular expression too big", /* REG_ESIZE */
  3422. "Unmatched ) or \\)", /* REG_ERPAREN */
  3423. };
  3424. /*
  3425. * Macros used while compiling patterns.
  3426. *
  3427. * By convention, PEND points just past the end of the uncompiled pattern,
  3428. * P points to the read position in the pattern. `translate' is the name
  3429. * of the translation table (`TRANSLATE' is the name of a macro that looks
  3430. * things up in `translate').
  3431. */
  3432. /*
  3433. * Fetch the next character in the uncompiled pattern---translating it
  3434. * if necessary. *Also cast from a signed character in the constant
  3435. * string passed to us by the user to an unsigned char that we can use
  3436. * as an array index (in, e.g., `translate').
  3437. */
  3438. #define PATFETCH(c) \
  3439. do {if (p == pend) return REG_EEND; \
  3440. c = (unsigned char) *p++; \
  3441. c = translate[c]; \
  3442. } while (0)
  3443. /*
  3444. * Fetch the next character in the uncompiled pattern, with no
  3445. * translation.
  3446. */
  3447. #define PATFETCH_RAW(c) \
  3448. do {if (p == pend) return REG_EEND; \
  3449. c = (unsigned char) *p++; \
  3450. } while (0)
  3451. /* Go backwards one character in the pattern. */
  3452. #define PATUNFETCH p--
  3453. #define TRANSLATE(d) translate[(unsigned char) (d)]
  3454. typedef unsigned regnum_t;
  3455. /* Since offsets can go either forwards or backwards, this type needs to
  3456. * be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.
  3457. */
  3458. typedef int pattern_offset_t;
  3459. typedef struct {
  3460. struct rexp_node **top_expression; /* was begalt */
  3461. struct rexp_node **last_expression; /* was laststart */
  3462. pattern_offset_t inner_group_offset;
  3463. regnum_t regnum;
  3464. } compile_stack_elt_t;
  3465. typedef struct {
  3466. compile_stack_elt_t *stack;
  3467. unsigned size;
  3468. unsigned avail; /* Offset of next open position. */
  3469. } compile_stack_type;
  3470. static boolean group_in_compile_stack(compile_stack_type, regnum_t);
  3471. static reg_errcode_t
  3472. compile_range(struct re_pattern_buffer *, rx_Bitset,
  3473. __const__ char **, __const__ char *,
  3474. unsigned char *, reg_syntax_t, rx_Bitset, char *);
  3475. static void find_backrefs(char *, struct rexp_node *,
  3476. struct re_se_params *);
  3477. static int compute_fastset(struct re_pattern_buffer *, struct rexp_node *);
  3478. static int is_anchored(struct rexp_node *, rx_side_effect);
  3479. static struct rexp_node
  3480. *remove_unecessary_side_effects
  3481. (struct rx *, char *, struct rexp_node *, struct re_se_params *);
  3482. static int pointless_if_repeated(struct rexp_node *,
  3483. struct re_se_params *);
  3484. static int registers_on_stack(struct re_pattern_buffer *,
  3485. struct rexp_node *,
  3486. int, struct re_se_params *);
  3487. static int has_any_se(struct rx *, struct rexp_node *);
  3488. static int has_non_idempotent_epsilon_path
  3489. (struct rx *, struct rexp_node *, struct re_se_params *);
  3490. static int begins_with_complex_se(struct rx *, struct rexp_node *);
  3491. static void speed_up_alt(struct rx *, struct rexp_node *, int);
  3492. RX_DECL reg_errcode_t
  3493. rx_compile(__const__ char *, int, reg_syntax_t,
  3494. struct re_pattern_buffer *);
  3495. RX_DECL void rx_blow_up_fastmap(struct re_pattern_buffer *);
  3496. static __inline__ enum rx_get_burst_return
  3497. re_search_2_get_burst(struct rx_string_position *, void *, int);
  3498. static __inline__ enum rx_back_check_return
  3499. re_search_2_back_check(struct rx_string_position *, int,
  3500. int, unsigned char *, void *, int);
  3501. static __inline__ int
  3502. re_search_2_fetch_char(struct rx_string_position *, int, void *, int);
  3503. #define INIT_COMPILE_STACK_SIZE 32
  3504. #define COMPILE_STACK_EMPTY (compile_stack.avail == 0)
  3505. #define COMPILE_STACK_FULL (compile_stack.avail == compile_stack.size)
  3506. /* The next available element. */
  3507. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  3508. /* Set the bit for character C in a list. */
  3509. #define SET_LIST_BIT(c) \
  3510. (b[((unsigned char) (c)) / CHARBITS] \
  3511. |= 1 << (((unsigned char) c) % CHARBITS))
  3512. /* Get the next unsigned number in the uncompiled pattern. */
  3513. #define GET_UNSIGNED_NUMBER(num) \
  3514. { if (p != pend) \
  3515. { \
  3516. PATFETCH (c); \
  3517. while (isdigit (c)) \
  3518. { \
  3519. if (num < 0) \
  3520. num = 0; \
  3521. num = num * 10 + c - '0'; \
  3522. if (p == pend) \
  3523. break; \
  3524. PATFETCH (c); \
  3525. } \
  3526. } \
  3527. }
  3528. #define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  3529. #define IS_CHAR_CLASS(string) \
  3530. (!strcmp (string, "alpha") || !strcmp (string, "upper") \
  3531. || !strcmp (string, "lower") || !strcmp (string, "digit") \
  3532. || !strcmp (string, "alnum") || !strcmp (string, "xdigit") \
  3533. || !strcmp (string, "space") || !strcmp (string, "print") \
  3534. || !strcmp (string, "punct") || !strcmp (string, "graph") \
  3535. || !strcmp (string, "cntrl") || !strcmp (string, "blank"))
  3536. /* These predicates are used in regex_compile. */
  3537. /* P points to just after a ^ in PATTERN. Return true if that ^ comes
  3538. * after an alternative or a begin-subexpression. We assume there is at
  3539. * least one character before the ^.
  3540. */
  3541. #ifdef __STDC__
  3542. static boolean
  3543. at_begline_loc_p(__const__ char *pattern, __const__ char *p,
  3544. reg_syntax_t syntax)
  3545. #else
  3546. static boolean at_begline_loc_p(pattern, p, syntax)
  3547. __const__ char *pattern;
  3548. __const__ char *p;
  3549. reg_syntax_t syntax;
  3550. #endif
  3551. {
  3552. __const__ char *prev = p - 2;
  3553. boolean prev_prev_backslash = ((prev > pattern) && (prev[-1] == '\\'));
  3554. return ( /* After a subexpression? */
  3555. ((*prev == '(') && ((syntax & RE_NO_BK_PARENS) || prev_prev_backslash))
  3556. ||
  3557. /* After an alternative? */
  3558. ((*prev == '|') && ((syntax & RE_NO_BK_VBAR) || prev_prev_backslash))
  3559. );
  3560. }
  3561. /* The dual of at_begline_loc_p. This one is for $. We assume there is
  3562. * at least one character after the $, i.e., `P < PEND'.
  3563. */
  3564. #ifdef __STDC__
  3565. static boolean
  3566. at_endline_loc_p(__const__ char *p, __const__ char *pend, int syntax)
  3567. #else
  3568. static boolean at_endline_loc_p(p, pend, syntax)
  3569. __const__ char *p;
  3570. __const__ char *pend;
  3571. int syntax;
  3572. #endif
  3573. {
  3574. __const__ char *next = p;
  3575. boolean next_backslash = (*next == '\\');
  3576. __const__ char *next_next = (p + 1 < pend) ? (p + 1) : 0;
  3577. return (
  3578. /* Before a subexpression? */
  3579. ((syntax & RE_NO_BK_PARENS)
  3580. ? (*next == ')')
  3581. : (next_backslash && next_next && (*next_next == ')')))
  3582. ||
  3583. /* Before an alternative? */
  3584. ((syntax & RE_NO_BK_VBAR)
  3585. ? (*next == '|')
  3586. : (next_backslash && next_next && (*next_next == '|')))
  3587. );
  3588. }
  3589. unsigned char rx_id_translation[256] = {
  3590. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  3591. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  3592. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  3593. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  3594. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  3595. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  3596. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  3597. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  3598. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  3599. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  3600. 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  3601. 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  3602. 120, 121, 122, 123, 124, 125, 126, 127, 128, 129,
  3603. 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
  3604. 140, 141, 142, 143, 144, 145, 146, 147, 148, 149,
  3605. 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  3606. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
  3607. 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
  3608. 180, 181, 182, 183, 184, 185, 186, 187, 188, 189,
  3609. 190, 191, 192, 193, 194, 195, 196, 197, 198, 199,
  3610. 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
  3611. 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  3612. 220, 221, 222, 223, 224, 225, 226, 227, 228, 229,
  3613. 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  3614. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249,
  3615. 250, 251, 252, 253, 254, 255
  3616. };
  3617. /* The compiler keeps an inverted translation table.
  3618. * This looks up/inititalize elements.
  3619. * VALID is an array of booleans that validate CACHE.
  3620. */
  3621. #ifdef __STDC__
  3622. static rx_Bitset
  3623. inverse_translation(struct re_pattern_buffer *rxb,
  3624. char *valid, rx_Bitset cache,
  3625. unsigned char *translate, int c)
  3626. #else
  3627. static rx_Bitset inverse_translation(rxb, valid, cache, translate, c)
  3628. struct re_pattern_buffer *rxb;
  3629. char *valid;
  3630. rx_Bitset cache;
  3631. unsigned char *translate;
  3632. int c;
  3633. #endif
  3634. {
  3635. rx_Bitset cs
  3636. = cache + c * rx_bitset_numb_subsets(rxb->rx.local_cset_size);
  3637. if (!valid[c]) {
  3638. int x;
  3639. int c_tr = TRANSLATE(c);
  3640. rx_bitset_null(rxb->rx.local_cset_size, cs);
  3641. for (x = 0; x < 256; ++x) /* &&&& 13.37 */
  3642. if (TRANSLATE(x) == c_tr)
  3643. RX_bitset_enjoin(cs, x);
  3644. valid[c] = 1;
  3645. }
  3646. return cs;
  3647. }
  3648. /* More subroutine declarations and macros for regex_compile. */
  3649. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
  3650. false if it's not. */
  3651. #ifdef __STDC__
  3652. static boolean
  3653. group_in_compile_stack(compile_stack_type compile_stack, regnum_t regnum)
  3654. #else
  3655. static boolean group_in_compile_stack(compile_stack, regnum)
  3656. compile_stack_type compile_stack;
  3657. regnum_t regnum;
  3658. #endif
  3659. {
  3660. int this_element;
  3661. for (this_element = compile_stack.avail - 1;
  3662. this_element >= 0; this_element--)
  3663. if (compile_stack.stack[this_element].regnum == regnum)
  3664. return true;
  3665. return false;
  3666. }
  3667. /*
  3668. * Read the ending character of a range (in a bracket expression) from the
  3669. * uncompiled pattern *P_PTR (which ends at PEND). We assume the
  3670. * starting character is in `P[-2]'. (`P[-1]' is the character `-'.)
  3671. * Then we set the translation of all bits between the starting and
  3672. * ending characters (inclusive) in the compiled pattern B.
  3673. *
  3674. * Return an error code.
  3675. *
  3676. * We use these short variable names so we can use the same macros as
  3677. * `regex_compile' itself.
  3678. */
  3679. #ifdef __STDC__
  3680. static reg_errcode_t
  3681. compile_range(struct re_pattern_buffer *rxb, rx_Bitset cs,
  3682. __const__ char **p_ptr, __const__ char *pend,
  3683. unsigned char *translate, reg_syntax_t syntax,
  3684. rx_Bitset inv_tr, char *valid_inv_tr)
  3685. #else
  3686. static reg_errcode_t
  3687. compile_range(rxb, cs, p_ptr, pend, translate, syntax, inv_tr,
  3688. valid_inv_tr)
  3689. struct re_pattern_buffer *rxb;
  3690. rx_Bitset cs;
  3691. __const__ char **p_ptr;
  3692. __const__ char *pend;
  3693. unsigned char *translate;
  3694. reg_syntax_t syntax;
  3695. rx_Bitset inv_tr;
  3696. char *valid_inv_tr;
  3697. #endif
  3698. {
  3699. unsigned this_char;
  3700. __const__ char *p = *p_ptr;
  3701. unsigned char range_end;
  3702. unsigned char range_start = TRANSLATE(p[-2]);
  3703. if (p == pend)
  3704. return REG_ERANGE;
  3705. PATFETCH(range_end);
  3706. (*p_ptr)++;
  3707. if (range_start > range_end)
  3708. return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  3709. for (this_char = range_start; this_char <= range_end; this_char++) {
  3710. rx_Bitset it =
  3711. inverse_translation(rxb, valid_inv_tr, inv_tr, translate,
  3712. this_char);
  3713. rx_bitset_union(rxb->rx.local_cset_size, cs, it);
  3714. }
  3715. return REG_NOERROR;
  3716. }
  3717. /* This searches a regexp for backreference side effects.
  3718. * It fills in the array OUT with 1 at the index of every register pair
  3719. * referenced by a backreference.
  3720. *
  3721. * This is used to help optimize patterns for searching. The information is
  3722. * useful because, if the caller doesn't want register values, backreferenced
  3723. * registers are the only registers for which we need rx_backtrack.
  3724. */
  3725. #ifdef __STDC__
  3726. static void
  3727. find_backrefs(char *out, struct rexp_node *rexp,
  3728. struct re_se_params *params)
  3729. #else
  3730. static void find_backrefs(out, rexp, params)
  3731. char *out;
  3732. struct rexp_node *rexp;
  3733. struct re_se_params *params;
  3734. #endif
  3735. {
  3736. if (rexp)
  3737. switch (rexp->type) {
  3738. case r_cset:
  3739. case r_data:
  3740. return;
  3741. case r_alternate:
  3742. case r_concat:
  3743. case r_opt:
  3744. case r_star:
  3745. case r_2phase_star:
  3746. find_backrefs(out, rexp->params.pair.left, params);
  3747. find_backrefs(out, rexp->params.pair.right, params);
  3748. return;
  3749. case r_side_effect:
  3750. if (((long) rexp->params.side_effect >= 0)
  3751. && (params[(long) rexp->params.side_effect].se ==
  3752. re_se_backref))
  3753. out[params[(long) rexp->params.side_effect].op1] = 1;
  3754. return;
  3755. }
  3756. }
  3757. /* Returns 0 unless the pattern can match the empty string. */
  3758. #ifdef __STDC__
  3759. static int
  3760. compute_fastset(struct re_pattern_buffer *rxb, struct rexp_node *rexp)
  3761. #else
  3762. static int compute_fastset(rxb, rexp)
  3763. struct re_pattern_buffer *rxb;
  3764. struct rexp_node *rexp;
  3765. #endif
  3766. {
  3767. if (!rexp)
  3768. return 1;
  3769. switch (rexp->type) {
  3770. case r_data:
  3771. return 1;
  3772. case r_cset:
  3773. {
  3774. rx_bitset_union(rxb->rx.local_cset_size,
  3775. rxb->fastset, rexp->params.cset);
  3776. }
  3777. return 0;
  3778. case r_concat:
  3779. return (compute_fastset(rxb, rexp->params.pair.left)
  3780. && compute_fastset(rxb, rexp->params.pair.right));
  3781. case r_2phase_star:
  3782. compute_fastset(rxb, rexp->params.pair.left);
  3783. /* compute_fastset (rxb, rexp->params.pair.right); nope... */
  3784. return 1;
  3785. case r_alternate:
  3786. return !!(compute_fastset(rxb, rexp->params.pair.left)
  3787. + compute_fastset(rxb, rexp->params.pair.right));
  3788. case r_opt:
  3789. case r_star:
  3790. compute_fastset(rxb, rexp->params.pair.left);
  3791. return 1;
  3792. case r_side_effect:
  3793. return 1;
  3794. }
  3795. /* this should never happen */
  3796. return 0;
  3797. }
  3798. /* returns
  3799. * 1 -- yes, definately anchored by the given side effect.
  3800. * 2 -- maybe anchored, maybe the empty string.
  3801. * 0 -- definately not anchored
  3802. * There is simply no other possibility.
  3803. */
  3804. #ifdef __STDC__
  3805. static int is_anchored(struct rexp_node *rexp, rx_side_effect se)
  3806. #else
  3807. static int is_anchored(rexp, se)
  3808. struct rexp_node *rexp;
  3809. rx_side_effect se;
  3810. #endif
  3811. {
  3812. if (!rexp)
  3813. return 2;
  3814. switch (rexp->type) {
  3815. case r_cset:
  3816. case r_data:
  3817. return 0;
  3818. case r_concat:
  3819. case r_2phase_star:
  3820. {
  3821. int l = is_anchored(rexp->params.pair.left, se);
  3822. return (l == 2 ? is_anchored(rexp->params.pair.right, se) : l);
  3823. }
  3824. case r_alternate:
  3825. {
  3826. int l = is_anchored(rexp->params.pair.left, se);
  3827. int r = l ? is_anchored(rexp->params.pair.right, se) : 0;
  3828. if (l == r)
  3829. return l;
  3830. else if ((l == 0) || (r == 0))
  3831. return 0;
  3832. else
  3833. return 2;
  3834. }
  3835. case r_opt:
  3836. case r_star:
  3837. return is_anchored(rexp->params.pair.left, se) ? 2 : 0;
  3838. case r_side_effect:
  3839. return ((rexp->params.side_effect == se)
  3840. ? 1 : 2);
  3841. }
  3842. /* this should never happen */
  3843. return 0;
  3844. }
  3845. /* This removes register assignments that aren't required by backreferencing.
  3846. * This can speed up explore_future, especially if it eliminates
  3847. * non-determinism in the superstate NFA.
  3848. *
  3849. * NEEDED is an array of characters, presumably filled in by FIND_BACKREFS.
  3850. * The non-zero elements of the array indicate which register assignments
  3851. * can NOT be removed from the expression.
  3852. */
  3853. #ifdef __STDC__
  3854. static struct rexp_node *remove_unecessary_side_effects(struct rx *rx,
  3855. char *needed,
  3856. struct rexp_node
  3857. *rexp,
  3858. struct re_se_params
  3859. *params)
  3860. #else
  3861. static struct rexp_node *remove_unecessary_side_effects(rx, needed, rexp,
  3862. params)
  3863. struct rx *rx;
  3864. char *needed;
  3865. struct rexp_node *rexp;
  3866. struct re_se_params *params;
  3867. #endif
  3868. {
  3869. struct rexp_node *l;
  3870. struct rexp_node *r;
  3871. if (!rexp)
  3872. return 0;
  3873. else
  3874. switch (rexp->type) {
  3875. case r_cset:
  3876. case r_data:
  3877. return rexp;
  3878. case r_alternate:
  3879. case r_concat:
  3880. case r_2phase_star:
  3881. l = remove_unecessary_side_effects(rx, needed,
  3882. rexp->params.pair.left,
  3883. params);
  3884. r =
  3885. remove_unecessary_side_effects(rx, needed,
  3886. rexp->params.pair.right,
  3887. params);
  3888. if ((l && r) || (rexp->type != r_concat)) {
  3889. rexp->params.pair.left = l;
  3890. rexp->params.pair.right = r;
  3891. return rexp;
  3892. } else {
  3893. rexp->params.pair.left = rexp->params.pair.right = 0;
  3894. rx_free_rexp(rx, rexp);
  3895. return l ? l : r;
  3896. }
  3897. case r_opt:
  3898. case r_star:
  3899. l = remove_unecessary_side_effects(rx, needed,
  3900. rexp->params.pair.left,
  3901. params);
  3902. if (l) {
  3903. rexp->params.pair.left = l;
  3904. return rexp;
  3905. } else {
  3906. rexp->params.pair.left = 0;
  3907. rx_free_rexp(rx, rexp);
  3908. return 0;
  3909. }
  3910. case r_side_effect:
  3911. {
  3912. int se = (long) rexp->params.side_effect;
  3913. if ((se >= 0)
  3914. && (((enum re_side_effects) params[se].se == re_se_lparen)
  3915. || ((enum re_side_effects) params[se].se ==
  3916. re_se_rparen)) && (params[se].op1 > 0)
  3917. && (!needed[params[se].op1])) {
  3918. rx_free_rexp(rx, rexp);
  3919. return 0;
  3920. } else
  3921. return rexp;
  3922. }
  3923. }
  3924. /* this should never happen */
  3925. return 0;
  3926. }
  3927. #ifdef __STDC__
  3928. static int
  3929. pointless_if_repeated(struct rexp_node *node, struct re_se_params *params)
  3930. #else
  3931. static int pointless_if_repeated(node, params)
  3932. struct rexp_node *node;
  3933. struct re_se_params *params;
  3934. #endif
  3935. {
  3936. if (!node)
  3937. return 1;
  3938. switch (node->type) {
  3939. case r_cset:
  3940. return 0;
  3941. case r_alternate:
  3942. case r_concat:
  3943. case r_2phase_star:
  3944. return (pointless_if_repeated(node->params.pair.left, params)
  3945. && pointless_if_repeated(node->params.pair.right, params));
  3946. case r_opt:
  3947. case r_star:
  3948. return pointless_if_repeated(node->params.pair.left, params);
  3949. case r_side_effect:
  3950. switch (((long) node->params.side_effect < 0)
  3951. ? (enum re_side_effects) node->params.side_effect
  3952. : (enum re_side_effects) params[(long) node->
  3953. params.side_effect].se) {
  3954. case re_se_try:
  3955. case re_se_at_dot:
  3956. case re_se_begbuf:
  3957. case re_se_hat:
  3958. case re_se_wordbeg:
  3959. case re_se_wordbound:
  3960. case re_se_notwordbound:
  3961. case re_se_wordend:
  3962. case re_se_endbuf:
  3963. case re_se_dollar:
  3964. case re_se_fail:
  3965. case re_se_win:
  3966. return 1;
  3967. case re_se_lparen:
  3968. case re_se_rparen:
  3969. case re_se_iter:
  3970. case re_se_end_iter:
  3971. case re_se_syntax:
  3972. case re_se_not_syntax:
  3973. case re_se_backref:
  3974. return 0;
  3975. }
  3976. case r_data:
  3977. default:
  3978. return 0;
  3979. }
  3980. }
  3981. #ifdef __STDC__
  3982. static int
  3983. registers_on_stack(struct re_pattern_buffer *rxb,
  3984. struct rexp_node *rexp, int in_danger,
  3985. struct re_se_params *params)
  3986. #else
  3987. static int registers_on_stack(rxb, rexp, in_danger, params)
  3988. struct re_pattern_buffer *rxb;
  3989. struct rexp_node *rexp;
  3990. int in_danger;
  3991. struct re_se_params *params;
  3992. #endif
  3993. {
  3994. if (!rexp)
  3995. return 0;
  3996. else
  3997. switch (rexp->type) {
  3998. case r_cset:
  3999. case r_data:
  4000. return 0;
  4001. case r_alternate:
  4002. case r_concat:
  4003. return (registers_on_stack(rxb, rexp->params.pair.left,
  4004. in_danger, params)
  4005. || (registers_on_stack
  4006. (rxb, rexp->params.pair.right,
  4007. in_danger, params)));
  4008. case r_opt:
  4009. return registers_on_stack(rxb, rexp->params.pair.left, 0,
  4010. params);
  4011. case r_star:
  4012. return registers_on_stack(rxb, rexp->params.pair.left, 1,
  4013. params);
  4014. case r_2phase_star:
  4015. return
  4016. (registers_on_stack(rxb, rexp->params.pair.left, 1, params)
  4017. || registers_on_stack(rxb, rexp->params.pair.right, 1,
  4018. params));
  4019. case r_side_effect:
  4020. {
  4021. int se = (long) rexp->params.side_effect;
  4022. if (in_danger && (se >= 0)
  4023. && (params[se].op1 > 0)
  4024. && (((enum re_side_effects) params[se].se == re_se_lparen)
  4025. || ((enum re_side_effects) params[se].se ==
  4026. re_se_rparen))) return 1;
  4027. else
  4028. return 0;
  4029. }
  4030. }
  4031. /* this should never happen */
  4032. return 0;
  4033. }
  4034. static char idempotent_complex_se[] = {
  4035. #define RX_WANT_SE_DEFS 1
  4036. #undef RX_DEF_SE
  4037. #undef RX_DEF_CPLX_SE
  4038. #define RX_DEF_SE(IDEM, NAME, VALUE)
  4039. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE) IDEM,
  4040. #include <regex.h>
  4041. #undef RX_DEF_SE
  4042. #undef RX_DEF_CPLX_SE
  4043. #undef RX_WANT_SE_DEFS
  4044. 23
  4045. };
  4046. static char idempotent_se[] = {
  4047. 13,
  4048. #define RX_WANT_SE_DEFS 1
  4049. #undef RX_DEF_SE
  4050. #undef RX_DEF_CPLX_SE
  4051. #define RX_DEF_SE(IDEM, NAME, VALUE) IDEM,
  4052. #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE)
  4053. #include <regex.h>
  4054. #undef RX_DEF_SE
  4055. #undef RX_DEF_CPLX_SE
  4056. #undef RX_WANT_SE_DEFS
  4057. 42
  4058. };
  4059. #ifdef __STDC__
  4060. static int has_any_se(struct rx *rx, struct rexp_node *rexp)
  4061. #else
  4062. static int has_any_se(rx, rexp)
  4063. struct rx *rx;
  4064. struct rexp_node *rexp;
  4065. #endif
  4066. {
  4067. if (!rexp)
  4068. return 0;
  4069. switch (rexp->type) {
  4070. case r_cset:
  4071. case r_data:
  4072. return 0;
  4073. case r_side_effect:
  4074. return 1;
  4075. case r_2phase_star:
  4076. case r_concat:
  4077. case r_alternate:
  4078. return (has_any_se(rx, rexp->params.pair.left)
  4079. || has_any_se(rx, rexp->params.pair.right));
  4080. case r_opt:
  4081. case r_star:
  4082. return has_any_se(rx, rexp->params.pair.left);
  4083. }
  4084. /* this should never happen */
  4085. return 0;
  4086. }
  4087. /* This must be called AFTER `convert_hard_loops' for a given REXP. */
  4088. #ifdef __STDC__
  4089. static int
  4090. has_non_idempotent_epsilon_path(struct rx *rx,
  4091. struct rexp_node *rexp,
  4092. struct re_se_params *params)
  4093. #else
  4094. static int has_non_idempotent_epsilon_path(rx, rexp, params)
  4095. struct rx *rx;
  4096. struct rexp_node *rexp;
  4097. struct re_se_params *params;
  4098. #endif
  4099. {
  4100. if (!rexp)
  4101. return 0;
  4102. switch (rexp->type) {
  4103. case r_cset:
  4104. case r_data:
  4105. case r_star:
  4106. return 0;
  4107. case r_side_effect:
  4108. return
  4109. !((long) rexp->params.side_effect > 0
  4110. ?
  4111. idempotent_complex_se[params
  4112. [(long) rexp->params.
  4113. side_effect].se] :
  4114. idempotent_se[-(long) rexp->params.side_effect]);
  4115. case r_alternate:
  4116. return
  4117. (has_non_idempotent_epsilon_path(rx,
  4118. rexp->params.pair.left,
  4119. params)
  4120. || has_non_idempotent_epsilon_path(rx,
  4121. rexp->params.pair.right,
  4122. params));
  4123. case r_2phase_star:
  4124. case r_concat:
  4125. return
  4126. (has_non_idempotent_epsilon_path(rx,
  4127. rexp->params.pair.left,
  4128. params)
  4129. && has_non_idempotent_epsilon_path(rx,
  4130. rexp->params.pair.right,
  4131. params));
  4132. case r_opt:
  4133. return has_non_idempotent_epsilon_path(rx,
  4134. rexp->params.pair.left,
  4135. params);
  4136. }
  4137. /* this should never happen */
  4138. return 0;
  4139. }
  4140. /* This computes rougly what it's name suggests. It can (and does) go wrong
  4141. * in the direction of returning spurious 0 without causing disasters.
  4142. */
  4143. #ifdef __STDC__
  4144. static int begins_with_complex_se(struct rx *rx, struct rexp_node *rexp)
  4145. #else
  4146. static int begins_with_complex_se(rx, rexp)
  4147. struct rx *rx;
  4148. struct rexp_node *rexp;
  4149. #endif
  4150. {
  4151. if (!rexp)
  4152. return 0;
  4153. switch (rexp->type) {
  4154. case r_cset:
  4155. case r_data:
  4156. return 0;
  4157. case r_side_effect:
  4158. return ((long) rexp->params.side_effect >= 0);
  4159. case r_alternate:
  4160. return (begins_with_complex_se(rx, rexp->params.pair.left)
  4161. && begins_with_complex_se(rx, rexp->params.pair.right));
  4162. case r_concat:
  4163. return has_any_se(rx, rexp->params.pair.left);
  4164. case r_opt:
  4165. case r_star:
  4166. case r_2phase_star:
  4167. return 0;
  4168. }
  4169. /* this should never happen */
  4170. return 0;
  4171. }
  4172. /* This destructively removes some of the re_se_tv side effects from
  4173. * a rexp tree. In particular, during parsing re_se_tv was inserted on the
  4174. * right half of every | to guarantee that posix path preference could be
  4175. * honored. This function removes some which it can be determined aren't
  4176. * needed.
  4177. */
  4178. #ifdef __STDC__
  4179. static void
  4180. speed_up_alt(struct rx *rx, struct rexp_node *rexp, int unposix)
  4181. #else
  4182. static void speed_up_alt(rx, rexp, unposix)
  4183. struct rx *rx;
  4184. struct rexp_node *rexp;
  4185. int unposix;
  4186. #endif
  4187. {
  4188. if (!rexp)
  4189. return;
  4190. switch (rexp->type) {
  4191. case r_cset:
  4192. case r_data:
  4193. case r_side_effect:
  4194. return;
  4195. case r_opt:
  4196. case r_star:
  4197. speed_up_alt(rx, rexp->params.pair.left, unposix);
  4198. return;
  4199. case r_2phase_star:
  4200. case r_concat:
  4201. speed_up_alt(rx, rexp->params.pair.left, unposix);
  4202. speed_up_alt(rx, rexp->params.pair.right, unposix);
  4203. return;
  4204. case r_alternate:
  4205. /* the right child is guaranteed to be (concat re_se_tv <subexp>) */
  4206. speed_up_alt(rx, rexp->params.pair.left, unposix);
  4207. speed_up_alt(rx, rexp->params.pair.right->params.pair.right,
  4208. unposix);
  4209. if (unposix
  4210. || (begins_with_complex_se
  4211. (rx, rexp->params.pair.right->params.pair.right))
  4212. || !(has_any_se(rx, rexp->params.pair.right->params.pair.right)
  4213. || has_any_se(rx, rexp->params.pair.left))) {
  4214. struct rexp_node *conc = rexp->params.pair.right;
  4215. rexp->params.pair.right = conc->params.pair.right;
  4216. conc->params.pair.right = 0;
  4217. rx_free_rexp(rx, conc);
  4218. }
  4219. }
  4220. }
  4221. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  4222. Returns one of error codes defined in `regex.h', or zero for success.
  4223. Assumes the `allocated' (and perhaps `buffer') and `translate'
  4224. fields are set in BUFP on entry.
  4225. If it succeeds, results are put in BUFP (if it returns an error, the
  4226. contents of BUFP are undefined):
  4227. `buffer' is the compiled pattern;
  4228. `syntax' is set to SYNTAX;
  4229. `used' is set to the length of the compiled pattern;
  4230. `fastmap_accurate' is set to zero;
  4231. `re_nsub' is set to the number of groups in PATTERN;
  4232. `not_bol' and `not_eol' are set to zero.
  4233. The `fastmap' and `newline_anchor' fields are neither
  4234. examined nor set. */
  4235. #ifdef __STDC__
  4236. RX_DECL reg_errcode_t
  4237. rx_compile(__const__ char *pattern, int size,
  4238. reg_syntax_t syntax, struct re_pattern_buffer *rxb)
  4239. #else
  4240. RX_DECL reg_errcode_t rx_compile(pattern, size, syntax, rxb)
  4241. __const__ char *pattern;
  4242. int size;
  4243. reg_syntax_t syntax;
  4244. struct re_pattern_buffer *rxb;
  4245. #endif
  4246. {
  4247. RX_subset
  4248. inverse_translate[CHAR_SET_SIZE *
  4249. rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  4250. char validate_inv_tr[CHAR_SET_SIZE *
  4251. rx_bitset_numb_subsets(CHAR_SET_SIZE)];
  4252. /* We fetch characters from PATTERN here. Even though PATTERN is
  4253. `char *' (i.e., signed), we declare these variables as unsigned, so
  4254. they can be reliably used as array indices. */
  4255. register unsigned char c, c1;
  4256. /* A random tempory spot in PATTERN. */
  4257. __const__ char *p1;
  4258. /* Keeps track of unclosed groups. */
  4259. compile_stack_type compile_stack;
  4260. /* Points to the current (ending) position in the pattern. */
  4261. __const__ char *p = pattern;
  4262. __const__ char *pend = pattern + size;
  4263. /* How to translate the characters in the pattern. */
  4264. unsigned char *translate = (rxb->translate
  4265. ? rxb->translate : rx_id_translation);
  4266. /* When parsing is done, this will hold the expression tree. */
  4267. struct rexp_node *rexp = 0;
  4268. /* In the midst of compilation, this holds onto the regexp
  4269. * first parst while rexp goes on to aquire additional constructs.
  4270. */
  4271. struct rexp_node *orig_rexp = 0;
  4272. struct rexp_node *fewer_side_effects = 0;
  4273. /* This and top_expression are saved on the compile stack. */
  4274. struct rexp_node **top_expression = &rexp;
  4275. struct rexp_node **last_expression = top_expression;
  4276. /* Parameter to `goto append_node' */
  4277. struct rexp_node *append;
  4278. /* Counts open-groups as they are encountered. This is the index of the
  4279. * innermost group being compiled.
  4280. */
  4281. regnum_t regnum = 0;
  4282. /* Place in the uncompiled pattern (i.e., the {) to
  4283. * which to go back if the interval is invalid.
  4284. */
  4285. __const__ char *beg_interval;
  4286. struct re_se_params *params = 0;
  4287. int paramc = 0; /* How many complex side effects so far? */
  4288. rx_side_effect side; /* param to `goto add_side_effect' */
  4289. bzero(validate_inv_tr, sizeof(validate_inv_tr));
  4290. rxb->rx.instruction_table = rx_id_instruction_table;
  4291. /* Initialize the compile stack. */
  4292. compile_stack.stack = ((compile_stack_elt_t *)
  4293. malloc((INIT_COMPILE_STACK_SIZE) *
  4294. sizeof(compile_stack_elt_t)));
  4295. if (compile_stack.stack == 0)
  4296. return REG_ESPACE;
  4297. compile_stack.size = INIT_COMPILE_STACK_SIZE;
  4298. compile_stack.avail = 0;
  4299. /* Initialize the pattern buffer. */
  4300. rxb->rx.cache = &default_cache;
  4301. rxb->syntax = syntax;
  4302. rxb->fastmap_accurate = 0;
  4303. rxb->not_bol = rxb->not_eol = 0;
  4304. rxb->least_subs = 0;
  4305. /* Always count groups, whether or not rxb->no_sub is set.
  4306. * The whole pattern is implicitly group 0, so counting begins
  4307. * with 1.
  4308. */
  4309. rxb->re_nsub = 0;
  4310. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  4311. /* Initialize the syntax table. */
  4312. init_syntax_once();
  4313. #endif
  4314. /* Loop through the uncompiled pattern until we're at the end. */
  4315. while (p != pend) {
  4316. PATFETCH(c);
  4317. switch (c) {
  4318. case '^':
  4319. {
  4320. if ( /* If at start of pattern, it's an operator. */
  4321. p == pattern + 1
  4322. /* If context independent, it's an operator. */
  4323. || syntax & RE_CONTEXT_INDEP_ANCHORS
  4324. /* Otherwise, depends on what's come before. */
  4325. || at_begline_loc_p(pattern, p, syntax)) {
  4326. struct rexp_node *n = rx_mk_r_side_effect(&rxb->rx,
  4327. (rx_side_effect) re_se_hat);
  4328. if (!n)
  4329. return REG_ESPACE;
  4330. append = n;
  4331. goto append_node;
  4332. } else
  4333. goto normal_char;
  4334. }
  4335. break;
  4336. case '$':
  4337. {
  4338. if ( /* If at end of pattern, it's an operator. */
  4339. p == pend
  4340. /* If context independent, it's an operator. */
  4341. || syntax & RE_CONTEXT_INDEP_ANCHORS
  4342. /* Otherwise, depends on what's next. */
  4343. || at_endline_loc_p(p, pend, syntax)) {
  4344. struct rexp_node *n = rx_mk_r_side_effect(&rxb->rx,
  4345. (rx_side_effect) re_se_dollar);
  4346. if (!n)
  4347. return REG_ESPACE;
  4348. append = n;
  4349. goto append_node;
  4350. } else
  4351. goto normal_char;
  4352. }
  4353. break;
  4354. case '+':
  4355. case '?':
  4356. if ((syntax & RE_BK_PLUS_QM)
  4357. || (syntax & RE_LIMITED_OPS))
  4358. goto normal_char;
  4359. handle_plus:
  4360. case '*':
  4361. /* If there is no previous pattern... */
  4362. if (pointless_if_repeated(*last_expression, params)) {
  4363. if (syntax & RE_CONTEXT_INVALID_OPS)
  4364. return REG_BADRPT;
  4365. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  4366. goto normal_char;
  4367. }
  4368. {
  4369. /* 1 means zero (many) matches is allowed. */
  4370. char zero_times_ok = 0, many_times_ok = 0;
  4371. /* If there is a sequence of repetition chars, collapse it
  4372. down to just one (the right one). We can't combine
  4373. interval operators with these because of, e.g., `a{2}*',
  4374. which should only match an even number of `a's. */
  4375. for (;;) {
  4376. zero_times_ok |= c != '+';
  4377. many_times_ok |= c != '?';
  4378. if (p == pend)
  4379. break;
  4380. PATFETCH(c);
  4381. if (c == '*' || (!(syntax & RE_BK_PLUS_QM)
  4382. && (c == '+' || c == '?')));
  4383. else if (syntax & RE_BK_PLUS_QM && c == '\\') {
  4384. if (p == pend)
  4385. return REG_EESCAPE;
  4386. PATFETCH(c1);
  4387. if (!(c1 == '+' || c1 == '?')) {
  4388. PATUNFETCH;
  4389. PATUNFETCH;
  4390. break;
  4391. }
  4392. c = c1;
  4393. } else {
  4394. PATUNFETCH;
  4395. break;
  4396. }
  4397. /* If we get here, we found another repeat character. */
  4398. }
  4399. /* Star, etc. applied to an empty pattern is equivalent
  4400. to an empty pattern. */
  4401. if (!last_expression)
  4402. break;
  4403. /* Now we know whether or not zero matches is allowed
  4404. * and also whether or not two or more matches is allowed.
  4405. */
  4406. {
  4407. struct rexp_node *inner_exp = *last_expression;
  4408. int need_sync = 0;
  4409. if (many_times_ok
  4410. && has_non_idempotent_epsilon_path(&rxb->rx,
  4411. inner_exp,
  4412. params)) {
  4413. struct rexp_node *pusher =
  4414. rx_mk_r_side_effect(&rxb->rx,
  4415. (rx_side_effect) re_se_pushpos);
  4416. struct rexp_node *checker
  4417. = rx_mk_r_side_effect(&rxb->rx,
  4418. (rx_side_effect) re_se_chkpos);
  4419. struct rexp_node *pushback
  4420. = rx_mk_r_side_effect(&rxb->rx,
  4421. (rx_side_effect) re_se_pushback);
  4422. rx_Bitset cs = rx_cset(&rxb->rx);
  4423. struct rexp_node *lit_t;
  4424. struct rexp_node *fake_state;
  4425. struct rexp_node *phase2;
  4426. struct rexp_node *popper;
  4427. struct rexp_node *star;
  4428. struct rexp_node *a;
  4429. struct rexp_node *whole_thing;
  4430. if (!cs)
  4431. return REG_ESPACE;
  4432. lit_t = rx_mk_r_cset(&rxb->rx, cs);
  4433. fake_state =
  4434. rx_mk_r_concat(&rxb->rx, pushback, lit_t);
  4435. phase2 =
  4436. rx_mk_r_concat(&rxb->rx, checker, fake_state);
  4437. popper =
  4438. rx_mk_r_side_effect(&rxb->rx,
  4439. (rx_side_effect)
  4440. re_se_poppos);
  4441. star =
  4442. rx_mk_r_2phase_star(&rxb->rx, inner_exp,
  4443. phase2);
  4444. a = rx_mk_r_concat(&rxb->rx, pusher, star);
  4445. whole_thing = rx_mk_r_concat(&rxb->rx, a, popper);
  4446. if (!
  4447. (pusher && star && pushback && lit_t
  4448. && fake_state && lit_t && phase2 && checker
  4449. && popper && a && whole_thing))
  4450. return REG_ESPACE;
  4451. RX_bitset_enjoin(cs, 't');
  4452. *last_expression = whole_thing;
  4453. } else {
  4454. struct rexp_node *star =
  4455. (many_times_ok ? rx_mk_r_star : rx_mk_r_opt)
  4456. (&rxb->rx, *last_expression);
  4457. if (!star)
  4458. return REG_ESPACE;
  4459. *last_expression = star;
  4460. need_sync = has_any_se(&rxb->rx, *last_expression);
  4461. }
  4462. if (!zero_times_ok) {
  4463. struct rexp_node *concat
  4464. = rx_mk_r_concat(&rxb->rx, inner_exp,
  4465. rx_copy_rexp(&rxb->rx,
  4466. *last_expression));
  4467. if (!concat)
  4468. return REG_ESPACE;
  4469. *last_expression = concat;
  4470. }
  4471. if (need_sync) {
  4472. int sync_se = paramc;
  4473. params = (params ? ((struct re_se_params *)
  4474. realloc(params,
  4475. sizeof(*params) * (1 +
  4476. paramc)))
  4477. : ((struct re_se_params *)
  4478. malloc(sizeof(*params))));
  4479. if (!params)
  4480. return REG_ESPACE;
  4481. ++paramc;
  4482. params[sync_se].se = re_se_tv;
  4483. side = (rx_side_effect) sync_se;
  4484. goto add_side_effect;
  4485. }
  4486. }
  4487. /* The old regex.c used to optimize `.*\n'.
  4488. * Maybe rx should too?
  4489. */
  4490. }
  4491. break;
  4492. case '.':
  4493. {
  4494. rx_Bitset cs = rx_cset(&rxb->rx);
  4495. struct rexp_node *n = rx_mk_r_cset(&rxb->rx, cs);
  4496. if (!(cs && n))
  4497. return REG_ESPACE;
  4498. rx_bitset_universe(rxb->rx.local_cset_size, cs);
  4499. if (!(rxb->syntax & RE_DOT_NEWLINE))
  4500. RX_bitset_remove(cs, '\n');
  4501. if (!(rxb->syntax & RE_DOT_NOT_NULL))
  4502. RX_bitset_remove(cs, 0);
  4503. append = n;
  4504. goto append_node;
  4505. break;
  4506. }
  4507. case '[':
  4508. if (p == pend)
  4509. return REG_EBRACK;
  4510. {
  4511. boolean had_char_class = false;
  4512. rx_Bitset cs = rx_cset(&rxb->rx);
  4513. struct rexp_node *node = rx_mk_r_cset(&rxb->rx, cs);
  4514. int is_inverted = *p == '^';
  4515. if (!(node && cs))
  4516. return REG_ESPACE;
  4517. /* This branch of the switch is normally exited with
  4518. *`goto append_node'
  4519. */
  4520. append = node;
  4521. if (is_inverted)
  4522. p++;
  4523. /* Remember the first position in the bracket expression. */
  4524. p1 = p;
  4525. /* Read in characters and ranges, setting map bits. */
  4526. for (;;) {
  4527. if (p == pend)
  4528. return REG_EBRACK;
  4529. PATFETCH(c);
  4530. /* \ might escape characters inside [...] and [^...]. */
  4531. if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
  4532. && c == '\\') {
  4533. if (p == pend)
  4534. return REG_EESCAPE;
  4535. PATFETCH(c1);
  4536. {
  4537. rx_Bitset it = inverse_translation(rxb,
  4538. validate_inv_tr,
  4539. inverse_translate,
  4540. translate,
  4541. c1);
  4542. rx_bitset_union(rxb->rx.local_cset_size, cs,
  4543. it);
  4544. }
  4545. continue;
  4546. }
  4547. /* Could be the end of the bracket expression. If it's
  4548. not (i.e., when the bracket expression is `[]' so
  4549. far), the ']' character bit gets set way below. */
  4550. if (c == ']' && p != p1 + 1)
  4551. goto finalize_class_and_append;
  4552. /* Look ahead to see if it's a range when the last thing
  4553. was a character class. */
  4554. if (had_char_class && c == '-' && *p != ']')
  4555. return REG_ERANGE;
  4556. /* Look ahead to see if it's a range when the last thing
  4557. was a character: if this is a hyphen not at the
  4558. beginning or the end of a list, then it's the range
  4559. operator. */
  4560. if (c == '-' && !(p - 2 >= pattern && p[-2] == '[')
  4561. && !(p - 3 >= pattern && p[-3] == '['
  4562. && p[-2] == '^') && *p != ']') {
  4563. reg_errcode_t ret =
  4564. compile_range(rxb, cs, &p, pend, translate,
  4565. syntax,
  4566. inverse_translate,
  4567. validate_inv_tr);
  4568. if (ret != REG_NOERROR)
  4569. return ret;
  4570. }
  4571. else if (p[0] == '-' && p[1] != ']') { /* This handles ranges made up of characters only. */
  4572. reg_errcode_t ret;
  4573. /* Move past the `-'. */
  4574. PATFETCH(c1);
  4575. ret =
  4576. compile_range(rxb, cs, &p, pend, translate,
  4577. syntax, inverse_translate,
  4578. validate_inv_tr);
  4579. if (ret != REG_NOERROR)
  4580. return ret;
  4581. }
  4582. /* See if we're at the beginning of a possible character
  4583. class. */
  4584. else if ((syntax & RE_CHAR_CLASSES)
  4585. && (c == '[') && (*p == ':')) {
  4586. char str[CHAR_CLASS_MAX_LENGTH + 1];
  4587. PATFETCH(c);
  4588. c1 = 0;
  4589. /* If pattern is `[[:'. */
  4590. if (p == pend)
  4591. return REG_EBRACK;
  4592. for (;;) {
  4593. PATFETCH(c);
  4594. if (c == ':' || c == ']' || p == pend
  4595. || c1 == CHAR_CLASS_MAX_LENGTH) break;
  4596. str[c1++] = c;
  4597. }
  4598. str[c1] = '\0';
  4599. /* If isn't a word bracketed by `[:' and:`]':
  4600. undo the ending character, the letters, and leave
  4601. the leading `:' and `[' (but set bits for them). */
  4602. if (c == ':' && *p == ']') {
  4603. int ch;
  4604. boolean is_alnum = !strcmp(str, "alnum");
  4605. boolean is_alpha = !strcmp(str, "alpha");
  4606. boolean is_blank = !strcmp(str, "blank");
  4607. boolean is_cntrl = !strcmp(str, "cntrl");
  4608. boolean is_digit = !strcmp(str, "digit");
  4609. boolean is_graph = !strcmp(str, "graph");
  4610. boolean is_lower = !strcmp(str, "lower");
  4611. boolean is_print = !strcmp(str, "print");
  4612. boolean is_punct = !strcmp(str, "punct");
  4613. boolean is_space = !strcmp(str, "space");
  4614. boolean is_upper = !strcmp(str, "upper");
  4615. boolean is_xdigit = !strcmp(str, "xdigit");
  4616. if (!IS_CHAR_CLASS(str))
  4617. return REG_ECTYPE;
  4618. /* Throw away the ] at the end of the character
  4619. class. */
  4620. PATFETCH(c);
  4621. if (p == pend)
  4622. return REG_EBRACK;
  4623. for (ch = 0; ch < 1 << CHARBITS; ch++) {
  4624. if ((is_alnum && isalnum(ch))
  4625. || (is_alpha && isalpha(ch))
  4626. || (is_blank && isblank(ch))
  4627. || (is_cntrl && iscntrl(ch))
  4628. || (is_digit && isdigit(ch))
  4629. || (is_graph && isgraph(ch))
  4630. || (is_lower && islower(ch))
  4631. || (is_print && isprint(ch))
  4632. || (is_punct && ispunct(ch))
  4633. || (is_space && isspace(ch))
  4634. || (is_upper && isupper(ch))
  4635. || (is_xdigit && isxdigit(ch))) {
  4636. rx_Bitset it = inverse_translation(rxb,
  4637. validate_inv_tr,
  4638. inverse_translate,
  4639. translate,
  4640. ch);
  4641. rx_bitset_union(rxb->
  4642. rx.local_cset_size, cs,
  4643. it);
  4644. }
  4645. }
  4646. had_char_class = true;
  4647. } else {
  4648. c1++;
  4649. while (c1--)
  4650. PATUNFETCH;
  4651. {
  4652. rx_Bitset it = inverse_translation(rxb,
  4653. validate_inv_tr,
  4654. inverse_translate,
  4655. translate,
  4656. '[');
  4657. rx_bitset_union(rxb->rx.local_cset_size,
  4658. cs, it);
  4659. }
  4660. {
  4661. rx_Bitset it = inverse_translation(rxb,
  4662. validate_inv_tr,
  4663. inverse_translate,
  4664. translate,
  4665. ':');
  4666. rx_bitset_union(rxb->rx.local_cset_size,
  4667. cs, it);
  4668. }
  4669. had_char_class = false;
  4670. }
  4671. } else {
  4672. had_char_class = false;
  4673. {
  4674. rx_Bitset it = inverse_translation(rxb,
  4675. validate_inv_tr,
  4676. inverse_translate,
  4677. translate,
  4678. c);
  4679. rx_bitset_union(rxb->rx.local_cset_size, cs,
  4680. it);
  4681. }
  4682. }
  4683. }
  4684. finalize_class_and_append:
  4685. if (is_inverted) {
  4686. rx_bitset_complement(rxb->rx.local_cset_size, cs);
  4687. if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
  4688. RX_bitset_remove(cs, '\n');
  4689. }
  4690. goto append_node;
  4691. }
  4692. break;
  4693. case '(':
  4694. if (syntax & RE_NO_BK_PARENS)
  4695. goto handle_open;
  4696. else
  4697. goto normal_char;
  4698. case ')':
  4699. if (syntax & RE_NO_BK_PARENS)
  4700. goto handle_close;
  4701. else
  4702. goto normal_char;
  4703. case '\n':
  4704. if (syntax & RE_NEWLINE_ALT)
  4705. goto handle_alt;
  4706. else
  4707. goto normal_char;
  4708. case '|':
  4709. if (syntax & RE_NO_BK_VBAR)
  4710. goto handle_alt;
  4711. else
  4712. goto normal_char;
  4713. case '{':
  4714. if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  4715. goto handle_interval;
  4716. else
  4717. goto normal_char;
  4718. case '\\':
  4719. if (p == pend)
  4720. return REG_EESCAPE;
  4721. /* Do not translate the character after the \, so that we can
  4722. distinguish, e.g., \B from \b, even if we normally would
  4723. translate, e.g., B to b. */
  4724. PATFETCH_RAW(c);
  4725. switch (c) {
  4726. case '(':
  4727. if (syntax & RE_NO_BK_PARENS)
  4728. goto normal_backslash;
  4729. handle_open:
  4730. rxb->re_nsub++;
  4731. regnum++;
  4732. if (COMPILE_STACK_FULL) {
  4733. ((compile_stack.stack) =
  4734. (compile_stack_elt_t *) realloc(compile_stack.stack,
  4735. (compile_stack.size <<
  4736. 1) *
  4737. sizeof
  4738. (compile_stack_elt_t)));
  4739. if (compile_stack.stack == 0)
  4740. return REG_ESPACE;
  4741. compile_stack.size <<= 1;
  4742. }
  4743. if (*last_expression) {
  4744. struct rexp_node *concat
  4745. = rx_mk_r_concat(&rxb->rx, *last_expression, 0);
  4746. if (!concat)
  4747. return REG_ESPACE;
  4748. *last_expression = concat;
  4749. last_expression = &concat->params.pair.right;
  4750. }
  4751. /*
  4752. * These are the values to restore when we hit end of this
  4753. * group.
  4754. */
  4755. COMPILE_STACK_TOP.top_expression = top_expression;
  4756. COMPILE_STACK_TOP.last_expression = last_expression;
  4757. COMPILE_STACK_TOP.regnum = regnum;
  4758. compile_stack.avail++;
  4759. top_expression = last_expression;
  4760. break;
  4761. case ')':
  4762. if (syntax & RE_NO_BK_PARENS)
  4763. goto normal_backslash;
  4764. handle_close:
  4765. /* See similar code for backslashed left paren above. */
  4766. if (COMPILE_STACK_EMPTY) {
  4767. if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) {
  4768. goto normal_char;
  4769. } else {
  4770. return REG_ERPAREN;
  4771. }
  4772. }
  4773. /* Since we just checked for an empty stack above, this
  4774. ``can't happen''. */
  4775. {
  4776. /* We don't just want to restore into `regnum', because
  4777. later groups should continue to be numbered higher,
  4778. as in `(ab)c(de)' -- the second group is #2. */
  4779. regnum_t this_group_regnum;
  4780. struct rexp_node **inner = top_expression;
  4781. compile_stack.avail--;
  4782. top_expression = COMPILE_STACK_TOP.top_expression;
  4783. last_expression = COMPILE_STACK_TOP.last_expression;
  4784. this_group_regnum = COMPILE_STACK_TOP.regnum;
  4785. {
  4786. int left_se = paramc;
  4787. int right_se = paramc + 1;
  4788. params = (params ? ((struct re_se_params *)
  4789. realloc(params,
  4790. (paramc +
  4791. 2) *
  4792. sizeof(params[0])))
  4793. : ((struct re_se_params *)
  4794. malloc(2 * sizeof(params[0]))));
  4795. if (!params)
  4796. return REG_ESPACE;
  4797. paramc += 2;
  4798. params[left_se].se = re_se_lparen;
  4799. params[left_se].op1 = this_group_regnum;
  4800. params[right_se].se = re_se_rparen;
  4801. params[right_se].op1 = this_group_regnum;
  4802. {
  4803. struct rexp_node *left
  4804. = rx_mk_r_side_effect(&rxb->rx,
  4805. (rx_side_effect) left_se);
  4806. struct rexp_node *right
  4807. = rx_mk_r_side_effect(&rxb->rx,
  4808. (rx_side_effect) right_se);
  4809. struct rexp_node *c1
  4810. = (*inner ? rx_mk_r_concat(&rxb->rx, left,
  4811. *inner) : left);
  4812. struct rexp_node *c2 =
  4813. rx_mk_r_concat(&rxb->rx, c1, right);
  4814. if (!(left && right && c1 && c2))
  4815. return REG_ESPACE;
  4816. *inner = c2;
  4817. }
  4818. }
  4819. break;
  4820. }
  4821. case '|': /* `\|'. */
  4822. if ((syntax & RE_LIMITED_OPS) || (syntax & RE_NO_BK_VBAR))
  4823. goto normal_backslash;
  4824. handle_alt:
  4825. if (syntax & RE_LIMITED_OPS)
  4826. goto normal_char;
  4827. {
  4828. struct rexp_node *alt
  4829. = rx_mk_r_alternate(&rxb->rx, *top_expression, 0);
  4830. if (!alt)
  4831. return REG_ESPACE;
  4832. *top_expression = alt;
  4833. last_expression = &alt->params.pair.right;
  4834. {
  4835. int sync_se = paramc;
  4836. params = (params ? ((struct re_se_params *)
  4837. realloc(params,
  4838. (paramc +
  4839. 1) *
  4840. sizeof(params[0])))
  4841. : ((struct re_se_params *)
  4842. malloc(sizeof(params[0]))));
  4843. if (!params)
  4844. return REG_ESPACE;
  4845. ++paramc;
  4846. params[sync_se].se = re_se_tv;
  4847. {
  4848. struct rexp_node *sync
  4849. = rx_mk_r_side_effect(&rxb->rx,
  4850. (rx_side_effect) sync_se);
  4851. struct rexp_node *conc
  4852. = rx_mk_r_concat(&rxb->rx, sync, 0);
  4853. if (!sync || !conc)
  4854. return REG_ESPACE;
  4855. *last_expression = conc;
  4856. last_expression = &conc->params.pair.right;
  4857. }
  4858. }
  4859. }
  4860. break;
  4861. case '{':
  4862. /* If \{ is a literal. */
  4863. if (!(syntax & RE_INTERVALS)
  4864. /* If we're at `\{' and it's not the open-interval
  4865. operator. */
  4866. || ((syntax & RE_INTERVALS)
  4867. && (syntax & RE_NO_BK_BRACES)) || (p - 2 == pattern
  4868. && p == pend))
  4869. goto normal_backslash;
  4870. handle_interval:
  4871. {
  4872. /* If got here, then the syntax allows intervals. */
  4873. /* At least (most) this many matches must be made. */
  4874. int lower_bound = -1, upper_bound = -1;
  4875. beg_interval = p - 1;
  4876. if (p == pend) {
  4877. if (syntax & RE_NO_BK_BRACES)
  4878. goto unfetch_interval;
  4879. else
  4880. return REG_EBRACE;
  4881. }
  4882. GET_UNSIGNED_NUMBER(lower_bound);
  4883. if (c == ',') {
  4884. GET_UNSIGNED_NUMBER(upper_bound);
  4885. if (upper_bound < 0)
  4886. upper_bound = RE_DUP_MAX;
  4887. } else
  4888. /* Interval such as `{1}' => match exactly once. */
  4889. upper_bound = lower_bound;
  4890. if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  4891. || lower_bound > upper_bound) {
  4892. if (syntax & RE_NO_BK_BRACES)
  4893. goto unfetch_interval;
  4894. else
  4895. return REG_BADBR;
  4896. }
  4897. if (!(syntax & RE_NO_BK_BRACES)) {
  4898. if (c != '\\')
  4899. return REG_EBRACE;
  4900. PATFETCH(c);
  4901. }
  4902. if (c != '}') {
  4903. if (syntax & RE_NO_BK_BRACES)
  4904. goto unfetch_interval;
  4905. else
  4906. return REG_BADBR;
  4907. }
  4908. /* We just parsed a valid interval. */
  4909. /* If it's invalid to have no preceding re. */
  4910. if (pointless_if_repeated(*last_expression, params)) {
  4911. if (syntax & RE_CONTEXT_INVALID_OPS)
  4912. return REG_BADRPT;
  4913. else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  4914. goto unfetch_interval;
  4915. /* was: else laststart = b; */
  4916. }
  4917. /* If the upper bound is zero, don't want to iterate
  4918. * at all.
  4919. */
  4920. if (upper_bound == 0) {
  4921. if (*last_expression) {
  4922. rx_free_rexp(&rxb->rx, *last_expression);
  4923. *last_expression = 0;
  4924. }
  4925. } else
  4926. /* Otherwise, we have a nontrivial interval. */
  4927. {
  4928. int iter_se = paramc;
  4929. int end_se = paramc + 1;
  4930. params = (params ? ((struct re_se_params *)
  4931. realloc(params,
  4932. sizeof(*params) * (2 +
  4933. paramc)))
  4934. : ((struct re_se_params *)
  4935. malloc(2 * sizeof(*params))));
  4936. if (!params)
  4937. return REG_ESPACE;
  4938. paramc += 2;
  4939. params[iter_se].se = re_se_iter;
  4940. params[iter_se].op1 = lower_bound;
  4941. params[iter_se].op2 = upper_bound;
  4942. params[end_se].se = re_se_end_iter;
  4943. params[end_se].op1 = lower_bound;
  4944. params[end_se].op2 = upper_bound;
  4945. {
  4946. struct rexp_node *push0
  4947. = rx_mk_r_side_effect(&rxb->rx,
  4948. (rx_side_effect) re_se_push0);
  4949. struct rexp_node *start_one_iter
  4950. = rx_mk_r_side_effect(&rxb->rx,
  4951. (rx_side_effect) iter_se);
  4952. struct rexp_node *phase1
  4953. = rx_mk_r_concat(&rxb->rx, start_one_iter,
  4954. *last_expression);
  4955. struct rexp_node *pushback
  4956. = rx_mk_r_side_effect(&rxb->rx,
  4957. (rx_side_effect) re_se_pushback);
  4958. rx_Bitset cs = rx_cset(&rxb->rx);
  4959. struct rexp_node *lit_t;
  4960. struct rexp_node *phase2;
  4961. struct rexp_node *loop;
  4962. struct rexp_node *push_n_loop;
  4963. struct rexp_node *final_test;
  4964. struct rexp_node *full_exp;
  4965. if (!cs)
  4966. return REG_ESPACE;
  4967. lit_t = rx_mk_r_cset(&rxb->rx, cs);
  4968. phase2 =
  4969. rx_mk_r_concat(&rxb->rx, pushback, lit_t);
  4970. loop =
  4971. rx_mk_r_2phase_star(&rxb->rx, phase1,
  4972. phase2);
  4973. push_n_loop =
  4974. rx_mk_r_concat(&rxb->rx, push0, loop);
  4975. final_test =
  4976. rx_mk_r_side_effect(&rxb->rx,
  4977. (rx_side_effect)
  4978. end_se);
  4979. full_exp =
  4980. rx_mk_r_concat(&rxb->rx, push_n_loop,
  4981. final_test);
  4982. if (!(push0 && start_one_iter && phase1
  4983. && pushback && lit_t && phase2
  4984. && loop && push_n_loop && final_test
  4985. && full_exp)) return REG_ESPACE;
  4986. RX_bitset_enjoin(cs, 't');
  4987. *last_expression = full_exp;
  4988. }
  4989. }
  4990. beg_interval = 0;
  4991. }
  4992. break;
  4993. unfetch_interval:
  4994. /* If an invalid interval, match the characters as literals. */
  4995. p = beg_interval;
  4996. beg_interval = 0;
  4997. /* normal_char and normal_backslash need `c'. */
  4998. PATFETCH(c);
  4999. if (!(syntax & RE_NO_BK_BRACES)) {
  5000. if (p > pattern && p[-1] == '\\')
  5001. goto normal_backslash;
  5002. }
  5003. goto normal_char;
  5004. #ifdef emacs
  5005. /* There is no way to specify the before_dot and after_dot
  5006. operators. rms says this is ok. --karl */
  5007. case '=':
  5008. side = (rx_side_effect) rx_se_at_dot;
  5009. goto add_side_effect;
  5010. break;
  5011. case 's':
  5012. case 'S':
  5013. {
  5014. rx_Bitset cs = rx_cset(&rxb->rx);
  5015. struct rexp_node *set = rx_mk_r_cset(&rxb->rx, cs);
  5016. if (!(cs && set))
  5017. return REG_ESPACE;
  5018. if (c == 'S')
  5019. rx_bitset_universe(rxb->rx.local_cset_size, cs);
  5020. PATFETCH(c);
  5021. {
  5022. int x;
  5023. enum syntaxcode code = syntax_spec_code[c];
  5024. for (x = 0; x < 256; ++x) {
  5025. if (SYNTAX(x) == code) {
  5026. rx_Bitset it =
  5027. inverse_translation(rxb, validate_inv_tr,
  5028. inverse_translate,
  5029. translate, x);
  5030. rx_bitset_xor(rxb->rx.local_cset_size, cs, it);
  5031. }
  5032. }
  5033. }
  5034. append = set;
  5035. goto append_node;
  5036. }
  5037. break;
  5038. #endif /* emacs */
  5039. case 'w':
  5040. case 'W':
  5041. if (syntax & RE_NO_GNU_OPS)
  5042. goto normal_char;
  5043. {
  5044. rx_Bitset cs = rx_cset(&rxb->rx);
  5045. struct rexp_node *n =
  5046. (cs ? rx_mk_r_cset(&rxb->rx, cs) : 0);
  5047. if (!(cs && n))
  5048. return REG_ESPACE;
  5049. if (c == 'W')
  5050. rx_bitset_universe(rxb->rx.local_cset_size, cs);
  5051. {
  5052. int x;
  5053. for (x = rxb->rx.local_cset_size - 1; x > 0; --x)
  5054. if (SYNTAX(x) & Sword)
  5055. RX_bitset_toggle(cs, x);
  5056. }
  5057. append = n;
  5058. goto append_node;
  5059. }
  5060. break;
  5061. /* With a little extra work, some of these side effects could be optimized
  5062. * away (basicly by looking at what we already know about the surrounding
  5063. * chars).
  5064. */
  5065. case '<':
  5066. if (syntax & RE_NO_GNU_OPS)
  5067. goto normal_char;
  5068. side = (rx_side_effect) re_se_wordbeg;
  5069. goto add_side_effect;
  5070. break;
  5071. case '>':
  5072. if (syntax & RE_NO_GNU_OPS)
  5073. goto normal_char;
  5074. side = (rx_side_effect) re_se_wordend;
  5075. goto add_side_effect;
  5076. break;
  5077. case 'b':
  5078. if (syntax & RE_NO_GNU_OPS)
  5079. goto normal_char;
  5080. side = (rx_side_effect) re_se_wordbound;
  5081. goto add_side_effect;
  5082. break;
  5083. case 'B':
  5084. if (syntax & RE_NO_GNU_OPS)
  5085. goto normal_char;
  5086. side = (rx_side_effect) re_se_notwordbound;
  5087. goto add_side_effect;
  5088. break;
  5089. case '`':
  5090. if (syntax & RE_NO_GNU_OPS)
  5091. goto normal_char;
  5092. side = (rx_side_effect) re_se_begbuf;
  5093. goto add_side_effect;
  5094. break;
  5095. case '\'':
  5096. if (syntax & RE_NO_GNU_OPS)
  5097. goto normal_char;
  5098. side = (rx_side_effect) re_se_endbuf;
  5099. goto add_side_effect;
  5100. break;
  5101. add_side_effect:
  5102. {
  5103. struct rexp_node *se
  5104. = rx_mk_r_side_effect(&rxb->rx, side);
  5105. if (!se)
  5106. return REG_ESPACE;
  5107. append = se;
  5108. goto append_node;
  5109. }
  5110. break;
  5111. case '1':
  5112. case '2':
  5113. case '3':
  5114. case '4':
  5115. case '5':
  5116. case '6':
  5117. case '7':
  5118. case '8':
  5119. case '9':
  5120. if (syntax & RE_NO_BK_REFS)
  5121. goto normal_char;
  5122. c1 = c - '0';
  5123. if (c1 > regnum)
  5124. return REG_ESUBREG;
  5125. /* Can't back reference to a subexpression if inside of it. */
  5126. if (group_in_compile_stack(compile_stack, c1))
  5127. return REG_ESUBREG;
  5128. {
  5129. int backref_se = paramc;
  5130. params = (params ? ((struct re_se_params *)
  5131. realloc(params,
  5132. sizeof(*params) * (1 +
  5133. paramc)))
  5134. : ((struct re_se_params *)
  5135. malloc(sizeof(*params))));
  5136. if (!params)
  5137. return REG_ESPACE;
  5138. ++paramc;
  5139. params[backref_se].se = re_se_backref;
  5140. params[backref_se].op1 = c1;
  5141. side = (rx_side_effect) backref_se;
  5142. goto add_side_effect;
  5143. }
  5144. break;
  5145. case '+':
  5146. case '?':
  5147. if (syntax & RE_BK_PLUS_QM)
  5148. goto handle_plus;
  5149. else
  5150. goto normal_backslash;
  5151. default:
  5152. normal_backslash:
  5153. /* You might think it would be useful for \ to mean
  5154. not to translate; but if we don't translate it
  5155. it will never match anything. */
  5156. c = TRANSLATE(c);
  5157. goto normal_char;
  5158. }
  5159. break;
  5160. default:
  5161. /* Expects the character in `c'. */
  5162. normal_char:
  5163. {
  5164. rx_Bitset cs = rx_cset(&rxb->rx);
  5165. struct rexp_node *match = rx_mk_r_cset(&rxb->rx, cs);
  5166. rx_Bitset it;
  5167. if (!(cs && match))
  5168. return REG_ESPACE;
  5169. it = inverse_translation(rxb, validate_inv_tr,
  5170. inverse_translate, translate, c);
  5171. rx_bitset_union(CHAR_SET_SIZE, cs, it);
  5172. append = match;
  5173. append_node:
  5174. /* This genericly appends the rexp APPEND to *LAST_EXPRESSION
  5175. * and then parses the next character normally.
  5176. */
  5177. if (*last_expression) {
  5178. struct rexp_node *concat
  5179. = rx_mk_r_concat(&rxb->rx, *last_expression, append);
  5180. if (!concat)
  5181. return REG_ESPACE;
  5182. *last_expression = concat;
  5183. last_expression = &concat->params.pair.right;
  5184. } else
  5185. *last_expression = append;
  5186. }
  5187. } /* switch (c) */
  5188. } /* while p != pend */
  5189. {
  5190. int win_se = paramc;
  5191. params = (params ? ((struct re_se_params *)
  5192. realloc(params,
  5193. sizeof(*params) * (1 + paramc)))
  5194. : ((struct re_se_params *)
  5195. malloc(sizeof(*params))));
  5196. if (!params)
  5197. return REG_ESPACE;
  5198. ++paramc;
  5199. params[win_se].se = re_se_win;
  5200. {
  5201. struct rexp_node *se
  5202. = rx_mk_r_side_effect(&rxb->rx, (rx_side_effect) win_se);
  5203. struct rexp_node *concat = rx_mk_r_concat(&rxb->rx, rexp, se);
  5204. if (!(se && concat))
  5205. return REG_ESPACE;
  5206. rexp = concat;
  5207. }
  5208. }
  5209. /* Through the pattern now. */
  5210. if (!COMPILE_STACK_EMPTY)
  5211. return REG_EPAREN;
  5212. free(compile_stack.stack);
  5213. orig_rexp = rexp;
  5214. #ifdef RX_DEBUG
  5215. if (rx_debug_compile) {
  5216. dbug_rxb = rxb;
  5217. fputs("\n\nCompiling ", stdout);
  5218. fwrite(pattern, 1, size, stdout);
  5219. fputs(":\n", stdout);
  5220. rxb->se_params = params;
  5221. print_rexp(&rxb->rx, orig_rexp, 2, re_seprint, stdout);
  5222. }
  5223. #endif
  5224. {
  5225. rx_Bitset cs = rx_cset(&rxb->rx);
  5226. rx_Bitset cs2 = rx_cset(&rxb->rx);
  5227. char *se_map = (char *) alloca(paramc);
  5228. struct rexp_node *new_rexp = 0;
  5229. bzero(se_map, paramc);
  5230. find_backrefs(se_map, rexp, params);
  5231. fewer_side_effects =
  5232. remove_unecessary_side_effects(&rxb->rx, se_map,
  5233. rx_copy_rexp(&rxb->rx, rexp),
  5234. params);
  5235. speed_up_alt(&rxb->rx, rexp, 0);
  5236. speed_up_alt(&rxb->rx, fewer_side_effects, 1);
  5237. {
  5238. char *syntax_parens = rxb->syntax_parens;
  5239. if (syntax_parens == (char *) 0x1)
  5240. rexp = remove_unecessary_side_effects
  5241. (&rxb->rx, se_map, rexp, params);
  5242. else if (syntax_parens) {
  5243. int x;
  5244. for (x = 0; x < paramc; ++x)
  5245. if (((params[x].se == re_se_lparen)
  5246. || (params[x].se == re_se_rparen))
  5247. && (!syntax_parens[params[x].op1]))
  5248. se_map[x] = 1;
  5249. rexp = remove_unecessary_side_effects
  5250. (&rxb->rx, se_map, rexp, params);
  5251. }
  5252. }
  5253. /* At least one more optimization would be nice to have here but i ran out
  5254. * of time. The idea would be to delay side effects.
  5255. * For examle, `(abc)' is the same thing as `abc()' except that the
  5256. * left paren is offset by 3 (which we know at compile time).
  5257. * (In this comment, write that second pattern `abc(:3:)'
  5258. * where `(:3:' is a syntactic unit.)
  5259. *
  5260. * Trickier: `(abc|defg)' is the same as `(abc(:3:|defg(:4:))'
  5261. * (The paren nesting may be hard to follow -- that's an alternation
  5262. * of `abc(:3:' and `defg(:4:' inside (purely syntactic) parens
  5263. * followed by the closing paren from the original expression.)
  5264. *
  5265. * Neither the expression tree representation nor the the nfa make
  5266. * this very easy to write. :(
  5267. */
  5268. /* What we compile is different than what the parser returns.
  5269. * Suppose the parser returns expression R.
  5270. * Let R' be R with unnecessary register assignments removed
  5271. * (see REMOVE_UNECESSARY_SIDE_EFFECTS, above).
  5272. *
  5273. * What we will compile is the expression:
  5274. *
  5275. * m{try}R{win}\|s{try}R'{win}
  5276. *
  5277. * {try} and {win} denote side effect epsilons (see EXPLORE_FUTURE).
  5278. *
  5279. * When trying a match, we insert an `m' at the beginning of the
  5280. * string if the user wants registers to be filled, `s' if not.
  5281. */
  5282. new_rexp =
  5283. rx_mk_r_alternate
  5284. (&rxb->rx,
  5285. rx_mk_r_concat(&rxb->rx, rx_mk_r_cset(&rxb->rx, cs2), rexp),
  5286. rx_mk_r_concat(&rxb->rx,
  5287. rx_mk_r_cset(&rxb->rx, cs),
  5288. fewer_side_effects));
  5289. if (!(new_rexp && cs && cs2))
  5290. return REG_ESPACE;
  5291. RX_bitset_enjoin(cs2, '\0'); /* prefixed to the rexp used for matching. */
  5292. RX_bitset_enjoin(cs, '\1'); /* prefixed to the rexp used for searching. */
  5293. rexp = new_rexp;
  5294. }
  5295. #ifdef RX_DEBUG
  5296. if (rx_debug_compile) {
  5297. fputs("\n...which is compiled as:\n", stdout);
  5298. print_rexp(&rxb->rx, rexp, 2, re_seprint, stdout);
  5299. }
  5300. #endif
  5301. {
  5302. struct rx_nfa_state *start = 0;
  5303. struct rx_nfa_state *end = 0;
  5304. if (!rx_build_nfa(&rxb->rx, rexp, &start, &end))
  5305. return REG_ESPACE; /* */
  5306. else {
  5307. void *mem = (void *) rxb->buffer;
  5308. unsigned long size = rxb->allocated;
  5309. int start_id;
  5310. char *perm_mem;
  5311. int iterator_size = paramc * sizeof(params[0]);
  5312. end->is_final = 1;
  5313. start->is_start = 1;
  5314. rx_name_nfa_states(&rxb->rx);
  5315. start_id = start->id;
  5316. #ifdef RX_DEBUG
  5317. if (rx_debug_compile) {
  5318. fputs("...giving the NFA: \n", stdout);
  5319. dbug_rxb = rxb;
  5320. print_nfa(&rxb->rx, rxb->rx.nfa_states, re_seprint,
  5321. stdout);
  5322. }
  5323. #endif
  5324. if (!rx_eclose_nfa(&rxb->rx))
  5325. return REG_ESPACE;
  5326. else {
  5327. rx_delete_epsilon_transitions(&rxb->rx);
  5328. /* For compatability reasons, we need to shove the
  5329. * compiled nfa into one chunk of malloced memory.
  5330. */
  5331. rxb->rx.reserved = (sizeof(params[0]) * paramc
  5332. +
  5333. rx_sizeof_bitset(rxb->
  5334. rx.local_cset_size));
  5335. #ifdef RX_DEBUG
  5336. if (rx_debug_compile) {
  5337. dbug_rxb = rxb;
  5338. fputs("...which cooks down (uncompactified) to: \n",
  5339. stdout);
  5340. print_nfa(&rxb->rx, rxb->rx.nfa_states, re_seprint,
  5341. stdout);
  5342. }
  5343. #endif
  5344. if (!rx_compactify_nfa(&rxb->rx, &mem, &size))
  5345. return REG_ESPACE;
  5346. rxb->buffer = mem;
  5347. rxb->allocated = size;
  5348. rxb->rx.buffer = mem;
  5349. rxb->rx.allocated = size;
  5350. perm_mem = ((char *) rxb->rx.buffer
  5351. + rxb->rx.allocated - rxb->rx.reserved);
  5352. rxb->se_params = ((struct re_se_params *) perm_mem);
  5353. bcopy(params, rxb->se_params, iterator_size);
  5354. perm_mem += iterator_size;
  5355. rxb->fastset = (rx_Bitset) perm_mem;
  5356. rxb->start = rx_id_to_nfa_state(&rxb->rx, start_id);
  5357. }
  5358. rx_bitset_null(rxb->rx.local_cset_size, rxb->fastset);
  5359. rxb->can_match_empty = compute_fastset(rxb, orig_rexp);
  5360. rxb->match_regs_on_stack =
  5361. registers_on_stack(rxb, orig_rexp, 0, params);
  5362. rxb->search_regs_on_stack =
  5363. registers_on_stack(rxb, fewer_side_effects, 0, params);
  5364. if (rxb->can_match_empty)
  5365. rx_bitset_universe(rxb->rx.local_cset_size, rxb->fastset);
  5366. rxb->is_anchored =
  5367. is_anchored(orig_rexp, (rx_side_effect) re_se_hat);
  5368. rxb->begbuf_only =
  5369. is_anchored(orig_rexp, (rx_side_effect) re_se_begbuf);
  5370. }
  5371. rx_free_rexp(&rxb->rx, rexp);
  5372. if (params)
  5373. free(params);
  5374. #ifdef RX_DEBUG
  5375. if (rx_debug_compile) {
  5376. dbug_rxb = rxb;
  5377. fputs("...which cooks down to: \n", stdout);
  5378. print_nfa(&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout);
  5379. }
  5380. #endif
  5381. }
  5382. return REG_NOERROR;
  5383. }
  5384. /* This table gives an error message for each of the error codes listed
  5385. in regex.h. Obviously the order here has to be same as there. */
  5386. __const__ char *rx_error_msg[] = { 0, /* REG_NOERROR */
  5387. "No match", /* REG_NOMATCH */
  5388. "Invalid regular expression", /* REG_BADPAT */
  5389. "Invalid collation character", /* REG_ECOLLATE */
  5390. "Invalid character class name", /* REG_ECTYPE */
  5391. "Trailing backslash", /* REG_EESCAPE */
  5392. "Invalid back reference", /* REG_ESUBREG */
  5393. "Unmatched [ or [^", /* REG_EBRACK */
  5394. "Unmatched ( or \\(", /* REG_EPAREN */
  5395. "Unmatched \\{", /* REG_EBRACE */
  5396. "Invalid content of \\{\\}", /* REG_BADBR */
  5397. "Invalid range end", /* REG_ERANGE */
  5398. "Memory exhausted", /* REG_ESPACE */
  5399. "Invalid preceding regular expression", /* REG_BADRPT */
  5400. "Premature end of regular expression", /* REG_EEND */
  5401. "Regular expression too big", /* REG_ESIZE */
  5402. "Unmatched ) or \\)", /* REG_ERPAREN */
  5403. };
  5404. char rx_slowmap[256] = {
  5405. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5406. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5407. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5408. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5409. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5410. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5411. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5412. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5413. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5414. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5415. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5416. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5417. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5418. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5419. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5420. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  5421. };
  5422. #ifdef __STDC__
  5423. RX_DECL void rx_blow_up_fastmap(struct re_pattern_buffer *rxb)
  5424. #else
  5425. RX_DECL void rx_blow_up_fastmap(rxb)
  5426. struct re_pattern_buffer *rxb;
  5427. #endif
  5428. {
  5429. int x;
  5430. for (x = 0; x < 256; ++x) /* &&&& 3.6 % */
  5431. rxb->fastmap[x] = !!RX_bitset_member(rxb->fastset, x);
  5432. rxb->fastmap_accurate = 1;
  5433. }
  5434. #if !defined(REGEX_MALLOC) && !defined(__GNUC__)
  5435. #define RE_SEARCH_2_FN inner_re_search_2
  5436. #define RE_S2_QUAL static
  5437. #else
  5438. #define RE_SEARCH_2_FN re_search_2
  5439. #define RE_S2_QUAL
  5440. #endif
  5441. struct re_search_2_closure {
  5442. __const__ char *string1;
  5443. int size1;
  5444. __const__ char *string2;
  5445. int size2;
  5446. };
  5447. RE_S2_QUAL int
  5448. RE_SEARCH_2_FN(struct re_pattern_buffer *,
  5449. __const__ char *,
  5450. int, __const__ char *, int, int,
  5451. int, struct re_registers *, int);
  5452. int re_rx_search(struct re_pattern_buffer *, int,
  5453. int, int, int, rx_get_burst_fn,
  5454. rx_back_check_fn, rx_fetch_char_fn,
  5455. void *, struct re_registers *,
  5456. struct rx_search_state *, struct rx_search_state *);
  5457. #if !defined(REGEX_MALLOC) && !defined(__GNUC__)
  5458. int re_search_2(struct re_pattern_buffer *,
  5459. __const__ char *, int,
  5460. __const__ char *, int,
  5461. int, int, struct re_registers *, int);
  5462. #endif
  5463. int re_search(struct re_pattern_buffer *,
  5464. __const__ char *, int, int, int, struct re_registers *);
  5465. int re_match_2(struct re_pattern_buffer *,
  5466. __const__ char *, int,
  5467. __const__ char *, int, int, struct re_registers *, int);
  5468. int re_match(struct re_pattern_buffer *,
  5469. __const__ char *, int, int, struct re_registers *);
  5470. reg_syntax_t re_set_syntax(reg_syntax_t);
  5471. void re_set_registers(struct re_pattern_buffer *,
  5472. struct re_registers *, unsigned,
  5473. regoff_t *, regoff_t *);
  5474. static int cplx_se_sublist_len(struct rx_se_list *);
  5475. static int posix_se_list_order(struct rx *, struct rx_se_list *,
  5476. struct rx_se_list *);
  5477. __const__ char
  5478. *re_compile_pattern(__const__ char *, int, struct re_pattern_buffer *);
  5479. int re_compile_fastmap(struct re_pattern_buffer *);
  5480. char *re_comp(__const__ char *);
  5481. int re_exec(__const__ char *);
  5482. int regcomp(regex_t *, __const__ char *, int);
  5483. int regexec(__const__ regex_t *,
  5484. __const__ char *, size_t, regmatch_t pmatch[], int);
  5485. size_t regerror(int, __const__ regex_t *, char *, size_t);
  5486. #ifdef __STDC__
  5487. static __inline__ enum rx_get_burst_return
  5488. re_search_2_get_burst(struct rx_string_position *pos,
  5489. void *vclosure, int stop)
  5490. #else
  5491. static __inline__ enum rx_get_burst_return
  5492. re_search_2_get_burst(pos, vclosure, stop)
  5493. struct rx_string_position *pos;
  5494. void *vclosure;
  5495. int stop;
  5496. #endif
  5497. {
  5498. struct re_search_2_closure *closure;
  5499. closure = (struct re_search_2_closure *) vclosure;
  5500. if (!closure->string2) {
  5501. int inset;
  5502. inset = pos->pos - pos->string;
  5503. if ((inset < -1) || (inset > closure->size1))
  5504. return rx_get_burst_no_more;
  5505. else {
  5506. pos->pos =
  5507. (__const__ unsigned char *) closure->string1 + inset;
  5508. pos->string = (__const__ unsigned char *) closure->string1;
  5509. pos->size = closure->size1;
  5510. pos->end = ((__const__ unsigned char *)
  5511. MIN(closure->string1 + closure->size1,
  5512. closure->string1 + stop));
  5513. pos->offset = 0;
  5514. return ((pos->pos < pos->end)
  5515. ? rx_get_burst_ok : rx_get_burst_no_more);
  5516. }
  5517. } else if (!closure->string1) {
  5518. int inset;
  5519. inset = pos->pos - pos->string;
  5520. pos->pos = (__const__ unsigned char *) closure->string2 + inset;
  5521. pos->string = (__const__ unsigned char *) closure->string2;
  5522. pos->size = closure->size2;
  5523. pos->end = ((__const__ unsigned char *)
  5524. MIN(closure->string2 + closure->size2,
  5525. closure->string2 + stop));
  5526. pos->offset = 0;
  5527. return ((pos->pos < pos->end)
  5528. ? rx_get_burst_ok : rx_get_burst_no_more);
  5529. } else {
  5530. int inset;
  5531. inset = pos->pos - pos->string + pos->offset;
  5532. if (inset < closure->size1) {
  5533. pos->pos =
  5534. (__const__ unsigned char *) closure->string1 + inset;
  5535. pos->string = (__const__ unsigned char *) closure->string1;
  5536. pos->size = closure->size1;
  5537. pos->end = ((__const__ unsigned char *)
  5538. MIN(closure->string1 + closure->size1,
  5539. closure->string1 + stop));
  5540. pos->offset = 0;
  5541. return rx_get_burst_ok;
  5542. } else {
  5543. pos->pos = ((__const__ unsigned char *)
  5544. closure->string2 + inset - closure->size1);
  5545. pos->string = (__const__ unsigned char *) closure->string2;
  5546. pos->size = closure->size2;
  5547. pos->end = ((__const__ unsigned char *)
  5548. MIN(closure->string2 + closure->size2,
  5549. closure->string2 + stop - closure->size1));
  5550. pos->offset = closure->size1;
  5551. return ((pos->pos < pos->end)
  5552. ? rx_get_burst_ok : rx_get_burst_no_more);
  5553. }
  5554. }
  5555. }
  5556. #ifdef __STDC__
  5557. static __inline__ enum rx_back_check_return
  5558. re_search_2_back_check(struct rx_string_position *pos,
  5559. int lparen, int rparen, unsigned char *translate,
  5560. void *vclosure, int stop)
  5561. #else
  5562. static __inline__ enum rx_back_check_return
  5563. re_search_2_back_check(pos, lparen, rparen, translate, vclosure, stop)
  5564. struct rx_string_position *pos;
  5565. int lparen;
  5566. int rparen;
  5567. unsigned char *translate;
  5568. void *vclosure;
  5569. int stop;
  5570. #endif
  5571. {
  5572. struct rx_string_position there;
  5573. struct rx_string_position past;
  5574. there = *pos;
  5575. there.pos = there.string + lparen - there.offset;
  5576. re_search_2_get_burst(&there, vclosure, stop);
  5577. past = *pos;
  5578. past.pos = past.string + rparen - there.offset;
  5579. re_search_2_get_burst(&past, vclosure, stop);
  5580. ++pos->pos;
  5581. re_search_2_get_burst(pos, vclosure, stop);
  5582. while ((there.pos != past.pos)
  5583. && (pos->pos != pos->end))
  5584. if (TRANSLATE(*there.pos) != TRANSLATE(*pos->pos))
  5585. return rx_back_check_fail;
  5586. else {
  5587. ++there.pos;
  5588. ++pos->pos;
  5589. if (there.pos == there.end)
  5590. re_search_2_get_burst(&there, vclosure, stop);
  5591. if (pos->pos == pos->end)
  5592. re_search_2_get_burst(pos, vclosure, stop);
  5593. }
  5594. if (there.pos != past.pos)
  5595. return rx_back_check_fail;
  5596. --pos->pos;
  5597. re_search_2_get_burst(pos, vclosure, stop);
  5598. return rx_back_check_pass;
  5599. }
  5600. #ifdef __STDC__
  5601. static __inline__ int
  5602. re_search_2_fetch_char(struct rx_string_position *pos, int offset,
  5603. void *app_closure, int stop)
  5604. #else
  5605. static __inline__ int
  5606. re_search_2_fetch_char(pos, offset, app_closure, stop)
  5607. struct rx_string_position *pos;
  5608. int offset;
  5609. void *app_closure;
  5610. int stop;
  5611. #endif
  5612. {
  5613. struct re_search_2_closure *closure;
  5614. closure = (struct re_search_2_closure *) app_closure;
  5615. if (offset == 0) {
  5616. if (pos->pos >= pos->string)
  5617. return *pos->pos;
  5618. else {
  5619. if (
  5620. (pos->string ==
  5621. (__const__ unsigned char *) closure->string2)
  5622. && (closure->string1) && (closure->size1))
  5623. return closure->string1[closure->size1 - 1];
  5624. else
  5625. return 0; /* sure, why not. */
  5626. }
  5627. }
  5628. if (pos->pos == pos->end)
  5629. return *closure->string2;
  5630. else
  5631. #if 0
  5632. return pos->pos[1];
  5633. #else
  5634. return pos->pos[offset]; /* FIXME */
  5635. #endif
  5636. }
  5637. #ifdef __STDC__
  5638. RE_S2_QUAL int
  5639. RE_SEARCH_2_FN(struct re_pattern_buffer *rxb,
  5640. __const__ char *string1, int size1,
  5641. __const__ char *string2, int size2,
  5642. int startpos, int range,
  5643. struct re_registers *regs, int stop)
  5644. #else
  5645. RE_S2_QUAL int
  5646. RE_SEARCH_2_FN(rxb,
  5647. string1, size1, string2, size2, startpos, range, regs, stop)
  5648. struct re_pattern_buffer *rxb;
  5649. __const__ char *string1;
  5650. int size1;
  5651. __const__ char *string2;
  5652. int size2;
  5653. int startpos;
  5654. int range;
  5655. struct re_registers *regs;
  5656. int stop;
  5657. #endif
  5658. {
  5659. int answer;
  5660. struct re_search_2_closure closure;
  5661. closure.string1 = string1;
  5662. closure.size1 = size1;
  5663. closure.string2 = string2;
  5664. closure.size2 = size2;
  5665. answer = rx_search(rxb, startpos, range, stop, size1 + size2,
  5666. re_search_2_get_burst,
  5667. re_search_2_back_check,
  5668. re_search_2_fetch_char,
  5669. (void *) &closure, regs, 0, 0);
  5670. switch (answer) {
  5671. case rx_search_continuation:
  5672. abort();
  5673. case rx_search_error:
  5674. return -2;
  5675. case rx_search_soft_fail:
  5676. case rx_search_fail:
  5677. return -1;
  5678. default:
  5679. return answer;
  5680. }
  5681. }
  5682. /* Export rx_search to callers outside this file. */
  5683. #ifdef __STDC__
  5684. int
  5685. re_rx_search(struct re_pattern_buffer *rxb, int startpos, int range,
  5686. int stop, int total_size, rx_get_burst_fn get_burst,
  5687. rx_back_check_fn back_check, rx_fetch_char_fn fetch_char,
  5688. void *app_closure, struct re_registers *regs,
  5689. struct rx_search_state *resume_state,
  5690. struct rx_search_state *save_state)
  5691. #else
  5692. int
  5693. re_rx_search(rxb, startpos, range, stop, total_size,
  5694. get_burst, back_check, fetch_char,
  5695. app_closure, regs, resume_state, save_state)
  5696. struct re_pattern_buffer *rxb;
  5697. int startpos;
  5698. int range;
  5699. int stop;
  5700. int total_size;
  5701. rx_get_burst_fn get_burst;
  5702. rx_back_check_fn back_check;
  5703. rx_fetch_char_fn fetch_char;
  5704. void *app_closure;
  5705. struct re_registers *regs;
  5706. struct rx_search_state *resume_state;
  5707. struct rx_search_state *save_state;
  5708. #endif
  5709. {
  5710. return rx_search(rxb, startpos, range, stop, total_size,
  5711. get_burst, back_check, fetch_char, app_closure,
  5712. regs, resume_state, save_state);
  5713. }
  5714. #if !defined(REGEX_MALLOC) && !defined(__GNUC__)
  5715. #ifdef __STDC__
  5716. int
  5717. re_search_2(struct re_pattern_buffer *rxb,
  5718. __const__ char *string1, int size1,
  5719. __const__ char *string2, int size2,
  5720. int startpos, int range, struct re_registers *regs, int stop)
  5721. #else
  5722. int
  5723. re_search_2(rxb, string1, size1, string2, size2, startpos, range, regs,
  5724. stop)
  5725. struct re_pattern_buffer *rxb;
  5726. __const__ char *string1;
  5727. int size1;
  5728. __const__ char *string2;
  5729. int size2;
  5730. int startpos;
  5731. int range;
  5732. struct re_registers *regs;
  5733. int stop;
  5734. #endif
  5735. {
  5736. int ret;
  5737. ret = inner_re_search_2(rxb, string1, size1, string2, size2, startpos,
  5738. range, regs, stop);
  5739. alloca(0);
  5740. return ret;
  5741. }
  5742. #endif
  5743. /* Like re_search_2, above, but only one string is specified, and
  5744. * doesn't let you say where to stop matching.
  5745. */
  5746. #ifdef __STDC__
  5747. int
  5748. re_search(struct re_pattern_buffer *rxb, __const__ char *string,
  5749. int size, int startpos, int range, struct re_registers *regs)
  5750. #else
  5751. int re_search(rxb, string, size, startpos, range, regs)
  5752. struct re_pattern_buffer *rxb;
  5753. __const__ char *string;
  5754. int size;
  5755. int startpos;
  5756. int range;
  5757. struct re_registers *regs;
  5758. #endif
  5759. {
  5760. return re_search_2(rxb, 0, 0, string, size, startpos, range, regs,
  5761. size);
  5762. }
  5763. #ifdef __STDC__
  5764. int
  5765. re_match_2(struct re_pattern_buffer *rxb,
  5766. __const__ char *string1, int size1,
  5767. __const__ char *string2, int size2,
  5768. int pos, struct re_registers *regs, int stop)
  5769. #else
  5770. int re_match_2(rxb, string1, size1, string2, size2, pos, regs, stop)
  5771. struct re_pattern_buffer *rxb;
  5772. __const__ char *string1;
  5773. int size1;
  5774. __const__ char *string2;
  5775. int size2;
  5776. int pos;
  5777. struct re_registers *regs;
  5778. int stop;
  5779. #endif
  5780. {
  5781. struct re_registers some_regs;
  5782. regoff_t start;
  5783. regoff_t end;
  5784. int srch;
  5785. int save = rxb->regs_allocated;
  5786. struct re_registers *regs_to_pass = regs;
  5787. char *old_fastmap = rxb->fastmap;
  5788. if (!regs) {
  5789. some_regs.start = &start;
  5790. some_regs.end = &end;
  5791. some_regs.num_regs = 1;
  5792. regs_to_pass = &some_regs;
  5793. rxb->regs_allocated = REGS_FIXED;
  5794. }
  5795. rxb->fastmap = NULL;
  5796. srch = re_search_2(rxb, string1, size1, string2, size2,
  5797. pos, 1, regs_to_pass, stop);
  5798. rxb->fastmap = old_fastmap;
  5799. if (regs_to_pass != regs)
  5800. rxb->regs_allocated = save;
  5801. if (srch < 0)
  5802. return srch;
  5803. return regs_to_pass->end[0] - regs_to_pass->start[0];
  5804. }
  5805. /* re_match is like re_match_2 except it takes only a single string. */
  5806. #ifdef __STDC__
  5807. int
  5808. re_match(struct re_pattern_buffer *rxb,
  5809. __const__ char *string,
  5810. int size, int pos, struct re_registers *regs)
  5811. #else
  5812. int re_match(rxb, string, size, pos, regs)
  5813. struct re_pattern_buffer *rxb;
  5814. __const__ char *string;
  5815. int size;
  5816. int pos;
  5817. struct re_registers *regs;
  5818. #endif
  5819. {
  5820. return re_match_2(rxb, string, size, 0, 0, pos, regs, size);
  5821. }
  5822. /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can
  5823. also be assigned to arbitrarily: each pattern buffer stores its own
  5824. syntax, so it can be changed between regex compilations. */
  5825. reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS;
  5826. /* Specify the precise syntax of regexps for compilation. This provides
  5827. for compatibility for various utilities which historically have
  5828. different, incompatible syntaxes.
  5829. The argument SYNTAX is a bit mask comprised of the various bits
  5830. defined in regex.h. We return the old syntax. */
  5831. #ifdef __STDC__
  5832. reg_syntax_t re_set_syntax(reg_syntax_t syntax)
  5833. #else
  5834. reg_syntax_t re_set_syntax(syntax)
  5835. reg_syntax_t syntax;
  5836. #endif
  5837. {
  5838. reg_syntax_t ret = re_syntax_options;
  5839. re_syntax_options = syntax;
  5840. return ret;
  5841. }
  5842. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  5843. ENDS. Subsequent matches using PATTERN_BUFFER and REGS will use
  5844. this memory for recording register information. STARTS and ENDS
  5845. must be allocated using the malloc library routine, and must each
  5846. be at least NUM_REGS * sizeof (regoff_t) bytes long.
  5847. If NUM_REGS == 0, then subsequent matches should allocate their own
  5848. register data.
  5849. Unless this function is called, the first search or match using
  5850. PATTERN_BUFFER will allocate its own register data, without
  5851. freeing the old data. */
  5852. #ifdef __STDC__
  5853. void
  5854. re_set_registers(struct re_pattern_buffer *bufp,
  5855. struct re_registers *regs,
  5856. unsigned num_regs, regoff_t * starts, regoff_t * ends)
  5857. #else
  5858. void re_set_registers(bufp, regs, num_regs, starts, ends)
  5859. struct re_pattern_buffer *bufp;
  5860. struct re_registers *regs;
  5861. unsigned num_regs;
  5862. regoff_t *starts;
  5863. regoff_t *ends;
  5864. #endif
  5865. {
  5866. if (num_regs) {
  5867. bufp->regs_allocated = REGS_REALLOCATE;
  5868. regs->num_regs = num_regs;
  5869. regs->start = starts;
  5870. regs->end = ends;
  5871. } else {
  5872. bufp->regs_allocated = REGS_UNALLOCATED;
  5873. regs->num_regs = 0;
  5874. regs->start = regs->end = (regoff_t) 0;
  5875. }
  5876. }
  5877. #ifdef __STDC__
  5878. static int cplx_se_sublist_len(struct rx_se_list *list)
  5879. #else
  5880. static int cplx_se_sublist_len(list)
  5881. struct rx_se_list *list;
  5882. #endif
  5883. {
  5884. int x = 0;
  5885. while (list) {
  5886. if ((long) list->car >= 0)
  5887. ++x;
  5888. list = list->cdr;
  5889. }
  5890. return x;
  5891. }
  5892. /* For rx->se_list_cmp */
  5893. #ifdef __STDC__
  5894. static int
  5895. posix_se_list_order(struct rx *rx,
  5896. struct rx_se_list *a, struct rx_se_list *b)
  5897. #else
  5898. static int posix_se_list_order(rx, a, b)
  5899. struct rx *rx;
  5900. struct rx_se_list *a;
  5901. struct rx_se_list *b;
  5902. #endif
  5903. {
  5904. int al = cplx_se_sublist_len(a);
  5905. int bl = cplx_se_sublist_len(b);
  5906. if (!al && !bl)
  5907. return ((a == b)
  5908. ? 0 : ((a < b) ? -1 : 1));
  5909. else if (!al)
  5910. return -1;
  5911. else if (!bl)
  5912. return 1;
  5913. else {
  5914. rx_side_effect *av = ((rx_side_effect *)
  5915. alloca(sizeof(rx_side_effect) * (al + 1)));
  5916. rx_side_effect *bv = ((rx_side_effect *)
  5917. alloca(sizeof(rx_side_effect) * (bl + 1)));
  5918. struct rx_se_list *ap = a;
  5919. struct rx_se_list *bp = b;
  5920. int ai, bi;
  5921. for (ai = al - 1; ai >= 0; --ai) {
  5922. while ((long) ap->car < 0)
  5923. ap = ap->cdr;
  5924. av[ai] = ap->car;
  5925. ap = ap->cdr;
  5926. }
  5927. av[al] = (rx_side_effect) - 2;
  5928. for (bi = bl - 1; bi >= 0; --bi) {
  5929. while ((long) bp->car < 0)
  5930. bp = bp->cdr;
  5931. bv[bi] = bp->car;
  5932. bp = bp->cdr;
  5933. }
  5934. bv[bl] = (rx_side_effect) - 1;
  5935. {
  5936. int ret;
  5937. int x = 0;
  5938. while (av[x] == bv[x])
  5939. ++x;
  5940. ret = (((unsigned *) (av[x]) < (unsigned *) (bv[x])) ? -1 : 1);
  5941. return ret;
  5942. }
  5943. }
  5944. }
  5945. /* re_compile_pattern is the GNU regular expression compiler: it
  5946. compiles PATTERN (of length SIZE) and puts the result in RXB.
  5947. Returns 0 if the pattern was valid, otherwise an error string.
  5948. Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  5949. are set in RXB on entry.
  5950. We call rx_compile to do the actual compilation. */
  5951. #ifdef __STDC__
  5952. __const__ char *re_compile_pattern(__const__ char *pattern,
  5953. int length,
  5954. struct re_pattern_buffer *rxb)
  5955. #else
  5956. __const__ char *re_compile_pattern(pattern, length, rxb)
  5957. __const__ char *pattern;
  5958. int length;
  5959. struct re_pattern_buffer *rxb;
  5960. #endif
  5961. {
  5962. reg_errcode_t ret;
  5963. /* GNU code is written to assume at least RE_NREGS registers will be set
  5964. (and at least one extra will be -1). */
  5965. rxb->regs_allocated = REGS_UNALLOCATED;
  5966. /* And GNU code determines whether or not to get register information
  5967. by passing null for the REGS argument to re_match, etc., not by
  5968. setting no_sub. */
  5969. rxb->no_sub = 0;
  5970. rxb->rx.local_cset_size = 256;
  5971. /* Match anchors at newline. */
  5972. rxb->newline_anchor = 1;
  5973. rxb->re_nsub = 0;
  5974. rxb->start = 0;
  5975. rxb->se_params = 0;
  5976. rxb->rx.nodec = 0;
  5977. rxb->rx.epsnodec = 0;
  5978. rxb->rx.instruction_table = 0;
  5979. rxb->rx.nfa_states = 0;
  5980. rxb->rx.se_list_cmp = posix_se_list_order;
  5981. rxb->rx.start_set = 0;
  5982. ret = rx_compile(pattern, length, re_syntax_options, rxb);
  5983. alloca(0);
  5984. return rx_error_msg[(int) ret];
  5985. }
  5986. #ifdef __STDC__
  5987. int re_compile_fastmap(struct re_pattern_buffer *rxb)
  5988. #else
  5989. int re_compile_fastmap(rxb)
  5990. struct re_pattern_buffer *rxb;
  5991. #endif
  5992. {
  5993. rx_blow_up_fastmap(rxb);
  5994. return 0;
  5995. }
  5996. /* Entry points compatible with 4.2 BSD regex library. We don't define
  5997. them if this is an Emacs or POSIX compilation. */
  5998. #if (!defined (emacs) && !defined (_POSIX_SOURCE)) || defined(USE_BSD_REGEX)
  5999. /* BSD has one and only one pattern buffer. */
  6000. static struct re_pattern_buffer rx_comp_buf;
  6001. #ifdef __STDC__
  6002. char *re_comp(__const__ char *s)
  6003. #else
  6004. char *re_comp(s)
  6005. __const__ char *s;
  6006. #endif
  6007. {
  6008. reg_errcode_t ret;
  6009. if (!s || (*s == '\0')) {
  6010. if (!rx_comp_buf.buffer)
  6011. return "No previous regular expression";
  6012. return 0;
  6013. }
  6014. if (!rx_comp_buf.fastmap) {
  6015. rx_comp_buf.fastmap = (char *) malloc(1 << CHARBITS);
  6016. if (!rx_comp_buf.fastmap)
  6017. return "Memory exhausted";
  6018. }
  6019. /* Since `rx_exec' always passes NULL for the `regs' argument, we
  6020. don't need to initialize the pattern buffer fields which affect it. */
  6021. /* Match anchors at newlines. */
  6022. rx_comp_buf.newline_anchor = 1;
  6023. rx_comp_buf.re_nsub = 0;
  6024. rx_comp_buf.start = 0;
  6025. rx_comp_buf.se_params = 0;
  6026. rx_comp_buf.rx.nodec = 0;
  6027. rx_comp_buf.rx.epsnodec = 0;
  6028. rx_comp_buf.rx.instruction_table = 0;
  6029. rx_comp_buf.rx.nfa_states = 0;
  6030. rx_comp_buf.rx.start = 0;
  6031. rx_comp_buf.rx.se_list_cmp = posix_se_list_order;
  6032. rx_comp_buf.rx.start_set = 0;
  6033. rx_comp_buf.rx.local_cset_size = 256;
  6034. ret = rx_compile(s, strlen(s), re_syntax_options, &rx_comp_buf);
  6035. alloca(0);
  6036. /* Yes, we're discarding `__const__' here. */
  6037. return (char *) rx_error_msg[(int) ret];
  6038. }
  6039. #ifdef __STDC__
  6040. int re_exec(__const__ char *s)
  6041. #else
  6042. int re_exec(s)
  6043. __const__ char *s;
  6044. #endif
  6045. {
  6046. __const__ int len = strlen(s);
  6047. return
  6048. 0 <= re_search(&rx_comp_buf, s, len, 0, len,
  6049. (struct re_registers *) 0);
  6050. }
  6051. #endif /* not emacs and not _POSIX_SOURCE */
  6052. /* POSIX.2 functions. Don't define these for Emacs. */
  6053. #if !defined(emacs)
  6054. /* regcomp takes a regular expression as a string and compiles it.
  6055. PREG is a regex_t *. We do not expect any fields to be initialized,
  6056. since POSIX says we shouldn't. Thus, we set
  6057. `buffer' to the compiled pattern;
  6058. `used' to the length of the compiled pattern;
  6059. `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  6060. REG_EXTENDED bit in CFLAGS is set; otherwise, to
  6061. RE_SYNTAX_POSIX_BASIC;
  6062. `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  6063. `fastmap' and `fastmap_accurate' to zero;
  6064. `re_nsub' to the number of subexpressions in PATTERN.
  6065. PATTERN is the address of the pattern string.
  6066. CFLAGS is a series of bits which affect compilation.
  6067. If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  6068. use POSIX basic syntax.
  6069. If REG_NEWLINE is set, then . and [^...] don't match newline.
  6070. Also, regexec will try a match beginning after every newline.
  6071. If REG_ICASE is set, then we considers upper- and lowercase
  6072. versions of letters to be equivalent when matching.
  6073. If REG_NOSUB is set, then when PREG is passed to regexec, that
  6074. routine will report only success or failure, and nothing about the
  6075. registers.
  6076. It returns 0 if it succeeds, nonzero if it doesn't. (See regex.h for
  6077. the return codes and their meanings.) */
  6078. #ifdef __STDC__
  6079. int regcomp(regex_t * preg, __const__ char *pattern, int cflags)
  6080. #else
  6081. int regcomp(preg, pattern, cflags)
  6082. regex_t *preg;
  6083. __const__ char *pattern;
  6084. int cflags;
  6085. #endif
  6086. {
  6087. reg_errcode_t ret;
  6088. unsigned syntax
  6089. =
  6090. cflags & REG_EXTENDED ? RE_SYNTAX_POSIX_EXTENDED :
  6091. RE_SYNTAX_POSIX_BASIC;
  6092. /* regex_compile will allocate the space for the compiled pattern. */
  6093. preg->buffer = 0;
  6094. preg->allocated = 0;
  6095. preg->fastmap = malloc(256);
  6096. if (!preg->fastmap)
  6097. return REG_ESPACE;
  6098. preg->fastmap_accurate = 0;
  6099. if (cflags & REG_ICASE) {
  6100. unsigned i;
  6101. preg->translate = (unsigned char *) malloc(256);
  6102. if (!preg->translate)
  6103. return (int) REG_ESPACE;
  6104. /* Map uppercase characters to corresponding lowercase ones. */
  6105. for (i = 0; i < CHAR_SET_SIZE; i++)
  6106. preg->translate[i] = isupper(i) ? tolower(i) : i;
  6107. } else
  6108. preg->translate = 0;
  6109. /* If REG_NEWLINE is set, newlines are treated differently. */
  6110. if (cflags & REG_NEWLINE) { /* REG_NEWLINE implies neither . nor [^...] match newline. */
  6111. syntax &= ~RE_DOT_NEWLINE;
  6112. syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  6113. /* It also changes the matching behavior. */
  6114. preg->newline_anchor = 1;
  6115. } else
  6116. preg->newline_anchor = 0;
  6117. preg->no_sub = !!(cflags & REG_NOSUB);
  6118. /* POSIX says a null character in the pattern terminates it, so we
  6119. can use strlen here in compiling the pattern. */
  6120. preg->re_nsub = 0;
  6121. preg->start = 0;
  6122. preg->se_params = 0;
  6123. preg->syntax_parens = 0;
  6124. preg->rx.nodec = 0;
  6125. preg->rx.epsnodec = 0;
  6126. preg->rx.instruction_table = 0;
  6127. preg->rx.nfa_states = 0;
  6128. preg->rx.local_cset_size = 256;
  6129. preg->rx.start = 0;
  6130. preg->rx.se_list_cmp = posix_se_list_order;
  6131. preg->rx.start_set = 0;
  6132. ret = rx_compile(pattern, strlen(pattern), syntax, preg);
  6133. alloca(0);
  6134. /* POSIX doesn't distinguish between an unmatched open-group and an
  6135. unmatched close-group: both are REG_EPAREN. */
  6136. if (ret == REG_ERPAREN)
  6137. ret = REG_EPAREN;
  6138. return (int) ret;
  6139. }
  6140. /* regexec searches for a given pattern, specified by PREG, in the
  6141. string STRING.
  6142. If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  6143. `regcomp', we ignore PMATCH. Otherwise, we assume PMATCH has at
  6144. least NMATCH elements, and we set them to the offsets of the
  6145. corresponding matched substrings.
  6146. EFLAGS specifies `execution flags' which affect matching: if
  6147. REG_NOTBOL is set, then ^ does not match at the beginning of the
  6148. string; if REG_NOTEOL is set, then $ does not match at the end.
  6149. We return 0 if we find a match and REG_NOMATCH if not. */
  6150. #ifdef __STDC__
  6151. int
  6152. regexec(__const__ regex_t * preg, __const__ char *string,
  6153. size_t nmatch, regmatch_t pmatch[], int eflags)
  6154. #else
  6155. int regexec(preg, string, nmatch, pmatch, eflags)
  6156. __const__ regex_t *preg;
  6157. __const__ char *string;
  6158. size_t nmatch;
  6159. regmatch_t pmatch[];
  6160. int eflags;
  6161. #endif
  6162. {
  6163. int ret;
  6164. struct re_registers regs;
  6165. regex_t private_preg;
  6166. int len = strlen(string);
  6167. boolean want_reg_info = !preg->no_sub && nmatch > 0;
  6168. private_preg = *preg;
  6169. private_preg.not_bol = !!(eflags & REG_NOTBOL);
  6170. private_preg.not_eol = !!(eflags & REG_NOTEOL);
  6171. /* The user has told us exactly how many registers to return
  6172. * information about, via `nmatch'. We have to pass that on to the
  6173. * matching routines.
  6174. */
  6175. private_preg.regs_allocated = REGS_FIXED;
  6176. if (want_reg_info) {
  6177. regs.num_regs = nmatch;
  6178. regs.start = ((regoff_t *) malloc((nmatch) * sizeof(regoff_t)));
  6179. regs.end = ((regoff_t *) malloc((nmatch) * sizeof(regoff_t)));
  6180. if (regs.start == 0 || regs.end == 0)
  6181. return (int) REG_NOMATCH;
  6182. }
  6183. /* Perform the searching operation. */
  6184. ret = re_search(&private_preg, string, len,
  6185. /* start: */ 0,
  6186. /* range: */ len,
  6187. want_reg_info ? &regs : (struct re_registers *) 0);
  6188. /* Copy the register information to the POSIX structure. */
  6189. if (want_reg_info) {
  6190. if (ret >= 0) {
  6191. unsigned r;
  6192. for (r = 0; r < nmatch; r++) {
  6193. pmatch[r].rm_so = regs.start[r];
  6194. pmatch[r].rm_eo = regs.end[r];
  6195. }
  6196. }
  6197. /* If we needed the temporary register info, free the space now. */
  6198. free(regs.start);
  6199. free(regs.end);
  6200. }
  6201. /* We want zero return to mean success, unlike `re_search'. */
  6202. return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  6203. }
  6204. /* Returns a message corresponding to an error code, ERRCODE, returned
  6205. from either regcomp or regexec. */
  6206. #ifdef __STDC__
  6207. size_t
  6208. regerror(int errcode, __const__ regex_t * preg,
  6209. char *errbuf, size_t errbuf_size)
  6210. #else
  6211. size_t regerror(errcode, preg, errbuf, errbuf_size)
  6212. int errcode;
  6213. __const__ regex_t *preg;
  6214. char *errbuf;
  6215. size_t errbuf_size;
  6216. #endif
  6217. {
  6218. __const__ char *msg
  6219. = rx_error_msg[errcode] == 0 ? "Success" : rx_error_msg[errcode];
  6220. size_t msg_size = strlen(msg) + 1; /* Includes the 0. */
  6221. if (errbuf_size != 0) {
  6222. if (msg_size > errbuf_size) {
  6223. strncpy(errbuf, msg, errbuf_size - 1);
  6224. errbuf[errbuf_size - 1] = 0;
  6225. } else
  6226. strcpy(errbuf, msg);
  6227. }
  6228. return msg_size;
  6229. }
  6230. /* Free dynamically allocated space used by PREG. */
  6231. #ifdef __STDC__
  6232. void regfree(regex_t * preg)
  6233. #else
  6234. void regfree(preg)
  6235. regex_t *preg;
  6236. #endif
  6237. {
  6238. if (preg->buffer != 0)
  6239. free(preg->buffer);
  6240. preg->buffer = 0;
  6241. preg->allocated = 0;
  6242. if (preg->fastmap != 0)
  6243. free(preg->fastmap);
  6244. preg->fastmap = 0;
  6245. preg->fastmap_accurate = 0;
  6246. if (preg->translate != 0)
  6247. free(preg->translate);
  6248. preg->translate = 0;
  6249. }
  6250. #endif /* not emacs */