ppCommandFile.cxx 71 KB

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