]> granicus.if.org Git - imagemagick/blob - MagickCore/locale.c
(no commit message)
[imagemagick] / MagickCore / locale.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                  L       OOO    CCCC   AAA   L      EEEEE                   %
7 %                  L      O   O  C      A   A  L      E                       %
8 %                  L      O   O  C      AAAAA  L      EEE                     %
9 %                  L      O   O  C      A   A  L      E                       %
10 %                  LLLLL   OOO    CCCC  A   A  LLLLL  EEEEE                   %
11 %                                                                             %
12 %                                                                             %
13 %                      MagickCore Image Locale Methods                        %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 2003                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
28 %  Unless required by applicable law or agreed to in writing, software        %
29 %  distributed under the License is distributed on an "AS IS" BASIS,          %
30 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31 %  See the License for the specific language governing permissions and        %
32 %  limitations under the License.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/hashmap.h"
49 #include "MagickCore/locale_.h"
50 #include "MagickCore/locale-private.h"
51 #include "MagickCore/log.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/semaphore.h"
54 #include "MagickCore/splay-tree.h"
55 #include "MagickCore/string_.h"
56 #include "MagickCore/string-private.h"
57 #include "MagickCore/token.h"
58 #include "MagickCore/utility.h"
59 #include "MagickCore/utility-private.h"
60 #include "MagickCore/xml-tree.h"
61 \f
62 /*
63   Define declarations.
64 */
65 #define LocaleFilename  "locale.xml"
66 #define MaxRecursionDepth  200
67 \f
68 /*
69   Typedef declarations.
70 */
71 #if defined(__CYGWIN__)
72 typedef struct _locale_t *locale_t;
73 #endif
74 \f
75 /*
76   Static declarations.
77 */
78 static const char
79   *LocaleMap =
80     "<?xml version=\"1.0\"?>"
81     "<localemap>"
82     "  <locale name=\"C\">"
83     "    <Exception>"
84     "     <Message name=\"\">"
85     "     </Message>"
86     "    </Exception>"
87     "  </locale>"
88     "</localemap>";
89
90 static SemaphoreInfo
91   *locale_semaphore = (SemaphoreInfo *) NULL;
92
93 static SplayTreeInfo
94   *locale_list = (SplayTreeInfo *) NULL;
95
96 #if defined(MAGICKCORE_HAVE_STRTOD_L)
97 static volatile locale_t
98   c_locale = (locale_t) NULL;
99 #endif
100
101 static volatile MagickBooleanType
102   instantiate_locale = MagickFalse;
103 \f
104 /*
105   Forward declarations.
106 */
107 static MagickBooleanType
108   InitializeLocaleList(ExceptionInfo *),
109   LoadLocaleLists(const char *,const char *,ExceptionInfo *);
110 \f
111 #if defined(MAGICKCORE_HAVE_STRTOD_L)
112 /*
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %                                                                             %
115 %                                                                             %
116 %                                                                             %
117 +   A c q u i r e C L o c a l e                                               %
118 %                                                                             %
119 %                                                                             %
120 %                                                                             %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 %
123 %  AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
124 %  errno set if it cannot be acquired.
125 %
126 %  The format of the AcquireCLocale method is:
127 %
128 %      locale_t AcquireCLocale(void)
129 %
130 */
131 static locale_t AcquireCLocale(void)
132 {
133 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
134   if (c_locale == (locale_t) NULL)
135     c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
136 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
137   if (c_locale == (locale_t) NULL)
138     c_locale=_create_locale(LC_ALL,"C");
139 #endif
140   return(c_locale);
141 }
142 #endif
143 \f
144 #if defined(MAGICKCORE_HAVE_STRTOD_L)
145 /*
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 %                                                                             %
148 %                                                                             %
149 %                                                                             %
150 +   D e s t r o y C L o c a l e                                               %
151 %                                                                             %
152 %                                                                             %
153 %                                                                             %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 %
156 %  DestroyCLocale() releases the resources allocated for a locale object
157 %  returned by a call to the AcquireCLocale() method.
158 %
159 %  The format of the DestroyCLocale method is:
160 %
161 %      void DestroyCLocale(void)
162 %
163 */
164 static void DestroyCLocale(void)
165 {
166 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
167   if (c_locale != (locale_t) NULL)
168     freelocale(c_locale);
169 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
170   if (c_locale != (locale_t) NULL)
171     _free_locale(c_locale);
172 #endif
173   c_locale=(locale_t) NULL;
174 }
175 #endif
176 \f
177 /*
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 %                                                                             %
180 %                                                                             %
181 %                                                                             %
182 %   D e s t r o y L o c a l e O p t i o n s                                   %
183 %                                                                             %
184 %                                                                             %
185 %                                                                             %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 %
188 %  DestroyLocaleOptions() releases memory associated with an locale
189 %  messages.
190 %
191 %  The format of the DestroyProfiles method is:
192 %
193 %      LinkedListInfo *DestroyLocaleOptions(Image *image)
194 %
195 %  A description of each parameter follows:
196 %
197 %    o image: the image.
198 %
199 */
200
201 static void *DestroyOptions(void *message)
202 {
203   return(DestroyStringInfo((StringInfo *) message));
204 }
205
206 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
207 {
208   assert(messages != (LinkedListInfo *) NULL);
209   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
210   return(DestroyLinkedList(messages,DestroyOptions));
211 }
212 \f
213 /*
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %                                                                             %
216 %                                                                             %
217 %                                                                             %
218 +  F o r m a t L o c a l e F i l e                                            %
219 %                                                                             %
220 %                                                                             %
221 %                                                                             %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 %
224 %  FormatLocaleFile() prints formatted output of a variable argument list to a
225 %  file in the "C" locale.
226 %
227 %  The format of the FormatLocaleFile method is:
228 %
229 %      ssize_t FormatLocaleFile(FILE *file,const char *format,...)
230 %
231 %  A description of each parameter follows.
232 %
233 %   o file:  the file.
234 %
235 %   o format:  A file describing the format to use to write the remaining
236 %     arguments.
237 %
238 */
239
240 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
241   const char *restrict format,va_list operands)
242 {
243   ssize_t
244     n;
245
246 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
247   {
248     locale_t
249       locale;
250
251     locale=AcquireCLocale();
252     if (locale == (locale_t) NULL)
253       n=(ssize_t) vfprintf(file,format,operands);
254     else
255 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
256       n=(ssize_t) vfprintf_l(file,format,locale,operands);
257 #else
258       n=(ssize_t) vfprintf_l(file,locale,format,operands);
259 #endif
260   }
261 #else
262 #if defined(MAGICKCORE_HAVE_USELOCALE)
263   {
264     locale_t
265       locale,
266       previous_locale;
267
268     locale=AcquireCLocale();
269     if (locale == (locale_t) NULL)
270       n=(ssize_t) vfprintf(file,format,operands);
271     else
272       {
273         previous_locale=uselocale(locale);
274         n=(ssize_t) vfprintf(file,format,operands);
275         uselocale(previous_locale);
276       }
277   }
278 #else
279   n=(ssize_t) vfprintf(file,format,operands);
280 #endif
281 #endif
282   return(n);
283 }
284
285 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
286   ...)
287 {
288   ssize_t
289     n;
290
291   va_list
292     operands;
293
294   va_start(operands,format);
295   n=FormatLocaleFileList(file,format,operands);
296   va_end(operands);
297   return(n);
298 }
299 \f
300 /*
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 %                                                                             %
303 %                                                                             %
304 %                                                                             %
305 +  F o r m a t L o c a l e S t r i n g                                        %
306 %                                                                             %
307 %                                                                             %
308 %                                                                             %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 %
311 %  FormatLocaleString() prints formatted output of a variable argument list to
312 %  a string buffer in the "C" locale.
313 %
314 %  The format of the FormatLocaleString method is:
315 %
316 %      ssize_t FormatLocaleString(char *string,const size_t length,
317 %        const char *format,...)
318 %
319 %  A description of each parameter follows.
320 %
321 %   o string:  FormatLocaleString() returns the formatted string in this
322 %     character buffer.
323 %
324 %   o length: the maximum length of the string.
325 %
326 %   o format:  A string describing the format to use to write the remaining
327 %     arguments.
328 %
329 */
330
331 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
332   const size_t length,const char *restrict format,va_list operands)
333 {
334   ssize_t
335     n;
336
337 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
338   {
339     locale_t
340       locale;
341
342     locale=AcquireCLocale();
343     if (locale == (locale_t) NULL)
344       n=(ssize_t) vsnprintf(string,length,format,operands);
345     else
346 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
347       n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
348 #else
349       n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
350 #endif
351   }
352 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
353 #if defined(MAGICKCORE_HAVE_USELOCALE)
354   {
355     locale_t
356       locale,
357       previous_locale;
358
359     locale=AcquireCLocale();
360     if (locale == (locale_t) NULL)
361       n=(ssize_t) vsnprintf(string,length,format,operands);
362     else
363       {
364         previous_locale=uselocale(locale);
365         n=(ssize_t) vsnprintf(string,length,format,operands);
366         uselocale(previous_locale);
367       }
368   }
369 #else
370   n=(ssize_t) vsnprintf(string,length,format,operands);
371 #endif
372 #else
373   n=(ssize_t) vsprintf(string,format,operands);
374 #endif
375   if (n < 0)
376     string[length-1]='\0';
377   return(n);
378 }
379
380 MagickExport ssize_t FormatLocaleString(char *restrict string,
381   const size_t length,const char *restrict format,...)
382 {
383   ssize_t
384     n;
385
386   va_list
387     operands;
388
389   va_start(operands,format);
390   n=FormatLocaleStringList(string,length,format,operands);
391   va_end(operands);
392   return(n);
393 }
394 \f
395 /*
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %                                                                             %
398 %                                                                             %
399 %                                                                             %
400 +   G e t L o c a l e I n f o _                                               %
401 %                                                                             %
402 %                                                                             %
403 %                                                                             %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 %
406 %  GetLocaleInfo_() searches the locale list for the specified tag and if
407 %  found returns attributes for that element.
408 %
409 %  The format of the GetLocaleInfo method is:
410 %
411 %      const LocaleInfo *GetLocaleInfo_(const char *tag,
412 %        ExceptionInfo *exception)
413 %
414 %  A description of each parameter follows:
415 %
416 %    o tag: the locale tag.
417 %
418 %    o exception: return any errors or warnings in this structure.
419 %
420 */
421 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
422   ExceptionInfo *exception)
423 {
424   assert(exception != (ExceptionInfo *) NULL);
425   if ((locale_list == (SplayTreeInfo *) NULL) ||
426       (instantiate_locale == MagickFalse))
427     if (InitializeLocaleList(exception) == MagickFalse)
428       return((const LocaleInfo *) NULL);
429   if ((locale_list == (SplayTreeInfo *) NULL) ||
430       (GetNumberOfNodesInSplayTree(locale_list) == 0))
431     return((const LocaleInfo *) NULL);
432   if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
433     {
434       ResetSplayTreeIterator(locale_list);
435       return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
436     }
437   return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
438 }
439 \f
440 /*
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 %                                                                             %
443 %                                                                             %
444 %                                                                             %
445 %   G e t L o c a l e I n f o L i s t                                         %
446 %                                                                             %
447 %                                                                             %
448 %                                                                             %
449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450 %
451 %  GetLocaleInfoList() returns any locale messages that match the
452 %  specified pattern.
453 %
454 %  The format of the GetLocaleInfoList function is:
455 %
456 %      const LocaleInfo **GetLocaleInfoList(const char *pattern,
457 %        size_t *number_messages,ExceptionInfo *exception)
458 %
459 %  A description of each parameter follows:
460 %
461 %    o pattern: Specifies a pointer to a text string containing a pattern.
462 %
463 %    o number_messages:  This integer returns the number of locale messages in
464 %    the list.
465 %
466 %    o exception: return any errors or warnings in this structure.
467 %
468 */
469
470 #if defined(__cplusplus) || defined(c_plusplus)
471 extern "C" {
472 #endif
473
474 static int LocaleInfoCompare(const void *x,const void *y)
475 {
476   const LocaleInfo
477     **p,
478     **q;
479
480   p=(const LocaleInfo **) x,
481   q=(const LocaleInfo **) y;
482   if (LocaleCompare((*p)->path,(*q)->path) == 0)
483     return(LocaleCompare((*p)->tag,(*q)->tag));
484   return(LocaleCompare((*p)->path,(*q)->path));
485 }
486
487 #if defined(__cplusplus) || defined(c_plusplus)
488 }
489 #endif
490
491 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
492   size_t *number_messages,ExceptionInfo *exception)
493 {
494   const LocaleInfo
495     **messages;
496
497   register const LocaleInfo
498     *p;
499
500   register ssize_t
501     i;
502
503   /*
504     Allocate locale list.
505   */
506   assert(pattern != (char *) NULL);
507   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
508   assert(number_messages != (size_t *) NULL);
509   *number_messages=0;
510   p=GetLocaleInfo_("*",exception);
511   if (p == (const LocaleInfo *) NULL)
512     return((const LocaleInfo **) NULL);
513   messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
514     GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
515   if (messages == (const LocaleInfo **) NULL)
516     return((const LocaleInfo **) NULL);
517   /*
518     Generate locale list.
519   */
520   LockSemaphoreInfo(locale_semaphore);
521   ResetSplayTreeIterator(locale_list);
522   p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
523   for (i=0; p != (const LocaleInfo *) NULL; )
524   {
525     if ((p->stealth == MagickFalse) &&
526         (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
527       messages[i++]=p;
528     p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
529   }
530   UnlockSemaphoreInfo(locale_semaphore);
531   qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
532   messages[i]=(LocaleInfo *) NULL;
533   *number_messages=(size_t) i;
534   return(messages);
535 }
536 \f
537 /*
538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 %                                                                             %
540 %                                                                             %
541 %                                                                             %
542 %   G e t L o c a l e L i s t                                                 %
543 %                                                                             %
544 %                                                                             %
545 %                                                                             %
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 %
548 %  GetLocaleList() returns any locale messages that match the specified
549 %  pattern.
550 %
551 %  The format of the GetLocaleList function is:
552 %
553 %      char **GetLocaleList(const char *pattern,size_t *number_messages,
554 %        Exceptioninfo *exception)
555 %
556 %  A description of each parameter follows:
557 %
558 %    o pattern: Specifies a pointer to a text string containing a pattern.
559 %
560 %    o number_messages:  This integer returns the number of messages in the
561 %      list.
562 %
563 %    o exception: return any errors or warnings in this structure.
564 %
565 */
566
567 #if defined(__cplusplus) || defined(c_plusplus)
568 extern "C" {
569 #endif
570
571 static int LocaleTagCompare(const void *x,const void *y)
572 {
573   register char
574     **p,
575     **q;
576
577   p=(char **) x;
578   q=(char **) y;
579   return(LocaleCompare(*p,*q));
580 }
581
582 #if defined(__cplusplus) || defined(c_plusplus)
583 }
584 #endif
585
586 MagickExport char **GetLocaleList(const char *pattern,
587   size_t *number_messages,ExceptionInfo *exception)
588 {
589   char
590     **messages;
591
592   register const LocaleInfo
593     *p;
594
595   register ssize_t
596     i;
597
598   /*
599     Allocate locale list.
600   */
601   assert(pattern != (char *) NULL);
602   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
603   assert(number_messages != (size_t *) NULL);
604   *number_messages=0;
605   p=GetLocaleInfo_("*",exception);
606   if (p == (const LocaleInfo *) NULL)
607     return((char **) NULL);
608   messages=(char **) AcquireQuantumMemory((size_t)
609     GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
610   if (messages == (char **) NULL)
611     return((char **) NULL);
612   LockSemaphoreInfo(locale_semaphore);
613   p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
614   for (i=0; p != (const LocaleInfo *) NULL; )
615   {
616     if ((p->stealth == MagickFalse) &&
617         (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
618       messages[i++]=ConstantString(p->tag);
619     p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
620   }
621   UnlockSemaphoreInfo(locale_semaphore);
622   qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
623   messages[i]=(char *) NULL;
624   *number_messages=(size_t) i;
625   return(messages);
626 }
627 \f
628 /*
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 %                                                                             %
631 %                                                                             %
632 %                                                                             %
633 %   G e t L o c a l e M e s s a g e                                           %
634 %                                                                             %
635 %                                                                             %
636 %                                                                             %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 %
639 %  GetLocaleMessage() returns a message in the current locale that matches the
640 %  supplied tag.
641 %
642 %  The format of the GetLocaleMessage method is:
643 %
644 %      const char *GetLocaleMessage(const char *tag)
645 %
646 %  A description of each parameter follows:
647 %
648 %    o tag: Return a message that matches this tag in the current locale.
649 %
650 */
651 MagickExport const char *GetLocaleMessage(const char *tag)
652 {
653   char
654     name[MaxTextExtent];
655
656   const LocaleInfo
657     *locale_info;
658
659   ExceptionInfo
660     *exception;
661
662   if ((tag == (const char *) NULL) || (*tag == '\0'))
663     return(tag);
664   exception=AcquireExceptionInfo();
665   (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
666   locale_info=GetLocaleInfo_(name,exception);
667   exception=DestroyExceptionInfo(exception);
668   if (locale_info != (const LocaleInfo *) NULL)
669     return(locale_info->message);
670   return(tag);
671 }
672 \f
673 /*
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 %                                                                             %
676 %                                                                             %
677 %                                                                             %
678 %  G e t L o c a l e O p t i o n s                                            %
679 %                                                                             %
680 %                                                                             %
681 %                                                                             %
682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 %
684 %  GetLocaleOptions() returns any Magick configuration messages associated
685 %  with the specified filename.
686 %
687 %  The format of the GetLocaleOptions method is:
688 %
689 %      LinkedListInfo *GetLocaleOptions(const char *filename,
690 %        ExceptionInfo *exception)
691 %
692 %  A description of each parameter follows:
693 %
694 %    o filename: the locale file tag.
695 %
696 %    o exception: return any errors or warnings in this structure.
697 %
698 */
699 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
700   ExceptionInfo *exception)
701 {
702   char
703     path[MaxTextExtent];
704
705   const char
706     *element;
707
708   LinkedListInfo
709     *messages,
710     *paths;
711
712   StringInfo
713     *xml;
714
715   assert(filename != (const char *) NULL);
716   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
717   assert(exception != (ExceptionInfo *) NULL);
718   (void) CopyMagickString(path,filename,MaxTextExtent);
719   /*
720     Load XML from configuration files to linked-list.
721   */
722   messages=NewLinkedList(0);
723   paths=GetConfigurePaths(filename,exception);
724   if (paths != (LinkedListInfo *) NULL)
725     {
726       ResetLinkedListIterator(paths);
727       element=(const char *) GetNextValueInLinkedList(paths);
728       while (element != (const char *) NULL)
729       {
730         (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
731         (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
732           "Searching for locale file: \"%s\"",path);
733         xml=ConfigureFileToStringInfo(path);
734         if (xml != (StringInfo *) NULL)
735           (void) AppendValueToLinkedList(messages,xml);
736         element=(const char *) GetNextValueInLinkedList(paths);
737       }
738       paths=DestroyLinkedList(paths,RelinquishMagickMemory);
739     }
740 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
741   {
742     char
743       *blob;
744
745     blob=(char *) NTResourceToBlob(filename);
746     if (blob != (char *) NULL)
747       {
748         xml=StringToStringInfo(blob);
749         (void) AppendValueToLinkedList(messages,xml);
750         blob=DestroyString(blob);
751       }
752   }
753 #endif
754   ResetLinkedListIterator(messages);
755   return(messages);
756 }
757 \f
758 /*
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 %                                                                             %
761 %                                                                             %
762 %                                                                             %
763 %   G e t L o c a l e V a l u e                                               %
764 %                                                                             %
765 %                                                                             %
766 %                                                                             %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 %
769 %  GetLocaleValue() returns the message associated with the locale info.
770 %
771 %  The format of the GetLocaleValue method is:
772 %
773 %      const char *GetLocaleValue(const LocaleInfo *locale_info)
774 %
775 %  A description of each parameter follows:
776 %
777 %    o locale_info:  The locale info.
778 %
779 */
780 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
781 {
782   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
783   assert(locale_info != (LocaleInfo *) NULL);
784   assert(locale_info->signature == MagickSignature);
785   return(locale_info->message);
786 }
787 \f
788 /*
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 %                                                                             %
791 %                                                                             %
792 %                                                                             %
793 +   I n i t i a l i z e L o c a l e L i s t                                   %
794 %                                                                             %
795 %                                                                             %
796 %                                                                             %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 %
799 %  InitializeLocaleList() initializes the locale list.
800 %
801 %  The format of the InitializeLocaleList method is:
802 %
803 %      MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
804 %
805 %  A description of each parameter follows.
806 %
807 %    o exception: return any errors or warnings in this structure.
808 %
809 */
810 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
811 {
812   if ((locale_list == (SplayTreeInfo *) NULL) &&
813       (instantiate_locale == MagickFalse))
814     {
815       if (locale_semaphore == (SemaphoreInfo *) NULL)
816         AcquireSemaphoreInfo(&locale_semaphore);
817       LockSemaphoreInfo(locale_semaphore);
818       if ((locale_list == (SplayTreeInfo *) NULL) &&
819           (instantiate_locale == MagickFalse))
820         {
821           char
822             *locale;
823
824           register const char
825             *p;
826
827           locale=(char *) NULL;
828           p=setlocale(LC_CTYPE,(const char *) NULL);
829           if (p != (const char *) NULL)
830             locale=ConstantString(p);
831           if (locale == (char *) NULL)
832             locale=GetEnvironmentValue("LC_ALL");
833           if (locale == (char *) NULL)
834             locale=GetEnvironmentValue("LC_MESSAGES");
835           if (locale == (char *) NULL)
836             locale=GetEnvironmentValue("LC_CTYPE");
837           if (locale == (char *) NULL)
838             locale=GetEnvironmentValue("LANG");
839           if (locale == (char *) NULL)
840             locale=ConstantString("C");
841           (void) LoadLocaleLists(LocaleFilename,locale,exception);
842           locale=DestroyString(locale);
843           instantiate_locale=MagickTrue;
844         }
845       UnlockSemaphoreInfo(locale_semaphore);
846     }
847   return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
848 }
849 \f
850 /*
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 %                                                                             %
853 %                                                                             %
854 %                                                                             %
855 +   I n t e r p r e t L o c a l e V a l u e                                   %
856 %                                                                             %
857 %                                                                             %
858 %                                                                             %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 %
861 %  InterpretLocaleValue() interprets the string as a floating point number in
862 %  the "C" locale and returns its value as a double. If sentinal is not a null
863 %  pointer, the method also sets the value pointed by sentinal to point to the
864 %  first character after the number.
865 %
866 %  The format of the InterpretLocaleValue method is:
867 %
868 %      double InterpretLocaleValue(const char *value,char **sentinal)
869 %
870 %  A description of each parameter follows:
871 %
872 %    o value: the string value.
873 %
874 %    o sentinal:  if sentinal is not NULL, a pointer to the character after the
875 %      last character used in the conversion is stored in the location
876 %      referenced by sentinal.
877 %
878 */
879 MagickExport double InterpretLocaleValue(const char *restrict string,
880   char **restrict sentinal)
881 {
882   char
883     *q;
884
885   double
886     value;
887
888   static const double
889     SIPrefixes['z'-'E'+1] =
890     {
891       ['y'-'E'] = (-24.0),
892       ['z'-'E'] = (-21.0),
893       ['a'-'E'] = (-18.0),
894       ['f'-'E'] = (-15.0),
895       ['p'-'E'] = (-12.0),
896       ['n'-'E'] = (-9.0),
897       ['u'-'E'] = (-6.0),
898       ['m'-'E'] = (-3.0),
899       ['c'-'E'] = (-2.0),
900       ['d'-'E'] = (-1.0),
901       ['h'-'E'] = 2.0,
902       ['k'-'E'] = 3.0,
903       ['K'-'E'] = 3.0,
904       ['M'-'E'] = 6.0,
905       ['G'-'E'] = 9.0,
906       ['T'-'E'] = 12.0,
907       ['P'-'E'] = 15.0,
908       ['E'-'E'] = 18.0,
909       ['Z'-'E'] = 21.0,
910       ['Y'-'E'] = 24.0
911     };
912
913   if ((*string == '0') && ((string[1] | 0x20)=='x'))
914     value=(double) strtoul(string,&q,16);
915   else
916     {
917 #if defined(MAGICKCORE_HAVE_STRTOD_L)
918       locale_t
919         locale;
920
921       locale=AcquireCLocale();
922       if (locale == (locale_t) NULL)
923         value=strtod(string,&q);
924       else
925         value=strtod_l(string,&q,locale);
926 #else
927       value=strtod(string,&q);
928 #endif
929     }
930   if (q != string)
931     {
932       if ((*q >= 'E') && (*q <= 'z'))
933         {
934           double
935             e;
936
937           e=SIPrefixes[*q-'E'];
938           if (e >= MagickEpsilon)
939             {
940               if (q[1] == 'i')
941                 {
942                   value*=pow(2.0,e/0.3);
943                   q+=2;
944                 }
945               else
946                 {
947                   value*=pow(10.0,e);
948                   q++;
949                 }
950             }
951         }
952       if (*q == 'B')
953         {
954           value*=8.0;
955           q++;
956         }
957     }
958   if (sentinal != (char **) NULL)
959     *sentinal=q;
960   return(value);
961 }
962 \f
963 /*
964 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
965 %                                                                             %
966 %                                                                             %
967 %                                                                             %
968 %  L i s t L o c a l e I n f o                                                %
969 %                                                                             %
970 %                                                                             %
971 %                                                                             %
972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 %
974 %  ListLocaleInfo() lists the locale info to a file.
975 %
976 %  The format of the ListLocaleInfo method is:
977 %
978 %      MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
979 %
980 %  A description of each parameter follows.
981 %
982 %    o file:  An pointer to a FILE.
983 %
984 %    o exception: return any errors or warnings in this structure.
985 %
986 */
987 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
988   ExceptionInfo *exception)
989 {
990   const char
991     *path;
992
993   const LocaleInfo
994     **locale_info;
995
996   register ssize_t
997     i;
998
999   size_t
1000     number_messages;
1001
1002   if (file == (const FILE *) NULL)
1003     file=stdout;
1004   number_messages=0;
1005   locale_info=GetLocaleInfoList("*",&number_messages,exception);
1006   if (locale_info == (const LocaleInfo **) NULL)
1007     return(MagickFalse);
1008   path=(const char *) NULL;
1009   for (i=0; i < (ssize_t) number_messages; i++)
1010   {
1011     if (locale_info[i]->stealth != MagickFalse)
1012       continue;
1013     if ((path == (const char *) NULL) ||
1014         (LocaleCompare(path,locale_info[i]->path) != 0))
1015       {
1016         if (locale_info[i]->path != (char *) NULL)
1017           (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1018         (void) FormatLocaleFile(file,"Tag/Message\n");
1019         (void) FormatLocaleFile(file,
1020           "-------------------------------------------------"
1021           "------------------------------\n");
1022       }
1023     path=locale_info[i]->path;
1024     (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1025     if (locale_info[i]->message != (char *) NULL)
1026       (void) FormatLocaleFile(file,"  %s",locale_info[i]->message);
1027     (void) FormatLocaleFile(file,"\n");
1028   }
1029   (void) fflush(file);
1030   locale_info=(const LocaleInfo **)
1031     RelinquishMagickMemory((void *) locale_info);
1032   return(MagickTrue);
1033 }
1034 \f
1035 /*
1036 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1037 %                                                                             %
1038 %                                                                             %
1039 %                                                                             %
1040 +   L o a d L o c a l e L i s t                                               %
1041 %                                                                             %
1042 %                                                                             %
1043 %                                                                             %
1044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1045 %
1046 %  LoadLocaleList() loads the locale configuration file which provides a mapping
1047 %  between locale attributes and a locale name.
1048 %
1049 %  The format of the LoadLocaleList method is:
1050 %
1051 %      MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1052 %        const size_t depth,ExceptionInfo *exception)
1053 %
1054 %  A description of each parameter follows:
1055 %
1056 %    o xml:  The locale list in XML format.
1057 %
1058 %    o filename:  The locale list filename.
1059 %
1060 %    o depth: depth of <include /> statements.
1061 %
1062 %    o exception: return any errors or warnings in this structure.
1063 %
1064 */
1065
1066 static void ChopLocaleComponents(char *path,const size_t components)
1067 {
1068   register char
1069     *p;
1070
1071   ssize_t
1072     count;
1073
1074   if (*path == '\0')
1075     return;
1076   p=path+strlen(path)-1;
1077   if (*p == '/')
1078     *p='\0';
1079   for (count=0; (count < (ssize_t) components) && (p > path); p--)
1080     if (*p == '/')
1081       {
1082         *p='\0';
1083         count++;
1084       }
1085   if (count < (ssize_t) components)
1086     *path='\0';
1087 }
1088
1089 static void *DestroyLocaleNode(void *locale_info)
1090 {
1091   register LocaleInfo
1092     *p;
1093
1094   p=(LocaleInfo *) locale_info;
1095   if (p->path != (char *) NULL)
1096     p->path=DestroyString(p->path);
1097   if (p->tag != (char *) NULL)
1098     p->tag=DestroyString(p->tag);
1099   if (p->message != (char *) NULL)
1100     p->message=DestroyString(p->message);
1101   return(RelinquishMagickMemory(p));
1102 }
1103
1104 static void LocaleFatalErrorHandler(
1105   const ExceptionType magick_unused(severity),
1106   const char *reason,const char *description)
1107 {
1108   if (reason == (char *) NULL)
1109     return;
1110   (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1111   if (description != (char *) NULL)
1112     (void) FormatLocaleFile(stderr," (%s)",description);
1113   (void) FormatLocaleFile(stderr,".\n");
1114   (void) fflush(stderr);
1115   exit(1);
1116 }
1117
1118
1119 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1120   const char *locale,const size_t depth,ExceptionInfo *exception)
1121 {
1122   char
1123     keyword[MaxTextExtent],
1124     message[MaxTextExtent],
1125     tag[MaxTextExtent],
1126     *token;
1127
1128   const char
1129     *q;
1130
1131   FatalErrorHandler
1132     fatal_handler;
1133
1134   LocaleInfo
1135     *locale_info;
1136
1137   MagickBooleanType
1138     status;
1139
1140   register char
1141     *p;
1142
1143   /*
1144     Read the locale configure file.
1145   */
1146   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1147     "Loading locale configure file \"%s\" ...",filename);
1148   if (xml == (const char *) NULL)
1149     return(MagickFalse);
1150   if (locale_list == (SplayTreeInfo *) NULL)
1151     {
1152       locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1153         DestroyLocaleNode);
1154       if (locale_list == (SplayTreeInfo *) NULL)
1155         return(MagickFalse);
1156     }
1157   status=MagickTrue;
1158   locale_info=(LocaleInfo *) NULL;
1159   *tag='\0';
1160   *message='\0';
1161   *keyword='\0';
1162   fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1163   token=AcquireString(xml);
1164   for (q=(char *) xml; *q != '\0'; )
1165   {
1166     /*
1167       Interpret XML.
1168     */
1169     GetMagickToken(q,&q,token);
1170     if (*token == '\0')
1171       break;
1172     (void) CopyMagickString(keyword,token,MaxTextExtent);
1173     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1174       {
1175         /*
1176           Doctype element.
1177         */
1178         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1179         {
1180           GetMagickToken(q,&q,token);
1181           while (isspace((int) ((unsigned char) *q)) != 0)
1182             q++;
1183         }
1184         continue;
1185       }
1186     if (LocaleNCompare(keyword,"<!--",4) == 0)
1187       {
1188         /*
1189           Comment element.
1190         */
1191         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1192         {
1193           GetMagickToken(q,&q,token);
1194           while (isspace((int) ((unsigned char) *q)) != 0)
1195             q++;
1196         }
1197         continue;
1198       }
1199     if (LocaleCompare(keyword,"<include") == 0)
1200       {
1201         /*
1202           Include element.
1203         */
1204         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1205         {
1206           (void) CopyMagickString(keyword,token,MaxTextExtent);
1207           GetMagickToken(q,&q,token);
1208           if (*token != '=')
1209             continue;
1210           GetMagickToken(q,&q,token);
1211           if (LocaleCompare(keyword,"locale") == 0)
1212             {
1213               if (LocaleCompare(locale,token) != 0)
1214                 break;
1215               continue;
1216             }
1217           if (LocaleCompare(keyword,"file") == 0)
1218             {
1219               if (depth > 200)
1220                 (void) ThrowMagickException(exception,GetMagickModule(),
1221                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1222               else
1223                 {
1224                   char
1225                     path[MaxTextExtent],
1226                     *xml;
1227
1228                   *path='\0';
1229                   GetPathComponent(filename,HeadPath,path);
1230                   if (*path != '\0')
1231                     (void) ConcatenateMagickString(path,DirectorySeparator,
1232                       MaxTextExtent);
1233                   if (*token == *DirectorySeparator)
1234                     (void) CopyMagickString(path,token,MaxTextExtent);
1235                   else
1236                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
1237                   xml=FileToString(path,~0,exception);
1238                   if (xml != (char *) NULL)
1239                     {
1240                       status=LoadLocaleList(xml,path,locale,depth+1,exception);
1241                       xml=(char *) RelinquishMagickMemory(xml);
1242                     }
1243                 }
1244             }
1245         }
1246         continue;
1247       }
1248     if (LocaleCompare(keyword,"<locale") == 0)
1249       {
1250         /*
1251           Locale element.
1252         */
1253         while ((*token != '>') && (*q != '\0'))
1254         {
1255           (void) CopyMagickString(keyword,token,MaxTextExtent);
1256           GetMagickToken(q,&q,token);
1257           if (*token != '=')
1258             continue;
1259           GetMagickToken(q,&q,token);
1260         }
1261         continue;
1262       }
1263     if (LocaleCompare(keyword,"</locale>") == 0)
1264       {
1265         ChopLocaleComponents(tag,1);
1266         (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1267         continue;
1268       }
1269     if (LocaleCompare(keyword,"<localemap>") == 0)
1270       continue;
1271     if (LocaleCompare(keyword,"</localemap>") == 0)
1272       continue;
1273     if (LocaleCompare(keyword,"<message") == 0)
1274       {
1275         /*
1276           Message element.
1277         */
1278         while ((*token != '>') && (*q != '\0'))
1279         {
1280           (void) CopyMagickString(keyword,token,MaxTextExtent);
1281           GetMagickToken(q,&q,token);
1282           if (*token != '=')
1283             continue;
1284           GetMagickToken(q,&q,token);
1285           if (LocaleCompare(keyword,"name") == 0)
1286             {
1287               (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1288               (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1289             }
1290         }
1291         for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1292         while (isspace((int) ((unsigned char) *p)) != 0)
1293           p++;
1294         q--;
1295         while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1296           q--;
1297         (void) CopyMagickString(message,p,(size_t) (q-p+2));
1298         locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1299         if (locale_info == (LocaleInfo *) NULL)
1300           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1301         (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1302         locale_info->path=ConstantString(filename);
1303         locale_info->tag=ConstantString(tag);
1304         locale_info->message=ConstantString(message);
1305         locale_info->signature=MagickSignature;
1306         status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1307         if (status == MagickFalse)
1308           (void) ThrowMagickException(exception,GetMagickModule(),
1309             ResourceLimitError,"MemoryAllocationFailed","`%s'",
1310             locale_info->tag);
1311         (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1312         (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1313         q++;
1314         continue;
1315       }
1316     if (LocaleCompare(keyword,"</message>") == 0)
1317       {
1318         ChopLocaleComponents(tag,2);
1319         (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1320         continue;
1321       }
1322     if (*keyword == '<')
1323       {
1324         /*
1325           Subpath element.
1326         */
1327         if (*(keyword+1) == '?')
1328           continue;
1329         if (*(keyword+1) == '/')
1330           {
1331             ChopLocaleComponents(tag,1);
1332             if (*tag != '\0')
1333               (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1334             continue;
1335           }
1336         token[strlen(token)-1]='\0';
1337         (void) CopyMagickString(token,token+1,MaxTextExtent);
1338         (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1339         (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1340         continue;
1341       }
1342     GetMagickToken(q,(const char **) NULL,token);
1343     if (*token != '=')
1344       continue;
1345   }
1346   token=(char *) RelinquishMagickMemory(token);
1347   (void) SetFatalErrorHandler(fatal_handler);
1348   return(status);
1349 }
1350 \f
1351 /*
1352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1353 %                                                                             %
1354 %                                                                             %
1355 %                                                                             %
1356 %  L o a d L o c a l e L i s t s                                              %
1357 %                                                                             %
1358 %                                                                             %
1359 %                                                                             %
1360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1361 %
1362 %  LoadLocaleList() loads one or more locale configuration file which
1363 %  provides a mapping between locale attributes and a locale tag.
1364 %
1365 %  The format of the LoadLocaleLists method is:
1366 %
1367 %      MagickBooleanType LoadLocaleLists(const char *filename,
1368 %        ExceptionInfo *exception)
1369 %
1370 %  A description of each parameter follows:
1371 %
1372 %    o filename: the font file tag.
1373 %
1374 %    o locale: the actual locale.
1375 %
1376 %    o exception: return any errors or warnings in this structure.
1377 %
1378 */
1379 static MagickBooleanType LoadLocaleLists(const char *filename,
1380   const char *locale,ExceptionInfo *exception)
1381 {
1382 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1383   return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1384 #else
1385   const StringInfo
1386     *option;
1387
1388   LinkedListInfo
1389     *options;
1390
1391   MagickStatusType
1392     status;
1393
1394   status=MagickFalse;
1395   options=GetLocaleOptions(filename,exception);
1396   option=(const StringInfo *) GetNextValueInLinkedList(options);
1397   while (option != (const StringInfo *) NULL)
1398   {
1399     status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1400       GetStringInfoPath(option),locale,0,exception);
1401     option=(const StringInfo *) GetNextValueInLinkedList(options);
1402   }
1403   options=DestroyLocaleOptions(options);
1404   if ((locale_list == (SplayTreeInfo *) NULL) ||
1405       (GetNumberOfNodesInSplayTree(locale_list) == 0))
1406     {
1407       options=GetLocaleOptions("english.xml",exception);
1408       option=(const StringInfo *) GetNextValueInLinkedList(options);
1409       while (option != (const StringInfo *) NULL)
1410       {
1411         status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1412           GetStringInfoPath(option),locale,0,exception);
1413         option=(const StringInfo *) GetNextValueInLinkedList(options);
1414       }
1415       options=DestroyLocaleOptions(options);
1416     }
1417   if ((locale_list == (SplayTreeInfo *) NULL) ||
1418       (GetNumberOfNodesInSplayTree(locale_list) == 0))
1419     status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1420   return(status != 0 ? MagickTrue : MagickFalse);
1421 #endif
1422 }
1423 \f
1424 /*
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 %                                                                             %
1427 %                                                                             %
1428 %                                                                             %
1429 +   L o c a l e C o m p o n e n t G e n e s i s                               %
1430 %                                                                             %
1431 %                                                                             %
1432 %                                                                             %
1433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 %
1435 %  LocaleComponentGenesis() instantiates the locale component.
1436 %
1437 %  The format of the LocaleComponentGenesis method is:
1438 %
1439 %      MagickBooleanType LocaleComponentGenesis(void)
1440 %
1441 */
1442 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1443 {
1444   AcquireSemaphoreInfo(&locale_semaphore);
1445   return(MagickTrue);
1446 }
1447 \f
1448 /*
1449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450 %                                                                             %
1451 %                                                                             %
1452 %                                                                             %
1453 +   L o c a l e C o m p o n e n t T e r m i n u s                             %
1454 %                                                                             %
1455 %                                                                             %
1456 %                                                                             %
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 %
1459 %  LocaleComponentTerminus() destroys the locale component.
1460 %
1461 %  The format of the LocaleComponentTerminus method is:
1462 %
1463 %      LocaleComponentTerminus(void)
1464 %
1465 */
1466 MagickPrivate void LocaleComponentTerminus(void)
1467 {
1468   if (locale_semaphore == (SemaphoreInfo *) NULL)
1469     AcquireSemaphoreInfo(&locale_semaphore);
1470   LockSemaphoreInfo(locale_semaphore);
1471 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1472   DestroyCLocale();
1473 #endif
1474   instantiate_locale=MagickFalse;
1475   UnlockSemaphoreInfo(locale_semaphore);
1476   DestroySemaphoreInfo(&locale_semaphore);
1477 }