Debian Codegraph @

src/cups/i18n.c

raw symbols SCIP
1 /*
2 * Internationalization functions for CUPS drivers.
3 *
4 * Copyright 2008 Michael Sweet ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * Contents:
20 *
21 * stp_i18n_load() - Load a message catalog for a locale.
22 * stp_i18n_lookup() - Lookup a string in the message catalog...
23 * stp_i18n_printf() - Send a formatted string to stderr.
24 * stpi_unquote() - Unquote characters in strings.
25 */
26
27 /*
28 * Include necessary files...
29 */
30
31 #include "i18n.h"
32 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <iconv.h>
41 #include <strings.h>
42
43
44 /*
45 * GNU gettext uses a simple .po file format:
46 *
47 * # comment
48 * msgid "id"
49 * "optional continuation"
50 * msgstr "str"
51 * "optional continuation"
52 *
53 * Both the id and str strings use standard C quoting for special characters
54 * like newline and the double quote character.
55 */
56
57
58 /*
59 * Cache structure...
60 */
61
62 typedef struct stpi_i18n_s
63 {
64 struct stpi_i18n_s *next; /* Next catalog */
65 char locale[6]; /* Locale */
66 stp_string_list_t *po; /* Message catalog */
67 } stpi_i18n_t;
68
69
70 /*
71 * Local functions...
72 */
73
74 static void stpi_unquote(char *s);
75
76
77 /*
78 * Local globals...
79 */
80
81 static stpi_i18n_t *stpi_pocache = NULL;
82
83
84 /*
85 * 'stp_i18n_load()' - Load a message catalog for a locale.
86 */
87
88 const stp_string_list_t * /* O - Message catalog */
89 stp_i18n_load(const char *locale) /* I - Locale name */
90 {
91 stp_string_list_t *po; /* Message catalog */
92 char ll_CC[6], /* Locale ID */
93 poname[1024]; /* .po filename */
94 stpi_i18n_t *pocache; /* Current cache entry */
95 FILE *pofile; /* .po file */
96 const char *stp_localedir; /* STP_LOCALEDIR environment variable */
97 char line[4096], /* Line buffer */
98 *ptr, /* Pointer into buffer */
99 id[4096], /* Translation ID */
100 str[4096], /* Translation string */
101 utf8str[4096]; /* UTF-8 translation string */
102 int in_id, /* Processing "id" string? */
103 in_str, /* Processing "str" string? */
104 linenum; /* Line number in .po file */
105 iconv_t ic; /* Transcoder to UTF-8 */
106 size_t inbytes, /* Number of input buffer bytes */
107 outbytes; /* Number of output buffer bytes */
108 char *inptr, /* Pointer into input buffer */
109 *outptr; /* Pointer into output buffer */
110 int fuzzy = 0; /* Fuzzy translation? */
111
112
113 if (!locale)
114 return (NULL);
115
116 /*
117 * See if the locale is already loaded...
118 */
119
120 for (pocache = stpi_pocache; pocache; pocache = pocache->next)
121 if (!strcmp(locale, pocache->locale))
122 return (pocache->po);
123
124 /*
125 * Find the message catalog for the given locale...
126 */
127
128 if ((stp_localedir = getenv("STP_LOCALEDIR")) == NULL)
129 stp_localedir = PACKAGE_LOCALE_DIR;
130
131 strncpy(ll_CC, locale, sizeof(ll_CC) - 1);
132 ll_CC[sizeof(ll_CC) - 1] = '\0';
133
134 if ((ptr = strchr(ll_CC, '.')) != NULL)
135 *ptr = '\0';
136
137 snprintf(poname, sizeof(poname), "%s/%s/gutenprint_%s.po", stp_localedir,
138 ll_CC, ll_CC);
139 if (access(poname, 0) && strlen(ll_CC) > 2)
140 {
141 ll_CC[2] = '\0';
142
143 snprintf(poname, sizeof(poname), "%s/%s/gutenprint_%s.po", stp_localedir,
144 ll_CC, ll_CC);
145 }
146
147 if ((pofile = fopen(poname, "rb")) == NULL)
148 return (NULL);
149
150 /*
151 * Read the messages and add them to a string list...
152 */
153
154 if ((po = stp_string_list_create()) == NULL)
155 {
156 fclose(pofile);
157 return (NULL);
158 }
159
160 linenum = 0;
161 id[0] = '\0';
162 str[0] = '\0';
163 in_id = 0;
164 in_str = 0;
165 ic = 0;
166
167 while (fgets(line, sizeof(line), pofile))
168 {
169 linenum ++;
170
171 /*
172 * Skip blank and comment lines...
173 */
174
175 if (line[0] == '#')
176 {
177 if (line[1] == ':')
178 fuzzy = 0;
179
180 if (strstr(line, "fuzzy"))
181 fuzzy = 1;
182 }
183
184 if (fuzzy || line[0] == '#' || line[0] == '\n')
185 continue;
186
187 /*
188 * Strip the trailing quote...
189 */
190
191 if ((ptr = (char *)strrchr(line, '\"')) == NULL)
192 {
193 fprintf(stderr, "DEBUG: Expected quoted string on line %d of %s!\n",
194 linenum, poname);
195 break;
196 }
197
198 *ptr = '\0';
199
200 /*
201 * Find start of value...
202 */
203
204 if ((ptr = strchr(line, '\"')) == NULL)
205 {
206 fprintf(stderr, "DEBUG: Expected quoted string on line %d of %s!\n",
207 linenum, poname);
208 break;
209 }
210
211 ptr ++;
212
213 /*
214 * Create or add to a message...
215 */
216
217 if (!strncmp(line, "msgid", 5))
218 {
219 in_id = 1;
220 in_str = 0;
221
222 if (id[0] && str[0])
223 {
224 stpi_unquote(id);
225
226 if (ic)
227 {
228 /*
229 * Convert string to UTF-8...
230 */
231
232 inbytes = strlen(str);
233 inptr = str;
234 outbytes = sizeof(utf8str);
235 outptr = utf8str;
236
237 iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
238 *outptr = '\0';
239
240 /*
241 * Add it to the string list...
242 */
243
244 stpi_unquote(utf8str);
245 stp_string_list_add_string_unsafe(po, id, utf8str);
246 }
247 else
248 {
249 stpi_unquote(str);
250 stp_string_list_add_string_unsafe(po, id, str);
251 }
252 }
253 else if (!id[0] && str[0] && !ic)
254 {
255 /*
256 * Look for the character set...
257 */
258
259 const char *charset = strstr(str, "charset=");
260 /* Source character set definition */
261 char fromcode[255], /* Source character set */
262 *fromptr; /* Pointer into fromcode */
263
264 if (charset)
265 {
266 /*
267 * Extract character set and setup a transcode context...
268 */
269
270 strncpy(fromcode, charset + 8, sizeof(fromcode) - 1);
271 fromcode[sizeof(fromcode) - 1] = '\0';
272 for (fromptr = fromcode; *fromptr; fromptr ++)
273 if (!isalnum(*fromptr & 255) && *fromptr != '-')
274 break;
275 *fromptr = '\0';
276
277 if (strcasecmp(fromcode, "utf-8"))
278 {
279 if ((ic = iconv_open("UTF-8", fromcode)) == (iconv_t)-1)
280 {
281 fprintf(stderr,
282 "DEBUG: Unable to convert character set \"%s\": %s\n",
283 fromcode, strerror(errno));
284 ic = 0;
285 }
286 }
287 }
288 }
289
290 strncpy(id, ptr, sizeof(id) - 1);
291 id[sizeof(id) - 1] = '\0';
292 str[0] = '\0';
293 }
294 else if (!strncmp(line, "msgstr", 6))
295 {
296 in_id = 0;
297 in_str = 1;
298
299 strncpy(str, ptr, sizeof(str) - 1);
300 str[sizeof(str) - 1] = '\0';
301 }
302 else if (line[0] == '\"' && in_str)
303 {
304 unsigned int str_len = strlen(str),
305 ptr_len = strlen(ptr);
306
307
308 if ((str_len + ptr_len + 1) > sizeof(str))
309 ptr_len = sizeof(str) - str_len - 1;
310
311 if (ptr_len > 0)
312 {
313 memcpy(str + str_len, ptr, ptr_len);
314 str[str_len + ptr_len] = '\0';
315 }
316 }
317 else if (line[0] == '\"' && in_id)
318 {
319 unsigned int id_len = strlen(id),
320 ptr_len = strlen(ptr);
321
322
323 if ((id_len + ptr_len + 1) > sizeof(id))
324 ptr_len = sizeof(id) - id_len - 1;
325
326 if (ptr_len > 0)
327 {
328 memcpy(id + id_len, ptr, ptr_len);
329 id[id_len + ptr_len] = '\0';
330 }
331 }
332 else
333 {
334 fprintf(stderr, "DEBUG: Unexpected text on line %d of %s!\n",
335 linenum, poname);
336 break;
337 }
338 }
339
340 if (id[0] && str[0])
341 {
342 stpi_unquote(id);
343
344 if (ic)
345 {
346 /*
347 * Convert string to UTF-8...
348 */
349
350 inbytes = strlen(str);
351 inptr = str;
352 outbytes = sizeof(utf8str);
353 outptr = utf8str;
354
355 iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
356 *outptr = '\0';
357
358 /*
359 * Add it to the string list...
360 */
361
362 stpi_unquote(utf8str);
363 stp_string_list_add_string_unsafe(po, id, utf8str);
364 }
365 else
366 {
367 stpi_unquote(str);
368 stp_string_list_add_string_unsafe(po, id, str);
369 }
370 }
371
372 fclose(pofile);
373
374 /*
375 * Add this to the cache...
376 */
377
378 if ((pocache = calloc(1, sizeof(stpi_i18n_t))) != NULL)
379 {
380 strncpy(pocache->locale, locale, sizeof(pocache->locale) - 1);
381 pocache->po = po;
382 pocache->next = stpi_pocache;
383 stpi_pocache = pocache;
384 }
385
386 if (ic)
387 iconv_close(ic);
388 return (po);
389 }
390
391
392 /*
393 * 'stp_i18n_lookup()' - Lookup a string in the message catalog...
394 */
395
396 const char * /* O - Localized message */
397 stp_i18n_lookup(
398 const stp_string_list_t *po, /* I - Message catalog */
399 const char *message) /* I - Message */
400 {
401 stp_param_string_t *param; /* Matching message */
402
403
404 if (po && (param = stp_string_list_find(po, message)) != NULL && param->text)
405 return (param->text);
406 else
407 return (message);
408 }
409
410
411 /*
412 * 'stp_i18n_printf()' - Send a formatted string to stderr.
413 */
414
415 void
416 stp_i18n_printf(
417 const stp_string_list_t *po, /* I - Message catalog */
418 const char *message, /* I - Printf-style message */
419 ...) /* I - Additional arguments as needed */
420 {
421 va_list ap; /* Argument pointer */
422
423
424 va_start(ap, message);
425 vfprintf(stderr, stp_i18n_lookup(po, message), ap);
426 va_end(ap);
427 }
428
429
430 /*
431 * 'stpi_unquote()' - Unquote characters in strings.
432 */
433
434 static void
435 stpi_unquote(char *s) /* IO - Original string */
436 {
437 char *d = s; /* Destination pointer */
438
439
440 while (*s)
441 {
442 if (*s == '\\')
443 {
444 s ++;
445 if (isdigit(*s))
446 {
447 *d = 0;
448
449 while (isdigit(*s))
450 {
451 *d = *d * 8 + *s - '0';
452 s ++;
453 }
454
455 d ++;
456 }
457 else
458 {
459 if (*s == 'n')
460 *d ++ = '\n';
461 else if (*s == 'r')
462 *d ++ = '\r';
463 else if (*s == 't')
464 *d ++ = '\t';
465 else
466 *d++ = *s;
467
468 s ++;
469 }
470 }
471 else
472 *d++ = *s++;
473 }
474
475 *d = '\0';
476 }