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