1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3%                                                                             %
4%                                                                             %
5%                                                                             %
6%                 M   M   AAA    GGGG  IIIII   CCCC  K   K                    %
7%                 MM MM  A   A  G        I    C      K  K                     %
8%                 M M M  AAAAA  G GGG    I    C      KKK                      %
9%                 M   M  A   A  G   G    I    C      K  K                     %
10%                 M   M  A   A   GGGG  IIIII   CCCC  K   K                    %
11%                                                                             %
12%                                                                             %
13%       Perform "Magick" on Images via the Command Line Interface             %
14%                                                                             %
15%                             Dragon Computing                                %
16%                             Anthony Thyssen                                 %
17%                               January 2012                                  %
18%                                                                             %
19%                                                                             %
20%  Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization      %
21%  dedicated to making software imaging solutions freely available.           %
22%                                                                             %
23%  You may not use this file except in compliance with the License.  You may  %
24%  obtain a copy of the License at                                            %
25%                                                                             %
26%    http://www.imagemagick.org/script/license.php                            %
27%                                                                             %
28%  Unless required by applicable law or agreed to in writing, software        %
29%  distributed under the License is distributed on an "AS IS" BASIS,          %
30%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
31%  See the License for the specific language governing permissions and        %
32%  limitations under the License.                                             %
33%                                                                             %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%  Read CLI arguments, script files, and pipelines, to provide options that
37%  manipulate images from many different formats.
38%
39*/
40
41/*
42  Include declarations.
43*/
44#include "MagickWand/studio.h"
45#include "MagickWand/MagickWand.h"
46
47/*
48%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49%                                                                             %
50%                                                                             %
51%                                                                             %
52%  M a i n                                                                    %
53%                                                                             %
54%                                                                             %
55%                                                                             %
56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57%
58%
59*/
60
61static int MagickMain(int argc,char **argv)
62{
63#define MagickCommandSize(name,use_metadata,command) \
64  { (name), sizeof(name)-1, (use_metadata), (command) }
65
66  typedef struct _CommandInfo
67  {
68    const char
69      *client_name;
70
71    size_t
72      extent;
73
74    MagickBooleanType
75      use_metadata;
76
77    MagickCommand
78      command;
79  } CommandInfo;
80
81  const CommandInfo
82    MagickCommands[] =
83    {
84      MagickCommandSize("magick", MagickFalse, MagickImageCommand),
85      MagickCommandSize("convert", MagickFalse, ConvertImageCommand),
86      MagickCommandSize("composite", MagickFalse, CompositeImageCommand),
87      MagickCommandSize("identify", MagickTrue, IdentifyImageCommand),
88      MagickCommandSize("animate", MagickFalse, AnimateImageCommand),
89      MagickCommandSize("compare", MagickTrue, CompareImagesCommand),
90      MagickCommandSize("conjure", MagickFalse, ConjureImageCommand),
91      MagickCommandSize("display", MagickFalse, DisplayImageCommand),
92      MagickCommandSize("import", MagickFalse, ImportImageCommand),
93      MagickCommandSize("mogrify", MagickFalse, MogrifyImageCommand),
94      MagickCommandSize("montage", MagickFalse, MontageImageCommand),
95      MagickCommandSize("stream", MagickFalse, StreamImageCommand)
96    };
97
98  char
99    client_name[MagickPathExtent],
100    *metadata;
101
102  ExceptionInfo
103    *exception;
104
105  ImageInfo
106    *image_info;
107
108  int
109    exit_code,
110    offset;
111
112  MagickBooleanType
113    status;
114
115  register ssize_t
116    i;
117
118  MagickCoreGenesis(*argv,MagickTrue);
119  exception=AcquireExceptionInfo();
120  image_info=AcquireImageInfo();
121  GetPathComponent(argv[0],TailPath,client_name);
122  for (i=0; i < (ssize_t) (sizeof(MagickCommands)/sizeof(MagickCommands[0])); i++)
123  {
124    offset=LocaleNCompare(MagickCommands[i].client_name,client_name,
125      MagickCommands[i].extent);
126    if (offset == 0)
127      break;
128  }
129  i%=(sizeof(MagickCommands)/sizeof(MagickCommands[0]));
130  if ((i == 0) && (argc > 1))
131    {
132      for (i=1; i < (ssize_t) (sizeof(MagickCommands)/sizeof(MagickCommands[0])); i++)
133      {
134        offset=LocaleCompare(MagickCommands[i].client_name,argv[1]);
135        if (offset == 0)
136          {
137            argc--;
138            argv++;
139            break;
140          }
141      }
142      i%=(sizeof(MagickCommands)/sizeof(MagickCommands[0]));
143    }
144  metadata=(char *) NULL;
145  status=MagickCommandGenesis(image_info,MagickCommands[i].command,argc,argv,
146    MagickCommands[i].use_metadata ? &metadata : (char **) NULL,exception);
147  if (metadata != (char *) NULL)
148    {
149      (void) fputs(metadata,stdout);
150      metadata=DestroyString(metadata);
151    }
152  if (MagickCommands[i].command != CompareImagesCommand)
153    exit_code=status != MagickFalse ? 0 : 1;
154  else
155    {
156      if (status == MagickFalse)
157        exit_code=2;
158      else
159      {
160        const char
161          *option;
162
163        option=GetImageOption(image_info,"compare:dissimilar");
164        exit_code=IsStringTrue(option) ? 1 : 0;
165      }
166    }
167  image_info=DestroyImageInfo(image_info);
168  exception=DestroyExceptionInfo(exception);
169  MagickCoreTerminus();
170  return(exit_code);
171}
172
173#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__MINGW64__)
174int main(int argc,char **argv)
175{
176  return(MagickMain(argc,argv));
177}
178#else
179int wmain(int argc,wchar_t *argv[])
180{
181  char
182    **utf8;
183
184  int
185    status;
186
187  register int
188    i;
189
190  utf8=NTArgvToUTF8(argc,argv);
191  status=MagickMain(argc,utf8);
192  for (i=0; i < argc; i++)
193    utf8[i]=DestroyString(utf8[i]);
194  utf8=(char **) RelinquishMagickMemory(utf8);
195  return(status);
196}
197#endif
198