]> git.ipfire.org Git - thirdparty/cups.git/blob - locale/checkpo.c
638e1bb27fbabc0ca5ea25a432539f60f3a558dd
[thirdparty/cups.git] / locale / checkpo.c
1 /*
2 * "$Id$"
3 *
4 * Verify that translations in the .po file have the same number and type of
5 * printf-style format strings.
6 *
7 * Copyright 2007-2010 by Apple Inc.
8 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 *
10 * These coded instructions, statements, and computer programs are the
11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
15 *
16 * Usage:
17 *
18 * checkpo filename.po [... filenameN.po]
19 *
20 * Compile with:
21 *
22 * gcc -o checkpo checkpo.c `cups-config --libs`
23 *
24 * Contents:
25 *
26 * main() - Validate .po files.
27 * abbreviate() - Abbreviate a message string as needed.
28 * collect_formats() - Collect all of the format strings in the msgid.
29 * free_formats() - Free all of the format strings.
30 */
31
32 #include <cups/cups-private.h>
33
34
35 /*
36 * Local functions...
37 */
38
39 static char *abbreviate(const char *s, char *buf, int bufsize);
40 static cups_array_t *collect_formats(const char *id);
41 static void free_formats(cups_array_t *fmts);
42
43
44 /*
45 * 'main()' - Validate .po files.
46 */
47
48 int /* O - Exit code */
49 main(int argc, /* I - Number of command-line args */
50 char *argv[]) /* I - Command-line arguments */
51 {
52 int i; /* Looping var */
53 cups_array_t *po; /* .po file */
54 _cups_message_t *msg; /* Current message */
55 cups_array_t *idfmts, /* Format strings in msgid */
56 *strfmts; /* Format strings in msgstr */
57 char *idfmt, /* Current msgid format string */
58 *strfmt; /* Current msgstr format string */
59 int fmtidx; /* Format index */
60 int status, /* Exit status */
61 pass, /* Pass/fail status */
62 untranslated; /* Untranslated messages */
63 char idbuf[80], /* Abbreviated msgid */
64 strbuf[80]; /* Abbreviated msgstr */
65
66
67 if (argc < 2)
68 {
69 puts("Usage: checkpo filename.po [... filenameN.po]");
70 return (1);
71 }
72
73 /*
74 * Check every .po file on the command-line...
75 */
76
77 for (i = 1, status = 0; i < argc; i ++)
78 {
79 /*
80 * Use the CUPS .po loader to get the message strings...
81 */
82
83 if ((po = _cupsMessageLoad(argv[i], 0)) == NULL)
84 {
85 perror(argv[i]);
86 return (1);
87 }
88
89 printf("%s: ", argv[i]);
90 fflush(stdout);
91
92 /*
93 * Scan every message for a % string and then match them up with
94 * the corresponding string in the translation...
95 */
96
97 pass = 1;
98 untranslated = 0;
99
100 for (msg = (_cups_message_t *)cupsArrayFirst(po);
101 msg;
102 msg = (_cups_message_t *)cupsArrayNext(po))
103 {
104 if (!msg->str || !msg->str[0])
105 {
106 untranslated ++;
107 continue;
108 }
109 else if (strchr(msg->id, '%'))
110 {
111 idfmts = collect_formats(msg->id);
112 strfmts = collect_formats(msg->str);
113 fmtidx = 0;
114
115 for (strfmt = (char *)cupsArrayFirst(strfmts);
116 strfmt;
117 strfmt = (char *)cupsArrayNext(strfmts))
118 {
119 if (isdigit(strfmt[1] & 255) && strfmt[2] == '$')
120 {
121 /*
122 * Handle positioned format stuff...
123 */
124
125 fmtidx = strfmt[1] - '1';
126 strfmt += 3;
127 if ((idfmt = (char *)cupsArrayIndex(idfmts, fmtidx)) != NULL)
128 idfmt ++;
129 }
130 else
131 {
132 /*
133 * Compare against the current format...
134 */
135
136 idfmt = (char *)cupsArrayIndex(idfmts, fmtidx);
137 }
138
139 fmtidx ++;
140
141 if (!idfmt || strcmp(strfmt, idfmt))
142 break;
143 }
144
145 if (cupsArrayCount(strfmts) != cupsArrayCount(idfmts) || strfmt)
146 {
147 if (pass)
148 {
149 pass = 0;
150 puts("FAIL");
151 }
152
153 printf(" Bad translation string \"%s\"\n for \"%s\"\n",
154 abbreviate(msg->str, strbuf, sizeof(strbuf)),
155 abbreviate(msg->id, idbuf, sizeof(idbuf)));
156 fputs(" Translation formats:", stdout);
157 for (strfmt = (char *)cupsArrayFirst(strfmts);
158 strfmt;
159 strfmt = (char *)cupsArrayNext(strfmts))
160 printf(" %s", strfmt);
161 fputs("\n Original formats:", stdout);
162 for (idfmt = (char *)cupsArrayFirst(idfmts);
163 idfmt;
164 idfmt = (char *)cupsArrayNext(idfmts))
165 printf(" %s", idfmt);
166 putchar('\n');
167 putchar('\n');
168 }
169
170 free_formats(idfmts);
171 free_formats(strfmts);
172 }
173
174 /*
175 * Only allow \\, \n, \r, \t, \", and \### character escapes...
176 */
177
178 for (strfmt = msg->str; *strfmt; strfmt ++)
179 if (*strfmt == '\\' &&
180 strfmt[1] != '\\' && strfmt[1] != 'n' && strfmt[1] != 'r' &&
181 strfmt[1] != 't' && strfmt[1] != '\"' && !isdigit(strfmt[1] & 255))
182 {
183 if (pass)
184 {
185 pass = 0;
186 puts("FAIL");
187 }
188
189 printf(" Bad escape \\%c in filter message \"%s\"\n"
190 " for \"%s\"\n\n", strfmt[1],
191 abbreviate(msg->str, strbuf, sizeof(strbuf)),
192 abbreviate(msg->id, idbuf, sizeof(idbuf)));
193 break;
194 }
195
196 /*
197 * Make sure filter message prefixes are not translated...
198 */
199
200 if (!strncmp(msg->id, "ALERT:", 6) || !strncmp(msg->id, "CRIT:", 5) ||
201 !strncmp(msg->id, "DEBUG:", 6) || !strncmp(msg->id, "DEBUG2:", 7) ||
202 !strncmp(msg->id, "EMERG:", 6) || !strncmp(msg->id, "ERROR:", 6) ||
203 !strncmp(msg->id, "INFO:", 5) || !strncmp(msg->id, "NOTICE:", 7) ||
204 !strncmp(msg->id, "WARNING:", 8))
205 {
206 if (pass)
207 {
208 pass = 0;
209 puts("FAIL");
210 }
211
212 printf(" Bad prefix on filter message \"%s\"\n\n",
213 abbreviate(msg->id, idbuf, sizeof(idbuf)));
214 }
215 }
216
217 if (pass)
218 {
219 if ((untranslated * 10) >= cupsArrayCount(po))
220 {
221 /*
222 * Only allow 10% of messages to be untranslated before we fail...
223 */
224
225 pass = 0;
226 puts("FAIL");
227 printf(" Too many untranslated messages (%d of %d)\n\n",
228 untranslated, cupsArrayCount(po));
229 }
230 else if (untranslated > 0)
231 printf("PASS (%d of %d untranslated)\n\n", untranslated,
232 cupsArrayCount(po));
233 else
234 puts("PASS\n");
235 }
236
237 if (!pass)
238 status = 1;
239
240 _cupsMessageFree(po);
241 }
242
243 return (status);
244 }
245
246
247 /*
248 * 'abbreviate()' - Abbreviate a message string as needed.
249 */
250
251 static char * /* O - Abbreviated string */
252 abbreviate(const char *s, /* I - String to abbreviate */
253 char *buf, /* I - Buffer */
254 int bufsize) /* I - Size of buffer */
255 {
256 char *bufptr; /* Pointer into buffer */
257
258
259 for (bufptr = buf, bufsize -= 4; *s && bufsize > 0; s ++)
260 {
261 if (*s == '\n')
262 {
263 if (bufsize < 2)
264 break;
265
266 *bufptr++ = '\\';
267 *bufptr++ = 'n';
268 bufsize -= 2;
269 }
270 else if (*s == '\t')
271 {
272 if (bufsize < 2)
273 break;
274
275 *bufptr++ = '\\';
276 *bufptr++ = 't';
277 bufsize -= 2;
278 }
279 else if (*s >= 0 && *s < ' ')
280 {
281 if (bufsize < 4)
282 break;
283
284 sprintf(bufptr, "\\%03o", *s);
285 bufptr += 4;
286 bufsize -= 4;
287 }
288 else
289 {
290 *bufptr++ = *s;
291 bufsize --;
292 }
293 }
294
295 if (*s)
296 strcpy(bufptr, "...");
297 else
298 *bufptr = '\0';
299
300 return (buf);
301 }
302
303
304 /*
305 * 'collect_formats()' - Collect all of the format strings in the msgid.
306 */
307
308 static cups_array_t * /* O - Array of format strings */
309 collect_formats(const char *id) /* I - msgid string */
310 {
311 cups_array_t *fmts; /* Array of format strings */
312 char buf[255], /* Format string buffer */
313 *bufptr; /* Pointer into format string */
314
315
316 fmts = cupsArrayNew(NULL, NULL);
317
318 while ((id = strchr(id, '%')) != NULL)
319 {
320 if (id[1] == '%')
321 {
322 /*
323 * Skip %%...
324 */
325
326 id += 2;
327 continue;
328 }
329
330 for (bufptr = buf; *id && bufptr < (buf + sizeof(buf) - 1); id ++)
331 {
332 *bufptr++ = *id;
333
334 if (strchr("CDEFGIOSUXcdeifgopsux", *id))
335 {
336 id ++;
337 break;
338 }
339 }
340
341 *bufptr = '\0';
342 cupsArrayAdd(fmts, strdup(buf));
343 }
344
345 return (fmts);
346 }
347
348
349 /*
350 * 'free_formats()' - Free all of the format strings.
351 */
352
353 static void
354 free_formats(cups_array_t *fmts) /* I - Array of format strings */
355 {
356 char *s; /* Current string */
357
358
359 for (s = (char *)cupsArrayFirst(fmts); s; s = (char *)cupsArrayNext(fmts))
360 free(s);
361
362 cupsArrayDelete(fmts);
363 }
364
365
366 /*
367 * End of "$Id$".
368 */