Skip to content

Commit 11c528d

Browse files
committed
Add setlocale() call (fix #1740)
1 parent ac3b70d commit 11c528d

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ AC_CHECK_MEMBER([struct tm.tm_gmtoff], [AC_DEFINE([HAVE_TM_TM_GMT_OFF],1,[Define
149149
[], [[#include <time.h>]])
150150
AC_CHECK_MEMBER([struct tm.__tm_gmtoff], [AC_DEFINE([HAVE_TM___TM_GMT_OFF],1,[Define to 1 if the system has the __tm_gmt_off field in struct tm])],
151151
[], [[#include <time.h>]])
152+
AC_FIND_FUNC([setlocale], [c], [#include <locale.h>], [0,0])
152153

153154
dnl Figure out if we have the pthread functions we actually need
154155
AC_FIND_FUNC_NO_LIBS([pthread_key_create], [], [#include <pthread.h>], [NULL, NULL])

src/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include <ctype.h>
33
#include <errno.h>
44
#include <libgen.h>
5+
#ifdef HAVE_SETLOCALE
6+
#include <locale.h>
7+
#endif
58
#include <stdlib.h>
69
#include <stdio.h>
710
#include <string.h>
@@ -319,6 +322,10 @@ int main(int argc, char* argv[]) {
319322
jv ARGS = jv_array(); /* positional arguments */
320323
jv program_arguments = jv_object(); /* named arguments */
321324

325+
#ifdef HAVE_SETLOCALE
326+
(void) setlocale(LC_ALL, "");
327+
#endif
328+
322329
#ifdef WIN32
323330
jv_tsd_dtoa_ctx_init();
324331
fflush(stdout);

tests/shtest

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
. "${0%/*}/setup" "$@"
44

5+
msys=false
6+
mingw=false
7+
case "$(uname -s)" in
8+
MSYS*) msys=true;;
9+
MINGW*) mingw=true;;
10+
esac
11+
512
JQ_NO_B=$JQ
613
JQ="$JQ -b"
714

@@ -496,11 +503,9 @@ cmp $d/color $d/expect
496503
cmp $d/warning $d/expect_warning
497504

498505
# Check $NO_COLOR
499-
case "$(uname -s)" in
500-
MSYS*) test_no_color=false;;
501-
MINGW*) test_no_color=false;;
502-
*) test_no_color=true;;
503-
esac
506+
test_no_color=true
507+
$msys && test_no_color=false
508+
$mingw && test_no_color=false
504509
if $test_no_color && command -v script >/dev/null 2>&1; then
505510
unset NO_COLOR
506511
if script -qc echo /dev/null >/dev/null 2>&1; then
@@ -561,4 +566,17 @@ if ! $VALGRIND $Q $JQ -n -f "$JQTESTDIR/yes-main-program.jq" > $d/out 2>&1; then
561566
exit 1
562567
fi
563568

569+
if ( ! $msys && ! $mingw ) && locale -a > /dev/null; then
570+
locale=$(locale -a | egrep -v '^(C|LANG|POSIX|en)' | egrep -i 'utf8|utf-8' | head -1)
571+
if [ -z "$locale" ]; then
572+
echo "WARNING: Not testing localization"
573+
else
574+
date=$(LC_ALL="$locale" date +"%a %d %b %Y at %H:%M:%S")
575+
if ! LC_ALL="$locale" ./jq -nRe --arg date "$date" '$date|strptime("%a %d %b %Y at %H:%M:%S")? // false'; then
576+
echo "jq does not honor LC_ALL environment variable"
577+
exit 1;
578+
fi
579+
fi
580+
fi
581+
564582
exit 0

0 commit comments

Comments
 (0)