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"
31 static int verbose = 0;
32 static void dbg(const char*format, ...)
39 va_start(arglist, format);
40 vsprintf(buf, format, arglist);
43 while(l && buf[l-1]=='\n') {
47 printf("(includefilehandler) ");
55 char* current_filename=0;
56 char* current_filename_short=0;
57 char* current_filename_long=0;
58 include_dir_t* current_include_dirs=0;
60 #define MAX_INCLUDE_DEPTH 16
62 void*include_stack[MAX_INCLUDE_DEPTH];
63 int line_stack[MAX_INCLUDE_DEPTH];
64 int column_stack[MAX_INCLUDE_DEPTH];
65 char* filename_stack[MAX_INCLUDE_DEPTH];
66 char* shortfilename_stack[MAX_INCLUDE_DEPTH];
67 char* longfilename_stack[MAX_INCLUDE_DEPTH];
68 include_dir_t* includedir_stack[MAX_INCLUDE_DEPTH];
69 int include_stack_ptr = 0;
71 void add_include_dir(char*dir)
73 include_dir_t*d = malloc(sizeof(include_dir_t));
74 memset(d , 0, sizeof(include_dir_t));
75 d->path = strdup(dir);
76 d->next = current_include_dirs;
77 current_include_dirs = d;
80 void del_include_dirs(include_dir_t*d, include_dir_t*d2)
83 include_dir_t*next = d->next;
84 free(d->path);d->path=0;
91 char*get_path(const char*file)
93 char*path = strdup(file);
94 char*r1 = strrchr(path, '/');
95 char*r2 = strrchr(path, '\\');
106 char* normalize_path(const char*path)
111 char*c = getcwd(buf,512);
113 d = n = malloc(l+strlen(path)+10);
114 strcpy(n, buf);d += l;
115 if(!l || n[l-1]!='/') {
119 d = n = strdup(path);
125 if(init && s[0] == '.' && (s[1]=='/' || s[1]=='\0')) {
131 if(init && s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2]=='\0')) {
137 if(!(last=strrchr(n, '/'))) {
152 if(d!=n && d[-1]=='/')
157 static void testnormalize()
159 #define TEST(x) {printf("%s -> %s\n", (x), normalize_path(x));}
163 TEST("../as3/parser.y");
164 TEST("../as3/ok/../ok/scope.as");
166 TEST("ok/./scope.as");
167 TEST("./ok/scope.as");
174 TEST("/tmp/../usr/");
178 char* concat_paths(const char*base, const char*add)
180 int l1 = strlen(base);
181 int l2 = strlen(add);
184 while(l1 && base[l1-1] == '/')
186 while(pos < l2 && add[pos] == '/')
188 n = (char*)malloc(l1 + (l2-pos) + 2);
191 memcpy(&n[l1+1],&add[pos],l2-pos+1);
194 char is_absolute(const char*filename)
196 if(!filename || !filename[0])
198 if(filename[0]=='/' || filename[0]=='\\')
200 if(filename[1]==':' && filename[1]=='/')
202 if(filename[1]==':' && filename[1]=='\\')
207 char*find_file(const char*filename, char error)
209 include_dir_t*i = current_include_dirs;
211 if(is_absolute(filename)) {
212 FILE*fi = fopen(filename, "rb");
215 return strdup(filename);
219 as3_warning("Include directory stack is empty, while looking for file %s", filename);
222 char*p = concat_paths(i->path, filename);
237 as3_error("Couldn't find file %s", filename);
238 i = current_include_dirs;
240 fprintf(stderr, "include dir: %s\n", i->path);
246 void enter_file(const char*name, const char*filename, void*state)
248 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
249 as3_error("Includes nested too deeply");
252 include_stack[include_stack_ptr] = state;
253 line_stack[include_stack_ptr] = current_line;
254 column_stack[include_stack_ptr] = current_column;
255 shortfilename_stack[include_stack_ptr] = current_filename_short;
256 longfilename_stack[include_stack_ptr] = current_filename_long;
257 filename_stack[include_stack_ptr] = current_filename;
258 includedir_stack[include_stack_ptr] = current_include_dirs;
260 /*char*dir = get_path(filename);
261 add_include_dir(dir);
266 dbg("entering file %s", filename);
270 current_filename = strdup(name);
271 current_filename_short = strdup(name);
272 current_filename_long = strdup(filename);
275 FILE*enter_file2(const char*name, const char*filename, void*state)
277 enter_file(name, filename, state);
278 FILE*fi = fopen(filename, "rb");
280 as3_error("Couldn't find file %s: %s", filename, strerror(errno));
287 dbg("leaving file %s", current_filename);
288 if(--include_stack_ptr<=0) {
291 free(current_filename);current_filename = filename_stack[include_stack_ptr];
292 free(current_filename_short);current_filename_short = shortfilename_stack[include_stack_ptr];
293 free(current_filename_long);current_filename_long = longfilename_stack[include_stack_ptr];
294 current_column = column_stack[include_stack_ptr];
295 current_line = line_stack[include_stack_ptr];
296 del_include_dirs(includedir_stack[include_stack_ptr], current_include_dirs);
297 current_include_dirs = includedir_stack[include_stack_ptr];
298 return include_stack[include_stack_ptr];