ppCommandFile.cxx 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172
  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. if (nest->_block != (BlockNesting *)NULL) {
  689. cerr << "If block not closed within scoping block " << nest->_block->_name << ".\n";
  690. } else {
  691. cerr << "If block not closed within scoping block " << _block_nesting->_name << ".\n";
  692. }
  693. return false;
  694. }
  695. delete nest;
  696. return true;
  697. }
  698. ////////////////////////////////////////////////////////////////////
  699. // Function: PPCommandFile::handle_begin_command
  700. // Access: Protected
  701. // Description: Handles the #begin command: begin a named scope
  702. // block. The variables defined between this command
  703. // and the corresponding #end command will be local to
  704. // this named scope.
  705. ////////////////////////////////////////////////////////////////////
  706. bool PPCommandFile::
  707. handle_begin_command() {
  708. string name = trim_blanks(_params);
  709. BlockNesting *nest = new BlockNesting(BS_begin, name);
  710. if (contains_whitespace(name)) {
  711. cerr << "Attempt to define scope named \"" << name
  712. << "\".\nScope names may not contain whitespace.\n";
  713. return false;
  714. }
  715. if (name.find(SCOPE_DIRNAME_SEPARATOR) != string::npos) {
  716. cerr << "Attempt to define scope named \"" << name
  717. << "\".\nScope names may not contain the '"
  718. << SCOPE_DIRNAME_SEPARATOR << "' character.\n";
  719. return false;
  720. }
  721. nest->push(this);
  722. PPScope *named_scope = _scope->get_named_scopes()->make_scope(name);
  723. named_scope->set_parent(_scope);
  724. _scope = named_scope;
  725. return true;
  726. }
  727. ////////////////////////////////////////////////////////////////////
  728. // Function: PPCommandFile::handle_while_command
  729. // Access: Protected
  730. // Description: Handles the #while command: repeat a block of
  731. // commands while a condition is true (nonempty).
  732. // Unlike many of the other block commands, this does
  733. // not begin a new scope.
  734. ////////////////////////////////////////////////////////////////////
  735. bool PPCommandFile::
  736. handle_while_command() {
  737. BlockState state = _in_for ? BS_nested_while : BS_while;
  738. string name = trim_blanks(_params);
  739. BlockNesting *nest = new BlockNesting(state, name);
  740. nest->push(this);
  741. if (!_in_for) {
  742. _in_for = true;
  743. _saved_lines.clear();
  744. }
  745. return true;
  746. }
  747. ////////////////////////////////////////////////////////////////////
  748. // Function: PPCommandFile::handle_for_command
  749. // Access: Protected
  750. // Description: Handles the #for command: repeat a block of
  751. // commands with a loop variable iterating over a range
  752. // of numeric values.
  753. ////////////////////////////////////////////////////////////////////
  754. bool PPCommandFile::
  755. handle_for_command() {
  756. // We get the name of the variable out first.
  757. size_t p = _scope->scan_to_whitespace(_params);
  758. string name = _params.substr(0, p);
  759. if (name.empty()) {
  760. cerr << "#for without varname\n";
  761. return false;
  762. }
  763. // The rest is the comma-delimited range of values.
  764. vector<string> words;
  765. _scope->tokenize_params(_params.substr(p), words, true);
  766. if (words.size() != 2 && words.size() != 3) {
  767. cerr << "Invalid numeric range: '" << _params.substr(p)
  768. << "' for #for " << name << "\n";
  769. return false;
  770. }
  771. BlockState state = _in_for ? BS_nested_for : BS_for;
  772. BlockNesting *nest = new BlockNesting(state, name);
  773. nest->push(this);
  774. if (!_in_for) {
  775. _in_for = true;
  776. _saved_lines.clear();
  777. nest->_words.swap(words);
  778. }
  779. return true;
  780. }
  781. ////////////////////////////////////////////////////////////////////
  782. // Function: PPCommandFile::handle_forscopes_command
  783. // Access: Protected
  784. // Description: Handles the #forscopes command: interpret all the lines
  785. // between this command and the corresponding #end
  786. // command once for each occurrence of a named scope
  787. // with the given name.
  788. ////////////////////////////////////////////////////////////////////
  789. bool PPCommandFile::
  790. handle_forscopes_command() {
  791. BlockState state = _in_for ? BS_nested_forscopes : BS_forscopes;
  792. string name = trim_blanks(_params);
  793. BlockNesting *nest = new BlockNesting(state, name);
  794. nest->push(this);
  795. if (!_in_for) {
  796. _in_for = true;
  797. _saved_lines.clear();
  798. }
  799. return true;
  800. }
  801. ////////////////////////////////////////////////////////////////////
  802. // Function: PPCommandFile::handle_foreach_command
  803. // Access: Protected
  804. // Description: Handles the #foreach command: interpret all the lines
  805. // between this command and the corresponding #end
  806. // command once for each word in the argument.
  807. ////////////////////////////////////////////////////////////////////
  808. bool PPCommandFile::
  809. handle_foreach_command() {
  810. // Get the parameters of the foreach command. The first word is the
  811. // name of the variable to substitute in (and which should appear on
  812. // the matching #end command), and the remaining words are the
  813. // values to substitute in.
  814. vector<string> words;
  815. tokenize_whitespace(_scope->expand_string(_params), words);
  816. if (words.empty()) {
  817. cerr << "#foreach requires at least one parameter.\n";
  818. return false;
  819. }
  820. string variable_name = words.front();
  821. BlockState state = _in_for ? BS_nested_foreach : BS_foreach;
  822. BlockNesting *nest = new BlockNesting(state, variable_name);
  823. nest->push(this);
  824. // We insert in all but the first word in the words vector.
  825. nest->_words.insert(nest->_words.end(), words.begin() + 1, words.end());
  826. if (!_in_for) {
  827. _in_for = true;
  828. _saved_lines.clear();
  829. }
  830. return true;
  831. }
  832. ////////////////////////////////////////////////////////////////////
  833. // Function: PPCommandFile::handle_formap_command
  834. // Access: Protected
  835. // Description: Handles the #formap command: interpret all the lines
  836. // between this command and the corresponding #end
  837. // command once for each key in the map, and also within
  838. // the corresponding scope of that particular key.
  839. ////////////////////////////////////////////////////////////////////
  840. bool PPCommandFile::
  841. handle_formap_command() {
  842. // Get the parameters of the formap command. The first word is the
  843. // name of the variable to substitute in (and which should appear on
  844. // the matching #end command), and the second word is the name of
  845. // the map variable.
  846. vector<string> words;
  847. tokenize_whitespace(_scope->expand_string(_params), words);
  848. if (words.size() != 2) {
  849. cerr << "#formap requires exactly two parameters.\n";
  850. return false;
  851. }
  852. string variable_name = words.front();
  853. BlockState state = _in_for ? BS_nested_formap : BS_formap;
  854. BlockNesting *nest = new BlockNesting(state, words[0]);
  855. nest->push(this);
  856. nest->_words.push_back(words[1]);
  857. if (!_in_for) {
  858. _in_for = true;
  859. _saved_lines.clear();
  860. }
  861. return true;
  862. }
  863. ////////////////////////////////////////////////////////////////////
  864. // Function: PPCommandFile::handle_defsub_command
  865. // Access: Protected
  866. // Description: Handles the #defsub (or #defun) command: save all the
  867. // lines between this command and the matching #end as a
  868. // callable subroutine to be invoked by a later #call
  869. // command. If is_defsub is false, it means this
  870. // subroutine was actually defined via a #defun command,
  871. // so it is to be invoked by a later variable reference,
  872. // instead of by a #call command.
  873. ////////////////////////////////////////////////////////////////////
  874. bool PPCommandFile::
  875. handle_defsub_command(bool is_defsub) {
  876. string command = (is_defsub) ? "#defsub" : "#defun";
  877. // The first word of the parameter list is the subroutine name; the
  878. // rest is the comma-separated list of formal parameter names.
  879. // Pull off the first word and the rest of the params.
  880. size_t p = 0;
  881. while (p < _params.length() && !isspace(_params[p])) {
  882. p++;
  883. }
  884. string subroutine_name = trim_blanks(_params.substr(0, p));
  885. if (subroutine_name.empty()) {
  886. cerr << command << " requires at least one parameter.\n";
  887. return false;
  888. }
  889. vector<string> formals;
  890. _scope->tokenize_params(_params.substr(p), formals, false);
  891. vector<string>::const_iterator fi;
  892. for (fi = formals.begin(); fi != formals.end(); ++fi) {
  893. if (!is_valid_formal(*fi)) {
  894. cerr << command << " " << subroutine_name
  895. << ": invalid formal parameter name '" << (*fi) << "'\n";
  896. return false;
  897. }
  898. }
  899. if (_in_for) {
  900. cerr << command << " may not appear within another block scoping command like\n"
  901. << "#forscopes, #foreach, #formap, #defsub, or #defun.\n";
  902. return false;
  903. }
  904. BlockState state = is_defsub ? BS_defsub : BS_defun;
  905. BlockNesting *nest = new BlockNesting(state, subroutine_name);
  906. nest->push(this);
  907. nest->_words.swap(formals);
  908. _in_for = true;
  909. _saved_lines.clear();
  910. return true;
  911. }
  912. ////////////////////////////////////////////////////////////////////
  913. // Function: PPCommandFile::handle_output_command
  914. // Access: Protected
  915. // Description: Handles the #output command: all text between this
  916. // command and the corresponding #end command will be
  917. // sent to the indicated output file.
  918. ////////////////////////////////////////////////////////////////////
  919. bool PPCommandFile::
  920. handle_output_command() {
  921. // We must get the filename out (the first parameter) without
  922. // expanding it, because it might involve a string that has to be
  923. // expanded later.
  924. size_t p = _scope->scan_to_whitespace(_params);
  925. string name = _params.substr(0, p);
  926. if (name.empty()) {
  927. cerr << "#output command requires one parameter.\n";
  928. return false;
  929. }
  930. // Now get the remaining parameters out; these we will expand immediately.
  931. vector<string> words;
  932. tokenize_whitespace(_scope->expand_string(_params.substr(p)), words);
  933. BlockNesting *nest = new BlockNesting(BS_output, name);
  934. // Also check the output flags.
  935. for (int i = 0; i < (int)words.size(); i++) {
  936. if (words[i] == "notouch") {
  937. nest->_flags |= OF_notouch;
  938. } else {
  939. cerr << "Invalid output flag: " << words[i] << "\n";
  940. }
  941. }
  942. nest->push(this);
  943. if (!_in_for) {
  944. Filename filename = trim_blanks(_scope->expand_string(nest->_name));
  945. if (filename.empty()) {
  946. cerr << "Attempt to output to empty filename\n";
  947. return false;
  948. }
  949. if (filename.is_local()) {
  950. string prefix = _scope->expand_variable("DIRPREFIX");
  951. filename = Filename(prefix, filename);
  952. }
  953. nest->_params = filename;
  954. // Generate an in-memory copy of the file first.
  955. _write_state = new WriteState(*_write_state);
  956. _write_state->_out = &nest->_output;
  957. }
  958. return true;
  959. }
  960. ////////////////////////////////////////////////////////////////////
  961. // Function: PPCommandFile::handle_end_command
  962. // Access: Protected
  963. // Description: Handles the #end command. This closes a number of
  964. // different kinds of blocks, like #begin and #forscopes.
  965. ////////////////////////////////////////////////////////////////////
  966. bool PPCommandFile::
  967. handle_end_command() {
  968. if (_block_nesting == (BlockNesting *)NULL) {
  969. cerr << "Unmatched end " << _params << ".\n";
  970. return false;
  971. }
  972. // We don't expand the closing name string, because we didn't expand
  973. // the opening string. (This is especially true for an #output
  974. // statement. On some of the other statements, we might have
  975. // inadvertently expanded this string, but probably that was a
  976. // mistake; and there's no reason for programmers to take advantage
  977. // of an expansion there.)
  978. string name = trim_blanks(_params);
  979. if (name != _block_nesting->_name) {
  980. cerr << "end " << name << " encountered where end "
  981. << _block_nesting->_name << " expected.\n";
  982. return false;
  983. }
  984. BlockNesting *nest = _block_nesting;
  985. nest->pop(this);
  986. if (nest->_if != _if_nesting) {
  987. cerr << "If block not closed within scoping block " << name << ".\n";
  988. return false;
  989. }
  990. if (nest->_state == BS_while) {
  991. // Now replay all of the saved lines.
  992. _in_for = false;
  993. if (!replay_while(nest->_name)) {
  994. return false;
  995. }
  996. } else if (nest->_state == BS_for) {
  997. // Now replay all of the saved lines.
  998. _in_for = false;
  999. if (!replay_for(nest->_name, nest->_words)) {
  1000. return false;
  1001. }
  1002. } else if (nest->_state == BS_forscopes) {
  1003. // Now replay all of the saved lines.
  1004. _in_for = false;
  1005. if (!replay_forscopes(nest->_name)) {
  1006. return false;
  1007. }
  1008. } else if (nest->_state == BS_foreach) {
  1009. // Now replay all of the saved lines.
  1010. _in_for = false;
  1011. if (!replay_foreach(nest->_name, nest->_words)) {
  1012. return false;
  1013. }
  1014. } else if (nest->_state == BS_formap) {
  1015. // Now replay all of the saved lines.
  1016. _in_for = false;
  1017. assert(nest->_words.size() == 1);
  1018. if (!replay_formap(nest->_name, nest->_words[0])) {
  1019. return false;
  1020. }
  1021. } else if (nest->_state == BS_defsub || nest->_state == BS_defun) {
  1022. // Save all of the saved lines as a named subroutine.
  1023. _in_for = false;
  1024. PPSubroutine *sub = new PPSubroutine;
  1025. sub->_formals.swap(nest->_words);
  1026. sub->_lines.swap(_saved_lines);
  1027. // Remove the #end command. This will fail if someone makes an
  1028. // #end command that spans multiple lines. Don't do that.
  1029. assert(!sub->_lines.empty());
  1030. sub->_lines.pop_back();
  1031. if (nest->_state == BS_defsub) {
  1032. PPSubroutine::define_sub(nest->_name, sub);
  1033. } else {
  1034. PPSubroutine::define_func(nest->_name, sub);
  1035. }
  1036. } else if (nest->_state == BS_output) {
  1037. if (!_in_for) {
  1038. if (!nest->_output) {
  1039. cerr << "Error while writing " << nest->_params << "\n";
  1040. return false;
  1041. }
  1042. // Now compare the file we generated to the file that's already
  1043. // there, if there is one.
  1044. nest->_output << ends;
  1045. const char *generated_file = nest->_output.str();
  1046. if (!compare_output(generated_file, nest->_params,
  1047. (nest->_flags & OF_notouch) != 0)) {
  1048. return false;
  1049. }
  1050. }
  1051. }
  1052. delete nest;
  1053. return true;
  1054. }
  1055. ////////////////////////////////////////////////////////////////////
  1056. // Function: PPCommandFile::handle_format_command
  1057. // Access: Protected
  1058. // Description: Handles the #format command: change the formatting
  1059. // mode of lines as they are output.
  1060. ////////////////////////////////////////////////////////////////////
  1061. bool PPCommandFile::
  1062. handle_format_command() {
  1063. _params = trim_blanks(_scope->expand_string(_params));
  1064. if (_params == "straight") {
  1065. _write_state->_format = WF_straight;
  1066. } else if (_params == "collapse") {
  1067. _write_state->_format = WF_collapse;
  1068. } else if (_params == "makefile") {
  1069. _write_state->_format = WF_makefile;
  1070. } else {
  1071. cerr << "Ignoring invalid write format: " << _params << "\n";
  1072. }
  1073. return true;
  1074. }
  1075. ////////////////////////////////////////////////////////////////////
  1076. // Function: PPCommandFile::handle_print_command
  1077. // Access: Protected
  1078. // Description: Handles the #print command: immediately output the
  1079. // arguments to this line to standard error.
  1080. ////////////////////////////////////////////////////////////////////
  1081. bool PPCommandFile::
  1082. handle_print_command() {
  1083. cerr << _scope->expand_string(_params) << "\n";
  1084. return true;
  1085. }
  1086. ////////////////////////////////////////////////////////////////////
  1087. // Function: PPCommandFile::handle_printvar_command
  1088. // Access: Protected
  1089. // Description: Writes the literal contents of the named variable(s)
  1090. // (the variables are named directly without enclosing
  1091. // $[ ... ] syntax) to cerr, for debugging.
  1092. ////////////////////////////////////////////////////////////////////
  1093. bool PPCommandFile::
  1094. handle_printvar_command() {
  1095. size_t p = 0;
  1096. while (p < _params.length()) {
  1097. // Pull off the next varname.
  1098. size_t q = _scope->scan_to_whitespace(_params, p);
  1099. string varname = trim_blanks(_scope->expand_string(_params.substr(p, q)));
  1100. cerr << varname << " = \"" << _scope->get_variable(varname)
  1101. << "\" ";
  1102. p = q;
  1103. while (p < _params.length() && isspace(_params[p])) {
  1104. p++;
  1105. }
  1106. }
  1107. cerr << "\n";
  1108. return true;
  1109. }
  1110. ////////////////////////////////////////////////////////////////////
  1111. // Function: PPCommandFile::handle_include_command
  1112. // Access: Protected
  1113. // Description: Handles the #include command: the indicated file is
  1114. // read and processed at this point.
  1115. ////////////////////////////////////////////////////////////////////
  1116. bool PPCommandFile::
  1117. handle_include_command() {
  1118. string filename = trim_blanks(_scope->expand_string(_params));
  1119. // We allow optional quotation marks around the filename.
  1120. if (filename.length() >= 2 &&
  1121. filename[0] == '"' &&
  1122. filename[filename.length() - 1] == '"') {
  1123. filename = filename.substr(1, filename.length() - 2);
  1124. }
  1125. return include_file(filename);
  1126. }
  1127. ////////////////////////////////////////////////////////////////////
  1128. // Function: PPCommandFile::handle_sinclude_command
  1129. // Access: Protected
  1130. // Description: Handles the #sinclude command: the indicated file is
  1131. // read and processed at this point. This is different
  1132. // from #include only in that if the file does not
  1133. // exist, there is no error; instead, nothing happens.
  1134. ////////////////////////////////////////////////////////////////////
  1135. bool PPCommandFile::
  1136. handle_sinclude_command() {
  1137. string filename = trim_blanks(_scope->expand_string(_params));
  1138. // We allow optional quotation marks around the filename.
  1139. if (filename.length() >= 2 &&
  1140. filename[0] == '"' &&
  1141. filename[filename.length() - 1] == '"') {
  1142. filename = filename.substr(1, filename.length() - 2);
  1143. }
  1144. Filename fn(filename);
  1145. if (!fn.exists()) {
  1146. // No such file; no error.
  1147. return true;
  1148. }
  1149. return include_file(filename);
  1150. }
  1151. ////////////////////////////////////////////////////////////////////
  1152. // Function: PPCommandFile::handle_call_command
  1153. // Access: Protected
  1154. // Description: Handles the #call command: the indicated named
  1155. // subroutine is read and processed at this point.
  1156. ////////////////////////////////////////////////////////////////////
  1157. bool PPCommandFile::
  1158. handle_call_command() {
  1159. // The first word is the name of the subroutine; the rest is the
  1160. // comma-separated list of expressions to substitute in for the
  1161. // subroutine's formal parameters.
  1162. // Pull off the first word and the rest of the params.
  1163. size_t p = 0;
  1164. while (p < _params.length() && !isspace(_params[p])) {
  1165. p++;
  1166. }
  1167. string subroutine_name = trim_blanks(_params.substr(0, p));
  1168. string params = _params.substr(p);
  1169. if (subroutine_name.empty()) {
  1170. cerr << "#call requires at least one parameter.\n";
  1171. return false;
  1172. }
  1173. const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
  1174. if (sub == (const PPSubroutine *)NULL) {
  1175. cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
  1176. }
  1177. PPScope *old_scope = _scope;
  1178. PPScope::push_scope(_scope);
  1179. PPScope nested_scope(_scope->get_named_scopes());
  1180. _scope = &nested_scope;
  1181. nested_scope.define_formals(subroutine_name, sub->_formals, params);
  1182. vector<string>::const_iterator li;
  1183. for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
  1184. if (!read_line(*li)) {
  1185. PPScope::pop_scope();
  1186. _scope = old_scope;
  1187. return false;
  1188. }
  1189. }
  1190. PPScope::pop_scope();
  1191. _scope = old_scope;
  1192. return true;
  1193. }
  1194. ////////////////////////////////////////////////////////////////////
  1195. // Function: PPCommandFile::handle_error_command
  1196. // Access: Protected
  1197. // Description: Handles the #error command: terminate immediately
  1198. // with the given error message.
  1199. ////////////////////////////////////////////////////////////////////
  1200. bool PPCommandFile::
  1201. handle_error_command() {
  1202. string message = trim_blanks(_scope->expand_string(_params));
  1203. if (!message.empty()) {
  1204. cerr << message << "\n";
  1205. }
  1206. return false;
  1207. }
  1208. ////////////////////////////////////////////////////////////////////
  1209. // Function: PPCommandFile::handle_mkdir_command
  1210. // Access: Protected
  1211. // Description: Handles the #mkdir command: create a directory or
  1212. // directories.
  1213. ////////////////////////////////////////////////////////////////////
  1214. bool PPCommandFile::
  1215. handle_mkdir_command() {
  1216. vector<string> words;
  1217. tokenize_whitespace(_scope->expand_string(_params), words);
  1218. vector<string>::const_iterator wi;
  1219. for (wi = words.begin(); wi != words.end(); ++wi) {
  1220. Filename dirname(*wi);
  1221. if (dirname.is_local()) {
  1222. string prefix = _scope->expand_variable("DIRPREFIX");
  1223. dirname = Filename(prefix, dirname);
  1224. }
  1225. Filename filename(dirname, "file");
  1226. if (!filename.make_dir()) {
  1227. if (!dirname.is_directory()) {
  1228. cerr << "Unable to create directory " << dirname << "\n";
  1229. }
  1230. }
  1231. }
  1232. return true;
  1233. }
  1234. ////////////////////////////////////////////////////////////////////
  1235. // Function: PPCommandFile::handle_defer_command
  1236. // Access: Protected
  1237. // Description: Handles the #defer command: define a new variable or
  1238. // change the definition of an existing variable. This
  1239. // is different from #define in that the variable
  1240. // definition is not immediately expanded; it will be
  1241. // expanded when the variable is later used. This
  1242. // allows the definition of variables that depend on
  1243. // other variables whose values have not yet been
  1244. // defined. This is akin to GNU make's = assignment.
  1245. ////////////////////////////////////////////////////////////////////
  1246. bool PPCommandFile::
  1247. handle_defer_command() {
  1248. // Pull off the first word and the rest of the params.
  1249. size_t p = _scope->scan_to_whitespace(_params);
  1250. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1251. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1252. cerr << "Warning: variable " << varname
  1253. << " shadowed by function definition.\n";
  1254. }
  1255. // Skip whitespace between the variable name and its definition.
  1256. while (p < _params.length() && isspace(_params[p])) {
  1257. p++;
  1258. }
  1259. string def = _params.substr(p);
  1260. // We don't expand the variable's definition immediately; it will be
  1261. // expanded when the variable is referenced later. However, we
  1262. // should expand any simple self-reference immediately, to allow for
  1263. // recursive definitions.
  1264. def = _scope->expand_self_reference(def, varname);
  1265. _scope->define_variable(varname, def);
  1266. return true;
  1267. }
  1268. ////////////////////////////////////////////////////////////////////
  1269. // Function: PPCommandFile::handle_define_command
  1270. // Access: Protected
  1271. // Description: Handles the #define command: define a new variable or
  1272. // change the definition of an existing variable. The
  1273. // variable definition is immediately expanded. This is
  1274. // akin to GNU make's := assignment.
  1275. ////////////////////////////////////////////////////////////////////
  1276. bool PPCommandFile::
  1277. handle_define_command() {
  1278. // Pull off the first word and the rest of the params.
  1279. size_t p = _scope->scan_to_whitespace(_params);
  1280. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1281. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1282. cerr << "Warning: variable " << varname
  1283. << " shadowed by function definition.\n";
  1284. }
  1285. // Skip whitespace between the variable name and its definition.
  1286. while (p < _params.length() && isspace(_params[p])) {
  1287. p++;
  1288. }
  1289. string def = _scope->expand_string(_params.substr(p));
  1290. _scope->define_variable(varname, def);
  1291. return true;
  1292. }
  1293. ////////////////////////////////////////////////////////////////////
  1294. // Function: PPCommandFile::handle_set_command
  1295. // Access: Protected
  1296. // Description: Handles the #set command: change the definition of an
  1297. // existing variable.
  1298. //
  1299. // This is different from #defer in two ways: (1) the
  1300. // variable in question must already have been #defined
  1301. // elsewhere, (2) if the variable was #defined in some
  1302. // parent scope, this will actually change the variable
  1303. // in the parent scope, rather than shadowing it in the
  1304. // local scope. Like #define and unlike #defer, the
  1305. // variable definition is expanded immediately, similar
  1306. // to GNU make's := operator.
  1307. ////////////////////////////////////////////////////////////////////
  1308. bool PPCommandFile::
  1309. handle_set_command() {
  1310. // Pull off the first word and the rest of the params.
  1311. size_t p = _scope->scan_to_whitespace(_params);
  1312. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1313. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1314. cerr << "Warning: variable " << varname
  1315. << " shadowed by function definition.\n";
  1316. }
  1317. // Skip whitespace between the variable name and its definition.
  1318. while (p < _params.length() && isspace(_params[p])) {
  1319. p++;
  1320. }
  1321. string def = _scope->expand_string(_params.substr(p));
  1322. if (!_scope->set_variable(varname, def)) {
  1323. cerr << "Attempt to set undefined variable " << varname << "\n";
  1324. return false;
  1325. }
  1326. return true;
  1327. }
  1328. ////////////////////////////////////////////////////////////////////
  1329. // Function: PPCommandFile::handle_map_command
  1330. // Access: Protected
  1331. // Description: Handles the #map command: define a new map variable.
  1332. // This is a special kind of variable declaration that
  1333. // creates a variable that can be used as a function to
  1334. // look up variable expansions within a number of
  1335. // different named scopes, accessed by keyword.
  1336. ////////////////////////////////////////////////////////////////////
  1337. bool PPCommandFile::
  1338. handle_map_command() {
  1339. // Pull off the first word and the rest of the params.
  1340. size_t p = _scope->scan_to_whitespace(_params);
  1341. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1342. // Skip whitespace between the variable name and its definition.
  1343. while (p < _params.length() && isspace(_params[p])) {
  1344. p++;
  1345. }
  1346. string def = trim_blanks(_params.substr(p));
  1347. _scope->define_map_variable(varname, def);
  1348. return true;
  1349. }
  1350. ////////////////////////////////////////////////////////////////////
  1351. // Function: PPCommandFile::handle_addmap_command
  1352. // Access: Protected
  1353. // Description: Handles the #addmap command: add a new key/scope pair
  1354. // to an existing map variable.
  1355. ////////////////////////////////////////////////////////////////////
  1356. bool PPCommandFile::
  1357. handle_addmap_command() {
  1358. // Pull off the first word and the rest of the params.
  1359. size_t p = _scope->scan_to_whitespace(_params);
  1360. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1361. // Skip whitespace between the variable name and the key.
  1362. while (p < _params.length() && isspace(_params[p])) {
  1363. p++;
  1364. }
  1365. string key = trim_blanks(_scope->expand_string(_params.substr(p)));
  1366. _scope->add_to_map_variable(varname, key, _scope);
  1367. return true;
  1368. }
  1369. ////////////////////////////////////////////////////////////////////
  1370. // Function: PPCommandFile::handle_push_command
  1371. // Access: Protected
  1372. // Description: Handles the #push command: push a variable definition
  1373. // out to the enclosing scope. Useful for defining
  1374. // variables within a #forscopes block that you want to
  1375. // persist longer than the block itself.
  1376. //
  1377. // Syntax is:
  1378. //
  1379. // #push n varname [varname2 ... ]
  1380. //
  1381. // Where n is the number of levels out to push.
  1382. ////////////////////////////////////////////////////////////////////
  1383. bool PPCommandFile::
  1384. handle_push_command() {
  1385. // The first word is the number of levels.
  1386. size_t p = _scope->scan_to_whitespace(_params);
  1387. string levels_str = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1388. // Validate the number.
  1389. const char *param = levels_str.c_str();
  1390. char *n;
  1391. int levels = strtol(param, &n, 10);
  1392. if (n == param || levels < 0) {
  1393. // Invalid integer.
  1394. cerr << "#push with invalid level count: " << levels_str << "\n";
  1395. return false;
  1396. }
  1397. PPScope *enclosing_scope = _scope;
  1398. if (levels > 0) {
  1399. enclosing_scope = _scope->get_enclosing_scope(levels - 1);
  1400. }
  1401. // Skip whitespace to the first variable name.
  1402. while (p < _params.length() && isspace(_params[p])) {
  1403. p++;
  1404. }
  1405. while (p < _params.length()) {
  1406. // Pull off the next varname.
  1407. size_t q = _scope->scan_to_whitespace(_params, p);
  1408. string varname = trim_blanks(_scope->expand_string(_params.substr(p, q - p)));
  1409. string def = _scope->get_variable(varname);
  1410. enclosing_scope->define_variable(varname, def);
  1411. p = q;
  1412. while (p < _params.length() && isspace(_params[p])) {
  1413. p++;
  1414. }
  1415. }
  1416. return true;
  1417. }
  1418. ////////////////////////////////////////////////////////////////////
  1419. // Function: PPCommandFile::include_file
  1420. // Access: Protected
  1421. // Description: The internal implementation of #include: includes a
  1422. // particular named file at this point.
  1423. ////////////////////////////////////////////////////////////////////
  1424. bool PPCommandFile::
  1425. include_file(Filename filename) {
  1426. filename.set_text();
  1427. ifstream in;
  1428. if (!filename.open_read(in)) {
  1429. cerr << "Unable to open include file " << filename << ".\n";
  1430. return false;
  1431. }
  1432. PushFilename pushed(_scope, filename);
  1433. string line;
  1434. getline(in, line);
  1435. while (!in.fail() && !in.eof()) {
  1436. if (!read_line(line)) {
  1437. return false;
  1438. }
  1439. getline(in, line);
  1440. }
  1441. if (!in.eof()) {
  1442. cerr << "Error reading " << filename << ".\n";
  1443. return false;
  1444. }
  1445. return true;
  1446. }
  1447. ////////////////////////////////////////////////////////////////////
  1448. // Function: PPCommandFile::replay_while
  1449. // Access: Protected
  1450. // Description: Replays all the lines that were saved during a
  1451. // previous #while..#end block.
  1452. ////////////////////////////////////////////////////////////////////
  1453. bool PPCommandFile::
  1454. replay_while(const string &name) {
  1455. assert(!_in_for);
  1456. bool okflag = true;
  1457. vector<string> lines;
  1458. lines.swap(_saved_lines);
  1459. // Remove the #end command. This will fail if someone makes an #end
  1460. // command that spans multiple lines. Don't do that.
  1461. assert(!lines.empty());
  1462. lines.pop_back();
  1463. // Now replay all of the saved lines.
  1464. BlockNesting *saved_block = _block_nesting;
  1465. IfNesting *saved_if = _if_nesting;
  1466. while (!_scope->expand_string(name).empty()) {
  1467. vector<string>::const_iterator li;
  1468. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1469. okflag = read_line(*li);
  1470. }
  1471. }
  1472. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1473. cerr << "Misplaced #end or #endif.\n";
  1474. okflag = false;
  1475. }
  1476. return okflag;
  1477. }
  1478. ////////////////////////////////////////////////////////////////////
  1479. // Function: PPCommandFile::replay_for
  1480. // Access: Protected
  1481. // Description: Replays all the lines that were saved during a
  1482. // previous #for..#end block.
  1483. ////////////////////////////////////////////////////////////////////
  1484. bool PPCommandFile::
  1485. replay_for(const string &name, const vector<string> &words) {
  1486. assert(!_in_for);
  1487. bool okflag = true;
  1488. vector<string> lines;
  1489. lines.swap(_saved_lines);
  1490. // Remove the #end command. This will fail if someone makes an #end
  1491. // command that spans multiple lines. Don't do that.
  1492. assert(!lines.empty());
  1493. lines.pop_back();
  1494. // Expand the variable name.
  1495. string varname = _scope->expand_string(name);
  1496. // Get out the numeric range.
  1497. int range[3] = {0, 0, 1};
  1498. assert(words.size() <= 3);
  1499. for (int i = 0; i < (int)words.size(); i++) {
  1500. const char *param = words[i].c_str();
  1501. char *n;
  1502. range[i] = strtol(param, &n, 10);
  1503. if (n == param) {
  1504. cerr << "Invalid integer in #for: " << param << "\n";
  1505. return false;
  1506. }
  1507. }
  1508. if (range[2] == 0) {
  1509. cerr << "Step by zero in #for " << name << "\n";
  1510. return false;
  1511. }
  1512. // Now replay all of the saved lines.
  1513. BlockNesting *saved_block = _block_nesting;
  1514. IfNesting *saved_if = _if_nesting;
  1515. int index_var;
  1516. if (range[2] > 0) {
  1517. for (index_var = range[0]; index_var <= range[1]; index_var += range[2]) {
  1518. _scope->define_variable(varname, _scope->format_int(index_var));
  1519. vector<string>::const_iterator li;
  1520. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1521. okflag = read_line(*li);
  1522. }
  1523. }
  1524. } else {
  1525. for (index_var = range[0]; index_var >= range[1]; index_var += range[2]) {
  1526. _scope->define_variable(varname, _scope->format_int(index_var));
  1527. vector<string>::const_iterator li;
  1528. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1529. okflag = read_line(*li);
  1530. }
  1531. }
  1532. }
  1533. _scope->define_variable(varname, _scope->format_int(index_var));
  1534. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1535. cerr << "Misplaced #end or #endif.\n";
  1536. okflag = false;
  1537. }
  1538. return okflag;
  1539. }
  1540. ////////////////////////////////////////////////////////////////////
  1541. // Function: PPCommandFile::replay_forscopes
  1542. // Access: Protected
  1543. // Description: Replays all the lines that were saved during a
  1544. // previous #forscopes..#end block.
  1545. ////////////////////////////////////////////////////////////////////
  1546. bool PPCommandFile::
  1547. replay_forscopes(const string &name) {
  1548. assert(!_in_for);
  1549. bool okflag = true;
  1550. vector<string> lines;
  1551. lines.swap(_saved_lines);
  1552. // Remove the #end command. This will fail if someone makes an #end
  1553. // command that spans multiple lines. Don't do that.
  1554. assert(!lines.empty());
  1555. lines.pop_back();
  1556. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1557. // Extract out the scope names from the #forscopes .. #end name. This
  1558. // is a space-delimited list of scope names.
  1559. vector<string> words;
  1560. tokenize_whitespace(name, words);
  1561. // Now build up the list of scopes with these names.
  1562. PPNamedScopes::Scopes scopes;
  1563. vector<string>::const_iterator wi;
  1564. for (wi = words.begin(); wi != words.end(); ++wi) {
  1565. named_scopes->get_scopes(*wi, scopes);
  1566. }
  1567. PPNamedScopes::sort_by_dependency(scopes);
  1568. // And finally, replay all of the saved lines.
  1569. BlockNesting *saved_block = _block_nesting;
  1570. IfNesting *saved_if = _if_nesting;
  1571. PPNamedScopes::Scopes::const_iterator si;
  1572. for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
  1573. PPScope::push_scope(_scope);
  1574. _scope = (*si);
  1575. vector<string>::const_iterator li;
  1576. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1577. okflag = read_line(*li);
  1578. }
  1579. _scope = PPScope::pop_scope();
  1580. }
  1581. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1582. cerr << "Misplaced #end or #endif.\n";
  1583. okflag = false;
  1584. }
  1585. return okflag;
  1586. }
  1587. ////////////////////////////////////////////////////////////////////
  1588. // Function: PPCommandFile::replay_foreach
  1589. // Access: Protected
  1590. // Description: Replays all the lines that were saved during a
  1591. // previous #foreach..#end block.
  1592. ////////////////////////////////////////////////////////////////////
  1593. bool PPCommandFile::
  1594. replay_foreach(const string &varname, const vector<string> &words) {
  1595. assert(!_in_for);
  1596. bool okflag = true;
  1597. vector<string> lines;
  1598. lines.swap(_saved_lines);
  1599. // Remove the #end command. This will fail if someone makes an #end
  1600. // command that spans multiple lines. Don't do that.
  1601. assert(!lines.empty());
  1602. lines.pop_back();
  1603. // Now traverse through the saved words.
  1604. BlockNesting *saved_block = _block_nesting;
  1605. IfNesting *saved_if = _if_nesting;
  1606. vector<string>::const_iterator wi;
  1607. for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
  1608. _scope->define_variable(varname, (*wi));
  1609. vector<string>::const_iterator li;
  1610. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1611. okflag = read_line(*li);
  1612. }
  1613. }
  1614. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1615. cerr << "Misplaced #end or #endif.\n";
  1616. okflag = false;
  1617. }
  1618. return okflag;
  1619. }
  1620. ////////////////////////////////////////////////////////////////////
  1621. // Function: PPCommandFile::replay_formap
  1622. // Access: Protected
  1623. // Description: Replays all the lines that were saved during a
  1624. // previous #formap..#end block.
  1625. ////////////////////////////////////////////////////////////////////
  1626. bool PPCommandFile::
  1627. replay_formap(const string &varname, const string &mapvar) {
  1628. assert(!_in_for);
  1629. bool okflag = true;
  1630. vector<string> lines;
  1631. lines.swap(_saved_lines);
  1632. // Remove the #end command. This will fail if someone makes an #end
  1633. // command that spans multiple lines. Don't do that.
  1634. assert(!lines.empty());
  1635. lines.pop_back();
  1636. // Now look up the map variable.
  1637. PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
  1638. if (&def == &PPScope::_null_map_def) {
  1639. cerr << "Undefined map variable: #formap " << varname << " "
  1640. << mapvar << "\n";
  1641. return false;
  1642. }
  1643. // Now traverse through the map definition.
  1644. BlockNesting *saved_block = _block_nesting;
  1645. IfNesting *saved_if = _if_nesting;
  1646. PPScope::MapVariableDefinition::const_iterator di;
  1647. for (di = def.begin(); di != def.end() && okflag; ++di) {
  1648. _scope->define_variable(varname, (*di).first);
  1649. PPScope::push_scope(_scope);
  1650. _scope = (*di).second;
  1651. vector<string>::const_iterator li;
  1652. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1653. okflag = read_line(*li);
  1654. }
  1655. _scope = PPScope::pop_scope();
  1656. }
  1657. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1658. cerr << "Misplaced #end or #endif.\n";
  1659. okflag = false;
  1660. }
  1661. return okflag;
  1662. }
  1663. ////////////////////////////////////////////////////////////////////
  1664. // Function: PPCommandFile::compare_output
  1665. // Access: Protected
  1666. // Description: After a file has been written to a (potentially
  1667. // large) string via an #output command, compare the
  1668. // results to the original file. If they are different,
  1669. // remove the original file and replace it with the new
  1670. // contents; otherwise, leave the original alone.
  1671. ////////////////////////////////////////////////////////////////////
  1672. bool PPCommandFile::
  1673. compare_output(const string &new_contents, Filename filename,
  1674. bool notouch) {
  1675. filename.set_text();
  1676. bool exists = filename.exists();
  1677. bool differ = false;
  1678. if (exists) {
  1679. size_t len = new_contents.length();
  1680. size_t want_bytes = len + 1;
  1681. char *orig_contents = new char[want_bytes];
  1682. ifstream in;
  1683. if (!filename.open_read(in)) {
  1684. cerr << "Cannot read existing " << filename << ", regenerating.\n";
  1685. differ = true;
  1686. } else {
  1687. in.read(orig_contents, want_bytes);
  1688. if (in.gcount() != len) {
  1689. // The wrong number of bytes.
  1690. differ = true;
  1691. } else {
  1692. differ = !(new_contents == string(orig_contents, len));
  1693. }
  1694. }
  1695. }
  1696. if (differ || !exists) {
  1697. #ifndef WIN32_VC
  1698. if (verbose_dry_run) {
  1699. // Write our new contents to a file so we can run diff on both
  1700. // of them.
  1701. Filename temp_filename = filename.get_fullpath() + string(".ppd");
  1702. temp_filename.set_text();
  1703. ofstream out_b;
  1704. if (!temp_filename.open_write(out_b)) {
  1705. cerr << "Unable to open temporary file " << filename << " for writing.\n";
  1706. return false;
  1707. }
  1708. out_b.write(new_contents.data(), new_contents.length());
  1709. bool diff_ok = true;
  1710. if (!out_b) {
  1711. cerr << "Unable to write to temporary file " << filename << "\n";
  1712. diff_ok = true;
  1713. }
  1714. out_b.close();
  1715. string command = "diff -ub '" + filename.get_fullpath() + "' '" +
  1716. temp_filename.get_fullpath() + "'";
  1717. int sys_result = system(command.c_str());
  1718. if (sys_result < 0) {
  1719. cerr << "Unable to invoke diff\n";
  1720. diff_ok = false;
  1721. }
  1722. out_b.close();
  1723. temp_filename.unlink();
  1724. return diff_ok;
  1725. } else
  1726. #endif
  1727. if (dry_run) {
  1728. cerr << "Would generate " << filename << "\n";
  1729. } else {
  1730. cerr << "Generating " << filename << "\n";
  1731. if (exists) {
  1732. if (!filename.unlink()) {
  1733. cerr << "Unable to remove old " << filename << "\n";
  1734. return false;
  1735. }
  1736. }
  1737. ofstream out_b;
  1738. if (!filename.open_write(out_b)) {
  1739. cerr << "Unable to open file " << filename << " for writing.\n";
  1740. return false;
  1741. }
  1742. out_b.write(new_contents.data(), new_contents.length());
  1743. if (!out_b) {
  1744. cerr << "Unable to write to file " << filename << "\n";
  1745. return false;
  1746. }
  1747. out_b.close();
  1748. }
  1749. } else {
  1750. // Even though the file is unchanged, unless the "notouch" flag is
  1751. // set, we want to update the modification time. This helps the
  1752. // makefiles know we did something.
  1753. if (!notouch && !dry_run) {
  1754. if (!filename.touch()) {
  1755. cerr << "Warning: unable to update timestamp for " << filename << "\n";
  1756. }
  1757. }
  1758. }
  1759. return true;
  1760. }
  1761. ////////////////////////////////////////////////////////////////////
  1762. // Function: PPCommandFile::failed_if
  1763. // Access: Protected
  1764. // Description: Returns true if we are currently within a failed #if
  1765. // block (or in an #else block for a passed #if block),
  1766. // or false otherwise.
  1767. ////////////////////////////////////////////////////////////////////
  1768. bool PPCommandFile::
  1769. failed_if() const {
  1770. return (_if_nesting != (IfNesting *)NULL &&
  1771. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1772. }
  1773. ////////////////////////////////////////////////////////////////////
  1774. // Function: PPCommandFile::is_valid_formal_parameter_name
  1775. // Access: Protected
  1776. // Description: Returns true if the indicated name is an acceptable
  1777. // name for a formal parameter. This means it includes
  1778. // no whitespace or crazy punctuation. Mainly this is
  1779. // to protect the user from making some stupid syntax
  1780. // mistake.
  1781. ////////////////////////////////////////////////////////////////////
  1782. bool PPCommandFile::
  1783. is_valid_formal(const string &formal_parameter_name) const {
  1784. if (formal_parameter_name.empty()) {
  1785. return false;
  1786. }
  1787. string::const_iterator si;
  1788. for (si = formal_parameter_name.begin();
  1789. si != formal_parameter_name.end();
  1790. ++si) {
  1791. switch (*si) {
  1792. case ' ':
  1793. case '\n':
  1794. case '\t':
  1795. case '$':
  1796. case '[':
  1797. case ']':
  1798. case ',':
  1799. return false;
  1800. }
  1801. }
  1802. return true;
  1803. }
  1804. ////////////////////////////////////////////////////////////////////
  1805. // Function: PPCommandFile::PushFilename::Constructor
  1806. // Access: Public
  1807. // Description: This special class changes the current filename of
  1808. // the PPCommandFile. The idea is to create one of
  1809. // these when the filename is changed (for instance, to
  1810. // read in a new file via an #include directive); when
  1811. // the variable then goes out of scope, it will restore
  1812. // the previous filename.
  1813. //
  1814. // This updates the scope with the appropriate
  1815. // variables.
  1816. ////////////////////////////////////////////////////////////////////
  1817. PPCommandFile::PushFilename::
  1818. PushFilename(PPScope *scope, const string &filename) {
  1819. _scope = scope;
  1820. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1821. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1822. string thisfilename = filename;
  1823. string thisdirprefix;
  1824. size_t slash = filename.rfind('/');
  1825. if (slash == string::npos) {
  1826. thisdirprefix = string();
  1827. } else {
  1828. thisdirprefix = filename.substr(0, slash + 1);
  1829. }
  1830. _scope->define_variable("THISFILENAME", thisfilename);
  1831. _scope->define_variable("THISDIRPREFIX", thisdirprefix);
  1832. }
  1833. ////////////////////////////////////////////////////////////////////
  1834. // Function: PPCommandFile::PushFilename::Destructor
  1835. // Access: Public
  1836. // Description:
  1837. ////////////////////////////////////////////////////////////////////
  1838. PPCommandFile::PushFilename::
  1839. ~PushFilename() {
  1840. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1841. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1842. }