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://www.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"
47 #include "MagickCore/memory_.h"
49 #include "MagickCore/string_.h"
51 #include "MagickCore/token.h"
53 #include "MagickCore/utility.h"
55 
56 /*
57  Typedef declaractions.
58 */
59 struct _TokenInfo
60 {
61  int
63 
66 
67  ssize_t
69 
70  char
72 
73  size_t
75 };
76 
77 /*
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % %
80 % %
81 % %
82 % A c q u i r e T o k e n I n f o %
83 % %
84 % %
85 % %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 %
88 % AcquireTokenInfo() allocates the TokenInfo structure.
89 %
90 % The format of the AcquireTokenInfo method is:
91 %
92 % TokenInfo *AcquireTokenInfo()
93 %
94 */
96 {
97  TokenInfo
98  *token_info;
99 
100  token_info=(TokenInfo *) AcquireCriticalMemory(sizeof(*token_info));
101  token_info->signature=MagickCoreSignature;
102  return(token_info);
103 }
104 
105 /*
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % %
108 % %
109 % %
110 % D e s t r o y T o k e n I n f o %
111 % %
112 % %
113 % %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %
116 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
117 % structure.
118 %
119 % The format of the DestroyTokenInfo method is:
120 %
121 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
122 %
123 % A description of each parameter follows:
124 %
125 % o token_info: Specifies a pointer to an TokenInfo structure.
126 %
127 */
129 {
130  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
131  assert(token_info != (TokenInfo *) NULL);
132  assert(token_info->signature == MagickCoreSignature);
133  token_info->signature=(~MagickCoreSignature);
134  token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
135  return(token_info);
136 }
137 
138 /*
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 % %
141 % %
142 % %
143 + G e t N e x t T o k e n %
144 % %
145 % %
146 % %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %
149 % GetNextToken() 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)). GetNextToken() also recognizes these
153 % separator characters: ':', '=', ',', and ';'.
154 %
155 % The format of the GetNextToken method is:
156 %
157 % void GetNextToken(const char *start,const char **end,
158 % const size_t extent,char *token)
159 %
160 % A description of each parameter follows:
161 %
162 % o start: the start of the token sequence.
163 %
164 % o end: point to the end of the token sequence.
165 %
166 % o extent: maximum extent of the token.
167 %
168 % o token: copy the token to this buffer.
169 %
170 */
171 MagickExport void GetNextToken(const char *start,const char **end,
172  const size_t extent,char *token)
173 {
174  double
175  value;
176 
177  register const char
178  *p;
179 
180  register ssize_t
181  i;
182 
183  size_t
184  length;
185 
186  assert(start != (const char *) NULL);
187  assert(token != (char *) NULL);
188  i=0;
189  length=strlen(start);
190  p=start;
191  while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
192  p++;
193  switch (*p)
194  {
195  case '\0':
196  break;
197  case '"':
198  case '\'':
199  case '`':
200  case '{':
201  {
202  register char
203  escape;
204 
205  switch (*p)
206  {
207  case '"': escape='"'; break;
208  case '\'': escape='\''; break;
209  case '`': escape='\''; break;
210  case '{': escape='}'; break;
211  default: escape=(*p); break;
212  }
213  for (p++; *p != '\0'; p++)
214  {
215  if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
216  p++;
217  else
218  if (*p == escape)
219  {
220  p++;
221  break;
222  }
223  if (i < (ssize_t) (extent-1))
224  token[i++]=(*p);
225  if ((size_t) (p-start) >= length)
226  break;
227  }
228  break;
229  }
230  case '/':
231  {
232  if (i < (ssize_t) (extent-1))
233  token[i++]=(*p);
234  p++;
235  if ((*p == '>') || (*p == '/'))
236  {
237  if (i < (ssize_t) (extent-1))
238  token[i++]=(*p);
239  p++;
240  }
241  break;
242  }
243  default:
244  {
245  char
246  *q;
247 
248  value=StringToDouble(p,&q);
249  (void) value;
250  if ((p != q) && (*p != ','))
251  {
252  for ( ; (p < q) && (*p != ','); p++)
253  {
254  if (i < (ssize_t) (extent-1))
255  token[i++]=(*p);
256  if ((size_t) (p-start) >= length)
257  break;
258  }
259  if (*p == '%')
260  {
261  if (i < (ssize_t) (extent-1))
262  token[i++]=(*p);
263  p++;
264  }
265  break;
266  }
267  if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
268  (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
269  {
270  if (i < (ssize_t) (extent-1))
271  token[i++]=(*p);
272  p++;
273  break;
274  }
275  for ( ; *p != '\0'; p++)
276  {
277  if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
278  (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
279  break;
280  if ((i > 0) && (*p == '<'))
281  break;
282  if (i < (ssize_t) (extent-1))
283  token[i++]=(*p);
284  if (*p == '>')
285  break;
286  if (*p == '(')
287  for (p++; *p != '\0'; p++)
288  {
289  if (i < (ssize_t) (extent-1))
290  token[i++]=(*p);
291  if ((*p == ')') && (*(p-1) != '\\'))
292  break;
293  if ((size_t) (p-start) >= length)
294  break;
295  }
296  if ((size_t) (p-start) >= length)
297  break;
298  }
299  break;
300  }
301  }
302  token[i]='\0';
303  if ((LocaleNCompare(token,"url(",4) == 0) && (strlen(token) > 5))
304  {
305  ssize_t
306  offset;
307 
308  offset=4;
309  if (token[offset] == '#')
310  offset++;
311  i=(ssize_t) strlen(token);
312  if (i > offset)
313  {
314  (void) CopyMagickString(token,token+offset,MagickPathExtent);
315  token[i-offset-1]='\0';
316  }
317  }
318  while (isspace((int) ((unsigned char) *p)) != 0)
319  p++;
320  if (end != (const char **) NULL)
321  *end=(const char *) p;
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  pattern+=GetUTFOctets(pattern);
508  while ((GetUTFCode(pattern) != '}') && (GetUTFCode(pattern) != 0))
509  {
510  p=expression;
511  match=MagickTrue;
512  while ((GetUTFCode(p) != 0) && (GetUTFCode(pattern) != 0) &&
513  (GetUTFCode(pattern) != ',') && (GetUTFCode(pattern) != '}') &&
514  (match != MagickFalse))
515  {
516  if (GetUTFCode(pattern) == '\\')
517  pattern+=GetUTFOctets(pattern);
518  match=(GetUTFCode(pattern) == GetUTFCode(p)) ? MagickTrue :
519  MagickFalse;
520  p+=GetUTFOctets(p);
521  pattern+=GetUTFOctets(pattern);
522  }
523  if (GetUTFCode(pattern) == 0)
524  {
525  match=MagickFalse;
526  done=MagickTrue;
527  break;
528  }
529  if (match != MagickFalse)
530  {
531  expression=p;
532  while ((GetUTFCode(pattern) != '}') &&
533  (GetUTFCode(pattern) != 0))
534  {
535  pattern+=GetUTFOctets(pattern);
536  if (GetUTFCode(pattern) == '\\')
537  {
538  pattern+=GetUTFOctets(pattern);
539  if (GetUTFCode(pattern) == '}')
540  pattern+=GetUTFOctets(pattern);
541  }
542  }
543  }
544  else
545  {
546  while ((GetUTFCode(pattern) != '}') &&
547  (GetUTFCode(pattern) != ',') &&
548  (GetUTFCode(pattern) != 0))
549  {
550  pattern+=GetUTFOctets(pattern);
551  if (GetUTFCode(pattern) == '\\')
552  {
553  pattern+=GetUTFOctets(pattern);
554  if ((GetUTFCode(pattern) == '}') ||
555  (GetUTFCode(pattern) == ','))
556  pattern+=GetUTFOctets(pattern);
557  }
558  }
559  }
560  if (GetUTFCode(pattern) != 0)
561  pattern+=GetUTFOctets(pattern);
562  }
563  break;
564  }
565  case '\\':
566  {
567  pattern+=GetUTFOctets(pattern);
568  if (GetUTFCode(pattern) == 0)
569  break;
570  }
571  default:
572  {
573  if (case_insensitive != MagickFalse)
574  {
575  if (tolower((int) GetUTFCode(expression)) !=
576  tolower((int) GetUTFCode(pattern)))
577  {
578  done=MagickTrue;
579  break;
580  }
581  }
582  else
583  if (GetUTFCode(expression) != GetUTFCode(pattern))
584  {
585  done=MagickTrue;
586  break;
587  }
588  expression+=GetUTFOctets(expression);
589  pattern+=GetUTFOctets(pattern);
590  }
591  }
592  }
593  while (GetUTFCode(pattern) == '*')
594  pattern+=GetUTFOctets(pattern);
595  match=(GetUTFCode(expression) == 0) && (GetUTFCode(pattern) == 0) ?
597  return(match);
598 }
599 
600 /*
601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
602 % %
603 % %
604 % %
605 + I s G l o b %
606 % %
607 % %
608 % %
609 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610 %
611 % IsGlob() returns MagickTrue if the path specification contains a globbing
612 % pattern.
613 %
614 % The format of the IsGlob method is:
615 %
616 % MagickBooleanType IsGlob(const char *geometry)
617 %
618 % A description of each parameter follows:
619 %
620 % o path: the path.
621 %
622 */
624 {
626  status = MagickFalse;
627 
628  register const char
629  *p;
630 
631  if (IsPathAccessible(path) != MagickFalse)
632  return(MagickFalse);
633  for (p=path; *p != '\0'; p++)
634  {
635  switch (*p)
636  {
637  case '*':
638  case '?':
639  case '{':
640  case '}':
641  case '[':
642  case ']':
643  {
644  status=MagickTrue;
645  break;
646  }
647  default:
648  break;
649  }
650  }
651  return(status);
652 }
653 
654 /*
655 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
656 % %
657 % %
658 % %
659 % T o k e n i z e r %
660 % %
661 % %
662 % %
663 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 %
665 % Tokenizer() is a generalized, finite state token parser. It extracts tokens
666 % one at a time from a string of characters. The characters used for white
667 % space, for break characters, and for quotes can be specified. Also,
668 % characters in the string can be preceded by a specifiable escape character
669 % which removes any special meaning the character may have.
670 %
671 % Here is some terminology:
672 %
673 % o token: A single unit of information in the form of a group of
674 % characters.
675 %
676 % o white space: Apace that gets ignored (except within quotes or when
677 % escaped), like blanks and tabs. in addition, white space terminates a
678 % non-quoted token.
679 %
680 % o break set: One or more characters that separates non-quoted tokens.
681 % Commas are a common break character. The usage of break characters to
682 % signal the end of a token is the same as that of white space, except
683 % multiple break characters with nothing or only white space between
684 % generate a null token for each two break characters together.
685 %
686 % For example, if blank is set to be the white space and comma is set to
687 % be the break character, the line
688 %
689 % A, B, C , , DEF
690 %
691 % ... consists of 5 tokens:
692 %
693 % 1) "A"
694 % 2) "B"
695 % 3) "C"
696 % 4) "" (the null string)
697 % 5) "DEF"
698 %
699 % o Quote character: A character that, when surrounding a group of other
700 % characters, causes the group of characters to be treated as a single
701 % token, no matter how many white spaces or break characters exist in
702 % the group. Also, a token always terminates after the closing quote.
703 % For example, if ' is the quote character, blank is white space, and
704 % comma is the break character, the following string
705 %
706 % A, ' B, CD'EF GHI
707 %
708 % ... consists of 4 tokens:
709 %
710 % 1) "A"
711 % 2) " B, CD" (note the blanks & comma)
712 % 3) "EF"
713 % 4) "GHI"
714 %
715 % The quote characters themselves do not appear in the resultant
716 % tokens. The double quotes are delimiters i use here for
717 % documentation purposes only.
718 %
719 % o Escape character: A character which itself is ignored but which
720 % causes the next character to be used as is. ^ and \ are often used
721 % as escape characters. An escape in the last position of the string
722 % gets treated as a "normal" (i.e., non-quote, non-white, non-break,
723 % and non-escape) character. For example, assume white space, break
724 % character, and quote are the same as in the above examples, and
725 % further, assume that ^ is the escape character. Then, in the string
726 %
727 % ABC, ' DEF ^' GH' I ^ J K^ L ^
728 %
729 % ... there are 7 tokens:
730 %
731 % 1) "ABC"
732 % 2) " DEF ' GH"
733 % 3) "I"
734 % 4) " " (a lone blank)
735 % 5) "J"
736 % 6) "K L"
737 % 7) "^" (passed as is at end of line)
738 %
739 % The format of the Tokenizer method is:
740 %
741 % int Tokenizer(TokenInfo *token_info,const unsigned flag,char *token,
742 % const size_t max_token_length,const char *line,const char *white,
743 % const char *break_set,const char *quote,const char escape,
744 % char *breaker,int *next,char *quoted)
745 %
746 % A description of each parameter follows:
747 %
748 % o flag: right now, only the low order 3 bits are used.
749 %
750 % 1 => convert non-quoted tokens to upper case
751 % 2 => convert non-quoted tokens to lower case
752 % 0 => do not convert non-quoted tokens
753 %
754 % o token: a character string containing the returned next token
755 %
756 % o max_token_length: the maximum size of "token". Characters beyond
757 % "max_token_length" are truncated.
758 %
759 % o string: the string to be parsed.
760 %
761 % o white: a string of the valid white spaces. example:
762 %
763 % char whitesp[]={" \t"};
764 %
765 % blank and tab will be valid white space.
766 %
767 % o break: a string of the valid break characters. example:
768 %
769 % char breakch[]={";,"};
770 %
771 % semicolon and comma will be valid break characters.
772 %
773 % o quote: a string of the valid quote characters. An example would be
774 %
775 % char whitesp[]={"'\"");
776 %
777 % (this causes single and double quotes to be valid) Note that a
778 % token starting with one of these characters needs the same quote
779 % character to terminate it.
780 %
781 % for example:
782 %
783 % "ABC '
784 %
785 % is unterminated, but
786 %
787 % "DEF" and 'GHI'
788 %
789 % are properly terminated. Note that different quote characters
790 % can appear on the same line; only for a given token do the quote
791 % characters have to be the same.
792 %
793 % o escape: the escape character (NOT a string ... only one
794 % allowed). Use zero if none is desired.
795 %
796 % o breaker: the break character used to terminate the current
797 % token. If the token was quoted, this will be the quote used. If
798 % the token is the last one on the line, this will be zero.
799 %
800 % o next: this variable points to the first character of the
801 % next token. it gets reset by "tokenizer" as it steps through the
802 % string. Set it to 0 upon initialization, and leave it alone
803 % after that. You can change it if you want to jump around in the
804 % string or re-parse from the beginning, but be careful.
805 %
806 % o quoted: set to True if the token was quoted and MagickFalse
807 % if not. You may need this information (for example: in C, a
808 % string with quotes around it is a character string, while one
809 % without is an identifier).
810 %
811 % o result: 0 if we haven't reached EOS (end of string), and 1
812 % if we have.
813 %
814 */
815 
816 #define IN_WHITE 0
817 #define IN_TOKEN 1
818 #define IN_QUOTE 2
819 #define IN_OZONE 3
820 
821 static ssize_t sindex(int c,const char *string)
822 {
823  register const char
824  *p;
825 
826  for (p=string; *p != '\0'; p++)
827  if (c == (int) (*p))
828  return((ssize_t) (p-string));
829  return(-1);
830 }
831 
832 static void StoreToken(TokenInfo *token_info,char *string,
833  size_t max_token_length,int c)
834 {
835  register ssize_t
836  i;
837 
838  if ((token_info->offset < 0) ||
839  ((size_t) token_info->offset >= (max_token_length-1)))
840  return;
841  i=token_info->offset++;
842  string[i]=(char) c;
843  if (token_info->state == IN_QUOTE)
844  return;
845  switch (token_info->flag & 0x03)
846  {
847  case 1:
848  {
849  string[i]=(char) toupper(c);
850  break;
851  }
852  case 2:
853  {
854  string[i]=(char) tolower(c);
855  break;
856  }
857  default:
858  break;
859  }
860 }
861 
862 MagickExport int Tokenizer(TokenInfo *token_info,const unsigned flag,
863  char *token,const size_t max_token_length,const char *line,const char *white,
864  const char *break_set,const char *quote,const char escape,char *breaker,
865  int *next,char *quoted)
866 {
867  int
868  c;
869 
870  register ssize_t
871  i;
872 
873  *breaker='\0';
874  *quoted='\0';
875  if (line[*next] == '\0')
876  return(1);
877  token_info->state=IN_WHITE;
878  token_info->quote=(char) MagickFalse;
879  token_info->flag=flag;
880  for (token_info->offset=0; (int) line[*next] != 0; (*next)++)
881  {
882  c=(int) line[*next];
883  i=sindex(c,break_set);
884  if (i >= 0)
885  {
886  switch (token_info->state)
887  {
888  case IN_WHITE:
889  case IN_TOKEN:
890  case IN_OZONE:
891  {
892  (*next)++;
893  *breaker=break_set[i];
894  token[token_info->offset]='\0';
895  return(0);
896  }
897  case IN_QUOTE:
898  {
899  StoreToken(token_info,token,max_token_length,c);
900  break;
901  }
902  }
903  continue;
904  }
905  i=sindex(c,quote);
906  if (i >= 0)
907  {
908  switch (token_info->state)
909  {
910  case IN_WHITE:
911  {
912  token_info->state=IN_QUOTE;
913  token_info->quote=quote[i];
914  *quoted=(char) MagickTrue;
915  break;
916  }
917  case IN_QUOTE:
918  {
919  if (quote[i] != token_info->quote)
920  StoreToken(token_info,token,max_token_length,c);
921  else
922  {
923  token_info->state=IN_OZONE;
924  token_info->quote='\0';
925  }
926  break;
927  }
928  case IN_TOKEN:
929  case IN_OZONE:
930  {
931  *breaker=(char) c;
932  token[token_info->offset]='\0';
933  return(0);
934  }
935  }
936  continue;
937  }
938  i=sindex(c,white);
939  if (i >= 0)
940  {
941  switch (token_info->state)
942  {
943  case IN_WHITE:
944  case IN_OZONE:
945  break;
946  case IN_TOKEN:
947  {
948  token_info->state=IN_OZONE;
949  break;
950  }
951  case IN_QUOTE:
952  {
953  StoreToken(token_info,token,max_token_length,c);
954  break;
955  }
956  }
957  continue;
958  }
959  if (c == (int) escape)
960  {
961  if (line[(*next)+1] == '\0')
962  {
963  *breaker='\0';
964  StoreToken(token_info,token,max_token_length,c);
965  (*next)++;
966  token[token_info->offset]='\0';
967  return(0);
968  }
969  switch (token_info->state)
970  {
971  case IN_WHITE:
972  {
973  (*next)--;
974  token_info->state=IN_TOKEN;
975  break;
976  }
977  case IN_TOKEN:
978  case IN_QUOTE:
979  {
980  (*next)++;
981  c=(int) line[*next];
982  StoreToken(token_info,token,max_token_length,c);
983  break;
984  }
985  case IN_OZONE:
986  {
987  token[token_info->offset]='\0';
988  return(0);
989  }
990  }
991  continue;
992  }
993  switch (token_info->state)
994  {
995  case IN_WHITE:
996  {
997  token_info->state=IN_TOKEN;
998  StoreToken(token_info,token,max_token_length,c);
999  break;
1000  }
1001  case IN_TOKEN:
1002  case IN_QUOTE:
1003  {
1004  StoreToken(token_info,token,max_token_length,c);
1005  break;
1006  }
1007  case IN_OZONE:
1008  {
1009  token[token_info->offset]='\0';
1010  return(0);
1011  }
1012  }
1013  }
1014  token[token_info->offset]='\0';
1015  return(0);
1016 }
static ssize_t sindex(int c, const char *string)
Definition: token.c:821
MagickExport TokenInfo * DestroyTokenInfo(TokenInfo *token_info)
Definition: token.c:128
MagickExport ImageInfo * AcquireImageInfo(void)
Definition: image.c:343
MagickPrivate MagickBooleanType IsGlob(const char *path)
Definition: token.c:623
#define IN_OZONE
Definition: token.c:819
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:62
static void * AcquireCriticalMemory(const size_t size)
ssize_t offset
Definition: token.c:68
MagickStatusType flag
Definition: token.c:65
Definition: log.h:52
static void StoreToken(TokenInfo *token_info, char *string, size_t max_token_length, int c)
Definition: token.c:832
MagickExport TokenInfo * AcquireTokenInfo(void)
Definition: token.c:95
#define MagickCoreSignature
MagickBooleanType
Definition: magick-type.h:156
#define DirectorySeparator
Definition: studio.h:259
unsigned int MagickStatusType
Definition: magick-type.h:119
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:352
#define MagickPathExtent
#define IN_WHITE
Definition: token.c:816
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:74
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1397
MagickExport MagickBooleanType IsPathAccessible(const char *path)
Definition: utility.c:1468
MagickExport size_t CopyMagickString(char *destination, const char *source, const size_t length)
Definition: string.c:755
char quote
Definition: token.c:71
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:171
MagickExport ImageInfo * DestroyImageInfo(ImageInfo *image_info)
Definition: image.c:1248
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1053
#define MagickPrivate
#define MagickExport
#define IN_QUOTE
Definition: token.c:818
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:862
MagickExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition: exception.c:411
#define IN_TOKEN
Definition: token.c:817