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)
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);
231 as3_error("Couldn't find file %s", filename);
232 i = current_include_dirs;
234 fprintf(stderr, "include dir: %s\n", i->path);
240 void enter_file(const char*name, const char*filename, void*state)
242 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
243 as3_error("Includes nested too deeply");
246 include_stack[include_stack_ptr] = state;
247 line_stack[include_stack_ptr] = current_line;
248 column_stack[include_stack_ptr] = current_column;
249 shortfilename_stack[include_stack_ptr] = current_filename_short;
250 filename_stack[include_stack_ptr] = current_filename;
251 includedir_stack[include_stack_ptr] = current_include_dirs;
253 /*char*dir = get_path(filename);
254 add_include_dir(dir);
259 dbg("entering file %s", filename);
263 current_filename = strdup(filename);
264 current_filename_short = strdup(name);
267 FILE*enter_file2(const char*name, const char*filename, void*state)
269 enter_file(name, filename, state);
270 FILE*fi = fopen(filename, "rb");
272 as3_error("Couldn't find file %s: %s", filename, strerror(errno));
279 dbg("leaving file %s", current_filename);
280 if(--include_stack_ptr<=0) {
283 free(current_filename);current_filename = filename_stack[include_stack_ptr];
284 free(current_filename_short);current_filename_short = shortfilename_stack[include_stack_ptr];
285 current_column = column_stack[include_stack_ptr];
286 current_line = line_stack[include_stack_ptr];
287 del_include_dirs(includedir_stack[include_stack_ptr], current_include_dirs);
288 current_include_dirs = includedir_stack[include_stack_ptr];
289 return include_stack[include_stack_ptr];