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