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

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