ppCommandFile.cxx 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  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. if(line[p+1]==COMMAND_PREFIX) {
  397. // double prefix at start of line indicates echo single prefix, like '\\' in C
  398. line.erase(0,1);
  399. }
  400. return _write_state->write_line(_scope->expand_string(line));
  401. }
  402. }
  403. }
  404. return true;
  405. }
  406. ////////////////////////////////////////////////////////////////////
  407. // Function: PPCommandFile::end_read
  408. // Access: Public
  409. // Description: Finishes up the input stream, after a sequence of
  410. // read_line() calls.
  411. ////////////////////////////////////////////////////////////////////
  412. bool PPCommandFile::
  413. end_read() {
  414. bool okflag = true;
  415. if (_if_nesting != (IfNesting *)NULL) {
  416. cerr << "Unclosed if\n";
  417. _if_nesting = (IfNesting *)NULL;
  418. okflag = false;
  419. }
  420. if (_block_nesting != (BlockNesting *)NULL) {
  421. switch (_block_nesting->_state) {
  422. case BS_begin:
  423. cerr << "Unclosed begin " << _block_nesting->_name << "\n";
  424. break;
  425. case BS_forscopes:
  426. case BS_nested_forscopes:
  427. cerr << "Unclosed forscopes " << _block_nesting->_name << "\n";
  428. break;
  429. case BS_foreach:
  430. case BS_nested_foreach:
  431. cerr << "Unclosed foreach " << _block_nesting->_name << "\n";
  432. break;
  433. case BS_formap:
  434. case BS_nested_formap:
  435. cerr << "Unclosed formap " << _block_nesting->_name << "\n";
  436. break;
  437. case BS_defsub:
  438. cerr << "Unclosed defsub " << _block_nesting->_name << "\n";
  439. break;
  440. case BS_defun:
  441. cerr << "Unclosed defun " << _block_nesting->_name << "\n";
  442. break;
  443. case BS_output:
  444. cerr << "Unclosed output " << _block_nesting->_name << "\n";
  445. break;
  446. }
  447. _block_nesting = (BlockNesting *)NULL;
  448. okflag = false;
  449. }
  450. return okflag;
  451. }
  452. ////////////////////////////////////////////////////////////////////
  453. // Function: PPCommandFile::handle_command
  454. // Access: Protected
  455. // Description: Handles a macro command.
  456. ////////////////////////////////////////////////////////////////////
  457. bool PPCommandFile::
  458. handle_command(const string &line) {
  459. if (_got_command) {
  460. // If we were still processing a command from last time, keep
  461. // going; this line is just a continuation. But skip any initial
  462. // whitespace.
  463. size_t p = 0;
  464. while (p < line.length() && isspace(line[p])) {
  465. p++;
  466. }
  467. _params += ' ';
  468. _params += line.substr(p);
  469. } else {
  470. // This is the first line of a new command.
  471. // Pull off the first word and the rest of the line.
  472. size_t p = 0;
  473. while (p < line.length() && !isspace(line[p])) {
  474. p++;
  475. }
  476. _command = line.substr(0, p);
  477. // Skip whitespace between the command and its arguments.
  478. while (p < line.length() && isspace(line[p])) {
  479. p++;
  480. }
  481. _params = line.substr(p);
  482. }
  483. if (!_params.empty() && _params[_params.length() - 1] == '\\') {
  484. // If the line ends with a backslash, there's more to come before
  485. // we can process the command.
  486. _got_command = true;
  487. // Truncate off the backslash, and any whitespace before it.
  488. size_t p = _params.length() - 1;
  489. while (p > 0 && isspace(_params[p - 1])) {
  490. p--;
  491. }
  492. _params = _params.substr(0, p);
  493. return true;
  494. }
  495. // We're completely done scanning the command now.
  496. _got_command = false;
  497. if (_command == "if") {
  498. return handle_if_command();
  499. } else if (_command == "elif") {
  500. return handle_elif_command();
  501. } else if (_command == "else") {
  502. return handle_else_command();
  503. } else if (_command == "endif") {
  504. return handle_endif_command();
  505. } else if (failed_if()) {
  506. // If we're in the middle of a failed #if, we ignore all commands
  507. // except for the if-related commands, above.
  508. return true;
  509. } else if (_command == "begin") {
  510. return handle_begin_command();
  511. } else if (_command == "forscopes") {
  512. return handle_forscopes_command();
  513. } else if (_command == "foreach") {
  514. return handle_foreach_command();
  515. } else if (_command == "formap") {
  516. return handle_formap_command();
  517. } else if (_command == "defsub") {
  518. return handle_defsub_command(true);
  519. } else if (_command == "defun") {
  520. return handle_defsub_command(false);
  521. } else if (_command == "output") {
  522. return handle_output_command();
  523. } else if (_command == "end") {
  524. return handle_end_command();
  525. } else if (_in_for) {
  526. // If we're currently saving up lines within a block sequence, we
  527. // ignore all commands except for the block-related commands,
  528. // above.
  529. return true;
  530. } else if (_command == "format") {
  531. return handle_format_command();
  532. } else if (_command == "print") {
  533. return handle_print_command();
  534. } else if (_command == "include") {
  535. return handle_include_command();
  536. } else if (_command == "sinclude") {
  537. return handle_sinclude_command();
  538. } else if (_command == "call") {
  539. return handle_call_command();
  540. } else if (_command == "error") {
  541. return handle_error_command();
  542. } else if (_command == "defer") {
  543. return handle_defer_command();
  544. } else if (_command == "define") {
  545. return handle_define_command();
  546. } else if (_command == "set") {
  547. return handle_set_command();
  548. } else if (_command == "map") {
  549. return handle_map_command();
  550. } else if (_command == "addmap") {
  551. return handle_addmap_command();
  552. }
  553. cerr << "Invalid command: " << COMMAND_PREFIX << _command << "\n";
  554. return false;
  555. }
  556. ////////////////////////////////////////////////////////////////////
  557. // Function: PPCommandFile::handle_if_command
  558. // Access: Protected
  559. // Description: Handles the #if command: conditionally evaluate the
  560. // following code.
  561. ////////////////////////////////////////////////////////////////////
  562. bool PPCommandFile::
  563. handle_if_command() {
  564. if (failed_if()) {
  565. // If we're *already* inside a failed if, we don't have to
  566. // evaluate this one, but we do need to record the nesting level.
  567. IfNesting *nest = new IfNesting(IS_done);
  568. nest->push(this);
  569. } else {
  570. // If the parameter string evaluates to empty, the case is false.
  571. // Otherwise the case is true. However, if we're currently
  572. // scanning #forscopes or something, we don't evaluate this at
  573. // all, because it doesn't matter.
  574. bool is_empty = true;
  575. if (!_in_for) {
  576. _params = _scope->expand_string(_params);
  577. string::const_iterator si;
  578. for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
  579. is_empty = isspace(*si);
  580. }
  581. }
  582. IfState state = is_empty ? IS_off : IS_on;
  583. IfNesting *nest = new IfNesting(state);
  584. nest->push(this);
  585. }
  586. return true;
  587. }
  588. ////////////////////////////////////////////////////////////////////
  589. // Function: PPCommandFile::handle_elif_command
  590. // Access: Protected
  591. // Description: Handles the #elif command: conditionally evaluate
  592. // the following code, following a failed #if command.
  593. ////////////////////////////////////////////////////////////////////
  594. bool PPCommandFile::
  595. handle_elif_command() {
  596. if (_if_nesting == (IfNesting *)NULL) {
  597. cerr << "elif encountered without if.\n";
  598. return false;
  599. }
  600. if (_if_nesting->_state == IS_else) {
  601. cerr << "elif encountered after else.\n";
  602. return false;
  603. }
  604. if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
  605. // If we passed the #if above, we don't need to evaluate the #elif.
  606. _if_nesting->_state = IS_done;
  607. return true;
  608. }
  609. // If the parameter string evaluates to empty, the case is false.
  610. // Otherwise the case is true.
  611. bool is_empty = true;
  612. if (!_in_for) {
  613. _params = _scope->expand_string(_params);
  614. string::const_iterator si;
  615. for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
  616. is_empty = isspace(*si);
  617. }
  618. }
  619. _if_nesting->_state = is_empty ? IS_off : IS_on;
  620. return true;
  621. }
  622. ////////////////////////////////////////////////////////////////////
  623. // Function: PPCommandFile::handle_else_command
  624. // Access: Protected
  625. // Description: Handles the #else command: evaluate the following
  626. // code following a failed #if command.
  627. ////////////////////////////////////////////////////////////////////
  628. bool PPCommandFile::
  629. handle_else_command() {
  630. if (_if_nesting == (IfNesting *)NULL) {
  631. cerr << "else encountered without if.\n";
  632. return false;
  633. }
  634. if (_if_nesting->_state == IS_else) {
  635. cerr << "else encountered after else.\n";
  636. return false;
  637. }
  638. if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
  639. _if_nesting->_state = IS_done;
  640. return true;
  641. }
  642. _if_nesting->_state = IS_else;
  643. return true;
  644. }
  645. ////////////////////////////////////////////////////////////////////
  646. // Function: PPCommandFile::handle_endif_command
  647. // Access: Protected
  648. // Description: Handles the #endif command: close a preceeding #if
  649. // command.
  650. ////////////////////////////////////////////////////////////////////
  651. bool PPCommandFile::
  652. handle_endif_command() {
  653. if (_if_nesting == (IfNesting *)NULL) {
  654. cerr << "endif encountered without if.\n";
  655. return false;
  656. }
  657. IfNesting *nest = _if_nesting;
  658. nest->pop(this);
  659. if (nest->_block != _block_nesting) {
  660. cerr << "If block not closed within scoping block.\n";
  661. return false;
  662. }
  663. delete nest;
  664. return true;
  665. }
  666. ////////////////////////////////////////////////////////////////////
  667. // Function: PPCommandFile::handle_begin_command
  668. // Access: Protected
  669. // Description: Handles the #begin command: begin a named scope
  670. // block. The variables defined between this command
  671. // and the corresponding #end command will be local to
  672. // this named scope.
  673. ////////////////////////////////////////////////////////////////////
  674. bool PPCommandFile::
  675. handle_begin_command() {
  676. string name = trim_blanks(_scope->expand_string(_params));
  677. BlockNesting *nest = new BlockNesting(BS_begin, name);
  678. if (contains_whitespace(name)) {
  679. cerr << "Attempt to define scope named \"" << name
  680. << "\".\nScope names may not contain whitespace.\n";
  681. return false;
  682. }
  683. if (name.find(SCOPE_DIRNAME_SEPARATOR) != string::npos) {
  684. cerr << "Attempt to define scope named \"" << name
  685. << "\".\nScope names may not contain the '"
  686. << SCOPE_DIRNAME_SEPARATOR << "' character.\n";
  687. return false;
  688. }
  689. nest->push(this);
  690. PPScope *named_scope = _scope->get_named_scopes()->make_scope(name);
  691. named_scope->set_parent(_scope);
  692. _scope = named_scope;
  693. return true;
  694. }
  695. ////////////////////////////////////////////////////////////////////
  696. // Function: PPCommandFile::handle_forscopes_command
  697. // Access: Protected
  698. // Description: Handles the #forscopes command: interpret all the lines
  699. // between this command and the corresponding #end
  700. // command once for each occurrence of a named scope
  701. // with the given name.
  702. ////////////////////////////////////////////////////////////////////
  703. bool PPCommandFile::
  704. handle_forscopes_command() {
  705. BlockState state = _in_for ? BS_nested_forscopes : BS_forscopes;
  706. string name = trim_blanks(_scope->expand_string(_params));
  707. BlockNesting *nest = new BlockNesting(state, name);
  708. nest->push(this);
  709. if (!_in_for) {
  710. _in_for = true;
  711. _saved_lines.clear();
  712. }
  713. return true;
  714. }
  715. ////////////////////////////////////////////////////////////////////
  716. // Function: PPCommandFile::handle_foreach_command
  717. // Access: Protected
  718. // Description: Handles the #foreach command: interpret all the lines
  719. // between this command and the corresponding #end
  720. // command once for each word in the argument.
  721. ////////////////////////////////////////////////////////////////////
  722. bool PPCommandFile::
  723. handle_foreach_command() {
  724. // Get the parameters of the foreach command. The first word is the
  725. // name of the variable to substitute in (and which should appear on
  726. // the matching #end command), and the remaining words are the
  727. // values to substitute in.
  728. vector<string> words;
  729. tokenize_whitespace(_scope->expand_string(_params), words);
  730. if (words.empty()) {
  731. cerr << "#foreach requires at least one parameter.\n";
  732. return false;
  733. }
  734. string variable_name = words.front();
  735. BlockState state = _in_for ? BS_nested_foreach : BS_foreach;
  736. BlockNesting *nest = new BlockNesting(state, variable_name);
  737. nest->push(this);
  738. // We insert in all but the first word in the words vector.
  739. nest->_words.insert(nest->_words.end(), words.begin() + 1, words.end());
  740. if (!_in_for) {
  741. _in_for = true;
  742. _saved_lines.clear();
  743. }
  744. return true;
  745. }
  746. ////////////////////////////////////////////////////////////////////
  747. // Function: PPCommandFile::handle_formap_command
  748. // Access: Protected
  749. // Description: Handles the #formap command: interpret all the lines
  750. // between this command and the corresponding #end
  751. // command once for each key in the map, and also within
  752. // the corresponding scope of that particular key.
  753. ////////////////////////////////////////////////////////////////////
  754. bool PPCommandFile::
  755. handle_formap_command() {
  756. // Get the parameters of the formap command. The first word is the
  757. // name of the variable to substitute in (and which should appear on
  758. // the matching #end command), and the second word is the name of
  759. // the map variable.
  760. vector<string> words;
  761. tokenize_whitespace(_scope->expand_string(_params), words);
  762. if (words.size() != 2) {
  763. cerr << "#formap requires exactly two parameters.\n";
  764. return false;
  765. }
  766. string variable_name = words.front();
  767. BlockState state = _in_for ? BS_nested_formap : BS_formap;
  768. BlockNesting *nest = new BlockNesting(state, words[0]);
  769. nest->push(this);
  770. nest->_words.push_back(words[1]);
  771. if (!_in_for) {
  772. _in_for = true;
  773. _saved_lines.clear();
  774. }
  775. return true;
  776. }
  777. ////////////////////////////////////////////////////////////////////
  778. // Function: PPCommandFile::handle_defsub_command
  779. // Access: Protected
  780. // Description: Handles the #defsub (or #defun) command: save all the
  781. // lines between this command and the matching #end as a
  782. // callable subroutine to be invoked by a later #call
  783. // command. If is_defsub is false, it means this
  784. // subroutine was actually defined via a #defun command,
  785. // so it is to be invoked by a later variable reference,
  786. // instead of by a #call command.
  787. ////////////////////////////////////////////////////////////////////
  788. bool PPCommandFile::
  789. handle_defsub_command(bool is_defsub) {
  790. string command = (is_defsub) ? "#defsub" : "#defun";
  791. // The first word of the parameter list is the subroutine name; the
  792. // rest is the comma-separated list of formal parameter names.
  793. // Pull off the first word and the rest of the params.
  794. size_t p = 0;
  795. while (p < _params.length() && !isspace(_params[p])) {
  796. p++;
  797. }
  798. string subroutine_name = trim_blanks(_params.substr(0, p));
  799. if (subroutine_name.empty()) {
  800. cerr << command << " requires at least one parameter.\n";
  801. return false;
  802. }
  803. vector<string> formals;
  804. _scope->tokenize_params(_params.substr(p), formals, false);
  805. vector<string>::const_iterator fi;
  806. for (fi = formals.begin(); fi != formals.end(); ++fi) {
  807. if (!is_valid_formal(*fi)) {
  808. cerr << command << " " << subroutine_name
  809. << ": invalid formal parameter name '" << (*fi) << "'\n";
  810. return false;
  811. }
  812. }
  813. if (_in_for) {
  814. cerr << command << " may not appear within another block scoping command like\n"
  815. << "#forscopes, #foreach, #formap, #defsub, or #defun.\n";
  816. return false;
  817. }
  818. BlockState state = is_defsub ? BS_defsub : BS_defun;
  819. BlockNesting *nest = new BlockNesting(state, subroutine_name);
  820. nest->push(this);
  821. nest->_words.swap(formals);
  822. _in_for = true;
  823. _saved_lines.clear();
  824. return true;
  825. }
  826. ////////////////////////////////////////////////////////////////////
  827. // Function: PPCommandFile::handle_output_command
  828. // Access: Protected
  829. // Description: Handles the #output command: all text between this
  830. // command and the corresponding #end command will be
  831. // sent to the indicated output file.
  832. ////////////////////////////////////////////////////////////////////
  833. bool PPCommandFile::
  834. handle_output_command() {
  835. vector<string> words;
  836. tokenize_whitespace(_scope->expand_string(_params), words);
  837. if (words.empty()) {
  838. cerr << "#output command requires one parameter.\n";
  839. return false;
  840. }
  841. BlockNesting *nest = new BlockNesting(BS_output, words[0]);
  842. // Also check the output flags.
  843. for (int i = 1; i < (int)words.size(); i++) {
  844. if (words[i] == "notouch") {
  845. nest->_flags |= OF_notouch;
  846. } else {
  847. cerr << "Invalid output flag: " << words[i] << "\n";
  848. }
  849. }
  850. nest->push(this);
  851. if (!_in_for) {
  852. string filename = nest->_name;
  853. if (filename.empty()) {
  854. cerr << "Attempt to output to empty filename\n";
  855. return false;
  856. }
  857. string prefix = _scope->expand_variable("DIRPREFIX");
  858. if (filename[0] != '/') {
  859. filename = prefix + filename;
  860. }
  861. nest->_filename = filename;
  862. // Generate an in-memory copy of the file first.
  863. _write_state = new WriteState(*_write_state);
  864. _write_state->_out = &nest->_output;
  865. }
  866. return true;
  867. }
  868. ////////////////////////////////////////////////////////////////////
  869. // Function: PPCommandFile::handle_end_command
  870. // Access: Protected
  871. // Description: Handles the #end command. This closes a number of
  872. // different kinds of blocks, like #begin and #forscopes.
  873. ////////////////////////////////////////////////////////////////////
  874. bool PPCommandFile::
  875. handle_end_command() {
  876. if (_block_nesting == (BlockNesting *)NULL) {
  877. cerr << "Unmatched end " << _params << ".\n";
  878. return false;
  879. }
  880. string name = trim_blanks(_scope->expand_string(_params));
  881. if (name != _block_nesting->_name) {
  882. cerr << "end " << name << " encountered where end "
  883. << _block_nesting->_name << " expected.\n";
  884. return false;
  885. }
  886. BlockNesting *nest = _block_nesting;
  887. nest->pop(this);
  888. if (nest->_if != _if_nesting) {
  889. cerr << "If block not closed within scoping block.\n";
  890. return false;
  891. }
  892. if (nest->_state == BS_forscopes) {
  893. // Now replay all of the saved lines.
  894. _in_for = false;
  895. if (!replay_forscopes(nest->_name)) {
  896. return false;
  897. }
  898. } else if (nest->_state == BS_foreach) {
  899. // Now replay all of the saved lines.
  900. _in_for = false;
  901. if (!replay_foreach(nest->_name, nest->_words)) {
  902. return false;
  903. }
  904. } else if (nest->_state == BS_formap) {
  905. // Now replay all of the saved lines.
  906. _in_for = false;
  907. assert(nest->_words.size() == 1);
  908. if (!replay_formap(nest->_name, nest->_words[0])) {
  909. return false;
  910. }
  911. } else if (nest->_state == BS_defsub || nest->_state == BS_defun) {
  912. // Save all of the saved lines as a named subroutine.
  913. _in_for = false;
  914. PPSubroutine *sub = new PPSubroutine;
  915. sub->_formals.swap(nest->_words);
  916. sub->_lines.swap(_saved_lines);
  917. // Remove the #end command. This will fail if someone makes an
  918. // #end command that spans multiple lines. Don't do that.
  919. assert(!sub->_lines.empty());
  920. sub->_lines.pop_back();
  921. if (nest->_state == BS_defsub) {
  922. PPSubroutine::define_sub(nest->_name, sub);
  923. } else {
  924. PPSubroutine::define_func(nest->_name, sub);
  925. }
  926. } else if (nest->_state == BS_output) {
  927. if (!_in_for) {
  928. if (!nest->_output) {
  929. cerr << "Error while writing " << nest->_filename << "\n";
  930. return false;
  931. }
  932. // Now compare the file we generated to the file that's already
  933. // there, if there is one.
  934. nest->_output << ends;
  935. const char *generated_file = nest->_output.str();
  936. if (!compare_output(generated_file, nest->_filename,
  937. (nest->_flags & OF_notouch) != 0)) {
  938. return false;
  939. }
  940. }
  941. }
  942. delete nest;
  943. return true;
  944. }
  945. ////////////////////////////////////////////////////////////////////
  946. // Function: PPCommandFile::handle_format_command
  947. // Access: Protected
  948. // Description: Handles the #format command: change the formatting
  949. // mode of lines as they are output.
  950. ////////////////////////////////////////////////////////////////////
  951. bool PPCommandFile::
  952. handle_format_command() {
  953. _params = trim_blanks(_scope->expand_string(_params));
  954. if (_params == "straight") {
  955. _write_state->_format = WF_straight;
  956. } else if (_params == "collapse") {
  957. _write_state->_format = WF_collapse;
  958. } else if (_params == "makefile") {
  959. _write_state->_format = WF_makefile;
  960. } else {
  961. cerr << "Ignoring invalid write format: " << _params << "\n";
  962. }
  963. return true;
  964. }
  965. ////////////////////////////////////////////////////////////////////
  966. // Function: PPCommandFile::handle_print_command
  967. // Access: Protected
  968. // Description: Handles the #print command: immediately output the
  969. // arguments to this line to standard error.
  970. ////////////////////////////////////////////////////////////////////
  971. bool PPCommandFile::
  972. handle_print_command() {
  973. cerr << _scope->expand_string(_params) << "\n";
  974. return true;
  975. }
  976. ////////////////////////////////////////////////////////////////////
  977. // Function: PPCommandFile::handle_include_command
  978. // Access: Protected
  979. // Description: Handles the #include command: the indicated file is
  980. // read and processed at this point.
  981. ////////////////////////////////////////////////////////////////////
  982. bool PPCommandFile::
  983. handle_include_command() {
  984. string filename = trim_blanks(_scope->expand_string(_params));
  985. // We allow optional quotation marks around the filename.
  986. if (filename.length() >= 2 &&
  987. filename[0] == '"' &&
  988. filename[filename.length() - 1] == '"') {
  989. filename = filename.substr(1, filename.length() - 2);
  990. }
  991. return include_file(filename);
  992. }
  993. ////////////////////////////////////////////////////////////////////
  994. // Function: PPCommandFile::handle_sinclude_command
  995. // Access: Protected
  996. // Description: Handles the #sinclude command: the indicated file is
  997. // read and processed at this point. This is different
  998. // from #include only in that if the file does not
  999. // exist, there is no error; instead, nothing happens.
  1000. ////////////////////////////////////////////////////////////////////
  1001. bool PPCommandFile::
  1002. handle_sinclude_command() {
  1003. string filename = trim_blanks(_scope->expand_string(_params));
  1004. // We allow optional quotation marks around the filename.
  1005. if (filename.length() >= 2 &&
  1006. filename[0] == '"' &&
  1007. filename[filename.length() - 1] == '"') {
  1008. filename = filename.substr(1, filename.length() - 2);
  1009. }
  1010. if (access(filename.c_str(), F_OK) != 0) {
  1011. // No such file; no error.
  1012. return true;
  1013. }
  1014. return include_file(filename);
  1015. }
  1016. ////////////////////////////////////////////////////////////////////
  1017. // Function: PPCommandFile::handle_call_command
  1018. // Access: Protected
  1019. // Description: Handles the #call command: the indicated named
  1020. // subroutine is read and processed at this point.
  1021. ////////////////////////////////////////////////////////////////////
  1022. bool PPCommandFile::
  1023. handle_call_command() {
  1024. // The first word is the name of the subroutine; the rest is the
  1025. // comma-separated list of expressions to substitute in for the
  1026. // subroutine's formal parameters.
  1027. // Pull off the first word and the rest of the params.
  1028. size_t p = 0;
  1029. while (p < _params.length() && !isspace(_params[p])) {
  1030. p++;
  1031. }
  1032. string subroutine_name = trim_blanks(_params.substr(0, p));
  1033. string params = _params.substr(p);
  1034. if (subroutine_name.empty()) {
  1035. cerr << "#call requires at least one parameter.\n";
  1036. return false;
  1037. }
  1038. const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
  1039. if (sub == (const PPSubroutine *)NULL) {
  1040. cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
  1041. }
  1042. PPScope *old_scope = _scope;
  1043. PPScope::push_scope(_scope);
  1044. PPScope nested_scope(_scope->get_named_scopes());
  1045. _scope = &nested_scope;
  1046. nested_scope.define_formals(subroutine_name, sub->_formals, params);
  1047. vector<string>::const_iterator li;
  1048. for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
  1049. if (!read_line(*li)) {
  1050. PPScope::pop_scope();
  1051. _scope = old_scope;
  1052. return false;
  1053. }
  1054. }
  1055. PPScope::pop_scope();
  1056. _scope = old_scope;
  1057. return true;
  1058. }
  1059. ////////////////////////////////////////////////////////////////////
  1060. // Function: PPCommandFile::handle_error_command
  1061. // Access: Protected
  1062. // Description: Handles the #error command: terminate immediately
  1063. // with the given error message.
  1064. ////////////////////////////////////////////////////////////////////
  1065. bool PPCommandFile::
  1066. handle_error_command() {
  1067. string message = trim_blanks(_scope->expand_string(_params));
  1068. if (!message.empty()) {
  1069. cerr << message << "\n";
  1070. }
  1071. return false;
  1072. }
  1073. ////////////////////////////////////////////////////////////////////
  1074. // Function: PPCommandFile::handle_defer_command
  1075. // Access: Protected
  1076. // Description: Handles the #defer command: define a new variable or
  1077. // change the definition of an existing variable. This
  1078. // is different from #define in that the variable
  1079. // definition is not immediately expanded; it will be
  1080. // expanded when the variable is later used. This
  1081. // allows the definition of variables that depend on
  1082. // other variables whose values have not yet been
  1083. // defined. This is akin to GNU make's = assignment.
  1084. ////////////////////////////////////////////////////////////////////
  1085. bool PPCommandFile::
  1086. handle_defer_command() {
  1087. // Pull off the first word and the rest of the params.
  1088. size_t p = 0;
  1089. while (p < _params.length() && !isspace(_params[p])) {
  1090. p++;
  1091. }
  1092. string varname = _params.substr(0, p);
  1093. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1094. cerr << "Warning: variable " << varname
  1095. << " shadowed by function definition.\n";
  1096. }
  1097. // Skip whitespace between the variable name and its definition.
  1098. while (p < _params.length() && isspace(_params[p])) {
  1099. p++;
  1100. }
  1101. string def = _params.substr(p);
  1102. // We don't expand the variable's definition immediately; it will be
  1103. // expanded when the variable is referenced later. However, we
  1104. // should expand any simple self-reference immediately, to allow for
  1105. // recursive definitions.
  1106. def = _scope->expand_self_reference(def, varname);
  1107. _scope->define_variable(varname, def);
  1108. return true;
  1109. }
  1110. ////////////////////////////////////////////////////////////////////
  1111. // Function: PPCommandFile::handle_define_command
  1112. // Access: Protected
  1113. // Description: Handles the #define command: define a new variable or
  1114. // change the definition of an existing variable. The
  1115. // variable definition is immediately expanded. This is
  1116. // akin to GNU make's := assignment.
  1117. ////////////////////////////////////////////////////////////////////
  1118. bool PPCommandFile::
  1119. handle_define_command() {
  1120. // Pull off the first word and the rest of the params.
  1121. size_t p = 0;
  1122. while (p < _params.length() && !isspace(_params[p])) {
  1123. p++;
  1124. }
  1125. string varname = _params.substr(0, p);
  1126. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1127. cerr << "Warning: variable " << varname
  1128. << " shadowed by function definition.\n";
  1129. }
  1130. // Skip whitespace between the variable name and its definition.
  1131. while (p < _params.length() && isspace(_params[p])) {
  1132. p++;
  1133. }
  1134. string def = _scope->expand_string(_params.substr(p));
  1135. _scope->define_variable(varname, def);
  1136. return true;
  1137. }
  1138. ////////////////////////////////////////////////////////////////////
  1139. // Function: PPCommandFile::handle_set_command
  1140. // Access: Protected
  1141. // Description: Handles the #set command: change the definition of an
  1142. // existing variable.
  1143. //
  1144. // This is different from #defer in two ways: (1) the
  1145. // variable in question must already have been #defined
  1146. // elsewhere, (2) if the variable was #defined in some
  1147. // parent scope, this will actually change the variable
  1148. // in the parent scope, rather than shadowing it in the
  1149. // local scope. Like #define and unlike #defer, the
  1150. // variable definition is expanded immediately, similar
  1151. // to GNU make's := operator.
  1152. ////////////////////////////////////////////////////////////////////
  1153. bool PPCommandFile::
  1154. handle_set_command() {
  1155. // Pull off the first word and the rest of the params.
  1156. size_t p = 0;
  1157. while (p < _params.length() && !isspace(_params[p])) {
  1158. p++;
  1159. }
  1160. string varname = _params.substr(0, p);
  1161. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1162. cerr << "Warning: variable " << varname
  1163. << " shadowed by function definition.\n";
  1164. }
  1165. // Skip whitespace between the variable name and its definition.
  1166. while (p < _params.length() && isspace(_params[p])) {
  1167. p++;
  1168. }
  1169. string def = _scope->expand_string(_params.substr(p));
  1170. if (!_scope->set_variable(varname, def)) {
  1171. cerr << "Attempt to set undefined variable " << varname << "\n";
  1172. return false;
  1173. }
  1174. return true;
  1175. }
  1176. ////////////////////////////////////////////////////////////////////
  1177. // Function: PPCommandFile::handle_map_command
  1178. // Access: Protected
  1179. // Description: Handles the #map command: define a new map variable.
  1180. // This is a special kind of variable declaration that
  1181. // creates a variable that can be used as a function to
  1182. // look up variable expansions within a number of
  1183. // different named scopes, accessed by keyword.
  1184. ////////////////////////////////////////////////////////////////////
  1185. bool PPCommandFile::
  1186. handle_map_command() {
  1187. // Pull off the first word and the rest of the params.
  1188. size_t p = 0;
  1189. while (p < _params.length() && !isspace(_params[p])) {
  1190. p++;
  1191. }
  1192. string varname = _params.substr(0, p);
  1193. // Skip whitespace between the variable name and its definition.
  1194. while (p < _params.length() && isspace(_params[p])) {
  1195. p++;
  1196. }
  1197. string def = trim_blanks(_params.substr(p));
  1198. _scope->define_map_variable(varname, def);
  1199. return true;
  1200. }
  1201. ////////////////////////////////////////////////////////////////////
  1202. // Function: PPCommandFile::handle_addmap_command
  1203. // Access: Protected
  1204. // Description: Handles the #addmap command: add a new key/scope pair
  1205. // to an existing map variable.
  1206. ////////////////////////////////////////////////////////////////////
  1207. bool PPCommandFile::
  1208. handle_addmap_command() {
  1209. // Pull off the first word and the rest of the params.
  1210. size_t p = 0;
  1211. while (p < _params.length() && !isspace(_params[p])) {
  1212. p++;
  1213. }
  1214. string varname = _params.substr(0, p);
  1215. // Skip whitespace between the variable name and the key.
  1216. while (p < _params.length() && isspace(_params[p])) {
  1217. p++;
  1218. }
  1219. string key = trim_blanks(_scope->expand_string(_params.substr(p)));
  1220. _scope->add_to_map_variable(varname, key, _scope);
  1221. return true;
  1222. }
  1223. ////////////////////////////////////////////////////////////////////
  1224. // Function: PPCommandFile::include_file
  1225. // Access: Protected
  1226. // Description: The internal implementation of #include: includes a
  1227. // particular named file at this point.
  1228. ////////////////////////////////////////////////////////////////////
  1229. bool PPCommandFile::
  1230. include_file(const string &filename) {
  1231. ifstream in(filename.c_str());
  1232. if (!in) {
  1233. cerr << "Unable to open include file " << filename << ".\n";
  1234. return false;
  1235. }
  1236. PushFilename pushed(_scope, filename);
  1237. string line;
  1238. getline(in, line);
  1239. while (!in.fail() && !in.eof()) {
  1240. if (!read_line(line)) {
  1241. return false;
  1242. }
  1243. getline(in, line);
  1244. }
  1245. if (!in.eof()) {
  1246. cerr << "Error reading " << filename << ".\n";
  1247. return false;
  1248. }
  1249. return true;
  1250. }
  1251. ////////////////////////////////////////////////////////////////////
  1252. // Function: PPCommandFile::replay_forscopes
  1253. // Access: Protected
  1254. // Description: Replays all the lines that were saved during a
  1255. // previous #forscopes..#end block.
  1256. ////////////////////////////////////////////////////////////////////
  1257. bool PPCommandFile::
  1258. replay_forscopes(const string &name) {
  1259. assert(!_in_for);
  1260. bool okflag = true;
  1261. vector<string> lines;
  1262. lines.swap(_saved_lines);
  1263. // Remove the #end command. This will fail if someone makes an #end
  1264. // command that spans multiple lines. Don't do that.
  1265. assert(!lines.empty());
  1266. lines.pop_back();
  1267. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1268. // Extract out the scope names from the #forscopes .. #end name. This
  1269. // is a space-delimited list of scope names.
  1270. vector<string> words;
  1271. tokenize_whitespace(name, words);
  1272. // Now build up the list of scopes with these names.
  1273. PPNamedScopes::Scopes scopes;
  1274. vector<string>::const_iterator wi;
  1275. for (wi = words.begin(); wi != words.end(); ++wi) {
  1276. named_scopes->get_scopes(*wi, scopes);
  1277. }
  1278. PPNamedScopes::sort_by_dependency(scopes);
  1279. // And finally, replay all of the saved lines.
  1280. BlockNesting *saved_block = _block_nesting;
  1281. IfNesting *saved_if = _if_nesting;
  1282. PPNamedScopes::Scopes::const_iterator si;
  1283. for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
  1284. PPScope::push_scope(_scope);
  1285. _scope = (*si);
  1286. vector<string>::const_iterator li;
  1287. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1288. okflag = read_line(*li);
  1289. }
  1290. _scope = PPScope::pop_scope();
  1291. }
  1292. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1293. cerr << "Misplaced #end or #endif.\n";
  1294. okflag = false;
  1295. }
  1296. return okflag;
  1297. }
  1298. ////////////////////////////////////////////////////////////////////
  1299. // Function: PPCommandFile::replay_foreach
  1300. // Access: Protected
  1301. // Description: Replays all the lines that were saved during a
  1302. // previous #foreach..#end block.
  1303. ////////////////////////////////////////////////////////////////////
  1304. bool PPCommandFile::
  1305. replay_foreach(const string &varname, const vector<string> &words) {
  1306. assert(!_in_for);
  1307. bool okflag = true;
  1308. vector<string> lines;
  1309. lines.swap(_saved_lines);
  1310. // Remove the #end command. This will fail if someone makes an #end
  1311. // command that spans multiple lines. Don't do that.
  1312. assert(!lines.empty());
  1313. lines.pop_back();
  1314. // Now traverse through the saved words.
  1315. BlockNesting *saved_block = _block_nesting;
  1316. IfNesting *saved_if = _if_nesting;
  1317. vector<string>::const_iterator wi;
  1318. for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
  1319. _scope->define_variable(varname, (*wi));
  1320. vector<string>::const_iterator li;
  1321. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1322. okflag = read_line(*li);
  1323. }
  1324. }
  1325. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1326. cerr << "Misplaced #end or #endif.\n";
  1327. okflag = false;
  1328. }
  1329. return okflag;
  1330. }
  1331. ////////////////////////////////////////////////////////////////////
  1332. // Function: PPCommandFile::replay_formap
  1333. // Access: Protected
  1334. // Description: Replays all the lines that were saved during a
  1335. // previous #formap..#end block.
  1336. ////////////////////////////////////////////////////////////////////
  1337. bool PPCommandFile::
  1338. replay_formap(const string &varname, const string &mapvar) {
  1339. assert(!_in_for);
  1340. bool okflag = true;
  1341. vector<string> lines;
  1342. lines.swap(_saved_lines);
  1343. // Remove the #end command. This will fail if someone makes an #end
  1344. // command that spans multiple lines. Don't do that.
  1345. assert(!lines.empty());
  1346. lines.pop_back();
  1347. // Now look up the map variable.
  1348. PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
  1349. if (&def == &PPScope::_null_map_def) {
  1350. cerr << "Undefined map variable: #formap " << varname << " "
  1351. << mapvar << "\n";
  1352. return false;
  1353. }
  1354. // Now traverse through the map definition.
  1355. BlockNesting *saved_block = _block_nesting;
  1356. IfNesting *saved_if = _if_nesting;
  1357. PPScope::MapVariableDefinition::const_iterator di;
  1358. for (di = def.begin(); di != def.end() && okflag; ++di) {
  1359. _scope->define_variable(varname, (*di).first);
  1360. PPScope::push_scope(_scope);
  1361. _scope = (*di).second;
  1362. vector<string>::const_iterator li;
  1363. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1364. okflag = read_line(*li);
  1365. }
  1366. _scope = PPScope::pop_scope();
  1367. }
  1368. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1369. cerr << "Misplaced #end or #endif.\n";
  1370. okflag = false;
  1371. }
  1372. return okflag;
  1373. }
  1374. ////////////////////////////////////////////////////////////////////
  1375. // Function: PPCommandFile::compare_output
  1376. // Access: Protected
  1377. // Description: After a file has been written to a (potentially
  1378. // large) string via an #output command, compare the
  1379. // results to the original file. If they are different,
  1380. // remove the original file and replace it with the new
  1381. // contents; otherwise, leave the original alone.
  1382. ////////////////////////////////////////////////////////////////////
  1383. bool PPCommandFile::
  1384. compare_output(const string &new_contents, const string &filename,
  1385. bool notouch) {
  1386. bool exists = (access(filename.c_str(), F_OK) == 0);
  1387. bool differ = false;
  1388. if (exists) {
  1389. size_t len = new_contents.length();
  1390. size_t want_bytes = len + 1;
  1391. char *orig_contents = new char[want_bytes];
  1392. ifstream in(filename.c_str());
  1393. in.read(orig_contents, want_bytes);
  1394. if (in.gcount() != len) {
  1395. // The wrong number of bytes.
  1396. differ = true;
  1397. } else {
  1398. differ = !(new_contents == string(orig_contents, len));
  1399. }
  1400. }
  1401. if (differ || !exists) {
  1402. cerr << "Generating " << filename << "\n";
  1403. if (exists) {
  1404. if (unlink(filename.c_str()) < 0) {
  1405. cerr << "Unable to remove old " << filename << "\n";
  1406. return false;
  1407. }
  1408. }
  1409. ofstream out_b(filename.c_str(), ios::out, 0666);
  1410. if (!out_b) {
  1411. cerr << "Unable to open file " << filename << " for writing.\n";
  1412. return false;
  1413. }
  1414. out_b.write(new_contents.data(), new_contents.length());
  1415. if (!out_b) {
  1416. cerr << "Unable to write to file " << filename << "\n";
  1417. return false;
  1418. }
  1419. out_b.close();
  1420. } else {
  1421. // cerr << "File " << filename << " is unchanged.\n";
  1422. // Even though the file is unchanged, unless the "notouch" flag is
  1423. // set, we want to update the modification time. This helps the
  1424. // makefiles know we did something.
  1425. if (!notouch) {
  1426. if (utime(filename.c_str(), (struct utimbuf *)NULL) < 0) {
  1427. cerr << "Warning: unable to touch " << filename << "\n";
  1428. }
  1429. }
  1430. }
  1431. return true;
  1432. }
  1433. ////////////////////////////////////////////////////////////////////
  1434. // Function: PPCommandFile::failed_if
  1435. // Access: Protected
  1436. // Description: Returns true if we are currently within a failed #if
  1437. // block (or in an #else block for a passed #if block),
  1438. // or false otherwise.
  1439. ////////////////////////////////////////////////////////////////////
  1440. bool PPCommandFile::
  1441. failed_if() const {
  1442. return (_if_nesting != (IfNesting *)NULL &&
  1443. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1444. }
  1445. ////////////////////////////////////////////////////////////////////
  1446. // Function: PPCommandFile::is_valid_formal_parameter_name
  1447. // Access: Protected
  1448. // Description: Returns true if the indicated name is an acceptable
  1449. // name for a formal parameter. This means it includes
  1450. // no whitespace or crazy punctuation. Mainly this is
  1451. // to protect the user from making some stupid syntax
  1452. // mistake.
  1453. ////////////////////////////////////////////////////////////////////
  1454. bool PPCommandFile::
  1455. is_valid_formal(const string &formal_parameter_name) const {
  1456. if (formal_parameter_name.empty()) {
  1457. return false;
  1458. }
  1459. string::const_iterator si;
  1460. for (si = formal_parameter_name.begin();
  1461. si != formal_parameter_name.end();
  1462. ++si) {
  1463. switch (*si) {
  1464. case ' ':
  1465. case '\n':
  1466. case '\t':
  1467. case '$':
  1468. case '[':
  1469. case ']':
  1470. case ',':
  1471. return false;
  1472. }
  1473. }
  1474. return true;
  1475. }
  1476. ////////////////////////////////////////////////////////////////////
  1477. // Function: PPCommandFile::PushFilename::Constructor
  1478. // Access: Public
  1479. // Description: This special class changes the current filename of
  1480. // the PPCommandFile. The idea is to create one of
  1481. // these when the filename is changed (for instance, to
  1482. // read in a new file via an #include directive); when
  1483. // the variable then goes out of scope, it will restore
  1484. // the previous filename.
  1485. //
  1486. // This updates the scope with the appropriate
  1487. // variables.
  1488. ////////////////////////////////////////////////////////////////////
  1489. PPCommandFile::PushFilename::
  1490. PushFilename(PPScope *scope, const string &filename) {
  1491. _scope = scope;
  1492. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1493. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1494. string thisfilename = filename;
  1495. string thisdirprefix;
  1496. size_t slash = filename.rfind('/');
  1497. if (slash == string::npos) {
  1498. thisdirprefix = string();
  1499. } else {
  1500. thisdirprefix = filename.substr(0, slash + 1);
  1501. }
  1502. _scope->define_variable("THISFILENAME", thisfilename);
  1503. _scope->define_variable("THISDIRPREFIX", thisdirprefix);
  1504. }
  1505. ////////////////////////////////////////////////////////////////////
  1506. // Function: PPCommandFile::PushFilename::Destructor
  1507. // Access: Public
  1508. // Description:
  1509. ////////////////////////////////////////////////////////////////////
  1510. PPCommandFile::PushFilename::
  1511. ~PushFilename() {
  1512. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1513. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1514. }