Console: Implement 'cd -'
[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;
61             try {
62                 (void) cwd()(i->first);
63             }
64             catch (std::bad_cast &) {
65                 output << "/";
66             }
67             output << "\n";
68         }
69         break;
70
71     case ParseCommandInfo::BuiltinPUSHD :
72         dirstack_.push_back(cwd_);
73         if ( command.arguments()
74              && ! chdir(command.arguments().begin()[0]) )
75             output << "invalid directory\n";
76         break;
77
78     case ParseCommandInfo::BuiltinPOPD :
79         if (! dirstack_.empty()) {
80             cwd_ = dirstack_.back();
81             dirstack_.pop_back();
82         }
83         break;
84
85     case ParseCommandInfo::BuiltinEXIT :
86         throw ExitException();
87     }
88     return true;
89 }
90
91 prefix_ bool senf::console::Executor::chdir(ParseCommandInfo::argument_value_type const & path)
92 {
93     if (path.size() == 1 && path.begin()->value() == "-") {
94         if (oldCwd_.expired()) {
95             oldCwd_ = cwd_;
96             cwd_ = boost::static_pointer_cast<DirectoryNode>(
97                 root().shared_from_this());
98         } else
99             swap(cwd_, oldCwd_);
100     }
101     else {
102         try {
103             DirectoryNode::ptr dir (cwd_.lock());
104             ParseCommandInfo::token_iterator i (path.begin());
105             ParseCommandInfo::token_iterator const i_end (path.end());
106             if (i != i_end && i->value().empty()) {
107                 dir = boost::static_pointer_cast<DirectoryNode>(
108                     root().shared_from_this());
109                 ++ i;
110             }
111             for (; i != i_end; ++i) {
112                 if (i->value() == "..") {
113                     dir = dir->parent(); 
114                     if (! dir)
115                         dir = boost::static_pointer_cast<DirectoryNode>(
116                             root().shared_from_this());
117                 }
118                 else if (! i->value().empty() && i->value() != ".")
119                     dir = boost::static_pointer_cast<DirectoryNode>(
120                         (*dir)[i->value()].shared_from_this());
121             }
122             oldCwd_ = cwd_;
123             cwd_ = dir;
124         }
125         catch (std::bad_cast &) {
126             return false;
127         }
128         catch (UnknownNodeNameException &) {
129             return false;
130         }
131     }
132     return true;
133 }
134         
135 ///////////////////////////////cc.e////////////////////////////////////////
136 #undef prefix_
137 //#include "Executor.mpp"
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // comment-column: 40
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // End: