locale.c 11 KB

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