2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 % DDDD EEEEE L EEEEE GGGG AAA TTTTT EEEEE %
6 % D D E L E G A A T E %
7 % D D EEE L EEE G GG AAAAA T EEE %
8 % D D E L E G G A A T E %
9 % DDDD EEEEE LLLLL EEEEE GGG A A T EEEEE %
12 % MagickCore Methods to Read/Write/Invoke Delegates %
19 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
25 % http://www.imagemagick.org/script/license.php %
27 % Unless required by applicable law or agreed to in writing, software %
28 % distributed under the License is distributed on an "AS IS" BASIS, %
29 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30 % See the License for the specific language governing permissions and %
31 % limitations under the License. %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 % The Delegates methods associate a set of commands with a particular
36 % image format. ImageMagick uses delegates for formats it does not handle
39 % Thanks to Bob Friesenhahn for the initial inspiration and design of the
48 #include "MagickCore/studio.h"
49 #include "MagickCore/property.h"
50 #include "MagickCore/blob.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/configure.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/delegate.h"
55 #include "MagickCore/delegate-private.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/hashmap.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/nt-base-private.h"
62 #include "MagickCore/policy.h"
63 #include "MagickCore/resource_.h"
64 #include "MagickCore/semaphore.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/token.h"
67 #include "MagickCore/utility.h"
68 #include "MagickCore/utility-private.h"
69 #include "MagickCore/xml-tree.h"
70 #include "MagickCore/xml-tree-private.h"
75 #define DelegateFilename "delegates.xml"
81 *DelegateMap = (const char *)
82 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
84 " <delegate decode=\"autotrace\" stealth=\"True\" command=\""autotrace" -output-format svg -output-file "%o" "%i"\"/>"
85 " <delegate decode=\"avi:decode\" stealth=\"True\" command=\""mplayer" "%i" -really-quiet -ao null -vo png:z=3\"/>"
86 " <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\""xdg-open" http://www.imagemagick.org/; rm "%i"\"/>"
87 " <delegate decode=\"cgm\" thread-support=\"False\" command=\""ralcgm" -d ps -oC < "%i" > "%o" 2> "%u"\"/>"
88 " <delegate decode=\"dng:decode\" command=\""/usr/bin/ufraw-batch" --silent --wb=camera --black-point=auto --exposure=auto --create-id=also --out-type=ppm16 "--output=%u.pnm" "%i"\"/>"
89 " <delegate decode=\"edit\" stealth=\"True\" command=\""xterm" -title "Edit Image Comment" -e vi "%o"\"/>"
90 " <delegate decode=\"eps\" encode=\"pdf\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i"\"/>"
91 " <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pswrite" "-sOutputFile=%o" "-f%i"\"/>"
92 " <delegate decode=\"fig\" command=\""fig2dev" -L ps "%i" "%o"\"/>"
93 " <delegate decode=\"gplt\" command=\""echo" "set size 1.25,0.62 set terminal postscript portrait color solid; set output "%o"; load "%i"" > "%u";"gnuplot" "%u"\"/>"
94 " <delegate decode=\"hpg\" command=\""hp2xx" -q -m eps -f `basename "%o"` "%i" mv -f `basename "%o"` "%o"\"/>"
95 " <delegate decode=\"hpgl\" command=\""hp2xx" -q -m eps -f `basename "%o"` "%i" mv -f `basename "%o"` "%o"\"/>"
96 " <delegate decode=\"htm\" command=\""html2ps" -U -o "%o" "%i"\"/>"
97 " <delegate decode=\"html\" command=\""html2ps" -U -o "%o" "%i"\"/>"
98 " <delegate decode=\"https\" command=\""wget" -q -O "%o" "https:%M"\"/>"
99 " <delegate decode=\"ilbm\" command=\""ilbmtoppm" "%i" > "%o"\"/>"
100 " <delegate decode=\"man\" command=\""groff" -man -Tps "%i" > "%o"\"/>"
101 " <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\""ffmpeg" -v -1 -vframes %S -i "%i" -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z"\"/>"
102 " <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\""ffmpeg" -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i "%M%%d.jpg" "%u.%m" 2> "%Z"\"/>"
103 " <delegate decode=\"pcl:color\" stealth=\"True\" command=\""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ppmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s"\"/>"
104 " <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=bmpsep8" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s"\"/>"
105 " <delegate decode=\"pcl:mono\" stealth=\"True\" command=\""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s"\"/>"
106 " <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i"\"/>"
107 " <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pswrite" "-sOutputFile=%o" "-f%i"\"/>"
108 " <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\""ppmtoilbm" -24if "%i" > "%o"\"/>"
109 " <delegate decode=\"pnm\" encode=\"launch\" mode=\"encode\" command=\""gimp" "%i"\"/>"
110 " <delegate decode=\"pov\" command=\""povray" "+i"%i"" -D0 +o"%o" +fn%q +w%w +h%h +a -q9 -kfi"%s" -kff"%n" "convert" -concatenate "%o*.png" "%o"\"/>"
111 " <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i"\"/>"
112 " <delegate decode=\"ps\" encode=\"pdf\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i"\"/>"
113 " <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr "%i"\"/>"
114 " <delegate decode=\"ps:alpha\" stealth=\"True\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"\"/>"
115 " <delegate decode=\"ps:bbox\" stealth=\"True\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=bbox" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"\"/>"
116 " <delegate decode=\"ps:cmyk\" stealth=\"True\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=bmpsep8" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"\"/>"
117 " <delegate decode=\"ps:color\" stealth=\"True\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"\"/>"
118 " <delegate decode=\"ps:mono\" stealth=\"True\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s"\"/>"
119 " <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\""rawtorle" -o "%o" -v "%i"\"/>"
120 " <delegate decode=\"scan\" command=\""scanimage" -d "%i" > "%o"\"/>"
121 " <delegate encode=\"show\" spawn=\"True\" command=\""display" -immutable -delay 0 -window-group %g -title "%l of %f" "temporary:%i"\"/>"
122 " <delegate decode=\"shtml\" command=\""html2ps" -U -o "%o" "%i"\"/>"
123 " <delegate decode=\"svg\" command=\""rsvg" "%i" "%o"\"/>"
124 " <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\""enscript" -o "%o" "%i"\"/>"
125 " <delegate encode=\"win\" stealth=\"True\" spawn=\"True\" command=\""display" -immutable -delay 0 -window-group %g -title "%l of %f" "temporary:%i"\"/>"
126 " <delegate decode=\"wmf\" command=\""wmf2eps" -o "%o" "%i"\"/>"
130 Global declaractions.
132 static LinkedListInfo
133 *delegate_cache = (LinkedListInfo *) NULL;
136 *delegate_semaphore = (SemaphoreInfo *) NULL;
139 Forward declaractions.
141 static MagickBooleanType
142 IsDelegateCacheInstantiated(ExceptionInfo *),
143 LoadDelegateCache(LinkedListInfo *,const char *,const char *,const size_t,
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % A c q u i r e D e l e g a t e C a c h e %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 % AcquireDelegateCache() caches one or more delegate configurations which
158 % provides a mapping between delegate attributes and a delegate name.
160 % The format of the AcquireDelegateCache method is:
162 % LinkedListInfo *AcquireDelegateCache(const char *filename,
163 % ExceptionInfo *exception)
165 % A description of each parameter follows:
167 % o filename: the font file name.
169 % o exception: return any errors or warnings in this structure.
172 static LinkedListInfo *AcquireDelegateCache(const char *filename,
173 ExceptionInfo *exception)
181 delegate_cache=NewLinkedList(0);
182 if (delegate_cache == (LinkedListInfo *) NULL)
183 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
185 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
193 options=GetConfigureOptions(filename,exception);
194 option=(const StringInfo *) GetNextValueInLinkedList(options);
195 while (option != (const StringInfo *) NULL)
197 status&=LoadDelegateCache(delegate_cache,(const char *)
198 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
199 option=(const StringInfo *) GetNextValueInLinkedList(options);
201 options=DestroyConfigureOptions(options);
204 if (IfMagickTrue(IsLinkedListEmpty(delegate_cache)))
205 status&=LoadDelegateCache(delegate_cache,DelegateMap,"built-in",0,
207 return(delegate_cache);
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 + D e l e g a t e C o m p o n e n t G e n e s i s %
219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 % DelegateComponentGenesis() instantiates the delegate component.
223 % The format of the DelegateComponentGenesis method is:
225 % MagickBooleanType DelegateComponentGenesis(void)
228 MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
230 if (delegate_semaphore == (SemaphoreInfo *) NULL)
231 delegate_semaphore=AcquireSemaphoreInfo();
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240 % D e l e g a t e C o m p o n e n t T e r m i n u s %
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % DelegateComponentTerminus() destroys the delegate component.
248 % The format of the DelegateComponentTerminus method is:
250 % DelegateComponentTerminus(void)
254 static void *DestroyDelegate(void *delegate_info)
256 register DelegateInfo
259 p=(DelegateInfo *) delegate_info;
260 if (p->path != (char *) NULL)
261 p->path=DestroyString(p->path);
262 if (p->decode != (char *) NULL)
263 p->decode=DestroyString(p->decode);
264 if (p->encode != (char *) NULL)
265 p->encode=DestroyString(p->encode);
266 if (p->commands != (char *) NULL)
267 p->commands=DestroyString(p->commands);
268 if (p->semaphore != (SemaphoreInfo *) NULL)
269 RelinquishSemaphoreInfo(&p->semaphore);
270 p=(DelegateInfo *) RelinquishMagickMemory(p);
271 return((void *) NULL);
274 MagickPrivate void DelegateComponentTerminus(void)
276 if (delegate_semaphore == (SemaphoreInfo *) NULL)
277 ActivateSemaphoreInfo(&delegate_semaphore);
278 LockSemaphoreInfo(delegate_semaphore);
279 if (delegate_cache != (LinkedListInfo *) NULL)
280 delegate_cache=DestroyLinkedList(delegate_cache,DestroyDelegate);
281 UnlockSemaphoreInfo(delegate_semaphore);
282 RelinquishSemaphoreInfo(&delegate_semaphore);
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % G e t D e l e g a t e C o m m a n d %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % GetDelegateCommand() replaces any embedded formatting characters with the
297 % appropriate image attribute and returns the resulting command.
299 % The format of the GetDelegateCommand method is:
301 % char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
302 % const char *decode,const char *encode,ExceptionInfo *exception)
304 % A description of each parameter follows:
306 % o command: Method GetDelegateCommand returns the command associated
307 % with specified delegate tag.
309 % o image_info: the image info.
311 % o image: the image.
313 % o decode: Specifies the decode delegate we are searching for as a
316 % o encode: Specifies the encode delegate we are searching for as a
319 % o exception: return any errors or warnings in this structure.
322 MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
323 const char *decode,const char *encode,ExceptionInfo *exception)
335 assert(image_info != (ImageInfo *) NULL);
336 assert(image_info->signature == MagickSignature);
337 assert(image != (Image *) NULL);
338 assert(image->signature == MagickSignature);
339 if( IfMagickTrue(image->debug) )
340 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
342 delegate_info=GetDelegateInfo(decode,encode,exception);
343 if (delegate_info == (const DelegateInfo *) NULL)
345 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
346 "NoTagFound","`%s'",decode ? decode : encode);
347 return((char *) NULL);
349 commands=StringToList(delegate_info->commands);
350 if (commands == (char **) NULL)
352 (void) ThrowMagickException(exception,GetMagickModule(),
353 ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
355 return((char *) NULL);
357 command=InterpretImageProperties((ImageInfo *) image_info,image,commands[0],
359 if (command == (char *) NULL)
360 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
361 "MemoryAllocationFailed","`%s'",commands[0]);
363 Relinquish resources.
365 for (i=0; commands[i] != (char *) NULL; i++)
366 commands[i]=DestroyString(commands[i]);
367 commands=(char **) RelinquishMagickMemory(commands);
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % G e t D e l e g a t e C o m m a n d s %
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 % GetDelegateCommands() returns the commands associated with a delegate.
384 % The format of the GetDelegateCommands method is:
386 % const char *GetDelegateCommands(const DelegateInfo *delegate_info)
388 % A description of each parameter follows:
390 % o delegate_info: The delegate info.
393 MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
395 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
397 assert(delegate_info != (DelegateInfo *) NULL);
398 assert(delegate_info->signature == MagickSignature);
399 return(delegate_info->commands);
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 % G e t D e l e g a t e I n f o %
411 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413 % GetDelegateInfo() returns any delegates associated with the specified tag.
415 % The format of the GetDelegateInfo method is:
417 % const DelegateInfo *GetDelegateInfo(const char *decode,
418 % const char *encode,ExceptionInfo *exception)
420 % A description of each parameter follows:
422 % o decode: Specifies the decode delegate we are searching for as a
425 % o encode: Specifies the encode delegate we are searching for as a
428 % o exception: return any errors or warnings in this structure.
431 MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
432 const char *encode,ExceptionInfo *exception)
434 register const DelegateInfo
437 assert(exception != (ExceptionInfo *) NULL);
438 if (IfMagickFalse(IsDelegateCacheInstantiated(exception)))
439 return((const DelegateInfo *) NULL);
441 Search for named delegate.
443 LockSemaphoreInfo(delegate_semaphore);
444 ResetLinkedListIterator(delegate_cache);
445 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
446 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
448 UnlockSemaphoreInfo(delegate_semaphore);
451 while (p != (const DelegateInfo *) NULL)
455 if (LocaleCompare(p->decode,decode) == 0)
457 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
462 if (LocaleCompare(p->encode,encode) == 0)
464 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
467 if (LocaleCompare(decode,p->decode) == 0)
468 if (LocaleCompare(encode,p->encode) == 0)
470 if (LocaleCompare(decode,"*") == 0)
471 if (LocaleCompare(encode,p->encode) == 0)
473 if (LocaleCompare(decode,p->decode) == 0)
474 if (LocaleCompare(encode,"*") == 0)
476 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
478 if (p != (const DelegateInfo *) NULL)
479 (void) InsertValueInLinkedList(delegate_cache,0,
480 RemoveElementByValueFromLinkedList(delegate_cache,p));
481 UnlockSemaphoreInfo(delegate_semaphore);
486 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 % G e t D e l e g a t e I n f o L i s t %
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 % GetDelegateInfoList() returns any delegates that match the specified pattern.
498 % The delegate of the GetDelegateInfoList function is:
500 % const DelegateInfo **GetDelegateInfoList(const char *pattern,
501 % size_t *number_delegates,ExceptionInfo *exception)
503 % A description of each parameter follows:
505 % o pattern: Specifies a pointer to a text string containing a pattern.
507 % o number_delegates: This integer returns the number of delegates in the
510 % o exception: return any errors or warnings in this structure.
514 #if defined(__cplusplus) || defined(c_plusplus)
518 static int DelegateInfoCompare(const void *x,const void *y)
524 p=(const DelegateInfo **) x,
525 q=(const DelegateInfo **) y;
526 if (LocaleCompare((*p)->path,(*q)->path) == 0)
528 if ((*p)->decode == (char *) NULL)
529 if (((*p)->encode != (char *) NULL) &&
530 ((*q)->encode != (char *) NULL))
531 return(strcmp((*p)->encode,(*q)->encode));
532 if (((*p)->decode != (char *) NULL) &&
533 ((*q)->decode != (char *) NULL))
534 return(strcmp((*p)->decode,(*q)->decode));
536 return(LocaleCompare((*p)->path,(*q)->path));
539 #if defined(__cplusplus) || defined(c_plusplus)
543 MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
544 size_t *number_delegates,ExceptionInfo *exception)
549 register const DelegateInfo
556 Allocate delegate list.
558 assert(number_delegates != (size_t *) NULL);
559 assert(pattern != (char *) NULL);
560 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
563 p=GetDelegateInfo("*","*",exception);
564 if (p == (const DelegateInfo *) NULL)
565 return((const DelegateInfo **) NULL);
566 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
567 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
568 if (delegates == (const DelegateInfo **) NULL)
569 return((const DelegateInfo **) NULL);
571 Generate delegate list.
573 LockSemaphoreInfo(delegate_semaphore);
574 ResetLinkedListIterator(delegate_cache);
575 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
576 for (i=0; p != (const DelegateInfo *) NULL; )
578 if( IfMagickFalse(p->stealth) &&
579 ( IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) ||
580 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse))) )
582 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
584 UnlockSemaphoreInfo(delegate_semaphore);
585 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
586 delegates[i]=(DelegateInfo *) NULL;
587 *number_delegates=(size_t) i;
592 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
596 % G e t D e l e g a t e L i s t %
600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602 % GetDelegateList() returns any image format delegates that match the
605 % The format of the GetDelegateList function is:
607 % char **GetDelegateList(const char *pattern,
608 % size_t *number_delegates,ExceptionInfo *exception)
610 % A description of each parameter follows:
612 % o pattern: Specifies a pointer to a text string containing a pattern.
614 % o number_delegates: This integer returns the number of delegates
617 % o exception: return any errors or warnings in this structure.
621 #if defined(__cplusplus) || defined(c_plusplus)
625 static int DelegateCompare(const void *x,const void *y)
633 return(LocaleCompare(*p,*q));
636 #if defined(__cplusplus) || defined(c_plusplus)
640 MagickExport char **GetDelegateList(const char *pattern,
641 size_t *number_delegates,ExceptionInfo *exception)
646 register const DelegateInfo
653 Allocate delegate list.
655 assert(pattern != (char *) NULL);
656 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
658 assert(number_delegates != (size_t *) NULL);
660 p=GetDelegateInfo("*","*",exception);
661 if (p == (const DelegateInfo *) NULL)
662 return((char **) NULL);
663 delegates=(char **) AcquireQuantumMemory((size_t)
664 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
665 if (delegates == (char **) NULL)
666 return((char **) NULL);
667 LockSemaphoreInfo(delegate_semaphore);
668 ResetLinkedListIterator(delegate_cache);
669 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
670 for (i=0; p != (const DelegateInfo *) NULL; )
672 if( IfMagickFalse(p->stealth) &&
673 IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) )
674 delegates[i++]=ConstantString(p->decode);
675 if( IfMagickFalse(p->stealth) &&
676 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse)) )
677 delegates[i++]=ConstantString(p->encode);
678 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
680 UnlockSemaphoreInfo(delegate_semaphore);
681 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
682 delegates[i]=(char *) NULL;
683 *number_delegates=(size_t) i;
688 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
692 % G e t D e l e g a t e M o d e %
696 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 % GetDelegateMode() returns the mode of the delegate.
700 % The format of the GetDelegateMode method is:
702 % ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
704 % A description of each parameter follows:
706 % o delegate_info: The delegate info.
709 MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
713 assert(delegate_info != (DelegateInfo *) NULL);
714 assert(delegate_info->signature == MagickSignature);
715 return(delegate_info->mode);
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723 + G e t D e l e g a t e T h r e a d S u p p o r t %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % GetDelegateThreadSupport() returns MagickTrue if the delegate supports
732 % The format of the GetDelegateThreadSupport method is:
734 % MagickBooleanType GetDelegateThreadSupport(
735 % const DelegateInfo *delegate_info)
737 % A description of each parameter follows:
739 % o delegate_info: The delegate info.
742 MagickExport MagickBooleanType GetDelegateThreadSupport(
743 const DelegateInfo *delegate_info)
745 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
747 assert(delegate_info != (DelegateInfo *) NULL);
748 assert(delegate_info->signature == MagickSignature);
749 return(delegate_info->thread_support);
753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
757 + I s D e l e g a t e C a c h e I n s t a n t i a t e d %
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % IsDelegateCacheInstantiated() determines if the delegate cache is
764 % instantiated. If not, it instantiates the cache and returns it.
766 % The format of the IsDelegateInstantiated method is:
768 % MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
770 % A description of each parameter follows.
772 % o exception: return any errors or warnings in this structure.
775 static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
777 if (delegate_cache == (LinkedListInfo *) NULL)
779 if (delegate_semaphore == (SemaphoreInfo *) NULL)
780 ActivateSemaphoreInfo(&delegate_semaphore);
781 LockSemaphoreInfo(delegate_semaphore);
782 if (delegate_cache == (LinkedListInfo *) NULL)
783 delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
784 UnlockSemaphoreInfo(delegate_semaphore);
786 return(IsMagickNotNULL(delegate_cache));
790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % I n v o k e D e l e g a t e %
798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800 % InvokeDelegate replaces any embedded formatting characters with the
801 % appropriate image attribute and executes the resulting command. MagickFalse
802 % is returned if the commands execute with success otherwise MagickTrue.
804 % The format of the InvokeDelegate method is:
806 % MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
807 % const char *decode,const char *encode,ExceptionInfo *exception)
809 % A description of each parameter follows:
811 % o image_info: the imageInfo.
813 % o image: the image.
815 % o exception: return any errors or warnings in this structure.
819 static inline size_t MagickMin(const size_t x,const size_t y)
826 static MagickBooleanType CopyDelegateFile(const char *source,
827 const char *destination)
853 Copy source file to destination.
855 assert(source != (const char *) NULL);
856 assert(destination != (char *) NULL);
857 status=GetPathAttributes(destination,&attributes);
858 if( IfMagickTrue(status) && (attributes.st_size != 0))
860 destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
861 if (destination_file == -1)
863 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
864 if (source_file == -1)
866 (void) close(destination_file);
869 quantum=(size_t) MagickMaxBufferExtent;
870 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
871 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
872 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
873 if (buffer == (unsigned char *) NULL)
875 (void) close(source_file);
876 (void) close(destination_file);
880 for (i=0; ; i+=count)
882 count=(ssize_t) read(source_file,buffer,quantum);
885 length=(size_t) count;
886 count=(ssize_t) write(destination_file,buffer,length);
887 if ((size_t) count != length)
890 (void) close(destination_file);
891 (void) close(source_file);
892 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
893 return(IsMagickTrue(i!=0));
896 MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
897 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
902 input_filename[MaxTextExtent],
903 output_filename[MaxTextExtent];
921 assert(image_info != (ImageInfo *) NULL);
922 assert(image_info->signature == MagickSignature);
923 assert(image != (Image *) NULL);
924 assert(image->signature == MagickSignature);
925 if( IfMagickTrue(image->debug) )
926 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
928 rights=ExecutePolicyRights;
929 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,decode)) )
932 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
933 "NotAuthorized","`%s'",decode);
936 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,encode)) )
939 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
940 "NotAuthorized","`%s'",encode);
943 temporary=IsMagickTrue(*image->filename == '\0');
944 if( IfMagickTrue(temporary) )
945 if( IfMagickFalse(AcquireUniqueFilename(image->filename)) )
947 ThrowFileException(exception,FileOpenError,
948 "UnableToCreateTemporaryFile",image->filename);
951 delegate_info=GetDelegateInfo(decode,encode,exception);
952 if (delegate_info == (DelegateInfo *) NULL)
954 if( IfMagickTrue(temporary) )
955 (void) RelinquishUniqueFileResource(image->filename);
956 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
957 "NoTagFound","`%s'",decode ? decode : encode);
960 if (*image_info->filename == '\0')
962 if( IfMagickFalse(AcquireUniqueFilename(image_info->filename)) )
964 if( IfMagickTrue(temporary) )
965 (void) RelinquishUniqueFileResource(image->filename);
966 ThrowFileException(exception,FileOpenError,
967 "UnableToCreateTemporaryFile",image_info->filename);
970 image_info->temporary=MagickTrue;
972 if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
973 (delegate_info->encode != (char *) NULL)) ||
974 ((encode != (const char *) NULL) &&
975 (delegate_info->decode != (char *) NULL))))
987 Delegate requires a particular image format.
989 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
991 ThrowFileException(exception,FileOpenError,
992 "UnableToCreateTemporaryFile",image_info->unique);
995 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
997 (void) RelinquishUniqueFileResource(image_info->unique);
998 ThrowFileException(exception,FileOpenError,
999 "UnableToCreateTemporaryFile",image_info->zero);
1000 return(MagickFalse);
1002 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
1003 delegate_info->encode : delegate_info->decode,exception);
1004 if (magick == (char *) NULL)
1006 (void) RelinquishUniqueFileResource(image_info->unique);
1007 (void) RelinquishUniqueFileResource(image_info->zero);
1008 if( IfMagickTrue(temporary) )
1009 (void) RelinquishUniqueFileResource(image->filename);
1010 (void) ThrowMagickException(exception,GetMagickModule(),
1011 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1012 return(MagickFalse);
1014 LocaleUpper(magick);
1015 clone_info=CloneImageInfo(image_info);
1016 (void) CopyMagickString((char *) clone_info->magick,magick,
1018 if (LocaleCompare(magick,"NULL") != 0)
1019 (void) CopyMagickString(image->magick,magick,MaxTextExtent);
1020 magick=DestroyString(magick);
1021 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:",
1022 delegate_info->decode);
1023 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1025 (void) CopyMagickString(clone_info->filename,image_info->filename,
1027 (void) CopyMagickString(image_info->filename,image->filename,
1029 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1031 (void) FormatLocaleString(p->filename,MaxTextExtent,"%s:%s",
1032 delegate_info->decode,clone_info->filename);
1033 status=WriteImage(clone_info,p,exception);
1034 if( IfMagickFalse(status) )
1036 (void) RelinquishUniqueFileResource(image_info->unique);
1037 (void) RelinquishUniqueFileResource(image_info->zero);
1038 if( IfMagickTrue(temporary) )
1039 (void) RelinquishUniqueFileResource(image->filename);
1040 clone_info=DestroyImageInfo(clone_info);
1041 (void) ThrowMagickException(exception,GetMagickModule(),
1042 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1043 return(MagickFalse);
1045 if( IfMagickTrue(clone_info->adjoin) )
1048 (void) RelinquishUniqueFileResource(image_info->unique);
1049 (void) RelinquishUniqueFileResource(image_info->zero);
1050 clone_info=DestroyImageInfo(clone_info);
1055 commands=StringToList(delegate_info->commands);
1056 if (commands == (char **) NULL)
1058 if( IfMagickTrue(temporary) )
1059 (void) RelinquishUniqueFileResource(image->filename);
1060 (void) ThrowMagickException(exception,GetMagickModule(),
1061 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1062 decode ? decode : encode);
1063 return(MagickFalse);
1065 command=(char *) NULL;
1067 (void) CopyMagickString(output_filename,image_info->filename,MaxTextExtent);
1068 (void) CopyMagickString(input_filename,image->filename,MaxTextExtent);
1069 for (i=0; commands[i] != (char *) NULL; i++)
1071 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1072 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
1074 ThrowFileException(exception,FileOpenError,
1075 "UnableToCreateTemporaryFile",image_info->unique);
1078 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
1080 (void) RelinquishUniqueFileResource(image_info->unique);
1081 ThrowFileException(exception,FileOpenError,
1082 "UnableToCreateTemporaryFile",image_info->zero);
1085 if (LocaleCompare(decode,"SCAN") != 0)
1087 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1088 if( IfMagickFalse(status) )
1090 ThrowFileException(exception,FileOpenError,
1091 "UnableToCreateTemporaryFile",input_filename);
1096 command=InterpretImageProperties(image_info,image,commands[i],exception);
1097 if (command != (char *) NULL)
1102 status=IsMagickTrue(SystemCommand(delegate_info->spawn,
1103 image_info->verbose,command,exception) != 0);
1104 if (IfMagickTrue(delegate_info->spawn))
1110 Wait for input file to 'disappear', or maximum 10 seconds.
1113 while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
1114 (void) MagickDelay(100); /* sleep 0.1 seconds */
1116 command=DestroyString(command);
1118 if (LocaleCompare(decode,"SCAN") != 0)
1120 if( IfMagickFalse(CopyDelegateFile(image->filename,input_filename)) )
1121 (void) RelinquishUniqueFileResource(input_filename);
1123 if( IfMagickFalse(CopyDelegateFile(image_info->filename,output_filename)) )
1124 (void) RelinquishUniqueFileResource(output_filename);
1125 if( IfMagickTrue(image_info->temporary) )
1126 (void) RelinquishUniqueFileResource(image_info->filename);
1127 (void) RelinquishUniqueFileResource(image_info->unique);
1128 (void) RelinquishUniqueFileResource(image_info->zero);
1129 (void) RelinquishUniqueFileResource(image_info->filename);
1130 (void) RelinquishUniqueFileResource(image->filename);
1131 if( IfMagickTrue(status) )
1133 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1134 "DelegateFailed","`%s'",commands[i]);
1137 commands[i]=DestroyString(commands[i]);
1139 (void) CopyMagickString(image_info->filename,output_filename,MaxTextExtent);
1140 (void) CopyMagickString(image->filename,input_filename,MaxTextExtent);
1142 Relinquish resources.
1144 for ( ; commands[i] != (char *) NULL; i++)
1145 commands[i]=DestroyString(commands[i]);
1146 commands=(char **) RelinquishMagickMemory(commands);
1147 if( IfMagickTrue(temporary) )
1148 (void) RelinquishUniqueFileResource(image->filename);
1149 return(IsMagickFalse(status));
1153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1157 % L i s t D e l e g a t e I n f o %
1161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1163 % ListDelegateInfo() lists the image formats to a file.
1165 % The format of the ListDelegateInfo method is:
1167 % MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1169 % A description of each parameter follows.
1171 % o file: An pointer to a FILE.
1173 % o exception: return any errors or warnings in this structure.
1176 MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1177 ExceptionInfo *exception)
1184 delegate[MaxTextExtent];
1198 if (file == (const FILE *) NULL)
1200 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1201 if (delegate_info == (const DelegateInfo **) NULL)
1202 return(MagickFalse);
1203 path=(const char *) NULL;
1204 for (i=0; i < (ssize_t) number_delegates; i++)
1206 if( IfMagickTrue(delegate_info[i]->stealth) )
1208 if ((path == (const char *) NULL) ||
1209 (LocaleCompare(path,delegate_info[i]->path) != 0))
1211 if (delegate_info[i]->path != (char *) NULL)
1212 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1213 (void) FormatLocaleFile(file,"Delegate Command\n");
1214 (void) FormatLocaleFile(file,
1215 "-------------------------------------------------"
1216 "------------------------------\n");
1218 path=delegate_info[i]->path;
1220 if (delegate_info[i]->encode != (char *) NULL)
1221 (void) CopyMagickString(delegate,delegate_info[i]->encode,MaxTextExtent);
1222 (void) ConcatenateMagickString(delegate," ",MaxTextExtent);
1224 commands=StringToList(delegate_info[i]->commands);
1225 if (commands == (char **) NULL)
1227 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
1228 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1229 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1230 StripString(commands[0]);
1231 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
1232 for (j=1; commands[j] != (char *) NULL; j++)
1234 StripString(commands[j]);
1235 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
1237 for (j=0; commands[j] != (char *) NULL; j++)
1238 commands[j]=DestroyString(commands[j]);
1239 commands=(char **) RelinquishMagickMemory(commands);
1241 (void) fflush(file);
1242 delegate_info=(const DelegateInfo **)
1243 RelinquishMagickMemory((void *) delegate_info);
1248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1252 + L o a d D e l e g a t e L i s t %
1256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1258 % LoadDelegateCache() loads the delegate configurations which provides a
1259 % mapping between delegate attributes and a delegate name.
1261 % The format of the LoadDelegateCache method is:
1263 % MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1264 % const char *xml,const char *filename,const size_t depth,
1265 % ExceptionInfo *exception)
1267 % A description of each parameter follows:
1269 % o xml: The delegate list in XML format.
1271 % o filename: The delegate list filename.
1273 % o depth: depth of <include /> statements.
1275 % o exception: return any errors or warnings in this structure.
1278 static MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1279 const char *xml,const char *filename,const size_t depth,
1280 ExceptionInfo *exception)
1283 keyword[MaxTextExtent],
1296 Load the delegate map file.
1298 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1299 "Loading delegate configuration file \"%s\" ...",filename);
1300 if (xml == (const char *) NULL)
1301 return(MagickFalse);
1303 delegate_info=(DelegateInfo *) NULL;
1304 token=AcquireString(xml);
1305 for (q=(const char *) xml; *q != '\0'; )
1310 GetMagickToken(q,&q,token);
1313 (void) CopyMagickString(keyword,token,MaxTextExtent);
1314 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1319 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1320 GetMagickToken(q,&q,token);
1323 if (LocaleNCompare(keyword,"<!--",4) == 0)
1328 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1329 GetMagickToken(q,&q,token);
1332 if (LocaleCompare(keyword,"<include") == 0)
1337 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1339 (void) CopyMagickString(keyword,token,MaxTextExtent);
1340 GetMagickToken(q,&q,token);
1343 GetMagickToken(q,&q,token);
1344 if (LocaleCompare(keyword,"file") == 0)
1347 (void) ThrowMagickException(exception,GetMagickModule(),
1348 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1352 path[MaxTextExtent],
1355 GetPathComponent(filename,HeadPath,path);
1357 (void) ConcatenateMagickString(path,DirectorySeparator,
1359 if (*token == *DirectorySeparator)
1360 (void) CopyMagickString(path,token,MaxTextExtent);
1362 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1363 xml=FileToXML(path,~0UL);
1364 if (xml != (char *) NULL)
1366 status&=LoadDelegateCache(delegate_cache,xml,path,
1368 xml=(char *) RelinquishMagickMemory(xml);
1375 if (LocaleCompare(keyword,"<delegate") == 0)
1380 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1381 sizeof(*delegate_info));
1382 if (delegate_info == (DelegateInfo *) NULL)
1383 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1384 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1385 delegate_info->path=ConstantString(filename);
1386 delegate_info->thread_support=MagickTrue;
1387 delegate_info->signature=MagickSignature;
1390 if (delegate_info == (DelegateInfo *) NULL)
1392 if (LocaleCompare(keyword,"/>") == 0)
1394 status=AppendValueToLinkedList(delegate_cache,delegate_info);
1395 if( IfMagickFalse(status) )
1396 (void) ThrowMagickException(exception,GetMagickModule(),
1397 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1398 delegate_info->commands);
1399 delegate_info=(DelegateInfo *) NULL;
1402 GetMagickToken(q,(const char **) NULL,token);
1405 GetMagickToken(q,&q,token);
1406 GetMagickToken(q,&q,token);
1412 if (LocaleCompare((char *) keyword,"command") == 0)
1417 commands=AcquireString(token);
1418 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1419 if (strchr(commands,'@') != (char *) NULL)
1422 path[MaxTextExtent];
1424 NTGhostscriptEXE(path,MaxTextExtent);
1425 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1427 (void) SubstituteString((char **) &commands,"\\","/");
1430 (void) SubstituteString((char **) &commands,"&","&");
1431 (void) SubstituteString((char **) &commands,""","\"");
1432 (void) SubstituteString((char **) &commands,">",">");
1433 (void) SubstituteString((char **) &commands,"<","<");
1434 delegate_info->commands=commands;
1442 if (LocaleCompare((char *) keyword,"decode") == 0)
1444 delegate_info->decode=ConstantString(token);
1445 delegate_info->mode=1;
1453 if (LocaleCompare((char *) keyword,"encode") == 0)
1455 delegate_info->encode=ConstantString(token);
1456 delegate_info->mode=(-1);
1464 if (LocaleCompare((char *) keyword,"mode") == 0)
1466 delegate_info->mode=1;
1467 if (LocaleCompare(token,"bi") == 0)
1468 delegate_info->mode=0;
1470 if (LocaleCompare(token,"encode") == 0)
1471 delegate_info->mode=(-1);
1479 if (LocaleCompare((char *) keyword,"spawn") == 0)
1481 delegate_info->spawn=IsStringTrue(token);
1484 if (LocaleCompare((char *) keyword,"stealth") == 0)
1486 delegate_info->stealth=IsStringTrue(token);
1494 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1496 delegate_info->thread_support=IsStringTrue(token);
1497 if (delegate_info->thread_support == MagickFalse)
1498 delegate_info->semaphore=AcquireSemaphoreInfo();
1507 token=(char *) RelinquishMagickMemory(token);