Console: Add console routing to testServer example
[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         
67     private:
68         virtual void v_disablePrompt();
69         virtual void v_enablePrompt();
70         virtual void v_translate(std::string & data);
71
72         void charEvent(Scheduler::EventId event);
73
74         static ReadlineClientReader * instance_;
75         int ch_;
76         unsigned skipChars_;
77         char nameBuffer_[256];
78         char promptBuffer_[1024];
79         SchedulerBinding schedBinding_;
80         bool terminate_;
81
82         char * savedLineBuffer_;
83         int savedPoint_;
84         int savedEnd_;
85         int savedMark_;
86     };
87
88     /** \brief Internal: Safe GNU readline based ClientReader implementation
89         
90         This implementation of the ClientReader interface forwards all functionality to either
91         ReadlineClientReader or DumbClientReader. A RadlineClientReader is used, if none is active,
92         otherwise a DumbClientReader is allocated. Using this ClientReader implementation, the first
93         console client will have readline functionality enabled, all further will have it disabled.
94      */
95     class SafeReadlineClientReader
96         : public ClientReader
97     {
98     public:
99         SafeReadlineClientReader(Client & client);
100
101     private:
102         virtual void v_disablePrompt();
103         virtual void v_enablePrompt();
104         virtual void v_translate(std::string & data);
105
106         boost::scoped_ptr<ClientReader> reader_;
107     };
108
109 }}}
110
111 ///////////////////////////////hh.e////////////////////////////////////////
112 #include "Readline.cci"
113 //#include "Readline.ct"
114 //#include "Readline.cti"
115 #endif
116
117 \f
118 // Local Variables:
119 // mode: c++
120 // fill-column: 100
121 // comment-column: 40
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"
126 // End: