iPXE
editstring.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <assert.h>
00027 #include <string.h>
00028 #include <ctype.h>
00029 #include <ipxe/keys.h>
00030 #include <ipxe/editstring.h>
00031 
00032 /** @file
00033  *
00034  * Editable strings
00035  *
00036  */
00037 
00038 static void insert_delete ( struct edit_string *string, size_t delete_len,
00039                             const char *insert_text ) 
00040                             __attribute__ (( nonnull (1) ));
00041 static void insert_character ( struct edit_string *string,
00042                                unsigned int character ) __nonnull;
00043 static void delete_character ( struct edit_string *string ) __nonnull;
00044 static void backspace ( struct edit_string *string ) __nonnull;
00045 static void previous_word ( struct edit_string *string ) __nonnull;
00046 static void kill_word ( struct edit_string *string ) __nonnull;
00047 static void kill_sol ( struct edit_string *string ) __nonnull;
00048 static void kill_eol ( struct edit_string *string ) __nonnull;
00049 
00050 /**
00051  * Insert and/or delete text within an editable string
00052  *
00053  * @v string            Editable string
00054  * @v delete_len        Length of text to delete from current cursor position
00055  * @v insert_text       Text to insert at current cursor position, or NULL
00056  */
00057 static void insert_delete ( struct edit_string *string, size_t delete_len,
00058                             const char *insert_text ) {
00059         size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
00060 
00061         /* Calculate lengths */
00062         old_len = strlen ( string->buf );
00063         assert ( string->cursor <= old_len );
00064         max_delete_len = ( old_len - string->cursor );
00065         if ( delete_len > max_delete_len )
00066                 delete_len = max_delete_len;
00067         insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
00068         max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
00069         if ( insert_len > max_insert_len )
00070                 insert_len = max_insert_len;
00071         new_len = ( old_len - delete_len + insert_len );
00072 
00073         /* Fill in edit history */
00074         string->mod_start = string->cursor;
00075         string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
00076 
00077         /* Move data following the cursor */
00078         memmove ( ( string->buf + string->cursor + insert_len ),
00079                   ( string->buf + string->cursor + delete_len ),
00080                   ( max_delete_len + 1 - delete_len ) );
00081 
00082         /* Copy inserted text to cursor position */
00083         memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
00084         string->cursor += insert_len;
00085 }
00086 
00087 /**
00088  * Insert character at current cursor position
00089  *
00090  * @v string            Editable string
00091  * @v character         Character to insert
00092  */
00093 static void insert_character ( struct edit_string *string,
00094                               unsigned int character ) {
00095         char insert_text[2] = { character, '\0' };
00096         insert_delete ( string, 0, insert_text );
00097 }
00098 
00099 /**
00100  * Delete character at current cursor position
00101  *
00102  * @v string            Editable string
00103  */
00104 static void delete_character ( struct edit_string *string ) {
00105         insert_delete ( string, 1, NULL );
00106 }
00107 
00108 /**
00109  * Delete character to left of current cursor position
00110  *
00111  * @v string            Editable string
00112  */
00113 static void backspace ( struct edit_string *string ) {
00114         if ( string->cursor > 0 ) {
00115                 string->cursor--;
00116                 delete_character ( string );
00117         }
00118 }
00119 
00120 /**
00121  * Move to start of previous word
00122  *
00123  * @v string            Editable string
00124  */
00125 static void previous_word ( struct edit_string *string ) {
00126         while ( string->cursor &&
00127                 isspace ( string->buf[ string->cursor - 1 ] ) ) {
00128                 string->cursor--;
00129         }
00130         while ( string->cursor &&
00131                 ( ! isspace ( string->buf[ string->cursor - 1 ] ) ) ) {
00132                 string->cursor--;
00133         }
00134 }
00135 
00136 /**
00137  * Delete to end of previous word
00138  *
00139  * @v string            Editable string
00140  */
00141 static void kill_word ( struct edit_string *string ) {
00142         size_t old_cursor = string->cursor;
00143         previous_word ( string );
00144         insert_delete ( string, ( old_cursor - string->cursor ), NULL );
00145 }
00146 
00147 /**
00148  * Delete to start of line
00149  *
00150  * @v string            Editable string
00151  */
00152 static void kill_sol ( struct edit_string *string ) {
00153         size_t old_cursor = string->cursor;
00154         string->cursor = 0;
00155         insert_delete ( string, old_cursor, NULL );
00156 }
00157 
00158 /**
00159  * Delete to end of line
00160  *
00161  * @v string            Editable string
00162  */
00163 static void kill_eol ( struct edit_string *string ) {
00164         insert_delete ( string, ~( ( size_t ) 0 ), NULL );
00165 }
00166 
00167 /**
00168  * Replace editable string
00169  *
00170  * @v string            Editable string
00171  * @v replacement       Replacement string
00172  */
00173 void replace_string ( struct edit_string *string, const char *replacement ) {
00174         string->cursor = 0;
00175         insert_delete ( string, ~( ( size_t ) 0 ), replacement );
00176 }
00177 
00178 /**
00179  * Edit editable string
00180  *
00181  * @v string            Editable string
00182  * @v key               Key pressed by user
00183  * @ret key             Key returned to application, or zero
00184  *
00185  * Handles keypresses and updates the content of the editable string.
00186  * Basic line editing facilities (delete/insert/cursor) are supported.
00187  * If edit_string() understands and uses the keypress it will return
00188  * zero, otherwise it will return the original key.
00189  *
00190  * This function does not update the display in any way.
00191  *
00192  * The string's edit history will be updated to allow the caller to
00193  * efficiently bring the display into sync with the string content.
00194  */
00195 int edit_string ( struct edit_string *string, int key ) {
00196         int retval = 0;
00197         size_t len = strlen ( string->buf );
00198 
00199         /* Prepare edit history */
00200         string->last_cursor = string->cursor;
00201         string->mod_start = string->cursor;
00202         string->mod_end = string->cursor;
00203 
00204         /* Interpret key */
00205         if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
00206                 /* Printable character; insert at current position */
00207                 insert_character ( string, key );
00208         } else switch ( key ) {
00209         case KEY_BACKSPACE:
00210                 /* Backspace */
00211                 backspace ( string );
00212                 break;
00213         case KEY_DC:
00214         case CTRL_D:
00215                 /* Delete character */
00216                 delete_character ( string );
00217                 break;
00218         case CTRL_W:
00219                 /* Delete word */
00220                 kill_word ( string );
00221                 break;
00222         case CTRL_U:
00223                 /* Delete to start of line */
00224                 kill_sol ( string );
00225                 break;
00226         case CTRL_K:
00227                 /* Delete to end of line */
00228                 kill_eol ( string );
00229                 break;
00230         case KEY_HOME:
00231         case CTRL_A:
00232                 /* Start of line */
00233                 string->cursor = 0;
00234                 break;
00235         case KEY_END:
00236         case CTRL_E:
00237                 /* End of line */
00238                 string->cursor = len;
00239                 break;
00240         case KEY_LEFT:
00241         case CTRL_B:
00242                 /* Cursor left */
00243                 if ( string->cursor > 0 )
00244                         string->cursor--;
00245                 break;
00246         case KEY_RIGHT:
00247         case CTRL_F:
00248                 /* Cursor right */
00249                 if ( string->cursor < len )
00250                         string->cursor++;
00251                 break;
00252         default:
00253                 retval = key;
00254                 break;
00255         }
00256 
00257         return retval;
00258 }