showkey: new applet by Vladimir

diff --git a/console-tools/Config.in b/console-tools/Config.in
index efba9cd..a08ee42 100644
--- a/console-tools/Config.in
+++ b/console-tools/Config.in
@@ -114,4 +114,10 @@
 	help
 	  This program redirects the output console of kernel messages.
 
+config SHOWKEY
+	bool "showkey"
+	default n
+	help
+	  Shows keys presses.
+
 endmenu
diff --git a/console-tools/Kbuild b/console-tools/Kbuild
index 0a18250..df5ffdb 100644
--- a/console-tools/Kbuild
+++ b/console-tools/Kbuild
@@ -19,3 +19,4 @@
 lib-$(CONFIG_SETFONT)		+= loadfont.o
 lib-$(CONFIG_SETKEYCODES)	+= setkeycodes.o
 lib-$(CONFIG_SETLOGCONS)	+= setlogcons.o
+lib-$(CONFIG_SHOWKEY)		+= showkey.o
diff --git a/console-tools/showkey.c b/console-tools/showkey.c
new file mode 100644
index 0000000..e4603e2
--- /dev/null
+++ b/console-tools/showkey.c
@@ -0,0 +1,138 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * shows keys pressed. inspired by kbd package
+ *
+ * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+#include <linux/kd.h>
+
+// set raw tty mode
+// also used by microcom
+// libbb candidates?
+static void xget1(int fd, struct termios *t, struct termios *oldt)
+{
+	tcgetattr(fd, oldt);
+	*t = *oldt;
+	cfmakeraw(t);
+}
+
+static int xset1(int fd, struct termios *tio, const char *device)
+{
+	int ret = tcsetattr(fd, TCSAFLUSH, tio);
+
+	if (ret) {
+		bb_perror_msg("can't tcsetattr for %s", device);
+	}
+	return ret;
+}
+
+/*
+ * GLOBALS
+ */
+struct globals {
+	int kbmode;
+	struct termios tio, tio0;
+};
+#define G (*ptr_to_globals)
+#define kbmode	(G.kbmode)
+#define tio	(G.tio)
+#define tio0	(G.tio0)
+#define INIT_G() do { \
+	SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+} while (0)
+
+
+static void signal_handler(int signo)
+{
+	// restore keyboard and console settings
+	xset1(STDIN_FILENO, &tio0, "stdin");
+	xioctl(STDIN_FILENO, KDSKBMODE, (void *)kbmode);
+	// alarmed? -> exit 0
+	exit(SIGALRM == signo);
+}
+
+int showkey_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int showkey_main(int argc UNUSED_PARAM, char **argv)
+{
+	enum {
+		OPT_a = (1<<0),	// display the decimal/octal/hex values of the keys
+		OPT_k = (1<<1),	// display only the interpreted keycodes (default)
+		OPT_s = (1<<2),	// display only the raw scan-codes
+	};
+
+	// FIXME: aks are all mutually exclusive
+	getopt32(argv, "aks");
+
+	INIT_G();
+
+	// get keyboard settings
+	xioctl(STDIN_FILENO, KDGKBMODE, &kbmode);
+	printf("kb mode was %s\n\nPress any keys. Program terminates %s\n\n",
+		kbmode == K_RAW ? "RAW" : 
+			(kbmode == K_XLATE ? "XLATE" : 
+				(kbmode == K_MEDIUMRAW ? "MEDIUMRAW" : 
+					(kbmode == K_UNICODE ? "UNICODE" : "?UNKNOWN?")))
+		, (option_mask32 & OPT_a) ? "when CTRL+D pressed" : "10s after last keypress"
+	);
+	// prepare for raw mode
+	xget1(STDIN_FILENO, &tio, &tio0);
+	// put stdin in raw mode
+	xset1(STDIN_FILENO, &tio, "stdin");
+
+	if (option_mask32 & OPT_a) {
+		char c;
+		// just read stdin char by char
+		while (1 == safe_read(STDIN_FILENO, &c, 1)) {
+			printf("%3d 0%03o 0x%02x\r\n", c, c, c);
+			if (04 /*CTRL-D*/ == c)
+				break;
+		}
+	} else {
+		// we should exit on any signal
+		bb_signals(BB_FATAL_SIGS, signal_handler);
+		// set raw keyboard mode
+		xioctl(STDIN_FILENO, KDSKBMODE, (void *)((option_mask32 & OPT_k) ? K_MEDIUMRAW : K_RAW));
+
+		// read and show scancodes
+		while (1) {
+			char buf[18];
+			int i, n;
+			// setup 10s watchdog
+			alarm(10);
+			// read scancodes
+			n = read(STDIN_FILENO, buf, sizeof(buf));
+			i = 0;
+			while (i < n) {
+				char c = buf[i];
+				// show raw scancodes ordered? ->
+				if (option_mask32 & OPT_s) {
+					printf("0x%02x ", buf[i++]);
+				// show interpreted scancodes (default) ? ->
+				} else {
+					int kc;
+					if (i+2 < n && (c & 0x7f) == 0
+						&& (buf[i+1] & 0x80) != 0
+						&& (buf[i+2] & 0x80) != 0) {
+						kc = ((buf[i+1] & 0x7f) << 7) | (buf[i+2] & 0x7f);
+						i += 3;
+					} else {
+						kc = (c & 0x7f);
+						i++;
+					}
+					printf("keycode %3d %s", kc, (c & 0x80) ? "release" : "press");
+				}
+			}
+			puts("\r");
+		}
+	}
+
+	// cleanup
+	signal_handler(SIGALRM);
+
+	// should never be here!
+	return EXIT_SUCCESS;
+}
diff --git a/include/applets.h b/include/applets.h
index ee96357..27c118e 100644
--- a/include/applets.h
+++ b/include/applets.h
@@ -329,6 +329,7 @@
 USE_FEATURE_SH_IS_HUSH(APPLET_ODDNAME(sh, hush, _BB_DIR_BIN, _BB_SUID_NEVER, sh))
 USE_FEATURE_SH_IS_MSH(APPLET_ODDNAME(sh, msh, _BB_DIR_BIN, _BB_SUID_NEVER, sh))
 USE_SHA1SUM(APPLET_ODDNAME(sha1sum, md5_sha1_sum, _BB_DIR_USR_BIN, _BB_SUID_NEVER, sha1sum))
