Sfoglia il codice sorgente

[syslog] Separate out generic line-based console functionality

Abstract out the generic line-handling portions of the syslog
putchar() routine, to allow use by other console types.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 anni fa
parent
commit
851b93fbc3
3 ha cambiato i file con 117 aggiunte e 36 eliminazioni
  1. 76
    0
      src/core/lineconsole.c
  2. 33
    0
      src/include/ipxe/lineconsole.h
  3. 8
    36
      src/net/udp/syslog.c

+ 76
- 0
src/core/lineconsole.c Vedi File

@@ -0,0 +1,76 @@
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+FILE_LICENCE ( GPL2_OR_LATER );
20
+
21
+/** @file
22
+ *
23
+ * Line-based console
24
+ *
25
+ */
26
+
27
+#include <stdint.h>
28
+#include <stddef.h>
29
+#include <ipxe/ansiesc.h>
30
+#include <ipxe/lineconsole.h>
31
+
32
+/** Line-based console ANSI escape sequence handlers */
33
+static struct ansiesc_handler line_ansiesc_handlers[] = {
34
+	{ 0, NULL }
35
+};
36
+
37
+/** Line-based console ANSI escape sequence context */
38
+static struct ansiesc_context line_ansiesc_ctx = {
39
+	.handlers = line_ansiesc_handlers,
40
+};
41
+
42
+/**
43
+ * Print a character to a line-based console
44
+ *
45
+ * @v character		Character to be printed
46
+ * @ret print		Print line
47
+ */
48
+size_t line_putchar ( struct line_console *line, int character ) {
49
+
50
+	/* Strip ANSI escape sequences */
51
+	character = ansiesc_process ( &line_ansiesc_ctx, character );
52
+	if ( character < 0 )
53
+		return 0;
54
+
55
+	/* Ignore carriage return */
56
+	if ( character == '\r' )
57
+		return 0;
58
+
59
+	/* Treat newline as a terminator */
60
+	if ( character == '\n' )
61
+		character = 0;
62
+
63
+	/* Add character to buffer */
64
+	line->buffer[line->index++] = character;
65
+
66
+	/* Do nothing more unless we reach end-of-line (or end-of-buffer) */
67
+	if ( ( character != 0 ) &&
68
+	     ( line->index < ( line->len - 1 /* NUL */ ) ) ) {
69
+		return 0;
70
+	}
71
+
72
+	/* Reset to start of buffer */
73
+	line->index = 0;
74
+
75
+	return 1;
76
+}

+ 33
- 0
src/include/ipxe/lineconsole.h Vedi File

@@ -0,0 +1,33 @@
1
+#ifndef _IPXE_LINECONSOLE_H
2
+#define _IPXE_LINECONSOLE_H
3
+
4
+/** @file
5
+ *
6
+ * Line-based console
7
+ *
8
+ */
9
+
10
+FILE_LICENCE ( GPL2_OR_LATER );
11
+
12
+#include <stdint.h>
13
+
14
+/** A line-based console */
15
+struct line_console {
16
+	/** Data buffer
17
+	 *
18
+	 * Must initially be filled with NULs
19
+	 */
20
+	char *buffer;
21
+	/** Current index within buffer */
22
+	size_t index;
23
+	/** Length of buffer
24
+	 *
25
+	 * The final character of the buffer will only ever be used as
26
+	 * a potential terminating NUL.
27
+	 */
28
+	size_t len;
29
+};
30
+
31
+extern size_t line_putchar ( struct line_console *line, int character );
32
+
33
+#endif /* _IPXE_LINECONSOLE_H */

+ 8
- 36
src/net/udp/syslog.c Vedi File

@@ -32,7 +32,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 32
 #include <ipxe/dhcp.h>
33 33
 #include <ipxe/settings.h>
34 34
 #include <ipxe/console.h>
35
-#include <ipxe/ansiesc.h>
35
+#include <ipxe/lineconsole.h>
36 36
 #include <ipxe/syslog.h>
37 37
 
38 38
 /** The syslog server */
@@ -61,22 +61,15 @@ static struct interface syslogger = INTF_INIT ( syslogger_desc );
61 61
 /** Syslog line buffer */
62 62
 static char syslog_buffer[SYSLOG_BUFSIZE];
63 63
 
64
-/** Index into syslog line buffer */
65
-static unsigned int syslog_idx;
64
+/** Syslog line console */
65
+static struct line_console syslog_line = {
66
+	.buffer = syslog_buffer,
67
+	.len = sizeof ( syslog_buffer ),
68
+};
66 69
 
67 70
 /** Syslog recursion marker */
68 71
 static int syslog_entered;
69 72
 
70
-/** Syslog ANSI escape sequence handlers */
71
-static struct ansiesc_handler syslog_ansiesc_handlers[] = {
72
-	{ 0, NULL }
73
-};
74
-
75
-/** Syslog ANSI escape sequence context */
76
-static struct ansiesc_context syslog_ansiesc_ctx = {
77
-	.handlers = syslog_ansiesc_handlers,
78
-};
79
-
80 73
 /**
81 74
  * Print a character to syslog console
82 75
  *
@@ -89,31 +82,10 @@ static void syslog_putchar ( int character ) {
89 82
 	if ( syslog_entered )
90 83
 		return;
91 84
 
92
-	/* Strip ANSI escape sequences */
93
-	character = ansiesc_process ( &syslog_ansiesc_ctx, character );
94
-	if ( character < 0 )
95
-		return;
96
-
97
-	/* Ignore carriage return */
98
-	if ( character == '\r' )
85
+	/* Fill line buffer */
86
+	if ( line_putchar ( &syslog_line, character ) == 0 )
99 87
 		return;
100 88
 
101
-	/* Treat newline as a terminator */
102
-	if ( character == '\n' )
103
-		character = 0;
104
-
105
-	/* Add character to buffer */
106
-	syslog_buffer[syslog_idx++] = character;
107
-
108
-	/* Do nothing more unless we reach end-of-line (or end-of-buffer) */
109
-	if ( ( character != 0 ) &&
110
-	     ( syslog_idx < ( sizeof ( syslog_buffer ) - 1 /* NUL */ ) ) ) {
111
-		return;
112
-	}
113
-
114
-	/* Reset to start of buffer */
115
-	syslog_idx = 0;
116
-
117 89
 	/* Guard against re-entry */
118 90
 	syslog_entered = 1;
119 91
 

Loading…
Annulla
Salva