forked from postgres/postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption_utils.c
More file actions
146 lines (130 loc) · 3.39 KB
/
Copy pathoption_utils.c
File metadata and controls
146 lines (130 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
82
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
108
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
/*-------------------------------------------------------------------------
*
* Command line option processing facilities for frontend code
*
* Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* src/fe_utils/option_utils.c
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "common/logging.h"
#include "common/string.h"
#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);
}
}
}
/*
* 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);
/*
* 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')
{
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;
}
/*
* Provide strictly harmonized handling of the --sync-method option.
*/
bool
parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
{
if (strcmp(optarg, "fsync") == 0)
*sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
else if (strcmp(optarg, "syncfs") == 0)
{
#ifdef HAVE_SYNCFS
*sync_method = DATA_DIR_SYNC_METHOD_SYNCFS;
#else
pg_log_error("this build does not support sync method \"%s\"",
"syncfs");
return false;
#endif
}
else
{
pg_log_error("unrecognized sync method: %s", optarg);
return false;
}
return true;
}
/*
* Fail with appropriate error if 2 or more of the specified options are set.
* The first parameter is the number of arguments. Following that is an
* arbitrary number of bool/string pairs. The bool indicates whether the
* option is set, and the string is used for the error message. Note that only
* the first pair of enabled options we find are reported.
*
* Don't call this directly. Use the check_mut_excl_opts() macro in
* option_utils.h, which is the exact same except it doesn't take the first
* parameter (it discovers the number of arguments automagically).
*/
void
check_mut_excl_opts_internal(int n, ...)
{
char *first = NULL;
va_list args;
Assert(n % 2 == 0);
va_start(args, n);
for (int i = 0; i < n; i += 2)
{
bool set = va_arg(args, int);
char *opt = va_arg(args, char *);
if (set && first)
pg_fatal("options %s and %s cannot be used together",
first, opt);
if (set)
first = opt;
}
va_end(args);
}