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

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