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

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