Move some code from src/bin/scripts to src/fe_utils to permit reuse.
The parallel slots infrastructure (which implements client-side
multiplexing of server connections doing similar things, not
threading or multiple processes or anything like that) are moved from
src/bin/scripts/scripts_parallel.c to src/fe_utils/parallel_slot.c.
The functions consumeQueryResult() and processQueryResult() which were
previously part of src/bin/scripts/common.c are now moved into that
file as well, becoming static helper functions. This might need to be
changed in the future, but currently they're not used for anything
else.
Some other functions from src/bin/scripts/common.c are moved to to
src/fe_utils and are split up among several files. connectDatabase(),
connectMaintenanceDatabase(), and disconnectDatabase() are moved to
connect_utils.c. executeQuery(), executeCommand(), and
executeMaintenanceCommand() are move to query_utils.c.
handle_help_version_opts() is moved to option_utils.c.
Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether any
of them have reviewed this bit specifically.
Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com
2021-02-05 19:33:38 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Command line option processing facilities for frontend code
|
|
|
|
*
|
2023-01-02 21:00:37 +01:00
|
|
|
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
|
Move some code from src/bin/scripts to src/fe_utils to permit reuse.
The parallel slots infrastructure (which implements client-side
multiplexing of server connections doing similar things, not
threading or multiple processes or anything like that) are moved from
src/bin/scripts/scripts_parallel.c to src/fe_utils/parallel_slot.c.
The functions consumeQueryResult() and processQueryResult() which were
previously part of src/bin/scripts/common.c are now moved into that
file as well, becoming static helper functions. This might need to be
changed in the future, but currently they're not used for anything
else.
Some other functions from src/bin/scripts/common.c are moved to to
src/fe_utils and are split up among several files. connectDatabase(),
connectMaintenanceDatabase(), and disconnectDatabase() are moved to
connect_utils.c. executeQuery(), executeCommand(), and
executeMaintenanceCommand() are move to query_utils.c.
handle_help_version_opts() is moved to option_utils.c.
Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether any
of them have reviewed this bit specifically.
Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com
2021-02-05 19:33:38 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* src/fe_utils/option_utils.c
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres_fe.h"
|
|
|
|
|
2021-07-24 11:35:03 +02:00
|
|
|
#include "common/logging.h"
|
|
|
|
#include "common/string.h"
|
Move some code from src/bin/scripts to src/fe_utils to permit reuse.
The parallel slots infrastructure (which implements client-side
multiplexing of server connections doing similar things, not
threading or multiple processes or anything like that) are moved from
src/bin/scripts/scripts_parallel.c to src/fe_utils/parallel_slot.c.
The functions consumeQueryResult() and processQueryResult() which were
previously part of src/bin/scripts/common.c are now moved into that
file as well, becoming static helper functions. This might need to be
changed in the future, but currently they're not used for anything
else.
Some other functions from src/bin/scripts/common.c are moved to to
src/fe_utils and are split up among several files. connectDatabase(),
connectMaintenanceDatabase(), and disconnectDatabase() are moved to
connect_utils.c. executeQuery(), executeCommand(), and
executeMaintenanceCommand() are move to query_utils.c.
handle_help_version_opts() is moved to option_utils.c.
Mark Dilger, reviewed by me. The larger patch series of which this is
a part has also had review from Peter Geoghegan, Andres Freund, Álvaro
Herrera, Michael Paquier, and Amul Sul, but I don't know whether any
of them have reviewed this bit specifically.
Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/5F743835-3399-419C-8324-2D424237E999@enterprisedb.com
Discussion: http://postgr.es/m/70655DF3-33CE-4527-9A4D-DDEB582B6BA0@enterprisedb.com
2021-02-05 19:33:38 +01:00
|
|
|
#include "fe_utils/option_utils.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-24 11:35:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* option_parse_int
|
|
|
|
*
|
|
|
|
* Parse integer value for an option. If the parsing is successful, returns
|
|
|
|
* true and stores the result in *result if that's given; if parsing fails,
|
|
|
|
* returns false.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
option_parse_int(const char *optarg, const char *optname,
|
|
|
|
int min_range, int max_range,
|
|
|
|
int *result)
|
|
|
|
{
|
|
|
|
char *endptr;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoint(optarg, &endptr, 10);
|
|
|
|
|
2021-07-27 03:39:05 +02:00
|
|
|
/*
|
|
|
|
* Skip any trailing whitespace; if anything but whitespace remains before
|
|
|
|
* the terminating character, fail.
|
|
|
|
*/
|
|
|
|
while (*endptr != '\0' && isspace((unsigned char) *endptr))
|
|
|
|
endptr++;
|
|
|
|
|
|
|
|
if (*endptr != '\0')
|
2021-07-24 11:35:03 +02:00
|
|
|
{
|
|
|
|
pg_log_error("invalid value \"%s\" for option %s",
|
|
|
|
optarg, optname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == ERANGE || val < min_range || val > max_range)
|
|
|
|
{
|
|
|
|
pg_log_error("%s must be in range %d..%d",
|
|
|
|
optname, min_range, max_range);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
*result = val;
|
|
|
|
return true;
|
|
|
|
}
|