iPXE
lineconsole.c
Go to the documentation of this file.
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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 /** @file
28  *
29  * Line-based console
30  *
31  */
32 
33 #include <stdint.h>
34 #include <stddef.h>
35 #include <ipxe/ansiesc.h>
36 #include <ipxe/lineconsole.h>
37 
38 /**
39  * Print a character to a line-based console
40  *
41  * @v character Character to be printed
42  * @ret print Print line
43  */
44 size_t line_putchar ( struct line_console *line, int character ) {
45 
46  /* Strip ANSI escape sequences */
47  character = ansiesc_process ( &line->ctx, character );
48  if ( character < 0 )
49  return 0;
50 
51  /* Handle backspace characters */
52  if ( character == '\b' ) {
53  if ( line->index )
54  line->index--;
55  return 0;
56  }
57 
58  /* Ignore carriage return */
59  if ( character == '\r' )
60  return 0;
61 
62  /* Treat newline as a terminator */
63  if ( character == '\n' )
64  character = 0;
65 
66  /* Add character to buffer */
67  line->buffer[line->index++] = character;
68 
69  /* Do nothing more unless we reach end-of-line (or end-of-buffer) */
70  if ( ( character != 0 ) &&
71  ( line->index < ( line->len - 1 /* NUL */ ) ) ) {
72  return 0;
73  }
74 
75  /* Reset to start of buffer */
76  line->index = 0;
77 
78  return 1;
79 }
size_t index
Current index within buffer.
Definition: lineconsole.h:24
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
A line-based console.
Definition: lineconsole.h:17
Line-based console.
size_t len
Length of buffer.
Definition: lineconsole.h:30
size_t line_putchar(struct line_console *line, int character)
Print a character to a line-based console.
Definition: lineconsole.c:44
int ansiesc_process(struct ansiesc_context *ctx, int c)
Process character that may be part of ANSI escape sequence.
Definition: ansiesc.c:75
ANSI escape sequences.
struct ansiesc_context ctx
ANSI escape sequence context.
Definition: lineconsole.h:32
char * buffer
Data buffer.
Definition: lineconsole.h:22
FILE_SECBOOT(PERMITTED)