termlib.h 9.2 KB

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