Utils: Fix hexump() of nevative values
[senf.git] / Utils / Termlib / Terminfo.hh
1 // $Id$
2 //
3 // Copyright (C) 2009 
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 Terminfo public header */
25
26 #ifndef HH_SENF_Utils_Termlib_Terminfo_
27 #define HH_SENF_Utils_Termlib_Terminfo_ 1
28
29 // Custom includes
30 #include <string>
31 #include <vector>
32 #include <map>
33 #include <iostream>
34 #include <boost/cstdint.hpp>
35 #include <boost/array.hpp>
36 #include <senf/Utils/Exception.hh>
37
38 //#include "Terminfo.mpp"
39 ///////////////////////////////hh.p////////////////////////////////////////
40
41 namespace senf {
42 namespace term {
43
44     class Terminfo
45     {
46     public:
47         ///////////////////////////////////////////////////////////////////////////
48         // Types
49
50         enum Color {
51             Black, Red, Green, Brown, Blue, Magenta, Cyan, LightGray, DarkGray, LightRed,
52             LightGreen, Yellow, LightBlue, Pink, LightCyan, White,
53             Preserve, LastColor = Preserve };
54
55         enum Attribute {
56             Standout, Underline, Reverse, Blink, HalfBright, Bold, Invisible, Protect, AltCharset,
57             Italic, Substript, Superscript, LastAttribute };
58
59         enum { NoValue = -1 };
60
61         struct properties
62         {
63             enum Boolean {
64                 AutoLeftMargin, AutoRightMargin, NoEscCtlc, CeolStandoutGlitch, EatNewlineGlitch,
65                 EraseOverstrike, GenericType, HardCopy, HasMetaKey, HasStatusLine, InsertNullGlitch,
66                 MemoryAbove, MemoryBelow, MoveInsertMode, MoveStandoutMode, OverStrike,
67                 StatusLineEscOk, DestTabsMagicSmso, TildeGlitch, TransparentUnderline, XonXoff,
68                 NeedsXonXoff, PrtrSilent, HardCursor, NonRevRmcup, NoPadChar, NonDestScrollRegion,
69                 CanChange, BackColorErase, HueLightnessSaturation, ColAddrGlitch,
70                 CrCancelsMicroMode, HasPrintWheel, RowAddrGlitch, SemiAutoRightMargin,
71                 CpiChangesRes, LpiChangesRes, BackspacesWithBs, CrtNoScrolling,
72                 NoCorrectlyWorkingCr, GnuHasMetaKey, LinefeedIsNewline, HasHardwareTabs,
73                 ReturnDoesClrEol };
74             
75             static char const * const BooleanNames[];
76
77             enum Numeric {
78                 Columns, InitTabs, Lines, LinesOfMemory, MagicCookieGlitch, PaddingBaudRate,
79                 VirtualTerminal, WidthStatusLine, NumLabels, LabelHeight, LabelWidth, MaxAttributes,
80                 MaximumWindows, MaxColors, MaxPairs, NoColorVideo, BufferCapacity, DotVertSpacing,
81                 DotHorzSpacing, MaxMicroAddress, MaxMicroJump, MicroColSize, MicroLineSize,
82                 NumberOfPins, OutputResChar, OutputResLine, OutputResHorzInch, OutputResVertInch,
83                 PrintRate, WideCharSize, Buttons, BitImageEntwining, BitImageType,
84                 MagicCookieGlitchUl, CarriageReturnDelay, NewLineDelay, BackspaceDelay,
85                 HorizontalTabDelay, NumberOfFunctionKeys };
86
87             static char const * const NumericNames[];
88             
89             enum String {
90                 BackTab, Bell, CarriageReturn, ChangeScrollRegion, ClearAllTabs, ClearScreen,
91                 ClrEol, ClrEos, ColumnAddress, CommandCharacter, CursorAddress, CursorDown,
92                 CursorHome, CursorInvisible, CursorLeft, CursorMemAddress, CursorNormal,
93                 CursorRight, CursorToLl, CursorUp, CursorVisible, DeleteCharacter, DeleteLine,
94                 DisStatusLine, DownHalfLine, EnterAltCharsetMode, EnterBlinkMode, EnterBoldMode,
95                 EnterCaMode, EnterDeleteMode, EnterDimMode, EnterInsertMode, EnterSecureMode,
96                 EnterProtectedMode, EnterReverseMode, EnterStandoutMode, EnterUnderlineMode,
97                 EraseChars, ExitAltCharsetMode, ExitAttributeMode, ExitCaMode, ExitDeleteMode,
98                 ExitInsertMode, ExitStandoutMode, ExitUnderlineMode, FlashScreen, FormFeed,
99                 FromStatusLine, Init1string, Init2string, Init3string, InitFile, InsertCharacter,
100                 InsertLine, InsertPadding, KeyBackspace, KeyCatab, KeyClear, KeyCtab, KeyDc, KeyDl,
101                 KeyDown, KeyEic, KeyEol, KeyEos, KeyF0, KeyF1, KeyF10, KeyF2, KeyF3, KeyF4, KeyF5,
102                 KeyF6, KeyF7, KeyF8, KeyF9, KeyHome, KeyIc, KeyIl, KeyLeft, KeyLl, KeyNpage,
103                 KeyPpage, KeyRight, KeySf, KeySr, KeyStab, KeyUp, KeypadLocal, KeypadXmit, LabF0,
104                 LabF1, LabF10, LabF2, LabF3, LabF4, LabF5, LabF6, LabF7, LabF8, LabF9, MetaOff,
105                 MetaOn, Newline, PadChar, ParmDch, ParmDeleteLine, ParmDownCursor, ParmIch,
106                 ParmIndex, ParmInsertLine, ParmLeftCursor, ParmRightCursor, ParmRindex,
107                 ParmUpCursor, PkeyKey, PkeyLocal, PkeyXmit, PrintScreen, PrtrOff, PrtrOn,
108                 RepeatChar, Reset1string, Reset2string, Reset3string, ResetFile, RestoreCursor,
109                 RowAddress, SaveCursor, ScrollForward, ScrollReverse, SetAttributes, SetTab,
110                 SetWindow, Tab, ToStatusLine, UnderlineChar, UpHalfLine, InitProg, KeyA1, KeyA3,
111                 KeyB2, KeyC1, KeyC3, PrtrNon, CharPadding, AcsChars, PlabNorm, KeyBtab,
112                 EnterXonMode, ExitXonMode, EnterAmMode, ExitAmMode, XonCharacter, XoffCharacter,
113                 EnaAcs, LabelOn, LabelOff, KeyBeg, KeyCancel, KeyClose, KeyCommand, KeyCopy,
114                 KeyCreate, KeyEnd, KeyEnter, KeyExit, KeyFind, KeyHelp, KeyMark, KeyMessage,
115                 KeyMove, KeyNext, KeyOpen, KeyOptions, KeyPrevious, KeyPrint, KeyRedo, KeyReference,
116                 KeyRefresh, KeyReplace, KeyRestart, KeyResume, KeySave, KeySuspend, KeyUndo,
117                 KeySbeg, KeyScancel, KeyScommand, KeyScopy, KeyScreate, KeySdc, KeySdl, KeySelect,
118                 KeySend, KeySeol, KeySexit, KeySfind, KeyShelp, KeyShome, KeySic, KeySleft,
119                 KeySmessage, KeySmove, KeySnext, KeySoptions, KeySprevious, KeySprint, KeySredo,
120                 KeySreplace, KeySright, KeySrsume, KeySsave, KeySsuspend, KeySundo, ReqForInput,
121                 KeyF11, KeyF12, KeyF13, KeyF14, KeyF15, KeyF16, KeyF17, KeyF18, KeyF19, KeyF20,
122                 KeyF21, KeyF22, KeyF23, KeyF24, KeyF25, KeyF26, KeyF27, KeyF28, KeyF29, KeyF30,
123                 KeyF31, KeyF32, KeyF33, KeyF34, KeyF35, KeyF36, KeyF37, KeyF38, KeyF39, KeyF40,
124                 KeyF41, KeyF42, KeyF43, KeyF44, KeyF45, KeyF46, KeyF47, KeyF48, KeyF49, KeyF50,
125                 KeyF51, KeyF52, KeyF53, KeyF54, KeyF55, KeyF56, KeyF57, KeyF58, KeyF59, KeyF60,
126                 KeyF61, KeyF62, KeyF63, ClrBol, ClearMargins, SetLeftMargin, SetRightMargin,
127                 LabelFormat, SetClock, DisplayClock, RemoveClock, CreateWindow, GotoWindow, Hangup,
128                 DialPhone, QuickDial, Tone, Pulse, FlashHook, FixedPause, WaitTone, User0, User1,
129                 User2, User3, User4, User5, User6, User7, User8, User9, OrigPair, OrigColors,
130                 InitializeColor, InitializePair, SetColorPair, SetForeground, SetBackground,
131                 ChangeCharPitch, ChangeLinePitch, ChangeResHorz, ChangeResVert, DefineChar,
132                 EnterDoublewideMode, EnterDraftQuality, EnterItalicsMode, EnterLeftwardMode,
133                 EnterMicroMode, EnterNearLetterQuality, EnterNormalQuality, EnterShadowMode,
134                 EnterSubscriptMode, EnterSuperscriptMode, EnterUpwardMode, ExitDoublewideMode,
135                 ExitItalicsMode, ExitLeftwardMode, ExitMicroMode, ExitShadowMode, ExitSubscriptMode,
136                 ExitSuperscriptMode, ExitUpwardMode, MicroColumnAddress, MicroDown, MicroLeft,
137                 MicroRight, MicroRowAddress, MicroUp, OrderOfPins, ParmDownMicro, ParmLeftMicro,
138                 ParmRightMicro, ParmUpMicro, SelectCharSet, SetBottomMargin, SetBottomMarginParm,
139                 SetLeftMarginParm, SetRightMarginParm, SetTopMargin, SetTopMarginParm,
140                 StartBitImage, StartCharSetDef, StopBitImage, StopCharSetDef, SubscriptCharacters,
141                 SuperscriptCharacters, TheseCauseCr, ZeroMotion, CharSetNames, KeyMouse, MouseInfo,
142                 ReqMousePos, GetMouse, SetAForeground, SetABackground, PkeyPlab, DeviceType,
143                 CodeSetInit, Set0DesSeq, Set1DesSeq, Set2DesSeq, Set3DesSeq, SetLrMargin,
144                 SetTbMargin, BitImageRepeat, BitImageNewline, BitImageCarriageReturn, ColorNames,
145                 DefineBitImageRegion, EndBitImageRegion, SetColorBand, SetPageLength, DisplayPcChar,
146                 EnterPcCharsetMode, ExitPcCharsetMode, EnterScancodeMode, ExitScancodeMode,
147                 PcTermOptions, ScancodeEscape, AltScancodeEsc, EnterHorizontalHlMode,
148                 EnterLeftHlMode, EnterLowHlMode, EnterRightHlMode, EnterTopHlMode,
149                 EnterVerticalHlMode, SetAAttributes, SetPglenInch, TermcapInit2, TermcapReset,
150                 LinefeedIfNotLf, BackspaceIfNotBs, OtherNonFunctionKeys, ArrowKeyMap, AcsUlcorner,
151                 AcsLlcorner, AcsUrcorner, AcsLrcorner, AcsLtee, AcsRtee, AcsBtee, AcsTtee, AcsHline,
152                 AcsVline, AcsPlus, MemoryLock, MemoryUnlock, BoxChars1 };
153
154             static char const * const StringNames[];
155         };
156
157         typedef boost::int16_t number_t;
158         typedef char const* string_t;
159
160         ///////////////////////////////////////////////////////////////////////////
161         
162         Terminfo();
163         explicit Terminfo(std::string const & term);
164         void load(std::string const & term);
165         
166         bool getFlag(properties::Boolean p) const;
167         number_t getNumber(properties::Numeric p) const;
168         string_t getString(properties::String p) const;
169
170         void dump(std::ostream & os) const;
171
172         struct InvalidTerminfoException : public senf::Exception
173         { InvalidTerminfoException() : senf::Exception("Unreadable terminfo file") {} };
174
175     private:
176         typedef std::vector<bool> BoolVec;
177         typedef std::vector<number_t> NumberVec;
178         typedef std::vector<string_t> StringVec;
179         typedef std::vector<char> StringPool;
180
181         std::string findTerminfo(std::string const & name);
182         void load(std::istream & is);
183
184         std::string name_;
185         BoolVec booleans_;
186         NumberVec numbers_;
187         StringVec strings_;
188         StringPool stringPool_;
189     };
190
191
192     class KeyParser
193     {
194     public:
195         ///////////////////////////////////////////////////////////////////////////
196         // Types
197
198         enum KeyCode {
199             Space = ' ', Tab = '\t', First = 0xE000, Esc = First, Backspace, Backtab, Begin, CATab,
200             CTab, Cancel, Center, Clear, ClearToEOL, ClearToEOS, Close, Command, Copy, Create,
201             Delete, DeleteLine, Down, DownLeft, DownRight, End, Enter, Exit, F0, F1, F2, F3, F4, F5,
202             F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23,
203             F24, F25, F26, F27, F28, F29, F30, F31, F32, F33, F34, F35, F36, F37, F38, F39, F40,
204             F41, F42, F43, F44, F45, F46, F47, F48, F49, F50, F51, F52, F53, F54, F55, F56, F57,
205             F58, F59, F60, F61, F62, F63, Find, Help, Home, Insert, InsertLine, Left, Mark, Message,
206             Mouse, Move, Next, Open, Options, PageDown, PageUp, Previous, Print, Redo, Reference,
207             Refresh, Replace, Restart, Resume, Right, Save, Select, ShiftBegin, ShiftCancel,
208             ShiftCommand, ShiftCopy, ShiftCreate, ShiftDelete, ShiftDeleteLine, ShiftEnd,
209             ShiftClearToEOL, ShiftExit, ShiftFind, ShiftHelp, ShiftHome, ShiftInsert, ShiftLeft,
210             ShiftMessage, ShiftMove, ShiftNext, ShiftOptions, ShiftPrevious, ShiftPrint, ShiftRedo,
211             ShiftReplace, ShiftResume, ShiftRight, ShiftSave, ShiftSuspend, ShiftTab, ShiftUndo,
212             Suspend, Undo, Up, UpLeft, UpRight, Incomplete = 0xE0FF };
213
214         static char const * const KeyNames[];
215
216         typedef wchar_t keycode_t;
217         typedef std::string::size_type size_type;
218
219         ///////////////////////////////////////////////////////////////////////////
220
221         KeyParser();
222         explicit KeyParser(Terminfo const & ti);
223         void load(Terminfo const & ti);
224
225         std::pair<keycode_t, size_type> lookup(std::string const & key) const;
226         static std::string describe(keycode_t key);
227
228         void dump(std::ostream & os) const;
229
230     private:
231         typedef std::map<std::string, KeyCode> Keytable;
232
233         Keytable table_;
234     };
235         
236 }}
237
238 ///////////////////////////////hh.e////////////////////////////////////////
239 //#include "Terminfo.cci"
240 //#include "Terminfo.ct"
241 //#include "Terminfo.cti"
242 #endif
243
244 \f
245 // Local Variables:
246 // mode: c++
247 // fill-column: 100
248 // comment-column: 40
249 // c-file-style: "senf"
250 // indent-tabs-mode: nil
251 // ispell-local-dictionary: "american"
252 // compile-command: "scons -u test"
253 // End: