您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

editstring.c 6.7KB

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