osLocale.c 6.1 KB
Newer Older
S
locale  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

wafwerar's avatar
wafwerar 已提交
16
#define ALLOW_FORBID_FUNC
S
locale  
Shengliang Guan 已提交
17 18 19
#define _DEFAULT_SOURCE
#include "osLocale.h"

wafwerar's avatar
wafwerar 已提交
20
#ifdef WINDOWS
S
locale  
Shengliang Guan 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#if (_WIN64)
#include <iphlpapi.h>
#include <mswsock.h>
#include <psapi.h>
#include <stdio.h>
#include <windows.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Mswsock.lib ")
#endif
#include <objbase.h>
#pragma warning(push)
#pragma warning(disable : 4091)
#include <DbgHelp.h>
#pragma warning(pop)
#elif defined(_TD_DARWIN_64)
#include <errno.h>
#include <libproc.h>
#else
#include <argp.h>
#include <linux/sysctl.h>
#include <sys/file.h>
#include <sys/resource.h>
#include <sys/statvfs.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
#endif

typedef struct CharsetPair {
  char *oldCharset;
  char *newCharset;
} CharsetPair;

char *taosCharsetReplace(char *charsetstr) {
  CharsetPair charsetRep[] = {
      {"utf8", "UTF-8"},
      {"936", "CP936"},
  };

  for (int32_t i = 0; i < tListLen(charsetRep); ++i) {
    if (strcasecmp(charsetRep[i].oldCharset, charsetstr) == 0) {
62
      return taosStrdup(charsetRep[i].newCharset);
S
locale  
Shengliang Guan 已提交
63 64 65
    }
  }

66
  return taosStrdup(charsetstr);
S
locale  
Shengliang Guan 已提交
67 68 69
}

/**
H
Haojun Liao 已提交
70 71 72
 * TODO: here we may employ the systemctl API to set/get the correct locale on the Linux. In some cases, the setlocale
 *  seems does not response as expected.
 *
S
locale  
Shengliang Guan 已提交
73
 * In some Linux systems, setLocale(LC_CTYPE, "") may return NULL, in which case the launch of
sangshuduo's avatar
sangshuduo 已提交
74
 * both the Server and the Client may be interrupted.
S
locale  
Shengliang Guan 已提交
75 76 77 78 79 80 81
 *
 * In case that the setLocale failed to be executed, the right charset needs to be set.
 */
void taosSetSystemLocale(const char *inLocale, const char *inCharSet) {
  char *locale = setlocale(LC_CTYPE, inLocale);

  // default locale or user specified locale is not valid, abort launch
S
Shengliang Guan 已提交
82
  if (inLocale == NULL || strlen(inLocale) == 0) {
H
Hongze Cheng 已提交
83
    // printf("Invalid locale:%s, please set the valid locale in config file\n", inLocale);
S
locale  
Shengliang Guan 已提交
84 85 86
  }

  if (!taosValidateEncodec(inCharSet)) {
wafwerar's avatar
wafwerar 已提交
87
    printf("Invalid charset:%s, please set the valid charset in config file\n", inCharSet);
S
locale  
Shengliang Guan 已提交
88 89 90 91 92
    exit(-1);
  }
}

void taosGetSystemLocale(char *outLocale, char *outCharset) {
wafwerar's avatar
wafwerar 已提交
93
#ifdef WINDOWS
wafwerar's avatar
wafwerar 已提交
94
  char *locale = setlocale(LC_CTYPE, "en_US.UTF-8");
S
locale  
Shengliang Guan 已提交
95 96 97
  if (locale != NULL) {
    tstrncpy(outLocale, locale, TD_LOCALE_LEN);
  }
wafwerar's avatar
wafwerar 已提交
98
  strcpy(outCharset, "UTF-8");
S
locale  
Shengliang Guan 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

#elif defined(_TD_DARWIN_64)
  /*
   * originally from src/os/src/detail/osSysinfo.c
   * POSIX format locale string:
   * (Language Strings)_(Country/Region Strings).(code_page)
   *
   * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8,
   *
   * if user does not specify the locale in taos.cfg the program use default LC_CTYPE as system locale.
   *
   * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page
   * for libiconv that is employed to convert string in this system. This program will automatically use
   * UTF-8 instead as the charset.
   *
   * In case of windows client, the locale string is not valid POSIX format, user needs to set the
   * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is
   * CP936, CP437 for English charset.
   *
   */

  char  sep = '.';
  char *locale = NULL;

  locale = setlocale(LC_CTYPE, "");
  if (locale == NULL) {
    // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno));
    strcpy(outLocale, "en_US.UTF-8");
  } else {
    tstrncpy(outLocale, locale, TD_LOCALE_LEN);
    // printf("locale not configured, set to system default:%s", outLocale);
  }

  /* if user does not specify the charset, extract it from locale */
  char *str = strrchr(outLocale, sep);
  if (str != NULL) {
    str++;

    char *revisedCharset = taosCharsetReplace(str);
    tstrncpy(outCharset, revisedCharset, TD_CHARSET_LEN);

wafwerar's avatar
wafwerar 已提交
140
    taosMemoryFree(revisedCharset);
S
locale  
Shengliang Guan 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153
    // printf("charset not configured, set to system default:%s", outCharset);
  } else {
    strcpy(outCharset, "UTF-8");
    // printf("can't get locale and charset from system, set it to UTF-8");
  }

#else
  /*
   * POSIX format locale string:
   * (Language Strings)_(Country/Region Strings).(code_page)
   *
   * example: en_US.UTF-8, zh_CN.GB18030, zh_CN.UTF-8,
   *
H
Haojun Liao 已提交
154
   * If user does not specify the locale in taos.cfg, the program then uses default LC_CTYPE as system locale.
S
locale  
Shengliang Guan 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
   *
   * In case of some CentOS systems, their default locale is "en_US.utf8", which is not valid code_page
   * for libiconv that is employed to convert string in this system. This program will automatically use
   * UTF-8 instead as the charset.
   *
   * In case of windows client, the locale string is not valid POSIX format, user needs to set the
   * correct code_page for libiconv. Usually, the code_page of windows system with simple chinese is
   * CP936, CP437 for English charset.
   *
   */
  char  sep = '.';
  char *locale = NULL;

  locale = setlocale(LC_CTYPE, "");
  if (locale == NULL) {
    // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno));
    strcpy(outLocale, "en_US.UTF-8");
  } else {
    tstrncpy(outLocale, locale, TD_LOCALE_LEN);
C
cadem 已提交
174
    printf("locale not configured, set to system default:%s\n", outLocale);
S
locale  
Shengliang Guan 已提交
175 176 177 178 179 180 181 182 183 184
  }

  // if user does not specify the charset, extract it from locale
  char *str = strrchr(outLocale, sep);
  if (str != NULL) {
    str++;

    char *revisedCharset = taosCharsetReplace(str);
    tstrncpy(outCharset, revisedCharset, TD_LOCALE_LEN);

wafwerar's avatar
wafwerar 已提交
185
    taosMemoryFree(revisedCharset);
S
locale  
Shengliang Guan 已提交
186 187 188 189 190 191 192 193
    // printf("charset not configured, set to system default:%s", outCharset);
  } else {
    strcpy(outCharset, "UTF-8");
    // printf("can't get locale and charset from system, set it to UTF-8");
  }

#endif
}