<commit_before>/*
 * SchemeShell.c
 *
 * Simple scheme shell
 * Copyright (c) 2008 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_GUILE

#include <opencog/server/ConsoleSocket.h>
#include <opencog/util/Logger.h>
#include <opencog/util/platform.h>

#include "SchemeShell.h"

using namespace opencog;

// Some random RFC 854 characters
#define IAC 0xff  // Telnet Interpret As Command
#define IP 0xf4   // Telnet IP Interrupt Process
#define AO 0xf5   // Telnet AO Abort Output
#define EL 0xf8   // Telnet EL Erase Line
#define WILL 0xfb // Telnet WILL
#define DO 0xfd   // Telnet DO
#define TIMING_MARK 0x6 // Telnet RFC 860 timing mark

DECLARE_MODULE(SchemeShell);

SchemeShell::SchemeShell(void)
{
	show_output = true;
	normal_prompt = "guile> ";
	pending_prompt = "... ";
	abort_prompt = "asdf";
	abort_prompt[0] = IAC;
	abort_prompt[1] = WILL;
	abort_prompt[2] = TIMING_MARK;
	abort_prompt[3] = '\n';
	abort_prompt += normal_prompt;
	evaluator = NULL;
	holder = NULL;
}

void SchemeShell::init(void)
{
	if (!evaluator) evaluator = new SchemeEval();
	shellout_register();
}

SchemeShell::~SchemeShell()
{
	if (holder)
	{
		holder->SetShell(NULL);
		holder->AtomicInc(-1);
		holder = NULL;
	}
	shellout_unregister();
	if (evaluator) delete evaluator;
}

/**
 * Register this shell with the console.
 */
std::string SchemeShell::shellout(Request *req, std::list<std::string> args)
{
	SocketHolder *h = req->getSocketHolder();
	if (holder)
	{
		holder->SetShell(NULL);
		holder->AtomicInc(-1);
	}

	holder = h;
	holder->AtomicInc(+1);
	holder->SetShell(this);

	return "Entering scheme shell; use ^D or a single . on a "
	       "line by itself to exit.";
}

/* ============================================================== */

void SchemeShell::hush_output(bool hush)
{
	show_output = !hush;
}

const std::string& SchemeShell::get_prompt(void)
{
	// Use different prompts, depending on whether there is pending
	// input or not.
	if (evaluator->input_pending())
	{
		return pending_prompt;
	}
	else
	{
		return normal_prompt;
	}
}

/* ============================================================== */

void SchemeShell::eval(const std::string &expr, SocketHolder *h)
{<commit_msg>Defer initialization till later to avoid some sort of ongoing nutiness.<commit_after>/*
 * SchemeShell.c
 *
 * Simple scheme shell
 * Copyright (c) 2008 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_GUILE

#include <opencog/server/ConsoleSocket.h>
#include <opencog/util/Logger.h>
#include <opencog/util/platform.h>

#include "SchemeShell.h"

using namespace opencog;

// Some random RFC 854 characters
#define IAC 0xff  // Telnet Interpret As Command
#define IP 0xf4   // Telnet IP Interrupt Process
#define AO 0xf5   // Telnet AO Abort Output
#define EL 0xf8   // Telnet EL Erase Line
#define WILL 0xfb // Telnet WILL
#define DO 0xfd   // Telnet DO
#define TIMING_MARK 0x6 // Telnet RFC 860 timing mark

DECLARE_MODULE(SchemeShell);

SchemeShell::SchemeShell(void)
{
	show_output = true;
	normal_prompt = "guile> ";
	pending_prompt = "... ";
	abort_prompt = "asdf";
	abort_prompt[0] = IAC;
	abort_prompt[1] = WILL;
	abort_prompt[2] = TIMING_MARK;
	abort_prompt[3] = '\n';
	abort_prompt += normal_prompt;
	evaluator = NULL;
	holder = NULL;
}

void SchemeShell::init(void)
{
	shellout_register();
}

SchemeShell::~SchemeShell()
{
	if (holder)
	{
		holder->SetShell(NULL);
		holder->AtomicInc(-1);
		holder = NULL;
	}
	shellout_unregister();
	if (evaluator) delete evaluator;
}

/**
 * Register this shell with the console.
 */
std::string SchemeShell::shellout(Request *req, std::list<std::string> args)
{
	SocketHolder *h = req->getSocketHolder();
	if (holder)
	{
		holder->SetShell(NULL);
		holder->AtomicInc(-1);
	}

	holder = h;
	holder->AtomicInc(+1);
	holder->SetShell(this);

	if (!evaluator) evaluator = new SchemeEval();
	return "Entering scheme shell; use ^D or a single . on a "
	       "line by itself to exit.";
}

/* ============================================================== */

void SchemeShell::hush_output(bool hush)
{
	show_output = !hush;
}

const std::string& SchemeShell::get_prompt(void)
{
	// Use different prompts, depending on whether there is pending
	// input or not.
	if (evaluator->input_pending())
	{
		return pending_prompt;
	}
	else
	{
		return normal_prompt;
	}
}

/* ============================================================== */

void SchemeShell::eval(const std::string &expr, SocketHolder *h)
{
{'fraction_non_alphanumeric': 0.15357020840450974, 'fraction_numerical': 0.013665869490946362, 'mean_word_length': 4.265287769784172, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6247114', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1850, 'n_words': 602}