locale.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. *
  3. * Copyright (c) 2008 STMicroelectronics Ltd
  4. * Filippo Arcidiacono (filippo.arcidiacono@st.com)
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. *
  8. * A 'locale' command implementation for uClibc.
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <langinfo.h>
  15. #include <getopt.h>
  16. typedef struct {
  17. unsigned char idx_name;
  18. char dot_cs; /* 0 if no codeset specified */
  19. char cs;
  20. unsigned char lc_ctype_row;
  21. unsigned char lc_numeric_row;
  22. unsigned char lc_monetary_row;
  23. unsigned char lc_time_row;
  24. unsigned char lc_collate_row;
  25. unsigned char lc_messages_row;
  26. } locale_entry;
  27. /* Need to include this before locale.h and xlocale.h! */
  28. #include <bits/uClibc_locale.h>
  29. #undef CODESET_LIST
  30. #define CODESET_LIST (__locale_mmap->codeset_list)
  31. #include <locale.h>
  32. #define LOCALE_NAMES (__locale_mmap->locale_names5)
  33. #define LOCALES (__locale_mmap->locales)
  34. #define LOCALE_AT_MODIFIERS (__locale_mmap->locale_at_modifiers)
  35. #define CATEGORY_NAMES (__locale_mmap->lc_names)
  36. #define GET_CODESET_NAME(N) (CODESET_LIST + *(CODESET_LIST + N - 3))
  37. #define GET_LOCALE_ENTRY(R) (locale_entry *)(LOCALES + (__LOCALE_DATA_WIDTH_LOCALES * R))
  38. #define GET_CATEGORY_NAME(X) (CATEGORY_NAMES + *(CATEGORY_NAMES + X))
  39. #define GET_LOCALE_NAME(I) (const char *)(LOCALE_NAMES + 5 * (I - 1))
  40. static const char utf8[] = "UTF-8";
  41. static const char ascii[] = "ASCII";
  42. /* If set print the name of the category. */
  43. static int show_category_name = 0;
  44. /* If set print the name of the item. */
  45. static int show_keyword_name = 0;
  46. /* If set print the usage command. */
  47. static int show_usage = 0;
  48. /* Print names of all available locales. */
  49. static int do_all = 0;
  50. /* Print names of all available character maps. */
  51. static int do_charmaps = 0;
  52. static int remaining = 0;
  53. /* We can map the types of the entries into a few categories. */
  54. enum value_type {
  55. none,
  56. string,
  57. stringarray,
  58. byte,
  59. bytearray,
  60. word,
  61. stringlist,
  62. wordarray,
  63. wstring,
  64. wstringarray,
  65. wstringlist
  66. };
  67. /* Definition of the data structure which represents a category and its
  68. items. */
  69. struct category {
  70. int cat_id;
  71. const char *name;
  72. size_t number;
  73. struct cat_item {
  74. int item_id;
  75. const char *name;
  76. enum { std, opt } status;
  77. enum value_type value_type;
  78. int min;
  79. int max;
  80. } *item_desc;
  81. };
  82. /* Simple helper macro. */
  83. #define NELEMS(arr) ((sizeof (arr)) / (sizeof (arr[0])))
  84. /* For some tricky stuff. */
  85. #define NO_PAREN(Item, More...) Item, ## More
  86. /* We have all categories defined in `categories.def'. Now construct
  87. the description and data structure used for all categories. */
  88. #define DEFINE_ELEMENT(Item, More...) { Item, ## More },
  89. #define DEFINE_CATEGORY(category, name, items, postload) \
  90. static struct cat_item category##_desc[] = \
  91. { \
  92. NO_PAREN items \
  93. };
  94. #include "categories.def"
  95. #undef DEFINE_CATEGORY
  96. static struct category category[] = {
  97. #define DEFINE_CATEGORY(category, name, items, postload) \
  98. [category] = { _NL_NUM_##category, name, NELEMS (category##_desc), \
  99. category##_desc },
  100. #include "categories.def"
  101. #undef DEFINE_CATEGORY
  102. };
  103. #define NCATEGORIES NELEMS (category)
  104. static void usage(const char *name);
  105. static void usage(const char *name)
  106. {
  107. const char *s;
  108. s = basename(name);
  109. fprintf(stderr,
  110. "Usage: %s [-ck] [--category-name] [--keyword-name] [--help] NAME\n"
  111. "or: %s [OPTION...] [-a|-m] [--all-locales] [--charmaps] \n", s,
  112. s);
  113. }
  114. static int argp_parse(int argc, char *argv[]);
  115. static int argp_parse(int argc, char *argv[])
  116. {
  117. static const struct option long_options[] = {
  118. {"all-locales", no_argument, NULL, 'a'},
  119. {"charmaps", no_argument, NULL, 'm'},
  120. {"category-name", no_argument, NULL, 'c'},
  121. {"keyword-name", no_argument, NULL, 'k'},
  122. {"help", no_argument, NULL, 'h'},
  123. {NULL, 0, NULL, 0}
  124. };
  125. int c;
  126. char *progname;
  127. progname = *argv;
  128. while ((c = getopt_long(argc, argv, "amckh", long_options, NULL)) >= 0)
  129. switch (c) {
  130. case 'a':
  131. do_all = 1;
  132. break;
  133. case 'c':
  134. show_category_name = 1;
  135. break;
  136. case 'm':
  137. do_charmaps = 1;
  138. break;
  139. case 'k':
  140. show_keyword_name = 1;
  141. break;
  142. case 'h':
  143. show_usage = 1;
  144. break;
  145. case '?':
  146. fprintf(stderr, "Unknown option.\n");
  147. usage(progname);
  148. return 1;
  149. default:
  150. fprintf(stderr, "This should never happen!\n");
  151. return 1;
  152. }
  153. remaining = optind;
  154. return 0;
  155. }
  156. static unsigned const char *find_at(char c);
  157. static unsigned const char *find_at(char c)
  158. {
  159. const unsigned char *q;
  160. q = LOCALE_AT_MODIFIERS;
  161. do {
  162. if (q[1] == c) {
  163. return (unsigned const char *) q + 2;
  164. }
  165. q += 2 + *q;
  166. } while (*q);
  167. return NULL;
  168. }
  169. static void find_locale_string(locale_entry * loc_rec, char *loc)
  170. {
  171. char at = 0;
  172. unsigned char idx;
  173. uint16_t dotcs, cs;
  174. idx = loc_rec->idx_name;
  175. if (!idx) {
  176. *loc++ = 'C'; /* jump the first locale (C) */
  177. *loc = '\0';
  178. } else {
  179. dotcs = (uint16_t) loc_rec->dot_cs;
  180. cs = (uint16_t) loc_rec->cs;;
  181. loc = strncpy(loc, GET_LOCALE_NAME(idx), 5);
  182. if (loc[2] == '_') {
  183. sprintf(loc, "%5.5s%c%s\0", loc, (dotcs != 0) ? '.' : ' ',
  184. (cs ==
  185. 1) ? ascii : ((cs == 2) ? utf8 : GET_CODESET_NAME(cs)));
  186. } else {
  187. at = loc[2];
  188. loc[2] = '_';
  189. sprintf(loc, "%5.5s%c%s@%s\0", loc, (dotcs != 0) ? '.' : ' ',
  190. (cs ==
  191. 1) ? ascii : ((cs == 2) ? utf8 : GET_CODESET_NAME(cs)),
  192. find_at(at));
  193. }
  194. }
  195. }
  196. static void list_locale(void);
  197. static void list_locale()
  198. {
  199. char loc[40];
  200. uint16_t n = 0;
  201. locale_entry *locales = (locale_entry *) LOCALES;
  202. do {
  203. find_locale_string(locales, loc);
  204. printf("%s\n", loc);
  205. ++n;
  206. locales++;
  207. } while (n < __LOCALE_DATA_NUM_LOCALES);
  208. }
  209. static void list_charmaps(void);
  210. static void list_charmaps()
  211. {
  212. unsigned const char *cl;
  213. cl = CODESET_LIST;
  214. do {
  215. printf("%s\n", CODESET_LIST + *cl);
  216. } while (*++cl);
  217. }
  218. static void print_item(struct cat_item *item);
  219. static void print_item(struct cat_item *item)
  220. {
  221. switch (item->value_type) {
  222. case string:
  223. if (show_keyword_name)
  224. printf("%s=\"", item->name);
  225. fputs(nl_langinfo(item->item_id) ? : "", stdout);
  226. if (show_keyword_name)
  227. putchar('"');
  228. putchar('\n');
  229. break;
  230. case stringarray:
  231. {
  232. int cnt;
  233. const char *val;
  234. if (show_keyword_name)
  235. printf("%s=\"", item->name);
  236. for (cnt = 0; cnt < item->max - 1; ++cnt) {
  237. val = nl_langinfo(item->item_id + cnt);
  238. if (val != NULL)
  239. fputs(val, stdout);
  240. putchar(';');
  241. }
  242. val = nl_langinfo(item->item_id + cnt);
  243. if (val != NULL)
  244. fputs(val, stdout);
  245. if (show_keyword_name)
  246. putchar('"');
  247. putchar('\n');
  248. }
  249. break;
  250. case stringlist:
  251. {
  252. int first = 1;
  253. const char *val = nl_langinfo(item->item_id) ? : "";
  254. int cnt;
  255. if (show_keyword_name)
  256. printf("%s=", item->name);
  257. for (cnt = 0; cnt < item->max && *val != '\0'; ++cnt) {
  258. printf("%s%s%s%s", first ? "" : ";",
  259. show_keyword_name ? "\"" : "", val,
  260. show_keyword_name ? "\"" : "");
  261. val = strchr(val, '\0') + 1;
  262. first = 0;
  263. }
  264. putchar('\n');
  265. }
  266. break;
  267. case byte:
  268. {
  269. const char *val = nl_langinfo(item->item_id);
  270. if (show_keyword_name)
  271. printf("%s=", item->name);
  272. if (val != NULL)
  273. printf("%d", *val == '\177' ? -1 : *val);
  274. putchar('\n');
  275. }
  276. break;
  277. case bytearray:
  278. {
  279. const char *val = nl_langinfo(item->item_id);
  280. int cnt = val ? strlen(val) : 0;
  281. if (show_keyword_name)
  282. printf("%s=", item->name);
  283. while (cnt > 1) {
  284. printf("%d;", *val == '\177' ? -1 : *val);
  285. --cnt;
  286. ++val;
  287. }
  288. printf("%d\n", cnt == 0 || *val == '\177' ? -1 : *val);
  289. }
  290. break;
  291. case word:
  292. {
  293. union {
  294. unsigned int word;
  295. char *string;
  296. } val;
  297. val.string = nl_langinfo(item->item_id);
  298. if (show_keyword_name)
  299. printf("%s=", item->name);
  300. printf("%d\n", val.word);
  301. }
  302. break;
  303. case wstring:
  304. case wstringarray:
  305. case wstringlist:
  306. /* We don't print wide character information since the same
  307. information is available in a multibyte string. */
  308. default:
  309. break;
  310. }
  311. }
  312. /* Show the information request for NAME. */
  313. static void show_info(const char *name);
  314. static void show_info(const char *name)
  315. {
  316. size_t cat_no, item_no;
  317. const unsigned char *cat_name;
  318. /* Now all categories in an unspecified order. */
  319. for (cat_no = 0; cat_no < __LC_ALL; ++cat_no) {
  320. cat_name = GET_CATEGORY_NAME(cat_no);
  321. if (strcmp(name, (const char *) cat_name) == 0) {
  322. if (show_category_name)
  323. printf("%s\n", name);
  324. for (item_no = 0; item_no < category[cat_no].number; ++item_no)
  325. print_item(&category[cat_no].item_desc[item_no]);
  326. return;
  327. }
  328. for (item_no = 0; item_no < category[cat_no].number; ++item_no)
  329. if (strcmp(name, category[cat_no].item_desc[item_no].name) == 0) {
  330. if (show_category_name != 0)
  331. puts(category[cat_no].name);
  332. print_item(&category[cat_no].item_desc[item_no]);
  333. return;
  334. }
  335. }
  336. }
  337. static void show_locale_vars(void);
  338. static void show_locale_vars()
  339. {
  340. size_t cat_no;
  341. int row; /* locale row */
  342. const char *lcall = getenv("LC_ALL");
  343. const char *lang = getenv("LANG") ? : "";
  344. unsigned char *cur_loc = __global_locale->cur_locale + 1;
  345. char loc_name[40];
  346. locale_entry *locales;
  347. /* LANG has to be the first value. */
  348. printf("LANG=%s\n", lang);
  349. /* Now all categories in an unspecified order. */
  350. for (cat_no = 0; cat_no < __LC_ALL; ++cat_no) {
  351. row = (((int) (*cur_loc & 0x7f)) << 7) + (cur_loc[1] & 0x7f);
  352. /* assert(row < __LOCALE_DATA_NUM_LOCALES); */
  353. locales = GET_LOCALE_ENTRY(row);
  354. find_locale_string(locales, loc_name);
  355. printf("%s=%s\n", GET_CATEGORY_NAME(cat_no), loc_name);
  356. cur_loc += 2;
  357. }
  358. /* The last is the LC_ALL value. */
  359. printf("LC_ALL=%s\n", lcall ? : "");
  360. }
  361. int main(int argc, char *argv[])
  362. {
  363. /* Parse and process arguments. */
  364. if (argp_parse(argc, argv))
  365. return 1;
  366. if (do_all) {
  367. list_locale();
  368. exit(EXIT_SUCCESS);
  369. }
  370. if (do_charmaps) {
  371. list_charmaps();
  372. exit(EXIT_SUCCESS);
  373. }
  374. if (show_usage) {
  375. usage(*argv);
  376. exit(EXIT_SUCCESS);
  377. }
  378. /* If no real argument is given we have to print the contents of the
  379. current locale definition variables. These are LANG and the LC_*. */
  380. if (remaining == argc && show_category_name == 0
  381. && show_keyword_name == 0) {
  382. show_locale_vars();
  383. exit(EXIT_SUCCESS);
  384. }
  385. /* Process all given names. */
  386. while (remaining < argc)
  387. show_info(argv[remaining++]);
  388. exit(EXIT_SUCCESS);
  389. }