ppCommandFile.cxx 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795
  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. static const string begin_comment(BEGIN_COMMENT);
  17. ////////////////////////////////////////////////////////////////////
  18. // Function: PPCommandFile::IfNesting::Constructor
  19. // Access: Public
  20. // Description:
  21. ////////////////////////////////////////////////////////////////////
  22. PPCommandFile::IfNesting::
  23. IfNesting(IfState state) :
  24. _state(state)
  25. {
  26. _block = (PPCommandFile::BlockNesting *)NULL;
  27. _next = (PPCommandFile::IfNesting *)NULL;
  28. }
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: PPCommandFile::IfNesting::push
  31. // Access: Public
  32. // Description: Adds this IfNesting object to the top of the
  33. // nesting stack.
  34. ////////////////////////////////////////////////////////////////////
  35. void PPCommandFile::IfNesting::
  36. push(PPCommandFile *file) {
  37. _block = file->_block_nesting;
  38. _next = file->_if_nesting;
  39. file->_if_nesting = this;
  40. }
  41. ////////////////////////////////////////////////////////////////////
  42. // Function: PPCommandFile::IfNesting::pop
  43. // Access: Public
  44. // Description: Removes this IfNesting object from the top of the
  45. // nesting stack, and restores the command file's
  46. // nesting state.
  47. ////////////////////////////////////////////////////////////////////
  48. void PPCommandFile::IfNesting::
  49. pop(PPCommandFile *file) {
  50. assert(file->_if_nesting == this);
  51. file->_if_nesting = _next;
  52. }
  53. ////////////////////////////////////////////////////////////////////
  54. // Function: PPCommandFile::WriteState::Constructor
  55. // Access: Public
  56. // Description:
  57. ////////////////////////////////////////////////////////////////////
  58. PPCommandFile::WriteState::
  59. WriteState() {
  60. _out = &cout;
  61. _format = WF_collapse;
  62. _last_blank = true;
  63. }
  64. ////////////////////////////////////////////////////////////////////
  65. // Function: PPCommandFile::WriteState::Copy Constructor
  66. // Access: Public
  67. // Description:
  68. ////////////////////////////////////////////////////////////////////
  69. PPCommandFile::WriteState::
  70. WriteState(const WriteState &copy) :
  71. _out(copy._out),
  72. _format(copy._format),
  73. _last_blank(copy._last_blank)
  74. {
  75. }
  76. ////////////////////////////////////////////////////////////////////
  77. // Function: PPCommandFile::WriteState::write_line
  78. // Access: Public
  79. // Description:
  80. ////////////////////////////////////////////////////////////////////
  81. bool PPCommandFile::WriteState::
  82. write_line(const string &line) {
  83. switch (_format) {
  84. case WF_straight:
  85. (*_out) << line << "\n";
  86. return true;
  87. case WF_collapse:
  88. return write_collapse_line(line);
  89. case WF_makefile:
  90. return write_makefile_line(line);
  91. }
  92. cerr << "Unsupported write format: " << (int)_format << "\n";
  93. return false;
  94. }
  95. ////////////////////////////////////////////////////////////////////
  96. // Function: PPCommandFile::WriteState::write_collapse_line
  97. // Access: Public
  98. // Description:
  99. ////////////////////////////////////////////////////////////////////
  100. bool PPCommandFile::WriteState::
  101. write_collapse_line(const string &line) {
  102. if (line.empty()) {
  103. if (!_last_blank) {
  104. (*_out) << "\n";
  105. _last_blank = true;
  106. }
  107. } else {
  108. _last_blank = false;
  109. (*_out) << line << "\n";
  110. }
  111. return true;
  112. }
  113. ////////////////////////////////////////////////////////////////////
  114. // Function: PPCommandFile::WriteState::write_makefile_line
  115. // Access: Public
  116. // Description:
  117. ////////////////////////////////////////////////////////////////////
  118. bool PPCommandFile::WriteState::
  119. write_makefile_line(const string &line) {
  120. if (line.length() <= 72) {
  121. return write_collapse_line(line);
  122. }
  123. _last_blank = false;
  124. // In makefile mode, long variable assignment lines are folded after
  125. // the assignment.
  126. vector<string> words;
  127. tokenize_whitespace(line, words);
  128. if (words.size() > 2 && (words[1] == "=" || words[1] == ":")) {
  129. // This appears to be a variable assignment or a dependency rule;
  130. // fold it.
  131. (*_out) << words[0] << " " << words[1];
  132. vector<string>::const_iterator wi;
  133. int col = 80;
  134. wi = words.begin() + 2;
  135. while (wi != words.end()) {
  136. col += (*wi).length() + 1;
  137. if (col > 72) {
  138. (*_out) << " \\\n ";
  139. col = 4 + (*wi).length();
  140. }
  141. (*_out) << " " << (*wi);
  142. ++wi;
  143. }
  144. (*_out) << "\n";
  145. } else {
  146. // This is not a variable assignment, so just write it out.
  147. (*_out) << line << "\n";
  148. }
  149. return true;
  150. }
  151. ////////////////////////////////////////////////////////////////////
  152. // Function: PPCommandFile::BlockNesting::Constructor
  153. // Access: Public
  154. // Description:
  155. ////////////////////////////////////////////////////////////////////
  156. PPCommandFile::BlockNesting::
  157. BlockNesting(BlockState state, const string &name) :
  158. _state(state),
  159. _name(name)
  160. {
  161. _if = (PPCommandFile::IfNesting *)NULL;
  162. _write_state = (PPCommandFile::WriteState *)NULL;
  163. _scope = (PPScope *)NULL;
  164. _tempnam = (char *)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->_true_name = filename;
  858. nest->_tempnam = (char *)NULL;
  859. if (access(filename.c_str(), F_OK) == 0) {
  860. // If the file already exists, create a temporary file first.
  861. nest->_tempnam = tempnam((prefix + ".").c_str(), "pptmp");
  862. assert(nest->_tempnam != (char *)NULL);
  863. nest->_output.open(nest->_tempnam);
  864. if (nest->_output.fail()) {
  865. cerr << "Unable to open output file " << nest->_tempnam << "\n";
  866. return false;
  867. }
  868. } else {
  869. // If the file does not already exist, create it directly instead
  870. // of monkeying around with temporary files.
  871. cerr << "Generating " << filename << "\n";
  872. nest->_output.open(filename.c_str(), ios::out, 0666);
  873. if (nest->_output.fail()) {
  874. cerr << "Unable to open output file " << filename << "\n";
  875. return false;
  876. }
  877. }
  878. _write_state = new WriteState(*_write_state);
  879. _write_state->_out = &nest->_output;
  880. }
  881. return true;
  882. }
  883. ////////////////////////////////////////////////////////////////////
  884. // Function: PPCommandFile::handle_end_command
  885. // Access: Protected
  886. // Description: Handles the #end command. This closes a number of
  887. // different kinds of blocks, like #begin and #forscopes.
  888. ////////////////////////////////////////////////////////////////////
  889. bool PPCommandFile::
  890. handle_end_command() {
  891. if (_block_nesting == (BlockNesting *)NULL) {
  892. cerr << "Unmatched end " << _params << ".\n";
  893. return false;
  894. }
  895. string name = trim_blanks(_scope->expand_string(_params));
  896. if (name != _block_nesting->_name) {
  897. cerr << "end " << name << " encountered where end "
  898. << _block_nesting->_name << " expected.\n";
  899. return false;
  900. }
  901. BlockNesting *nest = _block_nesting;
  902. nest->pop(this);
  903. if (nest->_if != _if_nesting) {
  904. cerr << "If block not closed within scoping block.\n";
  905. return false;
  906. }
  907. if (nest->_state == BS_forscopes) {
  908. // Now replay all of the saved lines.
  909. _in_for = false;
  910. if (!replay_forscopes(nest->_name)) {
  911. return false;
  912. }
  913. } else if (nest->_state == BS_foreach) {
  914. // Now replay all of the saved lines.
  915. _in_for = false;
  916. if (!replay_foreach(nest->_name, nest->_words)) {
  917. return false;
  918. }
  919. } else if (nest->_state == BS_formap) {
  920. // Now replay all of the saved lines.
  921. _in_for = false;
  922. assert(nest->_words.size() == 1);
  923. if (!replay_formap(nest->_name, nest->_words[0])) {
  924. return false;
  925. }
  926. } else if (nest->_state == BS_defsub || nest->_state == BS_defun) {
  927. // Save all of the saved lines as a named subroutine.
  928. _in_for = false;
  929. PPSubroutine *sub = new PPSubroutine;
  930. sub->_formals.swap(nest->_words);
  931. sub->_lines.swap(_saved_lines);
  932. // Remove the #end command. This will fail if someone makes an
  933. // #end command that spans multiple lines. Don't do that.
  934. assert(!sub->_lines.empty());
  935. sub->_lines.pop_back();
  936. if (nest->_state == BS_defsub) {
  937. PPSubroutine::define_sub(nest->_name, sub);
  938. } else {
  939. PPSubroutine::define_func(nest->_name, sub);
  940. }
  941. } else if (nest->_state == BS_output) {
  942. if (!_in_for) {
  943. if (!nest->_output) {
  944. cerr << "Error while writing " << nest->_true_name << "\n";
  945. return false;
  946. }
  947. nest->_output.close();
  948. // Verify the output file.
  949. if (nest->_tempnam != (char *)NULL) {
  950. if (!compare_output(nest->_tempnam, nest->_true_name,
  951. (nest->_flags & OF_notouch) != 0)) {
  952. return false;
  953. }
  954. free(nest->_tempnam);
  955. }
  956. }
  957. }
  958. delete nest;
  959. return true;
  960. }
  961. ////////////////////////////////////////////////////////////////////
  962. // Function: PPCommandFile::handle_format_command
  963. // Access: Protected
  964. // Description: Handles the #format command: change the formatting
  965. // mode of lines as they are output.
  966. ////////////////////////////////////////////////////////////////////
  967. bool PPCommandFile::
  968. handle_format_command() {
  969. _params = trim_blanks(_scope->expand_string(_params));
  970. if (_params == "straight") {
  971. _write_state->_format = WF_straight;
  972. } else if (_params == "collapse") {
  973. _write_state->_format = WF_collapse;
  974. } else if (_params == "makefile") {
  975. _write_state->_format = WF_makefile;
  976. } else {
  977. cerr << "Ignoring invalid write format: " << _params << "\n";
  978. }
  979. return true;
  980. }
  981. ////////////////////////////////////////////////////////////////////
  982. // Function: PPCommandFile::handle_print_command
  983. // Access: Protected
  984. // Description: Handles the #print command: immediately output the
  985. // arguments to this line to standard error.
  986. ////////////////////////////////////////////////////////////////////
  987. bool PPCommandFile::
  988. handle_print_command() {
  989. cerr << _scope->expand_string(_params) << "\n";
  990. return true;
  991. }
  992. ////////////////////////////////////////////////////////////////////
  993. // Function: PPCommandFile::handle_include_command
  994. // Access: Protected
  995. // Description: Handles the #include command: the indicated file is
  996. // read and processed at this point.
  997. ////////////////////////////////////////////////////////////////////
  998. bool PPCommandFile::
  999. handle_include_command() {
  1000. string filename = trim_blanks(_scope->expand_string(_params));
  1001. // We allow optional quotation marks around the filename.
  1002. if (filename.length() >= 2 &&
  1003. filename[0] == '"' &&
  1004. filename[filename.length() - 1] == '"') {
  1005. filename = filename.substr(1, filename.length() - 2);
  1006. }
  1007. return include_file(filename);
  1008. }
  1009. ////////////////////////////////////////////////////////////////////
  1010. // Function: PPCommandFile::handle_sinclude_command
  1011. // Access: Protected
  1012. // Description: Handles the #sinclude command: the indicated file is
  1013. // read and processed at this point. This is different
  1014. // from #include only in that if the file does not
  1015. // exist, there is no error; instead, nothing happens.
  1016. ////////////////////////////////////////////////////////////////////
  1017. bool PPCommandFile::
  1018. handle_sinclude_command() {
  1019. string filename = trim_blanks(_scope->expand_string(_params));
  1020. // We allow optional quotation marks around the filename.
  1021. if (filename.length() >= 2 &&
  1022. filename[0] == '"' &&
  1023. filename[filename.length() - 1] == '"') {
  1024. filename = filename.substr(1, filename.length() - 2);
  1025. }
  1026. if (access(filename.c_str(), F_OK) != 0) {
  1027. // No such file; no error.
  1028. return true;
  1029. }
  1030. return include_file(filename);
  1031. }
  1032. ////////////////////////////////////////////////////////////////////
  1033. // Function: PPCommandFile::handle_call_command
  1034. // Access: Protected
  1035. // Description: Handles the #call command: the indicated named
  1036. // subroutine is read and processed at this point.
  1037. ////////////////////////////////////////////////////////////////////
  1038. bool PPCommandFile::
  1039. handle_call_command() {
  1040. // The first word is the name of the subroutine; the rest is the
  1041. // comma-separated list of expressions to substitute in for the
  1042. // subroutine's formal parameters.
  1043. // Pull off the first word and the rest of the params.
  1044. size_t p = 0;
  1045. while (p < _params.length() && !isspace(_params[p])) {
  1046. p++;
  1047. }
  1048. string subroutine_name = trim_blanks(_params.substr(0, p));
  1049. string params = _params.substr(p);
  1050. if (subroutine_name.empty()) {
  1051. cerr << "#call requires at least one parameter.\n";
  1052. return false;
  1053. }
  1054. const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
  1055. if (sub == (const PPSubroutine *)NULL) {
  1056. cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
  1057. }
  1058. PPScope *old_scope = _scope;
  1059. PPScope::push_scope(_scope);
  1060. PPScope nested_scope(_scope->get_named_scopes());
  1061. _scope = &nested_scope;
  1062. nested_scope.define_formals(subroutine_name, sub->_formals, params);
  1063. vector<string>::const_iterator li;
  1064. for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
  1065. if (!read_line(*li)) {
  1066. PPScope::pop_scope();
  1067. _scope = old_scope;
  1068. return false;
  1069. }
  1070. }
  1071. PPScope::pop_scope();
  1072. _scope = old_scope;
  1073. return true;
  1074. }
  1075. ////////////////////////////////////////////////////////////////////
  1076. // Function: PPCommandFile::handle_error_command
  1077. // Access: Protected
  1078. // Description: Handles the #error command: terminate immediately
  1079. // with the given error message.
  1080. ////////////////////////////////////////////////////////////////////
  1081. bool PPCommandFile::
  1082. handle_error_command() {
  1083. string message = trim_blanks(_scope->expand_string(_params));
  1084. if (!message.empty()) {
  1085. cerr << message << "\n";
  1086. }
  1087. return false;
  1088. }
  1089. ////////////////////////////////////////////////////////////////////
  1090. // Function: PPCommandFile::handle_defer_command
  1091. // Access: Protected
  1092. // Description: Handles the #defer command: define a new variable or
  1093. // change the definition of an existing variable. This
  1094. // is different from #define in that the variable
  1095. // definition is not immediately expanded; it will be
  1096. // expanded when the variable is later used. This
  1097. // allows the definition of variables that depend on
  1098. // other variables whose values have not yet been
  1099. // defined. This is akin to GNU make's = assignment.
  1100. ////////////////////////////////////////////////////////////////////
  1101. bool PPCommandFile::
  1102. handle_defer_command() {
  1103. // Pull off the first word and the rest of the params.
  1104. size_t p = 0;
  1105. while (p < _params.length() && !isspace(_params[p])) {
  1106. p++;
  1107. }
  1108. string varname = _params.substr(0, p);
  1109. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1110. cerr << "Warning: variable " << varname
  1111. << " shadowed by function definition.\n";
  1112. }
  1113. // Skip whitespace between the variable name and its definition.
  1114. while (p < _params.length() && isspace(_params[p])) {
  1115. p++;
  1116. }
  1117. string def = _params.substr(p);
  1118. // We don't expand the variable's definition immediately; it will be
  1119. // expanded when the variable is referenced later. However, we
  1120. // should expand any simple self-reference immediately, to allow for
  1121. // recursive definitions.
  1122. def = _scope->expand_self_reference(def, varname);
  1123. _scope->define_variable(varname, def);
  1124. return true;
  1125. }
  1126. ////////////////////////////////////////////////////////////////////
  1127. // Function: PPCommandFile::handle_define_command
  1128. // Access: Protected
  1129. // Description: Handles the #define command: define a new variable or
  1130. // change the definition of an existing variable. The
  1131. // variable definition is immediately expanded. This is
  1132. // akin to GNU make's := assignment.
  1133. ////////////////////////////////////////////////////////////////////
  1134. bool PPCommandFile::
  1135. handle_define_command() {
  1136. // Pull off the first word and the rest of the params.
  1137. size_t p = 0;
  1138. while (p < _params.length() && !isspace(_params[p])) {
  1139. p++;
  1140. }
  1141. string varname = _params.substr(0, p);
  1142. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1143. cerr << "Warning: variable " << varname
  1144. << " shadowed by function definition.\n";
  1145. }
  1146. // Skip whitespace between the variable name and its definition.
  1147. while (p < _params.length() && isspace(_params[p])) {
  1148. p++;
  1149. }
  1150. string def = _scope->expand_string(_params.substr(p));
  1151. _scope->define_variable(varname, def);
  1152. return true;
  1153. }
  1154. ////////////////////////////////////////////////////////////////////
  1155. // Function: PPCommandFile::handle_set_command
  1156. // Access: Protected
  1157. // Description: Handles the #set command: change the definition of an
  1158. // existing variable.
  1159. //
  1160. // This is different from #defer in two ways: (1) the
  1161. // variable in question must already have been #defined
  1162. // elsewhere, (2) if the variable was #defined in some
  1163. // parent scope, this will actually change the variable
  1164. // in the parent scope, rather than shadowing it in the
  1165. // local scope. Like #define and unlike #defer, the
  1166. // variable definition is expanded immediately, similar
  1167. // to GNU make's := operator.
  1168. ////////////////////////////////////////////////////////////////////
  1169. bool PPCommandFile::
  1170. handle_set_command() {
  1171. // Pull off the first word and the rest of the params.
  1172. size_t p = 0;
  1173. while (p < _params.length() && !isspace(_params[p])) {
  1174. p++;
  1175. }
  1176. string varname = _params.substr(0, p);
  1177. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1178. cerr << "Warning: variable " << varname
  1179. << " shadowed by function definition.\n";
  1180. }
  1181. // Skip whitespace between the variable name and its definition.
  1182. while (p < _params.length() && isspace(_params[p])) {
  1183. p++;
  1184. }
  1185. string def = _scope->expand_string(_params.substr(p));
  1186. if (!_scope->set_variable(varname, def)) {
  1187. cerr << "Attempt to set undefined variable " << varname << "\n";
  1188. return false;
  1189. }
  1190. return true;
  1191. }
  1192. ////////////////////////////////////////////////////////////////////
  1193. // Function: PPCommandFile::handle_map_command
  1194. // Access: Protected
  1195. // Description: Handles the #map command: define a new map variable.
  1196. // This is a special kind of variable declaration that
  1197. // creates a variable that can be used as a function to
  1198. // look up variable expansions within a number of
  1199. // different named scopes, accessed by keyword.
  1200. ////////////////////////////////////////////////////////////////////
  1201. bool PPCommandFile::
  1202. handle_map_command() {
  1203. // Pull off the first word and the rest of the params.
  1204. size_t p = 0;
  1205. while (p < _params.length() && !isspace(_params[p])) {
  1206. p++;
  1207. }
  1208. string varname = _params.substr(0, p);
  1209. // Skip whitespace between the variable name and its definition.
  1210. while (p < _params.length() && isspace(_params[p])) {
  1211. p++;
  1212. }
  1213. string def = trim_blanks(_params.substr(p));
  1214. _scope->define_map_variable(varname, def);
  1215. return true;
  1216. }
  1217. ////////////////////////////////////////////////////////////////////
  1218. // Function: PPCommandFile::handle_addmap_command
  1219. // Access: Protected
  1220. // Description: Handles the #addmap command: add a new key/scope pair
  1221. // to an existing map variable.
  1222. ////////////////////////////////////////////////////////////////////
  1223. bool PPCommandFile::
  1224. handle_addmap_command() {
  1225. // Pull off the first word and the rest of the params.
  1226. size_t p = 0;
  1227. while (p < _params.length() && !isspace(_params[p])) {
  1228. p++;
  1229. }
  1230. string varname = _params.substr(0, p);
  1231. // Skip whitespace between the variable name and the key.
  1232. while (p < _params.length() && isspace(_params[p])) {
  1233. p++;
  1234. }
  1235. string key = trim_blanks(_scope->expand_string(_params.substr(p)));
  1236. _scope->add_to_map_variable(varname, key, _scope);
  1237. return true;
  1238. }
  1239. ////////////////////////////////////////////////////////////////////
  1240. // Function: PPCommandFile::include_file
  1241. // Access: Protected
  1242. // Description: The internal implementation of #include: includes a
  1243. // particular named file at this point.
  1244. ////////////////////////////////////////////////////////////////////
  1245. bool PPCommandFile::
  1246. include_file(const string &filename) {
  1247. ifstream in(filename.c_str());
  1248. if (!in) {
  1249. cerr << "Unable to open include file " << filename << ".\n";
  1250. return false;
  1251. }
  1252. PushFilename pushed(_scope, filename);
  1253. string line;
  1254. getline(in, line);
  1255. while (!in.fail() && !in.eof()) {
  1256. if (!read_line(line)) {
  1257. return false;
  1258. }
  1259. getline(in, line);
  1260. }
  1261. if (!in.eof()) {
  1262. cerr << "Error reading " << filename << ".\n";
  1263. return false;
  1264. }
  1265. return true;
  1266. }
  1267. ////////////////////////////////////////////////////////////////////
  1268. // Function: PPCommandFile::replay_forscopes
  1269. // Access: Protected
  1270. // Description: Replays all the lines that were saved during a
  1271. // previous #forscopes..#end block.
  1272. ////////////////////////////////////////////////////////////////////
  1273. bool PPCommandFile::
  1274. replay_forscopes(const string &name) {
  1275. assert(!_in_for);
  1276. bool okflag = true;
  1277. vector<string> lines;
  1278. lines.swap(_saved_lines);
  1279. // Remove the #end command. This will fail if someone makes an #end
  1280. // command that spans multiple lines. Don't do that.
  1281. assert(!lines.empty());
  1282. lines.pop_back();
  1283. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1284. // Extract out the scope names from the #forscopes .. #end name. This
  1285. // is a space-delimited list of scope names.
  1286. vector<string> words;
  1287. tokenize_whitespace(name, words);
  1288. // Now build up the list of scopes with these names.
  1289. PPNamedScopes::Scopes scopes;
  1290. vector<string>::const_iterator wi;
  1291. for (wi = words.begin(); wi != words.end(); ++wi) {
  1292. named_scopes->get_scopes(*wi, scopes);
  1293. }
  1294. PPNamedScopes::sort_by_dependency(scopes);
  1295. // And finally, replay all of the saved lines.
  1296. BlockNesting *saved_block = _block_nesting;
  1297. IfNesting *saved_if = _if_nesting;
  1298. PPNamedScopes::Scopes::const_iterator si;
  1299. for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
  1300. PPScope::push_scope(_scope);
  1301. _scope = (*si);
  1302. vector<string>::const_iterator li;
  1303. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1304. okflag = read_line(*li);
  1305. }
  1306. _scope = PPScope::pop_scope();
  1307. }
  1308. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1309. cerr << "Misplaced #end or #endif.\n";
  1310. okflag = false;
  1311. }
  1312. return okflag;
  1313. }
  1314. ////////////////////////////////////////////////////////////////////
  1315. // Function: PPCommandFile::replay_foreach
  1316. // Access: Protected
  1317. // Description: Replays all the lines that were saved during a
  1318. // previous #foreach..#end block.
  1319. ////////////////////////////////////////////////////////////////////
  1320. bool PPCommandFile::
  1321. replay_foreach(const string &varname, const vector<string> &words) {
  1322. assert(!_in_for);
  1323. bool okflag = true;
  1324. vector<string> lines;
  1325. lines.swap(_saved_lines);
  1326. // Remove the #end command. This will fail if someone makes an #end
  1327. // command that spans multiple lines. Don't do that.
  1328. assert(!lines.empty());
  1329. lines.pop_back();
  1330. // Now traverse through the saved words.
  1331. BlockNesting *saved_block = _block_nesting;
  1332. IfNesting *saved_if = _if_nesting;
  1333. vector<string>::const_iterator wi;
  1334. for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
  1335. _scope->define_variable(varname, (*wi));
  1336. vector<string>::const_iterator li;
  1337. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1338. okflag = read_line(*li);
  1339. }
  1340. }
  1341. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1342. cerr << "Misplaced #end or #endif.\n";
  1343. okflag = false;
  1344. }
  1345. return okflag;
  1346. }
  1347. ////////////////////////////////////////////////////////////////////
  1348. // Function: PPCommandFile::replay_formap
  1349. // Access: Protected
  1350. // Description: Replays all the lines that were saved during a
  1351. // previous #formap..#end block.
  1352. ////////////////////////////////////////////////////////////////////
  1353. bool PPCommandFile::
  1354. replay_formap(const string &varname, const string &mapvar) {
  1355. assert(!_in_for);
  1356. bool okflag = true;
  1357. vector<string> lines;
  1358. lines.swap(_saved_lines);
  1359. // Remove the #end command. This will fail if someone makes an #end
  1360. // command that spans multiple lines. Don't do that.
  1361. assert(!lines.empty());
  1362. lines.pop_back();
  1363. // Now look up the map variable.
  1364. PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
  1365. if (&def == &PPScope::_null_map_def) {
  1366. cerr << "Undefined map variable: #formap " << varname << " "
  1367. << mapvar << "\n";
  1368. return false;
  1369. }
  1370. // Now traverse through the map definition.
  1371. BlockNesting *saved_block = _block_nesting;
  1372. IfNesting *saved_if = _if_nesting;
  1373. PPScope::MapVariableDefinition::const_iterator di;
  1374. for (di = def.begin(); di != def.end() && okflag; ++di) {
  1375. _scope->define_variable(varname, (*di).first);
  1376. PPScope::push_scope(_scope);
  1377. _scope = (*di).second;
  1378. vector<string>::const_iterator li;
  1379. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1380. okflag = read_line(*li);
  1381. }
  1382. _scope = PPScope::pop_scope();
  1383. }
  1384. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1385. cerr << "Misplaced #end or #endif.\n";
  1386. okflag = false;
  1387. }
  1388. return okflag;
  1389. }
  1390. ////////////////////////////////////////////////////////////////////
  1391. // Function: PPCommandFile::compare_output
  1392. // Access: Protected
  1393. // Description: After a temporary file has been written due to an
  1394. // #output command, compare the results to the original
  1395. // file. If they are different, remove the original
  1396. // file and rename the temporary file; if they are the
  1397. // same, remove the temporary file.
  1398. ////////////////////////////////////////////////////////////////////
  1399. bool PPCommandFile::
  1400. compare_output(const string &temp_name, const string &true_name,
  1401. bool notouch) {
  1402. ifstream in_a(temp_name.c_str());
  1403. ifstream in_b(true_name.c_str());
  1404. int a = in_a.get();
  1405. int b = in_b.get();
  1406. bool differ = (a != b);
  1407. while (!in_a.eof() && !in_b.eof() && !differ) {
  1408. a = in_a.get();
  1409. b = in_b.get();
  1410. differ = (a != b);
  1411. }
  1412. in_a.close();
  1413. in_b.close();
  1414. if (differ) {
  1415. cerr << "Generating " << true_name << "\n";
  1416. if (unlink(true_name.c_str()) < 0) {
  1417. cerr << "Unable to remove old " << true_name << "\n";
  1418. return false;
  1419. }
  1420. if (rename(temp_name.c_str(), true_name.c_str()) < 0) {
  1421. cerr << "Unable to rename temporary file " << temp_name
  1422. << " to " << true_name << "\n";
  1423. return false;
  1424. }
  1425. } else {
  1426. // cerr << "File " << true_name << " is unchanged.\n";
  1427. if (unlink(temp_name.c_str()) < 0) {
  1428. cerr << "Warning: unable to remove temporary file " << temp_name << "\n";
  1429. }
  1430. // Even though the file is unchanged, unless the "notouch" flag is
  1431. // set, we want to update the modification time. This helps the
  1432. // makefiles know we did something.
  1433. if (!notouch) {
  1434. if (utime(true_name.c_str(), (struct utimbuf *)NULL) < 0) {
  1435. cerr << "Warning: unable to touch " << true_name << "\n";
  1436. }
  1437. }
  1438. }
  1439. return true;
  1440. }
  1441. ////////////////////////////////////////////////////////////////////
  1442. // Function: PPCommandFile::failed_if
  1443. // Access: Protected
  1444. // Description: Returns true if we are currently within a failed #if
  1445. // block (or in an #else block for a passed #if block),
  1446. // or false otherwise.
  1447. ////////////////////////////////////////////////////////////////////
  1448. bool PPCommandFile::
  1449. failed_if() const {
  1450. return (_if_nesting != (IfNesting *)NULL &&
  1451. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1452. }
  1453. ////////////////////////////////////////////////////////////////////
  1454. // Function: PPCommandFile::is_valid_formal_parameter_name
  1455. // Access: Protected
  1456. // Description: Returns true if the indicated name is an acceptable
  1457. // name for a formal parameter. This means it includes
  1458. // no whitespace or crazy punctuation. Mainly this is
  1459. // to protect the user from making some stupid syntax
  1460. // mistake.
  1461. ////////////////////////////////////////////////////////////////////
  1462. bool PPCommandFile::
  1463. is_valid_formal(const string &formal_parameter_name) const {
  1464. if (formal_parameter_name.empty()) {
  1465. return false;
  1466. }
  1467. string::const_iterator si;
  1468. for (si = formal_parameter_name.begin();
  1469. si != formal_parameter_name.end();
  1470. ++si) {
  1471. switch (*si) {
  1472. case ' ':
  1473. case '\n':
  1474. case '\t':
  1475. case '$':
  1476. case '[':
  1477. case ']':
  1478. case ',':
  1479. return false;
  1480. }
  1481. }
  1482. return true;
  1483. }
  1484. ////////////////////////////////////////////////////////////////////
  1485. // Function: PPCommandFile::PushFilename::Constructor
  1486. // Access: Public
  1487. // Description: This special class changes the current filename of
  1488. // the PPCommandFile. The idea is to create one of
  1489. // these when the filename is changed (for instance, to
  1490. // read in a new file via an #include directive); when
  1491. // the variable then goes out of scope, it will restore
  1492. // the previous filename.
  1493. //
  1494. // This updates the scope with the appropriate
  1495. // variables.
  1496. ////////////////////////////////////////////////////////////////////
  1497. PPCommandFile::PushFilename::
  1498. PushFilename(PPScope *scope, const string &filename) {
  1499. _scope = scope;
  1500. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1501. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1502. string thisfilename = filename;
  1503. string thisdirprefix;
  1504. size_t slash = filename.rfind('/');
  1505. if (slash == string::npos) {
  1506. thisdirprefix = string();
  1507. } else {
  1508. thisdirprefix = filename.substr(0, slash + 1);
  1509. }
  1510. _scope->define_variable("THISFILENAME", thisfilename);
  1511. _scope->define_variable("THISDIRPREFIX", thisdirprefix);
  1512. }
  1513. ////////////////////////////////////////////////////////////////////
  1514. // Function: PPCommandFile::PushFilename::Destructor
  1515. // Access: Public
  1516. // Description:
  1517. ////////////////////////////////////////////////////////////////////
  1518. PPCommandFile::PushFilename::
  1519. ~PushFilename() {
  1520. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1521. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1522. }