vt100.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2009 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * \file gorp/edline/vt100.c
  36. * \brief VT100 key mapping.
  37. *
  38. * \verbatim
  39. * $Id$
  40. * \endverbatim
  41. */
  42. #include <gorp/edline.h>
  43. /*!
  44. * \addtogroup xgEdLine
  45. */
  46. /*@{*/
  47. /*!
  48. * \brief Optional VT100 key mapping routine.
  49. *
  50. * Pass this function to \ref EdLineRegisterKeymap to enable VT100
  51. * key mapping.
  52. *
  53. * This routine has been tested with the TeraTerm terminal emulator
  54. * after loading the keymap contained in ibmkeyb.cnf. It allows to
  55. * use the arrow keys for moving the cursor and walking through
  56. * the history. This should work with most other VT100 emulations as
  57. * well.
  58. *
  59. * The following command sequences are remapped:
  60. *
  61. * - ESC [ A remapped to \ref EDIT_KEY_UP.
  62. * - ESC [ B remapped to \ref EDIT_KEY_DOWN.
  63. * - ESC [ C remapped to \ref EDIT_KEY_RIGHT.
  64. * - ESC [ D remapped to \ref EDIT_KEY_LEFT.
  65. *
  66. * Internally calls \ref EdLineKeyMap to handle carriage return and
  67. * linefeed.
  68. *
  69. * \param key Input character to remap.
  70. * \param seq Pointer to an integer, which is used by the key mapping
  71. * routine to store the current state. Note, that its value
  72. * must be preserved by the caller between calls to
  73. * \ref EdLineRead.
  74. *
  75. * \return The mapped character. If \ref EDIT_KEY_IGNORE is returned
  76. * the caller must ignore this character.
  77. */
  78. int EdLineKeyMapVt100(int key, int *seq)
  79. {
  80. if (*seq > 0) {
  81. if (*seq == 1 && key == '[') {
  82. key = EDIT_KEY_IGNORE;
  83. *seq = 2;
  84. } else {
  85. /* Right arrow. */
  86. if (key == 'C') {
  87. key = EDIT_KEY_RIGHT;
  88. }
  89. /* Left arrow. */
  90. else if (key == 'D') {
  91. key = EDIT_KEY_LEFT;
  92. }
  93. #ifndef EDIT_DISABLE_HISTORY
  94. /* Up arrow. */
  95. else if (key == 'A') {
  96. key = EDIT_KEY_UP;
  97. }
  98. /* Down arrow. */
  99. else if (key == 'B') {
  100. key = EDIT_KEY_DOWN;
  101. }
  102. #endif
  103. *seq = 0;
  104. }
  105. }
  106. else if (key == 0x1b) {
  107. (*seq) = 1;
  108. key = EDIT_KEY_IGNORE;
  109. }
  110. else {
  111. key = EdLineKeyMap(key, seq);
  112. }
  113. return key;
  114. }
  115. /*@}*/