termlib.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*++
  2. Copyright (c) 2014 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. termlib.h
  5. Abstract:
  6. This header contains definitions for the terminal support library.
  7. Author:
  8. Evan Green 24-Jul-2014
  9. --*/
  10. //
  11. // ------------------------------------------------------------------- Includes
  12. //
  13. //
  14. // ---------------------------------------------------------------- Definitions
  15. //
  16. //
  17. // Define the maximum number of ANSI parameters.
  18. //
  19. #define TERMINAL_MAX_PARAMETERS 8
  20. #define TERMINAL_MAX_COMMAND_CHARACTERS 4
  21. #define TERMINAL_MAX_CONTROL_SEQUENCE 32
  22. #define TERMINAL_MAX_KEY_CHARACTERS 5
  23. //
  24. // Define control characters.
  25. //
  26. #define TERMINAL_ESCAPE 0x1B
  27. #define TERMINAL_INTRODUCER '['
  28. #define TERMINAL_PARAMETER_SEPARATOR ';'
  29. #define TERMINAL_RUBOUT 0x7F
  30. //
  31. // Define terminal command flags.
  32. //
  33. #define TERMINAL_COMMAND_SEEN_ESCAPE 0x00000001
  34. #define TERMINAL_COMMAND_SEEN_PARAMETER 0x00000002
  35. //
  36. // Define terminal key data flags.
  37. //
  38. #define TERMINAL_KEY_FLAG_ALT 0x00000001
  39. //
  40. // Define known terminal mode values.
  41. //
  42. #define TERMINAL_MODE_KEYBOARD_LOCKED 2
  43. #define TERMINAL_MODE_INSERT 4
  44. #define TERMINAL_MODE_DISABLE_LOCAL_ECHO 12
  45. #define TERMINAL_MODE_NEW_LINE 20
  46. #define TERMINAL_PRIVATE_MODE_APPLICATION_CURSOR_KEYS 1
  47. #define TERMINAL_PRIVATE_MODE_VT52 2
  48. #define TERMINAL_PRIVATE_MODE_132_COLUMNS 3
  49. #define TERMINAL_PRIVATE_MODE_SMOOTH_SCROLLING 4
  50. #define TERMINAL_PRIVATE_MODE_REVERSE_VIDEO 5
  51. #define TERMINAL_PRIVATE_MODE_ORIGIN 6
  52. #define TERMINAL_PRIVATE_MODE_AUTO_WRAP 7
  53. #define TERMINAL_PRIVATE_MODE_AUTO_REPEAT 8
  54. #define TERMINAL_PRIVATE_MODE_BLINKING_CURSOR 12
  55. #define TERMINAL_PRIVATE_MODE_FORM_FEED 18
  56. #define TERMINAL_PRIVATE_MODE_PRINT_FULL_SCREEN 19
  57. #define TERMINAL_PRIVATE_MODE_CURSOR 25
  58. #define TERMINAL_PRIVATE_MODE_NATIONAL 42
  59. #define TERMINAL_PRIVATE_MODE_ALTERNATE_SCREEN 1047
  60. #define TERMINAL_PRIVATE_MODE_SAVE_CURSOR 1048
  61. #define TERMINAL_PRIVATE_MODE_ALTERNATE_SCREEN_SAVE_CURSOR 1049
  62. //
  63. // Define terminal graphics rendition values.
  64. //
  65. #define TERMINAL_GRAPHICS_BOLD 1
  66. #define TERMINAL_GRAPHICS_NEGATIVE 7
  67. #define TERMINAL_GRAPHICS_FOREGROUND 30
  68. #define TERMINAL_GRAPHICS_BACKGROUND 40
  69. //
  70. // ------------------------------------------------------ Data Type Definitions
  71. //
  72. typedef enum _TERMINAL_PARSE_RESULT {
  73. TerminalParseResultInvalid,
  74. TerminalParseResultNormalCharacter,
  75. TerminalParseResultPartialCommand,
  76. TerminalParseResultCompleteCommand,
  77. } TERMINAL_PARSE_RESULT, *PTERMINAL_PARSE_RESULT;
  78. typedef enum _TERMINAL_COMMAND {
  79. TerminalCommandInvalid,
  80. TerminalCommandCursorUp,
  81. TerminalCommandCursorDown,
  82. TerminalCommandCursorLeft,
  83. TerminalCommandCursorRight,
  84. TerminalCommandCursorMove,
  85. TerminalCommandSetCursorRowAbsolute,
  86. TerminalCommandSetCursorColumnAbsolute,
  87. TerminalCommandNextLine,
  88. TerminalCommandReverseLineFeed,
  89. TerminalCommandSaveCursorAndAttributes,
  90. TerminalCommandRestoreCursorAndAttributes,
  91. TerminalCommandSetHorizontalTab,
  92. TerminalCommandClearHorizontalTab,
  93. TerminalCommandSetTopAndBottomMargin,
  94. TerminalCommandEraseInDisplay,
  95. TerminalCommandEraseInDisplaySelective,
  96. TerminalCommandEraseInLine,
  97. TerminalCommandEraseInLineSelective,
  98. TerminalCommandInsertLines,
  99. TerminalCommandDeleteLines,
  100. TerminalCommandInsertCharacters,
  101. TerminalCommandDeleteCharacters,
  102. TerminalCommandEraseCharacters,
  103. TerminalCommandKeypadNumeric,
  104. TerminalCommandKeypadApplication,
  105. TerminalCommandSetMode,
  106. TerminalCommandClearMode,
  107. TerminalCommandSetPrivateMode,
  108. TerminalCommandClearPrivateMode,
  109. TerminalCommandSelectG0CharacterSet,
  110. TerminalCommandSelectG1CharacterSet,
  111. TerminalCommandSelectG2CharacterSet,
  112. TerminalCommandSelectG3CharacterSet,
  113. TerminalCommandSelectGraphicRendition,
  114. TerminalCommandReset,
  115. TerminalCommandSoftReset,
  116. TerminalCommandDeviceAttributesPrimary,
  117. TerminalCommandDeviceAttributesSecondary,
  118. TerminalCommandScrollUp,
  119. TerminalCommandScrollDown,
  120. TerminalCommandDoubleLineHeightTopHalf,
  121. TerminalCommandDoubleLineHeightBottomHalf,
  122. TerminalCommandSingleWidthLine,
  123. TerminalCommandDoubleWidthLine,
  124. } TERMINAL_COMMAND, *PTERMINAL_COMMAND;
  125. typedef enum _TERMINAL_KEY {
  126. TerminalKeyInvalid,
  127. TerminalKeyInsert,
  128. TerminalKeyDelete,
  129. TerminalKeyHome,
  130. TerminalKeyEnd,
  131. TerminalKeyPageUp,
  132. TerminalKeyPageDown,
  133. TerminalKeyUp,
  134. TerminalKeyDown,
  135. TerminalKeyLeft,
  136. TerminalKeyRight,
  137. } TERMINAL_KEY, *PTERMINAL_KEY;
  138. /*++
  139. Structure Description:
  140. This structure stores the state for parsing or generating a terminal
  141. command.
  142. Members:
  143. Flags - Stores parsing state. See TERMINAL_COMMAND_* flags.
  144. CommandCharacters - Stores the command characters.
  145. CommandCharacterCount - Stores the number of valid characters in the
  146. command.
  147. Command - Stores the interpreted command.
  148. ParameterCount - Stores the number of valid parameters.
  149. ParameterIndex - Stores the current parameter number being parsed.
  150. Parameters - Stores the array of numeric parameters.
  151. --*/
  152. typedef struct _TERMINAL_COMMAND_DATA {
  153. ULONG Flags;
  154. UINTN PreParameterSize;
  155. UINTN PostParameterSize;
  156. CHAR PreParameter[TERMINAL_MAX_COMMAND_CHARACTERS];
  157. CHAR PostParameter[TERMINAL_MAX_COMMAND_CHARACTERS];
  158. LONG CommandCharacterCount;
  159. TERMINAL_COMMAND Command;
  160. LONG ParameterCount;
  161. LONG ParameterIndex;
  162. LONG Parameter[TERMINAL_MAX_PARAMETERS];
  163. } TERMINAL_COMMAND_DATA, *PTERMINAL_COMMAND_DATA;
  164. /*++
  165. Structure Description:
  166. This structure stores the state for parsing or generating a terminal
  167. command.
  168. Members:
  169. Flags - Stores parsing state. See TERMINAL_KEY_* flags.
  170. Buffer - Stores the buffer containing the keys so far.
  171. BufferSize - Stores the number of valid bytes in the buffer.
  172. Key - Stores the resulting key.
  173. --*/
  174. typedef struct _TERMINAL_KEY_DATA {
  175. ULONG Flags;
  176. CHAR Buffer[TERMINAL_MAX_KEY_CHARACTERS];
  177. ULONG BufferSize;
  178. TERMINAL_KEY Key;
  179. } TERMINAL_KEY_DATA, *PTERMINAL_KEY_DATA;
  180. //
  181. // -------------------------------------------------------------------- Globals
  182. //
  183. //
  184. // -------------------------------------------------------- Function Prototypes
  185. //
  186. TERMINAL_PARSE_RESULT
  187. TermProcessOutput (
  188. PTERMINAL_COMMAND_DATA Command,
  189. CHAR Character
  190. );
  191. /*++
  192. Routine Description:
  193. This routine processes a character destined for the terminal output.
  194. Arguments:
  195. Command - Supplies a pointer to the current command state. If this is the
  196. first character ever, zero out the command before calling this function.
  197. Character - Supplies the input character.
  198. Return Value:
  199. Returns a terminal output result code indicating if the character is just a
  200. normal display character, part of a command, or the last character in a
  201. complete command.
  202. --*/
  203. VOID
  204. TermNormalizeParameters (
  205. PTERMINAL_COMMAND_DATA Command
  206. );
  207. /*++
  208. Routine Description:
  209. This routine normalizes the command parameters to their expected defaults
  210. and allowed.
  211. Arguments:
  212. Command - Supplies a pointer to the complete command.
  213. Return Value:
  214. None.
  215. --*/
  216. BOOL
  217. TermCreateOutputSequence (
  218. PTERMINAL_COMMAND_DATA Command,
  219. PSTR Buffer,
  220. UINTN BufferSize
  221. );
  222. /*++
  223. Routine Description:
  224. This routine creates a terminal command sequence for a given command.
  225. Arguments:
  226. Command - Supplies a pointer to the complete command.
  227. Buffer - Supplies a pointer where the null-terminated command sequence will
  228. be returned.
  229. BufferSize - Supplies the size of the supplied buffer in bytes.
  230. Return Value:
  231. TRUE on success.
  232. FALSE on failure.
  233. --*/
  234. TERMINAL_PARSE_RESULT
  235. TermProcessInput (
  236. PTERMINAL_KEY_DATA KeyData,
  237. CHAR Character
  238. );
  239. /*++
  240. Routine Description:
  241. This routine processes a character destined for the terminal input.
  242. Arguments:
  243. KeyData - Supplies a pointer to the key parsing state. If this is the first
  244. time calling this function, zero out this structure.
  245. Character - Supplies the input character.
  246. Return Value:
  247. Returns a terminal parse result code indicating if the character is just a
  248. normal input character, part of a command, or the last character in a
  249. complete command.
  250. --*/
  251. BOOL
  252. TermCreateInputSequence (
  253. PTERMINAL_KEY_DATA KeyData,
  254. PSTR Buffer,
  255. UINTN BufferSize
  256. );
  257. /*++
  258. Routine Description:
  259. This routine creates a terminal keyboard sequence for a given key.
  260. Arguments:
  261. KeyData - Supplies the complete key data.
  262. Buffer - Supplies a pointer where the null-terminated control sequence will
  263. be returned.
  264. BufferSize - Supplies the size of the supplied buffer in bytes.
  265. Return Value:
  266. TRUE on success.
  267. FALSE on failure.
  268. --*/