00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #ifndef HH_SENF_Utils_Termlib_Terminfo_
00027 #define HH_SENF_Utils_Termlib_Terminfo_ 1
00028
00029
00030 #include <string>
00031 #include <vector>
00032 #include <map>
00033 #include <iostream>
00034 #include <boost/cstdint.hpp>
00035 #include <boost/array.hpp>
00036 #include <senf/Utils/Exception.hh>
00037
00038
00039
00040
00041 namespace senf {
00042 namespace term {
00043
00065 class Terminfo
00066 {
00067 public:
00068
00069
00070
00073 enum { NoValue = -1 };
00074
00079 struct properties
00080 {
00082 enum Boolean {
00083 AutoLeftMargin, AutoRightMargin, NoEscCtlc, CeolStandoutGlitch, EatNewlineGlitch,
00084 EraseOverstrike, GenericType, HardCopy, HasMetaKey, HasStatusLine, InsertNullGlitch,
00085 MemoryAbove, MemoryBelow, MoveInsertMode, MoveStandoutMode, OverStrike,
00086 StatusLineEscOk, DestTabsMagicSmso, TildeGlitch, TransparentUnderline, XonXoff,
00087 NeedsXonXoff, PrtrSilent, HardCursor, NonRevRmcup, NoPadChar, NonDestScrollRegion,
00088 CanChange, BackColorErase, HueLightnessSaturation, ColAddrGlitch,
00089 CrCancelsMicroMode, HasPrintWheel, RowAddrGlitch, SemiAutoRightMargin,
00090 CpiChangesRes, LpiChangesRes, BackspacesWithBs, CrtNoScrolling,
00091 NoCorrectlyWorkingCr, GnuHasMetaKey, LinefeedIsNewline, HasHardwareTabs,
00092 ReturnDoesClrEol };
00093
00096 static char const * const BooleanNames[];
00097
00099 enum Numeric {
00100 Columns, InitTabs, Lines, LinesOfMemory, MagicCookieGlitch, PaddingBaudRate,
00101 VirtualTerminal, WidthStatusLine, NumLabels, LabelHeight, LabelWidth, MaxAttributes,
00102 MaximumWindows, MaxColors, MaxPairs, NoColorVideo, BufferCapacity, DotVertSpacing,
00103 DotHorzSpacing, MaxMicroAddress, MaxMicroJump, MicroColSize, MicroLineSize,
00104 NumberOfPins, OutputResChar, OutputResLine, OutputResHorzInch, OutputResVertInch,
00105 PrintRate, WideCharSize, Buttons, BitImageEntwining, BitImageType,
00106 MagicCookieGlitchUl, CarriageReturnDelay, NewLineDelay, BackspaceDelay,
00107 HorizontalTabDelay, NumberOfFunctionKeys };
00108
00111 static char const * const NumericNames[];
00112
00114 enum String {
00115 BackTab, Bell, CarriageReturn, ChangeScrollRegion, ClearAllTabs, ClearScreen,
00116 ClrEol, ClrEos, ColumnAddress, CommandCharacter, CursorAddress, CursorDown,
00117 CursorHome, CursorInvisible, CursorLeft, CursorMemAddress, CursorNormal,
00118 CursorRight, CursorToLl, CursorUp, CursorVisible, DeleteCharacter, DeleteLine,
00119 DisStatusLine, DownHalfLine, EnterAltCharsetMode, EnterBlinkMode, EnterBoldMode,
00120 EnterCaMode, EnterDeleteMode, EnterDimMode, EnterInsertMode, EnterSecureMode,
00121 EnterProtectedMode, EnterReverseMode, EnterStandoutMode, EnterUnderlineMode,
00122 EraseChars, ExitAltCharsetMode, ExitAttributeMode, ExitCaMode, ExitDeleteMode,
00123 ExitInsertMode, ExitStandoutMode, ExitUnderlineMode, FlashScreen, FormFeed,
00124 FromStatusLine, Init1string, Init2string, Init3string, InitFile, InsertCharacter,
00125 InsertLine, InsertPadding, KeyBackspace, KeyCatab, KeyClear, KeyCtab, KeyDc, KeyDl,
00126 KeyDown, KeyEic, KeyEol, KeyEos, KeyF0, KeyF1, KeyF10, KeyF2, KeyF3, KeyF4, KeyF5,
00127 KeyF6, KeyF7, KeyF8, KeyF9, KeyHome, KeyIc, KeyIl, KeyLeft, KeyLl, KeyNpage,
00128 KeyPpage, KeyRight, KeySf, KeySr, KeyStab, KeyUp, KeypadLocal, KeypadXmit, LabF0,
00129 LabF1, LabF10, LabF2, LabF3, LabF4, LabF5, LabF6, LabF7, LabF8, LabF9, MetaOff,
00130 MetaOn, Newline, PadChar, ParmDch, ParmDeleteLine, ParmDownCursor, ParmIch,
00131 ParmIndex, ParmInsertLine, ParmLeftCursor, ParmRightCursor, ParmRindex,
00132 ParmUpCursor, PkeyKey, PkeyLocal, PkeyXmit, PrintScreen, PrtrOff, PrtrOn,
00133 RepeatChar, Reset1string, Reset2string, Reset3string, ResetFile, RestoreCursor,
00134 RowAddress, SaveCursor, ScrollForward, ScrollReverse, SetAttributes, SetTab,
00135 SetWindow, Tab, ToStatusLine, UnderlineChar, UpHalfLine, InitProg, KeyA1, KeyA3,
00136 KeyB2, KeyC1, KeyC3, PrtrNon, CharPadding, AcsChars, PlabNorm, KeyBtab,
00137 EnterXonMode, ExitXonMode, EnterAmMode, ExitAmMode, XonCharacter, XoffCharacter,
00138 EnaAcs, LabelOn, LabelOff, KeyBeg, KeyCancel, KeyClose, KeyCommand, KeyCopy,
00139 KeyCreate, KeyEnd, KeyEnter, KeyExit, KeyFind, KeyHelp, KeyMark, KeyMessage,
00140 KeyMove, KeyNext, KeyOpen, KeyOptions, KeyPrevious, KeyPrint, KeyRedo, KeyReference,
00141 KeyRefresh, KeyReplace, KeyRestart, KeyResume, KeySave, KeySuspend, KeyUndo,
00142 KeySbeg, KeyScancel, KeyScommand, KeyScopy, KeyScreate, KeySdc, KeySdl, KeySelect,
00143 KeySend, KeySeol, KeySexit, KeySfind, KeyShelp, KeyShome, KeySic, KeySleft,
00144 KeySmessage, KeySmove, KeySnext, KeySoptions, KeySprevious, KeySprint, KeySredo,
00145 KeySreplace, KeySright, KeySrsume, KeySsave, KeySsuspend, KeySundo, ReqForInput,
00146 KeyF11, KeyF12, KeyF13, KeyF14, KeyF15, KeyF16, KeyF17, KeyF18, KeyF19, KeyF20,
00147 KeyF21, KeyF22, KeyF23, KeyF24, KeyF25, KeyF26, KeyF27, KeyF28, KeyF29, KeyF30,
00148 KeyF31, KeyF32, KeyF33, KeyF34, KeyF35, KeyF36, KeyF37, KeyF38, KeyF39, KeyF40,
00149 KeyF41, KeyF42, KeyF43, KeyF44, KeyF45, KeyF46, KeyF47, KeyF48, KeyF49, KeyF50,
00150 KeyF51, KeyF52, KeyF53, KeyF54, KeyF55, KeyF56, KeyF57, KeyF58, KeyF59, KeyF60,
00151 KeyF61, KeyF62, KeyF63, ClrBol, ClearMargins, SetLeftMargin, SetRightMargin,
00152 LabelFormat, SetClock, DisplayClock, RemoveClock, CreateWindow, GotoWindow, Hangup,
00153 DialPhone, QuickDial, Tone, Pulse, FlashHook, FixedPause, WaitTone, User0, User1,
00154 User2, User3, User4, User5, User6, User7, User8, User9, OrigPair, OrigColors,
00155 InitializeColor, InitializePair, SetColorPair, SetForeground, SetBackground,
00156 ChangeCharPitch, ChangeLinePitch, ChangeResHorz, ChangeResVert, DefineChar,
00157 EnterDoublewideMode, EnterDraftQuality, EnterItalicsMode, EnterLeftwardMode,
00158 EnterMicroMode, EnterNearLetterQuality, EnterNormalQuality, EnterShadowMode,
00159 EnterSubscriptMode, EnterSuperscriptMode, EnterUpwardMode, ExitDoublewideMode,
00160 ExitItalicsMode, ExitLeftwardMode, ExitMicroMode, ExitShadowMode, ExitSubscriptMode,
00161 ExitSuperscriptMode, ExitUpwardMode, MicroColumnAddress, MicroDown, MicroLeft,
00162 MicroRight, MicroRowAddress, MicroUp, OrderOfPins, ParmDownMicro, ParmLeftMicro,
00163 ParmRightMicro, ParmUpMicro, SelectCharSet, SetBottomMargin, SetBottomMarginParm,
00164 SetLeftMarginParm, SetRightMarginParm, SetTopMargin, SetTopMarginParm,
00165 StartBitImage, StartCharSetDef, StopBitImage, StopCharSetDef, SubscriptCharacters,
00166 SuperscriptCharacters, TheseCauseCr, ZeroMotion, CharSetNames, KeyMouse, MouseInfo,
00167 ReqMousePos, GetMouse, SetAForeground, SetABackground, PkeyPlab, DeviceType,
00168 CodeSetInit, Set0DesSeq, Set1DesSeq, Set2DesSeq, Set3DesSeq, SetLrMargin,
00169 SetTbMargin, BitImageRepeat, BitImageNewline, BitImageCarriageReturn, ColorNames,
00170 DefineBitImageRegion, EndBitImageRegion, SetColorBand, SetPageLength, DisplayPcChar,
00171 EnterPcCharsetMode, ExitPcCharsetMode, EnterScancodeMode, ExitScancodeMode,
00172 PcTermOptions, ScancodeEscape, AltScancodeEsc, EnterHorizontalHlMode,
00173 EnterLeftHlMode, EnterLowHlMode, EnterRightHlMode, EnterTopHlMode,
00174 EnterVerticalHlMode, SetAAttributes, SetPglenInch, TermcapInit2, TermcapReset,
00175 LinefeedIfNotLf, BackspaceIfNotBs, OtherNonFunctionKeys, ArrowKeyMap, AcsUlcorner,
00176 AcsLlcorner, AcsUrcorner, AcsLrcorner, AcsLtee, AcsRtee, AcsBtee, AcsTtee, AcsHline,
00177 AcsVline, AcsPlus, MemoryLock, MemoryUnlock, BoxChars1 };
00178
00181 static char const * const StringNames[];
00182 };
00183
00184 typedef boost::int16_t number_t;
00185 typedef char const* string_t;
00186
00187
00188
00189 Terminfo();
00190 explicit Terminfo(std::string const & term);
00191 void load(std::string const & term);
00192
00193 bool getFlag(properties::Boolean p) const;
00194 number_t getNumber(properties::Numeric p) const;
00195 string_t getString(properties::String p) const;
00196
00197 bool hasProperty(properties::Boolean p) const;
00198 bool hasProperty(properties::Numeric p ) const;
00199 bool hasProperty(properties::String p ) const;
00200
00201 std::string formatString(properties::String p,
00202 number_t arg1=NoValue, number_t arg2=NoValue,
00203 number_t arg3=NoValue, number_t arg4=NoValue,
00204 number_t arg5=NoValue, number_t arg6=NoValue,
00205 number_t arg7=NoValue, number_t arg8=NoValue,
00206 number_t arg9=NoValue) const;
00208
00211
00212
00213 void dump(std::ostream & os) const;
00214
00216 struct InvalidTerminfoException : public senf::Exception
00217 { InvalidTerminfoException() : senf::Exception("Unreadable terminfo file") {} };
00218
00219 private:
00220 typedef std::vector<bool> BoolVec;
00221 typedef std::vector<number_t> NumberVec;
00222 typedef std::vector<string_t> StringVec;
00223 typedef std::vector<char> StringPool;
00224
00225 std::string findTerminfo(std::string const & name);
00226 void load(std::istream & is);
00227
00228 std::string name_;
00229 BoolVec booleans_;
00230 NumberVec numbers_;
00231 StringVec strings_;
00232 StringPool stringPool_;
00233 };
00234
00245 class KeyParser
00246 {
00247 public:
00248
00249
00250
00252 enum KeyCode {
00253 Space = ' ', Tab = '\t', Return = '\r', First = 0xE000, Esc = First, Backspace, Backtab,
00254 Begin, CATab, CTab, Cancel, Center, Clear, ClearToEOL, ClearToEOS, Close, Command, Copy,
00255 Create, Delete, DeleteLine, Down, DownLeft, DownRight, End, Enter, Exit, F0, F1, F2, F3,
00256 F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
00257 F23, F24, F25, F26, F27, F28, F29, F30, F31, F32, F33, F34, F35, F36, F37, F38, F39,
00258 F40, F41, F42, F43, F44, F45, F46, F47, F48, F49, F50, F51, F52, F53, F54, F55, F56,
00259 F57, F58, F59, F60, F61, F62, F63, Find, Help, Home, Insert, InsertLine, Left, Mark,
00260 Message, Mouse, Move, Next, Open, Options, PageDown, PageUp, Previous, Print, Redo,
00261 Reference, Refresh, Replace, Restart, Resume, Right, Save, Select, ShiftBegin,
00262 ShiftCancel, ShiftCommand, ShiftCopy, ShiftCreate, ShiftDelete, ShiftDeleteLine,
00263 ShiftEnd, ShiftClearToEOL, ShiftExit, ShiftFind, ShiftHelp, ShiftHome, ShiftInsert,
00264 ShiftLeft, ShiftMessage, ShiftMove, ShiftNext, ShiftOptions, ShiftPrevious, ShiftPrint,
00265 ShiftRedo, ShiftReplace, ShiftResume, ShiftRight, ShiftSave, ShiftSuspend, ShiftTab,
00266 ShiftUndo, Suspend, Undo, Up, UpLeft, UpRight, Incomplete = 0xE0FF };
00267
00270 static char const * const KeyNames[];
00271
00272 typedef wchar_t keycode_t;
00273 typedef std::string::size_type size_type;
00274
00276 static keycode_t Ctrl(char ch) { return ch-'@'; }
00277
00278
00279
00280 KeyParser();
00281 explicit KeyParser(Terminfo const & ti);
00282 void load(Terminfo const & ti);
00283
00284 std::pair<keycode_t, size_type> lookup(std::string const & key) const;
00286
00305 static std::string describe(keycode_t key);
00306
00307 void dump(std::ostream & os) const;
00308
00309 private:
00310 typedef std::map<std::string, KeyCode> Keytable;
00311
00312 Keytable table_;
00313 };
00314
00315 }}
00316
00317
00318
00319
00320
00321 #endif
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332