pval.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006, Digium, Inc.
  5. *
  6. * Steve Murphy <murf@parsetree.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Compile symbolic Asterisk Extension Logic into Asterisk extensions, version 2.
  21. *
  22. */
  23. /*** MODULEINFO
  24. <support_level>extended</support_level>
  25. ***/
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include <sys/types.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <errno.h>
  35. #include <regex.h>
  36. #include <sys/stat.h>
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/config.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/logger.h"
  41. #include "asterisk/cli.h"
  42. #include "asterisk/app.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/callerid.h"
  45. #include "asterisk/pval.h"
  46. #include "asterisk/ael_structs.h"
  47. #ifdef AAL_ARGCHECK
  48. #include "asterisk/argdesc.h"
  49. #endif
  50. #include "asterisk/utils.h"
  51. extern int localized_pbx_load_module(void);
  52. static char expr_output[2096];
  53. #define BUF_SIZE 2000
  54. /* these functions are in ../ast_expr2.fl */
  55. static int errs, warns;
  56. static int notes;
  57. #ifdef STANDALONE
  58. static int extensions_dot_conf_loaded = 0;
  59. #endif
  60. static char *registrar = "pbx_ael";
  61. static pval *current_db;
  62. static pval *current_context;
  63. static pval *current_extension;
  64. static const char *match_context;
  65. static const char *match_exten;
  66. static const char *match_label;
  67. static int in_abstract_context;
  68. static int count_labels; /* true, put matcher in label counting mode */
  69. static int label_count; /* labels are only meant to be counted in a context or exten */
  70. static int return_on_context_match;
  71. static pval *last_matched_label;
  72. struct pval *match_pval(pval *item);
  73. static void check_timerange(pval *p);
  74. static void check_dow(pval *DOW);
  75. static void check_day(pval *DAY);
  76. static void check_month(pval *MON);
  77. static void check_expr2_input(pval *expr, char *str);
  78. static int extension_matches(pval *here, const char *exten, const char *pattern);
  79. static void check_goto(pval *item);
  80. static void find_pval_goto_item(pval *item, int lev);
  81. static void find_pval_gotos(pval *item, int lev);
  82. static int check_break(pval *item);
  83. static int check_continue(pval *item);
  84. static void check_label(pval *item);
  85. static void check_macro_returns(pval *macro);
  86. static struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont);
  87. static struct pval *find_first_label_in_current_context(char *label, pval *curr_cont);
  88. static void print_pval_list(FILE *fin, pval *item, int depth);
  89. static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext);
  90. static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label);
  91. static pval *get_goto_target(pval *item);
  92. static int label_inside_case(pval *label);
  93. static void attach_exten(struct ael_extension **list, struct ael_extension *newmem);
  94. static void fix_gotos_in_extensions(struct ael_extension *exten);
  95. static pval *get_extension_or_contxt(pval *p);
  96. static pval *get_contxt(pval *p);
  97. static void remove_spaces_before_equals(char *str);
  98. /* PRETTY PRINTER FOR AEL: ============================================================================= */
  99. static void print_pval(FILE *fin, pval *item, int depth)
  100. {
  101. int i;
  102. pval *lp;
  103. for (i=0; i<depth; i++) {
  104. fprintf(fin, "\t"); /* depth == indentation */
  105. }
  106. switch ( item->type ) {
  107. case PV_WORD:
  108. fprintf(fin,"%s;\n", item->u1.str); /* usually, words are encapsulated in something else */
  109. break;
  110. case PV_MACRO:
  111. fprintf(fin,"macro %s(", item->u1.str);
  112. for (lp=item->u2.arglist; lp; lp=lp->next) {
  113. if (lp != item->u2.arglist )
  114. fprintf(fin,", ");
  115. fprintf(fin,"%s", lp->u1.str);
  116. }
  117. fprintf(fin,") {\n");
  118. print_pval_list(fin,item->u3.macro_statements,depth+1);
  119. for (i=0; i<depth; i++) {
  120. fprintf(fin,"\t"); /* depth == indentation */
  121. }
  122. fprintf(fin,"};\n\n");
  123. break;
  124. case PV_CONTEXT:
  125. if ( item->u3.abstract )
  126. fprintf(fin,"abstract context %s {\n", item->u1.str);
  127. else
  128. fprintf(fin,"context %s {\n", item->u1.str);
  129. print_pval_list(fin,item->u2.statements,depth+1);
  130. for (i=0; i<depth; i++) {
  131. fprintf(fin,"\t"); /* depth == indentation */
  132. }
  133. fprintf(fin,"};\n\n");
  134. break;
  135. case PV_MACRO_CALL:
  136. fprintf(fin,"&%s(", item->u1.str);
  137. for (lp=item->u2.arglist; lp; lp=lp->next) {
  138. if ( lp != item->u2.arglist )
  139. fprintf(fin,", ");
  140. fprintf(fin,"%s", lp->u1.str);
  141. }
  142. fprintf(fin,");\n");
  143. break;
  144. case PV_APPLICATION_CALL:
  145. fprintf(fin,"%s(", item->u1.str);
  146. for (lp=item->u2.arglist; lp; lp=lp->next) {
  147. if ( lp != item->u2.arglist )
  148. fprintf(fin,",");
  149. fprintf(fin,"%s", lp->u1.str);
  150. }
  151. fprintf(fin,");\n");
  152. break;
  153. case PV_CASE:
  154. fprintf(fin,"case %s:\n", item->u1.str);
  155. print_pval_list(fin,item->u2.statements, depth+1);
  156. break;
  157. case PV_PATTERN:
  158. fprintf(fin,"pattern %s:\n", item->u1.str);
  159. print_pval_list(fin,item->u2.statements, depth+1);
  160. break;
  161. case PV_DEFAULT:
  162. fprintf(fin,"default:\n");
  163. print_pval_list(fin,item->u2.statements, depth+1);
  164. break;
  165. case PV_CATCH:
  166. fprintf(fin,"catch %s {\n", item->u1.str);
  167. print_pval_list(fin,item->u2.statements, depth+1);
  168. for (i=0; i<depth; i++) {
  169. fprintf(fin,"\t"); /* depth == indentation */
  170. }
  171. fprintf(fin,"};\n");
  172. break;
  173. case PV_SWITCHES:
  174. fprintf(fin,"switches {\n");
  175. print_pval_list(fin,item->u1.list,depth+1);
  176. for (i=0; i<depth; i++) {
  177. fprintf(fin,"\t"); /* depth == indentation */
  178. }
  179. fprintf(fin,"};\n");
  180. break;
  181. case PV_ESWITCHES:
  182. fprintf(fin,"eswitches {\n");
  183. print_pval_list(fin,item->u1.list,depth+1);
  184. for (i=0; i<depth; i++) {
  185. fprintf(fin,"\t"); /* depth == indentation */
  186. }
  187. fprintf(fin,"};\n");
  188. break;
  189. case PV_INCLUDES:
  190. fprintf(fin,"includes {\n");
  191. for (lp=item->u1.list; lp; lp=lp->next) {
  192. for (i=0; i<depth+1; i++) {
  193. fprintf(fin,"\t"); /* depth == indentation */
  194. }
  195. fprintf(fin,"%s", lp->u1.str); /* usually, words are encapsulated in something else */
  196. if (lp->u2.arglist)
  197. fprintf(fin,"|%s|%s|%s|%s",
  198. lp->u2.arglist->u1.str,
  199. lp->u2.arglist->next->u1.str,
  200. lp->u2.arglist->next->next->u1.str,
  201. lp->u2.arglist->next->next->next->u1.str
  202. );
  203. fprintf(fin,";\n"); /* usually, words are encapsulated in something else */
  204. }
  205. for (i=0; i<depth; i++) {
  206. fprintf(fin,"\t"); /* depth == indentation */
  207. }
  208. fprintf(fin,"};\n");
  209. break;
  210. case PV_STATEMENTBLOCK:
  211. fprintf(fin,"{\n");
  212. print_pval_list(fin,item->u1.list, depth+1);
  213. for (i=0; i<depth; i++) {
  214. fprintf(fin,"\t"); /* depth == indentation */
  215. }
  216. fprintf(fin,"}\n");
  217. break;
  218. case PV_VARDEC:
  219. fprintf(fin,"%s=%s;\n", item->u1.str, item->u2.val);
  220. break;
  221. case PV_LOCALVARDEC:
  222. fprintf(fin,"local %s=%s;\n", item->u1.str, item->u2.val);
  223. break;
  224. case PV_GOTO:
  225. fprintf(fin,"goto %s", item->u1.list->u1.str);
  226. if ( item->u1.list->next )
  227. fprintf(fin,",%s", item->u1.list->next->u1.str);
  228. if ( item->u1.list->next && item->u1.list->next->next )
  229. fprintf(fin,",%s", item->u1.list->next->next->u1.str);
  230. fprintf(fin,"\n");
  231. break;
  232. case PV_LABEL:
  233. fprintf(fin,"%s:\n", item->u1.str);
  234. break;
  235. case PV_FOR:
  236. fprintf(fin,"for (%s; %s; %s)\n", item->u1.for_init, item->u2.for_test, item->u3.for_inc);
  237. print_pval_list(fin,item->u4.for_statements,depth+1);
  238. break;
  239. case PV_WHILE:
  240. fprintf(fin,"while (%s)\n", item->u1.str);
  241. print_pval_list(fin,item->u2.statements,depth+1);
  242. break;
  243. case PV_BREAK:
  244. fprintf(fin,"break;\n");
  245. break;
  246. case PV_RETURN:
  247. fprintf(fin,"return;\n");
  248. break;
  249. case PV_CONTINUE:
  250. fprintf(fin,"continue;\n");
  251. break;
  252. case PV_RANDOM:
  253. case PV_IFTIME:
  254. case PV_IF:
  255. if ( item->type == PV_IFTIME ) {
  256. fprintf(fin,"ifTime ( %s|%s|%s|%s )\n",
  257. item->u1.list->u1.str,
  258. item->u1.list->next->u1.str,
  259. item->u1.list->next->next->u1.str,
  260. item->u1.list->next->next->next->u1.str
  261. );
  262. } else if ( item->type == PV_RANDOM ) {
  263. fprintf(fin,"random ( %s )\n", item->u1.str );
  264. } else
  265. fprintf(fin,"if ( %s )\n", item->u1.str);
  266. if ( item->u2.statements && item->u2.statements->next ) {
  267. for (i=0; i<depth; i++) {
  268. fprintf(fin,"\t"); /* depth == indentation */
  269. }
  270. fprintf(fin,"{\n");
  271. print_pval_list(fin,item->u2.statements,depth+1);
  272. for (i=0; i<depth; i++) {
  273. fprintf(fin,"\t"); /* depth == indentation */
  274. }
  275. if ( item->u3.else_statements )
  276. fprintf(fin,"}\n");
  277. else
  278. fprintf(fin,"};\n");
  279. } else if (item->u2.statements ) {
  280. print_pval_list(fin,item->u2.statements,depth+1);
  281. } else {
  282. if (item->u3.else_statements )
  283. fprintf(fin, " {} ");
  284. else
  285. fprintf(fin, " {}; ");
  286. }
  287. if ( item->u3.else_statements ) {
  288. for (i=0; i<depth; i++) {
  289. fprintf(fin,"\t"); /* depth == indentation */
  290. }
  291. fprintf(fin,"else\n");
  292. print_pval_list(fin,item->u3.else_statements, depth);
  293. }
  294. break;
  295. case PV_SWITCH:
  296. fprintf(fin,"switch( %s ) {\n", item->u1.str);
  297. print_pval_list(fin,item->u2.statements,depth+1);
  298. for (i=0; i<depth; i++) {
  299. fprintf(fin,"\t"); /* depth == indentation */
  300. }
  301. fprintf(fin,"}\n");
  302. break;
  303. case PV_EXTENSION:
  304. if ( item->u4.regexten )
  305. fprintf(fin, "regexten ");
  306. if ( item->u3.hints )
  307. fprintf(fin,"hints(%s) ", item->u3.hints);
  308. fprintf(fin,"%s => ", item->u1.str);
  309. print_pval_list(fin,item->u2.statements,depth+1);
  310. fprintf(fin,"\n");
  311. break;
  312. case PV_IGNOREPAT:
  313. fprintf(fin,"ignorepat => %s;\n", item->u1.str);
  314. break;
  315. case PV_GLOBALS:
  316. fprintf(fin,"globals {\n");
  317. print_pval_list(fin,item->u1.statements,depth+1);
  318. for (i=0; i<depth; i++) {
  319. fprintf(fin,"\t"); /* depth == indentation */
  320. }
  321. fprintf(fin,"}\n");
  322. break;
  323. }
  324. }
  325. static void print_pval_list(FILE *fin, pval *item, int depth)
  326. {
  327. pval *i;
  328. for (i=item; i; i=i->next) {
  329. print_pval(fin, i, depth);
  330. }
  331. }
  332. void ael2_print(char *fname, pval *tree)
  333. {
  334. FILE *fin = fopen(fname,"w");
  335. if ( !fin ) {
  336. ast_log(LOG_ERROR, "Couldn't open %s for writing.\n", fname);
  337. return;
  338. }
  339. print_pval_list(fin, tree, 0);
  340. fclose(fin);
  341. }
  342. /* EMPTY TEMPLATE FUNCS FOR AEL TRAVERSAL: ============================================================================= */
  343. void traverse_pval_template(pval *item, int depth);
  344. void traverse_pval_item_template(pval *item, int depth);
  345. void traverse_pval_item_template(pval *item, int depth)/* depth comes in handy for a pretty print (indentation),
  346. but you may not need it */
  347. {
  348. pval *lp;
  349. switch ( item->type ) {
  350. case PV_WORD:
  351. /* fields: item->u1.str == string associated with this (word). */
  352. break;
  353. case PV_MACRO:
  354. /* fields: item->u1.str == name of macro
  355. item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
  356. item->u2.arglist->u1.str == argument
  357. item->u2.arglist->next == next arg
  358. item->u3.macro_statements == pval list of statements in macro body.
  359. */
  360. for (lp=item->u2.arglist; lp; lp=lp->next) {
  361. }
  362. traverse_pval_item_template(item->u3.macro_statements,depth+1);
  363. break;
  364. case PV_CONTEXT:
  365. /* fields: item->u1.str == name of context
  366. item->u2.statements == pval list of statements in context body
  367. item->u3.abstract == int 1 if an abstract keyword were present
  368. */
  369. traverse_pval_item_template(item->u2.statements,depth+1);
  370. break;
  371. case PV_MACRO_CALL:
  372. /* fields: item->u1.str == name of macro to call
  373. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  374. item->u2.arglist->u1.str == argument
  375. item->u2.arglist->next == next arg
  376. */
  377. for (lp=item->u2.arglist; lp; lp=lp->next) {
  378. }
  379. break;
  380. case PV_APPLICATION_CALL:
  381. /* fields: item->u1.str == name of application to call
  382. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  383. item->u2.arglist->u1.str == argument
  384. item->u2.arglist->next == next arg
  385. */
  386. for (lp=item->u2.arglist; lp; lp=lp->next) {
  387. }
  388. break;
  389. case PV_CASE:
  390. /* fields: item->u1.str == value of case
  391. item->u2.statements == pval list of statements under the case
  392. */
  393. traverse_pval_item_template(item->u2.statements,depth+1);
  394. break;
  395. case PV_PATTERN:
  396. /* fields: item->u1.str == value of case
  397. item->u2.statements == pval list of statements under the case
  398. */
  399. traverse_pval_item_template(item->u2.statements,depth+1);
  400. break;
  401. case PV_DEFAULT:
  402. /* fields:
  403. item->u2.statements == pval list of statements under the case
  404. */
  405. traverse_pval_item_template(item->u2.statements,depth+1);
  406. break;
  407. case PV_CATCH:
  408. /* fields: item->u1.str == name of extension to catch
  409. item->u2.statements == pval list of statements in context body
  410. */
  411. traverse_pval_item_template(item->u2.statements,depth+1);
  412. break;
  413. case PV_SWITCHES:
  414. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  415. */
  416. traverse_pval_item_template(item->u1.list,depth+1);
  417. break;
  418. case PV_ESWITCHES:
  419. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  420. */
  421. traverse_pval_item_template(item->u1.list,depth+1);
  422. break;
  423. case PV_INCLUDES:
  424. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  425. item->u2.arglist == pval list of 4 PV_WORD elements for time values
  426. */
  427. traverse_pval_item_template(item->u1.list,depth+1);
  428. traverse_pval_item_template(item->u2.arglist,depth+1);
  429. break;
  430. case PV_STATEMENTBLOCK:
  431. /* fields: item->u1.list == pval list of statements in block, one per entry in the list
  432. */
  433. traverse_pval_item_template(item->u1.list,depth+1);
  434. break;
  435. case PV_LOCALVARDEC:
  436. case PV_VARDEC:
  437. /* fields: item->u1.str == variable name
  438. item->u2.val == variable value to assign
  439. */
  440. break;
  441. case PV_GOTO:
  442. /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
  443. item->u1.list->u1.str == where the data on a PV_WORD will always be.
  444. */
  445. if ( item->u1.list->next )
  446. ;
  447. if ( item->u1.list->next && item->u1.list->next->next )
  448. ;
  449. break;
  450. case PV_LABEL:
  451. /* fields: item->u1.str == label name
  452. */
  453. break;
  454. case PV_FOR:
  455. /* fields: item->u1.for_init == a string containing the initalizer
  456. item->u2.for_test == a string containing the loop test
  457. item->u3.for_inc == a string containing the loop increment
  458. item->u4.for_statements == a pval list of statements in the for ()
  459. */
  460. traverse_pval_item_template(item->u4.for_statements,depth+1);
  461. break;
  462. case PV_WHILE:
  463. /* fields: item->u1.str == the while conditional, as supplied by user
  464. item->u2.statements == a pval list of statements in the while ()
  465. */
  466. traverse_pval_item_template(item->u2.statements,depth+1);
  467. break;
  468. case PV_BREAK:
  469. /* fields: none
  470. */
  471. break;
  472. case PV_RETURN:
  473. /* fields: none
  474. */
  475. break;
  476. case PV_CONTINUE:
  477. /* fields: none
  478. */
  479. break;
  480. case PV_IFTIME:
  481. /* fields: item->u1.list == there are 4 linked PV_WORDs here.
  482. item->u2.statements == a pval list of statements in the if ()
  483. item->u3.else_statements == a pval list of statements in the else
  484. (could be zero)
  485. */
  486. traverse_pval_item_template(item->u2.statements,depth+1);
  487. if ( item->u3.else_statements ) {
  488. traverse_pval_item_template(item->u3.else_statements,depth+1);
  489. }
  490. break;
  491. case PV_RANDOM:
  492. /* fields: item->u1.str == the random number expression, as supplied by user
  493. item->u2.statements == a pval list of statements in the if ()
  494. item->u3.else_statements == a pval list of statements in the else
  495. (could be zero)
  496. */
  497. traverse_pval_item_template(item->u2.statements,depth+1);
  498. if ( item->u3.else_statements ) {
  499. traverse_pval_item_template(item->u3.else_statements,depth+1);
  500. }
  501. break;
  502. case PV_IF:
  503. /* fields: item->u1.str == the if conditional, as supplied by user
  504. item->u2.statements == a pval list of statements in the if ()
  505. item->u3.else_statements == a pval list of statements in the else
  506. (could be zero)
  507. */
  508. traverse_pval_item_template(item->u2.statements,depth+1);
  509. if ( item->u3.else_statements ) {
  510. traverse_pval_item_template(item->u3.else_statements,depth+1);
  511. }
  512. break;
  513. case PV_SWITCH:
  514. /* fields: item->u1.str == the switch expression
  515. item->u2.statements == a pval list of statements in the switch,
  516. (will be case statements, most likely!)
  517. */
  518. traverse_pval_item_template(item->u2.statements,depth+1);
  519. break;
  520. case PV_EXTENSION:
  521. /* fields: item->u1.str == the extension name, label, whatever it's called
  522. item->u2.statements == a pval list of statements in the extension
  523. item->u3.hints == a char * hint argument
  524. item->u4.regexten == an int boolean. non-zero says that regexten was specified
  525. */
  526. traverse_pval_item_template(item->u2.statements,depth+1);
  527. break;
  528. case PV_IGNOREPAT:
  529. /* fields: item->u1.str == the ignorepat data
  530. */
  531. break;
  532. case PV_GLOBALS:
  533. /* fields: item->u1.statements == pval list of statements, usually vardecs
  534. */
  535. traverse_pval_item_template(item->u1.statements,depth+1);
  536. break;
  537. }
  538. }
  539. void traverse_pval_template(pval *item, int depth) /* depth comes in handy for a pretty print (indentation),
  540. but you may not need it */
  541. {
  542. pval *i;
  543. for (i=item; i; i=i->next) {
  544. traverse_pval_item_template(i, depth);
  545. }
  546. }
  547. /* SEMANTIC CHECKING FOR AEL: ============================================================================= */
  548. /* (not all that is syntactically legal is good! */
  549. static void check_macro_returns(pval *macro)
  550. {
  551. pval *i;
  552. if (!macro->u3.macro_statements)
  553. {
  554. pval *z = calloc(1, sizeof(struct pval));
  555. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s is empty! I will insert a return.\n",
  556. macro->filename, macro->startline, macro->endline, macro->u1.str);
  557. z->type = PV_RETURN;
  558. z->startline = macro->startline;
  559. z->endline = macro->endline;
  560. z->startcol = macro->startcol;
  561. z->endcol = macro->endcol;
  562. z->filename = strdup(macro->filename);
  563. macro->u3.macro_statements = z;
  564. return;
  565. }
  566. for (i=macro->u3.macro_statements; i; i=i->next) {
  567. /* if the last statement in the list is not return, then insert a return there */
  568. if (i->next == NULL) {
  569. if (i->type != PV_RETURN) {
  570. pval *z = calloc(1, sizeof(struct pval));
  571. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The macro %s does not end with a return; I will insert one.\n",
  572. macro->filename, macro->startline, macro->endline, macro->u1.str);
  573. z->type = PV_RETURN;
  574. z->startline = macro->startline;
  575. z->endline = macro->endline;
  576. z->startcol = macro->startcol;
  577. z->endcol = macro->endcol;
  578. z->filename = strdup(macro->filename);
  579. i->next = z;
  580. return;
  581. }
  582. }
  583. }
  584. return;
  585. }
  586. static int extension_matches(pval *here, const char *exten, const char *pattern)
  587. {
  588. int err1;
  589. regex_t preg;
  590. /* simple case, they match exactly, the pattern and exten name */
  591. if (strcmp(pattern,exten) == 0)
  592. return 1;
  593. if (pattern[0] == '_') {
  594. char reg1[2000];
  595. const char *p;
  596. char *r = reg1;
  597. if ( strlen(pattern)*5 >= 2000 ) /* safety valve */ {
  598. ast_log(LOG_ERROR,"Error: The pattern %s is way too big. Pattern matching cancelled.\n",
  599. pattern);
  600. return 0;
  601. }
  602. /* form a regular expression from the pattern, and then match it against exten */
  603. *r++ = '^'; /* what if the extension is a pattern ?? */
  604. *r++ = '_'; /* what if the extension is a pattern ?? */
  605. *r++ = '?';
  606. for (p=pattern+1; *p; p++) {
  607. switch ( *p ) {
  608. case 'X':
  609. *r++ = '[';
  610. *r++ = '0';
  611. *r++ = '-';
  612. *r++ = '9';
  613. *r++ = 'X';
  614. *r++ = ']';
  615. break;
  616. case 'Z':
  617. *r++ = '[';
  618. *r++ = '1';
  619. *r++ = '-';
  620. *r++ = '9';
  621. *r++ = 'Z';
  622. *r++ = ']';
  623. break;
  624. case 'N':
  625. *r++ = '[';
  626. *r++ = '2';
  627. *r++ = '-';
  628. *r++ = '9';
  629. *r++ = 'N';
  630. *r++ = ']';
  631. break;
  632. case '[':
  633. while ( *p && *p != ']' ) {
  634. *r++ = *p++;
  635. }
  636. *r++ = ']';
  637. if ( *p != ']') {
  638. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The extension pattern '%s' is missing a closing bracket \n",
  639. here->filename, here->startline, here->endline, pattern);
  640. }
  641. break;
  642. case '.':
  643. case '!':
  644. *r++ = '.';
  645. *r++ = '*';
  646. break;
  647. case '*':
  648. *r++ = '\\';
  649. *r++ = '*';
  650. break;
  651. default:
  652. *r++ = *p;
  653. break;
  654. }
  655. }
  656. *r++ = '$'; /* what if the extension is a pattern ?? */
  657. *r++ = *p++; /* put in the closing null */
  658. err1 = regcomp(&preg, reg1, REG_NOSUB|REG_EXTENDED);
  659. if ( err1 ) {
  660. char errmess[500];
  661. regerror(err1,&preg,errmess,sizeof(errmess));
  662. regfree(&preg);
  663. ast_log(LOG_WARNING, "Regcomp of %s failed, error code %d\n",
  664. reg1, err1);
  665. return 0;
  666. }
  667. err1 = regexec(&preg, exten, 0, 0, 0);
  668. regfree(&preg);
  669. if ( err1 ) {
  670. /* ast_log(LOG_NOTICE,"*****************************[%d]Extension %s did not match %s(%s)\n",
  671. err1,exten, pattern, reg1); */
  672. return 0; /* no match */
  673. } else {
  674. /* ast_log(LOG_NOTICE,"*****************************Extension %s matched %s\n",
  675. exten, pattern); */
  676. return 1;
  677. }
  678. } else {
  679. if ( strcmp(exten,pattern) == 0 ) {
  680. return 1;
  681. } else
  682. return 0;
  683. }
  684. }
  685. static void check_expr2_input(pval *expr, char *str)
  686. {
  687. int spaces = strspn(str,"\t \n");
  688. if ( !strncmp(str+spaces,"$[",2) ) {
  689. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The expression '%s' is redundantly wrapped in '$[ ]'. \n",
  690. expr->filename, expr->startline, expr->endline, str);
  691. warns++;
  692. }
  693. }
  694. static void check_includes(pval *includes)
  695. {
  696. struct pval *p4;
  697. for (p4=includes->u1.list; p4; p4=p4->next) {
  698. /* for each context pointed to, find it, then find a context/label that matches the
  699. target here! */
  700. char *incl_context = p4->u1.str;
  701. /* find a matching context name */
  702. struct pval *that_other_context = find_context(incl_context);
  703. if (!that_other_context && strcmp(incl_context, "parkedcalls") != 0) {
  704. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The included context '%s' cannot be found.\n\
  705. (You may ignore this warning if '%s' exists in extensions.conf, or is created by another module. I cannot check for those.)\n",
  706. includes->filename, includes->startline, includes->endline, incl_context, incl_context);
  707. warns++;
  708. }
  709. }
  710. }
  711. static void check_timerange(pval *p)
  712. {
  713. char *times;
  714. char *e;
  715. int s1, s2;
  716. int e1, e2;
  717. times = ast_strdupa(p->u1.str);
  718. /* Star is all times */
  719. if (ast_strlen_zero(times) || !strcmp(times, "*")) {
  720. return;
  721. }
  722. /* Otherwise expect a range */
  723. e = strchr(times, '-');
  724. if (!e) {
  725. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) requires a '-' surrounded by two 24-hour times of day!\n",
  726. p->filename, p->startline, p->endline, times);
  727. warns++;
  728. return;
  729. }
  730. *e = '\0';
  731. e++;
  732. while (*e && !isdigit(*e))
  733. e++;
  734. if (!*e) {
  735. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The time range format (%s) is missing the end time!\n",
  736. p->filename, p->startline, p->endline, p->u1.str);
  737. warns++;
  738. }
  739. if (sscanf(times, "%2d:%2d", &s1, &s2) != 2) {
  740. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) isn't quite right!\n",
  741. p->filename, p->startline, p->endline, times);
  742. warns++;
  743. }
  744. if (sscanf(e, "%2d:%2d", &e1, &e2) != 2) {
  745. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) isn't quite right!\n",
  746. p->filename, p->startline, p->endline, times);
  747. warns++;
  748. }
  749. s1 = s1 * 30 + s2/2;
  750. if ((s1 < 0) || (s1 >= 24*30)) {
  751. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start time (%s) is out of range!\n",
  752. p->filename, p->startline, p->endline, times);
  753. warns++;
  754. }
  755. e1 = e1 * 30 + e2/2;
  756. if ((e1 < 0) || (e1 >= 24*30)) {
  757. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end time (%s) is out of range!\n",
  758. p->filename, p->startline, p->endline, e);
  759. warns++;
  760. }
  761. return;
  762. }
  763. static char *days[] =
  764. {
  765. "sun",
  766. "mon",
  767. "tue",
  768. "wed",
  769. "thu",
  770. "fri",
  771. "sat",
  772. };
  773. /*! \brief get_dow: Get day of week */
  774. static void check_dow(pval *DOW)
  775. {
  776. char *dow;
  777. char *c;
  778. /* The following line is coincidence, really! */
  779. int s, e;
  780. dow = ast_strdupa(DOW->u1.str);
  781. /* Check for all days */
  782. if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
  783. return;
  784. /* Get start and ending days */
  785. c = strchr(dow, '-');
  786. if (c) {
  787. *c = '\0';
  788. c++;
  789. } else
  790. c = NULL;
  791. /* Find the start */
  792. s = 0;
  793. while ((s < 7) && strcasecmp(dow, days[s])) s++;
  794. if (s >= 7) {
  795. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The day (%s) must be one of 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'!\n",
  796. DOW->filename, DOW->startline, DOW->endline, dow);
  797. warns++;
  798. }
  799. if (c) {
  800. e = 0;
  801. while ((e < 7) && strcasecmp(c, days[e])) e++;
  802. if (e >= 7) {
  803. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day (%s) must be one of 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'!\n",
  804. DOW->filename, DOW->startline, DOW->endline, c);
  805. warns++;
  806. }
  807. } else
  808. e = s;
  809. }
  810. static void check_day(pval *DAY)
  811. {
  812. char *day;
  813. char *c;
  814. /* The following line is coincidence, really! */
  815. int s, e;
  816. day = ast_strdupa(DAY->u1.str);
  817. /* Check for all days */
  818. if (ast_strlen_zero(day) || !strcmp(day, "*")) {
  819. return;
  820. }
  821. /* Get start and ending days */
  822. c = strchr(day, '-');
  823. if (c) {
  824. *c = '\0';
  825. c++;
  826. }
  827. /* Find the start */
  828. if (sscanf(day, "%2d", &s) != 1) {
  829. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number!\n",
  830. DAY->filename, DAY->startline, DAY->endline, day);
  831. warns++;
  832. }
  833. else if ((s < 1) || (s > 31)) {
  834. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start day of month (%s) must be a number in the range [1-31]!\n",
  835. DAY->filename, DAY->startline, DAY->endline, day);
  836. warns++;
  837. }
  838. s--;
  839. if (c) {
  840. if (sscanf(c, "%2d", &e) != 1) {
  841. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number!\n",
  842. DAY->filename, DAY->startline, DAY->endline, c);
  843. warns++;
  844. }
  845. else if ((e < 1) || (e > 31)) {
  846. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end day of month (%s) must be a number in the range [1-31]!\n",
  847. DAY->filename, DAY->startline, DAY->endline, day);
  848. warns++;
  849. }
  850. e--;
  851. } else
  852. e = s;
  853. }
  854. static char *months[] =
  855. {
  856. "jan",
  857. "feb",
  858. "mar",
  859. "apr",
  860. "may",
  861. "jun",
  862. "jul",
  863. "aug",
  864. "sep",
  865. "oct",
  866. "nov",
  867. "dec",
  868. };
  869. static void check_month(pval *MON)
  870. {
  871. char *mon;
  872. char *c;
  873. /* The following line is coincidence, really! */
  874. int s, e;
  875. mon = ast_strdupa(MON->u1.str);
  876. /* Check for all days */
  877. if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
  878. return ;
  879. /* Get start and ending days */
  880. c = strchr(mon, '-');
  881. if (c) {
  882. *c = '\0';
  883. c++;
  884. }
  885. /* Find the start */
  886. s = 0;
  887. while ((s < 12) && strcasecmp(mon, months[s])) s++;
  888. if (s >= 12) {
  889. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The start month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
  890. MON->filename, MON->startline, MON->endline, mon);
  891. warns++;
  892. }
  893. if (c) {
  894. e = 0;
  895. while ((e < 12) && strcasecmp(mon, months[e])) e++;
  896. if (e >= 12) {
  897. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The end month (%s) must be a one of: 'jan', 'feb', ..., 'dec'!\n",
  898. MON->filename, MON->startline, MON->endline, c);
  899. warns++;
  900. }
  901. } else
  902. e = s;
  903. }
  904. static int check_break(pval *item)
  905. {
  906. pval *p = item;
  907. while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
  908. /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
  909. no sense */
  910. if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN
  911. || p->type == PV_WHILE || p->type == PV_FOR ) {
  912. return 1;
  913. }
  914. p = p->dad;
  915. }
  916. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'break' not in switch, for, or while statement!\n",
  917. item->filename, item->startline, item->endline);
  918. errs++;
  919. return 0;
  920. }
  921. static int check_continue(pval *item)
  922. {
  923. pval *p = item;
  924. while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
  925. /* a break is allowed in WHILE, FOR, CASE, DEFAULT, PATTERN; otherwise, it don't make
  926. no sense */
  927. if( p->type == PV_WHILE || p->type == PV_FOR ) {
  928. return 1;
  929. }
  930. p = p->dad;
  931. }
  932. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: 'continue' not in 'for' or 'while' statement!\n",
  933. item->filename, item->startline, item->endline);
  934. errs++;
  935. return 0;
  936. }
  937. static struct pval *in_macro(pval *item)
  938. {
  939. struct pval *curr;
  940. curr = item;
  941. while( curr ) {
  942. if( curr->type == PV_MACRO ) {
  943. return curr;
  944. }
  945. curr = curr->dad;
  946. }
  947. return 0;
  948. }
  949. static struct pval *in_context(pval *item)
  950. {
  951. struct pval *curr;
  952. curr = item;
  953. while( curr ) {
  954. if( curr->type == PV_MACRO || curr->type == PV_CONTEXT ) {
  955. return curr;
  956. }
  957. curr = curr->dad;
  958. }
  959. return 0;
  960. }
  961. /* general purpose goto finder */
  962. static void check_label(pval *item)
  963. {
  964. struct pval *curr;
  965. struct pval *x;
  966. int alright = 0;
  967. /* A label outside an extension just plain does not make sense! */
  968. curr = item;
  969. while( curr ) {
  970. if( curr->type == PV_MACRO || curr->type == PV_EXTENSION ) {
  971. alright = 1;
  972. break;
  973. }
  974. curr = curr->dad;
  975. }
  976. if( !alright )
  977. {
  978. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Label %s is not within an extension or macro!\n",
  979. item->filename, item->startline, item->endline, item->u1.str);
  980. errs++;
  981. }
  982. /* basically, ensure that a label is not repeated in a context. Period.
  983. The method: well, for each label, find the first label in the context
  984. with the same name. If it's not the current label, then throw an error. */
  985. /* printf("==== check_label: ====\n"); */
  986. if( !current_extension )
  987. curr = current_context;
  988. else
  989. curr = current_extension;
  990. x = find_first_label_in_current_context((char *)item->u1.str, curr);
  991. /* printf("Hey, check_label found with item = %x, and x is %x, and currcont is %x, label name is %s\n", item,x, current_context, (char *)item->u1.str); */
  992. if( x && x != item )
  993. {
  994. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: Duplicate label %s! Previously defined at file %s, line %d.\n",
  995. item->filename, item->startline, item->endline, item->u1.str, x->filename, x->startline);
  996. errs++;
  997. }
  998. /* printf("<<<<< check_label: ====\n"); */
  999. }
  1000. static pval *get_goto_target(pval *item)
  1001. {
  1002. /* just one item-- the label should be in the current extension */
  1003. pval *curr_ext = get_extension_or_contxt(item); /* containing exten, or macro */
  1004. pval *curr_cont;
  1005. if (!item->u1.list) {
  1006. return NULL;
  1007. }
  1008. if (!item->u1.list->next && !strstr((item->u1.list)->u1.str,"${")) {
  1009. struct pval *x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), curr_ext);
  1010. return x;
  1011. }
  1012. curr_cont = get_contxt(item);
  1013. /* TWO items */
  1014. if (item->u1.list->next && !item->u1.list->next->next) {
  1015. if (!strstr((item->u1.list)->u1.str,"${")
  1016. && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
  1017. struct pval *x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, curr_cont);
  1018. return x;
  1019. }
  1020. }
  1021. /* All 3 items! */
  1022. if (item->u1.list->next && item->u1.list->next->next) {
  1023. /* all three */
  1024. pval *first = item->u1.list;
  1025. pval *second = item->u1.list->next;
  1026. pval *third = item->u1.list->next->next;
  1027. if (!strstr((item->u1.list)->u1.str,"${")
  1028. && !strstr(item->u1.list->next->u1.str,"${")
  1029. && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
  1030. struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
  1031. if (!x) {
  1032. struct pval *p3;
  1033. struct pval *that_context = find_context(item->u1.list->u1.str);
  1034. /* the target of the goto could be in an included context!! Fancy that!! */
  1035. /* look for includes in the current context */
  1036. if (that_context) {
  1037. for (p3=that_context->u2.statements; p3; p3=p3->next) {
  1038. if (p3->type == PV_INCLUDES) {
  1039. struct pval *p4;
  1040. for (p4=p3->u1.list; p4; p4=p4->next) {
  1041. /* for each context pointed to, find it, then find a context/label that matches the
  1042. target here! */
  1043. char *incl_context = p4->u1.str;
  1044. /* find a matching context name */
  1045. struct pval *that_other_context = find_context(incl_context);
  1046. if (that_other_context) {
  1047. struct pval *x3;
  1048. x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
  1049. if (x3) {
  1050. return x3;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. return x;
  1059. }
  1060. }
  1061. return NULL;
  1062. }
  1063. static void check_goto(pval *item)
  1064. {
  1065. if (!item->u1.list) {
  1066. return;
  1067. }
  1068. /* check for the target of the goto-- does it exist? */
  1069. if ( !(item->u1.list)->next && !(item->u1.list)->u1.str ) {
  1070. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: empty label reference found!\n",
  1071. item->filename, item->startline, item->endline);
  1072. errs++;
  1073. }
  1074. /* just one item-- the label should be in the current extension */
  1075. if (!item->u1.list->next && !strstr(item->u1.list->u1.str,"${")) {
  1076. struct pval *z = get_extension_or_contxt(item);
  1077. struct pval *x = 0;
  1078. if (z)
  1079. x = find_label_in_current_extension((char*)((item->u1.list)->u1.str), z); /* if in macro, use current context instead */
  1080. /* printf("Called find_label_in_current_extension with arg %s; current_extension is %x: %d\n",
  1081. (char*)((item->u1.list)->u1.str), current_extension?current_extension:current_context, current_extension?current_extension->type:current_context->type); */
  1082. if (!x) {
  1083. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s exists in the current extension!\n",
  1084. item->filename, item->startline, item->endline, item->u1.list->u1.str);
  1085. errs++;
  1086. }
  1087. else
  1088. return;
  1089. }
  1090. /* TWO items */
  1091. if (item->u1.list->next && !item->u1.list->next->next) {
  1092. /* two items */
  1093. /* printf("Calling find_label_in_current_context with args %s, %s\n",
  1094. (char*)((item->u1.list)->u1.str), (char *)item->u1.list->next->u1.str); */
  1095. if (!strstr((item->u1.list)->u1.str,"${")
  1096. && !strstr(item->u1.list->next->u1.str,"${") ) /* Don't try to match variables */ {
  1097. struct pval *z = get_contxt(item);
  1098. struct pval *x = 0;
  1099. if (z)
  1100. x = find_label_in_current_context((char *)item->u1.list->u1.str, (char *)item->u1.list->next->u1.str, z);
  1101. if (!x) {
  1102. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label '%s,%s' exists in the current context, or any of its inclusions!\n",
  1103. item->filename, item->startline, item->endline, item->u1.list->u1.str, item->u1.list->next->u1.str );
  1104. errs++;
  1105. }
  1106. else
  1107. return;
  1108. }
  1109. }
  1110. /* All 3 items! */
  1111. if (item->u1.list->next && item->u1.list->next->next) {
  1112. /* all three */
  1113. pval *first = item->u1.list;
  1114. pval *second = item->u1.list->next;
  1115. pval *third = item->u1.list->next->next;
  1116. /* printf("Calling find_label_in_current_db with args %s, %s, %s\n",
  1117. (char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str); */
  1118. if (!strstr((item->u1.list)->u1.str,"${")
  1119. && !strstr(item->u1.list->next->u1.str,"${")
  1120. && !strstr(item->u1.list->next->next->u1.str,"${")) /* Don't try to match variables */ {
  1121. struct pval *x = find_label_in_current_db((char*)first->u1.str, (char*)second->u1.str, (char*)third->u1.str);
  1122. if (!x) {
  1123. struct pval *p3;
  1124. struct pval *found = 0;
  1125. struct pval *that_context = find_context(item->u1.list->u1.str);
  1126. /* the target of the goto could be in an included context!! Fancy that!! */
  1127. /* look for includes in the current context */
  1128. if (that_context) {
  1129. for (p3=that_context->u2.statements; p3; p3=p3->next) {
  1130. if (p3->type == PV_INCLUDES) {
  1131. struct pval *p4;
  1132. for (p4=p3->u1.list; p4; p4=p4->next) {
  1133. /* for each context pointed to, find it, then find a context/label that matches the
  1134. target here! */
  1135. char *incl_context = p4->u1.str;
  1136. /* find a matching context name */
  1137. struct pval *that_other_context = find_context(incl_context);
  1138. if (that_other_context) {
  1139. struct pval *x3;
  1140. x3 = find_label_in_current_context((char *)item->u1.list->next->u1.str, (char *)item->u1.list->next->next->u1.str, that_other_context);
  1141. if (x3) {
  1142. found = x3;
  1143. break;
  1144. }
  1145. }
  1146. }
  1147. }
  1148. }
  1149. if (!found) {
  1150. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: goto: no label %s|%s exists in the context %s or its inclusions!\n",
  1151. item->filename, item->startline, item->endline, item->u1.list->next->u1.str, item->u1.list->next->next->u1.str, item->u1.list->u1.str );
  1152. errs++;
  1153. } else {
  1154. struct pval *mac = in_macro(item); /* is this goto inside a macro? */
  1155. if( mac ) { /* yes! */
  1156. struct pval *targ = in_context(found);
  1157. if( mac != targ )
  1158. {
  1159. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
  1160. item->filename, item->startline, item->endline);
  1161. warns++;
  1162. }
  1163. }
  1164. }
  1165. } else {
  1166. /* here is where code would go to check for target existence in extensions.conf files */
  1167. #ifdef STANDALONE
  1168. struct pbx_find_info pfiq = {.stacklen = 0 };
  1169. extern int localized_pbx_load_module(void);
  1170. /* if this is a standalone, we will need to make sure the
  1171. localized load of extensions.conf is done */
  1172. if (!extensions_dot_conf_loaded) {
  1173. localized_pbx_load_module();
  1174. extensions_dot_conf_loaded++;
  1175. }
  1176. pbx_find_extension(NULL, NULL, &pfiq, first->u1.str, second->u1.str, atoi(third->u1.str),
  1177. atoi(third->u1.str) ? NULL : third->u1.str, NULL,
  1178. atoi(third->u1.str) ? E_MATCH : E_FINDLABEL);
  1179. if (pfiq.status != STATUS_SUCCESS) {
  1180. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s, not even in extensions.conf!\n",
  1181. item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
  1182. warns++;
  1183. }
  1184. #else
  1185. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: goto: Couldn't find goto target %s|%s|%s in the AEL code!\n",
  1186. item->filename, item->startline, item->endline, first->u1.str, second->u1.str, third->u1.str);
  1187. warns++;
  1188. #endif
  1189. }
  1190. } else {
  1191. struct pval *mac = in_macro(item); /* is this goto inside a macro? */
  1192. if( mac ) { /* yes! */
  1193. struct pval *targ = in_context(x);
  1194. if( mac != targ )
  1195. {
  1196. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: It's bad form to have a goto in a macro to a target outside the macro!\n",
  1197. item->filename, item->startline, item->endline);
  1198. warns++;
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. }
  1205. static void find_pval_goto_item(pval *item, int lev)
  1206. {
  1207. struct pval *p4;
  1208. if (lev>100) {
  1209. ast_log(LOG_ERROR,"find_pval_goto in infinite loop! item_type: %u\n\n", item->type);
  1210. return;
  1211. }
  1212. switch ( item->type ) {
  1213. case PV_MACRO:
  1214. /* fields: item->u1.str == name of macro
  1215. item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
  1216. item->u2.arglist->u1.str == argument
  1217. item->u2.arglist->next == next arg
  1218. item->u3.macro_statements == pval list of statements in macro body.
  1219. */
  1220. /* printf("Descending into macro %s at line %d\n", item->u1.str, item->startline); */
  1221. find_pval_gotos(item->u3.macro_statements,lev+1); /* if we're just searching for a context, don't bother descending into them */
  1222. break;
  1223. case PV_CONTEXT:
  1224. /* fields: item->u1.str == name of context
  1225. item->u2.statements == pval list of statements in context body
  1226. item->u3.abstract == int 1 if an abstract keyword were present
  1227. */
  1228. break;
  1229. case PV_CASE:
  1230. /* fields: item->u1.str == value of case
  1231. item->u2.statements == pval list of statements under the case
  1232. */
  1233. /* printf("Descending into Case of %s\n", item->u1.str); */
  1234. find_pval_gotos(item->u2.statements,lev+1);
  1235. break;
  1236. case PV_PATTERN:
  1237. /* fields: item->u1.str == value of case
  1238. item->u2.statements == pval list of statements under the case
  1239. */
  1240. /* printf("Descending into Pattern of %s\n", item->u1.str); */
  1241. find_pval_gotos(item->u2.statements,lev+1);
  1242. break;
  1243. case PV_DEFAULT:
  1244. /* fields:
  1245. item->u2.statements == pval list of statements under the case
  1246. */
  1247. /* printf("Descending into default\n"); */
  1248. find_pval_gotos(item->u2.statements,lev+1);
  1249. break;
  1250. case PV_CATCH:
  1251. /* fields: item->u1.str == name of extension to catch
  1252. item->u2.statements == pval list of statements in context body
  1253. */
  1254. /* printf("Descending into catch of %s\n", item->u1.str); */
  1255. find_pval_gotos(item->u2.statements,lev+1);
  1256. break;
  1257. case PV_STATEMENTBLOCK:
  1258. /* fields: item->u1.list == pval list of statements in block, one per entry in the list
  1259. */
  1260. /* printf("Descending into statement block\n"); */
  1261. find_pval_gotos(item->u1.list,lev+1);
  1262. break;
  1263. case PV_GOTO:
  1264. /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
  1265. item->u1.list->u1.str == where the data on a PV_WORD will always be.
  1266. */
  1267. check_goto(item); /* THE WHOLE FUNCTION OF THIS ENTIRE ROUTINE!!!! */
  1268. break;
  1269. case PV_INCLUDES:
  1270. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  1271. */
  1272. for (p4=item->u1.list; p4; p4=p4->next) {
  1273. /* for each context pointed to, find it, then find a context/label that matches the
  1274. target here! */
  1275. char *incl_context = p4->u1.str;
  1276. /* find a matching context name */
  1277. struct pval *that_context = find_context(incl_context);
  1278. if (that_context && that_context->u2.statements) {
  1279. /* printf("Descending into include of '%s' at line %d; that_context=%s, that_context type=%d\n", incl_context, item->startline, that_context->u1.str, that_context->type); */
  1280. find_pval_gotos(that_context->u2.statements,lev+1); /* keep working up the includes */
  1281. }
  1282. }
  1283. break;
  1284. case PV_FOR:
  1285. /* fields: item->u1.for_init == a string containing the initalizer
  1286. item->u2.for_test == a string containing the loop test
  1287. item->u3.for_inc == a string containing the loop increment
  1288. item->u4.for_statements == a pval list of statements in the for ()
  1289. */
  1290. /* printf("Descending into for at line %d\n", item->startline); */
  1291. find_pval_gotos(item->u4.for_statements,lev+1);
  1292. break;
  1293. case PV_WHILE:
  1294. /* fields: item->u1.str == the while conditional, as supplied by user
  1295. item->u2.statements == a pval list of statements in the while ()
  1296. */
  1297. /* printf("Descending into while at line %d\n", item->startline); */
  1298. find_pval_gotos(item->u2.statements,lev+1);
  1299. break;
  1300. case PV_RANDOM:
  1301. /* fields: item->u1.str == the random number expression, as supplied by user
  1302. item->u2.statements == a pval list of statements in the if ()
  1303. item->u3.else_statements == a pval list of statements in the else
  1304. (could be zero)
  1305. fall thru to PV_IF */
  1306. case PV_IFTIME:
  1307. /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
  1308. item->u2.statements == a pval list of statements in the if ()
  1309. item->u3.else_statements == a pval list of statements in the else
  1310. (could be zero)
  1311. fall thru to PV_IF*/
  1312. case PV_IF:
  1313. /* fields: item->u1.str == the if conditional, as supplied by user
  1314. item->u2.statements == a pval list of statements in the if ()
  1315. item->u3.else_statements == a pval list of statements in the else
  1316. (could be zero)
  1317. */
  1318. /* printf("Descending into random/iftime/if at line %d\n", item->startline); */
  1319. find_pval_gotos(item->u2.statements,lev+1);
  1320. if (item->u3.else_statements) {
  1321. /* printf("Descending into random/iftime/if's ELSE at line %d\n", item->startline); */
  1322. find_pval_gotos(item->u3.else_statements,lev+1);
  1323. }
  1324. break;
  1325. case PV_SWITCH:
  1326. /* fields: item->u1.str == the switch expression
  1327. item->u2.statements == a pval list of statements in the switch,
  1328. (will be case statements, most likely!)
  1329. */
  1330. /* printf("Descending into switch at line %d\n", item->startline); */
  1331. find_pval_gotos(item->u3.else_statements,lev+1);
  1332. break;
  1333. case PV_EXTENSION:
  1334. /* fields: item->u1.str == the extension name, label, whatever it's called
  1335. item->u2.statements == a pval list of statements in the extension
  1336. item->u3.hints == a char * hint argument
  1337. item->u4.regexten == an int boolean. non-zero says that regexten was specified
  1338. */
  1339. /* printf("Descending into extension %s at line %d\n", item->u1.str, item->startline); */
  1340. find_pval_gotos(item->u2.statements,lev+1);
  1341. break;
  1342. default:
  1343. break;
  1344. }
  1345. }
  1346. static void find_pval_gotos(pval *item,int lev)
  1347. {
  1348. pval *i;
  1349. for (i=item; i; i=i->next) {
  1350. /* printf("About to call pval_goto_item, itemcount=%d, itemtype=%d\n", item_count, i->type); */
  1351. find_pval_goto_item(i, lev);
  1352. }
  1353. }
  1354. /* general purpose label finder */
  1355. static struct pval *match_pval_item(pval *item)
  1356. {
  1357. pval *x;
  1358. switch ( item->type ) {
  1359. case PV_MACRO:
  1360. /* fields: item->u1.str == name of macro
  1361. item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
  1362. item->u2.arglist->u1.str == argument
  1363. item->u2.arglist->next == next arg
  1364. item->u3.macro_statements == pval list of statements in macro body.
  1365. */
  1366. /* printf(" matching in MACRO %s, match_context=%s; retoncontmtch=%d; \n", item->u1.str, match_context, return_on_context_match); */
  1367. if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
  1368. /* printf("MACRO: match context is: %s\n", match_context); */
  1369. if (return_on_context_match && !strcmp(item->u1.str, match_context)) /* if we're just searching for a context, don't bother descending into them */ {
  1370. /* printf("Returning on matching macro %s\n", match_context); */
  1371. return item;
  1372. }
  1373. if (!return_on_context_match) {
  1374. /* printf("Descending into matching macro %s/%s\n", match_context, item->u1.str); */
  1375. if ((x=match_pval(item->u3.macro_statements))) {
  1376. /* printf("Responded with pval match %x\n", x); */
  1377. return x;
  1378. }
  1379. }
  1380. } else {
  1381. /* printf("Skipping context/macro %s\n", item->u1.str); */
  1382. }
  1383. break;
  1384. case PV_CONTEXT:
  1385. /* fields: item->u1.str == name of context
  1386. item->u2.statements == pval list of statements in context body
  1387. item->u3.abstract == int 1 if an abstract keyword were present
  1388. */
  1389. /* printf(" matching in CONTEXT\n"); */
  1390. if (!strcmp(match_context,"*") || !strcmp(item->u1.str, match_context)) {
  1391. if (return_on_context_match && !strcmp(item->u1.str, match_context)) {
  1392. /* printf("Returning on matching context %s\n", match_context); */
  1393. /* printf("non-CONTEXT: Responded with pval match %x\n", x); */
  1394. return item;
  1395. }
  1396. if (!return_on_context_match ) {
  1397. /* printf("Descending into matching context %s\n", match_context); */
  1398. if ((x=match_pval(item->u2.statements))) /* if we're just searching for a context, don't bother descending into them */ {
  1399. /* printf("CONTEXT: Responded with pval match %x\n", x); */
  1400. return x;
  1401. }
  1402. }
  1403. } else {
  1404. /* printf("Skipping context/macro %s\n", item->u1.str); */
  1405. }
  1406. break;
  1407. case PV_CASE:
  1408. /* fields: item->u1.str == value of case
  1409. item->u2.statements == pval list of statements under the case
  1410. */
  1411. /* printf(" matching in CASE\n"); */
  1412. if ((x=match_pval(item->u2.statements))) {
  1413. /* printf("CASE: Responded with pval match %x\n", x); */
  1414. return x;
  1415. }
  1416. break;
  1417. case PV_PATTERN:
  1418. /* fields: item->u1.str == value of case
  1419. item->u2.statements == pval list of statements under the case
  1420. */
  1421. /* printf(" matching in PATTERN\n"); */
  1422. if ((x=match_pval(item->u2.statements))) {
  1423. /* printf("PATTERN: Responded with pval match %x\n", x); */
  1424. return x;
  1425. }
  1426. break;
  1427. case PV_DEFAULT:
  1428. /* fields:
  1429. item->u2.statements == pval list of statements under the case
  1430. */
  1431. /* printf(" matching in DEFAULT\n"); */
  1432. if ((x=match_pval(item->u2.statements))) {
  1433. /* printf("DEFAULT: Responded with pval match %x\n", x); */
  1434. return x;
  1435. }
  1436. break;
  1437. case PV_CATCH:
  1438. /* fields: item->u1.str == name of extension to catch
  1439. item->u2.statements == pval list of statements in context body
  1440. */
  1441. /* printf(" matching in CATCH\n"); */
  1442. if ((x=match_pval(item->u2.statements))) {
  1443. /* printf("CATCH: Responded with pval match %x\n", x); */
  1444. return x;
  1445. }
  1446. break;
  1447. case PV_STATEMENTBLOCK:
  1448. /* fields: item->u1.list == pval list of statements in block, one per entry in the list
  1449. */
  1450. /* printf(" matching in STATEMENTBLOCK\n"); */
  1451. if ((x=match_pval(item->u1.list))) {
  1452. /* printf("STATEMENTBLOCK: Responded with pval match %x\n", x); */
  1453. return x;
  1454. }
  1455. break;
  1456. case PV_LABEL:
  1457. /* fields: item->u1.str == label name
  1458. */
  1459. /* printf("PV_LABEL %s (cont=%s, exten=%s\n",
  1460. item->u1.str, current_context->u1.str, (current_extension?current_extension->u1.str:"<macro>"));*/
  1461. if (count_labels) {
  1462. if (!strcmp(match_label, item->u1.str)) {
  1463. label_count++;
  1464. last_matched_label = item;
  1465. }
  1466. } else {
  1467. if (!strcmp(match_label, item->u1.str)) {
  1468. /* printf("LABEL: Responded with pval match %x\n", x); */
  1469. return item;
  1470. }
  1471. }
  1472. break;
  1473. case PV_FOR:
  1474. /* fields: item->u1.for_init == a string containing the initalizer
  1475. item->u2.for_test == a string containing the loop test
  1476. item->u3.for_inc == a string containing the loop increment
  1477. item->u4.for_statements == a pval list of statements in the for ()
  1478. */
  1479. /* printf(" matching in FOR\n"); */
  1480. if ((x=match_pval(item->u4.for_statements))) {
  1481. /* printf("FOR: Responded with pval match %x\n", x);*/
  1482. return x;
  1483. }
  1484. break;
  1485. case PV_WHILE:
  1486. /* fields: item->u1.str == the while conditional, as supplied by user
  1487. item->u2.statements == a pval list of statements in the while ()
  1488. */
  1489. /* printf(" matching in WHILE\n"); */
  1490. if ((x=match_pval(item->u2.statements))) {
  1491. /* printf("WHILE: Responded with pval match %x\n", x); */
  1492. return x;
  1493. }
  1494. break;
  1495. case PV_RANDOM:
  1496. /* fields: item->u1.str == the random number expression, as supplied by user
  1497. item->u2.statements == a pval list of statements in the if ()
  1498. item->u3.else_statements == a pval list of statements in the else
  1499. (could be zero)
  1500. fall thru to PV_IF */
  1501. case PV_IFTIME:
  1502. /* fields: item->u1.list == the time values, 4 of them, as PV_WORD structs in a list
  1503. item->u2.statements == a pval list of statements in the if ()
  1504. item->u3.else_statements == a pval list of statements in the else
  1505. (could be zero)
  1506. fall thru to PV_IF*/
  1507. case PV_IF:
  1508. /* fields: item->u1.str == the if conditional, as supplied by user
  1509. item->u2.statements == a pval list of statements in the if ()
  1510. item->u3.else_statements == a pval list of statements in the else
  1511. (could be zero)
  1512. */
  1513. /* printf(" matching in IF/IFTIME/RANDOM\n"); */
  1514. if ((x=match_pval(item->u2.statements))) {
  1515. return x;
  1516. }
  1517. if (item->u3.else_statements) {
  1518. if ((x=match_pval(item->u3.else_statements))) {
  1519. /* printf("IF/IFTIME/RANDOM: Responded with pval match %x\n", x); */
  1520. return x;
  1521. }
  1522. }
  1523. break;
  1524. case PV_SWITCH:
  1525. /* fields: item->u1.str == the switch expression
  1526. item->u2.statements == a pval list of statements in the switch,
  1527. (will be case statements, most likely!)
  1528. */
  1529. /* printf(" matching in SWITCH\n"); */
  1530. if ((x=match_pval(item->u2.statements))) {
  1531. /* printf("SWITCH: Responded with pval match %x\n", x); */
  1532. return x;
  1533. }
  1534. break;
  1535. case PV_EXTENSION:
  1536. /* fields: item->u1.str == the extension name, label, whatever it's called
  1537. item->u2.statements == a pval list of statements in the extension
  1538. item->u3.hints == a char * hint argument
  1539. item->u4.regexten == an int boolean. non-zero says that regexten was specified
  1540. */
  1541. /* printf(" matching in EXTENSION\n"); */
  1542. if (!strcmp(match_exten,"*") || extension_matches(item, match_exten, item->u1.str) ) {
  1543. /* printf("Descending into matching exten %s => %s\n", match_exten, item->u1.str); */
  1544. if (strcmp(match_label,"1") == 0) {
  1545. if (item->u2.statements) {
  1546. struct pval *p5 = item->u2.statements;
  1547. while (p5 && p5->type == PV_LABEL) /* find the first non-label statement in this context. If it exists, there's a "1" */
  1548. p5 = p5->next;
  1549. if (p5)
  1550. return p5;
  1551. else
  1552. return 0;
  1553. }
  1554. else
  1555. return 0;
  1556. }
  1557. if ((x=match_pval(item->u2.statements))) {
  1558. /* printf("EXTENSION: Responded with pval match %x\n", x); */
  1559. return x;
  1560. }
  1561. } else {
  1562. /* printf("Skipping exten %s\n", item->u1.str); */
  1563. }
  1564. break;
  1565. default:
  1566. /* printf(" matching in default = %d\n", item->type); */
  1567. break;
  1568. }
  1569. return 0;
  1570. }
  1571. struct pval *match_pval(pval *item)
  1572. {
  1573. pval *i;
  1574. for (i=item; i; i=i->next) {
  1575. pval *x;
  1576. /* printf(" -- match pval: item %d\n", i->type); */
  1577. if ((x = match_pval_item(i))) {
  1578. /* printf("match_pval: returning x=%x\n", (int)x); */
  1579. return x; /* cut the search short */
  1580. }
  1581. }
  1582. return 0;
  1583. }
  1584. #if 0
  1585. int count_labels_in_current_context(char *label)
  1586. {
  1587. label_count = 0;
  1588. count_labels = 1;
  1589. return_on_context_match = 0;
  1590. match_pval(current_context->u2.statements);
  1591. return label_count;
  1592. }
  1593. #endif
  1594. struct pval *find_first_label_in_current_context(char *label, pval *curr_cont)
  1595. {
  1596. /* printf(" --- Got args %s, %s\n", exten, label); */
  1597. struct pval *ret;
  1598. struct pval *p3;
  1599. count_labels = 0;
  1600. return_on_context_match = 0;
  1601. match_context = "*";
  1602. match_exten = "*";
  1603. match_label = label;
  1604. ret = match_pval(curr_cont);
  1605. if (ret)
  1606. return ret;
  1607. /* the target of the goto could be in an included context!! Fancy that!! */
  1608. /* look for includes in the current context */
  1609. for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
  1610. if (p3->type == PV_INCLUDES) {
  1611. struct pval *p4;
  1612. for (p4=p3->u1.list; p4; p4=p4->next) {
  1613. /* for each context pointed to, find it, then find a context/label that matches the
  1614. target here! */
  1615. char *incl_context = p4->u1.str;
  1616. /* find a matching context name */
  1617. struct pval *that_context = find_context(incl_context);
  1618. if (that_context) {
  1619. struct pval *x3;
  1620. x3 = find_first_label_in_current_context(label, that_context);
  1621. if (x3) {
  1622. return x3;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. }
  1628. return 0;
  1629. }
  1630. struct pval *find_label_in_current_context(char *exten, char *label, pval *curr_cont)
  1631. {
  1632. /* printf(" --- Got args %s, %s\n", exten, label); */
  1633. struct pval *ret;
  1634. struct pval *p3;
  1635. count_labels = 0;
  1636. return_on_context_match = 0;
  1637. match_context = "*";
  1638. match_exten = exten;
  1639. match_label = label;
  1640. ret = match_pval(curr_cont->u2.statements);
  1641. if (ret)
  1642. return ret;
  1643. /* the target of the goto could be in an included context!! Fancy that!! */
  1644. /* look for includes in the current context */
  1645. for (p3=curr_cont->u2.statements; p3; p3=p3->next) {
  1646. if (p3->type == PV_INCLUDES) {
  1647. struct pval *p4;
  1648. for (p4=p3->u1.list; p4; p4=p4->next) {
  1649. /* for each context pointed to, find it, then find a context/label that matches the
  1650. target here! */
  1651. char *incl_context = p4->u1.str;
  1652. /* find a matching context name */
  1653. struct pval *that_context = find_context(incl_context);
  1654. if (that_context) {
  1655. struct pval *x3;
  1656. x3 = find_label_in_current_context(exten, label, that_context);
  1657. if (x3) {
  1658. return x3;
  1659. }
  1660. }
  1661. }
  1662. }
  1663. }
  1664. return 0;
  1665. }
  1666. static struct pval *find_label_in_current_extension(const char *label, pval *curr_ext)
  1667. {
  1668. /* printf(" --- Got args %s\n", label); */
  1669. count_labels = 0;
  1670. return_on_context_match = 0;
  1671. match_context = "*";
  1672. match_exten = "*";
  1673. match_label = label;
  1674. return match_pval(curr_ext);
  1675. }
  1676. static struct pval *find_label_in_current_db(const char *context, const char *exten, const char *label)
  1677. {
  1678. /* printf(" --- Got args %s, %s, %s\n", context, exten, label); */
  1679. count_labels = 0;
  1680. return_on_context_match = 0;
  1681. match_context = context;
  1682. match_exten = exten;
  1683. match_label = label;
  1684. return match_pval(current_db);
  1685. }
  1686. struct pval *find_macro(char *name)
  1687. {
  1688. return_on_context_match = 1;
  1689. count_labels = 0;
  1690. match_context = name;
  1691. match_exten = "*"; /* don't really need to set these, shouldn't be reached */
  1692. match_label = "*";
  1693. return match_pval(current_db);
  1694. }
  1695. struct pval *find_context(char *name)
  1696. {
  1697. return_on_context_match = 1;
  1698. count_labels = 0;
  1699. match_context = name;
  1700. match_exten = "*"; /* don't really need to set these, shouldn't be reached */
  1701. match_label = "*";
  1702. return match_pval(current_db);
  1703. }
  1704. int is_float(char *arg )
  1705. {
  1706. char *s;
  1707. for (s=arg; *s; s++) {
  1708. if (*s != '.' && (*s < '0' || *s > '9'))
  1709. return 0;
  1710. }
  1711. return 1;
  1712. }
  1713. int is_int(char *arg )
  1714. {
  1715. char *s;
  1716. for (s=arg; *s; s++) {
  1717. if (*s < '0' || *s > '9')
  1718. return 0;
  1719. }
  1720. return 1;
  1721. }
  1722. int is_empty(char *arg)
  1723. {
  1724. if (!arg)
  1725. return 1;
  1726. if (*arg == 0)
  1727. return 1;
  1728. while (*arg) {
  1729. if (*arg != ' ' && *arg != '\t')
  1730. return 0;
  1731. arg++;
  1732. }
  1733. return 1;
  1734. }
  1735. #ifdef AAL_ARGCHECK
  1736. int option_matches_j( struct argdesc *should, pval *is, struct argapp *app)
  1737. {
  1738. struct argchoice *ac;
  1739. char *opcop,*q,*p;
  1740. switch (should->dtype) {
  1741. case ARGD_OPTIONSET:
  1742. if ( strstr(is->u1.str,"${") )
  1743. return 0; /* no checking anything if there's a var reference in there! */
  1744. opcop = ast_strdupa(is->u1.str);
  1745. for (q=opcop;*q;q++) { /* erase the innards of X(innard) type arguments, so we don't get confused later */
  1746. if ( *q == '(' ) {
  1747. p = q+1;
  1748. while (*p && *p != ')' )
  1749. *p++ = '+';
  1750. q = p+1;
  1751. }
  1752. }
  1753. for (ac=app->opts; ac; ac=ac->next) {
  1754. if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
  1755. return 0;
  1756. }
  1757. for (ac=app->opts; ac; ac=ac->next) {
  1758. if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
  1759. char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
  1760. if (p && *p == 'j') {
  1761. ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The j option in the %s application call is not appropriate for AEL!\n",
  1762. is->filename, is->startline, is->endline, app->name);
  1763. errs++;
  1764. }
  1765. if (p) {
  1766. *p = '+';
  1767. if (ac->name[1] == '(') {
  1768. if (*(p+1) != '(') {
  1769. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The %c option in the %s application call should have an (argument), but doesn't!\n",
  1770. is->filename, is->startline, is->endline, ac->name[0], app->name);
  1771. warns++;
  1772. }
  1773. }
  1774. }
  1775. }
  1776. }
  1777. for (q=opcop; *q; q++) {
  1778. if ( *q != '+' && *q != '(' && *q != ')') {
  1779. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: The %c option in the %s application call is not available as an option!\n",
  1780. is->filename, is->startline, is->endline, *q, app->name);
  1781. warns++;
  1782. }
  1783. }
  1784. return 1;
  1785. break;
  1786. default:
  1787. return 0;
  1788. }
  1789. }
  1790. int option_matches( struct argdesc *should, pval *is, struct argapp *app)
  1791. {
  1792. struct argchoice *ac;
  1793. char *opcop;
  1794. switch (should->dtype) {
  1795. case ARGD_STRING:
  1796. if (is_empty(is->u1.str) && should->type == ARGD_REQUIRED)
  1797. return 0;
  1798. if (is->u1.str && strlen(is->u1.str) > 0) /* most will match */
  1799. return 1;
  1800. break;
  1801. case ARGD_INT:
  1802. if (is_int(is->u1.str))
  1803. return 1;
  1804. else
  1805. return 0;
  1806. break;
  1807. case ARGD_FLOAT:
  1808. if (is_float(is->u1.str))
  1809. return 1;
  1810. else
  1811. return 0;
  1812. break;
  1813. case ARGD_ENUM:
  1814. if( !is->u1.str || strlen(is->u1.str) == 0 )
  1815. return 1; /* a null arg in the call will match an enum, I guess! */
  1816. for (ac=should->choices; ac; ac=ac->next) {
  1817. if (strcmp(ac->name,is->u1.str) == 0)
  1818. return 1;
  1819. }
  1820. return 0;
  1821. break;
  1822. case ARGD_OPTIONSET:
  1823. opcop = ast_strdupa(is->u1.str);
  1824. for (ac=app->opts; ac; ac=ac->next) {
  1825. if (strlen(ac->name)>1 && strchr(ac->name,'(') == 0 && strcmp(ac->name,is->u1.str) == 0) /* multichar option, no parens, and a match? */
  1826. return 1;
  1827. }
  1828. for (ac=app->opts; ac; ac=ac->next) {
  1829. if (strlen(ac->name)==1 || strchr(ac->name,'(')) {
  1830. char *p = strchr(opcop,ac->name[0]); /* wipe out all matched options in the user-supplied string */
  1831. if (p) {
  1832. *p = '+';
  1833. if (ac->name[1] == '(') {
  1834. if (*(p+1) == '(') {
  1835. char *q = p+1;
  1836. while (*q && *q != ')') {
  1837. *q++ = '+';
  1838. }
  1839. *q = '+';
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. return 1;
  1846. break;
  1847. case ARGD_VARARG:
  1848. return 1; /* matches anything */
  1849. break;
  1850. }
  1851. return 1; /* unless some for-sure match or non-match returns, then it must be close enough ... */
  1852. }
  1853. #endif
  1854. int check_app_args(pval* appcall, pval *arglist, struct argapp *app)
  1855. {
  1856. #ifdef AAL_ARGCHECK
  1857. struct argdesc *ad = app->args;
  1858. pval *pa;
  1859. int z;
  1860. for (pa = arglist; pa; pa=pa->next) {
  1861. if (!ad) {
  1862. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Extra argument %s not in application call to %s !\n",
  1863. arglist->filename, arglist->startline, arglist->endline, pa->u1.str, app->name);
  1864. warns++;
  1865. return 1;
  1866. } else {
  1867. /* find the first entry in the ad list that will match */
  1868. do {
  1869. if ( ad->dtype == ARGD_VARARG ) /* once we hit the VARARG, all bets are off. Discontinue the comparisons */
  1870. break;
  1871. z= option_matches( ad, pa, app);
  1872. if (!z) {
  1873. if ( !arglist )
  1874. arglist=appcall;
  1875. if (ad->type == ARGD_REQUIRED) {
  1876. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
  1877. arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
  1878. warns++;
  1879. return 1;
  1880. }
  1881. } else if (z && ad->dtype == ARGD_OPTIONSET) {
  1882. option_matches_j( ad, pa, app);
  1883. }
  1884. ad = ad->next;
  1885. } while (ad && !z);
  1886. }
  1887. }
  1888. /* any app nodes left, that are not optional? */
  1889. for ( ; ad; ad=ad->next) {
  1890. if (ad->type == ARGD_REQUIRED && ad->dtype != ARGD_VARARG) {
  1891. if ( !arglist )
  1892. arglist=appcall;
  1893. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: Required argument %s not in application call to %s !\n",
  1894. arglist->filename, arglist->startline, arglist->endline, ad->dtype==ARGD_OPTIONSET?"options":ad->name, app->name);
  1895. warns++;
  1896. return 1;
  1897. }
  1898. }
  1899. return 0;
  1900. #else
  1901. return 0;
  1902. #endif
  1903. }
  1904. void check_switch_expr(pval *item, struct argapp *apps)
  1905. {
  1906. #ifdef AAL_ARGCHECK
  1907. /* get and clean the variable name */
  1908. char *buff1, *p;
  1909. struct argapp *a,*a2;
  1910. struct appsetvar *v,*v2;
  1911. struct argchoice *c;
  1912. pval *t;
  1913. p = item->u1.str;
  1914. while (p && *p && (*p == ' ' || *p == '\t' || *p == '$' || *p == '{' ) )
  1915. p++;
  1916. buff1 = ast_strdupa(p);
  1917. while (strlen(buff1) > 0 && ( buff1[strlen(buff1)-1] == '}' || buff1[strlen(buff1)-1] == ' ' || buff1[strlen(buff1)-1] == '\t'))
  1918. buff1[strlen(buff1)-1] = 0;
  1919. /* buff1 now contains the variable name */
  1920. v = 0;
  1921. for (a=apps; a; a=a->next) {
  1922. for (v=a->setvars;v;v=v->next) {
  1923. if (strcmp(v->name,buff1) == 0) {
  1924. break;
  1925. }
  1926. }
  1927. if ( v )
  1928. break;
  1929. }
  1930. if (v && v->vals) {
  1931. /* we have a match, to a variable that has a set of determined values */
  1932. int def= 0;
  1933. int pat = 0;
  1934. int f1 = 0;
  1935. /* first of all, does this switch have a default case ? */
  1936. for (t=item->u2.statements; t; t=t->next) {
  1937. if (t->type == PV_DEFAULT) {
  1938. def =1;
  1939. break;
  1940. }
  1941. if (t->type == PV_PATTERN) {
  1942. pat++;
  1943. }
  1944. }
  1945. if (def || pat) /* nothing to check. All cases accounted for! */
  1946. return;
  1947. for (c=v->vals; c; c=c->next) {
  1948. f1 = 0;
  1949. for (t=item->u2.statements; t; t=t->next) {
  1950. if (t->type == PV_CASE || t->type == PV_PATTERN) {
  1951. if (!strcmp(t->u1.str,c->name)) {
  1952. f1 = 1;
  1953. break;
  1954. }
  1955. }
  1956. }
  1957. if (!f1) {
  1958. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: switch with expression(%s) does not handle the case of %s !\n",
  1959. item->filename, item->startline, item->endline, item->u1.str, c->name);
  1960. warns++;
  1961. }
  1962. }
  1963. /* next, is there an app call in the current exten, that would set this var? */
  1964. f1 = 0;
  1965. t = current_extension->u2.statements;
  1966. if ( t && t->type == PV_STATEMENTBLOCK )
  1967. t = t->u1.statements;
  1968. for (; t && t != item; t=t->next) {
  1969. if (t->type == PV_APPLICATION_CALL) {
  1970. /* find the application that matches the u1.str */
  1971. for (a2=apps; a2; a2=a2->next) {
  1972. if (strcasecmp(a2->name, t->u1.str)==0) {
  1973. for (v2=a2->setvars; v2; v2=v2->next) {
  1974. if (strcmp(v2->name, buff1) == 0) {
  1975. /* found an app that sets the var */
  1976. f1 = 1;
  1977. break;
  1978. }
  1979. }
  1980. }
  1981. if (f1)
  1982. break;
  1983. }
  1984. }
  1985. if (f1)
  1986. break;
  1987. }
  1988. /* see if it sets the var */
  1989. if (!f1) {
  1990. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: Couldn't find an application call in this extension that sets the expression (%s) value!\n",
  1991. item->filename, item->startline, item->endline, item->u1.str);
  1992. warns++;
  1993. }
  1994. }
  1995. #else
  1996. pval *t,*tl=0,*p2;
  1997. int def= 0;
  1998. /* first of all, does this switch have a default case ? */
  1999. for (t=item->u2.statements; t; t=t->next) {
  2000. if (t->type == PV_DEFAULT) {
  2001. def =1;
  2002. break;
  2003. }
  2004. tl = t;
  2005. }
  2006. if (def) /* nothing to check. All cases accounted for! */
  2007. return;
  2008. /* if no default, warn and insert a default case at the end */
  2009. p2 = tl->next = calloc(1, sizeof(struct pval));
  2010. p2->type = PV_DEFAULT;
  2011. p2->startline = tl->startline;
  2012. p2->endline = tl->endline;
  2013. p2->startcol = tl->startcol;
  2014. p2->endcol = tl->endcol;
  2015. p2->filename = strdup(tl->filename);
  2016. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: A default case was automatically added to the switch.\n",
  2017. p2->filename, p2->startline, p2->endline);
  2018. warns++;
  2019. #endif
  2020. }
  2021. static void check_context_names(void)
  2022. {
  2023. pval *i,*j;
  2024. for (i=current_db; i; i=i->next) {
  2025. if (i->type == PV_CONTEXT || i->type == PV_MACRO) {
  2026. for (j=i->next; j; j=j->next) {
  2027. if ( j->type == PV_CONTEXT || j->type == PV_MACRO ) {
  2028. if ( !strcmp(i->u1.str, j->u1.str) && !(i->u3.abstract&2) && !(j->u3.abstract&2) )
  2029. {
  2030. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: The context name (%s) is also declared in file %s, line %d-%d! (and neither is marked 'extend')\n",
  2031. i->filename, i->startline, i->endline, i->u1.str, j->filename, j->startline, j->endline);
  2032. warns++;
  2033. }
  2034. }
  2035. }
  2036. }
  2037. }
  2038. }
  2039. static void check_abstract_reference(pval *abstract_context)
  2040. {
  2041. pval *i,*j;
  2042. /* find some context includes that reference this context */
  2043. /* otherwise, print out a warning */
  2044. for (i=current_db; i; i=i->next) {
  2045. if (i->type == PV_CONTEXT) {
  2046. for (j=i->u2. statements; j; j=j->next) {
  2047. if ( j->type == PV_INCLUDES ) {
  2048. struct pval *p4;
  2049. for (p4=j->u1.list; p4; p4=p4->next) {
  2050. /* for each context pointed to, find it, then find a context/label that matches the
  2051. target here! */
  2052. if ( !strcmp(p4->u1.str, abstract_context->u1.str) )
  2053. return; /* found a match! */
  2054. }
  2055. }
  2056. }
  2057. }
  2058. }
  2059. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: Couldn't find a reference to this abstract context (%s) in any other context!\n",
  2060. abstract_context->filename, abstract_context->startline, abstract_context->endline, abstract_context->u1.str);
  2061. warns++;
  2062. }
  2063. void check_pval_item(pval *item, struct argapp *apps, int in_globals)
  2064. {
  2065. pval *lp;
  2066. #ifdef AAL_ARGCHECK
  2067. struct argapp *app, *found;
  2068. #endif
  2069. struct pval *macro_def;
  2070. struct pval *app_def;
  2071. char errmsg[4096];
  2072. char *strp;
  2073. switch (item->type) {
  2074. case PV_WORD:
  2075. /* fields: item->u1.str == string associated with this (word).
  2076. item->u2.arglist == pval list of 4 PV_WORD elements for time values (only in PV_INCLUDES) */
  2077. break;
  2078. case PV_MACRO:
  2079. /* fields: item->u1.str == name of macro
  2080. item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
  2081. item->u2.arglist->u1.str == argument
  2082. item->u2.arglist->next == next arg
  2083. item->u3.macro_statements == pval list of statements in macro body.
  2084. */
  2085. in_abstract_context = 0;
  2086. current_context = item;
  2087. current_extension = 0;
  2088. check_macro_returns(item);
  2089. for (lp=item->u2.arglist; lp; lp=lp->next) {
  2090. }
  2091. check_pval(item->u3.macro_statements, apps,in_globals);
  2092. break;
  2093. case PV_CONTEXT:
  2094. /* fields: item->u1.str == name of context
  2095. item->u2.statements == pval list of statements in context body
  2096. item->u3.abstract == int 1 if an abstract keyword were present
  2097. */
  2098. current_context = item;
  2099. current_extension = 0;
  2100. if ( item->u3.abstract ) {
  2101. in_abstract_context = 1;
  2102. check_abstract_reference(item);
  2103. } else
  2104. in_abstract_context = 0;
  2105. check_pval(item->u2.statements, apps,in_globals);
  2106. break;
  2107. case PV_MACRO_CALL:
  2108. /* fields: item->u1.str == name of macro to call
  2109. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  2110. item->u2.arglist->u1.str == argument
  2111. item->u2.arglist->next == next arg
  2112. */
  2113. #ifdef STANDALONE
  2114. /* if this is a standalone, we will need to make sure the
  2115. localized load of extensions.conf is done */
  2116. if (!extensions_dot_conf_loaded) {
  2117. localized_pbx_load_module();
  2118. extensions_dot_conf_loaded++;
  2119. }
  2120. #endif
  2121. macro_def = find_macro(item->u1.str);
  2122. if (!macro_def) {
  2123. #ifdef STANDALONE
  2124. struct pbx_find_info pfiq = {.stacklen = 0 };
  2125. struct pbx_find_info pfiq2 = {.stacklen = 0 };
  2126. /* look for the macro in the extensions.conf world */
  2127. pbx_find_extension(NULL, NULL, &pfiq, item->u1.str, "s", 1, NULL, NULL, E_MATCH);
  2128. if (pfiq.status != STATUS_SUCCESS) {
  2129. char namebuf2[256];
  2130. snprintf(namebuf2, 256, "macro-%s", item->u1.str);
  2131. /* look for the macro in the extensions.conf world */
  2132. pbx_find_extension(NULL, NULL, &pfiq2, namebuf2, "s", 1, NULL, NULL, E_MATCH);
  2133. if (pfiq2.status == STATUS_SUCCESS) {
  2134. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (macro-%s was found in the extensions.conf stuff, but we are using gosubs!)\n",
  2135. item->filename, item->startline, item->endline, item->u1.str, item->u1.str);
  2136. warns++;
  2137. } else {
  2138. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to non-existent %s! (Not even in the extensions.conf stuff!)\n",
  2139. item->filename, item->startline, item->endline, item->u1.str);
  2140. warns++;
  2141. }
  2142. }
  2143. #else
  2144. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s cannot be found in the AEL code!\n",
  2145. item->filename, item->startline, item->endline, item->u1.str);
  2146. warns++;
  2147. #endif
  2148. #ifdef THIS_IS_1DOT4
  2149. char namebuf2[256];
  2150. snprintf(namebuf2, 256, "macro-%s", item->u1.str);
  2151. /* look for the macro in the extensions.conf world */
  2152. pbx_find_extension(NULL, NULL, &pfiq, namebuf2, "s", 1, NULL, NULL, E_MATCH);
  2153. if (pfiq.status != STATUS_SUCCESS) {
  2154. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: macro call to %s was not found in the AEL, nor the extensions.conf !\n",
  2155. item->filename, item->startline, item->endline, item->u1.str);
  2156. warns++;
  2157. }
  2158. #endif
  2159. } else if (macro_def->type != PV_MACRO) {
  2160. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: macro call to %s references a context, not a macro!\n",
  2161. item->filename, item->startline, item->endline, item->u1.str);
  2162. errs++;
  2163. } else {
  2164. /* macro_def is a MACRO, so do the args match in number? */
  2165. int hereargs = 0;
  2166. int thereargs = 0;
  2167. for (lp=item->u2.arglist; lp; lp=lp->next) {
  2168. hereargs++;
  2169. }
  2170. for (lp=macro_def->u2.arglist; lp; lp=lp->next) {
  2171. thereargs++;
  2172. }
  2173. if (hereargs != thereargs ) {
  2174. ast_log(LOG_ERROR, "Error: file %s, line %d-%d: The macro call to %s has %d arguments, but the macro definition has %d arguments\n",
  2175. item->filename, item->startline, item->endline, item->u1.str, hereargs, thereargs);
  2176. errs++;
  2177. }
  2178. }
  2179. break;
  2180. case PV_APPLICATION_CALL:
  2181. /* fields: item->u1.str == name of application to call
  2182. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  2183. item->u2.arglist->u1.str == argument
  2184. item->u2.arglist->next == next arg
  2185. */
  2186. /* Need to check to see if the application is available! */
  2187. app_def = find_context(item->u1.str);
  2188. if (app_def && app_def->type == PV_MACRO) {
  2189. ast_log(LOG_ERROR,"Error: file %s, line %d-%d: application call to %s references an existing macro, but had no & preceding it!\n",
  2190. item->filename, item->startline, item->endline, item->u1.str);
  2191. errs++;
  2192. }
  2193. if (strcasecmp(item->u1.str,"GotoIf") == 0
  2194. || strcasecmp(item->u1.str,"GotoIfTime") == 0
  2195. || strcasecmp(item->u1.str,"while") == 0
  2196. || strcasecmp(item->u1.str,"endwhile") == 0
  2197. || strcasecmp(item->u1.str,"random") == 0
  2198. || strcasecmp(item->u1.str,"gosub") == 0
  2199. || strcasecmp(item->u1.str,"gosubif") == 0
  2200. || strcasecmp(item->u1.str,"continuewhile") == 0
  2201. || strcasecmp(item->u1.str,"endwhile") == 0
  2202. || strcasecmp(item->u1.str,"execif") == 0
  2203. || strcasecmp(item->u1.str,"execiftime") == 0
  2204. || strcasecmp(item->u1.str,"exitwhile") == 0
  2205. || strcasecmp(item->u1.str,"goto") == 0
  2206. || strcasecmp(item->u1.str,"macro") == 0
  2207. || strcasecmp(item->u1.str,"macroexclusive") == 0
  2208. || strcasecmp(item->u1.str,"macroif") == 0
  2209. || strcasecmp(item->u1.str,"stackpop") == 0
  2210. || strcasecmp(item->u1.str,"execIf") == 0 ) {
  2211. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s affects flow of control, and needs to be re-written using AEL if, while, goto, etc. keywords instead!\n",
  2212. item->filename, item->startline, item->endline, item->u1.str);
  2213. warns++;
  2214. }
  2215. if (strcasecmp(item->u1.str,"macroexit") == 0) {
  2216. ast_log(LOG_WARNING, "Warning: file %s, line %d-%d: I am converting the MacroExit call here to a return statement.\n",
  2217. item->filename, item->startline, item->endline);
  2218. item->type = PV_RETURN;
  2219. free(item->u1.str);
  2220. item->u1.str = 0;
  2221. }
  2222. #ifdef AAL_ARGCHECK
  2223. found = 0;
  2224. for (app=apps; app; app=app->next) {
  2225. if (strcasecmp(app->name, item->u1.str) == 0) {
  2226. found =app;
  2227. break;
  2228. }
  2229. }
  2230. if (!found) {
  2231. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: application call to %s not listed in applist database!\n",
  2232. item->filename, item->startline, item->endline, item->u1.str);
  2233. warns++;
  2234. } else
  2235. check_app_args(item, item->u2.arglist, app);
  2236. #endif
  2237. break;
  2238. case PV_CASE:
  2239. /* fields: item->u1.str == value of case
  2240. item->u2.statements == pval list of statements under the case
  2241. */
  2242. /* Make sure sequence of statements under case is terminated with goto, return, or break */
  2243. /* find the last statement */
  2244. check_pval(item->u2.statements, apps,in_globals);
  2245. break;
  2246. case PV_PATTERN:
  2247. /* fields: item->u1.str == value of case
  2248. item->u2.statements == pval list of statements under the case
  2249. */
  2250. /* Make sure sequence of statements under case is terminated with goto, return, or break */
  2251. /* find the last statement */
  2252. check_pval(item->u2.statements, apps,in_globals);
  2253. break;
  2254. case PV_DEFAULT:
  2255. /* fields:
  2256. item->u2.statements == pval list of statements under the case
  2257. */
  2258. check_pval(item->u2.statements, apps,in_globals);
  2259. break;
  2260. case PV_CATCH:
  2261. /* fields: item->u1.str == name of extension to catch
  2262. item->u2.statements == pval list of statements in context body
  2263. */
  2264. check_pval(item->u2.statements, apps,in_globals);
  2265. break;
  2266. case PV_SWITCHES:
  2267. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  2268. */
  2269. check_pval(item->u1.list, apps,in_globals);
  2270. break;
  2271. case PV_ESWITCHES:
  2272. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  2273. */
  2274. check_pval(item->u1.list, apps,in_globals);
  2275. break;
  2276. case PV_INCLUDES:
  2277. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  2278. */
  2279. check_pval(item->u1.list, apps,in_globals);
  2280. check_includes(item);
  2281. for (lp=item->u1.list; lp; lp=lp->next){
  2282. char *incl_context = lp->u1.str;
  2283. struct pval *that_context = find_context(incl_context);
  2284. if ( lp->u2.arglist ) {
  2285. check_timerange(lp->u2.arglist);
  2286. check_dow(lp->u2.arglist->next);
  2287. check_day(lp->u2.arglist->next->next);
  2288. check_month(lp->u2.arglist->next->next->next);
  2289. }
  2290. if (that_context) {
  2291. find_pval_gotos(that_context->u2.statements,0);
  2292. }
  2293. }
  2294. break;
  2295. case PV_STATEMENTBLOCK:
  2296. /* fields: item->u1.list == pval list of statements in block, one per entry in the list
  2297. */
  2298. check_pval(item->u1.list, apps,in_globals);
  2299. break;
  2300. case PV_VARDEC:
  2301. /* fields: item->u1.str == variable name
  2302. item->u2.val == variable value to assign
  2303. */
  2304. /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
  2305. if( !in_globals ) { /* don't check stuff inside the globals context; no wrapping in $[ ] there... */
  2306. snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u2.val);
  2307. ast_expr_register_extra_error_info(errmsg);
  2308. ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
  2309. ast_expr_clear_extra_error_info();
  2310. if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
  2311. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
  2312. item->filename, item->startline, item->endline, item->u2.val);
  2313. warns++;
  2314. }
  2315. check_expr2_input(item,item->u2.val);
  2316. }
  2317. break;
  2318. case PV_LOCALVARDEC:
  2319. /* fields: item->u1.str == variable name
  2320. item->u2.val == variable value to assign
  2321. */
  2322. /* the RHS of a vardec is encapsulated in a $[] expr. Is it legal? */
  2323. snprintf(errmsg,sizeof(errmsg), "file %s, line %d, columns %d-%d, variable declaration expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u2.val);
  2324. ast_expr_register_extra_error_info(errmsg);
  2325. ast_expr(item->u2.val, expr_output, sizeof(expr_output),NULL);
  2326. ast_expr_clear_extra_error_info();
  2327. if ( strpbrk(item->u2.val,"~!-+<>=*/&^") && !strstr(item->u2.val,"${") ) {
  2328. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
  2329. item->filename, item->startline, item->endline, item->u2.val);
  2330. warns++;
  2331. }
  2332. check_expr2_input(item,item->u2.val);
  2333. break;
  2334. case PV_GOTO:
  2335. /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
  2336. item->u1.list->u1.str == where the data on a PV_WORD will always be.
  2337. */
  2338. /* don't check goto's in abstract contexts */
  2339. if ( in_abstract_context )
  2340. break;
  2341. check_goto(item);
  2342. break;
  2343. case PV_LABEL:
  2344. /* fields: item->u1.str == label name
  2345. */
  2346. if ( strspn(item->u1.str, "0123456789") == strlen(item->u1.str) ) {
  2347. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: label '%s' is numeric, this is bad practice!\n",
  2348. item->filename, item->startline, item->endline, item->u1.str);
  2349. warns++;
  2350. }
  2351. check_label(item);
  2352. break;
  2353. case PV_FOR:
  2354. /* fields: item->u1.for_init == a string containing the initalizer
  2355. item->u2.for_test == a string containing the loop test
  2356. item->u3.for_inc == a string containing the loop increment
  2357. item->u4.for_statements == a pval list of statements in the for ()
  2358. */
  2359. snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, for test expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u2.for_test);
  2360. ast_expr_register_extra_error_info(errmsg);
  2361. strp = strchr(item->u1.for_init, '=');
  2362. if (strp) {
  2363. ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
  2364. }
  2365. ast_expr(item->u2.for_test, expr_output, sizeof(expr_output),NULL);
  2366. strp = strchr(item->u3.for_inc, '=');
  2367. if (strp) {
  2368. ast_expr(strp+1, expr_output, sizeof(expr_output),NULL);
  2369. }
  2370. if ( strpbrk(item->u2.for_test,"~!-+<>=*/&^") && !strstr(item->u2.for_test,"${") ) {
  2371. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
  2372. item->filename, item->startline, item->endline, item->u2.for_test);
  2373. warns++;
  2374. }
  2375. if ( strpbrk(item->u3.for_inc,"~!-+<>=*/&^") && !strstr(item->u3.for_inc,"${") ) {
  2376. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
  2377. item->filename, item->startline, item->endline, item->u3.for_inc);
  2378. warns++;
  2379. }
  2380. check_expr2_input(item,item->u2.for_test);
  2381. check_expr2_input(item,item->u3.for_inc);
  2382. ast_expr_clear_extra_error_info();
  2383. check_pval(item->u4.for_statements, apps,in_globals);
  2384. break;
  2385. case PV_WHILE:
  2386. /* fields: item->u1.str == the while conditional, as supplied by user
  2387. item->u2.statements == a pval list of statements in the while ()
  2388. */
  2389. snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, while expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u1.str);
  2390. ast_expr_register_extra_error_info(errmsg);
  2391. ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
  2392. ast_expr_clear_extra_error_info();
  2393. if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
  2394. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression %s has operators, but no variables. Interesting...\n",
  2395. item->filename, item->startline, item->endline, item->u1.str);
  2396. warns++;
  2397. }
  2398. check_expr2_input(item,item->u1.str);
  2399. check_pval(item->u2.statements, apps,in_globals);
  2400. break;
  2401. case PV_BREAK:
  2402. /* fields: none
  2403. */
  2404. check_break(item);
  2405. break;
  2406. case PV_RETURN:
  2407. /* fields: none
  2408. */
  2409. break;
  2410. case PV_CONTINUE:
  2411. /* fields: none
  2412. */
  2413. check_continue(item);
  2414. break;
  2415. case PV_RANDOM:
  2416. /* fields: item->u1.str == the random number expression, as supplied by user
  2417. item->u2.statements == a pval list of statements in the if ()
  2418. item->u3.else_statements == a pval list of statements in the else
  2419. (could be zero)
  2420. */
  2421. snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, random expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u1.str);
  2422. ast_expr_register_extra_error_info(errmsg);
  2423. ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
  2424. ast_expr_clear_extra_error_info();
  2425. if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
  2426. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: random expression '%s' has operators, but no variables. Interesting...\n",
  2427. item->filename, item->startline, item->endline, item->u1.str);
  2428. warns++;
  2429. }
  2430. check_expr2_input(item,item->u1.str);
  2431. check_pval(item->u2.statements, apps,in_globals);
  2432. if (item->u3.else_statements) {
  2433. check_pval(item->u3.else_statements, apps,in_globals);
  2434. }
  2435. break;
  2436. case PV_IFTIME:
  2437. /* fields: item->u1.list == the if time values, 4 of them, each in PV_WORD, linked list
  2438. item->u2.statements == a pval list of statements in the if ()
  2439. item->u3.else_statements == a pval list of statements in the else
  2440. (could be zero)
  2441. */
  2442. if ( item->u2.arglist ) {
  2443. check_timerange(item->u1.list);
  2444. check_dow(item->u1.list->next);
  2445. check_day(item->u1.list->next->next);
  2446. check_month(item->u1.list->next->next->next);
  2447. }
  2448. check_pval(item->u2.statements, apps,in_globals);
  2449. if (item->u3.else_statements) {
  2450. check_pval(item->u3.else_statements, apps,in_globals);
  2451. }
  2452. break;
  2453. case PV_IF:
  2454. /* fields: item->u1.str == the if conditional, as supplied by user
  2455. item->u2.statements == a pval list of statements in the if ()
  2456. item->u3.else_statements == a pval list of statements in the else
  2457. (could be zero)
  2458. */
  2459. snprintf(errmsg,sizeof(errmsg),"file %s, line %d, columns %d-%d, if expr '%s':", item->filename, item->startline, item->startcol, item->endcol, item->u1.str);
  2460. ast_expr_register_extra_error_info(errmsg);
  2461. ast_expr(item->u1.str, expr_output, sizeof(expr_output),NULL);
  2462. ast_expr_clear_extra_error_info();
  2463. if ( strpbrk(item->u1.str,"~!-+<>=*/&^") && !strstr(item->u1.str,"${") ) {
  2464. ast_log(LOG_WARNING,"Warning: file %s, line %d-%d: expression '%s' has operators, but no variables. Interesting...\n",
  2465. item->filename, item->startline, item->endline, item->u1.str);
  2466. warns++;
  2467. }
  2468. check_expr2_input(item,item->u1.str);
  2469. check_pval(item->u2.statements, apps,in_globals);
  2470. if (item->u3.else_statements) {
  2471. check_pval(item->u3.else_statements, apps,in_globals);
  2472. }
  2473. break;
  2474. case PV_SWITCH:
  2475. /* fields: item->u1.str == the switch expression
  2476. item->u2.statements == a pval list of statements in the switch,
  2477. (will be case statements, most likely!)
  2478. */
  2479. /* we can check the switch expression, see if it matches any of the app variables...
  2480. if it does, then, are all the possible cases accounted for? */
  2481. check_switch_expr(item, apps);
  2482. check_pval(item->u2.statements, apps,in_globals);
  2483. break;
  2484. case PV_EXTENSION:
  2485. /* fields: item->u1.str == the extension name, label, whatever it's called
  2486. item->u2.statements == a pval list of statements in the extension
  2487. item->u3.hints == a char * hint argument
  2488. item->u4.regexten == an int boolean. non-zero says that regexten was specified
  2489. */
  2490. current_extension = item ;
  2491. check_pval(item->u2.statements, apps,in_globals);
  2492. break;
  2493. case PV_IGNOREPAT:
  2494. /* fields: item->u1.str == the ignorepat data
  2495. */
  2496. break;
  2497. case PV_GLOBALS:
  2498. /* fields: item->u1.statements == pval list of statements, usually vardecs
  2499. */
  2500. in_abstract_context = 0;
  2501. check_pval(item->u1.statements, apps, 1);
  2502. break;
  2503. default:
  2504. break;
  2505. }
  2506. }
  2507. void check_pval(pval *item, struct argapp *apps, int in_globals)
  2508. {
  2509. pval *i;
  2510. /* checks to do:
  2511. 1. Do goto's point to actual labels?
  2512. 2. Do macro calls reference a macro?
  2513. 3. Does the number of macro args match the definition?
  2514. 4. Is a macro call missing its & at the front?
  2515. 5. Application calls-- we could check syntax for existing applications,
  2516. but I need some some sort of universal description bnf for a general
  2517. sort of method for checking arguments, in number, maybe even type, at least.
  2518. Don't want to hand code checks for hundreds of applications.
  2519. */
  2520. for (i=item; i; i=i->next) {
  2521. check_pval_item(i,apps,in_globals);
  2522. }
  2523. }
  2524. void ael2_semantic_check(pval *item, int *arg_errs, int *arg_warns, int *arg_notes)
  2525. {
  2526. #ifdef AAL_ARGCHECK
  2527. int argapp_errs =0;
  2528. char *rfilename;
  2529. #endif
  2530. struct argapp *apps=0;
  2531. if (!item)
  2532. return; /* don't check an empty tree */
  2533. #ifdef AAL_ARGCHECK
  2534. rfilename = ast_alloca(10 + strlen(ast_config_AST_VAR_DIR));
  2535. sprintf(rfilename, "%s/applist", ast_config_AST_VAR_DIR);
  2536. apps = argdesc_parse(rfilename, &argapp_errs); /* giveth */
  2537. #endif
  2538. current_db = item;
  2539. errs = warns = notes = 0;
  2540. check_context_names();
  2541. check_pval(item, apps, 0);
  2542. #ifdef AAL_ARGCHECK
  2543. argdesc_destroy(apps); /* taketh away */
  2544. #endif
  2545. current_db = 0;
  2546. *arg_errs = errs;
  2547. *arg_warns = warns;
  2548. *arg_notes = notes;
  2549. }
  2550. /* =============================================================================================== */
  2551. /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
  2552. /* =============================================================================================== */
  2553. static int control_statement_count = 0;
  2554. struct ael_priority *new_prio(void)
  2555. {
  2556. struct ael_priority *x = (struct ael_priority *)calloc(sizeof(struct ael_priority),1);
  2557. return x;
  2558. }
  2559. struct ael_extension *new_exten(void)
  2560. {
  2561. struct ael_extension *x = (struct ael_extension *)calloc(sizeof(struct ael_extension),1);
  2562. return x;
  2563. }
  2564. void linkprio(struct ael_extension *exten, struct ael_priority *prio, struct ael_extension *mother_exten)
  2565. {
  2566. char *p1, *p2;
  2567. if (!exten->plist) {
  2568. exten->plist = prio;
  2569. exten->plist_last = prio;
  2570. } else {
  2571. exten->plist_last->next = prio;
  2572. exten->plist_last = prio;
  2573. }
  2574. if( !prio->exten )
  2575. prio->exten = exten; /* don't override the switch value */
  2576. /* The following code will cause all priorities within an extension
  2577. to have ${EXTEN} or ${EXTEN: replaced with ~~EXTEN~~, which is
  2578. set just before the first switch in an exten. The switches
  2579. will muck up the original ${EXTEN} value, so we save it away
  2580. and the user accesses this copy instead. */
  2581. if (prio->appargs && ((mother_exten && mother_exten->has_switch) || exten->has_switch) ) {
  2582. while ((p1 = strstr(prio->appargs, "${EXTEN}"))) {
  2583. p2 = malloc(strlen(prio->appargs)+5);
  2584. *p1 = 0;
  2585. strcpy(p2, prio->appargs);
  2586. strcat(p2, "${~~EXTEN~~}");
  2587. if (*(p1+8))
  2588. strcat(p2, p1+8);
  2589. free(prio->appargs);
  2590. prio->appargs = p2;
  2591. }
  2592. while ((p1 = strstr(prio->appargs, "${EXTEN:"))) {
  2593. p2 = malloc(strlen(prio->appargs)+5);
  2594. *p1 = 0;
  2595. strcpy(p2, prio->appargs);
  2596. strcat(p2, "${~~EXTEN~~:");
  2597. if (*(p1+8))
  2598. strcat(p2, p1+8);
  2599. free(prio->appargs);
  2600. prio->appargs = p2;
  2601. }
  2602. }
  2603. }
  2604. void destroy_extensions(struct ael_extension *exten)
  2605. {
  2606. struct ael_extension *ne, *nen;
  2607. for (ne=exten; ne; ne=nen) {
  2608. struct ael_priority *pe, *pen;
  2609. if (ne->name)
  2610. free(ne->name);
  2611. /* cidmatch fields are allocated with name, and freed when
  2612. the name field is freed. Don't do a free for this field,
  2613. unless you LIKE to see a crash! */
  2614. if (ne->hints)
  2615. free(ne->hints);
  2616. for (pe=ne->plist; pe; pe=pen) {
  2617. pen = pe->next;
  2618. if (pe->app)
  2619. free(pe->app);
  2620. pe->app = 0;
  2621. if (pe->appargs)
  2622. free(pe->appargs);
  2623. pe->appargs = 0;
  2624. pe->origin = 0;
  2625. pe->goto_true = 0;
  2626. pe->goto_false = 0;
  2627. free(pe);
  2628. }
  2629. nen = ne->next_exten;
  2630. ne->next_exten = 0;
  2631. ne->plist =0;
  2632. ne->plist_last = 0;
  2633. ne->next_exten = 0;
  2634. ne->loop_break = 0;
  2635. ne->loop_continue = 0;
  2636. free(ne);
  2637. }
  2638. }
  2639. static int label_inside_case(pval *label)
  2640. {
  2641. pval *p = label;
  2642. while( p && p->type != PV_MACRO && p->type != PV_CONTEXT ) /* early cutout, sort of */ {
  2643. if( p->type == PV_CASE || p->type == PV_DEFAULT || p->type == PV_PATTERN ) {
  2644. return 1;
  2645. }
  2646. p = p->dad;
  2647. }
  2648. return 0;
  2649. }
  2650. static void linkexten(struct ael_extension *exten, struct ael_extension *add)
  2651. {
  2652. add->next_exten = exten->next_exten; /* this will reverse the order. Big deal. */
  2653. exten->next_exten = add;
  2654. }
  2655. static void remove_spaces_before_equals(char *str)
  2656. {
  2657. char *p;
  2658. while( str && *str && *str != '=' )
  2659. {
  2660. if( *str == ' ' || *str == '\n' || *str == '\r' || *str == '\t' )
  2661. {
  2662. p = str;
  2663. while( *p )
  2664. {
  2665. *p = *(p+1);
  2666. p++;
  2667. }
  2668. }
  2669. else
  2670. str++;
  2671. }
  2672. }
  2673. /* =============================================================================================== */
  2674. /* "CODE" GENERATOR -- Convert the AEL representation to asterisk extension language */
  2675. /* =============================================================================================== */
  2676. static void gen_match_to_pattern(char *pattern, char *result)
  2677. {
  2678. /* the result will be a string that will be matched by pattern */
  2679. char *p=pattern, *t=result;
  2680. while (*p) {
  2681. if (*p == 'x' || *p == 'n' || *p == 'z' || *p == 'X' || *p == 'N' || *p == 'Z')
  2682. *t++ = '9';
  2683. else if (*p == '[') {
  2684. char *z = p+1;
  2685. while (*z != ']')
  2686. z++;
  2687. if (*(z+1)== ']')
  2688. z++;
  2689. *t++=*(p+1); /* use the first char in the set */
  2690. p = z;
  2691. } else {
  2692. *t++ = *p;
  2693. }
  2694. p++;
  2695. }
  2696. *t++ = 0; /* cap it off */
  2697. }
  2698. /* ==== a set of routines to search for a switch statement contained in the pval description */
  2699. int find_switch_item(pval *item);
  2700. int contains_switch(pval *item);
  2701. int find_switch_item(pval *item)
  2702. {
  2703. switch ( item->type ) {
  2704. case PV_LOCALVARDEC:
  2705. /* fields: item->u1.str == string associated with this (word). */
  2706. break;
  2707. case PV_WORD:
  2708. /* fields: item->u1.str == string associated with this (word). */
  2709. break;
  2710. case PV_MACRO:
  2711. /* fields: item->u1.str == name of macro
  2712. item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
  2713. item->u2.arglist->u1.str == argument
  2714. item->u2.arglist->next == next arg
  2715. item->u3.macro_statements == pval list of statements in macro body.
  2716. */
  2717. /* had better not see this */
  2718. if (contains_switch(item->u3.macro_statements))
  2719. return 1;
  2720. break;
  2721. case PV_CONTEXT:
  2722. /* fields: item->u1.str == name of context
  2723. item->u2.statements == pval list of statements in context body
  2724. item->u3.abstract == int 1 if an abstract keyword were present
  2725. */
  2726. /* had better not see this */
  2727. if (contains_switch(item->u2.statements))
  2728. return 1;
  2729. break;
  2730. case PV_MACRO_CALL:
  2731. /* fields: item->u1.str == name of macro to call
  2732. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  2733. item->u2.arglist->u1.str == argument
  2734. item->u2.arglist->next == next arg
  2735. */
  2736. break;
  2737. case PV_APPLICATION_CALL:
  2738. /* fields: item->u1.str == name of application to call
  2739. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  2740. item->u2.arglist->u1.str == argument
  2741. item->u2.arglist->next == next arg
  2742. */
  2743. break;
  2744. case PV_CASE:
  2745. /* fields: item->u1.str == value of case
  2746. item->u2.statements == pval list of statements under the case
  2747. */
  2748. /* had better not see this */
  2749. if (contains_switch(item->u2.statements))
  2750. return 1;
  2751. break;
  2752. case PV_PATTERN:
  2753. /* fields: item->u1.str == value of case
  2754. item->u2.statements == pval list of statements under the case
  2755. */
  2756. /* had better not see this */
  2757. if (contains_switch(item->u2.statements))
  2758. return 1;
  2759. break;
  2760. case PV_DEFAULT:
  2761. /* fields:
  2762. item->u2.statements == pval list of statements under the case
  2763. */
  2764. /* had better not see this */
  2765. if (contains_switch(item->u2.statements))
  2766. return 1;
  2767. break;
  2768. case PV_CATCH:
  2769. /* fields: item->u1.str == name of extension to catch
  2770. item->u2.statements == pval list of statements in context body
  2771. */
  2772. /* had better not see this */
  2773. if (contains_switch(item->u2.statements))
  2774. return 1;
  2775. break;
  2776. case PV_SWITCHES:
  2777. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  2778. */
  2779. break;
  2780. case PV_ESWITCHES:
  2781. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  2782. */
  2783. break;
  2784. case PV_INCLUDES:
  2785. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  2786. item->u2.arglist == pval list of 4 PV_WORD elements for time values
  2787. */
  2788. break;
  2789. case PV_STATEMENTBLOCK:
  2790. /* fields: item->u1.list == pval list of statements in block, one per entry in the list
  2791. */
  2792. if (contains_switch(item->u1.list) )
  2793. return 1;
  2794. break;
  2795. case PV_VARDEC:
  2796. /* fields: item->u1.str == variable name
  2797. item->u2.val == variable value to assign
  2798. */
  2799. break;
  2800. case PV_GOTO:
  2801. /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
  2802. item->u1.list->u1.str == where the data on a PV_WORD will always be.
  2803. */
  2804. break;
  2805. case PV_LABEL:
  2806. /* fields: item->u1.str == label name
  2807. */
  2808. break;
  2809. case PV_FOR:
  2810. /* fields: item->u1.for_init == a string containing the initalizer
  2811. item->u2.for_test == a string containing the loop test
  2812. item->u3.for_inc == a string containing the loop increment
  2813. item->u4.for_statements == a pval list of statements in the for ()
  2814. */
  2815. if (contains_switch(item->u4.for_statements))
  2816. return 1;
  2817. break;
  2818. case PV_WHILE:
  2819. /* fields: item->u1.str == the while conditional, as supplied by user
  2820. item->u2.statements == a pval list of statements in the while ()
  2821. */
  2822. if (contains_switch(item->u2.statements))
  2823. return 1;
  2824. break;
  2825. case PV_BREAK:
  2826. /* fields: none
  2827. */
  2828. break;
  2829. case PV_RETURN:
  2830. /* fields: none
  2831. */
  2832. break;
  2833. case PV_CONTINUE:
  2834. /* fields: none
  2835. */
  2836. break;
  2837. case PV_IFTIME:
  2838. /* fields: item->u1.list == there are 4 linked PV_WORDs here.
  2839. item->u2.statements == a pval list of statements in the if ()
  2840. item->u3.else_statements == a pval list of statements in the else
  2841. (could be zero)
  2842. */
  2843. if (contains_switch(item->u2.statements))
  2844. return 1;
  2845. if ( item->u3.else_statements ) {
  2846. if (contains_switch(item->u3.else_statements))
  2847. return 1;
  2848. }
  2849. break;
  2850. case PV_RANDOM:
  2851. /* fields: item->u1.str == the random number expression, as supplied by user
  2852. item->u2.statements == a pval list of statements in the if ()
  2853. item->u3.else_statements == a pval list of statements in the else
  2854. (could be zero)
  2855. */
  2856. if (contains_switch(item->u2.statements))
  2857. return 1;
  2858. if ( item->u3.else_statements ) {
  2859. if (contains_switch(item->u3.else_statements))
  2860. return 1;
  2861. }
  2862. break;
  2863. case PV_IF:
  2864. /* fields: item->u1.str == the if conditional, as supplied by user
  2865. item->u2.statements == a pval list of statements in the if ()
  2866. item->u3.else_statements == a pval list of statements in the else
  2867. (could be zero)
  2868. */
  2869. if (contains_switch(item->u2.statements))
  2870. return 1;
  2871. if ( item->u3.else_statements ) {
  2872. if (contains_switch(item->u3.else_statements))
  2873. return 1;
  2874. }
  2875. break;
  2876. case PV_SWITCH:
  2877. /* fields: item->u1.str == the switch expression
  2878. item->u2.statements == a pval list of statements in the switch,
  2879. (will be case statements, most likely!)
  2880. */
  2881. return 1; /* JACKPOT */
  2882. break;
  2883. case PV_EXTENSION:
  2884. /* fields: item->u1.str == the extension name, label, whatever it's called
  2885. item->u2.statements == a pval list of statements in the extension
  2886. item->u3.hints == a char * hint argument
  2887. item->u4.regexten == an int boolean. non-zero says that regexten was specified
  2888. */
  2889. if (contains_switch(item->u2.statements))
  2890. return 1;
  2891. break;
  2892. case PV_IGNOREPAT:
  2893. /* fields: item->u1.str == the ignorepat data
  2894. */
  2895. break;
  2896. case PV_GLOBALS:
  2897. /* fields: item->u1.statements == pval list of statements, usually vardecs
  2898. */
  2899. break;
  2900. }
  2901. return 0;
  2902. }
  2903. int contains_switch(pval *item)
  2904. {
  2905. pval *i;
  2906. for (i=item; i; i=i->next) {
  2907. if (find_switch_item(i))
  2908. return 1;
  2909. }
  2910. return 0;
  2911. }
  2912. static int gen_prios(struct ael_extension *exten, char *label, pval *statement, struct ael_extension *mother_exten, struct ast_context *this_context )
  2913. {
  2914. pval *p,*p2,*p3;
  2915. struct ael_priority *pr;
  2916. struct ael_priority *for_init, *for_test, *for_inc, *for_loop, *for_end;
  2917. struct ael_priority *while_test, *while_loop, *while_end;
  2918. struct ael_priority *switch_set, *switch_test, *switch_end, *fall_thru, *switch_empty;
  2919. struct ael_priority *if_test, *if_end, *if_skip, *if_false;
  2920. #ifdef OLD_RAND_ACTION
  2921. struct ael_priority *rand_test, *rand_end, *rand_skip;
  2922. #endif
  2923. char *buf1;
  2924. char *buf2;
  2925. char *new_label;
  2926. char *strp, *strp2;
  2927. int default_exists;
  2928. int local_control_statement_count;
  2929. int first;
  2930. struct ael_priority *loop_break_save;
  2931. struct ael_priority *loop_continue_save;
  2932. struct ael_extension *switch_case,*switch_null;
  2933. if (!(buf1 = malloc(BUF_SIZE))) {
  2934. return -1;
  2935. }
  2936. if (!(buf2 = malloc(BUF_SIZE))) {
  2937. return -1;
  2938. }
  2939. if (!(new_label = malloc(BUF_SIZE))) {
  2940. return -1;
  2941. }
  2942. if ((mother_exten && !mother_exten->checked_switch) || (exten && !exten->checked_switch)) {
  2943. if (contains_switch(statement)) { /* only run contains_switch if you haven't checked before */
  2944. if (mother_exten) {
  2945. if (!mother_exten->has_switch) {
  2946. for (first = 1; first >= 0; first--) {
  2947. switch_set = new_prio();
  2948. switch_set->type = AEL_APPCALL;
  2949. switch_set->app = strdup("MSet");
  2950. /* Are we likely inside a gosub subroutine? */
  2951. if (!strcmp(mother_exten->name, "~~s~~") && first) {
  2952. /* If we're not actually within a gosub, this will fail, but the
  2953. * second time through, it will get set. If we are within gosub,
  2954. * the second time through is redundant, but acceptable. */
  2955. switch_set->appargs = strdup("LOCAL(~~EXTEN~~)=${EXTEN}");
  2956. } else {
  2957. switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
  2958. first = 0;
  2959. }
  2960. linkprio(exten, switch_set, mother_exten);
  2961. mother_exten->has_switch = 1;
  2962. mother_exten->checked_switch = 1;
  2963. if (exten) {
  2964. exten->has_switch = 1;
  2965. exten->checked_switch = 1;
  2966. }
  2967. }
  2968. }
  2969. } else if (exten) {
  2970. if (!exten->has_switch) {
  2971. for (first = 1; first >= 0; first--) {
  2972. switch_set = new_prio();
  2973. switch_set->type = AEL_APPCALL;
  2974. switch_set->app = strdup("MSet");
  2975. /* Are we likely inside a gosub subroutine? */
  2976. if (!strcmp(exten->name, "~~s~~")) {
  2977. /* If we're not actually within a gosub, this will fail, but the
  2978. * second time through, it will get set. If we are within gosub,
  2979. * the second time through is redundant, but acceptable. */
  2980. switch_set->appargs = strdup("LOCAL(~~EXTEN~~)=${EXTEN}");
  2981. } else {
  2982. switch_set->appargs = strdup("~~EXTEN~~=${EXTEN}");
  2983. first = 0;
  2984. }
  2985. linkprio(exten, switch_set, mother_exten);
  2986. exten->has_switch = 1;
  2987. exten->checked_switch = 1;
  2988. if (mother_exten) {
  2989. mother_exten->has_switch = 1;
  2990. mother_exten->checked_switch = 1;
  2991. }
  2992. }
  2993. }
  2994. }
  2995. } else {
  2996. if (mother_exten) {
  2997. mother_exten->checked_switch = 1;
  2998. }
  2999. if (exten) {
  3000. exten->checked_switch = 1;
  3001. }
  3002. }
  3003. }
  3004. for (p=statement; p; p=p->next) {
  3005. switch (p->type) {
  3006. case PV_VARDEC:
  3007. pr = new_prio();
  3008. pr->type = AEL_APPCALL;
  3009. snprintf(buf1, BUF_SIZE, "%s=$[%s]", p->u1.str, p->u2.val);
  3010. pr->app = strdup("MSet");
  3011. remove_spaces_before_equals(buf1);
  3012. pr->appargs = strdup(buf1);
  3013. pr->origin = p;
  3014. linkprio(exten, pr, mother_exten);
  3015. break;
  3016. case PV_LOCALVARDEC:
  3017. pr = new_prio();
  3018. pr->type = AEL_APPCALL;
  3019. snprintf(buf1, BUF_SIZE, "LOCAL(%s)=$[%s]", p->u1.str, p->u2.val);
  3020. pr->app = strdup("MSet");
  3021. remove_spaces_before_equals(buf1);
  3022. pr->appargs = strdup(buf1);
  3023. pr->origin = p;
  3024. linkprio(exten, pr, mother_exten);
  3025. break;
  3026. case PV_GOTO:
  3027. pr = new_prio();
  3028. pr->type = AEL_APPCALL;
  3029. p->u2.goto_target = get_goto_target(p);
  3030. if( p->u2.goto_target ) {
  3031. p->u3.goto_target_in_case = label_inside_case(p->u2.goto_target);
  3032. }
  3033. if (!p->u1.list->next) /* just one */ {
  3034. pr->app = strdup("Goto");
  3035. if (!mother_exten)
  3036. pr->appargs = strdup(p->u1.list->u1.str);
  3037. else { /* for the case of simple within-extension gotos in case/pattern/default statement blocks: */
  3038. snprintf(buf1, BUF_SIZE, "%s,%s", mother_exten->name, p->u1.list->u1.str);
  3039. pr->appargs = strdup(buf1);
  3040. }
  3041. } else if (p->u1.list->next && !p->u1.list->next->next) /* two */ {
  3042. snprintf(buf1, BUF_SIZE, "%s,%s", p->u1.list->u1.str, p->u1.list->next->u1.str);
  3043. pr->app = strdup("Goto");
  3044. pr->appargs = strdup(buf1);
  3045. } else if (p->u1.list->next && p->u1.list->next->next) {
  3046. snprintf(buf1, BUF_SIZE, "%s,%s,%s", p->u1.list->u1.str,
  3047. p->u1.list->next->u1.str,
  3048. p->u1.list->next->next->u1.str);
  3049. pr->app = strdup("Goto");
  3050. pr->appargs = strdup(buf1);
  3051. }
  3052. pr->origin = p;
  3053. linkprio(exten, pr, mother_exten);
  3054. break;
  3055. case PV_LABEL:
  3056. pr = new_prio();
  3057. pr->type = AEL_LABEL;
  3058. pr->origin = p;
  3059. p->u3.compiled_label = exten;
  3060. linkprio(exten, pr, mother_exten);
  3061. break;
  3062. case PV_FOR:
  3063. control_statement_count++;
  3064. loop_break_save = exten->loop_break; /* save them, then restore before leaving */
  3065. loop_continue_save = exten->loop_continue;
  3066. snprintf(new_label, BUF_SIZE, "for_%s_%d", label, control_statement_count);
  3067. for_init = new_prio();
  3068. for_inc = new_prio();
  3069. for_test = new_prio();
  3070. for_loop = new_prio();
  3071. for_end = new_prio();
  3072. for_init->type = AEL_APPCALL;
  3073. for_inc->type = AEL_APPCALL;
  3074. for_test->type = AEL_FOR_CONTROL;
  3075. for_test->goto_false = for_end;
  3076. for_loop->type = AEL_CONTROL1; /* simple goto */
  3077. for_end->type = AEL_APPCALL;
  3078. for_init->app = strdup("MSet");
  3079. strcpy(buf2,p->u1.for_init);
  3080. remove_spaces_before_equals(buf2);
  3081. strp = strchr(buf2, '=');
  3082. if (strp) {
  3083. strp2 = strchr(p->u1.for_init, '=');
  3084. *(strp+1) = 0;
  3085. strcat(buf2,"$[");
  3086. strncat(buf2,strp2+1, BUF_SIZE-strlen(strp2+1)-2);
  3087. strcat(buf2,"]");
  3088. for_init->appargs = strdup(buf2);
  3089. } else {
  3090. strp2 = p->u1.for_init;
  3091. while (*strp2 && isspace(*strp2))
  3092. strp2++;
  3093. if (*strp2 == '&') { /* itsa macro call */
  3094. char *strp3 = strp2+1;
  3095. while (*strp3 && isspace(*strp3))
  3096. strp3++;
  3097. strcpy(buf2, strp3);
  3098. strp3 = strchr(buf2,'(');
  3099. if (strp3) {
  3100. *strp3 = '|';
  3101. }
  3102. while ((strp3=strchr(buf2,','))) {
  3103. *strp3 = '|';
  3104. }
  3105. strp3 = strrchr(buf2, ')');
  3106. if (strp3)
  3107. *strp3 = 0; /* remove the closing paren */
  3108. for_init->appargs = strdup(buf2);
  3109. free(for_init->app);
  3110. for_init->app = strdup("Macro");
  3111. } else { /* must be a regular app call */
  3112. char *strp3;
  3113. strcpy(buf2, strp2);
  3114. strp3 = strchr(buf2,'(');
  3115. if (strp3) {
  3116. *strp3 = 0;
  3117. free(for_init->app);
  3118. for_init->app = strdup(buf2);
  3119. for_init->appargs = strdup(strp3+1);
  3120. strp3 = strrchr(for_init->appargs, ')');
  3121. if (strp3)
  3122. *strp3 = 0; /* remove the closing paren */
  3123. }
  3124. }
  3125. }
  3126. strcpy(buf2,p->u3.for_inc);
  3127. remove_spaces_before_equals(buf2);
  3128. strp = strchr(buf2, '=');
  3129. if (strp) { /* there's an = in this part; that means an assignment. set it up */
  3130. strp2 = strchr(p->u3.for_inc, '=');
  3131. *(strp+1) = 0;
  3132. strcat(buf2,"$[");
  3133. strncat(buf2,strp2+1, BUF_SIZE-strlen(strp2+1)-2);
  3134. strcat(buf2,"]");
  3135. for_inc->appargs = strdup(buf2);
  3136. for_inc->app = strdup("MSet");
  3137. } else {
  3138. strp2 = p->u3.for_inc;
  3139. while (*strp2 && isspace(*strp2))
  3140. strp2++;
  3141. if (*strp2 == '&') { /* itsa macro call */
  3142. char *strp3 = strp2+1;
  3143. while (*strp3 && isspace(*strp3))
  3144. strp3++;
  3145. strcpy(buf2, strp3);
  3146. strp3 = strchr(buf2,'(');
  3147. if (strp3) {
  3148. *strp3 = ',';
  3149. }
  3150. strp3 = strrchr(buf2, ')');
  3151. if (strp3)
  3152. *strp3 = 0; /* remove the closing paren */
  3153. for_inc->appargs = strdup(buf2);
  3154. for_inc->app = strdup("Macro");
  3155. } else { /* must be a regular app call */
  3156. char *strp3;
  3157. strcpy(buf2, strp2);
  3158. strp3 = strchr(buf2,'(');
  3159. if (strp3) {
  3160. *strp3 = 0;
  3161. for_inc->app = strdup(buf2);
  3162. for_inc->appargs = strdup(strp3+1);
  3163. strp3 = strrchr(for_inc->appargs, ')');
  3164. if (strp3)
  3165. *strp3 = 0; /* remove the closing paren */
  3166. }
  3167. }
  3168. }
  3169. snprintf(buf1, BUF_SIZE, "$[%s]",p->u2.for_test);
  3170. for_test->app = 0;
  3171. for_test->appargs = strdup(buf1);
  3172. for_loop->goto_true = for_test;
  3173. snprintf(buf1, BUF_SIZE, "Finish for_%s_%d", label, control_statement_count);
  3174. for_end->app = strdup("NoOp");
  3175. for_end->appargs = strdup(buf1);
  3176. /* link & load! */
  3177. linkprio(exten, for_init, mother_exten);
  3178. linkprio(exten, for_test, mother_exten);
  3179. /* now, put the body of the for loop here */
  3180. exten->loop_break = for_end;
  3181. exten->loop_continue = for_inc;
  3182. if (gen_prios(exten, new_label, p->u4.for_statements, mother_exten, this_context)) { /* this will link in all the statements here */
  3183. return -1;
  3184. }
  3185. linkprio(exten, for_inc, mother_exten);
  3186. linkprio(exten, for_loop, mother_exten);
  3187. linkprio(exten, for_end, mother_exten);
  3188. exten->loop_break = loop_break_save;
  3189. exten->loop_continue = loop_continue_save;
  3190. for_loop->origin = p;
  3191. break;
  3192. case PV_WHILE:
  3193. control_statement_count++;
  3194. loop_break_save = exten->loop_break; /* save them, then restore before leaving */
  3195. loop_continue_save = exten->loop_continue;
  3196. snprintf(new_label, BUF_SIZE, "while_%s_%d", label, control_statement_count);
  3197. while_test = new_prio();
  3198. while_loop = new_prio();
  3199. while_end = new_prio();
  3200. while_test->type = AEL_FOR_CONTROL;
  3201. while_test->goto_false = while_end;
  3202. while_loop->type = AEL_CONTROL1; /* simple goto */
  3203. while_end->type = AEL_APPCALL;
  3204. snprintf(buf1, BUF_SIZE, "$[%s]",p->u1.str);
  3205. while_test->app = 0;
  3206. while_test->appargs = strdup(buf1);
  3207. while_loop->goto_true = while_test;
  3208. snprintf(buf1, BUF_SIZE, "Finish while_%s_%d", label, control_statement_count);
  3209. while_end->app = strdup("NoOp");
  3210. while_end->appargs = strdup(buf1);
  3211. linkprio(exten, while_test, mother_exten);
  3212. /* now, put the body of the for loop here */
  3213. exten->loop_break = while_end;
  3214. exten->loop_continue = while_test;
  3215. if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the while body statements here */
  3216. return -1;
  3217. }
  3218. linkprio(exten, while_loop, mother_exten);
  3219. linkprio(exten, while_end, mother_exten);
  3220. exten->loop_break = loop_break_save;
  3221. exten->loop_continue = loop_continue_save;
  3222. while_loop->origin = p;
  3223. break;
  3224. case PV_SWITCH:
  3225. control_statement_count++;
  3226. local_control_statement_count = control_statement_count;
  3227. loop_break_save = exten->loop_break; /* save them, then restore before leaving */
  3228. loop_continue_save = exten->loop_continue;
  3229. snprintf(new_label, BUF_SIZE, "sw_%s_%d", label, control_statement_count);
  3230. switch_test = new_prio();
  3231. switch_end = new_prio();
  3232. switch_test->type = AEL_APPCALL;
  3233. switch_end->type = AEL_APPCALL;
  3234. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", control_statement_count, p->u1.str);
  3235. switch_test->app = strdup("Goto");
  3236. switch_test->appargs = strdup(buf1);
  3237. snprintf(buf1, BUF_SIZE, "Finish switch_%s_%d", label, control_statement_count);
  3238. switch_end->app = strdup("NoOp");
  3239. switch_end->appargs = strdup(buf1);
  3240. switch_end->origin = p;
  3241. switch_end->exten = exten;
  3242. linkprio(exten, switch_test, mother_exten);
  3243. linkprio(exten, switch_end, mother_exten);
  3244. exten->loop_break = switch_end;
  3245. exten->loop_continue = 0;
  3246. default_exists = 0;
  3247. for (p2=p->u2.statements; p2; p2=p2->next) {
  3248. /* now, for each case/default put the body of the for loop here */
  3249. if (p2->type == PV_CASE) {
  3250. /* ok, generate a extension and link it in */
  3251. switch_case = new_exten();
  3252. if (mother_exten && mother_exten->checked_switch) {
  3253. switch_case->has_switch = mother_exten->has_switch;
  3254. switch_case->checked_switch = mother_exten->checked_switch;
  3255. }
  3256. if (exten && exten->checked_switch) {
  3257. switch_case->has_switch = exten->has_switch;
  3258. switch_case->checked_switch = exten->checked_switch;
  3259. }
  3260. switch_case->context = this_context;
  3261. switch_case->is_switch = 1;
  3262. /* the break/continue locations are inherited from parent */
  3263. switch_case->loop_break = exten->loop_break;
  3264. switch_case->loop_continue = exten->loop_continue;
  3265. linkexten(exten,switch_case);
  3266. snprintf(buf1, BUF_SIZE, "sw_%d_%s", local_control_statement_count, p2->u1.str);
  3267. switch_case->name = strdup(buf1);
  3268. snprintf(new_label, BUF_SIZE, "sw_%s_%s_%d", label, p2->u1.str, local_control_statement_count);
  3269. if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the case body statements here */
  3270. return -1;
  3271. }
  3272. /* here is where we write code to "fall thru" to the next case... if there is one... */
  3273. for (p3=p2->u2.statements; p3; p3=p3->next) {
  3274. if (!p3->next)
  3275. break;
  3276. }
  3277. /* p3 now points the last statement... */
  3278. if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN) ) {
  3279. /* is there a following CASE/PATTERN/DEFAULT? */
  3280. if (p2->next && p2->next->type == PV_CASE) {
  3281. fall_thru = new_prio();
  3282. fall_thru->type = AEL_APPCALL;
  3283. fall_thru->app = strdup("Goto");
  3284. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
  3285. fall_thru->appargs = strdup(buf1);
  3286. linkprio(switch_case, fall_thru, mother_exten);
  3287. } else if (p2->next && p2->next->type == PV_PATTERN) {
  3288. fall_thru = new_prio();
  3289. fall_thru->type = AEL_APPCALL;
  3290. fall_thru->app = strdup("Goto");
  3291. gen_match_to_pattern(p2->next->u1.str, buf2);
  3292. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
  3293. fall_thru->appargs = strdup(buf1);
  3294. linkprio(switch_case, fall_thru, mother_exten);
  3295. } else if (p2->next && p2->next->type == PV_DEFAULT) {
  3296. fall_thru = new_prio();
  3297. fall_thru->type = AEL_APPCALL;
  3298. fall_thru->app = strdup("Goto");
  3299. snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
  3300. fall_thru->appargs = strdup(buf1);
  3301. linkprio(switch_case, fall_thru, mother_exten);
  3302. } else if (!p2->next) {
  3303. fall_thru = new_prio();
  3304. fall_thru->type = AEL_CONTROL1;
  3305. fall_thru->goto_true = switch_end;
  3306. fall_thru->app = strdup("Goto");
  3307. linkprio(switch_case, fall_thru, mother_exten);
  3308. }
  3309. }
  3310. if (switch_case->return_needed) { /* returns don't generate a goto eoe (end of extension) any more, just a Return() app call) */
  3311. char buf[2000];
  3312. struct ael_priority *np2 = new_prio();
  3313. np2->type = AEL_APPCALL;
  3314. np2->app = strdup("NoOp");
  3315. snprintf(buf, BUF_SIZE, "End of Extension %s", switch_case->name);
  3316. np2->appargs = strdup(buf);
  3317. linkprio(switch_case, np2, mother_exten);
  3318. switch_case-> return_target = np2;
  3319. }
  3320. } else if (p2->type == PV_PATTERN) {
  3321. /* ok, generate a extension and link it in */
  3322. switch_case = new_exten();
  3323. if (mother_exten && mother_exten->checked_switch) {
  3324. switch_case->has_switch = mother_exten->has_switch;
  3325. switch_case->checked_switch = mother_exten->checked_switch;
  3326. }
  3327. if (exten && exten->checked_switch) {
  3328. switch_case->has_switch = exten->has_switch;
  3329. switch_case->checked_switch = exten->checked_switch;
  3330. }
  3331. switch_case->context = this_context;
  3332. switch_case->is_switch = 1;
  3333. /* the break/continue locations are inherited from parent */
  3334. switch_case->loop_break = exten->loop_break;
  3335. switch_case->loop_continue = exten->loop_continue;
  3336. linkexten(exten,switch_case);
  3337. snprintf(buf1, BUF_SIZE, "_sw_%d_%s", local_control_statement_count, p2->u1.str);
  3338. switch_case->name = strdup(buf1);
  3339. snprintf(new_label, BUF_SIZE, "sw_%s_%s_%d", label, p2->u1.str, local_control_statement_count);
  3340. if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the while body statements here */
  3341. return -1;
  3342. }
  3343. /* here is where we write code to "fall thru" to the next case... if there is one... */
  3344. for (p3=p2->u2.statements; p3; p3=p3->next) {
  3345. if (!p3->next)
  3346. break;
  3347. }
  3348. /* p3 now points the last statement... */
  3349. if (!p3 || ( p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
  3350. /* is there a following CASE/PATTERN/DEFAULT? */
  3351. if (p2->next && p2->next->type == PV_CASE) {
  3352. fall_thru = new_prio();
  3353. fall_thru->type = AEL_APPCALL;
  3354. fall_thru->app = strdup("Goto");
  3355. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
  3356. fall_thru->appargs = strdup(buf1);
  3357. linkprio(switch_case, fall_thru, mother_exten);
  3358. } else if (p2->next && p2->next->type == PV_PATTERN) {
  3359. fall_thru = new_prio();
  3360. fall_thru->type = AEL_APPCALL;
  3361. fall_thru->app = strdup("Goto");
  3362. gen_match_to_pattern(p2->next->u1.str, buf2);
  3363. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
  3364. fall_thru->appargs = strdup(buf1);
  3365. linkprio(switch_case, fall_thru, mother_exten);
  3366. } else if (p2->next && p2->next->type == PV_DEFAULT) {
  3367. fall_thru = new_prio();
  3368. fall_thru->type = AEL_APPCALL;
  3369. fall_thru->app = strdup("Goto");
  3370. snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
  3371. fall_thru->appargs = strdup(buf1);
  3372. linkprio(switch_case, fall_thru, mother_exten);
  3373. } else if (!p2->next) {
  3374. fall_thru = new_prio();
  3375. fall_thru->type = AEL_CONTROL1;
  3376. fall_thru->goto_true = switch_end;
  3377. fall_thru->app = strdup("Goto");
  3378. linkprio(switch_case, fall_thru, mother_exten);
  3379. }
  3380. }
  3381. if (switch_case->return_needed) { /* returns don't generate a goto eoe (end of extension) any more, just a Return() app call) */
  3382. char buf[2000];
  3383. struct ael_priority *np2 = new_prio();
  3384. np2->type = AEL_APPCALL;
  3385. np2->app = strdup("NoOp");
  3386. snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
  3387. np2->appargs = strdup(buf);
  3388. linkprio(switch_case, np2, mother_exten);
  3389. switch_case-> return_target = np2;
  3390. }
  3391. } else if (p2->type == PV_DEFAULT) {
  3392. /* ok, generate a extension and link it in */
  3393. switch_case = new_exten();
  3394. if (mother_exten && mother_exten->checked_switch) {
  3395. switch_case->has_switch = mother_exten->has_switch;
  3396. switch_case->checked_switch = mother_exten->checked_switch;
  3397. }
  3398. if (exten && exten->checked_switch) {
  3399. switch_case->has_switch = exten->has_switch;
  3400. switch_case->checked_switch = exten->checked_switch;
  3401. }
  3402. switch_case->context = this_context;
  3403. switch_case->is_switch = 1;
  3404. /* new: the default case intros a pattern with ., which covers ALMOST everything.
  3405. but it doesn't cover a NULL pattern. So, we'll define a null extension to match
  3406. that goto's the default extension. */
  3407. default_exists++;
  3408. switch_null = new_exten();
  3409. if (mother_exten && mother_exten->checked_switch) {
  3410. switch_null->has_switch = mother_exten->has_switch;
  3411. switch_null->checked_switch = mother_exten->checked_switch;
  3412. }
  3413. if (exten && exten->checked_switch) {
  3414. switch_null->has_switch = exten->has_switch;
  3415. switch_null->checked_switch = exten->checked_switch;
  3416. }
  3417. switch_null->context = this_context;
  3418. switch_null->is_switch = 1;
  3419. switch_empty = new_prio();
  3420. snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
  3421. switch_empty->app = strdup("Goto");
  3422. switch_empty->appargs = strdup(buf1);
  3423. linkprio(switch_null, switch_empty, mother_exten);
  3424. snprintf(buf1, BUF_SIZE, "sw_%d_", local_control_statement_count);
  3425. switch_null->name = strdup(buf1);
  3426. switch_null->loop_break = exten->loop_break;
  3427. switch_null->loop_continue = exten->loop_continue;
  3428. linkexten(exten,switch_null);
  3429. /* the break/continue locations are inherited from parent */
  3430. switch_case->loop_break = exten->loop_break;
  3431. switch_case->loop_continue = exten->loop_continue;
  3432. linkexten(exten,switch_case);
  3433. snprintf(buf1, BUF_SIZE, "_sw_%d_.", local_control_statement_count);
  3434. switch_case->name = strdup(buf1);
  3435. snprintf(new_label, BUF_SIZE, "sw_%s_default_%d", label, local_control_statement_count);
  3436. if (gen_prios(switch_case, new_label, p2->u2.statements, exten, this_context)) { /* this will link in all the default: body statements here */
  3437. return -1;
  3438. }
  3439. /* here is where we write code to "fall thru" to the next case... if there is one... */
  3440. for (p3=p2->u2.statements; p3; p3=p3->next) {
  3441. if (!p3->next)
  3442. break;
  3443. }
  3444. /* p3 now points the last statement... */
  3445. if (!p3 || (p3->type != PV_GOTO && p3->type != PV_BREAK && p3->type != PV_RETURN)) {
  3446. /* is there a following CASE/PATTERN/DEFAULT? */
  3447. if (p2->next && p2->next->type == PV_CASE) {
  3448. fall_thru = new_prio();
  3449. fall_thru->type = AEL_APPCALL;
  3450. fall_thru->app = strdup("Goto");
  3451. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, p2->next->u1.str);
  3452. fall_thru->appargs = strdup(buf1);
  3453. linkprio(switch_case, fall_thru, mother_exten);
  3454. } else if (p2->next && p2->next->type == PV_PATTERN) {
  3455. fall_thru = new_prio();
  3456. fall_thru->type = AEL_APPCALL;
  3457. fall_thru->app = strdup("Goto");
  3458. gen_match_to_pattern(p2->next->u1.str, buf2);
  3459. snprintf(buf1, BUF_SIZE, "sw_%d_%s,10", local_control_statement_count, buf2);
  3460. fall_thru->appargs = strdup(buf1);
  3461. linkprio(switch_case, fall_thru, mother_exten);
  3462. } else if (p2->next && p2->next->type == PV_DEFAULT) {
  3463. fall_thru = new_prio();
  3464. fall_thru->type = AEL_APPCALL;
  3465. fall_thru->app = strdup("Goto");
  3466. snprintf(buf1, BUF_SIZE, "sw_%d_.,10", local_control_statement_count);
  3467. fall_thru->appargs = strdup(buf1);
  3468. linkprio(switch_case, fall_thru, mother_exten);
  3469. } else if (!p2->next) {
  3470. fall_thru = new_prio();
  3471. fall_thru->type = AEL_CONTROL1;
  3472. fall_thru->goto_true = switch_end;
  3473. fall_thru->app = strdup("Goto");
  3474. linkprio(switch_case, fall_thru, mother_exten);
  3475. }
  3476. }
  3477. if (switch_case->return_needed) { /* returns don't generate a goto eoe (end of extension) any more, just a Return() app call) */
  3478. char buf[2000];
  3479. struct ael_priority *np2 = new_prio();
  3480. np2->type = AEL_APPCALL;
  3481. np2->app = strdup("NoOp");
  3482. snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
  3483. np2->appargs = strdup(buf);
  3484. linkprio(switch_case, np2, mother_exten);
  3485. switch_case-> return_target = np2;
  3486. }
  3487. } else {
  3488. /* what could it be??? */
  3489. }
  3490. }
  3491. exten->loop_break = loop_break_save;
  3492. exten->loop_continue = loop_continue_save;
  3493. switch_test->origin = p;
  3494. switch_end->origin = p;
  3495. break;
  3496. case PV_MACRO_CALL:
  3497. pr = new_prio();
  3498. pr->type = AEL_APPCALL;
  3499. snprintf(buf1, BUF_SIZE, "%s,~~s~~,1", p->u1.str);
  3500. first = 1;
  3501. for (p2 = p->u2.arglist; p2; p2 = p2->next) {
  3502. if (first)
  3503. {
  3504. strcat(buf1,"(");
  3505. first = 0;
  3506. }
  3507. else
  3508. strcat(buf1,",");
  3509. strcat(buf1,p2->u1.str);
  3510. }
  3511. if (!first)
  3512. strcat(buf1,")");
  3513. pr->app = strdup("Gosub");
  3514. pr->appargs = strdup(buf1);
  3515. pr->origin = p;
  3516. linkprio(exten, pr, mother_exten);
  3517. break;
  3518. case PV_APPLICATION_CALL:
  3519. pr = new_prio();
  3520. pr->type = AEL_APPCALL;
  3521. buf1[0] = 0;
  3522. for (p2 = p->u2.arglist; p2; p2 = p2->next) {
  3523. if (p2 != p->u2.arglist )
  3524. strcat(buf1,",");
  3525. strcat(buf1,p2->u1.str);
  3526. }
  3527. pr->app = strdup(p->u1.str);
  3528. pr->appargs = strdup(buf1);
  3529. pr->origin = p;
  3530. linkprio(exten, pr, mother_exten);
  3531. break;
  3532. case PV_BREAK:
  3533. pr = new_prio();
  3534. pr->type = AEL_CONTROL1; /* simple goto */
  3535. pr->goto_true = exten->loop_break;
  3536. pr->origin = p;
  3537. linkprio(exten, pr, mother_exten);
  3538. break;
  3539. case PV_RETURN: /* hmmmm */
  3540. pr = new_prio();
  3541. pr->type = AEL_RETURN; /* simple Return */
  3542. /* exten->return_needed++; */
  3543. pr->app = strdup("Return");
  3544. pr->appargs = strdup("");
  3545. pr->origin = p;
  3546. linkprio(exten, pr, mother_exten);
  3547. break;
  3548. case PV_CONTINUE:
  3549. pr = new_prio();
  3550. pr->type = AEL_CONTROL1; /* simple goto */
  3551. pr->goto_true = exten->loop_continue;
  3552. pr->origin = p;
  3553. linkprio(exten, pr, mother_exten);
  3554. break;
  3555. case PV_IFTIME:
  3556. control_statement_count++;
  3557. snprintf(new_label, BUF_SIZE, "iftime_%s_%d", label, control_statement_count);
  3558. if_test = new_prio();
  3559. if_test->type = AEL_IFTIME_CONTROL;
  3560. snprintf(buf1, BUF_SIZE, "%s,%s,%s,%s",
  3561. p->u1.list->u1.str,
  3562. p->u1.list->next->u1.str,
  3563. p->u1.list->next->next->u1.str,
  3564. p->u1.list->next->next->next->u1.str);
  3565. if_test->app = 0;
  3566. if_test->appargs = strdup(buf1);
  3567. if_test->origin = p;
  3568. if_end = new_prio();
  3569. if_end->type = AEL_APPCALL;
  3570. snprintf(buf1, BUF_SIZE, "Finish iftime_%s_%d", label, control_statement_count);
  3571. if_end->app = strdup("NoOp");
  3572. if_end->appargs = strdup(buf1);
  3573. if (p->u3.else_statements) {
  3574. if_skip = new_prio();
  3575. if_skip->type = AEL_CONTROL1; /* simple goto */
  3576. if_skip->goto_true = if_end;
  3577. if_skip->origin = p;
  3578. } else {
  3579. if_skip = 0;
  3580. if_test->goto_false = if_end;
  3581. }
  3582. if_false = new_prio();
  3583. if_false->type = AEL_CONTROL1;
  3584. if (p->u3.else_statements) {
  3585. if_false->goto_true = if_skip; /* +1 */
  3586. } else {
  3587. if_false->goto_true = if_end;
  3588. }
  3589. /* link & load! */
  3590. linkprio(exten, if_test, mother_exten);
  3591. linkprio(exten, if_false, mother_exten);
  3592. /* now, put the body of the if here */
  3593. if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the statements here */
  3594. return -1;
  3595. }
  3596. if (p->u3.else_statements) {
  3597. linkprio(exten, if_skip, mother_exten);
  3598. if (gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context)) { /* this will link in all the statements here */
  3599. return -1;
  3600. }
  3601. }
  3602. linkprio(exten, if_end, mother_exten);
  3603. break;
  3604. case PV_RANDOM:
  3605. case PV_IF:
  3606. control_statement_count++;
  3607. snprintf(new_label, BUF_SIZE, "if_%s_%d", label, control_statement_count);
  3608. if_test = new_prio();
  3609. if_end = new_prio();
  3610. if_test->type = AEL_IF_CONTROL;
  3611. if_end->type = AEL_APPCALL;
  3612. if ( p->type == PV_RANDOM )
  3613. snprintf(buf1, BUF_SIZE, "$[${RAND(0,99)} < (%s)]", p->u1.str);
  3614. else
  3615. snprintf(buf1, BUF_SIZE, "$[%s]", p->u1.str);
  3616. if_test->app = 0;
  3617. if_test->appargs = strdup(buf1);
  3618. snprintf(buf1, BUF_SIZE, "Finish if_%s_%d", label, control_statement_count);
  3619. if_end->app = strdup("NoOp");
  3620. if_end->appargs = strdup(buf1);
  3621. if_test->origin = p;
  3622. if (p->u3.else_statements) {
  3623. if_skip = new_prio();
  3624. if_skip->type = AEL_CONTROL1; /* simple goto */
  3625. if_skip->goto_true = if_end;
  3626. if_test->goto_false = if_skip;;
  3627. } else {
  3628. if_skip = 0;
  3629. if_test->goto_false = if_end;;
  3630. }
  3631. /* link & load! */
  3632. linkprio(exten, if_test, mother_exten);
  3633. /* now, put the body of the if here */
  3634. if (gen_prios(exten, new_label, p->u2.statements, mother_exten, this_context)) { /* this will link in all the statements here */
  3635. return -1;
  3636. }
  3637. if (p->u3.else_statements) {
  3638. linkprio(exten, if_skip, mother_exten);
  3639. if (gen_prios(exten, new_label, p->u3.else_statements, mother_exten, this_context)) { /* this will link in all the statements here */
  3640. return -1;
  3641. }
  3642. }
  3643. linkprio(exten, if_end, mother_exten);
  3644. break;
  3645. case PV_STATEMENTBLOCK:
  3646. if (gen_prios(exten, label, p->u1.list, mother_exten, this_context)) { /* recurse into the block */
  3647. return -1;
  3648. }
  3649. break;
  3650. case PV_CATCH:
  3651. control_statement_count++;
  3652. /* generate an extension with name of catch, put all catch stats
  3653. into this exten! */
  3654. switch_case = new_exten();
  3655. if (mother_exten && mother_exten->checked_switch) {
  3656. switch_case->has_switch = mother_exten->has_switch;
  3657. switch_case->checked_switch = mother_exten->checked_switch;
  3658. }
  3659. if (exten && exten->checked_switch) {
  3660. switch_case->has_switch = exten->has_switch;
  3661. switch_case->checked_switch = exten->checked_switch;
  3662. }
  3663. switch_case->context = this_context;
  3664. linkexten(exten,switch_case);
  3665. switch_case->name = strdup(p->u1.str);
  3666. snprintf(new_label, BUF_SIZE, "catch_%s_%d",p->u1.str, control_statement_count);
  3667. if (gen_prios(switch_case, new_label, p->u2.statements, mother_exten,this_context)) { /* this will link in all the catch body statements here */
  3668. return -1;
  3669. }
  3670. if (switch_case->return_needed) { /* returns now generate a Return() app call, no longer a goto to the end of the exten */
  3671. char buf[2000];
  3672. struct ael_priority *np2 = new_prio();
  3673. np2->type = AEL_APPCALL;
  3674. np2->app = strdup("NoOp");
  3675. snprintf(buf,sizeof(buf),"End of Extension %s", switch_case->name);
  3676. np2->appargs = strdup(buf);
  3677. linkprio(switch_case, np2, mother_exten);
  3678. switch_case-> return_target = np2;
  3679. }
  3680. break;
  3681. default:
  3682. break;
  3683. }
  3684. }
  3685. free(buf1);
  3686. free(buf2);
  3687. free(new_label);
  3688. return 0;
  3689. }
  3690. void set_priorities(struct ael_extension *exten)
  3691. {
  3692. int i;
  3693. struct ael_priority *pr;
  3694. do {
  3695. if (exten->is_switch)
  3696. i = 10;
  3697. else if (exten->regexten)
  3698. i=2;
  3699. else
  3700. i=1;
  3701. for (pr=exten->plist; pr; pr=pr->next) {
  3702. pr->priority_num = i;
  3703. if (!pr->origin || (pr->origin && pr->origin->type != PV_LABEL) ) /* Labels don't show up in the dialplan,
  3704. but we want them to point to the right
  3705. priority, which would be the next line
  3706. after the label; */
  3707. i++;
  3708. }
  3709. exten = exten->next_exten;
  3710. } while ( exten );
  3711. }
  3712. void add_extensions(struct ael_extension *exten)
  3713. {
  3714. struct ael_priority *pr;
  3715. char *label=0;
  3716. char realext[AST_MAX_EXTENSION];
  3717. if (!exten) {
  3718. ast_log(LOG_WARNING, "This file is Empty!\n" );
  3719. return;
  3720. }
  3721. do {
  3722. struct ael_priority *last = 0;
  3723. pbx_substitute_variables_helper(NULL, exten->name, realext, sizeof(realext) - 1);
  3724. if (exten->hints) {
  3725. if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, PRIORITY_HINT, NULL, exten->cidmatch,
  3726. exten->hints, NULL, ast_free_ptr, registrar)) {
  3727. ast_log(LOG_WARNING, "Unable to add step at priority 'hint' of extension '%s'\n",
  3728. exten->name);
  3729. }
  3730. }
  3731. for (pr=exten->plist; pr; pr=pr->next) {
  3732. char app[2000];
  3733. char appargs[2000];
  3734. /* before we can add the extension, we need to prep the app/appargs;
  3735. the CONTROL types need to be done after the priority numbers are calculated.
  3736. */
  3737. if (pr->type == AEL_LABEL) /* don't try to put labels in the dialplan! */ {
  3738. last = pr;
  3739. continue;
  3740. }
  3741. if (pr->app)
  3742. strcpy(app, pr->app);
  3743. else
  3744. app[0] = 0;
  3745. if (pr->appargs )
  3746. strcpy(appargs, pr->appargs);
  3747. else
  3748. appargs[0] = 0;
  3749. switch( pr->type ) {
  3750. case AEL_APPCALL:
  3751. /* easy case. Everything is all set up */
  3752. break;
  3753. case AEL_CONTROL1: /* FOR loop, WHILE loop, BREAK, CONTINUE, IF, IFTIME */
  3754. /* simple, unconditional goto. */
  3755. strcpy(app,"Goto");
  3756. if (pr->goto_true->origin && pr->goto_true->origin->type == PV_SWITCH ) {
  3757. snprintf(appargs,sizeof(appargs),"%s,%d", pr->goto_true->exten->name, pr->goto_true->priority_num);
  3758. } else if (pr->goto_true->origin && pr->goto_true->origin->type == PV_IFTIME && pr->goto_true->origin->u3.else_statements ) {
  3759. snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num+1);
  3760. } else
  3761. snprintf(appargs,sizeof(appargs),"%d", pr->goto_true->priority_num);
  3762. break;
  3763. case AEL_FOR_CONTROL: /* WHILE loop test, FOR loop test */
  3764. strcpy(app,"GotoIf");
  3765. snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
  3766. break;
  3767. case AEL_IF_CONTROL:
  3768. strcpy(app,"GotoIf");
  3769. if (pr->origin->u3.else_statements )
  3770. snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num+1);
  3771. else
  3772. snprintf(appargs,sizeof(appargs),"%s?%d:%d", pr->appargs, pr->priority_num+1, pr->goto_false->priority_num);
  3773. break;
  3774. case AEL_RAND_CONTROL:
  3775. strcpy(app,"Random");
  3776. snprintf(appargs,sizeof(appargs),"%s:%d", pr->appargs, pr->goto_true->priority_num+1);
  3777. break;
  3778. case AEL_IFTIME_CONTROL:
  3779. strcpy(app,"GotoIfTime");
  3780. snprintf(appargs,sizeof(appargs),"%s?%d", pr->appargs, pr->priority_num+2);
  3781. break;
  3782. case AEL_RETURN:
  3783. strcpy(app,"Return");
  3784. appargs[0] = 0;
  3785. break;
  3786. default:
  3787. break;
  3788. }
  3789. if (last && last->type == AEL_LABEL ) {
  3790. label = last->origin->u1.str;
  3791. }
  3792. else
  3793. label = 0;
  3794. if (ast_add_extension2(exten->context, 0 /*no replace*/, realext, pr->priority_num, (label?label:NULL), exten->cidmatch,
  3795. app, strdup(appargs), ast_free_ptr, registrar)) {
  3796. ast_log(LOG_WARNING, "Unable to add step at priority '%d' of extension '%s'\n", pr->priority_num,
  3797. exten->name);
  3798. }
  3799. last = pr;
  3800. }
  3801. exten = exten->next_exten;
  3802. } while ( exten );
  3803. }
  3804. static void attach_exten(struct ael_extension **list, struct ael_extension *newmem)
  3805. {
  3806. /* travel to the end of the list... */
  3807. struct ael_extension *lptr;
  3808. if( !*list ) {
  3809. *list = newmem;
  3810. return;
  3811. }
  3812. lptr = *list;
  3813. while( lptr->next_exten ) {
  3814. lptr = lptr->next_exten;
  3815. }
  3816. /* lptr should now pointing to the last element in the list; it has a null next_exten pointer */
  3817. lptr->next_exten = newmem;
  3818. }
  3819. static pval *get_extension_or_contxt(pval *p)
  3820. {
  3821. while( p && p->type != PV_EXTENSION && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
  3822. p = p->dad;
  3823. }
  3824. return p;
  3825. }
  3826. static pval *get_contxt(pval *p)
  3827. {
  3828. while( p && p->type != PV_CONTEXT && p->type != PV_MACRO ) {
  3829. p = p->dad;
  3830. }
  3831. return p;
  3832. }
  3833. static void fix_gotos_in_extensions(struct ael_extension *exten)
  3834. {
  3835. struct ael_extension *e;
  3836. for(e=exten;e;e=e->next_exten) {
  3837. struct ael_priority *p;
  3838. for(p=e->plist;p;p=p->next) {
  3839. if( p->origin && p->origin->type == PV_GOTO && p->origin->u3.goto_target_in_case ) {
  3840. /* fix the extension of the goto target to the actual extension in the post-compiled dialplan */
  3841. pval *target = p->origin->u2.goto_target;
  3842. struct ael_extension *z = target->u3.compiled_label;
  3843. pval *pv2 = p->origin;
  3844. char buf1[500];
  3845. char *apparg_save = p->appargs;
  3846. p->appargs = 0;
  3847. if (!pv2->u1.list->next) /* just one -- it won't hurt to repeat the extension */ {
  3848. snprintf(buf1,sizeof(buf1),"%s,%s", z->name, pv2->u1.list->u1.str);
  3849. p->appargs = strdup(buf1);
  3850. } else if (pv2->u1.list->next && !pv2->u1.list->next->next) /* two */ {
  3851. snprintf(buf1,sizeof(buf1),"%s,%s", z->name, pv2->u1.list->next->u1.str);
  3852. p->appargs = strdup(buf1);
  3853. } else if (pv2->u1.list->next && pv2->u1.list->next->next) {
  3854. snprintf(buf1,sizeof(buf1),"%s,%s,%s", pv2->u1.list->u1.str,
  3855. z->name,
  3856. pv2->u1.list->next->next->u1.str);
  3857. p->appargs = strdup(buf1);
  3858. }
  3859. else
  3860. printf("WHAT? The goto doesn't fall into one of three cases for GOTO????\n");
  3861. if( apparg_save ) {
  3862. free(apparg_save);
  3863. }
  3864. }
  3865. }
  3866. }
  3867. }
  3868. static int context_used(struct ael_extension *exten_list, struct ast_context *context)
  3869. {
  3870. struct ael_extension *exten;
  3871. /* Check the simple elements first */
  3872. if (ast_walk_context_extensions(context, NULL) || ast_walk_context_includes(context, NULL) || ast_walk_context_ignorepats(context, NULL) || ast_walk_context_switches(context, NULL)) {
  3873. return 1;
  3874. }
  3875. for (exten = exten_list; exten; exten = exten->next_exten) {
  3876. if (exten->context == context) {
  3877. return 1;
  3878. }
  3879. }
  3880. return 0;
  3881. }
  3882. int ast_compile_ael2(struct ast_context **local_contexts, struct ast_hashtab *local_table, struct pval *root)
  3883. {
  3884. pval *p,*p2;
  3885. struct ast_context *context;
  3886. char buf[2000];
  3887. struct ael_extension *exten;
  3888. struct ael_extension *exten_list = 0;
  3889. for (p=root; p; p=p->next ) { /* do the globals first, so they'll be there
  3890. when we try to eval them */
  3891. switch (p->type) {
  3892. case PV_GLOBALS:
  3893. /* just VARDEC elements */
  3894. for (p2=p->u1.list; p2; p2=p2->next) {
  3895. char buf2[2000];
  3896. snprintf(buf2,sizeof(buf2),"%s=%s", p2->u1.str, p2->u2.val);
  3897. pbx_builtin_setvar(NULL, buf2);
  3898. }
  3899. break;
  3900. default:
  3901. break;
  3902. }
  3903. }
  3904. for (p=root; p; p=p->next ) {
  3905. pval *lp;
  3906. int argc;
  3907. switch (p->type) {
  3908. case PV_MACRO:
  3909. context = ast_context_find_or_create(local_contexts, local_table, p->u1.str, registrar);
  3910. exten = new_exten();
  3911. exten->context = context;
  3912. exten->name = strdup("~~s~~");
  3913. argc = 1;
  3914. for (lp=p->u2.arglist; lp; lp=lp->next) {
  3915. /* for each arg, set up a "Set" command */
  3916. struct ael_priority *np2 = new_prio();
  3917. np2->type = AEL_APPCALL;
  3918. np2->app = strdup("MSet");
  3919. snprintf(buf,sizeof(buf),"LOCAL(%s)=${ARG%d}", lp->u1.str, argc++);
  3920. remove_spaces_before_equals(buf);
  3921. np2->appargs = strdup(buf);
  3922. linkprio(exten, np2, NULL);
  3923. }
  3924. /* CONTAINS APPCALLS, CATCH, just like extensions... */
  3925. if (gen_prios(exten, p->u1.str, p->u3.macro_statements, 0, context)) {
  3926. return -1;
  3927. }
  3928. if (exten->return_needed) { /* most likely, this will go away */
  3929. struct ael_priority *np2 = new_prio();
  3930. np2->type = AEL_APPCALL;
  3931. np2->app = strdup("NoOp");
  3932. snprintf(buf,sizeof(buf),"End of Macro %s-%s",p->u1.str, exten->name);
  3933. np2->appargs = strdup(buf);
  3934. linkprio(exten, np2, NULL);
  3935. exten-> return_target = np2;
  3936. }
  3937. set_priorities(exten);
  3938. attach_exten(&exten_list, exten);
  3939. break;
  3940. case PV_GLOBALS:
  3941. /* already done */
  3942. break;
  3943. case PV_CONTEXT:
  3944. context = ast_context_find_or_create(local_contexts, local_table, p->u1.str, registrar);
  3945. /* contexts contain: ignorepat, includes, switches, eswitches, extensions, */
  3946. for (p2=p->u2.statements; p2; p2=p2->next) {
  3947. pval *p3;
  3948. char *s3;
  3949. switch (p2->type) {
  3950. case PV_EXTENSION:
  3951. exten = new_exten();
  3952. exten->name = strdup(p2->u1.str);
  3953. exten->context = context;
  3954. if( (s3=strchr(exten->name, '/') ) != 0 )
  3955. {
  3956. *s3 = 0;
  3957. exten->cidmatch = s3+1;
  3958. }
  3959. if ( p2->u3.hints )
  3960. exten->hints = strdup(p2->u3.hints);
  3961. exten->regexten = p2->u4.regexten;
  3962. if (gen_prios(exten, p->u1.str, p2->u2.statements, 0, context)) {
  3963. return -1;
  3964. }
  3965. if (exten->return_needed) { /* returns don't generate a goto eoe (end of extension) any more, just a Return() app call) */
  3966. struct ael_priority *np2 = new_prio();
  3967. np2->type = AEL_APPCALL;
  3968. np2->app = strdup("NoOp");
  3969. snprintf(buf,sizeof(buf),"End of Extension %s", exten->name);
  3970. np2->appargs = strdup(buf);
  3971. linkprio(exten, np2, NULL);
  3972. exten-> return_target = np2;
  3973. }
  3974. /* is the last priority in the extension a label? Then add a trailing no-op */
  3975. if ( exten->plist_last && exten->plist_last->type == AEL_LABEL ) {
  3976. struct ael_priority *np2 = new_prio();
  3977. np2->type = AEL_APPCALL;
  3978. np2->app = strdup("NoOp");
  3979. snprintf(buf,sizeof(buf),"A NoOp to follow a trailing label %s", exten->plist_last->origin->u1.str);
  3980. np2->appargs = strdup(buf);
  3981. linkprio(exten, np2, NULL);
  3982. }
  3983. set_priorities(exten);
  3984. attach_exten(&exten_list, exten);
  3985. break;
  3986. case PV_IGNOREPAT:
  3987. ast_context_add_ignorepat2(context, p2->u1.str, registrar);
  3988. break;
  3989. case PV_INCLUDES:
  3990. for (p3 = p2->u1.list; p3 ;p3=p3->next) {
  3991. if ( p3->u2.arglist ) {
  3992. snprintf(buf,sizeof(buf), "%s,%s,%s,%s,%s",
  3993. p3->u1.str,
  3994. p3->u2.arglist->u1.str,
  3995. p3->u2.arglist->next->u1.str,
  3996. p3->u2.arglist->next->next->u1.str,
  3997. p3->u2.arglist->next->next->next->u1.str);
  3998. ast_context_add_include2(context, buf, registrar);
  3999. } else
  4000. ast_context_add_include2(context, p3->u1.str, registrar);
  4001. }
  4002. break;
  4003. case PV_SWITCHES:
  4004. for (p3 = p2->u1.list; p3 ;p3=p3->next) {
  4005. char *c = strchr(p3->u1.str, '/');
  4006. if (c) {
  4007. *c = '\0';
  4008. c++;
  4009. } else
  4010. c = "";
  4011. ast_context_add_switch2(context, p3->u1.str, c, 0, registrar);
  4012. }
  4013. break;
  4014. case PV_ESWITCHES:
  4015. for (p3 = p2->u1.list; p3 ;p3=p3->next) {
  4016. char *c = strchr(p3->u1.str, '/');
  4017. if (c) {
  4018. *c = '\0';
  4019. c++;
  4020. } else
  4021. c = "";
  4022. ast_context_add_switch2(context, p3->u1.str, c, 1, registrar);
  4023. }
  4024. break;
  4025. default:
  4026. break;
  4027. }
  4028. }
  4029. break;
  4030. default:
  4031. /* huh? what? */
  4032. break;
  4033. }
  4034. }
  4035. /* Create default "h" bubble context */
  4036. if (ast_custom_function_find("DIALPLAN_EXISTS") && ast_custom_function_find("STACK_PEEK")) {
  4037. int i;
  4038. const char *h_context = "ael-builtin-h-bubble";
  4039. struct ael_priority *np;
  4040. struct {
  4041. int priority;
  4042. const char *app;
  4043. const char *arg;
  4044. } steps[] = {
  4045. /* Start high, to avoid conflict with existing h extensions */
  4046. { 1, "Goto", "9991" },
  4047. /* Save the context, because after the StackPop, it disappears */
  4048. { 9991, "Set", "~~parentcxt~~=${STACK_PEEK(1,c,1)}" },
  4049. /* If we're not in a Gosub frame, exit */
  4050. { 9992, "GotoIf", "$[\"${~~parentcxt~~}\"=\"\"]?9996" },
  4051. /* Check for an "h" extension in that context */
  4052. { 9993, "GotoIf", "${DIALPLAN_EXISTS(${~~parentcxt~~},h,1)}?9994:9996" },
  4053. /* Pop off the stack frame to prevent an infinite loop */
  4054. { 9994, "StackPop", "" },
  4055. /* Finally, go there. */
  4056. { 9995, "Goto", "${~~parentcxt~~},h,1" },
  4057. /* Just an empty priority for jumping out early */
  4058. { 9996, "NoOp", "" }
  4059. };
  4060. context = ast_context_find_or_create(local_contexts, local_table, h_context, registrar);
  4061. if (context_used(exten_list, context)) {
  4062. int found = 0;
  4063. while (!found) {
  4064. /* Pick a new context name that is not used. */
  4065. char h_context_template[] = "/tmp/ael-builtin-h-bubble-XXXXXX";
  4066. int fd = mkstemp(h_context_template);
  4067. unlink(h_context_template);
  4068. close(fd);
  4069. context = ast_context_find_or_create(local_contexts, local_table, h_context_template + 5, registrar);
  4070. found = !context_used(exten_list, context);
  4071. }
  4072. h_context = ast_get_context_name(context);
  4073. }
  4074. exten = new_exten();
  4075. exten->context = context;
  4076. exten->name = strdup("h");
  4077. for (i = 0; i < ARRAY_LEN(steps); i++) {
  4078. np = new_prio();
  4079. np->type = AEL_APPCALL;
  4080. np->priority_num = steps[i].priority;
  4081. np->app = strdup(steps[i].app);
  4082. np->appargs = strdup(steps[i].arg);
  4083. linkprio(exten, np, NULL);
  4084. }
  4085. attach_exten(&exten_list, exten);
  4086. /* Include the default "h" bubble context in each macro context */
  4087. for (exten = exten_list; exten; exten = exten->next_exten) {
  4088. /* All macros contain a "~~s~~" extension, and it's the first created. If
  4089. * we perchance get a non-macro context, it's no big deal; the logic is
  4090. * designed to exit out smoothly if not called from within a Gosub. */
  4091. if (!strcmp(exten->name, "~~s~~")) {
  4092. ast_context_add_include2(exten->context, h_context, registrar);
  4093. }
  4094. }
  4095. }
  4096. /* moved these from being done after a macro or extension were processed,
  4097. to after all processing is done, for the sake of fixing gotos to labels inside cases... */
  4098. /* I guess this would be considered 2nd pass of compiler now... */
  4099. fix_gotos_in_extensions(exten_list); /* find and fix extension ref in gotos to labels that are in case statements */
  4100. add_extensions(exten_list); /* actually makes calls to create priorities in ast_contexts -- feeds dialplan to asterisk */
  4101. destroy_extensions(exten_list); /* all that remains is an empty husk, discard of it as is proper */
  4102. return 0;
  4103. }
  4104. /* DESTROY the PVAL tree ============================================================================ */
  4105. void destroy_pval_item(pval *item)
  4106. {
  4107. if (item == NULL) {
  4108. ast_log(LOG_WARNING, "null item\n");
  4109. return;
  4110. }
  4111. if (item->filename)
  4112. free(item->filename);
  4113. switch (item->type) {
  4114. case PV_WORD:
  4115. /* fields: item->u1.str == string associated with this (word). */
  4116. if (item->u1.str )
  4117. free(item->u1.str);
  4118. if ( item->u2.arglist )
  4119. destroy_pval(item->u2.arglist);
  4120. break;
  4121. case PV_MACRO:
  4122. /* fields: item->u1.str == name of macro
  4123. item->u2.arglist == pval list of PV_WORD arguments of macro, as given by user
  4124. item->u2.arglist->u1.str == argument
  4125. item->u2.arglist->next == next arg
  4126. item->u3.macro_statements == pval list of statements in macro body.
  4127. */
  4128. destroy_pval(item->u2.arglist);
  4129. if (item->u1.str )
  4130. free(item->u1.str);
  4131. destroy_pval(item->u3.macro_statements);
  4132. break;
  4133. case PV_CONTEXT:
  4134. /* fields: item->u1.str == name of context
  4135. item->u2.statements == pval list of statements in context body
  4136. item->u3.abstract == int 1 if an abstract keyword were present
  4137. */
  4138. if (item->u1.str)
  4139. free(item->u1.str);
  4140. destroy_pval(item->u2.statements);
  4141. break;
  4142. case PV_MACRO_CALL:
  4143. /* fields: item->u1.str == name of macro to call
  4144. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  4145. item->u2.arglist->u1.str == argument
  4146. item->u2.arglist->next == next arg
  4147. */
  4148. if (item->u1.str)
  4149. free(item->u1.str);
  4150. destroy_pval(item->u2.arglist);
  4151. break;
  4152. case PV_APPLICATION_CALL:
  4153. /* fields: item->u1.str == name of application to call
  4154. item->u2.arglist == pval list of PV_WORD arguments of macro call, as given by user
  4155. item->u2.arglist->u1.str == argument
  4156. item->u2.arglist->next == next arg
  4157. */
  4158. if (item->u1.str)
  4159. free(item->u1.str);
  4160. destroy_pval(item->u2.arglist);
  4161. break;
  4162. case PV_CASE:
  4163. /* fields: item->u1.str == value of case
  4164. item->u2.statements == pval list of statements under the case
  4165. */
  4166. if (item->u1.str)
  4167. free(item->u1.str);
  4168. destroy_pval(item->u2.statements);
  4169. break;
  4170. case PV_PATTERN:
  4171. /* fields: item->u1.str == value of case
  4172. item->u2.statements == pval list of statements under the case
  4173. */
  4174. if (item->u1.str)
  4175. free(item->u1.str);
  4176. destroy_pval(item->u2.statements);
  4177. break;
  4178. case PV_DEFAULT:
  4179. /* fields:
  4180. item->u2.statements == pval list of statements under the case
  4181. */
  4182. destroy_pval(item->u2.statements);
  4183. break;
  4184. case PV_CATCH:
  4185. /* fields: item->u1.str == name of extension to catch
  4186. item->u2.statements == pval list of statements in context body
  4187. */
  4188. if (item->u1.str)
  4189. free(item->u1.str);
  4190. destroy_pval(item->u2.statements);
  4191. break;
  4192. case PV_SWITCHES:
  4193. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  4194. */
  4195. destroy_pval(item->u1.list);
  4196. break;
  4197. case PV_ESWITCHES:
  4198. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  4199. */
  4200. destroy_pval(item->u1.list);
  4201. break;
  4202. case PV_INCLUDES:
  4203. /* fields: item->u1.list == pval list of PV_WORD elements, one per entry in the list
  4204. item->u2.arglist == pval list of 4 PV_WORD elements for time values
  4205. */
  4206. destroy_pval(item->u1.list);
  4207. break;
  4208. case PV_STATEMENTBLOCK:
  4209. /* fields: item->u1.list == pval list of statements in block, one per entry in the list
  4210. */
  4211. destroy_pval(item->u1.list);
  4212. break;
  4213. case PV_LOCALVARDEC:
  4214. case PV_VARDEC:
  4215. /* fields: item->u1.str == variable name
  4216. item->u2.val == variable value to assign
  4217. */
  4218. if (item->u1.str)
  4219. free(item->u1.str);
  4220. if (item->u2.val)
  4221. free(item->u2.val);
  4222. break;
  4223. case PV_GOTO:
  4224. /* fields: item->u1.list == pval list of PV_WORD target names, up to 3, in order as given by user.
  4225. item->u1.list->u1.str == where the data on a PV_WORD will always be.
  4226. */
  4227. destroy_pval(item->u1.list);
  4228. break;
  4229. case PV_LABEL:
  4230. /* fields: item->u1.str == label name
  4231. */
  4232. if (item->u1.str)
  4233. free(item->u1.str);
  4234. break;
  4235. case PV_FOR:
  4236. /* fields: item->u1.for_init == a string containing the initalizer
  4237. item->u2.for_test == a string containing the loop test
  4238. item->u3.for_inc == a string containing the loop increment
  4239. item->u4.for_statements == a pval list of statements in the for ()
  4240. */
  4241. if (item->u1.for_init)
  4242. free(item->u1.for_init);
  4243. if (item->u2.for_test)
  4244. free(item->u2.for_test);
  4245. if (item->u3.for_inc)
  4246. free(item->u3.for_inc);
  4247. destroy_pval(item->u4.for_statements);
  4248. break;
  4249. case PV_WHILE:
  4250. /* fields: item->u1.str == the while conditional, as supplied by user
  4251. item->u2.statements == a pval list of statements in the while ()
  4252. */
  4253. if (item->u1.str)
  4254. free(item->u1.str);
  4255. destroy_pval(item->u2.statements);
  4256. break;
  4257. case PV_BREAK:
  4258. /* fields: none
  4259. */
  4260. break;
  4261. case PV_RETURN:
  4262. /* fields: none
  4263. */
  4264. break;
  4265. case PV_CONTINUE:
  4266. /* fields: none
  4267. */
  4268. break;
  4269. case PV_IFTIME:
  4270. /* fields: item->u1.list == the 4 time values, in PV_WORD structs, linked list
  4271. item->u2.statements == a pval list of statements in the if ()
  4272. item->u3.else_statements == a pval list of statements in the else
  4273. (could be zero)
  4274. */
  4275. destroy_pval(item->u1.list);
  4276. destroy_pval(item->u2.statements);
  4277. if (item->u3.else_statements) {
  4278. destroy_pval(item->u3.else_statements);
  4279. }
  4280. break;
  4281. case PV_RANDOM:
  4282. /* fields: item->u1.str == the random percentage, as supplied by user
  4283. item->u2.statements == a pval list of statements in the true part ()
  4284. item->u3.else_statements == a pval list of statements in the else
  4285. (could be zero)
  4286. fall thru to If */
  4287. case PV_IF:
  4288. /* fields: item->u1.str == the if conditional, as supplied by user
  4289. item->u2.statements == a pval list of statements in the if ()
  4290. item->u3.else_statements == a pval list of statements in the else
  4291. (could be zero)
  4292. */
  4293. if (item->u1.str)
  4294. free(item->u1.str);
  4295. destroy_pval(item->u2.statements);
  4296. if (item->u3.else_statements) {
  4297. destroy_pval(item->u3.else_statements);
  4298. }
  4299. break;
  4300. case PV_SWITCH:
  4301. /* fields: item->u1.str == the switch expression
  4302. item->u2.statements == a pval list of statements in the switch,
  4303. (will be case statements, most likely!)
  4304. */
  4305. if (item->u1.str)
  4306. free(item->u1.str);
  4307. destroy_pval(item->u2.statements);
  4308. break;
  4309. case PV_EXTENSION:
  4310. /* fields: item->u1.str == the extension name, label, whatever it's called
  4311. item->u2.statements == a pval list of statements in the extension
  4312. item->u3.hints == a char * hint argument
  4313. item->u4.regexten == an int boolean. non-zero says that regexten was specified
  4314. */
  4315. if (item->u1.str)
  4316. free(item->u1.str);
  4317. if (item->u3.hints)
  4318. free(item->u3.hints);
  4319. destroy_pval(item->u2.statements);
  4320. break;
  4321. case PV_IGNOREPAT:
  4322. /* fields: item->u1.str == the ignorepat data
  4323. */
  4324. if (item->u1.str)
  4325. free(item->u1.str);
  4326. break;
  4327. case PV_GLOBALS:
  4328. /* fields: item->u1.statements == pval list of statements, usually vardecs
  4329. */
  4330. destroy_pval(item->u1.statements);
  4331. break;
  4332. }
  4333. free(item);
  4334. }
  4335. void destroy_pval(pval *item)
  4336. {
  4337. pval *i,*nxt;
  4338. for (i=item; i; i=nxt) {
  4339. nxt = i->next;
  4340. destroy_pval_item(i);
  4341. }
  4342. }
  4343. #ifdef AAL_ARGCHECK
  4344. static char *ael_funclist[] =
  4345. {
  4346. "AGENT",
  4347. "ARRAY",
  4348. "BASE64_DECODE",
  4349. "BASE64_ENCODE",
  4350. "CALLERID",
  4351. "CDR",
  4352. "CHANNEL",
  4353. "CHECKSIPDOMAIN",
  4354. "CHECK_MD5",
  4355. "CURL",
  4356. "CUT",
  4357. "DB",
  4358. "DB_EXISTS",
  4359. "DUNDILOOKUP",
  4360. "ENUMLOOKUP",
  4361. "ENV",
  4362. "EVAL",
  4363. "EXISTS",
  4364. "FIELDQTY",
  4365. "FILTER",
  4366. "GROUP",
  4367. "GROUP_COUNT",
  4368. "GROUP_LIST",
  4369. "GROUP_MATCH_COUNT",
  4370. "IAXPEER",
  4371. "IF",
  4372. "IFTIME",
  4373. "ISNULL",
  4374. "KEYPADHASH",
  4375. "LANGUAGE",
  4376. "LEN",
  4377. "MATH",
  4378. "MD5",
  4379. "MUSICCLASS",
  4380. "QUEUEAGENTCOUNT",
  4381. "QUEUE_MEMBER_COUNT",
  4382. "QUEUE_MEMBER_LIST",
  4383. "QUOTE",
  4384. "RAND",
  4385. "REGEX",
  4386. "SET",
  4387. "SHA1",
  4388. "SIPCHANINFO",
  4389. "SIPPEER",
  4390. "SIP_HEADER",
  4391. "SORT",
  4392. "STAT",
  4393. "STRFTIME",
  4394. "STRPTIME",
  4395. "TIMEOUT",
  4396. "TXTCIDNAME",
  4397. "URIDECODE",
  4398. "URIENCODE",
  4399. "VMCOUNT"
  4400. };
  4401. int ael_is_funcname(char *name)
  4402. {
  4403. int s,t;
  4404. t = sizeof(ael_funclist)/sizeof(char*);
  4405. s = 0;
  4406. while ((s < t) && strcasecmp(name, ael_funclist[s]))
  4407. s++;
  4408. if ( s < t )
  4409. return 1;
  4410. else
  4411. return 0;
  4412. }
  4413. #endif
  4414. /* PVAL PI */
  4415. /* ----------------- implementation ------------------- */
  4416. int pvalCheckType( pval *p, char *funcname, pvaltype type )
  4417. {
  4418. if (p->type != type)
  4419. {
  4420. ast_log(LOG_ERROR, "Func: %s the pval passed is not appropriate for this function!\n", funcname);
  4421. return 0;
  4422. }
  4423. return 1;
  4424. }
  4425. pval *pvalCreateNode( pvaltype type )
  4426. {
  4427. pval *p = calloc(1,sizeof(pval)); /* why, oh why, don't I use ast_calloc? Way, way, way too messy if I do! */
  4428. p->type = type; /* remember, this can be used externally or internally to asterisk */
  4429. return p;
  4430. }
  4431. pvaltype pvalObjectGetType( pval *p )
  4432. {
  4433. return p->type;
  4434. }
  4435. void pvalWordSetString( pval *p, char *str)
  4436. {
  4437. if (!pvalCheckType(p, "pvalWordSetString", PV_WORD))
  4438. return;
  4439. p->u1.str = str;
  4440. }
  4441. char *pvalWordGetString( pval *p )
  4442. {
  4443. if (!pvalCheckType(p, "pvalWordGetString", PV_WORD))
  4444. return 0;
  4445. return p->u1.str;
  4446. }
  4447. void pvalMacroSetName( pval *p, char *name)
  4448. {
  4449. if (!pvalCheckType(p, "pvalMacroSetName", PV_MACRO))
  4450. return;
  4451. p->u1.str = name;
  4452. }
  4453. char *pvalMacroGetName( pval *p )
  4454. {
  4455. if (!pvalCheckType(p, "pvalMacroGetName", PV_MACRO))
  4456. return 0;
  4457. return p->u1.str;
  4458. }
  4459. void pvalMacroSetArglist( pval *p, pval *arglist )
  4460. {
  4461. if (!pvalCheckType(p, "pvalMacroSetArglist", PV_MACRO))
  4462. return;
  4463. p->u2.arglist = arglist;
  4464. }
  4465. void pvalMacroAddArg( pval *p, pval *arg ) /* single arg only! */
  4466. {
  4467. if (!pvalCheckType(p, "pvalMacroAddArg", PV_MACRO))
  4468. return;
  4469. if (!p->u2.arglist)
  4470. p->u2.arglist = arg;
  4471. else
  4472. linku1(p->u2.arglist, arg);
  4473. }
  4474. pval *pvalMacroWalkArgs( pval *p, pval **arg )
  4475. {
  4476. if (!pvalCheckType(p, "pvalMacroWalkArgs", PV_MACRO))
  4477. return 0;
  4478. if (!(*arg))
  4479. *arg = p->u2.arglist;
  4480. else {
  4481. *arg = (*arg)->next;
  4482. }
  4483. return *arg;
  4484. }
  4485. void pvalMacroAddStatement( pval *p, pval *statement )
  4486. {
  4487. if (!pvalCheckType(p, "pvalMacroAddStatement", PV_MACRO))
  4488. return;
  4489. if (!p->u3.macro_statements)
  4490. p->u3.macro_statements = statement;
  4491. else
  4492. linku1(p->u3.macro_statements, statement);
  4493. }
  4494. pval *pvalMacroWalkStatements( pval *p, pval **next_statement )
  4495. {
  4496. if (!pvalCheckType(p, "pvalMacroWalkStatements", PV_MACRO))
  4497. return 0;
  4498. if (!(*next_statement))
  4499. *next_statement = p->u3.macro_statements;
  4500. else {
  4501. *next_statement = (*next_statement)->next;
  4502. }
  4503. return *next_statement;
  4504. }
  4505. void pvalContextSetName( pval *p, char *name)
  4506. {
  4507. if (!pvalCheckType(p, "pvalContextSetName", PV_CONTEXT))
  4508. return;
  4509. p->u1.str = name;
  4510. }
  4511. char *pvalContextGetName( pval *p )
  4512. {
  4513. if (!pvalCheckType(p, "pvalContextGetName", PV_CONTEXT))
  4514. return 0;
  4515. return p->u1.str;
  4516. }
  4517. void pvalContextSetAbstract( pval *p )
  4518. {
  4519. if (!pvalCheckType(p, "pvalContextSetAbstract", PV_CONTEXT))
  4520. return;
  4521. p->u3.abstract = 1;
  4522. }
  4523. void pvalContextUnsetAbstract( pval *p )
  4524. {
  4525. if (!pvalCheckType(p, "pvalContextUnsetAbstract", PV_CONTEXT))
  4526. return;
  4527. p->u3.abstract = 0;
  4528. }
  4529. int pvalContextGetAbstract( pval *p )
  4530. {
  4531. if (!pvalCheckType(p, "pvalContextGetAbstract", PV_CONTEXT))
  4532. return 0;
  4533. return p->u3.abstract;
  4534. }
  4535. void pvalContextAddStatement( pval *p, pval *statement) /* this includes SWITCHES, INCLUDES, IGNOREPAT, etc */
  4536. {
  4537. if (!pvalCheckType(p, "pvalContextAddStatement", PV_CONTEXT))
  4538. return;
  4539. if (!p->u2.statements)
  4540. p->u2.statements = statement;
  4541. else
  4542. linku1(p->u2.statements, statement);
  4543. }
  4544. pval *pvalContextWalkStatements( pval *p, pval **statements )
  4545. {
  4546. if (!pvalCheckType(p, "pvalContextWalkStatements", PV_CONTEXT))
  4547. return 0;
  4548. if (!(*statements))
  4549. *statements = p->u2.statements;
  4550. else {
  4551. *statements = (*statements)->next;
  4552. }
  4553. return *statements;
  4554. }
  4555. void pvalMacroCallSetMacroName( pval *p, char *name )
  4556. {
  4557. if (!pvalCheckType(p, "pvalMacroCallSetMacroName", PV_MACRO_CALL))
  4558. return;
  4559. p->u1.str = name;
  4560. }
  4561. char* pvalMacroCallGetMacroName( pval *p )
  4562. {
  4563. if (!pvalCheckType(p, "pvalMacroCallGetMacroName", PV_MACRO_CALL))
  4564. return 0;
  4565. return p->u1.str;
  4566. }
  4567. void pvalMacroCallSetArglist( pval *p, pval *arglist )
  4568. {
  4569. if (!pvalCheckType(p, "pvalMacroCallSetArglist", PV_MACRO_CALL))
  4570. return;
  4571. p->u2.arglist = arglist;
  4572. }
  4573. void pvalMacroCallAddArg( pval *p, pval *arg )
  4574. {
  4575. if (!pvalCheckType(p, "pvalMacroCallGetAddArg", PV_MACRO_CALL))
  4576. return;
  4577. if (!p->u2.arglist)
  4578. p->u2.arglist = arg;
  4579. else
  4580. linku1(p->u2.arglist, arg);
  4581. }
  4582. pval *pvalMacroCallWalkArgs( pval *p, pval **args )
  4583. {
  4584. if (!pvalCheckType(p, "pvalMacroCallWalkArgs", PV_MACRO_CALL))
  4585. return 0;
  4586. if (!(*args))
  4587. *args = p->u2.arglist;
  4588. else {
  4589. *args = (*args)->next;
  4590. }
  4591. return *args;
  4592. }
  4593. void pvalAppCallSetAppName( pval *p, char *name )
  4594. {
  4595. if (!pvalCheckType(p, "pvalAppCallSetAppName", PV_APPLICATION_CALL))
  4596. return;
  4597. p->u1.str = name;
  4598. }
  4599. char* pvalAppCallGetAppName( pval *p )
  4600. {
  4601. if (!pvalCheckType(p, "pvalAppCallGetAppName", PV_APPLICATION_CALL))
  4602. return 0;
  4603. return p->u1.str;
  4604. }
  4605. void pvalAppCallSetArglist( pval *p, pval *arglist )
  4606. {
  4607. if (!pvalCheckType(p, "pvalAppCallSetArglist", PV_APPLICATION_CALL))
  4608. return;
  4609. p->u2.arglist = arglist;
  4610. }
  4611. void pvalAppCallAddArg( pval *p, pval *arg )
  4612. {
  4613. if (!pvalCheckType(p, "pvalAppCallAddArg", PV_APPLICATION_CALL))
  4614. return;
  4615. if (!p->u2.arglist)
  4616. p->u2.arglist = arg;
  4617. else
  4618. linku1(p->u2.arglist, arg);
  4619. }
  4620. pval *pvalAppCallWalkArgs( pval *p, pval **args )
  4621. {
  4622. if (!pvalCheckType(p, "pvalAppCallWalkArgs", PV_APPLICATION_CALL))
  4623. return 0;
  4624. if (!(*args))
  4625. *args = p->u2.arglist;
  4626. else {
  4627. *args = (*args)->next;
  4628. }
  4629. return *args;
  4630. }
  4631. void pvalCasePatSetVal( pval *p, char *val )
  4632. {
  4633. if (!pvalCheckType(p, "pvalAppCallWalkArgs", PV_APPLICATION_CALL))
  4634. return;
  4635. p->u1.str = val;
  4636. }
  4637. char* pvalCasePatGetVal( pval *p )
  4638. {
  4639. return p->u1.str;
  4640. }
  4641. void pvalCasePatDefAddStatement( pval *p, pval *statement )
  4642. {
  4643. if (!p->u2.arglist)
  4644. p->u2.statements = statement;
  4645. else
  4646. linku1(p->u2.statements, statement);
  4647. }
  4648. pval *pvalCasePatDefWalkStatements( pval *p, pval **statement )
  4649. {
  4650. if (!(*statement))
  4651. *statement = p->u2.statements;
  4652. else {
  4653. *statement = (*statement)->next;
  4654. }
  4655. return *statement;
  4656. }
  4657. void pvalCatchSetExtName( pval *p, char *name )
  4658. {
  4659. if (!pvalCheckType(p, "pvalCatchSetExtName", PV_CATCH))
  4660. return;
  4661. p->u1.str = name;
  4662. }
  4663. char* pvalCatchGetExtName( pval *p )
  4664. {
  4665. if (!pvalCheckType(p, "pvalCatchGetExtName", PV_CATCH))
  4666. return 0;
  4667. return p->u1.str;
  4668. }
  4669. void pvalCatchSetStatement( pval *p, pval *statement )
  4670. {
  4671. if (!pvalCheckType(p, "pvalCatchSetStatement", PV_CATCH))
  4672. return;
  4673. p->u2.statements = statement;
  4674. }
  4675. pval *pvalCatchGetStatement( pval *p )
  4676. {
  4677. if (!pvalCheckType(p, "pvalCatchGetStatement", PV_CATCH))
  4678. return 0;
  4679. return p->u2.statements;
  4680. }
  4681. void pvalSwitchesAddSwitch( pval *p, char *name )
  4682. {
  4683. pval *s;
  4684. if (!pvalCheckType(p, "pvalSwitchesAddSwitch", PV_SWITCHES))
  4685. return;
  4686. s = pvalCreateNode(PV_WORD);
  4687. s->u1.str = name;
  4688. p->u1.list = linku1(p->u1.list, s);
  4689. }
  4690. char* pvalSwitchesWalkNames( pval *p, pval **next_item )
  4691. {
  4692. if (!pvalCheckType(p, "pvalSwitchesWalkNames", PV_SWITCHES))
  4693. return 0;
  4694. if (!(*next_item))
  4695. *next_item = p->u1.list;
  4696. else {
  4697. *next_item = (*next_item)->next;
  4698. }
  4699. return (*next_item)->u1.str;
  4700. }
  4701. void pvalESwitchesAddSwitch( pval *p, char *name )
  4702. {
  4703. pval *s;
  4704. if (!pvalCheckType(p, "pvalESwitchesAddSwitch", PV_ESWITCHES))
  4705. return;
  4706. s = pvalCreateNode(PV_WORD);
  4707. s->u1.str = name;
  4708. p->u1.list = linku1(p->u1.list, s);
  4709. }
  4710. char* pvalESwitchesWalkNames( pval *p, pval **next_item )
  4711. {
  4712. if (!pvalCheckType(p, "pvalESwitchesWalkNames", PV_ESWITCHES))
  4713. return 0;
  4714. if (!(*next_item))
  4715. *next_item = p->u1.list;
  4716. else {
  4717. *next_item = (*next_item)->next;
  4718. }
  4719. return (*next_item)->u1.str;
  4720. }
  4721. void pvalIncludesAddInclude( pval *p, const char *include )
  4722. {
  4723. pval *s;
  4724. if (!pvalCheckType(p, "pvalIncludesAddSwitch", PV_INCLUDES))
  4725. return;
  4726. s = pvalCreateNode(PV_WORD);
  4727. s->u1.str = (char *)include;
  4728. p->u1.list = linku1(p->u1.list, s);
  4729. }
  4730. /* an include is a WORD with string set to path */
  4731. void pvalIncludesAddIncludeWithTimeConstraints( pval *p, const char *include, char *hour_range, char *dom_range, char *dow_range, char *month_range )
  4732. {
  4733. pval *hr = pvalCreateNode(PV_WORD);
  4734. pval *dom = pvalCreateNode(PV_WORD);
  4735. pval *dow = pvalCreateNode(PV_WORD);
  4736. pval *mon = pvalCreateNode(PV_WORD);
  4737. pval *s = pvalCreateNode(PV_WORD);
  4738. if (!pvalCheckType(p, "pvalIncludeAddIncludeWithTimeConstraints", PV_INCLUDES))
  4739. return;
  4740. s->u1.str = (char *)include;
  4741. p->u1.list = linku1(p->u1.list, s);
  4742. hr->u1.str = hour_range;
  4743. dom->u1.str = dom_range;
  4744. dow->u1.str = dow_range;
  4745. mon->u1.str = month_range;
  4746. s->u2.arglist = hr;
  4747. hr->next = dom;
  4748. dom->next = dow;
  4749. dow->next = mon;
  4750. mon->next = 0;
  4751. }
  4752. /* is this right??? come back and correct it */ /*the ptr is to the WORD */
  4753. void pvalIncludeGetTimeConstraints( pval *p, char **hour_range, char **dom_range, char **dow_range, char **month_range )
  4754. {
  4755. if (!pvalCheckType(p, "pvalIncludeGetTimeConstraints", PV_WORD))
  4756. return;
  4757. if (p->u2.arglist) {
  4758. *hour_range = p->u2.arglist->u1.str;
  4759. *dom_range = p->u2.arglist->next->u1.str;
  4760. *dow_range = p->u2.arglist->next->next->u1.str;
  4761. *month_range = p->u2.arglist->next->next->next->u1.str;
  4762. } else {
  4763. *hour_range = 0;
  4764. *dom_range = 0;
  4765. *dow_range = 0;
  4766. *month_range = 0;
  4767. }
  4768. }
  4769. /* is this right??? come back and correct it */ /*the ptr is to the WORD */
  4770. char* pvalIncludesWalk( pval *p, pval **next_item )
  4771. {
  4772. if (!pvalCheckType(p, "pvalIncludesWalk", PV_INCLUDES))
  4773. return 0;
  4774. if (!(*next_item))
  4775. *next_item = p->u1.list;
  4776. else {
  4777. *next_item = (*next_item)->next;
  4778. }
  4779. return (*next_item)->u1.str;
  4780. }
  4781. void pvalStatementBlockAddStatement( pval *p, pval *statement)
  4782. {
  4783. if (!pvalCheckType(p, "pvalStatementBlockAddStatement", PV_STATEMENTBLOCK))
  4784. return;
  4785. p->u1.list = linku1(p->u1.list, statement);
  4786. }
  4787. pval *pvalStatementBlockWalkStatements( pval *p, pval **next_statement)
  4788. {
  4789. if (!pvalCheckType(p, "pvalStatementBlockWalkStatements", PV_STATEMENTBLOCK))
  4790. return 0;
  4791. if (!(*next_statement))
  4792. *next_statement = p->u1.list;
  4793. else {
  4794. *next_statement = (*next_statement)->next;
  4795. }
  4796. return *next_statement;
  4797. }
  4798. void pvalVarDecSetVarname( pval *p, char *name )
  4799. {
  4800. if (!pvalCheckType(p, "pvalVarDecSetVarname", PV_VARDEC))
  4801. return;
  4802. p->u1.str = name;
  4803. }
  4804. void pvalVarDecSetValue( pval *p, char *value )
  4805. {
  4806. if (!pvalCheckType(p, "pvalVarDecSetValue", PV_VARDEC))
  4807. return;
  4808. p->u2.val = value;
  4809. }
  4810. char* pvalVarDecGetVarname( pval *p )
  4811. {
  4812. if (!pvalCheckType(p, "pvalVarDecGetVarname", PV_VARDEC))
  4813. return 0;
  4814. return p->u1.str;
  4815. }
  4816. char* pvalVarDecGetValue( pval *p )
  4817. {
  4818. if (!pvalCheckType(p, "pvalVarDecGetValue", PV_VARDEC))
  4819. return 0;
  4820. return p->u2.val;
  4821. }
  4822. void pvalGotoSetTarget( pval *p, char *context, char *exten, char *label )
  4823. {
  4824. pval *con, *ext, *pri;
  4825. if (!pvalCheckType(p, "pvalGotoSetTarget", PV_GOTO))
  4826. return;
  4827. if (context && strlen(context)) {
  4828. con = pvalCreateNode(PV_WORD);
  4829. ext = pvalCreateNode(PV_WORD);
  4830. pri = pvalCreateNode(PV_WORD);
  4831. con->u1.str = context;
  4832. ext->u1.str = exten;
  4833. pri->u1.str = label;
  4834. con->next = ext;
  4835. ext->next = pri;
  4836. p->u1.list = con;
  4837. } else if (exten && strlen(exten)) {
  4838. ext = pvalCreateNode(PV_WORD);
  4839. pri = pvalCreateNode(PV_WORD);
  4840. ext->u1.str = exten;
  4841. pri->u1.str = label;
  4842. ext->next = pri;
  4843. p->u1.list = ext;
  4844. } else {
  4845. pri = pvalCreateNode(PV_WORD);
  4846. pri->u1.str = label;
  4847. p->u1.list = pri;
  4848. }
  4849. }
  4850. void pvalGotoGetTarget( pval *p, char **context, char **exten, char **label )
  4851. {
  4852. if (!pvalCheckType(p, "pvalGotoGetTarget", PV_GOTO))
  4853. return;
  4854. if (p->u1.list && p->u1.list->next && p->u1.list->next->next) {
  4855. *context = p->u1.list->u1.str;
  4856. *exten = p->u1.list->next->u1.str;
  4857. *label = p->u1.list->next->next->u1.str;
  4858. } else if (p->u1.list && p->u1.list->next ) {
  4859. *exten = p->u1.list->u1.str;
  4860. *label = p->u1.list->next->u1.str;
  4861. *context = 0;
  4862. } else if (p->u1.list) {
  4863. *label = p->u1.list->u1.str;
  4864. *context = 0;
  4865. *exten = 0;
  4866. } else {
  4867. *context = 0;
  4868. *exten = 0;
  4869. *label = 0;
  4870. }
  4871. }
  4872. void pvalLabelSetName( pval *p, char *name )
  4873. {
  4874. if (!pvalCheckType(p, "pvalLabelSetName", PV_LABEL))
  4875. return;
  4876. p->u1.str = name;
  4877. }
  4878. char* pvalLabelGetName( pval *p )
  4879. {
  4880. if (!pvalCheckType(p, "pvalLabelGetName", PV_LABEL))
  4881. return 0;
  4882. return p->u1.str;
  4883. }
  4884. void pvalForSetInit( pval *p, char *init )
  4885. {
  4886. if (!pvalCheckType(p, "pvalForSetInit", PV_FOR))
  4887. return;
  4888. p->u1.for_init = init;
  4889. }
  4890. void pvalForSetTest( pval *p, char *test )
  4891. {
  4892. if (!pvalCheckType(p, "pvalForSetTest", PV_FOR))
  4893. return;
  4894. p->u2.for_test = test;
  4895. }
  4896. void pvalForSetInc( pval *p, char *inc )
  4897. {
  4898. if (!pvalCheckType(p, "pvalForSetInc", PV_FOR))
  4899. return;
  4900. p->u3.for_inc = inc;
  4901. }
  4902. void pvalForSetStatement( pval *p, pval *statement )
  4903. {
  4904. if (!pvalCheckType(p, "pvalForSetStatement", PV_FOR))
  4905. return;
  4906. p->u4.for_statements = statement;
  4907. }
  4908. char* pvalForGetInit( pval *p )
  4909. {
  4910. if (!pvalCheckType(p, "pvalForGetInit", PV_FOR))
  4911. return 0;
  4912. return p->u1.for_init;
  4913. }
  4914. char* pvalForGetTest( pval *p )
  4915. {
  4916. if (!pvalCheckType(p, "pvalForGetTest", PV_FOR))
  4917. return 0;
  4918. return p->u2.for_test;
  4919. }
  4920. char* pvalForGetInc( pval *p )
  4921. {
  4922. if (!pvalCheckType(p, "pvalForGetInc", PV_FOR))
  4923. return 0;
  4924. return p->u3.for_inc;
  4925. }
  4926. pval* pvalForGetStatement( pval *p )
  4927. {
  4928. if (!pvalCheckType(p, "pvalForGetStatement", PV_FOR))
  4929. return 0;
  4930. return p->u4.for_statements;
  4931. }
  4932. void pvalIfSetCondition( pval *p, char *expr )
  4933. {
  4934. if (!pvalCheckType(p, "pvalIfSetCondition", PV_IF))
  4935. return;
  4936. p->u1.str = expr;
  4937. }
  4938. char* pvalIfGetCondition( pval *p )
  4939. {
  4940. if (!pvalCheckType(p, "pvalIfGetCondition", PV_IFTIME))
  4941. return 0;
  4942. return p->u1.str;
  4943. }
  4944. void pvalIfTimeSetCondition( pval *p, char *hour_range, char *dow_range, char *dom_range, char *mon_range ) /* time range format: 24-hour format begin-end|dow range|dom range|month range */
  4945. {
  4946. pval *hr = pvalCreateNode(PV_WORD);
  4947. pval *dow = pvalCreateNode(PV_WORD);
  4948. pval *dom = pvalCreateNode(PV_WORD);
  4949. pval *mon = pvalCreateNode(PV_WORD);
  4950. if (!pvalCheckType(p, "pvalIfTimeSetCondition", PV_IFTIME))
  4951. return;
  4952. pvalWordSetString(hr, hour_range);
  4953. pvalWordSetString(dow, dow_range);
  4954. pvalWordSetString(dom, dom_range);
  4955. pvalWordSetString(mon, mon_range);
  4956. dom->next = mon;
  4957. dow->next = dom;
  4958. hr->next = dow;
  4959. p->u1.list = hr;
  4960. }
  4961. /* is this right??? come back and correct it */
  4962. void pvalIfTimeGetCondition( pval *p, char **hour_range, char **dow_range, char **dom_range, char **month_range )
  4963. {
  4964. if (!pvalCheckType(p, "pvalIfTimeGetCondition", PV_IFTIME))
  4965. return;
  4966. *hour_range = p->u1.list->u1.str;
  4967. *dow_range = p->u1.list->next->u1.str;
  4968. *dom_range = p->u1.list->next->next->u1.str;
  4969. *month_range = p->u1.list->next->next->next->u1.str;
  4970. }
  4971. void pvalRandomSetCondition( pval *p, char *percent )
  4972. {
  4973. if (!pvalCheckType(p, "pvalRandomSetCondition", PV_RANDOM))
  4974. return;
  4975. p->u1.str = percent;
  4976. }
  4977. char* pvalRandomGetCondition( pval *p )
  4978. {
  4979. if (!pvalCheckType(p, "pvalRandomGetCondition", PV_RANDOM))
  4980. return 0;
  4981. return p->u1.str;
  4982. }
  4983. void pvalConditionalSetThenStatement( pval *p, pval *statement )
  4984. {
  4985. p->u2.statements = statement;
  4986. }
  4987. void pvalConditionalSetElseStatement( pval *p, pval *statement )
  4988. {
  4989. p->u3.else_statements = statement;
  4990. }
  4991. pval* pvalConditionalGetThenStatement( pval *p )
  4992. {
  4993. return p->u2.statements;
  4994. }
  4995. pval* pvalConditionalGetElseStatement( pval *p )
  4996. {
  4997. return p->u3.else_statements;
  4998. }
  4999. void pvalSwitchSetTestexpr( pval *p, char *expr )
  5000. {
  5001. if (!pvalCheckType(p, "pvalSwitchSetTestexpr", PV_SWITCH))
  5002. return;
  5003. p->u1.str = expr;
  5004. }
  5005. char* pvalSwitchGetTestexpr( pval *p )
  5006. {
  5007. if (!pvalCheckType(p, "pvalSwitchGetTestexpr", PV_SWITCH))
  5008. return 0;
  5009. return p->u1.str;
  5010. }
  5011. void pvalSwitchAddCase( pval *p, pval *Case )
  5012. {
  5013. if (!pvalCheckType(p, "pvalSwitchAddCase", PV_SWITCH))
  5014. return;
  5015. if (!pvalCheckType(Case, "pvalSwitchAddCase", PV_CASE))
  5016. return;
  5017. if (!p->u2.statements)
  5018. p->u2.statements = Case;
  5019. else
  5020. linku1(p->u2.statements, Case);
  5021. }
  5022. pval* pvalSwitchWalkCases( pval *p, pval **next_case )
  5023. {
  5024. if (!pvalCheckType(p, "pvalSwitchWalkCases", PV_SWITCH))
  5025. return 0;
  5026. if (!(*next_case))
  5027. *next_case = p->u2.statements;
  5028. else {
  5029. *next_case = (*next_case)->next;
  5030. }
  5031. return *next_case;
  5032. }
  5033. void pvalExtenSetName( pval *p, char *name )
  5034. {
  5035. if (!pvalCheckType(p, "pvalExtenSetName", PV_EXTENSION))
  5036. return;
  5037. p->u1.str = name;
  5038. }
  5039. char* pvalExtenGetName( pval *p )
  5040. {
  5041. if (!pvalCheckType(p, "pvalExtenGetName", PV_EXTENSION))
  5042. return 0;
  5043. return p->u1.str;
  5044. }
  5045. void pvalExtenSetRegexten( pval *p )
  5046. {
  5047. if (!pvalCheckType(p, "pvalExtenSetRegexten", PV_EXTENSION))
  5048. return;
  5049. p->u4.regexten = 1;
  5050. }
  5051. void pvalExtenUnSetRegexten( pval *p )
  5052. {
  5053. if (!pvalCheckType(p, "pvalExtenUnSetRegexten", PV_EXTENSION))
  5054. return;
  5055. p->u4.regexten = 0;
  5056. }
  5057. int pvalExtenGetRegexten( pval *p )
  5058. {
  5059. if (!pvalCheckType(p, "pvalExtenGetRegexten", PV_EXTENSION))
  5060. return 0;
  5061. return p->u4.regexten;
  5062. }
  5063. void pvalExtenSetHints( pval *p, char *hints )
  5064. {
  5065. if (!pvalCheckType(p, "pvalExtenSetHints", PV_EXTENSION))
  5066. return;
  5067. p->u3.hints = hints;
  5068. }
  5069. char* pvalExtenGetHints( pval *p )
  5070. {
  5071. if (!pvalCheckType(p, "pvalExtenGetHints", PV_EXTENSION))
  5072. return 0;
  5073. return p->u3.hints;
  5074. }
  5075. void pvalExtenSetStatement( pval *p, pval *statement )
  5076. {
  5077. if (!pvalCheckType(p, "pvalExtenSetStatement", PV_EXTENSION))
  5078. return;
  5079. p->u2.statements = statement;
  5080. }
  5081. pval* pvalExtenGetStatement( pval *p )
  5082. {
  5083. if (!pvalCheckType(p, "pvalExtenGetStatement", PV_EXTENSION))
  5084. return 0;
  5085. return p->u2.statements;
  5086. }
  5087. void pvalIgnorePatSetPattern( pval *p, char *pat )
  5088. {
  5089. if (!pvalCheckType(p, "pvalIgnorePatSetPattern", PV_IGNOREPAT))
  5090. return;
  5091. p->u1.str = pat;
  5092. }
  5093. char* pvalIgnorePatGetPattern( pval *p )
  5094. {
  5095. if (!pvalCheckType(p, "pvalIgnorePatGetPattern", PV_IGNOREPAT))
  5096. return 0;
  5097. return p->u1.str;
  5098. }
  5099. void pvalGlobalsAddStatement( pval *p, pval *statement )
  5100. {
  5101. if (p->type != PV_GLOBALS) {
  5102. ast_log(LOG_ERROR, "pvalGlobalsAddStatement called where first arg is not a Globals!\n");
  5103. } else {
  5104. if (!p->u1.statements) {
  5105. p->u1.statements = statement;
  5106. } else {
  5107. p->u1.statements = linku1(p->u1.statements,statement);
  5108. }
  5109. }
  5110. }
  5111. pval* pvalGlobalsWalkStatements( pval *p, pval **next_statement )
  5112. {
  5113. if (!pvalCheckType(p, "pvalGlobalsWalkStatements", PV_GLOBALS))
  5114. return 0;
  5115. if (!*next_statement) {
  5116. *next_statement = p;
  5117. return p;
  5118. } else {
  5119. *next_statement = (*next_statement)->next;
  5120. return (*next_statement)->next;
  5121. }
  5122. }
  5123. void pvalTopLevAddObject( pval *p, pval *contextOrObj )
  5124. {
  5125. if (p) {
  5126. linku1(p,contextOrObj);
  5127. } else {
  5128. ast_log(LOG_ERROR, "First arg to pvalTopLevel is NULL!\n");
  5129. }
  5130. }
  5131. pval *pvalTopLevWalkObjects(pval *p, pval **next_obj )
  5132. {
  5133. if (!*next_obj) {
  5134. *next_obj = p;
  5135. return p;
  5136. } else {
  5137. *next_obj = (*next_obj)->next;
  5138. return (*next_obj)->next;
  5139. }
  5140. }
  5141. /* append second element to the list in the first one via next pointers */
  5142. pval * linku1(pval *head, pval *tail)
  5143. {
  5144. if (!head)
  5145. return tail;
  5146. if (tail) {
  5147. if (!head->next) {
  5148. head->next = tail;
  5149. } else {
  5150. head->u1_last->next = tail;
  5151. }
  5152. head->u1_last = tail;
  5153. tail->prev = head; /* the dad link only points to containers */
  5154. }
  5155. return head;
  5156. }