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

#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#ifdef WIN32
#include <io.h>
#include <window.h>
#else
#include <unistd.h>
#endif

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include <libpq-fe.h>
#include <pqsignal.h>
#include <version.h>

#include "settings.h"
#include "command.h"
#include "help.h"
#include "mainloop.h"
#include "common.h"
#include "input.h"
#include "variables.h"
#include "print.h"
#include "describe.h"



static void
			process_psqlrc(PsqlSettings *pset);

static void
			showVersion(PsqlSettings *pset, bool verbose);


/* Structures to pass information between the option parsing routine
 * and the main function
 */
enum _actions
{
	ACT_NOTHING = 0,
	ACT_SINGLE_SLASH,
	ACT_LIST_DB,
	ACT_SHOW_VER,
	ACT_SINGLE_QUERY,
	ACT_FILE
};

struct adhoc_opts
{
	char	   *dbname;
	char	   *host;
	char	   *port;
	char	   *username;
	enum _actions action;
	char	   *action_string;
	bool		no_readline;
};

static void
			parse_options(int argc, char *argv[], PsqlSettings *pset, struct adhoc_opts * options);



/*
 *
 * main()
 *
 */
int
main(int argc, char **argv)
{
	PsqlSettings settings;
	struct adhoc_opts options;
	int			successResult;

	char	   *username = NULL;
	char	   *password = <PASSWORD>;
	bool		need_pass;

	MemSet(&settings, 0, sizeof settings);

	settings.cur_cmd_source = stdin;
	settings.cur_cmd_interactive = false;

	settings.vars = CreateVariableSpace();
	settings.popt.topt.format = PRINT_ALIGNED;
	settings.queryFout = stdout;
	settings.popt.topt.fieldSep = strdup(DEFAULT_FIELD_SEP);
	settings.popt.topt.border = 1;

	SetVariable(settings.vars, "prompt1", DEFAULT_PROMPT1);
	SetVariable(settings.vars, "prompt2", DEFAULT_PROMPT2);
	SetVariable(settings.vars, "prompt3", DEFAULT_PROMPT3);

	settings.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));

	/* This is obsolete and will be removed very soon. */
#ifdef PSQL_ALWAYS_GET_PASSWORDS
	settings.getPassword = true;
#else
	settings.getPassword = false;
#endif

#ifdef MULTIBYTE
	settings.has_client_encoding = (getenv("PGCLIENTENCODING") != NULL);
#endif

	parse_options(argc, argv, &settings, &options);

	if (options.action == ACT_LIST_DB || options.action == ACT_SHOW_VER)
		options.dbname = "template1";

	if (options.username)
	{
		if (strcmp(options.username, "?") == 0)
			username = simple_prompt("Username: ", 100, true);
		else
			username = strdup(options.username);
	}

	if (settings.getPassword)
		password = simple_prompt("Password: ", 100, false);

	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
		settings.db = PQsetdbLogin(options.host, options.port, NULL, NULL, options.dbname, username, password);

		if (PQstatus(settings.db) == CONNECTION_BAD &&
			strcmp(PQerrorMessage(settings.db), "fe_sendauth: no password supplied\n") == 0)
		{
			need_pass = true;
			free(password);
			password = <PASSWORD>;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);

	free(username);
	free(password);

	if (PQstatus(settings.db) == CONNECTION_BAD)
	{
		fprintf(stderr, "Connection to database '%s' failed.\n%s\n", PQdb(settings.db), PQerrorMessage(settings.db));
		PQfinish(settings.db);
		exit(EXIT_BADCONN);
	}

	if (options.action == ACT_LIST_DB)
	{
		int			success = listAllDbs(&settings);

		PQfinish(settings.db);
		exit(!success);
	}

	if (options.action == ACT_SHOW_VER)
	{
		showVersion(&settings, true);
		PQfinish(settings.db);
		exit(EXIT_SUCCESS);
	}


	if (!GetVariable(settings.vars, "quiet") && !settings.notty && !options.action)
	{
		puts("Welcome to psql, the PostgreSQL interactive terminal.\n"
			 "(Please type \\copyright to see the distribution terms of PostgreSQL.)");

		//showVersion(&settings, false);

		puts("\n"
			 "Type \\h for help with SQL commands,\n"
			 "     \\? for help on internal slash commands,\n"
			 "     \\q to quit,\n"
			 "     \\g or terminate with semicolon to execute query.");
	}

	process_psqlrc(&settings);

	initializeInput(options.no_readline ? 0 : 1);

	/* Now find something to do */
<commit_msg>clean up startup banner<commit_after>#include <config.h>
#include <c.h>

#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

#ifdef WIN32
#include <io.h>
#include <window.h>
#else
#include <unistd.h>
#endif

