rx.c 192 KB

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