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 "../Logger/SenfLog.hh"
31 #include "../../Utils/range.hh"
33 //#include "LineEditor.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::console::detail::LineEditorSwitcher
40 prefix_ senf::console::detail::LineEditorSwitcher::LineEditorSwitcher(Client & client)
41 : ClientReader(client),
42 reader_ (new LineEditorClientReader(client, *this))
45 prefix_ void senf::console::detail::LineEditorSwitcher::editorSetupFailed()
47 // We need to delete the old reader *before* creating the new one such that all old scheduler
48 // events are removed before installing the new ones.
50 reader_.reset(new DumbClientReader(client()));
53 prefix_ void senf::console::detail::LineEditorSwitcher::v_disablePrompt()
55 reader_->disablePrompt();
58 prefix_ void senf::console::detail::LineEditorSwitcher::v_enablePrompt()
60 reader_->enablePrompt();
63 prefix_ void senf::console::detail::LineEditorSwitcher::v_write(std::string const & data)
68 prefix_ unsigned senf::console::detail::LineEditorSwitcher::v_width()
71 return reader_->width();
74 ///////////////////////////////////////////////////////////////////////////
75 // senf::console::detail::LineEditorClientReader
77 prefix_ senf::console::detail::LineEditorClientReader::
78 LineEditorClientReader(Client & client, LineEditorSwitcher & switcher)
79 : term::BaseTelnetProtocol(client.handle()), ClientReader(client),
80 editor_ (*this, senf::membind(&LineEditorClientReader::executeLine, this)),
83 editor_.prompt(promptString());
84 editor_.defineKey(senf::term::KeyParser::Ctrl('D'),
85 senf::membind(&LineEditorClientReader::deleteCharOrExit, this));
86 editor_.defineKey(senf::term::KeyParser::Tab,
87 boost::bind(&term::bindings::complete,
89 senf::membind(&LineEditorClientReader::completePath, this)));
92 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
95 switcher_->editorSetupFailed();
98 prefix_ void senf::console::detail::LineEditorClientReader::v_eof()
103 prefix_ void senf::console::detail::LineEditorClientReader::v_disablePrompt()
108 prefix_ void senf::console::detail::LineEditorClientReader::v_enablePrompt()
113 prefix_ void senf::console::detail::LineEditorClientReader::v_write(std::string const & data)
115 BaseTelnetProtocol::write(data);
118 prefix_ unsigned senf::console::detail::LineEditorClientReader::v_width()
121 return editor_.width();
125 senf::console::detail::LineEditorClientReader::executeLine(std::string const & text)
128 stream() << std::flush;
129 editor_.prompt(promptString());
134 senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor & editor)
136 if (editor.text().empty())
137 ClientReader::handle().facet<TCPSocketProtocol>().shutdown(TCPSocketProtocol::ShutRD);
139 term::bindings::deleteChar(editor);
142 prefix_ void senf::console::detail::LineEditorClientReader::
143 completePath(term::LineEditor & editor, unsigned b, unsigned e,
144 std::vector<std::string> & completions)
146 std::string base (editor.text().substr(b,e));
147 CommandParser parser;
148 ParseCommandInfo cmd;
150 parser.parsePath(base, cmd);
152 catch (CommandParser::ParserErrorException & ex) {
156 ParseCommandInfo::TokensRange path (cmd.commandPath());
158 DirectoryNode::ChildrenRange cs (client().cwd().children());
159 for (DirectoryNode::ChildrenRange::iterator i (cs.begin()); i != cs.end(); ++i)
160 completions.push_back(i->first + (i->second->followLink().isDirectory() ? "/" : " "));
164 ParseCommandInfo::TokensRange::const_iterator i (path.begin());
165 ParseCommandInfo::TokensRange::const_iterator const i_end (boost::prior(path.end()));
166 DirectoryNode * dir (& client().cwd());
167 std::string basePath;
168 for (; i != i_end; ++i)
169 if (*i == NoneToken()) {
170 if (i == path.begin()) {
171 dir = & client().root();
175 else if (*i == WordToken("..")) {
176 DirectoryNode * parent (dir->parent().get());
177 if (parent) dir = parent;
180 else if (*i == WordToken("."))
183 if (dir->hasChild(i->value())) {
185 dir = & dir->getDirectory(i->value());
187 catch (std::bad_cast &) {
190 basePath += i->value() + "/";
193 DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
194 if (has_one_elt(cs)) {
195 GenericNode & node (cs.begin()->second->followLink());
196 if (!node.isDirectory())
198 dir = static_cast<DirectoryNode*>(&node);
199 basePath += cs.begin()->first + "/";
206 DirectoryNode::ChildrenRange cs (dir->completions(i->value()));
207 for (DirectoryNode::ChildrenRange::iterator j (cs.begin()); j != cs.end(); ++j)
208 completions.push_back(basePath + j->first
209 + (j->second->followLink().isDirectory() ? "/" : " "));
212 ///////////////////////////////cc.e////////////////////////////////////////
214 //#include "LineEditor.mpp"
220 // comment-column: 40
221 // c-file-style: "senf"
222 // indent-tabs-mode: nil
223 // ispell-local-dictionary: "american"
224 // compile-command: "scons -u test"