#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif

#include <libpq-fe.h>
#include <pqsignal.h>
#include <version.h>

#include "settings.h"
#include "command.h"
#include "help.h"
#include "mainloop.h"
#include "common.h"
#include "input.h"
#include "variables.h"
#include "print.h"
#include "describe.h"



static void
			process_psqlrc(PsqlSettings *pset);

static void
			showVersion(PsqlSettings *pset, bool verbose);


/* Structures to pass information between the option parsing routine
 * and the main function
 */
enum _actions
{
	ACT_NOTHING = 0,
	ACT_SINGLE_SLASH,
	ACT_LIST_DB,
	ACT_SHOW_VER,
	ACT_SINGLE_QUERY,
	ACT_FILE
};

struct adhoc_opts
{
	char	   *dbname;
	char	   *host;
	char	   *port;
	char	   *username;
	enum _actions action;
	char	   *action_string;
	bool		no_readline;
};

static void
			parse_options(int argc, char *argv[], PsqlSettings *pset, struct adhoc_opts * options);



/*
 *
 * main()
 *
 */
int
main(int argc, char **argv)
{
	PsqlSettings settings;
	struct adhoc_opts options;
	int			successResult;

	char	   *username = NULL;
	char	   *password = NULL;
	bool		need_pass;

	MemSet(&settings, 0, sizeof settings);

	settings.cur_cmd_source = stdin;
	settings.cur_cmd_interactive = false;

	settings.vars = CreateVariableSpace();
	settings.popt.topt.format = PRINT_ALIGNED;
	settings.queryFout = stdout;
	settings.popt.topt.fieldSep = strdup(DEFAULT_FIELD_SEP);
	settings.popt.topt.border = 1;

	SetVariable(settings.vars, "prompt1", DEFAULT_PROMPT1);
	SetVariable(settings.vars, "prompt2", DEFAULT_PROMPT2);
	SetVariable(settings.vars, "prompt3", DEFAULT_PROMPT3);

	settings.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));

	/* This is obsolete and will be removed very soon. */
#ifdef PSQL_ALWAYS_GET_PASSWORDS
	settings.getPassword = true;
#else
	settings.getPassword = false;
#endif

#ifdef MULTIBYTE
	settings.has_client_encoding = (getenv("PGCLIENTENCODING") != NULL);
#endif

	parse_options(argc, argv, &settings, &options);

	if (options.action == ACT_LIST_DB || options.action == ACT_SHOW_VER)
		options.dbname = "template1";

	if (options.username)
	{
		if (strcmp(options.username, "?") == 0)
			username = simple_prompt("Username: ", 100, true);
		else
			username = strdup(options.username);
	}

	if (settings.getPassword)
		password = simple_prompt("Password: ", 100, false);

	/* loop until we have a password if requested by backend */
	do
	{
		need_pass = false;
		settings.db = PQsetdbLogin(options.host, options.port, NULL, NULL, options.dbname, username, password);

		if (PQstatus(settings.db) == CONNECTION_BAD &&
			strcmp(PQerrorMessage(settings.db), "fe_sendauth: no password supplied\n") == 0)
		{
			need_pass = true;
			free(password);
			password = <PASSWORD>;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);

	free(username);
	free(password);

	if (PQstatus(settings.db) == CONNECTION_BAD)
	{
		fprintf(stderr, "Connection to database '%s' failed.\n%s\n", PQdb(settings.db), PQerrorMessage(settings.db));
		PQfinish(settings.db);
		exit(EXIT_BADCONN);
	}

	if (options.action == ACT_LIST_DB)
	{
		int			success = listAllDbs(&settings);

		PQfinish(settings.db);
		exit(!success);
	}

	if (options.action == ACT_SHOW_VER)
	{
		showVersion(&settings, true);
		PQfinish(settings.db);
		exit(EXIT_SUCCESS);
	}


	if (!GetVariable(settings.vars, "quiet") && !settings.notty && !options.action)
	{
		puts("Welcome to psql, the PostgreSQL interactive terminal.\n"
			 "(Type \\copyright to see the distribution terms.)");

		//showVersion(&settings, false);

		puts("Type \\h for help with SQL commands,\n"
			 "     \\? for help on internal slash commands,\n"
			 "     \\g or terminate with semicolon to execute query."
			 "     \\q to quit,\n");
	}

	process_psqlrc(&settings);

	initializeInput(options.no_readline ? 0 : 1);

	/* Now find something to do */

{'fraction_non_alphanumeric': 0.14047904191616767, 'fraction_numerical': 0.005988023952095809, 'mean_word_length': 4.377334191886671, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, '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': '1742386', 'n_tokens_mistral': 3241, 'n_tokens_neox': 2984, 'n_words': 697}