4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief LineEditor non-inline non-template implementation */
31 #include "LineEditor.hh"
32 //#include "LineEditor.ih"
35 #include <senf/Utils/Logger/SenfLog.hh>
36 #include <senf/Utils/Range.hh>
37 #include <senf/Utils/membind.hh>
39 //#include "LineEditor.mpp"
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::console::detail::LineEditorSwitcher
46 prefix_ senf::console::detail::LineEditorSwitcher::LineEditorSwitcher(Client & client)
47 : ClientReader(client),
48 reader_ (new LineEditorClientReader(client, *this))
51 prefix_ void senf::console::detail::LineEditorSwitcher::editorSetupFailed()
53 // We need to delete the old reader *before* creating the new one such that all old scheduler
54 // events are removed before installing the new ones.
56 reader_.reset(new DumbClientReader(client()));
59 prefix_ void senf::console::detail::LineEditorSwitcher::v_disablePrompt()
61 reader_->disablePrompt();
64 prefix_ void senf::console::detail::LineEditorSwitcher::v_enablePrompt()
66 reader_->enablePrompt();
69 prefix_ void senf::console::detail::LineEditorSwitcher::v_write(std::string const & data)
74 prefix_ unsigned senf::console::detail::LineEditorSwitcher::v_width()
77 return reader_->width();
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::console::detail::LineEditorClientReader
83 prefix_ senf::console::detail::LineEditorClientReader::
84 LineEditorClientReader(Client & client, LineEditorSwitcher & switcher)
85 : term::BaseTelnetProtocol(client.handle()), ClientReader(client),
86 editor_ (*this, senf::membind(&LineEditorClientReader::executeLine, this)),
89 editor_.prompt(promptString());
90 editor_.defineKey(senf::term::KeyParser::Ctrl('D'),
91 senf::membind(&LineEditorClientReader::deleteCharOrExit, this));
92 editor_.defineKey(senf::term::KeyParser::Tab,
93 boost::bind(&term::bindings::complete,
95 senf::membind(&LineEditorClientReader::completePath, this)));
96 editor_.defineKey(senf::term::KeyParser::Return, &senf::term::bindings::acceptWithRepeat);
99 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
102 switcher_->editorSetupFailed();
105 prefix_ void senf::console::detail::LineEditorClientReader::v_eof()
110 prefix_ void senf::console::detail::LineEditorClientReader::v_disablePrompt()
115 prefix_ void senf::console::detail::LineEditorClientReader::v_enablePrompt()
120 prefix_ void senf::console::detail::LineEditorClientReader::v_write(std::string const & data)
122 BaseTelnetProtocol::write(data);
125 prefix_ unsigned senf::console::detail::LineEditorClientReader::v_width()
128 return editor_.width();
132 senf::console::detail::LineEditorClientReader::executeLine(std::string const & text)
135 stream() << std::flush;
136 editor_.prompt(promptString());
141 senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor & editor)
143 if (editor.text().empty())
144 ClientReader::handle().facet<TCPSocketProtocol>().shutdown(TCPSocketProtocol::ShutRD);
146 term::bindings::deleteChar(editor);
149 prefix_ void senf::console::detail::LineEditorClientReader::
150 completePath(term::LineEditor & editor, unsigned & b, unsigned & e, std::string & prefix,
151 std::vector<std::string> & completions)
153 std::string const & t (editor.text());
154 // Search backward from e finding the longest valid path. This does *not* accept all valid
155 // path's, only those without embedded white-space. However, this is only for completion so
160 if (! CommandParser::isWordChar(t[bb]) && t[bb] != '/') {
170 std::string base (t.substr(b,e));
171 CommandParser parser;
172 ParseCommandInfo cmd;
174 parser.parsePath(base, cmd);
176 catch (CommandParser::ParserErrorException & ex) {
180 ParseCommandInfo::TokensRange path (cmd.commandPath());
182 DirectoryNode::ChildrenRange cs (client().cwd().children());
183 for (DirectoryNode::ChildrenRange::iterator i (cs.begin()); i != cs.end(); ++i)
184 completions.push_back(i->first + (i->second->followLink().isDirectory() ? "/" : " "));
188 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
189 ParseCommandInfo::TokensRange::const_iterator const i_end (boost::prior(path.end()));
190 DirectoryNode * dir (& client().cwd());
191 for (; i != i_end; ++i)
192 if (*i == NoneToken()) {
193 if (i == path.begin()) {
194 dir = & client().root();
198 else if (*i == WordToken("..")) {
199 DirectoryNode * parent (dir->parent().get());
200 if (parent) dir = parent;
203 else if (*i == WordToken("."))
206 if (dir->hasChild(i->value())) {
208 dir = & dir->getDirectory(i->value());
210 catch (std::bad_cast &) {
213 prefix += i->value() + "/";
216 DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
217 if (has_one_elt(cs)) {
218 GenericNode & node (cs.begin()->second->followLink());
219 if (!node.isDirectory())
221 dir = static_cast<DirectoryNode*>(&node);
222 prefix += cs.begin()->first + "/";
229 DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
230 for (DirectoryNode::ChildrenRange::iterator j (cs.begin()); j != cs.end(); ++j)
231 completions.push_back(j->first + (j->second->followLink().isDirectory() ? "/" : " "));
234 //-/////////////////////////////////////////////////////////////////////////////////////////////////
236 //#include "LineEditor.mpp"
242 // comment-column: 40
243 // c-file-style: "senf"
244 // indent-tabs-mode: nil
245 // ispell-local-dictionary: "american"
246 // compile-command: "scons -u test"