Move setlocale() from rline() to the top of main()

This commit is contained in:
Klaus Alexander Seistrup 2024-02-26 04:24:40 +01:00
parent 0c8cb94b6a
commit 7976aa42f9
2 changed files with 22 additions and 8 deletions

View File

@ -5,6 +5,7 @@
* or executes scripts from the argument list.
*/
#define _DEFAULT_SOURCE
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@ -759,6 +760,26 @@ static int compileFile(char * argv[], int flags, char * fileName) {
return func == NULL;
}
void initializeLocale(void) {
char *myLocale;
#ifndef _WIN32
myLocale = setlocale(LC_ALL, "");
if (myLocale != NULL) {
struct lconv *lc = localeconv();
char *decimalPoint = lc->decimal_point;
if (decimalPoint) {
if (strcmp(decimalPoint, ".") != 0) {
myLocale = setlocale(LC_ALL, "C.UTF-8");
}
}
} else {
myLocale = setlocale(LC_ALL, "C.UTF-8");
}
#else
myLocale = setlocale(LC_ALL, "C.UTF-8");
#endif
}
int main(int argc, char * argv[]) {
#ifdef _WIN32
SetConsoleOutputCP(65001);
@ -770,6 +791,7 @@ int main(int argc, char * argv[]) {
int inspectAfter = 0;
int opt;
int maxDepth = -1;
initializeLocale();
while ((opt = getopt(argc, argv, "+:c:C:dgGim:rR:tTMSV-:")) != -1) {
switch (opt) {
case 'c':

8
src/vendor/rline.c vendored
View File

@ -21,7 +21,6 @@
#include <string.h>
#include <wchar.h>
#include <unistd.h>
#include <locale.h>
#include <signal.h>
#ifndef _WIN32
#include <termios.h>
@ -2429,13 +2428,6 @@ static int read_line(void) {
* Read a line of text with interactive editing.
*/
int rline(char * buffer, int buf_size) {
#ifndef _WIN32
setlocale(LC_ALL, "");
/* If the requested locale doesn't use . as a radix point, fall back to C to not break float parsing. */
if (strtod("0.5",NULL) != 0.5) setlocale(LC_ALL, "C.UTF-8");
#else
setlocale(LC_ALL, "C.UTF-8");
#endif
get_initial_termios();
set_unbuffered();
get_size();