diff --git a/gnucash/gnucash-cli.cpp b/gnucash/gnucash-cli.cpp index c2a1a6ae12a..89164fc967f 100644 --- a/gnucash/gnucash-cli.cpp +++ b/gnucash/gnucash-cli.cpp @@ -141,8 +141,6 @@ may be specified to describe some saved options.\n" #include "gnc-session.h" #include "gnc-prefs-utils.h" #include "libguile.h" -#include <iostream> // std::cout -#include <fstream> // std::ifstream struct cli_struct { @@ -171,18 +169,39 @@ scm_run_cli (void *data, [[maybe_unused]] int argc, [[maybe_unused]] char **argv std::cout << "Starting CLI... " << std::endl; scm_c_eval_string ("(start-repl)"); } + if (gnc_current_session_exist()) + { + std::cout << "Destroying session..." << std::endl; + gnc_clear_current_session (); + } } static void load_file (std::string m_file_to_load, bool m_interactive_rw) { - std::cout << "Loading " << (m_interactive_rw ? "" : "(readonly) ") - << m_file_to_load << "..."; + std::cout << "\n\nLoading " << m_file_to_load << (m_interactive_rw ? " (r/w)" : " (readonly)"); auto session = gnc_get_current_session(); - qof_session_begin (session, m_file_to_load.c_str(), - m_interactive_rw ? SESSION_READ_ONLY : SESSION_NORMAL_OPEN); - qof_session_load (session, NULL); - std::cout << "done!" << std::endl; + auto mode = m_interactive_rw ? SESSION_READ_ONLY : SESSION_NORMAL_OPEN; + while (true) + { + qof_session_begin (session, m_file_to_load.c_str(), mode); + auto io_err = qof_session_get_error (session); + switch (io_err) + { + case ERR_BACKEND_NO_ERR: + qof_session_load (session, NULL); + std::cout << "... done!\n\n"; + return; + case ERR_BACKEND_LOCKED: + case ERR_BACKEND_READONLY: + std::cout << " (forced readonly)"; + mode = SESSION_READ_ONLY; + break; + default: + std::cout << "... unknown error. Abort." << std::endl; + exit (1); + } + } } int