]>
Commit | Line | Data |
---|---|---|
0b95971d UD |
1 | /* Dump the character classes and character maps of a locale to a bunch |
2 | of individual files which can be processed with diff, sed etc. | |
04277e02 | 3 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
0b95971d UD |
4 | This file is part of the GNU C Library. |
5 | Contributed by Bruno Haible <haible@clisp.cons.org>, 2000. | |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
0b95971d UD |
11 | |
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
0b95971d | 16 | |
41bdb6e2 | 17 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 18 | License along with the GNU C Library; if not, see |
5a82c748 | 19 | <https://www.gnu.org/licenses/>. */ |
0b95971d UD |
20 | |
21 | /* Usage example: | |
22 | $ dump-ctype de_DE.UTF-8 | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <wctype.h> | |
28 | #include <locale.h> | |
29 | #include <sys/stat.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
32 | ||
33 | static const char *program_name = "dump-ctype"; | |
34 | static const char *locale; | |
35 | ||
36 | static const char *class_names[] = | |
37 | { | |
38 | "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower", | |
39 | "print", "punct", "space", "upper", "xdigit" | |
40 | }; | |
41 | ||
42 | static const char *map_names[] = | |
43 | { | |
44 | "tolower", "toupper", "totitle" | |
45 | }; | |
46 | ||
47 | static void dump_class (const char *class_name) | |
48 | { | |
49 | wctype_t class; | |
50 | FILE *f; | |
51 | unsigned int ch; | |
52 | ||
53 | class = wctype (class_name); | |
54 | if (class == (wctype_t) 0) | |
55 | { | |
56 | fprintf (stderr, "%s %s: noexistent class %s\n", program_name, | |
57 | locale, class_name); | |
58 | return; | |
59 | } | |
60 | ||
61 | f = fopen (class_name, "w"); | |
62 | if (f == NULL) | |
63 | { | |
64 | fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name, | |
65 | locale, locale, class_name); | |
66 | exit (1); | |
67 | } | |
68 | ||
69 | for (ch = 0; ch < 0x10000; ch++) | |
70 | if (iswctype (ch, class)) | |
71 | fprintf (f, "0x%04X\n", ch); | |
72 | ||
73 | if (ferror (f) || fclose (f)) | |
74 | { | |
75 | fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name, | |
76 | locale, locale, class_name); | |
77 | exit (1); | |
78 | } | |
79 | } | |
80 | ||
81 | static void dump_map (const char *map_name) | |
82 | { | |
83 | wctrans_t map; | |
84 | FILE *f; | |
85 | unsigned int ch; | |
86 | ||
87 | map = wctrans (map_name); | |
88 | if (map == (wctrans_t) 0) | |
89 | { | |
90 | fprintf (stderr, "%s %s: noexistent map %s\n", program_name, | |
91 | locale, map_name); | |
92 | return; | |
93 | } | |
94 | ||
95 | f = fopen (map_name, "w"); | |
96 | if (f == NULL) | |
97 | { | |
98 | fprintf (stderr, "%s %s: cannot open file %s/%s\n", program_name, | |
99 | locale, locale, map_name); | |
100 | exit (1); | |
101 | } | |
102 | ||
103 | for (ch = 0; ch < 0x10000; ch++) | |
104 | if (towctrans (ch, map) != ch) | |
105 | fprintf (f, "0x%04X\t0x%04X\n", ch, towctrans (ch, map)); | |
106 | ||
107 | if (ferror (f) || fclose (f)) | |
108 | { | |
109 | fprintf (stderr, "%s %s: I/O error on file %s/%s\n", program_name, | |
110 | locale, locale, map_name); | |
111 | exit (1); | |
112 | } | |
113 | } | |
114 | ||
f00f95d1 UD |
115 | int |
116 | main (int argc, char *argv[]) | |
0b95971d UD |
117 | { |
118 | size_t i; | |
119 | ||
120 | if (argc != 2) | |
121 | { | |
122 | fprintf (stderr, "Usage: dump-ctype locale\n"); | |
123 | exit (1); | |
124 | } | |
125 | locale = argv[1]; | |
126 | ||
127 | if (setlocale (LC_ALL, locale) == NULL) | |
128 | { | |
129 | fprintf (stderr, "%s: setlocale cannot switch to locale %s\n", | |
130 | program_name, locale); | |
131 | exit (1); | |
132 | } | |
133 | ||
134 | if (mkdir (locale, 0777) < 0) | |
135 | { | |
136 | char buf[100]; | |
137 | int save_errno = errno; | |
138 | ||
139 | sprintf (buf, "%s: cannot create directory %s", program_name, locale); | |
140 | errno = save_errno; | |
141 | perror (buf); | |
142 | exit (1); | |
143 | } | |
144 | ||
145 | if (chdir (locale) < 0) | |
146 | { | |
147 | char buf[100]; | |
148 | int save_errno = errno; | |
149 | ||
150 | sprintf (buf, "%s: cannot chdir to %s", program_name, locale); | |
151 | errno = save_errno; | |
152 | perror (buf); | |
153 | exit (1); | |
154 | } | |
155 | ||
156 | for (i = 0; i < sizeof (class_names) / sizeof (class_names[0]); i++) | |
157 | dump_class (class_names[i]); | |
158 | ||
159 | for (i = 0; i < sizeof (map_names) / sizeof (map_names[0]); i++) | |
160 | dump_map (map_names[i]); | |
161 | ||
162 | return 0; | |
163 | } |