public
Last active

  • Download Gist
save_shell.erl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
%% Copyright 2010 Erlang Solutions Ltd (ESL). All rights reserved.
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions are met:
%%
%% 1. Redistributions of source code must retain the above copyright notice,
%% this list of conditions and the following disclaimer.
%%
%% 2. Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer in
%% the documentation and/or other materials provided with the distribution.
%%
%% THIS SOFTWARE IS PROVIDED BY ESL ``AS IS'' AND ANY EXPRESS OR IMPLIED
%% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
%% IN NO EVENT SHALL ESL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
%% OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
%% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
%% THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
%%
%% The views and conclusions contained in the software and documentation are
%% those of the authors and should not be interpreted as representing
%% official policies, either expressed or implied, of ESL.
 
%% @doc
%% A collection of functions to manage saved shell histories
%% @end
 
-module(save_shell).
 
 
-export([shell/0,
h/0,
cmds/0,
w/1,
r/1]).
 
%% @spec () -> [Item]
%% Item = {{result,N}, any()} | {{command,N}, [Expr]} | {evaluator,pid()}
%%
%% @doc
%% Returns the shell history from the current shell process.
%% @end
%%
h() ->
shell_req(shell(), get_cmd).
 
%% @spec () -> {Seq::integer(), Exprs::list()}
%% @doc
%% Returns the shell history as a list of {CmdNo, Command}, where
%% Command is the pretty-printed command (sequence of expressions) entered
%% into the shell.
%% @end
%%
cmds() ->
CmdEs = [{N,Es} || {{command,N},Es} <- h()],
lists:keysort(1,[{N,lists:flatten(erl_pp:exprs(Es))} ||
{N,Es} <- CmdEs]).
 
 
%% @spec w(string()) -> ok | {error,Reason}
%% @doc
%% Writes the shell history to the output file F in a form that can be
%% processed by `file:script/1'.
%% @end
%%
w(F) ->
case cmds() of
[] ->
{error, empty_history};
[_|_] = Cmds ->
case file:open(F, [write]) of
{ok, Fd} ->
try [io:fwrite(Fd,"~s.~n", [C]) || {_,C} <- Cmds]
after
file:close(Fd)
end,
ok;
{error,_} = E ->
E
end
end.
 
%% @spec r(string()) -> any()
%% @doc
%% Reads a saved shell history and returns the value of the last
%% expression.
%% @end
r(F) ->
file:script(F).
 
 
%% @spec () -> pid()
%% @doc Returns the Pid of the shell process.
%% @end
%%
%% Modified version of shell:whereis_evaluator()
%%
shell() ->
%% locate top group leader, always registered as user
%% can be implemented by group (normally) or user
%% (if oldshell or noshell)
case whereis(user) of
undefined ->
undefined;
User ->
%% get user_drv pid from group, or shell pid from user
case group:interfaces(User) of
[] -> % old- or noshell
case user:interfaces(User) of
[] ->
undefined;
[{shell,Shell}] ->
Shell
end;
[{user_drv,UserDrv}] ->
%% get current group pid from user_drv
case user_drv:interfaces(UserDrv) of
[] ->
undefined;
[{current_group,Group}] ->
%% get shell pid from group
GrIfs = group:interfaces(Group),
case lists:keyfind(shell, 1, GrIfs) of
{shell, Shell} ->
Shell;
false ->
undefined
end
end
end
end.
 
 
shell_req(Shell, Req) ->
Shell ! {shell_req,self(),Req},
receive
{shell_rep,Shell,Rep} -> Rep
end.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.