<commit_before>/*
 * psql - the PostgreSQL interactive terminal
 *
 * Copyright 2000 by PostgreSQL Global Development Group
 *
 * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.30 2000/05/12 16:13:44 petere Exp $
 */
#include "postgres.h"
#include "mainloop.h"

#include "pqexpbuffer.h"

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

#ifndef WIN32
#include <setjmp.h>

sigjmp_buf	main_loop_jmp;

#endif


/*
 * 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.
 *
 * FIXME: rewrite this whole thing with flex
 */
int
MainLoop(FILE *source)
{
	PQExpBuffer query_buf;		/* buffer for query being accumulated */
	PQExpBuffer previous_buf;	/* if there isn't anything in the new
								 * buffer yet, use this one for \e, etc. */
	char	   *line;			/* current line of input */
	int			len;			/* length of the line */
	volatile int successResult = EXIT_SUCCESS;
	volatile backslashResult slashCmdStatus;

	bool		success;
	volatile char in_quote;		/* == 0 for no in_quote */
	volatile bool xcomment;		/* in extended comment */
	volatile int paren_level;
	unsigned int query_start;
	volatile int count_eof = 0;
	const char *var;
	volatile unsigned int bslash_count = 0;

	int			i,
				prevlen,
				thislen;

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

	unsigned int prev_lineno;
	volatile bool die_on_error = false;


	/* 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();
	previous_buf = createPQExpBuffer();
	if (!query_buf || !previous_buf)
	{
		psql_error("out of memory\n");
		exit(EXIT_FAILURE);
	}

	xcomment = false;
	in_quote = 0;
	paren_level = 0;
	slashCmdStatus = CMD_UNKNOWN;		/* set default */
	prev_lineno = pset.lineno;
	pset.lineno = 0;


	/* main loop to get queries and execute them */
	while (1)
	{
#ifndef WIN32

		/*
		 * Welcome code for Control-C
		 */
		if (cancel_pressed)
		{
			if (!pset.cur_cmd_interactive)
			{

				/*
				 * You get here if you stopped a script with Ctrl-C and a
				 * query cancel was issued. In that case we don't do the
				 * longjmp, so the query routine can finish nicely.
				 */
				successResult = EXIT_USER;
				break;
			}

			cancel_pressed = false;
		}

		if (sigsetjmp(main_loop_jmp, 1) != 0)
		{
			/* got here with longjmp */

			if (pset.cur_cmd_interactive)
			{
				fputc('\n', stdout);
				resetPQExpBuffer(query_buf);

				/* reset parsing state */
				xcomment = false;
				in_quote = 0;
				paren_level = 0;
				count_eof = 0;
				slashCmdStatus = CMD_UNKNOWN;
			}
			else
			{
				successResult = EXIT_USER;
				break;
			}
		}
#endif	 /* not WIN32 */

		if (slashCmdStatus == CMD_NEWEDIT)
		{

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

			/*
			 * 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)
					prompt_status = PROMPT_COMMENT;
				else if (paren_level)
					prompt_status = PROMPT_PAREN;
				else if (query_buf->len > 0)
					prompt_status = PROMPT_CONTINUE;
				else
					prompt_status = PROMPT_READY;

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


		/* Setting this will not have effect until next line. */
		die_on_error = GetVariableBool(pset.vars, "ON_ERROR_STOP");

		/*
		 * 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)
		{
			if (pset.cur_cmd_interactive)
			{
				bool		getout = true;

				/* This tries to mimic bash's IGNOREEOF feature. */
				const char *val = GetVariable(pset.vars, "IGNOREEOF");

				if (val)
				{
					long int	maxeof;
					char	   *endptr;

					if (*val == '\0')
						maxeof = 10;
					else
					{
						maxeof = strtol(val, &endptr, 0);
						if (*endptr != '\0')	/* string not valid as a
												 * number */
							maxeof = 10;
					}

					if (count_eof++ != maxeof)
						getout = false; /* not quite there yet */
				}

				if (getout)
				{
					if (QUIET())
						putc('\n', stdout);
					else
						puts("\\q");
					break;
				}
				else
				{
					if (!QUIET())
						printf("Use \"\\q\" to leave %s.\n", pset.progname);
					continue;
				}
			}
			else
/* not interactive */
				break;
		}
		else
			count_eof = 0;

		pset.lineno++;

		/* strip trailing backslashes, they don't have a clear meaning */
		while (1)
		{
			char	   *cp = strrchr(line, '\\');

			if (cp && (*(cp + 1) == '\0'))
				*cp = '\0';
			else
				break;
		}

		/* nothing left on line? then ignore */
		if (line[0] == '\0')
		{
			free(line);
			continue;
		}

		/* echo back if flag is set */
		var = GetVariable(pset.vars, "ECHO");
		if (!pset.cur_cmd_interactive && var && strcmp(var, "all") == 0)
			puts(line);
		fflush(stdout);

		len = strlen(line);
		query_start = 0;

		/*
		 * Parse line, looking for command separators.
		 *
		 * The current character is at line[i], the prior character at line[i
		 * - prevlen], the next character at line[i + thislen].
		 */
#define ADVANCE_1 (prevlen = thislen, i += thislen, thislen = PQmblen(line+i, pset.encoding))

		success = true;
		for (i = 0, prevlen = 0, thislen = (len > 0) ? PQmblen(line, pset.encoding) : 0;
			 i < len;
			 ADVANCE_1)
		{
			/* was the previous character a backslash? */
			bool		was_bslash = (i > 0 && line[i - prevlen] == '\\');

			if (was_bslash)
				bslash_count++;
			else
				bslash_count = 0;

		rescan:
			/* in quote? */
			if (in_quote)
			{
				/* end of quote */
				if (line[i] == in_quote && bslash_count % 2 == 0)
					in_quote = '\0';
			}

			/* start of quote */
			else if (!was_bslash && (line[i] == '\'' || line[i] == '"'))
				in_quote = line[i];

			/* in extended comment? */
			else if (xcomment)
			{
				if (line[i] == '*' && line[i + thislen] == '/')
				{
					xcomment = false;
					ADVANCE_1;
				}
			}

			/* start of extended comment? */
			else if (line[i] == '/' && line[i + thislen] == '*')
			{
				xcomment = true;
				ADVANCE_1;
			}

			/* single-line comment? truncate line */
			else if (line[i] == '-' && line[i + thislen] == '-')
			{
				line[i] = '\0'; /* remove comment */
				break;
			}

			/* count nested parentheses */
			else if (line[i] == '(')
				paren_level++;

			else if (line[i] == ')' && paren_level > 0)<commit_msg>Fix quotes in /* */ comments in psql.<commit_after>/*
 * psql - the PostgreSQL interactive terminal
 *
 * Copyright 2000 by PostgreSQL Global Development Group
 *
 * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.31 2000/06/29 16:27:57 momjian Exp $
 */
#include "postgres.h"
#include "mainloop.h"

#include "pqexpbuffer.h"

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

#ifndef WIN32
#include <setjmp.h>
sigjmp_buf	main_loop_jmp;
#endif


/*
 * 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.
 *
 * FIXME: rewrite this whole thing with flex
 */
int
MainLoop(FILE *source)
{
	PQExpBuffer query_buf;		/* buffer for query being accumulated */
	PQExpBuffer previous_buf;	/* if there isn't anything in the new
								 * buffer yet, use this one for \e, etc. */
	char	   *line;			/* current line of input */
	int			len;			/* length of the line */
	volatile int successResult = EXIT_SUCCESS;
	volatile backslashResult slashCmdStatus;

	bool		success;
	volatile char in_quote;		/* == 0 for no in_quote */
	volatile bool xcomment;		/* in extended comment */
	volatile int paren_level;
	unsigned int query_start;
	volatile int count_eof = 0;
	const char *var;
	volatile unsigned int bslash_count = 0;

	int			i,
				prevlen,
				thislen;

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

	unsigned int prev_lineno;
	volatile bool die_on_error = false;


	/* 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();
	previous_buf = createPQExpBuffer();
	if (!query_buf || !previous_buf)
	{
		psql_error("out of memory\n");
		exit(EXIT_FAILURE);
	}

	xcomment = false;
	in_quote = 0;
	paren_level = 0;
	slashCmdStatus = CMD_UNKNOWN;		/* set default */
	prev_lineno = pset.lineno;
	pset.lineno = 0;


	/* main loop to get queries and execute them */
	while (1)
	{
#ifndef WIN32

		/*
		 * Welcome code for Control-C
		 */
		if (cancel_pressed)
		{
			if (!pset.cur_cmd_interactive)
			{

				/*
				 * You get here if you stopped a script with Ctrl-C and a
				 * query cancel was issued. In that case we don't do the
				 * longjmp, so the query routine can finish nicely.
				 */
				successResult = EXIT_USER;
				break;
			}

			cancel_pressed = false;
		}

		if (sigsetjmp(main_loop_jmp, 1) != 0)
		{
			/* got here with longjmp */

			if (pset.cur_cmd_interactive)
			{
				fputc('\n', stdout);
				resetPQExpBuffer(query_buf);

				/* reset parsing state */
				xcomment = false;
				in_quote = 0;
				paren_level = 0;
				count_eof = 0;
				slashCmdStatus = CMD_UNKNOWN;
			}
			else
			{
				successResult = EXIT_USER;
				break;
			}
		}
#endif	 /* not WIN32 */

		if (slashCmdStatus == CMD_NEWEDIT)
		{

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

			/*
			 * 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)
					prompt_status = PROMPT_COMMENT;
				else if (paren_level)
					prompt_status = PROMPT_PAREN;
				else if (query_buf->len > 0)
					prompt_status = PROMPT_CONTINUE;
				else
					prompt_status = PROMPT_READY;

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


		/* Setting this will not have effect until next line. */
		die_on_error = GetVariableBool(pset.vars, "ON_ERROR_STOP");

		/*
		 * 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)
		{
			if (pset.cur_cmd_interactive)
			{
				bool		getout = true;

				/* This tries to mimic bash's IGNOREEOF feature. */
				const char *val = GetVariable(pset.vars, "IGNOREEOF");

				if (val)
				{
					long int	maxeof;
					char	   *endptr;

					if (*val == '\0')
						maxeof = 10;
					else
					{
						maxeof = strtol(val, &endptr, 0);
						if (*endptr != '\0')	/* string not valid as a
												 * number */
							maxeof = 10;
					}

					if (count_eof++ != maxeof)
						getout = false; /* not quite there yet */
				}

				if (getout)
				{
					if (QUIET())
						putc('\n', stdout);
					else
						puts("\\q");
					break;
				}
				else
				{
					if (!QUIET())
						printf("Use \"\\q\" to leave %s.\n", pset.progname);
					continue;
				}
			}
			else
/* not interactive */
				break;
		}
		else
			count_eof = 0;

		pset.lineno++;

		/* strip trailing backslashes, they don't have a clear meaning */
		while (1)
		{
			char	   *cp = strrchr(line, '\\');

			if (cp && (*(cp + 1) == '\0'))
				*cp = '\0';
			else
				break;
		}

		/* nothing left on line? then ignore */
		if (line[0] == '\0')
		{
			free(line);
			continue;
		}

		/* echo back if flag is set */
		var = GetVariable(pset.vars, "ECHO");
		if (!pset.cur_cmd_interactive && var && strcmp(var, "all") == 0)
			puts(line);
		fflush(stdout);

		len = strlen(line);
		query_start = 0;

		/*
		 * Parse line, looking for command separators.
		 *
		 * The current character is at line[i], the prior character at line[i
		 * - prevlen], the next character at line[i + thislen].
		 */
#define ADVANCE_1 (prevlen = thislen, i += thislen, thislen = PQmblen(line+i, pset.encoding))

		success = true;
		for (i = 0, prevlen = 0, thislen = (len > 0) ? PQmblen(line, pset.encoding) : 0;
			 i < len;
			 ADVANCE_1)
		{
			/* was the previous character a backslash? */
			bool		was_bslash = (i > 0 && line[i - prevlen] == '\\');

			if (was_bslash)
				bslash_count++;
			else
				bslash_count = 0;

		rescan:
			/* start of extended comment? */
			if (line[i] == '/' && line[i + thislen] == '*')
			{
				xcomment = true;
				ADVANCE_1;
			}

			/* in extended comment? */
			else if (xcomment)
			{
				if (line[i] == '*' && line[i + thislen] == '/')
				{
					xcomment = false;
					ADVANCE_1;
				}
			}

			/* single-line comment? truncate line */
			else if (line[i] == '-' && line[i + thislen] == '-')
			{
				line[i] = '\0'; /* remove comment */
				break;
			}

			/* in quote? */
			else if (in_quote)
			{
				/* end of quote */
				if (line[i] == in_quote && bslash_count % 2 == 0)
					in_quote = '\0';
			}

			/* start of quote */
			else if (!was_bslash &&
					 (line[i] == '\'' || line[i] == '"'))
				in_quote = line[i];

			/* count nested parentheses */
			else if (line[i] == '(')
				paren_level++;

			else if (line[i] == ')' && paren_level > 0)
{'fraction_non_alphanumeric': 0.13289195276772917, 'fraction_numerical': 0.009965190089413692, 'mean_word_length': 2.5094610778443114, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6852274', 'n_tokens_mistral': 6578, 'n_tokens_neox': 5223, 'n_words': 1466}