aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Bolinder <[email protected]>2013-03-18 15:31:07 +0100
committerHans Bolinder <[email protected]>2013-03-19 13:03:01 +0100
commitf247d246725d76412242e547e7c212bf947542f8 (patch)
treecfa871edd7a69c0004608cb8eaf97b375bcae201
parent395e5870020df6807c6e73462e75d594a07e40a8 (diff)
downloadotp-f247d246725d76412242e547e7c212bf947542f8.tar.gz
otp-f247d246725d76412242e547e7c212bf947542f8.tar.bz2
otp-f247d246725d76412242e547e7c212bf947542f8.zip
Let escript recognize an encoding comment on the second line
The manual says that an Emacs directive can be placed on the second line. With this patch that directive is also recognized when selecting encoding of the script.
-rw-r--r--erts/doc/src/escript.xml11
-rw-r--r--lib/stdlib/src/escript.erl8
-rw-r--r--lib/stdlib/test/escript_SUITE.erl3
-rwxr-xr-xlib/stdlib/test/escript_SUITE_data/unicode32
-rwxr-xr-xlib/stdlib/test/escript_SUITE_data/unicode412
-rwxr-xr-xlib/stdlib/test/escript_SUITE_data/unicode512
-rwxr-xr-xlib/stdlib/test/escript_SUITE_data/unicode613
7 files changed, 57 insertions, 4 deletions
diff --git a/erts/doc/src/escript.xml b/erts/doc/src/escript.xml
index 66e904f64f..9e2a87dde6 100644
--- a/erts/doc/src/escript.xml
+++ b/erts/doc/src/escript.xml
@@ -4,7 +4,7 @@
<comref>
<header>
<copyright>
- <year>2007</year><year>2011</year>
+ <year>2007</year><year>2013</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
@@ -85,6 +85,11 @@ $ <input>escript factorial 5</input> </pre>
enter the major mode for editing Erlang source files. If the
directive is present it must be located on the second
line.</p>
+
+ <p>If there is a comment selecting the <seealso
+ marker="stdlib:epp#encoding">encoding</seealso> it can be
+ located on the second line.</p>
+
<p>On the third line (or second line depending on the presence
of the Emacs directive), it is possible to give arguments to
the emulator, such as </p>
@@ -133,7 +138,9 @@ halt(1).</pre>
<pre>
-include_lib("kernel/include/file.hrl").</pre>
<p>to include the record definitions for the records used by the
- <c>file:read_link_info/1</c> function.</p>
+ <c>file:read_link_info/1</c> function. You can also select
+ encoding here, but if there is a valid encoding comment on
+ the second line it takes precedence.</p>
<p>The script will be checked for syntactic and semantic
correctness before being run. If there are warnings (such as
diff --git a/lib/stdlib/src/escript.erl b/lib/stdlib/src/escript.erl
index 32742e419b..fea718541d 100644
--- a/lib/stdlib/src/escript.erl
+++ b/lib/stdlib/src/escript.erl
@@ -602,9 +602,15 @@ parse_beam(S, File, HeaderSz, CheckOnly) ->
parse_source(S, File, Fd, StartLine, HeaderSz, CheckOnly) ->
{PreDefMacros, Module} = pre_def_macros(File),
IncludePath = [],
- {ok, _} = file:position(Fd, {bof, HeaderSz}),
+ %% Read the encoding on the second line, if there is any:
+ {ok, _} = file:position(Fd, 0),
+ _ = io:get_line(Fd, ''),
+ Encoding = epp:set_encoding(Fd),
+ {ok, _} = file:position(Fd, HeaderSz),
case epp:open(File, Fd, StartLine, IncludePath, PreDefMacros) of
{ok, Epp} ->
+ _ = [io:setopts(Fd, [{encoding,Encoding}]) ||
+ Encoding =/= none],
{ok, FileForm} = epp:parse_erl_form(Epp),
OptModRes = epp:parse_erl_form(Epp),
S2 = S#state{source = text, module = Module},
diff --git a/lib/stdlib/test/escript_SUITE.erl b/lib/stdlib/test/escript_SUITE.erl
index cf5fb12686..b6cdd0a9c7 100644
--- a/lib/stdlib/test/escript_SUITE.erl
+++ b/lib/stdlib/test/escript_SUITE.erl
@@ -919,6 +919,9 @@ unicode(Config) when is_list(Config) ->
" an arithmetic expression\n in operator '/'/2\n "
"called as <<\"\xaa\">> / <<\"\xaa\">>\nExitCode:127">>]),
run(Dir, "unicode3", [<<"ExitCode:0">>]),
+ run(Dir, "unicode4", [<<"ExitCode:0">>]),
+ run(Dir, "unicode5", [<<"ExitCode:0">>]),
+ run(Dir, "unicode6", [<<"ExitCode:0">>]),
ok.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
diff --git a/lib/stdlib/test/escript_SUITE_data/unicode3 b/lib/stdlib/test/escript_SUITE_data/unicode3
index 944487dcae..0702ecf309 100755
--- a/lib/stdlib/test/escript_SUITE_data/unicode3
+++ b/lib/stdlib/test/escript_SUITE_data/unicode3
@@ -1,5 +1,5 @@
#!/usr/bin/env escript
-%% -*- erlang; coding: utf-8 -*-
+%% -*- erlang; coding: latin-1 -*-
-export([main/1]).
diff --git a/lib/stdlib/test/escript_SUITE_data/unicode4 b/lib/stdlib/test/escript_SUITE_data/unicode4
new file mode 100755
index 0000000000..a7563a613a
--- /dev/null
+++ b/lib/stdlib/test/escript_SUITE_data/unicode4
@@ -0,0 +1,12 @@
+#!/usr/bin/env escript
+%% -*- erlang; encoding:utf-8 -*-
+
+-export([main/1]).
+
+main(_) ->
+ ok = io:setopts([{encoding,unicode}]),
+ Bin1 = <<"örn_Ѐ שלום-שלום+של 日本語">>,
+ L = [246,114,110,95,1024,32,1513,1500,1493,1501,45,1513,1500,1493,
+ 1501,43,1513,1500,32,26085,26412,35486],
+ L = unicode:characters_to_list(Bin1, utf8),
+ ok.
diff --git a/lib/stdlib/test/escript_SUITE_data/unicode5 b/lib/stdlib/test/escript_SUITE_data/unicode5
new file mode 100755
index 0000000000..e95da3361d
--- /dev/null
+++ b/lib/stdlib/test/escript_SUITE_data/unicode5
@@ -0,0 +1,12 @@
+#!/usr/bin/env escript
+%% -*- erlang -*-
+-export([main/1]).
+%% -*- encoding:latin-1 -*-
+
+main(_) ->
+ ok = io:setopts([{encoding,unicode}]),
+ Bin1 = <<"örn_Ѐ שלום-שלום+של 日本語">>,
+ L = [246,114,110,95,1024,32,1513,1500,1493,1501,45,1513,1500,1493,
+ 1501,43,1513,1500,32,26085,26412,35486],
+ L = unicode:characters_to_list(Bin1, utf8),
+ ok.
diff --git a/lib/stdlib/test/escript_SUITE_data/unicode6 b/lib/stdlib/test/escript_SUITE_data/unicode6
new file mode 100755
index 0000000000..8027a2a08c
--- /dev/null
+++ b/lib/stdlib/test/escript_SUITE_data/unicode6
@@ -0,0 +1,13 @@
+#!/usr/bin/env escript
+%% -*- erlang -*-
+%%! +pc unicode
+-export([main/1]).
+%% -*- encoding:utf-8 -*-
+
+main(_) ->
+ ok = io:setopts([{encoding,unicode}]),
+ Bin1 = <<"örn_Ѐ שלום-שלום+של 日本語">>,
+ L = [246,114,110,95,1024,32,1513,1500,1493,1501,45,1513,1500,1493,
+ 1501,43,1513,1500,32,26085,26412,35486],
+ L = unicode:characters_to_list(Bin1, utf8),
+ ok.