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