MagickCore  7.0.3
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-2019 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 ';'. GetNextToken() returns the
155 % length of the consumed token.
156 %
157 % The format of the GetNextToken method is:
158 %
159 % size_t GetNextToken(const char *start,const char **end,
160 % const size_t extent,char *token)
161 %
162 % A description of each parameter follows:
163 %
164 % o start: the start of the token sequence.
165 %
166 % o end: point to the end of the token sequence.
167 %
168 % o extent: maximum extent of the token.
169 %
170 % o token: copy the token to this buffer.
171 %
172 */
173 MagickExport size_t GetNextToken(const char *start,const char **end,
174  const size_t extent,char *token)
175 {
176  double
177  value;
178 
179  register char
180  *q;
181 
182  register const char
183  *p;
184 
185  register ssize_t
186  i;
187 
188  assert(start != (const char *) NULL);
189  assert(token != (char *) NULL);
190  i=0;
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) >= (extent-1))
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) >= (extent-1))
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  {
289  for (p++; *p != '\0'; p++)
290  {
291  if (i < (ssize_t) (extent-1))
292  token[i++]=(*p);
293  if ((*p == ')') && (*(p-1) != '\\'))
294  break;
295  if ((size_t) (p-start) >= (extent-1))
296  break;
297  }
298  if (*p == '\0')
299  break;
300  }
301  if ((size_t) (p-start) >= (extent-1))
302  break;
303  }
304  break;
305  }
306  }
307  token[i]='\0';
308  if (LocaleNCompare(token,"url(#",5) == 0)
309  {
310  q=strrchr(token,')');
311  if (q != (char *) NULL)
312  {
313  *q='\0';
314  (void) memmove(token,token+5,(size_t) (q-token-4));
315  }
316  }
317  while (isspace((int) ((unsigned char) *p)) != 0)
318  p++;
319  if (end != (const char **) NULL)
320  *end=(const char *) p;
321  return(p-start+1);
322 }
323 
324 /*
325 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 % %
327 % %
328 % %
329 % G l o b E x p r e s s i o n %
330 % %
331 % %
332 % %
333 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334 %
335 % GlobExpression() returns MagickTrue if the expression matches the pattern.
336 %
337 % The format of the GlobExpression function is:
338 %
339 % MagickBooleanType GlobExpression(const char *expression,
340 % const char *pattern,const MagickBooleanType case_insensitive)
341 %
342 % A description of each parameter follows:
343 %
344 % o expression: Specifies a pointer to a text string containing a file name.
345 %
346 % o pattern: Specifies a pointer to a text string containing a pattern.
347 %
348 % o case_insensitive: set to MagickTrue to ignore the case when matching
349 % an expression.
350 %
351 */
353  const char *pattern,const MagickBooleanType case_insensitive)
354 {
356  done,
357  match;
358 
359  register const char
360  *p;
361 
362  /*
363  Return on empty pattern or '*'.
364  */
365  if (pattern == (char *) NULL)
366  return(MagickTrue);
367  if (GetUTFCode(pattern) == 0)
368  return(MagickTrue);
369  if (LocaleCompare(pattern,"*") == 0)
370  return(MagickTrue);
371  p=pattern+strlen(pattern)-1;
372  if ((GetUTFCode(p) == ']') && (strchr(pattern,'[') != (char *) NULL))
373  {
375  *exception;
376 
377  ImageInfo
378  *image_info;
379 
380  /*
381  Determine if pattern is a scene, i.e. img0001.pcd[2].
382  */
383  image_info=AcquireImageInfo();
384  (void) CopyMagickString(image_info->filename,pattern,MagickPathExtent);
385  exception=AcquireExceptionInfo();
386  (void) SetImageInfo(image_info,0,exception);
387  exception=DestroyExceptionInfo(exception);
388  if (LocaleCompare(image_info->filename,pattern) != 0)
389  {
390  image_info=DestroyImageInfo(image_info);
391  return(MagickFalse);
392  }
393  image_info=DestroyImageInfo(image_info);
394  }
395  /*
396  Evaluate glob expression.
397  */
398  done=MagickFalse;
399  while ((GetUTFCode(pattern) != 0) && (done == MagickFalse))
400  {
401  if (GetUTFCode(expression) == 0)
402  if ((GetUTFCode(pattern) != '{') && (GetUTFCode(pattern) != '*'))
403  break;
404  switch (GetUTFCode(pattern))
405  {
406  case '*':
407  {
409  status;
410 
411  status=MagickFalse;
412  while (GetUTFCode(pattern) == '*')
413  pattern+=GetUTFOctets(pattern);
414  while ((GetUTFCode(expression) != 0) && (status == MagickFalse))
415  {
416  status=GlobExpression(expression,pattern,case_insensitive);
417  expression+=GetUTFOctets(expression);
418  }
419  if (status != MagickFalse)
420  {
421  while (GetUTFCode(expression) != 0)
422  expression+=GetUTFOctets(expression);
423  while (GetUTFCode(pattern) != 0)
424  pattern+=GetUTFOctets(pattern);
425  }
426  break;
427  }
428  case '[':
429  {
430  int
431  c;
432 
433  pattern+=GetUTFOctets(pattern);
434  for ( ; ; )
435  {
436  if ((GetUTFCode(pattern) == 0) || (GetUTFCode(pattern) == ']'))
437  {
438  done=MagickTrue;
439  break;
440  }
441  if (GetUTFCode(pattern) == '\\')
442  {
443  pattern+=GetUTFOctets(pattern);
444  if (GetUTFCode(pattern) == 0)
445  {
446  done=MagickTrue;
447  break;
448  }
449  }
450  if (GetUTFCode(pattern+GetUTFOctets(pattern)) == '-')
451  {
452  c=GetUTFCode(pattern);
453  pattern+=GetUTFOctets(pattern);
454  pattern+=GetUTFOctets(pattern);
455  if (GetUTFCode(pattern) == ']')
456  {
457  done=MagickTrue;
458  break;
459  }
460  if (GetUTFCode(pattern) == '\\')
461  {
462  pattern+=GetUTFOctets(pattern);
463  if (GetUTFCode(pattern) == 0)
464  {
465  done=MagickTrue;
466  break;
467  }
468  }
469  if ((GetUTFCode(expression) < c) ||
470  (GetUTFCode(expression) > GetUTFCode(pattern)))
471  {
472  pattern+=GetUTFOctets(pattern);
473  continue;
474  }
475  }
476  else
477  if (GetUTFCode(pattern) != GetUTFCode(expression))
478  {
479  pattern+=GetUTFOctets(pattern);
480  continue;
481  }
482  pattern+=GetUTFOctets(pattern);
483  while ((GetUTFCode(pattern) != ']') && (GetUTFCode(pattern) != 0))
484  {
485  if ((GetUTFCode(pattern) == '\\') &&
486  (GetUTFCode(pattern+GetUTFOctets(pattern)) > 0))
487  pattern+=GetUTFOctets(pattern);
488  pattern+=GetUTFOctets(pattern);
489  }
490  if (GetUTFCode(pattern) != 0)
491  {
492  pattern+=GetUTFOctets(pattern);
493  expression+=GetUTFOctets(expression);
494  }
495  break;
496  }
497  break;
498  }
499  case '?':
500  {
501  pattern+=GetUTFOctets(pattern);
502  expression+=GetUTFOctets(expression);
503  break;
504  }
505  case '{':
506  {
507  char
508  *target;
509 
510  register char
511  *p;
512 
513  target=AcquireString(pattern);
514  p=target;
515  pattern++;
516  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
517  {
518  *p++=(*pattern++);
519  if ((GetUTFCode(pattern) == ',') || (GetUTFCode(pattern) == '}'))
520  {
521  *p='\0';
522  match=GlobExpression(expression,target,case_insensitive);
523  if (match != MagickFalse)
524  {
525  expression+=MagickMin(strlen(expression),strlen(target));
526  break;
527  }
528  p=target;
529  pattern+=GetUTFOctets(pattern);
530  }
531  }
532  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
533  pattern+=GetUTFOctets(pattern);
534  if (GetUTFCode(pattern) != 0)
535  pattern+=GetUTFOctets(pattern);
536  target=DestroyString(target);
537  break;
538  }
539  case '\\':
540  {
541  pattern+=GetUTFOctets(pattern);
542  if (GetUTFCode(pattern) == 0)
543  break;
544  }
545  default:
546  {
547  if (case_insensitive != MagickFalse)
548  {
549  if (LocaleLowercase((int) GetUTFCode(expression)) != LocaleLowercase((int) GetUTFCode(pattern)))
550  {
551  done=MagickTrue;
552  break;
553  }
554  }
555  else
556  if (GetUTFCode(expression) != GetUTFCode(pattern))
557  {
558  done=MagickTrue;
559  break;
560  }
561  expression+=GetUTFOctets(expression);
562  pattern+=GetUTFOctets(pattern);
563  }
564  }
565  }
566  while (GetUTFCode(pattern) == '*')
567  pattern+=GetUTFOctets(pattern);
568  match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
570  return(match);
571 }
572 
573 /*
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 % %
576 % %
577 % %
578 + I s G l o b %
579 % %
580 % %
581 % %
582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583 %
584 % IsGlob() returns MagickTrue if the path specification contains a globbing
585 % pattern.
586 %
587 % The format of the IsGlob method is:
588 %
589 % MagickBooleanType IsGlob(const char *geometry)
590 %
591 % A description of each parameter follows:
592 %
593 % o path: the path.
594 %
595 */
597 {
599  status = MagickFalse;
600 
601  register const char
602  *p;
603 
604  if (IsPathAccessible(path) != MagickFalse)
605  return(MagickFalse);
606  for (p=path; *p != '\0'; p++)
607  {
608  switch (*p)
609  {
610  case '*':
611  case '?':
612  case '{':
613  case '}':
614  case '[':
615  case ']':
616  {
617  status=MagickTrue;
618  break;
619  }
620  default:
621  break;
622  }
623  }
624  return(status);
625 }
626 
627 /*
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % %
630 % %
631 % %
632 % T o k e n i z e r %
633 % %
634 % %
635 % %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 %
638 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
639 % one at a time from a string of characters. The characters used for white
640 % space, for break characters, and for quotes can be specified. Also,
641 % characters in the string can be preceded by a specifiable escape character
642 % which removes any special meaning the character may have.
643 %
644 % Here is some terminology:
645 %
646 % o token: A single unit of information in the form of a group of
647 % characters.
648 %
649 % o white space: Apace that gets ignored (except within quotes or when
650 % escaped), like blanks and tabs. in addition, white space terminates a
651 % non-quoted token.
652 %
653 % o break set: One or more characters that separates non-quoted tokens.
654 % Commas are a common break character. The usage of break characters to
655 % signal the end of a token is the same as that of white space, except
656 % multiple break characters with nothing or only white space between
657 % generate a null token for each two break characters together.
658 %
659 % For example, if blank is set to be the white space and comma is set to
660 % be the break character, the line
661 %
662 % A, B, C , , DEF
663 %
664 % ... consists of 5 tokens:
665 %
666 % 1) "A"
667 % 2) "B"
668 % 3) "C"
669 % 4) "" (the null string)
670 % 5) "DEF"
671 %
672 % o Quote character: A character that, when surrounding a group of other
673 % characters, causes the group of characters to be treated as a single
674 % token, no matter how many white spaces or break characters exist in
675 % the group. Also, a token always terminates after the closing quote.
676 % For example, if ' is the quote character, blank is white space, and
677 % comma is the break character, the following string
678 %
679 % A, ' B, CD'EF GHI
680 %
681 % ... consists of 4 tokens:
682 %
683 % 1) "A"
684 % 2) " B, CD" (note the blanks & comma)
685 % 3) "EF"
686 % 4) "GHI"
687 %
688 % The quote characters themselves do not appear in the resultant
689 % tokens. The double quotes are delimiters i use here for
690 % documentation purposes only.
691 %
692 % o Escape character: A character which itself is ignored but which
693 % causes the next character to be used as is. ^ and \ are often used
694 % as escape characters. An escape in the last position of the string
695 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
696 % and non-escape) character. For example, assume white space, break
697 % character, and quote are the same as in the above examples, and
698 % further, assume that ^ is the escape character. Then, in the string
699 %
700 % ABC, ' DEF ^' GH' I ^ J K^ L ^
701 %
702 % ... there are 7 tokens:
703 %
704 % 1) "ABC"
705 % 2) " DEF ' GH"
706 % 3) "I"
707 % 4) " " (a lone blank)
708 % 5) "J"
709 % 6) "K L"
710 % 7) "^" (passed as is at end of line)
711 %
712 % The format of the Tokenizer method is:
713 %
714 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
715 % const size_t max_token_length,const char *line,const char *white,
716 % const char *break_set,const char *quote,const char escape,
717 % char *breaker,int *next,char *quoted)
718 %
719 % A description of each parameter follows:
720 %
721 % o flag: right now, only the low order 3 bits are used.
722 %
723 % 1 => convert non-quoted tokens to upper case
724 % 2 => convert non-quoted tokens to lower case
725 % 0 => do not convert non-quoted tokens
726 %
727 % o token: a character string containing the returned next token
728 %
729 % o max_token_length: the maximum size of "token". Characters beyond
730 % "max_token_length" are truncated.
731 %
732 % o string: the string to be parsed.
733 %
734 % o white: a string of the valid white spaces. example:
735 %
736 % char whitesp[]={" \t"};
737 %
738 % blank and tab will be valid white space.
739 %
740 % o break: a string of the valid break characters. example:
741 %
742 % char breakch[]={";,"};
743 %
744 % semicolon and comma will be valid break characters.
745 %
746 % o quote: a string of the valid quote characters. An example would be
747 %
748 % char whitesp[]={"'\"");
749 %
750 % (this causes single and double quotes to be valid) Note that a
751 % token starting with one of these characters needs the same quote
752 % character to terminate it.
753 %
754 % for example:
755 %
756 % "ABC '
757 %
758 % is unterminated, but
759 %
760 % "DEF" and 'GHI'
761 %
762 % are properly terminated. Note that different quote characters
763 % can appear on the same line; only for a given token do the quote
764 % characters have to be the same.
765 %
766 % o escape: the escape character (NOT a string ... only one
767 % allowed). Use zero if none is desired.
768 %
769 % o breaker: the break character used to terminate the current
770 % token. If the token was quoted, this will be the quote used. If
771 % the token is the last one on the line, this will be zero.
772 %
773 % o next: this variable points to the first character of the
774 % next token. it gets reset by "tokenizer" as it steps through the
775 % string. Set it to 0 upon initialization, and leave it alone
776 % after that. You can change it if you want to jump around in the
777 % string or re-parse from the beginning, but be careful.
778 %
779 % o quoted: set to True if the token was quoted and MagickFalse
780 % if not. You may need this information (for example: in C, a
781 % string with quotes around it is a character string, while one
782 % without is an identifier).
783 %
784 % o result: 0 if we haven't reached EOS (end of string), and 1
785 % if we have.
786 %
787 */
788 
789 #define IN_WHITE 0
790 #define IN_TOKEN 1
791 #define IN_QUOTE 2
792 #define IN_OZONE 3
793 
794 static ssize_t sindex(int c,const char *string)
795 {
796  register const char
797  *p;
798 
799  for (p=string; *p != '\0'; p++)
800  if (c == (int) (*p))
801  return((ssize_t) (p-string));
802  return(-1);
803 }
804 
805 static void StoreToken(TokenInfo *token_info,char *string,
806  size_t max_token_length,int c)
807 {
808  register ssize_t
809  i;
810 
811  if ((token_info->offset < 0) ||
812  ((size_t) token_info->offset >= (max_token_length-1)))
813  return;
814  i=token_info->offset++;
815  string[i]=(char) c;
816  if (token_info->state == IN_QUOTE)
817  return;
818  switch (token_info->flag & 0x03)
819  {
820  case 1:
821  {
822  string[i]=(char) LocaleUppercase(c);
823  break;
824  }
825  case 2:
826  {
827  string[i]=(char) LocaleLowercase(c);
828  break;
829  }
830  default:
831  break;
832  }
833 }
834 
835 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
836  char *token,const size_t max_token_length,const char *line,const char *white,
837  const char *break_set,const char *quote,const char escape,char *breaker,
838  int *next,char *quoted)
839 {
840  int
841  c;
842 
843  register ssize_t
844  i;
845 
846  *breaker='\0';
847  *quoted='\0';
848  if (line[*next] == '\0')
849  return(1);
850  token_info->state=IN_WHITE;
851  token_info->quote=(char) MagickFalse;
852  token_info->flag=flag;
853  for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
854  {
855  c=(int) line[*next];
856  i=sindex(c,break_set);
857  if (i >= 0)
858  {
859  switch (token_info->state)
860  {
861  case IN_WHITE:
862  case IN_TOKEN:
863  case IN_OZONE:
864  {
865  (*next)++;
866  *breaker=break_set[i];
867  token[token_info->offset]='\0';
868  return(0);
869  }
870  case IN_QUOTE:
871  {
872  StoreToken(token_info,token,max_token_length,c);
873  break;
874  }
875  }
876  continue;
877  }
878  i=sindex(c,quote);
879  if (i >= 0)
880  {
881  switch (token_info->state)
882  {
883  case IN_WHITE:
884  {
885  token_info->state=IN_QUOTE;
886  token_info->quote=quote[i];
887  *quoted=(char) MagickTrue;
888  break;
889  }
890  case IN_QUOTE:
891  {
892  if (quote[i] != token_info->quote)
893  StoreToken(token_info,token,max_token_length,c);
894  else
895  {
896  token_info->state=IN_OZONE;
897  token_info->quote='\0';
898  }
899  break;
900  }
901  case IN_TOKEN:
902  case IN_OZONE:
903  {
904  *breaker=(char) c;
905  token[token_info->offset]='\0';
906  return(0);
907  }
908  }
909  continue;
910  }
911  i=sindex(c,white);
912  if (i >= 0)
913  {
914  switch (token_info->state)
915  {
916  case IN_WHITE:
917  case IN_OZONE:
918  break;
919  case IN_TOKEN:
920  {
921  token_info->state=IN_OZONE;
922  break;
923  }
924  case IN_QUOTE:
925  {
926  StoreToken(token_info,token,max_token_length,c);
927  break;
928  }
929  }
930  continue;
931  }
932  if (c == (int) escape)
933  {
934  if (line[(*next)+1] == '\0')
935  {
936  *breaker='\0';
937  StoreToken(token_info,token,max_token_length,c);
938  (*next)++;
939  token[token_info->offset]='\0';
940  return(0);
941  }
942  switch (token_info->state)
943  {
944  case IN_WHITE:
945  {
946  (*next)--;
947  token_info->state=IN_TOKEN;
948  break;
949  }
950  case IN_TOKEN:
951  case IN_QUOTE:
952  {
953  (*next)++;
954  c=(int) line[*next];
955  StoreToken(token_info,token,max_token_length,c);
956  break;
957  }
958  case IN_OZONE:
959  {
960  token[token_info->offset]='\0';
961  return(0);
962  }
963  }
964  continue;
965  }
966  switch (token_info->state)
967  {
968  case IN_WHITE:
969  {
970  token_info->state=IN_TOKEN;
971  StoreToken(token_info,token,max_token_length,c);
972  break;
973  }
974  case IN_TOKEN:
975  case IN_QUOTE:
976  {
977  StoreToken(token_info,token,max_token_length,c);
978  break;
979  }
980  case IN_OZONE:
981  {
982  token[token_info->offset]='\0';
983  return(0);
984  }
985  }
986  }
987  token[token_info->offset]='\0';
988  return(0);
989 }
static ssize_t sindex(int c, const char *string)
Definition: token.c:794
MagickExport TokenInfo * DestroyTokenInfo(TokenInfo *token_info)
Definition: token.c:129
MagickExport ImageInfo * AcquireImageInfo(void)
Definition: image.c:345
MagickExport int LocaleLowercase(const int c)
Definition: locale.c:1522
#define IN_OZONE
Definition: token.c:792
static double StringToDouble(const char *magick_restrict string, char **magick_restrict sentinal)
MagickExport ExceptionInfo * AcquireExceptionInfo(void)
Definition: exception.c:115
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
MagickExport size_t GetNextToken(const char *start, const char **end, const size_t extent, char *token)
Definition: token.c:173
Definition: log.h:52
static void StoreToken(TokenInfo *token_info, char *string, size_t max_token_length, int c)
Definition: token.c:805
MagickExport TokenInfo * AcquireTokenInfo(void)
Definition: token.c:96
#define MagickCoreSignature
MagickExport int LocaleUppercase(const int c)
Definition: locale.c:1662
MagickBooleanType
Definition: magick-type.h:158
#define DirectorySeparator
Definition: studio.h:259
unsigned int MagickStatusType
Definition: magick-type.h:121
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:1570
MagickPrivate MagickBooleanType IsGlob(const char *) magick_attribute((__pure__))
Definition: token.c:596
MagickExport MagickBooleanType GlobExpression(const char *expression, const char *pattern, const MagickBooleanType case_insensitive)
Definition: token.c:352
#define MagickPathExtent
#define IN_WHITE
Definition: token.c:789
static unsigned int GetUTFOctets(const char *text)
MagickExport MagickBooleanType SetImageInfo(ImageInfo *image_info, const unsigned int frames, ExceptionInfo *exception)
Definition: image.c:2703
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:1413
MagickExport MagickBooleanType IsPathAccessible(const char *path)
Definition: utility.c:1467
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:1435
#define GetMagickModule()
Definition: log.h:28
MagickExport ImageInfo * DestroyImageInfo(ImageInfo *image_info)
Definition: image.c:1252
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:1069
#define MagickPrivate
#define MagickExport
#define IN_QUOTE
Definition: token.c:791
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:835
MagickExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition: exception.c:418
#define IN_TOKEN
Definition: token.c:790