<commit_before>/*-------------------------------------------------------------------------
 *
 * psql.c--
 *    an interactive front-end to postgreSQL
 *
 * Copyright (c) 1996, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.55 1997/01/26 17:27:32 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>	/* for MAXPATHLEN */
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "postgres.h"
#include "libpq-fe.h"
#include "pqsignal.h"
#include "stringutils.h"
#include "psqlHelp.h"
#ifdef NEED_STRDUP
#include "strdup.h"
#endif

#ifdef HAVE_LIBREADLINE
# ifdef HAVE_READLINE_H
#  include <readline.h>
#  if defined(HAVE_HISTORY) || defined(HAVE_LIBHISTORY)
#   include <history.h>
#  endif
# else
#  include <readline/readline.h>
#  if defined(HAVE_HISTORY) || defined(HAVE_LIBHISTORY)
#   include <readline/history.h>
#  endif
# endif
#endif

#define PROMPT "=> "

#define MAX_QUERY_BUFFER 20000

#define COPYBUFSIZ  8192

#define DEFAULT_FIELD_SEP "|"
#define DEFAULT_EDITOR  "vi"
#define DEFAULT_SHELL  "/bin/sh"

typedef struct _psqlSettings {
    PGconn         *db;		/* connection to backend */
    FILE           *queryFout;	/* where to send the query results */
    PQprintOpt      opt;	/* options to be passed to PQprint */
    char           *prompt;	/* prompt to display */
    char           *gfname;	/* one-shot file output argument for \g */
    bool            notty;	/* input or output is not a tty */
    bool            pipe;	/* queryFout is from a popen() */
    bool            echoQuery;	/* echo the query before sending it */
    bool            quiet;	/* run quietly, no messages, no promt */
    bool            singleStep;	/* prompt before for each query */
    bool            singleLineMode;	/* query terminated by newline */
    bool            useReadline;/* use libreadline routines */
}               PsqlSettings;

/* declarations for functions in this file */
static void     usage(char *progname);
static void     slashUsage();
static void     handleCopyOut(PGresult * res, bool quiet, FILE * copystream);
static void
handleCopyIn(PGresult * res, const bool mustprompt,
	     FILE * copystream);
static int      tableList(PsqlSettings * ps, bool deep_tablelist);
static int      tableDesc(PsqlSettings * ps, char *table);

char           *gets_noreadline(char *prompt, FILE * source);
char           *gets_readline(char *prompt, FILE * source);
char           *gets_fromFile(char *prompt, FILE * source);
int             listAllDbs(PsqlSettings * settings);
void
SendQuery(bool * success_p, PsqlSettings * settings, const char *query,
	  const bool copy_in, const bool copy_out, FILE * copystream);
int
HandleSlashCmds(PsqlSettings * settings,
		char *line,
		char *query);
int             MainLoop(PsqlSettings * settings, FILE * source);
/* probably should move this into libpq */
void
PQprint(FILE * fp,
	PGresult * res,
	PQprintOpt * po
);

FILE           *setFout(PsqlSettings * ps, char *fname);

/*
 * usage print out usage for command line arguments
 */

static void
usage(char *progname)
{
    fprintf(stderr, "Usage: %s [options] [dbname]\n", progname);
    fprintf(stderr, "\t -a authsvc              set authentication service\n");
    fprintf(stderr, "\t -A                      turn off alignment when printing out attributes\n");
    fprintf(stderr, "\t -c query                run single query (slash commands too)\n");
    fprintf(stderr, "\t -d dbName               specify database name\n");
    fprintf(stderr, "\t -e                      echo the query sent to the backend\n");
    fprintf(stderr, "\t -f filename             use file as a source of queries\n");
    fprintf(stderr, "\t -F sep                  set the field separator (default is " ")\n");
    fprintf(stderr, "\t -h host                 set database server host\n");
    fprintf(stderr, "\t -H                      turn on html3.0 table output\n");
    fprintf(stderr, "\t -l                      list available databases\n");
    fprintf(stderr, "\t -n                      don't use readline library\n");
    fprintf(stderr, "\t -o filename             send output to filename or (|pipe)\n");
    fprintf(stderr, "\t -p port                 set port number\n");
    fprintf(stderr, "\t -q                      run quietly (no messages, no prompts)\n");
    fprintf(stderr, "\t -s                      single step mode (prompts for each query)\n");
    fprintf(stderr, "\t -S                      single line mode (i.e. query terminated by newline)\n");
    fprintf(stderr, "\t -t                      turn off printing of headings and row count\n");
    fprintf(stderr, "\t -T html                 set html3.0 table command options (cf. -H)\n");
    fprintf(stderr, "\t -x                      turn on expanded output (field names on left)\n");
    exit(1);
}

