anchor.diff:              {direct,F} ->
                   %% Exec called and a Fun or MFA is defined to use.  The F returns the
                   %% value to return.
  -                exec_direct(ConnectionHandler, F, Cmd);
  +                %% The standard I/O is directed from/to the channel ChannelId.
  +                exec_direct(ConnectionHandler, ChannelId, Cmd, F, WantReply, S0);
   
               undefined when S0#state.shell == ?DEFAULT_SHELL ->
                   %% Exec called and the shell is the default shell (= Erlang shell).
                   %% To be exact, eval the term as an Erlang term (but not using the
                   %% ?DEFAULT_SHELL directly). This disables banner, prompts and such.
  -                exec_in_erlang_default_shell(Cmd);
  +                %% The standard I/O is directed from/to the channel ChannelId.
  +                exec_in_erlang_default_shell(ConnectionHandler, ChannelId, Cmd, WantReply, S0);
   
               undefined ->
                   %% Exec called, but the a shell other than the default shell is defined.
  
positive.code:     write_chars(ConnectionHandler, ChannelId, Chars),
      {ok, State#state{buf = NewBuf}};
  
  handle_msg({'EXIT', Group, Reason}, #state{group = Group,
  					    cm = ConnectionHandler,
  					    channel = ChannelId} = State) ->
      Status = case Reason of
                   normal -> 0;
                   _      -> -1
               end,
      ssh_connection:exit_status(ConnectionHandler, ChannelId, Status),
      ssh_connection:send_eof(ConnectionHandler, ChannelId),
      {stop, ChannelId, State};
  
  
negative.code:                 buf = empty_buf()}.
  
  %%--------------------------------------------------------------------
  exec_in_erlang_default_shell(ConnectionHandler, ChannelId, Cmd, WantReply, State) ->
      exec_in_self_group(ConnectionHandler, ChannelId, WantReply, State,
        fun() ->
                eval(parse(scan(Cmd)))
        end).
  
  
  scan(Cmd) ->
      erl_scan:string(Cmd). 
  
  parse({ok, Tokens, _}) ->
      erl_parse:parse_exprs(Tokens);
  parse({error, {_,erl_scan,Cause}, _}) ->
      {error, erl_scan:format_error(Cause)}.
  
  eval({ok, Expr_list}) ->
      {value, Value, _NewBindings} = erl_eval:exprs(Expr_list, erl_eval:new_bindings()),
      {ok, Value};
  eval({error, {_,erl_parse,Cause}}) ->
      {error, erl_parse:format_error(Cause)};
  eval({error,Error}) ->
      {error, Error}.
  
  %%--------------------------------------------------------------------
  exec_direct(ConnectionHandler, ChannelId, Cmd, ExecSpec, WantReply, State) ->
      Fun =
          fun() ->
                  if
                      is_function(ExecSpec, 1) ->
                          ExecSpec(Cmd);
  
                      is_function(ExecSpec, 2) ->
                          [{user,User}] = ssh_connection_handler:connection_info(ConnectionHandler, [user]),
                          ExecSpec(Cmd, User);
  
                      is_function(ExecSpec, 3) ->
                          ConnectionInfo =
                              ssh_connection_handler:connection_info(ConnectionHandler, [peer, user]),
                          User = proplists:get_value(user, ConnectionInfo),
                          {_, PeerAddr} = proplists:get_value(peer, ConnectionInfo),
                          ExecSpec(Cmd, User, PeerAddr);
  
                      true ->
                          {error, "Bad exec fun in server"}
                  end
          end,
      exec_in_self_group(ConnectionHandler, ChannelId, WantReply, State, Fun).
  
  %%--------------------------------------------------------------------
  %% Help for directing stdin and stdout from and to the channel from/to the client
  %%
  exec_in_self_group(ConnectionHandler, ChannelId, WantReply, State, Fun) ->
      Exec =
          fun() ->
                  spawn(
                    fun() ->
                            case try
                                     ssh_connection:reply_request(ConnectionHandler, WantReply, success, ChannelId),
                                     Fun()
                                 of
                                     {ok, Result} ->
                                         {ok, Result};
                                     {error, Error} ->
                                         {error, Error};
                                     X ->
                                         {error, "Bad exec fun in server. Invalid return value: "++t2str(X)}
                                 catch error:Err ->
                                         {error,Err};
                                       Cls:Exp ->
                                         {error,{Cls,Exp}}
                                 end
                            of
                                {ok,Str} ->
                                    write_chars(ConnectionHandler, ChannelId, t2str(Str)),
                                    ssh_connection:exit_status(ConnectionHandler, ChannelId, 0);
                                {error, Str} ->
                                    write_chars(ConnectionHandler, ChannelId, 1, "**Error** "++t2str(Str)),
                                    ssh_connection:exit_status(ConnectionHandler, ChannelId, -1)
                            end
                    end)
          end,
      {ok, State#state{group = group:start(self(), Exec, [{echo,false}]),
                       buf = empty_buf()}}.
      
  
  t2str(T) -> try io_lib:format("~s",[T])
              catch _:_ -> io_lib:format("~p",[T])
              end.
  
  %%--------------------------------------------------------------------
  % Pty can be undefined if the client never sets any pty options before
  