db2313c4b3d96a8af01ecc5a2063c82598a12cb2
[senf.git] / senf / 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 <senf/Utils/Logger/SenfLog.hh>
31 #include <senf/Utils/Range.hh>
32 #include <senf/Utils/membind.hh>
33
34 //#include "LineEditor.mpp"
35 #define prefix_
36 ///////////////////////////////cc.p////////////////////////////////////////
37
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::console::detail::LineEditorSwitcher
40
41 prefix_ senf::console::detail::LineEditorSwitcher::LineEditorSwitcher(Client & client)
42     : ClientReader(client),
43       reader_ (new LineEditorClientReader(client, *this))
44 {}
45
46 prefix_ void senf::console::detail::LineEditorSwitcher::editorSetupFailed()
47 {
48     // We need to delete the old reader *before* creating the new one such that all old scheduler
49     // events are removed before installing the new ones.
50     reader_.reset(0);
51     reader_.reset(new DumbClientReader(client()));
52 }
53
54 prefix_ void senf::console::detail::LineEditorSwitcher::v_disablePrompt()
55 {
56     reader_->disablePrompt();
57 }
58
59 prefix_ void senf::console::detail::LineEditorSwitcher::v_enablePrompt()
60 {
61     reader_->enablePrompt();
62 }
63
64 prefix_ void senf::console::detail::LineEditorSwitcher::v_write(std::string const & data)
65 {
66     reader_->write(data);
67 }
68
69 prefix_ unsigned senf::console::detail::LineEditorSwitcher::v_width()
70     const
71 {
72     return reader_->width();
73 }
74
75 ///////////////////////////////////////////////////////////////////////////
76 // senf::console::detail::LineEditorClientReader
77
78 prefix_ senf::console::detail::LineEditorClientReader::
79 LineEditorClientReader(Client & client, LineEditorSwitcher & switcher)
80     : term::BaseTelnetProtocol(client.handle()), ClientReader(client),
81       editor_ (*this, senf::membind(&LineEditorClientReader::executeLine, this)),
82       switcher_ (&switcher)
83 {
84     editor_.prompt(promptString());
85     editor_.defineKey(senf::term::KeyParser::Ctrl('D'),
86                       senf::membind(&LineEditorClientReader::deleteCharOrExit, this));
87     editor_.defineKey(senf::term::KeyParser::Tab,
88                       boost::bind(&term::bindings::complete,
89                                   _1,
90                                   senf::membind(&LineEditorClientReader::completePath, this)));
91     editor_.defineKey(senf::term::KeyParser::Return, &senf::term::bindings::acceptWithRepeat);
92 }
93
94 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
95 {
96     // Commits suicide
97     switcher_->editorSetupFailed();
98 }
99
100 prefix_ void senf::console::detail::LineEditorClientReader::v_eof()
101 {
102     stopClient();
103 }
104
105 prefix_ void senf::console::detail::LineEditorClientReader::v_disablePrompt()
106 {
107     editor_.hide();
108 }
109
110 prefix_ void senf::console::detail::LineEditorClientReader::v_enablePrompt()
111 {
112     editor_.show();
113 }
114
115 prefix_ void senf::console::detail::LineEditorClientReader::v_write(std::string const & data)
116 {
117     BaseTelnetProtocol::write(data);
118 }
119
120 prefix_ unsigned senf::console::detail::LineEditorClientReader::v_width()
121     const
122 {
123     return editor_.width();
124 }
125
126 prefix_ void
127 senf::console::detail::LineEditorClientReader::executeLine(std::string const & text)
128 {
129     handleInput(text);
130     stream() << std::flush;
131     editor_.prompt(promptString());
132     editor_.show();
133 }
134
135 prefix_ void
136 senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor & editor)
137 {
138     if (editor.text().empty())
139         ClientReader::handle().facet<TCPSocketProtocol>().shutdown(TCPSocketProtocol::ShutRD);
140     else
141         term::bindings::deleteChar(editor);
142 }
143
144 prefix_ void senf::console::detail::LineEditorClientReader::
145 completePath(term::LineEditor & editor, unsigned & b, unsigned & e, std::string & prefix,
146              std::vector<std::string> & completions)
147 {
148     std::string const & t (editor.text());
149     // Search backward from e finding the longest valid path. This does *not* accept all valid
150     // path's, only those without embedded white-space. However, this is only for completion so
151     // it's ok.
152     if (b<e) {
153         unsigned bb (e-1);
154         for (;;) {
155             if (! CommandParser::isWordChar(t[bb]) && t[bb] != '/') {
156                 ++bb;
157                 break;
158             }
159             if (bb == b)
160                 break;
161             --bb;
162         }
163         b = bb;
164     }
165     std::string base (t.substr(b,e));
166     CommandParser parser;
167     ParseCommandInfo cmd;
168     try {
169         parser.parsePath(base, cmd);
170     }
171     catch (CommandParser::ParserErrorException & ex) {
172         return;
173     }
174
175     ParseCommandInfo::TokensRange path (cmd.commandPath());
176     if (path.empty()) {
177         DirectoryNode::ChildrenRange cs (client().cwd().children());
178         for (DirectoryNode::ChildrenRange::iterator i (cs.begin()); i != cs.end(); ++i)
179             completions.push_back(i->first + (i->second->followLink().isDirectory() ? "/" : " "));
180         return;
181     }
182
183     ParseCommandInfo::TokensRange::const_iterator i (path.begin());
184     ParseCommandInfo::TokensRange::const_iterator const i_end (boost::prior(path.end()));
185     DirectoryNode * dir (& client().cwd());
186     for (; i != i_end; ++i)
187         if (*i == NoneToken()) {
188             if (i == path.begin()) {
189                 dir = & client().root();
190                 prefix = "/";
191             }
192         }
193         else if (*i == WordToken("..")) {
194             DirectoryNode * parent (dir->parent().get());
195             if (parent) dir = parent;
196             prefix += "../";
197         }
198         else if (*i == WordToken("."))
199             prefix += "./";
200         else {
201             if (dir->hasChild(i->value())) {
202                 try {
203                     dir = & dir->getDirectory(i->value());
204                 }
205                 catch (std::bad_cast &) {
206                     return;
207                 }
208                 prefix += i->value() + "/";
209             }
210             else {
211                 DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
212                 if (has_one_elt(cs)) {
213                     GenericNode & node (cs.begin()->second->followLink());
214                     if (!node.isDirectory())
215                         return;
216                     dir = static_cast<DirectoryNode*>(&node);
217                     prefix += cs.begin()->first + "/";
218                 }
219                 else
220                     return;
221             }
222         }
223
224     DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
225     for (DirectoryNode::ChildrenRange::iterator j (cs.begin()); j != cs.end(); ++j)
226         completions.push_back(j->first + (j->second->followLink().isDirectory() ? "/" : " "));
227 }
228
229 ///////////////////////////////cc.e////////////////////////////////////////
230 #undef prefix_
231 //#include "LineEditor.mpp"
232
233 \f
234 // Local Variables:
235 // mode: c++
236 // fill-column: 100
237 // comment-column: 40
238 // c-file-style: "senf"
239 // indent-tabs-mode: nil
240 // ispell-local-dictionary: "american"
241 // compile-command: "scons -u test"
242 // End: