Debian Codegraph @

m4/vasnprintf.m4

raw
1 # vasnprintf.m4
2 # serial 54
3 dnl Copyright (C) 2002-2004, 2006-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
7 dnl This file is offered as-is, without any warranty.
8
9 AC_DEFUN([gl_FUNC_VASNPRINTF],
10 [
11 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
12 AC_CHECK_FUNCS_ONCE([vasnprintf])
13 if test $ac_cv_func_vasnprintf = yes; then
14 dnl On Cygwin, in directives with a huge width, the width is ignored, and
15 dnl the function returns a wrong result.
16 AC_CACHE_CHECK([whether vasnprintf works],
17 [gl_cv_func_vasnprintf_works],
18 [AC_RUN_IFELSE(
19 [AC_LANG_SOURCE(
20 [[#include <stdio.h>
21 ]],
22 [[size_t len;
23 char *res = vasnprintf (NULL, &len, "x%03000000000dy\n", -17);
24 /* On Cygwin 3.4.6, res is "x-17y\n" and len == 6: wrong. */
25 return (res != NULL && len < 10);
26 ]])
27 ],
28 [gl_cv_func_vasnprintf_works=yes],
29 [gl_cv_func_vasnprintf_works=no],
30 [case "$host_os" in
31 # Guess no on Cygwin.
32 cygwin*) gl_cv_func_vasnprintf_works="guessing no";;
33 # If we don't know, obey --enable-cross-guesses.
34 *) gl_cv_func_vasnprintf_works="$gl_cross_guess_normal";;
35 esac
36 ])
37 ])
38 fi
39 if test $ac_cv_func_vasnprintf != yes \
40 || case "$gl_cv_func_vasnprintf_works" in
41 *yes) false;;
42 *) true;;
43 esac
44 then
45 gl_REPLACE_VASNPRINTF
46 fi
47 ])
48
49 AC_DEFUN([gl_REPLACE_VASNPRINTF],
50 [
51 AC_CHECK_FUNCS_ONCE([vasnprintf])
52 AC_LIBOBJ([vasnprintf])
53 AC_LIBOBJ([printf-args])
54 AC_LIBOBJ([printf-parse])
55 AC_LIBOBJ([asnprintf])
56 if test $ac_cv_func_vasnprintf = yes; then
57 AC_DEFINE([REPLACE_VASNPRINTF], [1],
58 [Define if vasnprintf exists but is overridden by gnulib.])
59 fi
60 gl_PREREQ_PRINTF_ARGS
61 gl_PREREQ_PRINTF_PARSE
62 gl_PREREQ_VASNPRINTF
63 gl_PREREQ_ASNPRINTF
64 ])
65
66 AC_DEFUN([gl_FUNC_VASNWPRINTF],
67 [
68 AC_LIBOBJ([printf-args])
69 gl_PREREQ_PRINTF_ARGS
70 gl_PREREQ_PRINTF_PARSE
71 gl_PREREQ_VASNWPRINTF
72 gl_PREREQ_ASNPRINTF
73 ])
74
75 # Prerequisites of lib/printf-args.h, lib/printf-args.c.
76 AC_DEFUN([gl_PREREQ_PRINTF_ARGS],
77 [
78 AC_REQUIRE([gt_TYPE_WINT_T])
79 ])
80
81 # Prerequisites of lib/printf-parse.h, lib/printf-parse.c.
82 # Prerequisites of lib/wprintf-parse.h, lib/wprintf-parse.c.
83 AC_DEFUN([gl_PREREQ_PRINTF_PARSE],
84 [
85 AC_REQUIRE([gl_FEATURES_H])
86 AC_REQUIRE([gt_TYPE_WINT_T])
87 AC_REQUIRE([AC_TYPE_SIZE_T])
88 AC_CHECK_TYPE([ptrdiff_t], ,
89 [AC_DEFINE([ptrdiff_t], [long],
90 [Define as the type of the result of subtracting two pointers, if the system doesn't define it.])
91 ])
92 AC_REQUIRE([gt_AC_TYPE_INTMAX_T])
93 ])
94
95 # Prerequisites of lib/vasnprintf.c if !WIDE_CHAR_VERSION.
96 AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF],
97 [
98 AC_CHECK_FUNCS([snprintf strnlen wcrtomb])
99 dnl Use the _snprintf function only if it is declared (because on NetBSD it
100 dnl is defined as a weak alias of snprintf; we prefer to use the latter).
101 AC_CHECK_DECLS([_snprintf], , , [[#include <stdio.h>]])
102 dnl We can avoid a lot of code by assuming that snprintf's return value
103 dnl conforms to ISO C99. So check that.
104 AC_REQUIRE([gl_SNPRINTF_RETVAL_C99])
105 case "$gl_cv_func_snprintf_retval_c99" in
106 *yes)
107 AC_DEFINE([HAVE_SNPRINTF_RETVAL_C99], [1],
108 [Define if the return value of the snprintf function is the number of
109 of bytes (excluding the terminating NUL) that would have been produced
110 if the buffer had been large enough.])
111 ;;
112 esac
113 dnl Additionally, the use of %n can be eliminated by assuming that snprintf
114 dnl always produces NUL-terminated strings (no truncation).
115 AC_REQUIRE([gl_SNPRINTF_TRUNCATION_C99])
116 case "$gl_cv_func_snprintf_truncation_c99" in
117 *yes)
118 AC_DEFINE([HAVE_SNPRINTF_TRUNCATION_C99], [1],
119 [Define if the string produced by the snprintf function is always NUL
120 terminated.])
121 ;;
122 esac
123 gl_PREREQ_VASNXPRINTF
124 ])
125
126 # Prerequisites of lib/vasnwprintf.c.
127 AC_DEFUN_ONCE([gl_PREREQ_VASNWPRINTF],
128 [
129 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
130 AC_CHECK_FUNCS_ONCE([swprintf wcsnlen mbrtowc])
131 AC_CHECK_DECLS([_snwprintf], , , [[#include <stdio.h>]])
132 AC_CHECK_DECLS([wcsnlen], , , [[#include <wchar.h>]])
133 gl_SWPRINTF_WORKS
134 case "$gl_cv_func_swprintf_works" in
135 *yes)
136 AC_DEFINE([HAVE_WORKING_SWPRINTF], [1],
137 [Define if the swprintf function works correctly when it produces output
138 that contains null wide characters.])
139 ;;
140 esac
141 gl_MBRTOWC_C_LOCALE
142 case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
143 *yes)
144 AC_CACHE_CHECK([whether swprintf in the C locale is free of encoding errors],
145 [gl_cv_func_swprintf_C_locale_sans_EILSEQ],
146 [
147 AC_RUN_IFELSE(
148 [AC_LANG_SOURCE([[
149 #ifndef __USE_MINGW_ANSI_STDIO
150 # define __USE_MINGW_ANSI_STDIO 1
151 #endif
152 #include <stdio.h>
153 #include <wchar.h>
154 int main()
155 {
156 int result = 0;
157 { /* This test fails on glibc 2.35, musl libc 1.2.4, FreeBSD 13.2, NetBSD 9.3,
158 OpenBSD 7.2, Cygwin 2.9.0.
159 Reported at <https://www.openwall.com/lists/musl/2023/06/12/2>. */
160 wchar_t buf[12];
161 int ret = swprintf (buf, 12, L"%c", '\377');
162 if (ret < 0)
163 result |= 1;
164 }
165 return result;
166 }]])],
167 [gl_cv_func_swprintf_C_locale_sans_EILSEQ=yes],
168 [gl_cv_func_swprintf_C_locale_sans_EILSEQ=no],
169 [case "$host_os" in
170 # Guess no on glibc systems.
171 *-gnu* | gnu*) gl_cv_func_swprintf_C_locale_sans_EILSEQ="guessing yes";;
172 # Guess no on musl systems.
173 *-musl* | midipix*) gl_cv_func_swprintf_C_locale_sans_EILSEQ="guessing no";;
174 # If we don't know, obey --enable-cross-guesses.
175 *) gl_cv_func_swprintf_C_locale_sans_EILSEQ="$gl_cross_guess_normal";;
176 esac
177 ])
178 ])
179 ;;
180 esac
181 if case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
182 *yes) false ;;
183 *) true ;;
184 esac \
185 || case "$gl_cv_func_swprintf_C_locale_sans_EILSEQ" in
186 *yes) false ;;
187 *) true ;;
188 esac; then
189 AC_DEFINE([NEED_WPRINTF_DIRECTIVE_C], [1],
190 [Define if the vasnwprintf implementation needs special code for
191 the 'c' directive.])
192 fi
193 gl_SWPRINTF_DIRECTIVE_LA
194 case "$gl_cv_func_swprintf_directive_la" in
195 *yes) ;;
196 *)
197 AC_DEFINE([NEED_WPRINTF_DIRECTIVE_LA], [1],
198 [Define if the vasnwprintf implementation needs special code for
199 the 'a' directive with 'long double' arguments.])
200 ;;
201 esac
202 gl_SWPRINTF_DIRECTIVE_LC
203 case "$gl_cv_func_swprintf_directive_lc" in
204 *yes) ;;
205 *)
206 AC_DEFINE([NEED_WPRINTF_DIRECTIVE_LC], [1],
207 [Define if the vasnwprintf implementation needs special code for
208 the 'lc' directive.])
209 ;;
210 esac
211 gl_MUSL_LIBC
212 gl_PREREQ_VASNXPRINTF
213 ])
214
215 # Common prerequisites of lib/vasnprintf.c and lib/vasnwprintf.c.
216 AC_DEFUN_ONCE([gl_PREREQ_VASNXPRINTF],
217 [
218 AC_REQUIRE([AC_FUNC_ALLOCA])
219 AC_REQUIRE([gt_TYPE_WINT_T])
220 AC_CHECK_FUNCS([wcslen])
221 dnl Knowing DBL_EXPBIT0_WORD and DBL_EXPBIT0_BIT enables an optimization
222 dnl in the code for NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE.
223 AC_REQUIRE([gl_DOUBLE_EXPONENT_LOCATION])
224 ])
225
226 # Extra prerequisites of lib/vasnprintf.c for supporting 'long double'
227 # arguments.
228 AC_DEFUN_ONCE([gl_PREREQ_VASNPRINTF_LONG_DOUBLE],
229 [
230 AC_REQUIRE([gl_PRINTF_LONG_DOUBLE])
231 case "$gl_cv_func_printf_long_double" in
232 *yes)
233 ;;
234 *)
235 AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
236 [Define if the vasnprintf implementation needs special code for
237 'long double' arguments.])
238 ;;
239 esac
240 ])
241
242 # Extra prerequisites of lib/vasnprintf.c for supporting infinite 'double'
243 # arguments.
244 AC_DEFUN([gl_PREREQ_VASNPRINTF_INFINITE_DOUBLE],
245 [
246 AC_REQUIRE([gl_PRINTF_INFINITE])
247 case "$gl_cv_func_printf_infinite" in
248 *yes)
249 ;;
250 *)
251 AC_DEFINE([NEED_PRINTF_INFINITE_DOUBLE], [1],
252 [Define if the vasnprintf implementation needs special code for
253 infinite 'double' arguments.])
254 ;;
255 esac
256 ])
257
258 # Extra prerequisites of lib/vasnprintf.c for supporting infinite 'long double'
259 # arguments.
260 AC_DEFUN([gl_PREREQ_VASNPRINTF_INFINITE_LONG_DOUBLE],
261 [
262 AC_REQUIRE([gl_PRINTF_INFINITE_LONG_DOUBLE])
263 dnl There is no need to set NEED_PRINTF_INFINITE_LONG_DOUBLE if
264 dnl NEED_PRINTF_LONG_DOUBLE is already set.
265 AC_REQUIRE([gl_PREREQ_VASNPRINTF_LONG_DOUBLE])
266 case "$gl_cv_func_printf_long_double" in
267 *yes)
268 case "$gl_cv_func_printf_infinite_long_double" in
269 *yes)
270 ;;
271 *)
272 AC_DEFINE([NEED_PRINTF_INFINITE_LONG_DOUBLE], [1],
273 [Define if the vasnprintf implementation needs special code for
274 infinite 'long double' arguments.])
275 ;;
276 esac
277 ;;
278 esac
279 ])
280
281 # Extra prerequisites of lib/vasnprintf.c for supporting the 'a' directive.
282 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_A],
283 [
284 AC_REQUIRE([gl_PRINTF_DIRECTIVE_A])
285 case "$gl_cv_func_printf_directive_a" in
286 *yes)
287 ;;
288 *)
289 AC_DEFINE([NEED_PRINTF_DIRECTIVE_A], [1],
290 [Define if the vasnprintf implementation needs special code for
291 the 'a' and 'A' directives.])
292 gl_CHECK_FUNCS_ANDROID([nl_langinfo], [[#include <langinfo.h>]])
293 ;;
294 esac
295 ])
296
297 # Extra prerequisites of lib/vasnprintf.c for supporting the 'b' directive.
298 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_B],
299 [
300 AC_REQUIRE([gl_PRINTF_DIRECTIVE_B])
301 case "$gl_cv_func_printf_directive_b" in
302 *yes)
303 ;;
304 *)
305 AC_DEFINE([NEED_PRINTF_DIRECTIVE_B], [1],
306 [Define if the vasnprintf implementation needs special code for
307 the 'b' directive.])
308 ;;
309 esac
310 ])
311
312 # Extra prerequisites of lib/vasnprintf.c for supporting the 'F' directive.
313 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_F],
314 [
315 AC_REQUIRE([gl_PRINTF_DIRECTIVE_F])
316 case "$gl_cv_func_printf_directive_f" in
317 *yes)
318 ;;
319 *)
320 AC_DEFINE([NEED_PRINTF_DIRECTIVE_F], [1],
321 [Define if the vasnprintf implementation needs special code for
322 the 'F' directive.])
323 ;;
324 esac
325 ])
326
327 # Extra prerequisites of lib/vasnprintf.c for supporting the 'ls' directive.
328 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_LS],
329 [
330 AC_REQUIRE([gl_PRINTF_DIRECTIVE_LS])
331 case "$gl_cv_func_printf_directive_ls" in
332 *yes)
333 ;;
334 *)
335 AC_DEFINE([NEED_PRINTF_DIRECTIVE_LS], [1],
336 [Define if the vasnprintf implementation needs special code for
337 the 'ls' directive.])
338 ;;
339 esac
340 ])
341
342 # Extra prerequisites of lib/vasnprintf.c for supporting the 'lc' directive.
343 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_LC],
344 [
345 AC_REQUIRE([gl_PRINTF_DIRECTIVE_LC])
346 case "$gl_cv_func_printf_directive_lc" in
347 *yes)
348 ;;
349 *)
350 AC_DEFINE([NEED_PRINTF_DIRECTIVE_LC], [1],
351 [Define if the vasnprintf implementation needs special code for
352 the 'lc' directive.])
353 ;;
354 esac
355 ])
356
357 # Extra prerequisites of lib/vasnprintf.c for supporting the ' flag.
358 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_GROUPING],
359 [
360 AC_REQUIRE([gl_PRINTF_FLAG_GROUPING])
361 case "$gl_cv_func_printf_flag_grouping" in
362 *yes)
363 ;;
364 *)
365 AC_DEFINE([NEED_PRINTF_FLAG_GROUPING], [1],
366 [Define if the vasnprintf implementation needs special code for the
367 ' flag.])
368 ;;
369 esac
370 ])
371
372 # Extra prerequisites of lib/vasnprintf.c for supporting the '-' flag.
373 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_LEFTADJUST],
374 [
375 AC_REQUIRE([gl_PRINTF_FLAG_LEFTADJUST])
376 case "$gl_cv_func_printf_flag_leftadjust" in
377 *yes)
378 ;;
379 *)
380 AC_DEFINE([NEED_PRINTF_FLAG_LEFTADJUST], [1],
381 [Define if the vasnprintf implementation needs special code for the
382 '-' flag.])
383 ;;
384 esac
385 ])
386
387 # Extra prerequisites of lib/vasnprintf.c for supporting the 0 flag.
388 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_ZERO],
389 [
390 AC_REQUIRE([gl_PRINTF_FLAG_ZERO])
391 case "$gl_cv_func_printf_flag_zero" in
392 *yes)
393 ;;
394 *)
395 AC_DEFINE([NEED_PRINTF_FLAG_ZERO], [1],
396 [Define if the vasnprintf implementation needs special code for the
397 0 flag.])
398 ;;
399 esac
400 ])
401
402 # Extra prerequisites of lib/vasnprintf.c for supporting the # flag with a
403 # zero precision and a zero value in the 'x' and 'X' directives.
404 AC_DEFUN([gl_PREREQ_VASNPRINTF_FLAG_ALT_PRECISION_ZERO],
405 [
406 AC_REQUIRE([gl_PRINTF_FLAG_ALT_PRECISION_ZERO])
407 case "$gl_cv_func_printf_flag_alt_precision_zero" in
408 *yes)
409 ;;
410 *)
411 AC_DEFINE([NEED_PRINTF_FLAG_ALT_PRECISION_ZERO], [1],
412 [Define if the vasnprintf implementation needs special code for the
413 # flag with a zero precision and a zero value in the 'x' and 'X' directives.])
414 ;;
415 esac
416 ])
417
418 # Extra prerequisites of lib/vasnprintf.c for supporting large precisions.
419 AC_DEFUN([gl_PREREQ_VASNPRINTF_PRECISION],
420 [
421 AC_REQUIRE([gl_PRINTF_PRECISION])
422 case "$gl_cv_func_printf_precision" in
423 *yes)
424 ;;
425 *)
426 AC_DEFINE([NEED_PRINTF_UNBOUNDED_PRECISION], [1],
427 [Define if the vasnprintf implementation needs special code for
428 supporting large precisions without arbitrary bounds.])
429 AC_DEFINE([NEED_PRINTF_DOUBLE], [1],
430 [Define if the vasnprintf implementation needs special code for
431 'double' arguments.])
432 AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
433 [Define if the vasnprintf implementation needs special code for
434 'long double' arguments.])
435 ;;
436 esac
437 ])
438
439 # Extra prerequisites of lib/vasnprintf.c for surviving out-of-memory
440 # conditions.
441 AC_DEFUN([gl_PREREQ_VASNPRINTF_ENOMEM],
442 [
443 AC_REQUIRE([gl_PRINTF_ENOMEM])
444 case "$gl_cv_func_printf_enomem" in
445 *yes)
446 ;;
447 *)
448 AC_DEFINE([NEED_PRINTF_ENOMEM], [1],
449 [Define if the vasnprintf implementation needs special code for
450 surviving out-of-memory conditions.])
451 AC_DEFINE([NEED_PRINTF_DOUBLE], [1],
452 [Define if the vasnprintf implementation needs special code for
453 'double' arguments.])
454 AC_DEFINE([NEED_PRINTF_LONG_DOUBLE], [1],
455 [Define if the vasnprintf implementation needs special code for
456 'long double' arguments.])
457 ;;
458 esac
459 ])
460
461 # Prerequisites of lib/vasnprintf.c including all extras for POSIX compliance.
462 AC_DEFUN([gl_PREREQ_VASNPRINTF_WITH_POSIX_EXTRAS],
463 [
464 AC_REQUIRE([gl_PREREQ_VASNPRINTF])
465 gl_PREREQ_VASNPRINTF_LONG_DOUBLE
466 gl_PREREQ_VASNPRINTF_INFINITE_DOUBLE
467 gl_PREREQ_VASNPRINTF_INFINITE_LONG_DOUBLE
468 gl_PREREQ_VASNPRINTF_DIRECTIVE_A
469 gl_PREREQ_VASNPRINTF_DIRECTIVE_B
470 gl_PREREQ_VASNPRINTF_DIRECTIVE_F
471 gl_PREREQ_VASNPRINTF_DIRECTIVE_LS
472 gl_PREREQ_VASNPRINTF_DIRECTIVE_LC
473 gl_PREREQ_VASNPRINTF_FLAG_GROUPING
474 gl_PREREQ_VASNPRINTF_FLAG_LEFTADJUST
475 gl_PREREQ_VASNPRINTF_FLAG_ZERO
476 gl_PREREQ_VASNPRINTF_FLAG_ALT_PRECISION_ZERO
477 gl_PREREQ_VASNPRINTF_PRECISION
478 gl_PREREQ_VASNPRINTF_ENOMEM
479 ])
480
481 # Extra prerequisites of lib/vasnprintf.c for supporting the 'B' directive.
482 AC_DEFUN([gl_PREREQ_VASNPRINTF_DIRECTIVE_UPPERCASE_B],
483 [
484 AC_REQUIRE([gl_PRINTF_DIRECTIVE_UPPERCASE_B])
485 case "$gl_cv_func_printf_directive_uppercase_b" in
486 *yes)
487 ;;
488 *)
489 AC_DEFINE([NEED_PRINTF_DIRECTIVE_UPPERCASE_B], [1],
490 [Define if the vasnprintf implementation needs special code for
491 the 'B' directive.])
492 ;;
493 esac
494 ])
495
496 # Prerequisites of lib/vasnprintf.c including all extras for POSIX compliance
497 # and GNU compatibility.
498 AC_DEFUN([gl_PREREQ_VASNPRINTF_WITH_GNU_EXTRAS],
499 [
500 gl_PREREQ_VASNPRINTF_WITH_POSIX_EXTRAS
501 AC_DEFINE([SUPPORT_GNU_PRINTF_DIRECTIVES], [1],
502 [Define if the vasnprintf implementation should support GNU compatible
503 printf directives.])
504 gl_PREREQ_VASNPRINTF_DIRECTIVE_UPPERCASE_B
505 ])
506
507 # Prerequisites of lib/asnprintf.c.
508 # Prerequisites of lib/asnwprintf.c.
509 AC_DEFUN([gl_PREREQ_ASNPRINTF],
510 [
511 ])