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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/* XML resource locating rules
Copyright (C) 2015-2016 Free Software Foundation, Inc.
This file was written by Daiki Ueno <ueno@gnu.org>, 2015.
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 3 of the License, 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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* Specification. */
#include "locating-rule.h"
#include "basename.h"
#include "concat-filename.h"
#include "c-strcase.h"
#if HAVE_DIRENT_H
# include <dirent.h>
#endif
#if HAVE_DIRENT_H
# define HAVE_DIR 1
#else
# define HAVE_DIR 0
#endif
#include "dir-list.h"
#include <errno.h>
#include "error.h"
#include "filename.h"
#include <fnmatch.h>
#include "gettext.h"
#include "hash.h"
#include <libxml/parser.h>
#include <libxml/uri.h>
#include "xalloc.h"
#define _(str) gettext (str)
#define LOCATING_RULES_NS "https://www.gnu.org/s/gettext/ns/locating-rules/1.0"
struct document_locating_rule_ty
{
char *ns;
char *local_name;
char *target;
};
struct document_locating_rule_list_ty
{
struct document_locating_rule_ty *items;
size_t nitems;
size_t nitems_max;
};
struct locating_rule_ty
{
char *pattern;
char *name;
struct document_locating_rule_list_ty doc_rules;
char *target;
};
struct locating_rule_list_ty
{
struct locating_rule_ty *items;
size_t nitems;
size_t nitems_max;
};
static char *
get_attribute (xmlNode *node, const char *attr)
{
xmlChar *value;
char *result;
value = xmlGetProp (node, BAD_CAST attr);
result = xstrdup ((const char *) value);
xmlFree (value);
return result;
}
static const char *
document_locating_rule_match (struct document_locating_rule_ty *rule,
xmlDoc *doc)
{
xmlNode *root;
root = xmlDocGetRootElement (doc);
if (rule->ns != NULL)
{
if (root->ns == NULL
|| !xmlStrEqual (root->ns->href, BAD_CAST rule->ns))
return NULL;
}
if (rule->local_name != NULL)
{
if (!xmlStrEqual (root->name,
BAD_CAST rule->local_name))
return NULL;
}
return rule->target;
}
static const char *
locating_rule_match (struct locating_rule_ty *rule,
const char *filename,
const char *name)
{
if (name != NULL)
{
if (rule->name == NULL || c_strcasecmp (name, rule->name) != 0)
return NULL;
}
else
{
const char *base;
char *reduced;
int err;
base = strrchr (filename, '/');
if (!base)
base = filename;
reduced = xstrdup (base);
/* Remove a trailing ".in" - it's a generic suffix. */
while (strlen (reduced) >= 3
&& memcmp (reduced + strlen (reduced) - 3, ".in", 3) == 0)
reduced[strlen (reduced) - 3] = '\0';
err = fnmatch (rule->pattern, basename (reduced), FNM_PATHNAME);
free (reduced);
if (err != 0)
return NULL;
}
/* Check documentRules. */
if (rule->doc_rules.nitems > 0)
{
const char *target;
xmlDoc *doc;
size_t i;
doc = xmlReadFile (filename, NULL,
XML_PARSE_NONET
| XML_PARSE_NOWARNING
| XML_PARSE_NOBLANKS
| XML_PARSE_NOERROR);
if (doc == NULL)
{
xmlError *err = xmlGetLastError ();
error (0, 0, _("cannot read %s: %s"), filename, err->message);
return NULL;
}
for (i = 0, target = NULL; i < rule->doc_rules.nitems; i++)
{
target =
document_locating_rule_match (&rule->doc_rules.items[i], doc);
if (target)
break;
}
xmlFreeDoc (doc);
if (target != NULL)
return target;
}
if (rule->target != NULL)
return rule->target;
return NULL;
}
const char *
locating_rule_list_locate (struct locating_rule_list_ty *rules,
const char *filename,
const char *name)
{
const char *target = NULL;
size_t i;
for (i = 0; i < rules->nitems; i++)
{
if (IS_ABSOLUTE_PATH (filename))
{
target = locating_rule_match (&rules->items[i], filename, name);
if (target != NULL)
return target;
}
else
{
int j;
for (j = 0; ; ++j)
{
const char *dir = dir_list_nth (j);
char *new_filename;
if (dir == NULL)
break;
new_filename = xconcatenated_filename (dir, filename, NULL);
target = locating_rule_match (&rules->items[i], new_filename,
name);
free (new_filename);
if (target != NULL)
return target;
}
}
}
return NULL;
}
static void
missing_attribute (xmlNode *node, const char *attribute)
{
error (0, 0, _("\"%s\" node does not have \"%s\""), node->name, attribute);
}
static void
document_locating_rule_destroy (struct document_locating_rule_ty *rule)
{
free (rule->ns);
free (rule->local_name);
free (rule->target);
}
static void
document_locating_rule_list_add (struct document_locating_rule_list_ty *rules,
xmlNode *node)
{
struct document_locating_rule_ty rule;
if (!xmlHasProp (node, BAD_CAST "target"))
{
missing_attribute (node, "target");
return;
}
memset (&rule, 0, sizeof (struct document_locating_rule_ty));
if (xmlHasProp (node, BAD_CAST "ns"))
rule.ns = get_attribute (node, "ns");
if (xmlHasProp (node, BAD_CAST "localName"))
rule.local_name = get_attribute (node, "localName");
rule.target = get_attribute (node, "target");
if (rules->nitems == rules->nitems_max)
{
rules->nitems_max = 2 * rules->nitems_max + 1;
rules->items =
xrealloc (rules->items,
sizeof (struct document_locating_rule_ty)
* rules->nitems_max);
}
memcpy (&rules->items[rules->nitems++], &rule,
sizeof (struct document_locating_rule_ty));
}
static void
locating_rule_destroy (struct locating_rule_ty *rule)
{
size_t i;
for (i = 0; i < rule->doc_rules.nitems; i++)
document_locating_rule_destroy (&rule->doc_rules.items[i]);
free (rule->doc_rules.items);
free (rule->name);
free (rule->pattern);
free (rule->target);
}
static bool
locating_rule_list_add_from_file (struct locating_rule_list_ty *rules,
const char *rule_file_name)
{
xmlDoc *doc;
xmlNode *root, *node;
doc = xmlReadFile (rule_file_name, "utf-8",
XML_PARSE_NONET
| XML_PARSE_NOWARNING
| XML_PARSE_NOBLANKS
| XML_PARSE_NOERROR);
if (doc == NULL)
{
error (0, 0, _("cannot read XML file %s"), rule_file_name);
return false;
}
root = xmlDocGetRootElement (doc);
if (!(xmlStrEqual (root->name, BAD_CAST "locatingRules")
#if 0
&& root->ns
&& xmlStrEqual (root->ns->href, BAD_CAST LOCATING_RULES_NS)
#endif
))
{
error (0, 0, _("the root element is not \"locatingRules\""));
xmlFreeDoc (doc);
return false;
}
for (node = root->children; node; node = node->next)
{
if (xmlStrEqual (node->name, BAD_CAST "locatingRule"))
{
struct locating_rule_ty rule;
if (!xmlHasProp (node, BAD_CAST "pattern"))
{
missing_attribute (node, "pattern");
xmlFreeDoc (doc);
continue;
}
memset (&rule, 0, sizeof (struct locating_rule_ty));
rule.pattern = get_attribute (node, "pattern");
if (xmlHasProp (node, BAD_CAST "name"))
rule.name = get_attribute (node, "name");
if (xmlHasProp (node, BAD_CAST "target"))
rule.target = get_attribute (node, "target");
else
{
xmlNode *n;
for (n = node->children; n; n = n->next)
{
if (xmlStrEqual (n->name, BAD_CAST "documentRule"))
document_locating_rule_list_add (&rule.doc_rules, n);
}
}
if (rules->nitems == rules->nitems_max)
{
rules->nitems_max = 2 * rules->nitems_max + 1;
rules->items =
xrealloc (rules->items,
sizeof (struct locating_rule_ty) * rules->nitems_max);
}
memcpy (&rules->items[rules->nitems++], &rule,
sizeof (struct locating_rule_ty));
}
}
xmlFreeDoc (doc);
return true;
}
bool
locating_rule_list_add_from_directory (struct locating_rule_list_ty *rules,
const char *directory)
{
#if HAVE_DIR
DIR *dirp;
dirp = opendir (directory);
if (dirp == NULL)
return false;
for (;;)
{
struct dirent *dp;
errno = 0;
dp = readdir (dirp);
if (dp != NULL)
{
const char *name = dp->d_name;
size_t namlen = strlen (name);
if (namlen > 4 && memcmp (name + namlen - 4, ".loc", 4) == 0)
{
char *locator_file_name =
xconcatenated_filename (directory, name, NULL);
locating_rule_list_add_from_file (rules, locator_file_name);
free (locator_file_name);
}
}
else if (errno != 0)
return false;
else
break;
}
if (closedir (dirp))
return false;
#endif
return true;
}
struct locating_rule_list_ty *
locating_rule_list_alloc (void)
{
struct locating_rule_list_ty *result;
xmlCheckVersion (LIBXML_VERSION);
result = XCALLOC (1, struct locating_rule_list_ty);
return result;
}
void
locating_rule_list_destroy (struct locating_rule_list_ty *rules)
{
while (rules->nitems-- > 0)
locating_rule_destroy (&rules->items[rules->nitems]);
free (rules->items);
}
void
locating_rule_list_free (struct locating_rule_list_ty *rules)
{
if (rules != NULL)
locating_rule_list_destroy (rules);
free (rules);
}
|