anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 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 | % CCCC L IIIII % |
| 13 | % C L I % |
| 14 | % C L I % |
| 15 | % C L I % |
| 16 | % CCCC LLLLL IIIII % |
| 17 | % % |
| 18 | % Perform "Magick" on Images via the Command Line Interface % |
| 19 | % % |
| 20 | % Dragon Computing % |
| 21 | % Anthony Thyssen % |
| 22 | % January 2012 % |
| 23 | % % |
| 24 | % % |
| 25 | % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization % |
| 26 | % dedicated to making software imaging solutions freely available. % |
| 27 | % % |
| 28 | % You may not use this file except in compliance with the License. You may % |
| 29 | % obtain a copy of the License at % |
| 30 | % % |
| 31 | % http://www.imagemagick.org/script/license.php % |
| 32 | % % |
| 33 | % Unless required by applicable law or agreed to in writing, software % |
| 34 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 35 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 36 | % See the License for the specific language governing permissions and % |
| 37 | % limitations under the License. % |
| 38 | % % |
| 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 40 | % |
| 41 | % Read CLI arguments, script files, and pipelines, to provide options that |
| 42 | % manipulate images from many different formats. |
| 43 | % |
| 44 | */ |
| 45 | |
| 46 | /* |
| 47 | Include declarations. |
| 48 | */ |
| 49 | #include "MagickWand/studio.h" |
| 50 | #include "MagickWand/MagickWand.h" |
| 51 | #include "MagickWand/magick-wand-private.h" |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 52 | #include "MagickWand/operation.h" |
anthony | 2052d27 | 2012-02-28 12:48:29 +0000 | [diff] [blame] | 53 | #include "MagickWand/operation-private.h" |
| 54 | #include "MagickWand/magick-cli.h" |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 55 | #include "MagickWand/script-token.h" |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 56 | #include "MagickCore/utility-private.h" |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 57 | #include "MagickCore/version.h" |
| 58 | |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 59 | /* verbose debugging, |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 60 | 3 - option type details |
| 61 | 9 - output options/artifacts/propertys |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 62 | */ |
anthony | ce51b4a | 2012-04-06 02:32:25 +0000 | [diff] [blame^] | 63 | #define MagickCommandDebug 0 |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 64 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 65 | #define ThrowFileException(exception,severity,tag,context) \ |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 66 | { \ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 67 | char \ |
| 68 | *message; \ |
| 69 | \ |
| 70 | message=GetExceptionMessage(errno); \ |
| 71 | (void) ThrowMagickException(exception,GetMagickModule(),severity, \ |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 72 | tag == (const char *) NULL ? "unknown" : tag,"'%s': %s",context,message); \ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 73 | message=DestroyString(message); \ |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 74 | } |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 75 | |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 76 | #if MagickCommandDebug >= 9 |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 77 | /* |
| 78 | Temporary Debugging Information |
| 79 | FUTURE: these should be able to be printed out using 'percent escapes' |
| 80 | Actually 'Properities' can already be output with "%[*]" |
| 81 | */ |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 82 | static void OutputOptions(ImageInfo *image_info) |
| 83 | { |
| 84 | const char |
| 85 | *option, |
| 86 | *value; |
| 87 | |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 88 | (void) FormatLocaleFile(stdout," Global Options:\n"); |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 89 | ResetImageOptionIterator(image_info); |
| 90 | while ((option=GetNextImageOption(image_info)) != (const char *) NULL ) { |
| 91 | (void) FormatLocaleFile(stdout," %s: ",option); |
| 92 | value=GetImageOption(image_info,option); |
| 93 | if (value != (const char *) NULL) |
| 94 | (void) FormatLocaleFile(stdout,"%s\n",value); |
| 95 | } |
| 96 | ResetImageOptionIterator(image_info); |
| 97 | } |
| 98 | |
| 99 | static void OutputArtifacts(Image *image) |
| 100 | { |
| 101 | const char |
| 102 | *artifact, |
| 103 | *value; |
| 104 | |
| 105 | (void) FormatLocaleFile(stdout," Image Artifacts:\n"); |
| 106 | ResetImageArtifactIterator(image); |
| 107 | while ((artifact=GetNextImageArtifact(image)) != (const char *) NULL ) { |
| 108 | (void) FormatLocaleFile(stdout," %s: ",artifact); |
| 109 | value=GetImageArtifact(image,artifact); |
| 110 | if (value != (const char *) NULL) |
| 111 | (void) FormatLocaleFile(stdout,"%s\n",value); |
| 112 | } |
| 113 | ResetImageArtifactIterator(image); |
| 114 | } |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 115 | |
| 116 | static void OutputProperties(Image *image,ExceptionInfo *exception) |
| 117 | { |
| 118 | const char |
| 119 | *property, |
| 120 | *value; |
| 121 | |
| 122 | (void) FormatLocaleFile(stdout," Image Properity:\n"); |
| 123 | ResetImagePropertyIterator(image); |
| 124 | while ((property=GetNextImageProperty(image)) != (const char *) NULL ) { |
| 125 | (void) FormatLocaleFile(stdout," %s: ",property); |
| 126 | value=GetImageProperty(image,property,exception); |
| 127 | if (value != (const char *) NULL) |
| 128 | (void) FormatLocaleFile(stdout,"%s\n",value); |
| 129 | } |
| 130 | ResetImagePropertyIterator(image); |
| 131 | } |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 137 | % % |
| 138 | % % |
| 139 | % % |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 140 | + P r o c e s s S c r i p t O p t i o n s % |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 141 | % % |
| 142 | % % |
| 143 | % % |
| 144 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 145 | % |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 146 | % ProcessScriptOptions() reads options and processes options as they are |
| 147 | % found in the given file, or pipeline. The filename to open and read |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 148 | % options is given as the 'index' argument of the argument array given. |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 149 | % |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 150 | % Other arguments following index may be read by special script options |
| 151 | % as settings (strings), images, or as operations to be processed in various |
| 152 | % ways. How they are treated is up to the script being processed. |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 153 | % |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 154 | % Note that a script not 'return' to the command line processing, nor can |
| 155 | % they call (and return from) other scripts. At least not at this time. |
| 156 | % |
| 157 | % There are no 'ProcessOptionFlags' control flags at this time. |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 158 | % |
| 159 | % The format of the ProcessScriptOptions method is: |
| 160 | % |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 161 | % void ProcessScriptOptions(MagickCLI *cli_wand,int argc,char **argv, |
| 162 | % int index) |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 163 | % |
| 164 | % A description of each parameter follows: |
| 165 | % |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 166 | % o cli_wand: the main CLI Wand to use. |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 167 | % |
| 168 | % o argc: the number of elements in the argument vector. |
| 169 | % |
| 170 | % o argv: A text array containing the command line arguments. |
| 171 | % |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 172 | % o index: offset for argc to CLI argumnet count |
| 173 | % |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 174 | */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 175 | WandExport void ProcessScriptOptions(MagickCLI *cli_wand,int argc,char **argv, |
| 176 | int index) |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 177 | { |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 178 | ScriptTokenInfo |
| 179 | *token_info; |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 180 | |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 181 | CommandOptionFlags |
| 182 | option_type; |
| 183 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 184 | int |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 185 | count; |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 186 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 187 | char |
| 188 | *option, |
| 189 | *arg1, |
| 190 | *arg2; |
| 191 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 192 | assert(argc>index); /* at least one argument - script name */ |
| 193 | assert(argv != (char **)NULL); |
| 194 | assert(argv[index] != (char *)NULL); |
| 195 | assert(argv[argc-1] != (char *)NULL); |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 196 | assert(cli_wand != (MagickCLI *) NULL); |
| 197 | assert(cli_wand->signature == WandSignature); |
| 198 | if (cli_wand->wand.debug != MagickFalse) |
| 199 | (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",cli_wand->wand.name); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 200 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 201 | /* open file script or stream, and set up tokenizer */ |
| 202 | token_info = AcquireScriptTokenInfo(argv[index]); |
anthony | 8ac75b5 | 2012-03-18 11:56:44 +0000 | [diff] [blame] | 203 | if (token_info == (ScriptTokenInfo *) NULL) { |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 204 | ThrowFileException(cli_wand->wand.exception,OptionFatalError, |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 205 | "UnableToOpenScript",argv[index]); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 208 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 209 | /* define the error location string for use in exceptions |
anthony | 92c93bd | 2012-03-19 14:02:47 +0000 | [diff] [blame] | 210 | order of input escapes: option, (arg), filename, line, column */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 211 | cli_wand->location="'%s' in \"%s\" line %u column %u"; |
anthony | 92c93bd | 2012-03-19 14:02:47 +0000 | [diff] [blame] | 212 | cli_wand->location2="'%s' '%s' in \"%s\" line %u column %u"; |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 213 | if ( LocaleCompare("-", argv[index]) == 0 ) |
| 214 | cli_wand->filename="stdin"; |
| 215 | else |
| 216 | cli_wand->filename=argv[index]; |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 217 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 218 | /* Process Options from Script */ |
| 219 | option = arg1 = arg2 = (char*)NULL; |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 220 | while (1) { |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 221 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 222 | /* Get a option */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 223 | { MagickBooleanType status = GetScriptToken(token_info); |
| 224 | cli_wand->line=token_info->token_line; |
| 225 | cli_wand->column=token_info->token_column; |
anthony | 84d4279 | 2012-03-30 14:08:38 +0000 | [diff] [blame] | 226 | if( IfMagickFalse(status) ) |
| 227 | break; /* error or end of options */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 228 | } |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 229 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 230 | /* save option details */ |
| 231 | CloneString(&option,token_info->token); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 232 | |
| 233 | { /* get option type and argument count */ |
| 234 | const OptionInfo *option_info = GetCommandOptionInfo(option); |
| 235 | count=option_info->type; |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 236 | option_type=(CommandOptionFlags) option_info->flags; |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 237 | #if 0 |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 238 | (void) FormatLocaleFile(stderr, "Script: %u,%u: \"%s\" matched \"%s\"\n", |
| 239 | cli_wand->line, cli_wand->line, option, option_info->mnemonic ); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 240 | #endif |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 241 | } |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 242 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 243 | /* handle a undefined option - image read? */ |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 244 | if ( option_type == UndefinedOptionFlag || |
| 245 | (option_type & NonMagickOptionFlag) != 0 ) { |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 246 | #if MagickCommandDebug >= 3 |
| 247 | (void) FormatLocaleFile(stderr, "Script %u,%u Non-Option: \"%s\"\n", |
| 248 | cli_wand->line, cli_wand->line, option); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 249 | #endif |
anthony | 84d4279 | 2012-03-30 14:08:38 +0000 | [diff] [blame] | 250 | if ( IfMagickFalse(IsCommandOption(option))) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 251 | /* non-option -- treat as a image read */ |
| 252 | CLISpecialOperator(cli_wand,"-read",option); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 253 | else |
| 254 | CLIWandExceptionBreak(OptionFatalError,"UnrecognizedOption",option); |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 255 | count = 0; |
| 256 | goto next_token; |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 257 | } |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 258 | |
| 259 | if ( count >= 1 ) { |
anthony | 84d4279 | 2012-03-30 14:08:38 +0000 | [diff] [blame] | 260 | if( IfMagickFalse(GetScriptToken(token_info)) ) |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 261 | CLIWandException(OptionFatalError,"MissingArgument",option); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 262 | CloneString(&arg1,token_info->token); |
| 263 | } |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 264 | else |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 265 | CloneString(&arg1,(char *)NULL); |
anthony | 2052d27 | 2012-02-28 12:48:29 +0000 | [diff] [blame] | 266 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 267 | if ( count >= 2 ) { |
anthony | 84d4279 | 2012-03-30 14:08:38 +0000 | [diff] [blame] | 268 | if( IfMagickFalse(GetScriptToken(token_info)) ) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 269 | CLIWandExceptionBreak(OptionFatalError,"MissingArgument",option); |
| 270 | CloneString(&arg2,token_info->token); |
| 271 | } |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 272 | else |
| 273 | CloneString(&arg2,(char *)NULL); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 274 | |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 275 | #if MagickCommandDebug >= 3 |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 276 | (void) FormatLocaleFile(stderr, |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 277 | "Script %u,%u Option: \"%s\" Count: %d Flags: %04x Args: \"%s\" \"%s\"\n", |
| 278 | cli_wand->line,cli_wand->line,option,count,option_type,arg1,arg2); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 279 | #endif |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 280 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 281 | if ( (option_type & GenesisOptionFlag) != 0 ) { |
| 282 | /* Genesis Options have no place in a magick script */ |
| 283 | CLIWandExceptionBreak(OptionError,"InvalidUseOfOption",option); |
| 284 | goto next_token; |
| 285 | } |
anthony | a3ef4ed | 2012-03-17 06:52:53 +0000 | [diff] [blame] | 286 | if ( (option_type & DeprecateOptionFlag) != 0 ) { |
| 287 | CLIWandException(OptionWarning,"DeprecatedOption",option); |
| 288 | if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse ) |
| 289 | break; |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 290 | /* fall through - do the depreciated option */ |
| 291 | } |
| 292 | if (((option_type & ImageRequiredFlags) != 0 ) && |
anthony | 7152725 | 2012-04-06 02:31:40 +0000 | [diff] [blame] | 293 | ( cli_wand->wand.images == (Image *)NULL ) ) { |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 294 | CLIWandException(OptionError,"NoImagesFound",option); |
| 295 | goto next_token; |
anthony | a3ef4ed | 2012-03-17 06:52:53 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /* handle special script-argument options here */ |
| 299 | //either continue processing command line |
| 300 | // or making use of the command line options. |
| 301 | //CLICommandOptions(cli_wand,count+1,argv, MagickScriptArgsFlags); |
| 302 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 303 | /* |
| 304 | Process Option from file |
| 305 | */ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 306 | if ( (option_type & SpecialOptionFlag) != 0 ) { |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 307 | if ( LocaleCompare(option,"-exit") == 0 ) { |
| 308 | break; /* forced end of script */ |
| 309 | } |
| 310 | if ( LocaleCompare(option,"-script") == 0 ) { |
| 311 | CLIWandExceptionBreak(OptionError,"InvalidUseOfOption",option); |
| 312 | goto next_token; |
| 313 | } |
| 314 | /* handle any other special operators now */ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 315 | CLISpecialOperator(cli_wand,option,arg1); |
| 316 | } |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 317 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 318 | if ( (option_type & SettingOptionFlags) != 0 ) { |
| 319 | CLISettingOptionInfo(cli_wand, option, arg1); |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 320 | // FUTURE: Sync Specific Settings into Image Properities (not global) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 321 | } |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 322 | if ( cli_wand->wand.images != (Image *)NULL ) |
| 323 | SyncImagesSettings(cli_wand->wand.image_info,cli_wand->wand.images, |
| 324 | cli_wand->wand.exception); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 325 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 326 | if ( (option_type & SimpleOperatorOptionFlag) != 0) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 327 | CLISimpleOperatorImages(cli_wand, option, arg1, arg2); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 328 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 329 | if ( (option_type & ListOperatorOptionFlag) != 0 ) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 330 | CLIListOperatorImages(cli_wand, option, arg1, arg2); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 331 | |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 332 | next_token: |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 333 | #if MagickCommandDebug >= 9 |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 334 | OutputOptions(cli_wand->wand.image_info); |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 335 | if ( cli_wand->wand.images != (Image *)NULL ) { |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 336 | OutputArtifacts(cli_wand->wand.images); |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 337 | OutputProperties(cli_wand->wand.images,cli_wand->wand.exception); |
| 338 | } |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 339 | #endif |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 340 | if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse ) |
| 341 | break; |
| 342 | } |
| 343 | |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 344 | #if MagickCommandDebug >= 3 |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 345 | (void) FormatLocaleFile(stderr, "Script End: %d\n", token_info->status); |
| 346 | #endif |
| 347 | switch( token_info->status ) { |
| 348 | case TokenStatusOK: |
| 349 | case TokenStatusEOF: |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 350 | if (cli_wand->image_list_stack != (Stack *)NULL) |
| 351 | CLIWandException(OptionError,"UnbalancedParenthesis", "(eof)"); |
| 352 | else if (cli_wand->image_info_stack != (Stack *)NULL) |
| 353 | CLIWandException(OptionError,"UnbalancedBraces", "(eof)"); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 354 | break; |
| 355 | case TokenStatusBadQuotes: |
| 356 | /* Ensure last token has a sane length for error report */ |
| 357 | if( strlen(token_info->token) > INITAL_TOKEN_LENGTH-1 ) { |
| 358 | token_info->token[INITAL_TOKEN_LENGTH-4] = '.'; |
| 359 | token_info->token[INITAL_TOKEN_LENGTH-3] = '.'; |
| 360 | token_info->token[INITAL_TOKEN_LENGTH-2] = '.'; |
| 361 | token_info->token[INITAL_TOKEN_LENGTH-1] = '\0'; |
| 362 | } |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 363 | CLIWandException(OptionFatalError,"ScriptUnbalancedQuotes", |
| 364 | token_info->token); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 365 | break; |
| 366 | case TokenStatusMemoryFailed: |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 367 | CLIWandException(OptionFatalError,"ScriptTokenMemoryFailed",""); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 368 | break; |
| 369 | case TokenStatusBinary: |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 370 | CLIWandException(OptionFatalError,"ScriptIsBinary",""); |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | |
| 374 | /* Clean up */ |
| 375 | token_info = DestroyScriptTokenInfo(token_info); |
| 376 | |
| 377 | CloneString(&option,(char *)NULL); |
| 378 | CloneString(&arg1,(char *)NULL); |
| 379 | CloneString(&arg2,(char *)NULL); |
| 380 | |
| 381 | return; |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* |
| 385 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 386 | % % |
| 387 | % % |
| 388 | % % |
| 389 | + P r o c e s s C o m m a n d O p t i o n s % |
| 390 | % % |
| 391 | % % |
| 392 | % % |
| 393 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 394 | % |
| 395 | % ProcessCommandOptions() reads and processes arguments in the given |
| 396 | % command line argument array. The array does not contain the command |
| 397 | % being processed, only the options. |
| 398 | % |
| 399 | % The 'process_flags' can be used to control and limit option processing. |
| 400 | % For example, to only process one option, or how unknown and special options |
| 401 | % are to be handled, and if the last argument in array is to be regarded as a |
| 402 | % final image write argument (filename or special coder). |
| 403 | % |
| 404 | % The format of the ProcessCommandOptions method is: |
| 405 | % |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 406 | % int ProcessCommandOptions(MagickCLI *cli_wand,int argc,char **argv, |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 407 | % int index, ProcessOptionFlags process_flags ) |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 408 | % |
| 409 | % A description of each parameter follows: |
| 410 | % |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 411 | % o cli_wand: the main CLI Wand to use. |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 412 | % |
| 413 | % o argc: the number of elements in the argument vector. |
| 414 | % |
| 415 | % o argv: A text array containing the command line arguments. |
| 416 | % |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 417 | % o process_flags: What type of arguments will be processed, ignored |
| 418 | % or return errors. |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 419 | % |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 420 | % o index: index in the argv array to start processing from |
| 421 | % |
| 422 | % The function returns the index ot the next option to be processed. This |
| 423 | % is really only releven if process_flags contains a ProcessOneOptionOnly |
| 424 | % flag. |
| 425 | % |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 426 | */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 427 | WandExport int ProcessCommandOptions(MagickCLI *cli_wand, int argc, |
| 428 | char **argv, int index, ProcessOptionFlags process_flags ) |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 429 | { |
| 430 | const char |
| 431 | *option, |
| 432 | *arg1, |
| 433 | *arg2; |
| 434 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 435 | int |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 436 | i, |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 437 | end, |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 438 | count; |
| 439 | |
| 440 | CommandOptionFlags |
anthony | 686b1a3 | 2012-02-15 14:50:53 +0000 | [diff] [blame] | 441 | option_type; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 442 | |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 443 | assert(argc>=index); /* you may have no arguments left! */ |
| 444 | assert(argv != (char **)NULL); |
| 445 | assert(argv[index] != (char *)NULL); |
| 446 | assert(argv[argc-1] != (char *)NULL); |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 447 | assert(cli_wand != (MagickCLI *) NULL); |
| 448 | assert(cli_wand->signature == WandSignature); |
| 449 | if (cli_wand->wand.debug != MagickFalse) |
| 450 | (void) LogMagickEvent(WandEvent,GetMagickModule(),"%s",cli_wand->wand.name); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 451 | |
anthony | 92c93bd | 2012-03-19 14:02:47 +0000 | [diff] [blame] | 452 | /* define the error location string for use in exceptions |
| 453 | order of input escapes: option, (arg), filename, line, column */ |
| 454 | cli_wand->location="'%s' %s arg %u"; |
| 455 | cli_wand->location2="'%s' '%s' %s arg %u"; |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 456 | cli_wand->filename="CLI"; |
| 457 | |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 458 | end = argc; |
| 459 | if ( ( process_flags & ProcessOutputFile ) != 0 ) |
| 460 | end--; |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 461 | |
| 462 | for (i=index; i < end; i += count +1) { |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 463 | /* Finished processing one option? */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 464 | if ( ( process_flags & ProcessOneOptionOnly ) != 0 && i != index ) |
| 465 | return(i); |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 466 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 467 | option=argv[i]; |
| 468 | cli_wand->line=i; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 469 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 470 | { const OptionInfo *option_info = GetCommandOptionInfo(argv[i]); |
| 471 | count=option_info->type; |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 472 | option_type=(CommandOptionFlags) option_info->flags; |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 473 | #if 0 |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 474 | (void) FormatLocaleFile(stderr, "CLI %d: \"%s\" matched \"%s\"\n", |
| 475 | i, argv[i], option_info->mnemonic ); |
anthony | 686b1a3 | 2012-02-15 14:50:53 +0000 | [diff] [blame] | 476 | #endif |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 477 | } |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 478 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 479 | if ( option_type == UndefinedOptionFlag || |
| 480 | (option_type & NonMagickOptionFlag) != 0 ) { |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 481 | #if MagickCommandDebug >= 3 |
| 482 | (void) FormatLocaleFile(stderr, "CLI %d Non-Option: \"%s\"\n", i, option); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 483 | #endif |
anthony | 84d4279 | 2012-03-30 14:08:38 +0000 | [diff] [blame] | 484 | if ( IfMagickFalse(IsCommandOption(option) ) && |
| 485 | (process_flags & ProcessNonOptionImageRead) != 0 ) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 486 | /* non-option -- treat as a image read */ |
| 487 | CLISpecialOperator(cli_wand,"-read",option); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 488 | else if ( (process_flags & ProcessUnknownOptionError) != 0 ) |
| 489 | CLIWandException(OptionFatalError,"UnrecognizedOption",option); |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 490 | count = 0; |
| 491 | goto next_argument; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 492 | } |
| 493 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 494 | if ((i+count) >= end ) { |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 495 | CLIWandException(OptionFatalError,"MissingArgument",option); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 496 | if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse ) |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 497 | return(end); |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 498 | goto next_argument; /* no more arguments unable to proceed */ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | arg1 = ( count >= 1 ) ? argv[i+1] : (char *)NULL; |
| 502 | arg2 = ( count >= 2 ) ? argv[i+2] : (char *)NULL; |
| 503 | |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 504 | #if MagickCommandDebug >= 3 |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 505 | (void) FormatLocaleFile(stderr, |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 506 | "CLI %u Option: \"%s\" Count: %d Flags: %04x Args: \"%s\" \"%s\"\n", |
| 507 | i,option,count,option_type,arg1,arg2); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 508 | #endif |
| 509 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 510 | if ( (option_type & DeprecateOptionFlag) != 0 ) { |
| 511 | CLIWandException(OptionWarning,"DeprecatedOption",option); |
| 512 | if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse ) |
| 513 | return(end); |
| 514 | /* fall through - do the depreciated option */ |
| 515 | } |
| 516 | if ( (option_type & GenesisOptionFlag) != 0 ) { |
| 517 | goto next_argument; /* ignore genesis options */ |
| 518 | } |
| 519 | if (((option_type & ImageRequiredFlags) != 0 ) && |
| 520 | ( cli_wand->wand.images == (Image *)NULL ) ) { |
| 521 | CLIWandException(OptionError,"NoImagesFound",option); |
| 522 | goto next_argument; |
| 523 | } |
| 524 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 525 | if ( (option_type & SpecialOptionFlag) != 0 ) { |
| 526 | if ( ( process_flags & ProcessExitOption ) != 0 |
| 527 | && LocaleCompare(option,"-exit") == 0 ) |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 528 | return(i+count); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 529 | if ( ( process_flags & ProcessScriptOption ) != 0 |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 530 | && LocaleCompare(option,"-script") == 0) { |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 531 | // Call Script, with a filename as a zeroth argument |
| 532 | ProcessScriptOptions(cli_wand,argc,argv,i+1); |
| 533 | return(argc); /* no more options after script process! */ |
| 534 | } |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 535 | /* handle any other special operators now */ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 536 | CLISpecialOperator(cli_wand,option,arg1); |
| 537 | } |
| 538 | |
| 539 | if ( (option_type & SettingOptionFlags) != 0 ) { |
| 540 | CLISettingOptionInfo(cli_wand, option, arg1); |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 541 | // FUTURE: Sync Specific Settings into Image Properities (not global) |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 542 | } |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 543 | if ( cli_wand->wand.images != (Image *)NULL ) |
| 544 | SyncImagesSettings(cli_wand->wand.image_info,cli_wand->wand.images, |
| 545 | cli_wand->wand.exception); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 546 | |
| 547 | if ( (option_type & SimpleOperatorOptionFlag) != 0) |
| 548 | CLISimpleOperatorImages(cli_wand, option, arg1, arg2); |
| 549 | |
| 550 | if ( (option_type & ListOperatorOptionFlag) != 0 ) |
| 551 | CLIListOperatorImages(cli_wand, option, arg1, arg2); |
| 552 | |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 553 | next_argument: |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 554 | #if MagickCommandDebug >= 9 |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 555 | OutputOptions(cli_wand->wand.image_info); |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 556 | if ( cli_wand->wand.images != (Image *)NULL ) { |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 557 | OutputArtifacts(cli_wand->wand.images); |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 558 | OutputProperties(cli_wand->wand.images,cli_wand->wand.exception); |
| 559 | } |
anthony | 24aa882 | 2012-03-11 00:56:06 +0000 | [diff] [blame] | 560 | #endif |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 561 | if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse ) |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 562 | return(i+count); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 563 | } |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 564 | assert(i==end); |
anthony | 2052d27 | 2012-02-28 12:48:29 +0000 | [diff] [blame] | 565 | |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 566 | if ( ( process_flags & ProcessOutputFile ) == 0 ) |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 567 | return(end); |
| 568 | |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 569 | assert(end==argc-1); |
| 570 | |
| 571 | /* |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 572 | Implicit Write of images to final CLI argument |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 573 | */ |
| 574 | option=argv[i]; |
anthony | 799889a | 2012-03-11 11:00:32 +0000 | [diff] [blame] | 575 | cli_wand->line=i; |
anthony | 686b1a3 | 2012-02-15 14:50:53 +0000 | [diff] [blame] | 576 | |
anthony | f125a5e | 2012-04-03 13:14:42 +0000 | [diff] [blame] | 577 | #if MagickCommandDebug >= 3 |
| 578 | (void) FormatLocaleFile(stderr, "CLI %d Write File: \"%s\"\n", i, option ); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 579 | #endif |
| 580 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 581 | /* check that stacks are empty */ |
| 582 | if (cli_wand->image_list_stack != (Stack *)NULL) |
| 583 | CLIWandException(OptionError,"UnbalancedParenthesis", "(eof)"); |
| 584 | else if (cli_wand->image_info_stack != (Stack *)NULL) |
| 585 | CLIWandException(OptionError,"UnbalancedBraces", "(eof)"); |
| 586 | if ( CLICatchException(cli_wand, MagickFalse) != MagickFalse ) |
| 587 | return(argc); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 588 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 589 | /* This is a valid 'do no write' option - no images needed */ |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 590 | if (LocaleCompare(option,"-exit") == 0 ) |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 591 | return(argc); /* just exit, no image write */ |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 592 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 593 | /* If filename looks like an option -- produce an error */ |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 594 | if (IsCommandOption(option) != MagickFalse) { |
| 595 | CLIWandException(OptionError,"MissingOutputFilename",option); |
| 596 | return(argc); |
| 597 | } |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 598 | |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 599 | (void) SyncImagesSettings(cli_wand->wand.image_info,cli_wand->wand.images, |
| 600 | cli_wand->wand.exception); |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 601 | CLISpecialOperator(cli_wand,"-write",option); |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 602 | return(argc); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 607 | % % |
| 608 | % % |
| 609 | % % |
| 610 | + M a g i c k I m a g e C o m m a n d % |
| 611 | % % |
| 612 | % % |
| 613 | % % |
| 614 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 615 | % |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 616 | % MagickImageCommand() Handle special use CLI arguments and prepare a |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 617 | % CLI MagickCLI to process the command line or directly specified script. |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 618 | % |
| 619 | % This is essentualy interface function between the MagickCore library |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 620 | % initialization function MagickCommandGenesis(), and the option MagickCLI |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 621 | % processing functions ProcessCommandOptions() or ProcessScriptOptions() |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 622 | % |
| 623 | % The format of the MagickImageCommand method is: |
| 624 | % |
| 625 | % MagickBooleanType MagickImageCommand(ImageInfo *image_info, |
| 626 | % int argc, char **argv, char **metadata, ExceptionInfo *exception) |
| 627 | % |
| 628 | % A description of each parameter follows: |
| 629 | % |
| 630 | % o image_info: the starting image_info structure |
| 631 | % (for compatibilty with MagickCommandGenisis()) |
| 632 | % |
| 633 | % o argc: the number of elements in the argument vector. |
| 634 | % |
| 635 | % o argv: A text array containing the command line arguments. |
| 636 | % |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 637 | % o metadata: any metadata (for VBS) is returned here. |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 638 | % (for compatibilty with MagickCommandGenisis()) |
| 639 | % |
| 640 | % o exception: return any errors or warnings in this structure. |
| 641 | % |
| 642 | */ |
| 643 | |
anthony | 40b6015 | 2012-04-04 06:13:37 +0000 | [diff] [blame] | 644 | static void MagickUsage(MagickBooleanType verbose) |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 645 | { |
anthony | 40b6015 | 2012-04-04 06:13:37 +0000 | [diff] [blame] | 646 | (void) FormatLocaleFile(stdout, |
| 647 | "Usage: %s [{option}|{image}...] {output_image}\n",GetClientName()); |
| 648 | (void) FormatLocaleFile(stdout, |
| 649 | " %s [{option}|{image}...] -script {filename} [{script_args}...]\n", |
| 650 | GetClientName()); |
| 651 | (void) FormatLocaleFile(stdout, |
| 652 | " %s -help|-version|-usage|-list {option}\n", |
| 653 | GetClientName()); |
| 654 | |
| 655 | if (IfMagickFalse(verbose)) |
| 656 | return; |
| 657 | |
| 658 | (void) FormatLocaleFile(stdout,"\n%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n", |
| 659 | "All options are performed in a strict 'as you see them' order\n", |
| 660 | "You must read-in images before you can operate on them.\n", |
| 661 | "\n", |
| 662 | "Magick Script files can use any of the following forms...\n", |
| 663 | " #!/path/to/magick -script\n", |
| 664 | "or\n", |
| 665 | " #!/bin/sh\n", |
| 666 | " :; exec magick -script \"$0\" \"$@\"; exit 10\n", |
| 667 | " # Magick script from here...\n", |
| 668 | "or\n", |
| 669 | " #!/usr/bin/env magick-script\n", |
| 670 | "The latter two forms do not require the path to the command hard coded.\n", |
| 671 | "Note: \"magick-script\" needs to be linked to the \"magick\" command.\n", |
| 672 | "\n", |
| 673 | "For more information on usage, options, examples, and techniques\n", |
| 674 | "see the ImageMagick website at ", MagickAuthoritativeURL); |
| 675 | |
| 676 | return; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* |
| 680 | Concatanate given file arguments to the given output argument. |
| 681 | Used for a special -concatenate option used for specific 'delegates'. |
| 682 | The option is not formally documented. |
| 683 | |
| 684 | magick -concatenate files... output |
| 685 | |
| 686 | This is much like the UNIX "cat" command, but for both UNIX and Windows, |
| 687 | however the last argument provides the output filename. |
| 688 | */ |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 689 | static MagickBooleanType ConcatenateImages(int argc,char **argv, |
| 690 | ExceptionInfo *exception) |
| 691 | { |
| 692 | FILE |
| 693 | *input, |
| 694 | *output; |
| 695 | |
| 696 | int |
| 697 | c; |
| 698 | |
| 699 | register ssize_t |
| 700 | i; |
| 701 | |
| 702 | output=fopen_utf8(argv[argc-1],"wb"); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 703 | if (output == (FILE *) NULL) { |
| 704 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile", |
| 705 | argv[argc-1]); |
| 706 | return(MagickFalse); |
| 707 | } |
| 708 | for (i=2; i < (ssize_t) (argc-1); i++) { |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 709 | input=fopen_utf8(argv[i],"rb"); |
| 710 | if (input == (FILE *) NULL) |
| 711 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile",argv[i]); |
| 712 | for (c=fgetc(input); c != EOF; c=fgetc(input)) |
| 713 | (void) fputc((char) c,output); |
| 714 | (void) fclose(input); |
| 715 | (void) remove_utf8(argv[i]); |
| 716 | } |
| 717 | (void) fclose(output); |
| 718 | return(MagickTrue); |
| 719 | } |
| 720 | |
| 721 | WandExport MagickBooleanType MagickImageCommand(ImageInfo *image_info, |
| 722 | int argc,char **argv,char **metadata,ExceptionInfo *exception) |
| 723 | { |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 724 | MagickCLI |
| 725 | *cli_wand; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 726 | |
| 727 | const char |
| 728 | *option; |
| 729 | |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 730 | ProcessOptionFlags |
| 731 | process_flags = MagickCommandOptionFlags; |
| 732 | |
anthony | 4cb3726 | 2012-03-18 11:18:45 +0000 | [diff] [blame] | 733 | /* For specific OS command line requirements */ |
| 734 | ReadCommandlLine(argc,&argv); |
| 735 | |
| 736 | #if 0 |
anthony | 4cb3726 | 2012-03-18 11:18:45 +0000 | [diff] [blame] | 737 | status=ExpandFilenames(&argc,&argv); |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 738 | if ( IfMagickFalse(status) ) |
anthony | 4cb3726 | 2012-03-18 11:18:45 +0000 | [diff] [blame] | 739 | ThrowConvertException(ResourceLimitError,"MemoryAllocationFailed", |
| 740 | GetExceptionMessage(errno)); |
| 741 | #endif |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 742 | |
anthony | 668f43a | 2012-02-20 14:55:32 +0000 | [diff] [blame] | 743 | /* Initialize special "CLI Wand" to hold images and settings (empty) */ |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 744 | cli_wand=AcquireMagickCLI(image_info,exception); |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 745 | cli_wand->line=1; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 746 | |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 747 | GetPathComponent(argv[0],TailPath,cli_wand->wand.name); |
| 748 | ConcatenateMagickString(cli_wand->wand.name,"-CLI",MaxTextExtent); |
| 749 | |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 750 | /* "convert" command - give a "depreciation" warning" */ |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 751 | if ( (LocaleCompare("convert",argv[0]+strlen((argv[0])-7)) == 0) || |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 752 | (LocaleNCompare("convert",argv[0],7) == 0) || |
| 753 | (LocaleNCompare("lt-convert",argv[0],10) == 0) ) { |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 754 | process_flags = ConvertCommandOptionFlags; |
| 755 | /*(void) FormatLocaleFile(stderr,"WARNING: %s\n", |
| 756 | "The convert is depreciated in IMv7, use \"magick\"\n");*/ |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 757 | } |
anthony | eff71f5 | 2012-03-29 12:59:09 +0000 | [diff] [blame] | 758 | |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 759 | /* Special Case: If command name ends with "script" implied "-script" */ |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 760 | if (LocaleCompare("script",argv[0]+strlen(argv[0])-6) == 0) { |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 761 | GetPathComponent(argv[1],TailPath,cli_wand->wand.name); |
| 762 | ProcessScriptOptions(cli_wand,argc,argv,1); |
| 763 | goto Magick_Command_Cleanup; |
| 764 | } |
| 765 | |
| 766 | /* Special Case: Version Information and Abort */ |
| 767 | if (argc == 2) { |
| 768 | option=argv[1]; |
anthony | 40b6015 | 2012-04-04 06:13:37 +0000 | [diff] [blame] | 769 | if (LocaleCompare("-version",option) == 0) { |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 770 | CLISpecialOperator(cli_wand, "-version", (char *)NULL); |
| 771 | goto Magick_Command_Exit; |
| 772 | } |
anthony | 40b6015 | 2012-04-04 06:13:37 +0000 | [diff] [blame] | 773 | if ((LocaleCompare("-help",option) == 0) || /* GNU standard option */ |
| 774 | (LocaleCompare("--help",option) == 0) ) { |
| 775 | MagickUsage(MagickFalse); |
| 776 | goto Magick_Command_Exit; |
| 777 | } |
| 778 | if (LocaleCompare("-usage",option) == 0) { |
| 779 | CLISpecialOperator(cli_wand, "-version", (char *)NULL); |
| 780 | MagickUsage(MagickTrue); |
| 781 | goto Magick_Command_Exit; |
| 782 | } |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* not enough arguments -- including -help */ |
| 786 | if (argc < 3) { |
anthony | 40b6015 | 2012-04-04 06:13:37 +0000 | [diff] [blame] | 787 | (void) FormatLocaleFile(stderr, |
| 788 | "Error: Invalid argument or not enough arguments\n\n"); |
| 789 | MagickUsage(MagickFalse); |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 790 | goto Magick_Command_Exit; |
| 791 | } |
| 792 | |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 793 | /* List Information and Abort */ |
| 794 | if (LocaleCompare("-list",argv[1]) == 0) { |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 795 | CLISpecialOperator(cli_wand, argv[1], argv[2]); |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 796 | goto Magick_Command_Exit; |
| 797 | } |
| 798 | |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 799 | /* Special "concatenate option (hidden) for delegate usage */ |
| 800 | if (LocaleCompare("-concatenate",argv[1]) == 0) { |
| 801 | ConcatenateImages(argc,argv,exception); |
| 802 | goto Magick_Command_Exit; |
| 803 | } |
| 804 | |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 805 | /* ------------- */ |
| 806 | /* The Main Call */ |
| 807 | |
| 808 | if (LocaleCompare("-script",argv[1]) == 0) { |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 809 | /* Start processing directly from script, no pre-script options |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 810 | Replace wand command name with script name |
| 811 | First argument in the argv array is the script name to read. |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 812 | */ |
| 813 | GetPathComponent(argv[2],TailPath,cli_wand->wand.name); |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 814 | ProcessScriptOptions(cli_wand,argc,argv,2); |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 815 | } |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 816 | else { |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 817 | /* Normal Command Line, assumes output file as last option */ |
anthony | 0ea037a | 2012-04-03 12:14:39 +0000 | [diff] [blame] | 818 | ProcessCommandOptions(cli_wand,argc,argv,1, process_flags); |
anthony | 0b46ebe | 2012-03-06 04:15:35 +0000 | [diff] [blame] | 819 | } |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 820 | /* ------------- */ |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 821 | |
anthony | 4cb3726 | 2012-03-18 11:18:45 +0000 | [diff] [blame] | 822 | Magick_Command_Cleanup: |
anthony | 8226e72 | 2012-04-05 14:25:46 +0000 | [diff] [blame] | 823 | /* recover original image_info and clean up stacks */ |
| 824 | while (cli_wand->image_list_stack != (Stack *)NULL) |
| 825 | CLISpecialOperator(cli_wand,")",(const char *)NULL); |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 826 | while (cli_wand->image_info_stack != (Stack *)NULL) |
| 827 | CLISpecialOperator(cli_wand,"}",(const char *)NULL); |
anthony | 2052d27 | 2012-02-28 12:48:29 +0000 | [diff] [blame] | 828 | |
anthony | 1cdc5b7 | 2012-03-03 02:31:18 +0000 | [diff] [blame] | 829 | /* assert we have recovered the original structures */ |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 830 | assert(cli_wand->wand.image_info == image_info); |
| 831 | assert(cli_wand->wand.exception == exception); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 832 | |
| 833 | /* Handle metadata for ImageMagickObject COM object for Windows VBS */ |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 834 | if (metadata != (char **) NULL) { |
| 835 | const char |
| 836 | *format; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 837 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 838 | char |
| 839 | *text; |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 840 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 841 | format="%w,%h,%m"; // Get this from image_info Option splaytree |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 842 | |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 843 | text=InterpretImageProperties(image_info,cli_wand->wand.images,format, |
| 844 | exception); |
| 845 | if (text == (char *) NULL) |
| 846 | ThrowMagickException(exception,GetMagickModule(),ResourceLimitError, |
| 847 | "MemoryAllocationFailed","`%s'", GetExceptionMessage(errno)); |
| 848 | else { |
| 849 | (void) ConcatenateString(&(*metadata),text); |
| 850 | text=DestroyString(text); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 851 | } |
anthony | afa3dfc | 2012-03-03 11:31:30 +0000 | [diff] [blame] | 852 | } |
anthony | 4cb3726 | 2012-03-18 11:18:45 +0000 | [diff] [blame] | 853 | |
anthony | 52bef75 | 2012-03-27 13:54:47 +0000 | [diff] [blame] | 854 | Magick_Command_Exit: |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 855 | /* Destroy the special CLI Wand */ |
anthony | 43f425d | 2012-02-26 12:58:58 +0000 | [diff] [blame] | 856 | cli_wand->wand.image_info = (ImageInfo *)NULL; /* not these */ |
| 857 | cli_wand->wand.exception = (ExceptionInfo *)NULL; |
| 858 | cli_wand=DestroyMagickCLI(cli_wand); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 859 | |
anthony | c2b913e | 2012-03-31 00:22:33 +0000 | [diff] [blame] | 860 | return(IsMagickTrue(exception->severity > ErrorException)); |
anthony | fa1e43d | 2012-02-12 12:55:45 +0000 | [diff] [blame] | 861 | } |