Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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