9bb4f576ab141c211e35a65a8e77f65a12c52b3f
[swftools.git] / lib / as3 / files.c
1 /* files.c
2
3    Extension module for the rfxswf library.
4    Part of the swftools package.
5
6    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <memory.h>
26 #include <errno.h>
27 #include "files.h"
28 #include "common.h"
29 #include "tokenizer.h"
30 #include "../os.h"
31
32 static int verbose = 0;
33 static void dbg(const char*format, ...)
34 {
35     char buf[1024];
36     int l;
37     va_list arglist;
38     if(!verbose)
39         return;
40     va_start(arglist, format);
41     vsnprintf(buf, sizeof(buf)-1, format, arglist);
42     va_end(arglist);
43     l = strlen(buf);
44     while(l && buf[l-1]=='\n') {
45         buf[l-1] = 0;
46         l--;
47     }
48     printf("(includefilehandler) ");
49     printf("%s\n", buf);
50     fflush(stdout);
51 }
52
53
54 int current_line=1;
55 int current_column=0;
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;
60
61 #define MAX_INCLUDE_DEPTH 16
62
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;
71
72 void add_include_dir(char*dir)
73 {
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;
79 }
80
81 void del_include_dirs(include_dir_t*d, include_dir_t*d2)
82 {
83     while(d && d!=d2) {
84         include_dir_t*next = d->next;
85         free(d->path);d->path=0;
86         d->next = 0;
87         free(d);
88         d = next;
89     }
90 }
91
92 char*get_path(const char*file)
93 {
94     char*path = strdup(file);
95     char*r1 = strrchr(path, '/');
96     char*r2 = strrchr(path, '\\');
97     if(r1<r2) {
98         *r2=0;
99         return path;
100     } else if(r1>r2) {
101         *r1=0;
102         return path;
103     } else {
104         return strdup(".");
105     }
106 }
107
108 char is_absolute(const char*filename) 
109 {
110     if(!filename || !filename[0])
111         return 0;
112     if(filename[0]=='/' || filename[0]=='\\')
113         return 1;
114     if(filename[1]==':' && filename[2]=='/')
115         return 1;
116     if(filename[1]==':' && filename[2]=='\\')
117         return 1;
118     return 0;
119 }
120
121 char* normalize_path(const char*path)
122 {
123     char*n = 0, *d = 0;
124     if(!is_absolute(path)) {
125         char buf[512];
126         char*c = getcwd(buf,512);
127         int l = strlen(buf);
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++;
132         }
133     } else {
134         d = n = strdup(path);
135     }
136     const char*s=path;
137     char init = 1;
138
139     while(*s) {
140         if(init && s[0] == '.' && (s[1]==path_seperator || s[1]=='\0')) {
141             if(!s[1]) break;
142             s+=2;
143             init=1;
144             continue;
145         }
146         if(init && s[0] == '.' && s[1] == '.' && (s[2] == path_seperator || s[2]=='\0')) {
147             // step one down
148             char*last = 0;
149             if(d<=n) 
150                 return 0;
151             *--d = 0;
152             if(!(last=strrchr(n, path_seperator))) {
153                 return 0;
154             }
155             d = last+1;
156             if(!s[2]) break;
157             s+=3;
158             init=1;
159             continue;
160         }
161
162         *d = *s;
163         if(*s==path_seperator) init=1;
164         else init=0;
165         d++;s++;
166     }
167     if(d!=n && d[-1]==path_seperator) 
168         d--;
169     *d = 0;
170     return n;
171 }
172 static void testnormalize()
173 {
174 #define TEST(x) {printf("%s -> %s\n", (x), normalize_path(x));}
175     TEST(".");
176     TEST("../as3");
177     TEST("../as3/");
178     TEST("../as3/parser.y");
179     TEST("../as3/ok/../ok/scope.as");
180     TEST("ok/scope.as");
181     TEST("ok/./scope.as");
182     TEST("./ok/scope.as");
183     TEST("./");
184     TEST("/tmp/");
185     TEST("/./tmp/");
186     TEST("../");
187     TEST("/");
188     TEST("/tmp");
189     TEST("/tmp/../usr/");
190 }
191
192 char* concat_paths(const char*base, const char*add)
193 {
194     int l1 = strlen(base);
195     int l2 = strlen(add);
196     int pos = 0;
197     char*n = 0;
198     while(l1 && base[l1-1] == path_seperator)
199         l1--;
200     while(pos < l2 && add[pos] == path_seperator)
201         pos++;
202     n = (char*)malloc(l1 + (l2-pos) + 2);
203     memcpy(n,base,l1);
204     n[l1]=path_seperator;
205     memcpy(&n[l1+1],&add[pos],l2-pos+1);
206     return n;
207 }
208
209 char*find_file(const char*filename, char error)
210 {
211     include_dir_t*i = current_include_dirs;
212     FILE*fi = 0;
213     if(is_absolute(filename)) {
214         FILE*fi = fopen(filename, "rb");
215         if(fi) {
216             fclose(fi);
217             return strdup(filename);
218         }
219     } else {
220         if(!i && error) {
221             as3_warning("Include directory stack is empty, while looking for file %s", filename);
222         }
223         while(i) {
224             char*p = concat_paths(i->path, filename);
225             fi = fopen(p, "rb");
226             if(fi) {
227                 fclose(fi);
228                 return p;
229             } else {
230                 free(p);
231             }
232             i = i->next;
233         }
234     }
235     if(!error) {
236         return 0;
237     }
238
239     as3_error("Couldn't find file %s", filename);
240     i = current_include_dirs;
241     while(i) {
242         fprintf(stderr, "include dir: %s\n", i->path);
243         i = i->next;
244     }
245     return 0;
246 }
247
248 void enter_file(const char*name, const char*filename, void*state)
249 {
250     if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
251         as3_error("Includes nested too deeply");
252         exit(1);
253     }
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;
261     
262     /*char*dir = get_path(filename);
263     add_include_dir(dir);
264     free(dir);*/
265
266     include_stack_ptr++;
267     
268     dbg("entering file %s", filename);
269
270     current_line=1;
271     current_column=0;
272     current_filename = strdup(name);
273     current_filename_short = strdup(name);
274     current_filename_long = strdup(filename);
275 }
276
277 FILE*enter_file2(const char*name, const char*filename, void*state)
278 {
279     enter_file(name, filename, state);
280     FILE*fi = fopen(filename, "rb");
281     if(!fi) {
282         as3_error("Couldn't find file %s: %s", filename, strerror(errno));
283     }
284     return fi;
285 }
286
287 void* leave_file()
288 {
289     dbg("leaving file %s", current_filename);
290     if(--include_stack_ptr<=0) {
291         return 0;
292     } else {
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];
301     }
302 }