| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767 |
- // Filename: ppCommandFile.cxx
- // Created by: drose (25Sep00)
- //
- ////////////////////////////////////////////////////////////////////
- #include "ppCommandFile.h"
- #include "ppScope.h"
- #include "ppNamedScopes.h"
- #include "ppSubroutine.h"
- #include "tokenize.h"
- #include <ctype.h>
- #include <stdio.h> // for tempnam()
- #include <unistd.h>
- #include <sys/types.h>
- #include <utime.h>
- #include <assert.h>
- static const string begin_comment(BEGIN_COMMENT);
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::IfNesting::Constructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::IfNesting::
- IfNesting(IfState state) :
- _state(state)
- {
- _block = (PPCommandFile::BlockNesting *)NULL;
- _next = (PPCommandFile::IfNesting *)NULL;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::IfNesting::push
- // Access: Public
- // Description: Adds this IfNesting object to the top of the
- // nesting stack.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::IfNesting::
- push(PPCommandFile *file) {
- _block = file->_block_nesting;
- _next = file->_if_nesting;
- file->_if_nesting = this;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::IfNesting::pop
- // Access: Public
- // Description: Removes this IfNesting object from the top of the
- // nesting stack, and restores the command file's
- // nesting state.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::IfNesting::
- pop(PPCommandFile *file) {
- assert(file->_if_nesting == this);
- file->_if_nesting = _next;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::WriteState::Constructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::WriteState::
- WriteState() {
- _out = &cout;
- _format = WF_collapse;
- _last_blank = true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::WriteState::Copy Constructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::WriteState::
- WriteState(const WriteState ©) :
- _out(copy._out),
- _format(copy._format),
- _last_blank(copy._last_blank)
- {
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::WriteState::write_line
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::WriteState::
- write_line(const string &line) {
- switch (_format) {
- case WF_straight:
- (*_out) << line << "\n";
- return true;
- case WF_collapse:
- return write_collapse_line(line);
- case WF_makefile:
- return write_makefile_line(line);
- }
- cerr << "Unsupported write format: " << (int)_format << "\n";
- return false;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::WriteState::write_collapse_line
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::WriteState::
- write_collapse_line(const string &line) {
- if (line.empty()) {
- if (!_last_blank) {
- (*_out) << "\n";
- _last_blank = true;
- }
-
- } else {
- _last_blank = false;
- (*_out) << line << "\n";
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::WriteState::write_makefile_line
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::WriteState::
- write_makefile_line(const string &line) {
- if (line.length() <= 72) {
- return write_collapse_line(line);
- }
- _last_blank = false;
- // In makefile mode, long variable assignment lines are folded after
- // the assignment.
- vector<string> words;
- tokenize_whitespace(line, words);
- if (words.size() > 2 && (words[1] == "=" || words[1] == ":")) {
- // This appears to be a variable assignment or a dependency rule;
- // fold it.
- (*_out) << words[0] << " " << words[1];
- vector<string>::const_iterator wi;
- int col = 80;
- wi = words.begin() + 2;
- while (wi != words.end()) {
- col += (*wi).length() + 1;
- if (col > 72) {
- (*_out) << " \\\n ";
- col = 4 + (*wi).length();
- }
- (*_out) << " " << (*wi);
- ++wi;
- }
- (*_out) << "\n";
- } else {
- // This is not a variable assignment, so just write it out.
- (*_out) << line << "\n";
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::BlockNesting::Constructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::BlockNesting::
- BlockNesting(BlockState state, const string &name) :
- _state(state),
- _name(name)
- {
- _if = (PPCommandFile::IfNesting *)NULL;
- _write_state = (PPCommandFile::WriteState *)NULL;
- _scope = (PPScope *)NULL;
- _tempnam = (char *)NULL;
- _flags = 0;
- _next = (PPCommandFile::BlockNesting *)NULL;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::BlockNesting::push
- // Access: Public
- // Description: Adds this BlockNesting object to the top of the
- // nesting stack.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::BlockNesting::
- push(PPCommandFile *file) {
- _if = file->_if_nesting;
- _write_state = file->_write_state;
- _scope = file->_scope;
- _next = file->_block_nesting;
- file->_block_nesting = this;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::BlockNesting::pop
- // Access: Public
- // Description: Removes this BlockNesting object from the top of the
- // nesting stack, and restores the command file's
- // nesting state.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::BlockNesting::
- pop(PPCommandFile *file) {
- assert(file->_block_nesting == this);
- if (file->_write_state != _write_state) {
- delete file->_write_state;
- file->_write_state = _write_state;
- }
- file->_scope = _scope;
- file->_block_nesting = _next;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::Constructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::
- PPCommandFile(PPScope *scope) {
- _native_scope = scope;
- _scope = scope;
- _got_command = false;
- _in_for = false;
- _if_nesting = (IfNesting *)NULL;
- _block_nesting = (BlockNesting *)NULL;
- _write_state = new WriteState;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::Destructor
- // Access: Public, Virtual
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::
- ~PPCommandFile() {
- delete _write_state;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::set_output
- // Access: Public
- // Description: Changes the main output stream that will be written
- // to when text appears outside of a #output .. #end
- // block. This is cout by default.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::
- set_output(ostream *out) {
- _write_state->_out = out;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::set_scope
- // Access: Public
- // Description: Changes the command file to use the indicated scope.
- // This scope will *not* be deleted when the command
- // file destructs.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::
- set_scope(PPScope *scope) {
- _scope = scope;
- _native_scope = scope;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::get_scope
- // Access: Public
- // Description: Returns the current scope associated with the command
- // file. This may change as the command file is
- // processed (e.g. between #begin .. #end sequences),
- // and it may or may not be tied to the life of the
- // PPCommandFile itself.
- ////////////////////////////////////////////////////////////////////
- PPScope *PPCommandFile::
- get_scope() const {
- return _scope;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::read_file
- // Access: Public
- // Description: Reads input from the given filename.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- read_file(const string &filename) {
- ifstream in(filename.c_str());
- if (!in) {
- cerr << "Unable to open " << filename << ".\n";
- return false;
- }
- PushFilename pushed(_scope, filename);
- if (!read_stream(in)) {
- if (!in.eof()) {
- cerr << "Error reading " << filename << ".\n";
- }
- return false;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::read_stream
- // Access: Public
- // Description: Reads input from the given stream. Each line is
- // read, commands are processed, variables are expanded,
- // and the resulting output is sent to write_line()
- // one line at a time. The return value is true if the
- // entire file is read with no errors, false if there is
- // some problem.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- read_stream(istream &in) {
- string line;
- getline(in, line);
- begin_read();
- while (!in.fail() && !in.eof()) {
- if (!read_line(line)) {
- return false;
- }
- getline(in, line);
- }
- if (!end_read()) {
- return false;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::begin_read
- // Access: Public
- // Description: Resets to the beginning-of-the-stream state, in
- // preparation for a sequence of read_line() calls.
- ////////////////////////////////////////////////////////////////////
- void PPCommandFile::
- begin_read() {
- assert(_if_nesting == (IfNesting *)NULL);
- assert(_block_nesting == (BlockNesting *)NULL);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::read_line
- // Access: Public
- // Description: Reads one line at a time, as if from the input
- // stream.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- read_line(string line) {
- // First things first: strip off any comment in the line.
- // We only recognize comments that are proceeded by whitespace, or
- // that start at the beginning of the line.
- size_t comment = line.find(begin_comment);
- while (comment != string::npos &&
- !(comment == 0 || isspace(line[comment - 1]))) {
- comment = line.find(begin_comment, comment + begin_comment.length());
- }
- if (comment != string::npos) {
- // Also strip any whitespace leading up to the comment.
- while (comment > 0 && isspace(line[comment - 1])) {
- comment--;
- }
- line = line.substr(0, comment);
- }
- // If the comment was at the beginning of the line, ignore the whole
- // line, including its whitespace.
- if (comment != 0) {
- // We also strip off whitespace at the end of the line, since this
- // is generally invisible and almost always just leads to trouble.
- size_t eol = line.length();
- while (eol > 0 && (isspace(line[eol - 1]) || line[eol - 1] == '\r')) {
- eol--;
- }
- line = line.substr(0, eol);
- if (_in_for) {
- // Save up the lines for later execution if we're within a #forscopes.
- _saved_lines.push_back(line);
- }
-
- if (_got_command) {
- return handle_command(line);
-
- } else {
- // Find the beginning of the line--skip initial whitespace.
- size_t p = 0;
- while (p < line.length() && isspace(line[p])) {
- p++;
- }
-
- if (p == line.length()) {
- // The line is empty. Make it truly empty.
- line = "";
-
- } else {
- if (p + 1 < line.length() && line[p] == COMMAND_PREFIX &&
- isalpha(line[p + 1])) {
- // This is a special command.
- return handle_command(line.substr(p + 1));
- }
- }
-
- if (!_in_for && !failed_if()) {
- return _write_state->write_line(_scope->expand_string(line));
- }
- }
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::end_read
- // Access: Public
- // Description: Finishes up the input stream, after a sequence of
- // read_line() calls.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- end_read() {
- bool okflag = true;
- if (_if_nesting != (IfNesting *)NULL) {
- cerr << "Unclosed if\n";
- _if_nesting = (IfNesting *)NULL;
- okflag = false;
- }
- if (_block_nesting != (BlockNesting *)NULL) {
- switch (_block_nesting->_state) {
- case BS_begin:
- cerr << "Unclosed begin " << _block_nesting->_name << "\n";
- break;
- case BS_forscopes:
- case BS_nested_forscopes:
- cerr << "Unclosed forscopes " << _block_nesting->_name << "\n";
- break;
- case BS_foreach:
- case BS_nested_foreach:
- cerr << "Unclosed foreach " << _block_nesting->_name << "\n";
- break;
- case BS_formap:
- case BS_nested_formap:
- cerr << "Unclosed formap " << _block_nesting->_name << "\n";
- break;
- case BS_defsub:
- cerr << "Unclosed defsub " << _block_nesting->_name << "\n";
- break;
- case BS_defun:
- cerr << "Unclosed defun " << _block_nesting->_name << "\n";
- break;
- case BS_output:
- cerr << "Unclosed output " << _block_nesting->_name << "\n";
- break;
- }
- _block_nesting = (BlockNesting *)NULL;
- okflag = false;
- }
- return okflag;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_command
- // Access: Protected
- // Description: Handles a macro command.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_command(const string &line) {
- if (_got_command) {
- // If we were still processing a command from last time, keep
- // going; this line is just a continuation. But skip any initial
- // whitespace.
- size_t p = 0;
- while (p < line.length() && isspace(line[p])) {
- p++;
- }
- _params += ' ';
- _params += line.substr(p);
- } else {
- // This is the first line of a new command.
- // Pull off the first word and the rest of the line.
- size_t p = 0;
- while (p < line.length() && !isspace(line[p])) {
- p++;
- }
- _command = line.substr(0, p);
-
- // Skip whitespace between the command and its arguments.
- while (p < line.length() && isspace(line[p])) {
- p++;
- }
- _params = line.substr(p);
- }
- if (!_params.empty() && _params[_params.length() - 1] == '\\') {
- // If the line ends with a backslash, there's more to come before
- // we can process the command.
- _got_command = true;
- // Truncate off the backslash, and any whitespace before it.
- size_t p = _params.length() - 1;
- while (p > 0 && isspace(_params[p - 1])) {
- p--;
- }
- _params = _params.substr(0, p);
- return true;
- }
- // We're completely done scanning the command now.
- _got_command = false;
- if (_command == "if") {
- return handle_if_command();
- } else if (_command == "elif") {
- return handle_elif_command();
-
- } else if (_command == "else") {
- return handle_else_command();
- } else if (_command == "endif") {
- return handle_endif_command();
- } else if (failed_if()) {
- // If we're in the middle of a failed #if, we ignore all commands
- // except for the if-related commands, above.
- return true;
- } else if (_command == "begin") {
- return handle_begin_command();
- } else if (_command == "forscopes") {
- return handle_forscopes_command();
- } else if (_command == "foreach") {
- return handle_foreach_command();
- } else if (_command == "formap") {
- return handle_formap_command();
- } else if (_command == "defsub") {
- return handle_defsub_command(true);
- } else if (_command == "defun") {
- return handle_defsub_command(false);
- } else if (_command == "output") {
- return handle_output_command();
- } else if (_command == "end") {
- return handle_end_command();
- } else if (_in_for) {
- // If we're currently saving up lines within a block sequence, we
- // ignore all commands except for the block-related commands,
- // above.
- return true;
- } else if (_command == "format") {
- return handle_format_command();
- } else if (_command == "print") {
- return handle_print_command();
- } else if (_command == "include") {
- return handle_include_command();
- } else if (_command == "sinclude") {
- return handle_sinclude_command();
- } else if (_command == "call") {
- return handle_call_command();
- } else if (_command == "error") {
- return handle_error_command();
- } else if (_command == "defer") {
- return handle_defer_command();
- } else if (_command == "define") {
- return handle_define_command();
- } else if (_command == "set") {
- return handle_set_command();
- } else if (_command == "map") {
- return handle_map_command();
- } else if (_command == "addmap") {
- return handle_addmap_command();
- }
-
- cerr << "Invalid command: " << COMMAND_PREFIX << _command << "\n";
- return false;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_if_command
- // Access: Protected
- // Description: Handles the #if command: conditionally evaluate the
- // following code.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_if_command() {
- if (failed_if()) {
- // If we're *already* inside a failed if, we don't have to
- // evaluate this one, but we do need to record the nesting level.
- IfNesting *nest = new IfNesting(IS_done);
- nest->push(this);
- } else {
- // If the parameter string evaluates to empty, the case is false.
- // Otherwise the case is true. However, if we're currently
- // scanning #forscopes or something, we don't evaluate this at
- // all, because it doesn't matter.
- bool is_empty = true;
- if (!_in_for) {
- _params = _scope->expand_string(_params);
- string::const_iterator si;
- for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
- is_empty = isspace(*si);
- }
- }
-
- IfState state = is_empty ? IS_off : IS_on;
- IfNesting *nest = new IfNesting(state);
- nest->push(this);
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_elif_command
- // Access: Protected
- // Description: Handles the #elif command: conditionally evaluate
- // the following code, following a failed #if command.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_elif_command() {
- if (_if_nesting == (IfNesting *)NULL) {
- cerr << "elif encountered without if.\n";
- return false;
- }
- if (_if_nesting->_state == IS_else) {
- cerr << "elif encountered after else.\n";
- return false;
- }
- if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
- // If we passed the #if above, we don't need to evaluate the #elif.
- _if_nesting->_state = IS_done;
- return true;
- }
- // If the parameter string evaluates to empty, the case is false.
- // Otherwise the case is true.
- bool is_empty = true;
- if (!_in_for) {
- _params = _scope->expand_string(_params);
- string::const_iterator si;
- for (si = _params.begin(); si != _params.end() && is_empty; ++si) {
- is_empty = isspace(*si);
- }
- }
- _if_nesting->_state = is_empty ? IS_off : IS_on;
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_else_command
- // Access: Protected
- // Description: Handles the #else command: evaluate the following
- // code following a failed #if command.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_else_command() {
- if (_if_nesting == (IfNesting *)NULL) {
- cerr << "else encountered without if.\n";
- return false;
- }
- if (_if_nesting->_state == IS_else) {
- cerr << "else encountered after else.\n";
- return false;
- }
- if (_if_nesting->_state == IS_on || _if_nesting->_state == IS_done) {
- _if_nesting->_state = IS_done;
- return true;
- }
- _if_nesting->_state = IS_else;
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_endif_command
- // Access: Protected
- // Description: Handles the #endif command: close a preceeding #if
- // command.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_endif_command() {
- if (_if_nesting == (IfNesting *)NULL) {
- cerr << "endif encountered without if.\n";
- return false;
- }
- IfNesting *nest = _if_nesting;
- nest->pop(this);
- if (nest->_block != _block_nesting) {
- cerr << "If block not closed within scoping block.\n";
- return false;
- }
- delete nest;
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_begin_command
- // Access: Protected
- // Description: Handles the #begin command: begin a named scope
- // block. The variables defined between this command
- // and the corresponding #end command will be local to
- // this named scope.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_begin_command() {
- string name = trim_blanks(_scope->expand_string(_params));
- BlockNesting *nest = new BlockNesting(BS_begin, name);
- if (contains_whitespace(name)) {
- cerr << "Attempt to define scope named \"" << name
- << "\".\nScope names may not contain whitespace.\n";
- return false;
- }
- if (name.find(SCOPE_DIRNAME_SEPARATOR) != string::npos) {
- cerr << "Attempt to define scope named \"" << name
- << "\".\nScope names may not contain the '"
- << SCOPE_DIRNAME_SEPARATOR << "' character.\n";
- return false;
- }
- nest->push(this);
- PPScope *named_scope = _scope->get_named_scopes()->make_scope(name);
- named_scope->set_parent(_scope);
- _scope = named_scope;
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_forscopes_command
- // Access: Protected
- // Description: Handles the #forscopes command: interpret all the lines
- // between this command and the corresponding #end
- // command once for each occurrence of a named scope
- // with the given name.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_forscopes_command() {
- BlockState state = _in_for ? BS_nested_forscopes : BS_forscopes;
- string name = trim_blanks(_scope->expand_string(_params));
- BlockNesting *nest = new BlockNesting(state, name);
- nest->push(this);
- if (!_in_for) {
- _in_for = true;
- _saved_lines.clear();
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_foreach_command
- // Access: Protected
- // Description: Handles the #foreach command: interpret all the lines
- // between this command and the corresponding #end
- // command once for each word in the argument.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_foreach_command() {
- // Get the parameters of the foreach command. The first word is the
- // name of the variable to substitute in (and which should appear on
- // the matching #end command), and the remaining words are the
- // values to substitute in.
- vector<string> words;
- tokenize_whitespace(_scope->expand_string(_params), words);
- if (words.empty()) {
- cerr << "#foreach requires at least one parameter.\n";
- return false;
- }
- string variable_name = words.front();
- BlockState state = _in_for ? BS_nested_foreach : BS_foreach;
- BlockNesting *nest = new BlockNesting(state, variable_name);
- nest->push(this);
- // We insert in all but the first word in the words vector.
- nest->_words.insert(nest->_words.end(), words.begin() + 1, words.end());
- if (!_in_for) {
- _in_for = true;
- _saved_lines.clear();
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_formap_command
- // Access: Protected
- // Description: Handles the #formap command: interpret all the lines
- // between this command and the corresponding #end
- // command once for each key in the map, and also within
- // the corresponding scope of that particular key.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_formap_command() {
- // Get the parameters of the formap command. The first word is the
- // name of the variable to substitute in (and which should appear on
- // the matching #end command), and the second word is the name of
- // the map variable.
- vector<string> words;
- tokenize_whitespace(_scope->expand_string(_params), words);
- if (words.size() != 2) {
- cerr << "#formap requires exactly two parameters.\n";
- return false;
- }
- string variable_name = words.front();
- BlockState state = _in_for ? BS_nested_formap : BS_formap;
- BlockNesting *nest = new BlockNesting(state, words[0]);
- nest->push(this);
- nest->_words.push_back(words[1]);
- if (!_in_for) {
- _in_for = true;
- _saved_lines.clear();
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_defsub_command
- // Access: Protected
- // Description: Handles the #defsub (or #defun) command: save all the
- // lines between this command and the matching #end as a
- // callable subroutine to be invoked by a later #call
- // command. If is_defsub is false, it means this
- // subroutine was actually defined via a #defun command,
- // so it is to be invoked by a later variable reference,
- // instead of by a #call command.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_defsub_command(bool is_defsub) {
- string command = (is_defsub) ? "#defsub" : "#defun";
- // The first word of the parameter list is the subroutine name; the
- // rest is the comma-separated list of formal parameter names.
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string subroutine_name = trim_blanks(_params.substr(0, p));
- if (subroutine_name.empty()) {
- cerr << command << " requires at least one parameter.\n";
- return false;
- }
- vector<string> formals;
- _scope->tokenize_params(_params.substr(p), formals, false);
- vector<string>::const_iterator fi;
- for (fi = formals.begin(); fi != formals.end(); ++fi) {
- if (!is_valid_formal(*fi)) {
- cerr << command << " " << subroutine_name
- << ": invalid formal parameter name '" << (*fi) << "'\n";
- return false;
- }
- }
- if (_in_for) {
- cerr << command << " may not appear within another block scoping command like\n"
- << "#forscopes, #foreach, #formap, #defsub, or #defun.\n";
- return false;
- }
- BlockState state = is_defsub ? BS_defsub : BS_defun;
- BlockNesting *nest = new BlockNesting(state, subroutine_name);
- nest->push(this);
- nest->_words.swap(formals);
- _in_for = true;
- _saved_lines.clear();
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_output_command
- // Access: Protected
- // Description: Handles the #output command: all text between this
- // command and the corresponding #end command will be
- // sent to the indicated output file.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_output_command() {
- vector<string> words;
- tokenize_whitespace(_scope->expand_string(_params), words);
- if (words.empty()) {
- cerr << "#output command requires one parameter.\n";
- return false;
- }
- BlockNesting *nest = new BlockNesting(BS_output, words[0]);
- // Also check the output flags.
- for (int i = 1; i < (int)words.size(); i++) {
- if (words[i] == "notouch") {
- nest->_flags |= OF_notouch;
- } else {
- cerr << "Invalid output flag: " << words[i] << "\n";
- }
- }
- nest->push(this);
- if (!_in_for) {
- string filename = nest->_name;
- if (filename.empty()) {
- cerr << "Attempt to output to empty filename\n";
- return false;
- }
-
- string prefix = _scope->expand_variable("DIRPREFIX");
- if (filename[0] != '/') {
- filename = prefix + filename;
- }
-
- nest->_true_name = filename;
- nest->_tempnam = (char *)NULL;
- if (access(filename.c_str(), F_OK) == 0) {
- // If the file already exists, create a temporary file first.
-
- nest->_tempnam = tempnam((prefix + ".").c_str(), "pptmp");
- assert(nest->_tempnam != (char *)NULL);
-
- nest->_output.open(nest->_tempnam);
- if (nest->_output.fail()) {
- cerr << "Unable to open output file " << nest->_tempnam << "\n";
- return false;
- }
-
- } else {
- // If the file does not already exist, create it directly instead
- // of monkeying around with temporary files.
- cerr << "Generating " << filename << "\n";
-
- nest->_output.open(filename.c_str(), ios::out, 0666);
- if (nest->_output.fail()) {
- cerr << "Unable to open output file " << filename << "\n";
- return false;
- }
- }
-
- _write_state = new WriteState(*_write_state);
- _write_state->_out = &nest->_output;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_end_command
- // Access: Protected
- // Description: Handles the #end command. This closes a number of
- // different kinds of blocks, like #begin and #forscopes.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_end_command() {
- if (_block_nesting == (BlockNesting *)NULL) {
- cerr << "Unmatched end " << _params << ".\n";
- return false;
- }
- string name = trim_blanks(_scope->expand_string(_params));
- if (name != _block_nesting->_name) {
- cerr << "end " << name << " encountered where end "
- << _block_nesting->_name << " expected.\n";
- return false;
- }
- BlockNesting *nest = _block_nesting;
- nest->pop(this);
- if (nest->_if != _if_nesting) {
- cerr << "If block not closed within scoping block.\n";
- return false;
- }
- if (nest->_state == BS_forscopes) {
- // Now replay all of the saved lines.
- _in_for = false;
- if (!replay_forscopes(nest->_name)) {
- return false;
- }
- } else if (nest->_state == BS_foreach) {
- // Now replay all of the saved lines.
- _in_for = false;
- if (!replay_foreach(nest->_name, nest->_words)) {
- return false;
- }
- } else if (nest->_state == BS_formap) {
- // Now replay all of the saved lines.
- _in_for = false;
- assert(nest->_words.size() == 1);
- if (!replay_formap(nest->_name, nest->_words[0])) {
- return false;
- }
- } else if (nest->_state == BS_defsub || nest->_state == BS_defun) {
- // Save all of the saved lines as a named subroutine.
- _in_for = false;
- PPSubroutine *sub = new PPSubroutine;
- sub->_formals.swap(nest->_words);
- sub->_lines.swap(_saved_lines);
- // Remove the #end command. This will fail if someone makes an
- // #end command that spans multiple lines. Don't do that.
- assert(!sub->_lines.empty());
- sub->_lines.pop_back();
- if (nest->_state == BS_defsub) {
- PPSubroutine::define_sub(nest->_name, sub);
- } else {
- PPSubroutine::define_func(nest->_name, sub);
- }
- } else if (nest->_state == BS_output) {
- if (!_in_for) {
- if (!nest->_output) {
- cerr << "Error while writing " << nest->_true_name << "\n";
- return false;
- }
- nest->_output.close();
- // Verify the output file.
- if (nest->_tempnam != (char *)NULL) {
- if (!compare_output(nest->_tempnam, nest->_true_name,
- (nest->_flags & OF_notouch) != 0)) {
- return false;
- }
- free(nest->_tempnam);
- }
- }
- }
- delete nest;
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_format_command
- // Access: Protected
- // Description: Handles the #format command: change the formatting
- // mode of lines as they are output.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_format_command() {
- _params = trim_blanks(_scope->expand_string(_params));
- if (_params == "straight") {
- _write_state->_format = WF_straight;
- } else if (_params == "collapse") {
- _write_state->_format = WF_collapse;
- } else if (_params == "makefile") {
- _write_state->_format = WF_makefile;
- } else {
- cerr << "Ignoring invalid write format: " << _params << "\n";
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_print_command
- // Access: Protected
- // Description: Handles the #print command: immediately output the
- // arguments to this line to standard error.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_print_command() {
- cerr << _scope->expand_string(_params) << "\n";
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_include_command
- // Access: Protected
- // Description: Handles the #include command: the indicated file is
- // read and processed at this point.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_include_command() {
- string filename = trim_blanks(_scope->expand_string(_params));
- // We allow optional quotation marks around the filename.
- if (filename.length() >= 2 &&
- filename[0] == '"' &&
- filename[filename.length() - 1] == '"') {
- filename = filename.substr(1, filename.length() - 2);
- }
- return include_file(filename);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_sinclude_command
- // Access: Protected
- // Description: Handles the #sinclude command: the indicated file is
- // read and processed at this point. This is different
- // from #include only in that if the file does not
- // exist, there is no error; instead, nothing happens.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_sinclude_command() {
- string filename = trim_blanks(_scope->expand_string(_params));
- // We allow optional quotation marks around the filename.
- if (filename.length() >= 2 &&
- filename[0] == '"' &&
- filename[filename.length() - 1] == '"') {
- filename = filename.substr(1, filename.length() - 2);
- }
- if (access(filename.c_str(), F_OK) != 0) {
- // No such file; no error.
- return true;
- }
- return include_file(filename);
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_call_command
- // Access: Protected
- // Description: Handles the #call command: the indicated named
- // subroutine is read and processed at this point.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_call_command() {
- // The first word is the name of the subroutine; the rest is the
- // comma-separated list of expressions to substitute in for the
- // subroutine's formal parameters.
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string subroutine_name = trim_blanks(_params.substr(0, p));
- string params = _params.substr(p);
- if (subroutine_name.empty()) {
- cerr << "#call requires at least one parameter.\n";
- return false;
- }
- const PPSubroutine *sub = PPSubroutine::get_sub(subroutine_name);
- if (sub == (const PPSubroutine *)NULL) {
- cerr << "Attempt to call undefined subroutine " << subroutine_name << "\n";
- }
- PPScope *old_scope = _scope;
- PPScope::push_scope(_scope);
- PPScope nested_scope(_scope->get_named_scopes());
- _scope = &nested_scope;
- nested_scope.define_formals(subroutine_name, sub->_formals, params);
- vector<string>::const_iterator li;
- for (li = sub->_lines.begin(); li != sub->_lines.end(); ++li) {
- if (!read_line(*li)) {
- PPScope::pop_scope();
- _scope = old_scope;
- return false;
- }
- }
- PPScope::pop_scope();
- _scope = old_scope;
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_error_command
- // Access: Protected
- // Description: Handles the #error command: terminate immediately
- // with the given error message.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_error_command() {
- string message = trim_blanks(_scope->expand_string(_params));
-
- if (!message.empty()) {
- cerr << message << "\n";
- }
- return false;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_defer_command
- // Access: Protected
- // Description: Handles the #defer command: define a new variable or
- // change the definition of an existing variable. This
- // is different from #define in that the variable
- // definition is not immediately expanded; it will be
- // expanded when the variable is later used. This
- // allows the definition of variables that depend on
- // other variables whose values have not yet been
- // defined. This is akin to GNU make's = assignment.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_defer_command() {
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string varname = _params.substr(0, p);
- if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
- cerr << "Warning: variable " << varname
- << " shadowed by function definition.\n";
- }
-
- // Skip whitespace between the variable name and its definition.
- while (p < _params.length() && isspace(_params[p])) {
- p++;
- }
- string def = _params.substr(p);
- // We don't expand the variable's definition immediately; it will be
- // expanded when the variable is referenced later. However, we
- // should expand any simple self-reference immediately, to allow for
- // recursive definitions.
- def = _scope->expand_self_reference(def, varname);
- _scope->define_variable(varname, def);
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_define_command
- // Access: Protected
- // Description: Handles the #define command: define a new variable or
- // change the definition of an existing variable. The
- // variable definition is immediately expanded. This is
- // akin to GNU make's := assignment.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_define_command() {
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string varname = _params.substr(0, p);
- if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
- cerr << "Warning: variable " << varname
- << " shadowed by function definition.\n";
- }
-
- // Skip whitespace between the variable name and its definition.
- while (p < _params.length() && isspace(_params[p])) {
- p++;
- }
- string def = _scope->expand_string(_params.substr(p));
- _scope->define_variable(varname, def);
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_set_command
- // Access: Protected
- // Description: Handles the #set command: change the definition of an
- // existing variable.
- //
- // This is different from #defer in two ways: (1) the
- // variable in question must already have been #defined
- // elsewhere, (2) if the variable was #defined in some
- // parent scope, this will actually change the variable
- // in the parent scope, rather than shadowing it in the
- // local scope. Like #define and unlike #defer, the
- // variable definition is expanded immediately, similar
- // to GNU make's := operator.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_set_command() {
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string varname = _params.substr(0, p);
- if (PPSubroutine::get_func(varname) != (const PPSubroutine *)NULL) {
- cerr << "Warning: variable " << varname
- << " shadowed by function definition.\n";
- }
-
- // Skip whitespace between the variable name and its definition.
- while (p < _params.length() && isspace(_params[p])) {
- p++;
- }
- string def = _scope->expand_string(_params.substr(p));
- if (!_scope->set_variable(varname, def)) {
- cerr << "Attempt to set undefined variable " << varname << "\n";
- return false;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_map_command
- // Access: Protected
- // Description: Handles the #map command: define a new map variable.
- // This is a special kind of variable declaration that
- // creates a variable that can be used as a function to
- // look up variable expansions within a number of
- // different named scopes, accessed by keyword.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_map_command() {
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string varname = _params.substr(0, p);
-
- // Skip whitespace between the variable name and its definition.
- while (p < _params.length() && isspace(_params[p])) {
- p++;
- }
- string def = trim_blanks(_params.substr(p));
- _scope->define_map_variable(varname, def);
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::handle_addmap_command
- // Access: Protected
- // Description: Handles the #addmap command: add a new key/scope pair
- // to an existing map variable.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- handle_addmap_command() {
- // Pull off the first word and the rest of the params.
- size_t p = 0;
- while (p < _params.length() && !isspace(_params[p])) {
- p++;
- }
- string varname = _params.substr(0, p);
-
- // Skip whitespace between the variable name and the key.
- while (p < _params.length() && isspace(_params[p])) {
- p++;
- }
- string key = trim_blanks(_scope->expand_string(_params.substr(p)));
- _scope->add_to_map_variable(varname, key, _scope);
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::include_file
- // Access: Protected
- // Description: The internal implementation of #include: includes a
- // particular named file at this point.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- include_file(const string &filename) {
- ifstream in(filename.c_str());
- if (!in) {
- cerr << "Unable to open include file " << filename << ".\n";
- return false;
- }
- PushFilename pushed(_scope, filename);
- string line;
- getline(in, line);
- while (!in.fail() && !in.eof()) {
- if (!read_line(line)) {
- return false;
- }
- getline(in, line);
- }
- if (!in.eof()) {
- cerr << "Error reading " << filename << ".\n";
- return false;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::replay_forscopes
- // Access: Protected
- // Description: Replays all the lines that were saved during a
- // previous #forscopes..#end block.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- replay_forscopes(const string &name) {
- assert(!_in_for);
- bool okflag = true;
- vector<string> lines;
- lines.swap(_saved_lines);
- // Remove the #end command. This will fail if someone makes an #end
- // command that spans multiple lines. Don't do that.
- assert(!lines.empty());
- lines.pop_back();
- PPNamedScopes *named_scopes = _scope->get_named_scopes();
- // Extract out the scope names from the #forscopes .. #end name. This
- // is a space-delimited list of scope names.
- vector<string> words;
- tokenize_whitespace(name, words);
- // Now build up the list of scopes with these names.
- PPNamedScopes::Scopes scopes;
- vector<string>::const_iterator wi;
- for (wi = words.begin(); wi != words.end(); ++wi) {
- named_scopes->get_scopes(*wi, scopes);
- }
- PPNamedScopes::sort_by_dependency(scopes);
- // And finally, replay all of the saved lines.
- BlockNesting *saved_block = _block_nesting;
- IfNesting *saved_if = _if_nesting;
- PPNamedScopes::Scopes::const_iterator si;
- for (si = scopes.begin(); si != scopes.end() && okflag; ++si) {
- PPScope::push_scope(_scope);
- _scope = (*si);
-
- vector<string>::const_iterator li;
- for (li = lines.begin(); li != lines.end() && okflag; ++li) {
- okflag = read_line(*li);
- }
- _scope = PPScope::pop_scope();
- }
- if (saved_block != _block_nesting || saved_if != _if_nesting) {
- cerr << "Misplaced #end or #endif.\n";
- okflag = false;
- }
- return okflag;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::replay_foreach
- // Access: Protected
- // Description: Replays all the lines that were saved during a
- // previous #foreach..#end block.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- replay_foreach(const string &varname, const vector<string> &words) {
- assert(!_in_for);
- bool okflag = true;
- vector<string> lines;
- lines.swap(_saved_lines);
- // Remove the #end command. This will fail if someone makes an #end
- // command that spans multiple lines. Don't do that.
- assert(!lines.empty());
- lines.pop_back();
- // Now traverse through the saved words.
- BlockNesting *saved_block = _block_nesting;
- IfNesting *saved_if = _if_nesting;
- vector<string>::const_iterator wi;
- for (wi = words.begin(); wi != words.end() && okflag; ++wi) {
- _scope->define_variable(varname, (*wi));
- vector<string>::const_iterator li;
- for (li = lines.begin(); li != lines.end() && okflag; ++li) {
- okflag = read_line(*li);
- }
- }
- if (saved_block != _block_nesting || saved_if != _if_nesting) {
- cerr << "Misplaced #end or #endif.\n";
- okflag = false;
- }
- return okflag;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::replay_formap
- // Access: Protected
- // Description: Replays all the lines that were saved during a
- // previous #formap..#end block.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- replay_formap(const string &varname, const string &mapvar) {
- assert(!_in_for);
- bool okflag = true;
- vector<string> lines;
- lines.swap(_saved_lines);
- // Remove the #end command. This will fail if someone makes an #end
- // command that spans multiple lines. Don't do that.
- assert(!lines.empty());
- lines.pop_back();
- // Now look up the map variable.
- PPScope::MapVariableDefinition &def = _scope->find_map_variable(mapvar);
- if (&def == &PPScope::_null_map_def) {
- cerr << "Undefined map variable: #formap " << varname << " "
- << mapvar << "\n";
- return false;
- }
- // Now traverse through the map definition.
- BlockNesting *saved_block = _block_nesting;
- IfNesting *saved_if = _if_nesting;
- PPScope::MapVariableDefinition::const_iterator di;
- for (di = def.begin(); di != def.end() && okflag; ++di) {
- _scope->define_variable(varname, (*di).first);
- PPScope::push_scope(_scope);
- _scope = (*di).second;
- vector<string>::const_iterator li;
- for (li = lines.begin(); li != lines.end() && okflag; ++li) {
- okflag = read_line(*li);
- }
- _scope = PPScope::pop_scope();
- }
- if (saved_block != _block_nesting || saved_if != _if_nesting) {
- cerr << "Misplaced #end or #endif.\n";
- okflag = false;
- }
- return okflag;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::compare_output
- // Access: Protected
- // Description: After a temporary file has been written due to an
- // #output command, compare the results to the original
- // file. If they are different, remove the original
- // file and rename the temporary file; if they are the
- // same, remove the temporary file.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- compare_output(const string &temp_name, const string &true_name,
- bool notouch) {
- ifstream in_a(temp_name.c_str());
- ifstream in_b(true_name.c_str());
- int a = in_a.get();
- int b = in_b.get();
- bool differ = (a != b);
- while (!in_a.eof() && !in_b.eof() && !differ) {
- a = in_a.get();
- b = in_b.get();
- differ = (a != b);
- }
- in_a.close();
- in_b.close();
- if (differ) {
- cerr << "Generating " << true_name << "\n";
- if (unlink(true_name.c_str()) < 0) {
- cerr << "Unable to remove old " << true_name << "\n";
- return false;
- }
- if (rename(temp_name.c_str(), true_name.c_str()) < 0) {
- cerr << "Unable to rename temporary file " << temp_name
- << " to " << true_name << "\n";
- return false;
- }
- } else {
- // cerr << "File " << true_name << " is unchanged.\n";
- if (unlink(temp_name.c_str()) < 0) {
- cerr << "Warning: unable to remove temporary file " << temp_name << "\n";
- }
- // Even though the file is unchanged, unless the "notouch" flag is
- // set, we want to update the modification time. This helps the
- // makefiles know we did something.
- if (!notouch) {
- if (utime(true_name.c_str(), (struct utimbuf *)NULL) < 0) {
- cerr << "Warning: unable to touch " << true_name << "\n";
- }
- }
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::failed_if
- // Access: Protected
- // Description: Returns true if we are currently within a failed #if
- // block (or in an #else block for a passed #if block),
- // or false otherwise.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- failed_if() const {
- return (_if_nesting != (IfNesting *)NULL &&
- (_if_nesting->_state == IS_off || _if_nesting->_state == IS_done));
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::is_valid_formal_parameter_name
- // Access: Protected
- // Description: Returns true if the indicated name is an acceptable
- // name for a formal parameter. This means it includes
- // no whitespace or crazy punctuation. Mainly this is
- // to protect the user from making some stupid syntax
- // mistake.
- ////////////////////////////////////////////////////////////////////
- bool PPCommandFile::
- is_valid_formal(const string &formal_parameter_name) const {
- if (formal_parameter_name.empty()) {
- return false;
- }
-
- string::const_iterator si;
- for (si = formal_parameter_name.begin();
- si != formal_parameter_name.end();
- ++si) {
- switch (*si) {
- case ' ':
- case '\n':
- case '\t':
- case '$':
- case '[':
- case ']':
- case ',':
- return false;
- }
- }
- return true;
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::PushFilename::Constructor
- // Access: Public
- // Description: This special class changes the current filename of
- // the PPCommandFile. The idea is to create one of
- // these when the filename is changed (for instance, to
- // read in a new file via an #include directive); when
- // the variable then goes out of scope, it will restore
- // the previous filename.
- //
- // This updates the scope with the appropriate
- // variables.
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::PushFilename::
- PushFilename(PPScope *scope, const string &filename) {
- _scope = scope;
- _old_thisdirprefix = _scope->get_variable("THISDIRPREFIX");
- _old_thisfilename = _scope->get_variable("THISFILENAME");
- _scope->define_variable("THISFILENAME", filename);
- size_t slash = filename.rfind('/');
- if (slash == string::npos) {
- _scope->define_variable("THISDIRPREFIX", string());
- } else {
- _scope->define_variable("THISDIRPREFIX", filename.substr(0, slash + 1));
- }
- }
- ////////////////////////////////////////////////////////////////////
- // Function: PPCommandFile::PushFilename::Destructor
- // Access: Public
- // Description:
- ////////////////////////////////////////////////////////////////////
- PPCommandFile::PushFilename::
- ~PushFilename() {
- _scope->define_variable("THISDIRPREFIX", _old_thisdirprefix);
- _scope->define_variable("THISFILENAME", _old_thisfilename);
- }
|