2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT OOO K K EEEEE N N %
8 % T O O KKK EEE N N N %
10 % T OOO K K EEEEE N N %
13 % MagickCore Token Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/exception.h"
45 #include "MagickCore/exception-private.h"
46 #include "MagickCore/image.h"
47 #include "MagickCore/memory_.h"
48 #include "MagickCore/string_.h"
49 #include "MagickCore/string-private.h"
50 #include "MagickCore/token.h"
51 #include "MagickCore/token-private.h"
52 #include "MagickCore/utility.h"
55 Typedef declaractions.
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % A c q u i r e T o k e n I n f o %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % AcquireTokenInfo() allocates the TokenInfo structure.
88 % The format of the AcquireTokenInfo method is:
90 % TokenInfo *AcquireTokenInfo()
93 MagickExport TokenInfo *AcquireTokenInfo(void)
98 token_info=(TokenInfo *) AcquireMagickMemory(sizeof(*token_info));
99 if (token_info == (TokenInfo *) NULL)
100 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
101 token_info->signature=MagickSignature;
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % D e s t r o y T o k e n I n f o %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
119 % The format of the DestroyTokenInfo method is:
121 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
123 % A description of each parameter follows:
125 % o token_info: Specifies a pointer to an TokenInfo structure.
128 MagickExport TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
130 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
131 assert(token_info != (TokenInfo *) NULL);
132 assert(token_info->signature == MagickSignature);
133 token_info->signature=(~MagickSignature);
134 token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 + G e t M a g i c k T o k e n %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % GetMagickToken() gets a token from the token stream. A token is defined as
150 % a sequence of characters delimited by whitespace (e.g. clip-path), a
151 % sequence delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
152 % parenthesis (e.g. rgb(0,0,0)). GetMagickToken() also recognizes these
153 % separator characters: ':', '=', ',', and ';'.
155 % The format of the GetMagickToken method is:
157 % void GetMagickToken(const char *start,const char **end,char *token)
159 % A description of each parameter follows:
161 % o start: the start of the token sequence.
163 % o end: point to the end of the token sequence.
165 % o token: copy the token to this buffer.
168 MagickExport void GetMagickToken(const char *start,const char **end,char *token)
179 assert(start != (const char *) NULL);
180 assert(token != (char *) NULL);
182 for (p=start; *p != '\0'; )
184 while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
200 case '"': escape='"'; break;
201 case '\'': escape='\''; break;
202 case '`': escape='\''; break;
203 case '{': escape='}'; break;
204 default: escape=(*p); break;
206 for (p++; *p != '\0'; p++)
208 if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
223 if ((*p == '>') || (*p == '/'))
232 value=InterpretLocaleValue(p,&q);
234 if ((p != q) && (*p != ','))
236 for ( ; (p < q) && (*p != ','); p++)
242 if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
243 (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
248 for ( ; *p != '\0'; p++)
250 if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
251 (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
253 if ((i > 0) && (*p == '<'))
259 for (p++; *p != '\0'; p++)
262 if ((*p == ')') && (*(p-1) != '\\'))
272 if (LocaleNCompare(token,"url(",4) == 0)
278 if (token[offset] == '#')
280 i=(ssize_t) strlen(token);
281 (void) CopyMagickString(token,token+offset,MaxTextExtent);
282 token[i-offset-1]='\0';
284 while (isspace((int) ((unsigned char) *p)) != 0)
286 if (end != (const char **) NULL)
287 *end=(const char *) p;
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % G l o b E x p r e s s i o n %
299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
301 % GlobExpression() returns MagickTrue if the expression matches the pattern.
303 % The format of the GlobExpression function is:
305 % MagickBooleanType GlobExpression(const char *expression,
306 % const char *pattern,const MagickBooleanType case_insensitive)
308 % A description of each parameter follows:
310 % o expression: Specifies a pointer to a text string containing a file name.
312 % o pattern: Specifies a pointer to a text string containing a pattern.
314 % o case_insensitive: set to MagickTrue to ignore the case when matching
318 MagickExport MagickBooleanType GlobExpression(const char *expression,
319 const char *pattern,const MagickBooleanType case_insensitive)
329 Return on empty pattern or '*'.
331 if (pattern == (char *) NULL)
333 if (GetUTFCode(pattern) == 0)
335 if (LocaleCompare(pattern,"*") == 0)
337 p=pattern+strlen(pattern)-1;
338 if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
347 Determine if pattern is a scene, i.e. img0001.pcd[2].
349 image_info=AcquireImageInfo();
350 (void) CopyMagickString(image_info->filename,pattern,MaxTextExtent);
351 exception=AcquireExceptionInfo();
352 (void) SetImageInfo(image_info,0,exception);
353 exception=DestroyExceptionInfo(exception);
354 if (LocaleCompare(image_info->filename,pattern) != 0)
356 image_info=DestroyImageInfo(image_info);
359 image_info=DestroyImageInfo(image_info);
362 Evaluate glob expression.
365 while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
367 if (GetUTFCode(expression) == 0)
368 if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
370 switch (GetUTFCode(pattern))
378 pattern+=GetUTFOctets(pattern);
379 while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
381 status=GlobExpression(expression,pattern,case_insensitive);
382 expression+=GetUTFOctets(expression);
384 if (status != MagickFalse)
386 while (GetUTFCode(expression) != 0)
387 expression+=GetUTFOctets(expression);
388 while (GetUTFCode(pattern) != 0)
389 pattern+=GetUTFOctets(pattern);
398 pattern+=GetUTFOctets(pattern);
401 if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
406 if (GetUTFCode(pattern) == '\\')
408 pattern+=GetUTFOctets(pattern);
409 if (GetUTFCode(pattern) == 0)
415 if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
417 c=GetUTFCode(pattern);
418 pattern+=GetUTFOctets(pattern);
419 pattern+=GetUTFOctets(pattern);
420 if (GetUTFCode(pattern) == ']')
425 if (GetUTFCode(pattern) == '\\')
427 pattern+=GetUTFOctets(pattern);
428 if (GetUTFCode(pattern) == 0)
434 if ((GetUTFCode(expression) < c) ||
435 (GetUTFCode(expression) > GetUTFCode(pattern)))
437 pattern+=GetUTFOctets(pattern);
442 if (GetUTFCode(pattern) != GetUTFCode(expression))
444 pattern+=GetUTFOctets(pattern);
447 pattern+=GetUTFOctets(pattern);
448 while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
450 if ((GetUTFCode(pattern) == '\\') &&
451 (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
452 pattern+=GetUTFOctets(pattern);
453 pattern+=GetUTFOctets(pattern);
455 if (GetUTFCode(pattern) != 0)
457 pattern+=GetUTFOctets(pattern);
458 expression+=GetUTFOctets(expression);
466 pattern+=GetUTFOctets(pattern);
467 expression+=GetUTFOctets(expression);
475 pattern+=GetUTFOctets(pattern);
476 while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
480 while ((GetUTFCode(p) != 0) && (GetUTFCode(pattern) != 0) &&
481 (GetUTFCode(pattern) != ',') && (GetUTFCode(pattern) != '}') &&
482 (match != MagickFalse))
484 if (GetUTFCode(pattern) == '\\')
485 pattern+=GetUTFOctets(pattern);
486 match=(GetUTFCode(pattern) == GetUTFCode(p)) ? MagickTrue :
489 pattern+=GetUTFOctets(pattern);
491 if (GetUTFCode(pattern) == 0)
498 if (match != MagickFalse)
501 while ((GetUTFCode(pattern) != '}') &&
502 (GetUTFCode(pattern) != 0))
504 pattern+=GetUTFOctets(pattern);
505 if (GetUTFCode(pattern) == '\\')
507 pattern+=GetUTFOctets(pattern);
508 if (GetUTFCode(pattern) == '}')
509 pattern+=GetUTFOctets(pattern);
515 while ((GetUTFCode(pattern) != '}') &&
516 (GetUTFCode(pattern) != ',') &&
517 (GetUTFCode(pattern) != 0))
519 pattern+=GetUTFOctets(pattern);
520 if (GetUTFCode(pattern) == '\\')
522 pattern+=GetUTFOctets(pattern);
523 if ((GetUTFCode(pattern) == '}') ||
524 (GetUTFCode(pattern) == ','))
525 pattern+=GetUTFOctets(pattern);
529 if (GetUTFCode(pattern) != 0)
530 pattern+=GetUTFOctets(pattern);
536 pattern+=GetUTFOctets(pattern);
537 if (GetUTFCode(pattern) == 0)
542 if (case_insensitive != MagickFalse)
544 if (tolower((int) GetUTFCode(expression)) !=
545 tolower((int) GetUTFCode(pattern)))
552 if (GetUTFCode(expression) != GetUTFCode(pattern))
557 expression+=GetUTFOctets(expression);
558 pattern+=GetUTFOctets(pattern);
562 while (GetUTFCode(pattern) == '*')
563 pattern+=GetUTFOctets(pattern);
564 match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
565 MagickTrue : MagickFalse;
570 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580 % IsGlob() returns MagickTrue if the path specification contains a globbing
583 % The format of the IsGlob method is:
585 % MagickBooleanType IsGlob(const char *geometry)
587 % A description of each parameter follows:
592 MagickExport MagickBooleanType IsGlob(const char *path)
597 if (IsPathAccessible(path) != MagickFalse)
599 status=(strchr(path,'*') != (char *) NULL) ||
600 (strchr(path,'?') != (char *) NULL) ||
601 (strchr(path,'{') != (char *) NULL) ||
602 (strchr(path,'}') != (char *) NULL) ||
603 (strchr(path,'[') != (char *) NULL) ||
604 (strchr(path,']') != (char *) NULL) ? MagickTrue : MagickFalse;
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % T o k e n i z e r %
617 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
620 % one at a time from a string of characters. The characters used for white
621 % space, for break characters, and for quotes can be specified. Also,
622 % characters in the string can be preceded by a specifiable escape character
623 % which removes any special meaning the character may have.
625 % Here is some terminology:
627 % o token: A single unit of information in the form of a group of
630 % o white space: Apace that gets ignored (except within quotes or when
631 % escaped), like blanks and tabs. in addition, white space terminates a
634 % o break set: One or more characters that separates non-quoted tokens.
635 % Commas are a common break character. The usage of break characters to
636 % signal the end of a token is the same as that of white space, except
637 % multiple break characters with nothing or only white space between
638 % generate a null token for each two break characters together.
640 % For example, if blank is set to be the white space and comma is set to
641 % be the break character, the line
645 % ... consists of 5 tokens:
650 % 4) "" (the null string)
653 % o Quote character: A character that, when surrounding a group of other
654 % characters, causes the group of characters to be treated as a single
655 % token, no matter how many white spaces or break characters exist in
656 % the group. Also, a token always terminates after the closing quote.
657 % For example, if ' is the quote character, blank is white space, and
658 % comma is the break character, the following string
662 % ... consists of 4 tokens:
665 % 2) " B, CD" (note the blanks & comma)
669 % The quote characters themselves do not appear in the resultant
670 % tokens. The double quotes are delimiters i use here for
671 % documentation purposes only.
673 % o Escape character: A character which itself is ignored but which
674 % causes the next character to be used as is. ^ and \ are often used
675 % as escape characters. An escape in the last position of the string
676 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
677 % and non-escape) character. For example, assume white space, break
678 % character, and quote are the same as in the above examples, and
679 % further, assume that ^ is the escape character. Then, in the string
681 % ABC, ' DEF ^' GH' I ^ J K^ L ^
683 % ... there are 7 tokens:
688 % 4) " " (a lone blank)
691 % 7) "^" (passed as is at end of line)
693 % The format of the Tokenizer method is:
695 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
696 % const size_t max_token_length,const char *line,const char *white,
697 % const char *break_set,const char *quote,const char escape,
698 % char *breaker,int *next,char *quoted)
700 % A description of each parameter follows:
702 % o flag: right now, only the low order 3 bits are used.
704 % 1 => convert non-quoted tokens to upper case
705 % 2 => convert non-quoted tokens to lower case
706 % 0 => do not convert non-quoted tokens
708 % o token: a character string containing the returned next token
710 % o max_token_length: the maximum size of "token". Characters beyond
711 % "max_token_length" are truncated.
713 % o string: the string to be parsed.
715 % o white: a string of the valid white spaces. example:
717 % char whitesp[]={" \t"};
719 % blank and tab will be valid white space.
721 % o break: a string of the valid break characters. example:
723 % char breakch[]={";,"};
725 % semicolon and comma will be valid break characters.
727 % o quote: a string of the valid quote characters. An example would be
729 % char whitesp[]={"'\"");
731 % (this causes single and double quotes to be valid) Note that a
732 % token starting with one of these characters needs the same quote
733 % character to terminate it.
739 % is unterminated, but
743 % are properly terminated. Note that different quote characters
744 % can appear on the same line; only for a given token do the quote
745 % characters have to be the same.
747 % o escape: the escape character (NOT a string ... only one
748 % allowed). Use zero if none is desired.
750 % o breaker: the break character used to terminate the current
751 % token. If the token was quoted, this will be the quote used. If
752 % the token is the last one on the line, this will be zero.
754 % o next: this variable points to the first character of the
755 % next token. it gets reset by "tokenizer" as it steps through the
756 % string. Set it to 0 upon initialization, and leave it alone
757 % after that. You can change it if you want to jump around in the
758 % string or re-parse from the beginning, but be careful.
760 % o quoted: set to True if the token was quoted and MagickFalse
761 % if not. You may need this information (for example: in C, a
762 % string with quotes around it is a character string, while one
763 % without is an identifier).
765 % o result: 0 if we haven't reached EOS (end of string), and 1
775 static ssize_t sindex(int c,const char *string)
780 for (p=string; *p != '\0'; p++)
782 return((ssize_t) (p-string));
786 static void StoreToken(TokenInfo *token_info,char *string,
787 size_t max_token_length,int c)
792 if ((token_info->offset < 0) ||
793 ((size_t) token_info->offset >= (max_token_length-1)))
795 i=token_info->offset++;
797 if (token_info->state == IN_QUOTE)
799 switch (token_info->flag & 0x03)
803 string[i]=(char) toupper(c);
808 string[i]=(char) tolower(c);
816 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
817 char *token,const size_t max_token_length,const char *line,const char *white,
818 const char *break_set,const char *quote,const char escape,char *breaker,
819 int *next,char *quoted)
829 if (line[*next] == '\0')
831 token_info->state=IN_WHITE;
832 token_info->quote=(char) MagickFalse;
833 token_info->flag=flag;
834 for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
837 i=sindex(c,break_set);
840 switch (token_info->state)
847 *breaker=break_set[i];
848 token[token_info->offset]='\0';
853 StoreToken(token_info,token,max_token_length,c);
862 switch (token_info->state)
866 token_info->state=IN_QUOTE;
867 token_info->quote=quote[i];
868 *quoted=(char) MagickTrue;
873 if (quote[i] != token_info->quote)
874 StoreToken(token_info,token,max_token_length,c);
877 token_info->state=IN_OZONE;
878 token_info->quote='\0';
886 token[token_info->offset]='\0';
895 switch (token_info->state)
902 token_info->state=IN_OZONE;
907 StoreToken(token_info,token,max_token_length,c);
913 if (c == (int) escape)
915 if (line[(*next)+1] == '\0')
918 StoreToken(token_info,token,max_token_length,c);
920 token[token_info->offset]='\0';
923 switch (token_info->state)
928 token_info->state=IN_TOKEN;
936 StoreToken(token_info,token,max_token_length,c);
941 token[token_info->offset]='\0';
947 switch (token_info->state)
950 token_info->state=IN_TOKEN;
954 StoreToken(token_info,token,max_token_length,c);
959 token[token_info->offset]='\0';
964 token[token_info->offset]='\0';