4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief LineEditor non-inline non-template implementation */
26 #include "LineEditor.hh"
27 //#include "LineEditor.ih"
30 #include <senf/Utils/Logger/SenfLog.hh>
31 #include <senf/Utils/Range.hh>
32 #include <senf/Utils/membind.hh>
34 //#include "LineEditor.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////////
39 // senf::console::detail::LineEditorSwitcher
41 prefix_ senf::console::detail::LineEditorSwitcher::LineEditorSwitcher(Client & client)
42 : ClientReader(client),
43 reader_ (new LineEditorClientReader(client, *this))
46 prefix_ void senf::console::detail::LineEditorSwitcher::editorSetupFailed()
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.
51 reader_.reset(new DumbClientReader(client()));
54 prefix_ void senf::console::detail::LineEditorSwitcher::v_disablePrompt()
56 reader_->disablePrompt();
59 prefix_ void senf::console::detail::LineEditorSwitcher::v_enablePrompt()
61 reader_->enablePrompt();
64 prefix_ void senf::console::detail::LineEditorSwitcher::v_write(std::string const & data)
69 prefix_ unsigned senf::console::detail::LineEditorSwitcher::v_width()
72 return reader_->width();
75 ///////////////////////////////////////////////////////////////////////////
76 // senf::console::detail::LineEditorClientReader
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)),
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,
90 senf::membind(&LineEditorClientReader::completePath, this)));
91 editor_.defineKey(senf::term::KeyParser::Return, &senf::term::bindings::acceptWithRepeat);
94 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
97 switcher_->editorSetupFailed();
100 prefix_ void senf::console::detail::LineEditorClientReader::v_eof()
105 prefix_ void senf::console::detail::LineEditorClientReader::v_disablePrompt()
110 prefix_ void senf::console::detail::LineEditorClientReader::v_enablePrompt()
115 prefix_ void senf::console::detail::LineEditorClientReader::v_write(std::string const & data)
117 BaseTelnetProtocol::write(data);
120 prefix_ unsigned senf::console::detail::LineEditorClientReader::v_width()
123 return editor_.width();
127 senf::console::detail::LineEditorClientReader::executeLine(std::string const & text)
130 stream() << std::flush;
131 editor_.prompt(promptString());
136 senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor & editor)
138 if (editor.text().empty())
139 ClientReader::handle().facet<TCPSocketProtocol>().shutdown(TCPSocketProtocol::ShutRD);
141 term::bindings::deleteChar(editor);
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)
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
155 if (! CommandParser::isWordChar(t[bb]) && t[bb] != '/') {
165 std::string base (t.substr(b,e));
166 CommandParser parser;
167 ParseCommandInfo cmd;
169 parser.parsePath(base, cmd);
171 catch (CommandParser::ParserErrorException & ex) {
175 ParseCommandInfo::TokensRange path (cmd.commandPath());
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() ? "/" : " "));
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();
193 else if (*i == WordToken("..")) {
194 DirectoryNode * parent (dir->parent().get());
195 if (parent) dir = parent;
198 else if (*i == WordToken("."))
201 if (dir->hasChild(i->value())) {
203 dir = & dir->getDirectory(i->value());
205 catch (std::bad_cast &) {
208 prefix += i->value() + "/";
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())
216 dir = static_cast<DirectoryNode*>(&node);
217 prefix += cs.begin()->first + "/";
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() ? "/" : " "));
229 ///////////////////////////////cc.e////////////////////////////////////////
231 //#include "LineEditor.mpp"
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"