ppCommandFile.cxx 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  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. #ifdef HAVE_SSTREAM
  1045. string generated_file = nest->_output.str();
  1046. #else
  1047. nest->_output << ends;
  1048. char *c_str = nest->_output.str();
  1049. string generated_file = c_str;
  1050. delete[] c_str;
  1051. #endif // HAVE_SSTREAM
  1052. if (!compare_output(generated_file, nest->_params,
  1053. (nest->_flags & OF_notouch) != 0)) {
  1054. return false;
  1055. }
  1056. }
  1057. }
  1058. delete nest;
  1059. return true;
  1060. }
  1061. ////////////////////////////////////////////////////////////////////
  1062. // Function: PPCommandFile::handle_format_command
  1063. // Access: Protected
  1064. // Description: Handles the #format command: change the formatting
  1065. // mode of lines as they are output.
  1066. ////////////////////////////////////////////////////////////////////
  1067. bool PPCommandFile::
  1068. handle_format_command() {
  1069. _params = trim_blanks(_scope->expand_string(_params));
  1070. if (_params == "straight") {
  1071. _write_state->_format = WF_straight;
  1072. } else if (_params == "collapse") {
  1073. _write_state->_format = WF_collapse;
  1074. } else if (_params == "makefile") {
  1075. _write_state->_format = WF_makefile;
  1076. } else {
  1077. cerr << "Ignoring invalid write format: " << _params << "\n";
  1078. }
  1079. return true;
  1080. }
  1081. ////////////////////////////////////////////////////////////////////
  1082. // Function: PPCommandFile::handle_print_command
  1083. // Access: Protected
  1084. // Description: Handles the #print command: immediately output the
  1085. // arguments to this line to standard error.
  1086. ////////////////////////////////////////////////////////////////////
  1087. bool PPCommandFile::
  1088. handle_print_command() {
  1089. cerr << _scope->expand_string(_params) << "\n";
  1090. return true;
  1091. }
  1092. ////////////////////////////////////////////////////////////////////
  1093. // Function: PPCommandFile::handle_printvar_command
  1094. // Access: Protected
  1095. // Description: Writes the literal contents of the named variable(s)
  1096. // (the variables are named directly without enclosing
  1097. // $[ ... ] syntax) to cerr, for debugging.
  1098. ////////////////////////////////////////////////////////////////////
  1099. bool PPCommandFile::
  1100. handle_printvar_command() {
  1101. size_t p = 0;
  1102. while (p < _params.length()) {
  1103. // Pull off the next varname.
  1104. size_t q = _scope->scan_to_whitespace(_params, p);
  1105. string varname = trim_blanks(_scope->expand_string(_params.substr(p, q - p)));
  1106. cerr << varname << " = \"" << _scope->get_variable(varname)
  1107. << "\" ";
  1108. p = q;
  1109. while (p < _params.length() && isspace(_params[p])) {
  1110. p++;
  1111. }
  1112. }
  1113. cerr << "\n";
  1114. return true;
  1115. }
  1116. ////////////////////////////////////////////////////////////////////
  1117. // Function: PPCommandFile::handle_include_command
  1118. // Access: Protected
  1119. // Description: Handles the #include command: the indicated file is
  1120. // read and processed at this point.
  1121. ////////////////////////////////////////////////////////////////////
  1122. bool PPCommandFile::
  1123. handle_include_command() {
  1124. string filename = trim_blanks(_scope->expand_string(_params));
  1125. // We allow optional quotation marks around the filename.
  1126. if (filename.length() >= 2 &&
  1127. filename[0] == '"' &&
  1128. filename[filename.length() - 1] == '"') {
  1129. filename = filename.substr(1, filename.length() - 2);
  1130. }
  1131. return include_file(filename);
  1132. }
  1133. ////////////////////////////////////////////////////////////////////
  1134. // Function: PPCommandFile::handle_sinclude_command
  1135. // Access: Protected
  1136. // Description: Handles the #sinclude command: the indicated file is
  1137. // read and processed at this point. This is different
  1138. // from #include only in that if the file does not
  1139. // exist, there is no error; instead, nothing happens.
  1140. ////////////////////////////////////////////////////////////////////
  1141. bool PPCommandFile::
  1142. handle_sinclude_command() {
  1143. string filename = trim_blanks(_scope->expand_string(_params));
  1144. // We allow optional quotation marks around the filename.
  1145. if (filename.length() >= 2 &&
  1146. filename[0] == '"' &&
  1147. filename[filename.length() - 1] == '"') {
  1148. filename = filename.substr(1, filename.length() - 2);
  1149. }
  1150. Filename fn(filename);
  1151. if (!fn.exists()) {
  1152. // No such file; no error.
  1153. return true;
  1154. }
  1155. return include_file(filename);
  1156. }
  1157. ////////////////////////////////////////////////////////////////////
  1158. // Function: PPCommandFile::handle_call_command
  1159. // Access: Protected
  1160. // Description: Handles the #call command: the indicated named
  1161. // subroutine is read and processed at this point.
  1162. ////////////////////////////////////////////////////////////////////
  1163. bool PPCommandFile::
  1164. handle_call_command() {
  1165. // The first word is the name of the subroutine; the rest is the
  1166. // comma-separated list of expressions to substitute in for the
  1167. // subroutine's formal parameters.
  1168. // Pull off the first word and the rest of the params.
  1169. size_t p = 0;
  1170. while (p < _params.length() && !isspace(_params[p])) {
  1171. p++;
  1172. }
  1173. string subroutine_name = trim_blanks(_params.substr(0, p));
  1174. string params = _params.substr(p);
  1175. if (subroutine_name.empty()) {
  1176. cerr << "#call requires at least one parameter.\n";
  1177. return false;
  1178. }
  1179. const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
  1180. if (sub == (const PPSubroutine *)NULL) {
  1181. cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
  1182. }
  1183. PPScope *old_scope = _scope;
  1184. PPScope::push_scope(_scope);
  1185. PPScope nested_scope(_scope->get_named_scopes());
  1186. _scope = &nested_scope;
  1187. nested_scope.define_formals(subroutine_name, sub->_formals, params);
  1188. vector<string>::const_iterator li;
  1189. for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
  1190. if (!read_line(*li)) {
  1191. PPScope::pop_scope();
  1192. _scope = old_scope;
  1193. return false;
  1194. }
  1195. }
  1196. PPScope::pop_scope();
  1197. _scope = old_scope;
  1198. return true;
  1199. }
  1200. ////////////////////////////////////////////////////////////////////
  1201. // Function: PPCommandFile::handle_error_command
  1202. // Access: Protected
  1203. // Description: Handles the #error command: terminate immediately
  1204. // with the given error message.
  1205. ////////////////////////////////////////////////////////////////////
  1206. bool PPCommandFile::
  1207. handle_error_command() {
  1208. string message = trim_blanks(_scope->expand_string(_params));
  1209. if (!message.empty()) {
  1210. cerr << message << "\n";
  1211. }
  1212. return false;
  1213. }
  1214. ////////////////////////////////////////////////////////////////////
  1215. // Function: PPCommandFile::handle_mkdir_command
  1216. // Access: Protected
  1217. // Description: Handles the #mkdir command: create a directory or
  1218. // directories.
  1219. ////////////////////////////////////////////////////////////////////
  1220. bool PPCommandFile::
  1221. handle_mkdir_command() {
  1222. vector<string> words;
  1223. tokenize_whitespace(_scope->expand_string(_params), words);
  1224. vector<string>::const_iterator wi;
  1225. for (wi = words.begin(); wi != words.end(); ++wi) {
  1226. Filename dirname(*wi);
  1227. if (dirname.is_local()) {
  1228. string prefix = _scope->expand_variable("DIRPREFIX");
  1229. dirname = Filename(prefix, dirname);
  1230. }
  1231. Filename filename(dirname, "file");
  1232. if (!filename.make_dir()) {
  1233. if (!dirname.is_directory()) {
  1234. cerr << "Unable to create directory " << dirname << "\n";
  1235. }
  1236. }
  1237. }
  1238. return true;
  1239. }
  1240. ////////////////////////////////////////////////////////////////////
  1241. // Function: PPCommandFile::handle_defer_command
  1242. // Access: Protected
  1243. // Description: Handles the #defer command: define a new variable or
  1244. // change the definition of an existing variable. This
  1245. // is different from #define in that the variable
  1246. // definition is not immediately expanded; it will be
  1247. // expanded when the variable is later used. This
  1248. // allows the definition of variables that depend on
  1249. // other variables whose values have not yet been
  1250. // defined. This is akin to GNU make's = assignment.
  1251. ////////////////////////////////////////////////////////////////////
  1252. bool PPCommandFile::
  1253. handle_defer_command() {
  1254. // Pull off the first word and the rest of the params.
  1255. size_t p = _scope->scan_to_whitespace(_params);
  1256. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1257. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1258. cerr << "Warning: variable " << varname
  1259. << " shadowed by function definition.\n";
  1260. }
  1261. // Skip whitespace between the variable name and its definition.
  1262. while (p < _params.length() && isspace(_params[p])) {
  1263. p++;
  1264. }
  1265. string def = _params.substr(p);
  1266. // We don't expand the variable's definition immediately; it will be
  1267. // expanded when the variable is referenced later. However, we
  1268. // should expand any simple self-reference immediately, to allow for
  1269. // recursive definitions.
  1270. def = _scope->expand_self_reference(def, varname);
  1271. _scope->define_variable(varname, def);
  1272. return true;
  1273. }
  1274. ////////////////////////////////////////////////////////////////////
  1275. // Function: PPCommandFile::handle_define_command
  1276. // Access: Protected
  1277. // Description: Handles the #define command: define a new variable or
  1278. // change the definition of an existing variable. The
  1279. // variable definition is immediately expanded. This is
  1280. // akin to GNU make's := assignment.
  1281. ////////////////////////////////////////////////////////////////////
  1282. bool PPCommandFile::
  1283. handle_define_command() {
  1284. // Pull off the first word and the rest of the params.
  1285. size_t p = _scope->scan_to_whitespace(_params);
  1286. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1287. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1288. cerr << "Warning: variable " << varname
  1289. << " shadowed by function definition.\n";
  1290. }
  1291. // Skip whitespace between the variable name and its definition.
  1292. while (p < _params.length() && isspace(_params[p])) {
  1293. p++;
  1294. }
  1295. string def = _scope->expand_string(_params.substr(p));
  1296. _scope->define_variable(varname, def);
  1297. return true;
  1298. }
  1299. ////////////////////////////////////////////////////////////////////
  1300. // Function: PPCommandFile::handle_set_command
  1301. // Access: Protected
  1302. // Description: Handles the #set command: change the definition of an
  1303. // existing variable.
  1304. //
  1305. // This is different from #defer in two ways: (1) the
  1306. // variable in question must already have been #defined
  1307. // elsewhere, (2) if the variable was #defined in some
  1308. // parent scope, this will actually change the variable
  1309. // in the parent scope, rather than shadowing it in the
  1310. // local scope. Like #define and unlike #defer, the
  1311. // variable definition is expanded immediately, similar
  1312. // to GNU make's := operator.
  1313. ////////////////////////////////////////////////////////////////////
  1314. bool PPCommandFile::
  1315. handle_set_command() {
  1316. // Pull off the first word and the rest of the params.
  1317. size_t p = _scope->scan_to_whitespace(_params);
  1318. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1319. if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
  1320. cerr << "Warning: variable " << varname
  1321. << " shadowed by function definition.\n";
  1322. }
  1323. // Skip whitespace between the variable name and its definition.
  1324. while (p < _params.length() && isspace(_params[p])) {
  1325. p++;
  1326. }
  1327. string def = _scope->expand_string(_params.substr(p));
  1328. if (!_scope->set_variable(varname, def)) {
  1329. cerr << "Attempt to set undefined variable " << varname << "\n";
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. ////////////////////////////////////////////////////////////////////
  1335. // Function: PPCommandFile::handle_map_command
  1336. // Access: Protected
  1337. // Description: Handles the #map command: define a new map variable.
  1338. // This is a special kind of variable declaration that
  1339. // creates a variable that can be used as a function to
  1340. // look up variable expansions within a number of
  1341. // different named scopes, accessed by keyword.
  1342. ////////////////////////////////////////////////////////////////////
  1343. bool PPCommandFile::
  1344. handle_map_command() {
  1345. // Pull off the first word and the rest of the params.
  1346. size_t p = _scope->scan_to_whitespace(_params);
  1347. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1348. // Skip whitespace between the variable name and its definition.
  1349. while (p < _params.length() && isspace(_params[p])) {
  1350. p++;
  1351. }
  1352. string def = trim_blanks(_params.substr(p));
  1353. _scope->define_map_variable(varname, def);
  1354. return true;
  1355. }
  1356. ////////////////////////////////////////////////////////////////////
  1357. // Function: PPCommandFile::handle_addmap_command
  1358. // Access: Protected
  1359. // Description: Handles the #addmap command: add a new key/scope pair
  1360. // to an existing map variable.
  1361. ////////////////////////////////////////////////////////////////////
  1362. bool PPCommandFile::
  1363. handle_addmap_command() {
  1364. // Pull off the first word and the rest of the params.
  1365. size_t p = _scope->scan_to_whitespace(_params);
  1366. string varname = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1367. // Skip whitespace between the variable name and the key.
  1368. while (p < _params.length() && isspace(_params[p])) {
  1369. p++;
  1370. }
  1371. string key = trim_blanks(_scope->expand_string(_params.substr(p)));
  1372. _scope->add_to_map_variable(varname, key, _scope);
  1373. return true;
  1374. }
  1375. ////////////////////////////////////////////////////////////////////
  1376. // Function: PPCommandFile::handle_push_command
  1377. // Access: Protected
  1378. // Description: Handles the #push command: push a variable definition
  1379. // out to the enclosing scope. Useful for defining
  1380. // variables within a #forscopes block that you want to
  1381. // persist longer than the block itself.
  1382. //
  1383. // Syntax is:
  1384. //
  1385. // #push n varname [varname2 ... ]
  1386. //
  1387. // Where n is the number of levels out to push.
  1388. ////////////////////////////////////////////////////////////////////
  1389. bool PPCommandFile::
  1390. handle_push_command() {
  1391. // The first word is the number of levels.
  1392. size_t p = _scope->scan_to_whitespace(_params);
  1393. string levels_str = trim_blanks(_scope->expand_string(_params.substr(0, p)));
  1394. // Validate the number.
  1395. const char *param = levels_str.c_str();
  1396. char *n;
  1397. int levels = strtol(param, &n, 10);
  1398. if (n == param || levels < 0) {
  1399. // Invalid integer.
  1400. cerr << "#push with invalid level count: " << levels_str << "\n";
  1401. return false;
  1402. }
  1403. PPScope *enclosing_scope = _scope;
  1404. if (levels > 0) {
  1405. enclosing_scope = _scope->get_enclosing_scope(levels - 1);
  1406. }
  1407. // Skip whitespace to the first variable name.
  1408. while (p < _params.length() && isspace(_params[p])) {
  1409. p++;
  1410. }
  1411. while (p < _params.length()) {
  1412. // Pull off the next varname.
  1413. size_t q = _scope->scan_to_whitespace(_params, p);
  1414. string varname = trim_blanks(_scope->expand_string(_params.substr(p, q - p)));
  1415. string def = _scope->get_variable(varname);
  1416. enclosing_scope->define_variable(varname, def);
  1417. p = q;
  1418. while (p < _params.length() && isspace(_params[p])) {
  1419. p++;
  1420. }
  1421. }
  1422. return true;
  1423. }
  1424. ////////////////////////////////////////////////////////////////////
  1425. // Function: PPCommandFile::include_file
  1426. // Access: Protected
  1427. // Description: The internal implementation of #include: includes a
  1428. // particular named file at this point.
  1429. ////////////////////////////////////////////////////////////////////
  1430. bool PPCommandFile::
  1431. include_file(Filename filename) {
  1432. filename.set_text();
  1433. ifstream in;
  1434. if (!filename.open_read(in)) {
  1435. cerr << "Unable to open include file " << filename << ".\n";
  1436. return false;
  1437. }
  1438. PushFilename pushed(_scope, filename);
  1439. string line;
  1440. getline(in, line);
  1441. while (!in.fail() && !in.eof()) {
  1442. if (!read_line(line)) {
  1443. return false;
  1444. }
  1445. getline(in, line);
  1446. }
  1447. if (!in.eof()) {
  1448. cerr << "Error reading " << filename << ".\n";
  1449. return false;
  1450. }
  1451. return true;
  1452. }
  1453. ////////////////////////////////////////////////////////////////////
  1454. // Function: PPCommandFile::replay_while
  1455. // Access: Protected
  1456. // Description: Replays all the lines that were saved during a
  1457. // previous #while..#end block.
  1458. ////////////////////////////////////////////////////////////////////
  1459. bool PPCommandFile::
  1460. replay_while(const string &name) {
  1461. assert(!_in_for);
  1462. bool okflag = true;
  1463. vector<string> lines;
  1464. lines.swap(_saved_lines);
  1465. // Remove the #end command. This will fail if someone makes an #end
  1466. // command that spans multiple lines. Don't do that.
  1467. assert(!lines.empty());
  1468. lines.pop_back();
  1469. // Now replay all of the saved lines.
  1470. BlockNesting *saved_block = _block_nesting;
  1471. IfNesting *saved_if = _if_nesting;
  1472. while (!_scope->expand_string(name).empty()) {
  1473. vector<string>::const_iterator li;
  1474. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1475. okflag = read_line(*li);
  1476. }
  1477. }
  1478. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1479. cerr << "Misplaced #end or #endif.\n";
  1480. okflag = false;
  1481. }
  1482. return okflag;
  1483. }
  1484. ////////////////////////////////////////////////////////////////////
  1485. // Function: PPCommandFile::replay_for
  1486. // Access: Protected
  1487. // Description: Replays all the lines that were saved during a
  1488. // previous #for..#end block.
  1489. ////////////////////////////////////////////////////////////////////
  1490. bool PPCommandFile::
  1491. replay_for(const string &name, const vector<string> &words) {
  1492. assert(!_in_for);
  1493. bool okflag = true;
  1494. vector<string> lines;
  1495. lines.swap(_saved_lines);
  1496. // Remove the #end command. This will fail if someone makes an #end
  1497. // command that spans multiple lines. Don't do that.
  1498. assert(!lines.empty());
  1499. lines.pop_back();
  1500. // Expand the variable name.
  1501. string varname = _scope->expand_string(name);
  1502. // Get out the numeric range.
  1503. int range[3] = {0, 0, 1};
  1504. assert(words.size() <= 3);
  1505. for (int i = 0; i < (int)words.size(); i++) {
  1506. const char *param = words[i].c_str();
  1507. char *n;
  1508. range[i] = strtol(param, &n, 10);
  1509. if (n == param) {
  1510. cerr << "Invalid integer in #for: " << param << "\n";
  1511. return false;
  1512. }
  1513. }
  1514. if (range[2] == 0) {
  1515. cerr << "Step by zero in #for " << name << "\n";
  1516. return false;
  1517. }
  1518. // Now replay all of the saved lines.
  1519. BlockNesting *saved_block = _block_nesting;
  1520. IfNesting *saved_if = _if_nesting;
  1521. int index_var;
  1522. if (range[2] > 0) {
  1523. for (index_var = range[0]; index_var <= range[1]; index_var += range[2]) {
  1524. _scope->define_variable(varname, _scope->format_int(index_var));
  1525. vector<string>::const_iterator li;
  1526. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1527. okflag = read_line(*li);
  1528. }
  1529. }
  1530. } else {
  1531. for (index_var = range[0]; index_var >= range[1]; index_var += range[2]) {
  1532. _scope->define_variable(varname, _scope->format_int(index_var));
  1533. vector<string>::const_iterator li;
  1534. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1535. okflag = read_line(*li);
  1536. }
  1537. }
  1538. }
  1539. _scope->define_variable(varname, _scope->format_int(index_var));
  1540. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1541. cerr << "Misplaced #end or #endif.\n";
  1542. okflag = false;
  1543. }
  1544. return okflag;
  1545. }
  1546. ////////////////////////////////////////////////////////////////////
  1547. // Function: PPCommandFile::replay_forscopes
  1548. // Access: Protected
  1549. // Description: Replays all the lines that were saved during a
  1550. // previous #forscopes..#end block.
  1551. ////////////////////////////////////////////////////////////////////
  1552. bool PPCommandFile::
  1553. replay_forscopes(const string &name) {
  1554. assert(!_in_for);
  1555. bool okflag = true;
  1556. vector<string> lines;
  1557. lines.swap(_saved_lines);
  1558. // Remove the #end command. This will fail if someone makes an #end
  1559. // command that spans multiple lines. Don't do that.
  1560. assert(!lines.empty());
  1561. lines.pop_back();
  1562. PPNamedScopes *named_scopes = _scope->get_named_scopes();
  1563. // Extract out the scope names from the #forscopes .. #end name. This
  1564. // is a space-delimited list of scope names.
  1565. vector<string> words;
  1566. tokenize_whitespace(_scope->expand_string(name), words);
  1567. // Now build up the list of scopes with these names.
  1568. PPNamedScopes::Scopes scopes;
  1569. vector<string>::const_iterator wi;
  1570. for (wi = words.begin(); wi != words.end(); ++wi) {
  1571. named_scopes->get_scopes(*wi, scopes);
  1572. }
  1573. PPNamedScopes::sort_by_dependency(scopes);
  1574. // And finally, replay all of the saved lines.
  1575. BlockNesting *saved_block = _block_nesting;
  1576. IfNesting *saved_if = _if_nesting;
  1577. PPNamedScopes::Scopes::const_iterator si;
  1578. for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
  1579. PPScope::push_scope(_scope);
  1580. _scope = (*si);
  1581. vector<string>::const_iterator li;
  1582. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1583. okflag = read_line(*li);
  1584. }
  1585. _scope = PPScope::pop_scope();
  1586. }
  1587. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1588. cerr << "Misplaced #end or #endif.\n";
  1589. okflag = false;
  1590. }
  1591. return okflag;
  1592. }
  1593. ////////////////////////////////////////////////////////////////////
  1594. // Function: PPCommandFile::replay_foreach
  1595. // Access: Protected
  1596. // Description: Replays all the lines that were saved during a
  1597. // previous #foreach..#end block.
  1598. ////////////////////////////////////////////////////////////////////
  1599. bool PPCommandFile::
  1600. replay_foreach(const string &varname, const vector<string> &words) {
  1601. assert(!_in_for);
  1602. bool okflag = true;
  1603. vector<string> lines;
  1604. lines.swap(_saved_lines);
  1605. // Remove the #end command. This will fail if someone makes an #end
  1606. // command that spans multiple lines. Don't do that.
  1607. assert(!lines.empty());
  1608. lines.pop_back();
  1609. // Now traverse through the saved words.
  1610. BlockNesting *saved_block = _block_nesting;
  1611. IfNesting *saved_if = _if_nesting;
  1612. vector<string>::const_iterator wi;
  1613. for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
  1614. _scope->define_variable(varname, (*wi));
  1615. vector<string>::const_iterator li;
  1616. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1617. okflag = read_line(*li);
  1618. }
  1619. }
  1620. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1621. cerr << "Misplaced #end or #endif.\n";
  1622. okflag = false;
  1623. }
  1624. return okflag;
  1625. }
  1626. ////////////////////////////////////////////////////////////////////
  1627. // Function: PPCommandFile::replay_formap
  1628. // Access: Protected
  1629. // Description: Replays all the lines that were saved during a
  1630. // previous #formap..#end block.
  1631. ////////////////////////////////////////////////////////////////////
  1632. bool PPCommandFile::
  1633. replay_formap(const string &varname, const string &mapvar) {
  1634. assert(!_in_for);
  1635. bool okflag = true;
  1636. vector<string> lines;
  1637. lines.swap(_saved_lines);
  1638. // Remove the #end command. This will fail if someone makes an #end
  1639. // command that spans multiple lines. Don't do that.
  1640. assert(!lines.empty());
  1641. lines.pop_back();
  1642. // Now look up the map variable.
  1643. PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
  1644. if (&def == &PPScope::_null_map_def) {
  1645. cerr << "Undefined map variable: #formap " << varname << " "
  1646. << mapvar << "\n";
  1647. return false;
  1648. }
  1649. // Now traverse through the map definition.
  1650. BlockNesting *saved_block = _block_nesting;
  1651. IfNesting *saved_if = _if_nesting;
  1652. PPScope::MapVariableDefinition::const_iterator di;
  1653. for (di = def.begin(); di != def.end() && okflag; ++di) {
  1654. _scope->define_variable(varname, (*di).first);
  1655. PPScope::push_scope(_scope);
  1656. _scope = (*di).second;
  1657. vector<string>::const_iterator li;
  1658. for (li = lines.begin(); li != lines.end() && okflag; ++li) {
  1659. okflag = read_line(*li);
  1660. }
  1661. _scope = PPScope::pop_scope();
  1662. }
  1663. if (saved_block != _block_nesting || saved_if != _if_nesting) {
  1664. cerr << "Misplaced #end or #endif.\n";
  1665. okflag = false;
  1666. }
  1667. return okflag;
  1668. }
  1669. ////////////////////////////////////////////////////////////////////
  1670. // Function: PPCommandFile::compare_output
  1671. // Access: Protected
  1672. // Description: After a file has been written to a (potentially
  1673. // large) string via an #output command, compare the
  1674. // results to the original file. If they are different,
  1675. // remove the original file and replace it with the new
  1676. // contents; otherwise, leave the original alone.
  1677. ////////////////////////////////////////////////////////////////////
  1678. bool PPCommandFile::
  1679. compare_output(const string &new_contents, Filename filename,
  1680. bool notouch) {
  1681. filename.set_text();
  1682. bool exists = filename.exists();
  1683. bool differ = false;
  1684. if (exists) {
  1685. size_t len = new_contents.length();
  1686. size_t want_bytes = len + 1;
  1687. char *orig_contents = new char[want_bytes];
  1688. ifstream in;
  1689. if (!filename.open_read(in)) {
  1690. cerr << "Cannot read existing " << filename << ", regenerating.\n";
  1691. differ = true;
  1692. } else {
  1693. in.read(orig_contents, want_bytes);
  1694. if (in.gcount() != len) {
  1695. // The wrong number of bytes.
  1696. differ = true;
  1697. } else {
  1698. differ = !(new_contents == string(orig_contents, len));
  1699. }
  1700. }
  1701. }
  1702. if (differ || !exists) {
  1703. #ifndef WIN32_VC
  1704. if (verbose_dry_run) {
  1705. // Write our new contents to a file so we can run diff on both
  1706. // of them.
  1707. Filename temp_filename = filename.get_fullpath() + string(".ppd");
  1708. temp_filename.set_text();
  1709. ofstream out_b;
  1710. if (!temp_filename.open_write(out_b)) {
  1711. cerr << "Unable to open temporary file " << filename << " for writing.\n";
  1712. return false;
  1713. }
  1714. out_b.write(new_contents.data(), new_contents.length());
  1715. bool diff_ok = true;
  1716. if (!out_b) {
  1717. cerr << "Unable to write to temporary file " << filename << "\n";
  1718. diff_ok = true;
  1719. }
  1720. out_b.close();
  1721. string command = "diff -ub '" + filename.get_fullpath() + "' '" +
  1722. temp_filename.get_fullpath() + "'";
  1723. int sys_result = system(command.c_str());
  1724. if (sys_result < 0) {
  1725. cerr << "Unable to invoke diff\n";
  1726. diff_ok = false;
  1727. }
  1728. out_b.close();
  1729. temp_filename.unlink();
  1730. return diff_ok;
  1731. } else
  1732. #endif
  1733. if (dry_run) {
  1734. cerr << "Would generate " << filename << "\n";
  1735. } else {
  1736. cerr << "Generating " << filename << "\n";
  1737. if (exists) {
  1738. if (!filename.unlink()) {
  1739. cerr << "Unable to remove old " << filename << "\n";
  1740. return false;
  1741. }
  1742. }
  1743. ofstream out_b;
  1744. if (!filename.open_write(out_b)) {
  1745. cerr << "Unable to open file " << filename << " for writing.\n";
  1746. return false;
  1747. }
  1748. out_b.write(new_contents.data(), new_contents.length());
  1749. if (!out_b) {
  1750. cerr << "Unable to write to file " << filename << "\n";
  1751. return false;
  1752. }
  1753. out_b.close();
  1754. }
  1755. } else {
  1756. // Even though the file is unchanged, unless the "notouch" flag is
  1757. // set, we want to update the modification time. This helps the
  1758. // makefiles know we did something.
  1759. if (!notouch && !dry_run) {
  1760. if (!filename.touch()) {
  1761. cerr << "Warning: unable to update timestamp for " << filename << "\n";
  1762. }
  1763. }
  1764. }
  1765. return true;
  1766. }
  1767. ////////////////////////////////////////////////////////////////////
  1768. // Function: PPCommandFile::failed_if
  1769. // Access: Protected
  1770. // Description: Returns true if we are currently within a failed #if
  1771. // block (or in an #else block for a passed #if block),
  1772. // or false otherwise.
  1773. ////////////////////////////////////////////////////////////////////
  1774. bool PPCommandFile::
  1775. failed_if() const {
  1776. return (_if_nesting != (IfNesting *)NULL &&
  1777. (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
  1778. }
  1779. ////////////////////////////////////////////////////////////////////
  1780. // Function: PPCommandFile::is_valid_formal_parameter_name
  1781. // Access: Protected
  1782. // Description: Returns true if the indicated name is an acceptable
  1783. // name for a formal parameter. This means it includes
  1784. // no whitespace or crazy punctuation. Mainly this is
  1785. // to protect the user from making some stupid syntax
  1786. // mistake.
  1787. ////////////////////////////////////////////////////////////////////
  1788. bool PPCommandFile::
  1789. is_valid_formal(const string &formal_parameter_name) const {
  1790. if (formal_parameter_name.empty()) {
  1791. return false;
  1792. }
  1793. string::const_iterator si;
  1794. for (si = formal_parameter_name.begin();
  1795. si != formal_parameter_name.end();
  1796. ++si) {
  1797. switch (*si) {
  1798. case ' ':
  1799. case '\n':
  1800. case '\t':
  1801. case '$':
  1802. case '[':
  1803. case ']':
  1804. case ',':
  1805. return false;
  1806. }
  1807. }
  1808. return true;
  1809. }
  1810. ////////////////////////////////////////////////////////////////////
  1811. // Function: PPCommandFile::PushFilename::Constructor
  1812. // Access: Public
  1813. // Description: This special class changes the current filename of
  1814. // the PPCommandFile. The idea is to create one of
  1815. // these when the filename is changed (for instance, to
  1816. // read in a new file via an #include directive); when
  1817. // the variable then goes out of scope, it will restore
  1818. // the previous filename.
  1819. //
  1820. // This updates the scope with the appropriate
  1821. // variables.
  1822. ////////////////////////////////////////////////////////////////////
  1823. PPCommandFile::PushFilename::
  1824. PushFilename(PPScope *scope, const string &filename) {
  1825. _scope = scope;
  1826. _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
  1827. _old_thisfilename = _scope->get_variable("THISFILENAME");
  1828. string thisfilename = filename;
  1829. string thisdirprefix;
  1830. size_t slash = filename.rfind('/');
  1831. if (slash == string::npos) {
  1832. thisdirprefix = string();
  1833. } else {
  1834. thisdirprefix = filename.substr(0, slash + 1);
  1835. }
  1836. _scope->define_variable("THISFILENAME", thisfilename);
  1837. _scope->define_variable("THISDIRPREFIX", thisdirprefix);
  1838. }
  1839. ////////////////////////////////////////////////////////////////////
  1840. // Function: PPCommandFile::PushFilename::Destructor
  1841. // Access: Public
  1842. // Description:
  1843. ////////////////////////////////////////////////////////////////////
  1844. PPCommandFile::PushFilename::
  1845. ~PushFilename() {
  1846. _scope->define_variable("THISFILENAME", _old_thisfilename);
  1847. _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
  1848. }