nxterm_vt100.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /****************************************************************************
  2. * graphics/nxterm/nxterm_vt100.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <nuttx/vt100.h>
  27. #include "nxterm.h"
  28. /****************************************************************************
  29. * Private Types
  30. ****************************************************************************/
  31. typedef int (*seqhandler_t)(FAR struct nxterm_state_s *priv);
  32. struct vt100_sequence_s
  33. {
  34. FAR const char *seq;
  35. seqhandler_t handler;
  36. uint8_t size;
  37. };
  38. /****************************************************************************
  39. * Private Function Prototypes
  40. ****************************************************************************/
  41. static int nxterm_erasetoeol(FAR struct nxterm_state_s *priv);
  42. /****************************************************************************
  43. * Private Data
  44. ****************************************************************************/
  45. /* All recognized VT100 escape sequences. Very little as present, this is
  46. * a placeholder for a future, more complete VT100 emulation.
  47. */
  48. /* <esc>[K is the VT100 command erases to the end of the line. */
  49. static const char g_erasetoeol[] = VT100_CLEAREOL;
  50. /* The list of all VT100 sequences supported by the emulation */
  51. static const struct vt100_sequence_s g_vt100sequences[] =
  52. {
  53. {g_erasetoeol, nxterm_erasetoeol, sizeof(g_erasetoeol)},
  54. {NULL, NULL, 0}
  55. };
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: nxterm_erasetoeol
  61. *
  62. * Description:
  63. * Handle the erase-to-eol VT100 escapte sequence
  64. *
  65. * Input Parameters:
  66. * priv - Driver data structure
  67. *
  68. * Returned Value:
  69. * The index of the match in g_vt100sequences[]
  70. *
  71. ****************************************************************************/
  72. static int nxterm_erasetoeol(FAR struct nxterm_state_s *priv)
  73. {
  74. /* Does nothing yet (other than consume the sequence) */
  75. return OK;
  76. }
  77. /****************************************************************************
  78. * Name: nxterm_vt100part
  79. *
  80. * Description:
  81. * Return the next entry that is a partial match to the sequence.
  82. *
  83. * Input Parameters:
  84. * priv - Driver data structure
  85. * seqsize - The number of bytes in the sequence
  86. * startndx - The index to start searching
  87. *
  88. * Returned Value:
  89. * A pointer to the matching sequence in g_vt100sequences[]
  90. *
  91. ****************************************************************************/
  92. FAR const struct vt100_sequence_s *
  93. nxterm_vt100part(FAR struct nxterm_state_s *priv, int seqsize)
  94. {
  95. FAR const struct vt100_sequence_s *seq;
  96. int ndx;
  97. /* Search from the beginning of the sequence table */
  98. for (ndx = 0; g_vt100sequences[ndx].seq; ndx++)
  99. {
  100. /* Is this sequence big enough? */
  101. seq = &g_vt100sequences[ndx];
  102. if (seq->size >= seqsize)
  103. {
  104. /* Yes... are the first 'seqsize' bytes the same */
  105. if (memcmp(seq->seq, priv->seq, seqsize) == 0)
  106. {
  107. /* Yes.. return the match */
  108. return seq;
  109. }
  110. }
  111. }
  112. return NULL;
  113. }
  114. /****************************************************************************
  115. * Name: nxterm_vt100seq
  116. *
  117. * Description:
  118. * Determine if the new sequence is a part of a supported VT100 escape
  119. * sequence.
  120. *
  121. * Input Parameters:
  122. * priv - Driver data structure
  123. * seqsize - The number of bytes in the sequence
  124. *
  125. * Returned Value:
  126. * state - See enum nxterm_vt100state_e;
  127. *
  128. ****************************************************************************/
  129. static enum nxterm_vt100state_e nxterm_vt100seq(
  130. FAR struct nxterm_state_s *priv,
  131. int seqsize)
  132. {
  133. FAR const struct vt100_sequence_s *seq;
  134. enum nxterm_vt100state_e ret;
  135. /* Is there any VT100 escape sequence that matches what we have
  136. * buffered so far?
  137. */
  138. seq = nxterm_vt100part(priv, seqsize);
  139. if (seq)
  140. {
  141. /* Yes.. if the size of that escape sequence is the same as what we
  142. * have buffered, then we have an exact match.
  143. */
  144. if (seq->size == seqsize)
  145. {
  146. /* Process the VT100 sequence */
  147. seq->handler(priv);
  148. priv->nseq = 0;
  149. return VT100_PROCESSED;
  150. }
  151. /* The 'seqsize' is still smaller than the potential match(es). We
  152. * will need to collect more characters before we can make a decision.
  153. * Return an indication that we have consumed the character.
  154. */
  155. return VT100_CONSUMED;
  156. }
  157. /* We get here on a failure. The buffer sequence is not part of any
  158. * supported VT100 escape sequence. If seqsize > 1 then we need to
  159. * return a special value because we have to re-process the buffered
  160. * data.
  161. */
  162. ret = seqsize > 1 ? VT100_ABORT : VT100_NOT_CONSUMED;
  163. return ret;
  164. }
  165. /****************************************************************************
  166. * Public Functions
  167. ****************************************************************************/
  168. /****************************************************************************
  169. * Name: nxterm_vt100
  170. *
  171. * Description:
  172. * Test if the newly received byte is part of a VT100 escape sequence
  173. *
  174. * Input Parameters:
  175. * priv - Driver data structure
  176. * ch - The newly received character
  177. *
  178. * Returned Value:
  179. * state - See enum nxterm_vt100state_e;
  180. *
  181. ****************************************************************************/
  182. enum nxterm_vt100state_e nxterm_vt100(FAR struct nxterm_state_s *priv,
  183. char ch)
  184. {
  185. enum nxterm_vt100state_e ret;
  186. int seqsize;
  187. DEBUGASSERT(priv && priv->nseq < VT100_MAX_SEQUENCE);
  188. /* If we have no buffered characters, then 'ch' must be the first character
  189. * of an escape sequence.
  190. */
  191. if (priv->nseq < 1)
  192. {
  193. /* The first character of an escape sequence must be an an escape
  194. * character (duh).
  195. */
  196. if (ch != ASCII_ESC)
  197. {
  198. return VT100_NOT_CONSUMED;
  199. }
  200. /* Add the escape character to the buffer but don't bother with any
  201. * further checking.
  202. */
  203. priv->seq[0] = ASCII_ESC;
  204. priv->nseq = 1;
  205. return VT100_CONSUMED;
  206. }
  207. /* Temporarily add the next character to the buffer */
  208. seqsize = priv->nseq;
  209. priv->seq[seqsize] = ch;
  210. /* Then check if this sequence is part of an a valid escape sequence */
  211. seqsize++;
  212. ret = nxterm_vt100seq(priv, seqsize);
  213. if (ret == VT100_CONSUMED)
  214. {
  215. /* The newly added character is indeed part of a VT100 escape sequence
  216. * (which is still incomplete). Keep it in the buffer.
  217. */
  218. priv->nseq = seqsize;
  219. }
  220. return ret;
  221. }