Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / src / locating-rule.c
1 /* XML resource locating rules
2    Copyright (C) 2015 Free Software Foundation, Inc.
3
4    This file was written by Daiki Ueno <ueno@gnu.org>, 2015.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification.  */
24 #include "locating-rule.h"
25
26 #include "basename.h"
27 #include "concat-filename.h"
28 #include "c-strcase.h"
29
30 #if HAVE_DIRENT_H
31 # include <dirent.h>
32 #endif
33
34 #if HAVE_DIRENT_H
35 # define HAVE_DIR 1
36 #else
37 # define HAVE_DIR 0
38 #endif
39
40 #include "dir-list.h"
41 #include <errno.h>
42 #include "error.h"
43 #include "filename.h"
44 #include <fnmatch.h>
45 #include "gettext.h"
46 #include "hash.h"
47 #include <libxml/parser.h>
48 #include <libxml/uri.h>
49 #include "xalloc.h"
50
51 #define _(str) gettext (str)
52
53 #define LOCATING_RULES_NS "https://www.gnu.org/s/gettext/ns/locating-rules/1.0"
54
55 struct document_locating_rule_ty
56 {
57   char *ns;
58   char *local_name;
59
60   char *target;
61 };
62
63 struct document_locating_rule_list_ty
64 {
65   struct document_locating_rule_ty *items;
66   size_t nitems;
67   size_t nitems_max;
68 };
69
70 struct locating_rule_ty
71 {
72   char *pattern;
73   char *name;
74
75   struct document_locating_rule_list_ty doc_rules;
76   char *target;
77 };
78
79 struct locating_rule_list_ty
80 {
81   struct locating_rule_ty *items;
82   size_t nitems;
83   size_t nitems_max;
84 };
85
86 static char *
87 get_attribute (xmlNode *node, const char *attr)
88 {
89   xmlChar *value;
90   char *result;
91
92   value = xmlGetProp (node, BAD_CAST attr);
93   result = xstrdup ((const char *) value);
94   xmlFree (value);
95
96   return result;
97 }
98
99 static const char *
100 document_locating_rule_match (struct document_locating_rule_ty *rule,
101                               xmlDoc *doc)
102 {
103   xmlNode *root;
104
105   root = xmlDocGetRootElement (doc);
106   if (rule->ns != NULL)
107     {
108       if (root->ns == NULL
109           || !xmlStrEqual (root->ns->href, BAD_CAST rule->ns))
110         return NULL;
111     }
112
113   if (rule->local_name != NULL)
114     {
115       if (!xmlStrEqual (root->name,
116                         BAD_CAST rule->local_name))
117         return NULL;
118     }
119
120   return rule->target;
121 }
122
123 static const char *
124 locating_rule_match (struct locating_rule_ty *rule,
125                      const char *filename,
126                      const char *name)
127 {
128   if (name != NULL)
129     {
130       if (rule->name == NULL || c_strcasecmp (name, rule->name) != 0)
131         return NULL;
132     }
133   else
134     {
135       const char *base;
136       char *reduced;
137       int err;
138
139       base = strrchr (filename, '/');
140       if (!base)
141         base = filename;
142
143       reduced = xstrdup (base);
144       /* Remove a trailing ".in" - it's a generic suffix.  */
145       while (strlen (reduced) >= 3
146              && memcmp (reduced + strlen (reduced) - 3, ".in", 3) == 0)
147         reduced[strlen (reduced) - 3] = '\0';
148
149       err = fnmatch (rule->pattern, basename (reduced), FNM_PATHNAME);
150       free (reduced);
151       if (err != 0)
152         return NULL;
153     }
154
155   /* Check documentRules.  */
156   if (rule->doc_rules.nitems > 0)
157     {
158       const char *target;
159       xmlDoc *doc;
160       size_t i;
161
162       doc = xmlReadFile (filename, NULL,
163                          XML_PARSE_NONET
164                          | XML_PARSE_NOWARNING
165                          | XML_PARSE_NOBLANKS
166                          | XML_PARSE_NOERROR);
167       if (doc == NULL)
168         {
169           xmlError *err = xmlGetLastError ();
170           error (0, 0, _("cannot read %s: %s"), filename, err->message);
171           return NULL;
172         }
173
174       for (i = 0, target = NULL; i < rule->doc_rules.nitems; i++)
175         {
176           target =
177             document_locating_rule_match (&rule->doc_rules.items[i], doc);
178           if (target)
179             break;
180         }
181       xmlFreeDoc (doc);
182       if (target != NULL)
183         return target;
184     }
185
186   if (rule->target != NULL)
187     return rule->target;
188
189   return NULL;
190 }
191
192 const char *
193 locating_rule_list_locate (struct locating_rule_list_ty *rules,
194                            const char *filename,
195                            const char *name)
196 {
197   const char *target = NULL;
198   size_t i;
199
200   for (i = 0; i < rules->nitems; i++)
201     {
202       if (IS_ABSOLUTE_PATH (filename))
203         {
204           target = locating_rule_match (&rules->items[i], filename, name);
205           if (target != NULL)
206             return target;
207         }
208       else
209         {
210           int j;
211
212           for (j = 0; ; ++j)
213             {
214               const char *dir = dir_list_nth (j);
215               char *new_filename;
216
217               if (dir == NULL)
218                 break;
219               
220               new_filename = xconcatenated_filename (dir, filename, NULL);
221               target = locating_rule_match (&rules->items[i], new_filename,
222                                             name);
223               free (new_filename);
224               if (target != NULL)
225                 return target;
226             }
227         }
228     }
229
230   return NULL;
231 }
232
233 static void
234 missing_attribute (xmlNode *node, const char *attribute)
235 {
236   error (0, 0, _("\"%s\" node does not have \"%s\""), node->name, attribute);
237 }
238
239 static void
240 document_locating_rule_destroy (struct document_locating_rule_ty *rule)
241 {
242   free (rule->ns);
243   free (rule->local_name);
244   free (rule->target);
245 }
246
247 static void
248 document_locating_rule_list_add (struct document_locating_rule_list_ty *rules,
249                                  xmlNode *node)
250 {
251   struct document_locating_rule_ty rule;
252
253   if (!xmlHasProp (node, BAD_CAST "target"))
254     {
255       missing_attribute (node, "target");
256       return;
257     }
258
259   memset (&rule, 0, sizeof (struct document_locating_rule_ty));
260
261   if (xmlHasProp (node, BAD_CAST "ns"))
262     rule.ns = get_attribute (node, "ns");
263   if (xmlHasProp (node, BAD_CAST "localName"))
264     rule.local_name = get_attribute (node, "localName");
265   rule.target = get_attribute (node, "target");
266
267   if (rules->nitems == rules->nitems_max)
268     {
269       rules->nitems_max = 2 * rules->nitems_max + 1;
270       rules->items =
271         xrealloc (rules->items,
272                   sizeof (struct document_locating_rule_ty)
273                   * rules->nitems_max);
274     }
275   memcpy (&rules->items[rules->nitems++], &rule,
276           sizeof (struct document_locating_rule_ty));
277 }
278
279 static void
280 locating_rule_destroy (struct locating_rule_ty *rule)
281 {
282   size_t i;
283
284   for (i = 0; i < rule->doc_rules.nitems; i++)
285     document_locating_rule_destroy (&rule->doc_rules.items[i]);
286   free (rule->doc_rules.items);
287
288   free (rule->name);
289   free (rule->pattern);
290   free (rule->target);
291 }
292
293 static bool
294 locating_rule_list_add_from_file (struct locating_rule_list_ty *rules,
295                                   const char *rule_file_name)
296 {
297   xmlDoc *doc;
298   xmlNode *root, *node;
299
300   doc = xmlReadFile (rule_file_name, "utf-8",
301                      XML_PARSE_NONET
302                      | XML_PARSE_NOWARNING
303                      | XML_PARSE_NOBLANKS
304                      | XML_PARSE_NOERROR);
305   if (doc == NULL)
306     {
307       error (0, 0, _("cannot read XML file %s"), rule_file_name);
308       return false;
309     }
310
311   root = xmlDocGetRootElement (doc);
312   if (!(xmlStrEqual (root->name, BAD_CAST "locatingRules")
313 #if 0
314         && root->ns
315         && xmlStrEqual (root->ns->href, BAD_CAST LOCATING_RULES_NS)
316 #endif
317         ))
318     {
319       error (0, 0, _("the root element is not \"locatingRules\""));
320       xmlFreeDoc (doc);
321       return false;
322     }
323
324   for (node = root->children; node; node = node->next)
325     {
326       if (xmlStrEqual (node->name, BAD_CAST "locatingRule"))
327         {
328           struct locating_rule_ty rule;
329
330           if (!xmlHasProp (node, BAD_CAST "pattern"))
331             {
332               missing_attribute (node, "pattern");
333               xmlFreeDoc (doc);
334               continue;
335             }
336
337           memset (&rule, 0, sizeof (struct locating_rule_ty));
338           rule.pattern = get_attribute (node, "pattern");
339           if (xmlHasProp (node, BAD_CAST "name"))
340             rule.name = get_attribute (node, "name");
341           if (xmlHasProp (node, BAD_CAST "target"))
342             rule.target = get_attribute (node, "target");
343           else
344             {
345               xmlNode *n;
346
347               for (n = node->children; n; n = n->next)
348                 {
349                   if (xmlStrEqual (n->name, BAD_CAST "documentRule"))
350                     document_locating_rule_list_add (&rule.doc_rules, n);
351                 }
352             }
353           if (rules->nitems == rules->nitems_max)
354             {
355               rules->nitems_max = 2 * rules->nitems_max + 1;
356               rules->items =
357                 xrealloc (rules->items,
358                           sizeof (struct locating_rule_ty) * rules->nitems_max);
359             }
360           memcpy (&rules->items[rules->nitems++], &rule,
361                   sizeof (struct locating_rule_ty));
362         }
363     }
364
365   xmlFreeDoc (doc);
366   return true;
367 }
368
369 bool
370 locating_rule_list_add_from_directory (struct locating_rule_list_ty *rules,
371                                        const char *directory)
372 {
373 #if HAVE_DIR
374   DIR *dirp;
375
376   dirp = opendir (directory);
377   if (dirp == NULL)
378     return false;
379
380   for (;;)
381     {
382       struct dirent *dp;
383
384       errno = 0;
385       dp = readdir (dirp);
386       if (dp != NULL)
387         {
388           const char *name = dp->d_name;
389           size_t namlen = strlen (name);
390
391           if (namlen > 4 && memcmp (name + namlen - 4, ".loc", 4) == 0)
392             {
393               char *locator_file_name =
394                 xconcatenated_filename (directory, name, NULL);
395               locating_rule_list_add_from_file (rules, locator_file_name);
396               free (locator_file_name);
397             }
398         }
399       else if (errno != 0)
400         return false;
401       else
402         break;
403     }
404   if (closedir (dirp))
405     return false;
406
407 #endif
408   return true;
409 }
410
411 struct locating_rule_list_ty *
412 locating_rule_list_alloc (void)
413 {
414   struct locating_rule_list_ty *result;
415
416   xmlCheckVersion (LIBXML_VERSION);
417
418   result = XCALLOC (1, struct locating_rule_list_ty);
419
420   return result;
421 }
422
423 void
424 locating_rule_list_destroy (struct locating_rule_list_ty *rules)
425 {
426   while (rules->nitems-- > 0)
427     locating_rule_destroy (&rules->items[rules->nitems]);
428   free (rules->items);
429 }
430
431 void
432 locating_rule_list_free (struct locating_rule_list_ty *rules)
433 {
434   if (rules != NULL)
435     locating_rule_list_destroy (rules);
436   free (rules);
437 }