ppScope.cxx 108 KB

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