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,"&",MaxTextExtent);
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)
449 status=NTSystemCommand(sanitize_command,message);
450 #elif defined(macintosh)
451 status=MACSystemCommand(sanitize_command);
453 status=system(sanitize_command);
455 # error No suitable system() method.
459 if ((message != (char *) NULL) && (*message != '\0'))
460 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
461 "FailedToExecuteCommand","`%s' (%s)",command,message);
463 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
464 "FailedToExecuteCommand","`%s' (%d)",command,status);
466 sanitize_command=DestroyString(sanitize_command);
467 for (i=0; i < (ssize_t) number_arguments; i++)
468 arguments[i]=DestroyString(arguments[i]);
469 arguments=(char **) RelinquishMagickMemory(arguments);
474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
478 % G e t D e l e g a t e C o m m a n d %
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % GetDelegateCommand() replaces any embedded formatting characters with the
485 % appropriate image attribute and returns the resulting command.
487 % The format of the GetDelegateCommand method is:
489 % char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
490 % const char *decode,const char *encode,ExceptionInfo *exception)
492 % A description of each parameter follows:
494 % o command: Method GetDelegateCommand returns the command associated
495 % with specified delegate tag.
497 % o image_info: the image info.
499 % o image: the image.
501 % o decode: Specifies the decode delegate we are searching for as a
504 % o encode: Specifies the encode delegate we are searching for as a
507 % o exception: return any errors or warnings in this structure.
510 MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
511 const char *decode,const char *encode,ExceptionInfo *exception)
523 assert(image_info != (ImageInfo *) NULL);
524 assert(image_info->signature == MagickSignature);
525 assert(image != (Image *) NULL);
526 assert(image->signature == MagickSignature);
527 if (image->debug != MagickFalse)
528 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
530 delegate_info=GetDelegateInfo(decode,encode,exception);
531 if (delegate_info == (const DelegateInfo *) NULL)
533 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
534 "NoTagFound","`%s'",decode ? decode : encode);
535 return((char *) NULL);
537 commands=StringToList(delegate_info->commands);
538 if (commands == (char **) NULL)
540 (void) ThrowMagickException(exception,GetMagickModule(),
541 ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
543 return((char *) NULL);
545 command=InterpretImageProperties((ImageInfo *) image_info,image,commands[0],
547 if (command == (char *) NULL)
548 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
549 "MemoryAllocationFailed","`%s'",commands[0]);
551 Relinquish resources.
553 for (i=0; commands[i] != (char *) NULL; i++)
554 commands[i]=DestroyString(commands[i]);
555 commands=(char **) RelinquishMagickMemory(commands);
560 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % G e t D e l e g a t e C o m m a n d s %
568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % GetDelegateCommands() returns the commands associated with a delegate.
572 % The format of the GetDelegateCommands method is:
574 % const char *GetDelegateCommands(const DelegateInfo *delegate_info)
576 % A description of each parameter follows:
578 % o delegate_info: The delegate info.
581 MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
583 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
585 assert(delegate_info != (DelegateInfo *) NULL);
586 assert(delegate_info->signature == MagickSignature);
587 return(delegate_info->commands);
591 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595 % G e t D e l e g a t e I n f o %
599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
601 % GetDelegateInfo() returns any delegates associated with the specified tag.
603 % The format of the GetDelegateInfo method is:
605 % const DelegateInfo *GetDelegateInfo(const char *decode,
606 % const char *encode,ExceptionInfo *exception)
608 % A description of each parameter follows:
610 % o decode: Specifies the decode delegate we are searching for as a
613 % o encode: Specifies the encode delegate we are searching for as a
616 % o exception: return any errors or warnings in this structure.
619 MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
620 const char *encode,ExceptionInfo *exception)
622 register const DelegateInfo
625 assert(exception != (ExceptionInfo *) NULL);
626 if (IfMagickFalse(IsDelegateCacheInstantiated(exception)))
627 return((const DelegateInfo *) NULL);
629 Search for named delegate.
631 LockSemaphoreInfo(delegate_semaphore);
632 ResetLinkedListIterator(delegate_cache);
633 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
634 if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
636 UnlockSemaphoreInfo(delegate_semaphore);
639 while (p != (const DelegateInfo *) NULL)
643 if (LocaleCompare(p->decode,decode) == 0)
645 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
650 if (LocaleCompare(p->encode,encode) == 0)
652 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
655 if (LocaleCompare(decode,p->decode) == 0)
656 if (LocaleCompare(encode,p->encode) == 0)
658 if (LocaleCompare(decode,"*") == 0)
659 if (LocaleCompare(encode,p->encode) == 0)
661 if (LocaleCompare(decode,p->decode) == 0)
662 if (LocaleCompare(encode,"*") == 0)
664 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
666 if (p != (const DelegateInfo *) NULL)
667 (void) InsertValueInLinkedList(delegate_cache,0,
668 RemoveElementByValueFromLinkedList(delegate_cache,p));
669 UnlockSemaphoreInfo(delegate_semaphore);
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 % G e t D e l e g a t e I n f o L i s t %
682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 % GetDelegateInfoList() returns any delegates that match the specified pattern.
686 % The delegate of the GetDelegateInfoList function is:
688 % const DelegateInfo **GetDelegateInfoList(const char *pattern,
689 % size_t *number_delegates,ExceptionInfo *exception)
691 % A description of each parameter follows:
693 % o pattern: Specifies a pointer to a text string containing a pattern.
695 % o number_delegates: This integer returns the number of delegates in the
698 % o exception: return any errors or warnings in this structure.
702 #if defined(__cplusplus) || defined(c_plusplus)
706 static int DelegateInfoCompare(const void *x,const void *y)
715 p=(const DelegateInfo **) x,
716 q=(const DelegateInfo **) y;
717 cmp=LocaleCompare((*p)->path,(*q)->path);
720 if ((*p)->decode == (char *) NULL)
721 if (((*p)->encode != (char *) NULL) &&
722 ((*q)->encode != (char *) NULL))
723 return(strcmp((*p)->encode,(*q)->encode));
724 if (((*p)->decode != (char *) NULL) &&
725 ((*q)->decode != (char *) NULL))
726 return(strcmp((*p)->decode,(*q)->decode));
731 #if defined(__cplusplus) || defined(c_plusplus)
735 MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
736 size_t *number_delegates,ExceptionInfo *exception)
741 register const DelegateInfo
748 Allocate delegate list.
750 assert(number_delegates != (size_t *) NULL);
751 assert(pattern != (char *) NULL);
752 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
755 p=GetDelegateInfo("*","*",exception);
756 if (p == (const DelegateInfo *) NULL)
757 return((const DelegateInfo **) NULL);
758 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
759 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
760 if (delegates == (const DelegateInfo **) NULL)
761 return((const DelegateInfo **) NULL);
763 Generate delegate list.
765 LockSemaphoreInfo(delegate_semaphore);
766 ResetLinkedListIterator(delegate_cache);
767 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
768 for (i=0; p != (const DelegateInfo *) NULL; )
770 if( IfMagickFalse(p->stealth) &&
771 ( IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) ||
772 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse))) )
774 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
776 UnlockSemaphoreInfo(delegate_semaphore);
777 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
778 delegates[i]=(DelegateInfo *) NULL;
779 *number_delegates=(size_t) i;
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
788 % G e t D e l e g a t e L i s t %
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794 % GetDelegateList() returns any image format delegates that match the
797 % The format of the GetDelegateList function is:
799 % char **GetDelegateList(const char *pattern,
800 % size_t *number_delegates,ExceptionInfo *exception)
802 % A description of each parameter follows:
804 % o pattern: Specifies a pointer to a text string containing a pattern.
806 % o number_delegates: This integer returns the number of delegates
809 % o exception: return any errors or warnings in this structure.
813 #if defined(__cplusplus) || defined(c_plusplus)
817 static int DelegateCompare(const void *x,const void *y)
825 return(LocaleCompare(*p,*q));
828 #if defined(__cplusplus) || defined(c_plusplus)
832 MagickExport char **GetDelegateList(const char *pattern,
833 size_t *number_delegates,ExceptionInfo *exception)
838 register const DelegateInfo
845 Allocate delegate list.
847 assert(pattern != (char *) NULL);
848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
850 assert(number_delegates != (size_t *) NULL);
852 p=GetDelegateInfo("*","*",exception);
853 if (p == (const DelegateInfo *) NULL)
854 return((char **) NULL);
855 delegates=(char **) AcquireQuantumMemory((size_t)
856 GetNumberOfElementsInLinkedList(delegate_cache)+1UL,sizeof(*delegates));
857 if (delegates == (char **) NULL)
858 return((char **) NULL);
859 LockSemaphoreInfo(delegate_semaphore);
860 ResetLinkedListIterator(delegate_cache);
861 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
862 for (i=0; p != (const DelegateInfo *) NULL; )
864 if( IfMagickFalse(p->stealth) &&
865 IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) )
866 delegates[i++]=ConstantString(p->decode);
867 if( IfMagickFalse(p->stealth) &&
868 IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse)) )
869 delegates[i++]=ConstantString(p->encode);
870 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_cache);
872 UnlockSemaphoreInfo(delegate_semaphore);
873 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
874 delegates[i]=(char *) NULL;
875 *number_delegates=(size_t) i;
880 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % G e t D e l e g a t e M o d e %
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 % GetDelegateMode() returns the mode of the delegate.
892 % The format of the GetDelegateMode method is:
894 % ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
896 % A description of each parameter follows:
898 % o delegate_info: The delegate info.
901 MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
903 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
905 assert(delegate_info != (DelegateInfo *) NULL);
906 assert(delegate_info->signature == MagickSignature);
907 return(delegate_info->mode);
911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 + G e t D e l e g a t e T h r e a d S u p p o r t %
919 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
921 % GetDelegateThreadSupport() returns MagickTrue if the delegate supports
924 % The format of the GetDelegateThreadSupport method is:
926 % MagickBooleanType GetDelegateThreadSupport(
927 % const DelegateInfo *delegate_info)
929 % A description of each parameter follows:
931 % o delegate_info: The delegate info.
934 MagickExport MagickBooleanType GetDelegateThreadSupport(
935 const DelegateInfo *delegate_info)
937 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
939 assert(delegate_info != (DelegateInfo *) NULL);
940 assert(delegate_info->signature == MagickSignature);
941 return(delegate_info->thread_support);
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 + 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 %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % IsDelegateCacheInstantiated() determines if the delegate cache is
956 % instantiated. If not, it instantiates the cache and returns it.
958 % The format of the IsDelegateInstantiated method is:
960 % MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
962 % A description of each parameter follows.
964 % o exception: return any errors or warnings in this structure.
967 static MagickBooleanType IsDelegateCacheInstantiated(ExceptionInfo *exception)
969 if (delegate_cache == (LinkedListInfo *) NULL)
971 if (delegate_semaphore == (SemaphoreInfo *) NULL)
972 ActivateSemaphoreInfo(&delegate_semaphore);
973 LockSemaphoreInfo(delegate_semaphore);
974 if (delegate_cache == (LinkedListInfo *) NULL)
975 delegate_cache=AcquireDelegateCache(DelegateFilename,exception);
976 UnlockSemaphoreInfo(delegate_semaphore);
978 return(IsMagickNotNULL(delegate_cache));
982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986 % I n v o k e D e l e g a t e %
990 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
992 % InvokeDelegate replaces any embedded formatting characters with the
993 % appropriate image attribute and executes the resulting command. MagickFalse
994 % is returned if the commands execute with success otherwise MagickTrue.
996 % The format of the InvokeDelegate method is:
998 % MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
999 % const char *decode,const char *encode,ExceptionInfo *exception)
1001 % A description of each parameter follows:
1003 % o image_info: the imageInfo.
1005 % o image: the image.
1007 % o exception: return any errors or warnings in this structure.
1011 static MagickBooleanType CopyDelegateFile(const char *source,
1012 const char *destination)
1038 Copy source file to destination.
1040 assert(source != (const char *) NULL);
1041 assert(destination != (char *) NULL);
1042 status=GetPathAttributes(destination,&attributes);
1043 if( IfMagickTrue(status) && (attributes.st_size != 0))
1045 destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
1046 if (destination_file == -1)
1047 return(MagickFalse);
1048 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
1049 if (source_file == -1)
1051 (void) close(destination_file);
1052 return(MagickFalse);
1054 quantum=(size_t) MagickMaxBufferExtent;
1055 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
1056 quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
1057 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
1058 if (buffer == (unsigned char *) NULL)
1060 (void) close(source_file);
1061 (void) close(destination_file);
1062 return(MagickFalse);
1065 for (i=0; ; i+=count)
1067 count=(ssize_t) read(source_file,buffer,quantum);
1070 length=(size_t) count;
1071 count=(ssize_t) write(destination_file,buffer,length);
1072 if ((size_t) count != length)
1075 (void) close(destination_file);
1076 (void) close(source_file);
1077 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1078 return(IsMagickTrue(i!=0));
1081 MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
1082 Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
1087 input_filename[MaxTextExtent],
1088 output_filename[MaxTextExtent];
1106 assert(image_info != (ImageInfo *) NULL);
1107 assert(image_info->signature == MagickSignature);
1108 assert(image != (Image *) NULL);
1109 assert(image->signature == MagickSignature);
1110 if (image->debug != MagickFalse)
1111 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1113 rights=ExecutePolicyRights;
1114 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,decode)) )
1117 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1118 "NotAuthorized","`%s'",decode);
1119 return(MagickFalse);
1121 if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,encode)) )
1124 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
1125 "NotAuthorized","`%s'",encode);
1126 return(MagickFalse);
1128 temporary=IsMagickTrue(*image->filename == '\0');
1129 if( IfMagickTrue(temporary) )
1130 if( IfMagickFalse(AcquireUniqueFilename(image->filename)) )
1132 ThrowFileException(exception,FileOpenError,
1133 "UnableToCreateTemporaryFile",image->filename);
1134 return(MagickFalse);
1136 delegate_info=GetDelegateInfo(decode,encode,exception);
1137 if (delegate_info == (DelegateInfo *) NULL)
1139 if( IfMagickTrue(temporary) )
1140 (void) RelinquishUniqueFileResource(image->filename);
1141 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1142 "NoTagFound","`%s'",decode ? decode : encode);
1143 return(MagickFalse);
1145 if (*image_info->filename == '\0')
1147 if( IfMagickFalse(AcquireUniqueFilename(image_info->filename)) )
1149 if( IfMagickTrue(temporary) )
1150 (void) RelinquishUniqueFileResource(image->filename);
1151 ThrowFileException(exception,FileOpenError,
1152 "UnableToCreateTemporaryFile",image_info->filename);
1153 return(MagickFalse);
1155 image_info->temporary=MagickTrue;
1157 if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
1158 (delegate_info->encode != (char *) NULL)) ||
1159 ((encode != (const char *) NULL) &&
1160 (delegate_info->decode != (char *) NULL))))
1172 Delegate requires a particular image format.
1174 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
1176 ThrowFileException(exception,FileOpenError,
1177 "UnableToCreateTemporaryFile",image_info->unique);
1178 return(MagickFalse);
1180 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
1182 (void) RelinquishUniqueFileResource(image_info->unique);
1183 ThrowFileException(exception,FileOpenError,
1184 "UnableToCreateTemporaryFile",image_info->zero);
1185 return(MagickFalse);
1187 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
1188 delegate_info->encode : delegate_info->decode,exception);
1189 if (magick == (char *) NULL)
1191 (void) RelinquishUniqueFileResource(image_info->unique);
1192 (void) RelinquishUniqueFileResource(image_info->zero);
1193 if( IfMagickTrue(temporary) )
1194 (void) RelinquishUniqueFileResource(image->filename);
1195 (void) ThrowMagickException(exception,GetMagickModule(),
1196 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1197 return(MagickFalse);
1199 LocaleUpper(magick);
1200 clone_info=CloneImageInfo(image_info);
1201 (void) CopyMagickString((char *) clone_info->magick,magick,
1203 if (LocaleCompare(magick,"NULL") != 0)
1204 (void) CopyMagickString(image->magick,magick,MaxTextExtent);
1205 magick=DestroyString(magick);
1206 (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:",
1207 delegate_info->decode);
1208 (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
1210 (void) CopyMagickString(clone_info->filename,image_info->filename,
1212 (void) CopyMagickString(image_info->filename,image->filename,
1214 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
1216 (void) FormatLocaleString(p->filename,MaxTextExtent,"%s:%s",
1217 delegate_info->decode,clone_info->filename);
1218 status=WriteImage(clone_info,p,exception);
1219 if( IfMagickFalse(status) )
1221 (void) RelinquishUniqueFileResource(image_info->unique);
1222 (void) RelinquishUniqueFileResource(image_info->zero);
1223 if( IfMagickTrue(temporary) )
1224 (void) RelinquishUniqueFileResource(image->filename);
1225 clone_info=DestroyImageInfo(clone_info);
1226 (void) ThrowMagickException(exception,GetMagickModule(),
1227 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
1228 return(MagickFalse);
1230 if( IfMagickTrue(clone_info->adjoin) )
1233 (void) RelinquishUniqueFileResource(image_info->unique);
1234 (void) RelinquishUniqueFileResource(image_info->zero);
1235 clone_info=DestroyImageInfo(clone_info);
1240 commands=StringToList(delegate_info->commands);
1241 if (commands == (char **) NULL)
1243 if( IfMagickTrue(temporary) )
1244 (void) RelinquishUniqueFileResource(image->filename);
1245 (void) ThrowMagickException(exception,GetMagickModule(),
1246 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1247 decode ? decode : encode);
1248 return(MagickFalse);
1250 command=(char *) NULL;
1252 (void) CopyMagickString(output_filename,image_info->filename,MaxTextExtent);
1253 (void) CopyMagickString(input_filename,image->filename,MaxTextExtent);
1254 for (i=0; commands[i] != (char *) NULL; i++)
1256 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1257 if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
1259 ThrowFileException(exception,FileOpenError,
1260 "UnableToCreateTemporaryFile",image_info->unique);
1263 if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
1265 (void) RelinquishUniqueFileResource(image_info->unique);
1266 ThrowFileException(exception,FileOpenError,
1267 "UnableToCreateTemporaryFile",image_info->zero);
1270 if (LocaleCompare(decode,"SCAN") != 0)
1272 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1273 if( IfMagickFalse(status) )
1275 ThrowFileException(exception,FileOpenError,
1276 "UnableToCreateTemporaryFile",input_filename);
1281 command=InterpretImageProperties(image_info,image,commands[i],exception);
1282 if (command != (char *) NULL)
1287 status=IsMagickTrue(ExternalDelegateCommand(delegate_info->spawn,
1288 image_info->verbose,command,(char *) NULL,exception) != 0);
1289 if (IfMagickTrue(delegate_info->spawn))
1295 Wait for input file to 'disappear', or maximum 10 seconds.
1298 while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
1299 (void) MagickDelay(100); /* sleep 0.1 seconds */
1301 command=DestroyString(command);
1303 if (LocaleCompare(decode,"SCAN") != 0)
1305 if( IfMagickFalse(CopyDelegateFile(image->filename,input_filename)) )
1306 (void) RelinquishUniqueFileResource(input_filename);
1308 if( IfMagickFalse(CopyDelegateFile(image_info->filename,output_filename)) )
1309 (void) RelinquishUniqueFileResource(output_filename);
1310 if( IfMagickTrue(image_info->temporary) )
1311 (void) RelinquishUniqueFileResource(image_info->filename);
1312 (void) RelinquishUniqueFileResource(image_info->unique);
1313 (void) RelinquishUniqueFileResource(image_info->zero);
1314 (void) RelinquishUniqueFileResource(image_info->filename);
1315 (void) RelinquishUniqueFileResource(image->filename);
1316 if( IfMagickTrue(status) )
1318 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1319 "DelegateFailed","`%s'",commands[i]);
1322 commands[i]=DestroyString(commands[i]);
1324 (void) CopyMagickString(image_info->filename,output_filename,MaxTextExtent);
1325 (void) CopyMagickString(image->filename,input_filename,MaxTextExtent);
1327 Relinquish resources.
1329 for ( ; commands[i] != (char *) NULL; i++)
1330 commands[i]=DestroyString(commands[i]);
1331 commands=(char **) RelinquishMagickMemory(commands);
1332 if( IfMagickTrue(temporary) )
1333 (void) RelinquishUniqueFileResource(image->filename);
1334 return(IsMagickFalse(status));
1338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342 % L i s t D e l e g a t e I n f o %
1346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1348 % ListDelegateInfo() lists the image formats to a file.
1350 % The format of the ListDelegateInfo method is:
1352 % MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1354 % A description of each parameter follows.
1356 % o file: An pointer to a FILE.
1358 % o exception: return any errors or warnings in this structure.
1361 MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1362 ExceptionInfo *exception)
1369 delegate[MaxTextExtent];
1383 if (file == (const FILE *) NULL)
1385 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1386 if (delegate_info == (const DelegateInfo **) NULL)
1387 return(MagickFalse);
1388 path=(const char *) NULL;
1389 for (i=0; i < (ssize_t) number_delegates; i++)
1391 if( IfMagickTrue(delegate_info[i]->stealth) )
1393 if ((path == (const char *) NULL) ||
1394 (LocaleCompare(path,delegate_info[i]->path) != 0))
1396 if (delegate_info[i]->path != (char *) NULL)
1397 (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1398 (void) FormatLocaleFile(file,"Delegate Command\n");
1399 (void) FormatLocaleFile(file,
1400 "-------------------------------------------------"
1401 "------------------------------\n");
1403 path=delegate_info[i]->path;
1405 if (delegate_info[i]->encode != (char *) NULL)
1406 (void) CopyMagickString(delegate,delegate_info[i]->encode,MaxTextExtent);
1407 (void) ConcatenateMagickString(delegate," ",MaxTextExtent);
1409 commands=StringToList(delegate_info[i]->commands);
1410 if (commands == (char **) NULL)
1412 (void) FormatLocaleFile(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
1413 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1414 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1415 StripString(commands[0]);
1416 (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
1417 for (j=1; commands[j] != (char *) NULL; j++)
1419 StripString(commands[j]);
1420 (void) FormatLocaleFile(file," \"%s\"\n",commands[j]);
1422 for (j=0; commands[j] != (char *) NULL; j++)
1423 commands[j]=DestroyString(commands[j]);
1424 commands=(char **) RelinquishMagickMemory(commands);
1426 (void) fflush(file);
1427 delegate_info=(const DelegateInfo **)
1428 RelinquishMagickMemory((void *) delegate_info);
1433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1437 + L o a d D e l e g a t e L i s t %
1441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1443 % LoadDelegateCache() loads the delegate configurations which provides a
1444 % mapping between delegate attributes and a delegate name.
1446 % The format of the LoadDelegateCache method is:
1448 % MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1449 % const char *xml,const char *filename,const size_t depth,
1450 % ExceptionInfo *exception)
1452 % A description of each parameter follows:
1454 % o xml: The delegate list in XML format.
1456 % o filename: The delegate list filename.
1458 % o depth: depth of <include /> statements.
1460 % o exception: return any errors or warnings in this structure.
1463 static MagickBooleanType LoadDelegateCache(LinkedListInfo *delegate_cache,
1464 const char *xml,const char *filename,const size_t depth,
1465 ExceptionInfo *exception)
1468 keyword[MaxTextExtent],
1481 Load the delegate map file.
1483 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1484 "Loading delegate configuration file \"%s\" ...",filename);
1485 if (xml == (const char *) NULL)
1486 return(MagickFalse);
1488 delegate_info=(DelegateInfo *) NULL;
1489 token=AcquireString(xml);
1490 for (q=(const char *) xml; *q != '\0'; )
1495 GetMagickToken(q,&q,token);
1498 (void) CopyMagickString(keyword,token,MaxTextExtent);
1499 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1504 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1505 GetMagickToken(q,&q,token);
1508 if (LocaleNCompare(keyword,"<!--",4) == 0)
1513 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1514 GetMagickToken(q,&q,token);
1517 if (LocaleCompare(keyword,"<include") == 0)
1522 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1524 (void) CopyMagickString(keyword,token,MaxTextExtent);
1525 GetMagickToken(q,&q,token);
1528 GetMagickToken(q,&q,token);
1529 if (LocaleCompare(keyword,"file") == 0)
1532 (void) ThrowMagickException(exception,GetMagickModule(),
1533 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1537 path[MaxTextExtent],
1540 GetPathComponent(filename,HeadPath,path);
1542 (void) ConcatenateMagickString(path,DirectorySeparator,
1544 if (*token == *DirectorySeparator)
1545 (void) CopyMagickString(path,token,MaxTextExtent);
1547 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1548 xml=FileToXML(path,~0UL);
1549 if (xml != (char *) NULL)
1551 status&=LoadDelegateCache(delegate_cache,xml,path,
1553 xml=(char *) RelinquishMagickMemory(xml);
1560 if (LocaleCompare(keyword,"<delegate") == 0)
1565 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1566 sizeof(*delegate_info));
1567 if (delegate_info == (DelegateInfo *) NULL)
1568 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1569 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1570 delegate_info->path=ConstantString(filename);
1571 delegate_info->thread_support=MagickTrue;
1572 delegate_info->signature=MagickSignature;
1575 if (delegate_info == (DelegateInfo *) NULL)
1577 if (LocaleCompare(keyword,"/>") == 0)
1579 status=AppendValueToLinkedList(delegate_cache,delegate_info);
1580 if( IfMagickFalse(status) )
1581 (void) ThrowMagickException(exception,GetMagickModule(),
1582 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1583 delegate_info->commands);
1584 delegate_info=(DelegateInfo *) NULL;
1587 GetMagickToken(q,(const char **) NULL,token);
1590 GetMagickToken(q,&q,token);
1591 GetMagickToken(q,&q,token);
1597 if (LocaleCompare((char *) keyword,"command") == 0)
1602 commands=AcquireString(token);
1603 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1604 if (strchr(commands,'@') != (char *) NULL)
1607 path[MaxTextExtent];
1609 NTGhostscriptEXE(path,MaxTextExtent);
1610 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1612 (void) SubstituteString((char **) &commands,"\\","/");
1615 (void) SubstituteString((char **) &commands,"&","&");
1616 (void) SubstituteString((char **) &commands,""","\"");
1617 (void) SubstituteString((char **) &commands,">",">");
1618 (void) SubstituteString((char **) &commands,"<","<");
1619 delegate_info->commands=commands;
1627 if (LocaleCompare((char *) keyword,"decode") == 0)
1629 delegate_info->decode=ConstantString(token);
1630 delegate_info->mode=1;
1638 if (LocaleCompare((char *) keyword,"encode") == 0)
1640 delegate_info->encode=ConstantString(token);
1641 delegate_info->mode=(-1);
1649 if (LocaleCompare((char *) keyword,"mode") == 0)
1651 delegate_info->mode=1;
1652 if (LocaleCompare(token,"bi") == 0)
1653 delegate_info->mode=0;
1655 if (LocaleCompare(token,"encode") == 0)
1656 delegate_info->mode=(-1);
1664 if (LocaleCompare((char *) keyword,"spawn") == 0)
1666 delegate_info->spawn=IsStringTrue(token);
1669 if (LocaleCompare((char *) keyword,"stealth") == 0)
1671 delegate_info->stealth=IsStringTrue(token);
1679 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1681 delegate_info->thread_support=IsStringTrue(token);
1682 if (delegate_info->thread_support == MagickFalse)
1683 delegate_info->semaphore=AcquireSemaphoreInfo();
1692 token=(char *) RelinquishMagickMemory(token);
1693 return(status != 0 ? MagickTrue : MagickFalse);