ppCommandFile.cxx 68 KB

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