Console: Implement current directory management and builtins (cd, dirstack, ls)
[senf.git] / Console / Executor.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
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 Executor non-inline non-template implementation */
25
26 #include "Executor.hh"
27 //#include "Executor.ih"
28
29 // Custom includes
30
31 //#include "Executor.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::console::Executor
37
38 prefix_ bool senf::console::Executor::operator()(ParseCommandInfo const & command,
39                                                  std::ostream & output)
40 {
41     SENF_LOG(( "Executing: " << command ));
42
43     if (cwd_.expired())
44         cwd_ = boost::static_pointer_cast<DirectoryNode>(
45             root().shared_from_this());
46
47     switch(command.builtin()) {
48     case ParseCommandInfo::NoBuiltin :
49         break;
50
51     case ParseCommandInfo::BuiltinCD :
52         if ( command.arguments() &&
53              ! chdir(command.arguments().begin()[0]) )
54             output << "invalid directory\n";
55         break;
56
57     case ParseCommandInfo::BuiltinLS :
58         for (DirectoryNode::child_iterator i (cwd().children().begin());
59              i != cwd().children().end(); ++i)
60             output << i->first << "\n";
61         break;
62
63     case ParseCommandInfo::BuiltinPUSHD :
64         dirstack_.push_back(cwd_);
65         if ( command.arguments()
66              && ! chdir(command.arguments().begin()[0]) )
67             output << "invalid directory\n";
68         break;
69
70     case ParseCommandInfo::BuiltinPOPD :
71         if (! dirstack_.empty()) {
72             cwd_ = dirstack_.back();
73             dirstack_.pop_back();
74         }
75         break;
76
77     case ParseCommandInfo::BuiltinEXIT :
78         throw ExitException();
79     }
80     return true;
81 }
82
83 prefix_ bool senf::console::Executor::chdir(ParseCommandInfo::argument_value_type const & path)
84 {
85     try {
86         DirectoryNode::ptr dir (cwd_.lock());
87         ParseCommandInfo::token_iterator i (path.begin());
88         ParseCommandInfo::token_iterator const i_end (path.end());
89         if (i != i_end && i->value().empty()) {
90             dir = boost::static_pointer_cast<DirectoryNode>(
91                 root().shared_from_this());
92             ++ i;
93         }
94         for (; i != i_end; ++i) {
95             if (i->value() == "..") {
96                 dir = dir->parent(); 
97                 if (! dir)
98                     dir = boost::static_pointer_cast<DirectoryNode>(
99                         root().shared_from_this());
100             }
101             else if (! i->value().empty() && i->value() != ".")
102                 dir = boost::static_pointer_cast<DirectoryNode>(
103                     (*dir)[i->value()].shared_from_this());
104         }
105         cwd_ = dir;
106     }
107     catch (std::bad_cast &) {
108         return false;
109     }
110     catch (UnknownNodeNameException &) {
111         return false;
112     }
113     return true;
114 }
115         
116 ///////////////////////////////cc.e////////////////////////////////////////
117 #undef prefix_
118 //#include "Executor.mpp"
119
120 \f
121 // Local Variables:
122 // mode: c++
123 // fill-column: 100
124 // comment-column: 40
125 // c-file-style: "senf"
126 // indent-tabs-mode: nil
127 // ispell-local-dictionary: "american"
128 // compile-command: "scons -u test"
129 // End: