Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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