]>
git.ipfire.org Git - thirdparty/cups.git/blob - locale/checkpo.c
513af92ae901636d54ebf3c1967e237c3a8ac8bd
2 * Verify that translations in the .po file have the same number and type of
3 * printf-style format strings.
5 * Copyright 2007-2012 by Apple Inc.
6 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * checkpo filename.po [... filenameN.po]
20 * gcc -o checkpo checkpo.c `cups-config --libs`
23 #include <cups/cups-private.h>
30 static char *abbreviate(const char *s
, char *buf
, int bufsize
);
31 static cups_array_t
*collect_formats(const char *id
);
32 static void free_formats(cups_array_t
*fmts
);
36 * 'main()' - Validate .po files.
39 int /* O - Exit code */
40 main(int argc
, /* I - Number of command-line args */
41 char *argv
[]) /* I - Command-line arguments */
43 int i
; /* Looping var */
44 cups_array_t
*po
; /* .po file */
45 _cups_message_t
*msg
; /* Current message */
46 cups_array_t
*idfmts
, /* Format strings in msgid */
47 *strfmts
; /* Format strings in msgstr */
48 char *idfmt
, /* Current msgid format string */
49 *strfmt
; /* Current msgstr format string */
50 int fmtidx
; /* Format index */
51 int status
, /* Exit status */
52 pass
, /* Pass/fail status */
53 untranslated
; /* Untranslated messages */
54 char idbuf
[80], /* Abbreviated msgid */
55 strbuf
[80]; /* Abbreviated msgstr */
60 puts("Usage: checkpo filename.po [... filenameN.po]");
65 * Check every .po file on the command-line...
68 for (i
= 1, status
= 0; i
< argc
; i
++)
71 * Use the CUPS .po loader to get the message strings...
74 if ((po
= _cupsMessageLoad(argv
[i
], 1)) == NULL
)
82 printf("%s: ", argv
[i
]);
86 * Scan every message for a % string and then match them up with
87 * the corresponding string in the translation...
93 for (msg
= (_cups_message_t
*)cupsArrayFirst(po
);
95 msg
= (_cups_message_t
*)cupsArrayNext(po
))
98 * Make sure filter message prefixes are not translated...
101 if (!strncmp(msg
->id
, "ALERT:", 6) || !strncmp(msg
->id
, "CRIT:", 5) ||
102 !strncmp(msg
->id
, "DEBUG:", 6) || !strncmp(msg
->id
, "DEBUG2:", 7) ||
103 !strncmp(msg
->id
, "EMERG:", 6) || !strncmp(msg
->id
, "ERROR:", 6) ||
104 !strncmp(msg
->id
, "INFO:", 5) || !strncmp(msg
->id
, "NOTICE:", 7) ||
105 !strncmp(msg
->id
, "WARNING:", 8))
113 printf(" Bad prefix on filter message \"%s\"\n",
114 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
117 idfmt
= msg
->id
+ strlen(msg
->id
) - 1;
118 if (idfmt
>= msg
->id
&& *idfmt
== '\n')
126 printf(" Trailing newline in message \"%s\"\n",
127 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
130 for (; idfmt
>= msg
->id
; idfmt
--)
131 if (!isspace(*idfmt
& 255))
134 if (idfmt
>= msg
->id
&& *idfmt
== '!')
142 printf(" Exclamation in message \"%s\"\n",
143 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
146 if ((idfmt
- 2) >= msg
->id
&& !strncmp(idfmt
- 2, "...", 3))
154 printf(" Ellipsis in message \"%s\"\n",
155 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
159 if (!msg
->str
|| !msg
->str
[0])
164 else if (strchr(msg
->id
, '%'))
166 idfmts
= collect_formats(msg
->id
);
167 strfmts
= collect_formats(msg
->str
);
170 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
172 strfmt
= (char *)cupsArrayNext(strfmts
))
174 if (isdigit(strfmt
[1] & 255) && strfmt
[2] == '$')
177 * Handle positioned format stuff...
180 fmtidx
= strfmt
[1] - '1';
182 if ((idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
)) != NULL
)
188 * Compare against the current format...
191 idfmt
= (char *)cupsArrayIndex(idfmts
, fmtidx
);
196 if (!idfmt
|| strcmp(strfmt
, idfmt
))
200 if (cupsArrayCount(strfmts
) != cupsArrayCount(idfmts
) || strfmt
)
208 printf(" Bad translation string \"%s\"\n for \"%s\"\n",
209 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
210 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
211 fputs(" Translation formats:", stdout
);
212 for (strfmt
= (char *)cupsArrayFirst(strfmts
);
214 strfmt
= (char *)cupsArrayNext(strfmts
))
215 printf(" %s", strfmt
);
216 fputs("\n Original formats:", stdout
);
217 for (idfmt
= (char *)cupsArrayFirst(idfmts
);
219 idfmt
= (char *)cupsArrayNext(idfmts
))
220 printf(" %s", idfmt
);
225 free_formats(idfmts
);
226 free_formats(strfmts
);
230 * Only allow \\, \n, \r, \t, \", and \### character escapes...
233 for (strfmt
= msg
->str
; *strfmt
; strfmt
++)
234 if (*strfmt
== '\\' &&
235 strfmt
[1] != '\\' && strfmt
[1] != 'n' && strfmt
[1] != 'r' &&
236 strfmt
[1] != 't' && strfmt
[1] != '\"' && !isdigit(strfmt
[1] & 255))
244 printf(" Bad escape \\%c in filter message \"%s\"\n"
245 " for \"%s\"\n", strfmt
[1],
246 abbreviate(msg
->str
, strbuf
, sizeof(strbuf
)),
247 abbreviate(msg
->id
, idbuf
, sizeof(idbuf
)));
254 if ((untranslated
* 10) >= cupsArrayCount(po
) &&
255 strcmp(argv
[i
], "cups.pot"))
258 * Only allow 10% of messages to be untranslated before we fail...
263 printf(" Too many untranslated messages (%d of %d)\n",
264 untranslated
, cupsArrayCount(po
));
266 else if (untranslated
> 0)
267 printf("PASS (%d of %d untranslated)\n", untranslated
,
276 _cupsMessageFree(po
);
284 * 'abbreviate()' - Abbreviate a message string as needed.
287 static char * /* O - Abbreviated string */
288 abbreviate(const char *s
, /* I - String to abbreviate */
289 char *buf
, /* I - Buffer */
290 int bufsize
) /* I - Size of buffer */
292 char *bufptr
; /* Pointer into buffer */
295 for (bufptr
= buf
, bufsize
-= 4; *s
&& bufsize
> 0; s
++)
315 else if (*s
>= 0 && *s
< ' ')
320 sprintf(bufptr
, "\\%03o", *s
);
332 memcpy(bufptr
, "...", 4);
341 * 'collect_formats()' - Collect all of the format strings in the msgid.
344 static cups_array_t
* /* O - Array of format strings */
345 collect_formats(const char *id
) /* I - msgid string */
347 cups_array_t
*fmts
; /* Array of format strings */
348 char buf
[255], /* Format string buffer */
349 *bufptr
; /* Pointer into format string */
352 fmts
= cupsArrayNew(NULL
, NULL
);
354 while ((id
= strchr(id
, '%')) != NULL
)
366 for (bufptr
= buf
; *id
&& bufptr
< (buf
+ sizeof(buf
) - 1); id
++)
370 if (strchr("CDEFGIOSUXcdeifgopsux", *id
))
378 cupsArrayAdd(fmts
, strdup(buf
));
386 * 'free_formats()' - Free all of the format strings.
390 free_formats(cups_array_t
*fmts
) /* I - Array of format strings */
392 char *s
; /* Current string */
395 for (s
= (char *)cupsArrayFirst(fmts
); s
; s
= (char *)cupsArrayNext(fmts
))
398 cupsArrayDelete(fmts
);