ppCommandFile.cxx 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. // Filename: ppCommandFile.cxx
  2. // Created by: drose (25Sep00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. #include "ppCommandFile.h"
  6. #include "ppScope.h"
  7. #include "ppNamedScopes.h"
  8. #include "ppSubroutine.h"
  9. #include "tokenize.h"
  10. #include <ctype.h>
  11. #include <stdio.h> // for tempnam()
  12. #include <unistd.h>
  13. #include <sys/types.h>
  14. #include <utime.h>
  15. #include <assert.h>
  16. #include <strstream.h>
  17. static const string begin_comment(BEGIN_COMMENT);
  18. ////////////////////////////////////////////////////////////////////
  19. // Function: PPCommandFile::IfNesting::Constructor
  20. // Access: Public
  21. // Description:
  22. ////////////////////////////////////////////////////////////////////
  23. PPCommandFile::IfNesting::
  24. IfNesting(IfState state) :
  25. _state(state)
  26. {
  27. _block = (PPCommandFile::BlockNesting *)NULL;
  28. _next = (PPCommandFile::IfNesting *)NULL;
  29. }
  30. ////////////////////////////////////////////////////////////////////
  31. // Function: PPCommandFile::IfNesting::push
  32. // Access: Public
  33. // Description: Adds this IfNesting object to the top of the
  34. // nesting stack.
  35. ////////////////////////////////////////////////////////////////////
  36. void PPCommandFile::IfNesting::
  37. push(PPCommandFile *file) {
  38. _block = file->_block_nesting;
  39. _next = file->_if_nesting;
  40. file->_if_nesting = this;
  41. }
  42. ////////////////////////////////////////////////////////////////////
  43. // Function: PPCommandFile::IfNesting::pop
  44. // Access: Public
  45. // Description: Removes this IfNesting object from the top of the
  46. // nesting stack, and restores the command file's
  47. // nesting state.
  48. ////////////////////////////////////////////////////////////////////
  49. void PPCommandFile::IfNesting::
  50. pop(PPCommandFile *file) {
  51. assert(file->_if_nesting == this);
  52. file->_if_nesting = _next;
  53. }
  54. ////////////////////////////////////////////////////////////////////
  55. // Function: PPCommandFile::WriteState::Constructor
  56. // Access: Public
  57. // Description:
  58. ////////////////////////////////////////////////////////////////////
  59. PPCommandFile::WriteState::
  60. WriteState() {
  61. _out = &cout;
  62. _format = WF_collapse;
  63. _last_blank = true;
  64. }
  65. ////////////////////////////////////////////////////////////////////
  66. // Function: PPCommandFile::WriteState::Copy Constructor
  67. // Access: Public
  68. // Description:
  69. ////////////////////////////////////////////////////////////////////
  70. PPCommandFile::WriteState::
  71. WriteState(const WriteState &copy) :
  72. _out(copy._out),
  73. _format(copy._format),
  74. _last_blank(copy._last_blank)
  75. {
  76. }
  77. ////////////////////////////////////////////////////////////////////
  78. // Function: PPCommandFile::WriteState::write_line
  79. // Access: Public
  80. // Description:
  81. ////////////////////////////////////////////////////////////////////
  82. bool PPCommandFile::WriteState::
  83. write_line(const string &line) {
  84. switch (_format) {
  85. case WF_straight:
  86. (*_out) << line << "\n";
  87. return true;
  88. case WF_collapse:
  89. return write_collapse_line(line);
  90. case WF_makefile:
  91. return write_makefile_line(line);
  92. }
  93. cerr << "Unsupported write format: " << (int)_format << "\n";
  94. return false;
  95. }
  96. ////////////////////////////////////////////////////////////////////
  97. // Function: PPCommandFile::WriteState::write_collapse_line
  98. // Access: Public
  99. // Description:
  100. ////////////////////////////////////////////////////////////////////
  101. bool PPCommandFile::WriteState::
  102. write_collapse_line(const string &line) {
  103. if (line.empty()) {
  104. if (!_last_blank) {
  105. (*_out) << "\n";
  106. _last_blank = true;
  107. }
  108. } else {
  109. _last_blank = false;
  110. (*_out) << line << "\n";
  111. }
  112. return true;
  113. }
  114. ////////////////////////////////////////////////////////////////////
  115. // Function: PPCommandFile::WriteState::write_makefile_line
  116. // Access: Public
  117. // Description:
  118. ////////////////////////////////////////////////////////////////////
  119. bool PPCommandFile::WriteState::
  120. write_makefile_line(const string &line) {
  121. if (line.length() <= 72) {
  122. return write_collapse_line(line);
  123. }
  124. _last_blank = false;
  125. // In makefile mode, long variable assignment lines are folded after
  126. // the assignment.
  127. vector<string> words;
  128. tokenize_whitespace(line, words);
  129. if (words.size() > 2 && (words[1] == "=" || words[1] == ":")) {
  130. // This appears to be a variable assignment or a dependency rule;
  131. // fold it.
  132. (*_out) << words[0] << " " << words[1];
  133. vector<string>::const_iterator wi;
  134. int col = 80;
  135. wi = words.begin() + 2;
  136. while (wi != words.end()) {
  137. col += (*wi).length() + 1;
  138. if (col > 72) {
  139. (*_out) << " \\\n ";
  140. col = 4 + (*wi).length();
  141. }
  142. (*_out) << " " << (*wi);
  143. ++wi;
  144. }
  145. (*_out) << "\n";
  146. } else {
  147. // This is not a variable assignment, so just write it out.
  148. (*_out) << line << "\n";
  149. }
  150. return true;
  151. }
  152. ////////////////////////////////////////////////////////////////////
  153. // Function: PPCommandFile::BlockNesting::Constructor
  154. // Access: Public
  155. // Description:
  156. ////////////////////////////////////////////////////////////////////
  157. PPCommandFile::BlockNesting::
  158. BlockNesting(BlockState state, const string &name) :
  159. _state(state),
  160. _name(name)
  161. {
  162. _if = (PPCommandFile::IfNesting *)NULL;
  163. _write_state = (PPCommandFile::WriteState *)NULL;
  164. _scope = (PPScope *)NULL;
  165. _flags = 0;
  166. _next = (PPCommandFile::BlockNesting *)NULL;
  167. }
  168. ////////////////////////////////////////////////////////////////////
  169. // Function: PPCommandFile::BlockNesting::push
  170. // Access: Public
  171. // Description: Adds this BlockNesting object to the top of the
  172. // nesting stack.
  173. ////////////////////////////////////////////////////////////////////
  174. void PPCommandFile::BlockNesting::
  175. push(PPCommandFile *file) {
  176. _if = file->_if_nesting;
  177. _write_state = file->_write_state;
  178. _scope = file->_scope;
  179. _next = file->_block_nesting;
  180. file->_block_nesting = this;
  181. }
  182. ////////////////////////////////////////////////////////////////////
  183. // Function: PPCommandFile::BlockNesting::pop
  184. // Access: Public
  185. // Description: Removes this BlockNesting object from the top of the
  186. // nesting stack, and restores the command file's
  187. // nesting state.
  188. ////////////////////////////////////////////////////////////////////
  189. void PPCommandFile::BlockNesting::
  190. pop(PPCommandFile *file) {
  191. assert(file->_block_nesting == this);
  192. if (file->_write_state != _write_state) {
  193. delete file->_write_state;
  194. file->_write_state = _write_state;
  195. }
  196. file->_scope = _scope;
  197. file->_block_nesting = _next;
  198. }
  199. ////////////////////////////////////////////////////////////////////
  200. // Function: PPCommandFile::Constructor
  201. // Access: Public
  202. // Description:
  203. ////////////////////////////////////////////////////////////////////
  204. PPCommandFile::
  205. PPCommandFile(PPScope *scope) {
  206. _native_scope = scope;
  207. _scope = scope;
  208. _got_command = false;
  209. _in_for = false;
  210. _if_nesting = (IfNesting *)NULL;
  211. _block_nesting = (BlockNesting *)NULL;
  212. _write_state = new WriteState;
  213. }
  214. ////////////////////////////////////////////////////////////////////
  215. // Function: PPCommandFile::Destructor
  216. // Access: Public, Virtual
  217. // Description:
  218. ////////////////////////////////////////////////////////////////////
  219. PPCommandFile::
  220. ~PPCommandFile() {
  221. delete _write_state;
  222. }
  223. ////////////////////////////////////////////////////////////////////
  224. // Function: PPCommandFile::set_output
  225. // Access: Public
  226. // Description: Changes the main output stream that will be written
  227. // to when text appears outside of a #output .. #end
  228. // block. This is cout by default.
  229. ////////////////////////////////////////////////////////////////////
  230. void PPCommandFile::
  231. set_output(ostream *out) {
  232. _write_state->_out = out;
  233. }
  234. ////////////////////////////////////////////////////////////////////
  235. // Function: PPCommandFile::set_scope
  236. // Access: Public
  237. // Description: Changes the command file to use the indicated scope.
  238. // This scope will *not* be deleted when the command
  239. // file destructs.
  240. ////////////////////////////////////////////////////////////////////
  241. void PPCommandFile::
  242. set_scope(PPScope *scope) {
  243. _scope = scope;
  244. _native_scope = scope;
  245. }
  246. ////////////////////////////////////////////////////////////////////
  247. // Function: PPCommandFile::get_scope
  248. // Access: Public
  249. // Description: Returns the current scope associated with the command
  250. // file. This may change as the command file is
  251. // processed (e.g. between #begin .. #end sequences),
  252. // and it may or may not be tied to the life of the
  253. // PPCommandFile itself.
  254. ////////////////////////////////////////////////////////////////////
  255. PPScope *PPCommandFile::
  256. get_scope() const {
  257. return _scope;
  258. }
  259. ////////////////////////////////////////////////////////////////////
  260. // Function: PPCommandFile::read_file
  261. // Access: Public
  262. // Description: Reads input from the given filename.
  263. ////////////////////////////////////////////////////////////////////
  264. bool PPCommandFile::
  265. read_file(const string &filename) {
  266. ifstream in(filename.c_str());
  267. if (!in) {
  268. cerr << "Unable to open " << filename << ".\n";
  269. return false;
  270. }
  271. return read_stream(in, filename);
  272. }
  273. ////////////////////////////////////////////////////////////////////
  274. // Function: PPCommandFile::read_stream
  275. // Access: Public
  276. // Description: Reads input from the given stream. Each line is
  277. // read, commands are processed, variables are expanded,
  278. // and the resulting output is sent to write_line()
  279. // one line at a time. The return value is true if the
  280. // entire file is read with no errors, false if there is
  281. // some problem.
  282. //
  283. // The filename is just informational; it is used to
  284. // update the variables like THISFILENAME and
  285. // THISDIRPREFIX as appropriate, and to report errors to
  286. // the user.
  287. ////////////////////////////////////////////////////////////////////
  288. bool PPCommandFile::
  289. read_stream(istream &in, const string &filename) {
  290. PushFilename pushed(_scope, filename);
  291. if (!read_stream(in)) {
  292. if (!in.eof()) {
  293. cerr << "Error reading " << filename << ".\n";
  294. }
  295. return false;
  296. }
  297. return true;
  298. }
  299. ////////////////////////////////////////////////////////////////////
  300. // Function: PPCommandFile::read_stream
  301. // Access: Public
  302. // Description: Reads input from the given stream. Each line is
  303. // read, commands are processed, variables are expanded,
  304. // and the resulting output is sent to write_line()
  305. // one line at a time. The return value is true if the
  306. // entire file is read with no errors, false if there is
  307. // some problem.
  308. //
  309. // This flavor of read_stream() does not take a
  310. // filename. It does not, therefore, adjust
  311. // THISFILENAME and THISDIRPREFIX.
  312. ////////////////////////////////////////////////////////////////////
  313. bool PPCommandFile::
  314. read_stream(istream &in) {
  315. string line;
  316. getline(in, line);
  317. begin_read();
  318. while (!in.fail() && !in.eof()) {
  319. if (!read_line(line)) {
  320. return false;
  321. }
  322. getline(in, line);
  323. }
  324. if (!end_read()) {
  325. return false;
  326. }
  327. return true;
  328. }
  329. ////////////////////////////////////////////////////////////////////
  330. // Function: PPCommandFile::begin_read
  331. // Access: Public
  332. // Description: Resets to the beginning-of-the-stream state, in
  333. // preparation for a sequence of read_line() calls.
  334. ////////////////////////////////////////////////////////////////////
  335. void PPCommandFile::
  336. begin_read() {
  337. assert(_if_nesting == (IfNesting *)NULL);
  338. assert(_block_nesting == (BlockNesting *)NULL);
  339. }
  340. ////////////////////////////////////////////////////////////////////
  341. // Function: PPCommandFile::read_line
  342. // Access: Public
  343. // Description: Reads one line at a time, as if from the input
  344. // stream.
  345. ////////////////////////////////////////////////////////////////////
  346. bool PPCommandFile::
  347. read_line(string line) {
  348. // First things first: strip off any comment in the line.
  349. // We only recognize comments that are proceeded by whitespace, or
  350. // that start at the beginning of the line.
  351. size_t comment = line.find(begin_comment);
  352. while (comment != string::npos &&
  353. !(comment == 0 || isspace(line[comment - 1]))) {
  354. comment = line.find(begin_comment, comment + begin_comment.length());
  355. }
  356. if (comment != string::npos) {
  357. // Also strip any whitespace leading up to the comment.
  358. while (comment > 0 && isspace(line[comment - 1])) {
  359. comment--;
  360. }
  361. line = line.substr(0, comment);
  362. }
  363. // If the comment was at the beginning of the line, ignore the whole
  364. // line, including its whitespace.
  365. if (comment != 0) {
  366. // We also strip off whitespace at the end of the line, since this
  367. // is generally invisible and almost always just leads to trouble.
  368. size_t eol = line.length();
  369. while (eol > 0 && (isspace(line[eol - 1]) || line[eol - 1] == '\r')) {
  370. eol--;
  371. }
  372. line = line.substr(0, eol);
  373. if (_in_for) {
  374. // Save up the lines for later execution if we're within a #forscopes.
  375. _saved_lines.push_back(line);
  376. }
  377. if (_got_command) {
  378. return handle_command(line);
  379. } else {
  380. // Find the beginning of the line--skip initial whitespace.
  381. size_t p = 0;
  382. while (p < line.length() && isspace(line[p])) {
  383. p++;
  384. }
  385. if (p == line.length()) {
  386. // The line is empty. Make it truly empty.
  387. line = "";
  388. } else {
  389. if (p + 1 < line.length() && line[p] == COMMAND_PREFIX &&
  390. isalpha(line[p + 1])) {
  391. // This is a special command.
  392. return handle_command(line.substr(p + 1));
  393. }
  394. }
  395. if (!_in_for && !failed_if()) {
  396. return _write_state->write_line(_scope->expand_string(line));
  397. }
  398. }
  399. }
  400. return true;
  401. }
  402. ////////////////////////////////////////////////////////////////////
  403. // Function: PPCommandFile::end_read
  404. // Access: Public
  405. // Description: Finishes up the input stream, after a sequence of
  406. // read_line() calls.
  407. ////////////////////////////////////////////////////////////////////
  408. bool PPCommandFile::
  409. end_read() {
  410. bool okflag = true;
  411. if (_if_nesting != (IfNesting *)NULL) {
  412. cerr << "Unclosed if\n";
  413. _if_nesting = (IfNesting *)NULL;
  414. okflag = false;
  415. }
  416. if (_block_nesting != (BlockNesting *)NULL) {
  417. switch (_block_nesting->_state) {
  418. case BS_begin:
  419. cerr << "Unclosed begin " << _block_nesting->_name << "\n";
  420. break;
  421. case BS_forscopes:
  422. case BS_nested_forscopes:
  423. cerr << "Unclosed forscopes " << _block_nesting->_name << "\n";
  424. break;
  425. case BS_foreach:
  426. case BS_nested_foreach:
  427. cerr << "Unclosed foreach " << _block_nesting->_name << "\n";
  428. break;
  429. case BS_formap:
  430. case BS_nested_formap:
  431. cerr << "Unclosed formap " << _block_nesting->_name << "\n";
  432. break;
  433. case BS_defsub:
  434. cerr << "Unclosed defsub " << _block_nesting->_name << "\n";
  435. break;
  436. case BS_defun:
  437. cerr << "Unclosed defun " << _block_nesting->_name << "\n";
  438. break;
  439. case BS_output:
  440. cerr << "Unclosed output " << _block_nesting->_name << "\n";
  441. break;
  442. }
  443. _block_nesting = (BlockNesting *)NULL;
  444. okflag = false;
  445. }
  446. return okflag;
  447. }
  448. ////////////////////////////////////////////////////////////////////
  449. // Function: PPCommandFile::handle_command
  450. // Access: Protected
  451. // Description: Handles a macro command.
  452. ////////////////////////////////////////////////////////////////////
  453. bool PPCommandFile::
  454. handle_command(const string &line) {
  455. if (_got_command) {
  456. // If we were still processing a command from last time, keep
  457. // going; this line is just a continuation. But skip any initial
  458. // whitespace.
  459. size_t p = 0;
  460. while (p < line.length() && isspace(line[p])) {
  461. p++;
  462. }
  463. _params += ' ';
  464. _params += line.substr(p);
  465. } else {
  466. // This is the first line of a new command.
  467. // Pull off the first word and the rest of the line.
  468. size_t p = 0;
  469. while (p < line.length() && !isspace(line[p])) {
  470. p++;
  471. }
  472. _command = line.substr(0, p);
  473. // Skip whitespace between the command and its arguments.
  474. while (p < line.length() && isspace(line[p])) {
  475. p++;
  476. }
  477. _params = line.substr(p);
  478. }
  479. if (!_params.empty() && _params[_params.length() - 1] == '\\') {
  480. // If the line ends with a backslash, there's more to come before
  481. // we can process the command.
  482. _got_command = true;
  483. // Truncate off the backslash, and any whitespace before it.
  484. size_t p = _params.length() - 1;
  485. while (p > 0 && isspace(_params[p - 1])) {
  486. p--;
  487. }
  488. _params = _params.substr(0, p);
  489. return true;
  490. }
  491. // We're completely done scanning the command now.
  492. _got_command = false;
  493. if (_command == "if") {
  494. return handle_if_command();
  495. } else if (_command == "elif") {
  496. return handle_elif_command();
  497. } else if (_command == "else") {
  498. return handle_else_command();
  499. } else if (_command == "endif") {
  500. return handle_endif_command();
  501. } else if (failed_if()) {
  502. // If we're in the middle of a failed #if, we ignore all commands
  503. // except for the if-related commands, above.
  504. return true;
  505. } else if (_command == "begin") {
  506. return handle_begin_command();
  507. } else if (_command == "forscopes") {
  508. return handle_forscopes_command();
  509. } else if (_command == "foreach") {
  510. return handle_foreach_command();
  511. } else if (_command == "formap") {
  512. return handle_formap_command();
  513. } else if (_command == "defsub") {
  514. return handle_defsub_command(true);
  515. } else if (_command == "defun") {
  516. return handle_defsub_command(false);
  517. } else if (_command == "output") {
  518. return handle_output_command();
  519. } else if (_command == "end") {
  520. return handle_end_command();
  521. } else if (_in_for) {
  522. // If we're currently saving up lines within a block sequence, we
  523. // ignore all commands except for the block-related commands,
  524. // above.
  525. return true;
  526. } else if (_command == "format") {
  527. return handle_format_command();
  528. } else if (_command == "print") {
  529. return handle_print_command();
  530. } else if (_command == "include") {
  531. return handle_include_command();
  532. } else if (_command == "sinclude") {
  533. return handle_sinclude_command();
  534. } else if (_command == "call") {
  535. return handle_call_command();
  536. } else if (_command == "error") {
  537. return handle_error_command();
  538. } else if (_command == "defer") {
  539. return handle_defer_command();
  540. } else if (_command == "define") {
  541. return handle_define_command();
  542. } else if (_command == "set") {
  543. return handle_set_command();
  544. } else if (_command == "map") {
  545. return handle_map_command();
  546. } else if (_command == "addmap") {
  547. return handle_addmap_command();
  548. }
  549. cerr << "Invalid command: " << COMMAND_PREFIX << _command << "\n";
  550. return false;
  551. }
  552. ////////////////////////////////////////////////////////////////////
  553. // Function: PPCommandFile::handle_if_command
  554. // Access: Protected
  555. // Description: Handles the #if command: conditionally evaluate the
  556. // following code.
  557. ////////////////////////////////////////////////////////////////////
  558. bool PPCommandFile::
  559. handle_if_command() {
  560. if (failed_if()) {
  561. // If we're *already* inside a failed if, we don't have to
  562. // evaluate this one, but we do need to record the nesting level.
  563. IfNesting *nest = new IfNesting(IS_done);
  564. nest->push(this);
  565. } else {
  566. // If the parameter string evaluates to empty, the case is false.
  567. // Otherwise the case is true. However, if we're currently
  568. // scanning #forscopes or something, we don't evaluate this at
  569. // all, because it doesn't matter.
  570. bool is_empty = true;
  571. if (!_in_for) {
  572. _params = _scope->expand_string(_params);
  573. string::const_iterator si;
  574. for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
  575. is_empty = isspace(*si);
  576. }
  577. }
  578. IfState state = is_empty ? IS_off : IS_on;
  579. IfNesting *nest = new IfNesting(state);
  580. nest->push(this);
  581. }
  582. return true;
  583. }
  584. ////////////////////////////////////////////////////////////////////
  585. // Function: PPCommandFile::handle_elif_command
  586. // Access: Protected
  587. // Description: Handles the #elif command: conditionally evaluate
  588. // the following code, following a failed #if command.
  589. ////////////////////////////////////////////////////////////////////
  590. bool PPCommandFile::
  591. handle_elif_command() {
  592. if (_if_nesting == (IfNesting *)NULL) {
  593. cerr << "elif encountered without if.\n";
  594. return false;
  595. }
  596. if (_if_nesting->_state == IS_else) {
  597. cerr << "elif encountered after else.\n";
  598. return false;
  599. }
  600. if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
  601. // If we passed the #if above, we don't need to evaluate the #elif.
  602. _if_nesting->_state = IS_done;
  603. return true;
  604. }
  605. // If the parameter string evaluates to empty, the case is false.
  606. // Otherwise the case is true.
  607. bool is_empty = true;
  608. if (!_in_for) {
  609. _params = _scope->expand_string(_params);
  610. string::const_iterator si;
  611. for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
  612. is_empty = isspace(*si);
  613. }
  614. }
  615. _if_nesting->_state = is_empty ? IS_off : IS_on;
  616. return true;
  617. }
  618. ////////////////////////////////////////////////////////////////////
  619. // Function: PPCommandFile::handle_else_command
  620. // Access: Protected
  621. // Description: Handles the #else command: evaluate the following
  622. // code following a failed #if command.
  623. ////////////////////////////////////////////////////////////////////
  624. bool PPCommandFile::
  625. handle_else_command() {
  626. if (_if_nesting == (IfNesting *)NULL) {
  627. cerr << "else encountered without if.\n";
  628. return false;
  629. }
  630. if (_if_nesting->_state == IS_else) {
  631. cerr << "else encountered after else.\n";
  632. return false;
  633. }
  634. if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
  635. _if_nesting->_state = IS_done;
  636. return true;
  637. }
  638. _if_nesting->_state = IS_else;
  639. return true;
  640. }
  641. ////////////////////////////////////////////////////////////////////
  642. // Function: PPCommandFile::handle_endif_command
  643. // Access: Protected
  644. // Description: Handles the #endif command: close a preceeding #if
  645. // command.
  646. ////////////////////////////////////////////////////////////////////
  647. bool PPCommandFile::
  648. handle_endif_command() {
  649. if (_if_nesting == (IfNesting *)NULL) {
  650. cerr << "endif encountered without if.\n";
  651. return false;
  652. }
  653. IfNesting *nest = _if_nesting;
  654. nest->pop(this);
  655. if (nest->_block != _block_nesting) {
  656. cerr << "If block not closed within scoping block.\n";
  657. return false;
  658. }
  659. delete nest;
  660. return true;
  661. }
  662. ////////////////////////////////////////////////////////////////////
  663. // Function: PPCommandFile::handle_begin_command
  664. // Access: Protected
  665. // Description: Handles the #begin command: begin a named scope
  666. // block. The variables defined between this command
  667. // and the corresponding #end command will be local to
  668. // this named scope.
  669. ////////////////////////////////////////////////////////////////////
  670. bool PPCommandFile::
  671. handle_begin_command() {
  672. string name = trim_blanks(_scope->expand_string(_params));
  673. BlockNesting *nest = new BlockNesting(BS_begin, name);
  674. if (contains_whitespace(name)) {
  675. cerr << "Attempt to define scope named \"" << name
  676. << "\".\nScope names may not contain whitespace.\n";
  677. return false;
  678. }
  679. if (name.find(SCOPE_DIRNAME_SEPARATOR) != string::npos) {
  680. cerr << "Attempt to define scope named \"" << name
  681. << "\".\nScope names may not contain the '"
  682. << SCOPE_DIRNAME_SEPARATOR << "' character.\n";
  683. return false;
  684. }
  685. nest->push(this);
  686. PPScope *named_scope = _scope->get_named_scopes()->make_scope(name);
  687. named_scope->set_parent(_scope);
  688. _scope = named_scope;
  689. return true;
  690. }
  691. ////////////////////////////////////////////////////////////////////
  692. // Function: PPCommandFile::handle_forscopes_command
  693. // Access: Protected
  694. // Description: Handles the #forscopes command: interpret all the lines
  695. // between this command and the corresponding #end
  696. // command once for each occurrence of a named scope
  697. // with the given name.
  698. ////////////////////////////////////////////////////////////////////
  699. bool PPCommandFile::
  700. handle_forscopes_command() {
  701. BlockState state = _in_for ? BS_nested_forscopes : BS_forscopes;
  702. string name = trim_blanks(_scope->expand_string(_params));
  703. BlockNesting *nest = new BlockNesting(state, name);
  704. nest->push(this);
  705. if (!_in_for) {
  706. _in_for = true;
  707. _saved_lines.clear();
  708. }
  709. return true;
  710. }
  711. ////////////////////////////////////////////////////////////////////
  712. // Function: PPCommandFile::handle_foreach_command
  713. // Access: Protected
  714. // Description: Handles the #foreach command: interpret all the lines
  715. // between this command and the corresponding #end
  716. // command once for each word in the argument.
  717. ////////////////////////////////////////////////////////////////////
  718. bool PPCommandFile::
  719. handle_foreach_command() {
  720. // Get the parameters of the foreach command. The first word is the
  721. // name of the variable to substitute in (and which should appear on
  722. // the matching #end command), and the remaining words are the
  723. // values to substitute in.
  724. vector<string> words;
  725. tokenize_whitespace(_scope->expand_string(_params), words);
  726. if (words.empty()) {
  727. cerr << "#foreach requires at least one parameter.\n";
  728. return false;
  729. }
  730. string variable_name = words.front();
  731. BlockState state = _in_for ? BS_nested_foreach : BS_foreach;
  732. BlockNesting *nest = new BlockNesting(state, variable_name);
  733. nest->push(this);
  734. // We insert in all but the first word in the words vector.
  735. nest->_words.insert(nest->_words.end(), words.begin() + 1, words.end());
  736. if (!_in_for) {
  737. _in_for = true;
  738. _saved_lines.clear();
  739. }
  740. return true;
  741. }
  742. ////////////////////////////////////////////////////////////////////
  743. // Function: PPCommandFile::handle_formap_command
  744. // Access: Protected
  745. // Description: Handles the #formap command: interpret all the lines
  746. // between this command and the corresponding #end
  747. // command once for each key in the map, and also within
  748. // the corresponding scope of that particular key.
  749. ////////////////////////////////////////////////////////////////////
  750. bool PPCommandFile::
  751. handle_formap_command() {
  752. // Get the parameters of the formap command. The first word is the
  753. // name of the variable to substitute in (and which should appear on
  754. // the matching #end command), and the second word is the name of
  755. // the map variable.
  756. vector<string> words;
  757. tokenize_whitespace(_scope->expand_string(_params), words);
  758. if (words.size() != 2) {
  759. cerr << "#formap requires exactly two parameters.\n";
  760. return false;
  761. }
  762. string variable_name = words.front();
  763. BlockState state = _in_for ? BS_nested_formap : BS_formap;
  764. BlockNesting *nest = new BlockNesting(state, words[0]);
  765. nest->push(this);
  766. nest->_words.push_back(words[1]);
  767. if (!_in_for) {
  768. _in_for = true;
  769. _saved_lines.clear();
  770. }
  771. return true;
  772. }
  773. ////////////////////////////////////////////////////////////////////
  774. // Function: PPCommandFile::handle_defsub_command
  775. // Access: Protected
  776. // Description: Handles the #defsub (or #defun) command: save all the
  777. // lines between this command and the matching #end as a
  778. // callable subroutine to be invoked by a later #call
  779. // command. If is_defsub is false, it means this
  780. // subroutine was actually defined via a #defun command,
  781. // so it is to be invoked by a later variable reference,
  782. // instead of by a #call command.
  783. ////////////////////////////////////////////////////////////////////
  784. bool PPCommandFile::
  785. handle_defsub_command(bool is_defsub) {
  786. string command = (is_defsub) ? "#defsub" : "#defun";
  787. // The first word of the parameter list is the subroutine name; the
  788. // rest is the comma-separated list of formal parameter names.
  789. // Pull off the first word and the rest of the params.
  790. size_t p = 0;
  791. while (p < _params.length() && !isspace(_params[p])) {
  792. p++;
  793. }
  794. string subroutine_name = trim_blanks(_params.substr(0, p));
  795. if (subroutine_name.empty()) {
  796. cerr << command << " requires at least one parameter.\n";
  797. return false;
  798. }
  799. vector<string> formals;
  800. _scope->tokenize_params(_params.substr(p), formals, false);
  801. vector<string>::const_iterator fi;
  802. for (fi = formals.begin(); fi != formals.end(); ++fi) {
  803. if (!is_valid_formal(*fi)) {
  804. cerr << command << " " << subroutine_name
  805. << ": invalid formal parameter name '" << (*fi) << "'\n";
  806. return false;
  807. }
  808. }
  809. if (_in_for) {
  810. cerr << command << " may not appear within another block scoping command like\n"
  811. << "#forscopes, #foreach, #formap, #defsub, or #defun.\n";
  812. return false;
  813. }
  814. BlockState state = is_defsub ? BS_defsub : BS_defun;
  815. BlockNesting *nest = new BlockNesting(state, subroutine_name);
  816. nest->push(this);
  817. nest->_words.swap(formals);
  818. _in_for = true;
  819. _saved_lines.clear();
  820. return true;
  821. }
  822. ////////////////////////////////////////////////////////////////////
  823. // Function: PPCommandFile::handle_output_command
  824. // Access: Protected
  825. // Description: Handles the #output command: all text between this
  826. // command and the corresponding #end command will be
  827. // sent to the indicated output file.
  828. ////////////////////////////////////////////////////////////////////
  829. bool PPCommandFile::
  830. handle_output_command() {
  831. vector<string> words;
  832. tokenize_whitespace(_scope->expand_string(_params), words);
  833. if (words.empty()) {
  834. cerr << "#output command requires one parameter.\n";
  835. return false;
  836. }
  837. BlockNesting *nest = new BlockNesting(BS_output, words[0]);
  838. // Also check the output flags.
  839. for (int i = 1; i < (int)words.size(); i++) {
  840. if (words[i] == "notouch") {
  841. nest->_flags |= OF_notouch;
  842. } else {
  843. cerr << "Invalid output flag: " << words[i] << "\n";
  844. }
  845. }
  846. nest->push(this);
  847. if (!_in_for) {
  848. string filename = nest->_name;
  849. if (filename.empty()) {
  850. cerr << "Attempt to output to empty filename\n";
  851. return false;
  852. }
  853. string prefix = _scope->expand_variable("DIRPREFIX");
  854. if (filename[0] != '/') {
  855. filename = prefix + filename;
  856. }
  857. nest->_filename = filename;
  858. // Generate an in-memory copy of the file first.
  859. _write_state = new WriteState(*_write_state);
  860. _write_state->_out = &nest->_output;
  861. }
  862. return true;
  863. }
  864. ////////////////////////////////////////////////////////////////////
  865. // Function: PPCommandFile::handle_end_command
  866. // Access: Protected
  867. // Description: Handles the #end command. This closes a number of
  868. // different kinds of blocks, like #begin and #forscopes.
  869. ////////////////////////////////////////////////////////////////////
  870. bool PPCommandFile::
  871. handle_end_command() {
  872. if (_block_nesting == (BlockNesting *)NULL) {
  873. cerr << "Unmatched end " << _params << ".\n";
  874. return false;
  875. }
  876. string name = trim_blanks(_scope->expand_string(_params));
  877. if (name != _block_nesting->_name) {
  878. cerr << "end " << name << " encountered where end "
  879. << _block_nesting->_name << " expected.\n";
  880. return false;
  881. }
  882. BlockNesting *nest = _block_nesting;
  883. nest->pop(this);
  884. if (nest->_if != _if_nesting) {
  885. cerr << "If block not closed within scoping block.\n";
  886. return false;
  887. }
  888. if (nest->_state == BS_forscopes) {
  889. // Now replay all of the saved lines.
  890. _in_for = false;
  891. if (!replay_forscopes(nest->_name)) {
  892. return false;
  893. }
  894. } else if (nest->_state == BS_foreach) {
  895. // Now replay all of the saved lines.
  896. _in_for = false;
  897. if (!replay_foreach(nest->_name, nest->_words)) {
  898. return false;
  899. }
  900. } else if (nest->_state == BS_formap) {
  901. // Now replay all of the saved lines.
  902. _in_for = false;
  903. assert(nest->_words.size() == 1);
  904. if (!replay_formap(nest->_name, nest->_words[0])) {
  905. return false;
  906. }
  907. } else if (nest->_state == BS_defsub || nest->_state == BS_defun) {
  908. // Save all of the saved lines as a named subroutine.
  909. _in_for = false;
  910. PPSubroutine *sub = new PPSubroutine;
  911. sub->_formals.swap(nest->_words);
  912. sub->_lines.swap(_saved_lines);
  913. // Remove the #end command. This will fail if someone makes an
  914. // #end command that spans multiple lines. Don't do that.
  915. assert(!sub->_lines.empty());
  916. sub->_lines.pop_back();
  917. if (nest->_state == BS_defsub) {
  918. PPSubroutine::define_sub(nest->_name, sub);
  919. } else {
  920. PPSubroutine::define_func(nest->_name, sub);
  921. }
  922. } else if (nest->_state == BS_output) {
  923. if (!_in_for) {
  924. if (!nest->_output) {
  925. cerr << "Error while writing " << nest->_filename << "\n";
  926. return false;
  927. }
  928. // Now compare the file we generated to the file that's already
  929. // there, if there is one.
  930. nest->_output << ends;
  931. const char *generated_file = nest->_output.str();
  932. if (!compare_output(generated_file, nest->_filename,
  933. (nest->_flags & OF_notouch) != 0)) {
  934. return false;
  935. }
  936. }
  937. }
  938. delete nest;
  939. return true;
  940. }
  941. ////////////////////////////////////////////////////////////////////
  942. // Function: PPCommandFile::handle_format_command
  943. // Access: Protected
  944. // Description: Handles the #format command: change the formatting
  945. // mode of lines as they are output.
  946. ////////////////////////////////////////////////////////////////////
  947. bool PPCommandFile::
  948. handle_format_command() {
  949. _params = trim_blanks(_scope->expand_string(_params));
  950. if (_params == "straight") {
  951. _write_state->_format = WF_straight;
  952. } else if (_params == "collapse") {
  953. _write_state->_format = WF_collapse;
  954. } else if (_params == "makefile") {
  955. _write_state->_format = WF_makefile;
  956. } else {
  957. cerr << "Ignoring invalid write format: " << _params << "\n";
  958. }
  959. return true;
  960. }
  961. ////////////////////////////////////////////////////////////////////
  962. // Function: PPCommandFile::handle_print_command
  963. // Access: Protected
  964. // Description: Handles the #print command: immediately output the
  965. // arguments to this line to standard error.
  966. ////////////////////////////////////////////////////////////////////
  967. bool PPCommandFile::
  968. handle_print_command() {
  969. cerr << _scope->expand_string(_params) << "\n";
  970. return true;
  971. }
  972. ////////////////////////////////////////////////////////////////////
  973. // Function: PPCommandFile::handle_include_command
  974. // Access: Protected
  975. // Description: Handles the #include command: the indicated file is
  976. // read and processed at this point.
  977. ////////////////////////////////////////////////////////////////////
  978. bool PPCommandFile::
  979. handle_include_command() {
  980. string filename = trim_blanks(_scope->expand_string(_params));
  981. // We allow optional quotation marks around the filename.
  982. if (filename.length() >= 2 &&
  983. filename[0] == '"' &&
  984. filename[filename.length() - 1] == '"') {
  985. filename = filename.substr(1, filename.length() - 2);
  986. }
  987. return include_file(filename);
  988. }
  989. ////////////////////////////////////////////////////////////////////
  990. // Function: PPCommandFile::handle_sinclude_command
  991. // Access: Protected
  992. // Description: Handles the #sinclude command: the indicated file is
  993. // read and processed at this point. This is different
  994. // from #include only in that if the file does not
  995. // exist, there is no error; instead, nothing happens.
  996. ////////////////////////////////////////////////////////////////////
  997. bool PPCommandFile::
  998. handle_sinclude_command() {
  999. string filename = trim_blanks(_scope->expand_string(_params));
  1000. // We allow optional quotation marks around the filename.
  1001. if (filename.length() >= 2 &&
  1002. filename[0] == '"' &&
  1003. filename[filename.length() - 1] == '"') {
  1004. filename = filename.substr(1, filename.length() - 2);
  1005. }
  1006. if (access(filename.c_str(), F_OK) != 0) {
  1007. // No such file; no error.
  1008. return true;
  1009. }
  1010. return include_file(filename);
  1011. }
  1012. ////////////////////////////////////////////////////////////////////
  1013. // Function: PPCommandFile::handle_call_command
  1014. // Access: Protected
  1015. // Description: Handles the #call command: the indicated named
  1016. // subroutine is read and processed at this point.
  1017. ////////////////////////////////////////////////////////////////////
  1018. bool PPCommandFile::
  1019. handle_call_command() {
  1020. // The first word is the name of the subroutine; the rest is the
  1021. // comma-separated list of expressions to substitute in for the
  1022. // subroutine's formal parameters.
  1023. // Pull off the first word and the rest of the params.
  1024. size_t p = 0;
  1025. while (p < _params.length() && !isspace(_params[p])) {
  1026. p++;
  1027. }
  1028. string subroutine_name = trim_blanks(_params.substr(0, p));
  1029. string params = _params.substr(p);
  1030. if (subroutine_name.empty()) {
  1031. cerr << "#call requires at least one parameter.\n";
  1032. return false;
  1033. }
  1034. const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
  1035. if (sub == (const PPSubroutine *)NULL) {
  1036. cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
  1037. }
  1038. PPScope *old_scope = _scope;
  1039. PPScope::push_scope(_scope);
  1040. PPScope nested_scope(_scope->get_named_scopes());
  1041. _scope = &nested_scope;
  1042. nested_scope.define_formals(subroutine_name, sub->_formals, params);
  1043. vector<string>::const_iterator li;
  1044. for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
  1045. if (!read_line(*li)) {
  1046. PPScope::pop_scope();
  1047. _scope = old_scope;
  1048. return false;
  1049. }
  1050. }
  1051. PPScope::pop_scope();
  1052. _scope = old_scope;
  1053. return true;
  1054. }
  1055. ////////////////////////////////////////////////////////////////////
  1056. // Function: PPCommandFile::handle_error_command
  1057. // Access: Protected
  1058. // Description: Handles the #error command: terminate immediately
  1059. // with the given error message.
  1060. ////////////////////////////////////////////////////////////////////
  1061. bool PPCommandFile::
  1062. handle_error_command() {
  1063. string message = trim_blanks(_scope->expand_string(_params));
  1064. if (!message.empty()) {
  1065. cerr << message << "\n";
  1066. }
  1067. return false;
  1068. }
  1069. ////////////////////////////////////////////////////////////////////
  1070. // Function: PPCommandFile::handle_defer_command
  1071. // Access: Protected
  1072. // Description: Handles the #defer command: define a new variable or
  1073. // change the definition of an existing variable. This
  1074. // is different from #define in that the variable
  1075. // definition is not immediately expanded; it will be
  1076. // expanded when the variable is later used. This
  1077. // allows the definition of variables that depend on
  1078. // other variables whose values have not yet been
  1079. // defined. This is akin to GNU make's = assignment.
  1080. ////////////////////////////////////////////////////////////////////
  1081. bool PPCommandFile::
  1082. handle_defer_command() {
  1083. // Pull off the first word and the rest of the params.
  1084. size_t p = 0;
  1085. while (p < _params.length() && !isspace(_params[p])) {
  1086. p++;
  1087. }
  1088. string varname = _params.substr(0, p);
  1089. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1090. cerr << "Warning: variable " << varname
  1091. << " shadowed by function definition.\n";
  1092. }
  1093. // Skip whitespace between the variable name and its definition.
  1094. while (p < _params.length() && isspace(_params[p])) {
  1095. p++;
  1096. }
  1097. string def = _params.substr(p);
  1098. // We don't expand the variable's definition immediately; it will be
  1099. // expanded when the variable is referenced later. However, we
  1100. // should expand any simple self-reference immediately, to allow for
  1101. // recursive definitions.
  1102. def = _scope->expand_self_reference(def, varname);
  1103. _scope->define_variable(varname, def);
  1104. return true;
  1105. }
  1106. ////////////////////////////////////////////////////////////////////
  1107. // Function: PPCommandFile::handle_define_command
  1108. // Access: Protected
  1109. // Description: Handles the #define command: define a new variable or
  1110. // change the definition of an existing variable. The
  1111. // variable definition is immediately expanded. This is
  1112. // akin to GNU make's := assignment.
  1113. ////////////////////////////////////////////////////////////////////
  1114. bool PPCommandFile::
  1115. handle_define_command() {
  1116. // Pull off the first word and the rest of the params.
  1117. size_t p = 0;
  1118. while (p < _params.length() && !isspace(_params[p])) {
  1119. p++;
  1120. }
  1121. string varname = _params.substr(0, p);
  1122. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1123. cerr << "Warning: variable " << varname
  1124. << " shadowed by function definition.\n";
  1125. }
  1126. // Skip whitespace between the variable name and its definition.
  1127. while (p < _params.length() && isspace(_params[p])) {
  1128. p++;
  1129. }
  1130. string def = _scope->expand_string(_params.substr(p));
  1131. _scope->define_variable(varname, def);
  1132. return true;
  1133. }
  1134. ////////////////////////////////////////////////////////////////////
  1135. // Function: PPCommandFile::handle_set_command
  1136. // Access: Protected
  1137. // Description: Handles the #set command: change the definition of an
  1138. // existing variable.
  1139. //
  1140. // This is different from #defer in two ways: (1) the
  1141. // variable in question must already have been #defined
  1142. // elsewhere, (2) if the variable was #defined in some
  1143. // parent scope, this will actually change the variable
  1144. // in the parent scope, rather than shadowing it in the
  1145. // local scope. Like #define and unlike #defer, the
  1146. // variable definition is expanded immediately, similar
  1147. // to GNU make's := operator.
  1148. ////////////////////////////////////////////////////////////////////
  1149. bool PPCommandFile::
  1150. handle_set_command() {
  1151. // Pull off the first word and the rest of the params.
  1152. size_t p = 0;
  1153. while (p < _params.length() && !isspace(_params[p])) {
  1154. p++;
  1155. }
  1156. string varname = _params.substr(0, p);
  1157. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1158. cerr << "Warning: variable " << varname
  1159. << " shadowed by function definition.\n";
  1160. }
  1161. // Skip whitespace between the variable name and its definition.
  1162. while (p < _params.length() && isspace(_params[p])) {
  1163. p++;
  1164. }
  1165. string def = _scope->expand_string(_params.substr(p));
  1166. if (!_scope->set_variable(varname, def)) {
  1167. cerr << "Attempt to set undefined variable " << varname << "\n";
  1168. return false;
  1169. }
  1170. return true;
  1171. }
  1172. ////////////////////////////////////////////////////////////////////
  1173. // Function: PPCommandFile::handle_map_command
  1174. // Access: Protected
  1175. // Description: Handles the #map command: define a new map variable.
  1176. // This is a special kind of variable declaration that
  1177. // creates a variable that can be used as a function to
  1178. // look up variable expansions within a number of
  1179. // different named scopes, accessed by keyword.
  1180. ////////////////////////////////////////////////////////////////////
  1181. bool PPCommandFile::
  1182. handle_map_command() {
  1183. // Pull off the first word and the rest of the params.
  1184. size_t p = 0;
  1185. while (p < _params.length() && !isspace(_params[p])) {
  1186. p++;
  1187. }
  1188. string varname = _params.substr(0, p);
  1189. // Skip whitespace between the variable name and its definition.
  1190. while (p < _params.length() && isspace(_params[p])) {
  1191. p++;
  1192. }
  1193. string def = trim_blanks(_params.substr(p));
  1194. _scope->define_map_variable(varname, def);
  1195. return true;
  1196. }
  1197. ////////////////////////////////////////////////////////////////////
  1198. // Function: PPCommandFile::handle_addmap_command
  1199. // Access: Protected
  1200. // Description: Handles the #addmap command: add a new key/scope pair
  1201. // to an existing map variable.
  1202. ////////////////////////////////////////////////////////////////////
  1203. bool PPCommandFile::
  1204. handle_addmap_command() {
  1205. // Pull off the first word and the rest of the params.
  1206. size_t p = 0;
  1207. while (p < _params.length() && !isspace(_params[p])) {
  1208. p++;
  1209. }
  1210. string varname = _params.substr(0, p);
  1211. // Skip whitespace between the variable name and the key.
  1212. while (p < _params.length() && isspace(_params[p])) {
  1213. p++;
  1214. }
  1215. string key = trim_blanks(_scope->expand_string(_params.substr(p)));
  1216. _scope->add_to_map_variable(varname, key, _scope);
  1217. return true;
  1218. }
  1219. ////////////////////////////////////////////////////////////////////
  1220. // Function: PPCommandFile::include_file
  1221. // Access: Protected
  1222. // Description: The internal implementation of #include: includes a
  1223. // particular named file at this point.
  1224. ////////////////////////////////////////////////////////////////////
  1225. bool PPCommandFile::
  1226. include_file(const string &filename) {
  1227. ifstream in(filename.c_str());
  1228. if (!in) {
  1229. cerr << "Unable to open include file " << filename << ".\n";
  1230. return false;
  1231. }
  1232. PushFilename pushed(_scope, filename);
  1233. string line;
  1234. getline(in, line);
  1235. while (!in.fail() && !in.eof()) {
  1236. if (!read_line(line)) {
  1237. return false;
  1238. }
  1239. getline(in, line);
  1240. }
  1241. if (!in.eof()) {
  1242. cerr << "Error reading " << filename << ".\n";
  1243. return false;
  1244. }
  1245. return true;
  1246. }
  1247. ////////////////////////////////////////////////////////////////////
  1248. // Function: PPCommandFile::replay_forscopes
  1249. // Access: Protected
  1250. // Description: Replays all the lines that were saved during a
  1251. // previous #forscopes..#end block.
  1252. ////////////////////////////////////////////////////////////////////
  1253. bool PPCommandFile::
  1254. replay_forscopes(const string &name) {
  1255. assert(!_in_for);
  1256. bool okflag = true;
  1257. vector<string> lines;
  1258. lines.swap(_saved_lines);
  1259. // Remove the #end command. This will fail if someone makes an #end
  1260. // command that spans multiple lines. Don't do that.
  1261. assert(!lines.empty());
  1262. lines.pop_back();
  1263. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1264. // Extract out the scope names from the #forscopes .. #end name. This
  1265. // is a space-delimited list of scope names.
  1266. vector<string> words;
  1267. tokenize_whitespace(name, words);
  1268. // Now build up the list of scopes with these names.
  1269. PPNamedScopes::Scopes scopes;
  1270. vector<string>::const_iterator wi;
  1271. for (wi = words.begin(); wi != words.end(); ++wi) {
  1272. named_scopes->get_scopes(*wi, scopes);
  1273. }
  1274. PPNamedScopes::sort_by_dependency(scopes);
  1275. // And finally, replay all of the saved lines.
  1276. BlockNesting *saved_block = _block_nesting;
  1277. IfNesting *saved_if = _if_nesting;
  1278. PPNamedScopes::Scopes::const_iterator si;
  1279. for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
  1280. PPScope::push_scope(_scope);
  1281. _scope = (*si);
  1282. vector<string>::const_iterator li;
  1283. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1284. okflag = read_line(*li);
  1285. }
  1286. _scope = PPScope::pop_scope();
  1287. }
  1288. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1289. cerr << "Misplaced #end or #endif.\n";
  1290. okflag = false;
  1291. }
  1292. return okflag;
  1293. }
  1294. ////////////////////////////////////////////////////////////////////
  1295. // Function: PPCommandFile::replay_foreach
  1296. // Access: Protected
  1297. // Description: Replays all the lines that were saved during a
  1298. // previous #foreach..#end block.
  1299. ////////////////////////////////////////////////////////////////////
  1300. bool PPCommandFile::
  1301. replay_foreach(const string &varname, const vector<string> &words) {
  1302. assert(!_in_for);
  1303. bool okflag = true;
  1304. vector<string> lines;
  1305. lines.swap(_saved_lines);
  1306. // Remove the #end command. This will fail if someone makes an #end
  1307. // command that spans multiple lines. Don't do that.
  1308. assert(!lines.empty());
  1309. lines.pop_back();
  1310. // Now traverse through the saved words.
  1311. BlockNesting *saved_block = _block_nesting;
  1312. IfNesting *saved_if = _if_nesting;
  1313. vector<string>::const_iterator wi;
  1314. for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
  1315. _scope->define_variable(varname, (*wi));
  1316. vector<string>::const_iterator li;
  1317. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1318. okflag = read_line(*li);
  1319. }
  1320. }
  1321. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1322. cerr << "Misplaced #end or #endif.\n";
  1323. okflag = false;
  1324. }
  1325. return okflag;
  1326. }
  1327. ////////////////////////////////////////////////////////////////////
  1328. // Function: PPCommandFile::replay_formap
  1329. // Access: Protected
  1330. // Description: Replays all the lines that were saved during a
  1331. // previous #formap..#end block.
  1332. ////////////////////////////////////////////////////////////////////
  1333. bool PPCommandFile::
  1334. replay_formap(const string &varname, const string &mapvar) {
  1335. assert(!_in_for);
  1336. bool okflag = true;
  1337. vector<string> lines;
  1338. lines.swap(_saved_lines);
  1339. // Remove the #end command. This will fail if someone makes an #end
  1340. // command that spans multiple lines. Don't do that.
  1341. assert(!lines.empty());
  1342. lines.pop_back();
  1343. // Now look up the map variable.
  1344. PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
  1345. if (&def == &PPScope::_null_map_def) {
  1346. cerr << "Undefined map variable: #formap " << varname << " "
  1347. << mapvar << "\n";
  1348. return false;
  1349. }
  1350. // Now traverse through the map definition.
  1351. BlockNesting *saved_block = _block_nesting;
  1352. IfNesting *saved_if = _if_nesting;
  1353. PPScope::MapVariableDefinition::const_iterator di;
  1354. for (di = def.begin(); di != def.end() && okflag; ++di) {
  1355. _scope->define_variable(varname, (*di).first);
  1356. PPScope::push_scope(_scope);
  1357. _scope = (*di).second;
  1358. vector<string>::const_iterator li;
  1359. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1360. okflag = read_line(*li);
  1361. }
  1362. _scope = PPScope::pop_scope();
  1363. }
  1364. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1365. cerr << "Misplaced #end or #endif.\n";
  1366. okflag = false;
  1367. }
  1368. return okflag;
  1369. }
  1370. ////////////////////////////////////////////////////////////////////
  1371. // Function: PPCommandFile::compare_output
  1372. // Access: Protected
  1373. // Description: After a file has been written to a (potentially
  1374. // large) string via an #output command, compare the
  1375. // results to the original file. If they are different,
  1376. // remove the original file and replace it with the new
  1377. // contents; otherwise, leave the original alone.
  1378. ////////////////////////////////////////////////////////////////////
  1379. bool PPCommandFile::
  1380. compare_output(const string &new_contents, const string &filename,
  1381. bool notouch) {
  1382. bool exists = (access(filename.c_str(), F_OK) == 0);
  1383. bool differ = false;
  1384. if (exists) {
  1385. size_t len = new_contents.length();
  1386. size_t want_bytes = len + 1;
  1387. char *orig_contents = new char[want_bytes];
  1388. ifstream in(filename.c_str());
  1389. in.read(orig_contents, want_bytes);
  1390. if (in.gcount() != len) {
  1391. // The wrong number of bytes.
  1392. differ = true;
  1393. } else {
  1394. differ = !(new_contents == string(orig_contents, len));
  1395. }
  1396. }
  1397. if (differ || !exists) {
  1398. cerr << "Generating " << filename << "\n";
  1399. if (exists) {
  1400. if (unlink(filename.c_str()) < 0) {
  1401. cerr << "Unable to remove old " << filename << "\n";
  1402. return false;
  1403. }
  1404. }
  1405. ofstream out_b(filename.c_str(), ios::out, 0666);
  1406. if (!out_b) {
  1407. cerr << "Unable to open file " << filename << " for writing.\n";
  1408. return false;
  1409. }
  1410. out_b.write(new_contents.data(), new_contents.length());
  1411. if (!out_b) {
  1412. cerr << "Unable to write to file " << filename << "\n";
  1413. return false;
  1414. }
  1415. out_b.close();
  1416. } else {
  1417. // cerr << "File " << filename << " is unchanged.\n";
  1418. // Even though the file is unchanged, unless the "notouch" flag is
  1419. // set, we want to update the modification time. This helps the
  1420. // makefiles know we did something.
  1421. if (!notouch) {
  1422. if (utime(filename.c_str(), (struct utimbuf *)NULL) < 0) {
  1423. cerr << "Warning: unable to touch " << filename << "\n";
  1424. }
  1425. }
  1426. }
  1427. return true;
  1428. }
  1429. ////////////////////////////////////////////////////////////////////
  1430. // Function: PPCommandFile::failed_if
  1431. // Access: Protected
  1432. // Description: Returns true if we are currently within a failed #if
  1433. // block (or in an #else block for a passed #if block),
  1434. // or false otherwise.
  1435. ////////////////////////////////////////////////////////////////////
  1436. bool PPCommandFile::
  1437. failed_if() const {
  1438. return (_if_nesting != (IfNesting *)NULL &&
  1439. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1440. }
  1441. ////////////////////////////////////////////////////////////////////
  1442. // Function: PPCommandFile::is_valid_formal_parameter_name
  1443. // Access: Protected
  1444. // Description: Returns true if the indicated name is an acceptable
  1445. // name for a formal parameter. This means it includes
  1446. // no whitespace or crazy punctuation. Mainly this is
  1447. // to protect the user from making some stupid syntax
  1448. // mistake.
  1449. ////////////////////////////////////////////////////////////////////
  1450. bool PPCommandFile::
  1451. is_valid_formal(const string &formal_parameter_name) const {
  1452. if (formal_parameter_name.empty()) {
  1453. return false;
  1454. }
  1455. string::const_iterator si;
  1456. for (si = formal_parameter_name.begin();
  1457. si != formal_parameter_name.end();
  1458. ++si) {
  1459. switch (*si) {
  1460. case ' ':
  1461. case '\n':
  1462. case '\t':
  1463. case '$':
  1464. case '[':
  1465. case ']':
  1466. case ',':
  1467. return false;
  1468. }
  1469. }
  1470. return true;
  1471. }
  1472. ////////////////////////////////////////////////////////////////////
  1473. // Function: PPCommandFile::PushFilename::Constructor
  1474. // Access: Public
  1475. // Description: This special class changes the current filename of
  1476. // the PPCommandFile. The idea is to create one of
  1477. // these when the filename is changed (for instance, to
  1478. // read in a new file via an #include directive); when
  1479. // the variable then goes out of scope, it will restore
  1480. // the previous filename.
  1481. //
  1482. // This updates the scope with the appropriate
  1483. // variables.
  1484. ////////////////////////////////////////////////////////////////////
  1485. PPCommandFile::PushFilename::
  1486. PushFilename(PPScope *scope, const string &filename) {
  1487. _scope = scope;
  1488. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1489. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1490. string thisfilename = filename;
  1491. string thisdirprefix;
  1492. size_t slash = filename.rfind('/');
  1493. if (slash == string::npos) {
  1494. thisdirprefix = string();
  1495. } else {
  1496. thisdirprefix = filename.substr(0, slash + 1);
  1497. }
  1498. _scope->define_variable("THISFILENAME", thisfilename);
  1499. _scope->define_variable("THISDIRPREFIX", thisdirprefix);
  1500. }
  1501. ////////////////////////////////////////////////////////////////////
  1502. // Function: PPCommandFile::PushFilename::Destructor
  1503. // Access: Public
  1504. // Description:
  1505. ////////////////////////////////////////////////////////////////////
  1506. PPCommandFile::PushFilename::
  1507. ~PushFilename() {
  1508. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1509. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1510. }