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