ppCommandFile.cxx 67 KB

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