本文整理汇总了C++中simple_prompt函数的典型用法代码示例。如果您正苦于以下问题:C++ simple_prompt函数的具体用法?C++ simple_prompt怎么用?C++ simple_prompt使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了simple_prompt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connectDatabase
/*
* 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,
const char *pguser, enum trivalue prompt_password,
const char *progname)
{
PGconn *conn;
char *password = NULL;
bool new_pass;
if (prompt_password == TRI_YES)
password = simple_prompt("Password: ", 100, false);
/*
* Start the connection. Loop until we have a password if requested by
* backend.
*/
do
{
new_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 &&
PQconnectionNeedsPassword(conn) &&
password == NULL &&
prompt_password != TRI_NO)
{
PQfinish(conn);
password = simple_prompt("Password: ", 100, false);
new_pass = true;
}
} while (new_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;
}
示例2: yesno_prompt
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;
resp = simple_prompt(prompt, 1, true);
if (strcmp(resp, _(PG_YESLETTER)) == 0)
{
free(resp);
return true;
}
else if (strcmp(resp, _(PG_NOLETTER)) == 0)
{
free(resp);
return false;
}
free(resp);
printf(_("Please answer \"%s\" or \"%s\".\n"),
_(PG_YESLETTER), _(PG_NOLETTER));
}
}
示例3: prompt_for_password
/*
* Ask the user for a password; 'username' is the username the
* password is for, if one has been explicitly specified.
* Set malloc'd string to the global variable 'password'.
*/
static void
prompt_for_password(const char *username)
{
if (password)
{
free(password);
password = NULL;
}
if (username == NULL)
password = simple_prompt("Password: ", 100, false);
else
{
char message[256];
snprintf(message, lengthof(message), "Password for user %s: ", username);
password = simple_prompt(message, 100, false);
}
}
示例4: prompt_for_password
static char *
prompt_for_password(void)
#if PG_VERSION_NUM >= 100000
{
char buf[100];
simple_prompt("Password: ", buf, sizeof(buf), false);
return pg_strdup(buf);
}
示例5: prompt_for_password
/*
* Ask the user for a password; 'username' is the username the
* password is for, if one has been explicitly specified. Returns a
* malloc'd string.
*/
static char *
prompt_for_password(const char *username)
{
char *result;
if (username == NULL)
result = simple_prompt("Password: ", 100, false);
else
{
char *prompt_text;
prompt_text = malloc(strlen(username) + 100);
snprintf(prompt_text, strlen(username) + 100,
_("Password for user %s: "), username);
result = simple_prompt(prompt_text, 100, false);
free(prompt_text);
}
return result;
}
示例6: sql_conn
/* establish connection with database. */
PGconn *
sql_conn(struct options * my_opts)
{
PGconn *conn;
char *password = NULL;
bool new_pass;
/*
* Start the connection. Loop until we have a password if requested by
* backend.
*/
do
{
new_pass = false;
conn = PQsetdbLogin(my_opts->hostname,
my_opts->port,
NULL, /* options */
NULL, /* tty */
my_opts->dbname,
my_opts->username,
password);
if (!conn)
{
fprintf(stderr, "%s: could not connect to database %s\n",
"oid2name", my_opts->dbname);
exit(1);
}
if (PQstatus(conn) == CONNECTION_BAD &&
PQconnectionNeedsPassword(conn) &&
password == NULL)
{
PQfinish(conn);
password = simple_prompt("Password: ", 100, false);
new_pass = true;
}
} while (new_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",
"oid2name", my_opts->dbname, PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
/* return the conn if good */
return conn;
}
示例7: loginDatabase
static PGconn *
loginDatabase(char *host, int port, char *user, char *password, char *dbname, const char *progname, char *encoding, char *password_prompt)
{
bool new_pass = false;
PGconn *coord_conn;
char port_s[32];
#define PARAMS_ARRAY_SIZE 8
const char *keywords[PARAMS_ARRAY_SIZE];
const char *values[PARAMS_ARRAY_SIZE];
sprintf(port_s, "%d", port);
keywords[0] = "host";
values[0] = host;
keywords[1] = "port";
values[1] = port_s;
keywords[2] = "user";
values[2] = user;
keywords[3] = "password";
keywords[4] = "dbname";
values[4] = dbname;
keywords[5] = "fallback_application_name";
values[5] = progname;
keywords[6] = "client_encoding";
values[6] = encoding;
keywords[7] = NULL;
values[7] = NULL;
/* Loop until we have a password if requested by backend */
do
{
values[3] = password;
new_pass = false;
coord_conn = PQconnectdbParams(keywords, values, true);
if (PQstatus(coord_conn) == CONNECTION_BAD &&
PQconnectionNeedsPassword(coord_conn) &&
password == NULL &&
try_password_opt != TRI_NO)
{
PQfinish(coord_conn);
password = simple_prompt(password_prompt, 100, false);
new_pass = true;
}
} while (new_pass);
return(coord_conn);
}
示例8: DBConnect
/*
* Open a database connection
*/
static PGconn *
DBConnect(const char *host, const char *port, char *database, const char *user)
{
char *password = NULL;
char *password_prompt = NULL;
bool need_pass;
PGconn *conn = NULL;
/* loop until we have a password if requested by backend */
do
{
need_pass = false;
conn = PQsetdbLogin(host,
port,
NULL,
NULL,
database,
user,
password);
if (PQstatus(conn) == CONNECTION_BAD &&
strcmp(PQerrorMessage(conn), PQnoPasswordSupplied) == 0 &&
!feof(stdin))
{
PQfinish(conn);
need_pass = true;
free(password);
password = NULL;
printf("\nPassword: ");
password = simple_prompt(password_prompt, 100, false);
}
} while (need_pass);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_gracefuly(1);
}
if (password)
free(password);
return conn;
}
示例9: DBConnect
/*
* Open a database connection
*/
bool
DBConnect(const char *host, const char *port, char *database, const char *user)
{
pghost = strdup(host);
pgport = strdup(port);
pguser = strdup(user);
pgpass = NULL;
retry_login:
conn = PQsetdbLogin(pghost,
pgport,
NULL,
NULL,
database,
pguser,
pgpass);
if (PQstatus(conn) == CONNECTION_BAD)
{
/* wait a password if required to login, then retry. */
if (strcmp(PQerrorMessage(conn), PQnoPasswordSupplied) == 0 &&
!feof(stdin))
{
PQfinish(conn);
conn = NULL;
pgpass = simple_prompt("Password: ", 100, false);
goto retry_login;
}
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
PQfinish(conn);
conn = NULL;
return false;
}
return true;
}
示例10: main
int
main(int argc, char *argv[])
{
static struct option long_options[] = {
{"host", required_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{"username", required_argument, NULL, 'U'},
{"no-password", no_argument, NULL, 'w'},
{"password", no_argument, NULL, 'W'},
{"echo", no_argument, NULL, 'e'},
{"interactive", no_argument, NULL, 'i'},
{NULL, 0, NULL, 0}
};
const char *progname;
int optindex;
int c;
char *dropuser = NULL;
char *host = NULL;
char *port = NULL;
char *username = NULL;
enum trivalue prompt_password = TRI_DEFAULT;
bool echo = false;
bool interactive = false;
PQExpBufferData sql;
PGconn *conn;
PGresult *result;
progname = get_progname(argv[0]);
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
handle_help_version_opts(argc, argv, "dropuser", help);
while ((c = getopt_long(argc, argv, "h:p:U:wWei", long_options, &optindex)) != -1)
{
switch (c)
{
case 'h':
host = optarg;
break;
case 'p':
port = optarg;
break;
case 'U':
username = optarg;
break;
case 'w':
prompt_password = TRI_NO;
break;
case 'W':
prompt_password = TRI_YES;
break;
case 'e':
echo = true;
break;
case 'i':
interactive = true;
break;
default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
}
switch (argc - optind)
{
case 0:
break;
case 1:
dropuser = argv[optind];
break;
default:
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
progname, argv[optind + 1]);
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
exit(1);
}
if (dropuser == NULL)
dropuser = simple_prompt("Enter name of role to drop: ", 128, true);
if (interactive)
{
printf(_("Role \"%s\" will be permanently removed.\n"), dropuser);
if (!yesno_prompt("Are you sure?"))
exit(0);
}
initPQExpBuffer(&sql);
appendPQExpBuffer(&sql, "DROP ROLE %s;\n", fmtId(dropuser));
conn = connectDatabase("postgres", host, port, username, prompt_password, progname);
if (echo)
printf("%s", sql.data);
result = PQexec(conn, sql.data);
//.........这里部分代码省略.........
示例11: GetMasterConnection
/*
* GetMasterConnection: This function makes a database connection with the given parameters.
* The connection handle is returned.
* An interactive password prompt is automatically issued if required.
* This is a copy of the one in pg_dump.
*/
PGconn *
GetMasterConnection(const char *progName,
const char *dbname,
const char *pghost,
const char *pgport,
const char *username,
int reqPwd,
int ignoreVersion,
bool bDispatch)
{
char *pszPassword = NULL;
bool need_pass = false;
PGconn *pConn = NULL;
SegmentDatabase masterDB;
if (reqPwd)
{
pszPassword = simple_prompt("Password: ", 100, false);
if (pszPassword == NULL)
{
mpp_err_msg_cache("ERROR", progName, "out of memory when allocating password");
return NULL;
}
}
masterDB.dbid = 0;
masterDB.role = 0;
masterDB.port = pgport ? atoi(pgport) : 5432;
masterDB.pszHost = (char *) pghost;
masterDB.pszDBName = (char *) dbname;
masterDB.pszDBUser = (char *) username;
masterDB.pszDBPswd = pszPassword;
/*
* Start the connection. Loop until we have a password if requested by
* backend.
*/
do
{
need_pass = false;
pConn = MakeDBConnection(&masterDB, bDispatch);
if (pConn == NULL)
{
mpp_err_msg_cache("ERROR", progName, "failed to connect to database");
return (NULL);
}
if (PQstatus(pConn) == CONNECTION_BAD &&
strcmp(PQerrorMessage(pConn), "fe_sendauth: no password supplied\n") == 0 &&
!feof(stdin))
{
PQfinish(pConn);
need_pass = true;
free(pszPassword);
pszPassword = NULL;
pszPassword = simple_prompt("Password: ", 100, false);
masterDB.pszDBPswd = pszPassword;
}
} while (need_pass);
if (pszPassword)
free(pszPassword);
/* check to see that the backend connection was successfully made */
if (PQstatus(pConn) == CONNECTION_BAD)
{
mpp_err_msg_cache("ERROR", progName, "connection to database \"%s\" failed : %s",
PQdb(pConn), PQerrorMessage(pConn));
PQfinish(pConn);
return (NULL);
}
return pConn;
}
示例12: main
//.........这里部分代码省略.........
case 'R':
/* no-op, still accepted for backwards compatibility */
break;
case 'P': /* Function */
opts->selTypes = 1;
opts->selFunction = 1;
opts->functionNames = strdup(optarg);
break;
case 'I': /* Index */
opts->selTypes = 1;
opts->selIndex = 1;
opts->indexNames = strdup(optarg);
break;
case 'T': /* Trigger */
opts->selTypes = 1;
opts->selTrigger = 1;
opts->triggerNames = strdup(optarg);
break;
case 's': /* dump schema only */
opts->schemaOnly = 1;
break;
case 'S': /* Superuser username */
if (strlen(optarg) != 0)
opts->superuser = strdup(optarg);
break;
case 't': /* Dump data for this table only */
opts->selTypes = 1;
opts->selTable = 1;
opts->tableNames = strdup(optarg);
break;
case 'u':
opts->promptPassword = TRI_YES;
opts->username = simple_prompt("User name: ", 100, true);
break;
case 'U':
opts->username = optarg;
break;
case 'v': /* verbose */
opts->verbose = 1;
break;
case 'w':
opts->promptPassword = TRI_NO;
break;
case 'W':
opts->promptPassword = TRI_YES;
break;
case 'x': /* skip ACL dump */
opts->aclsSkip = 1;
break;
case 'X':
/* -X is a deprecated alternative to long options */
if (strcmp(optarg, "use-set-session-authorization") == 0)
use_setsessauth = 1;
else if (strcmp(optarg, "disable-triggers") == 0)
disable_triggers = 1;
else
{
fprintf(stderr,
_("%s: invalid -X option -- %s\n"),
示例13: connectDatabase
/*
* 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,
const char *pguser, enum trivalue prompt_password,
const char *progname)
{
PGconn *conn;
char *password = NULL;
bool new_pass;
if (prompt_password == TRI_YES)
password = simple_prompt("Password: ", 100, false);
/*
* Start the connection. Loop until we have a password if requested by
* backend.
*/
do
{
#define PARAMS_ARRAY_SIZE 7
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
if (!keywords || !values)
{
fprintf(stderr, _("%s: out of memory\n"), progname);
exit(1);
}
keywords[0] = "host";
values[0] = pghost;
keywords[1] = "port";
values[1] = pgport;
keywords[2] = "user";
values[2] = pguser;
keywords[3] = "password";
values[3] = password;
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);
free(keywords);
free(values);
if (!conn)
{
fprintf(stderr, _("%s: could not connect to database %s\n"),
progname, dbname);
exit(1);
}
if (PQstatus(conn) == CONNECTION_BAD &&
PQconnectionNeedsPassword(conn) &&
password == NULL &&
prompt_password != TRI_NO)
{
PQfinish(conn);
password = simple_prompt("Password: ", 100, false);
new_pass = true;
}
} while (new_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;
}
示例14: connectDatabase
/*
* Make a database connection with the given parameters. An
* interactive password prompt is automatically issued if required.
*
* If fail_on_error is false, we return NULL without printing any message
* on failure, but preserve any prompted password for the next try.
*/
static PGconn *
connectDatabase(const char *dbname, const char *pghost, const char *pgport,
const char *pguser, enum trivalue prompt_password, bool fail_on_error)
{
PGconn *conn;
bool new_pass;
const char *remoteversion_str;
int my_version;
static char *password = NULL;
if (prompt_password == TRI_YES && !password)
password = simple_prompt("Password: ", 100, false);
/*
* Start the connection. Loop until we have a password if requested by
* backend.
*/
do
{
#define PARAMS_ARRAY_SIZE 8
const char **keywords = malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
const char **values = malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
if (!keywords || !values)
{
fprintf(stderr, _("%s: out of memory\n"), progname);
exit(1);
}
keywords[0] = "host";
values[0] = pghost;
keywords[1] = "port";
values[1] = pgport;
keywords[2] = "user";
values[2] = pguser;
keywords[3] = "password";
values[3] = password;
keywords[4] = "dbname";
values[4] = dbname;
keywords[5] = "fallback_application_name";
values[5] = progname;
keywords[6] = "options";
values[6] = "-c gp_session_role=utility";
keywords[7] = NULL;
values[7] = NULL;
new_pass = false;
conn = PQconnectdbParams(keywords, values, true);
free(keywords);
free(values);
if (!conn)
{
fprintf(stderr, _("%s: could not connect to database \"%s\"\n"),
progname, dbname);
exit(1);
}
if (PQstatus(conn) == CONNECTION_BAD &&
PQconnectionNeedsPassword(conn) &&
password == NULL &&
prompt_password != TRI_NO)
{
PQfinish(conn);
password = simple_prompt("Password: ", 100, false);
new_pass = true;
}
} while (new_pass);
/* check to see that the backend connection was successfully made */
if (PQstatus(conn) == CONNECTION_BAD)
{
if (fail_on_error)
{
fprintf(stderr,
_("%s: could not connect to database \"%s\": %s\n"),
progname, dbname, PQerrorMessage(conn));
exit(1);
}
else
{
PQfinish(conn);
return NULL;
}
}
remoteversion_str = PQparameterStatus(conn, "server_version");
if (!remoteversion_str)
{
fprintf(stderr, _("%s: could not get server version\n"), progname);
exit(1);
}
//.........这里部分代码省略.........
示例15: main
/*
*
* Main
*
*/
int main(int argc, char *argv[])
{
/* Should setup pglocale when it is supported by XC core */
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
usage();
exit(0);
}
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{
showVersion();
exit(0);
}
}
parse_pgxc_clean_options(argc, argv);
/*
* Check missing arguments
*/
if (clean_all_databases == false && head_database_names == NULL)
{
fprintf(stderr, "%s: you must specify -a or -d option.\n", progname);
exit(1);
}
/*
* Arrange my environment
*/
if (output_filename)
{
/* Prepare output filename */
outf = fopen(output_filename, "w");
if (outf == NULL)
{
fprintf(stderr, "%s: Cannot ope output file %s (%s)\n", progname, output_filename, strerror(errno));
exit(1);
}
errf = outf;
}
else
{
outf = stdout;
errf = stderr;
}
if (coordinator_host == NULL)
{
/* Default Coordinator host */
if ((coordinator_host = getenv("PGHOST")) == NULL)
coordinator_host = "localhost";
}
if (coordinator_port == -1)
{
/* Default Coordinator port */
char *pgport;
if ((pgport = getenv("PGPORT")) == NULL)
coordinator_port = DEF_PGPORT; /* pg_config.h */
else
coordinator_port = atoi(pgport);
}
if (username == NULL)
strcpy(password_prompt, "Password: ");
else
sprintf(password_prompt, "Password for user %s: ", username);
if (try_password_opt == TRI_YES)
password = simple_prompt(password_prompt, 100, false);
if (verbose_opt)
{
/* Print environments */
fprintf(outf, "%s (%s): Cleanup outstanding 2PCs.\n", progname, PG_VERSION);
/* Target databaess */
fprintf(outf, "Target databases:");
if (clean_all_databases)
fprintf(outf, "(ALL)\n");
else
{
database_names *cur_name;
for(cur_name = head_database_names; cur_name; cur_name = cur_name->next)
fprintf(outf, " %s", cur_name->database_name);
fprintf(outf, "\n");
}
/* Username to use */
fprintf(outf, "Username: %s\n", username ? username : "default");
/* Status opt */
fprintf(outf, "Status opt: %s\n", status_opt ? "on" : "off");
/* No-dlean opt */
fprintf(outf, "no-clean: %s\n", no_clean_opt ? "on" : "off");
}
//.........这里部分代码省略.........