ppCommandFile.cxx 71 KB

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