diff --git a/src/sh.app.src b/src/sh.app.src index a327f89..6ac1876 100644 --- a/src/sh.app.src +++ b/src/sh.app.src @@ -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, []}}, diff --git a/src/sh.erl b/src/sh.erl index 54739c4..6e56d5c 100644 --- a/src/sh.erl +++ b/src/sh.erl @@ -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) -> @@ -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]).