1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2005-2017. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20 21%% 22 23%%% Description: user interaction for SSH 24 25-module(ssh_io). 26 27-export([yes_no/2, read_password/2, read_line/2, format/2]). 28-include("ssh.hrl"). 29 30read_line(Prompt, _Opts) -> 31 format("~s", [listify(Prompt)]), 32 unicode:characters_to_list(io:get_line("")). 33 34yes_no(Prompt, Opts) -> 35 format("~s [y/n]?", [Prompt]), 36 case trim(io:get_line("")) of 37 "y" -> yes; 38 "n" -> no; 39 "Y" -> yes; 40 "N" -> no; 41 _ -> 42 format("please answer y or n\n",[]), 43 yes_no(Prompt, Opts) 44 end. 45 46read_password(Prompt, Opts) -> 47 format("~s", [listify(Prompt)]), 48 case trim(io:get_password()) of 49 "" -> 50 read_password(Prompt, Opts); 51 Pwd -> 52 Pwd 53 end. 54 55format(Fmt, Args) -> 56 io:format(Fmt, Args). 57 58%%%================================================================ 59listify(A) when is_atom(A) -> atom_to_list(A); 60listify(L) when is_list(L) -> L; 61listify(B) when is_binary(B) -> binary_to_list(B). 62 63 64trim(Line) when is_list(Line) -> 65 lists:reverse(trim1(lists:reverse(trim1(Line)))); 66trim(Line) when is_binary(Line) -> 67 trim(unicode:characters_to_list(Line)); 68trim(Other) -> Other. 69 70trim1([$\s|Cs]) -> trim(Cs); 71trim1([$\r|Cs]) -> trim(Cs); 72trim1([$\n|Cs]) -> trim(Cs); 73trim1([$\t|Cs]) -> trim(Cs); 74trim1(Cs) -> Cs. 75