aboutsummaryrefslogtreecommitdiff
path: root/common/cli_getch.c
blob: a5ed6eb6fcfa7841b4f54d5125c4e60b804e325e (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// SPDX-License-Identifier: GPL-2.0+
/*
 * (C) Copyright 2000
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * Copyright 2022 Google LLC
 */

#include <cli.h>
#include <stdio.h>
#include <string.h>
#include <linux/errno.h>

/**
 * enum cli_esc_state_t - indicates what to do with an escape character
 *
 * @ESC_REJECT: Invalid escape sequence, so the esc_save[] characters are
 *	returned from each subsequent call to cli_ch_esc()
 * @ESC_SAVE: Character should be saved in esc_save until we have another one
 * @ESC_CONVERTED: Escape sequence has been completed and the resulting
 *	character is available
 */
enum cli_esc_state_t {
	ESC_REJECT,
	ESC_SAVE,
	ESC_CONVERTED
};

void cli_ch_init(struct cli_ch_state *cch)
{
	memset(cch, '\0', sizeof(*cch));
}

/**
 * cli_ch_esc() - Process a character in an ongoing escape sequence
 *
 * @cch: State information
 * @ichar: Character to process
 * @actp: Returns the action to take
 * Returns: Output character if *actp is ESC_CONVERTED, else 0
 */
static int cli_ch_esc(struct cli_ch_state *cch, int ichar,
		      enum cli_esc_state_t *actp)
{
	enum cli_esc_state_t act = ESC_REJECT;

	switch (cch->esc_len) {
	case 1:
		if (ichar == '[' || ichar == 'O')
			act = ESC_SAVE;
		else
			act = ESC_CONVERTED;
		break;
	case 2:
		switch (ichar) {
		case 'D':	/* <- key */
			ichar = CTL_CH('b');
			act = ESC_CONVERTED;
			break;	/* pass off to ^B handler */
		case 'C':	/* -> key */
			ichar = CTL_CH('f');
			act = ESC_CONVERTED;
			break;	/* pass off to ^F handler */
		case 'H':	/* Home key */
			ichar = CTL_CH('a');
			act = ESC_CONVERTED;
			break;	/* pass off to ^A handler */
		case 'F':	/* End key */
			ichar = CTL_CH('e');
			act = ESC_CONVERTED;
			break;	/* pass off to ^E handler */
		case 'A':	/* up arrow */
			ichar = CTL_CH('p');
			act = ESC_CONVERTED;
			break;	/* pass off to ^P handler */
		case 'B':	/* down arrow */
			ichar = CTL_CH('n');
			act = ESC_CONVERTED;
			break;	/* pass off to ^N handler */
		case '1':
		case '2':
		case '3':
		case '4':
		case '7':
		case '8':
			if (cch->esc_save[1] == '[') {
				/* see if next character is ~ */
				act = ESC_SAVE;
			}
			break;
		}
		break;
	case 3:
		switch (ichar) {
		case '~':
			switch (cch->esc_save[2]) {
			case '3':	/* Delete key */
				ichar = CTL_CH('d');
				act = ESC_CONVERTED;
				break;	/* pass to ^D handler */
			case '1':	/* Home key */
			case '7':
				ichar = CTL_CH('a');
				act = ESC_CONVERTED;
				break;	/* pass to ^A handler */
			case '4':	/* End key */
			case '8':
				ichar = CTL_CH('e');
				act = ESC_CONVERTED;
				break;	/* pass to ^E handler */
			}
			break;
		case '0':
			if (cch->esc_save[2] == '2')
				act = ESC_SAVE;
			break;
		}
		break;
	case 4:
		switch (ichar) {
		case '0':
		case '1':
			act = ESC_SAVE;
			break;		/* bracketed paste */
		}
		break;
	case 5:
		if (ichar == '~') {	/* bracketed paste */
			ichar = 0;
			act = ESC_CONVERTED;
		}
	}

	*actp = act;

	return ichar;
}

int cli_ch_process(struct cli_ch_state *cch, int ichar)
{
	/*
	 * ichar=0x0 when error occurs in U-Boot getchar() or when the caller
	 * wants to check if there are more characters saved in the escape
	 * sequence
	 */
	if (!ichar) {
		if (cch->emitting) {
			if (cch->emit_upto < cch->esc_len)
				return cch->esc_save[cch->emit_upto++];
			cch->emit_upto = 0;
			cch->emitting = false;
			cch->esc_len = 0;
		}
		return 0;
	} else if (ichar == -ETIMEDOUT) {
		/*
		 * If we are in an escape sequence but nothing has followed the
		 * Escape character, then the user probably just pressed the
		 * Escape key. Return it and clear the sequence.
		 */
		if (cch->esc_len) {
			cch->esc_len = 0;
			return '\e';
		}

		/* Otherwise there is nothing to return */
		return 0;
	}

	if (ichar == '\n' || ichar == '\r')
		return '\n';

	/* handle standard linux xterm esc sequences for arrow key, etc. */
	if (cch->esc_len != 0) {
		enum cli_esc_state_t act;

		ichar = cli_ch_esc(cch, ichar, &act);

		switch (act) {
		case ESC_SAVE:
			/* save this character and return nothing */
			cch->esc_save[cch->esc_len++] = ichar;
			ichar = 0;
			break;
		case ESC_REJECT:
			/*
			 * invalid escape sequence, start returning the
			 * characters in it
			 */
			cch->esc_save[cch->esc_len++] = ichar;
			ichar = cch->esc_save[cch->emit_upto++];
			cch->emitting = true;
			return ichar;
		case ESC_CONVERTED:
			/* valid escape sequence, return the resulting char */
			cch->esc_len = 0;
			break;
		}
	}

	if (ichar == '\e') {
		if (!cch->esc_len) {
			cch->esc_save[cch->esc_len] = ichar;
			cch->esc_len = 1;
		} else {
			puts("impossible condition #876\n");
			cch->esc_len = 0;
		}
		return 0;
	}

	return ichar;
}