]> granicus.if.org Git - imagemagick/blob - MagickCore/delegate.c
(no commit message)
[imagemagick] / MagickCore / delegate.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
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            %
10 %                                                                             %
11 %                                                                             %
12 %             MagickCore Methods to Read/Write/Invoke Delegates               %
13 %                                                                             %
14 %                             Software Design                                 %
15 %                                  Cristy                                     %
16 %                               October 1998                                  %
17 %                                                                             %
18 %                                                                             %
19 %  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
20 %  dedicated to making software imaging solutions freely available.           %
21 %                                                                             %
22 %  You may not use this file except in compliance with the License.  You may  %
23 %  obtain a copy of the License at                                            %
24 %                                                                             %
25 %    http://www.imagemagick.org/script/license.php                            %
26 %                                                                             %
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.                                             %
32 %                                                                             %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34 %
35 %  The Delegates methods associate a set of commands with a particular
36 %  image format.  ImageMagick uses delegates for formats it does not handle
37 %  directly.
38 %
39 %  Thanks to Bob Friesenhahn for the initial inspiration and design of the
40 %  delegates methods.
41 %
42 %
43 */
44 \f
45 /*
46   Include declarations.
47 */
48 #include "MagickCore/studio.h"
49 #include "MagickCore/property.h"
50 #include "MagickCore/blob.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/configure.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/delegate.h"
55 #include "MagickCore/delegate-private.h"
56 #include "MagickCore/exception.h"
57 #include "MagickCore/exception-private.h"
58 #include "MagickCore/hashmap.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/memory_.h"
61 #include "MagickCore/policy.h"
62 #include "MagickCore/resource_.h"
63 #include "MagickCore/semaphore.h"
64 #include "MagickCore/string_.h"
65 #include "MagickCore/token.h"
66 #include "MagickCore/utility.h"
67 #include "MagickCore/utility-private.h"
68 #include "MagickCore/xml-tree.h"
69 \f
70 /*
71   Define declarations.
72 */
73 #define DelegateFilename  "delegates.xml"
74 \f
75 /*
76   Declare delegate map.
77 */
78 static const char
79   *DelegateMap = (const char *)
80     "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
81     "<delegatemap>"
82     "  <delegate decode=\"autotrace\" stealth=\"True\" command=\"&quot;autotrace&quot; -output-format svg -output-file &quot;%o&quot; &quot;%i&quot;\"/>"
83     "  <delegate decode=\"avi:decode\" stealth=\"True\" command=\"&quot;mplayer&quot; &quot;%i&quot; -really-quiet -ao null -vo png:z=3\"/>"
84     "  <delegate decode=\"browse\" stealth=\"True\" spawn=\"True\" command=\"&quot;xdg-open&quot; http://www.imagemagick.org/; rm &quot;%i&quot;\"/>"
85     "  <delegate decode=\"cgm\" thread-support=\"False\" command=\"&quot;ralcgm&quot; -d ps -oC &lt; &quot;%i&quot; &gt; &quot;%o&quot; 2&gt; &quot;%u&quot;\"/>"
86     "  <delegate decode=\"dng:decode\" command=\"&quot;/usr/bin/ufraw-batch&quot; --silent --wb=camera --black-point=auto --exposure=auto --create-id=also --out-type=ppm16 &quot;--output=%u.pnm&quot; &quot;%i&quot;\"/>"
87     "  <delegate decode=\"edit\" stealth=\"True\" command=\"&quot;xterm&quot; -title &quot;Edit Image Comment&quot; -e vi &quot;%o&quot;\"/>"
88     "  <delegate decode=\"eps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
89     "  <delegate decode=\"eps\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
90     "  <delegate decode=\"fig\" command=\"&quot;fig2dev&quot; -L ps &quot;%i&quot; &quot;%o&quot;\"/>"
91     "  <delegate decode=\"gplt\" command=\"&quot;echo&quot; &quot;set size 1.25,0.62     set terminal postscript portrait color solid; set output &quot;%o&quot;; load &quot;%i&quot;&quot; &gt; &quot;%u&quot;;&quot;gnuplot&quot; &quot;%u&quot;\"/>"
92     "  <delegate decode=\"hpg\" command=\"&quot;hp2xx&quot; -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot;     mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
93     "  <delegate decode=\"hpgl\" command=\"&quot;hp2xx&quot; -q -m eps -f `basename &quot;%o&quot;` &quot;%i&quot;     mv -f `basename &quot;%o&quot;` &quot;%o&quot;\"/>"
94     "  <delegate decode=\"htm\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
95     "  <delegate decode=\"html\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
96     "  <delegate decode=\"https\" command=\"&quot;wget&quot; -q -O &quot;%o&quot; &quot;https:%M&quot;\"/>"
97     "  <delegate decode=\"ilbm\" command=\"&quot;ilbmtoppm&quot; &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
98     "  <delegate decode=\"man\" command=\"&quot;groff&quot; -man -Tps &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
99     "  <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; -v -1 -vframes %S -i &quot;%i&quot; -vcodec pam -an -f rawvideo -y &quot;%u.pam&quot; 2&gt; &quot;%Z&quot;\"/>"
100     "  <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\"&quot;ffmpeg&quot; -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i &quot;%M%%d.jpg&quot; &quot;%u.%m&quot; 2&gt; &quot;%Z&quot;\"/>"
101     "  <delegate decode=\"pcl:color\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ppmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
102     "  <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
103     "  <delegate decode=\"pcl:mono\" stealth=\"True\" command=\"&quot;pcl6&quot; -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pbmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;%s&quot;\"/>"
104     "  <delegate decode=\"pdf\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=epswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
105     "  <delegate decode=\"pdf\" encode=\"ps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
106     "  <delegate decode=\"pnm\" encode=\"ilbm\" mode=\"encode\" command=\"&quot;ppmtoilbm&quot; -24if &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
107     "  <delegate decode=\"pnm\" encode=\"launch\" mode=\"encode\" command=\"&quot;gimp&quot; &quot;%i&quot;\"/>"
108     "  <delegate decode=\"pov\" command=\"&quot;povray&quot; &quot;+i&quot;%i&quot;&quot; -D0 +o&quot;%o&quot; +fn%q +w%w +h%h +a -q9 -kfi&quot;%s&quot; -kff&quot;%n&quot;     &quot;convert&quot; -concatenate &quot;%o*.png&quot; &quot;%o&quot;\"/>"
109     "  <delegate decode=\"ps\" encode=\"eps\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=epswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
110     "  <delegate decode=\"ps\" encode=\"pdf\" mode=\"bi\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pdfwrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;\"/>"
111     "  <delegate decode=\"ps\" encode=\"print\" mode=\"encode\" command=\"lpr &quot;%i&quot;\"/>"
112     "  <delegate decode=\"ps:alpha\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
113     "  <delegate decode=\"ps:bbox\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bbox&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
114     "  <delegate decode=\"ps:cmyk\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=bmpsep8&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
115     "  <delegate decode=\"ps:color\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
116     "  <delegate decode=\"ps:mono\" stealth=\"True\" command=\"&quot;gs&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pnmraw&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;\"/>"
117     "  <delegate decode=\"rgba\" encode=\"rle\" mode=\"encode\" command=\"&quot;rawtorle&quot; -o &quot;%o&quot; -v &quot;%i&quot;\"/>"
118     "  <delegate decode=\"scan\" command=\"&quot;scanimage&quot; -d &quot;%i&quot; &gt; &quot;%o&quot;\"/>"
119     "  <delegate encode=\"show\" spawn=\"True\" command=\"&quot;display&quot; -immutable -delay 0 -window-group %g -title &quot;%l of %f&quot; &quot;temporary:%i&quot;\"/>"
120     "  <delegate decode=\"shtml\" command=\"&quot;html2ps&quot; -U -o &quot;%o&quot; &quot;%i&quot;\"/>"
121     "  <delegate decode=\"svg\" command=\"&quot;rsvg&quot; &quot;%i&quot; &quot;%o&quot;\"/>"
122     "  <delegate decode=\"txt\" encode=\"ps\" mode=\"bi\" command=\"&quot;enscript&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
123     "  <delegate encode=\"win\" stealth=\"True\" spawn=\"True\" command=\"&quot;display&quot; -immutable -delay 0 -window-group %g -title &quot;%l of %f&quot; &quot;temporary:%i&quot;\"/>"
124     "  <delegate decode=\"wmf\" command=\"&quot;wmf2eps&quot; -o &quot;%o&quot; &quot;%i&quot;\"/>"
125     "</delegatemap>";
126 \f
127 /*
128   Global declaractions.
129 */
130 static LinkedListInfo
131   *delegate_list = (LinkedListInfo *) NULL;
132
133 static SemaphoreInfo
134   *delegate_semaphore = (SemaphoreInfo *) NULL;
135
136 static volatile MagickBooleanType
137   instantiate_delegate = MagickFalse;
138 \f
139 /*
140   Forward declaractions.
141 */
142 static MagickBooleanType
143   InitializeDelegateList(ExceptionInfo *),
144   LoadDelegateLists(const char *,ExceptionInfo *);
145 \f
146 /*
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %                                                                             %
149 %                                                                             %
150 %                                                                             %
151 +   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                         %
152 %                                                                             %
153 %                                                                             %
154 %                                                                             %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 %
157 %  DelegateComponentGenesis() instantiates the delegate component.
158 %
159 %  The format of the DelegateComponentGenesis method is:
160 %
161 %      MagickBooleanType DelegateComponentGenesis(void)
162 %
163 */
164 MagickPrivate MagickBooleanType DelegateComponentGenesis(void)
165 {
166   AcquireSemaphoreInfo(&delegate_semaphore);
167   return(MagickTrue);
168 }
169 \f
170 /*
171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 %                                                                             %
173 %                                                                             %
174 %                                                                             %
175 %   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                         %
176 %                                                                             %
177 %                                                                             %
178 %                                                                             %
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180 %
181 %  DelegateComponentTerminus() destroys the delegate component.
182 %
183 %  The format of the DelegateComponentTerminus method is:
184 %
185 %      DelegateComponentTerminus(void)
186 %
187 */
188
189 static void *DestroyDelegate(void *delegate_info)
190 {
191   register DelegateInfo
192     *p;
193
194   p=(DelegateInfo *) delegate_info;
195   if (p->path != (char *) NULL)
196     p->path=DestroyString(p->path);
197   if (p->decode != (char *) NULL)
198     p->decode=DestroyString(p->decode);
199   if (p->encode != (char *) NULL)
200     p->encode=DestroyString(p->encode);
201   if (p->commands != (char *) NULL)
202     p->commands=DestroyString(p->commands);
203   p=(DelegateInfo *) RelinquishMagickMemory(p);
204   return((void *) NULL);
205 }
206
207 MagickPrivate void DelegateComponentTerminus(void)
208 {
209   if (delegate_semaphore == (SemaphoreInfo *) NULL)
210     AcquireSemaphoreInfo(&delegate_semaphore);
211   LockSemaphoreInfo(delegate_semaphore);
212   if (delegate_list != (LinkedListInfo *) NULL)
213     delegate_list=DestroyLinkedList(delegate_list,DestroyDelegate);
214   instantiate_delegate=MagickFalse;
215   UnlockSemaphoreInfo(delegate_semaphore);
216   DestroySemaphoreInfo(&delegate_semaphore);
217 }
218 \f
219 /*
220 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 %                                                                             %
222 %                                                                             %
223 %                                                                             %
224 %   G e t D e l e g a t e C o m m a n d                                       %
225 %                                                                             %
226 %                                                                             %
227 %                                                                             %
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 %
230 %  GetDelegateCommand() replaces any embedded formatting characters with the
231 %  appropriate image attribute and returns the resulting command.
232 %
233 %  The format of the GetDelegateCommand method is:
234 %
235 %      char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
236 %        const char *decode,const char *encode,ExceptionInfo *exception)
237 %
238 %  A description of each parameter follows:
239 %
240 %    o command: Method GetDelegateCommand returns the command associated
241 %      with specified delegate tag.
242 %
243 %    o image_info: the image info.
244 %
245 %    o image: the image.
246 %
247 %    o decode: Specifies the decode delegate we are searching for as a
248 %      character string.
249 %
250 %    o encode: Specifies the encode delegate we are searching for as a
251 %      character string.
252 %
253 %    o exception: return any errors or warnings in this structure.
254 %
255 */
256 MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
257   const char *decode,const char *encode,ExceptionInfo *exception)
258 {
259   char
260     *command,
261     **commands;
262
263   const DelegateInfo
264     *delegate_info;
265
266   register ssize_t
267     i;
268
269   assert(image_info != (ImageInfo *) NULL);
270   assert(image_info->signature == MagickSignature);
271   assert(image != (Image *) NULL);
272   assert(image->signature == MagickSignature);
273   if( IfMagickTrue(image->debug) )
274     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
275
276   delegate_info=GetDelegateInfo(decode,encode,exception);
277   if (delegate_info == (const DelegateInfo *) NULL)
278     {
279       (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
280         "NoTagFound","`%s'",decode ? decode : encode);
281       return((char *) NULL);
282     }
283   commands=StringToList(delegate_info->commands);
284   if (commands == (char **) NULL)
285     {
286       (void) ThrowMagickException(exception,GetMagickModule(),
287         ResourceLimitError,"MemoryAllocationFailed","`%s'",decode ? decode :
288         encode);
289       return((char *) NULL);
290     }
291   command=InterpretImageProperties((ImageInfo *) image_info,image,commands[0],
292     exception);
293   if (command == (char *) NULL)
294     (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
295       "MemoryAllocationFailed","`%s'",commands[0]);
296   /*
297     Relinquish resources.
298   */
299   for (i=0; commands[i] != (char *) NULL; i++)
300     commands[i]=DestroyString(commands[i]);
301   commands=(char **) RelinquishMagickMemory(commands);
302   return(command);
303 }
304 \f
305 /*
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 %                                                                             %
308 %                                                                             %
309 %                                                                             %
310 %   G e t D e l e g a t e C o m m a n d s                                     %
311 %                                                                             %
312 %                                                                             %
313 %                                                                             %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 %
316 %  GetDelegateCommands() returns the commands associated with a delegate.
317 %
318 %  The format of the GetDelegateCommands method is:
319 %
320 %      const char *GetDelegateCommands(const DelegateInfo *delegate_info)
321 %
322 %  A description of each parameter follows:
323 %
324 %    o delegate_info:  The delegate info.
325 %
326 */
327 MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
328 {
329   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
330
331   assert(delegate_info != (DelegateInfo *) NULL);
332   assert(delegate_info->signature == MagickSignature);
333   return(delegate_info->commands);
334 }
335 \f
336 /*
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 %                                                                             %
339 %                                                                             %
340 %                                                                             %
341 %   G e t D e l e g a t e I n f o                                             %
342 %                                                                             %
343 %                                                                             %
344 %                                                                             %
345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 %
347 %  GetDelegateInfo() returns any delegates associated with the specified tag.
348 %
349 %  The format of the GetDelegateInfo method is:
350 %
351 %      const DelegateInfo *GetDelegateInfo(const char *decode,
352 %        const char *encode,ExceptionInfo *exception)
353 %
354 %  A description of each parameter follows:
355 %
356 %    o decode: Specifies the decode delegate we are searching for as a
357 %      character string.
358 %
359 %    o encode: Specifies the encode delegate we are searching for as a
360 %      character string.
361 %
362 %    o exception: return any errors or warnings in this structure.
363 %
364 */
365 MagickExport const DelegateInfo *GetDelegateInfo(const char *decode,
366   const char *encode,ExceptionInfo *exception)
367 {
368   register const DelegateInfo
369     *p;
370
371   assert(exception != (ExceptionInfo *) NULL);
372   if ((delegate_list == (LinkedListInfo *) NULL) ||
373       (IfMagickFalse(instantiate_delegate)))
374     if( IfMagickFalse(InitializeDelegateList(exception)) )
375       return((const DelegateInfo *) NULL);
376   if ((delegate_list == (LinkedListInfo *) NULL) ||
377       (IfMagickTrue(IsLinkedListEmpty(delegate_list))))
378     return((const DelegateInfo *) NULL);
379   if ((LocaleCompare(decode,"*") == 0) && (LocaleCompare(encode,"*") == 0))
380     return((const DelegateInfo *) GetValueFromLinkedList(delegate_list,0));
381   /*
382     Search for named delegate.
383   */
384   LockSemaphoreInfo(delegate_semaphore);
385   ResetLinkedListIterator(delegate_list);
386   p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
387   while (p != (const DelegateInfo *) NULL)
388   {
389     if (p->mode > 0)
390       {
391         if (LocaleCompare(p->decode,decode) == 0)
392           break;
393         p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
394         continue;
395       }
396     if (p->mode < 0)
397       {
398         if (LocaleCompare(p->encode,encode) == 0)
399           break;
400         p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
401         continue;
402       }
403     if (LocaleCompare(decode,p->decode) == 0)
404       if (LocaleCompare(encode,p->encode) == 0)
405         break;
406     if (LocaleCompare(decode,"*") == 0)
407       if (LocaleCompare(encode,p->encode) == 0)
408         break;
409     if (LocaleCompare(decode,p->decode) == 0)
410       if (LocaleCompare(encode,"*") == 0)
411         break;
412     p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
413   }
414   if (p != (const DelegateInfo *) NULL)
415     (void) InsertValueInLinkedList(delegate_list,0,
416       RemoveElementByValueFromLinkedList(delegate_list,p));
417   UnlockSemaphoreInfo(delegate_semaphore);
418   return(p);
419 }
420 \f
421 /*
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 %                                                                             %
424 %                                                                             %
425 %                                                                             %
426 %   G e t D e l e g a t e I n f o L i s t                                     %
427 %                                                                             %
428 %                                                                             %
429 %                                                                             %
430 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 %
432 %  GetDelegateInfoList() returns any delegates that match the specified pattern.
433 %
434 %  The delegate of the GetDelegateInfoList function is:
435 %
436 %      const DelegateInfo **GetDelegateInfoList(const char *pattern,
437 %        size_t *number_delegates,ExceptionInfo *exception)
438 %
439 %  A description of each parameter follows:
440 %
441 %    o pattern: Specifies a pointer to a text string containing a pattern.
442 %
443 %    o number_delegates:  This integer returns the number of delegates in the
444 %      list.
445 %
446 %    o exception: return any errors or warnings in this structure.
447 %
448 */
449
450 #if defined(__cplusplus) || defined(c_plusplus)
451 extern "C" {
452 #endif
453
454 static int DelegateInfoCompare(const void *x,const void *y)
455 {
456   const DelegateInfo
457     **p,
458     **q;
459
460   p=(const DelegateInfo **) x,
461   q=(const DelegateInfo **) y;
462   if (LocaleCompare((*p)->path,(*q)->path) == 0)
463     {
464       if ((*p)->decode == (char *) NULL)
465         if (((*p)->encode != (char *) NULL) &&
466             ((*q)->encode != (char *) NULL))
467           return(strcmp((*p)->encode,(*q)->encode));
468       if (((*p)->decode != (char *) NULL) &&
469           ((*q)->decode != (char *) NULL))
470         return(strcmp((*p)->decode,(*q)->decode));
471     }
472   return(LocaleCompare((*p)->path,(*q)->path));
473 }
474
475 #if defined(__cplusplus) || defined(c_plusplus)
476 }
477 #endif
478
479 MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
480   size_t *number_delegates,ExceptionInfo *exception)
481 {
482   const DelegateInfo
483     **delegates;
484
485   register const DelegateInfo
486     *p;
487
488   register ssize_t
489     i;
490
491   /*
492     Allocate delegate list.
493   */
494   assert(number_delegates != (size_t *) NULL);
495   assert(pattern != (char *) NULL);
496   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
497
498   *number_delegates=0;
499   p=GetDelegateInfo("*","*",exception);
500   if (p == (const DelegateInfo *) NULL)
501     return((const DelegateInfo **) NULL);
502   delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
503     GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
504   if (delegates == (const DelegateInfo **) NULL)
505     return((const DelegateInfo **) NULL);
506   /*
507     Generate delegate list.
508   */
509   LockSemaphoreInfo(delegate_semaphore);
510   ResetLinkedListIterator(delegate_list);
511   p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
512   for (i=0; p != (const DelegateInfo *) NULL; )
513   {
514     if( IfMagickFalse(p->stealth) &&
515         ( IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) ||
516           IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse))) )
517       delegates[i++]=p;
518     p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
519   }
520   UnlockSemaphoreInfo(delegate_semaphore);
521   qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
522   delegates[i]=(DelegateInfo *) NULL;
523   *number_delegates=(size_t) i;
524   return(delegates);
525 }
526 \f
527 /*
528 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
529 %                                                                             %
530 %                                                                             %
531 %                                                                             %
532 %   G e t D e l e g a t e L i s t                                             %
533 %                                                                             %
534 %                                                                             %
535 %                                                                             %
536 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 %
538 %  GetDelegateList() returns any image format delegates that match the
539 %  specified  pattern.
540 %
541 %  The format of the GetDelegateList function is:
542 %
543 %      char **GetDelegateList(const char *pattern,
544 %        size_t *number_delegates,ExceptionInfo *exception)
545 %
546 %  A description of each parameter follows:
547 %
548 %    o pattern: Specifies a pointer to a text string containing a pattern.
549 %
550 %    o number_delegates:  This integer returns the number of delegates
551 %      in the list.
552 %
553 %    o exception: return any errors or warnings in this structure.
554 %
555 */
556
557 #if defined(__cplusplus) || defined(c_plusplus)
558 extern "C" {
559 #endif
560
561 static int DelegateCompare(const void *x,const void *y)
562 {
563   register const char
564     **p,
565     **q;
566
567   p=(const char **) x;
568   q=(const char **) y;
569   return(LocaleCompare(*p,*q));
570 }
571
572 #if defined(__cplusplus) || defined(c_plusplus)
573 }
574 #endif
575
576 MagickExport char **GetDelegateList(const char *pattern,
577   size_t *number_delegates,ExceptionInfo *exception)
578 {
579   char
580     **delegates;
581
582   register const DelegateInfo
583     *p;
584
585   register ssize_t
586     i;
587
588   /*
589     Allocate delegate list.
590   */
591   assert(pattern != (char *) NULL);
592   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
593
594   assert(number_delegates != (size_t *) NULL);
595   *number_delegates=0;
596   p=GetDelegateInfo("*","*",exception);
597   if (p == (const DelegateInfo *) NULL)
598     return((char **) NULL);
599   delegates=(char **) AcquireQuantumMemory((size_t)
600     GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
601   if (delegates == (char **) NULL)
602     return((char **) NULL);
603   LockSemaphoreInfo(delegate_semaphore);
604   ResetLinkedListIterator(delegate_list);
605   p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
606   for (i=0; p != (const DelegateInfo *) NULL; )
607   {
608     if( IfMagickFalse(p->stealth) &&
609         IfMagickTrue(GlobExpression(p->decode,pattern,MagickFalse)) )
610       delegates[i++]=ConstantString(p->decode);
611     if( IfMagickFalse(p->stealth) &&
612         IfMagickTrue(GlobExpression(p->encode,pattern,MagickFalse)) )
613       delegates[i++]=ConstantString(p->encode);
614     p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
615   }
616   UnlockSemaphoreInfo(delegate_semaphore);
617   qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
618   delegates[i]=(char *) NULL;
619   *number_delegates=(size_t) i;
620   return(delegates);
621 }
622 \f
623 /*
624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625 %                                                                             %
626 %                                                                             %
627 %                                                                             %
628 %   G e t D e l e g a t e M o d e                                             %
629 %                                                                             %
630 %                                                                             %
631 %                                                                             %
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 %
634 %  GetDelegateMode() returns the mode of the delegate.
635 %
636 %  The format of the GetDelegateMode method is:
637 %
638 %      ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
639 %
640 %  A description of each parameter follows:
641 %
642 %    o delegate_info:  The delegate info.
643 %
644 */
645 MagickExport ssize_t GetDelegateMode(const DelegateInfo *delegate_info)
646 {
647   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
648
649   assert(delegate_info != (DelegateInfo *) NULL);
650   assert(delegate_info->signature == MagickSignature);
651   return(delegate_info->mode);
652 }
653 \f
654 /*
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 %                                                                             %
657 %                                                                             %
658 %                                                                             %
659 +   G e t D e l e g a t e T h r e a d S u p p o r t                           %
660 %                                                                             %
661 %                                                                             %
662 %                                                                             %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 %
665 %  GetDelegateThreadSupport() returns MagickTrue if the delegate supports
666 %  threads.
667 %
668 %  The format of the GetDelegateThreadSupport method is:
669 %
670 %      MagickBooleanType GetDelegateThreadSupport(
671 %        const DelegateInfo *delegate_info)
672 %
673 %  A description of each parameter follows:
674 %
675 %    o delegate_info:  The delegate info.
676 %
677 */
678 MagickExport MagickBooleanType GetDelegateThreadSupport(
679   const DelegateInfo *delegate_info)
680 {
681   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
682
683   assert(delegate_info != (DelegateInfo *) NULL);
684   assert(delegate_info->signature == MagickSignature);
685   return(delegate_info->thread_support);
686 }
687 \f
688 /*
689 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690 %                                                                             %
691 %                                                                             %
692 %                                                                             %
693 +   I n i t i a l i z e D e l e g a t e L i s t                               %
694 %                                                                             %
695 %                                                                             %
696 %                                                                             %
697 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698 %
699 %  InitializeDelegateList() initializes the delegate list.
700 %
701 %  The format of the InitializeDelegateList method is:
702 %
703 %      MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
704 %
705 %  A description of each parameter follows.
706 %
707 %    o exception: return any errors or warnings in this structure.
708 %
709 */
710 static MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
711 {
712   if ((delegate_list == (LinkedListInfo *) NULL) &&
713       IfMagickFalse(instantiate_delegate))
714     {
715       if (delegate_semaphore == (SemaphoreInfo *) NULL)
716         AcquireSemaphoreInfo(&delegate_semaphore);
717       LockSemaphoreInfo(delegate_semaphore);
718       if ((delegate_list == (LinkedListInfo *) NULL) &&
719           IfMagickFalse(instantiate_delegate))
720         {
721           (void) LoadDelegateLists(DelegateFilename,exception);
722           instantiate_delegate=MagickTrue;
723         }
724       UnlockSemaphoreInfo(delegate_semaphore);
725     }
726   return(IsMagickNotNULL(delegate_list));
727 }
728 \f
729 /*
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 %                                                                             %
732 %                                                                             %
733 %                                                                             %
734 %   I n v o k e D e l e g a t e                                               %
735 %                                                                             %
736 %                                                                             %
737 %                                                                             %
738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
739 %
740 %  InvokeDelegate replaces any embedded formatting characters with the
741 %  appropriate image attribute and executes the resulting command.  MagickFalse
742 %  is returned if the commands execute with success otherwise MagickTrue.
743 %
744 %  The format of the InvokeDelegate method is:
745 %
746 %      MagickBooleanType InvokeDelegate(ImageInfo *image_info,Image *image,
747 %        const char *decode,const char *encode,ExceptionInfo *exception)
748 %
749 %  A description of each parameter follows:
750 %
751 %    o image_info: the imageInfo.
752 %
753 %    o image: the image.
754 %
755 %    o exception: return any errors or warnings in this structure.
756 %
757 */
758
759 static inline size_t MagickMin(const size_t x,const size_t y)
760 {
761   if (x < y)
762     return(x);
763   return(y);
764 }
765
766 static MagickBooleanType CopyDelegateFile(const char *source,
767   const char *destination)
768 {
769   int
770     destination_file,
771     source_file;
772
773   MagickBooleanType
774     status;
775
776   register size_t
777     i;
778
779   size_t
780     length,
781     quantum;
782
783   ssize_t
784     count;
785
786   struct stat
787     attributes;
788
789   unsigned char
790     *buffer;
791
792   /*
793     Copy source file to destination.
794   */
795   assert(source != (const char *) NULL);
796   assert(destination != (char *) NULL);
797   status=GetPathAttributes(destination,&attributes);
798   if( IfMagickTrue(status) && (attributes.st_size != 0))
799     return(MagickTrue);
800   destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
801   if (destination_file == -1)
802     return(MagickFalse);
803   source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
804   if (source_file == -1)
805     {
806       (void) close(destination_file);
807       return(MagickFalse);
808     }
809   quantum=(size_t) MagickMaxBufferExtent;
810   if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
811     quantum=MagickMin((size_t) attributes.st_size,MagickMaxBufferExtent);
812   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
813   if (buffer == (unsigned char *) NULL)
814     {
815       (void) close(source_file);
816       (void) close(destination_file);
817       return(MagickFalse);
818     }
819   length=0;
820   for (i=0; ; i+=count)
821   {
822     count=(ssize_t) read(source_file,buffer,quantum);
823     if (count <= 0)
824       break;
825     length=(size_t) count;
826     count=(ssize_t) write(destination_file,buffer,length);
827     if ((size_t) count != length)
828       break;
829   }
830   (void) close(destination_file);
831   (void) close(source_file);
832   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
833   return(IsMagickTrue(i!=0));
834 }
835
836 MagickExport MagickBooleanType InvokeDelegate(ImageInfo *image_info,
837   Image *image,const char *decode,const char *encode,ExceptionInfo *exception)
838 {
839   char
840     *command,
841     **commands,
842     input_filename[MaxTextExtent],
843     output_filename[MaxTextExtent];
844
845   const DelegateInfo
846     *delegate_info;
847
848   MagickBooleanType
849     status,
850     temporary;
851
852   register ssize_t
853     i;
854
855   PolicyRights
856     rights;
857
858   /*
859     Get delegate.
860   */
861   assert(image_info != (ImageInfo *) NULL);
862   assert(image_info->signature == MagickSignature);
863   assert(image != (Image *) NULL);
864   assert(image->signature == MagickSignature);
865   if( IfMagickTrue(image->debug) )
866     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
867
868   rights=ExecutePolicyRights;
869   if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,decode)) )
870     {
871       errno=EPERM;
872       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
873         "NotAuthorized","`%s'",decode);
874       return(MagickFalse);
875     }
876   if( IfMagickFalse(IsRightsAuthorized(DelegatePolicyDomain,rights,encode)) )
877     {
878       errno=EPERM;
879       (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
880         "NotAuthorized","`%s'",encode);
881       return(MagickFalse);
882     }
883   temporary=IsMagickTrue(*image->filename == '\0');
884   if( IfMagickTrue(temporary) )
885     if( IfMagickFalse(AcquireUniqueFilename(image->filename)) )
886       {
887         ThrowFileException(exception,FileOpenError,
888           "UnableToCreateTemporaryFile",image->filename);
889         return(MagickFalse);
890       }
891   delegate_info=GetDelegateInfo(decode,encode,exception);
892   if (delegate_info == (DelegateInfo *) NULL)
893     {
894       if( IfMagickTrue(temporary) )
895         (void) RelinquishUniqueFileResource(image->filename);
896       (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
897         "NoTagFound","`%s'",decode ? decode : encode);
898       return(MagickFalse);
899     }
900   if (*image_info->filename == '\0')
901     {
902       if( IfMagickFalse(AcquireUniqueFilename(image_info->filename)) )
903         {
904           if( IfMagickTrue(temporary) )
905             (void) RelinquishUniqueFileResource(image->filename);
906           ThrowFileException(exception,FileOpenError,
907             "UnableToCreateTemporaryFile",image_info->filename);
908           return(MagickFalse);
909         }
910       image_info->temporary=MagickTrue;
911     }
912   if ((delegate_info->mode != 0) && (((decode != (const char *) NULL) &&
913         (delegate_info->encode != (char *) NULL)) ||
914        ((encode != (const char *) NULL) &&
915         (delegate_info->decode != (char *) NULL))))
916     {
917       char
918         *magick;
919
920       ImageInfo
921         *clone_info;
922
923       register Image
924         *p;
925
926       /*
927         Delegate requires a particular image format.
928       */
929       if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
930         {
931           ThrowFileException(exception,FileOpenError,
932             "UnableToCreateTemporaryFile",image_info->unique);
933           return(MagickFalse);
934         }
935       if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
936         {
937           (void) RelinquishUniqueFileResource(image_info->unique);
938           ThrowFileException(exception,FileOpenError,
939             "UnableToCreateTemporaryFile",image_info->zero);
940           return(MagickFalse);
941         }
942       magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
943         delegate_info->encode : delegate_info->decode,exception);
944       if (magick == (char *) NULL)
945         {
946           (void) RelinquishUniqueFileResource(image_info->unique);
947           (void) RelinquishUniqueFileResource(image_info->zero);
948           if( IfMagickTrue(temporary) )
949             (void) RelinquishUniqueFileResource(image->filename);
950           (void) ThrowMagickException(exception,GetMagickModule(),
951             DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
952           return(MagickFalse);
953         }
954       LocaleUpper(magick);
955       clone_info=CloneImageInfo(image_info);
956       (void) CopyMagickString((char *) clone_info->magick,magick,
957         MaxTextExtent);
958       if (LocaleCompare(magick,"NULL") != 0)
959         (void) CopyMagickString(image->magick,magick,MaxTextExtent);
960       magick=DestroyString(magick);
961       (void) FormatLocaleString(clone_info->filename,MaxTextExtent,"%s:",
962         delegate_info->decode);
963       (void) SetImageInfo(clone_info,(unsigned int) GetImageListLength(image),
964         exception);
965       (void) CopyMagickString(clone_info->filename,image_info->filename,
966         MaxTextExtent);
967       (void) CopyMagickString(image_info->filename,image->filename,
968         MaxTextExtent);
969       for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
970       {
971         (void) FormatLocaleString(p->filename,MaxTextExtent,"%s:%s",
972           delegate_info->decode,clone_info->filename);
973         status=WriteImage(clone_info,p,exception);
974         if( IfMagickFalse(status) )
975           {
976             (void) RelinquishUniqueFileResource(image_info->unique);
977             (void) RelinquishUniqueFileResource(image_info->zero);
978             if( IfMagickTrue(temporary) )
979               (void) RelinquishUniqueFileResource(image->filename);
980             clone_info=DestroyImageInfo(clone_info);
981             (void) ThrowMagickException(exception,GetMagickModule(),
982               DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
983             return(MagickFalse);
984           }
985         if( IfMagickTrue(clone_info->adjoin) )
986           break;
987       }
988       (void) RelinquishUniqueFileResource(image_info->unique);
989       (void) RelinquishUniqueFileResource(image_info->zero);
990       clone_info=DestroyImageInfo(clone_info);
991     }
992   /*
993     Invoke delegate.
994   */
995   commands=StringToList(delegate_info->commands);
996   if (commands == (char **) NULL)
997     {
998       if( IfMagickTrue(temporary) )
999         (void) RelinquishUniqueFileResource(image->filename);
1000       (void) ThrowMagickException(exception,GetMagickModule(),
1001         ResourceLimitError,"MemoryAllocationFailed","`%s'",
1002         decode ? decode : encode);
1003       return(MagickFalse);
1004     }
1005   command=(char *) NULL;
1006   status=MagickFalse;
1007   (void) CopyMagickString(output_filename,image_info->filename,MaxTextExtent);
1008   (void) CopyMagickString(input_filename,image->filename,MaxTextExtent);
1009   for (i=0; commands[i] != (char *) NULL; i++)
1010   {
1011     status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1012     if( IfMagickFalse(AcquireUniqueFilename(image_info->unique)) )
1013       {
1014         ThrowFileException(exception,FileOpenError,
1015           "UnableToCreateTemporaryFile",image_info->unique);
1016         break;
1017       }
1018     if( IfMagickFalse(AcquireUniqueFilename(image_info->zero)) )
1019       {
1020         (void) RelinquishUniqueFileResource(image_info->unique);
1021         ThrowFileException(exception,FileOpenError,
1022           "UnableToCreateTemporaryFile",image_info->zero);
1023         break;
1024       }
1025     if (LocaleCompare(decode,"SCAN") != 0)
1026       {
1027         status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1028         if( IfMagickFalse(status) )
1029           {
1030             ThrowFileException(exception,FileOpenError,
1031               "UnableToCreateTemporaryFile",input_filename);
1032             break;
1033           }
1034       }
1035     status=MagickFalse;
1036     command=InterpretImageProperties(image_info,image,commands[i],exception);
1037     if (command != (char *) NULL)
1038       {
1039         /*
1040           Execute delegate.
1041         */
1042         status=IsMagickTrue(SystemCommand(delegate_info->spawn,
1043           image_info->verbose,command,exception) != 0);
1044         if (IfMagickTrue(delegate_info->spawn))
1045           {
1046             ssize_t
1047               count;
1048
1049             /*
1050               Wait for input file to 'disappear', or maximum 10 seconds.
1051             */
1052             count=100;
1053             while ((count-- > 0) && (access_utf8(image->filename,F_OK) == 0))
1054               (void) MagickDelay(100);  /* sleep 0.1 seconds */
1055           }
1056         command=DestroyString(command);
1057       }
1058     if (LocaleCompare(decode,"SCAN") != 0)
1059       {
1060         if( IfMagickFalse(CopyDelegateFile(image->filename,input_filename)) )
1061           (void) RelinquishUniqueFileResource(input_filename);
1062       }
1063     if( IfMagickFalse(CopyDelegateFile(image_info->filename,output_filename)) )
1064       (void) RelinquishUniqueFileResource(output_filename);
1065     if( IfMagickTrue(image_info->temporary) )
1066       (void) RelinquishUniqueFileResource(image_info->filename);
1067     (void) RelinquishUniqueFileResource(image_info->unique);
1068     (void) RelinquishUniqueFileResource(image_info->zero);
1069     (void) RelinquishUniqueFileResource(image_info->filename);
1070     (void) RelinquishUniqueFileResource(image->filename);
1071     if( IfMagickTrue(status) )
1072       {
1073         (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1074           "DelegateFailed","`%s'",commands[i]);
1075         break;
1076       }
1077     commands[i]=DestroyString(commands[i]);
1078   }
1079   (void) CopyMagickString(image_info->filename,output_filename,MaxTextExtent);
1080   (void) CopyMagickString(image->filename,input_filename,MaxTextExtent);
1081   /*
1082     Relinquish resources.
1083   */
1084   for ( ; commands[i] != (char *) NULL; i++)
1085     commands[i]=DestroyString(commands[i]);
1086   commands=(char **) RelinquishMagickMemory(commands);
1087   if( IfMagickTrue(temporary) )
1088     (void) RelinquishUniqueFileResource(image->filename);
1089   return(IsMagickFalse(status));
1090 }
1091 \f
1092 /*
1093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1094 %                                                                             %
1095 %                                                                             %
1096 %                                                                             %
1097 %  L i s t D e l e g a t e I n f o                                            %
1098 %                                                                             %
1099 %                                                                             %
1100 %                                                                             %
1101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1102 %
1103 %  ListDelegateInfo() lists the image formats to a file.
1104 %
1105 %  The format of the ListDelegateInfo method is:
1106 %
1107 %      MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1108 %
1109 %  A description of each parameter follows.
1110 %
1111 %    o file:  An pointer to a FILE.
1112 %
1113 %    o exception: return any errors or warnings in this structure.
1114 %
1115 */
1116 MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1117   ExceptionInfo *exception)
1118 {
1119   const DelegateInfo
1120     **delegate_info;
1121
1122   char
1123     **commands,
1124     delegate[MaxTextExtent];
1125
1126   const char
1127     *path;
1128
1129   register ssize_t
1130     i;
1131
1132   size_t
1133     number_delegates;
1134
1135   ssize_t
1136     j;
1137
1138   if (file == (const FILE *) NULL)
1139     file=stdout;
1140   delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1141   if (delegate_info == (const DelegateInfo **) NULL)
1142     return(MagickFalse);
1143   path=(const char *) NULL;
1144   for (i=0; i < (ssize_t) number_delegates; i++)
1145   {
1146     if( IfMagickTrue(delegate_info[i]->stealth) )
1147       continue;
1148     if ((path == (const char *) NULL) ||
1149         (LocaleCompare(path,delegate_info[i]->path) != 0))
1150       {
1151         if (delegate_info[i]->path != (char *) NULL)
1152           (void) FormatLocaleFile(file,"\nPath: %s\n\n",delegate_info[i]->path);
1153         (void) FormatLocaleFile(file,"Delegate                Command\n");
1154         (void) FormatLocaleFile(file,
1155           "-------------------------------------------------"
1156           "------------------------------\n");
1157       }
1158     path=delegate_info[i]->path;
1159     *delegate='\0';
1160     if (delegate_info[i]->encode != (char *) NULL)
1161       (void) CopyMagickString(delegate,delegate_info[i]->encode,MaxTextExtent);
1162     (void) ConcatenateMagickString(delegate,"        ",MaxTextExtent);
1163     delegate[8]='\0';
1164     commands=StringToList(delegate_info[i]->commands);
1165     if (commands == (char **) NULL)
1166       continue;
1167     (void) FormatLocaleFile(file,"%11s%c=%c%s  ",delegate_info[i]->decode ?
1168       delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1169       delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1170     StripString(commands[0]);
1171     (void) FormatLocaleFile(file,"\"%s\"\n",commands[0]);
1172     for (j=1; commands[j] != (char *) NULL; j++)
1173     {
1174       StripString(commands[j]);
1175       (void) FormatLocaleFile(file,"                     \"%s\"\n",commands[j]);
1176     }
1177     for (j=0; commands[j] != (char *) NULL; j++)
1178       commands[j]=DestroyString(commands[j]);
1179     commands=(char **) RelinquishMagickMemory(commands);
1180   }
1181   (void) fflush(file);
1182   delegate_info=(const DelegateInfo **)
1183     RelinquishMagickMemory((void *) delegate_info);
1184   return(MagickTrue);
1185 }
1186 \f
1187 /*
1188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1189 %                                                                             %
1190 %                                                                             %
1191 %                                                                             %
1192 +   L o a d D e l e g a t e L i s t                                           %
1193 %                                                                             %
1194 %                                                                             %
1195 %                                                                             %
1196 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1197 %
1198 %  LoadDelegateList() loads the delegate configuration file which provides a
1199 %  mapping between delegate attributes and a delegate name.
1200 %
1201 %  The format of the LoadDelegateList method is:
1202 %
1203 %      MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
1204 %        const size_t depth,ExceptionInfo *exception)
1205 %
1206 %  A description of each parameter follows:
1207 %
1208 %    o xml:  The delegate list in XML format.
1209 %
1210 %    o filename:  The delegate list filename.
1211 %
1212 %    o depth: depth of <include /> statements.
1213 %
1214 %    o exception: return any errors or warnings in this structure.
1215 %
1216 */
1217 static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
1218   const size_t depth,ExceptionInfo *exception)
1219 {
1220   char
1221     keyword[MaxTextExtent],
1222     *token;
1223
1224   const char
1225     *q;
1226
1227   DelegateInfo
1228     *delegate_info;
1229
1230   MagickBooleanType
1231     status;
1232
1233   /*
1234     Load the delegate map file.
1235   */
1236   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1237     "Loading delegate configuration file \"%s\" ...",filename);
1238   if (xml == (const char *) NULL)
1239     return(MagickFalse);
1240   if (delegate_list == (LinkedListInfo *) NULL)
1241     {
1242       delegate_list=NewLinkedList(0);
1243       if (delegate_list == (LinkedListInfo *) NULL)
1244         {
1245           ThrowFileException(exception,ResourceLimitError,
1246             "MemoryAllocationFailed",filename);
1247           return(MagickFalse);
1248         }
1249     }
1250   status=MagickTrue;
1251   delegate_info=(DelegateInfo *) NULL;
1252   token=AcquireString(xml);
1253   for (q=(const char *) xml; *q != '\0'; )
1254   {
1255     /*
1256       Interpret XML.
1257     */
1258     GetMagickToken(q,&q,token);
1259     if (*token == '\0')
1260       break;
1261     (void) CopyMagickString(keyword,token,MaxTextExtent);
1262     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1263       {
1264         /*
1265           Doctype element.
1266         */
1267         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1268           GetMagickToken(q,&q,token);
1269         continue;
1270       }
1271     if (LocaleNCompare(keyword,"<!--",4) == 0)
1272       {
1273         /*
1274           Comment element.
1275         */
1276         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1277           GetMagickToken(q,&q,token);
1278         continue;
1279       }
1280     if (LocaleCompare(keyword,"<include") == 0)
1281       {
1282         /*
1283           Include element.
1284         */
1285         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1286         {
1287           (void) CopyMagickString(keyword,token,MaxTextExtent);
1288           GetMagickToken(q,&q,token);
1289           if (*token != '=')
1290             continue;
1291           GetMagickToken(q,&q,token);
1292           if (LocaleCompare(keyword,"file") == 0)
1293             {
1294               if (depth > 200)
1295                 (void) ThrowMagickException(exception,GetMagickModule(),
1296                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1297               else
1298                 {
1299                   char
1300                     path[MaxTextExtent],
1301                     *xml;
1302
1303                   GetPathComponent(filename,HeadPath,path);
1304                   if (*path != '\0')
1305                     (void) ConcatenateMagickString(path,DirectorySeparator,
1306                       MaxTextExtent);
1307                   if (*token == *DirectorySeparator)
1308                     (void) CopyMagickString(path,token,MaxTextExtent);
1309                   else
1310                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
1311                   xml=FileToString(path,~0UL,exception);
1312                   if (xml != (char *) NULL)
1313                     {
1314                       status=LoadDelegateList(xml,path,depth+1,exception);
1315                       xml=(char *) RelinquishMagickMemory(xml);
1316                     }
1317                 }
1318             }
1319         }
1320         continue;
1321       }
1322     if (LocaleCompare(keyword,"<delegate") == 0)
1323       {
1324         /*
1325           Delegate element.
1326         */
1327         delegate_info=(DelegateInfo *) AcquireMagickMemory(
1328           sizeof(*delegate_info));
1329         if (delegate_info == (DelegateInfo *) NULL)
1330           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1331         (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1332         delegate_info->path=ConstantString(filename);
1333         delegate_info->signature=MagickSignature;
1334         continue;
1335       }
1336     if (delegate_info == (DelegateInfo *) NULL)
1337       continue;
1338     if (LocaleCompare(keyword,"/>") == 0)
1339       {
1340         status=AppendValueToLinkedList(delegate_list,delegate_info);
1341         if( IfMagickFalse(status) )
1342           (void) ThrowMagickException(exception,GetMagickModule(),
1343             ResourceLimitError,"MemoryAllocationFailed","`%s'",
1344             delegate_info->commands);
1345         delegate_info=(DelegateInfo *) NULL;
1346         continue;
1347       }
1348     GetMagickToken(q,(const char **) NULL,token);
1349     if (*token != '=')
1350       continue;
1351     GetMagickToken(q,&q,token);
1352     GetMagickToken(q,&q,token);
1353     switch (*keyword)
1354     {
1355       case 'C':
1356       case 'c':
1357       {
1358         if (LocaleCompare((char *) keyword,"command") == 0)
1359           {
1360             char
1361               *commands;
1362
1363             commands=AcquireString(token);
1364 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
1365             if (strchr(commands,'@') != (char *) NULL)
1366               {
1367                 char
1368                   path[MaxTextExtent];
1369
1370                 NTGhostscriptEXE(path,MaxTextExtent);
1371                 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1372                   path);
1373                 (void) SubstituteString((char **) &commands,"\\","/");
1374               }
1375 #endif
1376             (void) SubstituteString((char **) &commands,"&amp;","&");
1377             (void) SubstituteString((char **) &commands,"&quot;","\"");
1378             (void) SubstituteString((char **) &commands,"&gt;",">");
1379             (void) SubstituteString((char **) &commands,"&lt;","<");
1380             delegate_info->commands=commands;
1381             break;
1382           }
1383         break;
1384       }
1385       case 'D':
1386       case 'd':
1387       {
1388         if (LocaleCompare((char *) keyword,"decode") == 0)
1389           {
1390             delegate_info->decode=ConstantString(token);
1391             delegate_info->mode=1;
1392             break;
1393           }
1394         break;
1395       }
1396       case 'E':
1397       case 'e':
1398       {
1399         if (LocaleCompare((char *) keyword,"encode") == 0)
1400           {
1401             delegate_info->encode=ConstantString(token);
1402             delegate_info->mode=(-1);
1403             break;
1404           }
1405         break;
1406       }
1407       case 'M':
1408       case 'm':
1409       {
1410         if (LocaleCompare((char *) keyword,"mode") == 0)
1411           {
1412             delegate_info->mode=1;
1413             if (LocaleCompare(token,"bi") == 0)
1414               delegate_info->mode=0;
1415             else
1416               if (LocaleCompare(token,"encode") == 0)
1417                 delegate_info->mode=(-1);
1418             break;
1419           }
1420         break;
1421       }
1422       case 'S':
1423       case 's':
1424       {
1425         if (LocaleCompare((char *) keyword,"spawn") == 0)
1426           {
1427             delegate_info->spawn=IsStringTrue(token);
1428             break;
1429           }
1430         if (LocaleCompare((char *) keyword,"stealth") == 0)
1431           {
1432             delegate_info->stealth=IsStringTrue(token);
1433             break;
1434           }
1435         break;
1436       }
1437       case 'T':
1438       case 't':
1439       {
1440         if (LocaleCompare((char *) keyword,"thread-support") == 0)
1441           {
1442             delegate_info->thread_support=IsStringTrue(token);
1443             break;
1444           }
1445         break;
1446       }
1447       default:
1448         break;
1449     }
1450   }
1451   token=(char *) RelinquishMagickMemory(token);
1452   return(status);
1453 }
1454 \f
1455 /*
1456 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 %                                                                             %
1458 %                                                                             %
1459 %                                                                             %
1460 %  L o a d D e l e g a t e L i s t s                                          %
1461 %                                                                             %
1462 %                                                                             %
1463 %                                                                             %
1464 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 %
1466 %  LoadDelegateList() loads one or more delegate configuration file which
1467 %  provides a mapping between delegate attributes and a delegate name.
1468 %
1469 %  The format of the LoadDelegateLists method is:
1470 %
1471 %      MagickBooleanType LoadDelegateLists(const char *filename,
1472 %        ExceptionInfo *exception)
1473 %
1474 %  A description of each parameter follows:
1475 %
1476 %    o filename: the font file name.
1477 %
1478 %    o exception: return any errors or warnings in this structure.
1479 %
1480 */
1481 static MagickBooleanType LoadDelegateLists(const char *filename,
1482   ExceptionInfo *exception)
1483 {
1484 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1485   return(LoadDelegateList(DelegateMap,"built-in",0,exception));
1486 #else
1487   const StringInfo
1488     *option;
1489
1490   LinkedListInfo
1491     *options;
1492
1493   MagickStatusType
1494     status;
1495
1496   status=MagickFalse;
1497   options=GetConfigureOptions(filename,exception);
1498   option=(const StringInfo *) GetNextValueInLinkedList(options);
1499   while (option != (const StringInfo *) NULL)
1500   {
1501     status&=LoadDelegateList((const char *) GetStringInfoDatum(option),
1502       GetStringInfoPath(option),0,exception);
1503     option=(const StringInfo *) GetNextValueInLinkedList(options);
1504   }
1505   options=DestroyConfigureOptions(options);
1506   if ((delegate_list == (LinkedListInfo *) NULL) ||
1507       (IfMagickTrue(IsLinkedListEmpty(delegate_list))))
1508     status&=LoadDelegateList(DelegateMap,"built-in",0,exception);
1509   return(IsMagickTrue(status!=0));
1510 #endif
1511 }