3 Extension module for the rfxswf library.
4 Part of the swftools package.
6 Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
29 #include "tokenizer.h"
32 static int verbose = 0;
33 static void dbg(const char*format, ...)
40 va_start(arglist, format);
41 vsnprintf(buf, sizeof(buf)-1, format, arglist);
44 while(l && buf[l-1]=='\n') {
48 printf("(includefilehandler) ");
56 char* current_filename=0;
57 char* current_filename_short=0;
58 char* current_filename_long=0;
59 include_dir_t* current_include_dirs=0;
61 #define MAX_INCLUDE_DEPTH 16
63 void*include_stack[MAX_INCLUDE_DEPTH];
64 int line_stack[MAX_INCLUDE_DEPTH];
65 int column_stack[MAX_INCLUDE_DEPTH];
66 char* filename_stack[MAX_INCLUDE_DEPTH];
67 char* shortfilename_stack[MAX_INCLUDE_DEPTH];
68 char* longfilename_stack[MAX_INCLUDE_DEPTH];
69 include_dir_t* includedir_stack[MAX_INCLUDE_DEPTH];
70 int include_stack_ptr = 0;
72 void add_include_dir(char*dir)
74 include_dir_t*d = malloc(sizeof(include_dir_t));
75 memset(d , 0, sizeof(include_dir_t));
76 d->path = strdup(dir);
77 d->next = current_include_dirs;
78 current_include_dirs = d;
81 void del_include_dirs(include_dir_t*d, include_dir_t*d2)
84 include_dir_t*next = d->next;
85 free(d->path);d->path=0;
92 char*get_path(const char*file)
94 char*path = strdup(file);
95 char*r1 = strrchr(path, '/');
96 char*r2 = strrchr(path, '\\');
108 char is_absolute(const char*filename)
110 if(!filename || !filename[0])
112 if(filename[0]=='/' || filename[0]=='\\')
114 if(filename[1]==':' && filename[2]=='/')
116 if(filename[1]==':' && filename[2]=='\\')
121 char* normalize_path(const char*path)
124 if(!is_absolute(path)) {
126 char*c = getcwd(buf,512);
128 d = n = malloc(l+strlen(path)+10);
129 strcpy(n, buf);d += l;
130 if(!l || n[l-1]!=path_seperator) {
131 *d=path_seperator;d++;
134 d = n = strdup(path);
140 if(init && s[0] == '.' && (s[1]==path_seperator || s[1]=='\0')) {
146 if(init && s[0] == '.' && s[1] == '.' && (s[2] == path_seperator || s[2]=='\0')) {
152 if(!(last=strrchr(n, path_seperator))) {
163 if(*s==path_seperator) init=1;
167 if(d!=n && d[-1]==path_seperator)
172 static void testnormalize()
174 #define TEST(x) {printf("%s -> %s\n", (x), normalize_path(x));}
178 TEST("../as3/parser.y");
179 TEST("../as3/ok/../ok/scope.as");
181 TEST("ok/./scope.as");
182 TEST("./ok/scope.as");
189 TEST("/tmp/../usr/");
192 char* concat_paths(const char*base, const char*add)
194 int l1 = strlen(base);
195 int l2 = strlen(add);
198 while(l1 && base[l1-1] == path_seperator)
200 while(pos < l2 && add[pos] == path_seperator)
202 n = (char*)malloc(l1 + (l2-pos) + 2);
204 n[l1]=path_seperator;
205 memcpy(&n[l1+1],&add[pos],l2-pos+1);
209 char*find_file(const char*filename, char error)
211 include_dir_t*i = current_include_dirs;
213 if(is_absolute(filename)) {
214 FILE*fi = fopen(filename, "rb");
217 return strdup(filename);
221 as3_warning("Include directory stack is empty, while looking for file %s", filename);
224 char*p = concat_paths(i->path, filename);
239 as3_error("Couldn't find file %s", filename);
240 i = current_include_dirs;
242 fprintf(stderr, "include dir: %s\n", i->path);
248 void enter_file(const char*name, const char*filename, void*state)
250 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
251 as3_error("Includes nested too deeply");
254 include_stack[include_stack_ptr] = state;
255 line_stack[include_stack_ptr] = current_line;
256 column_stack[include_stack_ptr] = current_column;
257 shortfilename_stack[include_stack_ptr] = current_filename_short;
258 longfilename_stack[include_stack_ptr] = current_filename_long;
259 filename_stack[include_stack_ptr] = current_filename;
260 includedir_stack[include_stack_ptr] = current_include_dirs;
262 /*char*dir = get_path(filename);
263 add_include_dir(dir);
268 dbg("entering file %s", filename);
272 current_filename = strdup(name);
273 current_filename_short = strdup(name);
274 current_filename_long = strdup(filename);
277 FILE*enter_file2(const char*name, const char*filename, void*state)
279 enter_file(name, filename, state);
280 FILE*fi = fopen(filename, "rb");
282 as3_error("Couldn't find file %s: %s", filename, strerror(errno));
289 dbg("leaving file %s", current_filename);
290 if(--include_stack_ptr<=0) {
293 free(current_filename);current_filename = filename_stack[include_stack_ptr];
294 free(current_filename_short);current_filename_short = shortfilename_stack[include_stack_ptr];
295 free(current_filename_long);current_filename_long = longfilename_stack[include_stack_ptr];
296 current_column = column_stack[include_stack_ptr];
297 current_line = line_stack[include_stack_ptr];
298 del_include_dirs(includedir_stack[include_stack_ptr], current_include_dirs);
299 current_include_dirs = includedir_stack[include_stack_ptr];
300 return include_stack[include_stack_ptr];