locale.c 11 KB

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