/*
 * slashUsage print out usage for the backslash commands
 */

static char    *
on(bool f)
{
    return f ? "on" : "off";
}

static void
slashUsage(PsqlSettings * ps)
{
    fprintf(stderr, " \\?           -- help\n");
    fprintf(stderr, " \\a           -- toggle field-alignment (currenty %s)\n", on(ps->opt.align));
    fprintf(stderr, " \\C [<captn>] -- set html3 caption (currently '%s')\n", ps->opt.caption ? ps->opt.caption : "");
    fprintf(stderr, " \\connect <dbname>  -- connect to new database (currently '%s')\n", PQdb(ps->db));
    fprintf(stderr, " \\copy {<table> to <file> | <file> from <table>}\n");
    fprintf(stderr, " \\d [<table>] -- list tables in database or columns in <table>, * for all\n");
    fprintf(stderr, " \\e [<fname>] -- edit the current query buffer or <fname>, \\E execute too\n");
    fprintf(stderr, " \\f [<sep>]   -- change field separater (currently '%s')\n", ps->opt.fieldSep);
    fprintf(stderr, " \\g [<fname>] [|<cmd>] -- send query to backend [and results in <fname> or pipe]\n");
    fprintf(stderr, " \\h [<cmd>]   -- help on syntax of sql commands, * for all commands\n");<commit_msg>Fix \? syntax for copy command.<commit_after>/*-------------------------------------------------------------------------
 *
 * psql.c--
 *    an interactive front-end to postgreSQL
 *
 * Copyright (c) 1996, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.56 1997/02/11 03:11:33 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>	/* for MAXPATHLEN */
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include "postgres.h"
#include "libpq-fe.h"
#include "pqsignal.h"
#include "stringutils.h"
#include "psqlHelp.h"
#ifdef NEED_STRDUP
#include "strdup.h"
#endif

#ifdef HAVE_LIBREADLINE
# ifdef HAVE_READLINE_H
#  include <readline.h>
#  if defined(HAVE_HISTORY) || defined(HAVE_LIBHISTORY)
#   include <history.h>
#  endif
# else
#  include <readline/readline.h>
#  if defined(HAVE_HISTORY) || defined(HAVE_LIBHISTORY)
#   include <readline/history.h>
#  endif
# endif
#endif

#define PROMPT "=> "

#define MAX_QUERY_BUFFER 20000

#define COPYBUFSIZ  8192

#define DEFAULT_FIELD_SEP "|"
#define DEFAULT_EDITOR  "vi"
#define DEFAULT_SHELL  "/bin/sh"

typedef struct _psqlSettings {
    PGconn         *db;		/* connection to backend */
    FILE           *queryFout;	/* where to send the query results */
    PQprintOpt      opt;	/* options to be passed to PQprint */
    char           *prompt;	/* prompt to display */
    char           *gfname;	/* one-shot file output argument for \g */
    bool            notty;	/* input or output is not a tty */
    bool            pipe;	/* queryFout is from a popen() */
    bool            echoQuery;	/* echo the query before sending it */
    bool            quiet;	/* run quietly, no messages, no promt */
    bool            singleStep;	/* prompt before for each query */
    bool            singleLineMode;	/* query terminated by newline */
    bool            useReadline;/* use libreadline routines */
}               PsqlSettings;

/* declarations for functions in this file */
static void     usage(char *progname);
static void     slashUsage();
static void     handleCopyOut(PGresult * res, bool quiet, FILE * copystream);
static void
handleCopyIn(PGresult * res, const bool mustprompt,
	     FILE * copystream);
static int      tableList(PsqlSettings * ps, bool deep_tablelist);
static int      tableDesc(PsqlSettings * ps, char *table);

char           *gets_noreadline(char *prompt, FILE * source);
char           *gets_readline(char *prompt, FILE * source);
char           *gets_fromFile(char *prompt, FILE * source);
int             listAllDbs(PsqlSettings * settings);
void
SendQuery(bool * success_p, PsqlSettings * settings, const char *query,
	  const bool copy_in, const bool copy_out, FILE * copystream);
int
HandleSlashCmds(PsqlSettings * settings,
		char *line,
		char *query);
int             MainLoop(PsqlSettings * settings, FILE * source);
/* probably should move this into libpq */
void
PQprint(FILE * fp,
	PGresult * res,
	PQprintOpt * po
);

FILE           *setFout(PsqlSettings * ps, char *fname);

/*
 * usage print out usage for command line arguments
 */

static void
usage(char *progname)
{
    fprintf(stderr, "Usage: %s [options] [dbname]\n", progname);
    fprintf(stderr, "\t -a authsvc              set authentication service\n");
    fprintf(stderr, "\t -A                      turn off alignment when printing out attributes\n");
    fprintf(stderr, "\t -c query                run single query (slash commands too)\n");
    fprintf(stderr, "\t -d dbName               specify database name\n");
    fprintf(stderr, "\t -e                      echo the query sent to the backend\n");
    fprintf(stderr, "\t -f filename             use file as a source of queries\n");
    fprintf(stderr, "\t -F sep                  set the field separator (default is " ")\n");
    fprintf(stderr, "\t -h host                 set database server host\n");
    fprintf(stderr, "\t -H                      turn on html3.0 table output\n");
    fprintf(stderr, "\t -l                      list available databases\n");
    fprintf(stderr, "\t -n                      don't use readline library\n");
    fprintf(stderr, "\t -o filename             send output to filename or (|pipe)\n");
    fprintf(stderr, "\t -p port                 set port number\n");
    fprintf(stderr, "\t -q                      run quietly (no messages, no prompts)\n");
    fprintf(stderr, "\t -s                      single step mode (prompts for each query)\n");
    fprintf(stderr, "\t -S                      single line mode (i.e. query terminated by newline)\n");
    fprintf(stderr, "\t -t                      turn off printing of headings and row count\n");
    fprintf(stderr, "\t -T html                 set html3.0 table command options (cf. -H)\n");
    fprintf(stderr, "\t -x                      turn on expanded output (field names on left)\n");
    exit(1);
}

/*
 * slashUsage print out usage for the backslash commands
 */

static char    *
on(bool f)
{
    return f ? "on" : "off";
}

static void
slashUsage(PsqlSettings * ps)
{
    fprintf(stderr, " \\?           -- help\n");
    fprintf(stderr, " \\a           -- toggle field-alignment (currenty %s)\n", on(ps->opt.align));
    fprintf(stderr, " \\C [<captn>] -- set html3 caption (currently '%s')\n", ps->opt.caption ? ps->opt.caption : "");
    fprintf(stderr, " \\connect <dbname>  -- connect to new database (currently '%s')\n", PQdb(ps->db));
    fprintf(stderr, " \\copy table {from | to} <fname>\n");
    fprintf(stderr, " \\d [<table>] -- list tables in database or columns in <table>, * for all\n");
    fprintf(stderr, " \\e [<fname>] -- edit the current query buffer or <fname>, \\E execute too\n");
    fprintf(stderr, " \\f [<sep>]   -- change field separater (currently '%s')\n", ps->opt.fieldSep);
    fprintf(stderr, " \\g [<fname>] [|<cmd>] -- send query to backend [and results in <fname> or pipe]\n");
    fprintf(stderr, " \\h [<cmd>]   -- help on syntax of sql commands, * for all commands\n");
{'fraction_non_alphanumeric': 0.1538771562147348, 'fraction_numerical': 0.005803643398355635, 'mean_word_length': 2.2445083682008367, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 70, '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': '2182478', 'n_tokens_mistral': 3893, 'n_tokens_neox': 3758, 'n_words': 1213}