Utils/Console: Replace Readline with LineEditor
[senf.git] / Utils / Console / LineEditor.cc
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief LineEditor non-inline non-template implementation */
25
26 #include "LineEditor.hh"
27 //#include "LineEditor.ih"
28
29 // Custom includes
30 #include "../Logger/SenfLog.hh"
31
32 //#include "LineEditor.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::LineEditorSwitcher
38
39 prefix_ senf::console::detail::LineEditorSwitcher::LineEditorSwitcher(Client & client)
40     : ClientReader(client),
41       reader_ (new LineEditorClientReader(client, *this))
42 {}
43
44 prefix_ void senf::console::detail::LineEditorSwitcher::editorSetupFailed()
45 {
46     // We need to delete the old reader *before* creating the new one such that all old scheduler
47     // events are removed before installing the new ones.
48     reader_.reset(0);
49     reader_.reset(new DumbClientReader(client()));
50 }
51
52 prefix_ void senf::console::detail::LineEditorSwitcher::v_disablePrompt()
53 {
54     reader_->disablePrompt();
55 }
56
57 prefix_ void senf::console::detail::LineEditorSwitcher::v_enablePrompt()
58 {
59     reader_->enablePrompt();
60 }
61
62 prefix_ void senf::console::detail::LineEditorSwitcher::v_write(std::string const & data)
63 {
64     reader_->write(data);
65 }
66
67 ///////////////////////////////////////////////////////////////////////////
68 // senf::console::detail::LineEditorClientReader
69
70 prefix_ senf::console::detail::LineEditorClientReader::
71 LineEditorClientReader(Client & client, LineEditorSwitcher & switcher)
72     : term::BaseTelnetProtocol(client.handle()), ClientReader(client),
73       editor_ (*this, senf::membind(&LineEditorClientReader::executeLine, this)),
74       switcher_ (&switcher)
75 {
76     editor_.prompt(promptString());
77     editor_.defineKey(senf::term::KeyParser::Ctrl('D'),
78                       senf::membind(&LineEditorClientReader::deleteCharOrExit, this));
79 }
80
81 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
82 {
83     // Commits suicide
84     switcher_->editorSetupFailed();
85 }
86
87 prefix_ void senf::console::detail::LineEditorClientReader::v_eof()
88 {
89     stopClient();
90 }
91
92 prefix_ void senf::console::detail::LineEditorClientReader::v_disablePrompt()
93 {
94     editor_.hide();
95 }
96
97 prefix_ void senf::console::detail::LineEditorClientReader::v_enablePrompt()
98 {
99     editor_.show();
100 }
101
102 prefix_ void senf::console::detail::LineEditorClientReader::v_write(std::string const & data)
103 {
104     BaseTelnetProtocol::write(data);
105 }
106
107 prefix_ void
108 senf::console::detail::LineEditorClientReader::executeLine(std::string const & text)
109 {
110     handleInput(text);
111     stream() << std::flush;
112     editor_.prompt(promptString());
113     editor_.show();
114 }
115
116 prefix_ void
117 senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor & editor)
118 {
119     if (editor.text().empty())
120         ClientReader::handle().facet<TCPSocketProtocol>().shutdown(TCPSocketProtocol::ShutRD);
121     else
122         term::bindings::deleteChar(editor);
123 }
124
125 ///////////////////////////////cc.e////////////////////////////////////////
126 #undef prefix_
127 //#include "LineEditor.mpp"
128
129 \f
130 // Local Variables:
131 // mode: c++
132 // fill-column: 100
133 // comment-column: 40
134 // c-file-style: "senf"
135 // indent-tabs-mode: nil
136 // ispell-local-dictionary: "american"
137 // compile-command: "scons -u test"
138 // End: