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