Line | Count | Source |
1 | | #include "git-compat-util.h" |
2 | | #include "gettext.h" |
3 | | #include "parse.h" |
4 | | |
5 | | static uintmax_t get_unit_factor(const char *end) |
6 | 0 | { |
7 | 0 | if (!*end) |
8 | 0 | return 1; |
9 | 0 | else if (!strcasecmp(end, "k")) |
10 | 0 | return 1024; |
11 | 0 | else if (!strcasecmp(end, "m")) |
12 | 0 | return 1024 * 1024; |
13 | 0 | else if (!strcasecmp(end, "g")) |
14 | 0 | return 1024 * 1024 * 1024; |
15 | 0 | return 0; |
16 | 0 | } |
17 | | |
18 | | int git_parse_signed(const char *value, intmax_t *ret, intmax_t max) |
19 | 0 | { |
20 | 0 | if (value && *value) { |
21 | 0 | char *end; |
22 | 0 | intmax_t val; |
23 | 0 | intmax_t factor; |
24 | |
|
25 | 0 | if (max < 0) |
26 | 0 | BUG("max must be a positive integer"); |
27 | | |
28 | 0 | errno = 0; |
29 | 0 | val = strtoimax(value, &end, 0); |
30 | 0 | if (errno == ERANGE) |
31 | 0 | return 0; |
32 | 0 | if (end == value) { |
33 | 0 | errno = EINVAL; |
34 | 0 | return 0; |
35 | 0 | } |
36 | 0 | factor = get_unit_factor(end); |
37 | 0 | if (!factor) { |
38 | 0 | errno = EINVAL; |
39 | 0 | return 0; |
40 | 0 | } |
41 | 0 | if ((val < 0 && (-max - 1) / factor > val) || |
42 | 0 | (val > 0 && max / factor < val)) { |
43 | 0 | errno = ERANGE; |
44 | 0 | return 0; |
45 | 0 | } |
46 | 0 | val *= factor; |
47 | 0 | *ret = val; |
48 | 0 | return 1; |
49 | 0 | } |
50 | 0 | errno = EINVAL; |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | | int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max) |
55 | 0 | { |
56 | 0 | if (value && *value) { |
57 | 0 | char *end; |
58 | 0 | uintmax_t val; |
59 | 0 | uintmax_t factor; |
60 | | |
61 | | /* negative values would be accepted by strtoumax */ |
62 | 0 | if (strchr(value, '-')) { |
63 | 0 | errno = EINVAL; |
64 | 0 | return 0; |
65 | 0 | } |
66 | 0 | errno = 0; |
67 | 0 | val = strtoumax(value, &end, 0); |
68 | 0 | if (errno == ERANGE) |
69 | 0 | return 0; |
70 | 0 | if (end == value) { |
71 | 0 | errno = EINVAL; |
72 | 0 | return 0; |
73 | 0 | } |
74 | 0 | factor = get_unit_factor(end); |
75 | 0 | if (!factor) { |
76 | 0 | errno = EINVAL; |
77 | 0 | return 0; |
78 | 0 | } |
79 | 0 | if (unsigned_mult_overflows(factor, val) || |
80 | 0 | factor * val > max) { |
81 | 0 | errno = ERANGE; |
82 | 0 | return 0; |
83 | 0 | } |
84 | 0 | val *= factor; |
85 | 0 | *ret = val; |
86 | 0 | return 1; |
87 | 0 | } |
88 | 0 | errno = EINVAL; |
89 | 0 | return 0; |
90 | 0 | } |
91 | | |
92 | | int git_parse_int(const char *value, int *ret) |
93 | 0 | { |
94 | 0 | intmax_t tmp; |
95 | 0 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int))) |
96 | 0 | return 0; |
97 | 0 | *ret = tmp; |
98 | 0 | return 1; |
99 | 0 | } |
100 | | |
101 | | int git_parse_int64(const char *value, int64_t *ret) |
102 | 0 | { |
103 | 0 | intmax_t tmp; |
104 | 0 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t))) |
105 | 0 | return 0; |
106 | 0 | *ret = tmp; |
107 | 0 | return 1; |
108 | 0 | } |
109 | | |
110 | | int git_parse_uint(const char *value, unsigned int *ret) |
111 | 0 | { |
112 | 0 | uintmax_t tmp; |
113 | 0 | if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(unsigned int))) |
114 | 0 | return 0; |
115 | 0 | *ret = tmp; |
116 | 0 | return 1; |
117 | 0 | } |
118 | | |
119 | | int git_parse_ulong(const char *value, unsigned long *ret) |
120 | 0 | { |
121 | 0 | uintmax_t tmp; |
122 | 0 | if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long))) |
123 | 0 | return 0; |
124 | 0 | *ret = tmp; |
125 | 0 | return 1; |
126 | 0 | } |
127 | | |
128 | | int git_parse_ssize_t(const char *value, ssize_t *ret) |
129 | 0 | { |
130 | 0 | intmax_t tmp; |
131 | 0 | if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(ssize_t))) |
132 | 0 | return 0; |
133 | 0 | *ret = tmp; |
134 | 0 | return 1; |
135 | 0 | } |
136 | | |
137 | | int git_parse_size_t(const char *value, size_t *ret) |
138 | 0 | { |
139 | 0 | uintmax_t tmp; |
140 | 0 | if (!git_parse_unsigned(value, &tmp, maximum_signed_value_of_type(size_t))) |
141 | 0 | return 0; |
142 | 0 | *ret = tmp; |
143 | 0 | return 1; |
144 | 0 | } |
145 | | |
146 | | int git_parse_double(const char *value, double *ret) |
147 | 0 | { |
148 | 0 | char *end; |
149 | 0 | double val; |
150 | 0 | uintmax_t factor; |
151 | |
|
152 | 0 | if (!value || !*value) { |
153 | 0 | errno = EINVAL; |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | 0 | errno = 0; |
158 | 0 | val = strtod(value, &end); |
159 | 0 | if (errno == ERANGE) |
160 | 0 | return 0; |
161 | 0 | if (end == value) { |
162 | 0 | errno = EINVAL; |
163 | 0 | return 0; |
164 | 0 | } |
165 | 0 | factor = get_unit_factor(end); |
166 | 0 | if (!factor) { |
167 | 0 | errno = EINVAL; |
168 | 0 | return 0; |
169 | 0 | } |
170 | 0 | val *= factor; |
171 | 0 | *ret = val; |
172 | 0 | return 1; |
173 | 0 | } |
174 | | |
175 | | int git_parse_maybe_bool_text(const char *value) |
176 | 0 | { |
177 | 0 | if (!value) |
178 | 0 | return 1; |
179 | 0 | if (!*value) |
180 | 0 | return 0; |
181 | 0 | if (!strcasecmp(value, "true") |
182 | 0 | || !strcasecmp(value, "yes") |
183 | 0 | || !strcasecmp(value, "on")) |
184 | 0 | return 1; |
185 | 0 | if (!strcasecmp(value, "false") |
186 | 0 | || !strcasecmp(value, "no") |
187 | 0 | || !strcasecmp(value, "off")) |
188 | 0 | return 0; |
189 | 0 | return -1; |
190 | 0 | } |
191 | | |
192 | | int git_parse_maybe_bool(const char *value) |
193 | 0 | { |
194 | 0 | int v = git_parse_maybe_bool_text(value); |
195 | 0 | if (0 <= v) |
196 | 0 | return v; |
197 | 0 | if (git_parse_int(value, &v)) |
198 | 0 | return !!v; |
199 | 0 | return -1; |
200 | 0 | } |
201 | | |
202 | | /* |
203 | | * Parse environment variable 'k' as a boolean (in various |
204 | | * possible spellings); if missing, use the default value 'def'. |
205 | | */ |
206 | | int git_env_bool(const char *k, int def) |
207 | 0 | { |
208 | 0 | const char *v = getenv(k); |
209 | 0 | int val; |
210 | 0 | if (!v) |
211 | 0 | return def; |
212 | 0 | val = git_parse_maybe_bool(v); |
213 | 0 | if (val < 0) |
214 | 0 | die(_("bad boolean environment value '%s' for '%s'"), |
215 | 0 | v, k); |
216 | 0 | return val; |
217 | 0 | } |
218 | | |
219 | | /* |
220 | | * Parse environment variable 'k' as ulong with possibly a unit |
221 | | * suffix; if missing, use the default value 'val'. |
222 | | */ |
223 | | unsigned long git_env_ulong(const char *k, unsigned long val) |
224 | 0 | { |
225 | 0 | const char *v = getenv(k); |
226 | 0 | if (v && !git_parse_ulong(v, &val)) |
227 | 0 | die(_("failed to parse %s"), k); |
228 | 0 | return val; |
229 | 0 | } |