+USE_SHOWKEY(APPLET(showkey, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_SLATTACH(APPLET(slattach, _BB_DIR_SBIN, _BB_SUID_NEVER))
 USE_SLEEP(APPLET_NOFORK(sleep, sleep, _BB_DIR_BIN, _BB_SUID_NEVER, sleep))
 USE_SOFTLIMIT(APPLET_ODDNAME(softlimit, chpst, _BB_DIR_USR_BIN, _BB_SUID_NEVER, softlimit))
@@ -342,7 +343,7 @@
 USE_SULOGIN(APPLET(sulogin, _BB_DIR_SBIN, _BB_SUID_NEVER))
 USE_SUM(APPLET(sum, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_SV(APPLET(sv, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
-USE_SVLOGD(APPLET(svlogd, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
+USE_SVLOGD(APPLET(svlogd, _BB_DIR_USR_SBIN, _BB_SUID_NEVER))
 USE_SWAPONOFF(APPLET_ODDNAME(swapoff, swap_on_off, _BB_DIR_SBIN, _BB_SUID_NEVER,swapoff))
 USE_SWAPONOFF(APPLET_ODDNAME(swapon, swap_on_off, _BB_DIR_SBIN, _BB_SUID_NEVER, swapon))
 USE_SWITCH_ROOT(APPLET(switch_root, _BB_DIR_SBIN, _BB_SUID_NEVER))
diff --git a/include/usage.h b/include/usage.h
index feeaadd..1e05eab 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -3568,6 +3568,15 @@
      "\n	-w	Warn about improperly formatted SHA1 checksum lines" \
 	)
 
+#define showkey_trivial_usage \
+       "[-a | -k | -s]"
+#define showkey_full_usage "\n\n" \
+       "Show keys pressed\n" \
+     "\nOptions:" \
+     "\n	-a	Display decimal/octal/hex values of the keys" \
+     "\n	-k	Display interpreted keycodes (default)" \
+     "\n	-s	Display raw scan-codes" \
+
 #define slattach_trivial_usage \
        "[-cehmLF] [-s speed] [-p protocol] DEVICEs"
 #define slattach_full_usage "\n\n" \