termcap.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. #include <AK/Vector.h>
  30. #include <assert.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <termcap.h>
  34. extern "C" {
  35. char PC;
  36. char* UP;
  37. char* BC;
  38. int tgetent([[maybe_unused]] char* bp, [[maybe_unused]] const char* name)
  39. {
  40. #if TERMCAP_DEBUG
  41. fprintf(stderr, "tgetent: bp=%p, name='%s'\n", bp, name);
  42. #endif
  43. PC = '\0';
  44. BC = const_cast<char*>("\033[D");
  45. UP = const_cast<char*>("\033[A");
  46. return 1;
  47. }
  48. static HashMap<String, const char*>* caps = nullptr;
  49. static void ensure_caps()
  50. {
  51. if (caps)
  52. return;
  53. caps = new HashMap<String, const char*>;
  54. caps->set("DC", "\033[%p1%dP");
  55. caps->set("IC", "\033[%p1%d@");
  56. caps->set("ce", "\033[K");
  57. caps->set("cl", "\033[H\033[J");
  58. caps->set("cr", "\015");
  59. caps->set("dc", "\033[P");
  60. caps->set("ei", "");
  61. caps->set("ic", "");
  62. caps->set("im", "");
  63. caps->set("kd", "\033[B");
  64. caps->set("kl", "\033[D");
  65. caps->set("kr", "\033[C");
  66. caps->set("ku", "\033[A");
  67. caps->set("ks", "");
  68. caps->set("ke", "");
  69. caps->set("le", "\033[D");
  70. caps->set("mm", "");
  71. caps->set("mo", "");
  72. caps->set("pc", "");
  73. caps->set("up", "\033[A");
  74. caps->set("vb", "");
  75. caps->set("am", "");
  76. caps->set("@7", "");
  77. caps->set("kH", "");
  78. caps->set("kI", "\033[L");
  79. caps->set("kh", "\033[H");
  80. caps->set("vs", "");
  81. caps->set("ve", "");
  82. caps->set("E3", "");
  83. caps->set("kD", "");
  84. caps->set("nd", "\033[C");
  85. caps->set("co", "80");
  86. caps->set("li", "25");
  87. }
  88. // Unfortunately, tgetstr() doesn't accept a size argument for the buffer
  89. // pointed to by area, so we have to use bare strcpy().
  90. #pragma GCC diagnostic push
  91. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  92. char* tgetstr(const char* id, char** area)
  93. {
  94. ensure_caps();
  95. #if TERMCAP_DEBUG
  96. fprintf(stderr, "tgetstr: id='%s'\n", id);
  97. #endif
  98. auto it = caps->find(id);
  99. if (it != caps->end()) {
  100. char* ret = *area;
  101. const char* val = (*it).value;
  102. strcpy(*area, val);
  103. *area += strlen(val) + 1;
  104. return ret;
  105. }
  106. fprintf(stderr, "tgetstr: missing cap id='%s'\n", id);
  107. return nullptr;
  108. }
  109. #pragma GCC diagnostic pop
  110. int tgetflag([[maybe_unused]] const char* id)
  111. {
  112. #if TERMCAP_DEBUG
  113. fprintf(stderr, "tgetflag: '%s'\n", id);
  114. #endif
  115. auto it = caps->find(id);
  116. if (it != caps->end())
  117. return 1;
  118. return 0;
  119. }
  120. int tgetnum(const char* id)
  121. {
  122. #if TERMCAP_DEBUG
  123. fprintf(stderr, "tgetnum: '%s'\n", id);
  124. #endif
  125. auto it = caps->find(id);
  126. if (it != caps->end())
  127. return atoi((*it).value);
  128. ASSERT_NOT_REACHED();
  129. }
  130. static Vector<char> s_tgoto_buffer;
  131. char* tgoto([[maybe_unused]] const char* cap, [[maybe_unused]] int col, [[maybe_unused]] int row)
  132. {
  133. auto cap_str = String(cap);
  134. cap_str.replace("%p1%d", String::number(col));
  135. cap_str.replace("%p2%d", String::number(row));
  136. s_tgoto_buffer.clear_with_capacity();
  137. s_tgoto_buffer.ensure_capacity(cap_str.length());
  138. (void)cap_str.copy_characters_to_buffer(s_tgoto_buffer.data(), cap_str.length());
  139. return s_tgoto_buffer.data();
  140. }
  141. int tputs(const char* str, [[maybe_unused]] int affcnt, int (*putc)(int))
  142. {
  143. size_t len = strlen(str);
  144. for (size_t i = 0; i < len; ++i)
  145. putc(str[i]);
  146. return 0;
  147. }
  148. }