notcurses_init(): call setlocale unless INHIBIT_SETLOCALE #579

This commit is contained in:
nick black 2020-05-09 04:46:28 -04:00
parent d89d8c52e9
commit fcc73856cb
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
3 changed files with 16 additions and 0 deletions

View File

@ -92,6 +92,10 @@ typedef struct notcurses_options {
// strictly best-effort. Absolute coordinates are relative to the rendering
// area ((0, 0) is always the origin of the rendering area).
int margin_t, margin_r, margin_b, margin_l;
// General flags; see NCOPTION_*. This is expressed as a bitfield so that
// future options can be added without reshaping the struct. Undefined bits
// must be set to 0.
unsigned flags;
} notcurses_options;
struct notcurses* notcurses_init(const notcurses_options*, FILE*);
int notcurses_lex_margins(const char* op, notcurses_options* opts);

View File

@ -12,6 +12,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <locale.h>
#include <langinfo.h>
#include <sys/poll.h>
#include <stdatomic.h>
@ -824,6 +825,16 @@ notcurses* notcurses_init(const notcurses_options* opts, FILE* outfp){
fprintf(stderr, "Provided an illegal negative margin, refusing to start\n");
return NULL;
}
if(!(opts->flags & NCOPTION_INHIBIT_SETLOCALE)){
const char* locale = setlocale(LC_ALL, NULL);
if(locale && (!strcmp(locale, "C") || !strcmp(locale, "POSIX"))){
if(!(locale = setlocale(LC_ALL, ""))){
fprintf(stderr, "Couldn't set locale based off LANG\n");
}else{
fprintf(stderr, "Set locale based off LANG.\n You should call setlocale(2)\n");
}
}
}
const char* encoding = nl_langinfo(CODESET);
if(encoding == NULL || (strcmp(encoding, "ANSI_X3.4-1968") && strcmp(encoding, "UTF-8"))){
fprintf(stderr, "Encoding (\"%s\") was neither ANSI_X3.4-1968 nor UTF-8, refusing to start\n Did you call setlocale()?\n",

View File

@ -16,6 +16,7 @@ notcurses_options NotCurses::default_notcurses_options = {
/* margin_r */ 0,
/* margin_b */ 0,
/* margin_l */ 0,
/* flags */ 0,
};
NotCurses *NotCurses::_instance = nullptr;