added _noerror version of functions
[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 "tokenizer.h"
29
30 static int verbose = 0;
31 static void dbg(const char*format, ...)
32 {
33     char buf[1024];
34     int l;
35     va_list arglist;
36     if(!verbose)
37         return;
38     va_start(arglist, format);
39     vsprintf(buf, format, arglist);
40     va_end(arglist);
41     l = strlen(buf);
42     while(l && buf[l-1]=='\n') {
43         buf[l-1] = 0;
44         l--;
45     }
46     printf("(includefilehandler) ");
47     printf("%s\n", buf);
48     fflush(stdout);
49 }
50
51
52 int current_line=1;
53 int current_column=0;
54 char* current_filename=0;
55 char* current_filename_short=0;
56 include_dir_t* current_include_dirs=0;
57
58 #define MAX_INCLUDE_DEPTH 16
59
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;
67
68 void add_include_dir(char*dir)
69 {
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;
75 }
76
77 void del_include_dirs(include_dir_t*d, include_dir_t*d2)
78 {
79     while(d && d!=d2) {
80         include_dir_t*next = d->next;
81         free(d->path);d->path=0;
82         d->next = 0;
83         free(d);
84         d = next;
85     }
86 }
87
88 char*get_path(const char*file)
89 {
90     char*path = strdup(file);
91     char*r1 = strrchr(path, '/');
92     char*r2 = strrchr(path, '\\');
93     if(r1<r2) {
94         *r2=0;
95         return path;
96     } else if(r1>r2) {
97         *r1=0;
98         return path;
99     } else {
100         return strdup(".");
101     }
102 }
103 char* normalize_path(const char*path)
104 {
105     char*n = 0, *d = 0;
106     if(path[0] != '/') {
107         char buf[512];
108         char*c = getcwd(buf,512);
109         int l = strlen(buf);
110         d = n = malloc(l+strlen(path)+10);
111         strcpy(n, buf);d += l;
112         if(!l || n[l-1]!='/') {
113             *d='/';d++;
114         }
115     } else {
116         d = n = strdup(path);
117     }
118     const char*s=path;
119     char init = 1;
120
121     while(*s) {
122         if(init && s[0] == '.' && (s[1]=='/' || s[1]=='\0')) {
123             if(!s[1]) break;
124             s+=2;
125             init=1;
126             continue;
127         }
128         if(init && s[0] == '.' && s[1] == '.' && (s[2] == '/' || s[2]=='\0')) {
129             // step one down
130             char*last = 0;
131             if(d<=n) 
132                 return 0;
133             *--d = 0;
134             if(!(last=strrchr(n, '/'))) {
135                 return 0;
136             }
137             d = last+1;
138             if(!s[2]) break;
139             s+=3;
140             init=1;
141             continue;
142         }
143
144         *d = *s;
145         if(*s=='/') init=1;
146         else init=0;
147         d++;s++;
148     }
149     if(d!=n && d[-1]=='/') 
150         d--;
151     *d = 0;
152     return n;
153 }
154 static void testnormalize()
155 {
156 #define TEST(x) {printf("%s -> %s\n", (x), normalize_path(x));}
157     TEST(".");
158     TEST("../as3");
159     TEST("../as3/");
160     TEST("../as3/parser.y");
161     TEST("../as3/ok/../ok/scope.as");
162     TEST("ok/scope.as");
163     TEST("ok/./scope.as");
164     TEST("./ok/scope.as");
165     TEST("./");
166     TEST("/tmp/");
167     TEST("/./tmp/");
168     TEST("../");
169     TEST("/");
170     TEST("/tmp");
171     TEST("/tmp/../usr/");
172 }
173
174
175 char* concat_paths(const char*base, const char*add)
176 {
177     int l1 = strlen(base);
178     int l2 = strlen(add);
179     int pos = 0;
180     char*n = 0;
181     while(l1 && base[l1-1] == '/')
182         l1--;
183     while(pos < l2 && add[pos] == '/')
184         pos++;
185     n = (char*)malloc(l1 + (l2-pos) + 2);
186     memcpy(n,base,l1);
187     n[l1]='/';
188     memcpy(&n[l1+1],&add[pos],l2-pos+1);
189     return n;
190 }
191 char is_absolute(const char*filename) 
192 {
193     if(!filename || !filename[0])
194         return 0;
195     if(filename[0]=='/' || filename[0]=='\\')
196         return 1;
197     if(filename[1]==':' && filename[1]=='/')
198         return 1;
199     if(filename[1]==':' && filename[1]=='\\')
200         return 1;
201     return 0;
202 }
203
204 char*find_file(const char*filename, char error)
205 {
206     include_dir_t*i = current_include_dirs;
207     FILE*fi = 0;
208     if(is_absolute(filename)) {
209         FILE*fi = fopen(filename, "rb");
210         if(fi) {
211             fclose(fi);
212             return strdup(filename);
213         }
214     } else {
215         if(!i && error) {
216             as3_warning("Include directory stack is empty, while looking for file %s", filename);
217         }
218         while(i) {
219             char*p = concat_paths(i->path, filename);
220             fi = fopen(p, "rb");
221             if(fi) {
222                 fclose(fi);
223                 return p;
224             } else {
225                 free(p);
226             }
227             i = i->next;
228         }
229     }
230     if(!error) {
231         return 0;
232     }
233
234     as3_error("Couldn't find file %s", filename);
235     i = current_include_dirs;
236     while(i) {
237         fprintf(stderr, "include dir: %s\n", i->path);
238         i = i->next;
239     }
240     return 0;
241 }
242
243 void enter_file(const char*name, const char*filename, void*state)
244 {
245     if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
246         as3_error("Includes nested too deeply");
247         exit(1);
248     }
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;
255     
256     /*char*dir = get_path(filename);
257     add_include_dir(dir);
258     free(dir);*/
259
260     include_stack_ptr++;
261     
262     dbg("entering file %s", filename);
263
264     current_line=1;
265     current_column=0;
266     current_filename = strdup(filename);
267     current_filename_short = strdup(name);
268 }
269
270 FILE*enter_file2(const char*name, const char*filename, void*state)
271 {
272     enter_file(name, filename, state);
273     FILE*fi = fopen(filename, "rb");
274     if(!fi) {
275         as3_error("Couldn't find file %s: %s", filename, strerror(errno));
276     }
277     return fi;
278 }
279
280 void* leave_file()
281 {
282     dbg("leaving file %s", current_filename);
283     if(--include_stack_ptr<=0) {
284         return 0;
285     } else {
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];
293     }
294 }