Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / lineconsole.c
1 /*
2  * Copyright (C) 2012 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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Line-based console
25  *
26  */
27
28 #include <stdint.h>
29 #include <stddef.h>
30 #include <ipxe/ansiesc.h>
31 #include <ipxe/lineconsole.h>
32
33 /**
34  * Print a character to a line-based console
35  *
36  * @v character         Character to be printed
37  * @ret print           Print line
38  */
39 size_t line_putchar ( struct line_console *line, int character ) {
40
41         /* Strip ANSI escape sequences */
42         character = ansiesc_process ( &line->ctx, character );
43         if ( character < 0 )
44                 return 0;
45
46         /* Ignore carriage return */
47         if ( character == '\r' )
48                 return 0;
49
50         /* Treat newline as a terminator */
51         if ( character == '\n' )
52                 character = 0;
53
54         /* Add character to buffer */
55         line->buffer[line->index++] = character;
56
57         /* Do nothing more unless we reach end-of-line (or end-of-buffer) */
58         if ( ( character != 0 ) &&
59              ( line->index < ( line->len - 1 /* NUL */ ) ) ) {
60                 return 0;
61         }
62
63         /* Reset to start of buffer */
64         line->index = 0;
65
66         return 1;
67 }