blob: 9706f2db7c556ae4de1f98cb58225ee81ad0285e [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% %
19% Copyright 1999-2009 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
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{
cristy18b17442009-10-25 18:36:48 +0000167 assert(delegate_semaphore == (SemaphoreInfo *) NULL);
168 delegate_semaphore=AllocateSemaphoreInfo();
cristyf34a1452009-10-24 22:29:27 +0000169 return(MagickTrue);
170}
171
172/*
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174% %
175% %
176% %
177% 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 %
178% %
179% %
180% %
181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182%
183% DelegateComponentTerminus() destroys the delegate component.
184%
185% The format of the DelegateComponentTerminus method is:
186%
187% DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000188%
189*/
190
191static void *DestroyDelegate(void *delegate_info)
192{
193 register DelegateInfo
194 *p;
195
196 p=(DelegateInfo *) delegate_info;
197 if (p->path != (char *) NULL)
198 p->path=DestroyString(p->path);
199 if (p->decode != (char *) NULL)
200 p->decode=DestroyString(p->decode);
201 if (p->encode != (char *) NULL)
202 p->encode=DestroyString(p->encode);
203 if (p->commands != (char *) NULL)
204 p->commands=DestroyString(p->commands);
205 p=(DelegateInfo *) RelinquishMagickMemory(p);
206 return((void *) NULL);
207}
208
209
cristyf34a1452009-10-24 22:29:27 +0000210MagickExport void DelegateComponentTerminus(void)
cristy3ed852e2009-09-05 21:47:34 +0000211{
cristy18b17442009-10-25 18:36:48 +0000212 if (delegate_semaphore == (SemaphoreInfo *) NULL)
213 AcquireSemaphoreInfo(&delegate_semaphore);
214 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000215 if (delegate_list != (LinkedListInfo *) NULL)
216 delegate_list=DestroyLinkedList(delegate_list,DestroyDelegate);
217 instantiate_delegate=MagickFalse;
cristy18b17442009-10-25 18:36:48 +0000218 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000219 DestroySemaphoreInfo(&delegate_semaphore);
220}
221
222/*
223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224% %
225% %
226% %
227% G e t D e l e g a t e C o m m a n d %
228% %
229% %
230% %
231%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
232%
233% GetDelegateCommand() replaces any embedded formatting characters with the
234% appropriate image attribute and returns the resulting command.
235%
236% The format of the GetDelegateCommand method is:
237%
238% char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
239% const char *decode,const char *encode,ExceptionInfo *exception)
240%
241% A description of each parameter follows:
242%
243% o command: Method GetDelegateCommand returns the command associated
244% with specified delegate tag.
245%
246% o image_info: the image info.
247%
248% o image: the image.
249%
250% o decode: Specifies the decode delegate we are searching for as a
251% character string.
252%
253% o encode: Specifies the encode delegate we are searching for as a
254% character string.
255%
256% o exception: return any errors or warnings in this structure.
257%
258*/
259MagickExport char *GetDelegateCommand(const ImageInfo *image_info,Image *image,
260 const char *decode,const char *encode,ExceptionInfo *exception)
261{
262 char
263 *command,
264 **commands;
265
266 const DelegateInfo
267 *delegate_info;
268
269 register long
270 i;
271
272 assert(image_info != (ImageInfo *) NULL);
273 assert(image_info->signature == MagickSignature);
274 assert(image != (Image *) NULL);
275 assert(image->signature == MagickSignature);
276 if (image->debug != MagickFalse)
277 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
278 delegate_info=GetDelegateInfo(decode,encode,exception);
279 if (delegate_info == (const DelegateInfo *) NULL)
280 {
281 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
282 "NoTagFound","`%s'",decode ? decode : encode);
283 return((char *) NULL);
284 }
285 commands=StringToList(delegate_info->commands);
286 if (commands == (char **) NULL)
287 {
288 (void) ThrowMagickException(exception,GetMagickModule(),
289 ResourceLimitError,"MemoryAllocationFailed","`%s'",
290 decode ? decode : encode);
291 return((char *) NULL);
292 }
293 command=InterpretImageProperties(image_info,image,commands[0]);
294 if (command == (char *) NULL)
295 (void) ThrowMagickException(exception,GetMagickModule(),ResourceLimitError,
296 "MemoryAllocationFailed","`%s'",commands[0]);
297 /*
298 Relinquish resources.
299 */
300 for (i=0; commands[i] != (char *) NULL; i++)
301 commands[i]=DestroyString(commands[i]);
302 commands=(char **) RelinquishMagickMemory(commands);
303 return(command);
304}
305
306/*
307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308% %
309% %
310% %
311% G e t D e l e g a t e C o m m a n d s %
312% %
313% %
314% %
315%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316%
317% GetDelegateCommands() returns the commands associated with a delegate.
318%
319% The format of the GetDelegateCommands method is:
320%
321% const char *GetDelegateCommands(const DelegateInfo *delegate_info)
322%
323% A description of each parameter follows:
324%
325% o delegate_info: The delegate info.
326%
327*/
328MagickExport const char *GetDelegateCommands(const DelegateInfo *delegate_info)
329{
330 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
331 assert(delegate_info != (DelegateInfo *) NULL);
332 assert(delegate_info->signature == MagickSignature);
333 return(delegate_info->commands);
334}
335
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*/
365MagickExport 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 (instantiate_delegate == MagickFalse))
374 if (InitializeDelegateList(exception) == MagickFalse)
375 return((const DelegateInfo *) NULL);
376 if ((delegate_list == (LinkedListInfo *) NULL) ||
377 (IsLinkedListEmpty(delegate_list) != MagickFalse))
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 */
cristy18b17442009-10-25 18:36:48 +0000384 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000385 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));
cristy18b17442009-10-25 18:36:48 +0000417 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000418 return(p);
419}
420
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% unsigned long *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)
451extern "C" {
452#endif
453
454static 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
479MagickExport const DelegateInfo **GetDelegateInfoList(const char *pattern,
480 unsigned long *number_delegates,ExceptionInfo *exception)
481{
482 const DelegateInfo
483 **delegates;
484
485 register const DelegateInfo
486 *p;
487
488 register long
489 i;
490
491 /*
492 Allocate delegate list.
493 */
494 assert(pattern != (char *) NULL);
495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
496 assert(number_delegates != (unsigned long *) NULL);
497 *number_delegates=0;
498 p=GetDelegateInfo("*","*",exception);
499 if (p == (const DelegateInfo *) NULL)
500 return((const DelegateInfo **) NULL);
501 delegates=(const DelegateInfo **) AcquireQuantumMemory((size_t)
502 GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
503 if (delegates == (const DelegateInfo **) NULL)
504 return((const DelegateInfo **) NULL);
505 /*
506 Generate delegate list.
507 */
cristy18b17442009-10-25 18:36:48 +0000508 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000509 ResetLinkedListIterator(delegate_list);
510 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
511 for (i=0; p != (const DelegateInfo *) NULL; )
512 {
513 if ((p->stealth == MagickFalse) &&
514 ((GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse) ||
515 (GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse)))
516 delegates[i++]=p;
517 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
518 }
cristy18b17442009-10-25 18:36:48 +0000519 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000520 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateInfoCompare);
521 delegates[i]=(DelegateInfo *) NULL;
522 *number_delegates=(unsigned long) i;
523 return(delegates);
524}
525
526/*
527%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528% %
529% %
530% %
531% G e t D e l e g a t e L i s t %
532% %
533% %
534% %
535%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
536%
537% GetDelegateList() returns any image format delegates that match the
538% specified pattern.
539%
540% The format of the GetDelegateList function is:
541%
542% char **GetDelegateList(const char *pattern,
543% unsigned long *number_delegates,ExceptionInfo *exception)
544%
545% A description of each parameter follows:
546%
547% o pattern: Specifies a pointer to a text string containing a pattern.
548%
549% o number_delegates: This integer returns the number of delegates
550% in the list.
551%
552% o exception: return any errors or warnings in this structure.
553%
554*/
555
556#if defined(__cplusplus) || defined(c_plusplus)
557extern "C" {
558#endif
559
560static int DelegateCompare(const void *x,const void *y)
561{
562 register const char
563 **p,
564 **q;
565
566 p=(const char **) x;
567 q=(const char **) y;
568 return(LocaleCompare(*p,*q));
569}
570
571#if defined(__cplusplus) || defined(c_plusplus)
572}
573#endif
574
575MagickExport char **GetDelegateList(const char *pattern,
576 unsigned long *number_delegates,ExceptionInfo *exception)
577{
578 char
579 **delegates;
580
581 register const DelegateInfo
582 *p;
583
584 register long
585 i;
586
587 /*
588 Allocate delegate list.
589 */
590 assert(pattern != (char *) NULL);
591 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
592 assert(number_delegates != (unsigned long *) NULL);
593 *number_delegates=0;
594 p=GetDelegateInfo("*","*",exception);
595 if (p == (const DelegateInfo *) NULL)
596 return((char **) NULL);
597 delegates=(char **) AcquireQuantumMemory((size_t)
598 GetNumberOfElementsInLinkedList(delegate_list)+1UL,sizeof(*delegates));
599 if (delegates == (char **) NULL)
600 return((char **) NULL);
cristy18b17442009-10-25 18:36:48 +0000601 LockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000602 ResetLinkedListIterator(delegate_list);
603 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
604 for (i=0; p != (const DelegateInfo *) NULL; )
605 {
606 if ((p->stealth == MagickFalse) &&
607 (GlobExpression(p->decode,pattern,MagickFalse) != MagickFalse))
608 delegates[i++]=ConstantString(p->decode);
609 if ((p->stealth == MagickFalse) &&
610 (GlobExpression(p->encode,pattern,MagickFalse) != MagickFalse))
611 delegates[i++]=ConstantString(p->encode);
612 p=(const DelegateInfo *) GetNextValueInLinkedList(delegate_list);
613 }
cristy18b17442009-10-25 18:36:48 +0000614 UnlockSemaphoreInfo(delegate_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000615 qsort((void *) delegates,(size_t) i,sizeof(*delegates),DelegateCompare);
616 delegates[i]=(char *) NULL;
617 *number_delegates=(unsigned long) i;
618 return(delegates);
619}
620
621/*
622%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
623% %
624% %
625% %
626% G e t D e l e g a t e M o d e %
627% %
628% %
629% %
630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631%
632% GetDelegateMode() returns the mode of the delegate.
633%
634% The format of the GetDelegateMode method is:
635%
636% long GetDelegateMode(const DelegateInfo *delegate_info)
637%
638% A description of each parameter follows:
639%
640% o delegate_info: The delegate info.
641%
642*/
643MagickExport long GetDelegateMode(const DelegateInfo *delegate_info)
644{
645 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
646 assert(delegate_info != (DelegateInfo *) NULL);
647 assert(delegate_info->signature == MagickSignature);
648 return(delegate_info->mode);
649}
650
651/*
652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
653% %
654% %
655% %
656+ G e t D e l e g a t e T h r e a d S u p p o r t %
657% %
658% %
659% %
660%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
661%
662% GetDelegateThreadSupport() returns MagickTrue if the delegate supports
663% threads.
664%
665% The format of the GetDelegateThreadSupport method is:
666%
667% MagickBooleanType GetDelegateThreadSupport(
668% const DelegateInfo *delegate_info)
669%
670% A description of each parameter follows:
671%
672% o delegate_info: The delegate info.
673%
674*/
675MagickExport MagickBooleanType GetDelegateThreadSupport(
676 const DelegateInfo *delegate_info)
677{
678 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
679 assert(delegate_info != (DelegateInfo *) NULL);
680 assert(delegate_info->signature == MagickSignature);
681 return(delegate_info->thread_support);
682}
683
684/*
685%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686% %
687% %
688% %
689+ I n i t i a l i z e D e l e g a t e L i s t %
690% %
691% %
692% %
693%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
694%
695% InitializeDelegateList() initializes the delegate list.
696%
697% The format of the InitializeDelegateList method is:
698%
699% MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
700%
701% A description of each parameter follows.
702%
703% o exception: return any errors or warnings in this structure.
704%
705*/
706static MagickBooleanType InitializeDelegateList(ExceptionInfo *exception)
707{
708 if ((delegate_list == (LinkedListInfo *) NULL) &&
709 (instantiate_delegate == MagickFalse))
710 {
711 AcquireSemaphoreInfo(&delegate_semaphore);
cristy18b17442009-10-25 18:36:48 +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 }
cristy18b17442009-10-25 18:36:48 +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 {
868 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
869 "NotAuthorized","`%s'",decode);
870 return(MagickFalse);
871 }
872 if (IsRightsAuthorized(DelegatePolicyDomain,rights,encode) == MagickFalse)
873 {
874 (void) ThrowMagickException(exception,GetMagickModule(),PolicyError,
875 "NotAuthorized","`%s'",encode);
876 return(MagickFalse);
877 }
878 temporary=(*image->filename == '\0') ? MagickTrue : MagickFalse;
879 if (temporary != MagickFalse)
880 if (AcquireUniqueFilename(image->filename) == MagickFalse)
881 {
882 ThrowFileException(exception,FileOpenError,
883 "UnableToCreateTemporaryFile",image->filename);
884 return(MagickFalse);
885 }
886 delegate_info=GetDelegateInfo(decode,encode,exception);
887 if (delegate_info == (DelegateInfo *) NULL)
888 {
889 if (temporary != MagickFalse)
890 (void) RelinquishUniqueFileResource(image->filename);
891 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
892 "NoTagFound","`%s'",decode ? decode : encode);
893 return(MagickFalse);
894 }
895 if (*image_info->filename == '\0')
896 {
897 if (AcquireUniqueFilename(image_info->filename) == MagickFalse)
898 {
899 if (temporary != MagickFalse)
900 (void) RelinquishUniqueFileResource(image->filename);
901 ThrowFileException(exception,FileOpenError,
902 "UnableToCreateTemporaryFile",image_info->filename);
903 return(MagickFalse);
904 }
905 image_info->temporary=MagickTrue;
906 }
907 if ((delegate_info->mode != 0) &&
908 (((decode != (const char *) NULL) &&
909 (delegate_info->encode != (char *) NULL)) ||
910 ((encode != (const char *) NULL) &&
911 (delegate_info->decode != (char *) NULL))))
912 {
913 char
914 *magick;
915
916 ImageInfo
917 *clone_info;
918
919 register Image
920 *p;
921
922 /*
923 Delegate requires a particular image format.
924 */
925 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
926 {
927 ThrowFileException(exception,FileOpenError,
928 "UnableToCreateTemporaryFile",image_info->unique);
929 return(MagickFalse);
930 }
931 if (AcquireUniqueFilename(image_info->zero) == MagickFalse)
932 {
933 (void) RelinquishUniqueFileResource(image_info->zero);
934 ThrowFileException(exception,FileOpenError,
935 "UnableToCreateTemporaryFile",image_info->zero);
936 return(MagickFalse);
937 }
938 magick=InterpretImageProperties(image_info,image,decode != (char *) NULL ?
939 delegate_info->encode : delegate_info->decode);
940 if (magick == (char *) NULL)
941 {
942 (void) RelinquishUniqueFileResource(image_info->unique);
943 (void) RelinquishUniqueFileResource(image_info->zero);
944 if (temporary != MagickFalse)
945 (void) RelinquishUniqueFileResource(image->filename);
946 (void) ThrowMagickException(exception,GetMagickModule(),
947 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
948 return(MagickFalse);
949 }
950 LocaleUpper(magick);
951 clone_info=CloneImageInfo(image_info);
952 (void) CopyMagickString((char *) clone_info->magick,magick,
953 MaxTextExtent);
954 if (LocaleCompare(magick,"NULL") != 0)
955 (void) CopyMagickString(image->magick,magick,MaxTextExtent);
956 magick=DestroyString(magick);
957 (void) FormatMagickString(clone_info->filename,MaxTextExtent,"%s:",
958 delegate_info->decode);
959 (void) SetImageInfo(clone_info,MagickTrue,exception);
960 (void) CopyMagickString(clone_info->filename,image_info->filename,
961 MaxTextExtent);
962 (void) CopyMagickString(image_info->filename,image->filename,
963 MaxTextExtent);
964 for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
965 {
966 (void) FormatMagickString(p->filename,MaxTextExtent,"%s:%s",
967 delegate_info->decode,clone_info->filename);
968 status=WriteImage(clone_info,p);
969 if (status == MagickFalse)
970 {
971 (void) RelinquishUniqueFileResource(image_info->unique);
972 (void) RelinquishUniqueFileResource(image_info->zero);
973 if (temporary != MagickFalse)
974 (void) RelinquishUniqueFileResource(image->filename);
975 clone_info=DestroyImageInfo(clone_info);
976 (void) ThrowMagickException(exception,GetMagickModule(),
977 DelegateError,"DelegateFailed","`%s'",decode ? decode : encode);
978 return(MagickFalse);
979 }
980 if (clone_info->adjoin != MagickFalse)
981 break;
982 }
983 (void) RelinquishUniqueFileResource(image_info->unique);
984 (void) RelinquishUniqueFileResource(image_info->zero);
985 clone_info=DestroyImageInfo(clone_info);
986 }
987 /*
988 Invoke delegate.
989 */
990 commands=StringToList(delegate_info->commands);
991 if (commands == (char **) NULL)
992 {
993 if (temporary != MagickFalse)
994 (void) RelinquishUniqueFileResource(image->filename);
995 (void) ThrowMagickException(exception,GetMagickModule(),
996 ResourceLimitError,"MemoryAllocationFailed","`%s'",
997 decode ? decode : encode);
998 return(MagickFalse);
999 }
1000 command=(char *) NULL;
1001 status=MagickFalse;
1002 (void) CopyMagickString(output_filename,image_info->filename,MaxTextExtent);
1003 (void) CopyMagickString(input_filename,image->filename,MaxTextExtent);
1004 for (i=0; commands[i] != (char *) NULL; i++)
1005 {
1006 status=AcquireUniqueSymbolicLink(output_filename,image_info->filename);
1007 if (AcquireUniqueFilename(image_info->unique) == MagickFalse)
1008 {
1009 ThrowFileException(exception,FileOpenError,
1010 "UnableToCreateTemporaryFile",image_info->unique);
1011 break;
1012 }
1013 if (AcquireUniqueFilename(image_info->zero) == MagickFalse)
1014 {
1015 (void) RelinquishUniqueFileResource(image_info->unique);
1016 ThrowFileException(exception,FileOpenError,
1017 "UnableToCreateTemporaryFile",image_info->zero);
1018 break;
1019 }
1020 if (LocaleCompare(decode,"SCAN") != 0)
1021 {
1022 status=AcquireUniqueSymbolicLink(input_filename,image->filename);
1023 if (status == MagickFalse)
1024 {
1025 ThrowFileException(exception,FileOpenError,
1026 "UnableToCreateTemporaryFile",input_filename);
1027 break;
1028 }
1029 }
1030 status=MagickFalse;
1031 command=InterpretImageProperties(image_info,image,commands[i]);
1032 if (command != (char *) NULL)
1033 {
1034 /*
1035 Execute delegate.
1036 */
1037 if (delegate_info->spawn != MagickFalse)
1038 (void) ConcatenateString(&command," &");
cristyb32b90a2009-09-07 21:45:48 +00001039 status=SystemCommand(image_info->verbose,command,exception) != 0 ?
1040 MagickTrue : MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001041 if (delegate_info->spawn != MagickFalse)
1042 (void) sleep(2);
1043 command=DestroyString(command);
1044 }
1045 if (LocaleCompare(decode,"SCAN") != 0)
1046 {
1047 if (CopyDelegateFile(image->filename,input_filename) == MagickFalse)
1048 (void) RelinquishUniqueFileResource(input_filename);
1049 }
1050 if (CopyDelegateFile(image_info->filename,output_filename) == MagickFalse)
1051 (void) RelinquishUniqueFileResource(output_filename);
1052 if (image_info->temporary != MagickFalse)
1053 (void) RelinquishUniqueFileResource(image_info->filename);
1054 (void) RelinquishUniqueFileResource(image_info->unique);
1055 (void) RelinquishUniqueFileResource(image_info->zero);
1056 (void) RelinquishUniqueFileResource(image_info->filename);
1057 (void) RelinquishUniqueFileResource(image->filename);
1058 if (status != MagickFalse)
1059 {
1060 (void) ThrowMagickException(exception,GetMagickModule(),DelegateError,
1061 "DelegateFailed","`%s'",commands[i]);
1062 break;
1063 }
1064 commands[i]=DestroyString(commands[i]);
1065 }
1066 (void) CopyMagickString(image_info->filename,output_filename,MaxTextExtent);
1067 (void) CopyMagickString(image->filename,input_filename,MaxTextExtent);
1068 /*
1069 Relinquish resources.
1070 */
1071 for ( ; commands[i] != (char *) NULL; i++)
1072 commands[i]=DestroyString(commands[i]);
1073 commands=(char **) RelinquishMagickMemory(commands);
1074 if (temporary != MagickFalse)
1075 (void) RelinquishUniqueFileResource(image->filename);
1076 return(status == MagickFalse ? MagickTrue : MagickFalse);
1077}
1078
1079/*
1080%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081% %
1082% %
1083% %
1084% L i s t D e l e g a t e I n f o %
1085% %
1086% %
1087% %
1088%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089%
1090% ListDelegateInfo() lists the image formats to a file.
1091%
1092% The format of the ListDelegateInfo method is:
1093%
1094% MagickBooleanType ListDelegateInfo(FILE *file,ExceptionInfo *exception)
1095%
1096% A description of each parameter follows.
1097%
1098% o file: An pointer to a FILE.
1099%
1100% o exception: return any errors or warnings in this structure.
1101%
1102*/
1103MagickExport MagickBooleanType ListDelegateInfo(FILE *file,
1104 ExceptionInfo *exception)
1105{
1106 const DelegateInfo
1107 **delegate_info;
1108
1109 char
1110 **commands,
1111 delegate[MaxTextExtent];
1112
1113 const char
1114 *path;
1115
1116 long
1117 j;
1118
1119 register long
1120 i;
1121
1122 unsigned long
1123 number_delegates;
1124
1125 if (file == (const FILE *) NULL)
1126 file=stdout;
1127 delegate_info=GetDelegateInfoList("*",&number_delegates,exception);
1128 if (delegate_info == (const DelegateInfo **) NULL)
1129 return(MagickFalse);
1130 path=(const char *) NULL;
1131 for (i=0; i < (long) number_delegates; i++)
1132 {
1133 if (delegate_info[i]->stealth != MagickFalse)
1134 continue;
1135 if ((path == (const char *) NULL) ||
1136 (LocaleCompare(path,delegate_info[i]->path) != 0))
1137 {
1138 if (delegate_info[i]->path != (char *) NULL)
1139 (void) fprintf(file,"\nPath: %s\n\n",delegate_info[i]->path);
1140 (void) fprintf(file,"Delegate Command\n");
1141 (void) fprintf(file,"-------------------------------------------------"
1142 "------------------------------\n");
1143 }
1144 path=delegate_info[i]->path;
1145 *delegate='\0';
1146 if (delegate_info[i]->encode != (char *) NULL)
1147 (void) CopyMagickString(delegate,delegate_info[i]->encode,MaxTextExtent);
1148 (void) ConcatenateMagickString(delegate," ",MaxTextExtent);
1149 delegate[8]='\0';
1150 commands=StringToList(delegate_info[i]->commands);
1151 if (commands == (char **) NULL)
1152 continue;
1153 (void) fprintf(file,"%11s%c=%c%s ",delegate_info[i]->decode ?
1154 delegate_info[i]->decode : "",delegate_info[i]->mode <= 0 ? '<' : ' ',
1155 delegate_info[i]->mode >= 0 ? '>' : ' ',delegate);
1156 StripString(commands[0]);
1157 (void) fprintf(file,"\"%s\"\n",commands[0]);
1158 for (j=1; commands[j] != (char *) NULL; j++)
1159 {
1160 StripString(commands[j]);
1161 (void) fprintf(file," \"%s\"\n",commands[j]);
1162 }
1163 for (j=0; commands[j] != (char *) NULL; j++)
1164 commands[j]=DestroyString(commands[j]);
1165 commands=(char **) RelinquishMagickMemory(commands);
1166 }
1167 (void) fflush(file);
1168 delegate_info=(const DelegateInfo **)
1169 RelinquishMagickMemory((void *) delegate_info);
1170 return(MagickTrue);
1171}
1172
1173/*
1174%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175% %
1176% %
1177% %
1178+ L o a d D e l e g a t e L i s t %
1179% %
1180% %
1181% %
1182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183%
1184% LoadDelegateList() loads the delegate configuration file which provides a
1185% mapping between delegate attributes and a delegate name.
1186%
1187% The format of the LoadDelegateList method is:
1188%
1189% MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
1190% const unsigned long depth,ExceptionInfo *exception)
1191%
1192% A description of each parameter follows:
1193%
1194% o xml: The delegate list in XML format.
1195%
1196% o filename: The delegate list filename.
1197%
1198% o depth: depth of <include /> statements.
1199%
1200% o exception: return any errors or warnings in this structure.
1201%
1202*/
1203static MagickBooleanType LoadDelegateList(const char *xml,const char *filename,
1204 const unsigned long depth,ExceptionInfo *exception)
1205{
1206 char
1207 keyword[MaxTextExtent],
1208 *token;
1209
1210 const char
1211 *q;
1212
1213 DelegateInfo
1214 *delegate_info;
1215
1216 MagickBooleanType
1217 status;
1218
1219 /*
1220 Load the delegate map file.
1221 */
1222 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1223 "Loading delegate configuration file \"%s\" ...",filename);
1224 if (xml == (const char *) NULL)
1225 return(MagickFalse);
1226 if (delegate_list == (LinkedListInfo *) NULL)
1227 {
1228 delegate_list=NewLinkedList(0);
1229 if (delegate_list == (LinkedListInfo *) NULL)
1230 {
1231 ThrowFileException(exception,ResourceLimitError,
1232 "MemoryAllocationFailed",filename);
1233 return(MagickFalse);
1234 }
1235 }
1236 status=MagickTrue;
1237 delegate_info=(DelegateInfo *) NULL;
1238 token=AcquireString(xml);
1239 for (q=(const char *) xml; *q != '\0'; )
1240 {
1241 /*
1242 Interpret XML.
1243 */
1244 GetMagickToken(q,&q,token);
1245 if (*token == '\0')
1246 break;
1247 (void) CopyMagickString(keyword,token,MaxTextExtent);
1248 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1249 {
1250 /*
1251 Doctype element.
1252 */
1253 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1254 GetMagickToken(q,&q,token);
1255 continue;
1256 }
1257 if (LocaleNCompare(keyword,"<!--",4) == 0)
1258 {
1259 /*
1260 Comment element.
1261 */
1262 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1263 GetMagickToken(q,&q,token);
1264 continue;
1265 }
1266 if (LocaleCompare(keyword,"<include") == 0)
1267 {
1268 /*
1269 Include element.
1270 */
1271 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1272 {
1273 (void) CopyMagickString(keyword,token,MaxTextExtent);
1274 GetMagickToken(q,&q,token);
1275 if (*token != '=')
1276 continue;
1277 GetMagickToken(q,&q,token);
1278 if (LocaleCompare(keyword,"file") == 0)
1279 {
1280 if (depth > 200)
1281 (void) ThrowMagickException(exception,GetMagickModule(),
1282 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1283 else
1284 {
1285 char
1286 path[MaxTextExtent],
1287 *xml;
1288
1289 GetPathComponent(filename,HeadPath,path);
1290 if (*path != '\0')
1291 (void) ConcatenateMagickString(path,DirectorySeparator,
1292 MaxTextExtent);
1293 if (*token == *DirectorySeparator)
1294 (void) CopyMagickString(path,token,MaxTextExtent);
1295 else
1296 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1297 xml=FileToString(path,~0,exception);
1298 if (xml != (char *) NULL)
1299 {
1300 status=LoadDelegateList(xml,path,depth+1,exception);
1301 xml=(char *) RelinquishMagickMemory(xml);
1302 }
1303 }
1304 }
1305 }
1306 continue;
1307 }
1308 if (LocaleCompare(keyword,"<delegate") == 0)
1309 {
1310 /*
1311 Delegate element.
1312 */
1313 delegate_info=(DelegateInfo *) AcquireMagickMemory(
1314 sizeof(*delegate_info));
1315 if (delegate_info == (DelegateInfo *) NULL)
1316 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1317 (void) ResetMagickMemory(delegate_info,0,sizeof(*delegate_info));
1318 delegate_info->path=ConstantString(filename);
1319 delegate_info->signature=MagickSignature;
1320 continue;
1321 }
1322 if (delegate_info == (DelegateInfo *) NULL)
1323 continue;
1324 if (LocaleCompare(keyword,"/>") == 0)
1325 {
1326 status=AppendValueToLinkedList(delegate_list,delegate_info);
1327 if (status == MagickFalse)
1328 (void) ThrowMagickException(exception,GetMagickModule(),
1329 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1330 delegate_info->commands);
1331 delegate_info=(DelegateInfo *) NULL;
1332 }
1333 GetMagickToken(q,(const char **) NULL,token);
1334 if (*token != '=')
1335 continue;
1336 GetMagickToken(q,&q,token);
1337 GetMagickToken(q,&q,token);
1338 switch (*keyword)
1339 {
1340 case 'C':
1341 case 'c':
1342 {
1343 if (LocaleCompare((char *) keyword,"command") == 0)
1344 {
1345 char
1346 *commands;
1347
1348 commands=AcquireString(token);
1349#if defined(__WINDOWS__)
1350 if (strchr(commands,'@') != (char *) NULL)
1351 {
1352 char
1353 path[MaxTextExtent];
1354
1355 NTGhostscriptEXE(path,MaxTextExtent);
1356 (void) SubstituteString((char **) &commands,"@PSDelegate@",
1357 path);
1358 (void) SubstituteString((char **) &commands,"\\","/");
1359 }
1360#endif
1361 (void) SubstituteString((char **) &commands,"&amp;","&");
1362 (void) SubstituteString((char **) &commands,"&quot;","\"");
1363 (void) SubstituteString((char **) &commands,"&gt;",">");
1364 (void) SubstituteString((char **) &commands,"&lt;","<");
1365 delegate_info->commands=commands;
1366 break;
1367 }
1368 break;
1369 }
1370 case 'D':
1371 case 'd':
1372 {
1373 if (LocaleCompare((char *) keyword,"decode") == 0)
1374 {
1375 delegate_info->decode=ConstantString(token);
1376 delegate_info->mode=1;
1377 break;
1378 }
1379 break;
1380 }
1381 case 'E':
1382 case 'e':
1383 {
1384 if (LocaleCompare((char *) keyword,"encode") == 0)
1385 {
1386 delegate_info->encode=ConstantString(token);
1387 delegate_info->mode=(-1);
1388 break;
1389 }
1390 break;
1391 }
1392 case 'M':
1393 case 'm':
1394 {
1395 if (LocaleCompare((char *) keyword,"mode") == 0)
1396 {
1397 delegate_info->mode=1;
1398 if (LocaleCompare(token,"bi") == 0)
1399 delegate_info->mode=0;
1400 else
1401 if (LocaleCompare(token,"encode") == 0)
1402 delegate_info->mode=(-1);
1403 break;
1404 }
1405 break;
1406 }
1407 case 'S':
1408 case 's':
1409 {
1410 if (LocaleCompare((char *) keyword,"spawn") == 0)
1411 {
1412 delegate_info->spawn=IsMagickTrue(token);
1413 break;
1414 }
1415 if (LocaleCompare((char *) keyword,"stealth") == 0)
1416 {
1417 delegate_info->stealth=IsMagickTrue(token);
1418 break;
1419 }
1420 break;
1421 }
1422 case 'T':
1423 case 't':
1424 {
1425 if (LocaleCompare((char *) keyword,"thread-support") == 0)
1426 {
1427 delegate_info->thread_support=IsMagickTrue(token);
1428 break;
1429 }
1430 break;
1431 }
1432 default:
1433 break;
1434 }
1435 }
1436 token=(char *) RelinquishMagickMemory(token);
1437 return(status);
1438}
1439
1440/*
1441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1442% %
1443% %
1444% %
1445% L o a d D e l e g a t e L i s t s %
1446% %
1447% %
1448% %
1449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450%
1451% LoadDelegateList() loads one or more delegate configuration file which
1452% provides a mapping between delegate attributes and a delegate name.
1453%
1454% The format of the LoadDelegateLists method is:
1455%
1456% MagickBooleanType LoadDelegateLists(const char *filename,
1457% ExceptionInfo *exception)
1458%
1459% A description of each parameter follows:
1460%
1461% o filename: the font file name.
1462%
1463% o exception: return any errors or warnings in this structure.
1464%
1465*/
1466static MagickBooleanType LoadDelegateLists(const char *filename,
1467 ExceptionInfo *exception)
1468{
1469#if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1470 return(LoadDelegateList(DelegateMap,"built-in",0,exception));
1471#else
1472 const StringInfo
1473 *option;
1474
1475 LinkedListInfo
1476 *options;
1477
1478 MagickStatusType
1479 status;
1480
1481 status=MagickFalse;
1482 options=GetConfigureOptions(filename,exception);
1483 option=(const StringInfo *) GetNextValueInLinkedList(options);
1484 while (option != (const StringInfo *) NULL)
1485 {
1486 status|=LoadDelegateList((const char *) GetStringInfoDatum(option),
1487 GetStringInfoPath(option),0,exception);
1488 option=(const StringInfo *) GetNextValueInLinkedList(options);
1489 }
1490 options=DestroyConfigureOptions(options);
1491 if ((delegate_list == (LinkedListInfo *) NULL) ||
1492 (IsLinkedListEmpty(delegate_list) != MagickFalse))
1493 status|=LoadDelegateList(DelegateMap,"built-in",0,exception);
1494 return(status != 0 ? MagickTrue : MagickFalse);
1495#endif
1496}