Terminfo.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Termlib_Terminfo_
18 #define HH_SENF_Utils_Termlib_Terminfo_ 1
19 
20 // Custom includes
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <iosfwd>
25 #include <boost/cstdint.hpp>
26 #include <senf/Utils/Exception.hh>
27 
28 //#include "Terminfo.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
31 namespace senf {
32 namespace term {
33 
55  class Terminfo
56  {
57  public:
58  //-////////////////////////////////////////////////////////////////////////
59  // Types
60 
63  enum { NoValue = -1 };
64 
69  struct properties
70  {
72  enum Boolean {
83 
86  static char const * const BooleanNames[];
87 
89  enum Numeric {
98 
101  static char const * const NumericNames[];
102 
104  enum String {
168 
171  static char const * const StringNames[];
172  };
173 
174  typedef boost::int16_t number_t;
175  typedef char const* string_t;
176 
177  //-////////////////////////////////////////////////////////////////////////
178 
179  Terminfo();
180  explicit Terminfo(std::string const & term);
181  void load(std::string const & term);
182 
183  bool getFlag(properties::Boolean p) const;
184  number_t getNumber(properties::Numeric p) const;
185  string_t getString(properties::String p) const;
186 
187  bool hasProperty(properties::Boolean p) const;
188  bool hasProperty(properties::Numeric p ) const;
189  bool hasProperty(properties::String p ) const;
190 
191  std::string formatString(properties::String p,
192  number_t arg1=NoValue, number_t arg2=NoValue,
193  number_t arg3=NoValue, number_t arg4=NoValue,
194  number_t arg5=NoValue, number_t arg6=NoValue,
195  number_t arg7=NoValue, number_t arg8=NoValue,
196  number_t arg9=NoValue) const;
198 
201  //-////////////////////////////////////////////////////////////////////////
202 
203  void dump(std::ostream & os) const;
204 
207  InvalidTerminfoException(std::string const & term = "");
208  };
209 
210  private:
211  typedef std::vector<bool> BoolVec;
212  typedef std::vector<number_t> NumberVec;
213  typedef std::vector<string_t> StringVec;
214  typedef std::vector<char> StringPool;
215 
216  static std::string findTerminfo(std::string const & name);
217  void load(std::istream & is);
218 
219  std::string name_;
220  BoolVec booleans_;
221  NumberVec numbers_;
222  StringVec strings_;
223  StringPool stringPool_;
224  };
225 
236  class KeyParser
237  {
238  public:
239  //-////////////////////////////////////////////////////////////////////////
240  // Types
241 
243  enum KeyCode {
244  Space = ' ', Tab = '\t', Return = '\r', First = 0xE000, Esc = First, Backspace, Backtab,
245  Begin, CATab, CTab, Cancel, Center, Clear, ClearToEOL, ClearToEOS, Close, Command, Copy,
246  Create, Delete, DeleteLine, Down, DownLeft, DownRight, End, Enter, Exit, F0, F1, F2, F3,
247  F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
248  F23, F24, F25, F26, F27, F28, F29, F30, F31, F32, F33, F34, F35, F36, F37, F38, F39,
249  F40, F41, F42, F43, F44, F45, F46, F47, F48, F49, F50, F51, F52, F53, F54, F55, F56,
250  F57, F58, F59, F60, F61, F62, F63, Find, Help, Home, Insert, InsertLine, Left, Mark,
251  Message, Mouse, Move, Next, Open, Options, PageDown, PageUp, Previous, Print, Redo,
252  Reference, Refresh, Replace, Restart, Resume, Right, Save, Select, ShiftBegin,
253  ShiftCancel, ShiftCommand, ShiftCopy, ShiftCreate, ShiftDelete, ShiftDeleteLine,
254  ShiftEnd, ShiftClearToEOL, ShiftExit, ShiftFind, ShiftHelp, ShiftHome, ShiftInsert,
255  ShiftLeft, ShiftMessage, ShiftMove, ShiftNext, ShiftOptions, ShiftPrevious, ShiftPrint,
256  ShiftRedo, ShiftReplace, ShiftResume, ShiftRight, ShiftSave, ShiftSuspend, ShiftTab,
257  ShiftUndo, Suspend, Undo, Up, UpLeft, UpRight, Incomplete = 0xE0FF };
258 
261  static char const * const KeyNames[];
262 
263  typedef wchar_t keycode_t;
264  typedef std::string::size_type size_type;
265 
267  static keycode_t Ctrl(char ch) { return ch-'@'; }
268 
269  //-////////////////////////////////////////////////////////////////////////
270 
271  KeyParser();
272  explicit KeyParser(Terminfo const & ti);
273  void load(Terminfo const & ti);
274 
275  std::pair<keycode_t, size_type> lookup(std::string const & key) const;
277 
296  static std::string describe(keycode_t key);
297 
298  void dump(std::ostream & os) const;
299 
300  private:
301  typedef std::map<std::string, KeyCode> Keytable;
302 
303  Keytable table_;
304  };
305 
306 }}
307 
308 //-/////////////////////////////////////////////////////////////////////////////////////////////////
309 //#include "Terminfo.cci"
310 //#include "Terminfo.ct"
311 //#include "Terminfo.cti"
312 #endif
313 
314 
315 // Local Variables:
316 // mode: c++
317 // fill-column: 100
318 // comment-column: 40
319 // c-file-style: "senf"
320 // indent-tabs-mode: nil
321 // ispell-local-dictionary: "american"
322 // compile-command: "scons -u test"
323 // End:
char const * string_t
String terminfo property type.
Definition: Terminfo.hh:175
KeyCode
Special keyboard key codes.
Definition: Terminfo.hh:243
void load(std::string const &term)
Load terminfo entry term.
Definition: Terminfo.cc:145
wchar_t keycode_t
Key code data type.
Definition: Terminfo.hh:263
boost::int16_t number_t
Numeric terminfo property type.
Definition: Terminfo.hh:174
number_t getNumber(properties::Numeric p) const
Get numeric property value.
Definition: Terminfo.cc:167
bool hasProperty(properties::Boolean p) const
true, if boolean property p exists
Definition: Terminfo.cc:183
bool getFlag(properties::Boolean p) const
Get boolean property value.
Definition: Terminfo.cc:159
Numeric
Numeric terminfo properties.
Definition: Terminfo.hh:89
Parse key escape sequences.
Definition: Terminfo.hh:236
static char const *const NumericNames[]
Numeric property names.
Definition: Terminfo.hh:101
Invalid, incomplete or non-existent terminfo entry exception.
Definition: Terminfo.hh:206
string_t getString(properties::String p) const
Get string property value.
Definition: Terminfo.cc:175
void dump(std::ostream &os) const
Dump a description of the terminfo entry.
Definition: Terminfo.cc:326
std::string::size_type size_type
String length type.
Definition: Terminfo.hh:264
String
String terminfo properties.
Definition: Terminfo.hh:104
Boolean
Boolean terminfo properties.
Definition: Terminfo.hh:72
static keycode_t Ctrl(char ch)
Helper to convert uppercase char to Control key code.
Definition: Terminfo.hh:267
Terminfo database entry.
Definition: Terminfo.hh:55
Terminfo property constants.
Definition: Terminfo.hh:69
static char const *const StringNames[]
String property names.
Definition: Terminfo.hh:171
unspecified_keyword_type name
std::string formatString(properties::String p, number_t arg1=NoValue, number_t arg2=NoValue, number_t arg3=NoValue, number_t arg4=NoValue, number_t arg5=NoValue, number_t arg6=NoValue, number_t arg7=NoValue, number_t arg8=NoValue, number_t arg9=NoValue) const
Format string property value.
Definition: Terminfo.cc:235
static char const *const BooleanNames[]
Boolean property names.
Definition: Terminfo.hh:86