summaryrefslogtreecommitdiffstats
path: root/lib/readkey.c
blob: c26e9d51aba96ab35138b11b14fc605dbd2d9981 (plain) (blame)
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
/*
 * readkey.c - read keystrokes and decode standard escape sequences
 *
 * Partly based on busybox vi
 *
 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * 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.
 *
 */

#include <common.h>
#include <linux/ctype.h>
#include <readkey.h>

struct esc_cmds {
	const char *seq;
	unsigned char val;
};

static const struct esc_cmds esccmds[] = {
	{"OA", BB_KEY_UP},       // cursor key Up
	{"OB", BB_KEY_DOWN},     // cursor key Down
	{"OC", BB_KEY_RIGHT},    // Cursor Key Right
	{"OD", BB_KEY_LEFT},     // cursor key Left
	{"OH", BB_KEY_HOME},     // Cursor Key Home
	{"OF", BB_KEY_END},      // Cursor Key End
	{"[A", BB_KEY_UP},       // cursor key Up
	{"[B", BB_KEY_DOWN},     // cursor key Down
	{"[C", BB_KEY_RIGHT},    // Cursor Key Right
	{"[D", BB_KEY_LEFT},     // cursor key Left
	{"[H", BB_KEY_HOME},     // Cursor Key Home
	{"[F", BB_KEY_END},      // Cursor Key End
	{"[1~", BB_KEY_HOME},    // Cursor Key Home
	{"[2~", BB_KEY_INSERT},  // Cursor Key Insert
	{"[3~", BB_KEY_DEL},     // Cursor Key Delete
	{"[4~", BB_KEY_END},     // Cursor Key End
	{"[5~", BB_KEY_PAGEUP},  // Cursor Key Page Up
	{"[6~", BB_KEY_PAGEDOWN},// Cursor Key Page Down
};

int read_key(void)
{
	unsigned char c;
	unsigned char esc[5];
	c = getchar();

	if (c == 27) {
		int i = 0;
		esc[i++] = getchar();
		esc[i++] = getchar();
		if (isdigit(esc[1])) {
			while(1) {
				esc[i] = getchar();
				if (esc[i++] == '~')
					break;
				if (i == ARRAY_SIZE(esc) - 1)
					return -1;
			}
		}
		esc[i] = 0;
		for (i = 0; i < ARRAY_SIZE(esccmds); i++){
			if (!strcmp(esc, esccmds[i].seq))
				return esccmds[i].val;
		}
		return -1;
	}
	return c;
}
EXPORT_SYMBOL(read_key);