MagickCore  7.1.0
Convert, Edit, Or Compose Bitmap Images
token.c
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 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"
45 #include "MagickCore/exception-private.h"
46 #include "MagickCore/image.h"
47 #include "MagickCore/image-private.h"
48 #include "MagickCore/locale-private.h"
49 #include "MagickCore/memory_.h"
50 #include "MagickCore/memory-private.h"
51 #include "MagickCore/string_.h"
52 #include "MagickCore/string-private.h"
53 #include "MagickCore/token.h"
54 #include "MagickCore/token-private.h"
55 #include "MagickCore/utility.h"
56 #include "MagickCore/utility-private.h"
57 ␌
58 /*
59  Typedef declaractions.
60 */
61 struct _TokenInfo
62 {
63  int
64  state;
65 
66  MagickStatusType
67  flag;
68 
69  ssize_t
70  offset;
71 
72  char
73  quote;
74 
75  size_t
76  signature;
77 };
78 ␌
79 /*
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % %
82 % %
83 % %
84 % A c q u i r e T o k e n I n f o %
85 % %
86 % %
87 % %
88 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 %
90 % AcquireTokenInfo() allocates the TokenInfo structure.
91 %
92 % The format of the AcquireTokenInfo method is:
93 %
94 % TokenInfo *AcquireTokenInfo()
95 %
96 */
97 MagickExport TokenInfo *AcquireTokenInfo(void)
98 {
99  TokenInfo
100  *token_info;
101 
102  token_info=(TokenInfo *) AcquireCriticalMemory(sizeof(*token_info));
103  token_info->signature=MagickCoreSignature;
104  return(token_info);
105 }
106 ␌
107 /*
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % %
110 % %
111 % %
112 % D e s t r o y T o k e n I n f o %
113 % %
114 % %
115 % %
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 %
118 % DestroyTokenInfo() deallocates memory associated with an TokenInfo
119 % structure.
120 %
121 % The format of the DestroyTokenInfo method is:
122 %
123 % TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
124 %
125 % A description of each parameter follows:
126 %
127 % o token_info: Specifies a pointer to an TokenInfo structure.
128 %
129 */
130 MagickExport TokenInfo *DestroyTokenInfo(TokenInfo *token_info)
131 {
132  assert(token_info != (TokenInfo *) NULL);
133  assert(token_info->signature == MagickCoreSignature);
134  if (IsEventLogging() != MagickFalse)
135  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
136  token_info->signature=(~MagickCoreSignature);
137  token_info=(TokenInfo *) RelinquishMagickMemory(token_info);
138  return(token_info);
139 }
140 ␌
141 /*
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % %
144 % %
145 % %
146 + G e t N e x t T o k e n %
147 % %
148 % %
149 % %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 %
152 % GetNextToken() gets a token from the token stream. A token is defined as
153 % a sequence of characters delimited by whitespace (e.g. clip-path), a
154 % sequence delimited with quotes (.e.g "Quote me"), or a sequence enclosed in
155 % parenthesis (e.g. rgb(0,0,0)). GetNextToken() also recognizes these
156 % separator characters: ':', '=', ',', and ';'. GetNextToken() returns the
157 % length of the consumed token.
158 %
159 % The format of the GetNextToken method is:
160 %
161 % size_t GetNextToken(const char *magick_restrict start,
162 % const char **magick_restrict end,const size_t extent,
163 % char *magick_restrict token)
164 %
165 % A description of each parameter follows:
166 %
167 % o start: the start of the token sequence.
168 %
169 % o end: point to the end of the token sequence.
170 %
171 % o extent: maximum extent of the token.
172 %
173 % o token: copy the token to this buffer.
174 %
175 */
176 MagickExport magick_hot_spot size_t GetNextToken(
177  const char *magick_restrict start,const char **magick_restrict end,
178  const size_t extent,char *magick_restrict token)
179 {
180  char
181  *magick_restrict q;
182 
183  const char
184  *magick_restrict p;
185 
186  double
187  value;
188 
189  ssize_t
190  i;
191 
192  assert(start != (const char *) NULL);
193  assert(token != (char *) NULL);
194  i=0;
195  p=start;
196  while ((isspace((int) ((unsigned char) *p)) != 0) && (*p != '\0'))
197  p++;
198  switch (*p)
199  {
200  case '\0':
201  break;
202  case '"':
203  case '\'':
204  case '`':
205  case '{':
206  {
207  char
208  escape;
209 
210  switch (*p)
211  {
212  case '"': escape='"'; break;
213  case '\'': escape='\''; break;
214  case '`': escape='\''; break;
215  case '{': escape='}'; break;
216  default: escape=(*p); break;
217  }
218  for (p++; *p != '\0'; p++)
219  {
220  if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
221  p++;
222  else
223  if (*p == escape)
224  {
225  p++;
226  break;
227  }
228  if (i < (ssize_t) (extent-1))
229  token[i++]=(*p);
230  if ((size_t) (p-start) >= (extent-1))
231  break;
232  }
233  break;
234  }
235  case '/':
236  {
237  if (i < (ssize_t) (extent-1))
238  token[i++]=(*p);
239  p++;
240  if ((*p == '>') || (*p == '/'))
241  {
242  if (i < (ssize_t) (extent-1))
243  token[i++]=(*p);
244  p++;
245  }
246  break;
247  }
248  default:
249  {
250  value=StringToDouble(p,&q);
251  (void) value;
252  if ((p != q) && (*p != ','))
253  {
254  for ( ; (p < q) && (*p != ','); p++)
255  {
256  if (i < (ssize_t) (extent-1))
257  token[i++]=(*p);
258  if ((size_t) (p-start) >= (extent-1))
259  break;
260  }
261  if (*p == '%')
262  {
263  if (i < (ssize_t) (extent-1))
264  token[i++]=(*p);
265  p++;
266  }
267  break;
268  }
269  if ((*p != '\0') && (isalpha((int) ((unsigned char) *p)) == 0) &&
270  (*p != *DirectorySeparator) && (*p != '#') && (*p != '<'))
271  {
272  if (i < (ssize_t) (extent-1))
273  token[i++]=(*p);
274  p++;
275  break;
276  }
277  for ( ; *p != '\0'; p++)
278  {
279  if (((isspace((int) ((unsigned char) *p)) != 0) || (*p == '=') ||
280  (*p == ',') || (*p == ':') || (*p == ';')) && (*(p-1) != '\\'))
281  break;
282  if ((i > 0) && (*p == '<'))
283  break;
284  if (i < (ssize_t) (extent-1))
285  token[i++]=(*p);
286  if (*p == '>')
287  break;
288  if (*p == '(')
289  {
290  for (p++; *p != '\0'; p++)
291  {
292  if (i < (ssize_t) (extent-1))
293  token[i++]=(*p);
294  if ((*p == ')') && (*(p-1) != '\\'))
295  break;
296  if ((size_t) (p-start) >= (extent-1))
297  break;
298  }
299  if (*p == '\0')
300  break;
301  }
302  if ((size_t) (p-start) >= (extent-1))
303  break;
304  }
305  break;
306  }
307  }
308  token[i]='\0';
309  if (LocaleNCompare(token,"url(#",5) == 0)
310  {
311  q=strrchr(token,')');
312  if (q != (char *) NULL)
313  {
314  *q='\0';
315  (void) memmove(token,token+5,(size_t) (q-token-4));
316  }
317  }
318  while (isspace((int) ((unsigned char) *p)) != 0)
319  p++;
320  if (end != (const char **) NULL)
321  *end=(const char *) p;
322  return(p-start+1);
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 *magick_restrict expression,
341 % const char *magick_restrict pattern,
342 % const MagickBooleanType case_insensitive)
343 %
344 % A description of each parameter follows:
345 %
346 % o expression: Specifies a pointer to a text string containing a file name.
347 %
348 % o pattern: Specifies a pointer to a text string containing a pattern.
349 %
350 % o case_insensitive: set to MagickTrue to ignore the case when matching
351 % an expression.
352 %
353 */
354 MagickExport MagickBooleanType GlobExpression(
355  const char *magick_restrict expression,const char *magick_restrict pattern,
356  const MagickBooleanType case_insensitive)
357 {
358  MagickBooleanType
359  done,
360  match;
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  if ((GetUTFCode(pattern+strlen(pattern)-1) == ']') &&
372  (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  {
408  MagickBooleanType
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  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 (LocaleToLowercase((int) GetUTFCode(expression)) != LocaleToLowercase((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) ?
569  MagickTrue : MagickFalse;
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 */
596 MagickPrivate MagickBooleanType IsGlob(const char *path)
597 {
598  MagickBooleanType
599  status = MagickFalse;
600 
601  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  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  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) LocaleToUppercase(c);
823  break;
824  }
825  case 2:
826  {
827  string[i]=(char) LocaleToLowercase(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  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 }