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-2015 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/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/memory_.h"
62 #include "MagickCore/nt-base-private.h"
63 #include "MagickCore/policy.h"
64 #include "MagickCore/resource_.h"
65 #include "MagickCore/semaphore.h"
66 #include "MagickCore/string_.h"
67 #include "MagickCore/token.h"
68 #include "MagickCore/utility.h"
69 #include "MagickCore/utility-private.h"
70 #include "MagickCore/xml-tree.h"
71 #include "MagickCore/xml-tree-private.h"
76 #define DelegateFilename "delegates.xml"
82 *DelegateMap = (const char *)
83 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
85 " <delegate decode=\"autotrace\" stealth=\"True\" command=\""autotrace" -output-format svg -output-file "%o" "%i"\"/>"
86 " <delegate decode=\"avi:decode\" stealth=\"True\" command=\""mplayer" "%i" -really-quiet -ao null -vo png:z=3\"/>"
87 " <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\""xdg-open" http://www.imagemagick.org/; rm "%i"\"/>"
88 " <delegate decode=\"cgm\" thread-support=\"False\" command=\""ralcgm" -d ps -oC < "%i" > "%o" 2> "%u"\"/>"
89 " <delegate decode=\"dng:decode\" command=\""ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i"\"/>"
90 " <delegate decode=\"edit\" stealth=\"True\" command=\""xterm" -title "Edit Image Comment" -e vi "%o"\"/>"
91 " <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"\"/>"
92 " <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i"\"/>"
93 " <delegate decode=\"fig\" command=\""fig2dev" -L ps "%i" "%o"\"/>"
94 " <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"\"/>"
95 " <delegate decode=\"hpg\" command=\""hp2xx" -q -m eps -f `basename "%o"` "%i" mv -f `basename "%o"` "%o"\"/>"
96 " <delegate decode=\"hpgl\" command=\""hp2xx" -q -m eps -f `basename "%o"` "%i" mv -f `basename "%o"` "%o"\"/>"
97 " <delegate decode=\"htm\" command=\""html2ps" -U -o "%o" "%i"\"/>"
98 " <delegate decode=\"html\" command=\""html2ps" -U -o "%o" "%i"\"/>"
99 " <delegate decode=\"https\" command=\""wget" -q -O "%o" "https:%M"\"/>"
100 " <delegate decode=\"ilbm\" command=\""ilbmtoppm" "%i" > "%o"\"/>"
101 " <delegate decode=\"man\" command=\""groff" -man -Tps "%i" > "%o"\"/>"
102 " <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\""ffmpeg" -v -1 -vframes %S -i "%i" -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z"\"/>"
103 " <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"\"/>"
104 " <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"\"/>"
105 " <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"\"/>"
106 " <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"\"/>"
107 " <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=eps2write" "-sOutputFile=%o" "-f%i"\"/>"
108 " <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i"\"/>"
109 " <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\""ppmtoilbm" -24if "%i" > "%o"\"/>"
110 " <delegate decode=\"pnm\" encode=\"launch\" mode=\"encode\" command=\""gimp" "%i"\"/>"
111 " <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"\"/>"
112 " <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=eps2write" "-sOutputFile=%o" "-f%i"\"/>"
113 " <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"\"/>"
114 " <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr "%i"\"/>"
115 " <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"\"/>"
116 " <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"\"/>"
117 " <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"\"/>"
118 " <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"\"/>"
119 " <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"\"/>"
120 " <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\""rawtorle" -o "%o" -v "%i"\"/>"
121 " <delegate decode=\"scan\" command=\""scanimage" -d "%i" > "%o"\"/>"
122 " <delegate encode=\"show\" spawn=\"True\" command=\""display" -immutable -delay 0 -window-group %g -title "%l of %f" "temporary:%i"\"/>"
123 " <delegate decode=\"shtml\" command=\""html2ps" -U -o "%o" "%i"\"/>"
124 " <delegate decode=\"svg\" command=\""rsvg" "%i" "%o"\"/>"
125 " <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\""enscript" -o "%o" "%i"\"/>"
126 " <delegate encode=\"win\" stealth=\"True\" spawn=\"True\" command=\""display" -immutable -delay 0 -window-group %g -title "%l of %f" "temporary:%i"\"/>"
127 " <delegate decode=\"wmf\" command=\""wmf2eps" -o "%o" "%i"\"/>"
131 Global declaractions.
133 static LinkedListInfo
134 *delegate_cache = (LinkedListInfo *) NULL;
137 *delegate_semaphore = (SemaphoreInfo *) NULL;
140 Forward declaractions.
142 static MagickBooleanType
143 IsDelegateCacheInstantiated(ExceptionInfo *),
144 LoadDelegateCache(LinkedListInfo *,const char *,const char *,const size_t,
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % A c q u i r e D e l e g a t e C a c h e %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 % AcquireDelegateCache() caches one or more delegate configurations which
159 % provides a mapping between delegate attributes and a delegate name.
161 % The format of the AcquireDelegateCache method is:
163 % LinkedListInfo *AcquireDelegateCache(const char *filename,
164 % ExceptionInfo *exception)
166 % A description of each parameter follows:
168 % o filename: the font file name.
170 % o exception: return any errors or warnings in this structure.
173 static LinkedListInfo *AcquireDelegateCache(const char *filename,
174 ExceptionInfo *exception)
182 delegate_cache=NewLinkedList(0);
183 if (delegate_cache == (LinkedListInfo *) NULL)
184 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
186 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
194 options=GetConfigureOptions(filename,exception);
195 option=(const StringInfo *) GetNextValueInLinkedList(options);
196 while (option != (const StringInfo *) NULL)
198 status&=LoadDelegateCache(delegate_cache,(const char *)
199 GetStringInfoDatum(option),GetStringInfoPath(option),0,exception);
200 option=(const StringInfo *) GetNextValueInLinkedList(options);
202 options=DestroyConfigureOptions(options);
205 if (IfMagickTrue(IsLinkedListEmpty(delegate_cache)))
206 status&=LoadDelegateCache(delegate_cache,DelegateMap,"built-in",0,
208 return(delegate_cache);
212 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 + D e l e g a t e C o m p o n e n t G e n e s i s %
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
222 % DelegateComponentGenesis() instantiates the delegate component.
224 % The format of the DelegateComponentGenesis method is:
226 % MagickBooleanType DelegateComponentGenesis(void)
229 MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
231 if (delegate_semaphore == (SemaphoreInfo *) NULL)
232 delegate_semaphore=AcquireSemaphoreInfo();
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % 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 %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % DelegateComponentTerminus() destroys the delegate component.
249 % The format of the DelegateComponentTerminus method is:
251 % DelegateComponentTerminus(void)
255 static void *DestroyDelegate(void *delegate_info)
257 register DelegateInfo
260 p=(DelegateInfo *) delegate_info;
261 if (p->path != (char *) NULL)
262 p->path=DestroyString(p->path);
263 if (p->decode != (char *) NULL)
264 p->decode=DestroyString(p->decode);
265 if (p->encode != (char *) NULL)
266 p->encode=DestroyString(p->encode);
267 if (p->commands != (char *) NULL)
268 p->commands=DestroyString(p->commands);
269 if (p->semaphore != (SemaphoreInfo *) NULL)
270 RelinquishSemaphoreInfo(&p->semaphore);
271 p=(DelegateInfo *) RelinquishMagickMemory(p);
272 return((void *) NULL);
275 MagickPrivate void DelegateComponentTerminus(void)
277 if (delegate_semaphore == (SemaphoreInfo *) NULL)
278 ActivateSemaphoreInfo(&delegate_semaphore);
279 LockSemaphoreInfo(delegate_semaphore);
280 if (delegate_cache != (LinkedListInfo *) NULL)
281 delegate_cache=DestroyLinkedList(delegate_cache,DestroyDelegate);
282 UnlockSemaphoreInfo(delegate_semaphore);
283 RelinquishSemaphoreInfo(&delegate_semaphore);
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 + E x t e r n a l D e l e g a t e C o m m a n d %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % ExternalDelegateCommand() executes the specified command and waits until it
298 % terminates. The returned value is the exit status of the command.
300 % The format of the ExternalDelegateCommand method is:
302 % int ExternalDelegateCommand(const MagickBooleanType asynchronous,
303 % const MagickBooleanType verbose,const char *command,
304 % char *message,ExceptionInfo *exception)
306 % A description of each parameter follows:
308 % o asynchronous: a value other than 0 executes the parent program
309 % concurrently with the new child process.
311 % o verbose: a value other than 0 prints the executed command before it is
314 % o command: this string is the command to execute.
316 % o message: an option buffer to receive any message posted to stdout or
319 % o exception: return any errors here.
323 static char *SanitizeDelegateCommand(const char *command)
336 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_- "
337 ".@&;<>()|/\\\'\":%=~";
339 sanitize_command=AcquireString(command);
341 q=sanitize_command+strlen(sanitize_command);
342 for (p+=strspn(p,whitelist); p != q; p+=strspn(p,whitelist))
344 return(sanitize_command);
347 MagickExport int ExternalDelegateCommand(const MagickBooleanType asynchronous,
348 const MagickBooleanType verbose,const char *command,char *message,
349 ExceptionInfo *exception)
369 arguments=StringToArgv(command,&number_arguments);
370 if (arguments == (char **) NULL)
372 if (*arguments[1] == '\0')
374 for (i=0; i < (ssize_t) number_arguments; i++)
375 arguments[i]=DestroyString(arguments[i]);
376 arguments=(char **) RelinquishMagickMemory(arguments);
379 rights=ExecutePolicyRights;
380 domain=DelegatePolicyDomain;
381 if (IsRightsAuthorized(domain,rights,arguments[1]) == MagickFalse)
384 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
385 "NotAuthorized","`%s'",arguments[1]);
386 for (i=0; i < (ssize_t) number_arguments; i++)
387 arguments[i]=DestroyString(arguments[i]);
388 arguments=(char **) RelinquishMagickMemory(arguments);
391 if (verbose != MagickFalse)
393 (void) FormatLocaleFile(stderr,"%s\n",command);
394 (void) fflush(stderr);
396 sanitize_command=SanitizeDelegateCommand(command);
397 if (asynchronous != MagickFalse)
398 (void) ConcatenateMagickString(sanitize_command,"&",MagickPathExtent);
399 if (message != (char *) NULL)
401 #if defined(MAGICKCORE_POSIX_SUPPORT)
402 #if !defined(MAGICKCORE_HAVE_EXECVP)
403 status=system(sanitize_command);
405 if ((asynchronous != MagickFalse) ||
406 (strpbrk(sanitize_command,"&;<>|") != (char *) NULL))
407 status=system(sanitize_command);
414 Call application directly rather than from a shell.
416 child_pid=(pid_t) fork();
417 if (child_pid == (pid_t) -1)
418 status=system(sanitize_command);
422 status=execvp(arguments[1],arguments+1);
434 pid=(pid_t) waitpid(child_pid,&child_status,0);
439 if (WIFEXITED(child_status) != 0)
440 status=WEXITSTATUS(child_status);
442 if (WIFSIGNALED(child_status))
448 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
454 If a command shell is executed we need to change the forward slashes in
455 files to a backslash. We need to do this to keep Windows happy when we
456 want to 'move' a file.
458 TODO: This won't work if one of the delegate parameters has a forward
461 p=strstr(sanitize_command, "cmd.exe /c");
462 if (p != (char*) NULL)
465 for (; *p != '\0'; p++)
467 *p=*DirectorySeparator;
470 status=NTSystemCommand(sanitize_command,message);
471 #elif defined(macintosh)
472 status=MACSystemCommand(sanitize_command);
474 status=system(sanitize_command);
476 # error No suitable system() method.
480 if ((message != (char *) NULL) && (*message != '\0'))
481 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
482 "FailedToExecuteCommand","`%s' (%s)",sanitize_command,message);
484 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
485 "FailedToExecuteCommand","`%s' (%d)",sanitize_command,status);
487 sanitize_command=DestroyString(sanitize_command);
488 for (i=0; i < (ssize_t) number_arguments; i++)
489 arguments[i]=DestroyString(arguments[i]);
490 arguments=(char **) RelinquishMagickMemory(arguments);
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 % G e t D e l e g a t e C o m m a n d %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % GetDelegateCommand() replaces any embedded formatting characters with the
506 % appropriate image attribute and returns the resulting command.
508 % The format of the GetDelegateCommand method is:
510 % char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
511 % const char *decode,const char *encode,ExceptionInfo *exception)
513 % A description of each parameter follows:
515 % o command: Method GetDelegateCommand returns the command associated
516 % with specified delegate tag.
518 % o image_info: the image info.
520 % o image: the image.
522 % o decode: Specifies the decode delegate we are searching for as a
525 % o encode: Specifies the encode delegate we are searching for as a
528 % o exception: return any errors or warnings in this structure.
531 MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
532 const char *decode,const char *encode,ExceptionInfo *exception)
544 assert(image_info != (ImageInfo *) NULL);
545 assert(image_info->signature == MagickSignature);
546 assert(image != (Image *) NULL);
547 assert(image->signature == MagickSignature);
548 if (image->debug != MagickFalse)
549 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
551 delegate_info=GetDelegateInfo(decode,encode,exception);
552 if (delegate_info == (const DelegateInfo *) NULL)
554 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
555 "NoTagFound","`%s'",decode ? decode : encode);
556 return((char *) NULL);
558 commands=StringToList(delegate_info->commands);
559 if (commands == (char **) NULL)
561 (void) ThrowMagickException(exception,GetMagickModule(),
562 ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
564 return((char *) NULL);
566 command=InterpretImageProperties((ImageInfo *) image_info,image,commands[0],
568 if (command == (char *) NULL)
569 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
570 "MemoryAllocationFailed","`%s'",commands[0]);
572 Relinquish resources.
574 for (i=0; commands[i] != (char *) NULL; i++)
575 commands[i]=DestroyString(commands[i]);
576 commands=(char **) RelinquishMagickMemory(commands);
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 % G e t D e l e g a t e C o m m a n d s %
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 % GetDelegateCommands() returns the commands associated with a delegate.
593 % The format of the GetDelegateCommands method is:
595 % const char *GetDelegateCommands(const DelegateInfo *delegate_info)
597 % A description of each parameter follows:
599 % o delegate_info: The delegate info.
602 MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
606 assert(delegate_info != (DelegateInfo *) NULL);
607 assert(delegate_info->signature == MagickSignature);
608 return(delegate_info->commands);
612 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
616 % G e t D e l e g a t e I n f o %
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
622 % GetDelegateInfo() returns any delegates associated with the specified tag.
624 % The format of the GetDelegateInfo method is:
626 % const DelegateInfo *GetDelegateInfo(const char *decode,
627 % const char *encode,ExceptionInfo *exception)
629 % A description of each parameter follows:
631 % o decode: Specifies the decode delegate we are searching for as a
634 % o encode: Specifies the encode delegate we are searching for as a
637 % o exception: return any errors or warnings in this structure.
640 MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
641 const char *encode,ExceptionInfo *exception)
643 register const DelegateInfo
646 assert(exception != (ExceptionInfo *) NULL);
647 if (IfMagickFalse(IsDelegateCacheInstantiated(exception)))
648 return((const DelegateInfo *) NULL);
650 Search for named delegate.
652 LockSemaphoreInfo(delegate_semaphore);
653 ResetLinkedListIterator(delegate_cache);
654 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
655 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
657 UnlockSemaphoreInfo(delegate_semaphore);
660 while (p != (const DelegateInfo *) NULL)
664 if (LocaleCompare(p->decode,decode) == 0)
666 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
671 if (LocaleCompare(p->encode,encode) == 0)
673 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
676 if (LocaleCompare(decode,p->decode) == 0)
677 if (LocaleCompare(encode,p->encode) == 0)
679 if (LocaleCompare(decode,"*") == 0)
680 if (LocaleCompare(encode,p->encode) == 0)
682 if (LocaleCompare(decode,p->decode) == 0)
683 if (LocaleCompare(encode,"*") == 0)
685 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
687 if (p != (const DelegateInfo *) NULL)
688 (void) InsertValueInLinkedList(delegate_cache,0,
689 RemoveElementByValueFromLinkedList(delegate_cache,p));
690 UnlockSemaphoreInfo(delegate_semaphore);
695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699 % G e t D e l e g a t e I n f o L i s t %
703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
705 % GetDelegateInfoList() returns any delegates that match the specified pattern.
707 % The delegate of the GetDelegateInfoList function is:
709 % const DelegateInfo **GetDelegateInfoList(const char *pattern,
710 % size_t *number_delegates,ExceptionInfo *exception)
712 % A description of each parameter follows:
714 % o pattern: Specifies a pointer to a text string containing a pattern.
716 % o number_delegates: This integer returns the number of delegates in the
719 % o exception: return any errors or warnings in this structure.
723 #if defined(__cplusplus) || defined(c_plusplus)
727 static int DelegateInfoCompare(const void *x,const void *y)
736 p=(const DelegateInfo **) x,
737 q=(const DelegateInfo **) y;
738 cmp=LocaleCompare((*p)->path,(*q)->path);
741 if ((*p)->decode == (char *) NULL)
742 if (((*p)->encode != (char *) NULL) &&
743 ((*q)->encode != (char *) NULL))
744 return(strcmp((*p)->encode,(*q)->encode));
745 if (((*p)->decode != (char *) NULL) &&
746 ((*q)->decode != (char *) NULL))
747 return(strcmp((*p)->decode,(*q)->decode));
752 #if defined(__cplusplus) || defined(c_plusplus)
756 MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
757 size_t *number_delegates,ExceptionInfo *exception)
762 register const DelegateInfo
769 Allocate delegate list.
771 assert(number_delegates != (size_t *) NULL);
772 assert(pattern != (char *) NULL);
773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
776 p=GetDelegateInfo("*","*",exception);
777 if (p == (const DelegateInfo *) NULL)
778 return((const DelegateInfo **) NULL);
779 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
780 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
781 if (delegates == (const DelegateInfo **) NULL)
782 return((const DelegateInfo **) NULL);
784 Generate delegate list.
786 LockSemaphoreInfo(delegate_semaphore);
787 ResetLinkedListIterator(delegate_cache);
788 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
789 for (i=0; p != (const DelegateInfo *) NULL; )
791 if( IfMagickFalse(p->stealth) &&
792 ( IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) ||
793 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse))) )
795 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
797 UnlockSemaphoreInfo(delegate_semaphore);
798 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
799 delegates[i]=(DelegateInfo *) NULL;
800 *number_delegates=(size_t) i;
805 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809 % G e t D e l e g a t e L i s t %
813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
815 % GetDelegateList() returns any image format delegates that match the
818 % The format of the GetDelegateList function is:
820 % char **GetDelegateList(const char *pattern,
821 % size_t *number_delegates,ExceptionInfo *exception)
823 % A description of each parameter follows:
825 % o pattern: Specifies a pointer to a text string containing a pattern.
827 % o number_delegates: This integer returns the number of delegates
830 % o exception: return any errors or warnings in this structure.
834 #if defined(__cplusplus) || defined(c_plusplus)
838 static int DelegateCompare(const void *x,const void *y)
846 return(LocaleCompare(*p,*q));
849 #if defined(__cplusplus) || defined(c_plusplus)
853 MagickExport char **GetDelegateList(const char *pattern,
854 size_t *number_delegates,ExceptionInfo *exception)
859 register const DelegateInfo
866 Allocate delegate list.
868 assert(pattern != (char *) NULL);
869 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
871 assert(number_delegates != (size_t *) NULL);
873 p=GetDelegateInfo("*","*",exception);
874 if (p == (const DelegateInfo *) NULL)
875 return((char **) NULL);
876 delegates=(char **) AcquireQuantumMemory((size_t)
877 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
878 if (delegates == (char **) NULL)
879 return((char **) NULL);
880 LockSemaphoreInfo(delegate_semaphore);
881 ResetLinkedListIterator(delegate_cache);
882 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
883 for (i=0; p != (const DelegateInfo *) NULL; )
885 if( IfMagickFalse(p->stealth) &&
886 IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) )
887 delegates[i++]=ConstantString(p->decode);
888 if( IfMagickFalse(p->stealth) &&
889 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse)) )
890 delegates[i++]=ConstantString(p->encode);
891 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
893 UnlockSemaphoreInfo(delegate_semaphore);
894 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
895 delegates[i]=(char *) NULL;
896 *number_delegates=(size_t) i;
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
905 % G e t D e l e g a t e M o d e %
909 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 % GetDelegateMode() returns the mode of the delegate.
913 % The format of the GetDelegateMode method is:
915 % ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
917 % A description of each parameter follows:
919 % o delegate_info: The delegate info.
922 MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
926 assert(delegate_info != (DelegateInfo *) NULL);
927 assert(delegate_info->signature == MagickSignature);
928 return(delegate_info->mode);
932 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
936 + G e t D e l e g a t e T h r e a d S u p p o r t %
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
942 % GetDelegateThreadSupport() returns MagickTrue if the delegate supports
945 % The format of the GetDelegateThreadSupport method is:
947 % MagickBooleanType GetDelegateThreadSupport(
948 % const DelegateInfo *delegate_info)
950 % A description of each parameter follows:
952 % o delegate_info: The delegate info.
955 MagickExport MagickBooleanType GetDelegateThreadSupport(
956 const DelegateInfo *delegate_info)
958 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
960 assert(delegate_info != (DelegateInfo *) NULL);
961 assert(delegate_info->signature == MagickSignature);
962 return(delegate_info->thread_support);
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 + 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 %
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % IsDelegateCacheInstantiated() determines if the delegate cache is
977 % instantiated. If not, it instantiates the cache and returns it.
979 % The format of the IsDelegateInstantiated method is:
981 % MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
983 % A description of each parameter follows.
985 % o exception: return any errors or warnings in this structure.
988 static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
990 if (delegate_cache == (LinkedListInfo *) NULL)
992 if (delegate_semaphore == (SemaphoreInfo *) NULL)
993 ActivateSemaphoreInfo(&delegate_semaphore);
994 LockSemaphoreInfo(delegate_semaphore);
995 if (delegate_cache == (LinkedListInfo *) NULL)
996 delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
997 UnlockSemaphoreInfo(delegate_semaphore);
999 return(IsMagickNotNULL(delegate_cache));
1003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1007 % I n v o k e D e l e g a t e %
1011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013 % InvokeDelegate replaces any embedded formatting characters with the
1014 % appropriate image attribute and executes the resulting command. MagickFalse
1015 % is returned if the commands execute with success otherwise MagickTrue.
1017 % The format of the InvokeDelegate method is:
1019 % MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
1020 % const char *decode,const char *encode,ExceptionInfo *exception)
1022 % A description of each parameter follows:
1024 % o image_info: the imageInfo.
1026 % o image: the image.
1028 % o exception: return any errors or warnings in this structure.
1032 static MagickBooleanType CopyDelegateFile(const char *source,
1033 const char *destination)
1059 Copy source file to destination.
1061 assert(source != (const char *) NULL);
1062 assert(destination != (char *) NULL);
1063 status=GetPathAttributes(destination,&attributes);
1064 if (status != MagickFalse)
1066 destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
1067 if (destination_file == -1)
1068 return(MagickFalse);
1069 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
1070 if (source_file == -1)
1072 (void) close(destination_file);
1073 return(MagickFalse);
1075 quantum=(size_t) MagickMaxBufferExtent;
1076 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
1077 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
1078 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1079 if (buffer == (unsigned char *) NULL)
1081 (void) close(source_file);
1082 (void) close(destination_file);
1083 return(MagickFalse);
1086 for (i=0; ; i+=count)
1088 count=(ssize_t) read(source_file,buffer,quantum);
1091 length=(size_t) count;
1092 count=(ssize_t) write(destination_file,buffer,length);
1093 if ((size_t) count != length)
1096 (void) close(destination_file);
1097 (void) close(source_file);
1098 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1099 return(IsMagickTrue(i!=0));
1102 MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
1103 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
1108 input_filename[MagickPathExtent],
1109 output_filename[MagickPathExtent];
1127 assert(image_info != (ImageInfo *) NULL);
1128 assert(image_info->signature == MagickSignature);
1129 assert(image != (Image *) NULL);
1130 assert(image->signature == MagickSignature);
1131 if (image->debug != MagickFalse)
1132 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1134 rights=ExecutePolicyRights;
1135 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,decode)) )
1138 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1139 "NotAuthorized","`%s'",decode);
1140 return(MagickFalse);
1142 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,encode)) )
1145 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1146 "NotAuthorized","`%s'",encode);
1147 return(MagickFalse);
1149 temporary=IsMagickTrue(*image->filename == '\0');
1150 if( IfMagickTrue(temporary) )
1151 if( IfMagickFalse(AcquireUniqueFilename(image->filename)) )
1153 ThrowFileException(exception,FileOpenError,
1154 "UnableToCreateTemporaryFile",image->filename);
1155 return(MagickFalse);
1157 delegate_info=GetDelegateInfo(decode,encode,exception);
1158 if (delegate_info == (DelegateInfo *) NULL)
1160 if( IfMagickTrue(temporary) )
1161 (void) RelinquishUniqueFileResource(image->filename);
1162 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1163 "NoTagFound","`%s'",decode ? decode : encode);
1164 return(MagickFalse);
1166 if (*image_info->filename == '\0')
1168 if( IfMagickFalse(AcquireUniqueFilename(image_info->filename)) )
1170 if( IfMagickTrue(temporary) )
1171 (void) RelinquishUniqueFileResource(image->filename);
1172 ThrowFileException(exception,FileOpenError,
1173 "UnableToCreateTemporaryFile",image_info->filename);
1174 return(MagickFalse);
1176 image_info->temporary=MagickTrue;
1178 if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
1179 (delegate_info->encode != (char *) NULL)) ||
1180 ((encode != (const char *) NULL) &&
1181 (delegate_info->decode != (char *) NULL))))
1193 Delegate requires a particular image format.
1195 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
1197 ThrowFileException(exception,FileOpenError,
1198 "UnableToCreateTemporaryFile",image_info->unique);
1199 return(MagickFalse);
1201 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
1203 (void) RelinquishUniqueFileResource(image_info->unique);
1204 ThrowFileException(exception,FileOpenError,
1205 "UnableToCreateTemporaryFile",image_info->zero);
1206 return(MagickFalse);
1208 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
1209 delegate_info->encode : delegate_info->decode,exception);
1210 if (magick == (char *) NULL)
1212 (void) RelinquishUniqueFileResource(image_info->unique);
1213 (void) RelinquishUniqueFileResource(image_info->zero);
1214 if( IfMagickTrue(temporary) )
1215 (void) RelinquishUniqueFileResource(image->filename);
1216 (void) ThrowMagickException(exception,GetMagickModule(),
1217 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1218 return(MagickFalse);
1220 LocaleUpper(magick);
1221 clone_info=CloneImageInfo(image_info);
1222 (void) CopyMagickString((char *) clone_info->magick,magick,
1224 if (LocaleCompare(magick,"NULL") != 0)
1225 (void) CopyMagickString(image->magick,magick,MagickPathExtent);
1226 magick=DestroyString(magick);
1227 (void) FormatLocaleString(clone_info->filename,MagickPathExtent,"%s:",
1228 delegate_info->decode);
1229 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1231 (void) CopyMagickString(clone_info->filename,image_info->filename,
1233 (void) CopyMagickString(image_info->filename,image->filename,
1235 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1237 (void) FormatLocaleString(p->filename,MagickPathExtent,"%s:%s",
1238 delegate_info->decode,clone_info->filename);
1239 status=WriteImage(clone_info,p,exception);
1240 if( IfMagickFalse(status) )
1242 (void) RelinquishUniqueFileResource(image_info->unique);
1243 (void) RelinquishUniqueFileResource(image_info->zero);
1244 if( IfMagickTrue(temporary) )
1245 (void) RelinquishUniqueFileResource(image->filename);
1246 clone_info=DestroyImageInfo(clone_info);
1247 (void) ThrowMagickException(exception,GetMagickModule(),
1248 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1249 return(MagickFalse);
1251 if( IfMagickTrue(clone_info->adjoin) )
1254 (void) RelinquishUniqueFileResource(image_info->unique);
1255 (void) RelinquishUniqueFileResource(image_info->zero);
1256 clone_info=DestroyImageInfo(clone_info);
1261 commands=StringToList(delegate_info->commands);
1262 if (commands == (char **) NULL)
1264 if( IfMagickTrue(temporary) )
1265 (void) RelinquishUniqueFileResource(image->filename);
1266 (void) ThrowMagickException(exception,GetMagickModule(),
1267 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1268 decode ? decode : encode);
1269 return(MagickFalse);
1271 command=(char *) NULL;
1273 (void) CopyMagickString(output_filename,image_info->filename,MagickPathExtent);
1274 (void) CopyMagickString(input_filename,image->filename,MagickPathExtent);
1275 for (i=0; commands[i] != (char *) NULL; i++)
1277 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1278 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
1280 ThrowFileException(exception,FileOpenError,
1281 "UnableToCreateTemporaryFile",image_info->unique);
1284 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
1286 (void) RelinquishUniqueFileResource(image_info->unique);
1287 ThrowFileException(exception,FileOpenError,
1288 "UnableToCreateTemporaryFile",image_info->zero);
1291 if (LocaleCompare(decode,"SCAN") != 0)
1293 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1294 if( IfMagickFalse(status) )
1296 ThrowFileException(exception,FileOpenError,
1297 "UnableToCreateTemporaryFile",input_filename);
1302 command=InterpretImageProperties(image_info,image,commands[i],exception);
1303 if (command != (char *) NULL)
1308 status=IsMagickTrue(ExternalDelegateCommand(delegate_info->spawn,
1309 image_info->verbose,command,(char *) NULL,exception) != 0);
1310 if (IfMagickTrue(delegate_info->spawn))
1316 Wait for input file to 'disappear', or maximum 10 seconds.
1319 while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
1320 (void) MagickDelay(100); /* sleep 0.1 seconds */
1322 command=DestroyString(command);
1324 if (LocaleCompare(decode,"SCAN") != 0)
1326 if (IfMagickFalse(CopyDelegateFile(image->filename,input_filename)))
1327 (void) RelinquishUniqueFileResource(input_filename);
1329 if (IfMagickFalse(CopyDelegateFile(image_info->filename,output_filename)))
1330 (void) RelinquishUniqueFileResource(output_filename);
1331 if( IfMagickTrue(image_info->temporary) )
1332 (void) RelinquishUniqueFileResource(image_info->filename);
1333 (void) RelinquishUniqueFileResource(image_info->unique);
1334 (void) RelinquishUniqueFileResource(image_info->zero);
1335 (void) RelinquishUniqueFileResource(image_info->filename);
1336 (void) RelinquishUniqueFileResource(image->filename);
1337 if( IfMagickTrue(status) )
1339 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1340 "DelegateFailed","`%s'",commands[i]);
1343 commands[i]=DestroyString(commands[i]);
1345 (void) CopyMagickString(image_info->filename,output_filename,MagickPathExtent);
1346 (void) CopyMagickString(image->filename,input_filename,MagickPathExtent);
1348 Relinquish resources.
1350 for ( ; commands[i] != (char *) NULL; i++)
1351 commands[i]=DestroyString(commands[i]);
1352 commands=(char **) RelinquishMagickMemory(commands);
1353 if( IfMagickTrue(temporary) )
1354 (void) RelinquishUniqueFileResource(image->filename);
1355 return(IsMagickFalse(status));
1359 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363 % L i s t D e l e g a t e I n f o %
1367 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1369 % ListDelegateInfo() lists the image formats to a file.
1371 % The format of the ListDelegateInfo method is:
1373 % MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1375 % A description of each parameter follows.
1377 % o file: An pointer to a FILE.
1379 % o exception: return any errors or warnings in this structure.
1382 MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1383 ExceptionInfo *exception)
1390 delegate[MagickPathExtent];
1404 if (file == (const FILE *) NULL)
1406 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1407 if (delegate_info == (const DelegateInfo **) NULL)
1408 return(MagickFalse);
1409 path=(const char *) NULL;
1410 for (i=0; i < (ssize_t) number_delegates; i++)
1412 if( IfMagickTrue(delegate_info[i]->stealth) )
1414 if ((path == (const char *) NULL) ||
1415 (LocaleCompare(path,delegate_info[i]->path) != 0))
1417 if (delegate_info[i]->path != (char *) NULL)
1418 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1419 (void) FormatLocaleFile(file,"Delegate Command\n");
1420 (void) FormatLocaleFile(file,
1421 "-------------------------------------------------"
1422 "------------------------------\n");
1424 path=delegate_info[i]->path;
1426 if (delegate_info[i]->encode != (char *) NULL)
1427 (void) CopyMagickString(delegate,delegate_info[i]->encode,MagickPathExtent);
1428 (void) ConcatenateMagickString(delegate," ",MagickPathExtent);
1430 commands=StringToList(delegate_info[i]->commands);
1431 if (commands == (char **) NULL)
1433 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
1434 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1435 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1436 StripString(commands[0]);
1437 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
1438 for (j=1; commands[j] != (char *) NULL; j++)
1440 StripString(commands[j]);
1441 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
1443 for (j=0; commands[j] != (char *) NULL; j++)
1444 commands[j]=DestroyString(commands[j]);
1445 commands=(char **) RelinquishMagickMemory(commands);
1447 (void) fflush(file);
1448 delegate_info=(const DelegateInfo **)
1449 RelinquishMagickMemory((void *) delegate_info);
1454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 + L o a d D e l e g a t e L i s t %
1462 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464 % LoadDelegateCache() loads the delegate configurations which provides a
1465 % mapping between delegate attributes and a delegate name.
1467 % The format of the LoadDelegateCache method is:
1469 % MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1470 % const char *xml,const char *filename,const size_t depth,
1471 % ExceptionInfo *exception)
1473 % A description of each parameter follows:
1475 % o xml: The delegate list in XML format.
1477 % o filename: The delegate list filename.
1479 % o depth: depth of <include /> statements.
1481 % o exception: return any errors or warnings in this structure.
1484 static MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1485 const char *xml,const char *filename,const size_t depth,
1486 ExceptionInfo *exception)
1489 keyword[MagickPathExtent],
1502 Load the delegate map file.
1504 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1505 "Loading delegate configuration file \"%s\" ...",filename);
1506 if (xml == (const char *) NULL)
1507 return(MagickFalse);
1509 delegate_info=(DelegateInfo *) NULL;
1510 token=AcquireString(xml);
1511 for (q=(const char *) xml; *q != '\0'; )
1516 GetMagickToken(q,&q,token);
1519 (void) CopyMagickString(keyword,token,MagickPathExtent);
1520 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1525 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1526 GetMagickToken(q,&q,token);
1529 if (LocaleNCompare(keyword,"<!--",4) == 0)
1534 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1535 GetMagickToken(q,&q,token);
1538 if (LocaleCompare(keyword,"<include") == 0)
1543 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1545 (void) CopyMagickString(keyword,token,MagickPathExtent);
1546 GetMagickToken(q,&q,token);
1549 GetMagickToken(q,&q,token);
1550 if (LocaleCompare(keyword,"file") == 0)
1553 (void) ThrowMagickException(exception,GetMagickModule(),
1554 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1558 path[MagickPathExtent],
1561 GetPathComponent(filename,HeadPath,path);
1563 (void) ConcatenateMagickString(path,DirectorySeparator,
1565 if (*token == *DirectorySeparator)
1566 (void) CopyMagickString(path,token,MagickPathExtent);
1568 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1569 xml=FileToXML(path,~0UL);
1570 if (xml != (char *) NULL)
1572 status&=LoadDelegateCache(delegate_cache,xml,path,
1574 xml=(char *) RelinquishMagickMemory(xml);
1581 if (LocaleCompare(keyword,"<delegate") == 0)
1586 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1587 sizeof(*delegate_info));
1588 if (delegate_info == (DelegateInfo *) NULL)
1589 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1590 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1591 delegate_info->path=ConstantString(filename);
1592 delegate_info->thread_support=MagickTrue;
1593 delegate_info->signature=MagickSignature;
1596 if (delegate_info == (DelegateInfo *) NULL)
1598 if (LocaleCompare(keyword,"/>") == 0)
1600 status=AppendValueToLinkedList(delegate_cache,delegate_info);
1601 if( IfMagickFalse(status) )
1602 (void) ThrowMagickException(exception,GetMagickModule(),
1603 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1604 delegate_info->commands);
1605 delegate_info=(DelegateInfo *) NULL;
1608 GetMagickToken(q,(const char **) NULL,token);
1611 GetMagickToken(q,&q,token);
1612 GetMagickToken(q,&q,token);
1618 if (LocaleCompare((char *) keyword,"command") == 0)
1623 commands=AcquireString(token);
1624 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1625 if (strchr(commands,'@') != (char *) NULL)
1628 path[MagickPathExtent];
1630 NTGhostscriptEXE(path,MagickPathExtent);
1631 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1633 (void) SubstituteString((char **) &commands,"\\","/");
1636 (void) SubstituteString((char **) &commands,"&","&");
1637 (void) SubstituteString((char **) &commands,""","\"");
1638 (void) SubstituteString((char **) &commands,">",">");
1639 (void) SubstituteString((char **) &commands,"<","<");
1640 delegate_info->commands=commands;
1648 if (LocaleCompare((char *) keyword,"decode") == 0)
1650 delegate_info->decode=ConstantString(token);
1651 delegate_info->mode=1;
1659 if (LocaleCompare((char *) keyword,"encode") == 0)
1661 delegate_info->encode=ConstantString(token);
1662 delegate_info->mode=(-1);
1670 if (LocaleCompare((char *) keyword,"mode") == 0)
1672 delegate_info->mode=1;
1673 if (LocaleCompare(token,"bi") == 0)
1674 delegate_info->mode=0;
1676 if (LocaleCompare(token,"encode") == 0)
1677 delegate_info->mode=(-1);
1685 if (LocaleCompare((char *) keyword,"spawn") == 0)
1687 delegate_info->spawn=IsStringTrue(token);
1690 if (LocaleCompare((char *) keyword,"stealth") == 0)
1692 delegate_info->stealth=IsStringTrue(token);
1700 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1702 delegate_info->thread_support=IsStringTrue(token);
1703 if (delegate_info->thread_support == MagickFalse)
1704 delegate_info->semaphore=AcquireSemaphoreInfo();
1713 token=(char *) RelinquishMagickMemory(token);
1714 return(status != 0 ? MagickTrue : MagickFalse);