diff options
author | Dan Gudmundsson <[email protected]> | 2013-05-15 15:16:50 +0200 |
---|---|---|
committer | Dan Gudmundsson <[email protected]> | 2013-06-03 12:58:45 +0200 |
commit | 0057676c3ebfd603658631088c977219ba2b0cc6 (patch) | |
tree | 879f3125ed8b569e25eefc02c3b5c5a201d824a3 /lib/kernel/src/os.erl | |
parent | 892fb14882ef356c90eda8aa02ca43c3db8b53da (diff) | |
download | otp-0057676c3ebfd603658631088c977219ba2b0cc6.tar.gz otp-0057676c3ebfd603658631088c977219ba2b0cc6.tar.bz2 otp-0057676c3ebfd603658631088c977219ba2b0cc6.zip |
kernel: Handle unicode in os:cmd
Allow unicode strings in os:cmd and try to convert the
result bytes to unicode list.
Also test it.
Diffstat (limited to 'lib/kernel/src/os.erl')
-rw-r--r-- | lib/kernel/src/os.erl | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/kernel/src/os.erl b/lib/kernel/src/os.erl index 86f2f94f1f..c92bb463c1 100644 --- a/lib/kernel/src/os.erl +++ b/lib/kernel/src/os.erl @@ -183,20 +183,25 @@ extensions() -> Command :: atom() | io_lib:chars(). cmd(Cmd) -> validate(Cmd), - case type() of - {unix, _} -> - unix_cmd(Cmd); - {win32, Wtype} -> - Command0 = case {os:getenv("COMSPEC"),Wtype} of - {false,windows} -> lists:concat(["command.com /c", Cmd]); - {false,_} -> lists:concat(["cmd /c", Cmd]); - {Cspec,_} -> lists:concat([Cspec," /c",Cmd]) - end, - %% open_port/2 awaits string() in Command, but io_lib:chars() can be - %% deep lists according to io_lib module description. - Command = lists:flatten(Command0), - Port = open_port({spawn, Command}, [stream, in, eof, hide]), - get_data(Port, []) + Bytes = case type() of + {unix, _} -> + unix_cmd(Cmd); + {win32, Wtype} -> + Command0 = case {os:getenv("COMSPEC"),Wtype} of + {false,windows} -> lists:concat(["command.com /c", Cmd]); + {false,_} -> lists:concat(["cmd /c", Cmd]); + {Cspec,_} -> lists:concat([Cspec," /c",Cmd]) + end, + %% open_port/2 awaits string() in Command, but io_lib:chars() can be + %% deep lists according to io_lib module description. + Command = lists:flatten(Command0), + Port = open_port({spawn, Command}, [stream, in, eof, hide]), + get_data(Port, []) + end, + String = unicode:characters_to_list(list_to_binary(Bytes)), + if %% Convert to unicode list if possible otherwise return bytes + is_list(String) -> String; + true -> Bytes end. unix_cmd(Cmd) -> @@ -337,7 +342,7 @@ mk_cmd(Cmd) when is_atom(Cmd) -> % backward comp. mk_cmd(Cmd) -> %% We insert a new line after the command, in case the command %% contains a comment character. - io_lib:format("(~ts\n) </dev/null; echo \"\^D\"\n", [Cmd]). + [$(, unicode:characters_to_binary(Cmd), "\n) </dev/null; echo \"\^D\"\n"]. validate(Atom) when is_atom(Atom) -> @@ -345,7 +350,7 @@ validate(Atom) when is_atom(Atom) -> validate(List) when is_list(List) -> validate1(List). -validate1([C|Rest]) when is_integer(C), 0 =< C, C < 256 -> +validate1([C|Rest]) when is_integer(C) -> validate1(Rest); validate1([List|Rest]) when is_list(List) -> validate1(List), |