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
|
/*
Checks the po file for C format correctness.
To compile it you need the gettext package installed
with its development files.
Compilation commandline:
gcc -lgettextpo -o msgcheckformat msgcheckformat.c
Usage:
msgcheckformat <pofile> [ask|fix]
----------------------------------------------------------------------------
Copyright (C) 2004 Szymon Stefanek (pragma at kvirc dot net),
2010 Fabio Bas (ctrlaltca at gmail dot com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <gettext-po.h>
int total_errors = 0;
int total_warnings = 0;
int total_remove = 0;
int total_ignore = 0;
int total_fix = 0;
int solution_mode = 0;
static void po_xerror( int severity, po_message_t message,
const char *filename, size_t lineno, size_t column,
int multiline_p, const char *message_text )
{
fprintf( stderr, "%s:%u:%u: %s\n",
filename, (unsigned int)lineno, (unsigned int)column, message_text );
if (severity) exit(1);
}
static void po_xerror2( int severity, po_message_t message1,
const char *filename1, size_t lineno1, size_t column1,
int multiline_p1, const char *message_text1,
po_message_t message2,
const char *filename2, size_t lineno2, size_t column2,
int multiline_p2, const char *message_text2 )
{
fprintf( stderr, "%s:%u:%u: %s\n",
filename1, (unsigned int)lineno1, (unsigned int)column1, message_text1 );
fprintf( stderr, "%s:%u:%u: %s\n",
filename2, (unsigned int)lineno2, (unsigned int)column2, message_text2 );
if (severity) exit(1);
}
static const struct po_xerror_handler po_xerror_handler = { po_xerror, po_xerror2 };
static void replace_crlf(const char * msg,char * buf)
{
while(*msg)
{
if(*msg == '\r')
{
*buf++ = '\\';
*buf++ = 'r';
msg++;
} else if(*msg == '\n')
{
*buf++ = '\\';
*buf++ = 'n';
msg++;
} else *buf++ = *msg++;
}
}
static int print_error(po_message_t m,const char * msgid,const char * msgstr,const char * error, int solution)
{
char bufferid[16000];
char bufferstr[16000];
int s = 0;
int is_fuzzy = po_message_is_fuzzy(m) ? 1 : 0;
replace_crlf(msgid,bufferid);
replace_crlf(msgstr,bufferstr);
fprintf(stderr,"%s in %s:\n msgid: %s\n msgstr: %s\n reason: %s\n\n",
is_fuzzy ? "Warning" : "Error",
is_fuzzy ? "fuzzy entry" : "entry",
bufferid,bufferstr,error);
if(is_fuzzy)total_warnings++;
else total_errors++;
switch(solution_mode)
{
case 1:
// ask the user
switch(solution)
{
case 1:
printf("Pick a solution: (i)gnore the problem, or (r)emove the offending translation:");
while(s != 'i' && s != 'r') s = getchar();
if(s == 'r')
{
// delete the wrong translation
/* Change the msgstr (translation) of a message.
Use an empty string to denote an untranslated message. */
po_message_set_msgstr(m, "");
total_remove++;
} else {
total_ignore++;
}
break;
case 2:
printf("Pick a solution: (i)gnore the problem, (f)ix it or (r)emove the offending translation:");
while(s != 'i' && s != 'f' && s != 'r') s = getchar();
if(s == 'r')
{
// delete the wrong translation
/* Change the msgstr (translation) of a message.
Use an empty string to denote an untranslated message. */
po_message_set_msgstr(m, "");
total_remove++;
} else if(s == 'f') {
total_fix++;
return 1;
} else {
total_ignore++;
}
break;
}
break;
case 2:
// fix automatically
switch(solution)
{
case 1:
// delete the wrong translation
/* Change the msgstr (translation) of a message.
Use an empty string to denote an untranslated message. */
po_message_set_msgstr(m, "");
total_remove++;
break;
case 2:
total_fix++;
return 1;
break;
}
break;
default:
// do nothing
break;
}
return 0;
}
inline int is_valid_fmtstr(const char * p)
{
if(!*p) return 0;
if(*p != '%') return 0;
p++;
if(!*p) return 0;
if((*p >= '1' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z'))
{
p--;
return 1;
}
p--;
return 0;
}
static void process_message(po_message_t m)
{
const char * msgstr = po_message_msgstr(m);
const char * msgid = po_message_msgid(m);
const char *p;
char *q;
char *fixedstr;
if(msgid[0] == '\0')return;
if(strlen(msgid) < 1)return;
if(strlen(msgstr) < 1)return;
if(msgstr[0] == '\0')return;
fixedstr = strdup(msgstr);
p = msgid;
q = fixedstr;
while(*p && *q)
{
while(*p && !is_valid_fmtstr(p))p++;
while(*q && !is_valid_fmtstr(q))q++;
if(*q && *p)
{
q++;
p++;
if(*p != *q)
{
char buff[200];
sprintf(buff,"Mismatched format character (%c != %c)",*p,*q);
if(print_error(m,msgid,fixedstr,buff, 2))
{
//fix the format string
*q=*p;
po_message_set_msgstr(m, fixedstr);
}
}
}
}
if(*p || *q)
{
print_error(m,msgid,msgstr,"Mismatched number of format characters", 1);
}
}
static void process_messages(po_message_iterator_t it)
{
po_message_t m = po_next_message(it);
while(m)
{
process_message(m);
m = po_next_message(it);
}
}
int main(int argc, char **argv)
{
po_file_t po;
po_message_iterator_t it;
const char * const * domains;
if(!argv[1])
{
fprintf(stderr,"usage: %s <pofile> [ask|fix]\n",argv[0]);
return 0;
}
if(argv[2])
{
if(!strncmp(argv[2], "ask", 3))
{
solution_mode=1;
} else if(!strncmp(argv[2], "fix", 3)) {
solution_mode=2;
} else {
fprintf(stderr,"usage: %s <pofile> [ask|fix]\n",argv[0]);
return 0;
}
}
po = po_file_read(argv[1], &po_xerror_handler);
if(!po)
{
fprintf(stderr,"Couldn't read the input po file\n");
return 0;
}
domains = po_file_domains(po);
if(!domains)
{
fprintf(stderr,"Couldn't find the message domains in the po file\n");
return 0;
}
while(*domains)
{
it = po_message_iterator(po,*domains);
process_messages(it);
po_message_iterator_free(it);
domains++;
}
if(total_errors == 0 && total_warnings == 0)
{
fprintf(stderr,"No errors found\n");
} else if(solution_mode==0) {
fprintf(stderr,"%d warnings, %d errors\n",total_warnings,total_errors);
} else {
fprintf(stderr,"%d warnings, %d errors, %d fixes, %d removed, %d ignored\n",total_warnings,total_errors, total_fix, total_remove, total_ignore);
if(total_fix || total_remove)
{
int s = 0;
printf("Commit changes to file? (y)es, (n)o:");
while(s != 'y' && s != 'n') s = getchar();
if(s == 'y')
{
if(po_file_write(po, argv[1], &po_xerror_handler)) printf("Changes committed.");
else printf("Error writing file.");
} else {
printf("Changes discarded.");
}
}
}
po_file_free(po);
return 0;
}
|