iPXE
lineconsole.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 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 /** @file
00027  *
00028  * Line-based console
00029  *
00030  */
00031 
00032 #include <stdint.h>
00033 #include <stddef.h>
00034 #include <ipxe/ansiesc.h>
00035 #include <ipxe/lineconsole.h>
00036 
00037 /**
00038  * Print a character to a line-based console
00039  *
00040  * @v character         Character to be printed
00041  * @ret print           Print line
00042  */
00043 size_t line_putchar ( struct line_console *line, int character ) {
00044 
00045         /* Strip ANSI escape sequences */
00046         character = ansiesc_process ( &line->ctx, character );
00047         if ( character < 0 )
00048                 return 0;
00049 
00050         /* Handle backspace characters */
00051         if ( character == '\b' ) {
00052                 if ( line->index )
00053                         line->index--;
00054                 return 0;
00055         }
00056 
00057         /* Ignore carriage return */
00058         if ( character == '\r' )
00059                 return 0;
00060 
00061         /* Treat newline as a terminator */
00062         if ( character == '\n' )
00063                 character = 0;
00064 
00065         /* Add character to buffer */
00066         line->buffer[line->index++] = character;
00067 
00068         /* Do nothing more unless we reach end-of-line (or end-of-buffer) */
00069         if ( ( character != 0 ) &&
00070              ( line->index < ( line->len - 1 /* NUL */ ) ) ) {
00071                 return 0;
00072         }
00073 
00074         /* Reset to start of buffer */
00075         line->index = 0;
00076 
00077         return 1;
00078 }