Make ssh_cli.erl handle <CTRL>+C

Stefan Zegenhagen <[email protected]>
Newsgroups gmane.comp.lang.erlang.patches
Organization arcutronix GmbH
Message-ID <1367843626.31752.66.camel@ax-sze>
Dear all,

I've found that ssh_cli.erl does not scan the input received from the
remote for occurrences of <CTRL>+C to signal the user's interrupt
requests to the group_leader of the CLI session. The patch attached to
this e-mail fixes the issue.


Kind regards,

-- 
Dr. Stefan Zegenhagen

arcutronix GmbH
Garbsener Landstr. 10
30419 Hannover
Germany

Tel:   +49 511 277-2734
Fax:   +49 511 277-2709
Email: [email protected]
Web:   www.arcutronix.com

*Synchronize the Ethernet*

General Managers: Dipl. Ing. Juergen Schroeder, Dr. Josef Gfrerer -
Legal Form: GmbH, Registered office: Hannover, HRB 202442, Amtsgericht
Hannover; Ust-Id: DE257551767.

Please consider the environment before printing this message.

_______________________________________________
erlang-patches mailing list
[email protected]
http://erlang.org/mailman/listinfo/erlang-patches
ssh_cli.patch (text/x-patch, 1.7 KB)
From f1d056ed1bf419677098cdc57bc7ce8a327e6b43 Mon Sep 17 00:00:00 2001
From: Stefan Zegenhagen <[email protected]>
Date: Mon, 6 May 2013 14:29:45 +0200
Subject: [PATCH] [SSH-CLI] properly handle <CTRL>+C as shell interrupt

In ssh_cli.erl, check for the presence of <CTRL>+C in data received from
the remote. If detected, use the established mechanism (send an
'interrupt' exit signal to the group_leader) to signal an interrupt to
the shell.
---
 lib/ssh/src/ssh_cli.erl |   19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/lib/ssh/src/ssh_cli.erl b/lib/ssh/src/ssh_cli.erl
index 69b1ab1..54911e7 100644
--- a/lib/ssh/src/ssh_cli.erl
+++ b/lib/ssh/src/ssh_cli.erl
@@ -68,7 +68,8 @@ init([Shell]) ->
 handle_ssh_msg({ssh_cm, _ConnectionManager, 
 		{data, _ChannelId, _Type, Data}}, 
 	       #state{group = Group} = State) ->
-    Group ! {self(), {data, binary_to_list(Data)}},
+    List = binary_to_list(Data),
+    to_group(List, Group),
     {ok, State};
 
 handle_ssh_msg({ssh_cm, ConnectionManager, 
@@ -188,6 +189,22 @@ terminate(_Reason, _State) ->
 %%% Internal functions
 %%--------------------------------------------------------------------
 
+to_group([], _Group) ->
+    ok;
+to_group([$\^C | Tail], Group) ->
+    exit(Group, interrupt),
+    to_group(Tail, Group);
+to_group(Data, Group) ->
+    Func = fun(C) -> C /= $\^C end,
+    Tail = case lists:splitwith(Func, Data) of
+        {[], Right} ->
+            Right;
+        {Left, Right} ->
+            Group ! {self(), {data, Left}},
+            Right
+    end,
+    to_group(Tail, Group).
+
 exec(Cmd) ->
     case eval(parse(scan(Cmd))) of
 	{error, _} ->
-- 
1.7.9.5
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.