You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editstring.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <assert.h>
  19. #include <string.h>
  20. #include <gpxe/keys.h>
  21. #include <gpxe/editstring.h>
  22. /** @file
  23. *
  24. * Editable strings
  25. *
  26. */
  27. /**
  28. * Insert and/or delete text within an editable string
  29. *
  30. * @v string Editable string
  31. * @v delete_len Length of text to delete from current cursor position
  32. * @v insert_text Text to insert at current cursor position, or NULL
  33. */
  34. static void insert_delete ( struct edit_string *string, size_t delete_len,
  35. const char *insert_text ) {
  36. size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
  37. /* Calculate lengths */
  38. old_len = strlen ( string->buf );
  39. assert ( string->cursor <= old_len );
  40. max_delete_len = ( old_len - string->cursor );
  41. if ( delete_len > max_delete_len )
  42. delete_len = max_delete_len;
  43. insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
  44. max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
  45. if ( insert_len > max_insert_len )
  46. insert_len = max_insert_len;
  47. new_len = ( old_len - delete_len + insert_len );
  48. /* Fill in edit history */
  49. string->mod_start = string->cursor;
  50. string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
  51. /* Move data following the cursor */
  52. memmove ( ( string->buf + string->cursor + insert_len ),
  53. ( string->buf + string->cursor + delete_len ),
  54. ( max_delete_len + 1 - delete_len ) );
  55. /* Copy inserted text to cursor position */
  56. memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
  57. string->cursor += insert_len;
  58. }
  59. /**
  60. * Insert character at current cursor position
  61. *
  62. * @v string Editable string
  63. * @v character Character to insert
  64. */
  65. static void insert_character ( struct edit_string *string,
  66. unsigned int character ) {
  67. char insert_text[2] = { character, '\0' };
  68. insert_delete ( string, 0, insert_text );
  69. }
  70. /**
  71. * Delete character at current cursor position
  72. *
  73. * @v string Editable string
  74. */
  75. static void delete_character ( struct edit_string *string ) {
  76. insert_delete ( string, 1, NULL );
  77. }
  78. /**
  79. * Delete character to left of current cursor position
  80. *
  81. * @v string Editable string
  82. */
  83. static void backspace ( struct edit_string *string ) {
  84. if ( string->cursor > 0 ) {
  85. string->cursor--;
  86. delete_character ( string );
  87. }
  88. }
  89. /**
  90. * Delete to end of line
  91. *
  92. * @v string Editable string
  93. */
  94. static void kill_eol ( struct edit_string *string ) {
  95. insert_delete ( string, ~( ( size_t ) 0 ), NULL );
  96. }
  97. /**
  98. * Edit editable string
  99. *
  100. * @v string Editable string
  101. * @v key Key pressed by user
  102. * @ret key Key returned to application, or zero
  103. *
  104. * Handles keypresses and updates the content of the editable string.
  105. * Basic line editing facilities (delete/insert/cursor) are supported.
  106. * If edit_string() understands and uses the keypress it will return
  107. * zero, otherwise it will return the original key.
  108. *
  109. * This function does not update the display in any way.
  110. *
  111. * The string's edit history will be updated to allow the caller to
  112. * efficiently bring the display into sync with the string content.
  113. */
  114. int edit_string ( struct edit_string *string, int key ) {
  115. int retval = 0;
  116. size_t len = strlen ( string->buf );
  117. /* Prepare edit history */
  118. string->last_cursor = string->cursor;
  119. string->mod_start = string->cursor;
  120. string->mod_end = string->cursor;
  121. /* Interpret key */
  122. if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
  123. /* Printable character; insert at current position */
  124. insert_character ( string, key );
  125. } else switch ( key ) {
  126. case KEY_BACKSPACE:
  127. /* Backspace */
  128. backspace ( string );
  129. break;
  130. case KEY_DC:
  131. case CTRL_D:
  132. /* Delete character */
  133. delete_character ( string );
  134. break;
  135. case CTRL_K:
  136. /* Delete to end of line */
  137. kill_eol ( string );
  138. break;
  139. case KEY_HOME:
  140. case CTRL_A:
  141. /* Start of line */
  142. string->cursor = 0;
  143. break;
  144. case KEY_END:
  145. case CTRL_E:
  146. /* End of line */
  147. string->cursor = len;
  148. break;
  149. case KEY_LEFT:
  150. case CTRL_B:
  151. /* Cursor left */
  152. if ( string->cursor > 0 )
  153. string->cursor--;
  154. break;
  155. case KEY_RIGHT:
  156. case CTRL_F:
  157. /* Cursor right */
  158. if ( string->cursor < len )
  159. string->cursor++;
  160. break;
  161. default:
  162. retval = key;
  163. break;
  164. }
  165. return retval;
  166. }