ppScope.cxx 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436
  1. // Filename: ppScope.cxx
  2. // Created by: drose (25Sep00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "ppremake.h"
  6. #include "ppScope.h"
  7. #include "ppNamedScopes.h"
  8. #include "ppFilenamePattern.h"
  9. #include "ppDirectory.h"
  10. #include "ppSubroutine.h"
  11. #include "ppCommandFile.h"
  12. #include "ppDependableFile.h"
  13. #include "ppMain.h"
  14. #include "tokenize.h"
  15. #include "filename.h"
  16. #include "dSearchPath.h"
  17. #include "globPattern.h"
  18. #include "md5.h"
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #ifdef HAVE_SYS_TYPES_H
  23. #include <sys/types.h>
  24. #endif
  25. #ifdef HAVE_SYS_WAIT_H
  26. #include <sys/wait.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <algorithm>
  30. #include <ctype.h>
  31. #include <sys/stat.h>
  32. #include <stdio.h> // for perror() and sprintf().
  33. #include <errno.h>
  34. #include <signal.h>
  35. #include <assert.h>
  36. #ifdef WIN32_VC
  37. #include <windows.h> // for GetFileAttributes()
  38. #endif // WIN32_VC
  39. static const string variable_patsubst(VARIABLE_PATSUBST);
  40. PPScope::MapVariableDefinition PPScope::_null_map_def;
  41. PPScope::ScopeStack PPScope::_scope_stack;
  42. ////////////////////////////////////////////////////////////////////
  43. // Function: PPScope::Constructor
  44. // Access: Public
  45. // Description:
  46. ////////////////////////////////////////////////////////////////////
  47. PPScope::
  48. PPScope(PPNamedScopes *named_scopes) :
  49. _named_scopes(named_scopes)
  50. {
  51. _directory = (PPDirectory *)NULL;
  52. _parent_scope = (PPScope *)NULL;
  53. }
  54. ////////////////////////////////////////////////////////////////////
  55. // Function: PPScope::get_named_scopes
  56. // Access: Public
  57. // Description: Returns a pointer to the PPNamedScopes collection
  58. // associated with this scope. This pointer could be
  59. // NULL.
  60. ////////////////////////////////////////////////////////////////////
  61. PPNamedScopes *PPScope::
  62. get_named_scopes() {
  63. return _named_scopes;
  64. }
  65. ////////////////////////////////////////////////////////////////////
  66. // Function: PPScope::set_parent
  67. // Access: Public
  68. // Description: Sets a static parent scope to this scope. When a
  69. // variable reference is undefined in this scope, it
  70. // will search first up the static parent chain before
  71. // it searches the dynamic scope stack.
  72. ////////////////////////////////////////////////////////////////////
  73. void PPScope::
  74. set_parent(PPScope *parent) {
  75. _parent_scope = parent;
  76. }
  77. ////////////////////////////////////////////////////////////////////
  78. // Function: PPScope::get_parent
  79. // Access: Public
  80. // Description: Returns the static parent scope to this scope, if
  81. // any, or NULL if the static parent has not been set.
  82. // See set_parent().
  83. ////////////////////////////////////////////////////////////////////
  84. PPScope *PPScope::
  85. get_parent() {
  86. return _parent_scope;
  87. }
  88. ////////////////////////////////////////////////////////////////////
  89. // Function: PPScope::define_variable
  90. // Access: Public
  91. // Description: Makes a new variable definition. If the variable
  92. // does not already exist in this scope, a new variable
  93. // is created, possibly shadowing a variable declaration
  94. // in some parent scope.
  95. ////////////////////////////////////////////////////////////////////
  96. void PPScope::
  97. define_variable(const string &varname, const string &definition) {
  98. _variables[varname] = definition;
  99. }
  100. ////////////////////////////////////////////////////////////////////
  101. // Function: PPScope::set_variable
  102. // Access: Public
  103. // Description: Changes the definition of an already-existing
  104. // variable. The variable is changed in whichever scope
  105. // it is defined. Returns false if the variable has not
  106. // been defined.
  107. ////////////////////////////////////////////////////////////////////
  108. bool PPScope::
  109. set_variable(const string &varname, const string &definition) {
  110. if (p_set_variable(varname, definition)) {
  111. return true;
  112. }
  113. // Check the scopes on the stack for the variable definition.
  114. ScopeStack::reverse_iterator si;
  115. for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
  116. if ((*si)->p_set_variable(varname, definition)) {
  117. return true;
  118. }
  119. }
  120. // If the variable isn't defined, we check the environment.
  121. const char *env = getenv(varname.c_str());
  122. if (env != (const char *)NULL) {
  123. // It is defined in the environment; thus, it is implicitly
  124. // defined here at the global scope: the bottom of the stack.
  125. PPScope *bottom = this;
  126. if (!_scope_stack.empty()) {
  127. bottom = _scope_stack.front();
  128. }
  129. bottom->define_variable(varname, definition);
  130. return true;
  131. }
  132. // The variable isn't defined anywhere. Too bad.
  133. return false;
  134. }
  135. ////////////////////////////////////////////////////////////////////
  136. // Function: PPScope::define_map_variable
  137. // Access: Public
  138. // Description: Makes a new map variable definition. This defines a
  139. // new variable that can be used as a function to
  140. // retrieve variables from within a named scope, based
  141. // on a particular key variable.
  142. //
  143. // In this variant of define_map_variable(), the
  144. // definition is a string of the form
  145. // key_varname(scope_names).
  146. ////////////////////////////////////////////////////////////////////
  147. void PPScope::
  148. define_map_variable(const string &varname, const string &definition) {
  149. size_t p = definition.find(VARIABLE_OPEN_NESTED);
  150. if (p != string::npos && definition[definition.length() - 1] == VARIABLE_CLOSE_NESTED) {
  151. size_t q = definition.length() - 1;
  152. string scope_names = definition.substr(p + 1, q - (p + 1));
  153. string key_varname = definition.substr(0, p);
  154. define_map_variable(varname, key_varname, scope_names);
  155. } else {
  156. // No scoping; not really a map variable.
  157. define_map_variable(varname, definition, "");
  158. }
  159. }
  160. ////////////////////////////////////////////////////////////////////
  161. // Function: PPScope::define_map_variable
  162. // Access: Public
  163. // Description: Makes a new map variable definition. This defines a
  164. // new variable that can be used as a function to
  165. // retrieve variables from within a named scope, based
  166. // on a particular key variable.
  167. ////////////////////////////////////////////////////////////////////
  168. void PPScope::
  169. define_map_variable(const string &varname, const string &key_varname,
  170. const string &scope_names) {
  171. MapVariableDefinition &def = _map_variables[varname];
  172. def.clear();
  173. define_variable(varname, "");
  174. if (_named_scopes == (PPNamedScopes *)NULL) {
  175. return;
  176. }
  177. if (key_varname.empty()) {
  178. return;
  179. }
  180. vector<string> names;
  181. tokenize_whitespace(scope_names, names);
  182. // Get all of the named scopes.
  183. PPNamedScopes::Scopes scopes;
  184. vector<string>::const_iterator ni;
  185. for (ni = names.begin(); ni != names.end(); ++ni) {
  186. const string &name = (*ni);
  187. _named_scopes->get_scopes(name, scopes);
  188. }
  189. if (scopes.empty()) {
  190. return;
  191. }
  192. // Now go through the scopes and build up the results.
  193. vector<string> results;
  194. PPNamedScopes::Scopes::const_iterator si;
  195. for (si = scopes.begin(); si != scopes.end(); ++si) {
  196. PPScope *scope = (*si);
  197. string key_string = scope->expand_variable(key_varname);
  198. vector<string> keys;
  199. tokenize_whitespace(key_string, keys);
  200. if (!keys.empty()) {
  201. vector<string>::const_iterator ki;
  202. results.insert(results.end(), keys.begin(), keys.end());
  203. for (ki = keys.begin(); ki != keys.end(); ++ki) {
  204. def[*ki] = scope;
  205. }
  206. }
  207. }
  208. // Also define a traditional variable along with the map variable.
  209. define_variable(varname, repaste(results, " "));
  210. }
  211. ////////////////////////////////////////////////////////////////////
  212. // Function: PPScope::add_to_map_variable
  213. // Access: Public
  214. // Description: Adds a new key/scope pair to a previous map variable
  215. // definition.
  216. ////////////////////////////////////////////////////////////////////
  217. void PPScope::
  218. add_to_map_variable(const string &varname, const string &key,
  219. PPScope *scope) {
  220. MapVariableDefinition &def = find_map_variable(varname);
  221. if (&def == &_null_map_def) {
  222. cerr << "Warning: undefined map variable: " << varname << "\n";
  223. return;
  224. }
  225. def[key] = scope;
  226. // We need to do all this work to define the traditional expansion.
  227. // Maybe not a great idea.
  228. vector<string> results;
  229. MapVariableDefinition::const_iterator di;
  230. for (di = def.begin(); di != def.end(); ++di) {
  231. results.push_back((*di).first);
  232. }
  233. set_variable(varname, repaste(results, " "));
  234. }
  235. ////////////////////////////////////////////////////////////////////
  236. // Function: PPScope::define_formals
  237. // Access: Public
  238. // Description: Supplies values to a slew of variables at once,
  239. // typically to define actual values for a list of
  240. // formal parameters to a user-defined subroutine or
  241. // function.
  242. //
  243. // Formals is a vector of variable names to be defined,
  244. // and actuals is a comma-separated list of expressions
  245. // to be substituted in, one-per-one. The
  246. // subroutine_name is used only for error reporting.
  247. ////////////////////////////////////////////////////////////////////
  248. void PPScope::
  249. define_formals(const string &subroutine_name,
  250. const vector<string> &formals, const string &actuals) {
  251. vector<string> actual_words;
  252. tokenize_params(actuals, actual_words, true);
  253. if (actual_words.size() < formals.size()) {
  254. cerr << "Warning: not all parameters defined for " << subroutine_name
  255. << ": " << actuals << "\n";
  256. } else if (actual_words.size() > formals.size()) {
  257. cerr << "Warning: more parameters defined for " << subroutine_name
  258. << " than actually exist: " << actuals << "\n";
  259. }
  260. for (int i = 0; i < (int)formals.size(); i++) {
  261. if (i < (int)actual_words.size()) {
  262. define_variable(formals[i], actual_words[i]);
  263. } else {
  264. define_variable(formals[i], string());
  265. }
  266. }
  267. }
  268. ////////////////////////////////////////////////////////////////////
  269. // Function: PPScope::get_variable
  270. // Access: Public
  271. // Description: Returns the variable definition associated with the
  272. // indicated variable name.
  273. ////////////////////////////////////////////////////////////////////
  274. string PPScope::
  275. get_variable(const string &varname) {
  276. // Is it a user-defined function?
  277. const PPSubroutine *sub = PPSubroutine::get_func(varname);
  278. if (sub != (const PPSubroutine *)NULL) {
  279. return expand_function(varname, sub, string());
  280. }
  281. // cerr << "getvar arg is: '" << varname << "'" << endl;
  282. string result;
  283. if (p_get_variable(varname, result)) {
  284. return result;
  285. }
  286. // Check the scopes on the stack for the variable definition.
  287. ScopeStack::reverse_iterator si;
  288. for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
  289. if ((*si)->p_get_variable(varname, result)) {
  290. return result;
  291. }
  292. }
  293. // If the variable isn't defined, we check the environment.
  294. const char *env = getenv(varname.c_str());
  295. if (env != (const char *)NULL) {
  296. return env;
  297. }
  298. // It's not defined anywhere, so it's implicitly empty.
  299. return string();
  300. }
  301. ////////////////////////////////////////////////////////////////////
  302. // Function: PPScope::expand_defined
  303. // Access: Private
  304. // Description: Expands the "defined" function variable. Code mimics get_variable()
  305. ////////////////////////////////////////////////////////////////////
  306. string PPScope::
  307. expand_defined(const string &params) {
  308. // Split the string up into tokens based on the commas.
  309. vector<string> tokens;
  310. tokenize_params(params, tokens, true);
  311. if (tokens.size() != 1) {
  312. cerr << "error: defined requires one parameter.\n";
  313. errors_occurred = true;
  314. return string();
  315. }
  316. string varname = tokens[0];
  317. string falsestr;
  318. string truestr = "1";
  319. // Is it a user-defined function?
  320. const PPSubroutine *sub = PPSubroutine::get_func(varname);
  321. string nullstr;
  322. if (sub != (const PPSubroutine *)NULL) {
  323. if(nullstr != expand_function(varname, sub, string())) {
  324. return truestr;
  325. }
  326. }
  327. string result;
  328. if (p_get_variable(varname, result)) {
  329. return truestr;
  330. }
  331. // Check the scopes on the stack for the variable definition.
  332. ScopeStack::reverse_iterator si;
  333. for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
  334. if ((*si)->p_get_variable(varname, result)) {
  335. return truestr;
  336. }
  337. }
  338. // If the variable isn't defined, we check the environment.
  339. const char *env = getenv(varname.c_str());
  340. if (env != (const char *)NULL) {
  341. return truestr;
  342. }
  343. // It's not defined anywhere, so it's implicitly empty.
  344. return falsestr;
  345. }
  346. ////////////////////////////////////////////////////////////////////
  347. // Function: PPScope::expand_variable
  348. // Access: Public
  349. // Description: Similar to get_variable(), except the variable
  350. // definition is in turn expanded.
  351. ////////////////////////////////////////////////////////////////////
  352. string PPScope::
  353. expand_variable(const string &varname) {
  354. return expand_string(get_variable(varname));
  355. }
  356. ////////////////////////////////////////////////////////////////////
  357. // Function: PPScope::find_map_variable
  358. // Access: Public
  359. // Description: Looks for the map variable definition in this scope
  360. // or some ancestor scope. Returns the map variable
  361. // definition if it is found, or _null_map_def if it is
  362. // not.
  363. ////////////////////////////////////////////////////////////////////
  364. PPScope::MapVariableDefinition &PPScope::
  365. find_map_variable(const string &varname) {
  366. MapVariableDefinition &def = p_find_map_variable(varname);
  367. if (&def != &_null_map_def) {
  368. return def;
  369. }
  370. // No such map variable. Check the stack.
  371. ScopeStack::reverse_iterator si;
  372. for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
  373. MapVariableDefinition &def = (*si)->p_find_map_variable(varname);
  374. if (&def != &_null_map_def) {
  375. return def;
  376. }
  377. }
  378. // Nada.
  379. return _null_map_def;
  380. }
  381. ////////////////////////////////////////////////////////////////////
  382. // Function: PPScope::get_directory
  383. // Access: Public
  384. // Description: Returns the directory level associated with this
  385. // scope, if any, or with the nearest parent to this
  386. // scope.
  387. ////////////////////////////////////////////////////////////////////
  388. PPDirectory *PPScope::
  389. get_directory() {
  390. if (_directory != (PPDirectory *)NULL) {
  391. return _directory;
  392. }
  393. // Check the stack.
  394. ScopeStack::reverse_iterator si;
  395. for (si = _scope_stack.rbegin(); si != _scope_stack.rend(); ++si) {
  396. if ((*si)->_directory != (PPDirectory *)NULL) {
  397. return (*si)->_directory;
  398. }
  399. }
  400. return (PPDirectory *)NULL;
  401. }
  402. ////////////////////////////////////////////////////////////////////
  403. // Function: PPScope::set_directory
  404. // Access: Public
  405. // Description: Associates this scope with the indicated directory
  406. // level. Typically this is done when definition a
  407. // scope for a particular source file which exists at a
  408. // known directory level.
  409. ////////////////////////////////////////////////////////////////////
  410. void PPScope::
  411. set_directory(PPDirectory *directory) {
  412. _directory = directory;
  413. }
  414. ////////////////////////////////////////////////////////////////////
  415. // Function: PPScope::expand_string
  416. // Access: Public
  417. // Description: Expands out all the variable references in the given
  418. // string. Variables are expanded recursively; that is,
  419. // if a variable expansion includes a reference to
  420. // another variable name, the second variable name is
  421. // expanded. However, cyclical references are not
  422. // expanded.
  423. ////////////////////////////////////////////////////////////////////
  424. string PPScope::
  425. expand_string(const string &str) {
  426. string result = r_expand_string(str, (ExpandedVariable *)NULL);
  427. if (debug_expansions > 0 && str != result) {
  428. // Look for the str in our table--how many times has this
  429. // particular string been expanded?
  430. ExpandResultCount &result_count = debug_expand[str];
  431. // Then, how many times has it expanded to this same result?
  432. // First, assuming this is the first time it has expanded to this
  433. // result, try to insert the result string with an initial count
  434. // of 1.
  435. pair<ExpandResultCount::iterator, bool> r =
  436. result_count.insert(ExpandResultCount::value_type(result, 1));
  437. if (!r.second) {
  438. // If the result string was not successfully inserted into the
  439. // map, it was already there--so increment the count.
  440. ExpandResultCount::iterator rci = r.first;
  441. (*rci).second++;
  442. }
  443. }
  444. return result;
  445. }
  446. ////////////////////////////////////////////////////////////////////
  447. // Function: PPScope::expand_self_reference
  448. // Access: Public
  449. // Description: Similar to expand_string(), except that only simple
  450. // references to the named variable are expanded--other
  451. // variable references are left unchanged. This allows
  452. // us to define a variable in terms of its previous
  453. // definition.
  454. ////////////////////////////////////////////////////////////////////
  455. string PPScope::
  456. expand_self_reference(const string &str, const string &varname) {
  457. // Look for a simple reference to the named variable. A more
  458. // complex reference, like a computed variable name or something
  459. // equally loopy, won't work with this simple test. Too bad.
  460. string reference;
  461. reference += VARIABLE_PREFIX;
  462. reference += VARIABLE_OPEN_BRACE;
  463. reference += varname;
  464. reference += VARIABLE_CLOSE_BRACE;
  465. string result;
  466. size_t p = 0;
  467. size_t q = str.find(reference, p);
  468. while (q != string::npos) {
  469. result += str.substr(p, q - p);
  470. p = q;
  471. result += r_expand_variable(str, p, (ExpandedVariable *)NULL);
  472. q = str.find(reference, p);
  473. }
  474. result += str.substr(p);
  475. return result;
  476. }
  477. ////////////////////////////////////////////////////////////////////
  478. // Function: PPScope::push_scope
  479. // Access: Public, Static
  480. // Description: Pushes the indicated scope onto the top of the stack.
  481. // When a variable reference is unresolved in the
  482. // current scope, the scope stack is searched, in LIFO
  483. // order.
  484. ////////////////////////////////////////////////////////////////////
  485. void PPScope::
  486. push_scope(PPScope *scope) {
  487. _scope_stack.push_back(scope);
  488. }
  489. ////////////////////////////////////////////////////////////////////
  490. // Function: PPScope::pop_scope
  491. // Access: Public, Static
  492. // Description: Pops another level off the top of the stack. See
  493. // push_scope().
  494. ////////////////////////////////////////////////////////////////////
  495. PPScope *PPScope::
  496. pop_scope() {
  497. assert(!_scope_stack.empty());
  498. PPScope *back = _scope_stack.back();
  499. _scope_stack.pop_back();
  500. return back;
  501. }
  502. ////////////////////////////////////////////////////////////////////
  503. // Function: PPScope::get_bottom_scope
  504. // Access: Public, Static
  505. // Description: Returns the scope on the bottom of the stack. This
  506. // was the very first scope ever pushed, e.g. the global
  507. // scope.
  508. ////////////////////////////////////////////////////////////////////
  509. PPScope *PPScope::
  510. get_bottom_scope() {
  511. assert(!_scope_stack.empty());
  512. return _scope_stack.front();
  513. }
  514. ////////////////////////////////////////////////////////////////////
  515. // Function: PPScope::get_enclosing_scope
  516. // Access: Public, Static
  517. // Description: Returns the scope n below the top of the stack, or
  518. // the bottom scope if the stack has exactly n or fewer
  519. // scopes.
  520. //
  521. // This will be the scope associated with the nth
  522. // enclosing syntax in the source file.
  523. ////////////////////////////////////////////////////////////////////
  524. PPScope *PPScope::
  525. get_enclosing_scope(int n) {
  526. assert(n >= 0);
  527. if (n >= _scope_stack.size()) {
  528. return get_bottom_scope();
  529. }
  530. return _scope_stack[_scope_stack.size() - 1 - n];
  531. }
  532. ////////////////////////////////////////////////////////////////////
  533. // Function: PPScope::tokenize_params
  534. // Access: Public
  535. // Description: Separates a string into tokens based on comma
  536. // delimiters, e.g. for parameters to a function.
  537. // Nested variable references are skipped correctly,
  538. // even if they include commas. Leading and trailing
  539. // whitespace in each token is automatically stripped.
  540. //
  541. // If expand is true, the nested variables are
  542. // automatically expanded as the string is tokenized;
  543. // otherwise, they are left unexpanded.
  544. ////////////////////////////////////////////////////////////////////
  545. void PPScope::
  546. tokenize_params(const string &str, vector<string> &tokens,
  547. bool expand) {
  548. size_t p = 0;
  549. while (p < str.length()) {
  550. // Skip initial whitespace.
  551. while (p < str.length() && isspace(str[p])) {
  552. p++;
  553. }
  554. string token;
  555. while (p < str.length() && str[p] != FUNCTION_PARAMETER_SEPARATOR) {
  556. if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
  557. str[p + 1] == VARIABLE_OPEN_BRACE) {
  558. // Skip a nested variable reference.
  559. if (expand) {
  560. token += r_expand_variable(str, p, (ExpandedVariable *)NULL);
  561. } else {
  562. token += r_scan_variable(str, p);
  563. }
  564. } else {
  565. token += str[p];
  566. p++;
  567. }
  568. }
  569. // Back up past trailing whitespace.
  570. size_t q = token.length();
  571. while (q > 0 && isspace(token[q - 1])) {
  572. q--;
  573. }
  574. tokens.push_back(token.substr(0, q));
  575. p++;
  576. if (p == str.length()) {
  577. // In this case, we have just read past a trailing comma symbol
  578. // at the end of the string, so we have one more empty token.
  579. tokens.push_back(string());
  580. }
  581. }
  582. }
  583. ////////////////////////////////////////////////////////////////////
  584. // Function: PPScope::tokenize_numeric_pair
  585. // Access: Public
  586. // Description: This function is used by all the numeric comparision
  587. // functions, e.g. nne, nlt, etc. It splits the string
  588. // up into two parameters based on commas, and evaluates
  589. // each parameter as a number, into a and b. It returns
  590. // true if successful, or false if there was some user
  591. // error.
  592. ////////////////////////////////////////////////////////////////////
  593. bool PPScope::
  594. tokenize_numeric_pair(const string &str, double &a, double &b) {
  595. vector<string> words;
  596. tokenize_params(str, words, true);
  597. if (words.size() != 2) {
  598. cerr << words.size() << " parameters supplied when two were expected:\n"
  599. << str << "\n";
  600. errors_occurred = true;
  601. return false;
  602. }
  603. double results[2];
  604. for (int i = 0; i < 2; i++) {
  605. const char *param = words[i].c_str();
  606. char *n;
  607. results[i] = strtod(param, &n);
  608. if (*n != '\0') {
  609. // strtod failed--not a numeric representation.
  610. cerr << "Warning: " << words[i] << " is not a number.\n";
  611. if (n == param) {
  612. results[i] = 0.0;
  613. }
  614. }
  615. }
  616. a = results[0];
  617. b = results[1];
  618. return true;
  619. }
  620. ////////////////////////////////////////////////////////////////////
  621. // Function: PPScope::tokenize_ints
  622. // Access: Public
  623. // Description: This function is used by the arithmetic functions +,
  624. // -, etc. It separates the string into parameters
  625. // based on the comma, interprets each parameter as an
  626. // integer, and fills up the indicated vector.
  627. ////////////////////////////////////////////////////////////////////
  628. bool PPScope::
  629. tokenize_ints(const string &str, vector<int> &tokens) {
  630. vector<string> words;
  631. tokenize_params(str, words, true);
  632. vector<string>::const_iterator wi;
  633. for (wi = words.begin(); wi != words.end(); ++wi) {
  634. const char *param = (*wi).c_str();
  635. char *n;
  636. int result = strtol(param, &n, 0);
  637. if (*n != '\0') {
  638. // strtol failed--not an integer.
  639. cerr << "Warning: " << param << " is not an integer.\n";
  640. if (n == param) {
  641. result = 0;
  642. }
  643. }
  644. tokens.push_back(result);
  645. }
  646. return true;
  647. }
  648. ////////////////////////////////////////////////////////////////////
  649. // Function: PPScope::scan_to_whitespace
  650. // Access: Public
  651. // Description: Scans to the end of the first whitespace-delimited
  652. // word in the indicated string, even if it includes a
  653. // nested variable reference (which is itself allowed to
  654. // contain whitespace).
  655. //
  656. // On input, str is a string, and start is the starting
  657. // position within the string of the scan; it should
  658. // point to a non-whitespace character.
  659. //
  660. // The return value is the position within the string of
  661. // the first whitespace character encountered at its
  662. // original position or later, that is not part of a
  663. // variable reference. All variable references are left
  664. // unexpanded.
  665. ////////////////////////////////////////////////////////////////////
  666. size_t PPScope::
  667. scan_to_whitespace(const string &str, size_t start) {
  668. size_t p = start;
  669. while (p < str.length() && !isspace(str[p])) {
  670. string token;
  671. if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
  672. str[p + 1] == VARIABLE_OPEN_BRACE) {
  673. // Skip a nested variable reference.
  674. r_scan_variable(str, p);
  675. } else {
  676. p++;
  677. }
  678. }
  679. return p;
  680. }
  681. ////////////////////////////////////////////////////////////////////
  682. // Function: PPScope::format_int
  683. // Access: Private, Static
  684. // Description: Formats the indicated integer as a string and returns
  685. // the string.
  686. ////////////////////////////////////////////////////////////////////
  687. string PPScope::
  688. format_int(int num) {
  689. char buffer[32];
  690. sprintf(buffer, "%d", num);
  691. return buffer;
  692. }
  693. ////////////////////////////////////////////////////////////////////
  694. // Function: PPScope::p_set_variable
  695. // Access: Private
  696. // Description: The private implementation of p_set_variable.
  697. // Returns true if the variable's definition is found
  698. // and set, false otherwise.
  699. ////////////////////////////////////////////////////////////////////
  700. bool PPScope::
  701. p_set_variable(const string &varname, const string &definition) {
  702. Variables::iterator vi;
  703. vi = _variables.find(varname);
  704. if (vi != _variables.end()) {
  705. (*vi).second = definition;
  706. return true;
  707. }
  708. if (_parent_scope != (PPScope *)NULL) {
  709. return _parent_scope->p_set_variable(varname, definition);
  710. }
  711. return false;
  712. }
  713. ////////////////////////////////////////////////////////////////////
  714. // Function: PPScope::p_get_variable
  715. // Access: Private
  716. // Description: The private implementation of get_variable(). This
  717. // checks the local scope only; it does not check the
  718. // stack. It returns true if the variable is defined,
  719. // false otherwise..
  720. ////////////////////////////////////////////////////////////////////
  721. bool PPScope::
  722. p_get_variable(const string &varname, string &result) {
  723. Variables::const_iterator vi;
  724. vi = _variables.find(varname);
  725. if (vi != _variables.end()) {
  726. result = (*vi).second;
  727. return true;
  728. }
  729. if (varname == "RELDIR" &&
  730. _directory != (PPDirectory *)NULL &&
  731. current_output_directory != (PPDirectory *)NULL) {
  732. // $[RELDIR] is a special variable name that evaluates to the
  733. // relative directory of the current scope to the current output
  734. // directory.
  735. result = current_output_directory->get_rel_to(_directory);
  736. return true;
  737. }
  738. if (varname == "DEPENDS_INDEX" &&
  739. _directory != (PPDirectory *)NULL) {
  740. // $[DEPENDS_INDEX] is another special variable name that
  741. // evaluates to the numeric sorting index assigned to this
  742. // directory based on its dependency relationship with other
  743. // directories. It's useful primarily for debugging.
  744. char buffer[32];
  745. sprintf(buffer, "%d", _directory->get_depends_index());
  746. result = buffer;
  747. return true;
  748. }
  749. if (_parent_scope != (PPScope *)NULL) {
  750. return _parent_scope->p_get_variable(varname, result);
  751. }
  752. return false;
  753. }
  754. ////////////////////////////////////////////////////////////////////
  755. // Function: PPScope::r_expand_string
  756. // Access: Private
  757. // Description: The recursive implementation of expand_string().
  758. // This function detects cycles in the variable
  759. // expansion by storing the set of variable names that
  760. // have thus far been expanded in the linked list.
  761. ////////////////////////////////////////////////////////////////////
  762. string PPScope::
  763. r_expand_string(const string &str, PPScope::ExpandedVariable *expanded) {
  764. string result;
  765. // Search for a variable reference.
  766. size_t p = 0;
  767. while (p < str.length()) {
  768. if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
  769. str[p + 1] == VARIABLE_OPEN_BRACE) {
  770. // Here's a nested variable! Expand it fully.
  771. result += r_expand_variable(str, p, expanded);
  772. } else {
  773. result += str[p];
  774. p++;
  775. }
  776. }
  777. return result;
  778. }
  779. ////////////////////////////////////////////////////////////////////
  780. // Function: PPScope::r_scan_variable
  781. // Access: Private
  782. // Description: Scans past a single variable reference without
  783. // expanding it. On input, str is a string containing a
  784. // variable reference (among other stuff), and vp is the
  785. // position within the string of the prefix character at
  786. // the beginning of the variable reference.
  787. //
  788. // On output, vp is set to the position within the
  789. // string of the first character after the variable
  790. // reference's closing bracket. The variable reference
  791. // itself is returned.
  792. ////////////////////////////////////////////////////////////////////
  793. string PPScope::
  794. r_scan_variable(const string &str, size_t &vp) {
  795. // Search for the end of the variable name: an unmatched square
  796. // bracket.
  797. size_t start = vp;
  798. size_t p = vp + 2;
  799. while (p < str.length() && str[p] != VARIABLE_CLOSE_BRACE) {
  800. if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
  801. str[p + 1] == VARIABLE_OPEN_BRACE) {
  802. // Here's a nested variable! Scan past it, matching braces
  803. // properly.
  804. r_scan_variable(str, p);
  805. } else {
  806. p++;
  807. }
  808. }
  809. if (p < str.length()) {
  810. assert(str[p] == VARIABLE_CLOSE_BRACE);
  811. p++;
  812. } else {
  813. cerr << "Warning! Unclosed variable reference:\n"
  814. << str.substr(vp) << "\n";
  815. }
  816. vp = p;
  817. return str.substr(start, vp - start);
  818. }
  819. ////////////////////////////////////////////////////////////////////
  820. // Function: PPScope::r_expand_variable
  821. // Access: Private
  822. // Description: Expands a single variable reference. On input, str
  823. // is a string containing a variable reference (among
  824. // other stuff), and vp is the position within the
  825. // string of the prefix character at the beginning of
  826. // the variable reference.
  827. //
  828. // On output, vp is set to the position within the
  829. // string of the first character after the variable
  830. // reference's closing bracket, and the string expansion
  831. // of the variable reference is returned.
  832. ////////////////////////////////////////////////////////////////////
  833. string PPScope::
  834. r_expand_variable(const string &str, size_t &vp,
  835. PPScope::ExpandedVariable *expanded) {
  836. string varname;
  837. size_t whitespace_at = 0;
  838. size_t open_nested_at = 0;
  839. // Search for the end of the variable name: an unmatched square
  840. // bracket.
  841. size_t p = vp + 2;
  842. while (p < str.length() && str[p] != VARIABLE_CLOSE_BRACE) {
  843. if (p + 1 < str.length() && str[p] == VARIABLE_PREFIX &&
  844. str[p + 1] == VARIABLE_OPEN_BRACE) {
  845. if (whitespace_at != 0) {
  846. // Once we have encountered whitespace, we don't expand
  847. // variables inline anymore. These are now function
  848. // parameters, and might need to be expanded in some other
  849. // scope.
  850. varname += r_scan_variable(str, p);
  851. } else {
  852. varname += r_expand_variable(str, p, expanded);
  853. }
  854. } else {
  855. if (open_nested_at == 0 && str[p] == VARIABLE_OPEN_NESTED) {
  856. open_nested_at = p - (vp + 2);
  857. }
  858. if (open_nested_at == 0 && whitespace_at == 0 && isspace(str[p])) {
  859. whitespace_at = p - (vp + 2);
  860. }
  861. varname += str[p];
  862. p++;
  863. }
  864. }
  865. if (p < str.length()) {
  866. assert(str[p] == VARIABLE_CLOSE_BRACE);
  867. p++;
  868. } else {
  869. cerr << "Warning! Unclosed variable reference:\n"
  870. << str.substr(vp) << "\n";
  871. }
  872. vp = p;
  873. // Check for a function expansion.
  874. if (whitespace_at != 0) {
  875. string funcname = varname.substr(0, whitespace_at);
  876. p = whitespace_at;
  877. while (p < varname.length() && isspace(varname[p])) {
  878. p++;
  879. }
  880. string params = varname.substr(p);
  881. // Is it a user-defined function?
  882. const PPSubroutine *sub = PPSubroutine::get_func(funcname);
  883. if (sub != (const PPSubroutine *)NULL) {
  884. return expand_function(funcname, sub, params);
  885. }
  886. // Is it a built-in function?
  887. if (funcname == "isfullpath") {
  888. return expand_isfullpath(params);
  889. } else if (funcname == "osfilename") {
  890. return expand_osfilename(params);
  891. } else if (funcname == "unixfilename") {
  892. return expand_unixfilename(params);
  893. } else if (funcname == "cygpath_w") {
  894. // This maps to osfilename for historical reasons.
  895. return expand_osfilename(params);
  896. } else if (funcname == "cygpath_p") {
  897. // This maps to unixfilename for historical reasons.
  898. return expand_unixfilename(params);
  899. } else if (funcname == "wildcard") {
  900. return expand_wildcard(params);
  901. } else if (funcname == "isdir") {
  902. return expand_isdir(params);
  903. } else if (funcname == "isfile") {
  904. return expand_isfile(params);
  905. } else if (funcname == "libtest") {
  906. return expand_libtest(params);
  907. } else if (funcname == "bintest") {
  908. return expand_bintest(params);
  909. } else if (funcname == "shell") {
  910. return expand_shell(params);
  911. } else if (funcname == "standardize") {
  912. return expand_standardize(params);
  913. } else if (funcname == "canonical") {
  914. return expand_canonical(params);
  915. } else if (funcname == "length") {
  916. return expand_length(params);
  917. } else if (funcname == "substr") {
  918. return expand_substr(params);
  919. } else if (funcname == "findstring") {
  920. return expand_findstring(params);
  921. } else if (funcname == "dir") {
  922. return expand_dir(params);
  923. } else if (funcname == "notdir") {
  924. return expand_notdir(params);
  925. } else if (funcname == "suffix") {
  926. return expand_suffix(params);
  927. } else if (funcname == "basename") {
  928. return expand_basename(params);
  929. } else if (funcname == "makeguid") {
  930. return expand_makeguid(params);
  931. } else if (funcname == "word") {
  932. return expand_word(params);
  933. } else if (funcname == "wordlist") {
  934. return expand_wordlist(params);
  935. } else if (funcname == "words") {
  936. return expand_words(params);
  937. } else if (funcname == "firstword") {
  938. return expand_firstword(params);
  939. } else if (funcname == "patsubst") {
  940. return expand_patsubst(params, true);
  941. } else if (funcname == "patsubstw") {
  942. return expand_patsubst(params, false);
  943. } else if (funcname == "subst") {
  944. return expand_subst(params);
  945. } else if (funcname == "wordsubst") {
  946. return expand_wordsubst(params);
  947. } else if (funcname == "filter") {
  948. return expand_filter(params);
  949. } else if (funcname == "filter_out" || funcname == "filter-out") {
  950. return expand_filter_out(params);
  951. } else if (funcname == "join") {
  952. return expand_join(params);
  953. } else if (funcname == "sort") {
  954. return expand_sort(params);
  955. } else if (funcname == "unique") {
  956. return expand_unique(params);
  957. } else if (funcname == "matrix") {
  958. return expand_matrix(params);
  959. } else if (funcname == "if") {
  960. return expand_if(params);
  961. } else if (funcname == "eq") {
  962. return expand_eq(params);
  963. } else if (funcname == "defined") {
  964. return expand_defined(params);
  965. } else if (funcname == "ne") {
  966. return expand_ne(params);
  967. } else if (funcname == "=" || funcname == "==") {
  968. return expand_eqn(params);
  969. } else if (funcname == "!=") {
  970. return expand_nen(params);
  971. } else if (funcname == "<") {
  972. return expand_ltn(params);
  973. } else if (funcname == "<=") {
  974. return expand_len(params);
  975. } else if (funcname == ">") {
  976. return expand_gtn(params);
  977. } else if (funcname == ">=") {
  978. return expand_gen(params);
  979. } else if (funcname == "+") {
  980. return expand_plus(params);
  981. } else if (funcname == "-") {
  982. return expand_minus(params);
  983. } else if (funcname == "*") {
  984. return expand_times(params);
  985. } else if (funcname == "/") {
  986. return expand_divide(params);
  987. } else if (funcname == "%") {
  988. return expand_modulo(params);
  989. } else if (funcname == "not") {
  990. return expand_not(params);
  991. } else if (funcname == "or") {
  992. return expand_or(params);
  993. } else if (funcname == "and") {
  994. return expand_and(params);
  995. } else if (funcname == "upcase") {
  996. return expand_upcase(params);
  997. } else if (funcname == "downcase") {
  998. return expand_downcase(params);
  999. } else if (funcname == "cdefine") {
  1000. return expand_cdefine(params);
  1001. } else if (funcname == "closure") {
  1002. return expand_closure(params);
  1003. } else if (funcname == "unmapped") {
  1004. return expand_unmapped(params);
  1005. } else if (funcname == "dependencies") {
  1006. return expand_dependencies(params);
  1007. } else if (funcname == "foreach") {
  1008. return expand_foreach(params);
  1009. } else if (funcname == "forscopes") {
  1010. return expand_forscopes(params);
  1011. }
  1012. // It must be a map variable.
  1013. return expand_map_variable(funcname, params);
  1014. }
  1015. // Now we have the variable name; was it previously expanded?
  1016. ExpandedVariable *ev;
  1017. for (ev = expanded; ev != (ExpandedVariable *)NULL; ev = ev->_next) {
  1018. if (ev->_varname == varname) {
  1019. // Yes, this is a cyclical expansion.
  1020. cerr << "Ignoring cyclical expansion of " << varname << "\n";
  1021. return string();
  1022. }
  1023. }
  1024. // And now expand the variable.
  1025. string expansion;
  1026. // Check for a special inline patsubst operation, like GNU make:
  1027. // $[varname:%.c=%.o]
  1028. string patsubst;
  1029. bool got_patsubst = false;
  1030. p = varname.find(variable_patsubst);
  1031. if (p != string::npos) {
  1032. got_patsubst = true;
  1033. patsubst = varname.substr(p + variable_patsubst.length());
  1034. varname = varname.substr(0, p);
  1035. }
  1036. // Check for special scoping operators in the variable name.
  1037. p = varname.find(VARIABLE_OPEN_NESTED);
  1038. if (p != string::npos && varname[varname.length() - 1] == VARIABLE_CLOSE_NESTED) {
  1039. size_t q = varname.length() - 1;
  1040. string scope_names = varname.substr(p + 1, q - (p + 1));
  1041. varname = varname.substr(0, p);
  1042. expansion = expand_variable_nested(varname, scope_names);
  1043. } else {
  1044. // No special scoping; just expand the variable name.
  1045. expansion = get_variable(varname);
  1046. }
  1047. // Finally, recursively expand any variable references in the
  1048. // variable's expansion.
  1049. ExpandedVariable new_var;
  1050. new_var._varname = varname;
  1051. new_var._next = expanded;
  1052. string result = r_expand_string(expansion, &new_var);
  1053. // And *then* apply any inline patsubst.
  1054. if (got_patsubst) {
  1055. vector<string> tokens;
  1056. tokenize(patsubst, tokens, VARIABLE_PATSUBST_DELIM);
  1057. if (tokens.size() != 2) {
  1058. cerr << "inline patsubst should be of the form "
  1059. << VARIABLE_PREFIX << VARIABLE_OPEN_BRACE << "varname"
  1060. << VARIABLE_PATSUBST << PATTERN_WILDCARD << ".c"
  1061. << VARIABLE_PATSUBST_DELIM << PATTERN_WILDCARD << ".o"
  1062. << VARIABLE_CLOSE_BRACE << ".\n";
  1063. errors_occurred = true;
  1064. } else {
  1065. PPFilenamePattern from(tokens[0]);
  1066. PPFilenamePattern to(tokens[1]);
  1067. if (!from.has_wildcard() || !to.has_wildcard()) {
  1068. cerr << "The two parameters of inline patsubst must both include "
  1069. << PATTERN_WILDCARD << ".\n";
  1070. errors_occurred = true;
  1071. return string();
  1072. }
  1073. // Split the expansion into tokens based on the spaces.
  1074. vector<string> words;
  1075. tokenize_whitespace(result, words);
  1076. vector<string>::iterator wi;
  1077. for (wi = words.begin(); wi != words.end(); ++wi) {
  1078. (*wi) = to.transform(*wi, from);
  1079. }
  1080. result = repaste(words, " ");
  1081. }
  1082. }
  1083. return result;
  1084. }
  1085. ////////////////////////////////////////////////////////////////////
  1086. // Function: PPScope::expand_variable_nested
  1087. // Access: Private
  1088. // Description: Expands a variable reference of the form
  1089. // $[varname(scope scope scope)]. This means to
  1090. // concatenate the expansions of the variable in all of
  1091. // the named scopes.
  1092. ////////////////////////////////////////////////////////////////////
  1093. string PPScope::
  1094. expand_variable_nested(const string &varname,
  1095. const string &scope_names) {
  1096. if (_named_scopes == (PPNamedScopes *)NULL) {
  1097. return string();
  1098. }
  1099. vector<string> names;
  1100. tokenize_whitespace(scope_names, names);
  1101. // Get all of the named scopes.
  1102. PPNamedScopes::Scopes scopes;
  1103. vector<string>::const_iterator ni;
  1104. for (ni = names.begin(); ni != names.end(); ++ni) {
  1105. const string &name = (*ni);
  1106. _named_scopes->get_scopes(name, scopes);
  1107. }
  1108. if (scopes.empty()) {
  1109. return string();
  1110. }
  1111. // Now go through the scopes and build up the results.
  1112. vector<string> results;
  1113. PPNamedScopes::Scopes::const_iterator si;
  1114. for (si = scopes.begin(); si != scopes.end(); ++si) {
  1115. PPScope *scope = (*si);
  1116. string nested = scope->expand_variable(varname);
  1117. if (!nested.empty()) {
  1118. results.push_back(nested);
  1119. }
  1120. }
  1121. string result = repaste(results, " ");
  1122. return result;
  1123. }
  1124. ////////////////////////////////////////////////////////////////////
  1125. // Function: PPScope::expand_isfullpath
  1126. // Access: Private
  1127. // Description: Expands the "isfullpath" function variable. This
  1128. // returns true (actually, the same as its input) if the
  1129. // input parameter is a fully-specified path name,
  1130. // meaning it begins with a slash for unix_platform, and
  1131. // it begins with a slash or backslash, with an optional
  1132. // drive leterr, for windows_platform.
  1133. ////////////////////////////////////////////////////////////////////
  1134. string PPScope::
  1135. expand_isfullpath(const string &params) {
  1136. Filename filename = trim_blanks(expand_string(params));
  1137. string result;
  1138. if (filename.is_fully_qualified()) {
  1139. result = filename;
  1140. }
  1141. return result;
  1142. }
  1143. ////////////////////////////////////////////////////////////////////
  1144. // Function: PPScope::expand_osfilename
  1145. // Access: Private
  1146. // Description: Expands the "osfilename" function variable. This
  1147. // converts the filename from a Unix-style filename
  1148. // (e.g. with slash separators) to a platform-specific
  1149. // filename.
  1150. //
  1151. // This follows the same rules of Panda filename
  1152. // conversion; i.e. forward slashes become backslashes,
  1153. // and $PANDA_ROOT prefixes full pathnames, unless the
  1154. // topmost directory name is a single letter.
  1155. ////////////////////////////////////////////////////////////////////
  1156. string PPScope::
  1157. expand_osfilename(const string &params) {
  1158. // Split the parameter into tokens based on the spaces.
  1159. vector<string> words;
  1160. tokenize_whitespace(expand_string(params), words);
  1161. vector<string>::iterator wi;
  1162. for (wi = words.begin(); wi != words.end(); ++wi) {
  1163. Filename filename = (*wi);
  1164. (*wi) = filename.to_os_specific();
  1165. }
  1166. string result = repaste(words, " ");
  1167. return result;
  1168. }
  1169. ////////////////////////////////////////////////////////////////////
  1170. // Function: PPScope::expand_unixfilename
  1171. // Access: Private
  1172. // Description: Expands the "unixfilename" function variable. This
  1173. // converts the filename from a platform-specific
  1174. // filename to a Unix-style filename (e.g. with slash
  1175. // separators).
  1176. //
  1177. // This follows the rules of Panda filename conversion.
  1178. ////////////////////////////////////////////////////////////////////
  1179. string PPScope::
  1180. expand_unixfilename(const string &params) {
  1181. // Split the parameter into tokens based on the spaces.
  1182. vector<string> words;
  1183. tokenize_whitespace(expand_string(params), words);
  1184. vector<string>::iterator wi;
  1185. for (wi = words.begin(); wi != words.end(); ++wi) {
  1186. Filename filename = Filename::from_os_specific(*wi);
  1187. (*wi) = filename;
  1188. }
  1189. string result = repaste(words, " ");
  1190. return result;
  1191. }
  1192. ////////////////////////////////////////////////////////////////////
  1193. // Function: PPScope::expand_wildcard
  1194. // Access: Private
  1195. // Description: Expands the "wildcard" function variable. This
  1196. // returns the set of files matched by the parameters
  1197. // with shell matching characters.
  1198. ////////////////////////////////////////////////////////////////////
  1199. string PPScope::
  1200. expand_wildcard(const string &params) {
  1201. vector<string> results;
  1202. glob_string(expand_string(params), results);
  1203. string result = repaste(results, " ");
  1204. return result;
  1205. }
  1206. ////////////////////////////////////////////////////////////////////
  1207. // Function: PPScope::expand_isdir
  1208. // Access: Private
  1209. // Description: Expands the "isdir" function variable. This
  1210. // returns true if the parameter exists and is a
  1211. // directory, or false otherwise. This actually expands
  1212. // the parameter(s) with shell globbing characters,
  1213. // similar to the "wildcard" function, and looks only at
  1214. // the first expansion.
  1215. ////////////////////////////////////////////////////////////////////
  1216. string PPScope::
  1217. expand_isdir(const string &params) {
  1218. vector<string> results;
  1219. glob_string(expand_string(params), results);
  1220. if (results.empty()) {
  1221. // No matching file, too bad.
  1222. return string();
  1223. }
  1224. Filename filename = results[0];
  1225. if (filename.is_directory()) {
  1226. return filename.get_fullpath();
  1227. } else {
  1228. return string();
  1229. }
  1230. }
  1231. ////////////////////////////////////////////////////////////////////
  1232. // Function: PPScope::expand_isfile
  1233. // Access: Private
  1234. // Description: Expands the "isfile" function variable. This
  1235. // returns true if the parameter exists and is a
  1236. // regular file, or false otherwise. This actually
  1237. // expands the parameter(s) with shell globbing
  1238. // characters, similar to the "wildcard" function, and
  1239. // looks only at the first expansion.
  1240. ////////////////////////////////////////////////////////////////////
  1241. string PPScope::
  1242. expand_isfile(const string &params) {
  1243. vector<string> results;
  1244. glob_string(expand_string(params), results);
  1245. if (results.empty()) {
  1246. // No matching file, too bad.
  1247. return string();
  1248. }
  1249. Filename filename = results[0];
  1250. if (filename.is_regular_file()) {
  1251. return filename.get_fullpath();
  1252. } else {
  1253. return string();
  1254. }
  1255. }
  1256. ////////////////////////////////////////////////////////////////////
  1257. // Function: PPScope::expand_libtest
  1258. // Access: Private
  1259. // Description: Expands the "libtest" function variable. This
  1260. // serves as a poor man's autoconf feature to check to
  1261. // see if a library by the given name exists on the
  1262. // indicated search path, or on the system search path.
  1263. ////////////////////////////////////////////////////////////////////
  1264. string PPScope::
  1265. expand_libtest(const string &params) {
  1266. // Get the parameters out based on commas. The first parameter is a
  1267. // space-separated set of directories to search, the second
  1268. // parameter is a space-separated set of library names.
  1269. vector<string> tokens;
  1270. tokenize_params(params, tokens, true);
  1271. if (tokens.size() != 2) {
  1272. cerr << "libtest requires two parameters.\n";
  1273. errors_occurred = true;
  1274. return string();
  1275. }
  1276. DSearchPath directories;
  1277. directories.append_path(tokens[0], " \n\t");
  1278. // Also add the system directories to the list, whatever we think
  1279. // those should be. Here we have to make a few assumptions.
  1280. #ifdef WIN32
  1281. const char *windir = getenv("WINDIR");
  1282. if (windir != (const char *)NULL) {
  1283. Filename windir_filename = Filename::from_os_specific(windir);
  1284. directories.append_directory(Filename(windir_filename, "System"));
  1285. directories.append_directory(Filename(windir_filename, "System32"));
  1286. }
  1287. const char *lib = getenv("LIB");
  1288. if (lib != (const char *)NULL) {
  1289. vector<string> lib_dirs;
  1290. tokenize(lib, lib_dirs, ";");
  1291. vector<string>::const_iterator li;
  1292. for (li = lib_dirs.begin(); li != lib_dirs.end(); ++li) {
  1293. directories.append_directory(Filename::from_os_specific(*li));
  1294. }
  1295. }
  1296. #endif
  1297. // We'll also check the Unix standard places, even if we're building
  1298. // Windows, since we might be using Cygwin.
  1299. // Check LD_LIBRARY_PATH.
  1300. const char *ld_library_path = getenv("LD_LIBRARY_PATH");
  1301. if (ld_library_path != (const char *)NULL) {
  1302. directories.append_path(ld_library_path, ":");
  1303. }
  1304. directories.append_directory("/lib");
  1305. directories.append_directory("/usr/lib");
  1306. vector<string> libnames;
  1307. tokenize_whitespace(tokens[1], libnames);
  1308. if (libnames.empty()) {
  1309. // No libraries is a default "false".
  1310. return string();
  1311. }
  1312. // We only bother to search for the first library name in the list.
  1313. Filename libname = libnames[0];
  1314. bool found = false;
  1315. #ifdef WIN32
  1316. if (libname.get_extension() != string("lib")) {
  1317. libname = "lib" + libname.get_basename() + ".lib";
  1318. }
  1319. found = libname.resolve_filename(directories);
  1320. if (!found) {
  1321. libname.set_extension("dll");
  1322. found = libname.resolve_filename(directories);
  1323. }
  1324. #else
  1325. libname = "lib" + libname.get_basename() + ".a";
  1326. found = libname.resolve_filename(directories);
  1327. if (!found) {
  1328. libname.set_extension("so");
  1329. found = libname.resolve_filename(directories);
  1330. }
  1331. #endif
  1332. if (found) {
  1333. return libname.get_fullpath();
  1334. } else {
  1335. return string();
  1336. }
  1337. }
  1338. ////////////////////////////////////////////////////////////////////
  1339. // Function: PPScope::expand_bintest
  1340. // Access: Private
  1341. // Description: Expands the "bintest" function variable. This
  1342. // serves as a poor man's autoconf feature to check to
  1343. // see if an executable program by the given name exists
  1344. // on the indicated search path, or on the system search
  1345. // path.
  1346. ////////////////////////////////////////////////////////////////////
  1347. string PPScope::
  1348. expand_bintest(const string &params) {
  1349. // We only have one parameter: the filename of the executable. We
  1350. // always search for it on the path.
  1351. Filename binname = Filename::from_os_specific(expand_string(params));
  1352. if (binname.empty()) {
  1353. // No binary, no exist.
  1354. return string();
  1355. }
  1356. // An explicit path from the root does not require a search.
  1357. if (binname.is_fully_qualified()) {
  1358. if (binname.exists()) {
  1359. return binname.get_fullpath();
  1360. } else {
  1361. return string();
  1362. }
  1363. }
  1364. const char *path = getenv("PATH");
  1365. if (path == (const char *)NULL) {
  1366. // If the path is undefined, too bad.
  1367. return string();
  1368. }
  1369. string pathvar(path);
  1370. DSearchPath directories;
  1371. #ifdef WIN32
  1372. if (pathvar.find(';') != string::npos) {
  1373. // If the path contains semicolons, it's a native Windows-style
  1374. // path: split it up based on semicolons, and convert each
  1375. // directory from windows form.
  1376. vector<string> path_dirs;
  1377. tokenize(path, path_dirs, ";");
  1378. vector<string>::const_iterator pi;
  1379. for (pi = path_dirs.begin(); pi != path_dirs.end(); ++pi) {
  1380. directories.append_directory(Filename::from_os_specific(*pi));
  1381. }
  1382. } else {
  1383. // Otherwise, assume it's a Cygwin-style path: split it up based
  1384. // on colons.
  1385. directories.append_path(pathvar, ":");
  1386. }
  1387. #else
  1388. directories.append_path(pathvar, ":");
  1389. #endif
  1390. #ifdef WIN32
  1391. bool found = binname.resolve_filename(directories, "exe");
  1392. #else
  1393. bool found = binname.resolve_filename(directories);
  1394. #endif
  1395. if (found) {
  1396. return binname.get_fullpath();
  1397. } else {
  1398. return string();
  1399. }
  1400. }
  1401. ////////////////////////////////////////////////////////////////////
  1402. // Function: PPScope::expand_shell
  1403. // Access: Private
  1404. // Description: Expands the "shell" function variable. This executes
  1405. // the given command in a subprocess and returns the
  1406. // standard output.
  1407. ////////////////////////////////////////////////////////////////////
  1408. string PPScope::
  1409. expand_shell(const string &params) {
  1410. #ifdef WIN32_VC
  1411. cerr << "$[shell] is not presently supported on Win32 without Cygwin.\n";
  1412. errors_occurred = true;
  1413. string output;
  1414. #else // WIN32_VC
  1415. // We run $[shell] commands within the directory indicated by
  1416. // $[THISDIRPREFIX]. This way, local filenames will be expanded the
  1417. // way we expect.
  1418. string dirname = trim_blanks(expand_variable("THISDIRPREFIX"));
  1419. string command = expand_string(params);
  1420. int pid, status;
  1421. int pd[2];
  1422. if (pipe(pd) < 0) {
  1423. // pipe() failed.
  1424. perror("pipe");
  1425. return string();
  1426. }
  1427. pid = fork();
  1428. if (pid < 0) {
  1429. // fork() failed.
  1430. perror("fork");
  1431. return string();
  1432. }
  1433. if (pid == 0) {
  1434. // Child.
  1435. if (!dirname.empty()) {
  1436. // We don't have to restore the directory after we're done,
  1437. // because we're doing the chdir() call only within the child
  1438. // process.
  1439. if (chdir(dirname.c_str()) < 0) {
  1440. perror("chdir");
  1441. }
  1442. }
  1443. close(pd[0]);
  1444. dup2(pd[1], STDOUT_FILENO);
  1445. char *argv[4];
  1446. argv[0] = "sh";
  1447. argv[1] = "-c";
  1448. argv[2] = (char *)command.c_str();
  1449. argv[3] = (char *)NULL;
  1450. execv("/bin/sh", argv);
  1451. exit(127);
  1452. }
  1453. // Parent. Wait for the child to terminate, and read from its
  1454. // output while we're waiting.
  1455. close(pd[1]);
  1456. bool child_done = false;
  1457. bool pipe_closed = false;
  1458. string output;
  1459. while (!child_done && !pipe_closed) {
  1460. static const int buffer_size = 1024;
  1461. char buffer[buffer_size];
  1462. int read_bytes = (int)read(pd[0], buffer, buffer_size);
  1463. if (read_bytes < 0) {
  1464. perror("read");
  1465. } else if (read_bytes == 0) {
  1466. pipe_closed = true;
  1467. } else {
  1468. output += string(buffer, read_bytes);
  1469. }
  1470. if (!child_done) {
  1471. int waitresult = waitpid(pid, &status, WNOHANG);
  1472. if (waitresult < 0) {
  1473. if (errno != EINTR) {
  1474. perror("waitpid");
  1475. return string();
  1476. }
  1477. } else if (waitresult > 0) {
  1478. child_done = true;
  1479. }
  1480. }
  1481. }
  1482. close(pd[0]);
  1483. #endif // WIN32_VC
  1484. // Now get the output. We split it into words and then reconnect
  1485. // it, to simulate the shell's backpop operator.
  1486. vector<string> results;
  1487. tokenize_whitespace(output, results);
  1488. string result = repaste(results, " ");
  1489. return result;
  1490. }
  1491. ////////////////////////////////////////////////////////////////////
  1492. // Function: PPScope::expand_standardize
  1493. // Access: Private
  1494. // Description: Expands the "standardize" function variable. This
  1495. // converts the filename to standard form by removing
  1496. // consecutive repeated slashes and collapsing /../
  1497. // where possible.
  1498. ////////////////////////////////////////////////////////////////////
  1499. string PPScope::
  1500. expand_standardize(const string &params) {
  1501. Filename filename = trim_blanks(expand_string(params));
  1502. if (filename.empty()) {
  1503. return string();
  1504. }
  1505. filename.standardize();
  1506. return filename.get_fullpath();
  1507. }
  1508. ////////////////////////////////////////////////////////////////////
  1509. // Function: PPScope::expand_canonical
  1510. // Access: Private
  1511. // Description: Expands the "canonical" function variable. This
  1512. // converts this filename to a canonical name by
  1513. // replacing the directory part with the fully-qualified
  1514. // directory part. This is done by changing to that
  1515. // directory and calling getcwd().
  1516. //
  1517. // See filename::make_canonical() for a complete
  1518. // explanation of the implications of this and of the
  1519. // difference between this and standardize, above.
  1520. ////////////////////////////////////////////////////////////////////
  1521. string PPScope::
  1522. expand_canonical(const string &params) {
  1523. Filename filename = trim_blanks(expand_string(params));
  1524. filename.make_canonical();
  1525. return filename.get_fullpath();
  1526. }
  1527. ////////////////////////////////////////////////////////////////////
  1528. // Function: PPScope::expand_length
  1529. // Access: Private
  1530. // Description: Expands the "length" function variable. This returns
  1531. // the length of the argument in characters.
  1532. ////////////////////////////////////////////////////////////////////
  1533. string PPScope::
  1534. expand_length(const string &params) {
  1535. string word = trim_blanks(expand_string(params));
  1536. char buffer[32];
  1537. sprintf(buffer, "%d", word.length());
  1538. string result = buffer;
  1539. return result;
  1540. }
  1541. ////////////////////////////////////////////////////////////////////
  1542. // Function: PPScope::expand_substr
  1543. // Access: Private
  1544. // Description: Expands the "substr" function variable. $[substr
  1545. // S,E,string] returns the substring of "string"
  1546. // beginning at character S (1-based) and continuing to
  1547. // character E, inclusive.
  1548. ////////////////////////////////////////////////////////////////////
  1549. string PPScope::
  1550. expand_substr(const string &params) {
  1551. // Split the string up into tokens based on the commas.
  1552. vector<string> tokens;
  1553. tokenize_params(params, tokens, true);
  1554. if (tokens.size() != 3) {
  1555. cerr << "substr requires three parameters.\n";
  1556. errors_occurred = true;
  1557. return string();
  1558. }
  1559. int start = atoi(tokens[0].c_str());
  1560. int end = atoi(tokens[1].c_str());
  1561. if (end < start) {
  1562. // Following GNU make's convention, we swap start and end if
  1563. // they're out of order.
  1564. int t = end;
  1565. end = start;
  1566. start = t;
  1567. }
  1568. const string &word = tokens[2];
  1569. start = max(start, 1);
  1570. end = min(end, (int)word.length());
  1571. if (end < start) {
  1572. return string();
  1573. }
  1574. string result = word.substr(start - 1, end - start + 1);
  1575. return result;
  1576. }
  1577. ////////////////////////////////////////////////////////////////////
  1578. // Function: PPScope::expand_dir
  1579. // Access: Private
  1580. // Description: Expands the "dir" function variable. This returns
  1581. // the directory part of its filename argument(s), or ./
  1582. // if the words contain no slash.
  1583. ////////////////////////////////////////////////////////////////////
  1584. string PPScope::
  1585. expand_dir(const string &params) {
  1586. // Split the parameter into tokens based on the spaces.
  1587. vector<string> words;
  1588. tokenize_whitespace(expand_string(params), words);
  1589. vector<string>::iterator wi;
  1590. for (wi = words.begin(); wi != words.end(); ++wi) {
  1591. string &word = (*wi);
  1592. size_t slash = word.rfind('/');
  1593. if (slash != string::npos) {
  1594. word = word.substr(0, slash + 1);
  1595. } else {
  1596. word = "./";
  1597. }
  1598. }
  1599. string result = repaste(words, " ");
  1600. return result;
  1601. }
  1602. ////////////////////////////////////////////////////////////////////
  1603. // Function: PPScope::expand_notdir
  1604. // Access: Private
  1605. // Description: Expands the "notdir" function variable. This returns
  1606. // everything following the rightmost slash, or the
  1607. // string itself if there is no slash.
  1608. ////////////////////////////////////////////////////////////////////
  1609. string PPScope::
  1610. expand_notdir(const string &params) {
  1611. // Split the parameter into tokens based on the spaces.
  1612. vector<string> words;
  1613. tokenize_whitespace(expand_string(params), words);
  1614. vector<string>::iterator wi;
  1615. for (wi = words.begin(); wi != words.end(); ++wi) {
  1616. string &word = (*wi);
  1617. size_t slash = word.rfind('/');
  1618. if (slash != string::npos) {
  1619. word = word.substr(slash + 1);
  1620. }
  1621. }
  1622. string result = repaste(words, " ");
  1623. return result;
  1624. }
  1625. ////////////////////////////////////////////////////////////////////
  1626. // Function: PPScope::expand_suffix
  1627. // Access: Private
  1628. // Description: Expands the "suffix" function variable. This returns
  1629. // the filename extension, including a dot, if any.
  1630. ////////////////////////////////////////////////////////////////////
  1631. string PPScope::
  1632. expand_suffix(const string &params) {
  1633. // Split the parameter into tokens based on the spaces.
  1634. vector<string> words;
  1635. tokenize_whitespace(expand_string(params), words);
  1636. vector<string>::iterator wi;
  1637. for (wi = words.begin(); wi != words.end(); ++wi) {
  1638. string &word = (*wi);
  1639. size_t dot = word.rfind('.');
  1640. if (dot != string::npos) {
  1641. string ext = word.substr(dot);
  1642. if (ext.find('/') == string::npos) {
  1643. word = ext;
  1644. } else {
  1645. word = string();
  1646. }
  1647. } else {
  1648. word = string();
  1649. }
  1650. }
  1651. string result = repaste(words, " ");
  1652. return result;
  1653. }
  1654. ////////////////////////////////////////////////////////////////////
  1655. // Function: PPScope::expand_basename
  1656. // Access: Private
  1657. // Description: Expands the "basename" function variable. This returns
  1658. // everything but the filename extension (including the
  1659. // directory, if any).
  1660. ////////////////////////////////////////////////////////////////////
  1661. string PPScope::
  1662. expand_basename(const string &params) {
  1663. // Split the parameter into tokens based on the spaces.
  1664. vector<string> words;
  1665. tokenize_whitespace(expand_string(params), words);
  1666. vector<string>::iterator wi;
  1667. for (wi = words.begin(); wi != words.end(); ++wi) {
  1668. string &word = (*wi);
  1669. size_t dot = word.rfind('.');
  1670. if (dot != string::npos) {
  1671. string ext = word.substr(dot);
  1672. if (ext.find('/') == string::npos) {
  1673. word = word.substr(0, dot);
  1674. }
  1675. }
  1676. }
  1677. string result = repaste(words, " ");
  1678. return result;
  1679. }
  1680. ////////////////////////////////////////////////////////////////////
  1681. // Function: PPScope::expand_makeguid
  1682. // Access: Private
  1683. // Description: Expands the GUID (global unique identifier) of the
  1684. // given name (generally a directory). A GUID looks
  1685. // like this: 398F2CC4-C683-26EB-3251-6FC996738F7F
  1686. ////////////////////////////////////////////////////////////////////
  1687. string PPScope::
  1688. expand_makeguid(const string &params) {
  1689. // Expand all of the parameters into a single string.
  1690. string expansion = trim_blanks(expand_string(params));
  1691. if (expansion.size() == 0) {
  1692. cerr << "makeguid requires an argument.\n";
  1693. errors_occurred = true;
  1694. return string();
  1695. }
  1696. PP_MD5_CTX context;
  1697. unsigned char digest[16];
  1698. MD5Init(&context);
  1699. MD5Update(&context, reinterpret_cast<const unsigned char *>(expansion.data()),
  1700. expansion.size());
  1701. MD5Final(digest, &context);
  1702. string guid;
  1703. char hex[2];
  1704. int i;
  1705. for (i = 0; i < 4; i++) {
  1706. sprintf(hex, "%02x", digest[i]);
  1707. guid.append(hex);
  1708. }
  1709. guid += "-";
  1710. for (i = 4; i < 6; i++) {
  1711. sprintf(hex, "%02x", digest[i]);
  1712. guid.append(hex);
  1713. }
  1714. guid += "-";
  1715. for (i = 6; i < 8; i++) {
  1716. sprintf(hex, "%02x", digest[i]);
  1717. guid.append(hex);
  1718. }
  1719. guid += "-";
  1720. for (i = 8; i < 10; i++) {
  1721. sprintf(hex, "%02x", digest[i]);
  1722. guid.append(hex);
  1723. }
  1724. guid += "-";
  1725. for (i = 10; i < 16; i++) {
  1726. sprintf(hex, "%02x", digest[i]);
  1727. guid.append(hex);
  1728. }
  1729. // Convert the entire GUID string to uppercased letters.
  1730. string::iterator si;
  1731. for (si = guid.begin(); si != guid.end(); ++si) {
  1732. (*si) = toupper(*si);
  1733. }
  1734. return guid;
  1735. }
  1736. ////////////////////////////////////////////////////////////////////
  1737. // Function: PPScope::expand_word
  1738. // Access: Private
  1739. // Description: Expands the "word" function variable. This returns
  1740. // the nth word, 1-based, of the space-separated list of
  1741. // words in the second parameter.
  1742. ////////////////////////////////////////////////////////////////////
  1743. string PPScope::
  1744. expand_word(const string &params) {
  1745. // Split the string up into tokens based on the commas.
  1746. vector<string> tokens;
  1747. tokenize_params(params, tokens, true);
  1748. if (tokens.size() != 2) {
  1749. cerr << "word requires two parameters.\n";
  1750. errors_occurred = true;
  1751. return string();
  1752. }
  1753. int index = atoi(tokens[0].c_str());
  1754. // Split the second parameter into tokens based on the spaces.
  1755. vector<string> words;
  1756. tokenize_whitespace(expand_string(tokens[1]), words);
  1757. if (index < 1 || index > (int)words.size()) {
  1758. // Out of range.
  1759. return string();
  1760. }
  1761. return words[index - 1];
  1762. }
  1763. ////////////////////////////////////////////////////////////////////
  1764. // Function: PPScope::expand_wordlist
  1765. // Access: Private
  1766. // Description: Expands the "wordlist" function variable. This
  1767. // returns a range of words, 1-based, of the
  1768. // space-separated list of words in the third parameter.
  1769. ////////////////////////////////////////////////////////////////////
  1770. string PPScope::
  1771. expand_wordlist(const string &params) {
  1772. // Split the string up into tokens based on the commas.
  1773. vector<string> tokens;
  1774. tokenize_params(params, tokens, true);
  1775. if (tokens.size() != 3) {
  1776. cerr << "wordlist requires three parameters.\n";
  1777. errors_occurred = true;
  1778. return string();
  1779. }
  1780. int start = atoi(tokens[0].c_str());
  1781. int end = atoi(tokens[1].c_str());
  1782. if (end < start) {
  1783. // Following GNU make's convention, we swap start and end if
  1784. // they're out of order.
  1785. int t = end;
  1786. end = start;
  1787. start = t;
  1788. }
  1789. // Split the third parameter into tokens based on the spaces.
  1790. vector<string> words;
  1791. tokenize_whitespace(expand_string(tokens[2]), words);
  1792. start = max(start, 1);
  1793. end = min(end, (int)words.size() + 1);
  1794. if (end < start) {
  1795. return string();
  1796. }
  1797. vector<string> results;
  1798. results.insert(results.end(),
  1799. words.begin() + start - 1,
  1800. words.begin() + end - 1);
  1801. string result = repaste(results, " ");
  1802. return result;
  1803. }
  1804. ////////////////////////////////////////////////////////////////////
  1805. // Function: PPScope::expand_words
  1806. // Access: Private
  1807. // Description: Expands the "words" function variable. This
  1808. // returns the number of space-separated words in the
  1809. // list.
  1810. ////////////////////////////////////////////////////////////////////
  1811. string PPScope::
  1812. expand_words(const string &params) {
  1813. // Split the parameter into tokens based on the spaces.
  1814. vector<string> words;
  1815. tokenize_whitespace(expand_string(params), words);
  1816. char buffer[32];
  1817. sprintf(buffer, "%d", words.size());
  1818. string result = buffer;
  1819. return result;
  1820. }
  1821. ////////////////////////////////////////////////////////////////////
  1822. // Function: PPScope::expand_firstword
  1823. // Access: Private
  1824. // Description: Expands the "firstword" function variable. This
  1825. // returns the first of several words separated by
  1826. // whitespace.
  1827. ////////////////////////////////////////////////////////////////////
  1828. string PPScope::
  1829. expand_firstword(const string &params) {
  1830. // Split the parameter into tokens based on the spaces.
  1831. vector<string> words;
  1832. tokenize_whitespace(expand_string(params), words);
  1833. if (!words.empty()) {
  1834. return words[0];
  1835. }
  1836. return string();
  1837. }
  1838. ////////////////////////////////////////////////////////////////////
  1839. // Function: PPScope::expand_patsubst
  1840. // Access: Private
  1841. // Description: Expands the "patsubst" function variable.
  1842. ////////////////////////////////////////////////////////////////////
  1843. string PPScope::
  1844. expand_patsubst(const string &params, bool separate_words) {
  1845. // Split the string up into tokens based on the commas.
  1846. vector<string> tokens;
  1847. tokenize_params(params, tokens, false);
  1848. if (tokens.size() < 3) {
  1849. cerr << "patsubst requires at least three parameters.\n";
  1850. errors_occurred = true;
  1851. return string();
  1852. }
  1853. if ((tokens.size() % 2) != 1) {
  1854. cerr << "patsubst requires an odd number of parameters.\n";
  1855. errors_occurred = true;
  1856. return string();
  1857. }
  1858. // Split the last parameter into tokens based on the spaces--but
  1859. // only if separate_words is true.
  1860. vector<string> words;
  1861. if (separate_words) {
  1862. tokenize_whitespace(expand_string(tokens.back()), words);
  1863. } else {
  1864. words.push_back(expand_string(tokens.back()));
  1865. }
  1866. // Build up a vector of from/to patterns.
  1867. typedef vector<PPFilenamePattern> Patterns;
  1868. typedef vector<Patterns> FromPatterns;
  1869. FromPatterns from;
  1870. Patterns to;
  1871. size_t i;
  1872. for (i = 0; i < tokens.size() - 1; i += 2) {
  1873. // Each "from" pattern might be a collection of patterns separated
  1874. // by spaces, and it is expanded immediately.
  1875. from.push_back(Patterns());
  1876. vector<string> froms;
  1877. tokenize_whitespace(expand_string(tokens[i]), froms);
  1878. vector<string>::const_iterator fi;
  1879. for (fi = froms.begin(); fi != froms.end(); ++fi) {
  1880. PPFilenamePattern pattern(*fi);
  1881. if (!pattern.has_wildcard()) {
  1882. cerr << "All the \"from\" parameters of patsubst must include "
  1883. << PATTERN_WILDCARD << ".\n";
  1884. errors_occurred = true;
  1885. return string();
  1886. }
  1887. from.back().push_back(pattern);
  1888. }
  1889. // However, the corresponding "to" pattern is just one pattern,
  1890. // and it is expanded immediately only if it does not contain a
  1891. // wildcard character.
  1892. PPFilenamePattern to_pattern(tokens[i + 1]);
  1893. if (!to_pattern.has_wildcard()) {
  1894. to_pattern = PPFilenamePattern(expand_string(tokens[i + 1]));
  1895. }
  1896. to.push_back(to_pattern);
  1897. }
  1898. size_t num_patterns = from.size();
  1899. assert(num_patterns == to.size());
  1900. vector<string>::iterator wi;
  1901. for (wi = words.begin(); wi != words.end(); ++wi) {
  1902. bool matched = false;
  1903. for (i = 0; i < num_patterns && !matched; i++) {
  1904. Patterns::const_iterator pi;
  1905. for (pi = from[i].begin(); pi != from[i].end() && !matched; ++pi) {
  1906. if ((*pi).matches(*wi)) {
  1907. matched = true;
  1908. string transformed = to[i].transform(*wi, (*pi));
  1909. (*wi) = expand_string(transformed);
  1910. }
  1911. }
  1912. }
  1913. }
  1914. string result = repaste(words, " ");
  1915. return result;
  1916. }
  1917. ////////////////////////////////////////////////////////////////////
  1918. // Function: PPScope::expand_filter
  1919. // Access: Private
  1920. // Description: Expands the "filter" function variable.
  1921. ////////////////////////////////////////////////////////////////////
  1922. string PPScope::
  1923. expand_filter(const string &params) {
  1924. // Split the string up into tokens based on the commas.
  1925. vector<string> tokens;
  1926. tokenize_params(params, tokens, true);
  1927. if (tokens.size() != 2) {
  1928. cerr << "filter requires two parameters.\n";
  1929. errors_occurred = true;
  1930. return string();
  1931. }
  1932. // Split up the first parameter--the list of patterns to filter
  1933. // by--into tokens based on the spaces.
  1934. vector<string> pattern_strings;
  1935. tokenize_whitespace(tokens[0], pattern_strings);
  1936. vector<PPFilenamePattern> patterns;
  1937. vector<string>::const_iterator psi;
  1938. for (psi = pattern_strings.begin(); psi != pattern_strings.end(); ++psi) {
  1939. patterns.push_back(PPFilenamePattern(*psi));
  1940. }
  1941. // Split up the second parameter--the list of words to filter--into
  1942. // tokens based on the spaces.
  1943. vector<string> words;
  1944. tokenize_whitespace(tokens[1], words);
  1945. vector<string>::iterator wi, wnext;
  1946. wnext = words.begin();
  1947. for (wi = words.begin(); wi != words.end(); ++wi) {
  1948. const string &word = (*wi);
  1949. bool matches_pattern = false;
  1950. vector<PPFilenamePattern>::const_iterator pi;
  1951. for (pi = patterns.begin();
  1952. pi != patterns.end() && !matches_pattern;
  1953. ++pi) {
  1954. matches_pattern = (*pi).matches(word);
  1955. }
  1956. if (matches_pattern) {
  1957. *wnext++ = word;
  1958. }
  1959. }
  1960. words.erase(wnext, words.end());
  1961. string result = repaste(words, " ");
  1962. return result;
  1963. }
  1964. ////////////////////////////////////////////////////////////////////
  1965. // Function: PPScope::expand_filter_out
  1966. // Access: Private
  1967. // Description: Expands the "filter_out" function variable.
  1968. ////////////////////////////////////////////////////////////////////
  1969. string PPScope::
  1970. expand_filter_out(const string &params) {
  1971. // Split the string up into tokens based on the commas.
  1972. vector<string> tokens;
  1973. tokenize_params(params, tokens, true);
  1974. if (tokens.size() != 2) {
  1975. cerr << "filter-out requires two parameters.\n";
  1976. errors_occurred = true;
  1977. return string();
  1978. }
  1979. // Split up the first parameter--the list of patterns to filter
  1980. // by--into tokens based on the spaces.
  1981. vector<string> pattern_strings;
  1982. tokenize_whitespace(tokens[0], pattern_strings);
  1983. vector<PPFilenamePattern> patterns;
  1984. vector<string>::const_iterator psi;
  1985. for (psi = pattern_strings.begin(); psi != pattern_strings.end(); ++psi) {
  1986. patterns.push_back(PPFilenamePattern(*psi));
  1987. }
  1988. // Split up the second parameter--the list of words to filter--into
  1989. // tokens based on the spaces.
  1990. vector<string> words;
  1991. tokenize_whitespace(tokens[1], words);
  1992. vector<string>::iterator wi, wnext;
  1993. wnext = words.begin();
  1994. for (wi = words.begin(); wi != words.end(); ++wi) {
  1995. const string &word = (*wi);
  1996. bool matches_pattern = false;
  1997. vector<PPFilenamePattern>::const_iterator pi;
  1998. for (pi = patterns.begin();
  1999. pi != patterns.end() && !matches_pattern;
  2000. ++pi) {
  2001. matches_pattern = (*pi).matches(word);
  2002. }
  2003. if (!matches_pattern) {
  2004. *wnext++ = word;
  2005. }
  2006. }
  2007. words.erase(wnext, words.end());
  2008. string result = repaste(words, " ");
  2009. return result;
  2010. }
  2011. ////////////////////////////////////////////////////////////////////
  2012. // Function: PPScope::expand_subst
  2013. // Access: Private
  2014. // Description: Expands the "subst" function variable.
  2015. ////////////////////////////////////////////////////////////////////
  2016. string PPScope::
  2017. expand_subst(const string &params) {
  2018. // Split the string up into tokens based on the commas.
  2019. vector<string> tokens;
  2020. tokenize_params(params, tokens, true);
  2021. if (tokens.size() < 3) {
  2022. cerr << "subst requires at least three parameters.\n";
  2023. errors_occurred = true;
  2024. return string();
  2025. }
  2026. if ((tokens.size() % 2) != 1) {
  2027. cerr << "subst requires an odd number of parameters.\n";
  2028. errors_occurred = true;
  2029. return string();
  2030. }
  2031. // Now substitute each of the substitute strings out for the
  2032. // replacement strings.
  2033. string str = tokens.back();
  2034. for (size_t i = 0; i < tokens.size() - 1; i += 2) {
  2035. string new_str;
  2036. const string &subst = tokens[i];
  2037. const string &repl = tokens[i + 1];
  2038. size_t q = 0;
  2039. size_t p = str.find(subst, q);
  2040. while (p != string::npos) {
  2041. new_str += str.substr(q, p - q) + repl;
  2042. q = p + subst.length();
  2043. p = str.find(subst, q);
  2044. }
  2045. str = new_str + str.substr(q);
  2046. }
  2047. return str;
  2048. }
  2049. ////////////////////////////////////////////////////////////////////
  2050. // Function: PPScope::expand_findstrnig
  2051. // Access: Private
  2052. // Description: Expands the "findstring" function variable.
  2053. // $[findstring a,b] returns b if and only if it is a
  2054. // substring of a; otherwise, it returns the empty
  2055. // string.
  2056. ////////////////////////////////////////////////////////////////////
  2057. string PPScope::
  2058. expand_findstring(const string &params) {
  2059. // Split the string up into tokens based on the commas.
  2060. vector<string> tokens;
  2061. tokenize_params(params, tokens, true);
  2062. if (tokens.size() != 2) {
  2063. cerr << "findstring requires two parameters.\n";
  2064. errors_occurred = true;
  2065. return string();
  2066. }
  2067. string str = tokens.back();
  2068. const string &srchstr = tokens[0];
  2069. size_t q = 0;
  2070. size_t p = str.find(srchstr, q);
  2071. if(p == string::npos)
  2072. str = "";
  2073. return str;
  2074. }
  2075. ////////////////////////////////////////////////////////////////////
  2076. // Function: PPScope::expand_wordsubst
  2077. // Access: Private
  2078. // Description: Expands the "wordsubst" function variable. This is
  2079. // like "subst" except it only replaces whole words.
  2080. ////////////////////////////////////////////////////////////////////
  2081. string PPScope::
  2082. expand_wordsubst(const string &params) {
  2083. // Split the string up into tokens based on the commas.
  2084. vector<string> tokens;
  2085. tokenize_params(params, tokens, true);
  2086. if (tokens.size() < 3) {
  2087. cerr << "subst requires at least three parameters.\n";
  2088. errors_occurred = true;
  2089. return string();
  2090. }
  2091. if ((tokens.size() % 2) != 1) {
  2092. cerr << "subst requires an odd number of parameters.\n";
  2093. errors_occurred = true;
  2094. return string();
  2095. }
  2096. // Split the last parameter into tokens based on the spaces.
  2097. vector<string> words;
  2098. tokenize_whitespace(tokens.back(), words);
  2099. for (size_t i = 0; i < tokens.size() - 1; i += 2) {
  2100. const string &subst = tokens[i];
  2101. const string &repl = tokens[i + 1];
  2102. vector<string>::iterator wi;
  2103. for (wi = words.begin(); wi != words.end(); ++wi) {
  2104. if ((*wi) == subst) {
  2105. (*wi) = repl;
  2106. }
  2107. }
  2108. }
  2109. string result = repaste(words, " ");
  2110. return result;
  2111. }
  2112. ////////////////////////////////////////////////////////////////////
  2113. // Function: PPScope::expand_join
  2114. // Access: Private
  2115. // Description: Expands the "join" function variable: joins the list
  2116. // of words using the specified separator.
  2117. ////////////////////////////////////////////////////////////////////
  2118. string PPScope::
  2119. expand_join(const string &params) {
  2120. // Split the string up into tokens based on the spaces.
  2121. vector<string> tokens;
  2122. tokenize_params(params, tokens, true);
  2123. if (tokens.size() != 2) {
  2124. cerr << "join requires two parameters.\n";
  2125. errors_occurred = true;
  2126. return string();
  2127. }
  2128. const string &sep = tokens[0];
  2129. vector<string> words;
  2130. tokenize_whitespace(expand_string(tokens[1]), words);
  2131. string result = repaste(words, sep);
  2132. return result;
  2133. }
  2134. ////////////////////////////////////////////////////////////////////
  2135. // Function: PPScope::expand_sort
  2136. // Access: Private
  2137. // Description: Expands the "sort" function variable: sort the words
  2138. // into alphabetical order, and also remove duplicates.
  2139. ////////////////////////////////////////////////////////////////////
  2140. string PPScope::
  2141. expand_sort(const string &params) {
  2142. // Split the string up into tokens based on the spaces.
  2143. vector<string> words;
  2144. tokenize_whitespace(expand_string(params), words);
  2145. sort(words.begin(), words.end());
  2146. words.erase(unique(words.begin(), words.end()), words.end());
  2147. string result = repaste(words, " ");
  2148. return result;
  2149. }
  2150. ////////////////////////////////////////////////////////////////////
  2151. // Function: PPScope::expand_unique
  2152. // Access: Private
  2153. // Description: Expands the "unique" function variable: remove
  2154. // duplicates from the list of words without changing
  2155. // the order. The first appearance of each word
  2156. // remains.
  2157. ////////////////////////////////////////////////////////////////////
  2158. string PPScope::
  2159. expand_unique(const string &params) {
  2160. // Split the string up into tokens based on the spaces.
  2161. vector<string> words;
  2162. tokenize_whitespace(expand_string(params), words);
  2163. vector<string>::iterator win, wout;
  2164. set<string> included_words;
  2165. win = words.begin();
  2166. wout = words.begin();
  2167. while (win != words.end()) {
  2168. if (included_words.insert(*win).second) {
  2169. // This is a unique word so far.
  2170. *wout++ = *win;
  2171. }
  2172. ++win;
  2173. }
  2174. words.erase(wout, words.end());
  2175. string result = repaste(words, " ");
  2176. return result;
  2177. }
  2178. ////////////////////////////////////////////////////////////////////
  2179. // Function: PPScope::expand_matrix
  2180. // Access: Private
  2181. // Description: Expands the "matrix" function variable. This
  2182. // combines the different words of the n parameters in
  2183. // all possible ways, like the shell {a,b,c} expansion
  2184. // characters. For example, $[matrix a b,c,10 20 30]
  2185. // expands to ac10 ac20 ac30 bc10 bc20 bc30.
  2186. ////////////////////////////////////////////////////////////////////
  2187. string PPScope::
  2188. expand_matrix(const string &params) {
  2189. // Split the string up into tokens based on the commas.
  2190. vector<string> tokens;
  2191. tokenize_params(params, tokens, true);
  2192. // Each token gets split up into words based on the spaces.
  2193. vector<vector<string> > words;
  2194. for (int i = 0; i < (int)tokens.size(); i++) {
  2195. words.push_back(vector<string>());
  2196. tokenize_whitespace(tokens[i], words.back());
  2197. }
  2198. // Now synthesize the results recursively.
  2199. vector<string> results;
  2200. r_expand_matrix(results, words, 0, "");
  2201. string result = repaste(results, " ");
  2202. return result;
  2203. }
  2204. ////////////////////////////////////////////////////////////////////
  2205. // Function: PPScope::expand_if
  2206. // Access: Private
  2207. // Description: Expands the "if" function variable. This evaluates
  2208. // the first parameter and returns the second parameter
  2209. // if the result is true (i.e. nonempty) and the third
  2210. // parameter (if present) if the result is false
  2211. // (i.e. empty).
  2212. ////////////////////////////////////////////////////////////////////
  2213. string PPScope::
  2214. expand_if(const string &params) {
  2215. // Split the string up into tokens based on the commas.
  2216. vector<string> tokens;
  2217. tokenize_params(params, tokens, true);
  2218. if (tokens.size() == 2) {
  2219. if (!tokens[0].empty()) {
  2220. return tokens[1];
  2221. } else {
  2222. return "";
  2223. }
  2224. } else if (tokens.size() == 3) {
  2225. if (!tokens[0].empty()) {
  2226. return tokens[1];
  2227. } else {
  2228. return tokens[2];
  2229. }
  2230. }
  2231. cerr << "if requires two or three parameters.\n";
  2232. errors_occurred = true;
  2233. return string();
  2234. }
  2235. ////////////////////////////////////////////////////////////////////
  2236. // Function: PPScope::expand_eq
  2237. // Access: Private
  2238. // Description: Expands the "eq" function variable. This tests
  2239. // string equivalence.
  2240. ////////////////////////////////////////////////////////////////////
  2241. string PPScope::
  2242. expand_eq(const string &params) {
  2243. // Split the string up into tokens based on the commas.
  2244. vector<string> tokens;
  2245. tokenize_params(params, tokens, true);
  2246. if (tokens.size() != 2) {
  2247. cerr << "eq requires two parameters.\n";
  2248. errors_occurred = true;
  2249. return string();
  2250. }
  2251. string result;
  2252. if (tokens[0] == tokens[1]) {
  2253. result = "1";
  2254. }
  2255. return result;
  2256. }
  2257. ////////////////////////////////////////////////////////////////////
  2258. // Function: PPScope::expand_ne
  2259. // Access: Private
  2260. // Description: Expands the "ne" function variable. This tests
  2261. // string equivalence.
  2262. ////////////////////////////////////////////////////////////////////
  2263. string PPScope::
  2264. expand_ne(const string &params) {
  2265. // Split the string up into tokens based on the commas.
  2266. vector<string> tokens;
  2267. tokenize_params(params, tokens, true);
  2268. if (tokens.size() != 2) {
  2269. cerr << "ne requires two parameters.\n";
  2270. errors_occurred = true;
  2271. return string();
  2272. }
  2273. string result;
  2274. if (!(tokens[0] == tokens[1])) {
  2275. result = "1";
  2276. }
  2277. return result;
  2278. }
  2279. ////////////////////////////////////////////////////////////////////
  2280. // Function: PPScope::expand_eqn
  2281. // Access: Private
  2282. // Description: Expands the "=" function variable. This tests
  2283. // numeric equivalence.
  2284. ////////////////////////////////////////////////////////////////////
  2285. string PPScope::
  2286. expand_eqn(const string &params) {
  2287. double a, b;
  2288. if (!tokenize_numeric_pair(params, a, b)) {
  2289. return string();
  2290. }
  2291. string result;
  2292. if (a == b) {
  2293. result = "1";
  2294. }
  2295. return result;
  2296. }
  2297. ////////////////////////////////////////////////////////////////////
  2298. // Function: PPScope::expand_nen
  2299. // Access: Private
  2300. // Description: Expands the "!=" function variable. This tests
  2301. // numeric equivalence.
  2302. ////////////////////////////////////////////////////////////////////
  2303. string PPScope::
  2304. expand_nen(const string &params) {
  2305. double a, b;
  2306. if (!tokenize_numeric_pair(params, a, b)) {
  2307. return string();
  2308. }
  2309. string result;
  2310. if (a != b) {
  2311. result = "1";
  2312. }
  2313. return result;
  2314. }
  2315. ////////////////////////////////////////////////////////////////////
  2316. // Function: PPScope::expand_ltn
  2317. // Access: Private
  2318. // Description: Expands the "<" function variable. This tests
  2319. // numeric relationships.
  2320. ////////////////////////////////////////////////////////////////////
  2321. string PPScope::
  2322. expand_ltn(const string &params) {
  2323. double a, b;
  2324. if (!tokenize_numeric_pair(params, a, b)) {
  2325. return string();
  2326. }
  2327. string result;
  2328. if (a < b) {
  2329. result = "1";
  2330. }
  2331. return result;
  2332. }
  2333. ////////////////////////////////////////////////////////////////////
  2334. // Function: PPScope::expand_len
  2335. // Access: Private
  2336. // Description: Expands the "<=" function variable. This tests
  2337. // numeric relationships.
  2338. ////////////////////////////////////////////////////////////////////
  2339. string PPScope::
  2340. expand_len(const string &params) {
  2341. double a, b;
  2342. if (!tokenize_numeric_pair(params, a, b)) {
  2343. return string();
  2344. }
  2345. string result;
  2346. if (a <= b) {
  2347. result = "1";
  2348. }
  2349. return result;
  2350. }
  2351. ////////////////////////////////////////////////////////////////////
  2352. // Function: PPScope::expand_gtn
  2353. // Access: Private
  2354. // Description: Expands the ">" function variable. This tests
  2355. // numeric relationships.
  2356. ////////////////////////////////////////////////////////////////////
  2357. string PPScope::
  2358. expand_gtn(const string &params) {
  2359. double a, b;
  2360. if (!tokenize_numeric_pair(params, a, b)) {
  2361. return string();
  2362. }
  2363. string result;
  2364. if (a > b) {
  2365. result = "1";
  2366. }
  2367. return result;
  2368. }
  2369. ////////////////////////////////////////////////////////////////////
  2370. // Function: PPScope::expand_gen
  2371. // Access: Private
  2372. // Description: Expands the ">=" function variable. This tests
  2373. // numeric relationships.
  2374. ////////////////////////////////////////////////////////////////////
  2375. string PPScope::
  2376. expand_gen(const string &params) {
  2377. double a, b;
  2378. if (!tokenize_numeric_pair(params, a, b)) {
  2379. return string();
  2380. }
  2381. string result;
  2382. if (a >= b) {
  2383. result = "1";
  2384. }
  2385. return result;
  2386. }
  2387. ////////////////////////////////////////////////////////////////////
  2388. // Function: PPScope::expand_plus
  2389. // Access: Private
  2390. // Description: Expands the "+" function variable. This operates
  2391. // on integer numbers.
  2392. ////////////////////////////////////////////////////////////////////
  2393. string PPScope::
  2394. expand_plus(const string &params) {
  2395. vector<int> tokens;
  2396. if (!tokenize_ints(params, tokens)) {
  2397. return string();
  2398. }
  2399. int result = 0;
  2400. vector<int>::const_iterator ti;
  2401. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  2402. result += (*ti);
  2403. }
  2404. return format_int(result);
  2405. }
  2406. ////////////////////////////////////////////////////////////////////
  2407. // Function: PPScope::expand_minus
  2408. // Access: Private
  2409. // Description: Expands the "-" function variable. This operates
  2410. // on integer numbers.
  2411. ////////////////////////////////////////////////////////////////////
  2412. string PPScope::
  2413. expand_minus(const string &params) {
  2414. vector<int> tokens;
  2415. if (!tokenize_ints(params, tokens)) {
  2416. return string();
  2417. }
  2418. int result = 0;
  2419. if (tokens.size() == 1) {
  2420. // A special case: unary minus.
  2421. result = -tokens[0];
  2422. } else if (tokens.size() > 1) {
  2423. result = tokens[0];
  2424. for (int i = 1; i < (int)tokens.size(); i++) {
  2425. result -= tokens[i];
  2426. }
  2427. }
  2428. return format_int(result);
  2429. }
  2430. ////////////////////////////////////////////////////////////////////
  2431. // Function: PPScope::expand_times
  2432. // Access: Private
  2433. // Description: Expands the "*" function variable. This operates
  2434. // on integer numbers.
  2435. ////////////////////////////////////////////////////////////////////
  2436. string PPScope::
  2437. expand_times(const string &params) {
  2438. vector<int> tokens;
  2439. if (!tokenize_ints(params, tokens)) {
  2440. return string();
  2441. }
  2442. int result = 1;
  2443. vector<int>::const_iterator ti;
  2444. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  2445. result *= (*ti);
  2446. }
  2447. return format_int(result);
  2448. }
  2449. ////////////////////////////////////////////////////////////////////
  2450. // Function: PPScope::expand_divide
  2451. // Access: Private
  2452. // Description: Expands the "/" function variable. This operates
  2453. // on integer numbers.
  2454. ////////////////////////////////////////////////////////////////////
  2455. string PPScope::
  2456. expand_divide(const string &params) {
  2457. vector<int> tokens;
  2458. if (!tokenize_ints(params, tokens)) {
  2459. return string();
  2460. }
  2461. if (tokens.size() != 2) {
  2462. cerr << tokens.size() << " parameters supplied when two were expected:\n"
  2463. << params << "\n";
  2464. errors_occurred = true;
  2465. return string();
  2466. }
  2467. return format_int(tokens[0] / tokens[1]);
  2468. }
  2469. ////////////////////////////////////////////////////////////////////
  2470. // Function: PPScope::expand_modulo
  2471. // Access: Private
  2472. // Description: Expands the "%" function variable. This operates
  2473. // on integer numbers.
  2474. ////////////////////////////////////////////////////////////////////
  2475. string PPScope::
  2476. expand_modulo(const string &params) {
  2477. vector<int> tokens;
  2478. if (!tokenize_ints(params, tokens)) {
  2479. return string();
  2480. }
  2481. if (tokens.size() != 2) {
  2482. cerr << tokens.size() << " parameters supplied when two were expected:\n"
  2483. << params << "\n";
  2484. errors_occurred = true;
  2485. return string();
  2486. }
  2487. return format_int(tokens[0] % tokens[1]);
  2488. }
  2489. ////////////////////////////////////////////////////////////////////
  2490. // Function: PPScope::expand_not
  2491. // Access: Private
  2492. // Description: Expands the "not" function variable. This returns
  2493. // nonempty if its argument is empty, empty if its
  2494. // argument is nonempty.
  2495. ////////////////////////////////////////////////////////////////////
  2496. string PPScope::
  2497. expand_not(const string &params) {
  2498. // Split the string up into tokens based on the commas.
  2499. vector<string> tokens;
  2500. tokenize_params(params, tokens, true);
  2501. if (tokens.size() != 1) {
  2502. cerr << "not requires one parameter.\n";
  2503. errors_occurred = true;
  2504. return string();
  2505. }
  2506. string result;
  2507. if (tokens[0].empty()) {
  2508. result = "1";
  2509. }
  2510. return result;
  2511. }
  2512. ////////////////////////////////////////////////////////////////////
  2513. // Function: PPScope::expand_or
  2514. // Access: Private
  2515. // Description: Expands the "or" function variable. This returns
  2516. // nonempty if any of its arguments are nonempty.
  2517. // Specifically, it returns the first nonempty argument.
  2518. ////////////////////////////////////////////////////////////////////
  2519. string PPScope::
  2520. expand_or(const string &params) {
  2521. // Split the string up into tokens based on the commas.
  2522. vector<string> tokens;
  2523. tokenize_params(params, tokens, true);
  2524. vector<string>::const_iterator ti;
  2525. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  2526. if (!(*ti).empty()) {
  2527. return (*ti);
  2528. }
  2529. }
  2530. return string();
  2531. }
  2532. ////////////////////////////////////////////////////////////////////
  2533. // Function: PPScope::expand_and
  2534. // Access: Private
  2535. // Description: Expands the "and" function variable. This returns
  2536. // nonempty if all of its arguments are nonempty.
  2537. // Specifically, it returns the last argument.
  2538. ////////////////////////////////////////////////////////////////////
  2539. string PPScope::
  2540. expand_and(const string &params) {
  2541. // Split the string up into tokens based on the commas.
  2542. vector<string> tokens;
  2543. tokenize_params(params, tokens, true);
  2544. vector<string>::const_iterator ti;
  2545. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  2546. if ((*ti).empty()) {
  2547. return string();
  2548. }
  2549. }
  2550. string result = "1";
  2551. if (!tokens.empty()) {
  2552. result = tokens.back();
  2553. }
  2554. return result;
  2555. }
  2556. ////////////////////////////////////////////////////////////////////
  2557. // Function: PPScope::expand_upcase
  2558. // Access: Private
  2559. // Description: Expands the "upcase" function variable.
  2560. ////////////////////////////////////////////////////////////////////
  2561. string PPScope::
  2562. expand_upcase(const string &params) {
  2563. string result = expand_string(params);
  2564. string::iterator si;
  2565. for (si = result.begin(); si != result.end(); ++si) {
  2566. (*si) = toupper(*si);
  2567. }
  2568. return result;
  2569. }
  2570. ////////////////////////////////////////////////////////////////////
  2571. // Function: PPScope::expand_downcase
  2572. // Access: Private
  2573. // Description: Expands the "downcase" function variable.
  2574. ////////////////////////////////////////////////////////////////////
  2575. string PPScope::
  2576. expand_downcase(const string &params) {
  2577. string result = expand_string(params);
  2578. string::iterator si;
  2579. for (si = result.begin(); si != result.end(); ++si) {
  2580. (*si) = tolower(*si);
  2581. }
  2582. return result;
  2583. }
  2584. ////////////////////////////////////////////////////////////////////
  2585. // Function: PPScope::expand_cdefine
  2586. // Access: Private
  2587. // Description: Expands the "cdefine" function variable. This is a
  2588. // convenience function to output a C-style #define or
  2589. // #undef statement based on the value of the named
  2590. // variable. If the named string is a variable whose
  2591. // definition is nonempty, this returns "#define varname
  2592. // definition". Otherwise, it returns "#undef varname".
  2593. // This is particularly useful for building up a
  2594. // config.h file.
  2595. ////////////////////////////////////////////////////////////////////
  2596. string PPScope::
  2597. expand_cdefine(const string &params) {
  2598. string varname = trim_blanks(params);
  2599. string expansion = trim_blanks(expand_variable(varname));
  2600. string result;
  2601. if (expansion.empty()) {
  2602. result = "#undef " + varname;
  2603. } else {
  2604. result = "#define " + varname + " " + expansion;
  2605. }
  2606. return result;
  2607. }
  2608. ////////////////////////////////////////////////////////////////////
  2609. // Function: PPScope::expand_closure
  2610. // Access: Private
  2611. // Description: Expands the "closure" function variable. This is a
  2612. // special function that recursively expands a map
  2613. // variable with the given parameter string until all
  2614. // definitions have been encountered.
  2615. ////////////////////////////////////////////////////////////////////
  2616. string PPScope::
  2617. expand_closure(const string &params) {
  2618. // Split the string up into tokens based on the commas.
  2619. vector<string> tokens;
  2620. tokenize_params(params, tokens, false);
  2621. if (tokens.size() != 2 && tokens.size() != 3) {
  2622. cerr << "closure requires two or three parameters.\n";
  2623. errors_occurred = true;
  2624. return string();
  2625. }
  2626. // The first parameter is the map variable name, the second
  2627. // parameter is the expression to evaluate, and the third parameter
  2628. // (if present) is the expression that leads to the recursive
  2629. // evaluation of the map variable.
  2630. string varname = expand_string(tokens[0]);
  2631. string expression = tokens[1];
  2632. string close_on = expression;
  2633. if (tokens.size() > 2) {
  2634. close_on = tokens[2];
  2635. }
  2636. const MapVariableDefinition &def = find_map_variable(varname);
  2637. if (&def == &_null_map_def) {
  2638. cerr << "Warning: undefined map variable: " << varname << "\n";
  2639. return string();
  2640. }
  2641. // Now evaluate the expression within this scope, and then again
  2642. // within each scope indicated by the result, and then within each
  2643. // scope indicated by *that* result, and so on. We need to keep
  2644. // track of the words we have already evaluated (hence the set), and
  2645. // we also need to keep track of all the partial results we have yet
  2646. // to evaluate (hence the vector of strings).
  2647. set<string> closure;
  2648. vector<string> results;
  2649. vector<string> next_pass;
  2650. // Start off with the expression evaluated within the starting
  2651. // scope.
  2652. results.push_back(expand_string(expression));
  2653. next_pass.push_back(expand_string(close_on));
  2654. while (!next_pass.empty()) {
  2655. // Pull off one of the partial results (it doesn't matter which
  2656. // one), and chop it up into its constituent words.
  2657. vector<string> pass;
  2658. tokenize_whitespace(next_pass.back(), pass);
  2659. next_pass.pop_back();
  2660. // And then map each of those words into scopes.
  2661. vector<string>::const_iterator wi;
  2662. for (wi = pass.begin(); wi != pass.end(); ++wi) {
  2663. const string &word = (*wi);
  2664. bool inserted = closure.insert(word).second;
  2665. if (inserted) {
  2666. // This is a new word, which presumably maps to a scope.
  2667. MapVariableDefinition::const_iterator di;
  2668. di = def.find(word);
  2669. if (di != def.end()) {
  2670. PPScope *scope = (*di).second;
  2671. // Evaluate the expression within this scope.
  2672. results.push_back(scope->expand_string(expression));
  2673. // What does close_on evaluate to within this scope? That
  2674. // points us to the next scope(s).
  2675. next_pass.push_back(scope->expand_string(close_on));
  2676. }
  2677. }
  2678. }
  2679. }
  2680. // Now we have the complete transitive closure of $[mapvar close_on].
  2681. string result = repaste(results, " ");
  2682. return result;
  2683. }
  2684. ////////////////////////////////////////////////////////////////////
  2685. // Function: PPScope::expand_unmapped
  2686. // Access: Private
  2687. // Description: Expands the "closure" function variable. This is a
  2688. // special function that returns all the arguments to a
  2689. // map variable, unchanged, that did *not* match any of
  2690. // the keys in the map.
  2691. ////////////////////////////////////////////////////////////////////
  2692. string PPScope::
  2693. expand_unmapped(const string &params) {
  2694. // Split the string up into tokens based on the commas.
  2695. vector<string> tokens;
  2696. tokenize_params(params, tokens, false);
  2697. if (tokens.size() != 2) {
  2698. cerr << "unmapped requires two parameters.\n";
  2699. errors_occurred = true;
  2700. return string();
  2701. }
  2702. // The first parameter is the map variable name, and the second
  2703. // parameter is the space-separated list of arguments to the map.
  2704. string varname = expand_string(tokens[0]);
  2705. vector<string> keys;
  2706. tokenize_whitespace(expand_string(tokens[1]), keys);
  2707. const MapVariableDefinition &def = find_map_variable(varname);
  2708. if (&def == &_null_map_def) {
  2709. cerr << "Warning: undefined map variable: " << varname << "\n";
  2710. return string();
  2711. }
  2712. vector<string> results;
  2713. vector<string>::const_iterator ki;
  2714. for (ki = keys.begin(); ki != keys.end(); ++ki) {
  2715. MapVariableDefinition::const_iterator di;
  2716. di = def.find(*ki);
  2717. if (di == def.end()) {
  2718. // This key was undefined.
  2719. results.push_back(*ki);
  2720. }
  2721. }
  2722. string result = repaste(results, " ");
  2723. return result;
  2724. }
  2725. ////////////////////////////////////////////////////////////////////
  2726. // Function: PPScope::expand_dependencies
  2727. // Access: Private
  2728. // Description: Expands the "dependencies" function variable. This
  2729. // function returns all of the inter-file dependencies
  2730. // that the named file(s) depend on, as defined by the
  2731. // #include directives appearing within the files.
  2732. ////////////////////////////////////////////////////////////////////
  2733. string PPScope::
  2734. expand_dependencies(const string &params) {
  2735. // Split the string up into filenames based on whitespace.
  2736. vector<string> filenames;
  2737. tokenize_whitespace(expand_string(params), filenames);
  2738. PPDirectory *directory = get_directory();
  2739. assert(directory != (PPDirectory *)NULL);
  2740. vector<string> results;
  2741. vector<string>::const_iterator fi;
  2742. for (fi = filenames.begin(); fi != filenames.end(); ++fi) {
  2743. PPDependableFile *file = directory->get_dependable_file(*fi, false);
  2744. assert(file != (PPDependableFile *)NULL);
  2745. vector<PPDependableFile *> files;
  2746. file->get_complete_dependencies(files);
  2747. vector<PPDependableFile *>::const_iterator dfi;
  2748. for (dfi = files.begin(); dfi != files.end(); ++dfi) {
  2749. PPDependableFile *df = (*dfi);
  2750. string rel_filename =
  2751. current_output_directory->get_rel_to(df->get_directory()) + "/" +
  2752. df->get_filename();
  2753. results.push_back(rel_filename);
  2754. }
  2755. }
  2756. sort(results.begin(), results.end());
  2757. results.erase(unique(results.begin(), results.end()), results.end());
  2758. string result = repaste(results, " ");
  2759. return result;
  2760. }
  2761. ////////////////////////////////////////////////////////////////////
  2762. // Function: PPScope::expand_foreach
  2763. // Access: Private
  2764. // Description: Expands the "foreach" function variable. This
  2765. // evaluates an expression once for each word of a list.
  2766. ////////////////////////////////////////////////////////////////////
  2767. string PPScope::
  2768. expand_foreach(const string &params) {
  2769. // Split the string up into tokens based on the commas.
  2770. vector<string> tokens;
  2771. tokenize_params(params, tokens, false);
  2772. if (tokens.size() != 3) {
  2773. cerr << "foreach requires three parameters.\n";
  2774. errors_occurred = true;
  2775. return string();
  2776. }
  2777. // The first parameter is the temporary variable name that holds
  2778. // each word as it is expanded; the second parameter is the
  2779. // space-separated list of words. The third parameter is the
  2780. // expression to evaluate.
  2781. string varname = trim_blanks(expand_string(tokens[0]));
  2782. vector<string> words;
  2783. tokenize_whitespace(expand_string(tokens[1]), words);
  2784. vector<string> results;
  2785. vector<string>::const_iterator wi;
  2786. for (wi = words.begin(); wi != words.end(); ++wi) {
  2787. define_variable(varname, *wi);
  2788. results.push_back(expand_string(tokens[2]));
  2789. }
  2790. string result = repaste(results, " ");
  2791. return result;
  2792. }
  2793. ////////////////////////////////////////////////////////////////////
  2794. // Function: PPScope::expand_forscopes
  2795. // Access: Private
  2796. // Description: Expands the "forscopes" function variable. This
  2797. // evaluates an expression once within each of a number
  2798. // of named nested scopes.
  2799. ////////////////////////////////////////////////////////////////////
  2800. string PPScope::
  2801. expand_forscopes(const string &params) {
  2802. // Split the string up into tokens based on the commas.
  2803. vector<string> tokens;
  2804. tokenize_params(params, tokens, false);
  2805. if (tokens.size() != 2) {
  2806. cerr << "forscopes requires two parameters.\n";
  2807. errors_occurred = true;
  2808. return string();
  2809. }
  2810. // The first parameter is the space-separated list of nested scope
  2811. // names. The second parameter is the expression to evaluate.
  2812. vector<string> scope_names;
  2813. tokenize_whitespace(expand_string(tokens[0]), scope_names);
  2814. if (_named_scopes == (PPNamedScopes *)NULL) {
  2815. return string();
  2816. }
  2817. // Now build up the list of scopes with these names.
  2818. PPNamedScopes::Scopes scopes;
  2819. vector<string>::const_iterator wi;
  2820. for (wi = scope_names.begin(); wi != scope_names.end(); ++wi) {
  2821. _named_scopes->get_scopes(*wi, scopes);
  2822. }
  2823. PPNamedScopes::sort_by_dependency(scopes);
  2824. // Now evaluate the expression within each scope.
  2825. vector<string> results;
  2826. PPNamedScopes::Scopes::const_iterator si;
  2827. for (si = scopes.begin(); si != scopes.end(); ++si) {
  2828. PPScope *scope = *si;
  2829. results.push_back(scope->expand_string(tokens[1]));
  2830. }
  2831. string result = repaste(results, " ");
  2832. return result;
  2833. }
  2834. ////////////////////////////////////////////////////////////////////
  2835. // Function: PPScope::expand_function
  2836. // Access: Private
  2837. // Description: Expands the user-defined function reference. This
  2838. // invokes the nested commands within the function body,
  2839. // and returns all the output text as one line. Quite a
  2840. // job, really.
  2841. ////////////////////////////////////////////////////////////////////
  2842. string PPScope::
  2843. expand_function(const string &funcname,
  2844. const PPSubroutine *sub, const string &params) {
  2845. PPScope::push_scope((PPScope *)this);
  2846. PPScope nested_scope(_named_scopes);
  2847. nested_scope.define_formals(funcname, sub->_formals, params);
  2848. #ifdef HAVE_SSTREAM
  2849. ostringstream ostr;
  2850. #else
  2851. ostrstream ostr;
  2852. #endif
  2853. PPCommandFile command(&nested_scope);
  2854. command.set_output(&ostr);
  2855. command.begin_read();
  2856. bool okflag = true;
  2857. vector<string>::const_iterator li;
  2858. for (li = sub->_lines.begin(); li != sub->_lines.end() && okflag; ++li) {
  2859. okflag = command.read_line(*li);
  2860. }
  2861. if (okflag) {
  2862. okflag = command.end_read();
  2863. }
  2864. // We don't do anything with okflag here. What can we do?
  2865. PPScope::pop_scope();
  2866. // Now get the output. We split it into words and then reconnect
  2867. // it, to replace all whitespace with spaces.
  2868. #ifdef HAVE_SSTREAM
  2869. string str = ostr.str();
  2870. #else
  2871. ostr << ends;
  2872. char *c_str = ostr.str();
  2873. string str = c_str;
  2874. delete[] c_str;
  2875. #endif
  2876. vector<string> results;
  2877. tokenize_whitespace(str, results);
  2878. string result = repaste(results, " ");
  2879. return result;
  2880. }
  2881. ////////////////////////////////////////////////////////////////////
  2882. // Function: PPScope::expand_map_variable
  2883. // Access: Private
  2884. // Description: Expands a map variable function reference. This
  2885. // looks up the given keys in the map and expands the
  2886. // first parameter for each corresponding scope.
  2887. ////////////////////////////////////////////////////////////////////
  2888. string PPScope::
  2889. expand_map_variable(const string &varname, const string &params) {
  2890. // Split the string up into tokens based on the commas, but don't
  2891. // expand the variables yet.
  2892. vector<string> tokens;
  2893. tokenize_params(params, tokens, false);
  2894. if (tokens.size() != 2) {
  2895. cerr << "map variable expansions require two parameters: $["
  2896. << varname << " " << params << "]\n";
  2897. errors_occurred = true;
  2898. return string();
  2899. }
  2900. // Split the second parameter into tokens based on the spaces. This
  2901. // is the set of keys.
  2902. vector<string> keys;
  2903. tokenize_whitespace(expand_string(tokens[1]), keys);
  2904. return expand_map_variable(varname, tokens[0], keys);
  2905. }
  2906. ////////////////////////////////////////////////////////////////////
  2907. // Function: PPScope::expand_map_variable
  2908. // Access: Private
  2909. // Description: Expands a map variable function reference. This
  2910. // looks up the given keys in the map and expands the
  2911. // expression for each corresponding scope.
  2912. ////////////////////////////////////////////////////////////////////
  2913. string PPScope::
  2914. expand_map_variable(const string &varname, const string &expression,
  2915. const vector<string> &keys) {
  2916. const MapVariableDefinition &def = find_map_variable(varname);
  2917. if (&def == &_null_map_def) {
  2918. cerr << "Warning: undefined map variable: " << varname << "\n";
  2919. return string();
  2920. }
  2921. vector<string> results;
  2922. // Now build up the set of expansions of the expression in the
  2923. // various scopes indicated by the keys.
  2924. vector<string>::const_iterator wi;
  2925. for (wi = keys.begin(); wi != keys.end(); ++wi) {
  2926. MapVariableDefinition::const_iterator di;
  2927. di = def.find(*wi);
  2928. if (di != def.end()) {
  2929. PPScope *scope = (*di).second;
  2930. string expansion = scope->expand_string(expression);
  2931. if (!expansion.empty()) {
  2932. results.push_back(expansion);
  2933. }
  2934. }
  2935. }
  2936. string result = repaste(results, " ");
  2937. return result;
  2938. }
  2939. ////////////////////////////////////////////////////////////////////
  2940. // Function: PPScope::r_expand_matrix
  2941. // Access: Private
  2942. // Description: The recursive implementation of expand_matrix().
  2943. // This generates all of the combinations from the
  2944. // indicated index into the words array, with the given
  2945. // prefix.
  2946. ////////////////////////////////////////////////////////////////////
  2947. void PPScope::
  2948. r_expand_matrix(vector<string> &results, const vector<vector<string> > &words,
  2949. int index, const string &prefix) {
  2950. if (index >= (int)words.size()) {
  2951. // This is the terminal condition.
  2952. results.push_back(prefix);
  2953. } else {
  2954. // Otherwise, tack on the next set of words, and recurse.
  2955. const vector<string> &w = words[index];
  2956. vector<string>::const_iterator wi;
  2957. for (wi = w.begin(); wi != w.end(); ++wi) {
  2958. r_expand_matrix(results, words, index + 1, prefix + (*wi));
  2959. }
  2960. }
  2961. }
  2962. ////////////////////////////////////////////////////////////////////
  2963. // Function: PPScope::p_find_map_variable
  2964. // Access: Private
  2965. // Description: The implementation of find_map_variable() for a
  2966. // particular static scope, without checking the stack.
  2967. ////////////////////////////////////////////////////////////////////
  2968. PPScope::MapVariableDefinition &PPScope::
  2969. p_find_map_variable(const string &varname) {
  2970. MapVariables::const_iterator mvi;
  2971. mvi = _map_variables.find(varname);
  2972. if (mvi != _map_variables.end()) {
  2973. return (MapVariableDefinition &)(*mvi).second;
  2974. }
  2975. if (_parent_scope != (PPScope *)NULL) {
  2976. return _parent_scope->p_find_map_variable(varname);
  2977. }
  2978. return _null_map_def;
  2979. }
  2980. ////////////////////////////////////////////////////////////////////
  2981. // Function: PPScope::glob_string
  2982. // Access: Private
  2983. // Description: Expands the words in the string as if they were a set
  2984. // of filenames using the shell globbing characters.
  2985. // Fills up the results vector (which the user should
  2986. // ensure is empty before calling) with the set of all
  2987. // files that actually match the globbing characters.
  2988. ////////////////////////////////////////////////////////////////////
  2989. void PPScope::
  2990. glob_string(const string &str, vector<string> &results) {
  2991. // The globbing is relative to THISDIRPREFIX, not necessarily the
  2992. // current directory.
  2993. string dirname = trim_blanks(expand_variable("THISDIRPREFIX"));
  2994. vector<string> words;
  2995. tokenize_whitespace(str, words);
  2996. vector<string>::const_iterator wi;
  2997. for (wi = words.begin(); wi != words.end(); ++wi) {
  2998. GlobPattern glob(*wi);
  2999. glob.match_files(results, dirname);
  3000. }
  3001. // Sort the results into alphabetical order.
  3002. sort(results.begin(), results.end());
  3003. }