ppCommandFile.cxx 54 KB

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