<commit_before>/*
 * Copyright (c) 2003-2009, <NAME>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of New Artisans LLC nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "session.h"
#include "report.h"
#include "option.h"
#include "help.h"

#include "textual.h"
#include "qif.h"
#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
#include "xml.h"
#include "gnucash.h"
#endif
#ifdef HAVE_LIBOFX
#include "ofx.h"
#endif

#ifdef HAVE_UNIX_PIPES
#include <sys/types.h>
#include <sys/wait.h>
#include "fdstream.h"
#endif

namespace ledger {
  int read_and_report(ledger::report_t& report,
		      int argc, char * argv[], char * envp[])
  {
    using namespace ledger;

    session_t& session(report.session);

    // Setup global defaults

    optional<path> home;
    if (const char * home_var = std::getenv("HOME"))
      home = home_var;

    session.init_file  = home ? *home / ".ledgerrc"	: "./.ledgerrc";
    session.price_db   = home ? *home / ".pricedb"	: "./.pricedb";
    session.cache_file = home ? *home / ".ledger-cache" : "./.ledger-cache";

    // Process the environment settings

    TRACE_START(environment, 1, "Processed environment variables");

    process_environment(const_cast<const char **>(envp), "LEDGER_", report);

#if 1
    // These are here for backwards compatability, but are deprecated.

    if (const char * p = std::getenv("LEDGER"))
      process_option("file", report, p);
    if (const char * p = std::getenv("LEDGER_INIT"))
      process_option("init-file", report, p);
    if (const char * p = std::getenv("PRICE_HIST"))
      process_option("price-db", report, p);
    if (const char * p = std::getenv("PRICE_EXP"))
      process_option("price-exp", report, p);
#endif

    TRACE_FINISH(environment, 1);

    // Read the initialization file

    TRACE_START(init, 1, "Read initialization file");

    session.read_init();

    TRACE_FINISH(init, 1);

    // Handle the command-line arguments

    TRACE_START(arguments, 1, "Processing command-line arguments");

    strings_list args;
    process_arguments(argc - 1, argv + 1, report, args);

    if (args.empty()) {
      ledger::help(std::cout);
      return 1;
    }
    strings_list::iterator arg = args.begin();

    if (! session.cache_file)
      session.use_cache = false;
    else if (session.use_cache)
      session.use_cache = ! session.data_file.empty() && session.price_db;

    DEBUG("ledger.session.cache", "1. use_cache = " << session.use_cache);

    if (session.data_file == *session.cache_file)
      session.use_cache = false;

    DEBUG("ledger.session.cache", "2. use_cache = " << session.use_cache);

    INFO("Initialization file is " << session.init_file->string());
    INFO("Price database is " << session.price_db->string());
    INFO("Binary cache is " << session.cache_file->string());
    INFO("Journal file is " << session.data_file.string());

    if (! session.use_cache)
      INFO("Binary cache mechanism will not be used");

    TRACE_FINISH(arguments, 1);

    // Configure the output stream

#ifdef HAVE_UNIX_PIPES
    int status, pfd[2];		// Pipe file descriptors
#endif
    report.output_stream = &std::cout;

    if (report.output_file) {
      report.output_stream = new ofstream(*report.output_file);
    }
#ifdef HAVE_UNIX_PIPES
    else if (report.pager) {
      status = pipe(pfd);
      if (status == -1)
	throw_(std::logic_error, "Failed to create pipe");

      status = fork();
      if (status < 0) {
	throw_(std::logic_error, "Failed to fork child process");
      }
      else if (status == 0) {	// child
	// Duplicate pipe's reading end into stdin
	status = dup2(pfd[0], STDIN_FILENO);
	if (status == -1)
	  perror("dup2");

	// Close unuseful file descriptors: the pipe's writing and
	// reading ends (the latter is not needed anymore, after the
	// duplication).
	close(pfd[1]);
	close(pfd[0]);

	// Find command name: its the substring starting right of the
	// rightmost '/' character in the pager pathname.  See manpage
	// for strrchr.
	execlp(report.pager->native_file_string().c_str(),
	       basename(*report.pager).c_str(), NULL);
	perror("execl");
	exit(1);
      }
      else {			// parent
	close(pfd[0]);
	report.output_stream = new boost::fdostream(pfd[1]);
      }
    }
#endif

    // Read the command word and see if it's any of the debugging commands
    // that Ledger supports.

    std::ostream& out(*report.output_stream);

    string verb = *arg++;

    if (verb == "parse") {
      expr_t expr(*arg);

      out << "--- Input text ---" << std::endl;
      out << *arg << std::endl;

      out << std::endl << "--- Text as parsed ---" << std::endl;
      expr.print(out);
      out << std::endl;

      out << std::endl << "--- Expression tree ---" << std::endl;
      expr.dump(out);

      out << std::endl << "--- Calculated value ---" << std::endl;
      expr.calc(report).print(out);
      out << std::endl;

      return 0;
    }
    else if (verb == "compile") {
      expr_t expr(*arg);

      out << "--- Input text ---" << std::endl;
      out << *arg << std::endl;

      out << std::endl << "--- Text as parsed ---" << std::endl;
      expr.print(out);
      out << std::endl;

      out << std::endl << "--- Expression tree ---" << std::endl;
      expr.dump(out);

      expr.compile(report);

      out << std::endl << "--- Compiled tree ---" << std::endl;
      expr.dump(out);

      out << std::endl << "--- Calculated value ---" << std::endl;
      expr.calc(report).print(out);
      out << std::endl;

      return 0;
    }
    else if (verb == "eval") {
      expr_t expr(*arg);
      out << expr.calc(report).strip_annotations() << std::endl;
      return 0;
    }
    else if (verb == "format") {<commit_msg>Made the output from parse/compile commands more consistent.<commit_after>/*
 * Copyright (c) 2003-2009, <NAME>.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of New Artisans LLC nor the names of its
 *   contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "session.h"
#include "report.h"
#include "option.h"
#include "help.h"

#include "textual.h"
#include "qif.h"
#if defined(HAVE_EXPAT) || defined(HAVE_XMLPARSE)
#include "xml.h"
#include "gnucash.h"
#endif
#ifdef HAVE_LIBOFX
#include "ofx.h"
#endif

#ifdef HAVE_UNIX_PIPES
#include <sys/types.h>
#include <sys/wait.h>
#include "fdstream.h"
#endif

namespace ledger {
  int read_and_report(ledger::report_t& report,
		      int argc, char * argv[], char * envp[])
  {
    using namespace ledger;

    session_t& session(report.session);

    // Setup global defaults

    optional<path> home;
    if (const char * home_var = std::getenv("HOME"))
      home = home_var;

    session.init_file  = home ? *home / ".ledgerrc"	: "./.ledgerrc";
    session.price_db   = home ? *home / ".pricedb"	: "./.pricedb";
    session.cache_file = home ? *home / ".ledger-cache" : "./.ledger-cache";

    // Process the environment settings

    TRACE_START(environment, 1, "Processed environment variables");

    process_environment(const_cast<const char **>(envp), "LEDGER_", report);

#if 1
    // These are here for backwards compatability, but are deprecated.

    if (const char * p = std::getenv("LEDGER"))
      process_option("file", report, p);
    if (const char * p = std::getenv("LEDGER_INIT"))
      process_option("init-file", report, p);
    if (const char * p = std::getenv("PRICE_HIST"))
      process_option("price-db", report, p);
    if (const char * p = std::getenv("PRICE_EXP"))
      process_option("price-exp", report, p);
#endif

    TRACE_FINISH(environment, 1);

    // Read the initialization file

    TRACE_START(init, 1, "Read initialization file");

    session.read_init();

    TRACE_FINISH(init, 1);

    // Handle the command-line arguments

    TRACE_START(arguments, 1, "Processing command-line arguments");

    strings_list args;
    process_arguments(argc - 1, argv + 1, report, args);

    if (args.empty()) {
      ledger::help(std::cout);
      return 1;
    }
    strings_list::iterator arg = args.begin();

    if (! session.cache_file)
      session.use_cache = false;
    else if (session.use_cache)
      session.use_cache = ! session.data_file.empty() && session.price_db;

    DEBUG("ledger.session.cache", "1. use_cache = " << session.use_cache);

    if (session.data_file == *session.cache_file)
      session.use_cache = false;

    DEBUG("ledger.session.cache", "2. use_cache = " << session.use_cache);

    INFO("Initialization file is " << session.init_file->string());
    INFO("Price database is " << session.price_db->string());
    INFO("Binary cache is " << session.cache_file->string());
    INFO("Journal file is " << session.data_file.string());

    if (! session.use_cache)
      INFO("Binary cache mechanism will not be used");

    TRACE_FINISH(arguments, 1);

    // Configure the output stream

#ifdef HAVE_UNIX_PIPES
    int status, pfd[2];		// Pipe file descriptors
#endif
    report.output_stream = &std::cout;

    if (report.output_file) {
      report.output_stream = new ofstream(*report.output_file);
    }
#ifdef HAVE_UNIX_PIPES
    else if (report.pager) {
      status = pipe(pfd);
      if (status == -1)
	throw_(std::logic_error, "Failed to create pipe");

      status = fork();
      if (status < 0) {
	throw_(std::logic_error, "Failed to fork child process");
      }
      else if (status == 0) {	// child
	// Duplicate pipe's reading end into stdin
	status = dup2(pfd[0], STDIN_FILENO);
	if (status == -1)
	  perror("dup2");

	// Close unuseful file descriptors: the pipe's writing and
	// reading ends (the latter is not needed anymore, after the
	// duplication).
	close(pfd[1]);
	close(pfd[0]);

	// Find command name: its the substring starting right of the
	// rightmost '/' character in the pager pathname.  See manpage
	// for strrchr.
	execlp(report.pager->native_file_string().c_str(),
	       basename(*report.pager).c_str(), NULL);
	perror("execl");
	exit(1);
      }
      else {			// parent
	close(pfd[0]);
	report.output_stream = new boost::fdostream(pfd[1]);
      }
    }
#endif

    // Read the command word and see if it's any of the debugging commands
    // that Ledger supports.

    std::ostream& out(*report.output_stream);

    string verb = *arg++;

    if (verb == "parse") {
      out << "--- Input text ---" << std::endl;
      out << *arg << std::endl;

      out << std::endl << "--- Text as parsed ---" << std::endl;
      expr_t expr(*arg);
      expr.print(out);
      out << std::endl;

      out << std::endl << "--- Expression tree ---" << std::endl;
      expr.dump(out);

      out << std::endl << "--- Calculated value ---" << std::endl;
      expr.calc(report).print(out);
      out << std::endl;

      return 0;
    }
    else if (verb == "compile") {
      out << "--- Input text ---" << std::endl;
      out << *arg << std::endl;

      out << std::endl << "--- Text as parsed ---" << std::endl;
      expr_t expr(*arg);
      expr.print(out);
      out << std::endl;

      out << std::endl << "--- Expression tree ---" << std::endl;
      expr.dump(out);

      expr.compile(report);

      out << std::endl << "--- Compiled tree ---" << std::endl;
      expr.dump(out);

      out << std::endl << "--- Calculated value ---" << std::endl;
      expr.calc(report).print(out);
      out << std::endl;

      return 0;
    }
    else if (verb == "eval") {
      expr_t expr(*arg);
      out << expr.calc(report).strip_annotations() << std::endl;
      return 0;
    }
    else if (verb == "format") {
{'fraction_non_alphanumeric': 0.14058893474155418, 'fraction_numerical': 0.005036021542981045, 'mean_word_length': 3.0758266818700113, 'pattern_counts': {'":': 0, '<': 179, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1294168', 'n_tokens_mistral': 4613, 'n_tokens_neox': 4325, 'n_words': 1367}