From 3f74aa2f7a1ef3703d749edae627cb3766d40434 Mon Sep 17 00:00:00 2001 From: Magnus Henoch Date: Tue, 8 Jan 2013 12:35:33 +0000 Subject: escript to accept emulator arguments when script file has no shebang According to the documentation, if the second or third line in a script starts with %%!, then escript will use the rest of the line as emulator options. However, previously this was only the case if the first line started with #!. This change removes that check, and unconditionally uses the %%! line if present. --- erts/etc/common/escript.c | 2 +- lib/stdlib/test/escript_SUITE.erl | 17 +++++++++++++++++ .../test/escript_SUITE_data/emulator_flags_no_shebang | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 lib/stdlib/test/escript_SUITE_data/emulator_flags_no_shebang diff --git a/erts/etc/common/escript.c b/erts/etc/common/escript.c index 9e80ec6656..3566406bf3 100644 --- a/erts/etc/common/escript.c +++ b/erts/etc/common/escript.c @@ -264,7 +264,7 @@ append_shebang_args(char* scriptname) static char linebuf[LINEBUFSZ]; char* ptr = fgets(linebuf, LINEBUFSZ, fd); - if (ptr != NULL && linebuf[0] == '#' && linebuf[1] == '!') { + if (ptr != NULL) { /* Try to find args on second or third line */ ptr = fgets(linebuf, LINEBUFSZ, fd); if (ptr != NULL && linebuf[0] == '%' && linebuf[1] == '%' && linebuf[2] == '!') { diff --git a/lib/stdlib/test/escript_SUITE.erl b/lib/stdlib/test/escript_SUITE.erl index 7634c21a17..3749d594f2 100644 --- a/lib/stdlib/test/escript_SUITE.erl +++ b/lib/stdlib/test/escript_SUITE.erl @@ -26,6 +26,7 @@ errors/1, strange_name/1, emulator_flags/1, + emulator_flags_no_shebang/1, module_script/1, beam_script/1, archive_script/1, @@ -45,6 +46,7 @@ suite() -> [{ct_hooks,[ts_install_cth]}]. all() -> [basic, errors, strange_name, emulator_flags, + emulator_flags_no_shebang, module_script, beam_script, archive_script, epp, create_and_extract, foldl, overflow, archive_script_file_access, unicode]. @@ -149,6 +151,21 @@ emulator_flags(Config) when is_list(Config) -> "ExitCode:0">>]), ok. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +emulator_flags_no_shebang(Config) when is_list(Config) -> + Data = ?config(data_dir, Config), + Dir = filename:absname(Data), %Get rid of trailing slash. + %% Need run_with_opts, to always use "escript" explicitly + ?line run_with_opts(Dir, "", "emulator_flags_no_shebang -arg1 arg2 arg3", + [<<"main:[\"-arg1\",\"arg2\",\"arg3\"]\n" + "nostick:[{nostick,[]}]\n" + "mnesia:[{mnesia,[\"dir\",\"a/directory\"]},{mnesia,[\"debug\",\"verbose\"]}]\n" + "ERL_FLAGS=false\n" + "unknown:[]\n" + "ExitCode:0">>]), + ok. + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Pick the source code from the emulator_flags script %% Generate a new escript with a module header diff --git a/lib/stdlib/test/escript_SUITE_data/emulator_flags_no_shebang b/lib/stdlib/test/escript_SUITE_data/emulator_flags_no_shebang new file mode 100644 index 0000000000..47d843ebe1 --- /dev/null +++ b/lib/stdlib/test/escript_SUITE_data/emulator_flags_no_shebang @@ -0,0 +1,10 @@ +%% -*- erlang -*- +%%! -nostick -mnesia dir a/directory -mnesia debug verbose + +main(MainArgs) -> + io:format("main:~p\n",[MainArgs]), + ErlArgs = init:get_arguments(), + io:format("nostick:~p\n",[[E || E <- ErlArgs, element(1, E) =:= nostick]]), + io:format("mnesia:~p\n", [[E || E <- ErlArgs, element(1, E) =:= mnesia]]), + io:format("ERL_FLAGS=~p\n", [os:getenv("ERL_FLAGS")]), + io:format("unknown:~p\n",[[E || E <- ErlArgs, element(1, E) =:= unknown]]). -- cgit v1.2.3