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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <ipxe/keys.h>
  22. #include <ipxe/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. * Replace editable string
  108. *
  109. * @v string Editable string
  110. * @v replacement Replacement string
  111. */
  112. void replace_string ( struct edit_string *string, const char *replacement ) {
  113. string->cursor = 0;
  114. insert_delete ( string, ~( ( size_t ) 0 ), replacement );
  115. }
  116. /**
  117. * Edit editable string
  118. *
  119. * @v string Editable string
  120. * @v key Key pressed by user
  121. * @ret key Key returned to application, or zero
  122. *
  123. * Handles keypresses and updates the content of the editable string.
  124. * Basic line editing facilities (delete/insert/cursor) are supported.
  125. * If edit_string() understands and uses the keypress it will return
  126. * zero, otherwise it will return the original key.
  127. *
  128. * This function does not update the display in any way.
  129. *
  130. * The string's edit history will be updated to allow the caller to
  131. * efficiently bring the display into sync with the string content.
  132. */
  133. int edit_string ( struct edit_string *string, int key ) {
  134. int retval = 0;
  135. size_t len = strlen ( string->buf );
  136. /* Prepare edit history */
  137. string->last_cursor = string->cursor;
  138. string->mod_start = string->cursor;
  139. string->mod_end = string->cursor;
  140. /* Interpret key */
  141. if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
  142. /* Printable character; insert at current position */
  143. insert_character ( string, key );
  144. } else switch ( key ) {
  145. case KEY_BACKSPACE:
  146. /* Backspace */
  147. backspace ( string );
  148. break;
  149. case KEY_DC:
  150. case CTRL_D:
  151. /* Delete character */
  152. delete_character ( string );
  153. break;
  154. case CTRL_K:
  155. /* Delete to end of line */
  156. kill_eol ( string );
  157. break;
  158. case KEY_HOME:
  159. case CTRL_A:
  160. /* Start of line */
  161. string->cursor = 0;
  162. break;
  163. case KEY_END:
  164. case CTRL_E:
  165. /* End of line */
  166. string->cursor = len;
  167. break;
  168. case KEY_LEFT:
  169. case CTRL_B:
  170. /* Cursor left */
  171. if ( string->cursor > 0 )
  172. string->cursor--;
  173. break;
  174. case KEY_RIGHT:
  175. case CTRL_F:
  176. /* Cursor right */
  177. if ( string->cursor < len )
  178. string->cursor++;
  179. break;
  180. default:
  181. retval = key;
  182. break;
  183. }
  184. return retval;
  185. }