mirror of
https://git.postgresql.org/git/postgresql.git
synced 2026-02-10 22:47:36 +08:00
Change pg_bsd_indent to follow upstream rules for placement of comments to the right of code, and remove pgindent hack that caused comments following #endif to not obey the general rule. Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using the published version of pg_bsd_indent, but a hacked-up version that tried to minimize the amount of movement of comments to the right of code. The situation of interest is where such a comment has to be moved to the right of its default placement at column 33 because there's code there. BSD indent has always moved right in units of tab stops in such cases --- but in the previous incarnation, indent was working in 8-space tab stops, while now it knows we use 4-space tabs. So the net result is that in about half the cases, such comments are placed one tab stop left of before. This is better all around: it leaves more room on the line for comment text, and it means that in such cases the comment uniformly starts at the next 4-space tab stop after the code, rather than sometimes one and sometimes two tabs after. Also, ensure that comments following #endif are indented the same as comments following other preprocessor commands such as #else. That inconsistency turns out to have been self-inflicted damage from a poorly-thought-through post-indent "fixup" in pgindent. This patch is much less interesting than the first round of indent changes, but also bulkier, so I thought it best to separate the effects. Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
429 lines
9.0 KiB
C
429 lines
9.0 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* common.c
|
|
* Common support routines for bin/scripts/
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/bin/scripts/common.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
|
|
#include "common.h"
|
|
|
|
|
|
static PGcancel *volatile cancelConn = NULL;
|
|
bool CancelRequested = false;
|
|
|
|
#ifdef WIN32
|
|
static CRITICAL_SECTION cancelConnLock;
|
|
#endif
|
|
|
|
/*
|
|
* Provide strictly harmonized handling of --help and --version
|
|
* options.
|
|
*/
|
|
void
|
|
handle_help_version_opts(int argc, char *argv[],
|
|
const char *fixed_progname, help_handler hlp)
|
|
{
|
|
if (argc > 1)
|
|
{
|
|
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
|
|
{
|
|
hlp(get_progname(argv[0]));
|
|
exit(0);
|
|
}
|
|
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
|
|
{
|
|
printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
|
|
exit(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* Make a database connection with the given parameters.
|
|
*
|
|
* An interactive password prompt is automatically issued if needed and
|
|
* allowed by prompt_password.
|
|
*
|
|
* If allow_password_reuse is true, we will try to re-use any password
|
|
* given during previous calls to this routine. (Callers should not pass
|
|
* allow_password_reuse=true unless reconnecting to the same database+user
|
|
* as before, else we might create password exposure hazards.)
|
|
*/
|
|
PGconn *
|
|
connectDatabase(const char *dbname, const char *pghost, const char *pgport,
|
|
const char *pguser, enum trivalue prompt_password,
|
|
const char *progname, bool fail_ok, bool allow_password_reuse)
|
|
{
|
|
PGconn *conn;
|
|
bool new_pass;
|
|
static bool have_password = false;
|
|
static char password[100];
|
|
|
|
if (!allow_password_reuse)
|
|
have_password = false;
|
|
|
|
if (!have_password && prompt_password == TRI_YES)
|
|
{
|
|
simple_prompt("Password: ", password, sizeof(password), false);
|
|
have_password = true;
|
|
}
|
|
|
|
/*
|
|
* Start the connection. Loop until we have a password if requested by
|
|
* backend.
|
|
*/
|
|
do
|
|
{
|
|
const char *keywords[7];
|
|
const char *values[7];
|
|
|
|
keywords[0] = "host";
|
|
values[0] = pghost;
|
|
keywords[1] = "port";
|
|
values[1] = pgport;
|
|
keywords[2] = "user";
|
|
values[2] = pguser;
|
|
keywords[3] = "password";
|
|
values[3] = have_password ? password : NULL;
|
|
keywords[4] = "dbname";
|
|
values[4] = dbname;
|
|
keywords[5] = "fallback_application_name";
|
|
values[5] = progname;
|
|
keywords[6] = NULL;
|
|
values[6] = NULL;
|
|
|
|
new_pass = false;
|
|
conn = PQconnectdbParams(keywords, values, true);
|
|
|
|
if (!conn)
|
|
{
|
|
fprintf(stderr, _("%s: could not connect to database %s: out of memory\n"),
|
|
progname, dbname);
|
|
exit(1);
|
|
}
|
|
|
|
/*
|
|
* No luck? Trying asking (again) for a password.
|
|
*/
|
|
if (PQstatus(conn) == CONNECTION_BAD &&
|
|
PQconnectionNeedsPassword(conn) &&
|
|
prompt_password != TRI_NO)
|
|
{
|
|
PQfinish(conn);
|
|
simple_prompt("Password: ", password, sizeof(password), false);
|
|
have_password = true;
|
|
new_pass = true;
|
|
}
|
|
} while (new_pass);
|
|
|
|
/* check to see that the backend connection was successfully made */
|
|
if (PQstatus(conn) == CONNECTION_BAD)
|
|
{
|
|
if (fail_ok)
|
|
{
|
|
PQfinish(conn);
|
|
return NULL;
|
|
}
|
|
fprintf(stderr, _("%s: could not connect to database %s: %s"),
|
|
progname, dbname, PQerrorMessage(conn));
|
|
exit(1);
|
|
}
|
|
|
|
return conn;
|
|
}
|
|
|
|
/*
|
|
* Try to connect to the appropriate maintenance database.
|
|
*/
|
|
PGconn *
|
|
connectMaintenanceDatabase(const char *maintenance_db, const char *pghost,
|
|
const char *pgport, const char *pguser,
|
|
enum trivalue prompt_password,
|
|
const char *progname)
|
|
{
|
|
PGconn *conn;
|
|
|
|
/* If a maintenance database name was specified, just connect to it. */
|
|
if (maintenance_db)
|
|
return connectDatabase(maintenance_db, pghost, pgport, pguser,
|
|
prompt_password, progname, false, false);
|
|
|
|
/* Otherwise, try postgres first and then template1. */
|
|
conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password,
|
|
progname, true, false);
|
|
if (!conn)
|
|
conn = connectDatabase("template1", pghost, pgport, pguser,
|
|
prompt_password, progname, false, false);
|
|
|
|
return conn;
|
|
}
|
|
|
|
/*
|
|
* Run a query, return the results, exit program on failure.
|
|
*/
|
|
PGresult *
|
|
executeQuery(PGconn *conn, const char *query, const char *progname, bool echo)
|
|
{
|
|
PGresult *res;
|
|
|
|
if (echo)
|
|
printf("%s\n", query);
|
|
|
|
res = PQexec(conn, query);
|
|
if (!res ||
|
|
PQresultStatus(res) != PGRES_TUPLES_OK)
|
|
{
|
|
fprintf(stderr, _("%s: query failed: %s"),
|
|
progname, PQerrorMessage(conn));
|
|
fprintf(stderr, _("%s: query was: %s\n"),
|
|
progname, query);
|
|
PQfinish(conn);
|
|
exit(1);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
/*
|
|
* As above for a SQL command (which returns nothing).
|
|
*/
|
|
void
|
|
executeCommand(PGconn *conn, const char *query,
|
|
const char *progname, bool echo)
|
|
{
|
|
PGresult *res;
|
|
|
|
if (echo)
|
|
printf("%s\n", query);
|
|
|
|
res = PQexec(conn, query);
|
|
if (!res ||
|
|
PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
{
|
|
fprintf(stderr, _("%s: query failed: %s"),
|
|
progname, PQerrorMessage(conn));
|
|
fprintf(stderr, _("%s: query was: %s\n"),
|
|
progname, query);
|
|
PQfinish(conn);
|
|
exit(1);
|
|
}
|
|
|
|
PQclear(res);
|
|
}
|
|
|
|
|
|
/*
|
|
* As above for a SQL maintenance command (returns command success).
|
|
* Command is executed with a cancel handler set, so Ctrl-C can
|
|
* interrupt it.
|
|
*/
|
|
bool
|
|
executeMaintenanceCommand(PGconn *conn, const char *query, bool echo)
|
|
{
|
|
PGresult *res;
|
|
bool r;
|
|
|
|
if (echo)
|
|
printf("%s\n", query);
|
|
|
|
SetCancelConn(conn);
|
|
res = PQexec(conn, query);
|
|
ResetCancelConn();
|
|
|
|
r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
|
|
|
|
if (res)
|
|
PQclear(res);
|
|
|
|
return r;
|
|
}
|
|
|
|
/*
|
|
* Check yes/no answer in a localized way. 1=yes, 0=no, -1=neither.
|
|
*/
|
|
|
|
/* translator: abbreviation for "yes" */
|
|
#define PG_YESLETTER gettext_noop("y")
|
|
/* translator: abbreviation for "no" */
|
|
#define PG_NOLETTER gettext_noop("n")
|
|
|
|
bool
|
|
yesno_prompt(const char *question)
|
|
{
|
|
char prompt[256];
|
|
|
|
/*------
|
|
translator: This is a question followed by the translated options for
|
|
"yes" and "no". */
|
|
snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
|
|
_(question), _(PG_YESLETTER), _(PG_NOLETTER));
|
|
|
|
for (;;)
|
|
{
|
|
char resp[10];
|
|
|
|
simple_prompt(prompt, resp, sizeof(resp), true);
|
|
|
|
if (strcmp(resp, _(PG_YESLETTER)) == 0)
|
|
return true;
|
|
if (strcmp(resp, _(PG_NOLETTER)) == 0)
|
|
return false;
|
|
|
|
printf(_("Please answer \"%s\" or \"%s\".\n"),
|
|
_(PG_YESLETTER), _(PG_NOLETTER));
|
|
}
|
|
}
|
|
|
|
/*
|
|
* SetCancelConn
|
|
*
|
|
* Set cancelConn to point to the current database connection.
|
|
*/
|
|
void
|
|
SetCancelConn(PGconn *conn)
|
|
{
|
|
PGcancel *oldCancelConn;
|
|
|
|
#ifdef WIN32
|
|
EnterCriticalSection(&cancelConnLock);
|
|
#endif
|
|
|
|
/* Free the old one if we have one */
|
|
oldCancelConn = cancelConn;
|
|
|
|
/* be sure handle_sigint doesn't use pointer while freeing */
|
|
cancelConn = NULL;
|
|
|
|
if (oldCancelConn != NULL)
|
|
PQfreeCancel(oldCancelConn);
|
|
|
|
cancelConn = PQgetCancel(conn);
|
|
|
|
#ifdef WIN32
|
|
LeaveCriticalSection(&cancelConnLock);
|
|
#endif
|
|
}
|
|
|
|
/*
|
|
* ResetCancelConn
|
|
*
|
|
* Free the current cancel connection, if any, and set to NULL.
|
|
*/
|
|
void
|
|
ResetCancelConn(void)
|
|
{
|
|
PGcancel *oldCancelConn;
|
|
|
|
#ifdef WIN32
|
|
EnterCriticalSection(&cancelConnLock);
|
|
#endif
|
|
|
|
oldCancelConn = cancelConn;
|
|
|
|
/* be sure handle_sigint doesn't use pointer while freeing */
|
|
cancelConn = NULL;
|
|
|
|
if (oldCancelConn != NULL)
|
|
PQfreeCancel(oldCancelConn);
|
|
|
|
#ifdef WIN32
|
|
LeaveCriticalSection(&cancelConnLock);
|
|
#endif
|
|
}
|
|
|
|
#ifndef WIN32
|
|
/*
|
|
* Handle interrupt signals by canceling the current command, if a cancelConn
|
|
* is set.
|
|
*/
|
|
static void
|
|
handle_sigint(SIGNAL_ARGS)
|
|
{
|
|
int save_errno = errno;
|
|
char errbuf[256];
|
|
|
|
/* Send QueryCancel if we are processing a database query */
|
|
if (cancelConn != NULL)
|
|
{
|
|
if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
|
|
{
|
|
CancelRequested = true;
|
|
fprintf(stderr, _("Cancel request sent\n"));
|
|
}
|
|
else
|
|
fprintf(stderr, _("Could not send cancel request: %s"), errbuf);
|
|
}
|
|
else
|
|
CancelRequested = true;
|
|
|
|
errno = save_errno; /* just in case the write changed it */
|
|
}
|
|
|
|
void
|
|
setup_cancel_handler(void)
|
|
{
|
|
pqsignal(SIGINT, handle_sigint);
|
|
}
|
|
#else /* WIN32 */
|
|
|
|
/*
|
|
* Console control handler for Win32. Note that the control handler will
|
|
* execute on a *different thread* than the main one, so we need to do
|
|
* proper locking around those structures.
|
|
*/
|
|
static BOOL WINAPI
|
|
consoleHandler(DWORD dwCtrlType)
|
|
{
|
|
char errbuf[256];
|
|
|
|
if (dwCtrlType == CTRL_C_EVENT ||
|
|
dwCtrlType == CTRL_BREAK_EVENT)
|
|
{
|
|
/* Send QueryCancel if we are processing a database query */
|
|
EnterCriticalSection(&cancelConnLock);
|
|
if (cancelConn != NULL)
|
|
{
|
|
if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
|
|
{
|
|
fprintf(stderr, _("Cancel request sent\n"));
|
|
CancelRequested = true;
|
|
}
|
|
else
|
|
fprintf(stderr, _("Could not send cancel request: %s"), errbuf);
|
|
}
|
|
else
|
|
CancelRequested = true;
|
|
|
|
LeaveCriticalSection(&cancelConnLock);
|
|
|
|
return TRUE;
|
|
}
|
|
else
|
|
/* Return FALSE for any signals not being handled */
|
|
return FALSE;
|
|
}
|
|
|
|
void
|
|
setup_cancel_handler(void)
|
|
{
|
|
InitializeCriticalSection(&cancelConnLock);
|
|
|
|
SetConsoleCtrlHandler(consoleHandler, TRUE);
|
|
}
|
|
|
|
#endif /* WIN32 */
|