PostgreSQL Source Code git master
chklocale.c File Reference
#include "postgres.h"
#include <langinfo.h>
#include "mb/pg_wchar.h"
Include dependency graph for chklocale.c:

Go to the source code of this file.

Data Structures

struct  encoding_match
 

Functions

int pg_get_encoding_from_locale (const char *ctype, bool write_message)
 

Variables

static const struct encoding_match encoding_match_list []
 

Function Documentation

◆ pg_get_encoding_from_locale()

int pg_get_encoding_from_locale ( const char *  ctype,
bool  write_message 
)

Definition at line 301 of file chklocale.c.

302{
303 char *sys;
304 int i;
305
306#ifndef WIN32
307 locale_t loc;
308#endif
309
310 /* Get the CODESET property, and also LC_CTYPE if not passed in */
311 if (!ctype)
312 ctype = setlocale(LC_CTYPE, NULL);
313
314
315 /* If locale is C or POSIX, we can allow all encodings */
316 if (pg_strcasecmp(ctype, "C") == 0 ||
317 pg_strcasecmp(ctype, "POSIX") == 0)
318 return PG_SQL_ASCII;
319
320
321#ifndef WIN32
322 loc = newlocale(LC_CTYPE_MASK, ctype, (locale_t) 0);
323 if (loc == (locale_t) 0)
324 return -1; /* bogus ctype passed in? */
325
326 sys = nl_langinfo_l(CODESET, loc);
327 if (sys)
328 sys = strdup(sys);
329
330 freelocale(loc);
331#else
332 sys = win32_get_codeset(ctype);
333#endif
334
335 if (!sys)
336 return -1; /* out of memory; unlikely */
337
338 /* Check the table */
340 {
341 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
342 {
343 free(sys);
345 }
346 }
347
348 /* Special-case kluges for particular platforms go here */
349
350#ifdef __darwin__
351
352 /*
353 * Current macOS has many locales that report an empty string for CODESET,
354 * but they all seem to actually use UTF-8.
355 */
356 if (strlen(sys) == 0)
357 {
358 free(sys);
359 return PG_UTF8;
360 }
361#endif
362
363 /*
364 * We print a warning if we got a CODESET string but couldn't recognize
365 * it. This means we need another entry in the table.
366 */
367 if (write_message)
368 {
369#ifdef FRONTEND
370 fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
371 ctype, sys);
372 /* keep newline separate so there's only one translatable string */
373 fputc('\n', stderr);
374#else
376 (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
377 ctype, sys)));
378#endif
379 }
380
381 free(sys);
382 return -1;
383}
static const struct encoding_match encoding_match_list[]
Definition: chklocale.c:45
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define _(x)
Definition: elog.c:90
#define WARNING
Definition: elog.h:36
#define ereport(elevel,...)
Definition: elog.h:149
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:72
@ PG_SQL_ASCII
Definition: pg_wchar.h:226
@ PG_UTF8
Definition: pg_wchar.h:232
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
enum pg_enc pg_enc_code
Definition: chklocale.c:41
const char * system_enc_name
Definition: chklocale.c:42
#define locale_t
Definition: win32_port.h:432
#define setlocale(a, b)
Definition: win32_port.h:475

References _, encoding_match_list, ereport, errmsg(), fprintf, free, i, locale_t, encoding_match::pg_enc_code, PG_SQL_ASCII, pg_strcasecmp(), PG_UTF8, setlocale, encoding_match::system_enc_name, and WARNING.

Referenced by cache_locale_time(), check_encoding_locale_matches(), check_locale_encoding(), main(), PGLC_localeconv(), pqConnectOptions2(), PQsetClientEncoding(), and setup_locale_encoding().

Variable Documentation

◆ encoding_match_list

const struct encoding_match encoding_match_list[]
static

Definition at line 45 of file chklocale.c.

Referenced by pg_get_encoding_from_locale().