ppCommandFile.cxx 69 KB

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