~ubuntu-branches/ubuntu/wily/gargoyle-free/wily-proposed

« back to all changes in this revision

Viewing changes to terps/jacl/jpp.c

  • Committer: Bazaar Package Importer
  • Author(s): Sylvain Beucler
  • Date: 2009-09-11 20:09:43 UTC
  • Revision ID: james.westby@ubuntu.com-20090911200943-idgzoyupq6650zpn
Tags: upstream-2009-08-25
ImportĀ upstreamĀ versionĀ 2009-08-25

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <string.h>
 
24
 
 
25
extern char                     text_buffer[];
 
26
extern char                     temp_buffer[];
 
27
extern char                     *word[];
 
28
extern short int        quoted[];
 
29
extern short int        punctuated[];
 
30
extern int                      wp;
 
31
 
 
32
extern char                     user_id[];
 
33
extern char                     prefix[];
 
34
extern char                     game_path[];
 
35
extern char                     game_file[];
 
36
extern char                     processed_file[];
 
37
 
 
38
extern char                     include_directory[];
 
39
extern char                     temp_directory[];
 
40
 
 
41
extern char                     error_buffer[];
 
42
 
 
43
int                                     lines_written;
 
44
 
 
45
FILE                    *outputFile = NULL;
 
46
FILE                    *inputFile = NULL;
 
47
 
 
48
/* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED 
 
49
 * WITH IS ENCRYPTED */
 
50
short int                       encrypted = FALSE;
 
51
 
 
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;
 
55
 
 
56
/* INDICATES THAT THE CURRENT '.j2' FILE BEING WORKED 
 
57
 * SHOULD BE ENCRYPTED */
 
58
short int                       encrypt = TRUE;
 
59
 
 
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;
 
63
 
 
64
int
 
65
jacl_whitespace(character)
 
66
        int character;
 
67
{
 
68
        /* CHECK IF A CHARACTER IS CONSIDERED WHITE SPACE IN THE JACL LANGUAGE */
 
69
        switch (character) {
 
70
                case ':':
 
71
                case '\t':
 
72
                case ' ':
 
73
                        return(TRUE);
 
74
                default:
 
75
                        return(FALSE);
 
76
        }
 
77
}
 
78
 
 
79
void
 
80
stripwhite (string)
 
81
     char *string;
 
82
{
 
83
        register int i = 0;
 
84
 
 
85
        /* STRIP WHITESPACE FROM THE START AND END OF STRING. */
 
86
        while (jacl_whitespace (string[i])) i++;
 
87
 
 
88
        if (i) strcpy (string, string + i);
 
89
 
 
90
        i = strlen (string) - 1;
 
91
 
 
92
        while (i >= 0 && (jacl_whitespace (string[i]) || string[i] == '\n' || string[i] == '\r')) i--;
 
93
 
 
94
#ifdef WIN32
 
95
        string[++i] = '\r';
 
96
#endif
 
97
        string[++i] = '\n';
 
98
        string[++i] = '\0';
 
99
}
 
100
 
 
101
int
 
102
jpp()
 
103
{
 
104
        int                             game_version;
 
105
 
 
106
        lines_written = 0;
 
107
 
 
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) {
 
112
                int index = 0;
 
113
                char *result = NULL;
 
114
 
 
115
                result = fgets(text_buffer, 1024, inputFile);
 
116
                if (!result) return (FALSE);
 
117
 
 
118
                while (!feof(inputFile) && index < 10) {
 
119
                        if (strstr(text_buffer, "#processed")) {
 
120
                                /* THE GAME FILE IS ALREADY A PROCESSED FILE, JUST USE IT
 
121
                                 * DIRECTLY */
 
122
                                if (sscanf(text_buffer, "#processed:%d", &game_version)) {
 
123
                                        if (INTERPRETER_VERSION < game_version) {
 
124
                                                sprintf (error_buffer, OLD_INTERPRETER);
 
125
                                                return (FALSE);
 
126
                                        }
 
127
                                }
 
128
                                strcpy(processed_file, game_file);
 
129
                                
 
130
                                return (TRUE);  
 
131
                        }                                       
 
132
                        result = fgets(text_buffer, 1024, inputFile);
 
133
                        if (!result) return (FALSE);
 
134
                        index++;
 
135
                }
 
136
 
 
137
                fclose(inputFile);
 
138
        } else {
 
139
                sprintf (error_buffer, NOT_FOUND);
 
140
                return (FALSE);
 
141
        }
 
142
 
 
143
        /* SAVE A TEMPORARY FILENAME INTO PROCESSED_FILE */
 
144
        sprintf(processed_file, "%s%s.j2", temp_directory, prefix);
 
145
 
 
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);
 
153
                        return (FALSE);
 
154
                }
 
155
        }
 
