Debian Codegraph @

terps/jacl/jpp.c

raw symbols SCIP
1 /* jpp.c --- The JACL Preprocessor
2 (C) 2001 Andreas Matthias
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "jacl.h"
20 #include "language.h"
21 #include "types.h"
22 #include "prototypes.h"
23 #include "encapsulate.h"
24 #include <string.h>
25
26 static int lines_written;
27
28 static FILE *outputFile = NULL;
29 static FILE *inputFile = NULL;
30
31 static char *stripped_line;
32
33 /* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED
34 * WITH BEING PREPARED FOR RELEASE (DON'T INCLUDE DEBUG LIBARIES) */
35 short int release = FALSE;
36
37 /* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED
38 * SHOULD BE ENCRYPTED */
39 static short int do_encrypt = TRUE;
40
41 /* INDICATES THAT THE CURRENT '.processed' FILE BRING WRITTEN SHOULD NOW
42 * HAVE EACH LINE ENCRYPTED AS THE FIRST NONE COMMENT LINE HAS BEEN HIT */
43 static short int encrypting = FALSE;
44
45 static int process_file(const char *sourceFile1, const char *sourceFile2);
46
47 int
48 jpp()
49 {
50 int game_version;
51
52 lines_written = 0;
53
54 /* CHECK IF GAME FILE IS ALREADY A PROCESSED FILE BY LOOKING FOR THE
55 * STRING "#encrypted" OR "#processed" WITHIN THE FIRST FIVE LINES OF
56 * THE GAME FILE IF SO, RETURN THE GAME FILE AS THE PROCESSED FILE */
57 if ((inputFile = fopen(game_file, "r")) != NULL) {
58 int index = 0;
59 char *result = NULL;
60
61 result = fgets(text_buffer, 1024, inputFile);
62 if (!result) {
63 sprintf(error_buffer, CANT_OPEN_SOURCE, game_file);
64 return (FALSE);
65 }
66
67 while (!feof(inputFile) && index < 10) {
68 if (strstr(text_buffer, "#processed")) {
69 /* THE GAME FILE IS ALREADY A PROCESSED FILE, JUST USE IT
70 * DIRECTLY */
71 if (sscanf(text_buffer, "#processed:%d", &game_version)) {
72 if (INTERPRETER_VERSION < game_version) {
73 sprintf (error_buffer, OLD_INTERPRETER, game_version);
74 return (FALSE);
75 }
76 }
77 strcpy(processed_file, game_file);
78
79 return (TRUE);
80 }
81 result = fgets(text_buffer, 1024, inputFile);
82 if (!result) {
83 //return (FALSE);
84 break;
85 }
86 index++;
87 }
88
89 fclose(inputFile);
90 } else {
91 sprintf (error_buffer, NOT_FOUND);
92 return (FALSE);
93 }
94
95 /* SAVE A TEMPORARY FILENAME INTO PROCESSED_FILE */
96 sprintf(processed_file, "%s%s.j2", temp_directory, prefix);
97
98 /* ATTEMPT TO OPEN THE PROCESSED FILE IN THE TEMP DIRECTORY */
99 if ((outputFile = fopen(processed_file, "w")) == NULL) {
100 /* NO LUCK, TRY OPEN THE PROCESSED FILE IN THE CURRENT DIRECTORY */
101 sprintf(processed_file, "%s.j2", prefix);
102 if ((outputFile = fopen(processed_file, "w")) == NULL) {
103 /* NO LUCK, CAN'T CONTINUE */
104 sprintf(error_buffer, CANT_OPEN_PROCESSED, processed_file);
105 return (FALSE);
106 }
107 }
108
109 if (process_file(game_file, (char *) NULL) == FALSE) {
110 return (FALSE);
111 }
112
113 fclose(outputFile);
114
115 /* ALL OKAY, RETURN TRUE */
116 return (TRUE);
117 }
118
119 int
120 process_file(const char *sourceFile1, const char *sourceFile2)
121 {
122 char temp_buffer1[1025];
123 char temp_buffer2[1025];
124 FILE *inputFile = NULL;
125 char *includeFile = NULL;
126
127 /* THIS FUNCTION WILL CREATE A PROCESSED FILE THAT HAS HAD ALL
128 * LEADING AND TRAILING WHITE SPACE REMOVED AND ALL INCLUDED
129 * FILES INSERTED */
130 if ((inputFile = fopen(sourceFile1, "r")) == NULL) {
131 if (sourceFile2 != NULL) {
132 if ((inputFile = fopen(sourceFile2, "r")) == NULL) {
133 sprintf(error_buffer, CANT_OPEN_OR, sourceFile1, sourceFile2);
134 return (FALSE);
135 }
136
137 } else {
138 sprintf(error_buffer, CANT_OPEN_SOURCE, sourceFile1);
139 return (FALSE);
140 }
141 }
142
143 *text_buffer = 0;
144
145 if (fgets(text_buffer, 1024, inputFile) == NULL) {
146 sprintf (error_buffer, READ_ERROR);
147 return (FALSE);
148 }
149
150 while (!feof(inputFile) || *text_buffer != 0) {
151 if (!strncmp(text_buffer, "#include", 8) ||
152 (!strncmp(text_buffer, "#debug", 6) & !release)) {
153 includeFile = strrchr(text_buffer, '"');
154
155 if (includeFile != NULL)
156 *includeFile = 0;
157
158 includeFile = strchr(text_buffer, '"');
159
160 if (includeFile != NULL) {
161 strcpy(temp_buffer1, game_path);
162 strcat(temp_buffer1, includeFile + 1);
163 strcpy(temp_buffer2, include_directory);
164 strcat(temp_buffer2, includeFile + 1);
165 if (process_file(temp_buffer1, temp_buffer2) == FALSE) {
166 return (FALSE);
167 }
168 } else {
169 sprintf (error_buffer, BAD_INCLUDE);
170 return (FALSE);
171 }
172 } else {
173 /* STRIP WHITESPACE FROM LINE BEFORE WRITING TO OUTPUTFILE. */
174 stripped_line = stripwhite(text_buffer);
175
176 if (!encrypting && *stripped_line != '#' && *stripped_line != '\0' && do_encrypt & release) {
177 /* START ENCRYPTING FROM THE FIRST NON-COMMENT LINE IN
178 * THE SOURCE FILE */
179 fputs("#encrypted\n", outputFile);
180 encrypting = TRUE;
181 }
182
183 /* ENCRYPT PROCESSED FILE IF REQUIRED */
184 if (encrypting) {
185 jacl_encrypt(stripped_line);
186 }
187
188 fputs(stripped_line, outputFile);
189
190 lines_written++;
191 if (lines_written == 1) {
192 sprintf(temp_buffer, "#processed:%d\n", INTERPRETER_VERSION);
193 fputs(temp_buffer, outputFile);
194 }
195 }
196
197 *text_buffer = 0;
198
199 if (fgets(text_buffer, 1024, inputFile) == NULL) {
200 // EOF HAS BEEN REACHED
201 break;
202 }
203 }
204
205 fclose(inputFile);
206
207 /* ALL OKAY, RETURN TRUE */
208 return (TRUE);
209 }