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