ppScope.cxx 95 KB

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