etags.c 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719
  1. /* Tags file maker to go with GNU Emacs -*- coding: latin-1 -*-
  2. Copyright (C) 1984 The Regents of the University of California
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are
  5. met:
  6. 1. Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the
  11. distribution.
  12. 3. Neither the name of the University nor the names of its
  13. contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
  19. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  22. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  24. OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  25. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. Copyright (C) 1984, 1987-1989, 1993-1995, 1998-2012
  27. Free Software Foundation, Inc.
  28. This file is not considered part of GNU Emacs.
  29. This program is free software: you can redistribute it and/or modify
  30. it under the terms of the GNU General Public License as published by
  31. the Free Software Foundation, either version 3 of the License, or
  32. (at your option) any later version.
  33. This program is distributed in the hope that it will be useful,
  34. but WITHOUT ANY WARRANTY; without even the implied warranty of
  35. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36. GNU General Public License for more details.
  37. You should have received a copy of the GNU General Public License
  38. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  39. /* NB To comply with the above BSD license, copyright information is
  40. reproduced in etc/ETAGS.README. That file should be updated when the
  41. above notices are.
  42. To the best of our knowledge, this code was originally based on the
  43. ctags.c distributed with BSD4.2, which was copyrighted by the
  44. University of California, as described above. */
  45. /*
  46. * Authors:
  47. * 1983 Ctags originally by Ken Arnold.
  48. * 1984 Fortran added by Jim Kleckner.
  49. * 1984 Ed Pelegri-Llopart added C typedefs.
  50. * 1985 Emacs TAGS format by Richard Stallman.
  51. * 1989 Sam Kendall added C++.
  52. * 1992 Joseph B. Wells improved C and C++ parsing.
  53. * 1993 Francesco Potortì reorganized C and C++.
  54. * 1994 Line-by-line regexp tags by Tom Tromey.
  55. * 2001 Nested classes by Francesco Potortì (concept by Mykola Dzyuba).
  56. * 2002 #line directives by Francesco Potortì.
  57. *
  58. * Francesco Potortì <pot@gnu.org> has maintained and improved it since 1993.
  59. */
  60. /*
  61. * If you want to add support for a new language, start by looking at the LUA
  62. * language, which is the simplest. Alternatively, consider distributing etags
  63. * together with a configuration file containing regexp definitions for etags.
  64. */
  65. char pot_etags_version[] = "@(#) pot revision number is 17.38.1.4";
  66. #define TRUE 1
  67. #define FALSE 0
  68. #ifdef DEBUG
  69. # undef DEBUG
  70. # define DEBUG TRUE
  71. #else
  72. # define DEBUG FALSE
  73. # define NDEBUG /* disable assert */
  74. #endif
  75. #ifdef HAVE_CONFIG_H
  76. # include <config.h>
  77. /* This is probably not necessary any more. On some systems, config.h
  78. used to define static as nothing for the sake of unexec. We don't
  79. want that here since we don't use unexec. None of these systems
  80. are supported any more, but the idea is still mentioned in
  81. etc/PROBLEMS. */
  82. # undef static
  83. # ifndef PTR /* for XEmacs */
  84. # define PTR void *
  85. # endif
  86. #else /* no config.h */
  87. # if defined (__STDC__) && (__STDC__ || defined (__SUNPRO_C))
  88. # define PTR void * /* for generic pointers */
  89. # else /* not standard C */
  90. # define const /* remove const for old compilers' sake */
  91. # define PTR long * /* don't use void* */
  92. # endif
  93. #endif /* !HAVE_CONFIG_H */
  94. #ifndef _GNU_SOURCE
  95. # define _GNU_SOURCE 1 /* enables some compiler checks on GNU */
  96. #endif
  97. /* WIN32_NATIVE is for XEmacs.
  98. MSDOS, WINDOWSNT, DOS_NT are for Emacs. */
  99. #ifdef WIN32_NATIVE
  100. # undef MSDOS
  101. # undef WINDOWSNT
  102. # define WINDOWSNT
  103. #endif /* WIN32_NATIVE */
  104. #ifdef MSDOS
  105. # undef MSDOS
  106. # define MSDOS TRUE
  107. # include <fcntl.h>
  108. # include <sys/param.h>
  109. # include <io.h>
  110. # ifndef HAVE_CONFIG_H
  111. # define DOS_NT
  112. # include <sys/config.h>
  113. # endif
  114. #else
  115. # define MSDOS FALSE
  116. #endif /* MSDOS */
  117. #ifdef WINDOWSNT
  118. # include <fcntl.h>
  119. # include <direct.h>
  120. # include <io.h>
  121. # define MAXPATHLEN _MAX_PATH
  122. # undef HAVE_NTGUI
  123. # undef DOS_NT
  124. # define DOS_NT
  125. # ifndef HAVE_GETCWD
  126. # define HAVE_GETCWD
  127. # endif /* undef HAVE_GETCWD */
  128. #else /* not WINDOWSNT */
  129. #endif /* !WINDOWSNT */
  130. #include <unistd.h>
  131. #ifndef HAVE_UNISTD_H
  132. # if defined (HAVE_GETCWD) && !defined (WINDOWSNT)
  133. extern char *getcwd (char *buf, size_t size);
  134. # endif
  135. #endif /* HAVE_UNISTD_H */
  136. #include <stdlib.h>
  137. #include <string.h>
  138. #include <stdio.h>
  139. #include <ctype.h>
  140. #include <errno.h>
  141. #include <sys/types.h>
  142. #include <sys/stat.h>
  143. #include <assert.h>
  144. #ifdef NDEBUG
  145. # undef assert /* some systems have a buggy assert.h */
  146. # define assert(x) ((void) 0)
  147. #endif
  148. #ifdef NO_LONG_OPTIONS /* define this if you don't have GNU getopt */
  149. # define NO_LONG_OPTIONS TRUE
  150. # define getopt_long(argc,argv,optstr,lopts,lind) getopt (argc, argv, optstr)
  151. extern char *optarg;
  152. extern int optind, opterr;
  153. #else
  154. # define NO_LONG_OPTIONS FALSE
  155. # include <getopt.h>
  156. #endif /* NO_LONG_OPTIONS */
  157. #ifndef HAVE_CONFIG_H /* this is a standalone compilation */
  158. # ifdef __CYGWIN__ /* compiling on Cygwin */
  159. !!! NOTICE !!!
  160. the regex.h distributed with Cygwin is not compatible with etags, alas!
  161. If you want regular expression support, you should delete this notice and
  162. arrange to use the GNU regex.h and regex.c.
  163. # endif
  164. #endif
  165. #include <regex.h>
  166. /* Define CTAGS to make the program "ctags" compatible with the usual one.
  167. Leave it undefined to make the program "etags", which makes emacs-style
  168. tag tables and tags typedefs, #defines and struct/union/enum by default. */
  169. #ifdef CTAGS
  170. # undef CTAGS
  171. # define CTAGS TRUE
  172. #else
  173. # define CTAGS FALSE
  174. #endif
  175. #define streq(s,t) (assert ((s)!=NULL || (t)!=NULL), !strcmp (s, t))
  176. #define strcaseeq(s,t) (assert ((s)!=NULL && (t)!=NULL), !etags_strcasecmp (s, t))
  177. #define strneq(s,t,n) (assert ((s)!=NULL || (t)!=NULL), !strncmp (s, t, n))
  178. #define strncaseeq(s,t,n) (assert ((s)!=NULL && (t)!=NULL), !etags_strncasecmp (s, t, n))
  179. #define CHARS 256 /* 2^sizeof(char) */
  180. #define CHAR(x) ((unsigned int)(x) & (CHARS - 1))
  181. #define iswhite(c) (_wht[CHAR (c)]) /* c is white (see white) */
  182. #define notinname(c) (_nin[CHAR (c)]) /* c is not in a name (see nonam) */
  183. #define begtoken(c) (_btk[CHAR (c)]) /* c can start token (see begtk) */
  184. #define intoken(c) (_itk[CHAR (c)]) /* c can be in token (see midtk) */
  185. #define endtoken(c) (_etk[CHAR (c)]) /* c ends tokens (see endtk) */
  186. #define ISALNUM(c) isalnum (CHAR (c))
  187. #define ISALPHA(c) isalpha (CHAR (c))
  188. #define ISDIGIT(c) isdigit (CHAR (c))
  189. #define ISLOWER(c) islower (CHAR (c))
  190. #define lowcase(c) tolower (CHAR (c))
  191. /*
  192. * xnew, xrnew -- allocate, reallocate storage
  193. *
  194. * SYNOPSIS: Type *xnew (int n, Type);
  195. * void xrnew (OldPointer, int n, Type);
  196. */
  197. #if DEBUG
  198. # include "chkmalloc.h"
  199. # define xnew(n,Type) ((Type *) trace_malloc (__FILE__, __LINE__, \
  200. (n) * sizeof (Type)))
  201. # define xrnew(op,n,Type) ((op) = (Type *) trace_realloc (__FILE__, __LINE__, \
  202. (char *) (op), (n) * sizeof (Type)))
  203. #else
  204. # define xnew(n,Type) ((Type *) xmalloc ((n) * sizeof (Type)))
  205. # define xrnew(op,n,Type) ((op) = (Type *) xrealloc ( \
  206. (char *) (op), (n) * sizeof (Type)))
  207. #endif
  208. #define bool int
  209. typedef void Lang_function (FILE *);
  210. typedef struct
  211. {
  212. const char *suffix; /* file name suffix for this compressor */
  213. const char *command; /* takes one arg and decompresses to stdout */
  214. } compressor;
  215. typedef struct
  216. {
  217. const char *name; /* language name */
  218. const char *help; /* detailed help for the language */
  219. Lang_function *function; /* parse function */
  220. const char **suffixes; /* name suffixes of this language's files */
  221. const char **filenames; /* names of this language's files */
  222. const char **interpreters; /* interpreters for this language */
  223. bool metasource; /* source used to generate other sources */
  224. } language;
  225. typedef struct fdesc
  226. {
  227. struct fdesc *next; /* for the linked list */
  228. char *infname; /* uncompressed input file name */
  229. char *infabsname; /* absolute uncompressed input file name */
  230. char *infabsdir; /* absolute dir of input file */
  231. char *taggedfname; /* file name to write in tagfile */
  232. language *lang; /* language of file */
  233. char *prop; /* file properties to write in tagfile */
  234. bool usecharno; /* etags tags shall contain char number */
  235. bool written; /* entry written in the tags file */
  236. } fdesc;
  237. typedef struct node_st
  238. { /* sorting structure */
  239. struct node_st *left, *right; /* left and right sons */
  240. fdesc *fdp; /* description of file to whom tag belongs */
  241. char *name; /* tag name */
  242. char *regex; /* search regexp */
  243. bool valid; /* write this tag on the tag file */
  244. bool is_func; /* function tag: use regexp in CTAGS mode */
  245. bool been_warned; /* warning already given for duplicated tag */
  246. int lno; /* line number tag is on */
  247. long cno; /* character number line starts on */
  248. } node;
  249. /*
  250. * A `linebuffer' is a structure which holds a line of text.
  251. * `readline_internal' reads a line from a stream into a linebuffer
  252. * and works regardless of the length of the line.
  253. * SIZE is the size of BUFFER, LEN is the length of the string in
  254. * BUFFER after readline reads it.
  255. */
  256. typedef struct
  257. {
  258. long size;
  259. int len;
  260. char *buffer;
  261. } linebuffer;
  262. /* Used to support mixing of --lang and file names. */
  263. typedef struct
  264. {
  265. enum {
  266. at_language, /* a language specification */
  267. at_regexp, /* a regular expression */
  268. at_filename, /* a file name */
  269. at_stdin, /* read from stdin here */
  270. at_end /* stop parsing the list */
  271. } arg_type; /* argument type */
  272. language *lang; /* language associated with the argument */
  273. char *what; /* the argument itself */
  274. } argument;
  275. /* Structure defining a regular expression. */
  276. typedef struct regexp
  277. {
  278. struct regexp *p_next; /* pointer to next in list */
  279. language *lang; /* if set, use only for this language */
  280. char *pattern; /* the regexp pattern */
  281. char *name; /* tag name */
  282. struct re_pattern_buffer *pat; /* the compiled pattern */
  283. struct re_registers regs; /* re registers */
  284. bool error_signaled; /* already signaled for this regexp */
  285. bool force_explicit_name; /* do not allow implicit tag name */
  286. bool ignore_case; /* ignore case when matching */
  287. bool multi_line; /* do a multi-line match on the whole file */
  288. } regexp;
  289. /* Many compilers barf on this:
  290. Lang_function Ada_funcs;
  291. so let's write it this way */
  292. static void Ada_funcs (FILE *);
  293. static void Asm_labels (FILE *);
  294. static void C_entries (int c_ext, FILE *);
  295. static void default_C_entries (FILE *);
  296. static void plain_C_entries (FILE *);
  297. static void Cjava_entries (FILE *);
  298. static void Cobol_paragraphs (FILE *);
  299. static void Cplusplus_entries (FILE *);
  300. static void Cstar_entries (FILE *);
  301. static void Erlang_functions (FILE *);
  302. static void Forth_words (FILE *);
  303. static void Fortran_functions (FILE *);
  304. static void HTML_labels (FILE *);
  305. static void Lisp_functions (FILE *);
  306. static void Lua_functions (FILE *);
  307. static void Makefile_targets (FILE *);
  308. static void Pascal_functions (FILE *);
  309. static void Perl_functions (FILE *);
  310. static void PHP_functions (FILE *);
  311. static void PS_functions (FILE *);
  312. static void Prolog_functions (FILE *);
  313. static void Python_functions (FILE *);
  314. static void Scheme_functions (FILE *);
  315. static void TeX_commands (FILE *);
  316. static void Texinfo_nodes (FILE *);
  317. static void Yacc_entries (FILE *);
  318. static void just_read_file (FILE *);
  319. static void print_language_names (void);
  320. static void print_version (void);
  321. static void print_help (argument *);
  322. int main (int, char **);
  323. static compressor *get_compressor_from_suffix (char *, char **);
  324. static language *get_language_from_langname (const char *);
  325. static language *get_language_from_interpreter (char *);
  326. static language *get_language_from_filename (char *, bool);
  327. static void readline (linebuffer *, FILE *);
  328. static long readline_internal (linebuffer *, FILE *);
  329. static bool nocase_tail (const char *);
  330. static void get_tag (char *, char **);
  331. static void analyse_regex (char *);
  332. static void free_regexps (void);
  333. static void regex_tag_multiline (void);
  334. static void error (const char *, const char *);
  335. static void suggest_asking_for_help (void) NO_RETURN;
  336. void fatal (const char *, const char *) NO_RETURN;
  337. static void pfatal (const char *) NO_RETURN;
  338. static void add_node (node *, node **);
  339. static void init (void);
  340. static void process_file_name (char *, language *);
  341. static void process_file (FILE *, char *, language *);
  342. static void find_entries (FILE *);
  343. static void free_tree (node *);
  344. static void free_fdesc (fdesc *);
  345. static void pfnote (char *, bool, char *, int, int, long);
  346. static void make_tag (const char *, int, bool, char *, int, int, long);
  347. static void invalidate_nodes (fdesc *, node **);
  348. static void put_entries (node *);
  349. static char *concat (const char *, const char *, const char *);
  350. static char *skip_spaces (char *);
  351. static char *skip_non_spaces (char *);
  352. static char *savenstr (const char *, int);
  353. static char *savestr (const char *);
  354. static char *etags_strchr (const char *, int);
  355. static char *etags_strrchr (const char *, int);
  356. static int etags_strcasecmp (const char *, const char *);
  357. static int etags_strncasecmp (const char *, const char *, int);
  358. static char *etags_getcwd (void);
  359. static char *relative_filename (char *, char *);
  360. static char *absolute_filename (char *, char *);
  361. static char *absolute_dirname (char *, char *);
  362. static bool filename_is_absolute (char *f);
  363. static void canonicalize_filename (char *);
  364. static void linebuffer_init (linebuffer *);
  365. static void linebuffer_setlen (linebuffer *, int);
  366. static PTR xmalloc (size_t);
  367. static PTR xrealloc (char *, size_t);
  368. static char searchar = '/'; /* use /.../ searches */
  369. static char *tagfile; /* output file */
  370. static char *progname; /* name this program was invoked with */
  371. static char *cwd; /* current working directory */
  372. static char *tagfiledir; /* directory of tagfile */
  373. static FILE *tagf; /* ioptr for tags file */
  374. static ptrdiff_t whatlen_max; /* maximum length of any 'what' member */
  375. static fdesc *fdhead; /* head of file description list */
  376. static fdesc *curfdp; /* current file description */
  377. static int lineno; /* line number of current line */
  378. static long charno; /* current character number */
  379. static long linecharno; /* charno of start of current line */
  380. static char *dbp; /* pointer to start of current tag */
  381. static const int invalidcharno = -1;
  382. static node *nodehead; /* the head of the binary tree of tags */
  383. static node *last_node; /* the last node created */
  384. static linebuffer lb; /* the current line */
  385. static linebuffer filebuf; /* a buffer containing the whole file */
  386. static linebuffer token_name; /* a buffer containing a tag name */
  387. /* boolean "functions" (see init) */
  388. static bool _wht[CHARS], _nin[CHARS], _itk[CHARS], _btk[CHARS], _etk[CHARS];
  389. static const char
  390. /* white chars */
  391. *white = " \f\t\n\r\v",
  392. /* not in a name */
  393. *nonam = " \f\t\n\r()=,;", /* look at make_tag before modifying! */
  394. /* token ending chars */
  395. *endtk = " \t\n\r\"'#()[]{}=-+%*/&|^~!<>;,.:?",
  396. /* token starting chars */
  397. *begtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$~@",
  398. /* valid in-token chars */
  399. *midtk = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz$0123456789";
  400. static bool append_to_tagfile; /* -a: append to tags */
  401. /* The next five default to TRUE in C and derived languages. */
  402. static bool typedefs; /* -t: create tags for C and Ada typedefs */
  403. static bool typedefs_or_cplusplus; /* -T: create tags for C typedefs, level */
  404. /* 0 struct/enum/union decls, and C++ */
  405. /* member functions. */
  406. static bool constantypedefs; /* -d: create tags for C #define, enum */
  407. /* constants and variables. */
  408. /* -D: opposite of -d. Default under ctags. */
  409. static bool globals; /* create tags for global variables */
  410. static bool members; /* create tags for C member variables */
  411. static bool declarations; /* --declarations: tag them and extern in C&Co*/
  412. static bool no_line_directive; /* ignore #line directives (undocumented) */
  413. static bool no_duplicates; /* no duplicate tags for ctags (undocumented) */
  414. static bool update; /* -u: update tags */
  415. static bool vgrind_style; /* -v: create vgrind style index output */
  416. static bool no_warnings; /* -w: suppress warnings (undocumented) */
  417. static bool cxref_style; /* -x: create cxref style output */
  418. static bool cplusplus; /* .[hc] means C++, not C (undocumented) */
  419. static bool ignoreindent; /* -I: ignore indentation in C */
  420. static bool packages_only; /* --packages-only: in Ada, only tag packages*/
  421. /* STDIN is defined in LynxOS system headers */
  422. #ifdef STDIN
  423. # undef STDIN
  424. #endif
  425. #define STDIN 0x1001 /* returned by getopt_long on --parse-stdin */
  426. static bool parsing_stdin; /* --parse-stdin used */
  427. static regexp *p_head; /* list of all regexps */
  428. static bool need_filebuf; /* some regexes are multi-line */
  429. static struct option longopts[] =
  430. {
  431. { "append", no_argument, NULL, 'a' },
  432. { "packages-only", no_argument, &packages_only, TRUE },
  433. { "c++", no_argument, NULL, 'C' },
  434. { "declarations", no_argument, &declarations, TRUE },
  435. { "no-line-directive", no_argument, &no_line_directive, TRUE },
  436. { "no-duplicates", no_argument, &no_duplicates, TRUE },
  437. { "help", no_argument, NULL, 'h' },
  438. { "help", no_argument, NULL, 'H' },
  439. { "ignore-indentation", no_argument, NULL, 'I' },
  440. { "language", required_argument, NULL, 'l' },
  441. { "members", no_argument, &members, TRUE },
  442. { "no-members", no_argument, &members, FALSE },
  443. { "output", required_argument, NULL, 'o' },
  444. { "regex", required_argument, NULL, 'r' },
  445. { "no-regex", no_argument, NULL, 'R' },
  446. { "ignore-case-regex", required_argument, NULL, 'c' },
  447. { "parse-stdin", required_argument, NULL, STDIN },
  448. { "version", no_argument, NULL, 'V' },
  449. #if CTAGS /* Ctags options */
  450. { "backward-search", no_argument, NULL, 'B' },
  451. { "cxref", no_argument, NULL, 'x' },
  452. { "defines", no_argument, NULL, 'd' },
  453. { "globals", no_argument, &globals, TRUE },
  454. { "typedefs", no_argument, NULL, 't' },
  455. { "typedefs-and-c++", no_argument, NULL, 'T' },
  456. { "update", no_argument, NULL, 'u' },
  457. { "vgrind", no_argument, NULL, 'v' },
  458. { "no-warn", no_argument, NULL, 'w' },
  459. #else /* Etags options */
  460. { "no-defines", no_argument, NULL, 'D' },
  461. { "no-globals", no_argument, &globals, FALSE },
  462. { "include", required_argument, NULL, 'i' },
  463. #endif
  464. { NULL }
  465. };
  466. static compressor compressors[] =
  467. {
  468. { "z", "gzip -d -c"},
  469. { "Z", "gzip -d -c"},
  470. { "gz", "gzip -d -c"},
  471. { "GZ", "gzip -d -c"},
  472. { "bz2", "bzip2 -d -c" },
  473. { "xz", "xz -d -c" },
  474. { NULL }
  475. };
  476. /*
  477. * Language stuff.
  478. */
  479. /* Ada code */
  480. static const char *Ada_suffixes [] =
  481. { "ads", "adb", "ada", NULL };
  482. static const char Ada_help [] =
  483. "In Ada code, functions, procedures, packages, tasks and types are\n\
  484. tags. Use the `--packages-only' option to create tags for\n\
  485. packages only.\n\
  486. Ada tag names have suffixes indicating the type of entity:\n\
  487. Entity type: Qualifier:\n\
  488. ------------ ----------\n\
  489. function /f\n\
  490. procedure /p\n\
  491. package spec /s\n\
  492. package body /b\n\
  493. type /t\n\
  494. task /k\n\
  495. Thus, `M-x find-tag <RET> bidule/b <RET>' will go directly to the\n\
  496. body of the package `bidule', while `M-x find-tag <RET> bidule <RET>'\n\
  497. will just search for any tag `bidule'.";
  498. /* Assembly code */
  499. static const char *Asm_suffixes [] =
  500. { "a", /* Unix assembler */
  501. "asm", /* Microcontroller assembly */
  502. "def", /* BSO/Tasking definition includes */
  503. "inc", /* Microcontroller include files */
  504. "ins", /* Microcontroller include files */
  505. "s", "sa", /* Unix assembler */
  506. "S", /* cpp-processed Unix assembler */
  507. "src", /* BSO/Tasking C compiler output */
  508. NULL
  509. };
  510. static const char Asm_help [] =
  511. "In assembler code, labels appearing at the beginning of a line,\n\
  512. followed by a colon, are tags.";
  513. /* Note that .c and .h can be considered C++, if the --c++ flag was
  514. given, or if the `class' or `template' keywords are met inside the file.
  515. That is why default_C_entries is called for these. */
  516. static const char *default_C_suffixes [] =
  517. { "c", "h", NULL };
  518. #if CTAGS /* C help for Ctags */
  519. static const char default_C_help [] =
  520. "In C code, any C function is a tag. Use -t to tag typedefs.\n\
  521. Use -T to tag definitions of `struct', `union' and `enum'.\n\
  522. Use -d to tag `#define' macro definitions and `enum' constants.\n\
  523. Use --globals to tag global variables.\n\
  524. You can tag function declarations and external variables by\n\
  525. using `--declarations', and struct members by using `--members'.";
  526. #else /* C help for Etags */
  527. static const char default_C_help [] =
  528. "In C code, any C function or typedef is a tag, and so are\n\
  529. definitions of `struct', `union' and `enum'. `#define' macro\n\
  530. definitions and `enum' constants are tags unless you specify\n\
  531. `--no-defines'. Global variables are tags unless you specify\n\
  532. `--no-globals' and so are struct members unless you specify\n\
  533. `--no-members'. Use of `--no-globals', `--no-defines' and\n\
  534. `--no-members' can make the tags table file much smaller.\n\
  535. You can tag function declarations and external variables by\n\
  536. using `--declarations'.";
  537. #endif /* C help for Ctags and Etags */
  538. static const char *Cplusplus_suffixes [] =
  539. { "C", "c++", "cc", "cpp", "cxx", "H", "h++", "hh", "hpp", "hxx",
  540. "M", /* Objective C++ */
  541. "pdb", /* PostScript with C syntax */
  542. NULL };
  543. static const char Cplusplus_help [] =
  544. "In C++ code, all the tag constructs of C code are tagged. (Use\n\
  545. --help --lang=c --lang=c++ for full help.)\n\
  546. In addition to C tags, member functions are also recognized. Member\n\
  547. variables are recognized unless you use the `--no-members' option.\n\
  548. Tags for variables and functions in classes are named `CLASS::VARIABLE'\n\
  549. and `CLASS::FUNCTION'. `operator' definitions have tag names like\n\
  550. `operator+'.";
  551. static const char *Cjava_suffixes [] =
  552. { "java", NULL };
  553. static char Cjava_help [] =
  554. "In Java code, all the tags constructs of C and C++ code are\n\
  555. tagged. (Use --help --lang=c --lang=c++ --lang=java for full help.)";
  556. static const char *Cobol_suffixes [] =
  557. { "COB", "cob", NULL };
  558. static char Cobol_help [] =
  559. "In Cobol code, tags are paragraph names; that is, any word\n\
  560. starting in column 8 and followed by a period.";
  561. static const char *Cstar_suffixes [] =
  562. { "cs", "hs", NULL };
  563. static const char *Erlang_suffixes [] =
  564. { "erl", "hrl", NULL };
  565. static const char Erlang_help [] =
  566. "In Erlang code, the tags are the functions, records and macros\n\
  567. defined in the file.";
  568. const char *Forth_suffixes [] =
  569. { "fth", "tok", NULL };
  570. static const char Forth_help [] =
  571. "In Forth code, tags are words defined by `:',\n\
  572. constant, code, create, defer, value, variable, buffer:, field.";
  573. static const char *Fortran_suffixes [] =
  574. { "F", "f", "f90", "for", NULL };
  575. static const char Fortran_help [] =
  576. "In Fortran code, functions, subroutines and block data are tags.";
  577. static const char *HTML_suffixes [] =
  578. { "htm", "html", "shtml", NULL };
  579. static const char HTML_help [] =
  580. "In HTML input files, the tags are the `title' and the `h1', `h2',\n\
  581. `h3' headers. Also, tags are `name=' in anchors and all\n\
  582. occurrences of `id='.";
  583. static const char *Lisp_suffixes [] =
  584. { "cl", "clisp", "el", "l", "lisp", "LSP", "lsp", "ml", NULL };
  585. static const char Lisp_help [] =
  586. "In Lisp code, any function defined with `defun', any variable\n\
  587. defined with `defvar' or `defconst', and in general the first\n\
  588. argument of any expression that starts with `(def' in column zero\n\
  589. is a tag.";
  590. static const char *Lua_suffixes [] =
  591. { "lua", "LUA", NULL };
  592. static const char Lua_help [] =
  593. "In Lua scripts, all functions are tags.";
  594. static const char *Makefile_filenames [] =
  595. { "Makefile", "makefile", "GNUMakefile", "Makefile.in", "Makefile.am", NULL};
  596. static const char Makefile_help [] =
  597. "In makefiles, targets are tags; additionally, variables are tags\n\
  598. unless you specify `--no-globals'.";
  599. static const char *Objc_suffixes [] =
  600. { "lm", /* Objective lex file */
  601. "m", /* Objective C file */
  602. NULL };
  603. static const char Objc_help [] =
  604. "In Objective C code, tags include Objective C definitions for classes,\n\
  605. class categories, methods and protocols. Tags for variables and\n\
  606. functions in classes are named `CLASS::VARIABLE' and `CLASS::FUNCTION'.\n\
  607. (Use --help --lang=c --lang=objc --lang=java for full help.)";
  608. static const char *Pascal_suffixes [] =
  609. { "p", "pas", NULL };
  610. static const char Pascal_help [] =
  611. "In Pascal code, the tags are the functions and procedures defined\n\
  612. in the file.";
  613. /* " // this is for working around an Emacs highlighting bug... */
  614. static const char *Perl_suffixes [] =
  615. { "pl", "pm", NULL };
  616. static const char *Perl_interpreters [] =
  617. { "perl", "@PERL@", NULL };
  618. static const char Perl_help [] =
  619. "In Perl code, the tags are the packages, subroutines and variables\n\
  620. defined by the `package', `sub', `my' and `local' keywords. Use\n\
  621. `--globals' if you want to tag global variables. Tags for\n\
  622. subroutines are named `PACKAGE::SUB'. The name for subroutines\n\
  623. defined in the default package is `main::SUB'.";
  624. static const char *PHP_suffixes [] =
  625. { "php", "php3", "php4", NULL };
  626. static const char PHP_help [] =
  627. "In PHP code, tags are functions, classes and defines. Unless you use\n\
  628. the `--no-members' option, vars are tags too.";
  629. static const char *plain_C_suffixes [] =
  630. { "pc", /* Pro*C file */
  631. NULL };
  632. static const char *PS_suffixes [] =
  633. { "ps", "psw", NULL }; /* .psw is for PSWrap */
  634. static const char PS_help [] =
  635. "In PostScript code, the tags are the functions.";
  636. static const char *Prolog_suffixes [] =
  637. { "prolog", NULL };
  638. static const char Prolog_help [] =
  639. "In Prolog code, tags are predicates and rules at the beginning of\n\
  640. line.";
  641. static const char *Python_suffixes [] =
  642. { "py", NULL };
  643. static const char Python_help [] =
  644. "In Python code, `def' or `class' at the beginning of a line\n\
  645. generate a tag.";
  646. /* Can't do the `SCM' or `scm' prefix with a version number. */
  647. static const char *Scheme_suffixes [] =
  648. { "oak", "sch", "scheme", "SCM", "scm", "SM", "sm", "ss", "t", NULL };
  649. static const char Scheme_help [] =
  650. "In Scheme code, tags include anything defined with `def' or with a\n\
  651. construct whose name starts with `def'. They also include\n\
  652. variables set with `set!' at top level in the file.";
  653. static const char *TeX_suffixes [] =
  654. { "bib", "clo", "cls", "ltx", "sty", "TeX", "tex", NULL };
  655. static const char TeX_help [] =
  656. "In LaTeX text, the argument of any of the commands `\\chapter',\n\
  657. `\\section', `\\subsection', `\\subsubsection', `\\eqno', `\\label',\n\
  658. `\\ref', `\\cite', `\\bibitem', `\\part', `\\appendix', `\\entry',\n\
  659. `\\index', `\\def', `\\newcommand', `\\renewcommand',\n\
  660. `\\newenvironment' or `\\renewenvironment' is a tag.\n\
  661. \n\
  662. Other commands can be specified by setting the environment variable\n\
  663. `TEXTAGS' to a colon-separated list like, for example,\n\
  664. TEXTAGS=\"mycommand:myothercommand\".";
  665. static const char *Texinfo_suffixes [] =
  666. { "texi", "texinfo", "txi", NULL };
  667. static const char Texinfo_help [] =
  668. "for texinfo files, lines starting with @node are tagged.";
  669. static const char *Yacc_suffixes [] =
  670. { "y", "y++", "ym", "yxx", "yy", NULL }; /* .ym is Objective yacc file */
  671. static const char Yacc_help [] =
  672. "In Bison or Yacc input files, each rule defines as a tag the\n\
  673. nonterminal it constructs. The portions of the file that contain\n\
  674. C code are parsed as C code (use --help --lang=c --lang=yacc\n\
  675. for full help).";
  676. static const char auto_help [] =
  677. "`auto' is not a real language, it indicates to use\n\
  678. a default language for files base on file name suffix and file contents.";
  679. static const char none_help [] =
  680. "`none' is not a real language, it indicates to only do\n\
  681. regexp processing on files.";
  682. static const char no_lang_help [] =
  683. "No detailed help available for this language.";
  684. /*
  685. * Table of languages.
  686. *
  687. * It is ok for a given function to be listed under more than one
  688. * name. I just didn't.
  689. */
  690. static language lang_names [] =
  691. {
  692. { "ada", Ada_help, Ada_funcs, Ada_suffixes },
  693. { "asm", Asm_help, Asm_labels, Asm_suffixes },
  694. { "c", default_C_help, default_C_entries, default_C_suffixes },
  695. { "c++", Cplusplus_help, Cplusplus_entries, Cplusplus_suffixes },
  696. { "c*", no_lang_help, Cstar_entries, Cstar_suffixes },
  697. { "cobol", Cobol_help, Cobol_paragraphs, Cobol_suffixes },
  698. { "erlang", Erlang_help, Erlang_functions, Erlang_suffixes },
  699. { "forth", Forth_help, Forth_words, Forth_suffixes },
  700. { "fortran", Fortran_help, Fortran_functions, Fortran_suffixes },
  701. { "html", HTML_help, HTML_labels, HTML_suffixes },
  702. { "java", Cjava_help, Cjava_entries, Cjava_suffixes },
  703. { "lisp", Lisp_help, Lisp_functions, Lisp_suffixes },
  704. { "lua", Lua_help, Lua_functions, Lua_suffixes },
  705. { "makefile", Makefile_help,Makefile_targets,NULL,Makefile_filenames},
  706. { "objc", Objc_help, plain_C_entries, Objc_suffixes },
  707. { "pascal", Pascal_help, Pascal_functions, Pascal_suffixes },
  708. { "perl",Perl_help,Perl_functions,Perl_suffixes,NULL,Perl_interpreters},
  709. { "php", PHP_help, PHP_functions, PHP_suffixes },
  710. { "postscript",PS_help, PS_functions, PS_suffixes },
  711. { "proc", no_lang_help, plain_C_entries, plain_C_suffixes },
  712. { "prolog", Prolog_help, Prolog_functions, Prolog_suffixes },
  713. { "python", Python_help, Python_functions, Python_suffixes },
  714. { "scheme", Scheme_help, Scheme_functions, Scheme_suffixes },
  715. { "tex", TeX_help, TeX_commands, TeX_suffixes },
  716. { "texinfo", Texinfo_help, Texinfo_nodes, Texinfo_suffixes },
  717. { "yacc", Yacc_help,Yacc_entries,Yacc_suffixes,NULL,NULL,TRUE},
  718. { "auto", auto_help }, /* default guessing scheme */
  719. { "none", none_help, just_read_file }, /* regexp matching only */
  720. { NULL } /* end of list */
  721. };
  722. static void
  723. print_language_names (void)
  724. {
  725. language *lang;
  726. const char **name, **ext;
  727. puts ("\nThese are the currently supported languages, along with the\n\
  728. default file names and dot suffixes:");
  729. for (lang = lang_names; lang->name != NULL; lang++)
  730. {
  731. printf (" %-*s", 10, lang->name);
  732. if (lang->filenames != NULL)
  733. for (name = lang->filenames; *name != NULL; name++)
  734. printf (" %s", *name);
  735. if (lang->suffixes != NULL)
  736. for (ext = lang->suffixes; *ext != NULL; ext++)
  737. printf (" .%s", *ext);
  738. puts ("");
  739. }
  740. puts ("where `auto' means use default language for files based on file\n\
  741. name suffix, and `none' means only do regexp processing on files.\n\
  742. If no language is specified and no matching suffix is found,\n\
  743. the first line of the file is read for a sharp-bang (#!) sequence\n\
  744. followed by the name of an interpreter. If no such sequence is found,\n\
  745. Fortran is tried first; if no tags are found, C is tried next.\n\
  746. When parsing any C file, a \"class\" or \"template\" keyword\n\
  747. switches to C++.");
  748. puts ("Compressed files are supported using gzip, bzip2, and xz.\n\
  749. \n\
  750. For detailed help on a given language use, for example,\n\
  751. etags --help --lang=ada.");
  752. }
  753. #ifndef EMACS_NAME
  754. # define EMACS_NAME "standalone"
  755. #endif
  756. #ifndef VERSION
  757. # define VERSION "17.38.1.4"
  758. #endif
  759. static void
  760. print_version (void)
  761. {
  762. /* Makes it easier to update automatically. */
  763. char emacs_copyright[] = "Copyright (C) 2012 Free Software Foundation, Inc.";
  764. printf ("%s (%s %s)\n", (CTAGS) ? "ctags" : "etags", EMACS_NAME, VERSION);
  765. puts (emacs_copyright);
  766. puts ("This program is distributed under the terms in ETAGS.README");
  767. exit (EXIT_SUCCESS);
  768. }
  769. #ifndef PRINT_UNDOCUMENTED_OPTIONS_HELP
  770. # define PRINT_UNDOCUMENTED_OPTIONS_HELP FALSE
  771. #endif
  772. static void
  773. print_help (argument *argbuffer)
  774. {
  775. bool help_for_lang = FALSE;
  776. for (; argbuffer->arg_type != at_end; argbuffer++)
  777. if (argbuffer->arg_type == at_language)
  778. {
  779. if (help_for_lang)
  780. puts ("");
  781. puts (argbuffer->lang->help);
  782. help_for_lang = TRUE;
  783. }
  784. if (help_for_lang)
  785. exit (EXIT_SUCCESS);
  786. printf ("Usage: %s [options] [[regex-option ...] file-name] ...\n\
  787. \n\
  788. These are the options accepted by %s.\n", progname, progname);
  789. if (NO_LONG_OPTIONS)
  790. puts ("WARNING: long option names do not work with this executable,\n\
  791. as it is not linked with GNU getopt.");
  792. else
  793. puts ("You may use unambiguous abbreviations for the long option names.");
  794. puts (" A - as file name means read names from stdin (one per line).\n\
  795. Absolute names are stored in the output file as they are.\n\
  796. Relative ones are stored relative to the output file's directory.\n");
  797. puts ("-a, --append\n\
  798. Append tag entries to existing tags file.");
  799. puts ("--packages-only\n\
  800. For Ada files, only generate tags for packages.");
  801. if (CTAGS)
  802. puts ("-B, --backward-search\n\
  803. Write the search commands for the tag entries using '?', the\n\
  804. backward-search command instead of '/', the forward-search command.");
  805. /* This option is mostly obsolete, because etags can now automatically
  806. detect C++. Retained for backward compatibility and for debugging and
  807. experimentation. In principle, we could want to tag as C++ even
  808. before any "class" or "template" keyword.
  809. puts ("-C, --c++\n\
  810. Treat files whose name suffix defaults to C language as C++ files.");
  811. */
  812. puts ("--declarations\n\
  813. In C and derived languages, create tags for function declarations,");
  814. if (CTAGS)
  815. puts ("\tand create tags for extern variables if --globals is used.");
  816. else
  817. puts
  818. ("\tand create tags for extern variables unless --no-globals is used.");
  819. if (CTAGS)
  820. puts ("-d, --defines\n\
  821. Create tag entries for C #define constants and enum constants, too.");
  822. else
  823. puts ("-D, --no-defines\n\
  824. Don't create tag entries for C #define constants and enum constants.\n\
  825. This makes the tags file smaller.");
  826. if (!CTAGS)
  827. puts ("-i FILE, --include=FILE\n\
  828. Include a note in tag file indicating that, when searching for\n\
  829. a tag, one should also consult the tags file FILE after\n\
  830. checking the current file.");
  831. puts ("-l LANG, --language=LANG\n\
  832. Force the following files to be considered as written in the\n\
  833. named language up to the next --language=LANG option.");
  834. if (CTAGS)
  835. puts ("--globals\n\
  836. Create tag entries for global variables in some languages.");
  837. else
  838. puts ("--no-globals\n\
  839. Do not create tag entries for global variables in some\n\
  840. languages. This makes the tags file smaller.");
  841. if (PRINT_UNDOCUMENTED_OPTIONS_HELP)
  842. puts ("--no-line-directive\n\
  843. Ignore #line preprocessor directives in C and derived languages.");
  844. if (CTAGS)
  845. puts ("--members\n\
  846. Create tag entries for members of structures in some languages.");
  847. else
  848. puts ("--no-members\n\
  849. Do not create tag entries for members of structures\n\
  850. in some languages.");
  851. puts ("-r REGEXP, --regex=REGEXP or --regex=@regexfile\n\
  852. Make a tag for each line matching a regular expression pattern\n\
  853. in the following files. {LANGUAGE}REGEXP uses REGEXP for LANGUAGE\n\
  854. files only. REGEXFILE is a file containing one REGEXP per line.\n\
  855. REGEXP takes the form /TAGREGEXP/TAGNAME/MODS, where TAGNAME/ is\n\
  856. optional. The TAGREGEXP pattern is anchored (as if preceded by ^).");
  857. puts (" If TAGNAME/ is present, the tags created are named.\n\
  858. For example Tcl named tags can be created with:\n\
  859. --regex=\"/proc[ \\t]+\\([^ \\t]+\\)/\\1/.\".\n\
  860. MODS are optional one-letter modifiers: `i' means to ignore case,\n\
  861. `m' means to allow multi-line matches, `s' implies `m' and\n\
  862. causes dot to match any character, including newline.");
  863. puts ("-R, --no-regex\n\
  864. Don't create tags from regexps for the following files.");
  865. puts ("-I, --ignore-indentation\n\
  866. In C and C++ do not assume that a closing brace in the first\n\
  867. column is the final brace of a function or structure definition.");
  868. puts ("-o FILE, --output=FILE\n\
  869. Write the tags to FILE.");
  870. puts ("--parse-stdin=NAME\n\
  871. Read from standard input and record tags as belonging to file NAME.");
  872. if (CTAGS)
  873. {
  874. puts ("-t, --typedefs\n\
  875. Generate tag entries for C and Ada typedefs.");
  876. puts ("-T, --typedefs-and-c++\n\
  877. Generate tag entries for C typedefs, C struct/enum/union tags,\n\
  878. and C++ member functions.");
  879. }
  880. if (CTAGS)
  881. puts ("-u, --update\n\
  882. Update the tag entries for the given files, leaving tag\n\
  883. entries for other files in place. Currently, this is\n\
  884. implemented by deleting the existing entries for the given\n\
  885. files and then rewriting the new entries at the end of the\n\
  886. tags file. It is often faster to simply rebuild the entire\n\
  887. tag file than to use this.");
  888. if (CTAGS)
  889. {
  890. puts ("-v, --vgrind\n\
  891. Print on the standard output an index of items intended for\n\
  892. human consumption, similar to the output of vgrind. The index\n\
  893. is sorted, and gives the page number of each item.");
  894. if (PRINT_UNDOCUMENTED_OPTIONS_HELP)
  895. puts ("-w, --no-duplicates\n\
  896. Do not create duplicate tag entries, for compatibility with\n\
  897. traditional ctags.");
  898. if (PRINT_UNDOCUMENTED_OPTIONS_HELP)
  899. puts ("-w, --no-warn\n\
  900. Suppress warning messages about duplicate tag entries.");
  901. puts ("-x, --cxref\n\
  902. Like --vgrind, but in the style of cxref, rather than vgrind.\n\
  903. The output uses line numbers instead of page numbers, but\n\
  904. beyond that the differences are cosmetic; try both to see\n\
  905. which you like.");
  906. }
  907. puts ("-V, --version\n\
  908. Print the version of the program.\n\
  909. -h, --help\n\
  910. Print this help message.\n\
  911. Followed by one or more `--language' options prints detailed\n\
  912. help about tag generation for the specified languages.");
  913. print_language_names ();
  914. puts ("");
  915. puts ("Report bugs to bug-gnu-emacs@gnu.org");
  916. exit (EXIT_SUCCESS);
  917. }
  918. int
  919. main (int argc, char **argv)
  920. {
  921. int i;
  922. unsigned int nincluded_files;
  923. char **included_files;
  924. argument *argbuffer;
  925. int current_arg, file_count;
  926. linebuffer filename_lb;
  927. bool help_asked = FALSE;
  928. ptrdiff_t len;
  929. char *optstring;
  930. int opt;
  931. #ifdef DOS_NT
  932. _fmode = O_BINARY; /* all of files are treated as binary files */
  933. #endif /* DOS_NT */
  934. progname = argv[0];
  935. nincluded_files = 0;
  936. included_files = xnew (argc, char *);
  937. current_arg = 0;
  938. file_count = 0;
  939. /* Allocate enough no matter what happens. Overkill, but each one
  940. is small. */
  941. argbuffer = xnew (argc, argument);
  942. /*
  943. * Always find typedefs and structure tags.
  944. * Also default to find macro constants, enum constants, struct
  945. * members and global variables. Do it for both etags and ctags.
  946. */
  947. typedefs = typedefs_or_cplusplus = constantypedefs = TRUE;
  948. globals = members = TRUE;
  949. /* When the optstring begins with a '-' getopt_long does not rearrange the
  950. non-options arguments to be at the end, but leaves them alone. */
  951. optstring = concat (NO_LONG_OPTIONS ? "" : "-",
  952. "ac:Cf:Il:o:r:RSVhH",
  953. (CTAGS) ? "BxdtTuvw" : "Di:");
  954. while ((opt = getopt_long (argc, argv, optstring, longopts, NULL)) != EOF)
  955. switch (opt)
  956. {
  957. case 0:
  958. /* If getopt returns 0, then it has already processed a
  959. long-named option. We should do nothing. */
  960. break;
  961. case 1:
  962. /* This means that a file name has been seen. Record it. */
  963. argbuffer[current_arg].arg_type = at_filename;
  964. argbuffer[current_arg].what = optarg;
  965. len = strlen (optarg);
  966. if (whatlen_max < len)
  967. whatlen_max = len;
  968. ++current_arg;
  969. ++file_count;
  970. break;
  971. case STDIN:
  972. /* Parse standard input. Idea by Vivek <vivek@etla.org>. */
  973. argbuffer[current_arg].arg_type = at_stdin;
  974. argbuffer[current_arg].what = optarg;
  975. len = strlen (optarg);
  976. if (whatlen_max < len)
  977. whatlen_max = len;
  978. ++current_arg;
  979. ++file_count;
  980. if (parsing_stdin)
  981. fatal ("cannot parse standard input more than once", (char *)NULL);
  982. parsing_stdin = TRUE;
  983. break;
  984. /* Common options. */
  985. case 'a': append_to_tagfile = TRUE; break;
  986. case 'C': cplusplus = TRUE; break;
  987. case 'f': /* for compatibility with old makefiles */
  988. case 'o':
  989. if (tagfile)
  990. {
  991. error ("-o option may only be given once.", (char *)NULL);
  992. suggest_asking_for_help ();
  993. /* NOTREACHED */
  994. }
  995. tagfile = optarg;
  996. break;
  997. case 'I':
  998. case 'S': /* for backward compatibility */
  999. ignoreindent = TRUE;
  1000. break;
  1001. case 'l':
  1002. {
  1003. language *lang = get_language_from_langname (optarg);
  1004. if (lang != NULL)
  1005. {
  1006. argbuffer[current_arg].lang = lang;
  1007. argbuffer[current_arg].arg_type = at_language;
  1008. ++current_arg;
  1009. }
  1010. }
  1011. break;
  1012. case 'c':
  1013. /* Backward compatibility: support obsolete --ignore-case-regexp. */
  1014. optarg = concat (optarg, "i", ""); /* memory leak here */
  1015. /* FALLTHRU */
  1016. case 'r':
  1017. argbuffer[current_arg].arg_type = at_regexp;
  1018. argbuffer[current_arg].what = optarg;
  1019. len = strlen (optarg);
  1020. if (whatlen_max < len)
  1021. whatlen_max = len;
  1022. ++current_arg;
  1023. break;
  1024. case 'R':
  1025. argbuffer[current_arg].arg_type = at_regexp;
  1026. argbuffer[current_arg].what = NULL;
  1027. ++current_arg;
  1028. break;
  1029. case 'V':
  1030. print_version ();
  1031. break;
  1032. case 'h':
  1033. case 'H':
  1034. help_asked = TRUE;
  1035. break;
  1036. /* Etags options */
  1037. case 'D': constantypedefs = FALSE; break;
  1038. case 'i': included_files[nincluded_files++] = optarg; break;
  1039. /* Ctags options. */
  1040. case 'B': searchar = '?'; break;
  1041. case 'd': constantypedefs = TRUE; break;
  1042. case 't': typedefs = TRUE; break;
  1043. case 'T': typedefs = typedefs_or_cplusplus = TRUE; break;
  1044. case 'u': update = TRUE; break;
  1045. case 'v': vgrind_style = TRUE; /*FALLTHRU*/
  1046. case 'x': cxref_style = TRUE; break;
  1047. case 'w': no_warnings = TRUE; break;
  1048. default:
  1049. suggest_asking_for_help ();
  1050. /* NOTREACHED */
  1051. }
  1052. /* No more options. Store the rest of arguments. */
  1053. for (; optind < argc; optind++)
  1054. {
  1055. argbuffer[current_arg].arg_type = at_filename;
  1056. argbuffer[current_arg].what = argv[optind];
  1057. len = strlen (argv[optind]);
  1058. if (whatlen_max < len)
  1059. whatlen_max = len;
  1060. ++current_arg;
  1061. ++file_count;
  1062. }
  1063. argbuffer[current_arg].arg_type = at_end;
  1064. if (help_asked)
  1065. print_help (argbuffer);
  1066. /* NOTREACHED */
  1067. if (nincluded_files == 0 && file_count == 0)
  1068. {
  1069. error ("no input files specified.", (char *)NULL);
  1070. suggest_asking_for_help ();
  1071. /* NOTREACHED */
  1072. }
  1073. if (tagfile == NULL)
  1074. tagfile = savestr (CTAGS ? "tags" : "TAGS");
  1075. cwd = etags_getcwd (); /* the current working directory */
  1076. if (cwd[strlen (cwd) - 1] != '/')
  1077. {
  1078. char *oldcwd = cwd;
  1079. cwd = concat (oldcwd, "/", "");
  1080. free (oldcwd);
  1081. }
  1082. /* Compute base directory for relative file names. */
  1083. if (streq (tagfile, "-")
  1084. || strneq (tagfile, "/dev/", 5))
  1085. tagfiledir = cwd; /* relative file names are relative to cwd */
  1086. else
  1087. {
  1088. canonicalize_filename (tagfile);
  1089. tagfiledir = absolute_dirname (tagfile, cwd);
  1090. }
  1091. init (); /* set up boolean "functions" */
  1092. linebuffer_init (&lb);
  1093. linebuffer_init (&filename_lb);
  1094. linebuffer_init (&filebuf);
  1095. linebuffer_init (&token_name);
  1096. if (!CTAGS)
  1097. {
  1098. if (streq (tagfile, "-"))
  1099. {
  1100. tagf = stdout;
  1101. #ifdef DOS_NT
  1102. /* Switch redirected `stdout' to binary mode (setting `_fmode'
  1103. doesn't take effect until after `stdout' is already open). */
  1104. if (!isatty (fileno (stdout)))
  1105. setmode (fileno (stdout), O_BINARY);
  1106. #endif /* DOS_NT */
  1107. }
  1108. else
  1109. tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
  1110. if (tagf == NULL)
  1111. pfatal (tagfile);
  1112. }
  1113. /*
  1114. * Loop through files finding functions.
  1115. */
  1116. for (i = 0; i < current_arg; i++)
  1117. {
  1118. static language *lang; /* non-NULL if language is forced */
  1119. char *this_file;
  1120. switch (argbuffer[i].arg_type)
  1121. {
  1122. case at_language:
  1123. lang = argbuffer[i].lang;
  1124. break;
  1125. case at_regexp:
  1126. analyse_regex (argbuffer[i].what);
  1127. break;
  1128. case at_filename:
  1129. this_file = argbuffer[i].what;
  1130. /* Input file named "-" means read file names from stdin
  1131. (one per line) and use them. */
  1132. if (streq (this_file, "-"))
  1133. {
  1134. if (parsing_stdin)
  1135. fatal ("cannot parse standard input AND read file names from it",
  1136. (char *)NULL);
  1137. while (readline_internal (&filename_lb, stdin) > 0)
  1138. process_file_name (filename_lb.buffer, lang);
  1139. }
  1140. else
  1141. process_file_name (this_file, lang);
  1142. break;
  1143. case at_stdin:
  1144. this_file = argbuffer[i].what;
  1145. process_file (stdin, this_file, lang);
  1146. break;
  1147. }
  1148. }
  1149. free_regexps ();
  1150. free (lb.buffer);
  1151. free (filebuf.buffer);
  1152. free (token_name.buffer);
  1153. if (!CTAGS || cxref_style)
  1154. {
  1155. /* Write the remaining tags to tagf (ETAGS) or stdout (CXREF). */
  1156. put_entries (nodehead);
  1157. free_tree (nodehead);
  1158. nodehead = NULL;
  1159. if (!CTAGS)
  1160. {
  1161. fdesc *fdp;
  1162. /* Output file entries that have no tags. */
  1163. for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
  1164. if (!fdp->written)
  1165. fprintf (tagf, "\f\n%s,0\n", fdp->taggedfname);
  1166. while (nincluded_files-- > 0)
  1167. fprintf (tagf, "\f\n%s,include\n", *included_files++);
  1168. if (fclose (tagf) == EOF)
  1169. pfatal (tagfile);
  1170. }
  1171. exit (EXIT_SUCCESS);
  1172. }
  1173. /* From here on, we are in (CTAGS && !cxref_style) */
  1174. if (update)
  1175. {
  1176. char *cmd =
  1177. xmalloc (strlen (tagfile) + whatlen_max +
  1178. sizeof "mv..OTAGS;fgrep -v '\t\t' OTAGS >;rm OTAGS");
  1179. for (i = 0; i < current_arg; ++i)
  1180. {
  1181. switch (argbuffer[i].arg_type)
  1182. {
  1183. case at_filename:
  1184. case at_stdin:
  1185. break;
  1186. default:
  1187. continue; /* the for loop */
  1188. }
  1189. strcpy (cmd, "mv ");
  1190. strcat (cmd, tagfile);
  1191. strcat (cmd, " OTAGS;fgrep -v '\t");
  1192. strcat (cmd, argbuffer[i].what);
  1193. strcat (cmd, "\t' OTAGS >");
  1194. strcat (cmd, tagfile);
  1195. strcat (cmd, ";rm OTAGS");
  1196. if (system (cmd) != EXIT_SUCCESS)
  1197. fatal ("failed to execute shell command", (char *)NULL);
  1198. }
  1199. free (cmd);
  1200. append_to_tagfile = TRUE;
  1201. }
  1202. tagf = fopen (tagfile, append_to_tagfile ? "a" : "w");
  1203. if (tagf == NULL)
  1204. pfatal (tagfile);
  1205. put_entries (nodehead); /* write all the tags (CTAGS) */
  1206. free_tree (nodehead);
  1207. nodehead = NULL;
  1208. if (fclose (tagf) == EOF)
  1209. pfatal (tagfile);
  1210. if (CTAGS)
  1211. if (append_to_tagfile || update)
  1212. {
  1213. char *cmd = xmalloc (2 * strlen (tagfile) + sizeof "sort -u -o..");
  1214. /* Maybe these should be used:
  1215. setenv ("LC_COLLATE", "C", 1);
  1216. setenv ("LC_ALL", "C", 1); */
  1217. strcpy (cmd, "sort -u -o ");
  1218. strcat (cmd, tagfile);
  1219. strcat (cmd, " ");
  1220. strcat (cmd, tagfile);
  1221. exit (system (cmd));
  1222. }
  1223. return EXIT_SUCCESS;
  1224. }
  1225. /*
  1226. * Return a compressor given the file name. If EXTPTR is non-zero,
  1227. * return a pointer into FILE where the compressor-specific
  1228. * extension begins. If no compressor is found, NULL is returned
  1229. * and EXTPTR is not significant.
  1230. * Idea by Vladimir Alexiev <vladimir@cs.ualberta.ca> (1998)
  1231. */
  1232. static compressor *
  1233. get_compressor_from_suffix (char *file, char **extptr)
  1234. {
  1235. compressor *compr;
  1236. char *slash, *suffix;
  1237. /* File has been processed by canonicalize_filename,
  1238. so we don't need to consider backslashes on DOS_NT. */
  1239. slash = etags_strrchr (file, '/');
  1240. suffix = etags_strrchr (file, '.');
  1241. if (suffix == NULL || suffix < slash)
  1242. return NULL;
  1243. if (extptr != NULL)
  1244. *extptr = suffix;
  1245. suffix += 1;
  1246. /* Let those poor souls who live with DOS 8+3 file name limits get
  1247. some solace by treating foo.cgz as if it were foo.c.gz, etc.
  1248. Only the first do loop is run if not MSDOS */
  1249. do
  1250. {
  1251. for (compr = compressors; compr->suffix != NULL; compr++)
  1252. if (streq (compr->suffix, suffix))
  1253. return compr;
  1254. if (!MSDOS)
  1255. break; /* do it only once: not really a loop */
  1256. if (extptr != NULL)
  1257. *extptr = ++suffix;
  1258. } while (*suffix != '\0');
  1259. return NULL;
  1260. }
  1261. /*
  1262. * Return a language given the name.
  1263. */
  1264. static language *
  1265. get_language_from_langname (const char *name)
  1266. {
  1267. language *lang;
  1268. if (name == NULL)
  1269. error ("empty language name", (char *)NULL);
  1270. else
  1271. {
  1272. for (lang = lang_names; lang->name != NULL; lang++)
  1273. if (streq (name, lang->name))
  1274. return lang;
  1275. error ("unknown language \"%s\"", name);
  1276. }
  1277. return NULL;
  1278. }
  1279. /*
  1280. * Return a language given the interpreter name.
  1281. */
  1282. static language *
  1283. get_language_from_interpreter (char *interpreter)
  1284. {
  1285. language *lang;
  1286. const char **iname;
  1287. if (interpreter == NULL)
  1288. return NULL;
  1289. for (lang = lang_names; lang->name != NULL; lang++)
  1290. if (lang->interpreters != NULL)
  1291. for (iname = lang->interpreters; *iname != NULL; iname++)
  1292. if (streq (*iname, interpreter))
  1293. return lang;
  1294. return NULL;
  1295. }
  1296. /*
  1297. * Return a language given the file name.
  1298. */
  1299. static language *
  1300. get_language_from_filename (char *file, int case_sensitive)
  1301. {
  1302. language *lang;
  1303. const char **name, **ext, *suffix;
  1304. /* Try whole file name first. */
  1305. for (lang = lang_names; lang->name != NULL; lang++)
  1306. if (lang->filenames != NULL)
  1307. for (name = lang->filenames; *name != NULL; name++)
  1308. if ((case_sensitive)
  1309. ? streq (*name, file)
  1310. : strcaseeq (*name, file))
  1311. return lang;
  1312. /* If not found, try suffix after last dot. */
  1313. suffix = etags_strrchr (file, '.');
  1314. if (suffix == NULL)
  1315. return NULL;
  1316. suffix += 1;
  1317. for (lang = lang_names; lang->name != NULL; lang++)
  1318. if (lang->suffixes != NULL)
  1319. for (ext = lang->suffixes; *ext != NULL; ext++)
  1320. if ((case_sensitive)
  1321. ? streq (*ext, suffix)
  1322. : strcaseeq (*ext, suffix))
  1323. return lang;
  1324. return NULL;
  1325. }
  1326. /*
  1327. * This routine is called on each file argument.
  1328. */
  1329. static void
  1330. process_file_name (char *file, language *lang)
  1331. {
  1332. struct stat stat_buf;
  1333. FILE *inf;
  1334. fdesc *fdp;
  1335. compressor *compr;
  1336. char *compressed_name, *uncompressed_name;
  1337. char *ext, *real_name;
  1338. int retval;
  1339. canonicalize_filename (file);
  1340. if (streq (file, tagfile) && !streq (tagfile, "-"))
  1341. {
  1342. error ("skipping inclusion of %s in self.", file);
  1343. return;
  1344. }
  1345. if ((compr = get_compressor_from_suffix (file, &ext)) == NULL)
  1346. {
  1347. compressed_name = NULL;
  1348. real_name = uncompressed_name = savestr (file);
  1349. }
  1350. else
  1351. {
  1352. real_name = compressed_name = savestr (file);
  1353. uncompressed_name = savenstr (file, ext - file);
  1354. }
  1355. /* If the canonicalized uncompressed name
  1356. has already been dealt with, skip it silently. */
  1357. for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
  1358. {
  1359. assert (fdp->infname != NULL);
  1360. if (streq (uncompressed_name, fdp->infname))
  1361. goto cleanup;
  1362. }
  1363. if (stat (real_name, &stat_buf) != 0)
  1364. {
  1365. /* Reset real_name and try with a different name. */
  1366. real_name = NULL;
  1367. if (compressed_name != NULL) /* try with the given suffix */
  1368. {
  1369. if (stat (uncompressed_name, &stat_buf) == 0)
  1370. real_name = uncompressed_name;
  1371. }
  1372. else /* try all possible suffixes */
  1373. {
  1374. for (compr = compressors; compr->suffix != NULL; compr++)
  1375. {
  1376. compressed_name = concat (file, ".", compr->suffix);
  1377. if (stat (compressed_name, &stat_buf) != 0)
  1378. {
  1379. if (MSDOS)
  1380. {
  1381. char *suf = compressed_name + strlen (file);
  1382. size_t suflen = strlen (compr->suffix) + 1;
  1383. for ( ; suf[1]; suf++, suflen--)
  1384. {
  1385. memmove (suf, suf + 1, suflen);
  1386. if (stat (compressed_name, &stat_buf) == 0)
  1387. {
  1388. real_name = compressed_name;
  1389. break;
  1390. }
  1391. }
  1392. if (real_name != NULL)
  1393. break;
  1394. } /* MSDOS */
  1395. free (compressed_name);
  1396. compressed_name = NULL;
  1397. }
  1398. else
  1399. {
  1400. real_name = compressed_name;
  1401. break;
  1402. }
  1403. }
  1404. }
  1405. if (real_name == NULL)
  1406. {
  1407. perror (file);
  1408. goto cleanup;
  1409. }
  1410. } /* try with a different name */
  1411. if (!S_ISREG (stat_buf.st_mode))
  1412. {
  1413. error ("skipping %s: it is not a regular file.", real_name);
  1414. goto cleanup;
  1415. }
  1416. if (real_name == compressed_name)
  1417. {
  1418. char *cmd = concat (compr->command, " ", real_name);
  1419. inf = (FILE *) popen (cmd, "r");
  1420. free (cmd);
  1421. }
  1422. else
  1423. inf = fopen (real_name, "r");
  1424. if (inf == NULL)
  1425. {
  1426. perror (real_name);
  1427. goto cleanup;
  1428. }
  1429. process_file (inf, uncompressed_name, lang);
  1430. if (real_name == compressed_name)
  1431. retval = pclose (inf);
  1432. else
  1433. retval = fclose (inf);
  1434. if (retval < 0)
  1435. pfatal (file);
  1436. cleanup:
  1437. free (compressed_name);
  1438. free (uncompressed_name);
  1439. last_node = NULL;
  1440. curfdp = NULL;
  1441. return;
  1442. }
  1443. static void
  1444. process_file (FILE *fh, char *fn, language *lang)
  1445. {
  1446. static const fdesc emptyfdesc;
  1447. fdesc *fdp;
  1448. /* Create a new input file description entry. */
  1449. fdp = xnew (1, fdesc);
  1450. *fdp = emptyfdesc;
  1451. fdp->next = fdhead;
  1452. fdp->infname = savestr (fn);
  1453. fdp->lang = lang;
  1454. fdp->infabsname = absolute_filename (fn, cwd);
  1455. fdp->infabsdir = absolute_dirname (fn, cwd);
  1456. if (filename_is_absolute (fn))
  1457. {
  1458. /* An absolute file name. Canonicalize it. */
  1459. fdp->taggedfname = absolute_filename (fn, NULL);
  1460. }
  1461. else
  1462. {
  1463. /* A file name relative to cwd. Make it relative
  1464. to the directory of the tags file. */
  1465. fdp->taggedfname = relative_filename (fn, tagfiledir);
  1466. }
  1467. fdp->usecharno = TRUE; /* use char position when making tags */
  1468. fdp->prop = NULL;
  1469. fdp->written = FALSE; /* not written on tags file yet */
  1470. fdhead = fdp;
  1471. curfdp = fdhead; /* the current file description */
  1472. find_entries (fh);
  1473. /* If not Ctags, and if this is not metasource and if it contained no #line
  1474. directives, we can write the tags and free all nodes pointing to
  1475. curfdp. */
  1476. if (!CTAGS
  1477. && curfdp->usecharno /* no #line directives in this file */
  1478. && !curfdp->lang->metasource)
  1479. {
  1480. node *np, *prev;
  1481. /* Look for the head of the sublist relative to this file. See add_node
  1482. for the structure of the node tree. */
  1483. prev = NULL;
  1484. for (np = nodehead; np != NULL; prev = np, np = np->left)
  1485. if (np->fdp == curfdp)
  1486. break;
  1487. /* If we generated tags for this file, write and delete them. */
  1488. if (np != NULL)
  1489. {
  1490. /* This is the head of the last sublist, if any. The following
  1491. instructions depend on this being true. */
  1492. assert (np->left == NULL);
  1493. assert (fdhead == curfdp);
  1494. assert (last_node->fdp == curfdp);
  1495. put_entries (np); /* write tags for file curfdp->taggedfname */
  1496. free_tree (np); /* remove the written nodes */
  1497. if (prev == NULL)
  1498. nodehead = NULL; /* no nodes left */
  1499. else
  1500. prev->left = NULL; /* delete the pointer to the sublist */
  1501. }
  1502. }
  1503. }
  1504. /*
  1505. * This routine sets up the boolean pseudo-functions which work
  1506. * by setting boolean flags dependent upon the corresponding character.
  1507. * Every char which is NOT in that string is not a white char. Therefore,
  1508. * all of the array "_wht" is set to FALSE, and then the elements
  1509. * subscripted by the chars in "white" are set to TRUE. Thus "_wht"
  1510. * of a char is TRUE if it is the string "white", else FALSE.
  1511. */
  1512. static void
  1513. init (void)
  1514. {
  1515. register const char *sp;
  1516. register int i;
  1517. for (i = 0; i < CHARS; i++)
  1518. iswhite (i) = notinname (i) = begtoken (i) = intoken (i) = endtoken (i) = FALSE;
  1519. for (sp = white; *sp != '\0'; sp++) iswhite (*sp) = TRUE;
  1520. for (sp = nonam; *sp != '\0'; sp++) notinname (*sp) = TRUE;
  1521. notinname ('\0') = notinname ('\n');
  1522. for (sp = begtk; *sp != '\0'; sp++) begtoken (*sp) = TRUE;
  1523. begtoken ('\0') = begtoken ('\n');
  1524. for (sp = midtk; *sp != '\0'; sp++) intoken (*sp) = TRUE;
  1525. intoken ('\0') = intoken ('\n');
  1526. for (sp = endtk; *sp != '\0'; sp++) endtoken (*sp) = TRUE;
  1527. endtoken ('\0') = endtoken ('\n');
  1528. }
  1529. /*
  1530. * This routine opens the specified file and calls the function
  1531. * which finds the function and type definitions.
  1532. */
  1533. static void
  1534. find_entries (FILE *inf)
  1535. {
  1536. char *cp;
  1537. language *lang = curfdp->lang;
  1538. Lang_function *parser = NULL;
  1539. /* If user specified a language, use it. */
  1540. if (lang != NULL && lang->function != NULL)
  1541. {
  1542. parser = lang->function;
  1543. }
  1544. /* Else try to guess the language given the file name. */
  1545. if (parser == NULL)
  1546. {
  1547. lang = get_language_from_filename (curfdp->infname, TRUE);
  1548. if (lang != NULL && lang->function != NULL)
  1549. {
  1550. curfdp->lang = lang;
  1551. parser = lang->function;
  1552. }
  1553. }
  1554. /* Else look for sharp-bang as the first two characters. */
  1555. if (parser == NULL
  1556. && readline_internal (&lb, inf) > 0
  1557. && lb.len >= 2
  1558. && lb.buffer[0] == '#'
  1559. && lb.buffer[1] == '!')
  1560. {
  1561. char *lp;
  1562. /* Set lp to point at the first char after the last slash in the
  1563. line or, if no slashes, at the first nonblank. Then set cp to
  1564. the first successive blank and terminate the string. */
  1565. lp = etags_strrchr (lb.buffer+2, '/');
  1566. if (lp != NULL)
  1567. lp += 1;
  1568. else
  1569. lp = skip_spaces (lb.buffer + 2);
  1570. cp = skip_non_spaces (lp);
  1571. *cp = '\0';
  1572. if (strlen (lp) > 0)
  1573. {
  1574. lang = get_language_from_interpreter (lp);
  1575. if (lang != NULL && lang->function != NULL)
  1576. {
  1577. curfdp->lang = lang;
  1578. parser = lang->function;
  1579. }
  1580. }
  1581. }
  1582. /* We rewind here, even if inf may be a pipe. We fail if the
  1583. length of the first line is longer than the pipe block size,
  1584. which is unlikely. */
  1585. rewind (inf);
  1586. /* Else try to guess the language given the case insensitive file name. */
  1587. if (parser == NULL)
  1588. {
  1589. lang = get_language_from_filename (curfdp->infname, FALSE);
  1590. if (lang != NULL && lang->function != NULL)
  1591. {
  1592. curfdp->lang = lang;
  1593. parser = lang->function;
  1594. }
  1595. }
  1596. /* Else try Fortran or C. */
  1597. if (parser == NULL)
  1598. {
  1599. node *old_last_node = last_node;
  1600. curfdp->lang = get_language_from_langname ("fortran");
  1601. find_entries (inf);
  1602. if (old_last_node == last_node)
  1603. /* No Fortran entries found. Try C. */
  1604. {
  1605. /* We do not tag if rewind fails.
  1606. Only the file name will be recorded in the tags file. */
  1607. rewind (inf);
  1608. curfdp->lang = get_language_from_langname (cplusplus ? "c++" : "c");
  1609. find_entries (inf);
  1610. }
  1611. return;
  1612. }
  1613. if (!no_line_directive
  1614. && curfdp->lang != NULL && curfdp->lang->metasource)
  1615. /* It may be that this is a bingo.y file, and we already parsed a bingo.c
  1616. file, or anyway we parsed a file that is automatically generated from
  1617. this one. If this is the case, the bingo.c file contained #line
  1618. directives that generated tags pointing to this file. Let's delete
  1619. them all before parsing this file, which is the real source. */
  1620. {
  1621. fdesc **fdpp = &fdhead;
  1622. while (*fdpp != NULL)
  1623. if (*fdpp != curfdp
  1624. && streq ((*fdpp)->taggedfname, curfdp->taggedfname))
  1625. /* We found one of those! We must delete both the file description
  1626. and all tags referring to it. */
  1627. {
  1628. fdesc *badfdp = *fdpp;
  1629. /* Delete the tags referring to badfdp->taggedfname
  1630. that were obtained from badfdp->infname. */
  1631. invalidate_nodes (badfdp, &nodehead);
  1632. *fdpp = badfdp->next; /* remove the bad description from the list */
  1633. free_fdesc (badfdp);
  1634. }
  1635. else
  1636. fdpp = &(*fdpp)->next; /* advance the list pointer */
  1637. }
  1638. assert (parser != NULL);
  1639. /* Generic initializations before reading from file. */
  1640. linebuffer_setlen (&filebuf, 0); /* reset the file buffer */
  1641. /* Generic initializations before parsing file with readline. */
  1642. lineno = 0; /* reset global line number */
  1643. charno = 0; /* reset global char number */
  1644. linecharno = 0; /* reset global char number of line start */
  1645. parser (inf);
  1646. regex_tag_multiline ();
  1647. }
  1648. /*
  1649. * Check whether an implicitly named tag should be created,
  1650. * then call `pfnote'.
  1651. * NAME is a string that is internally copied by this function.
  1652. *
  1653. * TAGS format specification
  1654. * Idea by Sam Kendall <kendall@mv.mv.com> (1997)
  1655. * The following is explained in some more detail in etc/ETAGS.EBNF.
  1656. *
  1657. * make_tag creates tags with "implicit tag names" (unnamed tags)
  1658. * if the following are all true, assuming NONAM=" \f\t\n\r()=,;":
  1659. * 1. NAME does not contain any of the characters in NONAM;
  1660. * 2. LINESTART contains name as either a rightmost, or rightmost but
  1661. * one character, substring;
  1662. * 3. the character, if any, immediately before NAME in LINESTART must
  1663. * be a character in NONAM;
  1664. * 4. the character, if any, immediately after NAME in LINESTART must
  1665. * also be a character in NONAM.
  1666. *
  1667. * The implementation uses the notinname() macro, which recognizes the
  1668. * characters stored in the string `nonam'.
  1669. * etags.el needs to use the same characters that are in NONAM.
  1670. */
  1671. static void
  1672. make_tag (const char *name, /* tag name, or NULL if unnamed */
  1673. int namelen, /* tag length */
  1674. int is_func, /* tag is a function */
  1675. char *linestart, /* start of the line where tag is */
  1676. int linelen, /* length of the line where tag is */
  1677. int lno, /* line number */
  1678. long int cno) /* character number */
  1679. {
  1680. bool named = (name != NULL && namelen > 0);
  1681. char *nname = NULL;
  1682. if (!CTAGS && named) /* maybe set named to false */
  1683. /* Let's try to make an implicit tag name, that is, create an unnamed tag
  1684. such that etags.el can guess a name from it. */
  1685. {
  1686. int i;
  1687. register const char *cp = name;
  1688. for (i = 0; i < namelen; i++)
  1689. if (notinname (*cp++))
  1690. break;
  1691. if (i == namelen) /* rule #1 */
  1692. {
  1693. cp = linestart + linelen - namelen;
  1694. if (notinname (linestart[linelen-1]))
  1695. cp -= 1; /* rule #4 */
  1696. if (cp >= linestart /* rule #2 */
  1697. && (cp == linestart
  1698. || notinname (cp[-1])) /* rule #3 */
  1699. && strneq (name, cp, namelen)) /* rule #2 */
  1700. named = FALSE; /* use implicit tag name */
  1701. }
  1702. }
  1703. if (named)
  1704. nname = savenstr (name, namelen);
  1705. pfnote (nname, is_func, linestart, linelen, lno, cno);
  1706. }
  1707. /* Record a tag. */
  1708. static void
  1709. pfnote (char *name, int is_func, char *linestart, int linelen, int lno, long int cno)
  1710. /* tag name, or NULL if unnamed */
  1711. /* tag is a function */
  1712. /* start of the line where tag is */
  1713. /* length of the line where tag is */
  1714. /* line number */
  1715. /* character number */
  1716. {
  1717. register node *np;
  1718. assert (name == NULL || name[0] != '\0');
  1719. if (CTAGS && name == NULL)
  1720. return;
  1721. np = xnew (1, node);
  1722. /* If ctags mode, change name "main" to M<thisfilename>. */
  1723. if (CTAGS && !cxref_style && streq (name, "main"))
  1724. {
  1725. register char *fp = etags_strrchr (curfdp->taggedfname, '/');
  1726. np->name = concat ("M", fp == NULL ? curfdp->taggedfname : fp + 1, "");
  1727. fp = etags_strrchr (np->name, '.');
  1728. if (fp != NULL && fp[1] != '\0' && fp[2] == '\0')
  1729. fp[0] = '\0';
  1730. }
  1731. else
  1732. np->name = name;
  1733. np->valid = TRUE;
  1734. np->been_warned = FALSE;
  1735. np->fdp = curfdp;
  1736. np->is_func = is_func;
  1737. np->lno = lno;
  1738. if (np->fdp->usecharno)
  1739. /* Our char numbers are 0-base, because of C language tradition?
  1740. ctags compatibility? old versions compatibility? I don't know.
  1741. Anyway, since emacs's are 1-base we expect etags.el to take care
  1742. of the difference. If we wanted to have 1-based numbers, we would
  1743. uncomment the +1 below. */
  1744. np->cno = cno /* + 1 */ ;
  1745. else
  1746. np->cno = invalidcharno;
  1747. np->left = np->right = NULL;
  1748. if (CTAGS && !cxref_style)
  1749. {
  1750. if (strlen (linestart) < 50)
  1751. np->regex = concat (linestart, "$", "");
  1752. else
  1753. np->regex = savenstr (linestart, 50);
  1754. }
  1755. else
  1756. np->regex = savenstr (linestart, linelen);
  1757. add_node (np, &nodehead);
  1758. }
  1759. /*
  1760. * free_tree ()
  1761. * recurse on left children, iterate on right children.
  1762. */
  1763. static void
  1764. free_tree (register node *np)
  1765. {
  1766. while (np)
  1767. {
  1768. register node *node_right = np->right;
  1769. free_tree (np->left);
  1770. free (np->name);
  1771. free (np->regex);
  1772. free (np);
  1773. np = node_right;
  1774. }
  1775. }
  1776. /*
  1777. * free_fdesc ()
  1778. * delete a file description
  1779. */
  1780. static void
  1781. free_fdesc (register fdesc *fdp)
  1782. {
  1783. free (fdp->infname);
  1784. free (fdp->infabsname);
  1785. free (fdp->infabsdir);
  1786. free (fdp->taggedfname);
  1787. free (fdp->prop);
  1788. free (fdp);
  1789. }
  1790. /*
  1791. * add_node ()
  1792. * Adds a node to the tree of nodes. In etags mode, sort by file
  1793. * name. In ctags mode, sort by tag name. Make no attempt at
  1794. * balancing.
  1795. *
  1796. * add_node is the only function allowed to add nodes, so it can
  1797. * maintain state.
  1798. */
  1799. static void
  1800. add_node (node *np, node **cur_node_p)
  1801. {
  1802. register int dif;
  1803. register node *cur_node = *cur_node_p;
  1804. if (cur_node == NULL)
  1805. {
  1806. *cur_node_p = np;
  1807. last_node = np;
  1808. return;
  1809. }
  1810. if (!CTAGS)
  1811. /* Etags Mode */
  1812. {
  1813. /* For each file name, tags are in a linked sublist on the right
  1814. pointer. The first tags of different files are a linked list
  1815. on the left pointer. last_node points to the end of the last
  1816. used sublist. */
  1817. if (last_node != NULL && last_node->fdp == np->fdp)
  1818. {
  1819. /* Let's use the same sublist as the last added node. */
  1820. assert (last_node->right == NULL);
  1821. last_node->right = np;
  1822. last_node = np;
  1823. }
  1824. else if (cur_node->fdp == np->fdp)
  1825. {
  1826. /* Scanning the list we found the head of a sublist which is
  1827. good for us. Let's scan this sublist. */
  1828. add_node (np, &cur_node->right);
  1829. }
  1830. else
  1831. /* The head of this sublist is not good for us. Let's try the
  1832. next one. */
  1833. add_node (np, &cur_node->left);
  1834. } /* if ETAGS mode */
  1835. else
  1836. {
  1837. /* Ctags Mode */
  1838. dif = strcmp (np->name, cur_node->name);
  1839. /*
  1840. * If this tag name matches an existing one, then
  1841. * do not add the node, but maybe print a warning.
  1842. */
  1843. if (no_duplicates && !dif)
  1844. {
  1845. if (np->fdp == cur_node->fdp)
  1846. {
  1847. if (!no_warnings)
  1848. {
  1849. fprintf (stderr, "Duplicate entry in file %s, line %d: %s\n",
  1850. np->fdp->infname, lineno, np->name);
  1851. fprintf (stderr, "Second entry ignored\n");
  1852. }
  1853. }
  1854. else if (!cur_node->been_warned && !no_warnings)
  1855. {
  1856. fprintf
  1857. (stderr,
  1858. "Duplicate entry in files %s and %s: %s (Warning only)\n",
  1859. np->fdp->infname, cur_node->fdp->infname, np->name);
  1860. cur_node->been_warned = TRUE;
  1861. }
  1862. return;
  1863. }
  1864. /* Actually add the node */
  1865. add_node (np, dif < 0 ? &cur_node->left : &cur_node->right);
  1866. } /* if CTAGS mode */
  1867. }
  1868. /*
  1869. * invalidate_nodes ()
  1870. * Scan the node tree and invalidate all nodes pointing to the
  1871. * given file description (CTAGS case) or free them (ETAGS case).
  1872. */
  1873. static void
  1874. invalidate_nodes (fdesc *badfdp, node **npp)
  1875. {
  1876. node *np = *npp;
  1877. if (np == NULL)
  1878. return;
  1879. if (CTAGS)
  1880. {
  1881. if (np->left != NULL)
  1882. invalidate_nodes (badfdp, &np->left);
  1883. if (np->fdp == badfdp)
  1884. np->valid = FALSE;
  1885. if (np->right != NULL)
  1886. invalidate_nodes (badfdp, &np->right);
  1887. }
  1888. else
  1889. {
  1890. assert (np->fdp != NULL);
  1891. if (np->fdp == badfdp)
  1892. {
  1893. *npp = np->left; /* detach the sublist from the list */
  1894. np->left = NULL; /* isolate it */
  1895. free_tree (np); /* free it */
  1896. invalidate_nodes (badfdp, npp);
  1897. }
  1898. else
  1899. invalidate_nodes (badfdp, &np->left);
  1900. }
  1901. }
  1902. static int total_size_of_entries (node *);
  1903. static int number_len (long);
  1904. /* Length of a non-negative number's decimal representation. */
  1905. static int
  1906. number_len (long int num)
  1907. {
  1908. int len = 1;
  1909. while ((num /= 10) > 0)
  1910. len += 1;
  1911. return len;
  1912. }
  1913. /*
  1914. * Return total number of characters that put_entries will output for
  1915. * the nodes in the linked list at the right of the specified node.
  1916. * This count is irrelevant with etags.el since emacs 19.34 at least,
  1917. * but is still supplied for backward compatibility.
  1918. */
  1919. static int
  1920. total_size_of_entries (register node *np)
  1921. {
  1922. register int total = 0;
  1923. for (; np != NULL; np = np->right)
  1924. if (np->valid)
  1925. {
  1926. total += strlen (np->regex) + 1; /* pat\177 */
  1927. if (np->name != NULL)
  1928. total += strlen (np->name) + 1; /* name\001 */
  1929. total += number_len ((long) np->lno) + 1; /* lno, */
  1930. if (np->cno != invalidcharno) /* cno */
  1931. total += number_len (np->cno);
  1932. total += 1; /* newline */
  1933. }
  1934. return total;
  1935. }
  1936. static void
  1937. put_entries (register node *np)
  1938. {
  1939. register char *sp;
  1940. static fdesc *fdp = NULL;
  1941. if (np == NULL)
  1942. return;
  1943. /* Output subentries that precede this one */
  1944. if (CTAGS)
  1945. put_entries (np->left);
  1946. /* Output this entry */
  1947. if (np->valid)
  1948. {
  1949. if (!CTAGS)
  1950. {
  1951. /* Etags mode */
  1952. if (fdp != np->fdp)
  1953. {
  1954. fdp = np->fdp;
  1955. fprintf (tagf, "\f\n%s,%d\n",
  1956. fdp->taggedfname, total_size_of_entries (np));
  1957. fdp->written = TRUE;
  1958. }
  1959. fputs (np->regex, tagf);
  1960. fputc ('\177', tagf);
  1961. if (np->name != NULL)
  1962. {
  1963. fputs (np->name, tagf);
  1964. fputc ('\001', tagf);
  1965. }
  1966. fprintf (tagf, "%d,", np->lno);
  1967. if (np->cno != invalidcharno)
  1968. fprintf (tagf, "%ld", np->cno);
  1969. fputs ("\n", tagf);
  1970. }
  1971. else
  1972. {
  1973. /* Ctags mode */
  1974. if (np->name == NULL)
  1975. error ("internal error: NULL name in ctags mode.", (char *)NULL);
  1976. if (cxref_style)
  1977. {
  1978. if (vgrind_style)
  1979. fprintf (stdout, "%s %s %d\n",
  1980. np->name, np->fdp->taggedfname, (np->lno + 63) / 64);
  1981. else
  1982. fprintf (stdout, "%-16s %3d %-16s %s\n",
  1983. np->name, np->lno, np->fdp->taggedfname, np->regex);
  1984. }
  1985. else
  1986. {
  1987. fprintf (tagf, "%s\t%s\t", np->name, np->fdp->taggedfname);
  1988. if (np->is_func)
  1989. { /* function or #define macro with args */
  1990. putc (searchar, tagf);
  1991. putc ('^', tagf);
  1992. for (sp = np->regex; *sp; sp++)
  1993. {
  1994. if (*sp == '\\' || *sp == searchar)
  1995. putc ('\\', tagf);
  1996. putc (*sp, tagf);
  1997. }
  1998. putc (searchar, tagf);
  1999. }
  2000. else
  2001. { /* anything else; text pattern inadequate */
  2002. fprintf (tagf, "%d", np->lno);
  2003. }
  2004. putc ('\n', tagf);
  2005. }
  2006. }
  2007. } /* if this node contains a valid tag */
  2008. /* Output subentries that follow this one */
  2009. put_entries (np->right);
  2010. if (!CTAGS)
  2011. put_entries (np->left);
  2012. }
  2013. /* C extensions. */
  2014. #define C_EXT 0x00fff /* C extensions */
  2015. #define C_PLAIN 0x00000 /* C */
  2016. #define C_PLPL 0x00001 /* C++ */
  2017. #define C_STAR 0x00003 /* C* */
  2018. #define C_JAVA 0x00005 /* JAVA */
  2019. #define C_AUTO 0x01000 /* C, but switch to C++ if `class' is met */
  2020. #define YACC 0x10000 /* yacc file */
  2021. /*
  2022. * The C symbol tables.
  2023. */
  2024. enum sym_type
  2025. {
  2026. st_none,
  2027. st_C_objprot, st_C_objimpl, st_C_objend,
  2028. st_C_gnumacro,
  2029. st_C_ignore, st_C_attribute,
  2030. st_C_javastruct,
  2031. st_C_operator,
  2032. st_C_class, st_C_template,
  2033. st_C_struct, st_C_extern, st_C_enum, st_C_define, st_C_typedef
  2034. };
  2035. static unsigned int hash (const char *, unsigned int);
  2036. static struct C_stab_entry * in_word_set (const char *, unsigned int);
  2037. static enum sym_type C_symtype (char *, int, int);
  2038. /* Feed stuff between (but not including) %[ and %] lines to:
  2039. gperf -m 5
  2040. %[
  2041. %compare-strncmp
  2042. %enum
  2043. %struct-type
  2044. struct C_stab_entry { char *name; int c_ext; enum sym_type type; }
  2045. %%
  2046. if, 0, st_C_ignore
  2047. for, 0, st_C_ignore
  2048. while, 0, st_C_ignore
  2049. switch, 0, st_C_ignore
  2050. return, 0, st_C_ignore
  2051. __attribute__, 0, st_C_attribute
  2052. GTY, 0, st_C_attribute
  2053. @interface, 0, st_C_objprot
  2054. @protocol, 0, st_C_objprot
  2055. @implementation,0, st_C_objimpl
  2056. @end, 0, st_C_objend
  2057. import, (C_JAVA & ~C_PLPL), st_C_ignore
  2058. package, (C_JAVA & ~C_PLPL), st_C_ignore
  2059. friend, C_PLPL, st_C_ignore
  2060. extends, (C_JAVA & ~C_PLPL), st_C_javastruct
  2061. implements, (C_JAVA & ~C_PLPL), st_C_javastruct
  2062. interface, (C_JAVA & ~C_PLPL), st_C_struct
  2063. class, 0, st_C_class
  2064. namespace, C_PLPL, st_C_struct
  2065. domain, C_STAR, st_C_struct
  2066. union, 0, st_C_struct
  2067. struct, 0, st_C_struct
  2068. extern, 0, st_C_extern
  2069. enum, 0, st_C_enum
  2070. typedef, 0, st_C_typedef
  2071. define, 0, st_C_define
  2072. undef, 0, st_C_define
  2073. operator, C_PLPL, st_C_operator
  2074. template, 0, st_C_template
  2075. # DEFUN used in emacs, the next three used in glibc (SYSCALL only for mach).
  2076. DEFUN, 0, st_C_gnumacro
  2077. SYSCALL, 0, st_C_gnumacro
  2078. ENTRY, 0, st_C_gnumacro
  2079. PSEUDO, 0, st_C_gnumacro
  2080. # These are defined inside C functions, so currently they are not met.
  2081. # EXFUN used in glibc, DEFVAR_* in emacs.
  2082. #EXFUN, 0, st_C_gnumacro
  2083. #DEFVAR_, 0, st_C_gnumacro
  2084. %]
  2085. and replace lines between %< and %> with its output, then:
  2086. - remove the #if characterset check
  2087. - make in_word_set static and not inline. */
  2088. /*%<*/
  2089. /* C code produced by gperf version 3.0.1 */
  2090. /* Command-line: gperf -m 5 */
  2091. /* Computed positions: -k'2-3' */
  2092. struct C_stab_entry { const char *name; int c_ext; enum sym_type type; };
  2093. /* maximum key range = 33, duplicates = 0 */
  2094. static inline unsigned int
  2095. hash (register const char *str, register unsigned int len)
  2096. {
  2097. static unsigned char asso_values[] =
  2098. {
  2099. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2100. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2101. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2102. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2103. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2104. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2105. 35, 35, 35, 35, 35, 35, 35, 35, 35, 3,
  2106. 26, 35, 35, 35, 35, 35, 35, 35, 27, 35,
  2107. 35, 35, 35, 24, 0, 35, 35, 35, 35, 0,
  2108. 35, 35, 35, 35, 35, 1, 35, 16, 35, 6,
  2109. 23, 0, 0, 35, 22, 0, 35, 35, 5, 0,
  2110. 0, 15, 1, 35, 6, 35, 8, 19, 35, 16,
  2111. 4, 5, 35, 35, 35, 35, 35, 35, 35, 35,
  2112. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2113. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2114. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2115. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2116. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2117. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2118. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2119. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2120. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2121. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2122. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2123. 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
  2124. 35, 35, 35, 35, 35, 35
  2125. };
  2126. register int hval = len;
  2127. switch (hval)
  2128. {
  2129. default:
  2130. hval += asso_values[(unsigned char)str[2]];
  2131. /*FALLTHROUGH*/
  2132. case 2:
  2133. hval += asso_values[(unsigned char)str[1]];
  2134. break;
  2135. }
  2136. return hval;
  2137. }
  2138. static struct C_stab_entry *
  2139. in_word_set (register const char *str, register unsigned int len)
  2140. {
  2141. enum
  2142. {
  2143. TOTAL_KEYWORDS = 33,
  2144. MIN_WORD_LENGTH = 2,
  2145. MAX_WORD_LENGTH = 15,
  2146. MIN_HASH_VALUE = 2,
  2147. MAX_HASH_VALUE = 34
  2148. };
  2149. static struct C_stab_entry wordlist[] =
  2150. {
  2151. {""}, {""},
  2152. {"if", 0, st_C_ignore},
  2153. {"GTY", 0, st_C_attribute},
  2154. {"@end", 0, st_C_objend},
  2155. {"union", 0, st_C_struct},
  2156. {"define", 0, st_C_define},
  2157. {"import", (C_JAVA & ~C_PLPL), st_C_ignore},
  2158. {"template", 0, st_C_template},
  2159. {"operator", C_PLPL, st_C_operator},
  2160. {"@interface", 0, st_C_objprot},
  2161. {"implements", (C_JAVA & ~C_PLPL), st_C_javastruct},
  2162. {"friend", C_PLPL, st_C_ignore},
  2163. {"typedef", 0, st_C_typedef},
  2164. {"return", 0, st_C_ignore},
  2165. {"@implementation",0, st_C_objimpl},
  2166. {"@protocol", 0, st_C_objprot},
  2167. {"interface", (C_JAVA & ~C_PLPL), st_C_struct},
  2168. {"extern", 0, st_C_extern},
  2169. {"extends", (C_JAVA & ~C_PLPL), st_C_javastruct},
  2170. {"struct", 0, st_C_struct},
  2171. {"domain", C_STAR, st_C_struct},
  2172. {"switch", 0, st_C_ignore},
  2173. {"enum", 0, st_C_enum},
  2174. {"for", 0, st_C_ignore},
  2175. {"namespace", C_PLPL, st_C_struct},
  2176. {"class", 0, st_C_class},
  2177. {"while", 0, st_C_ignore},
  2178. {"undef", 0, st_C_define},
  2179. {"package", (C_JAVA & ~C_PLPL), st_C_ignore},
  2180. {"__attribute__", 0, st_C_attribute},
  2181. {"SYSCALL", 0, st_C_gnumacro},
  2182. {"ENTRY", 0, st_C_gnumacro},
  2183. {"PSEUDO", 0, st_C_gnumacro},
  2184. {"DEFUN", 0, st_C_gnumacro}
  2185. };
  2186. if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
  2187. {
  2188. register int key = hash (str, len);
  2189. if (key <= MAX_HASH_VALUE && key >= 0)
  2190. {
  2191. register const char *s = wordlist[key].name;
  2192. if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')
  2193. return &wordlist[key];
  2194. }
  2195. }
  2196. return 0;
  2197. }
  2198. /*%>*/
  2199. static enum sym_type
  2200. C_symtype (char *str, int len, int c_ext)
  2201. {
  2202. register struct C_stab_entry *se = in_word_set (str, len);
  2203. if (se == NULL || (se->c_ext && !(c_ext & se->c_ext)))
  2204. return st_none;
  2205. return se->type;
  2206. }
  2207. /*
  2208. * Ignoring __attribute__ ((list))
  2209. */
  2210. static bool inattribute; /* looking at an __attribute__ construct */
  2211. /*
  2212. * C functions and variables are recognized using a simple
  2213. * finite automaton. fvdef is its state variable.
  2214. */
  2215. static enum
  2216. {
  2217. fvnone, /* nothing seen */
  2218. fdefunkey, /* Emacs DEFUN keyword seen */
  2219. fdefunname, /* Emacs DEFUN name seen */
  2220. foperator, /* func: operator keyword seen (cplpl) */
  2221. fvnameseen, /* function or variable name seen */
  2222. fstartlist, /* func: just after open parenthesis */
  2223. finlist, /* func: in parameter list */
  2224. flistseen, /* func: after parameter list */
  2225. fignore, /* func: before open brace */
  2226. vignore /* var-like: ignore until ';' */
  2227. } fvdef;
  2228. static bool fvextern; /* func or var: extern keyword seen; */
  2229. /*
  2230. * typedefs are recognized using a simple finite automaton.
  2231. * typdef is its state variable.
  2232. */
  2233. static enum
  2234. {
  2235. tnone, /* nothing seen */
  2236. tkeyseen, /* typedef keyword seen */
  2237. ttypeseen, /* defined type seen */
  2238. tinbody, /* inside typedef body */
  2239. tend, /* just before typedef tag */
  2240. tignore /* junk after typedef tag */
  2241. } typdef;
  2242. /*
  2243. * struct-like structures (enum, struct and union) are recognized
  2244. * using another simple finite automaton. `structdef' is its state
  2245. * variable.
  2246. */
  2247. static enum
  2248. {
  2249. snone, /* nothing seen yet,
  2250. or in struct body if bracelev > 0 */
  2251. skeyseen, /* struct-like keyword seen */
  2252. stagseen, /* struct-like tag seen */
  2253. scolonseen /* colon seen after struct-like tag */
  2254. } structdef;
  2255. /*
  2256. * When objdef is different from onone, objtag is the name of the class.
  2257. */
  2258. static const char *objtag = "<uninited>";
  2259. /*
  2260. * Yet another little state machine to deal with preprocessor lines.
  2261. */
  2262. static enum
  2263. {
  2264. dnone, /* nothing seen */
  2265. dsharpseen, /* '#' seen as first char on line */
  2266. ddefineseen, /* '#' and 'define' seen */
  2267. dignorerest /* ignore rest of line */
  2268. } definedef;
  2269. /*
  2270. * State machine for Objective C protocols and implementations.
  2271. * Idea by Tom R.Hageman <tom@basil.icce.rug.nl> (1995)
  2272. */
  2273. static enum
  2274. {
  2275. onone, /* nothing seen */
  2276. oprotocol, /* @interface or @protocol seen */
  2277. oimplementation, /* @implementations seen */
  2278. otagseen, /* class name seen */
  2279. oparenseen, /* parenthesis before category seen */
  2280. ocatseen, /* category name seen */
  2281. oinbody, /* in @implementation body */
  2282. omethodsign, /* in @implementation body, after +/- */
  2283. omethodtag, /* after method name */
  2284. omethodcolon, /* after method colon */
  2285. omethodparm, /* after method parameter */
  2286. oignore /* wait for @end */
  2287. } objdef;
  2288. /*
  2289. * Use this structure to keep info about the token read, and how it
  2290. * should be tagged. Used by the make_C_tag function to build a tag.
  2291. */
  2292. static struct tok
  2293. {
  2294. char *line; /* string containing the token */
  2295. int offset; /* where the token starts in LINE */
  2296. int length; /* token length */
  2297. /*
  2298. The previous members can be used to pass strings around for generic
  2299. purposes. The following ones specifically refer to creating tags. In this
  2300. case the token contained here is the pattern that will be used to create a
  2301. tag.
  2302. */
  2303. bool valid; /* do not create a tag; the token should be
  2304. invalidated whenever a state machine is
  2305. reset prematurely */
  2306. bool named; /* create a named tag */
  2307. int lineno; /* source line number of tag */
  2308. long linepos; /* source char number of tag */
  2309. } token; /* latest token read */
  2310. /*
  2311. * Variables and functions for dealing with nested structures.
  2312. * Idea by Mykola Dzyuba <mdzyuba@yahoo.com> (2001)
  2313. */
  2314. static void pushclass_above (int, char *, int);
  2315. static void popclass_above (int);
  2316. static void write_classname (linebuffer *, const char *qualifier);
  2317. static struct {
  2318. char **cname; /* nested class names */
  2319. int *bracelev; /* nested class brace level */
  2320. int nl; /* class nesting level (elements used) */
  2321. int size; /* length of the array */
  2322. } cstack; /* stack for nested declaration tags */
  2323. /* Current struct nesting depth (namespace, class, struct, union, enum). */
  2324. #define nestlev (cstack.nl)
  2325. /* After struct keyword or in struct body, not inside a nested function. */
  2326. #define instruct (structdef == snone && nestlev > 0 \
  2327. && bracelev == cstack.bracelev[nestlev-1] + 1)
  2328. static void
  2329. pushclass_above (int bracelev, char *str, int len)
  2330. {
  2331. int nl;
  2332. popclass_above (bracelev);
  2333. nl = cstack.nl;
  2334. if (nl >= cstack.size)
  2335. {
  2336. int size = cstack.size *= 2;
  2337. xrnew (cstack.cname, size, char *);
  2338. xrnew (cstack.bracelev, size, int);
  2339. }
  2340. assert (nl == 0 || cstack.bracelev[nl-1] < bracelev);
  2341. cstack.cname[nl] = (str == NULL) ? NULL : savenstr (str, len);
  2342. cstack.bracelev[nl] = bracelev;
  2343. cstack.nl = nl + 1;
  2344. }
  2345. static void
  2346. popclass_above (int bracelev)
  2347. {
  2348. int nl;
  2349. for (nl = cstack.nl - 1;
  2350. nl >= 0 && cstack.bracelev[nl] >= bracelev;
  2351. nl--)
  2352. {
  2353. free (cstack.cname[nl]);
  2354. cstack.nl = nl;
  2355. }
  2356. }
  2357. static void
  2358. write_classname (linebuffer *cn, const char *qualifier)
  2359. {
  2360. int i, len;
  2361. int qlen = strlen (qualifier);
  2362. if (cstack.nl == 0 || cstack.cname[0] == NULL)
  2363. {
  2364. len = 0;
  2365. cn->len = 0;
  2366. cn->buffer[0] = '\0';
  2367. }
  2368. else
  2369. {
  2370. len = strlen (cstack.cname[0]);
  2371. linebuffer_setlen (cn, len);
  2372. strcpy (cn->buffer, cstack.cname[0]);
  2373. }
  2374. for (i = 1; i < cstack.nl; i++)
  2375. {
  2376. char *s;
  2377. int slen;
  2378. s = cstack.cname[i];
  2379. if (s == NULL)
  2380. continue;
  2381. slen = strlen (s);
  2382. len += slen + qlen;
  2383. linebuffer_setlen (cn, len);
  2384. strncat (cn->buffer, qualifier, qlen);
  2385. strncat (cn->buffer, s, slen);
  2386. }
  2387. }
  2388. static bool consider_token (char *, int, int, int *, int, int, bool *);
  2389. static void make_C_tag (bool);
  2390. /*
  2391. * consider_token ()
  2392. * checks to see if the current token is at the start of a
  2393. * function or variable, or corresponds to a typedef, or
  2394. * is a struct/union/enum tag, or #define, or an enum constant.
  2395. *
  2396. * *IS_FUNC gets TRUE if the token is a function or #define macro
  2397. * with args. C_EXTP points to which language we are looking at.
  2398. *
  2399. * Globals
  2400. * fvdef IN OUT
  2401. * structdef IN OUT
  2402. * definedef IN OUT
  2403. * typdef IN OUT
  2404. * objdef IN OUT
  2405. */
  2406. static bool
  2407. consider_token (register char *str, register int len, register int c, int *c_extp, int bracelev, int parlev, int *is_func_or_var)
  2408. /* IN: token pointer */
  2409. /* IN: token length */
  2410. /* IN: first char after the token */
  2411. /* IN, OUT: C extensions mask */
  2412. /* IN: brace level */
  2413. /* IN: parenthesis level */
  2414. /* OUT: function or variable found */
  2415. {
  2416. /* When structdef is stagseen, scolonseen, or snone with bracelev > 0,
  2417. structtype is the type of the preceding struct-like keyword, and
  2418. structbracelev is the brace level where it has been seen. */
  2419. static enum sym_type structtype;
  2420. static int structbracelev;
  2421. static enum sym_type toktype;
  2422. toktype = C_symtype (str, len, *c_extp);
  2423. /*
  2424. * Skip __attribute__
  2425. */
  2426. if (toktype == st_C_attribute)
  2427. {
  2428. inattribute = TRUE;
  2429. return FALSE;
  2430. }
  2431. /*
  2432. * Advance the definedef state machine.
  2433. */
  2434. switch (definedef)
  2435. {
  2436. case dnone:
  2437. /* We're not on a preprocessor line. */
  2438. if (toktype == st_C_gnumacro)
  2439. {
  2440. fvdef = fdefunkey;
  2441. return FALSE;
  2442. }
  2443. break;
  2444. case dsharpseen:
  2445. if (toktype == st_C_define)
  2446. {
  2447. definedef = ddefineseen;
  2448. }
  2449. else
  2450. {
  2451. definedef = dignorerest;
  2452. }
  2453. return FALSE;
  2454. case ddefineseen:
  2455. /*
  2456. * Make a tag for any macro, unless it is a constant
  2457. * and constantypedefs is FALSE.
  2458. */
  2459. definedef = dignorerest;
  2460. *is_func_or_var = (c == '(');
  2461. if (!*is_func_or_var && !constantypedefs)
  2462. return FALSE;
  2463. else
  2464. return TRUE;
  2465. case dignorerest:
  2466. return FALSE;
  2467. default:
  2468. error ("internal error: definedef value.", (char *)NULL);
  2469. }
  2470. /*
  2471. * Now typedefs
  2472. */
  2473. switch (typdef)
  2474. {
  2475. case tnone:
  2476. if (toktype == st_C_typedef)
  2477. {
  2478. if (typedefs)
  2479. typdef = tkeyseen;
  2480. fvextern = FALSE;
  2481. fvdef = fvnone;
  2482. return FALSE;
  2483. }
  2484. break;
  2485. case tkeyseen:
  2486. switch (toktype)
  2487. {
  2488. case st_none:
  2489. case st_C_class:
  2490. case st_C_struct:
  2491. case st_C_enum:
  2492. typdef = ttypeseen;
  2493. }
  2494. break;
  2495. case ttypeseen:
  2496. if (structdef == snone && fvdef == fvnone)
  2497. {
  2498. fvdef = fvnameseen;
  2499. return TRUE;
  2500. }
  2501. break;
  2502. case tend:
  2503. switch (toktype)
  2504. {
  2505. case st_C_class:
  2506. case st_C_struct:
  2507. case st_C_enum:
  2508. return FALSE;
  2509. }
  2510. return TRUE;
  2511. }
  2512. switch (toktype)
  2513. {
  2514. case st_C_javastruct:
  2515. if (structdef == stagseen)
  2516. structdef = scolonseen;
  2517. return FALSE;
  2518. case st_C_template:
  2519. case st_C_class:
  2520. if ((*c_extp & C_AUTO) /* automatic detection of C++ language */
  2521. && bracelev == 0
  2522. && definedef == dnone && structdef == snone
  2523. && typdef == tnone && fvdef == fvnone)
  2524. *c_extp = (*c_extp | C_PLPL) & ~C_AUTO;
  2525. if (toktype == st_C_template)
  2526. break;
  2527. /* FALLTHRU */
  2528. case st_C_struct:
  2529. case st_C_enum:
  2530. if (parlev == 0
  2531. && fvdef != vignore
  2532. && (typdef == tkeyseen
  2533. || (typedefs_or_cplusplus && structdef == snone)))
  2534. {
  2535. structdef = skeyseen;
  2536. structtype = toktype;
  2537. structbracelev = bracelev;
  2538. if (fvdef == fvnameseen)
  2539. fvdef = fvnone;
  2540. }
  2541. return FALSE;
  2542. }
  2543. if (structdef == skeyseen)
  2544. {
  2545. structdef = stagseen;
  2546. return TRUE;
  2547. }
  2548. if (typdef != tnone)
  2549. definedef = dnone;
  2550. /* Detect Objective C constructs. */
  2551. switch (objdef)
  2552. {
  2553. case onone:
  2554. switch (toktype)
  2555. {
  2556. case st_C_objprot:
  2557. objdef = oprotocol;
  2558. return FALSE;
  2559. case st_C_objimpl:
  2560. objdef = oimplementation;
  2561. return FALSE;
  2562. }
  2563. break;
  2564. case oimplementation:
  2565. /* Save the class tag for functions or variables defined inside. */
  2566. objtag = savenstr (str, len);
  2567. objdef = oinbody;
  2568. return FALSE;
  2569. case oprotocol:
  2570. /* Save the class tag for categories. */
  2571. objtag = savenstr (str, len);
  2572. objdef = otagseen;
  2573. *is_func_or_var = TRUE;
  2574. return TRUE;
  2575. case oparenseen:
  2576. objdef = ocatseen;
  2577. *is_func_or_var = TRUE;
  2578. return TRUE;
  2579. case oinbody:
  2580. break;
  2581. case omethodsign:
  2582. if (parlev == 0)
  2583. {
  2584. fvdef = fvnone;
  2585. objdef = omethodtag;
  2586. linebuffer_setlen (&token_name, len);
  2587. strncpy (token_name.buffer, str, len);
  2588. token_name.buffer[len] = '\0';
  2589. return TRUE;
  2590. }
  2591. return FALSE;
  2592. case omethodcolon:
  2593. if (parlev == 0)
  2594. objdef = omethodparm;
  2595. return FALSE;
  2596. case omethodparm:
  2597. if (parlev == 0)
  2598. {
  2599. fvdef = fvnone;
  2600. objdef = omethodtag;
  2601. linebuffer_setlen (&token_name, token_name.len + len);
  2602. strncat (token_name.buffer, str, len);
  2603. return TRUE;
  2604. }
  2605. return FALSE;
  2606. case oignore:
  2607. if (toktype == st_C_objend)
  2608. {
  2609. /* Memory leakage here: the string pointed by objtag is
  2610. never released, because many tests would be needed to
  2611. avoid breaking on incorrect input code. The amount of
  2612. memory leaked here is the sum of the lengths of the
  2613. class tags.
  2614. free (objtag); */
  2615. objdef = onone;
  2616. }
  2617. return FALSE;
  2618. }
  2619. /* A function, variable or enum constant? */
  2620. switch (toktype)
  2621. {
  2622. case st_C_extern:
  2623. fvextern = TRUE;
  2624. switch (fvdef)
  2625. {
  2626. case finlist:
  2627. case flistseen:
  2628. case fignore:
  2629. case vignore:
  2630. break;
  2631. default:
  2632. fvdef = fvnone;
  2633. }
  2634. return FALSE;
  2635. case st_C_ignore:
  2636. fvextern = FALSE;
  2637. fvdef = vignore;
  2638. return FALSE;
  2639. case st_C_operator:
  2640. fvdef = foperator;
  2641. *is_func_or_var = TRUE;
  2642. return TRUE;
  2643. case st_none:
  2644. if (constantypedefs
  2645. && structdef == snone
  2646. && structtype == st_C_enum && bracelev > structbracelev)
  2647. return TRUE; /* enum constant */
  2648. switch (fvdef)
  2649. {
  2650. case fdefunkey:
  2651. if (bracelev > 0)
  2652. break;
  2653. fvdef = fdefunname; /* GNU macro */
  2654. *is_func_or_var = TRUE;
  2655. return TRUE;
  2656. case fvnone:
  2657. switch (typdef)
  2658. {
  2659. case ttypeseen:
  2660. return FALSE;
  2661. case tnone:
  2662. if ((strneq (str, "asm", 3) && endtoken (str[3]))
  2663. || (strneq (str, "__asm__", 7) && endtoken (str[7])))
  2664. {
  2665. fvdef = vignore;
  2666. return FALSE;
  2667. }
  2668. break;
  2669. }
  2670. /* FALLTHRU */
  2671. case fvnameseen:
  2672. if (len >= 10 && strneq (str+len-10, "::operator", 10))
  2673. {
  2674. if (*c_extp & C_AUTO) /* automatic detection of C++ */
  2675. *c_extp = (*c_extp | C_PLPL) & ~C_AUTO;
  2676. fvdef = foperator;
  2677. *is_func_or_var = TRUE;
  2678. return TRUE;
  2679. }
  2680. if (bracelev > 0 && !instruct)
  2681. break;
  2682. fvdef = fvnameseen; /* function or variable */
  2683. *is_func_or_var = TRUE;
  2684. return TRUE;
  2685. }
  2686. break;
  2687. }
  2688. return FALSE;
  2689. }
  2690. /*
  2691. * C_entries often keeps pointers to tokens or lines which are older than
  2692. * the line currently read. By keeping two line buffers, and switching
  2693. * them at end of line, it is possible to use those pointers.
  2694. */
  2695. static struct
  2696. {
  2697. long linepos;
  2698. linebuffer lb;
  2699. } lbs[2];
  2700. #define current_lb_is_new (newndx == curndx)
  2701. #define switch_line_buffers() (curndx = 1 - curndx)
  2702. #define curlb (lbs[curndx].lb)
  2703. #define newlb (lbs[newndx].lb)
  2704. #define curlinepos (lbs[curndx].linepos)
  2705. #define newlinepos (lbs[newndx].linepos)
  2706. #define plainc ((c_ext & C_EXT) == C_PLAIN)
  2707. #define cplpl (c_ext & C_PLPL)
  2708. #define cjava ((c_ext & C_JAVA) == C_JAVA)
  2709. #define CNL_SAVE_DEFINEDEF() \
  2710. do { \
  2711. curlinepos = charno; \
  2712. readline (&curlb, inf); \
  2713. lp = curlb.buffer; \
  2714. quotednl = FALSE; \
  2715. newndx = curndx; \
  2716. } while (0)
  2717. #define CNL() \
  2718. do { \
  2719. CNL_SAVE_DEFINEDEF(); \
  2720. if (savetoken.valid) \
  2721. { \
  2722. token = savetoken; \
  2723. savetoken.valid = FALSE; \
  2724. } \
  2725. definedef = dnone; \
  2726. } while (0)
  2727. static void
  2728. make_C_tag (int isfun)
  2729. {
  2730. /* This function is never called when token.valid is FALSE, but
  2731. we must protect against invalid input or internal errors. */
  2732. if (token.valid)
  2733. make_tag (token_name.buffer, token_name.len, isfun, token.line,
  2734. token.offset+token.length+1, token.lineno, token.linepos);
  2735. else if (DEBUG)
  2736. { /* this branch is optimized away if !DEBUG */
  2737. make_tag (concat ("INVALID TOKEN:-->", token_name.buffer, ""),
  2738. token_name.len + 17, isfun, token.line,
  2739. token.offset+token.length+1, token.lineno, token.linepos);
  2740. error ("INVALID TOKEN", NULL);
  2741. }
  2742. token.valid = FALSE;
  2743. }
  2744. /*
  2745. * C_entries ()
  2746. * This routine finds functions, variables, typedefs,
  2747. * #define's, enum constants and struct/union/enum definitions in
  2748. * C syntax and adds them to the list.
  2749. */
  2750. static void
  2751. C_entries (int c_ext, FILE *inf)
  2752. /* extension of C */
  2753. /* input file */
  2754. {
  2755. register char c; /* latest char read; '\0' for end of line */
  2756. register char *lp; /* pointer one beyond the character `c' */
  2757. int curndx, newndx; /* indices for current and new lb */
  2758. register int tokoff; /* offset in line of start of current token */
  2759. register int toklen; /* length of current token */
  2760. const char *qualifier; /* string used to qualify names */
  2761. int qlen; /* length of qualifier */
  2762. int bracelev; /* current brace level */
  2763. int bracketlev; /* current bracket level */
  2764. int parlev; /* current parenthesis level */
  2765. int attrparlev; /* __attribute__ parenthesis level */
  2766. int templatelev; /* current template level */
  2767. int typdefbracelev; /* bracelev where a typedef struct body begun */
  2768. bool incomm, inquote, inchar, quotednl, midtoken;
  2769. bool yacc_rules; /* in the rules part of a yacc file */
  2770. struct tok savetoken = {0}; /* token saved during preprocessor handling */
  2771. linebuffer_init (&lbs[0].lb);
  2772. linebuffer_init (&lbs[1].lb);
  2773. if (cstack.size == 0)
  2774. {
  2775. cstack.size = (DEBUG) ? 1 : 4;
  2776. cstack.nl = 0;
  2777. cstack.cname = xnew (cstack.size, char *);
  2778. cstack.bracelev = xnew (cstack.size, int);
  2779. }
  2780. tokoff = toklen = typdefbracelev = 0; /* keep compiler quiet */
  2781. curndx = newndx = 0;
  2782. lp = curlb.buffer;
  2783. *lp = 0;
  2784. fvdef = fvnone; fvextern = FALSE; typdef = tnone;
  2785. structdef = snone; definedef = dnone; objdef = onone;
  2786. yacc_rules = FALSE;
  2787. midtoken = inquote = inchar = incomm = quotednl = FALSE;
  2788. token.valid = savetoken.valid = FALSE;
  2789. bracelev = bracketlev = parlev = attrparlev = templatelev = 0;
  2790. if (cjava)
  2791. { qualifier = "."; qlen = 1; }
  2792. else
  2793. { qualifier = "::"; qlen = 2; }
  2794. while (!feof (inf))
  2795. {
  2796. c = *lp++;
  2797. if (c == '\\')
  2798. {
  2799. /* If we are at the end of the line, the next character is a
  2800. '\0'; do not skip it, because it is what tells us
  2801. to read the next line. */
  2802. if (*lp == '\0')
  2803. {
  2804. quotednl = TRUE;
  2805. continue;
  2806. }
  2807. lp++;
  2808. c = ' ';
  2809. }
  2810. else if (incomm)
  2811. {
  2812. switch (c)
  2813. {
  2814. case '*':
  2815. if (*lp == '/')
  2816. {
  2817. c = *lp++;
  2818. incomm = FALSE;
  2819. }
  2820. break;
  2821. case '\0':
  2822. /* Newlines inside comments do not end macro definitions in
  2823. traditional cpp. */
  2824. CNL_SAVE_DEFINEDEF ();
  2825. break;
  2826. }
  2827. continue;
  2828. }
  2829. else if (inquote)
  2830. {
  2831. switch (c)
  2832. {
  2833. case '"':
  2834. inquote = FALSE;
  2835. break;
  2836. case '\0':
  2837. /* Newlines inside strings do not end macro definitions
  2838. in traditional cpp, even though compilers don't
  2839. usually accept them. */
  2840. CNL_SAVE_DEFINEDEF ();
  2841. break;
  2842. }
  2843. continue;
  2844. }
  2845. else if (inchar)
  2846. {
  2847. switch (c)
  2848. {
  2849. case '\0':
  2850. /* Hmmm, something went wrong. */
  2851. CNL ();
  2852. /* FALLTHRU */
  2853. case '\'':
  2854. inchar = FALSE;
  2855. break;
  2856. }
  2857. continue;
  2858. }
  2859. else switch (c)
  2860. {
  2861. case '"':
  2862. inquote = TRUE;
  2863. if (bracketlev > 0)
  2864. continue;
  2865. if (inattribute)
  2866. break;
  2867. switch (fvdef)
  2868. {
  2869. case fdefunkey:
  2870. case fstartlist:
  2871. case finlist:
  2872. case fignore:
  2873. case vignore:
  2874. break;
  2875. default:
  2876. fvextern = FALSE;
  2877. fvdef = fvnone;
  2878. }
  2879. continue;
  2880. case '\'':
  2881. inchar = TRUE;
  2882. if (bracketlev > 0)
  2883. continue;
  2884. if (inattribute)
  2885. break;
  2886. if (fvdef != finlist && fvdef != fignore && fvdef != vignore)
  2887. {
  2888. fvextern = FALSE;
  2889. fvdef = fvnone;
  2890. }
  2891. continue;
  2892. case '/':
  2893. if (*lp == '*')
  2894. {
  2895. incomm = TRUE;
  2896. lp++;
  2897. c = ' ';
  2898. if (bracketlev > 0)
  2899. continue;
  2900. }
  2901. else if (/* cplpl && */ *lp == '/')
  2902. {
  2903. c = '\0';
  2904. }
  2905. break;
  2906. case '%':
  2907. if ((c_ext & YACC) && *lp == '%')
  2908. {
  2909. /* Entering or exiting rules section in yacc file. */
  2910. lp++;
  2911. definedef = dnone; fvdef = fvnone; fvextern = FALSE;
  2912. typdef = tnone; structdef = snone;
  2913. midtoken = inquote = inchar = incomm = quotednl = FALSE;
  2914. bracelev = 0;
  2915. yacc_rules = !yacc_rules;
  2916. continue;
  2917. }
  2918. else
  2919. break;
  2920. case '#':
  2921. if (definedef == dnone)
  2922. {
  2923. char *cp;
  2924. bool cpptoken = TRUE;
  2925. /* Look back on this line. If all blanks, or nonblanks
  2926. followed by an end of comment, this is a preprocessor
  2927. token. */
  2928. for (cp = newlb.buffer; cp < lp-1; cp++)
  2929. if (!iswhite (*cp))
  2930. {
  2931. if (*cp == '*' && cp[1] == '/')
  2932. {
  2933. cp++;
  2934. cpptoken = TRUE;
  2935. }
  2936. else
  2937. cpptoken = FALSE;
  2938. }
  2939. if (cpptoken)
  2940. definedef = dsharpseen;
  2941. } /* if (definedef == dnone) */
  2942. continue;
  2943. case '[':
  2944. bracketlev++;
  2945. continue;
  2946. default:
  2947. if (bracketlev > 0)
  2948. {
  2949. if (c == ']')
  2950. --bracketlev;
  2951. else if (c == '\0')
  2952. CNL_SAVE_DEFINEDEF ();
  2953. continue;
  2954. }
  2955. break;
  2956. } /* switch (c) */
  2957. /* Consider token only if some involved conditions are satisfied. */
  2958. if (typdef != tignore
  2959. && definedef != dignorerest
  2960. && fvdef != finlist
  2961. && templatelev == 0
  2962. && (definedef != dnone
  2963. || structdef != scolonseen)
  2964. && !inattribute)
  2965. {
  2966. if (midtoken)
  2967. {
  2968. if (endtoken (c))
  2969. {
  2970. if (c == ':' && *lp == ':' && begtoken (lp[1]))
  2971. /* This handles :: in the middle,
  2972. but not at the beginning of an identifier.
  2973. Also, space-separated :: is not recognized. */
  2974. {
  2975. if (c_ext & C_AUTO) /* automatic detection of C++ */
  2976. c_ext = (c_ext | C_PLPL) & ~C_AUTO;
  2977. lp += 2;
  2978. toklen += 2;
  2979. c = lp[-1];
  2980. goto still_in_token;
  2981. }
  2982. else
  2983. {
  2984. bool funorvar = FALSE;
  2985. if (yacc_rules
  2986. || consider_token (newlb.buffer + tokoff, toklen, c,
  2987. &c_ext, bracelev, parlev,
  2988. &funorvar))
  2989. {
  2990. if (fvdef == foperator)
  2991. {
  2992. char *oldlp = lp;
  2993. lp = skip_spaces (lp-1);
  2994. if (*lp != '\0')
  2995. lp += 1;
  2996. while (*lp != '\0'
  2997. && !iswhite (*lp) && *lp != '(')
  2998. lp += 1;
  2999. c = *lp++;
  3000. toklen += lp - oldlp;
  3001. }
  3002. token.named = FALSE;
  3003. if (!plainc
  3004. && nestlev > 0 && definedef == dnone)
  3005. /* in struct body */
  3006. {
  3007. write_classname (&token_name, qualifier);
  3008. linebuffer_setlen (&token_name,
  3009. token_name.len+qlen+toklen);
  3010. strcat (token_name.buffer, qualifier);
  3011. strncat (token_name.buffer,
  3012. newlb.buffer + tokoff, toklen);
  3013. token.named = TRUE;
  3014. }
  3015. else if (objdef == ocatseen)
  3016. /* Objective C category */
  3017. {
  3018. int len = strlen (objtag) + 2 + toklen;
  3019. linebuffer_setlen (&token_name, len);
  3020. strcpy (token_name.buffer, objtag);
  3021. strcat (token_name.buffer, "(");
  3022. strncat (token_name.buffer,
  3023. newlb.buffer + tokoff, toklen);
  3024. strcat (token_name.buffer, ")");
  3025. token.named = TRUE;
  3026. }
  3027. else if (objdef == omethodtag
  3028. || objdef == omethodparm)
  3029. /* Objective C method */
  3030. {
  3031. token.named = TRUE;
  3032. }
  3033. else if (fvdef == fdefunname)
  3034. /* GNU DEFUN and similar macros */
  3035. {
  3036. bool defun = (newlb.buffer[tokoff] == 'F');
  3037. int off = tokoff;
  3038. int len = toklen;
  3039. /* Rewrite the tag so that emacs lisp DEFUNs
  3040. can be found by their elisp name */
  3041. if (defun)
  3042. {
  3043. off += 1;
  3044. len -= 1;
  3045. }
  3046. linebuffer_setlen (&token_name, len);
  3047. strncpy (token_name.buffer,
  3048. newlb.buffer + off, len);
  3049. token_name.buffer[len] = '\0';
  3050. if (defun)
  3051. while (--len >= 0)
  3052. if (token_name.buffer[len] == '_')
  3053. token_name.buffer[len] = '-';
  3054. token.named = defun;
  3055. }
  3056. else
  3057. {
  3058. linebuffer_setlen (&token_name, toklen);
  3059. strncpy (token_name.buffer,
  3060. newlb.buffer + tokoff, toklen);
  3061. token_name.buffer[toklen] = '\0';
  3062. /* Name macros and members. */
  3063. token.named = (structdef == stagseen
  3064. || typdef == ttypeseen
  3065. || typdef == tend
  3066. || (funorvar
  3067. && definedef == dignorerest)
  3068. || (funorvar
  3069. && definedef == dnone
  3070. && structdef == snone
  3071. && bracelev > 0));
  3072. }
  3073. token.lineno = lineno;
  3074. token.offset = tokoff;
  3075. token.length = toklen;
  3076. token.line = newlb.buffer;
  3077. token.linepos = newlinepos;
  3078. token.valid = TRUE;
  3079. if (definedef == dnone
  3080. && (fvdef == fvnameseen
  3081. || fvdef == foperator
  3082. || structdef == stagseen
  3083. || typdef == tend
  3084. || typdef == ttypeseen
  3085. || objdef != onone))
  3086. {
  3087. if (current_lb_is_new)
  3088. switch_line_buffers ();
  3089. }
  3090. else if (definedef != dnone
  3091. || fvdef == fdefunname
  3092. || instruct)
  3093. make_C_tag (funorvar);
  3094. }
  3095. else /* not yacc and consider_token failed */
  3096. {
  3097. if (inattribute && fvdef == fignore)
  3098. {
  3099. /* We have just met __attribute__ after a
  3100. function parameter list: do not tag the
  3101. function again. */
  3102. fvdef = fvnone;
  3103. }
  3104. }
  3105. midtoken = FALSE;
  3106. }
  3107. } /* if (endtoken (c)) */
  3108. else if (intoken (c))
  3109. still_in_token:
  3110. {
  3111. toklen++;
  3112. continue;
  3113. }
  3114. } /* if (midtoken) */
  3115. else if (begtoken (c))
  3116. {
  3117. switch (definedef)
  3118. {
  3119. case dnone:
  3120. switch (fvdef)
  3121. {
  3122. case fstartlist:
  3123. /* This prevents tagging fb in
  3124. void (__attribute__((noreturn)) *fb) (void);
  3125. Fixing this is not easy and not very important. */
  3126. fvdef = finlist;
  3127. continue;
  3128. case flistseen:
  3129. if (plainc || declarations)
  3130. {
  3131. make_C_tag (TRUE); /* a function */
  3132. fvdef = fignore;
  3133. }
  3134. break;
  3135. }
  3136. if (structdef == stagseen && !cjava)
  3137. {
  3138. popclass_above (bracelev);
  3139. structdef = snone;
  3140. }
  3141. break;
  3142. case dsharpseen:
  3143. savetoken = token;
  3144. break;
  3145. }
  3146. if (!yacc_rules || lp == newlb.buffer + 1)
  3147. {
  3148. tokoff = lp - 1 - newlb.buffer;
  3149. toklen = 1;
  3150. midtoken = TRUE;
  3151. }
  3152. continue;
  3153. } /* if (begtoken) */
  3154. } /* if must look at token */
  3155. /* Detect end of line, colon, comma, semicolon and various braces
  3156. after having handled a token.*/
  3157. switch (c)
  3158. {
  3159. case ':':
  3160. if (inattribute)
  3161. break;
  3162. if (yacc_rules && token.offset == 0 && token.valid)
  3163. {
  3164. make_C_tag (FALSE); /* a yacc function */
  3165. break;
  3166. }
  3167. if (definedef != dnone)
  3168. break;
  3169. switch (objdef)
  3170. {
  3171. case otagseen:
  3172. objdef = oignore;
  3173. make_C_tag (TRUE); /* an Objective C class */
  3174. break;
  3175. case omethodtag:
  3176. case omethodparm:
  3177. objdef = omethodcolon;
  3178. linebuffer_setlen (&token_name, token_name.len + 1);
  3179. strcat (token_name.buffer, ":");
  3180. break;
  3181. }
  3182. if (structdef == stagseen)
  3183. {
  3184. structdef = scolonseen;
  3185. break;
  3186. }
  3187. /* Should be useless, but may be work as a safety net. */
  3188. if (cplpl && fvdef == flistseen)
  3189. {
  3190. make_C_tag (TRUE); /* a function */
  3191. fvdef = fignore;
  3192. break;
  3193. }
  3194. break;
  3195. case ';':
  3196. if (definedef != dnone || inattribute)
  3197. break;
  3198. switch (typdef)
  3199. {
  3200. case tend:
  3201. case ttypeseen:
  3202. make_C_tag (FALSE); /* a typedef */
  3203. typdef = tnone;
  3204. fvdef = fvnone;
  3205. break;
  3206. case tnone:
  3207. case tinbody:
  3208. case tignore:
  3209. switch (fvdef)
  3210. {
  3211. case fignore:
  3212. if (typdef == tignore || cplpl)
  3213. fvdef = fvnone;
  3214. break;
  3215. case fvnameseen:
  3216. if ((globals && bracelev == 0 && (!fvextern || declarations))
  3217. || (members && instruct))
  3218. make_C_tag (FALSE); /* a variable */
  3219. fvextern = FALSE;
  3220. fvdef = fvnone;
  3221. token.valid = FALSE;
  3222. break;
  3223. case flistseen:
  3224. if ((declarations
  3225. && (cplpl || !instruct)
  3226. && (typdef == tnone || (typdef != tignore && instruct)))
  3227. || (members
  3228. && plainc && instruct))
  3229. make_C_tag (TRUE); /* a function */
  3230. /* FALLTHRU */
  3231. default:
  3232. fvextern = FALSE;
  3233. fvdef = fvnone;
  3234. if (declarations
  3235. && cplpl && structdef == stagseen)
  3236. make_C_tag (FALSE); /* forward declaration */
  3237. else
  3238. token.valid = FALSE;
  3239. } /* switch (fvdef) */
  3240. /* FALLTHRU */
  3241. default:
  3242. if (!instruct)
  3243. typdef = tnone;
  3244. }
  3245. if (structdef == stagseen)
  3246. structdef = snone;
  3247. break;
  3248. case ',':
  3249. if (definedef != dnone || inattribute)
  3250. break;
  3251. switch (objdef)
  3252. {
  3253. case omethodtag:
  3254. case omethodparm:
  3255. make_C_tag (TRUE); /* an Objective C method */
  3256. objdef = oinbody;
  3257. break;
  3258. }
  3259. switch (fvdef)
  3260. {
  3261. case fdefunkey:
  3262. case foperator:
  3263. case fstartlist:
  3264. case finlist:
  3265. case fignore:
  3266. case vignore:
  3267. break;
  3268. case fdefunname:
  3269. fvdef = fignore;
  3270. break;
  3271. case fvnameseen:
  3272. if (parlev == 0
  3273. && ((globals
  3274. && bracelev == 0
  3275. && templatelev == 0
  3276. && (!fvextern || declarations))
  3277. || (members && instruct)))
  3278. make_C_tag (FALSE); /* a variable */
  3279. break;
  3280. case flistseen:
  3281. if ((declarations && typdef == tnone && !instruct)
  3282. || (members && typdef != tignore && instruct))
  3283. {
  3284. make_C_tag (TRUE); /* a function */
  3285. fvdef = fvnameseen;
  3286. }
  3287. else if (!declarations)
  3288. fvdef = fvnone;
  3289. token.valid = FALSE;
  3290. break;
  3291. default:
  3292. fvdef = fvnone;
  3293. }
  3294. if (structdef == stagseen)
  3295. structdef = snone;
  3296. break;
  3297. case ']':
  3298. if (definedef != dnone || inattribute)
  3299. break;
  3300. if (structdef == stagseen)
  3301. structdef = snone;
  3302. switch (typdef)
  3303. {
  3304. case ttypeseen:
  3305. case tend:
  3306. typdef = tignore;
  3307. make_C_tag (FALSE); /* a typedef */
  3308. break;
  3309. case tnone:
  3310. case tinbody:
  3311. switch (fvdef)
  3312. {
  3313. case foperator:
  3314. case finlist:
  3315. case fignore:
  3316. case vignore:
  3317. break;
  3318. case fvnameseen:
  3319. if ((members && bracelev == 1)
  3320. || (globals && bracelev == 0
  3321. && (!fvextern || declarations)))
  3322. make_C_tag (FALSE); /* a variable */
  3323. /* FALLTHRU */
  3324. default:
  3325. fvdef = fvnone;
  3326. }
  3327. break;
  3328. }
  3329. break;
  3330. case '(':
  3331. if (inattribute)
  3332. {
  3333. attrparlev++;
  3334. break;
  3335. }
  3336. if (definedef != dnone)
  3337. break;
  3338. if (objdef == otagseen && parlev == 0)
  3339. objdef = oparenseen;
  3340. switch (fvdef)
  3341. {
  3342. case fvnameseen:
  3343. if (typdef == ttypeseen
  3344. && *lp != '*'
  3345. && !instruct)
  3346. {
  3347. /* This handles constructs like:
  3348. typedef void OperatorFun (int fun); */
  3349. make_C_tag (FALSE);
  3350. typdef = tignore;
  3351. fvdef = fignore;
  3352. break;
  3353. }
  3354. /* FALLTHRU */
  3355. case foperator:
  3356. fvdef = fstartlist;
  3357. break;
  3358. case flistseen:
  3359. fvdef = finlist;
  3360. break;
  3361. }
  3362. parlev++;
  3363. break;
  3364. case ')':
  3365. if (inattribute)
  3366. {
  3367. if (--attrparlev == 0)
  3368. inattribute = FALSE;
  3369. break;
  3370. }
  3371. if (definedef != dnone)
  3372. break;
  3373. if (objdef == ocatseen && parlev == 1)
  3374. {
  3375. make_C_tag (TRUE); /* an Objective C category */
  3376. objdef = oignore;
  3377. }
  3378. if (--parlev == 0)
  3379. {
  3380. switch (fvdef)
  3381. {
  3382. case fstartlist:
  3383. case finlist:
  3384. fvdef = flistseen;
  3385. break;
  3386. }
  3387. if (!instruct
  3388. && (typdef == tend
  3389. || typdef == ttypeseen))
  3390. {
  3391. typdef = tignore;
  3392. make_C_tag (FALSE); /* a typedef */
  3393. }
  3394. }
  3395. else if (parlev < 0) /* can happen due to ill-conceived #if's. */
  3396. parlev = 0;
  3397. break;
  3398. case '{':
  3399. if (definedef != dnone)
  3400. break;
  3401. if (typdef == ttypeseen)
  3402. {
  3403. /* Whenever typdef is set to tinbody (currently only
  3404. here), typdefbracelev should be set to bracelev. */
  3405. typdef = tinbody;
  3406. typdefbracelev = bracelev;
  3407. }
  3408. switch (fvdef)
  3409. {
  3410. case flistseen:
  3411. make_C_tag (TRUE); /* a function */
  3412. /* FALLTHRU */
  3413. case fignore:
  3414. fvdef = fvnone;
  3415. break;
  3416. case fvnone:
  3417. switch (objdef)
  3418. {
  3419. case otagseen:
  3420. make_C_tag (TRUE); /* an Objective C class */
  3421. objdef = oignore;
  3422. break;
  3423. case omethodtag:
  3424. case omethodparm:
  3425. make_C_tag (TRUE); /* an Objective C method */
  3426. objdef = oinbody;
  3427. break;
  3428. default:
  3429. /* Neutralize `extern "C" {' grot. */
  3430. if (bracelev == 0 && structdef == snone && nestlev == 0
  3431. && typdef == tnone)
  3432. bracelev = -1;
  3433. }
  3434. break;
  3435. }
  3436. switch (structdef)
  3437. {
  3438. case skeyseen: /* unnamed struct */
  3439. pushclass_above (bracelev, NULL, 0);
  3440. structdef = snone;
  3441. break;
  3442. case stagseen: /* named struct or enum */
  3443. case scolonseen: /* a class */
  3444. pushclass_above (bracelev,token.line+token.offset, token.length);
  3445. structdef = snone;
  3446. make_C_tag (FALSE); /* a struct or enum */
  3447. break;
  3448. }
  3449. bracelev += 1;
  3450. break;
  3451. case '*':
  3452. if (definedef != dnone)
  3453. break;
  3454. if (fvdef == fstartlist)
  3455. {
  3456. fvdef = fvnone; /* avoid tagging `foo' in `foo (*bar()) ()' */
  3457. token.valid = FALSE;
  3458. }
  3459. break;
  3460. case '}':
  3461. if (definedef != dnone)
  3462. break;
  3463. bracelev -= 1;
  3464. if (!ignoreindent && lp == newlb.buffer + 1)
  3465. {
  3466. if (bracelev != 0)
  3467. token.valid = FALSE; /* unexpected value, token unreliable */
  3468. bracelev = 0; /* reset brace level if first column */
  3469. parlev = 0; /* also reset paren level, just in case... */
  3470. }
  3471. else if (bracelev < 0)
  3472. {
  3473. token.valid = FALSE; /* something gone amiss, token unreliable */
  3474. bracelev = 0;
  3475. }
  3476. if (bracelev == 0 && fvdef == vignore)
  3477. fvdef = fvnone; /* end of function */
  3478. popclass_above (bracelev);
  3479. structdef = snone;
  3480. /* Only if typdef == tinbody is typdefbracelev significant. */
  3481. if (typdef == tinbody && bracelev <= typdefbracelev)
  3482. {
  3483. assert (bracelev == typdefbracelev);
  3484. typdef = tend;
  3485. }
  3486. break;
  3487. case '=':
  3488. if (definedef != dnone)
  3489. break;
  3490. switch (fvdef)
  3491. {
  3492. case foperator:
  3493. case finlist:
  3494. case fignore:
  3495. case vignore:
  3496. break;
  3497. case fvnameseen:
  3498. if ((members && bracelev == 1)
  3499. || (globals && bracelev == 0 && (!fvextern || declarations)))
  3500. make_C_tag (FALSE); /* a variable */
  3501. /* FALLTHRU */
  3502. default:
  3503. fvdef = vignore;
  3504. }
  3505. break;
  3506. case '<':
  3507. if (cplpl
  3508. && (structdef == stagseen || fvdef == fvnameseen))
  3509. {
  3510. templatelev++;
  3511. break;
  3512. }
  3513. goto resetfvdef;
  3514. case '>':
  3515. if (templatelev > 0)
  3516. {
  3517. templatelev--;
  3518. break;
  3519. }
  3520. goto resetfvdef;
  3521. case '+':
  3522. case '-':
  3523. if (objdef == oinbody && bracelev == 0)
  3524. {
  3525. objdef = omethodsign;
  3526. break;
  3527. }
  3528. /* FALLTHRU */
  3529. resetfvdef:
  3530. case '#': case '~': case '&': case '%': case '/':
  3531. case '|': case '^': case '!': case '.': case '?':
  3532. if (definedef != dnone)
  3533. break;
  3534. /* These surely cannot follow a function tag in C. */
  3535. switch (fvdef)
  3536. {
  3537. case foperator:
  3538. case finlist:
  3539. case fignore:
  3540. case vignore:
  3541. break;
  3542. default:
  3543. fvdef = fvnone;
  3544. }
  3545. break;
  3546. case '\0':
  3547. if (objdef == otagseen)
  3548. {
  3549. make_C_tag (TRUE); /* an Objective C class */
  3550. objdef = oignore;
  3551. }
  3552. /* If a macro spans multiple lines don't reset its state. */
  3553. if (quotednl)
  3554. CNL_SAVE_DEFINEDEF ();
  3555. else
  3556. CNL ();
  3557. break;
  3558. } /* switch (c) */
  3559. } /* while not eof */
  3560. free (lbs[0].lb.buffer);
  3561. free (lbs[1].lb.buffer);
  3562. }
  3563. /*
  3564. * Process either a C++ file or a C file depending on the setting
  3565. * of a global flag.
  3566. */
  3567. static void
  3568. default_C_entries (FILE *inf)
  3569. {
  3570. C_entries (cplusplus ? C_PLPL : C_AUTO, inf);
  3571. }
  3572. /* Always do plain C. */
  3573. static void
  3574. plain_C_entries (FILE *inf)
  3575. {
  3576. C_entries (0, inf);
  3577. }
  3578. /* Always do C++. */
  3579. static void
  3580. Cplusplus_entries (FILE *inf)
  3581. {
  3582. C_entries (C_PLPL, inf);
  3583. }
  3584. /* Always do Java. */
  3585. static void
  3586. Cjava_entries (FILE *inf)
  3587. {
  3588. C_entries (C_JAVA, inf);
  3589. }
  3590. /* Always do C*. */
  3591. static void
  3592. Cstar_entries (FILE *inf)
  3593. {
  3594. C_entries (C_STAR, inf);
  3595. }
  3596. /* Always do Yacc. */
  3597. static void
  3598. Yacc_entries (FILE *inf)
  3599. {
  3600. C_entries (YACC, inf);
  3601. }
  3602. /* Useful macros. */
  3603. #define LOOP_ON_INPUT_LINES(file_pointer, line_buffer, char_pointer) \
  3604. for (; /* loop initialization */ \
  3605. !feof (file_pointer) /* loop test */ \
  3606. && /* instructions at start of loop */ \
  3607. (readline (&line_buffer, file_pointer), \
  3608. char_pointer = line_buffer.buffer, \
  3609. TRUE); \
  3610. )
  3611. #define LOOKING_AT(cp, kw) /* kw is the keyword, a literal string */ \
  3612. ((assert ("" kw), TRUE) /* syntax error if not a literal string */ \
  3613. && strneq ((cp), kw, sizeof (kw)-1) /* cp points at kw */ \
  3614. && notinname ((cp)[sizeof (kw)-1]) /* end of kw */ \
  3615. && ((cp) = skip_spaces ((cp)+sizeof (kw)-1))) /* skip spaces */
  3616. /* Similar to LOOKING_AT but does not use notinname, does not skip */
  3617. #define LOOKING_AT_NOCASE(cp, kw) /* the keyword is a literal string */ \
  3618. ((assert ("" kw), TRUE) /* syntax error if not a literal string */ \
  3619. && strncaseeq ((cp), kw, sizeof (kw)-1) /* cp points at kw */ \
  3620. && ((cp) += sizeof (kw)-1)) /* skip spaces */
  3621. /*
  3622. * Read a file, but do no processing. This is used to do regexp
  3623. * matching on files that have no language defined.
  3624. */
  3625. static void
  3626. just_read_file (FILE *inf)
  3627. {
  3628. while (!feof (inf))
  3629. readline (&lb, inf);
  3630. }
  3631. /* Fortran parsing */
  3632. static void F_takeprec (void);
  3633. static void F_getit (FILE *);
  3634. static void
  3635. F_takeprec (void)
  3636. {
  3637. dbp = skip_spaces (dbp);
  3638. if (*dbp != '*')
  3639. return;
  3640. dbp++;
  3641. dbp = skip_spaces (dbp);
  3642. if (strneq (dbp, "(*)", 3))
  3643. {
  3644. dbp += 3;
  3645. return;
  3646. }
  3647. if (!ISDIGIT (*dbp))
  3648. {
  3649. --dbp; /* force failure */
  3650. return;
  3651. }
  3652. do
  3653. dbp++;
  3654. while (ISDIGIT (*dbp));
  3655. }
  3656. static void
  3657. F_getit (FILE *inf)
  3658. {
  3659. register char *cp;
  3660. dbp = skip_spaces (dbp);
  3661. if (*dbp == '\0')
  3662. {
  3663. readline (&lb, inf);
  3664. dbp = lb.buffer;
  3665. if (dbp[5] != '&')
  3666. return;
  3667. dbp += 6;
  3668. dbp = skip_spaces (dbp);
  3669. }
  3670. if (!ISALPHA (*dbp) && *dbp != '_' && *dbp != '$')
  3671. return;
  3672. for (cp = dbp + 1; *cp != '\0' && intoken (*cp); cp++)
  3673. continue;
  3674. make_tag (dbp, cp-dbp, TRUE,
  3675. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  3676. }
  3677. static void
  3678. Fortran_functions (FILE *inf)
  3679. {
  3680. LOOP_ON_INPUT_LINES (inf, lb, dbp)
  3681. {
  3682. if (*dbp == '%')
  3683. dbp++; /* Ratfor escape to fortran */
  3684. dbp = skip_spaces (dbp);
  3685. if (*dbp == '\0')
  3686. continue;
  3687. if (LOOKING_AT_NOCASE (dbp, "recursive"))
  3688. dbp = skip_spaces (dbp);
  3689. if (LOOKING_AT_NOCASE (dbp, "pure"))
  3690. dbp = skip_spaces (dbp);
  3691. if (LOOKING_AT_NOCASE (dbp, "elemental"))
  3692. dbp = skip_spaces (dbp);
  3693. switch (lowcase (*dbp))
  3694. {
  3695. case 'i':
  3696. if (nocase_tail ("integer"))
  3697. F_takeprec ();
  3698. break;
  3699. case 'r':
  3700. if (nocase_tail ("real"))
  3701. F_takeprec ();
  3702. break;
  3703. case 'l':
  3704. if (nocase_tail ("logical"))
  3705. F_takeprec ();
  3706. break;
  3707. case 'c':
  3708. if (nocase_tail ("complex") || nocase_tail ("character"))
  3709. F_takeprec ();
  3710. break;
  3711. case 'd':
  3712. if (nocase_tail ("double"))
  3713. {
  3714. dbp = skip_spaces (dbp);
  3715. if (*dbp == '\0')
  3716. continue;
  3717. if (nocase_tail ("precision"))
  3718. break;
  3719. continue;
  3720. }
  3721. break;
  3722. }
  3723. dbp = skip_spaces (dbp);
  3724. if (*dbp == '\0')
  3725. continue;
  3726. switch (lowcase (*dbp))
  3727. {
  3728. case 'f':
  3729. if (nocase_tail ("function"))
  3730. F_getit (inf);
  3731. continue;
  3732. case 's':
  3733. if (nocase_tail ("subroutine"))
  3734. F_getit (inf);
  3735. continue;
  3736. case 'e':
  3737. if (nocase_tail ("entry"))
  3738. F_getit (inf);
  3739. continue;
  3740. case 'b':
  3741. if (nocase_tail ("blockdata") || nocase_tail ("block data"))
  3742. {
  3743. dbp = skip_spaces (dbp);
  3744. if (*dbp == '\0') /* assume un-named */
  3745. make_tag ("blockdata", 9, TRUE,
  3746. lb.buffer, dbp - lb.buffer, lineno, linecharno);
  3747. else
  3748. F_getit (inf); /* look for name */
  3749. }
  3750. continue;
  3751. }
  3752. }
  3753. }
  3754. /*
  3755. * Ada parsing
  3756. * Original code by
  3757. * Philippe Waroquiers (1998)
  3758. */
  3759. /* Once we are positioned after an "interesting" keyword, let's get
  3760. the real tag value necessary. */
  3761. static void
  3762. Ada_getit (FILE *inf, const char *name_qualifier)
  3763. {
  3764. register char *cp;
  3765. char *name;
  3766. char c;
  3767. while (!feof (inf))
  3768. {
  3769. dbp = skip_spaces (dbp);
  3770. if (*dbp == '\0'
  3771. || (dbp[0] == '-' && dbp[1] == '-'))
  3772. {
  3773. readline (&lb, inf);
  3774. dbp = lb.buffer;
  3775. }
  3776. switch (lowcase (*dbp))
  3777. {
  3778. case 'b':
  3779. if (nocase_tail ("body"))
  3780. {
  3781. /* Skipping body of procedure body or package body or ....
  3782. resetting qualifier to body instead of spec. */
  3783. name_qualifier = "/b";
  3784. continue;
  3785. }
  3786. break;
  3787. case 't':
  3788. /* Skipping type of task type or protected type ... */
  3789. if (nocase_tail ("type"))
  3790. continue;
  3791. break;
  3792. }
  3793. if (*dbp == '"')
  3794. {
  3795. dbp += 1;
  3796. for (cp = dbp; *cp != '\0' && *cp != '"'; cp++)
  3797. continue;
  3798. }
  3799. else
  3800. {
  3801. dbp = skip_spaces (dbp);
  3802. for (cp = dbp;
  3803. (*cp != '\0'
  3804. && (ISALPHA (*cp) || ISDIGIT (*cp) || *cp == '_' || *cp == '.'));
  3805. cp++)
  3806. continue;
  3807. if (cp == dbp)
  3808. return;
  3809. }
  3810. c = *cp;
  3811. *cp = '\0';
  3812. name = concat (dbp, name_qualifier, "");
  3813. *cp = c;
  3814. make_tag (name, strlen (name), TRUE,
  3815. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  3816. free (name);
  3817. if (c == '"')
  3818. dbp = cp + 1;
  3819. return;
  3820. }
  3821. }
  3822. static void
  3823. Ada_funcs (FILE *inf)
  3824. {
  3825. bool inquote = FALSE;
  3826. bool skip_till_semicolumn = FALSE;
  3827. LOOP_ON_INPUT_LINES (inf, lb, dbp)
  3828. {
  3829. while (*dbp != '\0')
  3830. {
  3831. /* Skip a string i.e. "abcd". */
  3832. if (inquote || (*dbp == '"'))
  3833. {
  3834. dbp = etags_strchr (dbp + !inquote, '"');
  3835. if (dbp != NULL)
  3836. {
  3837. inquote = FALSE;
  3838. dbp += 1;
  3839. continue; /* advance char */
  3840. }
  3841. else
  3842. {
  3843. inquote = TRUE;
  3844. break; /* advance line */
  3845. }
  3846. }
  3847. /* Skip comments. */
  3848. if (dbp[0] == '-' && dbp[1] == '-')
  3849. break; /* advance line */
  3850. /* Skip character enclosed in single quote i.e. 'a'
  3851. and skip single quote starting an attribute i.e. 'Image. */
  3852. if (*dbp == '\'')
  3853. {
  3854. dbp++ ;
  3855. if (*dbp != '\0')
  3856. dbp++;
  3857. continue;
  3858. }
  3859. if (skip_till_semicolumn)
  3860. {
  3861. if (*dbp == ';')
  3862. skip_till_semicolumn = FALSE;
  3863. dbp++;
  3864. continue; /* advance char */
  3865. }
  3866. /* Search for beginning of a token. */
  3867. if (!begtoken (*dbp))
  3868. {
  3869. dbp++;
  3870. continue; /* advance char */
  3871. }
  3872. /* We are at the beginning of a token. */
  3873. switch (lowcase (*dbp))
  3874. {
  3875. case 'f':
  3876. if (!packages_only && nocase_tail ("function"))
  3877. Ada_getit (inf, "/f");
  3878. else
  3879. break; /* from switch */
  3880. continue; /* advance char */
  3881. case 'p':
  3882. if (!packages_only && nocase_tail ("procedure"))
  3883. Ada_getit (inf, "/p");
  3884. else if (nocase_tail ("package"))
  3885. Ada_getit (inf, "/s");
  3886. else if (nocase_tail ("protected")) /* protected type */
  3887. Ada_getit (inf, "/t");
  3888. else
  3889. break; /* from switch */
  3890. continue; /* advance char */
  3891. case 'u':
  3892. if (typedefs && !packages_only && nocase_tail ("use"))
  3893. {
  3894. /* when tagging types, avoid tagging use type Pack.Typename;
  3895. for this, we will skip everything till a ; */
  3896. skip_till_semicolumn = TRUE;
  3897. continue; /* advance char */
  3898. }
  3899. case 't':
  3900. if (!packages_only && nocase_tail ("task"))
  3901. Ada_getit (inf, "/k");
  3902. else if (typedefs && !packages_only && nocase_tail ("type"))
  3903. {
  3904. Ada_getit (inf, "/t");
  3905. while (*dbp != '\0')
  3906. dbp += 1;
  3907. }
  3908. else
  3909. break; /* from switch */
  3910. continue; /* advance char */
  3911. }
  3912. /* Look for the end of the token. */
  3913. while (!endtoken (*dbp))
  3914. dbp++;
  3915. } /* advance char */
  3916. } /* advance line */
  3917. }
  3918. /*
  3919. * Unix and microcontroller assembly tag handling
  3920. * Labels: /^[a-zA-Z_.$][a-zA_Z0-9_.$]*[: ^I^J]/
  3921. * Idea by Bob Weiner, Motorola Inc. (1994)
  3922. */
  3923. static void
  3924. Asm_labels (FILE *inf)
  3925. {
  3926. register char *cp;
  3927. LOOP_ON_INPUT_LINES (inf, lb, cp)
  3928. {
  3929. /* If first char is alphabetic or one of [_.$], test for colon
  3930. following identifier. */
  3931. if (ISALPHA (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
  3932. {
  3933. /* Read past label. */
  3934. cp++;
  3935. while (ISALNUM (*cp) || *cp == '_' || *cp == '.' || *cp == '$')
  3936. cp++;
  3937. if (*cp == ':' || iswhite (*cp))
  3938. /* Found end of label, so copy it and add it to the table. */
  3939. make_tag (lb.buffer, cp - lb.buffer, TRUE,
  3940. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  3941. }
  3942. }
  3943. }
  3944. /*
  3945. * Perl support
  3946. * Perl sub names: /^sub[ \t\n]+[^ \t\n{]+/
  3947. * Perl variable names: /^(my|local).../
  3948. * Original code by Bart Robinson <lomew@cs.utah.edu> (1995)
  3949. * Additions by Michael Ernst <mernst@alum.mit.edu> (1997)
  3950. * Ideas by Kai Großjohann <Kai.Grossjohann@CS.Uni-Dortmund.DE> (2001)
  3951. */
  3952. static void
  3953. Perl_functions (FILE *inf)
  3954. {
  3955. char *package = savestr ("main"); /* current package name */
  3956. register char *cp;
  3957. LOOP_ON_INPUT_LINES (inf, lb, cp)
  3958. {
  3959. cp = skip_spaces (cp);
  3960. if (LOOKING_AT (cp, "package"))
  3961. {
  3962. free (package);
  3963. get_tag (cp, &package);
  3964. }
  3965. else if (LOOKING_AT (cp, "sub"))
  3966. {
  3967. char *pos;
  3968. char *sp = cp;
  3969. while (!notinname (*cp))
  3970. cp++;
  3971. if (cp == sp)
  3972. continue; /* nothing found */
  3973. if ((pos = etags_strchr (sp, ':')) != NULL
  3974. && pos < cp && pos[1] == ':')
  3975. /* The name is already qualified. */
  3976. make_tag (sp, cp - sp, TRUE,
  3977. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  3978. else
  3979. /* Qualify it. */
  3980. {
  3981. char savechar, *name;
  3982. savechar = *cp;
  3983. *cp = '\0';
  3984. name = concat (package, "::", sp);
  3985. *cp = savechar;
  3986. make_tag (name, strlen (name), TRUE,
  3987. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  3988. free (name);
  3989. }
  3990. }
  3991. else if (globals) /* only if we are tagging global vars */
  3992. {
  3993. /* Skip a qualifier, if any. */
  3994. bool qual = LOOKING_AT (cp, "my") || LOOKING_AT (cp, "local");
  3995. /* After "my" or "local", but before any following paren or space. */
  3996. char *varstart = cp;
  3997. if (qual /* should this be removed? If yes, how? */
  3998. && (*cp == '$' || *cp == '@' || *cp == '%'))
  3999. {
  4000. varstart += 1;
  4001. do
  4002. cp++;
  4003. while (ISALNUM (*cp) || *cp == '_');
  4004. }
  4005. else if (qual)
  4006. {
  4007. /* Should be examining a variable list at this point;
  4008. could insist on seeing an open parenthesis. */
  4009. while (*cp != '\0' && *cp != ';' && *cp != '=' && *cp != ')')
  4010. cp++;
  4011. }
  4012. else
  4013. continue;
  4014. make_tag (varstart, cp - varstart, FALSE,
  4015. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4016. }
  4017. }
  4018. free (package);
  4019. }
  4020. /*
  4021. * Python support
  4022. * Look for /^[\t]*def[ \t\n]+[^ \t\n(:]+/ or /^class[ \t\n]+[^ \t\n(:]+/
  4023. * Idea by Eric S. Raymond <esr@thyrsus.com> (1997)
  4024. * More ideas by seb bacon <seb@jamkit.com> (2002)
  4025. */
  4026. static void
  4027. Python_functions (FILE *inf)
  4028. {
  4029. register char *cp;
  4030. LOOP_ON_INPUT_LINES (inf, lb, cp)
  4031. {
  4032. cp = skip_spaces (cp);
  4033. if (LOOKING_AT (cp, "def") || LOOKING_AT (cp, "class"))
  4034. {
  4035. char *name = cp;
  4036. while (!notinname (*cp) && *cp != ':')
  4037. cp++;
  4038. make_tag (name, cp - name, TRUE,
  4039. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4040. }
  4041. }
  4042. }
  4043. /*
  4044. * PHP support
  4045. * Look for:
  4046. * - /^[ \t]*function[ \t\n]+[^ \t\n(]+/
  4047. * - /^[ \t]*class[ \t\n]+[^ \t\n]+/
  4048. * - /^[ \t]*define\(\"[^\"]+/
  4049. * Only with --members:
  4050. * - /^[ \t]*var[ \t\n]+\$[^ \t\n=;]/
  4051. * Idea by Diez B. Roggisch (2001)
  4052. */
  4053. static void
  4054. PHP_functions (FILE *inf)
  4055. {
  4056. register char *cp, *name;
  4057. bool search_identifier = FALSE;
  4058. LOOP_ON_INPUT_LINES (inf, lb, cp)
  4059. {
  4060. cp = skip_spaces (cp);
  4061. name = cp;
  4062. if (search_identifier
  4063. && *cp != '\0')
  4064. {
  4065. while (!notinname (*cp))
  4066. cp++;
  4067. make_tag (name, cp - name, TRUE,
  4068. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4069. search_identifier = FALSE;
  4070. }
  4071. else if (LOOKING_AT (cp, "function"))
  4072. {
  4073. if (*cp == '&')
  4074. cp = skip_spaces (cp+1);
  4075. if (*cp != '\0')
  4076. {
  4077. name = cp;
  4078. while (!notinname (*cp))
  4079. cp++;
  4080. make_tag (name, cp - name, TRUE,
  4081. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4082. }
  4083. else
  4084. search_identifier = TRUE;
  4085. }
  4086. else if (LOOKING_AT (cp, "class"))
  4087. {
  4088. if (*cp != '\0')
  4089. {
  4090. name = cp;
  4091. while (*cp != '\0' && !iswhite (*cp))
  4092. cp++;
  4093. make_tag (name, cp - name, FALSE,
  4094. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4095. }
  4096. else
  4097. search_identifier = TRUE;
  4098. }
  4099. else if (strneq (cp, "define", 6)
  4100. && (cp = skip_spaces (cp+6))
  4101. && *cp++ == '('
  4102. && (*cp == '"' || *cp == '\''))
  4103. {
  4104. char quote = *cp++;
  4105. name = cp;
  4106. while (*cp != quote && *cp != '\0')
  4107. cp++;
  4108. make_tag (name, cp - name, FALSE,
  4109. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4110. }
  4111. else if (members
  4112. && LOOKING_AT (cp, "var")
  4113. && *cp == '$')
  4114. {
  4115. name = cp;
  4116. while (!notinname (*cp))
  4117. cp++;
  4118. make_tag (name, cp - name, FALSE,
  4119. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4120. }
  4121. }
  4122. }
  4123. /*
  4124. * Cobol tag functions
  4125. * We could look for anything that could be a paragraph name.
  4126. * i.e. anything that starts in column 8 is one word and ends in a full stop.
  4127. * Idea by Corny de Souza (1993)
  4128. */
  4129. static void
  4130. Cobol_paragraphs (FILE *inf)
  4131. {
  4132. register char *bp, *ep;
  4133. LOOP_ON_INPUT_LINES (inf, lb, bp)
  4134. {
  4135. if (lb.len < 9)
  4136. continue;
  4137. bp += 8;
  4138. /* If eoln, compiler option or comment ignore whole line. */
  4139. if (bp[-1] != ' ' || !ISALNUM (bp[0]))
  4140. continue;
  4141. for (ep = bp; ISALNUM (*ep) || *ep == '-'; ep++)
  4142. continue;
  4143. if (*ep++ == '.')
  4144. make_tag (bp, ep - bp, TRUE,
  4145. lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
  4146. }
  4147. }
  4148. /*
  4149. * Makefile support
  4150. * Ideas by Assar Westerlund <assar@sics.se> (2001)
  4151. */
  4152. static void
  4153. Makefile_targets (FILE *inf)
  4154. {
  4155. register char *bp;
  4156. LOOP_ON_INPUT_LINES (inf, lb, bp)
  4157. {
  4158. if (*bp == '\t' || *bp == '#')
  4159. continue;
  4160. while (*bp != '\0' && *bp != '=' && *bp != ':')
  4161. bp++;
  4162. if (*bp == ':' || (globals && *bp == '='))
  4163. {
  4164. /* We should detect if there is more than one tag, but we do not.
  4165. We just skip initial and final spaces. */
  4166. char * namestart = skip_spaces (lb.buffer);
  4167. while (--bp > namestart)
  4168. if (!notinname (*bp))
  4169. break;
  4170. make_tag (namestart, bp - namestart + 1, TRUE,
  4171. lb.buffer, bp - lb.buffer + 2, lineno, linecharno);
  4172. }
  4173. }
  4174. }
  4175. /*
  4176. * Pascal parsing
  4177. * Original code by Mosur K. Mohan (1989)
  4178. *
  4179. * Locates tags for procedures & functions. Doesn't do any type- or
  4180. * var-definitions. It does look for the keyword "extern" or
  4181. * "forward" immediately following the procedure statement; if found,
  4182. * the tag is skipped.
  4183. */
  4184. static void
  4185. Pascal_functions (FILE *inf)
  4186. {
  4187. linebuffer tline; /* mostly copied from C_entries */
  4188. long save_lcno;
  4189. int save_lineno, namelen, taglen;
  4190. char c, *name;
  4191. bool /* each of these flags is TRUE if: */
  4192. incomment, /* point is inside a comment */
  4193. inquote, /* point is inside '..' string */
  4194. get_tagname, /* point is after PROCEDURE/FUNCTION
  4195. keyword, so next item = potential tag */
  4196. found_tag, /* point is after a potential tag */
  4197. inparms, /* point is within parameter-list */
  4198. verify_tag; /* point has passed the parm-list, so the
  4199. next token will determine whether this
  4200. is a FORWARD/EXTERN to be ignored, or
  4201. whether it is a real tag */
  4202. save_lcno = save_lineno = namelen = taglen = 0; /* keep compiler quiet */
  4203. name = NULL; /* keep compiler quiet */
  4204. dbp = lb.buffer;
  4205. *dbp = '\0';
  4206. linebuffer_init (&tline);
  4207. incomment = inquote = FALSE;
  4208. found_tag = FALSE; /* have a proc name; check if extern */
  4209. get_tagname = FALSE; /* found "procedure" keyword */
  4210. inparms = FALSE; /* found '(' after "proc" */
  4211. verify_tag = FALSE; /* check if "extern" is ahead */
  4212. while (!feof (inf)) /* long main loop to get next char */
  4213. {
  4214. c = *dbp++;
  4215. if (c == '\0') /* if end of line */
  4216. {
  4217. readline (&lb, inf);
  4218. dbp = lb.buffer;
  4219. if (*dbp == '\0')
  4220. continue;
  4221. if (!((found_tag && verify_tag)
  4222. || get_tagname))
  4223. c = *dbp++; /* only if don't need *dbp pointing
  4224. to the beginning of the name of
  4225. the procedure or function */
  4226. }
  4227. if (incomment)
  4228. {
  4229. if (c == '}') /* within { } comments */
  4230. incomment = FALSE;
  4231. else if (c == '*' && *dbp == ')') /* within (* *) comments */
  4232. {
  4233. dbp++;
  4234. incomment = FALSE;
  4235. }
  4236. continue;
  4237. }
  4238. else if (inquote)
  4239. {
  4240. if (c == '\'')
  4241. inquote = FALSE;
  4242. continue;
  4243. }
  4244. else
  4245. switch (c)
  4246. {
  4247. case '\'':
  4248. inquote = TRUE; /* found first quote */
  4249. continue;
  4250. case '{': /* found open { comment */
  4251. incomment = TRUE;
  4252. continue;
  4253. case '(':
  4254. if (*dbp == '*') /* found open (* comment */
  4255. {
  4256. incomment = TRUE;
  4257. dbp++;
  4258. }
  4259. else if (found_tag) /* found '(' after tag, i.e., parm-list */
  4260. inparms = TRUE;
  4261. continue;
  4262. case ')': /* end of parms list */
  4263. if (inparms)
  4264. inparms = FALSE;
  4265. continue;
  4266. case ';':
  4267. if (found_tag && !inparms) /* end of proc or fn stmt */
  4268. {
  4269. verify_tag = TRUE;
  4270. break;
  4271. }
  4272. continue;
  4273. }
  4274. if (found_tag && verify_tag && (*dbp != ' '))
  4275. {
  4276. /* Check if this is an "extern" declaration. */
  4277. if (*dbp == '\0')
  4278. continue;
  4279. if (lowcase (*dbp == 'e'))
  4280. {
  4281. if (nocase_tail ("extern")) /* superfluous, really! */
  4282. {
  4283. found_tag = FALSE;
  4284. verify_tag = FALSE;
  4285. }
  4286. }
  4287. else if (lowcase (*dbp) == 'f')
  4288. {
  4289. if (nocase_tail ("forward")) /* check for forward reference */
  4290. {
  4291. found_tag = FALSE;
  4292. verify_tag = FALSE;
  4293. }
  4294. }
  4295. if (found_tag && verify_tag) /* not external proc, so make tag */
  4296. {
  4297. found_tag = FALSE;
  4298. verify_tag = FALSE;
  4299. make_tag (name, namelen, TRUE,
  4300. tline.buffer, taglen, save_lineno, save_lcno);
  4301. continue;
  4302. }
  4303. }
  4304. if (get_tagname) /* grab name of proc or fn */
  4305. {
  4306. char *cp;
  4307. if (*dbp == '\0')
  4308. continue;
  4309. /* Find block name. */
  4310. for (cp = dbp + 1; *cp != '\0' && !endtoken (*cp); cp++)
  4311. continue;
  4312. /* Save all values for later tagging. */
  4313. linebuffer_setlen (&tline, lb.len);
  4314. strcpy (tline.buffer, lb.buffer);
  4315. save_lineno = lineno;
  4316. save_lcno = linecharno;
  4317. name = tline.buffer + (dbp - lb.buffer);
  4318. namelen = cp - dbp;
  4319. taglen = cp - lb.buffer + 1;
  4320. dbp = cp; /* set dbp to e-o-token */
  4321. get_tagname = FALSE;
  4322. found_tag = TRUE;
  4323. continue;
  4324. /* And proceed to check for "extern". */
  4325. }
  4326. else if (!incomment && !inquote && !found_tag)
  4327. {
  4328. /* Check for proc/fn keywords. */
  4329. switch (lowcase (c))
  4330. {
  4331. case 'p':
  4332. if (nocase_tail ("rocedure")) /* c = 'p', dbp has advanced */
  4333. get_tagname = TRUE;
  4334. continue;
  4335. case 'f':
  4336. if (nocase_tail ("unction"))
  4337. get_tagname = TRUE;
  4338. continue;
  4339. }
  4340. }
  4341. } /* while not eof */
  4342. free (tline.buffer);
  4343. }
  4344. /*
  4345. * Lisp tag functions
  4346. * look for (def or (DEF, quote or QUOTE
  4347. */
  4348. static void L_getit (void);
  4349. static void
  4350. L_getit (void)
  4351. {
  4352. if (*dbp == '\'') /* Skip prefix quote */
  4353. dbp++;
  4354. else if (*dbp == '(')
  4355. {
  4356. dbp++;
  4357. /* Try to skip "(quote " */
  4358. if (!LOOKING_AT (dbp, "quote") && !LOOKING_AT (dbp, "QUOTE"))
  4359. /* Ok, then skip "(" before name in (defstruct (foo)) */
  4360. dbp = skip_spaces (dbp);
  4361. }
  4362. get_tag (dbp, NULL);
  4363. }
  4364. static void
  4365. Lisp_functions (FILE *inf)
  4366. {
  4367. LOOP_ON_INPUT_LINES (inf, lb, dbp)
  4368. {
  4369. if (dbp[0] != '(')
  4370. continue;
  4371. if (strneq (dbp+1, "def", 3) || strneq (dbp+1, "DEF", 3))
  4372. {
  4373. dbp = skip_non_spaces (dbp);
  4374. dbp = skip_spaces (dbp);
  4375. L_getit ();
  4376. }
  4377. else
  4378. {
  4379. /* Check for (foo::defmumble name-defined ... */
  4380. do
  4381. dbp++;
  4382. while (!notinname (*dbp) && *dbp != ':');
  4383. if (*dbp == ':')
  4384. {
  4385. do
  4386. dbp++;
  4387. while (*dbp == ':');
  4388. if (strneq (dbp, "def", 3) || strneq (dbp, "DEF", 3))
  4389. {
  4390. dbp = skip_non_spaces (dbp);
  4391. dbp = skip_spaces (dbp);
  4392. L_getit ();
  4393. }
  4394. }
  4395. }
  4396. }
  4397. }
  4398. /*
  4399. * Lua script language parsing
  4400. * Original code by David A. Capello <dacap@users.sourceforge.net> (2004)
  4401. *
  4402. * "function" and "local function" are tags if they start at column 1.
  4403. */
  4404. static void
  4405. Lua_functions (FILE *inf)
  4406. {
  4407. register char *bp;
  4408. LOOP_ON_INPUT_LINES (inf, lb, bp)
  4409. {
  4410. if (bp[0] != 'f' && bp[0] != 'l')
  4411. continue;
  4412. (void)LOOKING_AT (bp, "local"); /* skip possible "local" */
  4413. if (LOOKING_AT (bp, "function"))
  4414. get_tag (bp, NULL);
  4415. }
  4416. }
  4417. /*
  4418. * PostScript tags
  4419. * Just look for lines where the first character is '/'
  4420. * Also look at "defineps" for PSWrap
  4421. * Ideas by:
  4422. * Richard Mlynarik <mly@adoc.xerox.com> (1997)
  4423. * Masatake Yamato <masata-y@is.aist-nara.ac.jp> (1999)
  4424. */
  4425. static void
  4426. PS_functions (FILE *inf)
  4427. {
  4428. register char *bp, *ep;
  4429. LOOP_ON_INPUT_LINES (inf, lb, bp)
  4430. {
  4431. if (bp[0] == '/')
  4432. {
  4433. for (ep = bp+1;
  4434. *ep != '\0' && *ep != ' ' && *ep != '{';
  4435. ep++)
  4436. continue;
  4437. make_tag (bp, ep - bp, TRUE,
  4438. lb.buffer, ep - lb.buffer + 1, lineno, linecharno);
  4439. }
  4440. else if (LOOKING_AT (bp, "defineps"))
  4441. get_tag (bp, NULL);
  4442. }
  4443. }
  4444. /*
  4445. * Forth tags
  4446. * Ignore anything after \ followed by space or in ( )
  4447. * Look for words defined by :
  4448. * Look for constant, code, create, defer, value, and variable
  4449. * OBP extensions: Look for buffer:, field,
  4450. * Ideas by Eduardo Horvath <eeh@netbsd.org> (2004)
  4451. */
  4452. static void
  4453. Forth_words (FILE *inf)
  4454. {
  4455. register char *bp;
  4456. LOOP_ON_INPUT_LINES (inf, lb, bp)
  4457. while ((bp = skip_spaces (bp))[0] != '\0')
  4458. if (bp[0] == '\\' && iswhite (bp[1]))
  4459. break; /* read next line */
  4460. else if (bp[0] == '(' && iswhite (bp[1]))
  4461. do /* skip to ) or eol */
  4462. bp++;
  4463. while (*bp != ')' && *bp != '\0');
  4464. else if ((bp[0] == ':' && iswhite (bp[1]) && bp++)
  4465. || LOOKING_AT_NOCASE (bp, "constant")
  4466. || LOOKING_AT_NOCASE (bp, "code")
  4467. || LOOKING_AT_NOCASE (bp, "create")
  4468. || LOOKING_AT_NOCASE (bp, "defer")
  4469. || LOOKING_AT_NOCASE (bp, "value")
  4470. || LOOKING_AT_NOCASE (bp, "variable")
  4471. || LOOKING_AT_NOCASE (bp, "buffer:")
  4472. || LOOKING_AT_NOCASE (bp, "field"))
  4473. get_tag (skip_spaces (bp), NULL); /* Yay! A definition! */
  4474. else
  4475. bp = skip_non_spaces (bp);
  4476. }
  4477. /*
  4478. * Scheme tag functions
  4479. * look for (def... xyzzy
  4480. * (def... (xyzzy
  4481. * (def ... ((...(xyzzy ....
  4482. * (set! xyzzy
  4483. * Original code by Ken Haase (1985?)
  4484. */
  4485. static void
  4486. Scheme_functions (FILE *inf)
  4487. {
  4488. register char *bp;
  4489. LOOP_ON_INPUT_LINES (inf, lb, bp)
  4490. {
  4491. if (strneq (bp, "(def", 4) || strneq (bp, "(DEF", 4))
  4492. {
  4493. bp = skip_non_spaces (bp+4);
  4494. /* Skip over open parens and white space. Don't continue past
  4495. '\0'. */
  4496. while (*bp && notinname (*bp))
  4497. bp++;
  4498. get_tag (bp, NULL);
  4499. }
  4500. if (LOOKING_AT (bp, "(SET!") || LOOKING_AT (bp, "(set!"))
  4501. get_tag (bp, NULL);
  4502. }
  4503. }
  4504. /* Find tags in TeX and LaTeX input files. */
  4505. /* TEX_toktab is a table of TeX control sequences that define tags.
  4506. * Each entry records one such control sequence.
  4507. *
  4508. * Original code from who knows whom.
  4509. * Ideas by:
  4510. * Stefan Monnier (2002)
  4511. */
  4512. static linebuffer *TEX_toktab = NULL; /* Table with tag tokens */
  4513. /* Default set of control sequences to put into TEX_toktab.
  4514. The value of environment var TEXTAGS is prepended to this. */
  4515. static const char *TEX_defenv = "\
  4516. :chapter:section:subsection:subsubsection:eqno:label:ref:cite:bibitem\
  4517. :part:appendix:entry:index:def\
  4518. :newcommand:renewcommand:newenvironment:renewenvironment";
  4519. static void TEX_mode (FILE *);
  4520. static void TEX_decode_env (const char *, const char *);
  4521. static char TEX_esc = '\\';
  4522. static char TEX_opgrp = '{';
  4523. static char TEX_clgrp = '}';
  4524. /*
  4525. * TeX/LaTeX scanning loop.
  4526. */
  4527. static void
  4528. TeX_commands (FILE *inf)
  4529. {
  4530. char *cp;
  4531. linebuffer *key;
  4532. /* Select either \ or ! as escape character. */
  4533. TEX_mode (inf);
  4534. /* Initialize token table once from environment. */
  4535. if (TEX_toktab == NULL)
  4536. TEX_decode_env ("TEXTAGS", TEX_defenv);
  4537. LOOP_ON_INPUT_LINES (inf, lb, cp)
  4538. {
  4539. /* Look at each TEX keyword in line. */
  4540. for (;;)
  4541. {
  4542. /* Look for a TEX escape. */
  4543. while (*cp++ != TEX_esc)
  4544. if (cp[-1] == '\0' || cp[-1] == '%')
  4545. goto tex_next_line;
  4546. for (key = TEX_toktab; key->buffer != NULL; key++)
  4547. if (strneq (cp, key->buffer, key->len))
  4548. {
  4549. register char *p;
  4550. int namelen, linelen;
  4551. bool opgrp = FALSE;
  4552. cp = skip_spaces (cp + key->len);
  4553. if (*cp == TEX_opgrp)
  4554. {
  4555. opgrp = TRUE;
  4556. cp++;
  4557. }
  4558. for (p = cp;
  4559. (!iswhite (*p) && *p != '#' &&
  4560. *p != TEX_opgrp && *p != TEX_clgrp);
  4561. p++)
  4562. continue;
  4563. namelen = p - cp;
  4564. linelen = lb.len;
  4565. if (!opgrp || *p == TEX_clgrp)
  4566. {
  4567. while (*p != '\0' && *p != TEX_opgrp && *p != TEX_clgrp)
  4568. p++;
  4569. linelen = p - lb.buffer + 1;
  4570. }
  4571. make_tag (cp, namelen, TRUE,
  4572. lb.buffer, linelen, lineno, linecharno);
  4573. goto tex_next_line; /* We only tag a line once */
  4574. }
  4575. }
  4576. tex_next_line:
  4577. ;
  4578. }
  4579. }
  4580. #define TEX_LESC '\\'
  4581. #define TEX_SESC '!'
  4582. /* Figure out whether TeX's escapechar is '\\' or '!' and set grouping
  4583. chars accordingly. */
  4584. static void
  4585. TEX_mode (FILE *inf)
  4586. {
  4587. int c;
  4588. while ((c = getc (inf)) != EOF)
  4589. {
  4590. /* Skip to next line if we hit the TeX comment char. */
  4591. if (c == '%')
  4592. while (c != '\n' && c != EOF)
  4593. c = getc (inf);
  4594. else if (c == TEX_LESC || c == TEX_SESC )
  4595. break;
  4596. }
  4597. if (c == TEX_LESC)
  4598. {
  4599. TEX_esc = TEX_LESC;
  4600. TEX_opgrp = '{';
  4601. TEX_clgrp = '}';
  4602. }
  4603. else
  4604. {
  4605. TEX_esc = TEX_SESC;
  4606. TEX_opgrp = '<';
  4607. TEX_clgrp = '>';
  4608. }
  4609. /* If the input file is compressed, inf is a pipe, and rewind may fail.
  4610. No attempt is made to correct the situation. */
  4611. rewind (inf);
  4612. }
  4613. /* Read environment and prepend it to the default string.
  4614. Build token table. */
  4615. static void
  4616. TEX_decode_env (const char *evarname, const char *defenv)
  4617. {
  4618. register const char *env, *p;
  4619. int i, len;
  4620. /* Append default string to environment. */
  4621. env = getenv (evarname);
  4622. if (!env)
  4623. env = defenv;
  4624. else
  4625. env = concat (env, defenv, "");
  4626. /* Allocate a token table */
  4627. for (len = 1, p = env; p;)
  4628. if ((p = etags_strchr (p, ':')) && *++p != '\0')
  4629. len++;
  4630. TEX_toktab = xnew (len, linebuffer);
  4631. /* Unpack environment string into token table. Be careful about */
  4632. /* zero-length strings (leading ':', "::" and trailing ':') */
  4633. for (i = 0; *env != '\0';)
  4634. {
  4635. p = etags_strchr (env, ':');
  4636. if (!p) /* End of environment string. */
  4637. p = env + strlen (env);
  4638. if (p - env > 0)
  4639. { /* Only non-zero strings. */
  4640. TEX_toktab[i].buffer = savenstr (env, p - env);
  4641. TEX_toktab[i].len = p - env;
  4642. i++;
  4643. }
  4644. if (*p)
  4645. env = p + 1;
  4646. else
  4647. {
  4648. TEX_toktab[i].buffer = NULL; /* Mark end of table. */
  4649. TEX_toktab[i].len = 0;
  4650. break;
  4651. }
  4652. }
  4653. }
  4654. /* Texinfo support. Dave Love, Mar. 2000. */
  4655. static void
  4656. Texinfo_nodes (FILE *inf)
  4657. {
  4658. char *cp, *start;
  4659. LOOP_ON_INPUT_LINES (inf, lb, cp)
  4660. if (LOOKING_AT (cp, "@node"))
  4661. {
  4662. start = cp;
  4663. while (*cp != '\0' && *cp != ',')
  4664. cp++;
  4665. make_tag (start, cp - start, TRUE,
  4666. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  4667. }
  4668. }
  4669. /*
  4670. * HTML support.
  4671. * Contents of <title>, <h1>, <h2>, <h3> are tags.
  4672. * Contents of <a name=xxx> are tags with name xxx.
  4673. *
  4674. * Francesco Potortì, 2002.
  4675. */
  4676. static void
  4677. HTML_labels (FILE *inf)
  4678. {
  4679. bool getnext = FALSE; /* next text outside of HTML tags is a tag */
  4680. bool skiptag = FALSE; /* skip to the end of the current HTML tag */
  4681. bool intag = FALSE; /* inside an html tag, looking for ID= */
  4682. bool inanchor = FALSE; /* when INTAG, is an anchor, look for NAME= */
  4683. char *end;
  4684. linebuffer_setlen (&token_name, 0); /* no name in buffer */
  4685. LOOP_ON_INPUT_LINES (inf, lb, dbp)
  4686. for (;;) /* loop on the same line */
  4687. {
  4688. if (skiptag) /* skip HTML tag */
  4689. {
  4690. while (*dbp != '\0' && *dbp != '>')
  4691. dbp++;
  4692. if (*dbp == '>')
  4693. {
  4694. dbp += 1;
  4695. skiptag = FALSE;
  4696. continue; /* look on the same line */
  4697. }
  4698. break; /* go to next line */
  4699. }
  4700. else if (intag) /* look for "name=" or "id=" */
  4701. {
  4702. while (*dbp != '\0' && *dbp != '>'
  4703. && lowcase (*dbp) != 'n' && lowcase (*dbp) != 'i')
  4704. dbp++;
  4705. if (*dbp == '\0')
  4706. break; /* go to next line */
  4707. if (*dbp == '>')
  4708. {
  4709. dbp += 1;
  4710. intag = FALSE;
  4711. continue; /* look on the same line */
  4712. }
  4713. if ((inanchor && LOOKING_AT_NOCASE (dbp, "name="))
  4714. || LOOKING_AT_NOCASE (dbp, "id="))
  4715. {
  4716. bool quoted = (dbp[0] == '"');
  4717. if (quoted)
  4718. for (end = ++dbp; *end != '\0' && *end != '"'; end++)
  4719. continue;
  4720. else
  4721. for (end = dbp; *end != '\0' && intoken (*end); end++)
  4722. continue;
  4723. linebuffer_setlen (&token_name, end - dbp);
  4724. strncpy (token_name.buffer, dbp, end - dbp);
  4725. token_name.buffer[end - dbp] = '\0';
  4726. dbp = end;
  4727. intag = FALSE; /* we found what we looked for */
  4728. skiptag = TRUE; /* skip to the end of the tag */
  4729. getnext = TRUE; /* then grab the text */
  4730. continue; /* look on the same line */
  4731. }
  4732. dbp += 1;
  4733. }
  4734. else if (getnext) /* grab next tokens and tag them */
  4735. {
  4736. dbp = skip_spaces (dbp);
  4737. if (*dbp == '\0')
  4738. break; /* go to next line */
  4739. if (*dbp == '<')
  4740. {
  4741. intag = TRUE;
  4742. inanchor = (lowcase (dbp[1]) == 'a' && !intoken (dbp[2]));
  4743. continue; /* look on the same line */
  4744. }
  4745. for (end = dbp + 1; *end != '\0' && *end != '<'; end++)
  4746. continue;
  4747. make_tag (token_name.buffer, token_name.len, TRUE,
  4748. dbp, end - dbp, lineno, linecharno);
  4749. linebuffer_setlen (&token_name, 0); /* no name in buffer */
  4750. getnext = FALSE;
  4751. break; /* go to next line */
  4752. }
  4753. else /* look for an interesting HTML tag */
  4754. {
  4755. while (*dbp != '\0' && *dbp != '<')
  4756. dbp++;
  4757. if (*dbp == '\0')
  4758. break; /* go to next line */
  4759. intag = TRUE;
  4760. if (lowcase (dbp[1]) == 'a' && !intoken (dbp[2]))
  4761. {
  4762. inanchor = TRUE;
  4763. continue; /* look on the same line */
  4764. }
  4765. else if (LOOKING_AT_NOCASE (dbp, "<title>")
  4766. || LOOKING_AT_NOCASE (dbp, "<h1>")
  4767. || LOOKING_AT_NOCASE (dbp, "<h2>")
  4768. || LOOKING_AT_NOCASE (dbp, "<h3>"))
  4769. {
  4770. intag = FALSE;
  4771. getnext = TRUE;
  4772. continue; /* look on the same line */
  4773. }
  4774. dbp += 1;
  4775. }
  4776. }
  4777. }
  4778. /*
  4779. * Prolog support
  4780. *
  4781. * Assumes that the predicate or rule starts at column 0.
  4782. * Only the first clause of a predicate or rule is added.
  4783. * Original code by Sunichirou Sugou (1989)
  4784. * Rewritten by Anders Lindgren (1996)
  4785. */
  4786. static size_t prolog_pr (char *, char *);
  4787. static void prolog_skip_comment (linebuffer *, FILE *);
  4788. static size_t prolog_atom (char *, size_t);
  4789. static void
  4790. Prolog_functions (FILE *inf)
  4791. {
  4792. char *cp, *last;
  4793. size_t len;
  4794. size_t allocated;
  4795. allocated = 0;
  4796. len = 0;
  4797. last = NULL;
  4798. LOOP_ON_INPUT_LINES (inf, lb, cp)
  4799. {
  4800. if (cp[0] == '\0') /* Empty line */
  4801. continue;
  4802. else if (iswhite (cp[0])) /* Not a predicate */
  4803. continue;
  4804. else if (cp[0] == '/' && cp[1] == '*') /* comment. */
  4805. prolog_skip_comment (&lb, inf);
  4806. else if ((len = prolog_pr (cp, last)) > 0)
  4807. {
  4808. /* Predicate or rule. Store the function name so that we
  4809. only generate a tag for the first clause. */
  4810. if (last == NULL)
  4811. last = xnew (len + 1, char);
  4812. else if (len + 1 > allocated)
  4813. xrnew (last, len + 1, char);
  4814. allocated = len + 1;
  4815. strncpy (last, cp, len);
  4816. last[len] = '\0';
  4817. }
  4818. }
  4819. free (last);
  4820. }
  4821. static void
  4822. prolog_skip_comment (linebuffer *plb, FILE *inf)
  4823. {
  4824. char *cp;
  4825. do
  4826. {
  4827. for (cp = plb->buffer; *cp != '\0'; cp++)
  4828. if (cp[0] == '*' && cp[1] == '/')
  4829. return;
  4830. readline (plb, inf);
  4831. }
  4832. while (!feof (inf));
  4833. }
  4834. /*
  4835. * A predicate or rule definition is added if it matches:
  4836. * <beginning of line><Prolog Atom><whitespace>(
  4837. * or <beginning of line><Prolog Atom><whitespace>:-
  4838. *
  4839. * It is added to the tags database if it doesn't match the
  4840. * name of the previous clause header.
  4841. *
  4842. * Return the size of the name of the predicate or rule, or 0 if no
  4843. * header was found.
  4844. */
  4845. static size_t
  4846. prolog_pr (char *s, char *last)
  4847. /* Name of last clause. */
  4848. {
  4849. size_t pos;
  4850. size_t len;
  4851. pos = prolog_atom (s, 0);
  4852. if (! pos)
  4853. return 0;
  4854. len = pos;
  4855. pos = skip_spaces (s + pos) - s;
  4856. if ((s[pos] == '.'
  4857. || (s[pos] == '(' && (pos += 1))
  4858. || (s[pos] == ':' && s[pos + 1] == '-' && (pos += 2)))
  4859. && (last == NULL /* save only the first clause */
  4860. || len != strlen (last)
  4861. || !strneq (s, last, len)))
  4862. {
  4863. make_tag (s, len, TRUE, s, pos, lineno, linecharno);
  4864. return len;
  4865. }
  4866. else
  4867. return 0;
  4868. }
  4869. /*
  4870. * Consume a Prolog atom.
  4871. * Return the number of bytes consumed, or 0 if there was an error.
  4872. *
  4873. * A prolog atom, in this context, could be one of:
  4874. * - An alphanumeric sequence, starting with a lower case letter.
  4875. * - A quoted arbitrary string. Single quotes can escape themselves.
  4876. * Backslash quotes everything.
  4877. */
  4878. static size_t
  4879. prolog_atom (char *s, size_t pos)
  4880. {
  4881. size_t origpos;
  4882. origpos = pos;
  4883. if (ISLOWER (s[pos]) || (s[pos] == '_'))
  4884. {
  4885. /* The atom is unquoted. */
  4886. pos++;
  4887. while (ISALNUM (s[pos]) || (s[pos] == '_'))
  4888. {
  4889. pos++;
  4890. }
  4891. return pos - origpos;
  4892. }
  4893. else if (s[pos] == '\'')
  4894. {
  4895. pos++;
  4896. for (;;)
  4897. {
  4898. if (s[pos] == '\'')
  4899. {
  4900. pos++;
  4901. if (s[pos] != '\'')
  4902. break;
  4903. pos++; /* A double quote */
  4904. }
  4905. else if (s[pos] == '\0')
  4906. /* Multiline quoted atoms are ignored. */
  4907. return 0;
  4908. else if (s[pos] == '\\')
  4909. {
  4910. if (s[pos+1] == '\0')
  4911. return 0;
  4912. pos += 2;
  4913. }
  4914. else
  4915. pos++;
  4916. }
  4917. return pos - origpos;
  4918. }
  4919. else
  4920. return 0;
  4921. }
  4922. /*
  4923. * Support for Erlang
  4924. *
  4925. * Generates tags for functions, defines, and records.
  4926. * Assumes that Erlang functions start at column 0.
  4927. * Original code by Anders Lindgren (1996)
  4928. */
  4929. static int erlang_func (char *, char *);
  4930. static void erlang_attribute (char *);
  4931. static int erlang_atom (char *);
  4932. static void
  4933. Erlang_functions (FILE *inf)
  4934. {
  4935. char *cp, *last;
  4936. int len;
  4937. int allocated;
  4938. allocated = 0;
  4939. len = 0;
  4940. last = NULL;
  4941. LOOP_ON_INPUT_LINES (inf, lb, cp)
  4942. {
  4943. if (cp[0] == '\0') /* Empty line */
  4944. continue;
  4945. else if (iswhite (cp[0])) /* Not function nor attribute */
  4946. continue;
  4947. else if (cp[0] == '%') /* comment */
  4948. continue;
  4949. else if (cp[0] == '"') /* Sometimes, strings start in column one */
  4950. continue;
  4951. else if (cp[0] == '-') /* attribute, e.g. "-define" */
  4952. {
  4953. erlang_attribute (cp);
  4954. if (last != NULL)
  4955. {
  4956. free (last);
  4957. last = NULL;
  4958. }
  4959. }
  4960. else if ((len = erlang_func (cp, last)) > 0)
  4961. {
  4962. /*
  4963. * Function. Store the function name so that we only
  4964. * generates a tag for the first clause.
  4965. */
  4966. if (last == NULL)
  4967. last = xnew (len + 1, char);
  4968. else if (len + 1 > allocated)
  4969. xrnew (last, len + 1, char);
  4970. allocated = len + 1;
  4971. strncpy (last, cp, len);
  4972. last[len] = '\0';
  4973. }
  4974. }
  4975. free (last);
  4976. }
  4977. /*
  4978. * A function definition is added if it matches:
  4979. * <beginning of line><Erlang Atom><whitespace>(
  4980. *
  4981. * It is added to the tags database if it doesn't match the
  4982. * name of the previous clause header.
  4983. *
  4984. * Return the size of the name of the function, or 0 if no function
  4985. * was found.
  4986. */
  4987. static int
  4988. erlang_func (char *s, char *last)
  4989. /* Name of last clause. */
  4990. {
  4991. int pos;
  4992. int len;
  4993. pos = erlang_atom (s);
  4994. if (pos < 1)
  4995. return 0;
  4996. len = pos;
  4997. pos = skip_spaces (s + pos) - s;
  4998. /* Save only the first clause. */
  4999. if (s[pos++] == '('
  5000. && (last == NULL
  5001. || len != (int)strlen (last)
  5002. || !strneq (s, last, len)))
  5003. {
  5004. make_tag (s, len, TRUE, s, pos, lineno, linecharno);
  5005. return len;
  5006. }
  5007. return 0;
  5008. }
  5009. /*
  5010. * Handle attributes. Currently, tags are generated for defines
  5011. * and records.
  5012. *
  5013. * They are on the form:
  5014. * -define(foo, bar).
  5015. * -define(Foo(M, N), M+N).
  5016. * -record(graph, {vtab = notable, cyclic = true}).
  5017. */
  5018. static void
  5019. erlang_attribute (char *s)
  5020. {
  5021. char *cp = s;
  5022. if ((LOOKING_AT (cp, "-define") || LOOKING_AT (cp, "-record"))
  5023. && *cp++ == '(')
  5024. {
  5025. int len = erlang_atom (skip_spaces (cp));
  5026. if (len > 0)
  5027. make_tag (cp, len, TRUE, s, cp + len - s, lineno, linecharno);
  5028. }
  5029. return;
  5030. }
  5031. /*
  5032. * Consume an Erlang atom (or variable).
  5033. * Return the number of bytes consumed, or -1 if there was an error.
  5034. */
  5035. static int
  5036. erlang_atom (char *s)
  5037. {
  5038. int pos = 0;
  5039. if (ISALPHA (s[pos]) || s[pos] == '_')
  5040. {
  5041. /* The atom is unquoted. */
  5042. do
  5043. pos++;
  5044. while (ISALNUM (s[pos]) || s[pos] == '_');
  5045. }
  5046. else if (s[pos] == '\'')
  5047. {
  5048. for (pos++; s[pos] != '\''; pos++)
  5049. if (s[pos] == '\0' /* multiline quoted atoms are ignored */
  5050. || (s[pos] == '\\' && s[++pos] == '\0'))
  5051. return 0;
  5052. pos++;
  5053. }
  5054. return pos;
  5055. }
  5056. static char *scan_separators (char *);
  5057. static void add_regex (char *, language *);
  5058. static char *substitute (char *, char *, struct re_registers *);
  5059. /*
  5060. * Take a string like "/blah/" and turn it into "blah", verifying
  5061. * that the first and last characters are the same, and handling
  5062. * quoted separator characters. Actually, stops on the occurrence of
  5063. * an unquoted separator. Also process \t, \n, etc. and turn into
  5064. * appropriate characters. Works in place. Null terminates name string.
  5065. * Returns pointer to terminating separator, or NULL for
  5066. * unterminated regexps.
  5067. */
  5068. static char *
  5069. scan_separators (char *name)
  5070. {
  5071. char sep = name[0];
  5072. char *copyto = name;
  5073. bool quoted = FALSE;
  5074. for (++name; *name != '\0'; ++name)
  5075. {
  5076. if (quoted)
  5077. {
  5078. switch (*name)
  5079. {
  5080. case 'a': *copyto++ = '\007'; break; /* BEL (bell) */
  5081. case 'b': *copyto++ = '\b'; break; /* BS (back space) */
  5082. case 'd': *copyto++ = 0177; break; /* DEL (delete) */
  5083. case 'e': *copyto++ = 033; break; /* ESC (delete) */
  5084. case 'f': *copyto++ = '\f'; break; /* FF (form feed) */
  5085. case 'n': *copyto++ = '\n'; break; /* NL (new line) */
  5086. case 'r': *copyto++ = '\r'; break; /* CR (carriage return) */
  5087. case 't': *copyto++ = '\t'; break; /* TAB (horizontal tab) */
  5088. case 'v': *copyto++ = '\v'; break; /* VT (vertical tab) */
  5089. default:
  5090. if (*name == sep)
  5091. *copyto++ = sep;
  5092. else
  5093. {
  5094. /* Something else is quoted, so preserve the quote. */
  5095. *copyto++ = '\\';
  5096. *copyto++ = *name;
  5097. }
  5098. break;
  5099. }
  5100. quoted = FALSE;
  5101. }
  5102. else if (*name == '\\')
  5103. quoted = TRUE;
  5104. else if (*name == sep)
  5105. break;
  5106. else
  5107. *copyto++ = *name;
  5108. }
  5109. if (*name != sep)
  5110. name = NULL; /* signal unterminated regexp */
  5111. /* Terminate copied string. */
  5112. *copyto = '\0';
  5113. return name;
  5114. }
  5115. /* Look at the argument of --regex or --no-regex and do the right
  5116. thing. Same for each line of a regexp file. */
  5117. static void
  5118. analyse_regex (char *regex_arg)
  5119. {
  5120. if (regex_arg == NULL)
  5121. {
  5122. free_regexps (); /* --no-regex: remove existing regexps */
  5123. return;
  5124. }
  5125. /* A real --regexp option or a line in a regexp file. */
  5126. switch (regex_arg[0])
  5127. {
  5128. /* Comments in regexp file or null arg to --regex. */
  5129. case '\0':
  5130. case ' ':
  5131. case '\t':
  5132. break;
  5133. /* Read a regex file. This is recursive and may result in a
  5134. loop, which will stop when the file descriptors are exhausted. */
  5135. case '@':
  5136. {
  5137. FILE *regexfp;
  5138. linebuffer regexbuf;
  5139. char *regexfile = regex_arg + 1;
  5140. /* regexfile is a file containing regexps, one per line. */
  5141. regexfp = fopen (regexfile, "r");
  5142. if (regexfp == NULL)
  5143. {
  5144. pfatal (regexfile);
  5145. return;
  5146. }
  5147. linebuffer_init (&regexbuf);
  5148. while (readline_internal (&regexbuf, regexfp) > 0)
  5149. analyse_regex (regexbuf.buffer);
  5150. free (regexbuf.buffer);
  5151. fclose (regexfp);
  5152. }
  5153. break;
  5154. /* Regexp to be used for a specific language only. */
  5155. case '{':
  5156. {
  5157. language *lang;
  5158. char *lang_name = regex_arg + 1;
  5159. char *cp;
  5160. for (cp = lang_name; *cp != '}'; cp++)
  5161. if (*cp == '\0')
  5162. {
  5163. error ("unterminated language name in regex: %s", regex_arg);
  5164. return;
  5165. }
  5166. *cp++ = '\0';
  5167. lang = get_language_from_langname (lang_name);
  5168. if (lang == NULL)
  5169. return;
  5170. add_regex (cp, lang);
  5171. }
  5172. break;
  5173. /* Regexp to be used for any language. */
  5174. default:
  5175. add_regex (regex_arg, NULL);
  5176. break;
  5177. }
  5178. }
  5179. /* Separate the regexp pattern, compile it,
  5180. and care for optional name and modifiers. */
  5181. static void
  5182. add_regex (char *regexp_pattern, language *lang)
  5183. {
  5184. static struct re_pattern_buffer zeropattern;
  5185. char sep, *pat, *name, *modifiers;
  5186. char empty[] = "";
  5187. const char *err;
  5188. struct re_pattern_buffer *patbuf;
  5189. regexp *rp;
  5190. bool
  5191. force_explicit_name = TRUE, /* do not use implicit tag names */
  5192. ignore_case = FALSE, /* case is significant */
  5193. multi_line = FALSE, /* matches are done one line at a time */
  5194. single_line = FALSE; /* dot does not match newline */
  5195. if (strlen (regexp_pattern) < 3)
  5196. {
  5197. error ("null regexp", (char *)NULL);
  5198. return;
  5199. }
  5200. sep = regexp_pattern[0];
  5201. name = scan_separators (regexp_pattern);
  5202. if (name == NULL)
  5203. {
  5204. error ("%s: unterminated regexp", regexp_pattern);
  5205. return;
  5206. }
  5207. if (name[1] == sep)
  5208. {
  5209. error ("null name for regexp \"%s\"", regexp_pattern);
  5210. return;
  5211. }
  5212. modifiers = scan_separators (name);
  5213. if (modifiers == NULL) /* no terminating separator --> no name */
  5214. {
  5215. modifiers = name;
  5216. name = empty;
  5217. }
  5218. else
  5219. modifiers += 1; /* skip separator */
  5220. /* Parse regex modifiers. */
  5221. for (; modifiers[0] != '\0'; modifiers++)
  5222. switch (modifiers[0])
  5223. {
  5224. case 'N':
  5225. if (modifiers == name)
  5226. error ("forcing explicit tag name but no name, ignoring", NULL);
  5227. force_explicit_name = TRUE;
  5228. break;
  5229. case 'i':
  5230. ignore_case = TRUE;
  5231. break;
  5232. case 's':
  5233. single_line = TRUE;
  5234. /* FALLTHRU */
  5235. case 'm':
  5236. multi_line = TRUE;
  5237. need_filebuf = TRUE;
  5238. break;
  5239. default:
  5240. {
  5241. char wrongmod [2];
  5242. wrongmod[0] = modifiers[0];
  5243. wrongmod[1] = '\0';
  5244. error ("invalid regexp modifier `%s', ignoring", wrongmod);
  5245. }
  5246. break;
  5247. }
  5248. patbuf = xnew (1, struct re_pattern_buffer);
  5249. *patbuf = zeropattern;
  5250. if (ignore_case)
  5251. {
  5252. static char lc_trans[CHARS];
  5253. int i;
  5254. for (i = 0; i < CHARS; i++)
  5255. lc_trans[i] = lowcase (i);
  5256. patbuf->translate = lc_trans; /* translation table to fold case */
  5257. }
  5258. if (multi_line)
  5259. pat = concat ("^", regexp_pattern, ""); /* anchor to beginning of line */
  5260. else
  5261. pat = regexp_pattern;
  5262. if (single_line)
  5263. re_set_syntax (RE_SYNTAX_EMACS | RE_DOT_NEWLINE);
  5264. else
  5265. re_set_syntax (RE_SYNTAX_EMACS);
  5266. err = re_compile_pattern (pat, strlen (pat), patbuf);
  5267. if (multi_line)
  5268. free (pat);
  5269. if (err != NULL)
  5270. {
  5271. error ("%s while compiling pattern", err);
  5272. return;
  5273. }
  5274. rp = p_head;
  5275. p_head = xnew (1, regexp);
  5276. p_head->pattern = savestr (regexp_pattern);
  5277. p_head->p_next = rp;
  5278. p_head->lang = lang;
  5279. p_head->pat = patbuf;
  5280. p_head->name = savestr (name);
  5281. p_head->error_signaled = FALSE;
  5282. p_head->force_explicit_name = force_explicit_name;
  5283. p_head->ignore_case = ignore_case;
  5284. p_head->multi_line = multi_line;
  5285. }
  5286. /*
  5287. * Do the substitutions indicated by the regular expression and
  5288. * arguments.
  5289. */
  5290. static char *
  5291. substitute (char *in, char *out, struct re_registers *regs)
  5292. {
  5293. char *result, *t;
  5294. int size, dig, diglen;
  5295. result = NULL;
  5296. size = strlen (out);
  5297. /* Pass 1: figure out how much to allocate by finding all \N strings. */
  5298. if (out[size - 1] == '\\')
  5299. fatal ("pattern error in \"%s\"", out);
  5300. for (t = etags_strchr (out, '\\');
  5301. t != NULL;
  5302. t = etags_strchr (t + 2, '\\'))
  5303. if (ISDIGIT (t[1]))
  5304. {
  5305. dig = t[1] - '0';
  5306. diglen = regs->end[dig] - regs->start[dig];
  5307. size += diglen - 2;
  5308. }
  5309. else
  5310. size -= 1;
  5311. /* Allocate space and do the substitutions. */
  5312. assert (size >= 0);
  5313. result = xnew (size + 1, char);
  5314. for (t = result; *out != '\0'; out++)
  5315. if (*out == '\\' && ISDIGIT (*++out))
  5316. {
  5317. dig = *out - '0';
  5318. diglen = regs->end[dig] - regs->start[dig];
  5319. strncpy (t, in + regs->start[dig], diglen);
  5320. t += diglen;
  5321. }
  5322. else
  5323. *t++ = *out;
  5324. *t = '\0';
  5325. assert (t <= result + size);
  5326. assert (t - result == (int)strlen (result));
  5327. return result;
  5328. }
  5329. /* Deallocate all regexps. */
  5330. static void
  5331. free_regexps (void)
  5332. {
  5333. regexp *rp;
  5334. while (p_head != NULL)
  5335. {
  5336. rp = p_head->p_next;
  5337. free (p_head->pattern);
  5338. free (p_head->name);
  5339. free (p_head);
  5340. p_head = rp;
  5341. }
  5342. return;
  5343. }
  5344. /*
  5345. * Reads the whole file as a single string from `filebuf' and looks for
  5346. * multi-line regular expressions, creating tags on matches.
  5347. * readline already dealt with normal regexps.
  5348. *
  5349. * Idea by Ben Wing <ben@666.com> (2002).
  5350. */
  5351. static void
  5352. regex_tag_multiline (void)
  5353. {
  5354. char *buffer = filebuf.buffer;
  5355. regexp *rp;
  5356. char *name;
  5357. for (rp = p_head; rp != NULL; rp = rp->p_next)
  5358. {
  5359. int match = 0;
  5360. if (!rp->multi_line)
  5361. continue; /* skip normal regexps */
  5362. /* Generic initializations before parsing file from memory. */
  5363. lineno = 1; /* reset global line number */
  5364. charno = 0; /* reset global char number */
  5365. linecharno = 0; /* reset global char number of line start */
  5366. /* Only use generic regexps or those for the current language. */
  5367. if (rp->lang != NULL && rp->lang != curfdp->lang)
  5368. continue;
  5369. while (match >= 0 && match < filebuf.len)
  5370. {
  5371. match = re_search (rp->pat, buffer, filebuf.len, charno,
  5372. filebuf.len - match, &rp->regs);
  5373. switch (match)
  5374. {
  5375. case -2:
  5376. /* Some error. */
  5377. if (!rp->error_signaled)
  5378. {
  5379. error ("regexp stack overflow while matching \"%s\"",
  5380. rp->pattern);
  5381. rp->error_signaled = TRUE;
  5382. }
  5383. break;
  5384. case -1:
  5385. /* No match. */
  5386. break;
  5387. default:
  5388. if (match == rp->regs.end[0])
  5389. {
  5390. if (!rp->error_signaled)
  5391. {
  5392. error ("regexp matches the empty string: \"%s\"",
  5393. rp->pattern);
  5394. rp->error_signaled = TRUE;
  5395. }
  5396. match = -3; /* exit from while loop */
  5397. break;
  5398. }
  5399. /* Match occurred. Construct a tag. */
  5400. while (charno < rp->regs.end[0])
  5401. if (buffer[charno++] == '\n')
  5402. lineno++, linecharno = charno;
  5403. name = rp->name;
  5404. if (name[0] == '\0')
  5405. name = NULL;
  5406. else /* make a named tag */
  5407. name = substitute (buffer, rp->name, &rp->regs);
  5408. if (rp->force_explicit_name)
  5409. /* Force explicit tag name, if a name is there. */
  5410. pfnote (name, TRUE, buffer + linecharno,
  5411. charno - linecharno + 1, lineno, linecharno);
  5412. else
  5413. make_tag (name, strlen (name), TRUE, buffer + linecharno,
  5414. charno - linecharno + 1, lineno, linecharno);
  5415. break;
  5416. }
  5417. }
  5418. }
  5419. }
  5420. static bool
  5421. nocase_tail (const char *cp)
  5422. {
  5423. register int len = 0;
  5424. while (*cp != '\0' && lowcase (*cp) == lowcase (dbp[len]))
  5425. cp++, len++;
  5426. if (*cp == '\0' && !intoken (dbp[len]))
  5427. {
  5428. dbp += len;
  5429. return TRUE;
  5430. }
  5431. return FALSE;
  5432. }
  5433. static void
  5434. get_tag (register char *bp, char **namepp)
  5435. {
  5436. register char *cp = bp;
  5437. if (*bp != '\0')
  5438. {
  5439. /* Go till you get to white space or a syntactic break */
  5440. for (cp = bp + 1; !notinname (*cp); cp++)
  5441. continue;
  5442. make_tag (bp, cp - bp, TRUE,
  5443. lb.buffer, cp - lb.buffer + 1, lineno, linecharno);
  5444. }
  5445. if (namepp != NULL)
  5446. *namepp = savenstr (bp, cp - bp);
  5447. }
  5448. /*
  5449. * Read a line of text from `stream' into `lbp', excluding the
  5450. * newline or CR-NL, if any. Return the number of characters read from
  5451. * `stream', which is the length of the line including the newline.
  5452. *
  5453. * On DOS or Windows we do not count the CR character, if any before the
  5454. * NL, in the returned length; this mirrors the behavior of Emacs on those
  5455. * platforms (for text files, it translates CR-NL to NL as it reads in the
  5456. * file).
  5457. *
  5458. * If multi-line regular expressions are requested, each line read is
  5459. * appended to `filebuf'.
  5460. */
  5461. static long
  5462. readline_internal (linebuffer *lbp, register FILE *stream)
  5463. {
  5464. char *buffer = lbp->buffer;
  5465. register char *p = lbp->buffer;
  5466. register char *pend;
  5467. int chars_deleted;
  5468. pend = p + lbp->size; /* Separate to avoid 386/IX compiler bug. */
  5469. for (;;)
  5470. {
  5471. register int c = getc (stream);
  5472. if (p == pend)
  5473. {
  5474. /* We're at the end of linebuffer: expand it. */
  5475. lbp->size *= 2;
  5476. xrnew (buffer, lbp->size, char);
  5477. p += buffer - lbp->buffer;
  5478. pend = buffer + lbp->size;
  5479. lbp->buffer = buffer;
  5480. }
  5481. if (c == EOF)
  5482. {
  5483. *p = '\0';
  5484. chars_deleted = 0;
  5485. break;
  5486. }
  5487. if (c == '\n')
  5488. {
  5489. if (p > buffer && p[-1] == '\r')
  5490. {
  5491. p -= 1;
  5492. #ifdef DOS_NT
  5493. /* Assume CRLF->LF translation will be performed by Emacs
  5494. when loading this file, so CRs won't appear in the buffer.
  5495. It would be cleaner to compensate within Emacs;
  5496. however, Emacs does not know how many CRs were deleted
  5497. before any given point in the file. */
  5498. chars_deleted = 1;
  5499. #else
  5500. chars_deleted = 2;
  5501. #endif
  5502. }
  5503. else
  5504. {
  5505. chars_deleted = 1;
  5506. }
  5507. *p = '\0';
  5508. break;
  5509. }
  5510. *p++ = c;
  5511. }
  5512. lbp->len = p - buffer;
  5513. if (need_filebuf /* we need filebuf for multi-line regexps */
  5514. && chars_deleted > 0) /* not at EOF */
  5515. {
  5516. while (filebuf.size <= filebuf.len + lbp->len + 1) /* +1 for \n */
  5517. {
  5518. /* Expand filebuf. */
  5519. filebuf.size *= 2;
  5520. xrnew (filebuf.buffer, filebuf.size, char);
  5521. }
  5522. strncpy (filebuf.buffer + filebuf.len, lbp->buffer, lbp->len);
  5523. filebuf.len += lbp->len;
  5524. filebuf.buffer[filebuf.len++] = '\n';
  5525. filebuf.buffer[filebuf.len] = '\0';
  5526. }
  5527. return lbp->len + chars_deleted;
  5528. }
  5529. /*
  5530. * Like readline_internal, above, but in addition try to match the
  5531. * input line against relevant regular expressions and manage #line
  5532. * directives.
  5533. */
  5534. static void
  5535. readline (linebuffer *lbp, FILE *stream)
  5536. {
  5537. long result;
  5538. linecharno = charno; /* update global char number of line start */
  5539. result = readline_internal (lbp, stream); /* read line */
  5540. lineno += 1; /* increment global line number */
  5541. charno += result; /* increment global char number */
  5542. /* Honor #line directives. */
  5543. if (!no_line_directive)
  5544. {
  5545. static bool discard_until_line_directive;
  5546. /* Check whether this is a #line directive. */
  5547. if (result > 12 && strneq (lbp->buffer, "#line ", 6))
  5548. {
  5549. unsigned int lno;
  5550. int start = 0;
  5551. if (sscanf (lbp->buffer, "#line %u \"%n", &lno, &start) >= 1
  5552. && start > 0) /* double quote character found */
  5553. {
  5554. char *endp = lbp->buffer + start;
  5555. while ((endp = etags_strchr (endp, '"')) != NULL
  5556. && endp[-1] == '\\')
  5557. endp++;
  5558. if (endp != NULL)
  5559. /* Ok, this is a real #line directive. Let's deal with it. */
  5560. {
  5561. char *taggedabsname; /* absolute name of original file */
  5562. char *taggedfname; /* name of original file as given */
  5563. char *name; /* temp var */
  5564. discard_until_line_directive = FALSE; /* found it */
  5565. name = lbp->buffer + start;
  5566. *endp = '\0';
  5567. canonicalize_filename (name);
  5568. taggedabsname = absolute_filename (name, tagfiledir);
  5569. if (filename_is_absolute (name)
  5570. || filename_is_absolute (curfdp->infname))
  5571. taggedfname = savestr (taggedabsname);
  5572. else
  5573. taggedfname = relative_filename (taggedabsname,tagfiledir);
  5574. if (streq (curfdp->taggedfname, taggedfname))
  5575. /* The #line directive is only a line number change. We
  5576. deal with this afterwards. */
  5577. free (taggedfname);
  5578. else
  5579. /* The tags following this #line directive should be
  5580. attributed to taggedfname. In order to do this, set
  5581. curfdp accordingly. */
  5582. {
  5583. fdesc *fdp; /* file description pointer */
  5584. /* Go look for a file description already set up for the
  5585. file indicated in the #line directive. If there is
  5586. one, use it from now until the next #line
  5587. directive. */
  5588. for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
  5589. if (streq (fdp->infname, curfdp->infname)
  5590. && streq (fdp->taggedfname, taggedfname))
  5591. /* If we remove the second test above (after the &&)
  5592. then all entries pertaining to the same file are
  5593. coalesced in the tags file. If we use it, then
  5594. entries pertaining to the same file but generated
  5595. from different files (via #line directives) will
  5596. go into separate sections in the tags file. These
  5597. alternatives look equivalent. The first one
  5598. destroys some apparently useless information. */
  5599. {
  5600. curfdp = fdp;
  5601. free (taggedfname);
  5602. break;
  5603. }
  5604. /* Else, if we already tagged the real file, skip all
  5605. input lines until the next #line directive. */
  5606. if (fdp == NULL) /* not found */
  5607. for (fdp = fdhead; fdp != NULL; fdp = fdp->next)
  5608. if (streq (fdp->infabsname, taggedabsname))
  5609. {
  5610. discard_until_line_directive = TRUE;
  5611. free (taggedfname);
  5612. break;
  5613. }
  5614. /* Else create a new file description and use that from
  5615. now on, until the next #line directive. */
  5616. if (fdp == NULL) /* not found */
  5617. {
  5618. fdp = fdhead;
  5619. fdhead = xnew (1, fdesc);
  5620. *fdhead = *curfdp; /* copy curr. file description */
  5621. fdhead->next = fdp;
  5622. fdhead->infname = savestr (curfdp->infname);
  5623. fdhead->infabsname = savestr (curfdp->infabsname);
  5624. fdhead->infabsdir = savestr (curfdp->infabsdir);
  5625. fdhead->taggedfname = taggedfname;
  5626. fdhead->usecharno = FALSE;
  5627. fdhead->prop = NULL;
  5628. fdhead->written = FALSE;
  5629. curfdp = fdhead;
  5630. }
  5631. }
  5632. free (taggedabsname);
  5633. lineno = lno - 1;
  5634. readline (lbp, stream);
  5635. return;
  5636. } /* if a real #line directive */
  5637. } /* if #line is followed by a number */
  5638. } /* if line begins with "#line " */
  5639. /* If we are here, no #line directive was found. */
  5640. if (discard_until_line_directive)
  5641. {
  5642. if (result > 0)
  5643. {
  5644. /* Do a tail recursion on ourselves, thus discarding the contents
  5645. of the line buffer. */
  5646. readline (lbp, stream);
  5647. return;
  5648. }
  5649. /* End of file. */
  5650. discard_until_line_directive = FALSE;
  5651. return;
  5652. }
  5653. } /* if #line directives should be considered */
  5654. {
  5655. int match;
  5656. regexp *rp;
  5657. char *name;
  5658. /* Match against relevant regexps. */
  5659. if (lbp->len > 0)
  5660. for (rp = p_head; rp != NULL; rp = rp->p_next)
  5661. {
  5662. /* Only use generic regexps or those for the current language.
  5663. Also do not use multiline regexps, which is the job of
  5664. regex_tag_multiline. */
  5665. if ((rp->lang != NULL && rp->lang != fdhead->lang)
  5666. || rp->multi_line)
  5667. continue;
  5668. match = re_match (rp->pat, lbp->buffer, lbp->len, 0, &rp->regs);
  5669. switch (match)
  5670. {
  5671. case -2:
  5672. /* Some error. */
  5673. if (!rp->error_signaled)
  5674. {
  5675. error ("regexp stack overflow while matching \"%s\"",
  5676. rp->pattern);
  5677. rp->error_signaled = TRUE;
  5678. }
  5679. break;
  5680. case -1:
  5681. /* No match. */
  5682. break;
  5683. case 0:
  5684. /* Empty string matched. */
  5685. if (!rp->error_signaled)
  5686. {
  5687. error ("regexp matches the empty string: \"%s\"", rp->pattern);
  5688. rp->error_signaled = TRUE;
  5689. }
  5690. break;
  5691. default:
  5692. /* Match occurred. Construct a tag. */
  5693. name = rp->name;
  5694. if (name[0] == '\0')
  5695. name = NULL;
  5696. else /* make a named tag */
  5697. name = substitute (lbp->buffer, rp->name, &rp->regs);
  5698. if (rp->force_explicit_name)
  5699. /* Force explicit tag name, if a name is there. */
  5700. pfnote (name, TRUE, lbp->buffer, match, lineno, linecharno);
  5701. else
  5702. make_tag (name, strlen (name), TRUE,
  5703. lbp->buffer, match, lineno, linecharno);
  5704. break;
  5705. }
  5706. }
  5707. }
  5708. }
  5709. /*
  5710. * Return a pointer to a space of size strlen(cp)+1 allocated
  5711. * with xnew where the string CP has been copied.
  5712. */
  5713. static char *
  5714. savestr (const char *cp)
  5715. {
  5716. return savenstr (cp, strlen (cp));
  5717. }
  5718. /*
  5719. * Return a pointer to a space of size LEN+1 allocated with xnew where
  5720. * the string CP has been copied for at most the first LEN characters.
  5721. */
  5722. static char *
  5723. savenstr (const char *cp, int len)
  5724. {
  5725. register char *dp;
  5726. dp = xnew (len + 1, char);
  5727. strncpy (dp, cp, len);
  5728. dp[len] = '\0';
  5729. return dp;
  5730. }
  5731. /*
  5732. * Return the ptr in sp at which the character c last
  5733. * appears; NULL if not found
  5734. *
  5735. * Identical to POSIX strrchr, included for portability.
  5736. */
  5737. static char *
  5738. etags_strrchr (register const char *sp, register int c)
  5739. {
  5740. register const char *r;
  5741. r = NULL;
  5742. do
  5743. {
  5744. if (*sp == c)
  5745. r = sp;
  5746. } while (*sp++);
  5747. return (char *)r;
  5748. }
  5749. /*
  5750. * Return the ptr in sp at which the character c first
  5751. * appears; NULL if not found
  5752. *
  5753. * Identical to POSIX strchr, included for portability.
  5754. */
  5755. static char *
  5756. etags_strchr (register const char *sp, register int c)
  5757. {
  5758. do
  5759. {
  5760. if (*sp == c)
  5761. return (char *)sp;
  5762. } while (*sp++);
  5763. return NULL;
  5764. }
  5765. /*
  5766. * Compare two strings, ignoring case for alphabetic characters.
  5767. *
  5768. * Same as BSD's strcasecmp, included for portability.
  5769. */
  5770. static int
  5771. etags_strcasecmp (register const char *s1, register const char *s2)
  5772. {
  5773. while (*s1 != '\0'
  5774. && (ISALPHA (*s1) && ISALPHA (*s2)
  5775. ? lowcase (*s1) == lowcase (*s2)
  5776. : *s1 == *s2))
  5777. s1++, s2++;
  5778. return (ISALPHA (*s1) && ISALPHA (*s2)
  5779. ? lowcase (*s1) - lowcase (*s2)
  5780. : *s1 - *s2);
  5781. }
  5782. /*
  5783. * Compare two strings, ignoring case for alphabetic characters.
  5784. * Stop after a given number of characters
  5785. *
  5786. * Same as BSD's strncasecmp, included for portability.
  5787. */
  5788. static int
  5789. etags_strncasecmp (register const char *s1, register const char *s2, register int n)
  5790. {
  5791. while (*s1 != '\0' && n-- > 0
  5792. && (ISALPHA (*s1) && ISALPHA (*s2)
  5793. ? lowcase (*s1) == lowcase (*s2)
  5794. : *s1 == *s2))
  5795. s1++, s2++;
  5796. if (n < 0)
  5797. return 0;
  5798. else
  5799. return (ISALPHA (*s1) && ISALPHA (*s2)
  5800. ? lowcase (*s1) - lowcase (*s2)
  5801. : *s1 - *s2);
  5802. }
  5803. /* Skip spaces (end of string is not space), return new pointer. */
  5804. static char *
  5805. skip_spaces (char *cp)
  5806. {
  5807. while (iswhite (*cp))
  5808. cp++;
  5809. return cp;
  5810. }
  5811. /* Skip non spaces, except end of string, return new pointer. */
  5812. static char *
  5813. skip_non_spaces (char *cp)
  5814. {
  5815. while (*cp != '\0' && !iswhite (*cp))
  5816. cp++;
  5817. return cp;
  5818. }
  5819. /* Print error message and exit. */
  5820. void
  5821. fatal (const char *s1, const char *s2)
  5822. {
  5823. error (s1, s2);
  5824. exit (EXIT_FAILURE);
  5825. }
  5826. static void
  5827. pfatal (const char *s1)
  5828. {
  5829. perror (s1);
  5830. exit (EXIT_FAILURE);
  5831. }
  5832. static void
  5833. suggest_asking_for_help (void)
  5834. {
  5835. fprintf (stderr, "\tTry `%s %s' for a complete list of options.\n",
  5836. progname, NO_LONG_OPTIONS ? "-h" : "--help");
  5837. exit (EXIT_FAILURE);
  5838. }
  5839. /* Print error message. `s1' is printf control string, `s2' is arg for it. */
  5840. static void
  5841. error (const char *s1, const char *s2)
  5842. {
  5843. fprintf (stderr, "%s: ", progname);
  5844. fprintf (stderr, s1, s2);
  5845. fprintf (stderr, "\n");
  5846. }
  5847. /* Return a newly-allocated string whose contents
  5848. concatenate those of s1, s2, s3. */
  5849. static char *
  5850. concat (const char *s1, const char *s2, const char *s3)
  5851. {
  5852. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  5853. char *result = xnew (len1 + len2 + len3 + 1, char);
  5854. strcpy (result, s1);
  5855. strcpy (result + len1, s2);
  5856. strcpy (result + len1 + len2, s3);
  5857. result[len1 + len2 + len3] = '\0';
  5858. return result;
  5859. }
  5860. /* Does the same work as the system V getcwd, but does not need to
  5861. guess the buffer size in advance. */
  5862. static char *
  5863. etags_getcwd (void)
  5864. {
  5865. #ifdef HAVE_GETCWD
  5866. int bufsize = 200;
  5867. char *path = xnew (bufsize, char);
  5868. while (getcwd (path, bufsize) == NULL)
  5869. {
  5870. if (errno != ERANGE)
  5871. pfatal ("getcwd");
  5872. bufsize *= 2;
  5873. free (path);
  5874. path = xnew (bufsize, char);
  5875. }
  5876. canonicalize_filename (path);
  5877. return path;
  5878. #else /* not HAVE_GETCWD */
  5879. #if MSDOS
  5880. char *p, path[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
  5881. getwd (path);
  5882. for (p = path; *p != '\0'; p++)
  5883. if (*p == '\\')
  5884. *p = '/';
  5885. else
  5886. *p = lowcase (*p);
  5887. return strdup (path);
  5888. #else /* not MSDOS */
  5889. linebuffer path;
  5890. FILE *pipe;
  5891. linebuffer_init (&path);
  5892. pipe = (FILE *) popen ("pwd 2>/dev/null", "r");
  5893. if (pipe == NULL || readline_internal (&path, pipe) == 0)
  5894. pfatal ("pwd");
  5895. pclose (pipe);
  5896. return path.buffer;
  5897. #endif /* not MSDOS */
  5898. #endif /* not HAVE_GETCWD */
  5899. }
  5900. /* Return a newly allocated string containing the file name of FILE
  5901. relative to the absolute directory DIR (which should end with a slash). */
  5902. static char *
  5903. relative_filename (char *file, char *dir)
  5904. {
  5905. char *fp, *dp, *afn, *res;
  5906. int i;
  5907. /* Find the common root of file and dir (with a trailing slash). */
  5908. afn = absolute_filename (file, cwd);
  5909. fp = afn;
  5910. dp = dir;
  5911. while (*fp++ == *dp++)
  5912. continue;
  5913. fp--, dp--; /* back to the first differing char */
  5914. #ifdef DOS_NT
  5915. if (fp == afn && afn[0] != '/') /* cannot build a relative name */
  5916. return afn;
  5917. #endif
  5918. do /* look at the equal chars until '/' */
  5919. fp--, dp--;
  5920. while (*fp != '/');
  5921. /* Build a sequence of "../" strings for the resulting relative file name. */
  5922. i = 0;
  5923. while ((dp = etags_strchr (dp + 1, '/')) != NULL)
  5924. i += 1;
  5925. res = xnew (3*i + strlen (fp + 1) + 1, char);
  5926. res[0] = '\0';
  5927. while (i-- > 0)
  5928. strcat (res, "../");
  5929. /* Add the file name relative to the common root of file and dir. */
  5930. strcat (res, fp + 1);
  5931. free (afn);
  5932. return res;
  5933. }
  5934. /* Return a newly allocated string containing the absolute file name
  5935. of FILE given DIR (which should end with a slash). */
  5936. static char *
  5937. absolute_filename (char *file, char *dir)
  5938. {
  5939. char *slashp, *cp, *res;
  5940. if (filename_is_absolute (file))
  5941. res = savestr (file);
  5942. #ifdef DOS_NT
  5943. /* We don't support non-absolute file names with a drive
  5944. letter, like `d:NAME' (it's too much hassle). */
  5945. else if (file[1] == ':')
  5946. fatal ("%s: relative file names with drive letters not supported", file);
  5947. #endif
  5948. else
  5949. res = concat (dir, file, "");
  5950. /* Delete the "/dirname/.." and "/." substrings. */
  5951. slashp = etags_strchr (res, '/');
  5952. while (slashp != NULL && slashp[0] != '\0')
  5953. {
  5954. if (slashp[1] == '.')
  5955. {
  5956. if (slashp[2] == '.'
  5957. && (slashp[3] == '/' || slashp[3] == '\0'))
  5958. {
  5959. cp = slashp;
  5960. do
  5961. cp--;
  5962. while (cp >= res && !filename_is_absolute (cp));
  5963. if (cp < res)
  5964. cp = slashp; /* the absolute name begins with "/.." */
  5965. #ifdef DOS_NT
  5966. /* Under MSDOS and NT we get `d:/NAME' as absolute
  5967. file name, so the luser could say `d:/../NAME'.
  5968. We silently treat this as `d:/NAME'. */
  5969. else if (cp[0] != '/')
  5970. cp = slashp;
  5971. #endif
  5972. memmove (cp, slashp + 3, strlen (slashp + 2));
  5973. slashp = cp;
  5974. continue;
  5975. }
  5976. else if (slashp[2] == '/' || slashp[2] == '\0')
  5977. {
  5978. memmove (slashp, slashp + 2, strlen (slashp + 1));
  5979. continue;
  5980. }
  5981. }
  5982. slashp = etags_strchr (slashp + 1, '/');
  5983. }
  5984. if (res[0] == '\0') /* just a safety net: should never happen */
  5985. {
  5986. free (res);
  5987. return savestr ("/");
  5988. }
  5989. else
  5990. return res;
  5991. }
  5992. /* Return a newly allocated string containing the absolute
  5993. file name of dir where FILE resides given DIR (which should
  5994. end with a slash). */
  5995. static char *
  5996. absolute_dirname (char *file, char *dir)
  5997. {
  5998. char *slashp, *res;
  5999. char save;
  6000. slashp = etags_strrchr (file, '/');
  6001. if (slashp == NULL)
  6002. return savestr (dir);
  6003. save = slashp[1];
  6004. slashp[1] = '\0';
  6005. res = absolute_filename (file, dir);
  6006. slashp[1] = save;
  6007. return res;
  6008. }
  6009. /* Whether the argument string is an absolute file name. The argument
  6010. string must have been canonicalized with canonicalize_filename. */
  6011. static bool
  6012. filename_is_absolute (char *fn)
  6013. {
  6014. return (fn[0] == '/'
  6015. #ifdef DOS_NT
  6016. || (ISALPHA (fn[0]) && fn[1] == ':' && fn[2] == '/')
  6017. #endif
  6018. );
  6019. }
  6020. /* Downcase DOS drive letter and collapse separators into single slashes.
  6021. Works in place. */
  6022. static void
  6023. canonicalize_filename (register char *fn)
  6024. {
  6025. register char* cp;
  6026. char sep = '/';
  6027. #ifdef DOS_NT
  6028. /* Canonicalize drive letter case. */
  6029. # define ISUPPER(c) isupper (CHAR (c))
  6030. if (fn[0] != '\0' && fn[1] == ':' && ISUPPER (fn[0]))
  6031. fn[0] = lowcase (fn[0]);
  6032. sep = '\\';
  6033. #endif
  6034. /* Collapse multiple separators into a single slash. */
  6035. for (cp = fn; *cp != '\0'; cp++, fn++)
  6036. if (*cp == sep)
  6037. {
  6038. *fn = '/';
  6039. while (cp[1] == sep)
  6040. cp++;
  6041. }
  6042. else
  6043. *fn = *cp;
  6044. *fn = '\0';
  6045. }
  6046. /* Initialize a linebuffer for use. */
  6047. static void
  6048. linebuffer_init (linebuffer *lbp)
  6049. {
  6050. lbp->size = (DEBUG) ? 3 : 200;
  6051. lbp->buffer = xnew (lbp->size, char);
  6052. lbp->buffer[0] = '\0';
  6053. lbp->len = 0;
  6054. }
  6055. /* Set the minimum size of a string contained in a linebuffer. */
  6056. static void
  6057. linebuffer_setlen (linebuffer *lbp, int toksize)
  6058. {
  6059. while (lbp->size <= toksize)
  6060. {
  6061. lbp->size *= 2;
  6062. xrnew (lbp->buffer, lbp->size, char);
  6063. }
  6064. lbp->len = toksize;
  6065. }
  6066. /* Like malloc but get fatal error if memory is exhausted. */
  6067. static PTR
  6068. xmalloc (size_t size)
  6069. {
  6070. PTR result = (PTR) malloc (size);
  6071. if (result == NULL)
  6072. fatal ("virtual memory exhausted", (char *)NULL);
  6073. return result;
  6074. }
  6075. static PTR
  6076. xrealloc (char *ptr, size_t size)
  6077. {
  6078. PTR result = (PTR) realloc (ptr, size);
  6079. if (result == NULL)
  6080. fatal ("virtual memory exhausted", (char *)NULL);
  6081. return result;
  6082. }
  6083. /*
  6084. * Local Variables:
  6085. * indent-tabs-mode: t
  6086. * tab-width: 8
  6087. * fill-column: 79
  6088. * c-font-lock-extra-types: ("FILE" "bool" "language" "linebuffer" "fdesc" "node" "regexp")
  6089. * c-file-style: "gnu"
  6090. * End:
  6091. */
  6092. /* etags.c ends here */