39
37
start(EscriptOptions) ->
41
[File|Args] = init:get_plain_arguments(),
42
do_run(File, Args, EscriptOptions)
39
%% Commands run using -run or -s are run in a process
40
%% trap_exit set to false. Because this behaviour is
41
%% surprising for users of escript, make sure to reset
42
%% trap_exit to false.
43
process_flag(trap_exit, false),
44
case init:get_plain_arguments() of
46
do_run(File, Args, EscriptOptions);
48
io:format("escript: Missing filename\n", []),
45
53
io:format("escript: ~s\n", [Str]),
66
76
fatal("There were compilation errors.")
69
79
eval_module(Mode, Parse, File, Args)
72
82
eval_module(interpret, Parse, File, Args) ->
73
83
interpret(File, Parse, Args);
74
84
eval_module(compile, Parse, _File, Args) ->
86
eval_module(run, Module, _File, Args) ->
87
run_code(Module, Args).
77
89
interpret(File, Parse0, Args) ->
78
90
case erl_lint:module(Parse0) of
131
143
Dict = dict:store({local, Name,Arity}, Clauses, Dict0),
132
144
parse_to_dict(T, Dict);
133
145
parse_to_dict([{attribute,_,import,{Mod,Funcs}}|T], Dict0) ->
134
Dict = foldl(fun(I, D) ->
135
dict:store({remote,I}, Mod, D)
146
Dict = lists:foldl(fun(I, D) ->
147
dict:store({remote,I}, Mod, D)
137
149
parse_to_dict(T, Dict);
138
150
parse_to_dict([_|T], Dict) ->
139
151
parse_to_dict(T, Dict);
148
160
integer_to_list(K)),
149
161
{attribute,0,module, Mod}.
163
-define(PRETTY_APPLY(M, F, A), pretty_apply(?MODULE, ?LINE, M, F, A)).
151
165
parse_file(File) ->
152
166
parse_check_error(File, parse_file(File, 0, [], interpret)).
154
168
parse_file(File, Nerrs, L, Mode) ->
155
{ok, P} = file:open(File, [read]),
156
%% This is to skip the first line in the script
158
Ret = parse_loop(P, File, io:parse_erl_form(P, '', 2), Nerrs, L, Mode),
170
case file:open(File, [read]) of
174
fatal(lists:concat([file:format_error(R), ": '", File, "'"]))
176
{HeaderSz, BodyLineNo, FirstBodyLine} = skip_header(P),
177
case FirstBodyLine of
180
ok = ?PRETTY_APPLY(file, close, [P]),
181
{ok, <<_FirstLine:HeaderSz/binary, Bin/binary>>} =
182
?PRETTY_APPLY(file, read_file, [File]),
183
case code:set_primary_archive(File, Bin) of
186
case init:get_argument(escript) of
187
{ok, [["main", M]]} ->
190
Ext = init:archive_extension(),
191
list_to_atom(filename:basename(File, Ext))
195
fatal("Not an archive file");
199
[$F, $O, $R, $1 | _] ->
201
ok = ?PRETTY_APPLY(file, close, [P]),
202
{ok, <<_FirstLine:HeaderSz/binary, Bin/binary>>} =
203
?PRETTY_APPLY(file, read_file, [File]),
204
case beam_lib:version(Bin) of
205
{ok, {Mod, _Version}} ->
206
{module, Mod} = erlang:load_module(Mod, Bin),
208
{error, beam_lib, Reason} when is_tuple(Reason) ->
209
fatal(element(1, Reason));
210
{error, beam_lib, Reason} ->
215
{ok, _} = ?PRETTY_APPLY(file, position, [P, {bof, HeaderSz}]), % Goto prev pos
216
Ret = parse_loop(P, File, io:parse_erl_form(P, '', BodyLineNo), Nerrs, L, Mode),
217
ok = ?PRETTY_APPLY(file, close, [P]),
221
pretty_apply(Module, Line, M, F, A) ->
222
case apply(M, F, A) of
228
fatal({Module, Line, M, F, A, Reason})
231
%% Skip header and return first body line
233
%% Skip shebang on first line
235
{ok, HeaderSz1} = file:position(P, cur),
237
%% Look for special comment on second line
239
{ok, HeaderSz2} = file:position(P, cur),
241
[$\%, $\%, $\! | _] ->
242
%% Skip special comment on second line
244
{HeaderSz2, 3, Line3};
246
%% Look for special comment on third line
248
{ok, HeaderSz3} = file:position(P, cur),
250
[$\%, $\%, $\! | _] ->
251
%% Skip special comment on third line
253
{HeaderSz3, 4, Line4};
255
%% Just skip shebang on first line
256
{HeaderSz1, 2, Line2}
261
case io:get_line(P, '') of
263
fatal("Premature end of file reached");
162
268
parse_include_lib(File, Nerrs, L0, Mode) ->
163
269
case open_lib_dir(File) of
275
384
list_errors(_F, []) -> ok.
277
386
report_warnings(Ws0) ->
278
Ws1 = flatmap(fun({{F,_L},Eds}) -> format_message(F, Eds);
279
({F,Eds}) -> format_message(F, Eds) end,
387
Ws1 = lists:flatmap(fun({{F,_L},Eds}) -> format_message(F, Eds);
388
({F,Eds}) -> format_message(F, Eds) end,
281
390
Ws = ordsets:from_list(Ws1),
282
foreach(fun({_,Str}) -> io:put_chars(Str) end, Ws).
391
lists:foreach(fun({_,Str}) -> io:put_chars(Str) end, Ws).
284
393
format_message(F, [{Line,Mod,E}|Es]) ->
285
394
M = {{F,Line},io_lib:format("~s:~w: Warning: ~s\n", [F,Line,Mod:format_error(E)])},