pval.c 164 KB

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