Skip to content

Commit

Permalink
preparing to 1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Sep 20, 2015
1 parent 4f1cfd9 commit 5b3fb0a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/sh.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, sh,
[{description, "VXZ SH Executor"},
{vsn, "0.9"},
[{description, "SH VXZ Executor"},
{vsn, "1.9"},
{registered, []},
{applications, [kernel,stdlib]},
{mod, { sh_app, []}},
Expand Down
29 changes: 7 additions & 22 deletions src/sh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@
-compile(export_all).

fdlink_executable() -> filename:absname(filename:join(code:priv_dir(sh), "fdlink")).
%fdlink_executable() -> filename:join(code:priv_dir(sh), "fdlink").
oneliner(C) -> run(C, ignoreeol, ".").
oneliner(C, Cwd) -> run(C, ignoreeol, Cwd).
run(C) -> run(C, binary, ".").
run(C, Log) -> run(C, Log, ".").
executable(C) ->
executable(C) ->
case filename:pathtype(C) of
absolute -> C;
relative -> case filename:split(C) of
[C] -> os:find_executable(C);
_ -> C % smth like deps/sh/priv/fdlink
end;
_ -> C end;
_ -> C
end.

run([C|Args], Log, Cwd) when is_list(C) ->
Executable = executable(C),
run(Executable, Args, Log, Cwd);

run([C|Args], Log, Cwd) when is_list(C) -> run(executable(C), Args, Log, Cwd);
run(Command, Log, Cwd) when is_list(Command) -> run("/bin/sh", ["-c", Command], Log, Cwd).

run(Command, Args, ignoreeol, Cwd) ->
Expand Down Expand Up @@ -49,26 +44,16 @@ run(Command, Args, _Log, Cwd, Env) ->
{args, Args}, {cd, Cwd}, {env, Env}]),
sh_loop(Port, binary).

%
% private functions
%

sh_loop(Port, binary) -> sh_loop(Port, fun(Chunk, Acc) -> [Chunk|Acc] end, []).
sh_loop(Port, Fun, Acc) when is_function(Fun) -> sh_loop(Port, Fun, Acc, fun erlang:iolist_to_binary/1).
sh_loop(Port, Fun, Acc, Flatten) when is_function(Fun) ->
receive
{Port, {data, {eol, Line}}} ->
sh_loop(Port, Fun, Fun({eol, Line}, Acc), Flatten);
{Port, {data, {noeol, Line}}} ->
sh_loop(Port, Fun, Fun({noeol, Line}, Acc), Flatten);
{Port, {data, Data}} ->
sh_loop(Port, Fun, Fun(Data, Acc), Flatten);
{Port, {exit_status, Status}} ->
{done, Status, Flatten(lists:reverse(Acc))}
{Port, {data, {eol, Line}}} -> sh_loop(Port, Fun, Fun({eol, Line}, Acc), Flatten);
{Port, {data, {noeol, Line}}} -> sh_loop(Port, Fun, Fun({noeol, Line}, Acc), Flatten);
{Port, {data, Data}} -> sh_loop(Port, Fun, Fun(Data, Acc), Flatten);
{Port, {exit_status, Status}} -> {done, Status, Flatten(lists:reverse(Acc))}
end.

%t() -> dbg:tracer(), dbg:p(self(), m), dbg:p(new, m), run("false", "/tmp/1", "/tmp").

ts() ->
Ts = {{_Y,_M,_D},{_H,_Min,_S}} = calendar:now_to_datetime(now()),
io_lib:format("~p", [Ts]).

0 comments on commit 5b3fb0a

Please sign in to comment.