Current section
Files
Jump to
Current section
Files
src/opener_ffi.erl
-module(opener_ffi).
-export([get_os/0, exec/2]).
get_os() ->
case os:type() of
{win32, _} ->
nt;
{unix, darwin} ->
darwin;
{unix, linux} ->
linux;
{_, _} ->
unknown
end.
%% This function has been vendored and modified from the original source available
%% at: https://github.com/lustre-labs/dev-tools/blob/91d89bfa8c6695846f03f700ec8b741ebaabfbe8/src/lustre_dev_tools_ffi.erl
%%
%% That work is licensed under the MIT license.
%% A full version that license text is made available below:
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
%% to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%% THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
%% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
%% TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%%
exec(Command, Args) ->
Command_ = binary_to_list(Command),
Args_ = lists:map(fun(Arg) -> binary_to_list(Arg) end, Args),
Name = {spawn_executable, os:find_executable(Command_)},
Port = open_port(
Name,
[
exit_status,
binary,
hide,
stream,
eof,
stderr_to_stdout,
{args, Args_}]),
do_exec(Port, []).
%% This function has been vendored and modified from the original source available
%% at: https://github.com/lustre-labs/dev-tools/blob/91d89bfa8c6695846f03f700ec8b741ebaabfbe8/src/lustre_dev_tools_ffi.erl
%%
%% That work is licensed under the MIT license.
%% A full version that license text is made available below:
%%
%% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
%% to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
%% and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%%
%% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%% THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%% MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
%% IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
%% TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%%
%% Helper function to handle the execution of the command and collect output.
%%
%% @param Port The port used for communication with the external process.
%% @param Acc An accumulator for collecting output data.
%%
%% @return A tuple indicating success or failure of the command execution.
do_exec(Port, Acc) ->
receive
{Port, {data, Data}} ->
do_exec(Port, [Data | Acc]);
{Port, {exit_status, 0}} ->
port_close(Port),
{ok, list_to_binary(lists:reverse(Acc))};
{Port, {exit_status, Code}} ->
port_close(Port),
{error, {Code, list_to_binary(lists:reverse(Acc))}}
end.