common.c 3.85 KB
Newer Older
1 2 3 4
/*-------------------------------------------------------------------------
 *
 * Miscellaneous shared code
 *
5
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
6 7
 * Portions Copyright (c) 1994, Regents of the University of California
 *
8
 * $PostgreSQL: pgsql/src/bin/scripts/common.c,v 1.17 2005/02/22 04:41:30 momjian Exp $
9 10 11 12 13 14
 *
 *-------------------------------------------------------------------------
 */

#include "postgres_fe.h"
#include "common.h"
15
#include "libpq-fe.h"
16 17 18 19 20 21 22 23 24 25 26

#include <pwd.h>
#include <unistd.h>


/*
 * Returns the current user name.
 */
const char *
get_user_name(const char *progname)
{
Bruce Momjian's avatar
Bruce Momjian committed
27
#ifndef WIN32
28 29
	struct passwd *pw;

30
	pw = getpwuid(geteuid());
31 32
	if (!pw)
	{
33
		fprintf(stderr, _("%s: could not obtain information about current user: %s\n"),
34
				progname, strerror(errno));
35 36 37
		exit(1);
	}
	return pw->pw_name;
Bruce Momjian's avatar
Bruce Momjian committed
38 39
#else
	static char username[128];	/* remains after function exit */
Bruce Momjian's avatar
Bruce Momjian committed
40
	DWORD		len = sizeof(username) - 1;
Bruce Momjian's avatar
Bruce Momjian committed
41

42 43
	if (!GetUserName(username, &len))
	{
44
		fprintf(stderr, _("%s: could not get current user name: %s\n"),
45
				progname, strerror(errno));
46 47
		exit(1);
	}
Bruce Momjian's avatar
Bruce Momjian committed
48
	return username;
49
#endif
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
}


/*
 * 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 required.
 */
PGconn *
connectDatabase(const char *dbname, const char *pghost, const char *pgport,
Bruce Momjian's avatar
Bruce Momjian committed
82
		 const char *pguser, bool require_password, const char *progname)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
{
	PGconn	   *conn;
	char	   *password = NULL;
	bool		need_pass = false;

	if (require_password)
		password = simple_prompt("Password: ", 100, false);

	/*
	 * Start the connection.  Loop until we have a password if requested
	 * by backend.
	 */
	do
	{
		need_pass = false;
		conn = PQsetdbLogin(pghost, pgport, NULL, NULL, dbname, pguser, password);

		if (!conn)
		{
			fprintf(stderr, _("%s: could not connect to database %s\n"),
					progname, dbname);
			exit(1);
		}

		if (PQstatus(conn) == CONNECTION_BAD &&
108
			strcmp(PQerrorMessage(conn), PQnoPasswordSupplied) == 0 &&
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
			!feof(stdin))
		{
			PQfinish(conn);
			need_pass = true;
			free(password);
			password = NULL;
			password = simple_prompt("Password: ", 100, false);
		}
	} while (need_pass);

	if (password)
		free(password);

	/* check to see that the backend connection was successfully made */
	if (PQstatus(conn) == CONNECTION_BAD)
	{
		fprintf(stderr, _("%s: could not connect to database %s: %s"),
				progname, dbname, PQerrorMessage(conn));
		exit(1);
	}

	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;
}
157 158 159


/*
Bruce Momjian's avatar
Bruce Momjian committed
160
 * Check yes/no answer in a localized way.	1=yes, 0=no, -1=neither.
161 162 163 164 165 166 167 168 169 170
 */

/* translator: Make sure the (y/n) prompts match the translation of this. */
#define PG_YESLETTER gettext_noop("y")
/* translator: Make sure the (y/n) prompts match the translation of this. */
#define PG_NOLETTER gettext_noop("n")

int
check_yesno_response(const char *string)
{
171
	if (strcmp(string, _(PG_YESLETTER)) == 0)
172
		return 1;
173
	else if (strcmp(string, _(PG_NOLETTER)) == 0)
174 175 176 177
		return 0;
	else
		return -1;
}