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-2010 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 "magick/studio.h"
44 #include "magick/exception.h"
45 #include "magick/exception-private.h"
46 #include "magick/image.h"
47 #include "magick/memory_.h"
48 #include "magick/string_.h"
49 #include "magick/token.h"
50 #include "magick/token-private.h"
51 #include "magick/utility.h"
54 Typedef declaractions.
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % A c q u i r e T o k e n I n f o %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % AcquireTokenInfo() allocates the TokenInfo structure.
87 % The format of the AcquireTokenInfo method is:
89 % TokenInfo *AcquireTokenInfo()
92 MagickExport TokenInfo *AcquireTokenInfo(void)
97 token_info=(TokenInfo *) AcquireMagickMemory(sizeof(*token_info));
98 if (token_info == (TokenInfo *) NULL)
99 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
100 token_info->signature=MagickSignature;
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % D e s t r o y T o k e n I n f o %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
118 % The format of the DestroyTokenInfo method is:
120 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
122 % A description of each parameter follows:
124 % o token_info: Specifies a pointer to an TokenInfo structure.
127 MagickExport TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
129 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
130 assert(token_info != (TokenInfo *) NULL);
131 assert(token_info->signature == MagickSignature);
132 token_info->signature=(~MagickSignature);
133 token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 + G e t M a g i c k T o k e n %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % GetMagickToken() gets a token from the token stream. A token is defined as a
149 % sequence of characters delimited by whitespace (e.g. clip-path), a sequence
150 % delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
151 % parenthesis (e.g. rgb(0,0,0)). GetMagickToken() also recognizes these
152 % separator characters: ':', '=', ',', and ';'.
154 % The format of the GetMagickToken method is:
156 % void GetMagickToken(const char *start,const char **end,char *token)
158 % A description of each parameter follows:
160 % o start: the start of the token sequence.
162 % o end: point to the end of the token sequence.
164 % o token: copy the token to this buffer.
167 MagickExport void GetMagickToken(const char *start,const char **end,char *token)
179 for (p=start; *p != '\0'; )
181 while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
197 case '"': escape='"'; break;
198 case '\'': escape='\''; break;
199 case '`': escape='\''; break;
200 case '{': escape='}'; break;
201 default: escape=(*p); break;
203 for (p++; *p != '\0'; p++)
205 if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
220 if ((*p == '>') || (*p == '/'))
230 if ((p != q) && (*p != ','))
232 for ( ; (p < q) && (*p != ','); p++)
238 if ((isalpha((int) ((unsigned char) *p)) == 0) &&
239 (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
244 for ( ; *p != '\0'; p++)
246 if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
247 (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
249 if ((i > 0) && (*p == '<'))
255 for (p++; *p != '\0'; p++)
258 if ((*p == ')') && (*(p-1) != '\\'))
268 if (LocaleNCompare(token,"url(",4) == 0)
274 if (token[offset] == '#')
276 i=(ssize_t) strlen(token);
277 (void) CopyMagickString(token,token+offset,MaxTextExtent);
278 token[i-offset-1]='\0';
280 while (isspace((int) ((unsigned char) *p)) != 0)
282 if (end != (const char **) NULL)
283 *end=(const char *) p;
287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % G l o b E x p r e s s i o n %
295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 % GlobExpression() returns MagickTrue if the expression matches the pattern.
299 % The format of the GlobExpression function is:
301 % MagickBooleanType GlobExpression(const char *expression,
302 % const char *pattern,const MagickBooleanType case_insensitive)
304 % A description of each parameter follows:
306 % o expression: Specifies a pointer to a text string containing a file name.
308 % o pattern: Specifies a pointer to a text string containing a pattern.
310 % o case_insensitive: set to MagickTrue to ignore the case when matching
314 MagickExport MagickBooleanType GlobExpression(const char *expression,
315 const char *pattern,const MagickBooleanType case_insensitive)
325 Return on empty pattern or '*'.
327 if (pattern == (char *) NULL)
329 if (GetUTFCode(pattern) == 0)
331 if (LocaleCompare(pattern,"*") == 0)
333 p=pattern+strlen(pattern)-1;
334 if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
343 Determine if pattern is a scene, i.e. img0001.pcd[2].
345 image_info=AcquireImageInfo();
346 (void) CopyMagickString(image_info->filename,pattern,MaxTextExtent);
347 exception=AcquireExceptionInfo();
348 (void) SetImageInfo(image_info,0,exception);
349 exception=DestroyExceptionInfo(exception);
350 if (LocaleCompare(image_info->filename,pattern) != 0)
352 image_info=DestroyImageInfo(image_info);
355 image_info=DestroyImageInfo(image_info);
358 Evaluate glob expression.
361 while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
363 if (GetUTFCode(expression) == 0)
364 if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
366 switch (GetUTFCode(pattern))
374 pattern+=GetUTFOctets(pattern);
375 while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
377 status=GlobExpression(expression,pattern,case_insensitive);
378 expression+=GetUTFOctets(expression);
380 if (status != MagickFalse)
382 while (GetUTFCode(expression) != 0)
383 expression+=GetUTFOctets(expression);
384 while (GetUTFCode(pattern) != 0)
385 pattern+=GetUTFOctets(pattern);
394 pattern+=GetUTFOctets(pattern);
397 if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
402 if (GetUTFCode(pattern) == '\\')
404 pattern+=GetUTFOctets(pattern);
405 if (GetUTFCode(pattern) == 0)
411 if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
413 c=GetUTFCode(pattern);
414 pattern+=GetUTFOctets(pattern);
415 pattern+=GetUTFOctets(pattern);
416 if (GetUTFCode(pattern) == ']')
421 if (GetUTFCode(pattern) == '\\')
423 pattern+=GetUTFOctets(pattern);
424 if (GetUTFCode(pattern) == 0)
430 if ((GetUTFCode(expression) < c) ||
431 (GetUTFCode(expression) > GetUTFCode(pattern)))
433 pattern+=GetUTFOctets(pattern);
438 if (GetUTFCode(pattern) != GetUTFCode(expression))
440 pattern+=GetUTFOctets(pattern);
443 pattern+=GetUTFOctets(pattern);
444 while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
446 if ((GetUTFCode(pattern) == '\\') &&
447 (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
448 pattern+=GetUTFOctets(pattern);
449 pattern+=GetUTFOctets(pattern);
451 if (GetUTFCode(pattern) != 0)
453 pattern+=GetUTFOctets(pattern);
454 expression+=GetUTFOctets(expression);
462 pattern+=GetUTFOctets(pattern);
463 expression+=GetUTFOctets(expression);
471 pattern+=GetUTFOctets(pattern);
472 while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
476 while ((GetUTFCode(p) != 0) && (GetUTFCode(pattern) != 0) &&
477 (GetUTFCode(pattern) != ',') && (GetUTFCode(pattern) != '}') &&
478 (match != MagickFalse))
480 if (GetUTFCode(pattern) == '\\')
481 pattern+=GetUTFOctets(pattern);
482 match=(GetUTFCode(pattern) == GetUTFCode(p)) ? MagickTrue :
485 pattern+=GetUTFOctets(pattern);
487 if (GetUTFCode(pattern) == 0)
494 if (match != MagickFalse)
497 while ((GetUTFCode(pattern) != '}') &&
498 (GetUTFCode(pattern) != 0))
500 pattern+=GetUTFOctets(pattern);
501 if (GetUTFCode(pattern) == '\\')
503 pattern+=GetUTFOctets(pattern);
504 if (GetUTFCode(pattern) == '}')
505 pattern+=GetUTFOctets(pattern);
511 while ((GetUTFCode(pattern) != '}') &&
512 (GetUTFCode(pattern) != ',') &&
513 (GetUTFCode(pattern) != 0))
515 pattern+=GetUTFOctets(pattern);
516 if (GetUTFCode(pattern) == '\\')
518 pattern+=GetUTFOctets(pattern);
519 if ((GetUTFCode(pattern) == '}') ||
520 (GetUTFCode(pattern) == ','))
521 pattern+=GetUTFOctets(pattern);
525 if (GetUTFCode(pattern) != 0)
526 pattern+=GetUTFOctets(pattern);
532 pattern+=GetUTFOctets(pattern);
533 if (GetUTFCode(pattern) == 0)
538 if (case_insensitive != MagickFalse)
540 if (tolower((int) GetUTFCode(expression)) !=
541 tolower((int) GetUTFCode(pattern)))
548 if (GetUTFCode(expression) != GetUTFCode(pattern))
553 expression+=GetUTFOctets(expression);
554 pattern+=GetUTFOctets(pattern);
558 while (GetUTFCode(pattern) == '*')
559 pattern+=GetUTFOctets(pattern);
560 match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
561 MagickTrue : MagickFalse;
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % IsGlob() returns MagickTrue if the path specification contains a globbing
579 % The format of the IsGlob method is:
581 % MagickBooleanType IsGlob(const char *geometry)
583 % A description of each parameter follows:
588 MagickExport MagickBooleanType IsGlob(const char *path)
593 if (IsPathAccessible(path) != MagickFalse)
595 status=(strchr(path,'*') != (char *) NULL) ||
596 (strchr(path,'?') != (char *) NULL) ||
597 (strchr(path,'{') != (char *) NULL) ||
598 (strchr(path,'}') != (char *) NULL) ||
599 (strchr(path,'[') != (char *) NULL) ||
600 (strchr(path,']') != (char *) NULL) ? MagickTrue : MagickFalse;
605 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
609 % T o k e n i z e r %
613 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
616 % one at a time from a string of characters. The characters used for white
617 % space, for break characters, and for quotes can be specified. Also,
618 % characters in the string can be preceded by a specifiable escape character
619 % which removes any special meaning the character may have.
621 % Here is some terminology:
623 % o token: A single unit of information in the form of a group of
626 % o white space: Apace that gets ignored (except within quotes or when
627 % escaped), like blanks and tabs. in addition, white space terminates a
630 % o break set: One or more characters that separates non-quoted tokens.
631 % Commas are a common break character. The usage of break characters to
632 % signal the end of a token is the same as that of white space, except
633 % multiple break characters with nothing or only white space between
634 % generate a null token for each two break characters together.
636 % For example, if blank is set to be the white space and comma is set to
637 % be the break character, the line
641 % ... consists of 5 tokens:
646 % 4) "" (the null string)
649 % o Quote character: A character that, when surrounding a group of other
650 % characters, causes the group of characters to be treated as a single
651 % token, no matter how many white spaces or break characters exist in
652 % the group. Also, a token always terminates after the closing quote.
653 % For example, if ' is the quote character, blank is white space, and
654 % comma is the break character, the following string
658 % ... consists of 4 tokens:
661 % 2) " B, CD" (note the blanks & comma)
665 % The quote characters themselves do not appear in the resultant
666 % tokens. The double quotes are delimiters i use here for
667 % documentation purposes only.
669 % o Escape character: A character which itself is ignored but which
670 % causes the next character to be used as is. ^ and \ are often used
671 % as escape characters. An escape in the last position of the string
672 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
673 % and non-escape) character. For example, assume white space, break
674 % character, and quote are the same as in the above examples, and
675 % further, assume that ^ is the escape character. Then, in the string
677 % ABC, ' DEF ^' GH' I ^ J K^ L ^
679 % ... there are 7 tokens:
684 % 4) " " (a lone blank)
687 % 7) "^" (passed as is at end of line)
689 % The format of the Tokenizer method is:
691 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
692 % const size_t max_token_length,const char *line,const char *white,
693 % const char *break_set,const char *quote,const char escape,
694 % char *breaker,int *next,char *quoted)
696 % A description of each parameter follows:
698 % o flag: right now, only the low order 3 bits are used.
700 % 1 => convert non-quoted tokens to upper case
701 % 2 => convert non-quoted tokens to lower case
702 % 0 => do not convert non-quoted tokens
704 % o token: a character string containing the returned next token
706 % o max_token_length: the maximum size of "token". Characters beyond
707 % "max_token_length" are truncated.
709 % o string: the string to be parsed.
711 % o white: a string of the valid white spaces. example:
713 % char whitesp[]={" \t"};
715 % blank and tab will be valid white space.
717 % o break: a string of the valid break characters. example:
719 % char breakch[]={";,"};
721 % semicolon and comma will be valid break characters.
723 % o quote: a string of the valid quote characters. An example would be
725 % char whitesp[]={"'\"");
727 % (this causes single and double quotes to be valid) Note that a
728 % token starting with one of these characters needs the same quote
729 % character to terminate it.
735 % is unterminated, but
739 % are properly terminated. Note that different quote characters
740 % can appear on the same line; only for a given token do the quote
741 % characters have to be the same.
743 % o escape: the escape character (NOT a string ... only one
744 % allowed). Use zero if none is desired.
746 % o breaker: the break character used to terminate the current
747 % token. If the token was quoted, this will be the quote used. If
748 % the token is the last one on the line, this will be zero.
750 % o next: this variable points to the first character of the
751 % next token. it gets reset by "tokenizer" as it steps through the
752 % string. Set it to 0 upon initialization, and leave it alone
753 % after that. You can change it if you want to jump around in the
754 % string or re-parse from the beginning, but be careful.
756 % o quoted: set to True if the token was quoted and MagickFalse
757 % if not. You may need this information (for example: in C, a
758 % string with quotes around it is a character string, while one
759 % without is an identifier).
761 % o result: 0 if we haven't reached EOS (end of string), and 1
771 static ssize_t sindex(int c,const char *string)
776 for (p=string; *p != '\0'; p++)
778 return((ssize_t) (p-string));
782 static void StoreToken(TokenInfo *token_info,char *string,
783 size_t max_token_length,int c)
788 if ((token_info->offset < 0) ||
789 ((size_t) token_info->offset >= (max_token_length-1)))
791 i=token_info->offset++;
793 if (token_info->state == IN_QUOTE)
795 switch (token_info->flag & 0x03)
799 string[i]=(char) toupper(c);
804 string[i]=(char) tolower(c);
812 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
813 char *token,const size_t max_token_length,const char *line,const char *white,
814 const char *break_set,const char *quote,const char escape,char *breaker,
815 int *next,char *quoted)
825 if (line[*next] == '\0')
827 token_info->state=IN_WHITE;
828 token_info->quote=(char) MagickFalse;
829 token_info->flag=flag;
830 for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
833 i=sindex(c,break_set);
836 switch (token_info->state)
843 *breaker=break_set[i];
844 token[token_info->offset]='\0';
849 StoreToken(token_info,token,max_token_length,c);
858 switch (token_info->state)
862 token_info->state=IN_QUOTE;
863 token_info->quote=quote[i];
864 *quoted=(char) MagickTrue;
869 if (quote[i] != token_info->quote)
870 StoreToken(token_info,token,max_token_length,c);
873 token_info->state=IN_OZONE;
874 token_info->quote='\0';
882 token[token_info->offset]='\0';
891 switch (token_info->state)
898 token_info->state=IN_OZONE;
903 StoreToken(token_info,token,max_token_length,c);
909 if (c == (int) escape)
911 if (line[(*next)+1] == '\0')
914 StoreToken(token_info,token,max_token_length,c);
916 token[token_info->offset]='\0';
919 switch (token_info->state)
924 token_info->state=IN_TOKEN;
932 StoreToken(token_info,token,max_token_length,c);
937 token[token_info->offset]='\0';
943 switch (token_info->state)
946 token_info->state=IN_TOKEN;
950 StoreToken(token_info,token,max_token_length,c);
955 token[token_info->offset]='\0';
960 token[token_info->offset]='\0';