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