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 */
28 #include "tokenizer.h"
30 static int verbose = 0;
31 static void dbg(const char*format, ...)
38 va_start(arglist, format);
39 vsprintf(buf, format, arglist);
42 while(l && buf[l-1]=='\n') {
46 printf("(includefilehandler) ");
54 char* current_filename=0;
55 char* current_filename_short=0;
56 include_dir_t* current_include_dirs=0;
58 #define MAX_INCLUDE_DEPTH 16
60 void*include_stack[MAX_INCLUDE_DEPTH];
61 int line_stack[MAX_INCLUDE_DEPTH];
62 int column_stack[MAX_INCLUDE_DEPTH];
63 char* filename_stack[MAX_INCLUDE_DEPTH];
64 char* shortfilename_stack[MAX_INCLUDE_DEPTH];
65 include_dir_t* includedir_stack[MAX_INCLUDE_DEPTH];
66 int include_stack_ptr = 0;
68 void add_include_dir(char*dir)
70 include_dir_t*d = malloc(sizeof(include_dir_t));
71 memset(d , 0, sizeof(include_dir_t));
72 d->path = strdup(dir);
73 d->next = current_include_dirs;
74 current_include_dirs = d;
77 void del_include_dirs(include_dir_t*d, include_dir_t*d2)
80 include_dir_t*next = d->next;
81 free(d->path);d->path=0;
88 char*get_path(const char*file)
90 char*path = strdup(file);
91 char*r1 = strrchr(path, '/');
92 char*r2 = strrchr(path, '\\');
103 char* normalize_path(const char*path)
108 char*c = getcwd(buf,512);
110 d = n = malloc(l+strlen(path)+10);
111 strcpy(n, buf);d += l;
112 if(!l || n[l-1]!='/') {
116 d = n = strdup(path);
122 if(init && s[0] == '.' && (s[1]=='/' || s[1]=='\0')) {
128 if(init && s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2]=='\0')) {
134 if(!(last=strrchr(n, '/'))) {
149 if(d!=n && d[-1]=='/')
154 static void testnormalize()
156 #define TEST(x) {printf("%s -> %s\n", (x), normalize_path(x));}
160 TEST("../as3/parser.y");
161 TEST("../as3/ok/../ok/scope.as");
163 TEST("ok/./scope.as");
164 TEST("./ok/scope.as");
171 TEST("/tmp/../usr/");
175 char* concat_paths(const char*base, const char*add)
177 int l1 = strlen(base);
178 int l2 = strlen(add);
181 while(l1 && base[l1-1] == '/')
183 while(pos < l2 && add[pos] == '/')
185 n = (char*)malloc(l1 + (l2-pos) + 2);
188 memcpy(&n[l1+1],&add[pos],l2-pos+1);
191 char is_absolute(const char*filename)
193 if(!filename || !filename[0])
195 if(filename[0]=='/' || filename[0]=='\\')
197 if(filename[1]==':' && filename[1]=='/')
199 if(filename[1]==':' && filename[1]=='\\')
204 char*find_file(const char*filename, char error)
206 include_dir_t*i = current_include_dirs;
208 if(is_absolute(filename)) {
209 FILE*fi = fopen(filename, "rb");
212 return strdup(filename);
216 as3_warning("Include directory stack is empty, while looking for file %s", filename);
219 char*p = concat_paths(i->path, filename);
234 as3_error("Couldn't find file %s", filename);
235 i = current_include_dirs;
237 fprintf(stderr, "include dir: %s\n", i->path);
243 void enter_file(const char*name, const char*filename, void*state)
245 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
246 as3_error("Includes nested too deeply");
249 include_stack[include_stack_ptr] = state;
250 line_stack[include_stack_ptr] = current_line;
251 column_stack[include_stack_ptr] = current_column;
252 shortfilename_stack[include_stack_ptr] = current_filename_short;
253 filename_stack[include_stack_ptr] = current_filename;
254 includedir_stack[include_stack_ptr] = current_include_dirs;
256 /*char*dir = get_path(filename);
257 add_include_dir(dir);
262 dbg("entering file %s", filename);
266 current_filename = strdup(filename);
267 current_filename_short = strdup(name);
270 FILE*enter_file2(const char*name, const char*filename, void*state)
272 enter_file(name, filename, state);
273 FILE*fi = fopen(filename, "rb");
275 as3_error("Couldn't find file %s: %s", filename, strerror(errno));
282 dbg("leaving file %s", current_filename);
283 if(--include_stack_ptr<=0) {
286 free(current_filename);current_filename = filename_stack[include_stack_ptr];
287 free(current_filename_short);current_filename_short = shortfilename_stack[include_stack_ptr];
288 current_column = column_stack[include_stack_ptr];
289 current_line = line_stack[include_stack_ptr];
290 del_include_dirs(includedir_stack[include_stack_ptr], current_include_dirs);
291 current_include_dirs = includedir_stack[include_stack_ptr];
292 return include_stack[include_stack_ptr];