ansi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @file ansi.c
  3. * @author Ji Youzhou
  4. * @version V0.1
  5. * @date 28 Oct 2019
  6. * @brief [brief]
  7. * *****************************************************************************
  8. * @attention
  9. *
  10. * MIT License
  11. *
  12. * Copyright (C) 2019 Ji Youzhou. or its affiliates. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in all
  22. * copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /* Includes ------------------------------------------------------------------ */
  33. #include "ansi.h"
  34. #include <stdio.h>
  35. ansi_st nr_ansi;
  36. const char nr_ansi_in_cmd[] = {'m', 'I', 'A', 'B', 'C', 'D', 'X', 'K', 'M', 'P', 'J', '@', 'L', 'l', 'h', 'n', 'H', 's', 'u', '~','\0'};
  37. void (*const nr_ansi_in_cmd_fun[])(ansi_st *) =
  38. {
  39. nr_ansi_in_m_function,
  40. nr_ansi_in_I_function,
  41. nr_ansi_in_A_function,
  42. nr_ansi_in_B_function,
  43. nr_ansi_in_C_function,
  44. nr_ansi_in_D_function,
  45. nr_ansi_in_X_function,
  46. nr_ansi_in_K_function,
  47. nr_ansi_in_M_function,
  48. nr_ansi_in_P_function,
  49. nr_ansi_in_J_function,
  50. nr_ansi_in_at_function,
  51. nr_ansi_in_L_function,
  52. nr_ansi_in_l_function,
  53. nr_ansi_in_h_function,
  54. nr_ansi_in_n_function,
  55. nr_ansi_in_H_function,
  56. nr_ansi_in_s_function,
  57. nr_ansi_in_u_function,
  58. nr_ansi_in___function,
  59. };
  60. const char nr_ansi_in_special_symbol[] = {'\b', '\n', '\r', '\t', '\0'};
  61. void (*const nr_ansi_in_special_symbol_fun[])(ansi_st *) =
  62. {
  63. nr_ansi_in_bsb_function,
  64. nr_ansi_in_bsn_function,
  65. nr_ansi_in_bsr_function,
  66. nr_ansi_in_bst_function};
  67. int ansi_search_char(char x, const char *buf)
  68. {
  69. int i = 0;
  70. for (i = 0; (buf[i] != x) && (buf[i] != '\0'); i++)
  71. ;
  72. if (buf[i] != '\0')
  73. {
  74. return i;
  75. }
  76. else
  77. {
  78. return -1;
  79. }
  80. }
  81. enum
  82. {
  83. ANSI_NO_CTRL_CHAR,
  84. ANSI_MAYBE_CTRL_CHAR,
  85. ANSI_WAIT_CTRL_CHAR_END,
  86. };
  87. void ansi_init(ansi_st *ansi)
  88. {
  89. ansi->counter = 0;
  90. ansi->p = -1;
  91. ansi->current_line[ansi->counter] = '\0';
  92. ansi->cmd_num = 0;
  93. ansi->combine_state = ANSI_NO_CTRL_CHAR;
  94. }
  95. void ansi_clear_current_line(ansi_st *ansi)
  96. {
  97. ansi->counter = 0;
  98. ansi->p = -1;
  99. ansi->current_line[ansi->counter] = '\0';
  100. }
  101. char ansi_get_char(char x, ansi_st *ansi)
  102. {
  103. int cmd_id = -1;
  104. if (ansi->combine_state == ANSI_NO_CTRL_CHAR)
  105. {
  106. cmd_id = ansi_search_char(x, nr_ansi_in_special_symbol);
  107. if (cmd_id >= 0)
  108. {
  109. if (nr_ansi_in_special_symbol_fun[cmd_id] != NULL)
  110. {
  111. nr_ansi_in_special_symbol_fun[cmd_id](ansi);
  112. }
  113. }
  114. else if (x == '\033')
  115. {
  116. ansi->combine_state = ANSI_WAIT_CTRL_CHAR_END;
  117. ansi->combine_buf[ansi->cmd_num] = x;
  118. ansi->cmd_num++;
  119. }
  120. else
  121. {
  122. nr_ansi_common_char_slover(ansi,x);
  123. }
  124. }
  125. else if (ansi->combine_state == ANSI_WAIT_CTRL_CHAR_END)
  126. {
  127. ansi->combine_buf[ansi->cmd_num] = x;
  128. if (('a' <= x && 'z' >= x) || ('A' <= x && 'Z' >= x) || x== '~')
  129. {
  130. cmd_id = ansi_search_char(x, nr_ansi_in_cmd);
  131. nr_ansi_in_cmd_fun[cmd_id](ansi);
  132. ansi->cmd_num = 0;
  133. ansi->combine_state = ANSI_NO_CTRL_CHAR;
  134. }
  135. else if (ansi->cmd_num > 18)
  136. {
  137. ansi->cmd_num = 0;
  138. ansi->combine_state = ANSI_NO_CTRL_CHAR;
  139. }
  140. else
  141. {
  142. ansi->cmd_num++;
  143. }
  144. }
  145. else
  146. {
  147. ansi->combine_state = ANSI_NO_CTRL_CHAR;
  148. }
  149. return x;
  150. }
  151. /******************* (C) COPYRIGHT 2019 Ji Youzhou *****END OF FILE*****************/