156
 
 
157
        if (process_file(game_file, (char *) NULL) == FALSE) {
 
158
                return (FALSE);
 
159
        }
 
160
 
 
161
        fclose(outputFile);
 
162
 
 
163
        /* ALL OKAY, RETURN TRUE */
 
164
        return (TRUE);
 
165
}
 
166
 
 
167
int
 
168
process_file(sourceFile1, sourceFile2)
 
169
         char           *sourceFile1;
 
170
         char           *sourceFile2;
 
171
{
 
172
        char            temp_buffer1[1025];
 
173
        char            temp_buffer2[1025];
 
174
        FILE           *inputFile = NULL;
 
175
        char           *includeFile = NULL;
 
176
 
 
177
        /* THIS FUNCTION WILL CREATE A PROCESSED FILE THAT HAS HAD ALL
 
178
         * LEADING AND TRAILING WHITE SPACE REMOVED AND ALL INCLUDED
 
179
         * FILES INSERTED */
 
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);
 
184
                                return (FALSE);
 
185
                        }
 
186
 
 
187
                } else {
 
188
                        sprintf(error_buffer, CANT_OPEN_SOURCE, sourceFile1);
 
189
                        return (FALSE);
 
190
                }
 
191
        }
 
192
 
 
193
        *text_buffer = 0;
 
194
 
 
195
        if (fgets(text_buffer, 1024, inputFile) == NULL) {
 
196
                sprintf (error_buffer, READ_ERROR);
 
197
                return (FALSE);
 
198
        }
 
199
 
 
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, '"');
 
204
 
 
205
                        if (includeFile != NULL)
 
206
                                *includeFile = 0;
 
207
 
 
208
                        includeFile = strchr(text_buffer, '"');
 
209
 
 
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) {
 
216
                                        return (FALSE);
 
217
                                }
 
218
                        } else {
 
219
                                sprintf (error_buffer, BAD_INCLUDE);
 
220
                                return (FALSE);
 
221
                        }
 
222
                } else {
 
223
                        /* STRIP WHITESPACE FROM LINE BEFORE WRITING TO OUTPUTFILE. */
 
224
                        stripwhite(text_buffer);
 
225
 
 
226
                        if (!encrypting && text_buffer[0] != '#' && text_buffer[0] != '\0' && encrypt & release) {
 
227
                                /* START ENCRYPTING FROM THE FIRST NON-COMMENT LINE IN
 
228
                                 * THE SOURCE FILE */
 
229
#ifdef WIN32
 
230
                                fputs("#encrypted\r\n", outputFile);
 
231
#else
 
232
                                fputs("#encrypted\n", outputFile);
 
233
#endif
 
234
                                encrypting = TRUE;
 
235
                        } 
 
236
 
 
237
                        /* ENCRYPT PROCESSED FILE IF REQUIRED */
 
238
                        if (encrypting) {
 
239
                                jacl_encrypt(text_buffer);
 
240
                        }
 
241
 
 
242
                        fputs(text_buffer, outputFile);
 
243
 
 
244
                        lines_written++;
 
245
                        if (lines_written == 1) {
 
246
#ifdef WIN32
 
247
                                sprintf(temp_buffer, "#processed:%d\r\n", INTERPRETER_VERSION);
 
248
#else
 
249
                                sprintf(temp_buffer, "#processed:%d\n", INTERPRETER_VERSION);
 
250
#endif
 
251
                                fputs(temp_buffer, outputFile);
 
252
                        }
 
253
                }
 
254
 
 
255
                *text_buffer = 0;
 
256
 
 
257
                if (fgets(text_buffer, 1024, inputFile) == NULL) {
 
258
                        // EOF HAS BEEN REACHED
 
259
                        break;
 
260
                }
 
261
        }
 
262
 
 
263
        fclose(inputFile);
 
264
 
 
265
        /* ALL OKAY, RETURN TRUE */
 
266
        return (TRUE);
 
267
}
 
268
 
 
269
void
 
270
jacl_encrypt(string)
 
271
  char *string;
 
272
{
 
273
        int index, length;
 
274
 
 
275
        length = strlen(string);
 
276
        
 
277
        for (index = 0; index < length; index++) {
 
278
                if (string[index] == '\n' || string[index] == '\r') {
 
279
                        return;
 
280
                }
 
281
                string[index] = string[index] ^ 255;
 
282
        }
 
283
}
 
284
 
 
285
void
 
286
jacl_decrypt(string)
 
287
  char *string;
 
288
{
 
289
        int index, length;
 
290
 
 
291
        length = strlen(string);
 
292
        
 
293
        for (index = 0; index < length; index++) {
 
294
                if (string[index] == '\n' || string[index] == '\r') {
 
295
                        return;
 
296
                }
 
297
                string[index] = string[index] ^ 255;
 
298
        }
 
299
}
 
300