MagickCore  7.0.8
Convert, Edit, Or Compose Bitmap Images
token.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % TTTTT OOO K K EEEEE N N %
7 % T O O K K E NN N %
8 % T O O KKK EEE N N N %
9 % T O O K K E N NN %
10 % T OOO K K EEEEE N N %
11 % %
12 % %
13 % MagickCore Token Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % January 1993 %
18 % %
19 % %
20 % Copyright 1999-2018 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 %
38 */
39 
40 /*
41  Include declarations.
42 */
43 #include "MagickCore/studio.h"
44 #include "MagickCore/exception.h"
46 #include "MagickCore/image.h"
48 #include "MagickCore/memory_.h"
50 #include "MagickCore/string_.h"
52 #include "MagickCore/token.h"
54 #include "MagickCore/utility.h"
56 
57 /*
58  Typedef declaractions.
59 */
60 struct _TokenInfo
61 {
62  int
64 
67 
68  ssize_t
70 
71  char
73 
74  size_t
76 };
77 
78 /*
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % %
81 % %
82 % %
83 % A c q u i r e T o k e n I n f o %
84 % %
85 % %
86 % %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 %
89 % AcquireTokenInfo() allocates the TokenInfo structure.
90 %
91 % The format of the AcquireTokenInfo method is:
92 %
93 % TokenInfo *AcquireTokenInfo()
94 %
95 */
97 {
98  TokenInfo
99  *token_info;
100 
101  token_info=(TokenInfo *) AcquireCriticalMemory(sizeof(*token_info));
102  token_info->signature=MagickCoreSignature;
103  return(token_info);
104 }
105 
106 /*
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % %
109 % %
110 % %
111 % D e s t r o y T o k e n I n f o %
112 % %
113 % %
114 % %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %
117 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
118 % structure.
119 %
120 % The format of the DestroyTokenInfo method is:
121 %
122 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
123 %
124 % A description of each parameter follows:
125 %
126 % o token_info: Specifies a pointer to an TokenInfo structure.
127 %
128 */
130 {
131  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
132  assert(token_info != (TokenInfo *) NULL);
133  assert(token_info->signature == MagickCoreSignature);
134  token_info->signature=(~MagickCoreSignature);
135  token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
136  return(token_info);
137 }
138 
139 /*
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % %
142 % %
143 % %
144 + G e t N e x t T o k e n %
145 % %
146 % %
147 % %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 %
150 % GetNextToken() gets a token from the token stream. A token is defined as
151 % a sequence of characters delimited by whitespace (e.g. clip-path), a
152 % sequence delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
153 % parenthesis (e.g. rgb(0,0,0)). GetNextToken() also recognizes these
154 % separator characters: ':', '=', ',', and ';'.
155 %
156 % The format of the GetNextToken method is:
157 %
158 % void GetNextToken(const char *start,const char **end,
159 % const size_t extent,char *token)
160 %
161 % A description of each parameter follows:
162 %
163 % o start: the start of the token sequence.
164 %
165 % o end: point to the end of the token sequence.
166 %
167 % o extent: maximum extent of the token.
168 %
169 % o token: copy the token to this buffer.
170 %
171 */
172 MagickExport void GetNextToken(const char *start,const char **end,
173  const size_t extent,char *token)
174 {
175  double
176  value;
177 
178  register const char
179  *p;
180 
181  register ssize_t
182  i;
183 
184  size_t
185  length;
186 
187  assert(start != (const char *) NULL);
188  assert(token != (char *) NULL);
189  i=0;
190  length=strlen(start);
191  p=start;
192  while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
193  p++;
194  switch (*p)
195  {
196  case '\0':
197  break;
198  case '"':
199  case '\'':
200  case '`':
201  case '{':
202  {
203  register char
204  escape;
205 
206  switch (*p)
207  {
208  case '"': escape='"'; break;
209  case '\'': escape='\''; break;
210  case '`': escape='\''; break;
211  case '{': escape='}'; break;
212  default: escape=(*p); break;
213  }
214  for (p++; *p != '\0'; p++)
215  {
216  if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
217  p++;
218  else
219  if (*p == escape)
220  {
221  p++;
222  break;
223  }
224  if (i < (ssize_t) (extent-1))
225  token[i++]=(*p);
226  if ((size_t) (p-start) >= length)
227  break;
228  }
229  break;
230  }
231  case '/':
232  {
233  if (i < (ssize_t) (extent-1))
234  token[i++]=(*p);
235  p++;
236  if ((*p == '>') || (*p == '/'))
237  {
238  if (i < (ssize_t) (extent-1))
239  token[i++]=(*p);
240  p++;
241  }
242  break;
243  }
244  default:
245  {
246  char
247  *q;
248 
249  value=StringToDouble(p,&q);
250  (void) value;
251  if ((p != q) && (*p != ','))
252  {
253  for ( ; (p < q) && (*p != ','); p++)
254  {
255  if (i < (ssize_t) (extent-1))
256  token[i++]=(*p);
257  if ((size_t) (p-start) >= length)
258  break;
259  }
260  if (*p == '%')
261  {
262  if (i < (ssize_t) (extent-1))
263  token[i++]=(*p);
264  p++;
265  }
266  break;
267  }
268  if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
269  (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
270  {
271  if (i < (ssize_t) (extent-1))
272  token[i++]=(*p);
273  p++;
274  break;
275  }
276  for ( ; *p != '\0'; p++)
277  {
278  if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
279  (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
280  break;
281  if ((i > 0) && (*p == '<'))
282  break;
283  if (i < (ssize_t) (extent-1))
284  token[i++]=(*p);
285  if (*p == '>')
286  break;
287  if (*p == '(')
288  for (p++; *p != '\0'; p++)
289  {
290  if (i < (ssize_t) (extent-1))
291  token[i++]=(*p);
292  if ((*p == ')') && (*(p-1) != '\\'))
293  break;
294  if ((size_t) (p-start) >= length)
295  break;
296  }
297  if ((size_t) (p-start) >= length)
298  break;
299  }
300  break;
301  }
302  }
303  token[i]='\0';
304  if ((LocaleNCompare(token,"url(",4) == 0) && (strlen(token) > 5))
305  {
306  ssize_t
307  offset;
308 
309  offset=4;
310  if (token[offset] == '#')
311  offset++;
312  i=(ssize_t) strlen(token);
313  if (i > offset)
314  {
315  (void) CopyMagickString(token,token+offset,MagickPathExtent);
316  token[i-offset-1]='\0';
317  }
318  }
319  while (isspace((int) ((unsigned char) *p)) != 0)
320  p++;
321  if (end != (const char **) NULL)
322  *end=(const char *) p;
323 }
324 
325 /*
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 % %
328 % %
329 % %
330 % G l o b E x p r e s s i o n %
331 % %
332 % %
333 % %
334 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 %
336 % GlobExpression() returns MagickTrue if the expression matches the pattern.
337 %
338 % The format of the GlobExpression function is:
339 %
340 % MagickBooleanType GlobExpression(const char *expression,
341 % const char *pattern,const MagickBooleanType case_insensitive)
342 %
343 % A description of each parameter follows:
344 %
345 % o expression: Specifies a pointer to a text string containing a file name.
346 %
347 % o pattern: Specifies a pointer to a text string containing a pattern.
348 %
349 % o case_insensitive: set to MagickTrue to ignore the case when matching
350 % an expression.
351 %
352 */
354  const char *pattern,const MagickBooleanType case_insensitive)
355 {
357  done,
358  match;
359 
360  register const char
361  *p;
362 
363  /*
364  Return on empty pattern or '*'.
365  */
366  if (pattern == (char *) NULL)
367  return(MagickTrue);
368  if (GetUTFCode(pattern) == 0)
369  return(MagickTrue);
370  if (LocaleCompare(pattern,"*") == 0)
371  return(MagickTrue);
372  p=pattern+strlen(pattern)-1;
373  if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
374  {
376  *exception;
377 
378  ImageInfo
379  *image_info;
380 
381  /*
382  Determine if pattern is a scene, i.e. img0001.pcd[2].
383  */
384  image_info=AcquireImageInfo();
385  (void) CopyMagickString(image_info->filename,pattern,MagickPathExtent);
386  exception=AcquireExceptionInfo();
387  (void) SetImageInfo(image_info,0,exception);
388  exception=DestroyExceptionInfo(exception);
389  if (LocaleCompare(image_info->filename,pattern) != 0)
390  {
391  image_info=DestroyImageInfo(image_info);
392  return(MagickFalse);
393  }
394  image_info=DestroyImageInfo(image_info);
395  }
396  /*
397  Evaluate glob expression.
398  */
399  done=MagickFalse;
400  while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
401  {
402  if (GetUTFCode(expression) == 0)
403  if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
404  break;
405  switch (GetUTFCode(pattern))
406  {
407  case '*':
408  {
410  status;
411 
412  status=MagickFalse;
413  while (GetUTFCode(pattern) == '*')
414  pattern+=GetUTFOctets(pattern);
415  while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
416  {
417  status=GlobExpression(expression,pattern,case_insensitive);
418  expression+=GetUTFOctets(expression);
419  }
420  if (status != MagickFalse)
421  {
422  while (GetUTFCode(expression) != 0)
423  expression+=GetUTFOctets(expression);
424  while (GetUTFCode(pattern) != 0)
425  pattern+=GetUTFOctets(pattern);
426  }
427  break;
428  }
429  case '[':
430  {
431  int
432  c;
433 
434  pattern+=GetUTFOctets(pattern);
435  for ( ; ; )
436  {
437  if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
438  {
439  done=MagickTrue;
440  break;
441  }
442  if (GetUTFCode(pattern) == '\\')
443  {
444  pattern+=GetUTFOctets(pattern);
445  if (GetUTFCode(pattern) == 0)
446  {
447  done=MagickTrue;
448  break;
449  }
450  }
451  if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
452  {
453  c=GetUTFCode(pattern);
454  pattern+=GetUTFOctets(pattern);
455  pattern+=GetUTFOctets(pattern);
456  if (GetUTFCode(pattern) == ']')
457  {
458  done=MagickTrue;
459  break;
460  }
461  if (GetUTFCode(pattern) == '\\')
462  {
463  pattern+=GetUTFOctets(pattern);
464  if (GetUTFCode(pattern) == 0)
465  {
466  done=MagickTrue;
467  break;
468  }
469  }
470  if ((GetUTFCode(expression) < c) ||
471  (GetUTFCode(expression) > GetUTFCode(pattern)))
472  {
473  pattern+=GetUTFOctets(pattern);
474  continue;
475  }
476  }
477  else
478  if (GetUTFCode(pattern) != GetUTFCode(expression))
479  {
480  pattern+=GetUTFOctets(pattern);
481  continue;
482  }
483  pattern+=GetUTFOctets(pattern);
484  while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
485  {
486  if ((GetUTFCode(pattern) == '\\') &&
487  (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
488  pattern+=GetUTFOctets(pattern);
489  pattern+=GetUTFOctets(pattern);
490  }
491  if (GetUTFCode(pattern) != 0)
492  {
493  pattern+=GetUTFOctets(pattern);
494  expression+=GetUTFOctets(expression);
495  }
496  break;
497  }
498  break;
499  }
500  case '?':
501  {
502  pattern+=GetUTFOctets(pattern);
503  expression+=GetUTFOctets(expression);
504  break;
505  }
506  case '{':
507  {
508  char
509  *target;
510 
511  register char
512  *p;
513 
514  target=AcquireString(pattern);
515  p=target;
516  pattern++;
517  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
518  {
519  *p++=(*pattern++);
520  if ((GetUTFCode(pattern) == ',') || (GetUTFCode(pattern) == '}'))
521  {
522  *p='\0';
523  match=GlobExpression(expression,target,case_insensitive);
524  if (match != MagickFalse)
525  {
526  expression+=MagickMin(strlen(expression),strlen(target));
527  break;
528  }
529  p=target;
530  pattern+=GetUTFOctets(pattern);
531  }
532  }
533  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
534  pattern+=GetUTFOctets(pattern);
535  if (GetUTFCode(pattern) != 0)
536  pattern+=GetUTFOctets(pattern);
537  target=DestroyString(target);
538  break;
539  }
540  case '\\':
541  {
542  pattern+=GetUTFOctets(pattern);
543  if (GetUTFCode(pattern) == 0)
544  break;
545  }
546  default:
547  {
548  if (case_insensitive != MagickFalse)
549  {
550  if (tolower((int) GetUTFCode(expression)) !=
551  tolower((int) GetUTFCode(pattern)))
552  {
553  done=MagickTrue;
554  break;
555  }
556  }
557  else
558  if (GetUTFCode(expression) != GetUTFCode(pattern))
559  {
560  done=MagickTrue;
561  break;
562  }
563  expression+=GetUTFOctets(expression);
564  pattern+=GetUTFOctets(pattern);
565  }
566  }
567  }
568  while (GetUTFCode(pattern) == '*')
569  pattern+=GetUTFOctets(pattern);
570  match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
572  return(match);
573 }
574 
575 /*
576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % %
578 % %
579 % %
580 + I s G l o b %
581 % %
582 % %
583 % %
584 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 %
586 % IsGlob() returns MagickTrue if the path specification contains a globbing
587 % pattern.
588 %
589 % The format of the IsGlob method is:
590 %
591 % MagickBooleanType IsGlob(const char *geometry)
592 %
593 % A description of each parameter follows:
594 %
595 % o path: the path.
596 %
597 */
599 {
601  status = MagickFalse;
602 
603  register const char
604  *p;
605 
606  if (IsPathAccessible(path) != MagickFalse)
607  return(MagickFalse);
608  for (p=path; *p != '\0'; p++)
609  {
610  switch (*p)
611  {
612  case '*':
613  case '?':
614  case '{':
615  case '}':
616  case '[':
617  case ']':
618  {
619  status=MagickTrue;
620  break;
621  }
622  default:
623  break;
624  }
625  }
626  return(status);
627 }
628 
629 /*
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % %
632 % %
633 % %
634 % T o k e n i z e r %
635 % %
636 % %
637 % %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 %
640 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
641 % one at a time from a string of characters. The characters used for white
642 % space, for break characters, and for quotes can be specified. Also,
643 % characters in the string can be preceded by a specifiable escape character
644 % which removes any special meaning the character may have.
645 %
646 % Here is some terminology:
647 %
648 % o token: A single unit of information in the form of a group of
649 % characters.
650 %
651 % o white space: Apace that gets ignored (except within quotes or when
652 % escaped), like blanks and tabs. in addition, white space terminates a
653 % non-quoted token.
654 %
655 % o break set: One or more characters that separates non-quoted tokens.
656 % Commas are a common break character. The usage of break characters to
657 % signal the end of a token is the same as that of white space, except
658 % multiple break characters with nothing or only white space between
659 % generate a null token for each two break characters together.
660 %
661 % For example, if blank is set to be the white space and comma is set to
662 % be the break character, the line
663 %
664 % A, B, C , , DEF
665 %
666 % ... consists of 5 tokens:
667 %
668 % 1) "A"
669 % 2) "B"
670 % 3) "C"
671 % 4) "" (the null string)
672 % 5) "DEF"
673 %
674 % o Quote character: A character that, when surrounding a group of other
675 % characters, causes the group of characters to be treated as a single
676 % token, no matter how many white spaces or break characters exist in
677 % the group. Also, a token always terminates after the closing quote.
678 % For example, if ' is the quote character, blank is white space, and
679 % comma is the break character, the following string
680 %
681 % A, ' B, CD'EF GHI
682 %
683 % ... consists of 4 tokens:
684 %
685 % 1) "A"
686 % 2) " B, CD" (note the blanks & comma)
687 % 3) "EF"
688 % 4) "GHI"
689 %
690 % The quote characters themselves do not appear in the resultant
691 % tokens. The double quotes are delimiters i use here for
692 % documentation purposes only.
693 %
694 % o Escape character: A character which itself is ignored but which
695 % causes the next character to be used as is. ^ and \ are often used
696 % as escape characters. An escape in the last position of the string
697 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
698 % and non-escape) character. For example, assume white space, break
699 % character, and quote are the same as in the above examples, and
700 % further, assume that ^ is the escape character. Then, in the string
701 %
702 % ABC, ' DEF ^' GH' I ^ J K^ L ^
703 %
704 % ... there are 7 tokens:
705 %
706 % 1) "ABC"
707 % 2) " DEF ' GH"
708 % 3) "I"
709 % 4) " " (a lone blank)
710 % 5) "J"
711 % 6) "K L"
712 % 7) "^" (passed as is at end of line)
713 %
714 % The format of the Tokenizer method is:
715 %
716 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
717 % const size_t max_token_length,const char *line,const char *white,
718 % const char *break_set,const char *quote,const char escape,
719 % char *breaker,int *next,char *quoted)
720 %
721 % A description of each parameter follows:
722 %
723 % o flag: right now, only the low order 3 bits are used.
724 %
725 % 1 => convert non-quoted tokens to upper case
726 % 2 => convert non-quoted tokens to lower case
727 % 0 => do not convert non-quoted tokens
728 %
729 % o token: a character string containing the returned next token
730 %
731 % o max_token_length: the maximum size of "token". Characters beyond
732 % "max_token_length" are truncated.
733 %
734 % o string: the string to be parsed.
735 %
736 % o white: a string of the valid white spaces. example:
737 %
738 % char whitesp[]={" \t"};
739 %
740 % blank and tab will be valid white space.
741 %
742 % o break: a string of the valid break characters. example:
743 %
744 % char breakch[]={";,"};
745 %
746 % semicolon and comma will be valid break characters.
747 %
748 % o quote: a string of the valid quote characters. An example would be
749 %
750 % char whitesp[]={"'\"");
751 %
752 % (this causes single and double quotes to be valid) Note that a
753 % token starting with one of these characters needs the same quote
754 % character to terminate it.
755 %
756 % for example:
757 %
758 % "ABC '
759 %
760 % is unterminated, but
761 %
762 % "DEF" and 'GHI'
763 %
764 % are properly terminated. Note that different quote characters
765 % can appear on the same line; only for a given token do the quote
766 % characters have to be the same.
767 %
768 % o escape: the escape character (NOT a string ... only one
769 % allowed). Use zero if none is desired.
770 %
771 % o breaker: the break character used to terminate the current
772 % token. If the token was quoted, this will be the quote used. If
773 % the token is the last one on the line, this will be zero.
774 %
775 % o next: this variable points to the first character of the
776 % next token. it gets reset by "tokenizer" as it steps through the
777 % string. Set it to 0 upon initialization, and leave it alone
778 % after that. You can change it if you want to jump around in the
779 % string or re-parse from the beginning, but be careful.
780 %
781 % o quoted: set to True if the token was quoted and MagickFalse
782 % if not. You may need this information (for example: in C, a
783 % string with quotes around it is a character string, while one
784 % without is an identifier).
785 %
786 % o result: 0 if we haven't reached EOS (end of string), and 1
787 % if we have.
788 %
789 */
790 
791 #define IN_WHITE 0
792 #define IN_TOKEN 1
793 #define IN_QUOTE 2
794 #define IN_OZONE 3
795 
796 static ssize_t sindex(int c,const char *string)
797 {
798  register const char
799  *p;
800 
801  for (p=string; *p != '\0'; p++)
802  if (c == (int) (*p))
803  return((ssize_t) (p-string));
804  return(-1);
805 }
806 
807 static void StoreToken(TokenInfo *token_info,char *string,
808  size_t max_token_length,int c)
809 {
810  register ssize_t
811  i;
812 
813  if ((token_info->offset < 0) ||
814  ((size_t) token_info->offset >= (max_token_length-1)))
815  return;
816  i=token_info->offset++;
817  string[i]=(char) c;
818  if (token_info->state == IN_QUOTE)
819  return;
820  switch (token_info->flag & 0x03)
821  {
822  case 1:
823  {
824  string[i]=(char) toupper(c);
825  break;
826  }
827  case 2:
828  {
829  string[i]=(char) tolower(c);
830  break;
831  }
832  default:
833  break;
834  }
835 }
836 
837 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
838  char *token,const size_t max_token_length,const char *line,const char *white,
839  const char *break_set,const char *quote,const char escape,char *breaker,
840  int *next,char *quoted)
841 {
842  int
843  c;
844 
845  register ssize_t
846  i;
847 
848  *breaker='\0';
849  *quoted='\0';
850  if (line[*next] == '\0')
851  return(1);
852  token_info->state=IN_WHITE;
853  token_info->quote=(char) MagickFalse;
854  token_info->flag=flag;
855  for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
856  {
857  c=(int) line[*next];
858  i=sindex(c,break_set);
859  if (i >= 0)
860  {
861  switch (token_info->state)
862  {
863  case IN_WHITE:
864  case IN_TOKEN:
865  case IN_OZONE:
866  {
867  (*next)++;
868  *breaker=break_set[i];
869  token[token_info->offset]='\0';
870  return(0);
871  }
872  case IN_QUOTE:
873  {
874  StoreToken(token_info,token,max_token_length,c);
875  break;
876  }
877  }
878  continue;
879  }
880  i=sindex(c,quote);
881  if (i >= 0)
882  {
883  switch (token_info->state)
884  {
885  case IN_WHITE:
886  {
887  token_info->state=IN_QUOTE;
888  token_info->quote=quote[i];
889  *quoted=(char) MagickTrue;
890  break;
891  }
892  case IN_QUOTE:
893  {
894  if (quote[i] != token_info->quote)
895  StoreToken(token_info,token,max_token_length,c);
896  else
897  {
898  token_info->state=IN_OZONE;
899  token_info->quote='\0';
900  }
901  break;
902  }
903  case IN_TOKEN:
904  case IN_OZONE:
905  {
906  *breaker=(char) c;
907  token[token_info->offset]='\0';
908  return(0);
909  }
910  }
911  continue;
912  }
913  i=sindex(c,white);
914  if (i >= 0)
915  {
916  switch (token_info->state)
917  {
918  case IN_WHITE:
919  case IN_OZONE:
920  break;
921  case IN_TOKEN:
922  {
923  token_info->state=IN_OZONE;
924  break;
925  }
926  case IN_QUOTE:
927  {
928  StoreToken(token_info,token,max_token_length,c);
929  break;
930  }
931  }
932  continue;
933  }
934  if (c == (int) escape)
935  {
936  if (line[(*next)+1] == '\0')
937  {
938  *breaker='\0';
939  StoreToken(token_info,token,max_token_length,c);
940  (*next)++;
941  token[token_info->offset]='\0';
942  return(0);
943  }
944  switch (token_info->state)
945  {
946  case IN_WHITE:
947  {
948  (*next)--;
949  token_info->state=IN_TOKEN;
950  break;
951  }
952  case IN_TOKEN:
953  case IN_QUOTE:
954  {
955  (*next)++;
956  c=(int) line[*next];
957  StoreToken(token_info,token,max_token_length,c);
958  break;
959  }
960  case IN_OZONE:
961  {
962  token[token_info->offset]='\0';
963  return(0);
964  }
965  }
966  continue;
967  }
968  switch (token_info->state)
969  {
970  case IN_WHITE:
971  {
972  token_info->state=IN_TOKEN;
973  StoreToken(token_info,token,max_token_length,c);
974  break;
975  }
976  case IN_TOKEN:
977  case IN_QUOTE:
978  {
979  StoreToken(token_info,token,max_token_length,c);
980  break;
981  }
982  case IN_OZONE:
983  {
984  token[token_info->offset]='\0';
985  return(0);
986  }
987  }
988  }
989  token[token_info->offset]='\0';
990  return(0);
991 }
static ssize_t sindex(int c, const char *string)
Definition: token.c:796
MagickExport TokenInfo * DestroyTokenInfo(TokenInfo *token_info)
Definition: token.c:129
MagickExport ImageInfo * AcquireImageInfo(void)
Definition: image.c:343
MagickPrivate MagickBooleanType IsGlob(const char *path)
Definition: token.c:598
#define IN_OZONE
Definition: token.c:794
static double StringToDouble(const char *magick_restrict string, char **magick_restrict sentinal)
MagickExport ExceptionInfo * AcquireExceptionInfo(void)
Definition: exception.c:108
int state
Definition: token.c:63
static void * AcquireCriticalMemory(const size_t size)
ssize_t offset
Definition: token.c:69
MagickStatusType flag
Definition: token.c:66
Definition: log.h:52
static void StoreToken(TokenInfo *token_info, char *string, size_t max_token_length, int c)
Definition: token.c:807
MagickExport TokenInfo * AcquireTokenInfo(void)
Definition: token.c:96
#define MagickCoreSignature
MagickBooleanType
Definition: magick-type.h:156
#define DirectorySeparator
Definition: studio.h:259
unsigned int MagickStatusType
Definition: magick-type.h:119
MagickExport char * AcquireString(const char *source)
Definition: string.c:129
static int GetUTFCode(const char *text)
char filename[MagickPathExtent]
Definition: image.h:480
MagickExport int LocaleNCompare(const char *p, const char *q, const size_t length)
Definition: locale.c:1540
MagickExport MagickBooleanType GlobExpression(const char *expression, const char *pattern, const MagickBooleanType case_insensitive)
Definition: token.c:353
#define MagickPathExtent
#define IN_WHITE
Definition: token.c:791
static unsigned int GetUTFOctets(const char *text)
MagickExport MagickBooleanType SetImageInfo(ImageInfo *image_info, const unsigned int frames, ExceptionInfo *exception)
Definition: image.c:2701
size_t signature
Definition: token.c:75
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1398
MagickExport MagickBooleanType IsPathAccessible(const char *path)
Definition: utility.c:1462
MagickExport size_t CopyMagickString(char *destination, const char *source, const size_t length)
Definition: string.c:755
char quote
Definition: token.c:72
MagickExport int LocaleCompare(const char *p, const char *q)
Definition: locale.c:1440
#define GetMagickModule()
Definition: log.h:28
MagickExport void GetNextToken(const char *start, const char **end, const size_t extent, char *token)
Definition: token.c:172
MagickExport ImageInfo * DestroyImageInfo(ImageInfo *image_info)
Definition: image.c:1248
MagickExport char * DestroyString(char *string)
Definition: string.c:823
#define MagickMin(x, y)
Definition: image-private.h:27
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1054
#define MagickPrivate
#define MagickExport
#define IN_QUOTE
Definition: token.c:793
MagickExport int Tokenizer(TokenInfo *token_info, const unsigned flag, char *token, const size_t max_token_length, const char *line, const char *white, const char *break_set, const char *quote, const char escape, char *breaker, int *next, char *quoted)
Definition: token.c:837
MagickExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition: exception.c:411
#define IN_TOKEN
Definition: token.c:792