00372d74071e311742c976c4fb20bc88c8b4c64a
[senf.git] / Console / Readline.hh
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 Readline public header */
25
26 #ifndef HH_Readline_
27 #define HH_Readline_ 1
28
29 // Custom includes
30 #include <boost/scoped_ptr.hpp>
31 #include "Server.hh"
32 #include "../Utils/Exception.hh"
33 #include "../Scheduler/Scheduler.hh"
34 #include "../Scheduler/Binding.hh"
35
36 //#include "Readline.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41 namespace detail {
42
43 #ifndef DOXYGEN
44
45     /** \brief Internal: GNU readline based ClientReader implementation
46         
47         This implementation of the ClientReader interface uses GNU readline library to provide a
48         rich editing environment for console sessions. Since an application can only use readline
49         once, only one ReadlineReader can be allocated at any time.
50      */
51     class ReadlineClientReader
52         : public ClientReader
53     {
54     public:
55         ReadlineClientReader(Client & client);
56         ~ReadlineClientReader();
57
58         static bool active();
59         static ReadlineClientReader & instance();
60
61         struct DuplicateReaderException : public Exception
62         { DuplicateReaderException() : Exception("duplicate readline instantiation") {} };
63
64         int getc();
65         void callback(std::string line);
66         void write(std::string text);
67         void terminate();
68         void eof();
69         
70     private:
71         virtual void v_disablePrompt();
72         virtual void v_enablePrompt();
73         virtual void v_translate(std::string & data);
74
75         void charEvent(Scheduler::EventId event);
76
77         static ReadlineClientReader * instance_;
78         int ch_;
79         unsigned skipChars_;
80         char nameBuffer_[256];
81         char promptBuffer_[1024];
82         SchedulerBinding schedBinding_;
83         bool terminate_;
84
85         char * savedLineBuffer_;
86         int savedPoint_;
87         int savedEnd_;
88         int savedMark_;
89     };
90
91     /** \brief Internal: Safe GNU readline based ClientReader implementation
92         
93         This implementation of the ClientReader interface forwards all functionality to either
94         ReadlineClientReader or DumbClientReader. A RadlineClientReader is used, if none is active,
95         otherwise a DumbClientReader is allocated. Using this ClientReader implementation, the first
96         console client will have readline functionality enabled, all further will have it disabled.
97      */
98     class SafeReadlineClientReader
99         : public ClientReader
100     {
101     public:
102         SafeReadlineClientReader(Client & client);
103
104     private:
105         virtual void v_disablePrompt();
106         virtual void v_enablePrompt();
107         virtual void v_translate(std::string & data);
108
109         boost::scoped_ptr<ClientReader> reader_;
110     };
111
112 #endif
113
114 }}}
115
116 ///////////////////////////////hh.e////////////////////////////////////////
117 #include "Readline.cci"
118 //#include "Readline.ct"
119 //#include "Readline.cti"
120 #endif
121
122 \f
123 // Local Variables:
124 // mode: c++
125 // fill-column: 100
126 // comment-column: 40
127 // c-file-style: "senf"
128 // indent-tabs-mode: nil
129 // ispell-local-dictionary: "american"
130 // compile-command: "scons -u test"
131 // End: