summaryrefslogtreecommitdiffstats
path: root/ui/ncurses/console-codes.c
blob: 5c2861549e31b62d0a42cda7a71e3b00ea6a1dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *  Copyright (C) 2017 IBM Corporation
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 */

#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "talloc/talloc.h"

#ifndef PETITBOOT_TEST
#include "log/log.h"
#include "nc-scr.h"
#endif

#include "console-codes.h"

#define ESC_CHAR			033
#define CSI_CHAR			'['
#define INTER_CHAR_START		040
#define INTER_CHAR_END			057
#define ESC_SEQUENCE_FINAL_START	060
#define ESC_SEQUENCE_FINAL_END		0176
#define CTRL_SEQUENCE_FINAL_START	0100
#define CTRL_SEQUENCE_FINAL_END		0176
#define DEC_PARAMETER			077

enum console_sequence_state {
	CONSOLE_STATE_START,
	CONSOLE_STATE_ESC_SEQ,
	CONSOLE_STATE_CTRL_SEQ_START,
	CONSOLE_STATE_CTRL_SEQ,
	CONSOLE_STATE_DONE,
	CONSOLE_STATE_CONFUSED,
};

static inline bool is_intermediate(signed char c)
{
	return c > INTER_CHAR_START && c < INTER_CHAR_END;
}

static inline bool is_parameter(signed char c)
{
	return (c >= 060 && c <= 071) || c == 073;
}

static inline bool is_escape_final(signed char c)
{
	return c >= ESC_SEQUENCE_FINAL_START && c < ESC_SEQUENCE_FINAL_END;
}

static inline bool is_control_final(signed char c)
{
	return c >= CTRL_SEQUENCE_FINAL_START && c <= CTRL_SEQUENCE_FINAL_END;
}

static char console_sequence_getch(char **sequence)
{
	signed char c = getch();

	if (c != ERR)
		*sequence = talloc_asprintf_append(*sequence, "%c", c);
	return c;
}

/*
 * Catch terminal control sequences that have accidentally been sent to
 * Petitboot. These are of the form
 * 	ESC I .. I F
 * where I is an Intermediate Character and F is a Final Character, eg:
 * 	ESC ^ [ ? 1 ; 0 c
 * or	ESC # 6
 *
 * This is based off the definitions provided by
 * https://vt100.net/docs/vt100-ug/contents.html
 */
char *handle_control_sequence(void *ctx, signed char start)
{
	enum console_sequence_state state = CONSOLE_STATE_START;
	bool in_sequence = true;
	signed char c;
	char *seq;

	if (start != ESC_CHAR) {
		pb_log("%s: Called with non-escape character: 0%o\n",
				__func__, start);
		return NULL;
	}

	seq = talloc_asprintf(ctx, "%c", start);

	while (in_sequence) {
		switch (state) {
		case CONSOLE_STATE_START:
			c = console_sequence_getch(&seq);
			if (c == CSI_CHAR)
				state = CONSOLE_STATE_CTRL_SEQ_START;
			else if (is_intermediate(c))
				state = CONSOLE_STATE_ESC_SEQ;
			else if (is_escape_final(c))
				state = CONSOLE_STATE_DONE;
			else if (c != ERR) {
				/* wait on c == ERR */
				pb_debug("Unexpected start: \\x%x\n", c);
				state = CONSOLE_STATE_CONFUSED;
			}
			break;
		case CONSOLE_STATE_ESC_SEQ:
			c = console_sequence_getch(&seq);
			if (is_intermediate(c))
				state = CONSOLE_STATE_ESC_SEQ;
			else if (is_escape_final(c))
				state = CONSOLE_STATE_DONE;
			else if (c != ERR) {
				/* wait on c == ERR */
				pb_debug("Unexpected character after intermediate: \\x%x\n",
						c);
				state = CONSOLE_STATE_CONFUSED;
			}
			break;
		case CONSOLE_STATE_CTRL_SEQ_START:
			c = console_sequence_getch(&seq);
			if (is_intermediate(c) || is_parameter(c) ||
					c == DEC_PARAMETER)
				state = CONSOLE_STATE_CTRL_SEQ;
			else if (is_control_final(c))
				state = CONSOLE_STATE_DONE;
			else if (c != ERR) {
				/* wait on c == ERR */
				pb_debug("Unexpected character in param string:  \\x%x\n",
						c);
				state = CONSOLE_STATE_CONFUSED;
			}
			break;
		case CONSOLE_STATE_CTRL_SEQ:
			c = console_sequence_getch(&seq);
			if (is_intermediate(c) || is_parameter(c))
				state = CONSOLE_STATE_CTRL_SEQ;
			else if (is_control_final(c))
				state = CONSOLE_STATE_DONE;
			else if (c != ERR) {
				/* wait on c == ERR */
				pb_debug("Unexpected character in param string:  \\x%x\n",
						c);
				state = CONSOLE_STATE_CONFUSED;
			}
			break;
		case CONSOLE_STATE_DONE:
			in_sequence = false;
			break;
		case CONSOLE_STATE_CONFUSED:
			/* fall-through */
		default:
			pb_debug("We got lost interpreting a control sequence!\n");
			seq = talloc_asprintf_append(seq, "...");
			in_sequence = false;
			break;
		};
	}

	return seq;
}
OpenPOWER on IntegriCloud