ppCommandFile.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  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) {
  1276. cerr<<"#defer "<<varname<<" = "<<_params.substr(p)<<endl;
  1277. }
  1278. return true;
  1279. }
  1280. ////////////////////////////////////////////////////////////////////
  1281. // Function: PPCommandFile::handle_define_command
  1282. // Access: Protected
  1283. // Description: Handles the #define command: define a new variable or
  1284. // change the definition of an existing variable. The
  1285. // variable definition is immediately expanded. This is
  1286. // akin to GNU make's := assignment.
  1287. ////////////////////////////////////////////////////////////////////
  1288. bool PPCommandFile::
  1289. handle_define_command() {
  1290. // Pull off the first word and the rest of the params.
  1291. size_t p = _scope->scan_to_whitespace(_params);
  1292. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1293. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1294. cerr << "Warning: variable " << varname
  1295. << " shadowed by function definition.\n";
  1296. }
  1297. // Skip whitespace between the variable name and its definition.
  1298. while (p < _params.length() && isspace(_params[p])) {
  1299. p++;
  1300. }
  1301. string def = _scope->expand_string(_params.substr(p));
  1302. _scope->define_variable(varname, def);
  1303. if (verbose>=2) {
  1304. cerr<<"#define "<<varname<<" = "<<_params.substr(p)<<"\n \""<<def<<"\""<<endl;
  1305. }
  1306. return true;
  1307. }
  1308. ////////////////////////////////////////////////////////////////////
  1309. // Function: PPCommandFile::handle_set_command
  1310. // Access: Protected
  1311. // Description: Handles the #set command: change the definition of an
  1312. // existing variable.
  1313. //
  1314. // This is different from #defer in two ways: (1) the
  1315. // variable in question must already have been #defined
  1316. // elsewhere, (2) if the variable was #defined in some
  1317. // parent scope, this will actually change the variable
  1318. // in the parent scope, rather than shadowing it in the
  1319. // local scope. Like #define and unlike #defer, the
  1320. // variable definition is expanded immediately, similar
  1321. // to GNU make's := operator.
  1322. ////////////////////////////////////////////////////////////////////
  1323. bool PPCommandFile::
  1324. handle_set_command() {
  1325. // Pull off the first word and the rest of the params.
  1326. size_t p = _scope->scan_to_whitespace(_params);
  1327. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1328. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1329. cerr << "Warning: variable " << varname
  1330. << " shadowed by function definition.\n";
  1331. }
  1332. // Skip whitespace between the variable name and its definition.
  1333. while (p < _params.length() && isspace(_params[p])) {
  1334. p++;
  1335. }
  1336. string def = _scope->expand_string(_params.substr(p));
  1337. if (!_scope->set_variable(varname, def)) {
  1338. cerr << "Attempt to set undefined variable " << varname << "\n";
  1339. return false;
  1340. }
  1341. return true;
  1342. }
  1343. ////////////////////////////////////////////////////////////////////
  1344. // Function: PPCommandFile::handle_map_command
  1345. // Access: Protected
  1346. // Description: Handles the #map command: define a new map variable.
  1347. // This is a special kind of variable declaration that
  1348. // creates a variable that can be used as a function to
  1349. // look up variable expansions within a number of
  1350. // different named scopes, accessed by keyword.
  1351. ////////////////////////////////////////////////////////////////////
  1352. bool PPCommandFile::
  1353. handle_map_command() {
  1354. // Pull off the first word and the rest of the params.
  1355. size_t p = _scope->scan_to_whitespace(_params);
  1356. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1357. // Skip whitespace between the variable name and its definition.
  1358. while (p < _params.length() && isspace(_params[p])) {
  1359. p++;
  1360. }
  1361. string def = trim_blanks(_params.substr(p));
  1362. _scope->define_map_variable(varname, def);
  1363. return true;
  1364. }
  1365. ////////////////////////////////////////////////////////////////////
  1366. // Function: PPCommandFile::handle_addmap_command
  1367. // Access: Protected
  1368. // Description: Handles the #addmap command: add a new key/scope pair
  1369. // to an existing map variable.
  1370. ////////////////////////////////////////////////////////////////////
  1371. bool PPCommandFile::
  1372. handle_addmap_command() {
  1373. // Pull off the first word and the rest of the params.
  1374. size_t p = _scope->scan_to_whitespace(_params);
  1375. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1376. // Skip whitespace between the variable name and the key.
  1377. while (p < _params.length() && isspace(_params[p])) {
  1378. p++;
  1379. }
  1380. string key = trim_blanks(_scope->expand_string(_params.substr(p)));
  1381. _scope->add_to_map_variable(varname, key, _scope);
  1382. return true;
  1383. }
  1384. ////////////////////////////////////////////////////////////////////
  1385. // Function: PPCommandFile::handle_push_command
  1386. // Access: Protected
  1387. // Description: Handles the #push command: push a variable definition
  1388. // out to the enclosing scope. Useful for defining
  1389. // variables within a #forscopes block that you want to
  1390. // persist longer than the block itself.
  1391. //
  1392. // Syntax is:
  1393. //
  1394. // #push n varname [varname2 ... ]
  1395. //
  1396. // Where n is the number of levels out to push.
  1397. ////////////////////////////////////////////////////////////////////
  1398. bool PPCommandFile::
  1399. handle_push_command() {
  1400. // The first word is the number of levels.
  1401. size_t p = _scope->scan_to_whitespace(_params);
  1402. string levels_str = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1403. // Validate the number.
  1404. const char *param = levels_str.c_str();
  1405. char *n;
  1406. int levels = strtol(param, &n, 10);
  1407. if (n == param || levels < 0) {
  1408. // Invalid integer.
  1409. cerr << "#push with invalid level count: " << levels_str << "\n";
  1410. return false;
  1411. }
  1412. PPScope *enclosing_scope = _scope;
  1413. if (levels > 0) {
  1414. enclosing_scope = _scope->get_enclosing_scope(levels - 1);
  1415. }
  1416. // Skip whitespace to the first variable name.
  1417. while (p < _params.length() && isspace(_params[p])) {
  1418. p++;
  1419. }
  1420. while (p < _params.length()) {
  1421. // Pull off the next varname.
  1422. size_t q = _scope->scan_to_whitespace(_params, p);
  1423. string varname = trim_blanks(_scope->expand_string(_params.substr(p, q - p)));
  1424. string def = _scope->get_variable(varname);
  1425. enclosing_scope->define_variable(varname, def);
  1426. p = q;
  1427. while (p < _params.length() && isspace(_params[p])) {
  1428. p++;
  1429. }
  1430. }
  1431. return true;
  1432. }
  1433. ////////////////////////////////////////////////////////////////////
  1434. // Function: PPCommandFile::include_file
  1435. // Access: Protected
  1436. // Description: The internal implementation of #include: includes a
  1437. // particular named file at this point.
  1438. ////////////////////////////////////////////////////////////////////
  1439. bool PPCommandFile::
  1440. include_file(Filename filename) {
  1441. filename.set_text();
  1442. ifstream in;
  1443. if (!filename.open_read(in)) {
  1444. cerr << "Unable to open include file " << filename << ".\n";
  1445. return false;
  1446. }
  1447. if (verbose) {
  1448. cerr << "Reading (inc) \"" << filename << "\"\n";
  1449. }
  1450. PushFilename pushed(_scope, filename);
  1451. string line;
  1452. getline(in, line);
  1453. while (!in.fail() && !in.eof()) {
  1454. if (!read_line(line)) {
  1455. return false;
  1456. }
  1457. getline(in, line);
  1458. }
  1459. if (!in.eof()) {
  1460. cerr << "Error reading " << filename << ".\n";
  1461. return false;
  1462. }
  1463. return true;
  1464. }
  1465. ////////////////////////////////////////////////////////////////////
  1466. // Function: PPCommandFile::replay_while
  1467. // Access: Protected
  1468. // Description: Replays all the lines that were saved during a
  1469. // previous #while..#end block.
  1470. ////////////////////////////////////////////////////////////////////
  1471. bool PPCommandFile::
  1472. replay_while(const string &name) {
  1473. assert(!_in_for);
  1474. bool okflag = true;
  1475. vector<string> lines;
  1476. lines.swap(_saved_lines);
  1477. // Remove the #end command. This will fail if someone makes an #end
  1478. // command that spans multiple lines. Don't do that.
  1479. assert(!lines.empty());
  1480. lines.pop_back();
  1481. // Now replay all of the saved lines.
  1482. BlockNesting *saved_block = _block_nesting;
  1483. IfNesting *saved_if = _if_nesting;
  1484. while (!_scope->expand_string(name).empty()) {
  1485. vector<string>::const_iterator li;
  1486. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1487. okflag = read_line(*li);
  1488. }
  1489. }
  1490. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1491. cerr << "Misplaced #end or #endif.\n";
  1492. okflag = false;
  1493. }
  1494. return okflag;
  1495. }
  1496. ////////////////////////////////////////////////////////////////////
  1497. // Function: PPCommandFile::replay_for
  1498. // Access: Protected
  1499. // Description: Replays all the lines that were saved during a
  1500. // previous #for..#end block.
  1501. ////////////////////////////////////////////////////////////////////
  1502. bool PPCommandFile::
  1503. replay_for(const string &name, const vector<string> &words) {
  1504. assert(!_in_for);
  1505. bool okflag = true;
  1506. vector<string> lines;
  1507. lines.swap(_saved_lines);
  1508. // Remove the #end command. This will fail if someone makes an #end
  1509. // command that spans multiple lines. Don't do that.
  1510. assert(!lines.empty());
  1511. lines.pop_back();
  1512. // Expand the variable name.
  1513. string varname = _scope->expand_string(name);
  1514. // Get out the numeric range.
  1515. int range[3] = {0, 0, 1};
  1516. assert(words.size() <= 3);
  1517. for (int i = 0; i < (int)words.size(); i++) {
  1518. const char *param = words[i].c_str();
  1519. char *n;
  1520. range[i] = strtol(param, &n, 10);
  1521. if (n == param) {
  1522. cerr << "Invalid integer in #for: " << param << "\n";
  1523. return false;
  1524. }
  1525. }
  1526. if (range[2] == 0) {
  1527. cerr << "Step by zero in #for " << name << "\n";
  1528. return false;
  1529. }
  1530. // Now replay all of the saved lines.
  1531. BlockNesting *saved_block = _block_nesting;
  1532. IfNesting *saved_if = _if_nesting;
  1533. int index_var;
  1534. if (range[2] > 0) {
  1535. for (index_var = range[0]; index_var <= range[1]; index_var += range[2]) {
  1536. _scope->define_variable(varname, _scope->format_int(index_var));
  1537. vector<string>::const_iterator li;
  1538. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1539. okflag = read_line(*li);
  1540. }
  1541. }
  1542. } else {
  1543. for (index_var = range[0]; index_var >= range[1]; index_var += range[2]) {
  1544. _scope->define_variable(varname, _scope->format_int(index_var));
  1545. vector<string>::const_iterator li;
  1546. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1547. okflag = read_line(*li);
  1548. }
  1549. }
  1550. }
  1551. _scope->define_variable(varname, _scope->format_int(index_var));
  1552. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1553. cerr << "Misplaced #end or #endif.\n";
  1554. okflag = false;
  1555. }
  1556. return okflag;
  1557. }
  1558. ////////////////////////////////////////////////////////////////////
  1559. // Function: PPCommandFile::replay_forscopes
  1560. // Access: Protected
  1561. // Description: Replays all the lines that were saved during a
  1562. // previous #forscopes..#end block.
  1563. ////////////////////////////////////////////////////////////////////
  1564. bool PPCommandFile::
  1565. replay_forscopes(const string &name) {
  1566. assert(!_in_for);
  1567. bool okflag = true;
  1568. vector<string> lines;
  1569. lines.swap(_saved_lines);
  1570. // Remove the #end command. This will fail if someone makes an #end
  1571. // command that spans multiple lines. Don't do that.
  1572. assert(!lines.empty());
  1573. lines.pop_back();
  1574. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1575. // Extract out the scope names from the #forscopes .. #end name. This
  1576. // is a space-delimited list of scope names.
  1577. vector<string> words;
  1578. tokenize_whitespace(_scope->expand_string(name), words);
  1579. // Now build up the list of scopes with these names.
  1580. PPNamedScopes::Scopes scopes;
  1581. vector<string>::const_iterator wi;
  1582. for (wi = words.begin(); wi != words.end(); ++wi) {
  1583. named_scopes->get_scopes(*wi, scopes);
  1584. }
  1585. PPNamedScopes::sort_by_dependency(scopes);
  1586. // And finally, replay all of the saved lines.
  1587. BlockNesting *saved_block = _block_nesting;
  1588. IfNesting *saved_if = _if_nesting;
  1589. PPNamedScopes::Scopes::const_iterator si;
  1590. for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
  1591. PPScope::push_scope(_scope);
  1592. _scope = (*si);
  1593. vector<string>::const_iterator li;
  1594. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1595. okflag = read_line(*li);
  1596. }
  1597. _scope = PPScope::pop_scope();
  1598. }
  1599. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1600. cerr << "Misplaced #end or #endif.\n";
  1601. okflag = false;
  1602. }
  1603. return okflag;
  1604. }
  1605. ////////////////////////////////////////////////////////////////////
  1606. // Function: PPCommandFile::replay_foreach
  1607. // Access: Protected
  1608. // Description: Replays all the lines that were saved during a
  1609. // previous #foreach..#end block.
  1610. ////////////////////////////////////////////////////////////////////
  1611. bool PPCommandFile::
  1612. replay_foreach(const string &varname, const vector<string> &words) {
  1613. assert(!_in_for);
  1614. bool okflag = true;
  1615. vector<string> lines;
  1616. lines.swap(_saved_lines);
  1617. // Remove the #end command. This will fail if someone makes an #end
  1618. // command that spans multiple lines. Don't do that.
  1619. assert(!lines.empty());
  1620. lines.pop_back();
  1621. // Now traverse through the saved words.
  1622. BlockNesting *saved_block = _block_nesting;
  1623. IfNesting *saved_if = _if_nesting;
  1624. vector<string>::const_iterator wi;
  1625. for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
  1626. _scope->define_variable(varname, (*wi));
  1627. vector<string>::const_iterator li;
  1628. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1629. okflag = read_line(*li);
  1630. }
  1631. }
  1632. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1633. cerr << "Misplaced #end or #endif.\n";
  1634. okflag = false;
  1635. }
  1636. return okflag;
  1637. }
  1638. ////////////////////////////////////////////////////////////////////
  1639. // Function: PPCommandFile::replay_formap
  1640. // Access: Protected
  1641. // Description: Replays all the lines that were saved during a
  1642. // previous #formap..#end block.
  1643. ////////////////////////////////////////////////////////////////////
  1644. bool PPCommandFile::
  1645. replay_formap(const string &varname, const string &mapvar) {
  1646. assert(!_in_for);
  1647. bool okflag = true;
  1648. vector<string> lines;
  1649. lines.swap(_saved_lines);
  1650. // Remove the #end command. This will fail if someone makes an #end
  1651. // command that spans multiple lines. Don't do that.
  1652. assert(!lines.empty());
  1653. lines.pop_back();
  1654. // Now look up the map variable.
  1655. PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
  1656. if (&def == &PPScope::_null_map_def) {
  1657. cerr << "Undefined map variable: #formap " << varname << " "
  1658. << mapvar << "\n";
  1659. return false;
  1660. }
  1661. // Now traverse through the map definition.
  1662. BlockNesting *saved_block = _block_nesting;
  1663. IfNesting *saved_if = _if_nesting;
  1664. PPScope::MapVariableDefinition::const_iterator di;
  1665. for (di = def.begin(); di != def.end() && okflag; ++di) {
  1666. _scope->define_variable(varname, (*di).first);
  1667. PPScope::push_scope(_scope);
  1668. _scope = (*di).second;
  1669. vector<string>::const_iterator li;
  1670. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1671. okflag = read_line(*li);
  1672. }
  1673. _scope = PPScope::pop_scope();
  1674. }
  1675. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1676. cerr << "Misplaced #end or #endif.\n";
  1677. okflag = false;
  1678. }
  1679. return okflag;
  1680. }
  1681. ////////////////////////////////////////////////////////////////////
  1682. // Function: PPCommandFile::compare_output
  1683. // Access: Protected
  1684. // Description: After a file has been written to a (potentially
  1685. // large) string via an #output command, compare the
  1686. // results to the original file. If they are different,
  1687. // remove the original file and replace it with the new
  1688. // contents; otherwise, leave the original alone.
  1689. ////////////////////////////////////////////////////////////////////
  1690. bool PPCommandFile::
  1691. compare_output(const string &new_contents, Filename filename,
  1692. bool notouch) {
  1693. filename.set_text();
  1694. bool exists = filename.exists();
  1695. bool differ = false;
  1696. if (exists) {
  1697. size_t len = new_contents.length();
  1698. size_t want_bytes = len + 1;
  1699. char *orig_contents = new char[want_bytes];
  1700. ifstream in;
  1701. if (!filename.open_read(in)) {
  1702. cerr << "Cannot read existing " << filename << ", regenerating.\n";
  1703. differ = true;
  1704. } else {
  1705. if (verbose) {
  1706. cerr << "Reading (cmp) \"" << filename << "\"\n";
  1707. }
  1708. in.read(orig_contents, want_bytes);
  1709. if (in.gcount() != len) {
  1710. // The wrong number of bytes.
  1711. differ = true;
  1712. } else {
  1713. differ = !(new_contents == string(orig_contents, len));
  1714. }
  1715. }
  1716. }
  1717. if (differ || !exists) {
  1718. #ifndef WIN32_VC
  1719. if (verbose_dry_run) {
  1720. // Write our new contents to a file so we can run diff on both
  1721. // of them.
  1722. Filename temp_filename = filename.get_fullpath() + string(".ppd");
  1723. temp_filename.set_text();
  1724. ofstream out_b;
  1725. if (!temp_filename.open_write(out_b)) {
  1726. cerr << "Unable to open temporary file " << filename << " for writing.\n";
  1727. return false;
  1728. }
  1729. out_b.write(new_contents.data(), new_contents.length());
  1730. bool diff_ok = true;
  1731. if (!out_b) {
  1732. cerr << "Unable to write to temporary file " << filename << "\n";
  1733. diff_ok = true;
  1734. }
  1735. out_b.close();
  1736. string command = "diff -ub '" + filename.get_fullpath() + "' '" +
  1737. temp_filename.get_fullpath() + "'";
  1738. int sys_result = system(command.c_str());
  1739. if (sys_result < 0) {
  1740. cerr << "Unable to invoke diff\n";
  1741. diff_ok = false;
  1742. }
  1743. out_b.close();
  1744. temp_filename.unlink();
  1745. return diff_ok;
  1746. } else
  1747. #endif
  1748. if (dry_run) {
  1749. cerr << "Would generate " << filename << "\n";
  1750. } else {
  1751. cerr << "Generating " << filename << "\n";
  1752. if (exists) {
  1753. if (!filename.unlink()) {
  1754. cerr << "Unable to remove old " << filename << "\n";
  1755. return false;
  1756. }
  1757. }
  1758. ofstream out_b;
  1759. if (!filename.open_write(out_b)) {
  1760. cerr << "Unable to open file " << filename << " for writing.\n";
  1761. return false;
  1762. }
  1763. out_b.write(new_contents.data(), new_contents.length());
  1764. if (!out_b) {
  1765. cerr << "Unable to write to file " << filename << "\n";
  1766. return false;
  1767. }
  1768. out_b.close();
  1769. }
  1770. } else {
  1771. // Even though the file is unchanged, unless the "notouch" flag is
  1772. // set, we want to update the modification time. This helps the
  1773. // makefiles know we did something.
  1774. if (!notouch && !dry_run) {
  1775. if (!filename.touch()) {
  1776. cerr << "Warning: unable to update timestamp for " << filename << "\n";
  1777. }
  1778. }
  1779. }
  1780. return true;
  1781. }
  1782. ////////////////////////////////////////////////////////////////////
  1783. // Function: PPCommandFile::failed_if
  1784. // Access: Protected
  1785. // Description: Returns true if we are currently within a failed #if
  1786. // block (or in an #else block for a passed #if block),
  1787. // or false otherwise.
  1788. ////////////////////////////////////////////////////////////////////
  1789. bool PPCommandFile::
  1790. failed_if() const {
  1791. return (_if_nesting != (IfNesting *)NULL &&
  1792. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1793. }
  1794. ////////////////////////////////////////////////////////////////////
  1795. // Function: PPCommandFile::is_valid_formal_parameter_name
  1796. // Access: Protected
  1797. // Description: Returns true if the indicated name is an acceptable
  1798. // name for a formal parameter. This means it includes
  1799. // no whitespace or crazy punctuation. Mainly this is
  1800. // to protect the user from making some stupid syntax
  1801. // mistake.
  1802. ////////////////////////////////////////////////////////////////////
  1803. bool PPCommandFile::
  1804. is_valid_formal(const string &formal_parameter_name) const {
  1805. if (formal_parameter_name.empty()) {
  1806. return false;
  1807. }
  1808. string::const_iterator si;
  1809. for (si = formal_parameter_name.begin();
  1810. si != formal_parameter_name.end();
  1811. ++si) {
  1812. switch (*si) {
  1813. case ' ':
  1814. case '\n':
  1815. case '\t':
  1816. case '$':
  1817. case '[':
  1818. case ']':
  1819. case ',':
  1820. return false;
  1821. }
  1822. }
  1823. return true;
  1824. }
  1825. ////////////////////////////////////////////////////////////////////
  1826. // Function: PPCommandFile::PushFilename::Constructor
  1827. // Access: Public
  1828. // Description: This special class changes the current filename of
  1829. // the PPCommandFile. The idea is to create one of
  1830. // these when the filename is changed (for instance, to
  1831. // read in a new file via an #include directive); when
  1832. // the variable then goes out of scope, it will restore
  1833. // the previous filename.
  1834. //
  1835. // This updates the scope with the appropriate
  1836. // variables.
  1837. ////////////////////////////////////////////////////////////////////
  1838. PPCommandFile::PushFilename::
  1839. PushFilename(PPScope *scope, const string &filename) {
  1840. _scope = scope;
  1841. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1842. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1843. string thisfilename = filename;
  1844. string thisdirprefix;
  1845. size_t slash = filename.rfind('/');
  1846. if (slash == string::npos) {
  1847. thisdirprefix = string();
  1848. } else {
  1849. thisdirprefix = filename.substr(0, slash + 1);
  1850. }
  1851. _scope->define_variable("THISFILENAME", thisfilename);
  1852. _scope->define_variable("THISDIRPREFIX", thisdirprefix);
  1853. }
  1854. ////////////////////////////////////////////////////////////////////
  1855. // Function: PPCommandFile::PushFilename::Destructor
  1856. // Access: Public
  1857. // Description:
  1858. ////////////////////////////////////////////////////////////////////
  1859. PPCommandFile::PushFilename::
  1860. ~PushFilename() {
  1861. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1862. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1863. }