Sat 21 Jan 2017 12:18:59 PM UTC, original submission:
The following code reports parse error only when at least one command line argument is supplied.
This happens because with command line arguments, source_file is called
twice.
First call loads binary file, second - tries to print value of undefined variable.
Without command line arguments, first call is skipped.
In this case, parse error is correctly reported.
#include <string>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
// Workaround for bug http://savannah.gnu.org/bugs/?50068
// Recommended by John W. Eaton
bool _octave_init() {
string_vector octave_argv (2);
octave_argv(0) = "embedded";
octave_argv(1) = "-q";
static octave::embedded_application app (2, octave_argv.c_str_vec ());
if (! app.execute ()) {
std::cerr << "ERROR: Creating embedded Octave interpreter failed!" << std::endl;
return false;
}
return true;
}
int main (int argc, char** argv) {
if( !_octave_init() ) return 1;
try {
int parse_status;
eval_string (std::string ("who"), 0, parse_status);
system("echo a=5 >my_octave_file.m");
system("octave -q --eval 'a=5; save(\"-binary\",\"my_octave_file.obm\",\"a\");'");
system("echo load my_octave_file.obm >my_octave_file.m");
system("cat my_octave_file.m");
system("octave -q --eval 'load my_octave_file.obm; a'");
if(argc < 2) {
source_file("my_octave_file.m");
}
eval_string (std::string ("who"), 0, parse_status);
system("echo junk >my_octave_file.m");
system("cat my_octave_file.m");
source_file("my_octave_file.m");
eval_string (std::string ("who"), 0, parse_status);
}
catch (const octave::exit_exception& ex) {
exit (ex.exit_status ());
}
catch (const octave::execution_exception&) {
std::cerr << "error evaluating Octave code!" << std::endl;
}
std::cout << "Finished" << std::endl;
// Workaround for another bug, discussion - see http://savannah.gnu.org/bugs/?50068
try {
clean_up_and_exit (0);
} catch(...) { }
std::_Exit(0); // Probably not needed in 4.2.0. Without sometimes main application crashed in previous versions
return 0;
}
|