a260998a8b07c7e11504468df07de73f662e4f5a
[gnulib.git] / m4 / wcwidth.m4
1 # wcwidth.m4 serial 11
2 dnl Copyright (C) 2006, 2007 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_WCWIDTH],
8 [
9   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
10
11   dnl Persuade glibc <wchar.h> to declare wcwidth().
12   AC_REQUIRE([AC_GNU_SOURCE])
13
14   AC_REQUIRE([gt_TYPE_WCHAR_T])
15   AC_REQUIRE([gt_TYPE_WINT_T])
16
17   AC_CHECK_HEADERS_ONCE([wchar.h])
18   AC_CHECK_FUNCS_ONCE([wcwidth])
19
20   AC_CHECK_DECLS([wcwidth], [], [], [
21 /* AIX 3.2.5 declares wcwidth in <string.h>. */
22 #include <string.h>
23 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
24    <wchar.h>.
25    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included
26    before <wchar.h>.  */
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <time.h>
30 #include <wchar.h>
31 ])
32   if test $ac_cv_have_decl_wcwidth != yes; then
33     HAVE_DECL_WCWIDTH=0
34   fi
35
36   if test $ac_cv_func_wcwidth = no; then
37     REPLACE_WCWIDTH=1
38   else
39     dnl On MacOS X 10.3, wcwidth(0x0301) (COMBINING ACUTE ACCENT) returns 1.
40     dnl This leads to bugs in 'ls' (coreutils).
41     AC_CACHE_CHECK([whether wcwidth works reasonably in UTF-8 locales],
42       [gl_cv_func_wcwidth_works],
43       [
44         AC_TRY_RUN([
45 #include <locale.h>
46 /* AIX 3.2.5 declares wcwidth in <string.h>. */
47 #include <string.h>
48 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
49    <wchar.h>.
50    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be included
51    before <wchar.h>.  */
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <time.h>
55 #include <wchar.h>
56 #if !HAVE_DECL_WCWIDTH
57 extern
58 # ifdef __cplusplus
59 "C"
60 # endif
61 int wcwidth (int);
62 #endif
63 int main ()
64 {
65   if (setlocale (LC_ALL, "fr_FR.UTF-8") != NULL)
66     if (wcwidth (0x0301) > 0)
67       return 1;
68   return 0;
69 }], [gl_cv_func_wcwidth_works=yes], [gl_cv_func_wcwidth_works=no],
70           [gl_cv_func_wcwidth_works="guessing no"])
71       ])
72     case "$gl_cv_func_wcwidth_works" in
73       *yes) ;;
74       *no) REPLACE_WCWIDTH=1 ;;
75     esac
76   fi
77   if test $REPLACE_WCWIDTH = 1; then
78     AC_LIBOBJ([wcwidth])
79   fi
80
81   if test $REPLACE_WCWIDTH = 1 || test $HAVE_DECL_WCWIDTH = 0; then
82     WCHAR_H=wchar.h
83   fi
84 ])