ppCommandFile.cxx 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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. static const string begin_comment(BEGIN_COMMENT);
  14. ////////////////////////////////////////////////////////////////////
  15. // Function: PPCommandFile::WriteState::Constructor
  16. // Access: Public
  17. // Description:
  18. ////////////////////////////////////////////////////////////////////
  19. PPCommandFile::WriteState::
  20. WriteState() {
  21. _out = &cout;
  22. _format = WF_collapse;
  23. _last_blank = true;
  24. }
  25. ////////////////////////////////////////////////////////////////////
  26. // Function: PPCommandFile::WriteState::Copy Constructor
  27. // Access: Public
  28. // Description:
  29. ////////////////////////////////////////////////////////////////////
  30. PPCommandFile::WriteState::
  31. WriteState(const WriteState &copy) :
  32. _out(copy._out),
  33. _format(copy._format),
  34. _last_blank(copy._last_blank)
  35. {
  36. }
  37. ////////////////////////////////////////////////////////////////////
  38. // Function: PPCommandFile::WriteState::write_line
  39. // Access: Public
  40. // Description:
  41. ////////////////////////////////////////////////////////////////////
  42. bool PPCommandFile::WriteState::
  43. write_line(const string &line) {
  44. switch (_format) {
  45. case WF_straight:
  46. (*_out) << line << "\n";
  47. return true;
  48. case WF_collapse:
  49. return write_collapse_line(line);
  50. case WF_makefile:
  51. return write_makefile_line(line);
  52. }
  53. cerr << "Unsupported write format: " << (int)_format << "\n";
  54. return false;
  55. }
  56. ////////////////////////////////////////////////////////////////////
  57. // Function: PPCommandFile::WriteState::write_collapse_line
  58. // Access: Public
  59. // Description:
  60. ////////////////////////////////////////////////////////////////////
  61. bool PPCommandFile::WriteState::
  62. write_collapse_line(const string &line) {
  63. if (line.empty()) {
  64. if (!_last_blank) {
  65. (*_out) << "\n";
  66. _last_blank = true;
  67. }
  68. } else {
  69. _last_blank = false;
  70. (*_out) << line << "\n";
  71. }
  72. return true;
  73. }
  74. ////////////////////////////////////////////////////////////////////
  75. // Function: PPCommandFile::WriteState::write_makefile_line
  76. // Access: Public
  77. // Description:
  78. ////////////////////////////////////////////////////////////////////
  79. bool PPCommandFile::WriteState::
  80. write_makefile_line(const string &line) {
  81. if (line.length() <= 72) {
  82. return write_collapse_line(line);
  83. }
  84. _last_blank = false;
  85. // In makefile mode, long variable assignment lines are folded after
  86. // the assignment.
  87. vector<string> words;
  88. tokenize_whitespace(line, words);
  89. if (words.size() > 2 && (words[1] == "=" || words[1] == ":")) {
  90. // This appears to be a variable assignment or a dependency rule;
  91. // fold it.
  92. (*_out) << words[0] << " " << words[1];
  93. vector<string>::const_iterator wi;
  94. int col = 80;
  95. wi = words.begin() + 2;
  96. while (wi != words.end()) {
  97. col += (*wi).length() + 1;
  98. if (col > 72) {
  99. (*_out) << " \\\n ";
  100. col = 4 + (*wi).length();
  101. }
  102. (*_out) << " " << (*wi);
  103. ++wi;
  104. }
  105. (*_out) << "\n";
  106. } else {
  107. // This is not a variable assignment, so just write it out.
  108. (*_out) << line << "\n";
  109. }
  110. return true;
  111. }
  112. ////////////////////////////////////////////////////////////////////
  113. // Function: PPCommandFile::Constructor
  114. // Access: Public
  115. // Description:
  116. ////////////////////////////////////////////////////////////////////
  117. PPCommandFile::
  118. PPCommandFile(PPScope *scope) {
  119. _native_scope = scope;
  120. _scope = scope;
  121. _got_command = false;
  122. _in_for = false;
  123. _if_nesting = (IfNesting *)NULL;
  124. _block_nesting = (BlockNesting *)NULL;
  125. _write_state = new WriteState;
  126. }
  127. ////////////////////////////////////////////////////////////////////
  128. // Function: PPCommandFile::Destructor
  129. // Access: Public, Virtual
  130. // Description:
  131. ////////////////////////////////////////////////////////////////////
  132. PPCommandFile::
  133. ~PPCommandFile() {
  134. delete _write_state;
  135. }
  136. ////////////////////////////////////////////////////////////////////
  137. // Function: PPCommandFile::set_scope
  138. // Access: Public
  139. // Description: Changes the command file to use the indicated scope.
  140. // This scope will *not* be deleted when the command
  141. // file destructs.
  142. ////////////////////////////////////////////////////////////////////
  143. void PPCommandFile::
  144. set_scope(PPScope *scope) {
  145. _scope = scope;
  146. _native_scope = scope;
  147. }
  148. ////////////////////////////////////////////////////////////////////
  149. // Function: PPCommandFile::get_scope
  150. // Access: Public
  151. // Description: Returns the current scope associated with the command
  152. // file. This may change as the command file is
  153. // processed (e.g. between #begin .. #end sequences),
  154. // and it may or may not be tied to the life of the
  155. // PPCommandFile itself.
  156. ////////////////////////////////////////////////////////////////////
  157. PPScope *PPCommandFile::
  158. get_scope() const {
  159. return _scope;
  160. }
  161. ////////////////////////////////////////////////////////////////////
  162. // Function: PPCommandFile::read_file
  163. // Access: Public
  164. // Description: Reads input from the given filename.
  165. ////////////////////////////////////////////////////////////////////
  166. bool PPCommandFile::
  167. read_file(const string &filename) {
  168. ifstream in(filename.c_str());
  169. if (!in) {
  170. cerr << "Unable to open " << filename << ".\n";
  171. return false;
  172. }
  173. PushFilename pushed(_scope, filename);
  174. if (!read_stream(in)) {
  175. if (!in.eof()) {
  176. cerr << "Error reading " << filename << ".\n";
  177. }
  178. return false;
  179. }
  180. return true;
  181. }
  182. ////////////////////////////////////////////////////////////////////
  183. // Function: PPCommandFile::read_stream
  184. // Access: Public
  185. // Description: Reads input from the given stream. Each line is
  186. // read, commands are processed, variables are expanded,
  187. // and the resulting output is sent to write_line()
  188. // one line at a time. The return value is true if the
  189. // entire file is read with no errors, false if there is
  190. // some problem.
  191. ////////////////////////////////////////////////////////////////////
  192. bool PPCommandFile::
  193. read_stream(istream &in) {
  194. string line;
  195. getline(in, line);
  196. begin_read();
  197. while (!in.fail() && !in.eof()) {
  198. if (!read_line(line)) {
  199. return false;
  200. }
  201. getline(in, line);
  202. }
  203. if (!end_read()) {
  204. return false;
  205. }
  206. return true;
  207. }
  208. ////////////////////////////////////////////////////////////////////
  209. // Function: PPCommandFile::begin_read
  210. // Access: Public
  211. // Description: Resets to the beginning-of-the-stream state, in
  212. // preparation for a sequence of read_line() calls.
  213. ////////////////////////////////////////////////////////////////////
  214. void PPCommandFile::
  215. begin_read() {
  216. assert(_if_nesting == (IfNesting *)NULL);
  217. assert(_block_nesting == (BlockNesting *)NULL);
  218. }
  219. ////////////////////////////////////////////////////////////////////
  220. // Function: PPCommandFile::read_line
  221. // Access: Public
  222. // Description: Reads one line at a time, as if from the input
  223. // stream.
  224. ////////////////////////////////////////////////////////////////////
  225. bool PPCommandFile::
  226. read_line(string line) {
  227. // First things first: strip off any comment in the line.
  228. // We only recognize comments that are proceeded by whitespace, or
  229. // that start at the beginning of the line.
  230. size_t comment = line.find(begin_comment);
  231. while (comment != string::npos &&
  232. !(comment == 0 || isspace(line[comment - 1]))) {
  233. comment = line.find(begin_comment, comment + begin_comment.length());
  234. }
  235. if (comment != string::npos) {
  236. // Also strip any whitespace leading up to the comment.
  237. while (comment > 0 && isspace(line[comment - 1])) {
  238. comment--;
  239. }
  240. line = line.substr(0, comment);
  241. }
  242. // If the comment was at the beginning of the line, ignore the whole
  243. // line, including its whitespace.
  244. if (comment != 0) {
  245. if (_in_for) {
  246. // Save up the lines for later execution if we're within a #forscopes.
  247. _saved_lines.push_back(line);
  248. }
  249. if (_got_command) {
  250. return handle_command(line);
  251. } else {
  252. // Find the beginning of the line--skip initial whitespace.
  253. size_t p = 0;
  254. while (p < line.length() && isspace(line[p])) {
  255. p++;
  256. }
  257. if (p == line.length()) {
  258. // The line is empty. Make it truly empty.
  259. line = "";
  260. } else {
  261. if (p + 1 < line.length() && line[p] == COMMAND_PREFIX &&
  262. isalpha(line[p + 1])) {
  263. // This is a special command.
  264. return handle_command(line.substr(p + 1));
  265. }
  266. }
  267. if (!_in_for && !failed_if()) {
  268. return _write_state->write_line(_scope->expand_string(line));
  269. }
  270. }
  271. }
  272. return true;
  273. }
  274. ////////////////////////////////////////////////////////////////////
  275. // Function: PPCommandFile::end_read
  276. // Access: Public
  277. // Description: Finishes up the input stream, after a sequence of
  278. // read_line() calls.
  279. ////////////////////////////////////////////////////////////////////
  280. bool PPCommandFile::
  281. end_read() {
  282. bool okflag = true;
  283. if (_if_nesting != (IfNesting *)NULL) {
  284. cerr << "Unclosed if\n";
  285. _if_nesting = (IfNesting *)NULL;
  286. okflag = false;
  287. }
  288. if (_block_nesting != (BlockNesting *)NULL) {
  289. switch (_block_nesting->_state) {
  290. case BS_begin:
  291. cerr << "Unclosed begin " << _block_nesting->_name << "\n";
  292. break;
  293. case BS_forscopes:
  294. case BS_nested_forscopes:
  295. cerr << "Unclosed forscopes " << _block_nesting->_name << "\n";
  296. break;
  297. case BS_foreach:
  298. case BS_nested_foreach:
  299. cerr << "Unclosed foreach " << _block_nesting->_name << "\n";
  300. break;
  301. case BS_defsub:
  302. cerr << "Unclosed defsub " << _block_nesting->_name << "\n";
  303. break;
  304. case BS_output:
  305. cerr << "Unclosed output " << _block_nesting->_name << "\n";
  306. break;
  307. }
  308. _block_nesting = (BlockNesting *)NULL;
  309. okflag = false;
  310. }
  311. return okflag;
  312. }
  313. ////////////////////////////////////////////////////////////////////
  314. // Function: PPCommandFile::handle_command
  315. // Access: Protected
  316. // Description: Handles a macro command.
  317. ////////////////////////////////////////////////////////////////////
  318. bool PPCommandFile::
  319. handle_command(const string &line) {
  320. if (_got_command) {
  321. // If we were still processing a command from last time, keep
  322. // going; this line is just a continuation.
  323. _params += line;
  324. } else {
  325. // This is the first line of a new command.
  326. // Pull off the first word and the rest of the line.
  327. size_t p = 0;
  328. while (p < line.length() && !isspace(line[p])) {
  329. p++;
  330. }
  331. _command = line.substr(0, p);
  332. // Skip whitespace between the command and its arguments.
  333. while (p < line.length() && isspace(line[p])) {
  334. p++;
  335. }
  336. _params = line.substr(p);
  337. }
  338. if (_params[_params.length() - 1] == '\\') {
  339. // If the line ends with a backslash, there's more to come before
  340. // we can process the command.
  341. _got_command = true;
  342. _params[_params.length() - 1] = ' ';
  343. return true;
  344. }
  345. // We're completely done scanning the command now.
  346. _got_command = false;
  347. if (_command == "if") {
  348. return handle_if_command();
  349. } else if (_command == "elif") {
  350. return handle_elif_command();
  351. } else if (_command == "else") {
  352. return handle_else_command();
  353. } else if (_command == "endif") {
  354. return handle_endif_command();
  355. } else if (failed_if()) {
  356. // If we're in the middle of a failed #if, we ignore all commands
  357. // except for the if-related commands, above.
  358. return true;
  359. } else if (_command == "begin") {
  360. return handle_begin_command();
  361. } else if (_command == "forscopes") {
  362. return handle_forscopes_command();
  363. } else if (_command == "foreach") {
  364. return handle_foreach_command();
  365. } else if (_command == "format") {
  366. return handle_format_command();
  367. } else if (_command == "output") {
  368. return handle_output_command();
  369. } else if (_command == "defsub") {
  370. return handle_defsub_command();
  371. } else if (_command == "end") {
  372. return handle_end_command();
  373. } else if (_in_for) {
  374. // If we're saving up #forscopes commands, we ignore any following
  375. // commands for now.
  376. return true;
  377. } else if (_command == "include") {
  378. return handle_include_command();
  379. } else if (_command == "sinclude") {
  380. return handle_sinclude_command();
  381. } else if (_command == "call") {
  382. return handle_call_command();
  383. } else if (_command == "error") {
  384. return handle_error_command();
  385. } else if (_command == "defer") {
  386. return handle_defer_command();
  387. } else if (_command == "define") {
  388. return handle_define_command();
  389. } else if (_command == "set") {
  390. return handle_set_command();
  391. } else if (_command == "map") {
  392. return handle_map_command();
  393. }
  394. cerr << "Invalid command: " << COMMAND_PREFIX << _command << "\n";
  395. return false;
  396. }
  397. ////////////////////////////////////////////////////////////////////
  398. // Function: PPCommandFile::handle_if_command
  399. // Access: Protected
  400. // Description: Handles the #if command: conditionally evaluate the
  401. // following code.
  402. ////////////////////////////////////////////////////////////////////
  403. bool PPCommandFile::
  404. handle_if_command() {
  405. if (failed_if()) {
  406. // If we're *already* inside a failed if, we don't have to
  407. // evaluate this one, but we do need to record the nesting level.
  408. IfNesting *nest = new IfNesting;
  409. nest->_state = IS_done;
  410. nest->_next = _if_nesting;
  411. _if_nesting = nest;
  412. } else {
  413. // If the parameter string evaluates to empty, the case is false.
  414. // Otherwise the case is true. However, if we're currently
  415. // scanning #forscopes or something, we don't evaluate this at
  416. // all, because it doesn't matter.
  417. if (!_in_for) {
  418. _params = _scope->expand_string(_params);
  419. }
  420. bool is_empty = true;
  421. string::const_iterator si;
  422. for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
  423. is_empty = isspace(*si);
  424. }
  425. IfNesting *nest = new IfNesting;
  426. nest->_state = is_empty ? IS_off : IS_on;
  427. nest->_next = _if_nesting;
  428. _if_nesting = nest;
  429. }
  430. return true;
  431. }
  432. ////////////////////////////////////////////////////////////////////
  433. // Function: PPCommandFile::handle_elif_command
  434. // Access: Protected
  435. // Description: Handles the #elif command: conditionally evaluate
  436. // the following code, following a failed #if command.
  437. ////////////////////////////////////////////////////////////////////
  438. bool PPCommandFile::
  439. handle_elif_command() {
  440. if (_if_nesting == (IfNesting *)NULL) {
  441. cerr << "elif encountered without if.\n";
  442. return false;
  443. }
  444. if (_if_nesting->_state == IS_else) {
  445. cerr << "elif encountered after else.\n";
  446. return false;
  447. }
  448. if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
  449. // If we passed the #if above, we don't need to evaluate the #elif.
  450. _if_nesting->_state = IS_done;
  451. return true;
  452. }
  453. // If the parameter string evaluates to empty, the case is false.
  454. // Otherwise the case is true.
  455. if (!_in_for) {
  456. _params = _scope->expand_string(_params);
  457. }
  458. bool is_empty = true;
  459. string::const_iterator si;
  460. for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
  461. is_empty = isspace(*si);
  462. }
  463. _if_nesting->_state = is_empty ? IS_off : IS_on;
  464. return true;
  465. }
  466. ////////////////////////////////////////////////////////////////////
  467. // Function: PPCommandFile::handle_else_command
  468. // Access: Protected
  469. // Description: Handles the #else command: evaluate the following
  470. // code following a failed #if command.
  471. ////////////////////////////////////////////////////////////////////
  472. bool PPCommandFile::
  473. handle_else_command() {
  474. if (_if_nesting == (IfNesting *)NULL) {
  475. cerr << "else encountered without if.\n";
  476. return false;
  477. }
  478. if (_if_nesting->_state == IS_else) {
  479. cerr << "else encountered after else.\n";
  480. return false;
  481. }
  482. if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
  483. _if_nesting->_state = IS_done;
  484. return true;
  485. }
  486. _if_nesting->_state = IS_else;
  487. return true;
  488. }
  489. ////////////////////////////////////////////////////////////////////
  490. // Function: PPCommandFile::handle_endif_command
  491. // Access: Protected
  492. // Description: Handles the #endif command: close a preceeding #if
  493. // command.
  494. ////////////////////////////////////////////////////////////////////
  495. bool PPCommandFile::
  496. handle_endif_command() {
  497. if (_if_nesting == (IfNesting *)NULL) {
  498. cerr << "endif encountered without if.\n";
  499. return false;
  500. }
  501. IfNesting *nest = _if_nesting;
  502. _if_nesting = _if_nesting->_next;
  503. delete nest;
  504. return true;
  505. }
  506. ////////////////////////////////////////////////////////////////////
  507. // Function: PPCommandFile::handle_begin_command
  508. // Access: Protected
  509. // Description: Handles the #begin command: begin a named scope
  510. // block. The variables defined between this command
  511. // and the corresponding #end command will be local to
  512. // this named scope.
  513. ////////////////////////////////////////////////////////////////////
  514. bool PPCommandFile::
  515. handle_begin_command() {
  516. BlockNesting *nest = new BlockNesting;
  517. nest->_state = BS_begin;
  518. nest->_name = trim_blanks(_scope->expand_string(_params));
  519. nest->_write_state = _write_state;
  520. nest->_scope = _scope;
  521. nest->_next = _block_nesting;
  522. if (contains_whitespace(nest->_name)) {
  523. cerr << "Attempt to define scope named \"" << nest->_name
  524. << "\".\nScope names may not contain whitespace.\n";
  525. return false;
  526. }
  527. if (nest->_name.find(SCOPE_DIRNAME_SEPARATOR) != string::npos) {
  528. cerr << "Attempt to define scope named \"" << nest->_name
  529. << "\".\nScope names may not contain the '"
  530. << SCOPE_DIRNAME_SEPARATOR << "' character.\n";
  531. return false;
  532. }
  533. _block_nesting = nest;
  534. if (nest->_name == "global") {
  535. // There's a special case for the named scope "global": this
  536. // refers to the global scope, allowing us to define macros
  537. // etc. that all scopes can see.
  538. _scope = PPScope::get_bottom_scope();
  539. } else {
  540. PPScope *named_scope = _scope->get_named_scopes()->make_scope(nest->_name);
  541. named_scope->set_parent(_scope);
  542. _scope = named_scope;
  543. }
  544. return true;
  545. }
  546. ////////////////////////////////////////////////////////////////////
  547. // Function: PPCommandFile::handle_forscopes_command
  548. // Access: Protected
  549. // Description: Handles the #forscopes command: interpret all the lines
  550. // between this command and the corresponding #end
  551. // command once for each occurrence of a named scope
  552. // with the given name.
  553. ////////////////////////////////////////////////////////////////////
  554. bool PPCommandFile::
  555. handle_forscopes_command() {
  556. BlockNesting *nest = new BlockNesting;
  557. nest->_state = _in_for ? BS_nested_forscopes : BS_forscopes;
  558. nest->_name = trim_blanks(_scope->expand_string(_params));
  559. nest->_write_state = _write_state;
  560. nest->_scope = _scope;
  561. nest->_next = _block_nesting;
  562. _block_nesting = nest;
  563. if (!_in_for) {
  564. _in_for = true;
  565. _saved_lines.clear();
  566. }
  567. return true;
  568. }
  569. ////////////////////////////////////////////////////////////////////
  570. // Function: PPCommandFile::handle_foreach_command
  571. // Access: Protected
  572. // Description: Handles the #foreach command: interpret all the lines
  573. // between this command and the corresponding #end
  574. // command once for each word in the argument.
  575. ////////////////////////////////////////////////////////////////////
  576. bool PPCommandFile::
  577. handle_foreach_command() {
  578. // Get the parameters of the foreach command. The first word is the
  579. // name of the variable to substitute in (and which should appear on
  580. // the matching #end command), and the remaining words are the
  581. // values to substitute in.
  582. vector<string> words;
  583. tokenize_whitespace(_scope->expand_string(_params), words);
  584. if (words.empty()) {
  585. cerr << "#foreach requires at least one parameter.\n";
  586. return false;
  587. }
  588. string variable_name = words.front();
  589. BlockNesting *nest = new BlockNesting;
  590. nest->_state = _in_for ? BS_nested_foreach : BS_foreach;
  591. nest->_name = variable_name;
  592. nest->_write_state = _write_state;
  593. nest->_scope = _scope;
  594. nest->_next = _block_nesting;
  595. nest->_words.insert(nest->_words.end(), words.begin() + 1, words.end());
  596. _block_nesting = nest;
  597. if (!_in_for) {
  598. _in_for = true;
  599. _saved_lines.clear();
  600. }
  601. return true;
  602. }
  603. ////////////////////////////////////////////////////////////////////
  604. // Function: PPCommandFile::handle_format_command
  605. // Access: Protected
  606. // Description: Handles the #format command: change the formatting
  607. // mode of lines as they are output.
  608. ////////////////////////////////////////////////////////////////////
  609. bool PPCommandFile::
  610. handle_format_command() {
  611. _params = trim_blanks(_scope->expand_string(_params));
  612. if (_params == "straight") {
  613. _write_state->_format = WF_straight;
  614. } else if (_params == "collapse") {
  615. _write_state->_format = WF_collapse;
  616. } else if (_params == "makefile") {
  617. _write_state->_format = WF_makefile;
  618. } else {
  619. cerr << "Ignoring invalid write format: " << _params << "\n";
  620. }
  621. return true;
  622. }
  623. ////////////////////////////////////////////////////////////////////
  624. // Function: PPCommandFile::handle_output_command
  625. // Access: Protected
  626. // Description: Handles the #output command: all text between this
  627. // command and the corresponding #end command will be
  628. // sent to the indicated output file.
  629. ////////////////////////////////////////////////////////////////////
  630. bool PPCommandFile::
  631. handle_output_command() {
  632. BlockNesting *nest = new BlockNesting;
  633. nest->_state = BS_output;
  634. nest->_name = trim_blanks(_scope->expand_string(_params));
  635. nest->_write_state = _write_state;
  636. nest->_scope = _scope;
  637. nest->_next = _block_nesting;
  638. _block_nesting = nest;
  639. if (!_in_for) {
  640. string filename = nest->_name;
  641. if (filename.empty()) {
  642. cerr << "Attempt to output to empty filename\n";
  643. return false;
  644. }
  645. string prefix = _scope->expand_variable("DIRPREFIX");
  646. if (filename[0] != '/') {
  647. filename = prefix + filename;
  648. }
  649. nest->_true_name = filename;
  650. nest->_tempnam = (char *)NULL;
  651. if (access(filename.c_str(), F_OK) == 0) {
  652. // If the file already exists, create a temporary file first.
  653. nest->_tempnam = tempnam((prefix + ".").c_str(), "pptmp");
  654. assert(nest->_tempnam != (char *)NULL);
  655. nest->_output.open(nest->_tempnam);
  656. if (nest->_output.fail()) {
  657. cerr << "Unable to open output file " << nest->_tempnam << "\n";
  658. return false;
  659. }
  660. } else {
  661. // If the file does not already exist, create it directly instead
  662. // of monkeying around with temporary files.
  663. cerr << "Generating " << filename << "\n";
  664. nest->_output.open(filename.c_str(), ios::out, 0666);
  665. if (nest->_output.fail()) {
  666. cerr << "Unable to open output file " << filename << "\n";
  667. return false;
  668. }
  669. }
  670. _write_state = new WriteState(*_write_state);
  671. _write_state->_out = &nest->_output;
  672. }
  673. return true;
  674. }
  675. ////////////////////////////////////////////////////////////////////
  676. // Function: PPCommandFile::handle_defsub_command
  677. // Access: Protected
  678. // Description: Handles the #defsub command: save all the lines
  679. // between this command and the matching #end as a
  680. // callable subroutine to be invoked by a later #call
  681. // command.
  682. ////////////////////////////////////////////////////////////////////
  683. bool PPCommandFile::
  684. handle_defsub_command() {
  685. // The only parameter of #defsub is the name of the subroutine.
  686. string subroutine_name = trim_blanks(_scope->expand_string(_params));
  687. if (subroutine_name.empty()) {
  688. cerr << "#defsub requires at least one parameter.\n";
  689. return false;
  690. }
  691. if (_in_for) {
  692. cerr << "#defsub may not appear within a #forscopes or #foreach block,\n"
  693. << "or within another #defsub block.\n";
  694. return false;
  695. }
  696. BlockNesting *nest = new BlockNesting;
  697. nest->_state = BS_defsub;
  698. nest->_name = subroutine_name;
  699. nest->_write_state = _write_state;
  700. nest->_scope = _scope;
  701. nest->_next = _block_nesting;
  702. _block_nesting = nest;
  703. _in_for = true;
  704. _saved_lines.clear();
  705. return true;
  706. }
  707. ////////////////////////////////////////////////////////////////////
  708. // Function: PPCommandFile::handle_end_command
  709. // Access: Protected
  710. // Description: Handles the #end command. This closes a number of
  711. // different kinds of blocks, like #begin and #forscopes.
  712. ////////////////////////////////////////////////////////////////////
  713. bool PPCommandFile::
  714. handle_end_command() {
  715. if (_block_nesting == (BlockNesting *)NULL) {
  716. cerr << "Unmatched end " << _params << ".\n";
  717. return false;
  718. }
  719. string name = trim_blanks(_scope->expand_string(_params));
  720. if (name != _block_nesting->_name) {
  721. cerr << "end " << name << " encountered where end "
  722. << _block_nesting->_name << " expected.\n";
  723. return false;
  724. }
  725. BlockNesting *nest = _block_nesting;
  726. _scope = nest->_scope;
  727. if (_write_state != nest->_write_state) {
  728. delete _write_state;
  729. _write_state = nest->_write_state;
  730. }
  731. _block_nesting = nest->_next;
  732. if (nest->_state == BS_forscopes) {
  733. // Now replay all of the saved lines.
  734. _in_for = false;
  735. if (!replay_forscopes(nest->_name)) {
  736. return false;
  737. }
  738. } else if (nest->_state == BS_foreach) {
  739. // Now replay all of the saved lines.
  740. _in_for = false;
  741. if (!replay_foreach(nest->_name, nest->_words)) {
  742. return false;
  743. }
  744. } else if (nest->_state == BS_defsub) {
  745. // Save all of the saved lines as a named subroutine.
  746. _in_for = false;
  747. PPSubroutine *sub = new PPSubroutine;
  748. sub->_lines.swap(_saved_lines);
  749. // Remove the #end command. This will fail if someone makes an
  750. // #end command that spans multiple lines. Don't do that.
  751. assert(!sub->_lines.empty());
  752. sub->_lines.pop_back();
  753. PPSubroutine::define_sub(nest->_name, sub);
  754. } else if (nest->_state == BS_output) {
  755. if (!_in_for) {
  756. if (!nest->_output) {
  757. cerr << "Error while writing " << nest->_true_name << "\n";
  758. return false;
  759. }
  760. nest->_output.close();
  761. // Verify the output file.
  762. if (nest->_tempnam != (char *)NULL) {
  763. if (!compare_output(nest->_tempnam, nest->_true_name)) {
  764. return false;
  765. }
  766. free(nest->_tempnam);
  767. }
  768. }
  769. }
  770. delete nest;
  771. return true;
  772. }
  773. ////////////////////////////////////////////////////////////////////
  774. // Function: PPCommandFile::handle_include_command
  775. // Access: Protected
  776. // Description: Handles the #include command: the indicated file is
  777. // read and processed at this point.
  778. ////////////////////////////////////////////////////////////////////
  779. bool PPCommandFile::
  780. handle_include_command() {
  781. string filename = trim_blanks(_scope->expand_string(_params));
  782. // We allow optional quotation marks around the filename.
  783. if (filename.length() >= 2 &&
  784. filename[0] == '"' &&
  785. filename[filename.length() - 1] == '"') {
  786. filename = filename.substr(1, filename.length() - 2);
  787. }
  788. return include_file(filename);
  789. }
  790. ////////////////////////////////////////////////////////////////////
  791. // Function: PPCommandFile::handle_sinclude_command
  792. // Access: Protected
  793. // Description: Handles the #sinclude command: the indicated file is
  794. // read and processed at this point. This is different
  795. // from #include only in that if the file does not
  796. // exist, there is no error; instead, nothing happens.
  797. ////////////////////////////////////////////////////////////////////
  798. bool PPCommandFile::
  799. handle_sinclude_command() {
  800. string filename = trim_blanks(_scope->expand_string(_params));
  801. // We allow optional quotation marks around the filename.
  802. if (filename.length() >= 2 &&
  803. filename[0] == '"' &&
  804. filename[filename.length() - 1] == '"') {
  805. filename = filename.substr(1, filename.length() - 2);
  806. }
  807. if (access(filename.c_str(), F_OK) != 0) {
  808. // No such file; no error.
  809. return true;
  810. }
  811. return include_file(filename);
  812. }
  813. ////////////////////////////////////////////////////////////////////
  814. // Function: PPCommandFile::handle_call_command
  815. // Access: Protected
  816. // Description: Handles the #call command: the indicated named
  817. // subroutine is read and processed at this point.
  818. ////////////////////////////////////////////////////////////////////
  819. bool PPCommandFile::
  820. handle_call_command() {
  821. string subroutine_name = trim_blanks(_scope->expand_string(_params));
  822. if (subroutine_name.empty()) {
  823. cerr << "#call with no parameter.\n";
  824. }
  825. const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
  826. if (sub == (const PPSubroutine *)NULL) {
  827. cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
  828. }
  829. vector<string>::const_iterator li;
  830. for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
  831. if (!read_line(*li)) {
  832. return false;
  833. }
  834. }
  835. return true;
  836. }
  837. ////////////////////////////////////////////////////////////////////
  838. // Function: PPCommandFile::handle_error_command
  839. // Access: Protected
  840. // Description: Handles the #error command: terminate immediately
  841. // with the given error message.
  842. ////////////////////////////////////////////////////////////////////
  843. bool PPCommandFile::
  844. handle_error_command() {
  845. if (!_params.empty()) {
  846. cerr << _params << "\n";
  847. }
  848. return false;
  849. }
  850. ////////////////////////////////////////////////////////////////////
  851. // Function: PPCommandFile::handle_defer_command
  852. // Access: Protected
  853. // Description: Handles the #defer command: define a new variable or
  854. // change the definition of an existing variable. This
  855. // is different from #define in that the variable
  856. // definition is not immediately expanded; it will be
  857. // expanded when the variable is later used. This
  858. // allows the definition of variables that depend on
  859. // other variables whose values have not yet been
  860. // defined. This is akin to GNU make's = assignment.
  861. ////////////////////////////////////////////////////////////////////
  862. bool PPCommandFile::
  863. handle_defer_command() {
  864. // Pull off the first word and the rest of the params.
  865. size_t p = 0;
  866. while (p < _params.length() && !isspace(_params[p])) {
  867. p++;
  868. }
  869. string varname = _params.substr(0, p);
  870. // Skip whitespace between the variable name and its definition.
  871. while (p < _params.length() && isspace(_params[p])) {
  872. p++;
  873. }
  874. string def = _params.substr(p);
  875. // We don't expand the variable's definition immediately; it will be
  876. // expanded when the variable is referenced later. However, we
  877. // should expand any simple self-reference immediately, to allow for
  878. // recursive definitions.
  879. def = _scope->expand_self_reference(def, varname);
  880. _scope->define_variable(varname, def);
  881. return true;
  882. }
  883. ////////////////////////////////////////////////////////////////////
  884. // Function: PPCommandFile::handle_define_command
  885. // Access: Protected
  886. // Description: Handles the #define command: define a new variable or
  887. // change the definition of an existing variable. The
  888. // variable definition is immediately expanded. This is
  889. // akin to GNU make's := assignment.
  890. ////////////////////////////////////////////////////////////////////
  891. bool PPCommandFile::
  892. handle_define_command() {
  893. // Pull off the first word and the rest of the params.
  894. size_t p = 0;
  895. while (p < _params.length() && !isspace(_params[p])) {
  896. p++;
  897. }
  898. string varname = _params.substr(0, p);
  899. // Skip whitespace between the variable name and its definition.
  900. while (p < _params.length() && isspace(_params[p])) {
  901. p++;
  902. }
  903. string def = _scope->expand_string(_params.substr(p));
  904. _scope->define_variable(varname, def);
  905. return true;
  906. }
  907. ////////////////////////////////////////////////////////////////////
  908. // Function: PPCommandFile::handle_set_command
  909. // Access: Protected
  910. // Description: Handles the #set command: change the definition of an
  911. // existing variable.
  912. //
  913. // This is different from #defer in two ways: (1) the
  914. // variable in question must already have been #defined
  915. // elsewhere, (2) if the variable was #defined in some
  916. // parent scope, this will actually change the variable
  917. // in the parent scope, rather than shadowing it in the
  918. // local scope. Like #define and unlike #defer, the
  919. // variable definition is expanded immediately, similar
  920. // to GNU make's := operator.
  921. ////////////////////////////////////////////////////////////////////
  922. bool PPCommandFile::
  923. handle_set_command() {
  924. // Pull off the first word and the rest of the params.
  925. size_t p = 0;
  926. while (p < _params.length() && !isspace(_params[p])) {
  927. p++;
  928. }
  929. string varname = _params.substr(0, p);
  930. // Skip whitespace between the variable name and its definition.
  931. while (p < _params.length() && isspace(_params[p])) {
  932. p++;
  933. }
  934. string def = _scope->expand_string(_params.substr(p));
  935. if (!_scope->set_variable(varname, def)) {
  936. cerr << "Attempt to set undefined variable " << varname << "\n";
  937. return false;
  938. }
  939. return true;
  940. }
  941. ////////////////////////////////////////////////////////////////////
  942. // Function: PPCommandFile::handle_map_command
  943. // Access: Protected
  944. // Description: Handles the #map command: define a new map variable.
  945. // This is a special kind of variable declaration that
  946. // creates a variable that can be used as a function to
  947. // look up variable expansions within a number of
  948. // different named scopes, accessed by keyword.
  949. ////////////////////////////////////////////////////////////////////
  950. bool PPCommandFile::
  951. handle_map_command() {
  952. // Pull off the first word and the rest of the params.
  953. size_t p = 0;
  954. while (p < _params.length() && !isspace(_params[p])) {
  955. p++;
  956. }
  957. string varname = _params.substr(0, p);
  958. // Skip whitespace between the variable name and its definition.
  959. while (p < _params.length() && isspace(_params[p])) {
  960. p++;
  961. }
  962. string def = _params.substr(p);
  963. // We don't expand the variable's definition immediately; it will be
  964. // expanded when the variable is referenced later. However, we
  965. // should expand any simple self-reference immediately, to allow for
  966. // recursive definitions.
  967. def = _scope->expand_string(def);
  968. _scope->define_map_variable(varname, def);
  969. return true;
  970. }
  971. ////////////////////////////////////////////////////////////////////
  972. // Function: PPCommandFile::include_file
  973. // Access: Protected
  974. // Description: The internal implementation of #include: includes a
  975. // particular named file at this point.
  976. ////////////////////////////////////////////////////////////////////
  977. bool PPCommandFile::
  978. include_file(const string &filename) {
  979. ifstream in(filename.c_str());
  980. if (!in) {
  981. cerr << "Unable to open include file " << filename << ".\n";
  982. return false;
  983. }
  984. PushFilename pushed(_scope, filename);
  985. string line;
  986. getline(in, line);
  987. while (!in.fail() && !in.eof()) {
  988. if (!read_line(line)) {
  989. return false;
  990. }
  991. getline(in, line);
  992. }
  993. if (!in.eof()) {
  994. cerr << "Error reading " << filename << ".\n";
  995. return false;
  996. }
  997. return true;
  998. }
  999. ////////////////////////////////////////////////////////////////////
  1000. // Function: PPCommandFile::replay_forscopes
  1001. // Access: Protected
  1002. // Description: Replays all the lines that were saved during a
  1003. // previous #forscopes..#end block.
  1004. ////////////////////////////////////////////////////////////////////
  1005. bool PPCommandFile::
  1006. replay_forscopes(const string &name) {
  1007. bool okflag = true;
  1008. vector<string> lines;
  1009. lines.swap(_saved_lines);
  1010. // Remove the #end command. This will fail if someone makes an #end
  1011. // command that spans multiple lines. Don't do that.
  1012. assert(!lines.empty());
  1013. lines.pop_back();
  1014. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1015. // Extract out the scope names from the #forscopes .. #end name. This
  1016. // is a space-delimited list of scope names.
  1017. vector<string> words;
  1018. tokenize_whitespace(name, words);
  1019. // Now traverse the named scopes with these names.
  1020. vector<string>::const_iterator wi;
  1021. for (wi = words.begin(); wi != words.end(); ++wi) {
  1022. PPNamedScopes::Scopes scopes;
  1023. named_scopes->get_scopes(*wi, scopes);
  1024. PPNamedScopes::Scopes::const_iterator si;
  1025. for (si = scopes.begin(); si != scopes.end(); ++si) {
  1026. PPScope::push_scope(_scope);
  1027. _scope = (*si);
  1028. vector<string>::const_iterator li;
  1029. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1030. okflag = read_line(*li);
  1031. }
  1032. _scope = PPScope::pop_scope();
  1033. }
  1034. }
  1035. return okflag;
  1036. }
  1037. ////////////////////////////////////////////////////////////////////
  1038. // Function: PPCommandFile::replay_foreach
  1039. // Access: Protected
  1040. // Description: Replays all the lines that were saved during a
  1041. // previous #foreach..#end block.
  1042. ////////////////////////////////////////////////////////////////////
  1043. bool PPCommandFile::
  1044. replay_foreach(const string &varname, const vector<string> &words) {
  1045. bool okflag = true;
  1046. vector<string> lines;
  1047. lines.swap(_saved_lines);
  1048. // Remove the #end command. This will fail if someone makes an #end
  1049. // command that spans multiple lines. Don't do that.
  1050. assert(!lines.empty());
  1051. lines.pop_back();
  1052. // Now traverse through the saved words.
  1053. vector<string>::const_iterator wi;
  1054. for (wi = words.begin(); wi != words.end(); ++wi) {
  1055. _scope->define_variable(varname, (*wi));
  1056. vector<string>::const_iterator li;
  1057. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1058. okflag = read_line(*li);
  1059. }
  1060. }
  1061. return okflag;
  1062. }
  1063. ////////////////////////////////////////////////////////////////////
  1064. // Function: PPCommandFile::compare_output
  1065. // Access: Protected
  1066. // Description: After a temporary file has been written due to an
  1067. // #output command, compare the results to the original
  1068. // file. If they are different, remove the original
  1069. // file and rename the temporary file; if they are the
  1070. // same, remove the temporary file.
  1071. ////////////////////////////////////////////////////////////////////
  1072. bool PPCommandFile::
  1073. compare_output(const string &temp_name, const string &true_name) {
  1074. ifstream in_a(temp_name.c_str());
  1075. ifstream in_b(true_name.c_str());
  1076. int a = in_a.get();
  1077. int b = in_b.get();
  1078. bool differ = (a != b);
  1079. while (!in_a.eof() && !in_b.eof() && !differ) {
  1080. a = in_a.get();
  1081. b = in_b.get();
  1082. differ = (a != b);
  1083. }
  1084. in_a.close();
  1085. in_b.close();
  1086. if (differ) {
  1087. cerr << "Generating " << true_name << "\n";
  1088. if (unlink(true_name.c_str()) < 0) {
  1089. cerr << "Unable to remove old " << true_name << "\n";
  1090. return false;
  1091. }
  1092. if (rename(temp_name.c_str(), true_name.c_str()) < 0) {
  1093. cerr << "Unable to rename temporary file " << temp_name
  1094. << " to " << true_name << "\n";
  1095. return false;
  1096. }
  1097. } else {
  1098. // cerr << "File " << true_name << " is unchanged.\n";
  1099. if (unlink(temp_name.c_str()) < 0) {
  1100. cerr << "Warning: unable to remove temporary file " << temp_name << "\n";
  1101. }
  1102. }
  1103. return true;
  1104. }
  1105. ////////////////////////////////////////////////////////////////////
  1106. // Function: PPCommandFile::failed_if
  1107. // Access: Protected
  1108. // Description: Returns true if we are currently within a failed #if
  1109. // block (or in an #else block for a passed #if block),
  1110. // or false otherwise.
  1111. ////////////////////////////////////////////////////////////////////
  1112. bool PPCommandFile::
  1113. failed_if() const {
  1114. return (_if_nesting != (IfNesting *)NULL &&
  1115. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1116. }
  1117. ////////////////////////////////////////////////////////////////////
  1118. // Function: PPCommandFile::PushFilename::Constructor
  1119. // Access: Public
  1120. // Description: This special class changes the current filename of
  1121. // the PPCommandFile. The idea is to create one of
  1122. // these when the filename is changed (for instance, to
  1123. // read in a new file via an #include directive); when
  1124. // the variable then goes out of scope, it will restore
  1125. // the previous filename.
  1126. //
  1127. // This updates the scope with the appropriate
  1128. // variables.
  1129. ////////////////////////////////////////////////////////////////////
  1130. PPCommandFile::PushFilename::
  1131. PushFilename(PPScope *scope, const string &filename) {
  1132. _scope = scope;
  1133. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1134. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1135. _scope->define_variable("THISFILENAME", filename);
  1136. size_t slash = filename.rfind('/');
  1137. if (slash == string::npos) {
  1138. _scope->define_variable("THISDIRPREFIX", string());
  1139. } else {
  1140. _scope->define_variable("THISDIRPREFIX", filename.substr(0, slash + 1));
  1141. }
  1142. }
  1143. ////////////////////////////////////////////////////////////////////
  1144. // Function: PPCommandFile::PushFilename::Destructor
  1145. // Access: Public
  1146. // Description:
  1147. ////////////////////////////////////////////////////////////////////
  1148. PPCommandFile::PushFilename::
  1149. ~PushFilename() {
  1150. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1151. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1152. }