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