info.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* $Id$ */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <locale.h>
  6. #include "jconv.h"
  7. #ifdef HAVE_CODESET
  8. #include <langinfo.h>
  9. #endif
  10. #define DEBUG_DO(x)
  11. #define JCONV_DEFAULT_CONFFILE "/etc/libjconv/default.conf"
  12. typedef struct {
  13. char *name;
  14. char *codeset;
  15. int num_pref_codesets;
  16. char **pref_codesets;
  17. } jconv_locale_info;
  18. static jconv_locale_info *jconv_locales = NULL;
  19. static int num_jconv_locales = 0;
  20. static int current_locale = -1;
  21. static void
  22. jconv_info_clear_config (void)
  23. {
  24. int i, j;
  25. for (i = 0; i < num_jconv_locales; i++) {
  26. jconv_locale_info *p;
  27. p = jconv_locales + i;
  28. free (p->name);
  29. free (p->codeset);
  30. for (j = 0; j < p->num_pref_codesets; j++) {
  31. free(p->pref_codesets[j]);
  32. }
  33. if (p->pref_codesets)
  34. free (p->pref_codesets);
  35. }
  36. if (jconv_locales)
  37. free (jconv_locales);
  38. }
  39. static char *
  40. jconv_gettok (char **strp)
  41. {
  42. const char *spc = " \t\n";
  43. char *p, *tok;
  44. p = *strp;
  45. p += strspn(p, spc);
  46. if (*p == 0) return NULL;
  47. tok = p;
  48. p += strcspn(p, spc);
  49. if (*p)
  50. *(p++) = 0;
  51. *strp = p;
  52. return tok;
  53. }
  54. static int
  55. jconv_parse_line (char *buffer)
  56. {
  57. /* locale_name : codeset_of_the_locale pref_codeset ... */
  58. char *locname, *colon, *codeset, **codesets, *c;
  59. int num_codesets;
  60. locname = jconv_gettok(&buffer);
  61. if (locname == NULL || locname[0] == '#') return -1;
  62. colon = jconv_gettok(&buffer);
  63. if (colon == NULL || strcmp(colon, ":") != 0) return -1;
  64. codeset = jconv_gettok(&buffer);
  65. if (codeset == NULL) return -1;
  66. DEBUG_DO(printf("jconv_parse_line: %s:%s", locname, codeset));
  67. codesets = NULL;
  68. num_codesets = 0;
  69. while ((c = jconv_gettok(&buffer)) != NULL) {
  70. codesets = realloc(codesets,
  71. (num_codesets + 1) * sizeof(*codesets));
  72. codesets[num_codesets] = strdup(c);
  73. DEBUG_DO(printf(" %s", c));
  74. num_codesets++;
  75. }
  76. DEBUG_DO(printf("\n"));
  77. jconv_locales = realloc(jconv_locales,
  78. (num_jconv_locales + 1) *
  79. sizeof(*jconv_locales));
  80. jconv_locales[num_jconv_locales].name = strdup(locname);
  81. jconv_locales[num_jconv_locales].codeset = strdup(codeset);
  82. jconv_locales[num_jconv_locales].num_pref_codesets = num_codesets;
  83. jconv_locales[num_jconv_locales].pref_codesets = codesets;
  84. num_jconv_locales++;
  85. return 0;
  86. }
  87. static int
  88. jconv_info_load_config (const char *conffile)
  89. {
  90. FILE *fp;
  91. char buffer[1024];
  92. jconv_info_clear_config();
  93. fp = fopen(conffile, "r");
  94. if (fp == NULL)
  95. return -1;
  96. while (fgets(buffer, 1024, fp) != NULL) {
  97. jconv_parse_line(buffer);
  98. }
  99. fclose(fp);
  100. return 0;
  101. }
  102. static int
  103. jconv_info_query (const char *name, size_t name_len)
  104. {
  105. int i;
  106. DEBUG_DO(printf("query %d %s\n", name_len, name));
  107. for (i = 0; i < num_jconv_locales; i++) {
  108. if (strlen(jconv_locales[i].name) == name_len &&
  109. strncasecmp(jconv_locales[i].name, name, name_len) == 0)
  110. return i;
  111. }
  112. return -1;
  113. }
  114. void
  115. jconv_info_set_locale (void)
  116. {
  117. char *locale_name;
  118. locale_name = setlocale(LC_CTYPE, NULL);
  119. if (current_locale >= 0 &&
  120. strcasecmp(jconv_locales[current_locale].name, locale_name) == 0)
  121. return;
  122. current_locale = -1;
  123. /* 1st try */
  124. current_locale = jconv_info_query(locale_name, strlen(locale_name));
  125. if (current_locale >= 0) return;
  126. /* 2nd try */
  127. current_locale = jconv_info_query(locale_name,
  128. strcspn(locale_name, "@"));
  129. if (current_locale >= 0) return;
  130. /* 3rd try */
  131. current_locale = jconv_info_query(locale_name,
  132. strcspn(locale_name, "@.+,"));
  133. if (current_locale >= 0) return;
  134. /* 4th try */
  135. current_locale = jconv_info_query(locale_name,
  136. strcspn(locale_name, "@.+,_"));
  137. if (current_locale >= 0) return;
  138. /* a;sldkjfaslf;kjsaf;lsakjf;alksjdf;laskdjf;a */
  139. current_locale = 0;
  140. }
  141. void
  142. jconv_info_init (const char *conffile)
  143. {
  144. jconv_info_load_config(conffile ?
  145. conffile : JCONV_DEFAULT_CONFFILE);
  146. jconv_info_set_locale();
  147. DEBUG_DO(printf("current_locale: %s %s\n",
  148. jconv_locales[current_locale].name,
  149. jconv_locales[current_locale].codeset));
  150. }
  151. void
  152. jconv_info_maybe_init (void)
  153. {
  154. if (current_locale < 0)
  155. jconv_info_init(NULL);
  156. }
  157. const char *
  158. jconv_info_get_current_codeset (void)
  159. {
  160. jconv_info_maybe_init();
  161. #ifdef HAVE_CODESET
  162. return nl_langinfo(CODESET);
  163. #else
  164. return jconv_locales[current_locale].codeset;
  165. #endif
  166. }
  167. const char *const *
  168. jconv_info_get_pref_codesets (int *num_codesets_r)
  169. {
  170. const char *const *codes;
  171. jconv_info_maybe_init();
  172. codes = (const char **)jconv_locales[current_locale].pref_codesets;
  173. if (codes == NULL) {
  174. if (num_codesets_r)
  175. *num_codesets_r = 1;
  176. return (const char **)&jconv_locales[current_locale].codeset;
  177. }
  178. if (num_codesets_r)
  179. *num_codesets_r =
  180. jconv_locales[current_locale].num_pref_codesets;
  181. return codes;
  182. }
  183. #if 0
  184. int
  185. main (void)
  186. {
  187. int n;
  188. const char *const *codes;
  189. setlocale(LC_ALL, "");
  190. jconv_info_init(NULL);
  191. codes = jconv_info_get_pref_codesets (&n);
  192. printf("codes[0] %s\n", codes[0]);
  193. /*
  194. codes[0] = NULL;
  195. free (codes[0]);
  196. */
  197. return 0;
  198. }
  199. #endif
  200. #if 0
  201. int
  202. main (void)
  203. {
  204. char *src, *dest;
  205. size_t len;
  206. setlocale(LC_ALL, "");
  207. for (len = 0, src = NULL; !feof(stdin); ) {
  208. src = realloc(src, len + 4096);
  209. len += fread(src + len, 1, 4096, stdin);
  210. }
  211. src[len] = 0;
  212. dest = jconv_strdup_conv_fullauto(src);
  213. fwrite(dest, strlen(dest), 1, stdout);
  214. return 0;
  215. }
  216. #endif