From f9fa482b833dfc0eb4475386099266f9fb08f012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 21 Sep 2022 16:03:51 +0800 Subject: [PATCH] Locale: try detecting with `locale` command in case $LC_* envs are not set ( macOS ) --- src/common/processing.c | 8 +++++--- src/common/processing.h | 2 +- src/modules/locale.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/common/processing.c b/src/common/processing.c index d361a63af..d8bb4e0bd 100644 --- a/src/common/processing.c +++ b/src/common/processing.c @@ -6,16 +6,16 @@ #include #include -void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]) +const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]) { int pipes[2]; if(pipe(pipes) == -1) - return; + return "pipe() failed"; pid_t childPid = fork(); if(childPid == -1) - return; + return "fork() failed"; //Child if(childPid == 0) @@ -33,4 +33,6 @@ void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]) waitpid(childPid, NULL, 0); ffAppendFDBuffer(pipes[0], buffer); close(pipes[0]); + + return NULL; } diff --git a/src/common/processing.h b/src/common/processing.h index 8bb67be85..51164b0e9 100644 --- a/src/common/processing.h +++ b/src/common/processing.h @@ -5,6 +5,6 @@ #include "util/FFstrbuf.h" -void ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]); +const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]); #endif diff --git a/src/modules/locale.c b/src/modules/locale.c index 5b5b63269..444ed5a11 100644 --- a/src/modules/locale.c +++ b/src/modules/locale.c @@ -2,6 +2,8 @@ #include "common/properties.h" #include "common/printing.h" #include "common/caching.h" +#include "common/processing.h" +#include "common/parsing.h" #include @@ -21,6 +23,31 @@ static void getLocaleFromEnv(FFstrbuf* locale) ffStrbufAppendS(locale, getenv("LC_MESSAGES")); } +static void getLocaleFromCmd(FFstrbuf* locale) +{ + FFstrbuf buffer; + ffStrbufInitA(&buffer, 0); + char* args[] = { "locale", NULL }; + if(ffProcessAppendStdOut(&buffer, args) == NULL) + { + ffParsePropLines(buffer.chars, "LANG=\"", locale); + ffStrbufTrimRight(locale, '"'); + + if(locale->length == 0) + { + ffParsePropLines(buffer.chars, "LC_ALL=\"", locale); + ffStrbufTrimRight(locale, '"'); + + if(locale->length == 0) + { + ffParsePropLines(buffer.chars, "LC_MESSAGES=\"", locale); + ffStrbufTrimRight(locale, '"'); + } + } + } + ffStrbufDestroy(&buffer); +} + void ffPrintLocale(FFinstance* instance) { if(ffPrintFromCache(instance, FF_LOCALE_MODULE_NAME, &instance->config.locale, FF_LOCALE_NUM_FORMAT_ARGS)) @@ -44,6 +71,11 @@ void ffPrintLocale(FFinstance* instance) getLocaleFromEnv(&locale); } + if(locale.length == 0) + { + getLocaleFromCmd(&locale); + } + if(locale.length == 0) { ffPrintError(instance, FF_LOCALE_MODULE_NAME, 0, &instance->config.locale, "No locale found");