Skip to content
Newer
Older
100644 510 lines (441 sloc) 17.5 KB
d69e4d8 @klacke ""
authored Feb 25, 2002
1 %%%----------------------------------------------------------------------
2 %%% File : yaws_compile.erl
3 %%% Author : Claes Wikstrom <klacke@hyber.org>
4 %%% Purpose :
5 %%% Created : 20 Feb 2002 by Claes Wikstrom <klacke@hyber.org>
6 %%%----------------------------------------------------------------------
7
8 -module(yaws_compile).
9 -author('klacke@hyber.org').
10
6cf3ece @klacke ""
authored Nov 20, 2005
11 -include("../include/yaws.hrl").
12 -include("../include/yaws_api.hrl").
3c06a9e @klacke rearr of includefiles
authored Oct 10, 2002
13 -include("yaws_debug.hrl").
d69e4d8 @klacke ""
authored Feb 25, 2002
14
15
16
17 %% tada !!
18 %% returns a CodeSpec which is:
19 %% a list {data, NumChars} |
20 %% {mod, LineNo, YawsFile, NumSkipChars, Mod, Func} |
21 %% {error, NumSkipChars, E}}
22
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
23 %% each erlang fragment inside <erl> .... </erl> is compiled into
24 %% its own module
d69e4d8 @klacke ""
authored Feb 25, 2002
25
26
27 -record(comp, {
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
28 gc, %% global conf
29 sc, %% server conf
30 startline = 0,
31 modnum = 1,
32 infile,
33 infd,
34 outfile,
35 outfd}).
d69e4d8 @klacke ""
authored Feb 25, 2002
36
9a74e5a @klacke Major general code cleanup, finally got rid of all the export_all sta…
authored May 12, 2008
37 -export([compile_file/1]).
38 %% internal exports
39 -export([compiler_proc/3]).
d69e4d8 @klacke ""
authored Feb 25, 2002
40
41 comp_opts(GC) ->
6c9c37f @klacke ""
authored Jun 15, 2002
42 ?Debug("I=~p~n", [GC#gconf.include_dir]),
d69e4d8 @klacke ""
authored Feb 25, 2002
43 I = lists:map(fun(Dir) -> {i, Dir} end, GC#gconf.include_dir),
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
44 Warnings = case get(use_yfile_name) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
45 true -> [return_warnings, debug_info];
46 _ -> []
47 end,
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
48 Opts = [binary, return_errors] ++ Warnings ++ I,
d69e4d8 @klacke ""
authored Feb 25, 2002
49 ?Debug("Compile opts = ~p~n", [Opts]),
50 Opts.
51
52
ef3a52a @klacke Removed the atoms in parse_post and parse query, backwards incompatib…
authored Mar 16, 2004
53 compile_file(File) ->
54 GC=get(gc), SC=get(sc),
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
55 case get(use_yfile_name) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
56 true ->
57 %% Run by 'yaws -check'
58 put(yfile,filename:rootname(yaws:to_list(File)));
59 _ ->
60 put(yfile,yaws:to_list(File))
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
61 end,
1c50de5 @klacke -yawsfile patcg from tobbe
authored Dec 3, 2003
62 %% broken erlang compiler isn't
63 %% reentrant, can only have one erlang compiler at a time running
9550a40 @klacke use lock on comiler, erlc is not reentrant
authored Apr 9, 2003
64 global:trans({yaws, self()},
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
65 fun() ->
66 ?Debug("Compile ~s~n", [File]),
67 case file_open(File) of
68 {ok, Fd} ->
69 Spec = compile_file(
70 #comp{infile = File,
71 infd = Fd, gc = GC, sc = SC},
72 1,
73 get_line(), init, 0, [], 0),
74 erase(yfile),
75 erase(yfile_data),
76 erase(yfile_data_orig),
77 ?Debug("Spec: ~p~n", [Spec]),
78 Spec;
79 _Err ->
80 yaws:elog("can't open ~s~n", [File]),
81 exit(normal)
82 end
83 end,
84 [node()], infinity).
85
86
f875c2b @klacke added <verbatim> tag for .yaws files, It works as the pre (or the cod…
authored Dec 10, 2004
87 clump_data([{data, I}, {data, J} | Tail]) ->
88 clump_data([{data, I+J}|Tail]);
89 clump_data([H|T]) ->
90 [H|clump_data(T)];
91 clump_data([]) ->
92 [].
93
d69e4d8 @klacke ""
authored Feb 25, 2002
94
9cd59a3 @klacke ""
authored Nov 16, 2002
95 compile_file(C, _LineNo, eof, _Mode, NumChars, Ack, Errors) ->
368cfec @klacke *** empty log message ***
authored Jan 24, 2003
96 file_close(C#comp.infd),
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
97 {ok, [{errors, Errors} |
98 clump_data(lists:reverse([{data, NumChars} |Ack]))]};
d69e4d8 @klacke ""
authored Feb 25, 2002
99
100
39c94ab @klacke ""
authored Mar 24, 2002
101 %% skip initial space if first thing is <erl> otherwise not
8722844 @klacke a file which has at least one compilation error shouldn't be cached i…
authored Sep 17, 2002
102 compile_file(C, LineNo, Chars, init, NumChars, Ack, Errs) ->
d69e4d8 @klacke ""
authored Feb 25, 2002
103 case Chars -- [$\s, $\t, $\n, $\r] of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
104 [] ->
105 ?Debug("SKIP ~p~n", [Chars]),
106 L=length(Chars),
107 compile_file(C, LineNo+1, line(), init, NumChars-L, Ack, Errs);
108 "<erl" ++ _ -> %% first chunk is erl, skip whistespace
109 compile_file(C, LineNo, Chars, html, NumChars, Ack, Errs);
110 _ ->
111 %% first chunk is html, keep whitespace
112 file_position_bof(),
113 compile_file(C,1,line(),html,0,[], Errs)
d69e4d8 @klacke ""
authored Feb 25, 2002
114 end;
115
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
116 compile_file(C, LineNo, Chars = "<erl" ++ Tail, html, NumChars, Ack,Es) ->
d69e4d8 @klacke ""
authored Feb 25, 2002
117 ?Debug("start erl:~p",[LineNo]),
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
118 C2 = new_out_file(LineNo, C, Tail, C#comp.gc),
d69e4d8 @klacke ""
authored Feb 25, 2002
119 C3 = C2#comp{startline = LineNo},
120 L = length(Chars),
121 if
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
122 NumChars > 0 ->
123 compile_file(C3, LineNo+1, line() , erl,L,
124 [{data, NumChars} | Ack], Es);
125 true -> %% just ignore zero byte data segments
126 compile_file(C3, LineNo+1, line() , erl, L + (-NumChars),
127 Ack, Es) %hack
d69e4d8 @klacke ""
authored Feb 25, 2002
128 end;
129
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
130 compile_file(C, LineNo, Chars = "<verbatim>" ++ _Tail, html,
131 NumChars, Ack,Es) ->
f875c2b @klacke added <verbatim> tag for .yaws files, It works as the pre (or the cod…
authored Dec 10, 2004
132 ?Debug("start verbatim:~p",[LineNo]),
133 Len = length(Chars),
134 C2 = C#comp{outfile = ["<pre>\n"]}, %% use as accumulator
135 compile_file(C2, LineNo+1, line() , verbatim , Len, [{data, NumChars} | Ack], Es);
136
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
137 compile_file(C, LineNo, Chars = "</verbatim>" ++ _Tail, verbatim,
138 NumChars, Ack, Es) ->
f875c2b @klacke added <verbatim> tag for .yaws files, It works as the pre (or the cod…
authored Dec 10, 2004
139 Data = list_to_binary(lists:reverse(["</pre>\n" | C#comp.outfile])),
140 Len = length(Chars),
141 compile_file(C#comp{outfile = undefined}, LineNo, line(), html, 0,
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
142 [{verbatim, NumChars+Len, Data} |Ack], Es);
f875c2b @klacke added <verbatim> tag for .yaws files, It works as the pre (or the cod…
authored Dec 10, 2004
143
144 compile_file(C, LineNo, Chars, verbatim, NumChars, Ack,Es) ->
145 case has_str(Chars, ["</verbatim>"]) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
146 {ok, Skipped, Chars2} ->
147 compile_file(C, LineNo, Chars2, verbatim,
148 NumChars + Skipped, Ack,Es);
149 false ->
150 C2 = C#comp{outfile = [yaws_api:htmlize(Chars) | C#comp.outfile]},
151 compile_file(C2, LineNo+1, line(), verbatim, NumChars +
152 length(Chars), Ack,Es)
f875c2b @klacke added <verbatim> tag for .yaws files, It works as the pre (or the cod…
authored Dec 10, 2004
153 end;
154
92bafb4 @klacke removed the ability change userid, also stopped writing to /tmp/yaws …
authored Mar 9, 2006
155 compile_file(C, LineNo, _Chars = "</erl>" ++ Tail, erl, NumChars, Ack, Es) ->
d69e4d8 @klacke ""
authored Feb 25, 2002
156 ?Debug("stop erl:~p",[LineNo]),
157 file:close(C#comp.outfd),
158 case proc_compile_file(C#comp.outfile, comp_opts(C#comp.gc)) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
159 {ok, ModuleName, Binary, Warnings} ->
160 case get(use_yfile_name) of
161 true ->
162 file:write_file("../../ebin/" ++
163 filename:rootname(C#comp.outfile)++".beam",
164 Binary);
165 _ ->
166 ok
167 end,
168 comp_warn(C, Warnings),
169 case code:load_binary(ModuleName, C#comp.outfile, Binary) of
170 {module, ModuleName} ->
171 C2 = C#comp{modnum = C#comp.modnum+1},
172 L2 = check_exported(C, LineNo, NumChars, ModuleName),
173 compile_file(C2, LineNo, Tail, html, 0,
174 L2++[{skip, 6}|Ack], Es);
175 Err ->
176 A2 = gen_err(C, LineNo, NumChars,
177 ?F("Cannot load module ~p: ~p",
178 [ModuleName, Err])),
179 compile_file(C, LineNo, Tail, html, 0,
180 [A2, {skip, 6}|Ack], Es+1)
181 end;
182 {error, Errors, Warnings} ->
183 %% FIXME remove outfile here ... keep while debuging
184 A2 = comp_err(C, LineNo, NumChars, Errors, Warnings),
185 compile_file(C, LineNo, Tail, html, 0, [A2, {skip, 6}|Ack], Es+1);
186 {error, Str} ->
187 %% this is boring but does actually happen
188 %% in order to get proper user errors here we need to catch i/o
189 %% or hack compiler/parser
190 yaws:elog("Dynamic compile error in file ~s (~s), line ~w~n~s",
191 [C#comp.infile, C#comp.outfile,LineNo, Str]),
192 A2 = {error, NumChars, ?F("<pre> Dynamic compile error in file "
193 " ~s line ~w~n~s </pre>",
194 [C#comp.infile, LineNo, Str])},
195 compile_file(C, LineNo, Tail, html, 0, [A2, {skip, 6}|Ack], Es+1)
d69e4d8 @klacke ""
authored Feb 25, 2002
196 end;
197
8722844 @klacke a file which has at least one compilation error shouldn't be cached i…
authored Sep 17, 2002
198 compile_file(C, LineNo, Chars, erl, NumChars, Ack,Es) ->
a9a9971 The yaws callback out/1 can now return {bindings, [{Key, Value}]}. All
Jocke Grebeno authored Jan 26, 2004
199 case has_str(Chars, ["</erl>"]) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
200 {ok, Skipped, Chars2} ->
201 compile_file(C, LineNo, Chars2, erl, NumChars + Skipped, Ack,Es);
202 false ->
203 ?Debug("Gen: ~s", [Chars]),
204 io:format(C#comp.outfd, "~s", [Chars]),
205 compile_file(C, LineNo+1, line(), erl, NumChars +
206 length(Chars), Ack,Es)
0d623c1 @klacke ""
authored Jun 10, 2002
207 end;
208
a9a9971 The yaws callback out/1 can now return {bindings, [{Key, Value}]}. All
Jocke Grebeno authored Jan 26, 2004
209 compile_file(C, LineNo, [], html, NumChars, Ack, Es) ->
210 compile_file(C, LineNo+1, line(), html, NumChars, Ack, Es);
d69e4d8 @klacke ""
authored Feb 25, 2002
211
8722844 @klacke a file which has at least one compilation error shouldn't be cached i…
authored Sep 17, 2002
212 compile_file(C, LineNo, Chars, html, NumChars, Ack,Es) ->
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
213 case has_str(Chars, ["<erl", "%%", "<verbatim>"]) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
214 {ok, Skipped, "<erl"++_ = Chars2} ->
215 compile_file(C, LineNo, Chars2, html, NumChars+Skipped, Ack, Es);
216 {ok, Skipped, "<verbatim>"++_ = Chars2} ->
217 compile_file(C, LineNo, Chars2, html, NumChars+Skipped, Ack, Es);
218 {ok, Skipped, "%%"++Chars2} ->
219 compile_file(C, LineNo, Chars2, binding, 2,
220 [{data, NumChars+Skipped}|Ack], Es);
221 false ->
222 compile_file(C, LineNo, tl(Chars), html, NumChars+1, Ack, Es)
0d623c1 @klacke ""
authored Jun 10, 2002
223 end;
d69e4d8 @klacke ""
authored Feb 25, 2002
224
a9a9971 The yaws callback out/1 can now return {bindings, [{Key, Value}]}. All
Jocke Grebeno authored Jan 26, 2004
225 compile_file(C, LineNo, [], binding, NumChars, Ack, Es) ->
226 compile_file(C, LineNo+1, line(), html, NumChars, Ack, Es);
227
228 compile_file(C, LineNo, "%%"++Chars, binding, NumChars, Ack, Es) ->
229 compile_file(C, LineNo, Chars, html, 0, [{binding, NumChars+2}|Ack], Es);
d69e4d8 @klacke ""
authored Feb 25, 2002
230
92bafb4 @klacke removed the ability change userid, also stopped writing to /tmp/yaws …
authored Mar 9, 2006
231 compile_file(C, LineNo, [_H|T], binding, NumChars, Ack, Es) ->
a9a9971 The yaws callback out/1 can now return {bindings, [{Key, Value}]}. All
Jocke Grebeno authored Jan 26, 2004
232 compile_file(C, LineNo, T, binding, NumChars+1, Ack, Es).
d69e4d8 @klacke ""
authored Feb 25, 2002
233
234
a9a9971 The yaws callback out/1 can now return {bindings, [{Key, Value}]}. All
Jocke Grebeno authored Jan 26, 2004
235 has_str(L, Strs) -> has_str(L, Strs, 0).
236 has_str([H|T], Strs, Num) ->
0d623c1 @klacke ""
authored Jun 10, 2002
237 case yaws:is_space(H) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
238 true -> has_str(T, Strs, Num+1);
239 false ->
240 case lists:any(fun(Str) -> lists:prefix(Str, [H|T]) end, Strs) of
241 true -> {ok, Num, [H|T]};
242 false -> false
243 end
0d623c1 @klacke ""
authored Jun 10, 2002
244 end;
a9a9971 The yaws callback out/1 can now return {bindings, [{Key, Value}]}. All
Jocke Grebeno authored Jan 26, 2004
245 has_str(_,_,_) -> false.
246
d69e4d8 @klacke ""
authored Feb 25, 2002
247
248 check_exported(C, LineNo, NumChars, Mod) ->
249 case is_exported(out, 1, Mod) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
250 true ->
251 [{mod, C#comp.startline, C#comp.infile,
252 NumChars,Mod,out}];
253 false ->
254 ?Debug("XX ~p~n", [C]),
255 [gen_err(C, LineNo, NumChars,
256 "out/1 is not defined ")]
d69e4d8 @klacke ""
authored Feb 25, 2002
257 end.
258
a19bb41 @klacke skip empty space after an </erl> and handle http_error which is gener…
authored Mar 6, 2003
259 line() ->
260 get_line().
d69e4d8 @klacke ""
authored Feb 25, 2002
261
262 is_exported(Fun, A, Mod) ->
263 case (catch Mod:module_info()) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
264 List when list(List) ->
265 case lists:keysearch(exports, 1, List) of
266 {value, {exports, Exp}} ->
267 lists:member({Fun, A}, Exp);
268 _ ->
269 false
270 end;
271 _ ->
272 false
d69e4d8 @klacke ""
authored Feb 25, 2002
273 end.
274
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
275
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
276 new_out_file_module(Tail) ->
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
277 case Tail of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
278 ">" ++ _ ->
279 Mnum = case catch gen_server:call(yaws_server, mnum) of
280 {'EXIT', _} ->
281 1;
282 Other ->
283 Other
284 end,
285 Prefix = case get(use_yfile_name) of
286 true -> filename:rootname(get(yfile))++"_yaws";
287 _ -> "m"
288 end,
289 Prefix ++ integer_to_list(Mnum);
290 _ ->
291 case string:tokens(Tail, " =>\r\n\"") of
292 ["module", Module] ->
293 Module
294 end
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
295 end.
296
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
297 new_out_file_name(Module, GC) ->
298 case get(use_yfile_name) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
299 true ->
300 Module ++ ".erl";
301 _ ->
c105945 @klacke Redid the code which decides where the ctldir resides, now it always …
authored Apr 2, 2008
302 filename:join([yaws:id_dir(GC#gconf.id), Module ++ ".erl"])
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
303 end.
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
304
1c50de5 @klacke -yawsfile patcg from tobbe
authored Dec 3, 2003
305 %% this will generate 10 lines
f6dbbc9 @klacke added possibility to name the module in a .yaws file
authored Mar 21, 2005
306 new_out_file(Line, C, Tail, GC) ->
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
307 Module = new_out_file_module(Tail),
308 OutFile = new_out_file_name(Module, GC),
d69e4d8 @klacke ""
authored Feb 25, 2002
309 ?Debug("Writing outout file~s~n", [OutFile]),
310 {ok, Out} = file:open(OutFile, [write]),
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
311 ok = io:format(Out, "-module(\'~s\').~n-export([out/1]).~n~n", [Module]),
1c50de5 @klacke -yawsfile patcg from tobbe
authored Dec 3, 2003
312 ok = io:format(Out, "-yawsfile('" ++ get(yfile) ++ "').~n",[]),
d69e4d8 @klacke ""
authored Feb 25, 2002
313 io:format(Out, "%%~n%% code at line ~w from file ~s~n%%~n",
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
314 [Line, C#comp.infile]),
dc00e52 @klacke postvar bug by hal snyder, added yaws_api:query_url/1 added the id su…
authored Mar 10, 2004
315 io:format(Out, "-import(yaws_api, [f/2, fl/1, postvar/2, queryvar/2])."
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
316 " ~n~n", []),
d69e4d8 @klacke ""
authored Feb 25, 2002
317 io:format(Out, '-include("~s/include/yaws_api.hrl").~n',
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
318 [GC#gconf.yaws_dir]),
d69e4d8 @klacke ""
authored Feb 25, 2002
319 C#comp{outfd = Out,
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
320 outfile = OutFile}.
d69e4d8 @klacke ""
authored Feb 25, 2002
321
322
2129adc @klacke ""
authored May 30, 2002
323 gen_err(C, _LineNo, NumChars, Err) ->
d69e4d8 @klacke ""
authored Feb 25, 2002
324 S = io_lib:format("<p> Error in File ~s Erlang code beginning "
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
325 "at line ~w~n"
326 "Error is: ~p~n", [C#comp.infile, C#comp.startline,
327 Err]),
d69e4d8 @klacke ""
authored Feb 25, 2002
328 yaws:elog("~s~n", [S]),
329 {error, NumChars, S}.
330
331
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
332 comp_err(C, LineNo, NumChars, Err, Warns) ->
333 case get(use_yfile_name) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
334 true ->
335 report_errors(C, Err),
336 report_warnings(C, Warns),
337 {error, NumChars, ""};
338 _ ->
339 comp_err(C, LineNo, NumChars, Err)
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
340 end.
341
2129adc @klacke ""
authored May 30, 2002
342 comp_err(C, _LineNo, NumChars, Err) ->
d69e4d8 @klacke ""
authored Feb 25, 2002
343 case Err of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
344 [{_FileName, [ {Line0, Mod, E} |_]} |_] when integer(Line0) ->
345 Line = Line0 + C#comp.startline - 10,
346 ?Debug("XX ~p~n", [{_LineNo, Line0}]),
347 Str = io_lib:format("~s:~w:~n ~s\ngenerated file at: ~s~n",
348 [C#comp.infile, Line,
349 apply(Mod, format_error, [E]),
350 C#comp.outfile
351 ]),
352 HtmlStr = ?F("~n<pre>~nDynamic compile error: ~s~n</pre>~n",
353 [Str]),
354 yaws:elog("Dynamic compiler err ~s", [Str]),
355 {error, NumChars, HtmlStr};
356 _Other ->
357 yaws:elog("Dynamic compile error ~p", [Err]),
358 {error, NumChars, ?F("<pre> Compile error - "
359 "Other err ~p</pre>~n", [Err])}
d69e4d8 @klacke ""
authored Feb 25, 2002
360 end.
361
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
362 comp_warn(C, Warnings) ->
363 case get(use_yfile_name) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
364 true ->
365 report_warnings(C, Warnings);
366 _ ->
367 ok
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
368 end.
d69e4d8 @klacke ""
authored Feb 25, 2002
369
370 %% due to compiler not producing proper error
371 %% we NEED to catch all io produced by the compiler
372
373 proc_compile_file(F, Opts) ->
374 G = group_leader(),
375 group_leader(self(), self()),
376 P = proc_lib:spawn(?MODULE, compiler_proc, [self(), F, Opts]),
377 Res = get_compiler_data(P, []),
378 group_leader(G, self()),
379 Res.
380
381 compiler_proc(Top, F, Opts) ->
382 R = (catch compile:file(F, Opts)),
383 Top ! {self(), result, R}.
384
385
386 get_compiler_data(P, Ack) ->
387 receive
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
388 {P, result, {ok, Mod, Bin}} ->
389 {ok, Mod, Bin, []};
390 {P, result, {ok, Mod, Bin, Warnings}} ->
391 {ok, Mod, Bin, Warnings};
392 {io_request, P1, P2, {put_chars, M, F, A}} ->
393 P1 ! {io_reply, P2, ok},
394 Str = apply(M, F, A),
395 get_compiler_data(P, [Str|Ack]);
396 {P, result, {error, Errors, Warnings}} ->
397 {error, Errors, Warnings};
398 {P, result, error} ->
399 S = lists:map(
400 fun(S) -> S ++ "\n" end, lists:reverse(Ack)),
401 {error, S};
402 {P, result, {'EXIT', Reason}} ->
403 S = lists:flatten(io_lib:format("~p", [Reason])),
1257b7f @klacke handle compiler crash
authored Dec 1, 2006
404 {error, S}
d69e4d8 @klacke ""
authored Feb 25, 2002
405 end.
406
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
407
d69e4d8 @klacke ""
authored Feb 25, 2002
408
7c5307a @klacke don't log a crash when we get methods we don't support and also a fix…
authored Jan 23, 2003
409 %% This code is so that we get the \r in the line
410 %% when we're parsing msdos files.
411
567034e @klacke *** empty log message ***
authored Jan 23, 2003
412 file_open(Fname) ->
413 case file:read_file(Fname) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
414 {ok, Bin} ->
415 put(yfile_data, binary_to_list(Bin)),
416 put(yfile_data_orig, Bin),
417 {ok, yfile_data};
418 Err ->
419 Err
7c5307a @klacke don't log a crash when we get methods we don't support and also a fix…
authored Jan 23, 2003
420 end.
421
368cfec @klacke *** empty log message ***
authored Jan 24, 2003
422 file_close(Key) ->
423 erase(Key).
567034e @klacke *** empty log message ***
authored Jan 23, 2003
424
78cbea3 @klacke ""
authored Jan 27, 2003
425 file_position_bof() ->
426 put(yfile_data, binary_to_list(get(yfile_data_orig))).
567034e @klacke *** empty log message ***
authored Jan 23, 2003
427
a19bb41 @klacke skip empty space after an </erl> and handle http_error which is gener…
authored Mar 6, 2003
428 get_line() ->
567034e @klacke *** empty log message ***
authored Jan 23, 2003
429 case get (yfile_data) of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
430 [] ->
431 eof;
432 Chars ->
433 case get_line_from_chars(Chars, []) of
434 {ok, Line, Tail} ->
435 put (yfile_data, Tail),
436 Line;
437 need_more ->
438 put(yfile_data, []),
439 Chars
440 end
7c5307a @klacke don't log a crash when we get methods we don't support and also a fix…
authored Jan 23, 2003
441 end.
442
443 get_line_from_chars([$\r, $\n | Tail], Line) ->
444 {ok, lists:reverse([$\n, $\r|Line]), Tail};
445
446 get_line_from_chars([$\n | Tail], Line) ->
447 {ok, lists:reverse([$\n|Line]), Tail};
d69e4d8 @klacke ""
authored Feb 25, 2002
448
a19bb41 @klacke skip empty space after an </erl> and handle http_error which is gener…
authored Mar 6, 2003
449 get_line_from_chars([], _Line) ->
7c5307a @klacke don't log a crash when we get methods we don't support and also a fix…
authored Jan 23, 2003
450 need_more;
451 get_line_from_chars([H|T], Line) ->
452 get_line_from_chars(T, [H|Line]).
d69e4d8 @klacke ""
authored Feb 25, 2002
453
454
455
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
456 %% -----------------------------------------------------------------
457 %% From compile.erl in order to print proper error/warning messages
458 %% if compiled with check option.
459 report_errors(C, Errors) ->
460 Check = true,
461 case Check of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
462 true ->
463 File = "./" ++ filename:basename(C#comp.infile),
464 SLine = C#comp.startline - 10,
465 lists:foreach(fun ({{_F,_L},Eds}) -> list_errors(File, SLine, Eds);
466 ({_F,Eds}) -> list_errors(File, SLine, Eds)
467 end, Errors);
468 false ->
469 ok
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
470 end.
471
472 report_warnings(C, Ws0) ->
473 Check = true,
474 case Check of
0be3c7e @klacke untabified all of yaws
authored Feb 14, 2008
475 true ->
476 File = "./" ++ filename:basename(C#comp.infile),
477 SLine = C#comp.startline - 10,
478 Ws1 = lists:flatmap(fun({{_F,_L},Eds}) ->
479 format_message(File, SLine, Eds);
480 ({_F,Eds}) ->
481 format_message(File, SLine, Eds)
482 end,
483 Ws0),
484 Ws = ordsets:from_list(Ws1),
485 lists:foreach(fun({_,Str}) -> io:put_chars(Str) end, Ws);
486 false -> ok
a7e93df @klacke patch from Magnus froberg to get better control over the files genera…
authored Aug 28, 2006
487 end.
488
489 format_message(F, SLine, [{Line0,Mod,E}|Es]) ->
490 Line = Line0 + SLine,
491 M = {{F,Line},io_lib:format("~s:~w: Warning: ~s\n", [F,Line,Mod:format_error(E)])},
492 [M|format_message(F, SLine, Es)];
493 format_message(F, SLine, [{Mod,E}|Es]) ->
494 M = {none,io_lib:format("~s: Warning: ~s\n", [F,Mod:format_error(E)])},
495 [M|format_message(F, SLine, Es)];
496 format_message(_, _, []) -> [].
497
498 %% list_errors(File, StartLine, ErrorDescriptors) -> ok
499
500 list_errors(F, SLine, [{Line0,Mod,E}|Es]) ->
501 Line = Line0 + SLine,
502 io:fwrite("~s:~w: ~s\n", [F,Line,Mod:format_error(E)]),
503 list_errors(F, SLine, Es);
504 list_errors(F, SLine, [{Mod,E}|Es]) ->
505 io:fwrite("~s: ~s\n", [F,Mod:format_error(E)]),
506 list_errors(F, SLine, Es);
507 list_errors(_F, _SLine, []) ->
508 ok.
509
Something went wrong with that request. Please try again.