<commit_before>#include <config.h>
#include <c.h>
#include "mainloop.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pqexpbuffer.h>

#include "settings.h"
#include "prompt.h"
#include "input.h"
#include "common.h"
#include "command.h"



/* MainLoop()
 * Main processing loop for reading lines of input
 *	and sending them to the backend.
 *
 * This loop is re-entrant. May be called by \i command
 *	which reads input from a file.
 */
int
MainLoop(PsqlSettings *pset, FILE *source)
{
	PQExpBuffer query_buf;		/* buffer for query being accumulated */
	char	   *line;			/* current line of input */
	char	   *xcomment;		/* start of extended comment */
	int			len;			/* length of the line */
	int			successResult = EXIT_SUCCESS;
	backslashResult slashCmdStatus;

	bool		eof = false;	/* end of our command input? */
	bool		success;
	char		in_quote;		/* == 0 for no in_quote */
	bool		was_bslash;		/* backslash */
	int			paren_level;
	unsigned int query_start;

	int			i,
				prevlen,
				thislen;

	/* Save the prior command source */
	FILE	   *prev_cmd_source;
	bool		prev_cmd_interactive;

	bool		die_on_error;
	const char *interpol_char;


	/* Save old settings */
	prev_cmd_source = pset->cur_cmd_source;
	prev_cmd_interactive = pset->cur_cmd_interactive;

	/* Establish new source */
	pset->cur_cmd_source = source;
	pset->cur_cmd_interactive = ((source == stdin) && !pset->notty);


	query_buf = createPQExpBuffer();
	if (!query_buf)
	{
		perror("createPQExpBuffer");
		exit(EXIT_FAILURE);
	}

	xcomment = NULL;
	in_quote = 0;
	paren_level = 0;
	slashCmdStatus = CMD_UNKNOWN;		/* set default */


	/* main loop to get queries and execute them */
	while (!eof)
	{
		if (slashCmdStatus == CMD_NEWEDIT)
		{

			/*
			 * just returned from editing the line? then just copy to the
			 * input buffer
			 */
			line = strdup(query_buf->data);
			resetPQExpBuffer(query_buf);
			/* reset parsing state since we are rescanning whole query */
			xcomment = NULL;
			in_quote = 0;
			paren_level = 0;
		}
		else
		{

			/*
			 * otherwise, set interactive prompt if necessary and get
			 * another line
			 */
			if (pset->cur_cmd_interactive)
			{
				int			prompt_status;

				if (in_quote && in_quote == '\'')
					prompt_status = PROMPT_SINGLEQUOTE;
				else if (in_quote && in_quote == '"')
					prompt_status = PROMPT_DOUBLEQUOTE;
				else if (xcomment != NULL)
					prompt_status = PROMPT_COMMENT;
				else if (query_buf->len > 0)
					prompt_status = PROMPT_CONTINUE;
				else
					prompt_status = PROMPT_READY;

				line = gets_interactive(get_prompt(pset, prompt_status));
			}
			else
				line = gets_fromFile(source);
		}


		/* Setting these will not have effect until next line */
		die_on_error = GetVariableBool(pset->vars, "die_on_error");
		interpol_char = GetVariable(pset->vars, "sql_interpol");;


		/*
		 * query_buf holds query already accumulated.  line is the
		 * malloc'd new line of input (note it must be freed before
		 * looping around!) query_start is the next command start location
		 * within the line.
		 */

		/* No more input.  Time to quit, or \i done */
		if (line == NULL || (!pset->cur_cmd_interactive && *line == '\0'))
		{
			if (GetVariableBool(pset->vars, "echo") && !GetVariableBool(pset->vars, "quiet"))
				puts("EOF");
			eof = true;
			continue;
		}<commit_msg>Fix EOF/control-D display in psql.<commit_after>#include <config.h>
#include <c.h>
#include "mainloop.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <pqexpbuffer.h>

#include "settings.h"
#include "prompt.h"
#include "input.h"
#include "common.h"
#include "command.h"



/* MainLoop()
 * Main processing loop for reading lines of input
 *	and sending them to the backend.
 *
 * This loop is re-entrant. May be called by \i command
 *	which reads input from a file.
 */
int
MainLoop(PsqlSettings *pset, FILE *source)
{
	PQExpBuffer query_buf;		/* buffer for query being accumulated */
	char	   *line;			/* current line of input */
	char	   *xcomment;		/* start of extended comment */
	int			len;			/* length of the line */
	int			successResult = EXIT_SUCCESS;
	backslashResult slashCmdStatus;

	bool		eof = false;	/* end of our command input? */
	bool		success;
	char		in_quote;		/* == 0 for no in_quote */
	bool		was_bslash;		/* backslash */
	int			paren_level;
	unsigned int query_start;

	int			i,
				prevlen,
				thislen;

	/* Save the prior command source */
	FILE	   *prev_cmd_source;
	bool		prev_cmd_interactive;

	bool		die_on_error;
	const char *interpol_char;


	/* Save old settings */
	prev_cmd_source = pset->cur_cmd_source;
	prev_cmd_interactive = pset->cur_cmd_interactive;

	/* Establish new source */
	pset->cur_cmd_source = source;
	pset->cur_cmd_interactive = ((source == stdin) && !pset->notty);


	query_buf = createPQExpBuffer();
	if (!query_buf)
	{
		perror("createPQExpBuffer");
		exit(EXIT_FAILURE);
	}

	xcomment = NULL;
	in_quote = 0;
	paren_level = 0;
	slashCmdStatus = CMD_UNKNOWN;		/* set default */


	/* main loop to get queries and execute them */
	while (!eof)
	{
		if (slashCmdStatus == CMD_NEWEDIT)
		{

			/*
			 * just returned from editing the line? then just copy to the
			 * input buffer
			 */
			line = strdup(query_buf->data);
			resetPQExpBuffer(query_buf);
			/* reset parsing state since we are rescanning whole query */
			xcomment = NULL;
			in_quote = 0;
			paren_level = 0;
		}
		else
		{

			/*
			 * otherwise, set interactive prompt if necessary and get
			 * another line
			 */
			if (pset->cur_cmd_interactive)
			{
				int			prompt_status;

				if (in_quote && in_quote == '\'')
					prompt_status = PROMPT_SINGLEQUOTE;
				else if (in_quote && in_quote == '"')
					prompt_status = PROMPT_DOUBLEQUOTE;
				else if (xcomment != NULL)
					prompt_status = PROMPT_COMMENT;
				else if (query_buf->len > 0)
					prompt_status = PROMPT_CONTINUE;
				else
					prompt_status = PROMPT_READY;

				line = gets_interactive(get_prompt(pset, prompt_status));
			}
			else
				line = gets_fromFile(source);
		}


		/* Setting these will not have effect until next line */
		die_on_error = GetVariableBool(pset->vars, "die_on_error");
		interpol_char = GetVariable(pset->vars, "sql_interpol");;


		/*
		 * query_buf holds query already accumulated.  line is the
		 * malloc'd new line of input (note it must be freed before
		 * looping around!) query_start is the next command start location
		 * within the line.
		 */

		/* No more input.  Time to quit, or \i done */
		if (line == NULL || (!pset->cur_cmd_interactive && *line == '\0'))
		{
			if (GetVariableBool(pset->vars, "echo") && !GetVariableBool(pset->vars, "quiet"))
				puts("EOF\n");
			else
				puts(""); /* put out newline */
			eof = true;
			continue;
		}
{'fraction_non_alphanumeric': 0.11907239482681731, 'fraction_numerical': 0.002081165452653486, 'mean_word_length': 3.1814791796146675, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, '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': '2230522', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2338, 'n_words': 659}