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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <gpxe/keys.h>
  22. #include <gpxe/editstring.h>
  23. /** @file
  24. *
  25. * Editable strings
  26. *
  27. */
  28. static void insert_delete ( struct edit_string *string, size_t delete_len,
  29. const char *insert_text )
  30. __attribute__ (( nonnull (1) ));
  31. static void insert_character ( struct edit_string *string,
  32. unsigned int character ) __nonnull;
  33. static void delete_character ( struct edit_string *string ) __nonnull;
  34. static void backspace ( struct edit_string *string ) __nonnull;
  35. static void kill_eol ( struct edit_string *string ) __nonnull;
  36. /**
  37. * Insert and/or delete text within an editable string
  38. *
  39. * @v string Editable string
  40. * @v delete_len Length of text to delete from current cursor position
  41. * @v insert_text Text to insert at current cursor position, or NULL
  42. */
  43. static void insert_delete ( struct edit_string *string, size_t delete_len,
  44. const char *insert_text ) {
  45. size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
  46. /* Calculate lengths */
  47. old_len = strlen ( string->buf );
  48. assert ( string->cursor <= old_len );
  49. max_delete_len = ( old_len - string->cursor );
  50. if ( delete_len > max_delete_len )
  51. delete_len = max_delete_len;
  52. insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
  53. max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
  54. if ( insert_len > max_insert_len )
  55. insert_len = max_insert_len;
  56. new_len = ( old_len - delete_len + insert_len );
  57. /* Fill in edit history */
  58. string->mod_start = string->cursor;
  59. string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
  60. /* Move data following the cursor */
  61. memmove ( ( string->buf + string->cursor + insert_len ),
  62. ( string->buf + string->cursor + delete_len ),
  63. ( max_delete_len + 1 - delete_len ) );
  64. /* Copy inserted text to cursor position */
  65. memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
  66. string->cursor += insert_len;
  67. }
  68. /**
  69. * Insert character at current cursor position
  70. *
  71. * @v string Editable string
  72. * @v character Character to insert
  73. */
  74. static void insert_character ( struct edit_string *string,
  75. unsigned int character ) {
  76. char insert_text[2] = { character, '\0' };
  77. insert_delete ( string, 0, insert_text );
  78. }
  79. /**
  80. * Delete character at current cursor position
  81. *
  82. * @v string Editable string
  83. */
  84. static void delete_character ( struct edit_string *string ) {
  85. insert_delete ( string, 1, NULL );
  86. }
  87. /**
  88. * Delete character to left of current cursor position
  89. *
  90. * @v string Editable string
  91. */
  92. static void backspace ( struct edit_string *string ) {
  93. if ( string->cursor > 0 ) {
  94. string->cursor--;
  95. delete_character ( string );
  96. }
  97. }
  98. /**
  99. * Delete to end of line
  100. *
  101. * @v string Editable string
  102. */
  103. static void kill_eol ( struct edit_string *string ) {
  104. insert_delete ( string, ~( ( size_t ) 0 ), NULL );
  105. }
  106. /**
  107. * Edit editable string
  108. *
  109. * @v string Editable string
  110. * @v key Key pressed by user
  111. * @ret key Key returned to application, or zero
  112. *
  113. * Handles keypresses and updates the content of the editable string.
  114. * Basic line editing facilities (delete/insert/cursor) are supported.
  115. * If edit_string() understands and uses the keypress it will return
  116. * zero, otherwise it will return the original key.
  117. *
  118. * This function does not update the display in any way.
  119. *
  120. * The string's edit history will be updated to allow the caller to
  121. * efficiently bring the display into sync with the string content.
  122. */
  123. int edit_string ( struct edit_string *string, int key ) {
  124. int retval = 0;
  125. size_t len = strlen ( string->buf );
  126. /* Prepare edit history */
  127. string->last_cursor = string->cursor;
  128. string->mod_start = string->cursor;
  129. string->mod_end = string->cursor;
  130. /* Interpret key */
  131. if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
  132. /* Printable character; insert at current position */
  133. insert_character ( string, key );
  134. } else switch ( key ) {
  135. case KEY_BACKSPACE:
  136. /* Backspace */
  137. backspace ( string );
  138. break;
  139. case KEY_DC:
  140. case CTRL_D:
  141. /* Delete character */
  142. delete_character ( string );
  143. break;
  144. case CTRL_K:
  145. /* Delete to end of line */
  146. kill_eol ( string );
  147. break;
  148. case KEY_HOME:
  149. case CTRL_A:
  150. /* Start of line */
  151. string->cursor = 0;
  152. break;
  153. case KEY_END:
  154. case CTRL_E:
  155. /* End of line */
  156. string->cursor = len;
  157. break;
  158. case KEY_LEFT:
  159. case CTRL_B:
  160. /* Cursor left */
  161. if ( string->cursor > 0 )
  162. string->cursor--;
  163. break;
  164. case KEY_RIGHT:
  165. case CTRL_F:
  166. /* Cursor right */
  167. if ( string->cursor < len )
  168. string->cursor++;
  169. break;
  170. default:
  171. retval = key;
  172. break;
  173. }
  174. return retval;
  175. }