switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / LineEditor.cc
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief LineEditor non-inline non-template implementation */
30
31 #include "LineEditor.hh"
32 //#include "LineEditor.ih"
33
34 // Custom includes
35 #include <senf/Utils/Logger/SenfLog.hh>
36 #include <senf/Utils/Range.hh>
37 #include <senf/Utils/membind.hh>
38
39 //#include "LineEditor.mpp"
40 #define prefix_
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 // senf::console::detail::LineEditorSwitcher
45
46 prefix_ senf::console::detail::LineEditorSwitcher::LineEditorSwitcher(Client & client)
47     : ClientReader(client),
48       reader_ (new LineEditorClientReader(client, *this))
49 {}
50
51 prefix_ void senf::console::detail::LineEditorSwitcher::editorSetupFailed()
52 {
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.
55     reader_.reset(0);
56     reader_.reset(new DumbClientReader(client()));
57 }
58
59 prefix_ void senf::console::detail::LineEditorSwitcher::v_disablePrompt()
60 {
61     reader_->disablePrompt();
62 }
63
64 prefix_ void senf::console::detail::LineEditorSwitcher::v_enablePrompt()
65 {
66     reader_->enablePrompt();
67 }
68
69 prefix_ void senf::console::detail::LineEditorSwitcher::v_write(std::string const & data)
70 {
71     reader_->write(data);
72 }
73
74 prefix_ unsigned senf::console::detail::LineEditorSwitcher::v_width()
75     const
76 {
77     return reader_->width();
78 }
79
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::console::detail::LineEditorClientReader
82
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)),
87       switcher_ (&switcher)
88 {
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,
94                                   _1,
95                                   senf::membind(&LineEditorClientReader::completePath, this)));
96     editor_.defineKey(senf::term::KeyParser::Return, &senf::term::bindings::acceptWithRepeat);
97 }
98
99 prefix_ void senf::console::detail::LineEditorClientReader::v_setupFailed()
100 {
101     // Commits suicide
102     switcher_->editorSetupFailed();
103 }
104
105 prefix_ void senf::console::detail::LineEditorClientReader::v_eof()
106 {
107     stopClient();
108 }
109
110 prefix_ void senf::console::detail::LineEditorClientReader::v_disablePrompt()
111 {
112     editor_.hide();
113 }
114
115 prefix_ void senf::console::detail::LineEditorClientReader::v_enablePrompt()
116 {
117     editor_.show();
118 }
119
120 prefix_ void senf::console::detail::LineEditorClientReader::v_write(std::string const & data)
121 {
122     BaseTelnetProtocol::write(data);
123 }
124
125 prefix_ unsigned senf::console::detail::LineEditorClientReader::v_width()
126     const
127 {
128     return editor_.width();
129 }
130
131 prefix_ void
132 senf::console::detail::LineEditorClientReader::executeLine(std::string const & text)
133 {
134     handleInput(text);
135     stream() << std::flush;
136     editor_.prompt(promptString());
137     editor_.show();
138 }
139
140 prefix_ void
141 senf::console::detail::LineEditorClientReader::deleteCharOrExit(term::LineEditor & editor)
142 {
143     if (editor.text().empty())
144         ClientReader::handle().facet<TCPSocketProtocol>().shutdown(TCPSocketProtocol::ShutRD);
145     else
146         term::bindings::deleteChar(editor);
147 }
148
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)
152 {
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
156     // it's ok.
157     if (b<e) {
158         unsigned bb (e-1);
159         for (;;) {
160             if (! CommandParser::isWordChar(t[bb]) && t[bb] != '/') {
161                 ++bb;
162                 break;
163             }
164             if (bb == b)
165                 break;
166             --bb;
167         }
168         b = bb;
169     }
170     std::string base (t.substr(b,e));
171     CommandParser parser;
172     ParseCommandInfo cmd;
173     try {
174         parser.parsePath(base, cmd);
175     }
176     catch (CommandParser::ParserErrorException & ex) {
177         return;
178     }
179
180     ParseCommandInfo::TokensRange path (cmd.commandPath());
181     if (path.empty()) {
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() ? "/" : " "));
185         return;
186     }
187
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();
195                 prefix = "/";
196             }
197         }
198         else if (*i == WordToken("..")) {
199             DirectoryNode * parent (dir->parent().get());
200             if (parent) dir = parent;
201             prefix += "../";
202         }
203         else if (*i == WordToken("."))
204             prefix += "./";
205         else {
206             if (dir->hasChild(i->value())) {
207                 try {
208                     dir = & dir->getDirectory(i->value());
209                 }
210                 catch (std::bad_cast &) {
211                     return;
212                 }
213                 prefix += i->value() + "/";
214             }
215             else {
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())
220                         return;
221                     dir = static_cast<DirectoryNode*>(&node);
222                     prefix += cs.begin()->first + "/";
223                 }
224                 else
225                     return;
226             }
227         }
228
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() ? "/" : " "));
232 }
233
234 //-/////////////////////////////////////////////////////////////////////////////////////////////////
235 #undef prefix_
236 //#include "LineEditor.mpp"
237
238 \f
239 // Local Variables:
240 // mode: c++
241 // fill-column: 100
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"
247 // End: