From 32a08f8db06857688a2aaf1fb95be515599ef3af Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 28 Feb 2017 14:34:22 +0100
Subject: [PATCH] preprocessor: add onlyjson option to allow exit upon writing
 of JSON output. #1387

---
 DynareMain.cc  |  9 ++++++---
 DynareMain2.cc | 10 +++++-----
 ModFile.cc     |  5 ++++-
 ModFile.hh     |  2 +-
 4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/DynareMain.cc b/DynareMain.cc
index cd62325c..3aa0aa3f 100644
--- a/DynareMain.cc
+++ b/DynareMain.cc
@@ -45,7 +45,7 @@ void main2(stringstream &in, string &basename, bool debug, bool clear_all, bool
 #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
            , bool cygwin, bool msvc, bool mingw
 #endif
-           , JsonOutputPointType json, JsonFileOutputType json_output_mode
+           , JsonOutputPointType json, JsonFileOutputType json_output_mode, bool onlyjson
            );
 
 void main1(char *modfile, string &basename, bool debug, bool save_macro, string &save_macro_file,
@@ -62,7 +62,7 @@ usage()
 #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
        << " [cygwin] [msvc] [mingw]"
 #endif
-       << "[json=parse|check|transform|compute] [jsonstdout]"
+       << "[json=parse|check|transform|compute] [jsonstdout] [onlyjson]"
        << endl;
   exit(EXIT_FAILURE);
 }
@@ -117,6 +117,7 @@ main(int argc, char **argv)
   FileOutputType output_mode = none;
   JsonOutputPointType json = nojson;
   JsonFileOutputType json_output_mode = file;
+  bool onlyjson = false;
   LanguageOutputType language = matlab;
 
   // Parse options
@@ -297,6 +298,8 @@ main(int argc, char **argv)
         }
       else if (!strcmp(argv[arg], "jsonstdout"))
         json_output_mode = standardout;
+      else if (!strcmp(argv[arg], "onlyjson"))
+        onlyjson = true;
       else if (strlen(argv[arg]) >= 4 && !strncmp(argv[arg], "json", 4))
         {
 	  if (strlen(argv[arg]) <= 5 || argv[arg][4] != '=')
@@ -364,7 +367,7 @@ main(int argc, char **argv)
 #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
         , cygwin, msvc, mingw
 #endif
-        , json, json_output_mode
+        , json, json_output_mode, onlyjson
         );
 
   return EXIT_SUCCESS;
diff --git a/DynareMain2.cc b/DynareMain2.cc
index bc2d34ff..5139cd3d 100644
--- a/DynareMain2.cc
+++ b/DynareMain2.cc
@@ -34,7 +34,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear
 #if defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__)
       , bool cygwin, bool msvc, bool mingw
 #endif
-      , JsonOutputPointType json, JsonFileOutputType json_output_mode
+      , JsonOutputPointType json, JsonFileOutputType json_output_mode, bool onlyjson
       )
 {
   ParsingDriver p(warnings, nostrict);
@@ -42,17 +42,17 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear
   // Do parsing and construct internal representation of mod file
   ModFile *mod_file = p.parse(in, debug);
   if (json == parsing)
-    mod_file->writeJsonOutput(basename, json, json_output_mode);
+    mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson);
 
   // Run checking pass
   mod_file->checkPass(nostrict);
   if (json == checkpass)
-    mod_file->writeJsonOutput(basename, json, json_output_mode);
+    mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson);
 
   // Perform transformations on the model (creation of auxiliary vars and equations)
   mod_file->transformPass(nostrict);
   if (json == transformpass)
-    mod_file->writeJsonOutput(basename, json, json_output_mode);
+    mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson);
 
   // Evaluate parameters initialization, initval, endval and pounds
   mod_file->evalAllExpressions(warn_uninit);
@@ -60,7 +60,7 @@ main2(stringstream &in, string &basename, bool debug, bool clear_all, bool clear
   // Do computations
   mod_file->computingPass(no_tmp_terms, output_mode, compute_xrefs, params_derivs_order);
   if (json == computingpass)
-    mod_file->writeJsonOutput(basename, json, json_output_mode);
+    mod_file->writeJsonOutput(basename, json, json_output_mode, onlyjson);
 
   // Write outputs
   if (output_mode != none)
diff --git a/ModFile.cc b/ModFile.cc
index 4228a393..32cb70bf 100644
--- a/ModFile.cc
+++ b/ModFile.cc
@@ -1248,7 +1248,7 @@ ModFile::writeExternalFilesJulia(const string &basename, FileOutputType output)
 }
 
 void
-ModFile::writeJsonOutput(const string &basename, JsonOutputPointType json, JsonFileOutputType json_output_mode)
+ModFile::writeJsonOutput(const string &basename, JsonOutputPointType json, JsonFileOutputType json_output_mode, bool onlyjson)
 {
   if (json == nojson)
     return;
@@ -1282,6 +1282,9 @@ ModFile::writeJsonOutput(const string &basename, JsonOutputPointType json, JsonF
       cerr << "ModFile::writeJsonOutput: should not arrive here." << endl;
       exit(EXIT_FAILURE);
     }
+
+  if (onlyjson)
+    exit(EXIT_SUCCESS);
 }
 
 void
diff --git a/ModFile.hh b/ModFile.hh
index 684d592e..385e97b6 100644
--- a/ModFile.hh
+++ b/ModFile.hh
@@ -174,7 +174,7 @@ public:
   //! Initially created to enable Julia to work with .mod files
   //! Potentially outputs ModFile after the various parts of processing (parsing, checkPass, transformPass, computingPass)
   //! Allows user of other host language platforms (python, fortran, etc) to provide support for dynare .mod files
-  void writeJsonOutput(const string &basename, JsonOutputPointType json, JsonFileOutputType json_output_mode);
+  void writeJsonOutput(const string &basename, JsonOutputPointType json, JsonFileOutputType json_output_mode, bool onlyjson);
 };
 
 #endif // ! MOD_FILE_HH
-- 
GitLab