1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % SSS CCC RRRR III PPPP TTTTT TTTTT OOO K K EEEE N N %
6 % S C R R I P P T T O O K K E NN N %
7 % SSS C RRRR I PPPP T T O O KK EEE N N N %
8 % S C R R I P T T O O K K E N NN %
9 % SSSS CCC R RR III P T T OOO K K EEEE N N %
10 % %
11 % TTTTT EEEEE SSSSS TTTTT %
12 % T E SS T %
13 % T EEE SSS T %
14 % T E SS T %
15 % T EEEEE SSSSS T %
16 % %
17 % Perform "Magick" on Images via the Command Line Interface %
18 % %
19 % Dragon Computing %
20 % Anthony Thyssen %
21 % January 2012 %
22 % %
23 % %
24 % Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization %
25 % dedicated to making software imaging solutions freely available. %
26 % %
27 % You may not use this file except in compliance with the License. You may %
28 % obtain a copy of the License at %
29 % %
30 % https://imagemagick.org/script/license.php %
31 % %
32 % Unless required by applicable law or agreed to in writing, software %
33 % distributed under the License is distributed on an "AS IS" BASIS, %
34 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
35 % See the License for the specific language governing permissions and %
36 % limitations under the License. %
37 % %
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 %
40 % Test the raw tokanization of the ScriptToken Subroutines
41 %
42 % This actually uses very little of the magic core functions
43 % and in fact creates a completely stand-alone program by substituting
44 % required MagickCore with direct system equivelents.
45 %
46 % Build
47 % cc script-token-test.c -o script-token-test
48 %
49 % For testing see script-token-test.sh
50 %
51 */
52
53 /* System Replacement for MagickWand includes */
54 #include <stdio.h>
55 #include <string.h>
56 #include <stdlib.h>
57 #include <assert.h>
58 #include <errno.h>
59
60 /* Defines to replace MagickWand / MagickCore defintions */
61 #define MagickPathExtent 4096
62 #define MagickFalse 0
63 #define MagickTrue 1
64 #define MagickBooleanType int
65
66 #define AcquireMagickMemory(s) malloc(s)
67 #define RelinquishMagickMemory(p) (free(p),NULL)
68 #define ResizeMagickMemory(p,s) realloc(p,s)
69 #define ResetMagickMemory(p,b,s) memset(p,b,s)
70 #define StringToLong(s) strtol(s,(char **) NULL,10)
71 #define LocaleCompare(p,q) strcasecmp(p,q)
72 #define LocaleNCompare(p,q,l) strncasecmp(p,q,l)
73 #define WandSignature 0xabacadabUL
74 #define fopen_utf8(p,q) fopen(p,q)
75 #define WandExport
76
77 /* Include the actual code for ScriptToken functions */
78 #define SCRIPT_TOKEN_TESTING 1 /* Prevent MagickWand Includes */
79 #include "../script-token.h"
80 #include "../script-token.c"
81
82 /* Test program to report what tokens it finds in given input file/stream */
83
main(int argc,char * argv[])84 int main(int argc, char *argv[])
85 {
86 ScriptTokenInfo
87 *token_info;
88
89 token_info = AcquireScriptTokenInfo( (argc>1) ? argv[1] : "-" );
90 if (token_info == (ScriptTokenInfo *) NULL) {
91 printf("Script Open Failure : %s\n", strerror(errno));
92 return(1);
93 }
94
95 while (1) {
96 if( GetScriptToken(token_info) == MagickFalse )
97 break;
98
99 if( strlen(token_info->token) > INITAL_TOKEN_LENGTH-1 ) {
100 token_info->token[INITAL_TOKEN_LENGTH-4] = '.';
101 token_info->token[INITAL_TOKEN_LENGTH-3] = '.';
102 token_info->token[INITAL_TOKEN_LENGTH-2] = '.';
103 token_info->token[INITAL_TOKEN_LENGTH-1] = '\0';
104 }
105 printf("l=%d, c=%d, stat=%d, len=%d, token=\"%s\"\n",
106 token_info->token_line, token_info->token_column,
107 token_info->status, token_info->length, token_info->token);
108 }
109
110 switch( token_info->status ) {
111 case TokenStatusOK:
112 break;
113 case TokenStatusEOF:
114 printf("EOF Found\n");
115 break;
116 case TokenStatusBadQuotes:
117 /* Ensure last token has a sane length for error report */
118 if( strlen(token_info->token) > INITAL_TOKEN_LENGTH-1 ) {
119 token_info->token[INITAL_TOKEN_LENGTH-4] = '.';
120 token_info->token[INITAL_TOKEN_LENGTH-3] = '.';
121 token_info->token[INITAL_TOKEN_LENGTH-2] = '.';
122 token_info->token[INITAL_TOKEN_LENGTH-1] = '\0';
123 }
124 printf("Bad Quotes l=%d, c=%d token=\"%s\"\n",
125 token_info->token_line,token_info->token_column, token_info->token);
126 break;
127 case TokenStatusMemoryFailed: /* token is invalid */
128 printf("Out of Memory l=%d, c=%d\n",
129 token_info->token_line,token_info->token_column);
130 break;
131 case TokenStatusBinary: /* token is invalid */
132 printf("Binary Char at l=%d, c=%d\n",
133 token_info->curr_line,token_info->curr_column);
134 break;
135 }
136
137 /* Clean up */
138 token_info = DestroyScriptTokenInfo(token_info);
139
140 return(0);
141 }
142