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

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