ppScope.cxx 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062
  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 == "findstring") {
  751. return expand_findstring(params);
  752. } else if (funcname == "dir") {
  753. return expand_dir(params);
  754. } else if (funcname == "notdir") {
  755. return expand_notdir(params);
  756. } else if (funcname == "suffix") {
  757. return expand_suffix(params);
  758. } else if (funcname == "basename") {
  759. return expand_basename(params);
  760. } else if (funcname == "word") {
  761. return expand_word(params);
  762. } else if (funcname == "wordlist") {
  763. return expand_wordlist(params);
  764. } else if (funcname == "words") {
  765. return expand_words(params);
  766. } else if (funcname == "firstword") {
  767. return expand_firstword(params);
  768. } else if (funcname == "patsubst") {
  769. return expand_patsubst(params, true);
  770. } else if (funcname == "patsubstw") {
  771. return expand_patsubst(params, false);
  772. } else if (funcname == "subst") {
  773. return expand_subst(params);
  774. } else if (funcname == "wordsubst") {
  775. return expand_wordsubst(params);
  776. } else if (funcname == "filter") {
  777. return expand_filter(params);
  778. } else if (funcname == "filter_out" || funcname == "filter-out") {
  779. return expand_filter_out(params);
  780. } else if (funcname == "sort") {
  781. return expand_sort(params);
  782. } else if (funcname == "unique") {
  783. return expand_unique(params);
  784. } else if (funcname == "matrix") {
  785. return expand_matrix(params);
  786. } else if (funcname == "if") {
  787. return expand_if(params);
  788. } else if (funcname == "eq") {
  789. return expand_eq(params);
  790. } else if (funcname == "ne") {
  791. return expand_ne(params);
  792. } else if (funcname == "=" || funcname == "==") {
  793. return expand_eqn(params);
  794. } else if (funcname == "!=") {
  795. return expand_nen(params);
  796. } else if (funcname == "<") {
  797. return expand_ltn(params);
  798. } else if (funcname == "<=") {
  799. return expand_len(params);
  800. } else if (funcname == ">") {
  801. return expand_gtn(params);
  802. } else if (funcname == ">=") {
  803. return expand_gen(params);
  804. } else if (funcname == "not") {
  805. return expand_not(params);
  806. } else if (funcname == "or") {
  807. return expand_or(params);
  808. } else if (funcname == "and") {
  809. return expand_and(params);
  810. } else if (funcname == "upcase") {
  811. return expand_upcase(params);
  812. } else if (funcname == "downcase") {
  813. return expand_downcase(params);
  814. } else if (funcname == "cdefine") {
  815. return expand_cdefine(params);
  816. } else if (funcname == "closure") {
  817. return expand_closure(params);
  818. } else if (funcname == "unmapped") {
  819. return expand_unmapped(params);
  820. } else if (funcname == "dependencies") {
  821. return expand_dependencies(params);
  822. } else if (funcname == "foreach") {
  823. return expand_foreach(params);
  824. } else if (funcname == "forscopes") {
  825. return expand_forscopes(params);
  826. }
  827. // It must be a map variable.
  828. return expand_map_variable(funcname, params);
  829. }
  830. // Now we have the variable name; was it previously expanded?
  831. ExpandedVariable *ev;
  832. for (ev = expanded; ev != (ExpandedVariable *)NULL; ev = ev->_next) {
  833. if (ev->_varname == varname) {
  834. // Yes, this is a cyclical expansion.
  835. cerr << "Ignoring cyclical expansion of " << varname << "\n";
  836. return string();
  837. }
  838. }
  839. // And now expand the variable.
  840. string expansion;
  841. // Check for a special inline patsubst operation, like GNU make:
  842. // $[varname:%.c=%.o]
  843. string patsubst;
  844. bool got_patsubst = false;
  845. p = varname.find(variable_patsubst);
  846. if (p != string::npos) {
  847. got_patsubst = true;
  848. patsubst = varname.substr(p + variable_patsubst.length());
  849. varname = varname.substr(0, p);
  850. }
  851. // Check for special scoping operators in the variable name.
  852. p = varname.find(VARIABLE_OPEN_NESTED);
  853. if (p != string::npos && varname[varname.length() - 1] == VARIABLE_CLOSE_NESTED) {
  854. size_t q = varname.length() - 1;
  855. string scope_names = varname.substr(p + 1, q - (p + 1));
  856. varname = varname.substr(0, p);
  857. expansion = expand_variable_nested(varname, scope_names);
  858. } else {
  859. // No special scoping; just expand the variable name.
  860. expansion = get_variable(varname);
  861. }
  862. // Finally, recursively expand any variable references in the
  863. // variable's expansion.
  864. ExpandedVariable new_var;
  865. new_var._varname = varname;
  866. new_var._next = expanded;
  867. string result = r_expand_string(expansion, &new_var);
  868. // And *then* apply any inline patsubst.
  869. if (got_patsubst) {
  870. vector<string> tokens;
  871. tokenize(patsubst, tokens, VARIABLE_PATSUBST_DELIM);
  872. if (tokens.size() != 2) {
  873. cerr << "inline patsubst should be of the form "
  874. << VARIABLE_PREFIX << VARIABLE_OPEN_BRACE << "varname"
  875. << VARIABLE_PATSUBST << PATTERN_WILDCARD << ".c"
  876. << VARIABLE_PATSUBST_DELIM << PATTERN_WILDCARD << ".o"
  877. << VARIABLE_CLOSE_BRACE << ".\n";
  878. } else {
  879. PPFilenamePattern from(tokens[0]);
  880. PPFilenamePattern to(tokens[1]);
  881. if (!from.has_wildcard() || !to.has_wildcard()) {
  882. cerr << "The two parameters of inline patsubst must both include "
  883. << PATTERN_WILDCARD << ".\n";
  884. return string();
  885. }
  886. // Split the expansion into tokens based on the spaces.
  887. vector<string> words;
  888. tokenize_whitespace(result, words);
  889. vector<string>::iterator wi;
  890. for (wi = words.begin(); wi != words.end(); ++wi) {
  891. (*wi) = to.transform(*wi, from);
  892. }
  893. result = repaste(words, " ");
  894. }
  895. }
  896. return result;
  897. }
  898. ////////////////////////////////////////////////////////////////////
  899. // Function: PPScope::expand_variable_nested
  900. // Access: Private
  901. // Description: Expands a variable reference of the form
  902. // $[varname(scope scope scope)]. This means to
  903. // concatenate the expansions of the variable in all of
  904. // the named scopes.
  905. ////////////////////////////////////////////////////////////////////
  906. string PPScope::
  907. expand_variable_nested(const string &varname,
  908. const string &scope_names) {
  909. if (_named_scopes == (PPNamedScopes *)NULL) {
  910. return string();
  911. }
  912. vector<string> names;
  913. tokenize_whitespace(scope_names, names);
  914. // Get all of the named scopes.
  915. PPNamedScopes::Scopes scopes;
  916. vector<string>::const_iterator ni;
  917. for (ni = names.begin(); ni != names.end(); ++ni) {
  918. const string &name = (*ni);
  919. _named_scopes->get_scopes(name, scopes);
  920. }
  921. if (scopes.empty()) {
  922. return string();
  923. }
  924. // Now go through the scopes and build up the results.
  925. vector<string> results;
  926. PPNamedScopes::Scopes::const_iterator si;
  927. for (si = scopes.begin(); si != scopes.end(); ++si) {
  928. PPScope *scope = (*si);
  929. string nested = scope->expand_variable(varname);
  930. if (!nested.empty()) {
  931. results.push_back(nested);
  932. }
  933. }
  934. string result = repaste(results, " ");
  935. return result;
  936. }
  937. ////////////////////////////////////////////////////////////////////
  938. // Function: PPScope::expand_isfullpath
  939. // Access: Private
  940. // Description: Expands the "isfullpath" function variable. This
  941. // returns true (actually, the same as its input) if the
  942. // input parameter is a fully-specified path name,
  943. // meaning it begins with a slash for unix_platform, and
  944. // it begins with a slash or backslash, with an optional
  945. // drive leterr, for windows_platform.
  946. ////////////////////////////////////////////////////////////////////
  947. string PPScope::
  948. expand_isfullpath(const string &params) {
  949. string filename = trim_blanks(expand_string(params));
  950. string result;
  951. if (is_fullpath(filename)) {
  952. result = filename;
  953. }
  954. return result;
  955. }
  956. ////////////////////////////////////////////////////////////////////
  957. // Function: PPScope::expand_osfilename
  958. // Access: Private
  959. // Description: Expands the "osfilename" function variable. This
  960. // converts the filename from a Unix-style filename
  961. // (e.g. with slash separators) to a platform-specific
  962. // filename. Currently, this only has an effect when
  963. // generating code for a Windows platform: it simply
  964. // converts forward slashes to backslashes. On other
  965. // platforms it has no effect.
  966. //
  967. // This is different from cygpath_w in that (a) it works
  968. // regardless of whether we are actually running under
  969. // Cygwin, and (b) it does nothing more intelligent than
  970. // reverse slashes.
  971. ////////////////////////////////////////////////////////////////////
  972. string PPScope::
  973. expand_osfilename(const string &params) {
  974. // Split the parameter into tokens based on the spaces.
  975. vector<string> words;
  976. tokenize_whitespace(expand_string(params), words);
  977. vector<string>::iterator wi;
  978. for (wi = words.begin(); wi != words.end(); ++wi) {
  979. (*wi) = to_os_filename(*wi);
  980. }
  981. string result = repaste(words, " ");
  982. return result;
  983. }
  984. ////////////////////////////////////////////////////////////////////
  985. // Function: PPScope::expand_unixfilename
  986. // Access: Private
  987. // Description: Expands the "unixfilename" function variable. This
  988. // converts the filename from a platform-specific
  989. // filename to a Unix-style filename (e.g. with slash
  990. // separators). Currently, this only has an effect when
  991. // generating code for a Windows platform: it simply
  992. // converts backslashes to forward slashes. On other
  993. // platforms it has no effect.
  994. //
  995. // This is different from cygpath_p in that (a) it works
  996. // regardless of whether we are actually running under
  997. // Cygwin, and (b) it does nothing more intelligent than
  998. // reverse slashes.
  999. ////////////////////////////////////////////////////////////////////
  1000. string PPScope::
  1001. expand_unixfilename(const string &params) {
  1002. // Split the parameter into tokens based on the spaces.
  1003. vector<string> words;
  1004. tokenize_whitespace(expand_string(params), words);
  1005. vector<string>::iterator wi;
  1006. for (wi = words.begin(); wi != words.end(); ++wi) {
  1007. (*wi) = to_unix_filename(*wi);
  1008. }
  1009. string result = repaste(words, " ");
  1010. return result;
  1011. }
  1012. ////////////////////////////////////////////////////////////////////
  1013. // Function: PPScope::expand_cygpath_w
  1014. // Access: Private
  1015. // Description: Expands the "cygpath_w" function variable. This is
  1016. // equivalent to $[shell cygpath -w ...] when running
  1017. // under Cygwin, and returns the parameter itself when
  1018. // not running under Cygwin.
  1019. ////////////////////////////////////////////////////////////////////
  1020. string PPScope::
  1021. expand_cygpath_w(const string &params) {
  1022. string filename = trim_blanks(expand_string(params));
  1023. #ifdef __CYGWIN__
  1024. char result[4096];
  1025. cygwin_conv_to_win32_path(filename.c_str(), result);
  1026. filename = result;
  1027. #endif
  1028. return filename;
  1029. }
  1030. ////////////////////////////////////////////////////////////////////
  1031. // Function: PPScope::expand_cygpath_p
  1032. // Access: Private
  1033. // Description: Expands the "cygpath_p" function variable. This is
  1034. // equivalent to $[shell cygpath -p ...] when running
  1035. // under Cygwin, and returns the parameter itself when
  1036. // not running under Cygwin.
  1037. ////////////////////////////////////////////////////////////////////
  1038. string PPScope::
  1039. expand_cygpath_p(const string &params) {
  1040. string filename = trim_blanks(expand_string(params));
  1041. #ifdef __CYGWIN__
  1042. char result[4096];
  1043. cygwin_conv_to_posix_path(filename.c_str(), result);
  1044. filename = result;
  1045. #endif
  1046. return filename;
  1047. }
  1048. ////////////////////////////////////////////////////////////////////
  1049. // Function: PPScope::expand_wildcard
  1050. // Access: Private
  1051. // Description: Expands the "wildcard" function variable. This
  1052. // returns the set of files matched by the parameters
  1053. // with shell matching characters.
  1054. ////////////////////////////////////////////////////////////////////
  1055. string PPScope::
  1056. expand_wildcard(const string &params) {
  1057. vector<string> results;
  1058. glob_string(expand_string(params), results);
  1059. string result = repaste(results, " ");
  1060. return result;
  1061. }
  1062. ////////////////////////////////////////////////////////////////////
  1063. // Function: PPScope::expand_isdir
  1064. // Access: Private
  1065. // Description: Expands the "isdir" function variable. This
  1066. // returns true if the parameter exists and is a
  1067. // directory, or false otherwise. This actually expands
  1068. // the parameter(s) with shell globbing characters,
  1069. // similar to the "wildcard" function, and looks only at
  1070. // the first expansion.
  1071. ////////////////////////////////////////////////////////////////////
  1072. string PPScope::
  1073. expand_isdir(const string &params) {
  1074. vector<string> results;
  1075. glob_string(expand_string(params), results);
  1076. if (results.empty()) {
  1077. // No matching file, too bad.
  1078. return string();
  1079. }
  1080. const string &filename = results[0];
  1081. struct stat stbuf;
  1082. string result;
  1083. if (stat(filename.c_str(), &stbuf) == 0) {
  1084. if (S_ISDIR(stbuf.st_mode)) {
  1085. result = filename;
  1086. }
  1087. }
  1088. return result;
  1089. }
  1090. ////////////////////////////////////////////////////////////////////
  1091. // Function: PPScope::expand_isfile
  1092. // Access: Private
  1093. // Description: Expands the "isfile" function variable. This
  1094. // returns true if the parameter exists and is a
  1095. // regular file, or false otherwise. This actually
  1096. // expands the parameter(s) with shell globbing
  1097. // characters, similar to the "wildcard" function, and
  1098. // looks only at the first expansion.
  1099. ////////////////////////////////////////////////////////////////////
  1100. string PPScope::
  1101. expand_isfile(const string &params) {
  1102. vector<string> results;
  1103. glob_string(expand_string(params), results);
  1104. if (results.empty()) {
  1105. // No matching file, too bad.
  1106. return string();
  1107. }
  1108. const string &filename = results[0];
  1109. struct stat stbuf;
  1110. string result;
  1111. if (stat(filename.c_str(), &stbuf) == 0) {
  1112. if (S_ISREG(stbuf.st_mode)) {
  1113. result = filename;
  1114. }
  1115. }
  1116. return result;
  1117. }
  1118. ////////////////////////////////////////////////////////////////////
  1119. // Function: PPScope::expand_libtest
  1120. // Access: Private
  1121. // Description: Expands the "libtest" function variable. This
  1122. // serves as a poor man's autoconf feature to check to
  1123. // see if a library by the given name exists on the
  1124. // indicated search path, or on the system search path.
  1125. ////////////////////////////////////////////////////////////////////
  1126. string PPScope::
  1127. expand_libtest(const string &params) {
  1128. // Get the parameters out based on commas. The first parameter is a
  1129. // space-separated set of directories to search, the second
  1130. // parameter is a space-separated set of library names.
  1131. vector<string> tokens;
  1132. tokenize_params(params, tokens, true);
  1133. if (tokens.size() != 2) {
  1134. cerr << "libtest requires two parameters.\n";
  1135. return string();
  1136. }
  1137. vector<string> directories;
  1138. tokenize_whitespace(tokens[0], directories);
  1139. // Also add the system directories to the list, whatever we think
  1140. // those should be. Here we have to make a few assumptions.
  1141. #ifdef PLATFORM_WIN32
  1142. const char *windir = getenv("WINDIR");
  1143. if (windir != (const char *)NULL) {
  1144. directories.push_back(string(windir) + "\\System");
  1145. directories.push_back(string(windir) + "\\System32");
  1146. }
  1147. const char *lib = getenv("LIB");
  1148. if (lib != (const char *)NULL) {
  1149. tokenize(lib, directories, ";");
  1150. }
  1151. #endif
  1152. // We'll also check the Unix standard places, even if we're building
  1153. // Windows, since we might be using Cygwin.
  1154. // Check LD_LIBRARY_PATH.
  1155. const char *ld_library_path = getenv("LD_LIBRARY_PATH");
  1156. if (ld_library_path != (const char *)NULL) {
  1157. tokenize(ld_library_path, directories, ":");
  1158. }
  1159. directories.push_back("/lib");
  1160. directories.push_back("/usr/lib");
  1161. vector<string> libnames;
  1162. tokenize_whitespace(tokens[1], libnames);
  1163. if (libnames.empty()) {
  1164. // No libraries is a default "false".
  1165. return string();
  1166. }
  1167. // We only bother to search for the first library name in the list.
  1168. string libname = libnames[0];
  1169. string found;
  1170. #ifdef PLATFORM_WIN32
  1171. if (libname.length() > 4 && libname.substr(libname.length() - 4) == ".lib") {
  1172. found = find_searchpath(directories, libname);
  1173. if (found.empty()) {
  1174. found = find_searchpath(directories, libname.substr(0, libname.length() - 4) + ".dll");
  1175. }
  1176. } else {
  1177. found = find_searchpath(directories, "lib" + libname + ".lib");
  1178. if (found.empty()) {
  1179. found = find_searchpath(directories, "lib" + libname + ".dll");
  1180. }
  1181. }
  1182. #else
  1183. found = find_searchpath(directories, "lib" + libname + ".a");
  1184. if (found.empty()) {
  1185. found = find_searchpath(directories, "lib" + libname + ".so");
  1186. }
  1187. #endif
  1188. return found;
  1189. }
  1190. ////////////////////////////////////////////////////////////////////
  1191. // Function: PPScope::expand_bintest
  1192. // Access: Private
  1193. // Description: Expands the "bintest" function variable. This
  1194. // serves as a poor man's autoconf feature to check to
  1195. // see if an executable program by the given name exists
  1196. // on the indicated search path, or on the system search
  1197. // path.
  1198. ////////////////////////////////////////////////////////////////////
  1199. string PPScope::
  1200. expand_bintest(const string &params) {
  1201. // We only have one parameter: the filename of the executable. We
  1202. // always search for it on the path.
  1203. string binname = expand_string(params);
  1204. if (binname.empty()) {
  1205. // No binary, no exist.
  1206. return string();
  1207. }
  1208. // An explicit path from the root does not require a search.
  1209. #ifdef PLATFORM_WIN32
  1210. if ((binname.length() > 2 && binname[1] == ':') || binname[0] == '/')
  1211. #else
  1212. if (binname[0] == '/')
  1213. #endif
  1214. {
  1215. if (access(binname.c_str(), F_OK) == 0) {
  1216. return binname;
  1217. }
  1218. return string();
  1219. }
  1220. const char *path = getenv("PATH");
  1221. if (path == (const char *)NULL) {
  1222. // If the path is undefined, too bad.
  1223. return string();
  1224. }
  1225. string pathvar(path);
  1226. vector<string> directories;
  1227. #ifdef PLATFORM_WIN32
  1228. if (pathvar.find(';') != string::npos) {
  1229. // If the path contains semicolons, it's a native Windows-style
  1230. // path: split it up based on semicolons.
  1231. tokenize(pathvar, directories, ";");
  1232. } else {
  1233. // Otherwise, assume it's a Cygwin-style path: split it up based
  1234. // on colons.
  1235. tokenize(pathvar, directories, ":");
  1236. }
  1237. #else
  1238. tokenize(pathvar, directories, ":");
  1239. #endif
  1240. string found;
  1241. #ifdef PLATFORM_WIN32
  1242. found = find_searchpath(directories, binname + ".exe");
  1243. if (found.empty()) {
  1244. found = find_searchpath(directories, binname);
  1245. }
  1246. #else
  1247. found = find_searchpath(directories, binname);
  1248. #endif
  1249. return found;
  1250. }
  1251. ////////////////////////////////////////////////////////////////////
  1252. // Function: PPScope::expand_shell
  1253. // Access: Private
  1254. // Description: Expands the "shell" function variable. This executes
  1255. // the given command in a subprocess and returns the
  1256. // standard output.
  1257. ////////////////////////////////////////////////////////////////////
  1258. string PPScope::
  1259. expand_shell(const string &params) {
  1260. // We run $[shell] commands within the directory indicated by
  1261. // $[THISDIRPREFIX]. This way, local filenames will be expanded the
  1262. // way we expect.
  1263. string dirname = trim_blanks(expand_variable("THISDIRPREFIX"));
  1264. string command = expand_string(params);
  1265. int pid, status;
  1266. int pd[2];
  1267. if (pipe(pd) < 0) {
  1268. // pipe() failed.
  1269. perror("pipe");
  1270. return string();
  1271. }
  1272. pid = fork();
  1273. if (pid < 0) {
  1274. // fork() failed.
  1275. perror("fork");
  1276. return string();
  1277. }
  1278. if (pid == 0) {
  1279. // Child.
  1280. if (!dirname.empty()) {
  1281. // We don't have to restore the directory after we're done,
  1282. // because we're doing the chdir() call only within the child
  1283. // process.
  1284. if (chdir(dirname.c_str()) < 0) {
  1285. perror("chdir");
  1286. }
  1287. }
  1288. close(pd[0]);
  1289. dup2(pd[1], STDOUT_FILENO);
  1290. char *argv[4];
  1291. argv[0] = "sh";
  1292. argv[1] = "-c";
  1293. argv[2] = (char *)command.c_str();
  1294. argv[3] = (char *)NULL;
  1295. execv("/bin/sh", argv);
  1296. exit(127);
  1297. }
  1298. // Parent. Wait for the child to terminate, and read from its
  1299. // output while we're waiting.
  1300. close(pd[1]);
  1301. bool child_done = false;
  1302. bool pipe_closed = false;
  1303. string output;
  1304. while (!child_done && !pipe_closed) {
  1305. static const int buffer_size = 1024;
  1306. char buffer[buffer_size];
  1307. int read_bytes = (int)read(pd[0], buffer, buffer_size);
  1308. if (read_bytes < 0) {
  1309. perror("read");
  1310. } else if (read_bytes == 0) {
  1311. pipe_closed = true;
  1312. } else {
  1313. output += string(buffer, read_bytes);
  1314. }
  1315. if (!child_done) {
  1316. int waitresult = waitpid(pid, &status, WNOHANG);
  1317. if (waitresult < 0) {
  1318. if (errno != EINTR) {
  1319. perror("waitpid");
  1320. return string();
  1321. }
  1322. } else if (waitresult > 0) {
  1323. child_done = true;
  1324. }
  1325. }
  1326. }
  1327. close(pd[0]);
  1328. // Now get the output. We split it into words and then reconnect
  1329. // it, to simulate the shell's backpop operator.
  1330. vector<string> results;
  1331. tokenize_whitespace(output, results);
  1332. string result = repaste(results, " ");
  1333. return result;
  1334. }
  1335. ////////////////////////////////////////////////////////////////////
  1336. // Function: PPScope::expand_standardize
  1337. // Access: Private
  1338. // Description: Expands the "standardize" function variable. This
  1339. // converts the filename to standard form by removing
  1340. // consecutive repeated slashes and collapsing /../
  1341. // where possible.
  1342. ////////////////////////////////////////////////////////////////////
  1343. string PPScope::
  1344. expand_standardize(const string &params) {
  1345. string filename = trim_blanks(expand_string(params));
  1346. if (filename.empty()) {
  1347. return string();
  1348. }
  1349. vector<string> components;
  1350. // Pull off the components of the filename one at a time.
  1351. bool global = (filename[0] == '/');
  1352. size_t p = 0;
  1353. while (p < filename.length() && filename[p] == '/') {
  1354. p++;
  1355. }
  1356. while (p < filename.length()) {
  1357. size_t slash = filename.find('/', p);
  1358. string component = filename.substr(p, slash - p);
  1359. if (component == ".") {
  1360. // Ignore /./.
  1361. } else if (component == ".." && !components.empty() &&
  1362. !(components.back() == "..")) {
  1363. // Back up.
  1364. components.pop_back();
  1365. } else {
  1366. components.push_back(component);
  1367. }
  1368. p = slash;
  1369. while (p < filename.length() && filename[p] == '/') {
  1370. p++;
  1371. }
  1372. }
  1373. // Now reassemble the filename.
  1374. string result;
  1375. if (global) {
  1376. result = "/";
  1377. }
  1378. if (!components.empty()) {
  1379. result += components[0];
  1380. for (int i = 1; i < (int)components.size(); i++) {
  1381. result += "/" + components[i];
  1382. }
  1383. }
  1384. return result;
  1385. }
  1386. ////////////////////////////////////////////////////////////////////
  1387. // Function: PPScope::expand_length
  1388. // Access: Private
  1389. // Description: Expands the "length" function variable. This returns
  1390. // the length of the argument in characters.
  1391. ////////////////////////////////////////////////////////////////////
  1392. string PPScope::
  1393. expand_length(const string &params) {
  1394. string word = trim_blanks(expand_string(params));
  1395. char buffer[32];
  1396. sprintf(buffer, "%d", word.length());
  1397. string result = buffer;
  1398. return result;
  1399. }
  1400. ////////////////////////////////////////////////////////////////////
  1401. // Function: PPScope::expand_substr
  1402. // Access: Private
  1403. // Description: Expands the "substr" function variable. $[substr
  1404. // S,E,string] returns the substring of "string"
  1405. // beginning at character S (1-based) and continuing to
  1406. // character E, inclusive.
  1407. ////////////////////////////////////////////////////////////////////
  1408. string PPScope::
  1409. expand_substr(const string &params) {
  1410. // Split the string up into tokens based on the commas.
  1411. vector<string> tokens;
  1412. tokenize_params(params, tokens, true);
  1413. if (tokens.size() != 3) {
  1414. cerr << "substr requires three parameters.\n";
  1415. return string();
  1416. }
  1417. int start = atoi(tokens[0].c_str());
  1418. int end = atoi(tokens[1].c_str());
  1419. if (end < start) {
  1420. // Following GNU make's convention, we swap start and end if
  1421. // they're out of order.
  1422. int t = end;
  1423. end = start;
  1424. start = t;
  1425. }
  1426. const string &word = tokens[2];
  1427. start = max(start, 1);
  1428. end = min(end, (int)word.length());
  1429. if (end < start) {
  1430. return string();
  1431. }
  1432. string result = word.substr(start - 1, end - start + 1);
  1433. return result;
  1434. }
  1435. ////////////////////////////////////////////////////////////////////
  1436. // Function: PPScope::expand_dir
  1437. // Access: Private
  1438. // Description: Expands the "dir" function variable. This returns
  1439. // the directory part of its filename argument(s), or ./
  1440. // if the words contain no slash.
  1441. ////////////////////////////////////////////////////////////////////
  1442. string PPScope::
  1443. expand_dir(const string &params) {
  1444. // Split the parameter into tokens based on the spaces.
  1445. vector<string> words;
  1446. tokenize_whitespace(expand_string(params), words);
  1447. vector<string>::iterator wi;
  1448. for (wi = words.begin(); wi != words.end(); ++wi) {
  1449. string &word = (*wi);
  1450. size_t slash = word.rfind('/');
  1451. if (slash != string::npos) {
  1452. word = word.substr(0, slash + 1);
  1453. } else {
  1454. word = "./";
  1455. }
  1456. }
  1457. string result = repaste(words, " ");
  1458. return result;
  1459. }
  1460. ////////////////////////////////////////////////////////////////////
  1461. // Function: PPScope::expand_notdir
  1462. // Access: Private
  1463. // Description: Expands the "notdir" function variable. This returns
  1464. // everything following the rightmost slash, or the
  1465. // string itself if there is no slash.
  1466. ////////////////////////////////////////////////////////////////////
  1467. string PPScope::
  1468. expand_notdir(const string &params) {
  1469. // Split the parameter into tokens based on the spaces.
  1470. vector<string> words;
  1471. tokenize_whitespace(expand_string(params), words);
  1472. vector<string>::iterator wi;
  1473. for (wi = words.begin(); wi != words.end(); ++wi) {
  1474. string &word = (*wi);
  1475. size_t slash = word.rfind('/');
  1476. if (slash != string::npos) {
  1477. word = word.substr(slash + 1);
  1478. }
  1479. }
  1480. string result = repaste(words, " ");
  1481. return result;
  1482. }
  1483. ////////////////////////////////////////////////////////////////////
  1484. // Function: PPScope::expand_suffix
  1485. // Access: Private
  1486. // Description: Expands the "suffix" function variable. This returns
  1487. // the filename extension, including a dot, if any.
  1488. ////////////////////////////////////////////////////////////////////
  1489. string PPScope::
  1490. expand_suffix(const string &params) {
  1491. // Split the parameter into tokens based on the spaces.
  1492. vector<string> words;
  1493. tokenize_whitespace(expand_string(params), words);
  1494. vector<string>::iterator wi;
  1495. for (wi = words.begin(); wi != words.end(); ++wi) {
  1496. string &word = (*wi);
  1497. size_t dot = word.rfind('.');
  1498. if (dot != string::npos) {
  1499. string ext = word.substr(dot);
  1500. if (ext.find('/') == string::npos) {
  1501. word = ext;
  1502. } else {
  1503. word = string();
  1504. }
  1505. } else {
  1506. word = string();
  1507. }
  1508. }
  1509. string result = repaste(words, " ");
  1510. return result;
  1511. }
  1512. ////////////////////////////////////////////////////////////////////
  1513. // Function: PPScope::expand_basename
  1514. // Access: Private
  1515. // Description: Expands the "basename" function variable. This returns
  1516. // everything but the filename extension (including the
  1517. // directory, if any).
  1518. ////////////////////////////////////////////////////////////////////
  1519. string PPScope::
  1520. expand_basename(const string &params) {
  1521. // Split the parameter into tokens based on the spaces.
  1522. vector<string> words;
  1523. tokenize_whitespace(expand_string(params), words);
  1524. vector<string>::iterator wi;
  1525. for (wi = words.begin(); wi != words.end(); ++wi) {
  1526. string &word = (*wi);
  1527. size_t dot = word.rfind('.');
  1528. if (dot != string::npos) {
  1529. string ext = word.substr(dot);
  1530. if (ext.find('/') == string::npos) {
  1531. word = word.substr(0, dot);
  1532. }
  1533. }
  1534. }
  1535. string result = repaste(words, " ");
  1536. return result;
  1537. }
  1538. ////////////////////////////////////////////////////////////////////
  1539. // Function: PPScope::expand_word
  1540. // Access: Private
  1541. // Description: Expands the "word" function variable. This returns
  1542. // the nth word, 1-based, of the space-separated list of
  1543. // words in the second parameter.
  1544. ////////////////////////////////////////////////////////////////////
  1545. string PPScope::
  1546. expand_word(const string &params) {
  1547. // Split the string up into tokens based on the commas.
  1548. vector<string> tokens;
  1549. tokenize_params(params, tokens, true);
  1550. if (tokens.size() != 2) {
  1551. cerr << "word requires two parameters.\n";
  1552. return string();
  1553. }
  1554. int index = atoi(tokens[0].c_str());
  1555. // Split the second parameter into tokens based on the spaces.
  1556. vector<string> words;
  1557. tokenize_whitespace(expand_string(tokens[1]), words);
  1558. if (index < 1 || index > (int)words.size()) {
  1559. // Out of range.
  1560. return string();
  1561. }
  1562. return words[index - 1];
  1563. }
  1564. ////////////////////////////////////////////////////////////////////
  1565. // Function: PPScope::expand_wordlist
  1566. // Access: Private
  1567. // Description: Expands the "wordlist" function variable. This
  1568. // returns a range of words, 1-based, of the
  1569. // space-separated list of words in the third parameter.
  1570. ////////////////////////////////////////////////////////////////////
  1571. string PPScope::
  1572. expand_wordlist(const string &params) {
  1573. // Split the string up into tokens based on the commas.
  1574. vector<string> tokens;
  1575. tokenize_params(params, tokens, true);
  1576. if (tokens.size() != 3) {
  1577. cerr << "wordlist requires three parameters.\n";
  1578. return string();
  1579. }
  1580. int start = atoi(tokens[0].c_str());
  1581. int end = atoi(tokens[1].c_str());
  1582. if (end < start) {
  1583. // Following GNU make's convention, we swap start and end if
  1584. // they're out of order.
  1585. int t = end;
  1586. end = start;
  1587. start = t;
  1588. }
  1589. // Split the third parameter into tokens based on the spaces.
  1590. vector<string> words;
  1591. tokenize_whitespace(expand_string(tokens[2]), words);
  1592. start = max(start, 1);
  1593. end = min(end, (int)words.size());
  1594. if (end < start) {
  1595. return string();
  1596. }
  1597. vector<string> results;
  1598. results.insert(results.end(),
  1599. words.begin() + start - 1,
  1600. words.begin() + end - 1);
  1601. string result = repaste(results, " ");
  1602. return result;
  1603. }
  1604. ////////////////////////////////////////////////////////////////////
  1605. // Function: PPScope::expand_words
  1606. // Access: Private
  1607. // Description: Expands the "words" function variable. This
  1608. // returns the number of space-separated words in the
  1609. // list.
  1610. ////////////////////////////////////////////////////////////////////
  1611. string PPScope::
  1612. expand_words(const string &params) {
  1613. // Split the parameter into tokens based on the spaces.
  1614. vector<string> words;
  1615. tokenize_whitespace(expand_string(params), words);
  1616. char buffer[32];
  1617. sprintf(buffer, "%d", words.size());
  1618. string result = buffer;
  1619. return result;
  1620. }
  1621. ////////////////////////////////////////////////////////////////////
  1622. // Function: PPScope::expand_firstword
  1623. // Access: Private
  1624. // Description: Expands the "firstword" function variable. This
  1625. // returns the first of several words separated by
  1626. // whitespace.
  1627. ////////////////////////////////////////////////////////////////////
  1628. string PPScope::
  1629. expand_firstword(const string &params) {
  1630. // Split the parameter into tokens based on the spaces.
  1631. vector<string> words;
  1632. tokenize_whitespace(expand_string(params), words);
  1633. if (!words.empty()) {
  1634. return words[0];
  1635. }
  1636. return string();
  1637. }
  1638. ////////////////////////////////////////////////////////////////////
  1639. // Function: PPScope::expand_patsubst
  1640. // Access: Private
  1641. // Description: Expands the "patsubst" function variable.
  1642. ////////////////////////////////////////////////////////////////////
  1643. string PPScope::
  1644. expand_patsubst(const string &params, bool separate_words) {
  1645. // Split the string up into tokens based on the commas.
  1646. vector<string> tokens;
  1647. tokenize_params(params, tokens, true);
  1648. if (tokens.size() < 3) {
  1649. cerr << "patsubst requires at least three parameters.\n";
  1650. return string();
  1651. }
  1652. if ((tokens.size() % 2) != 1) {
  1653. cerr << "subst requires an odd number of parameters.\n";
  1654. return string();
  1655. }
  1656. // Split the last parameter into tokens based on the spaces--but
  1657. // only if separate_words is true.
  1658. vector<string> words;
  1659. if (separate_words) {
  1660. tokenize_whitespace(tokens.back(), words);
  1661. } else {
  1662. words.push_back(tokens.back());
  1663. }
  1664. // Build up a vector of from/to patterns.
  1665. typedef vector<PPFilenamePattern> Patterns;
  1666. typedef vector<Patterns> FromPatterns;
  1667. FromPatterns from;
  1668. Patterns to;
  1669. size_t i;
  1670. for (i = 0; i < tokens.size() - 1; i += 2) {
  1671. // Each "from" pattern might be a collection of patterns separated
  1672. // by spaces.
  1673. from.push_back(Patterns());
  1674. vector<string> froms;
  1675. tokenize_whitespace(tokens[i], froms);
  1676. vector<string>::const_iterator fi;
  1677. for (fi = froms.begin(); fi != froms.end(); ++fi) {
  1678. PPFilenamePattern pattern(*fi);
  1679. if (!pattern.has_wildcard()) {
  1680. cerr << "All the \"from\" parameters of patsubst must include "
  1681. << PATTERN_WILDCARD << ".\n";
  1682. return string();
  1683. }
  1684. from.back().push_back(pattern);
  1685. }
  1686. // However, the corresponding "to" pattern is just one pattern.
  1687. to.push_back(PPFilenamePattern(tokens[i + 1]));
  1688. }
  1689. size_t num_patterns = from.size();
  1690. assert(num_patterns == to.size());
  1691. vector<string>::iterator wi;
  1692. for (wi = words.begin(); wi != words.end(); ++wi) {
  1693. bool matched = false;
  1694. for (i = 0; i < num_patterns && !matched; i++) {
  1695. Patterns::const_iterator pi;
  1696. for (pi = from[i].begin(); pi != from[i].end() && !matched; ++pi) {
  1697. if ((*pi).matches(*wi)) {
  1698. matched = true;
  1699. (*wi) = to[i].transform(*wi, (*pi));
  1700. }
  1701. }
  1702. }
  1703. }
  1704. string result = repaste(words, " ");
  1705. return result;
  1706. }
  1707. ////////////////////////////////////////////////////////////////////
  1708. // Function: PPScope::expand_filter
  1709. // Access: Private
  1710. // Description: Expands the "filter" function variable.
  1711. ////////////////////////////////////////////////////////////////////
  1712. string PPScope::
  1713. expand_filter(const string &params) {
  1714. // Split the string up into tokens based on the commas.
  1715. vector<string> tokens;
  1716. tokenize_params(params, tokens, true);
  1717. if (tokens.size() != 2) {
  1718. cerr << "filter requires two parameters.\n";
  1719. return string();
  1720. }
  1721. // Split up the first parameter--the list of patterns to filter
  1722. // by--into tokens based on the spaces.
  1723. vector<string> pattern_strings;
  1724. tokenize_whitespace(tokens[0], pattern_strings);
  1725. vector<PPFilenamePattern> patterns;
  1726. vector<string>::const_iterator psi;
  1727. for (psi = pattern_strings.begin(); psi != pattern_strings.end(); ++psi) {
  1728. patterns.push_back(PPFilenamePattern(*psi));
  1729. }
  1730. // Split up the second parameter--the list of words to filter--into
  1731. // tokens based on the spaces.
  1732. vector<string> words;
  1733. tokenize_whitespace(tokens[1], words);
  1734. vector<string>::iterator wi, wnext;
  1735. wnext = words.begin();
  1736. for (wi = words.begin(); wi != words.end(); ++wi) {
  1737. const string &word = (*wi);
  1738. bool matches_pattern = false;
  1739. vector<PPFilenamePattern>::const_iterator pi;
  1740. for (pi = patterns.begin();
  1741. pi != patterns.end() && !matches_pattern;
  1742. ++pi) {
  1743. matches_pattern = (*pi).matches(word);
  1744. }
  1745. if (matches_pattern) {
  1746. *wnext++ = word;
  1747. }
  1748. }
  1749. words.erase(wnext, words.end());
  1750. string result = repaste(words, " ");
  1751. return result;
  1752. }
  1753. ////////////////////////////////////////////////////////////////////
  1754. // Function: PPScope::expand_filter_out
  1755. // Access: Private
  1756. // Description: Expands the "filter_out" function variable.
  1757. ////////////////////////////////////////////////////////////////////
  1758. string PPScope::
  1759. expand_filter_out(const string &params) {
  1760. // Split the string up into tokens based on the commas.
  1761. vector<string> tokens;
  1762. tokenize_params(params, tokens, true);
  1763. if (tokens.size() != 2) {
  1764. cerr << "filter-out requires two parameters.\n";
  1765. return string();
  1766. }
  1767. // Split up the first parameter--the list of patterns to filter
  1768. // by--into tokens based on the spaces.
  1769. vector<string> pattern_strings;
  1770. tokenize_whitespace(tokens[0], pattern_strings);
  1771. vector<PPFilenamePattern> patterns;
  1772. vector<string>::const_iterator psi;
  1773. for (psi = pattern_strings.begin(); psi != pattern_strings.end(); ++psi) {
  1774. patterns.push_back(PPFilenamePattern(*psi));
  1775. }
  1776. // Split up the second parameter--the list of words to filter--into
  1777. // tokens based on the spaces.
  1778. vector<string> words;
  1779. tokenize_whitespace(tokens[1], words);
  1780. vector<string>::iterator wi, wnext;
  1781. wnext = words.begin();
  1782. for (wi = words.begin(); wi != words.end(); ++wi) {
  1783. const string &word = (*wi);
  1784. bool matches_pattern = false;
  1785. vector<PPFilenamePattern>::const_iterator pi;
  1786. for (pi = patterns.begin();
  1787. pi != patterns.end() && !matches_pattern;
  1788. ++pi) {
  1789. matches_pattern = (*pi).matches(word);
  1790. }
  1791. if (!matches_pattern) {
  1792. *wnext++ = word;
  1793. }
  1794. }
  1795. words.erase(wnext, words.end());
  1796. string result = repaste(words, " ");
  1797. return result;
  1798. }
  1799. ////////////////////////////////////////////////////////////////////
  1800. // Function: PPScope::expand_subst
  1801. // Access: Private
  1802. // Description: Expands the "subst" function variable.
  1803. ////////////////////////////////////////////////////////////////////
  1804. string PPScope::
  1805. expand_subst(const string &params) {
  1806. // Split the string up into tokens based on the commas.
  1807. vector<string> tokens;
  1808. tokenize_params(params, tokens, true);
  1809. if (tokens.size() < 3) {
  1810. cerr << "subst requires at least three parameters.\n";
  1811. return string();
  1812. }
  1813. if ((tokens.size() % 2) != 1) {
  1814. cerr << "subst requires an odd number of parameters.\n";
  1815. return string();
  1816. }
  1817. // Now substitute each of the substitute strings out for the
  1818. // replacement strings.
  1819. string str = tokens.back();
  1820. for (size_t i = 0; i < tokens.size() - 1; i += 2) {
  1821. string new_str;
  1822. const string &subst = tokens[i];
  1823. const string &repl = tokens[i + 1];
  1824. size_t q = 0;
  1825. size_t p = str.find(subst, q);
  1826. while (p != string::npos) {
  1827. new_str += str.substr(q, p - q) + repl;
  1828. q = p + subst.length();
  1829. p = str.find(subst, q);
  1830. }
  1831. str = new_str + str.substr(q);
  1832. }
  1833. return str;
  1834. }
  1835. ////////////////////////////////////////////////////////////////////
  1836. // Function: PPScope::expand_findstrnig
  1837. // Access: Private
  1838. // Description: Expands the "findstring" function variable.
  1839. // $[findstring a,b] returns b if and only if it is a
  1840. // substring of a; otherwise, it returns the empty
  1841. // string.
  1842. ////////////////////////////////////////////////////////////////////
  1843. string PPScope::
  1844. expand_findstring(const string &params) {
  1845. // Split the string up into tokens based on the commas.
  1846. vector<string> tokens;
  1847. tokenize_params(params, tokens, true);
  1848. if (tokens.size() != 2) {
  1849. cerr << "findstring requires two parameters.\n";
  1850. return string();
  1851. }
  1852. string str = tokens.back();
  1853. const string &srchstr = tokens[0];
  1854. size_t q = 0;
  1855. size_t p = str.find(srchstr, q);
  1856. if(p == string::npos)
  1857. str = "";
  1858. return str;
  1859. }
  1860. ////////////////////////////////////////////////////////////////////
  1861. // Function: PPScope::expand_wordsubst
  1862. // Access: Private
  1863. // Description: Expands the "wordsubst" function variable. This is
  1864. // like "subst" except it only replaces whole words.
  1865. ////////////////////////////////////////////////////////////////////
  1866. string PPScope::
  1867. expand_wordsubst(const string &params) {
  1868. // Split the string up into tokens based on the commas.
  1869. vector<string> tokens;
  1870. tokenize_params(params, tokens, true);
  1871. if (tokens.size() < 3) {
  1872. cerr << "subst requires at least three parameters.\n";
  1873. return string();
  1874. }
  1875. if ((tokens.size() % 2) != 1) {
  1876. cerr << "subst requires an odd number of parameters.\n";
  1877. return string();
  1878. }
  1879. // Split the last parameter into tokens based on the spaces.
  1880. vector<string> words;
  1881. tokenize_whitespace(tokens.back(), words);
  1882. for (size_t i = 0; i < tokens.size() - 1; i += 2) {
  1883. const string &subst = tokens[i];
  1884. const string &repl = tokens[i + 1];
  1885. vector<string>::iterator wi;
  1886. for (wi = words.begin(); wi != words.end(); ++wi) {
  1887. if ((*wi) == subst) {
  1888. (*wi) = repl;
  1889. }
  1890. }
  1891. }
  1892. string result = repaste(words, " ");
  1893. return result;
  1894. }
  1895. ////////////////////////////////////////////////////////////////////
  1896. // Function: PPScope::expand_sort
  1897. // Access: Private
  1898. // Description: Expands the "sort" function variable: sort the words
  1899. // into alphabetical order, and also remove duplicates.
  1900. ////////////////////////////////////////////////////////////////////
  1901. string PPScope::
  1902. expand_sort(const string &params) {
  1903. // Split the string up into tokens based on the spaces.
  1904. vector<string> words;
  1905. tokenize_whitespace(expand_string(params), words);
  1906. sort(words.begin(), words.end());
  1907. words.erase(unique(words.begin(), words.end()), words.end());
  1908. string result = repaste(words, " ");
  1909. return result;
  1910. }
  1911. ////////////////////////////////////////////////////////////////////
  1912. // Function: PPScope::expand_unique
  1913. // Access: Private
  1914. // Description: Expands the "unique" function variable: remove
  1915. // duplicates from the list of words without changing
  1916. // the order. The first appearance of each word
  1917. // remains.
  1918. ////////////////////////////////////////////////////////////////////
  1919. string PPScope::
  1920. expand_unique(const string &params) {
  1921. // Split the string up into tokens based on the spaces.
  1922. vector<string> words;
  1923. tokenize_whitespace(expand_string(params), words);
  1924. vector<string>::iterator win, wout;
  1925. set<string> included_words;
  1926. win = words.begin();
  1927. wout = words.begin();
  1928. while (win != words.end()) {
  1929. if (included_words.insert(*win).second) {
  1930. // This is a unique word so far.
  1931. *wout++ = *win;
  1932. }
  1933. ++win;
  1934. }
  1935. words.erase(wout, words.end());
  1936. string result = repaste(words, " ");
  1937. return result;
  1938. }
  1939. ////////////////////////////////////////////////////////////////////
  1940. // Function: PPScope::expand_matrix
  1941. // Access: Private
  1942. // Description: Expands the "matrix" function variable. This
  1943. // combines the different words of the n parameters in
  1944. // all possible ways, like the shell {a,b,c} expansion
  1945. // characters. For example, $[matrix a b,c,10 20 30]
  1946. // expands to ac10 ac20 ac30 bc10 bc20 bc30.
  1947. ////////////////////////////////////////////////////////////////////
  1948. string PPScope::
  1949. expand_matrix(const string &params) {
  1950. // Split the string up into tokens based on the commas.
  1951. vector<string> tokens;
  1952. tokenize_params(params, tokens, true);
  1953. // Each token gets split up into words based on the spaces.
  1954. vector<vector<string> > words;
  1955. for (int i = 0; i < (int)tokens.size(); i++) {
  1956. words.push_back(vector<string>());
  1957. tokenize_whitespace(tokens[i], words.back());
  1958. }
  1959. // Now synthesize the results recursively.
  1960. vector<string> results;
  1961. r_expand_matrix(results, words, 0, "");
  1962. string result = repaste(results, " ");
  1963. return result;
  1964. }
  1965. ////////////////////////////////////////////////////////////////////
  1966. // Function: PPScope::expand_if
  1967. // Access: Private
  1968. // Description: Expands the "if" function variable. This evaluates
  1969. // the first parameter and returns the second parameter
  1970. // if the result is true (i.e. nonempty) and the third
  1971. // parameter (if present) if the result is faluse
  1972. // (i.e. empty).
  1973. ////////////////////////////////////////////////////////////////////
  1974. string PPScope::
  1975. expand_if(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. if (!tokens[0].empty()) {
  1981. return tokens[1];
  1982. } else {
  1983. return "";
  1984. }
  1985. } else if (tokens.size() == 3) {
  1986. if (!tokens[0].empty()) {
  1987. return tokens[1];
  1988. } else {
  1989. return tokens[2];
  1990. }
  1991. }
  1992. cerr << "if requires two or three parameters.\n";
  1993. return string();
  1994. }
  1995. ////////////////////////////////////////////////////////////////////
  1996. // Function: PPScope::expand_eq
  1997. // Access: Private
  1998. // Description: Expands the "eq" function variable. This tests
  1999. // string equivalence.
  2000. ////////////////////////////////////////////////////////////////////
  2001. string PPScope::
  2002. expand_eq(const string &params) {
  2003. // Split the string up into tokens based on the commas.
  2004. vector<string> tokens;
  2005. tokenize_params(params, tokens, true);
  2006. if (tokens.size() != 2) {
  2007. cerr << "eq requires two parameters.\n";
  2008. return string();
  2009. }
  2010. string result;
  2011. if (tokens[0] == tokens[1]) {
  2012. result = "1";
  2013. }
  2014. return result;
  2015. }
  2016. ////////////////////////////////////////////////////////////////////
  2017. // Function: PPScope::expand_ne
  2018. // Access: Private
  2019. // Description: Expands the "ne" function variable. This tests
  2020. // string equivalence.
  2021. ////////////////////////////////////////////////////////////////////
  2022. string PPScope::
  2023. expand_ne(const string &params) {
  2024. // Split the string up into tokens based on the commas.
  2025. vector<string> tokens;
  2026. tokenize_params(params, tokens, true);
  2027. if (tokens.size() != 2) {
  2028. cerr << "ne requires two parameters.\n";
  2029. return string();
  2030. }
  2031. string result;
  2032. if (!(tokens[0] == tokens[1])) {
  2033. result = "1";
  2034. }
  2035. return result;
  2036. }
  2037. ////////////////////////////////////////////////////////////////////
  2038. // Function: PPScope::expand_eqn
  2039. // Access: Private
  2040. // Description: Expands the "=" function variable. This tests
  2041. // numeric equivalence.
  2042. ////////////////////////////////////////////////////////////////////
  2043. string PPScope::
  2044. expand_eqn(const string &params) {
  2045. double a, b;
  2046. if (!tokenize_numeric_pair(params, a, b)) {
  2047. return string();
  2048. }
  2049. string result;
  2050. if (a == b) {
  2051. result = "1";
  2052. }
  2053. return result;
  2054. }
  2055. ////////////////////////////////////////////////////////////////////
  2056. // Function: PPScope::expand_nen
  2057. // Access: Private
  2058. // Description: Expands the "!=" function variable. This tests
  2059. // numeric equivalence.
  2060. ////////////////////////////////////////////////////////////////////
  2061. string PPScope::
  2062. expand_nen(const string &params) {
  2063. double a, b;
  2064. if (!tokenize_numeric_pair(params, a, b)) {
  2065. return string();
  2066. }
  2067. string result;
  2068. if (a != b) {
  2069. result = "1";
  2070. }
  2071. return result;
  2072. }
  2073. ////////////////////////////////////////////////////////////////////
  2074. // Function: PPScope::expand_ltn
  2075. // Access: Private
  2076. // Description: Expands the "<" function variable. This tests
  2077. // numeric relationships.
  2078. ////////////////////////////////////////////////////////////////////
  2079. string PPScope::
  2080. expand_ltn(const string &params) {
  2081. double a, b;
  2082. if (!tokenize_numeric_pair(params, a, b)) {
  2083. return string();
  2084. }
  2085. string result;
  2086. if (a < b) {
  2087. result = "1";
  2088. }
  2089. return result;
  2090. }
  2091. ////////////////////////////////////////////////////////////////////
  2092. // Function: PPScope::expand_len
  2093. // Access: Private
  2094. // Description: Expands the "<=" function variable. This tests
  2095. // numeric relationships.
  2096. ////////////////////////////////////////////////////////////////////
  2097. string PPScope::
  2098. expand_len(const string &params) {
  2099. double a, b;
  2100. if (!tokenize_numeric_pair(params, a, b)) {
  2101. return string();
  2102. }
  2103. string result;
  2104. if (a <= b) {
  2105. result = "1";
  2106. }
  2107. return result;
  2108. }
  2109. ////////////////////////////////////////////////////////////////////
  2110. // Function: PPScope::expand_gtn
  2111. // Access: Private
  2112. // Description: Expands the ">" function variable. This tests
  2113. // numeric relationships.
  2114. ////////////////////////////////////////////////////////////////////
  2115. string PPScope::
  2116. expand_gtn(const string &params) {
  2117. double a, b;
  2118. if (!tokenize_numeric_pair(params, a, b)) {
  2119. return string();
  2120. }
  2121. string result;
  2122. if (a > b) {
  2123. result = "1";
  2124. }
  2125. return result;
  2126. }
  2127. ////////////////////////////////////////////////////////////////////
  2128. // Function: PPScope::expand_gen
  2129. // Access: Private
  2130. // Description: Expands the ">=" function variable. This tests
  2131. // numeric relationships.
  2132. ////////////////////////////////////////////////////////////////////
  2133. string PPScope::
  2134. expand_gen(const string &params) {
  2135. double a, b;
  2136. if (!tokenize_numeric_pair(params, a, b)) {
  2137. return string();
  2138. }
  2139. string result;
  2140. if (a >= b) {
  2141. result = "1";
  2142. }
  2143. return result;
  2144. }
  2145. ////////////////////////////////////////////////////////////////////
  2146. // Function: PPScope::expand_not
  2147. // Access: Private
  2148. // Description: Expands the "not" function variable. This returns
  2149. // nonempty if its argument is empty, empty if its
  2150. // argument is nonempty.
  2151. ////////////////////////////////////////////////////////////////////
  2152. string PPScope::
  2153. expand_not(const string &params) {
  2154. // Split the string up into tokens based on the commas.
  2155. vector<string> tokens;
  2156. tokenize_params(params, tokens, true);
  2157. if (tokens.size() != 1) {
  2158. cerr << "not requires one parameter.\n";
  2159. return string();
  2160. }
  2161. string result;
  2162. if (tokens[0].empty()) {
  2163. result = "1";
  2164. }
  2165. return result;
  2166. }
  2167. ////////////////////////////////////////////////////////////////////
  2168. // Function: PPScope::expand_or
  2169. // Access: Private
  2170. // Description: Expands the "or" function variable. This returns
  2171. // nonempty if any of its arguments are nonempty.
  2172. // Specifically, it returns the first nonempty argument.
  2173. ////////////////////////////////////////////////////////////////////
  2174. string PPScope::
  2175. expand_or(const string &params) {
  2176. // Split the string up into tokens based on the commas.
  2177. vector<string> tokens;
  2178. tokenize_params(params, tokens, true);
  2179. vector<string>::const_iterator ti;
  2180. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  2181. if (!(*ti).empty()) {
  2182. return (*ti);
  2183. }
  2184. }
  2185. return string();
  2186. }
  2187. ////////////////////////////////////////////////////////////////////
  2188. // Function: PPScope::expand_and
  2189. // Access: Private
  2190. // Description: Expands the "and" function variable. This returns
  2191. // nonempty if all of its arguments are nonempty.
  2192. // Specifically, it returns the last argument.
  2193. ////////////////////////////////////////////////////////////////////
  2194. string PPScope::
  2195. expand_and(const string &params) {
  2196. // Split the string up into tokens based on the commas.
  2197. vector<string> tokens;
  2198. tokenize_params(params, tokens, true);
  2199. vector<string>::const_iterator ti;
  2200. for (ti = tokens.begin(); ti != tokens.end(); ++ti) {
  2201. if ((*ti).empty()) {
  2202. return string();
  2203. }
  2204. }
  2205. string result = "1";
  2206. if (!tokens.empty()) {
  2207. result = tokens.back();
  2208. }
  2209. return result;
  2210. }
  2211. ////////////////////////////////////////////////////////////////////
  2212. // Function: PPScope::expand_upcase
  2213. // Access: Private
  2214. // Description: Expands the "upcase" function variable.
  2215. ////////////////////////////////////////////////////////////////////
  2216. string PPScope::
  2217. expand_upcase(const string &params) {
  2218. string result = expand_string(params);
  2219. string::iterator si;
  2220. for (si = result.begin(); si != result.end(); ++si) {
  2221. (*si) = toupper(*si);
  2222. }
  2223. return result;
  2224. }
  2225. ////////////////////////////////////////////////////////////////////
  2226. // Function: PPScope::expand_downcase
  2227. // Access: Private
  2228. // Description: Expands the "downcase" function variable.
  2229. ////////////////////////////////////////////////////////////////////
  2230. string PPScope::
  2231. expand_downcase(const string &params) {
  2232. string result = expand_string(params);
  2233. string::iterator si;
  2234. for (si = result.begin(); si != result.end(); ++si) {
  2235. (*si) = tolower(*si);
  2236. }
  2237. return result;
  2238. }
  2239. ////////////////////////////////////////////////////////////////////
  2240. // Function: PPScope::expand_cdefine
  2241. // Access: Private
  2242. // Description: Expands the "cdefine" function variable. This is a
  2243. // convenience function to output a C-style #define or
  2244. // #undef statement based on the value of the named
  2245. // variable. If the named string is a variable whose
  2246. // definition is nonempty, this returns "#define varname
  2247. // definition". Otherwise, it returns "#undef varname".
  2248. // This is particularly useful for building up a
  2249. // config.h file.
  2250. ////////////////////////////////////////////////////////////////////
  2251. string PPScope::
  2252. expand_cdefine(const string &params) {
  2253. string varname = trim_blanks(params);
  2254. string expansion = trim_blanks(expand_variable(varname));
  2255. string result;
  2256. if (expansion.empty()) {
  2257. result = "#undef " + varname;
  2258. } else {
  2259. result = "#define " + varname + " " + expansion;
  2260. }
  2261. return result;
  2262. }
  2263. ////////////////////////////////////////////////////////////////////
  2264. // Function: PPScope::expand_closure
  2265. // Access: Private
  2266. // Description: Expands the "closure" function variable. This is a
  2267. // special function that recursively expands a map
  2268. // variable with the given parameter string until all
  2269. // definitions have been encountered.
  2270. ////////////////////////////////////////////////////////////////////
  2271. string PPScope::
  2272. expand_closure(const string &params) {
  2273. // Split the string up into tokens based on the commas.
  2274. vector<string> tokens;
  2275. tokenize_params(params, tokens, false);
  2276. if (tokens.size() != 2 && tokens.size() != 3) {
  2277. cerr << "closure requires two or three parameters.\n";
  2278. return string();
  2279. }
  2280. // The first parameter is the map variable name, the second
  2281. // parameter is the expression to evaluate, and the third parameter
  2282. // (if present) is the expression that leads to the recursive
  2283. // evaluation of the map variable.
  2284. string varname = expand_string(tokens[0]);
  2285. string expression = tokens[1];
  2286. string close_on = expression;
  2287. if (tokens.size() > 2) {
  2288. close_on = tokens[2];
  2289. }
  2290. const MapVariableDefinition &def = find_map_variable(varname);
  2291. if (&def == &_null_map_def) {
  2292. cerr << "Warning: undefined map variable: " << varname << "\n";
  2293. return string();
  2294. }
  2295. // Now evaluate the expression within this scope, and then again
  2296. // within each scope indicated by the result, and then within each
  2297. // scope indicated by *that* result, and so on. We need to keep
  2298. // track of the words we have already evaluated (hence the set), and
  2299. // we also need to keep track of all the partial results we have yet
  2300. // to evaluate (hence the vector of strings).
  2301. set<string> closure;
  2302. vector<string> results;
  2303. vector<string> next_pass;
  2304. // Start off with the expression evaluated within the starting
  2305. // scope.
  2306. results.push_back(expand_string(expression));
  2307. next_pass.push_back(expand_string(close_on));
  2308. while (!next_pass.empty()) {
  2309. // Pull off one of the partial results (it doesn't matter which
  2310. // one), and chop it up into its constituent words.
  2311. vector<string> pass;
  2312. tokenize_whitespace(next_pass.back(), pass);
  2313. next_pass.pop_back();
  2314. // And then map each of those words into scopes.
  2315. vector<string>::const_iterator wi;
  2316. for (wi = pass.begin(); wi != pass.end(); ++wi) {
  2317. const string &word = (*wi);
  2318. bool inserted = closure.insert(word).second;
  2319. if (inserted) {
  2320. // This is a new word, which presumably maps to a scope.
  2321. MapVariableDefinition::const_iterator di;
  2322. di = def.find(word);
  2323. if (di != def.end()) {
  2324. PPScope *scope = (*di).second;
  2325. // Evaluate the expression within this scope.
  2326. results.push_back(scope->expand_string(expression));
  2327. // What does close_on evaluate to within this scope? That
  2328. // points us to the next scope(s).
  2329. next_pass.push_back(scope->expand_string(close_on));
  2330. }
  2331. }
  2332. }
  2333. }
  2334. // Now we have the complete transitive closure of $[mapvar close_on].
  2335. string result = repaste(results, " ");
  2336. return result;
  2337. }
  2338. ////////////////////////////////////////////////////////////////////
  2339. // Function: PPScope::expand_unmapped
  2340. // Access: Private
  2341. // Description: Expands the "closure" function variable. This is a
  2342. // special function that returns all the arguments to a
  2343. // map variable, unchanged, that did *not* match any of
  2344. // the keys in the map.
  2345. ////////////////////////////////////////////////////////////////////
  2346. string PPScope::
  2347. expand_unmapped(const string &params) {
  2348. // Split the string up into tokens based on the commas.
  2349. vector<string> tokens;
  2350. tokenize_params(params, tokens, false);
  2351. if (tokens.size() != 2) {
  2352. cerr << "unmapped requires two parameters.\n";
  2353. return string();
  2354. }
  2355. // The first parameter is the map variable name, and the second
  2356. // parameter is the space-separated list of arguments to the map.
  2357. string varname = expand_string(tokens[0]);
  2358. vector<string> keys;
  2359. tokenize_whitespace(expand_string(tokens[1]), keys);
  2360. const MapVariableDefinition &def = find_map_variable(varname);
  2361. if (&def == &_null_map_def) {
  2362. cerr << "Warning: undefined map variable: " << varname << "\n";
  2363. return string();
  2364. }
  2365. vector<string> results;
  2366. vector<string>::const_iterator ki;
  2367. for (ki = keys.begin(); ki != keys.end(); ++ki) {
  2368. MapVariableDefinition::const_iterator di;
  2369. di = def.find(*ki);
  2370. if (di == def.end()) {
  2371. // This key was undefined.
  2372. results.push_back(*ki);
  2373. }
  2374. }
  2375. string result = repaste(results, " ");
  2376. return result;
  2377. }
  2378. ////////////////////////////////////////////////////////////////////
  2379. // Function: PPScope::expand_dependencies
  2380. // Access: Private
  2381. // Description: Expands the "dependencies" function variable. This
  2382. // function returns all of the inter-file dependencies
  2383. // that the named file(s) depend on, as defined by the
  2384. // #include directives appearing within the files.
  2385. ////////////////////////////////////////////////////////////////////
  2386. string PPScope::
  2387. expand_dependencies(const string &params) {
  2388. // Split the string up into filenames based on whitespace.
  2389. vector<string> filenames;
  2390. tokenize_whitespace(expand_string(params), filenames);
  2391. PPDirectory *directory = get_directory();
  2392. assert(directory != (PPDirectory *)NULL);
  2393. vector<string> results;
  2394. vector<string>::const_iterator fi;
  2395. for (fi = filenames.begin(); fi != filenames.end(); ++fi) {
  2396. PPDependableFile *file = directory->get_dependable_file(*fi, false);
  2397. assert(file != (PPDependableFile *)NULL);
  2398. vector<PPDependableFile *> files;
  2399. file->get_complete_dependencies(files);
  2400. vector<PPDependableFile *>::const_iterator dfi;
  2401. for (dfi = files.begin(); dfi != files.end(); ++dfi) {
  2402. PPDependableFile *df = (*dfi);
  2403. string rel_filename =
  2404. current_output_directory->get_rel_to(df->get_directory()) + "/" +
  2405. df->get_filename();
  2406. results.push_back(rel_filename);
  2407. }
  2408. }
  2409. sort(results.begin(), results.end());
  2410. results.erase(unique(results.begin(), results.end()), results.end());
  2411. string result = repaste(results, " ");
  2412. return result;
  2413. }
  2414. ////////////////////////////////////////////////////////////////////
  2415. // Function: PPScope::expand_foreach
  2416. // Access: Private
  2417. // Description: Expands the "foreach" function variable. This
  2418. // evaluates an expression once for each word of a list.
  2419. ////////////////////////////////////////////////////////////////////
  2420. string PPScope::
  2421. expand_foreach(const string &params) {
  2422. // Split the string up into tokens based on the commas.
  2423. vector<string> tokens;
  2424. tokenize_params(params, tokens, false);
  2425. if (tokens.size() != 3) {
  2426. cerr << "foreach requires three parameters.\n";
  2427. return string();
  2428. }
  2429. // The first parameter is the temporary variable name that holds
  2430. // each word as it is expanded; the second parameter is the
  2431. // space-separated list of words. The third parameter is the
  2432. // expression to evaluate.
  2433. string varname = trim_blanks(expand_string(tokens[0]));
  2434. vector<string> words;
  2435. tokenize_whitespace(expand_string(tokens[1]), words);
  2436. vector<string> results;
  2437. vector<string>::const_iterator wi;
  2438. for (wi = words.begin(); wi != words.end(); ++wi) {
  2439. define_variable(varname, *wi);
  2440. results.push_back(expand_string(tokens[2]));
  2441. }
  2442. string result = repaste(results, " ");
  2443. return result;
  2444. }
  2445. ////////////////////////////////////////////////////////////////////
  2446. // Function: PPScope::expand_forscopes
  2447. // Access: Private
  2448. // Description: Expands the "forscopes" function variable. This
  2449. // evaluates an expression once within each of a number
  2450. // of named nested scopes.
  2451. ////////////////////////////////////////////////////////////////////
  2452. string PPScope::
  2453. expand_forscopes(const string &params) {
  2454. // Split the string up into tokens based on the commas.
  2455. vector<string> tokens;
  2456. tokenize_params(params, tokens, false);
  2457. if (tokens.size() != 2) {
  2458. cerr << "forscopes requires two parameters.\n";
  2459. return string();
  2460. }
  2461. // The first parameter is the space-separated list of nested scope
  2462. // names. The second parameter is the expression to evaluate.
  2463. vector<string> scope_names;
  2464. tokenize_whitespace(expand_string(tokens[0]), scope_names);
  2465. if (_named_scopes == (PPNamedScopes *)NULL) {
  2466. return string();
  2467. }
  2468. // Now build up the list of scopes with these names.
  2469. PPNamedScopes::Scopes scopes;
  2470. vector<string>::const_iterator wi;
  2471. for (wi = scope_names.begin(); wi != scope_names.end(); ++wi) {
  2472. _named_scopes->get_scopes(*wi, scopes);
  2473. }
  2474. PPNamedScopes::sort_by_dependency(scopes);
  2475. // Now evaluate the expression within each scope.
  2476. vector<string> results;
  2477. PPNamedScopes::Scopes::const_iterator si;
  2478. for (si = scopes.begin(); si != scopes.end(); ++si) {
  2479. PPScope *scope = *si;
  2480. results.push_back(scope->expand_string(tokens[1]));
  2481. }
  2482. string result = repaste(results, " ");
  2483. return result;
  2484. }
  2485. ////////////////////////////////////////////////////////////////////
  2486. // Function: PPScope::expand_function
  2487. // Access: Private
  2488. // Description: Expands the user-defined function reference. This
  2489. // invokes the nested commands within the function body,
  2490. // and returns all the output text as one line. Quite a
  2491. // job, really.
  2492. ////////////////////////////////////////////////////////////////////
  2493. string PPScope::
  2494. expand_function(const string &funcname,
  2495. const PPSubroutine *sub, const string &params) {
  2496. PPScope::push_scope((PPScope *)this);
  2497. PPScope nested_scope(_named_scopes);
  2498. nested_scope.define_formals(funcname, sub->_formals, params);
  2499. // This won't compile on VC++. It has only ostringstream, which is
  2500. // functionally equivalent but has a slightly different interface.
  2501. ostrstream ostr;
  2502. PPCommandFile command(&nested_scope);
  2503. command.set_output(&ostr);
  2504. command.begin_read();
  2505. bool okflag = true;
  2506. vector<string>::const_iterator li;
  2507. for (li = sub->_lines.begin(); li != sub->_lines.end() && okflag; ++li) {
  2508. okflag = command.read_line(*li);
  2509. }
  2510. if (okflag) {
  2511. okflag = command.end_read();
  2512. }
  2513. // We don't do anything with okflag here. What can we do?
  2514. PPScope::pop_scope();
  2515. // Now get the output. We split it into words and then reconnect
  2516. // it, to replace all whitespace with spaces.
  2517. ostr << ends;
  2518. char *str = ostr.str();
  2519. vector<string> results;
  2520. tokenize_whitespace(str, results);
  2521. string result = repaste(results, " ");
  2522. delete[] str;
  2523. return result;
  2524. }
  2525. ////////////////////////////////////////////////////////////////////
  2526. // Function: PPScope::expand_map_variable
  2527. // Access: Private
  2528. // Description: Expands a map variable function reference. This
  2529. // looks up the given keys in the map and expands the
  2530. // first parameter for each corresponding scope.
  2531. ////////////////////////////////////////////////////////////////////
  2532. string PPScope::
  2533. expand_map_variable(const string &varname, const string &params) {
  2534. // Split the string up into tokens based on the commas, but don't
  2535. // expand the variables yet.
  2536. vector<string> tokens;
  2537. tokenize_params(params, tokens, false);
  2538. if (tokens.size() != 2) {
  2539. cerr << "map variable expansions require two parameters: $["
  2540. << varname << " " << params << "]\n";
  2541. return string();
  2542. }
  2543. // Split the second parameter into tokens based on the spaces. This
  2544. // is the set of keys.
  2545. vector<string> keys;
  2546. tokenize_whitespace(expand_string(tokens[1]), keys);
  2547. return expand_map_variable(varname, tokens[0], keys);
  2548. }
  2549. ////////////////////////////////////////////////////////////////////
  2550. // Function: PPScope::expand_map_variable
  2551. // Access: Private
  2552. // Description: Expands a map variable function reference. This
  2553. // looks up the given keys in the map and expands the
  2554. // expression for each corresponding scope.
  2555. ////////////////////////////////////////////////////////////////////
  2556. string PPScope::
  2557. expand_map_variable(const string &varname, const string &expression,
  2558. const vector<string> &keys) {
  2559. const MapVariableDefinition &def = find_map_variable(varname);
  2560. if (&def == &_null_map_def) {
  2561. cerr << "Warning: undefined map variable: " << varname << "\n";
  2562. return string();
  2563. }
  2564. vector<string> results;
  2565. // Now build up the set of expansions of the expression in the
  2566. // various scopes indicated by the keys.
  2567. vector<string>::const_iterator wi;
  2568. for (wi = keys.begin(); wi != keys.end(); ++wi) {
  2569. MapVariableDefinition::const_iterator di;
  2570. di = def.find(*wi);
  2571. if (di != def.end()) {
  2572. PPScope *scope = (*di).second;
  2573. string expansion = scope->expand_string(expression);
  2574. if (!expansion.empty()) {
  2575. results.push_back(expansion);
  2576. }
  2577. }
  2578. }
  2579. string result = repaste(results, " ");
  2580. return result;
  2581. }
  2582. ////////////////////////////////////////////////////////////////////
  2583. // Function: PPScope::r_expand_matrix
  2584. // Access: Private
  2585. // Description: The recursive implementation of expand_matrix().
  2586. // This generates all of the combinations from the
  2587. // indicated index into the words array, with the given
  2588. // prefix.
  2589. ////////////////////////////////////////////////////////////////////
  2590. void PPScope::
  2591. r_expand_matrix(vector<string> &results, const vector<vector<string> > &words,
  2592. int index, const string &prefix) {
  2593. if (index >= (int)words.size()) {
  2594. // This is the terminal condition.
  2595. results.push_back(prefix);
  2596. } else {
  2597. // Otherwise, tack on the next set of words, and recurse.
  2598. const vector<string> &w = words[index];
  2599. vector<string>::const_iterator wi;
  2600. for (wi = w.begin(); wi != w.end(); ++wi) {
  2601. r_expand_matrix(results, words, index + 1, prefix + (*wi));
  2602. }
  2603. }
  2604. }
  2605. ////////////////////////////////////////////////////////////////////
  2606. // Function: PPScope::p_find_map_variable
  2607. // Access: Private
  2608. // Description: The implementation of find_map_variable() for a
  2609. // particular static scope, without checking the stack.
  2610. ////////////////////////////////////////////////////////////////////
  2611. PPScope::MapVariableDefinition &PPScope::
  2612. p_find_map_variable(const string &varname) {
  2613. MapVariables::const_iterator mvi;
  2614. mvi = _map_variables.find(varname);
  2615. if (mvi != _map_variables.end()) {
  2616. return (MapVariableDefinition &)(*mvi).second;
  2617. }
  2618. if (_parent_scope != (PPScope *)NULL) {
  2619. return _parent_scope->p_find_map_variable(varname);
  2620. }
  2621. return _null_map_def;
  2622. }
  2623. ////////////////////////////////////////////////////////////////////
  2624. // Function: PPScope::glob_string
  2625. // Access: Private
  2626. // Description: Expands the words in the string as if they were a set
  2627. // of filenames using the shell globbing characters.
  2628. // Fills up the results vector (which the user should
  2629. // ensure is empty before calling) with the set of all
  2630. // files that actually match the globbing characters.
  2631. ////////////////////////////////////////////////////////////////////
  2632. void PPScope::
  2633. glob_string(const string &str, vector<string> &results) {
  2634. // We run glob_string() within the directory indicated by
  2635. // $[THISDIRPREFIX]. This way, local filenames will be expanded the
  2636. // way we expect.
  2637. string dirname = trim_blanks(expand_variable("THISDIRPREFIX"));
  2638. bool changed_dir = false;
  2639. if (!dirname.empty()) {
  2640. if (chdir(dirname.c_str()) < 0) {
  2641. perror("chdir");
  2642. } else {
  2643. changed_dir = true;
  2644. }
  2645. }
  2646. vector<string> words;
  2647. tokenize_whitespace(str, words);
  2648. vector<string>::const_iterator wi;
  2649. glob_t pglob;
  2650. memset(&pglob, 0, sizeof(pglob));
  2651. int flags = 0;
  2652. for (wi = words.begin(); wi != words.end(); ++wi) {
  2653. glob((*wi).c_str(), flags, NULL, &pglob);
  2654. flags |= GLOB_APPEND;
  2655. }
  2656. for (int i = 0; i < (int)pglob.gl_pathc; i++) {
  2657. results.push_back(string(pglob.gl_pathv[i]));
  2658. }
  2659. globfree(&pglob);
  2660. // Sort the results into alphabetical order.
  2661. sort(results.begin(), results.end());
  2662. if (changed_dir) {
  2663. // Now restore the current directory back to where it should be.
  2664. PPMain::chdir_root();
  2665. }
  2666. }