1
/* jpp.c --- The JACL Preprocessor
2
(C) 2001 Andreas Matthias
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)
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.
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.
22
#include "prototypes.h"
25
extern char text_buffer[];
26
extern char temp_buffer[];
28
extern short int quoted[];
29
extern short int punctuated[];
32
extern char user_id[];
34
extern char game_path[];
35
extern char game_file[];
36
extern char processed_file[];
38
extern char include_directory[];
39
extern char temp_directory[];
41
extern char error_buffer[];
45
FILE *outputFile = NULL;
46
FILE *inputFile = NULL;
48
/* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED
49
* WITH IS ENCRYPTED */
50
short int encrypted = FALSE;
52
/* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED
53
* WITH BEING PREPARED FOR RELEASE (DON'T INCLUDE DEBUG LIBARIES) */
54
short int release = FALSE;
56
/* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED
57
* SHOULD BE ENCRYPTED */
58
short int encrypt = TRUE;
60
/* INDICATES THAT THE CURRENT '.processed' FILE BRING WRITTEN SHOULD NOW
61
* HAVE EACH LINE ENCRYPTED AS THE FIRST NONE COMMENT LINE HAS BEEN HIT */
62
short int encrypting = FALSE;
65
jacl_whitespace(character)
68
/* CHECK IF A CHARACTER IS CONSIDERED WHITE SPACE IN THE JACL LANGUAGE */
85
/* STRIP WHITESPACE FROM THE START AND END OF STRING. */
86
while (jacl_whitespace (string[i])) i++;
88
if (i) strcpy (string, string + i);
90
i = strlen (string) - 1;
92
while (i >= 0 && (jacl_whitespace (string[i]) || string[i] == '\n' || string[i] == '\r')) i--;
108
/* CHECK IF GAME FILE IS ALREADY A PROCESSED FILE BY LOOKING FOR THE
109
* STRING "#encrypted" OR "#processed" WITHIN THE FIRST FIVE LINES OF
110
* THE GAME FILE IF SO, RETURN THE GAME FILE AS THE PROCESSED FILE */
111
if ((inputFile = fopen(game_file, "r")) != NULL) {
115
result = fgets(text_buffer, 1024, inputFile);
116
if (!result) return (FALSE);
118
while (!feof(inputFile) && index < 10) {
119
if (strstr(text_buffer, "#processed")) {
120
/* THE GAME FILE IS ALREADY A PROCESSED FILE, JUST USE IT
122
if (sscanf(text_buffer, "#processed:%d", &game_version)) {
123
if (INTERPRETER_VERSION < game_version) {
124
sprintf (error_buffer, OLD_INTERPRETER);
128
strcpy(processed_file, game_file);
132
result = fgets(text_buffer, 1024, inputFile);
133
if (!result) return (FALSE);
139
sprintf (error_buffer, NOT_FOUND);
143
/* SAVE A TEMPORARY FILENAME INTO PROCESSED_FILE */
144
sprintf(processed_file, "%s%s.j2", temp_directory, prefix);
146
/* ATTEMPT TO OPEN THE PROCESSED FILE IN THE TEMP DIRECTORY */
147
if ((outputFile = fopen(processed_file, "w")) == NULL) {
148
/* NO LUCK, TRY OPEN THE PROCESSED FILE IN THE CURRENT DIRECTORY */
149
sprintf(processed_file, "%s.j2", prefix);
150
if ((outputFile = fopen(processed_file, "w")) == NULL) {
151
/* NO LUCK, CAN'T CONTINUE */
152
sprintf(error_buffer, CANT_OPEN_PROCESSED, processed_file);
157
if (process_file(game_file, (char *) NULL) == FALSE) {
163
/* ALL OKAY, RETURN TRUE */
168
process_file(sourceFile1, sourceFile2)
172
char temp_buffer1[1025];
173
char temp_buffer2[1025];
174
FILE *inputFile = NULL;
175
char *includeFile = NULL;
177
/* THIS FUNCTION WILL CREATE A PROCESSED FILE THAT HAS HAD ALL
178
* LEADING AND TRAILING WHITE SPACE REMOVED AND ALL INCLUDED
180
if ((inputFile = fopen(sourceFile1, "r")) == NULL) {
181
if (sourceFile2 != NULL) {
182
if ((inputFile = fopen(sourceFile2, "r")) == NULL) {
183
sprintf(error_buffer, CANT_OPEN_OR, sourceFile1, sourceFile2);
188
sprintf(error_buffer, CANT_OPEN_SOURCE, sourceFile1);
195
if (fgets(text_buffer, 1024, inputFile) == NULL) {
196
sprintf (error_buffer, READ_ERROR);
200
while (!feof(inputFile) || *text_buffer != 0) {
201
if (!strncmp(text_buffer, "#include", 8) ||
202
(!strncmp(text_buffer, "#debug", 6) & !release)) {
203
includeFile = strrchr(text_buffer, '"');
205
if (includeFile != NULL)
208
includeFile = strchr(text_buffer, '"');
210
if (includeFile != NULL) {
211
strcpy(temp_buffer1, game_path);
212
strcat(temp_buffer1, includeFile + 1);
213
strcpy(temp_buffer2, include_directory);
214
strcat(temp_buffer2, includeFile + 1);
215
if (process_file(temp_buffer1, temp_buffer2) == FALSE) {
219
sprintf (error_buffer, BAD_INCLUDE);
223
/* STRIP WHITESPACE FROM LINE BEFORE WRITING TO OUTPUTFILE. */
224
stripwhite(text_buffer);
226
if (!encrypting && text_buffer[0] != '#' && text_buffer[0] != '\0' && encrypt & release) {
227
/* START ENCRYPTING FROM THE FIRST NON-COMMENT LINE IN
230
fputs("#encrypted\r\n", outputFile);
232
fputs("#encrypted\n", outputFile);
237
/* ENCRYPT PROCESSED FILE IF REQUIRED */
239
jacl_encrypt(text_buffer);
242
fputs(text_buffer, outputFile);
245
if (lines_written == 1) {
247
sprintf(temp_buffer, "#processed:%d\r\n", INTERPRETER_VERSION);
249
sprintf(temp_buffer, "#processed:%d\n", INTERPRETER_VERSION);
251
fputs(temp_buffer, outputFile);
257
if (fgets(text_buffer, 1024, inputFile) == NULL) {
258
// EOF HAS BEEN REACHED
265
/* ALL OKAY, RETURN TRUE */
275
length = strlen(string);
277
for (index = 0; index < length; index++) {
278
if (string[index] == '\n' || string[index] == '\r') {
281
string[index] = string[index] ^ 255;
291
length = strlen(string);
293
for (index = 0; index < length; index++) {
294
if (string[index] == '\n' || string[index] == '\r') {
297
string[index] = string[index] ^ 255;