[U-Boot] [PATCH 2/4] Add ctrlc_ignore environment variable to ignore Ctrl-C

Simon Glass sjg at chromium.org
Thu Jun 5 20:27:50 CEST 2014


Sometimes it is useful to ignore Ctrl-C, because checking for it causes the
CLI to drop characters. In particular for tests involving sandbox, where
input commands are piped in, some commands will call ctrlc() which will
drop characters from the test script.

Add a CONFIG_SYS_CTRLC_IGNORE option which enables this variable. If the
variable is present (e.g. "setenv ctrlc_ignore ignore") then no checking
for Ctrl-C will be performed.

Signed-off-by: Simon Glass <sjg at chromium.org>
---

 README                    |  3 +++
 common/console.c          | 28 ++++++++++++++++++++++------
 include/configs/sandbox.h |  1 +
 include/env_callback.h    |  7 +++++++
 4 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/README b/README
index 6edb2e7..e25780e 100644
--- a/README
+++ b/README
@@ -3584,6 +3584,9 @@ Configuration Settings:
 - CONFIG_SYS_PROMPT:	This is what U-Boot prints on the console to
 		prompt for user input.
 
+- CONFIG_SYS_CTRLC_IGNORE: If enabled, then the console will only check for
+		Ctrl-C if the 'ctrlc_ignore" environment variable is unset.
+
 - CONFIG_SYS_CBSIZE:	Buffer size for input from the Console
 
 - CONFIG_SYS_PBSIZE:	Buffer size for Console output
diff --git a/common/console.c b/common/console.c
index 5453726..607b96f 100644
--- a/common/console.c
+++ b/common/console.c
@@ -520,15 +520,31 @@ int vprintf(const char *fmt, va_list args)
 }
 
 /* test if ctrl-c was pressed */
-static int ctrlc_disabled = 0;	/* see disable_ctrl() */
-static int ctrlc_was_pressed = 0;
+static bool ctrlc_ignore;	/* ctrlc detection always disabled */
+static bool ctrlc_disabled;	/* see disable_ctrl() */
+static bool ctrlc_was_pressed;
+
+#ifdef CONFIG_SYS_CTRLC_IGNORE
+static int on_ctrlc_ignore(const char *name, const char *value,
+			   enum env_op op, int flags)
+{
+	if (value != NULL)
+		ctrlc_ignore = true;
+	else
+		ctrlc_ignore = false;
+
+	return 0;
+}
+U_BOOT_ENV_CALLBACK(ctrlc_ignore, on_ctrlc_ignore);
+#endif /* CONFIG_SYS_CTRLC_IGNORE */
+
 int ctrlc(void)
 {
-	if (!ctrlc_disabled && gd->have_console) {
+	if (!ctrlc_ignore && !ctrlc_disabled && gd->have_console) {
 		if (tstc()) {
 			switch (getc()) {
 			case 0x03:		/* ^C - Control C */
-				ctrlc_was_pressed = 1;
+				ctrlc_was_pressed = true;
 				return 1;
 			default:
 				break;
@@ -571,7 +587,7 @@ int disable_ctrlc(int disable)
 {
 	int prev = ctrlc_disabled;	/* save previous state */
 
-	ctrlc_disabled = disable;
+	ctrlc_disabled = disable != 0;
 	return prev;
 }
 
@@ -582,7 +598,7 @@ int had_ctrlc (void)
 
 void clear_ctrlc(void)
 {
-	ctrlc_was_pressed = 0;
+	ctrlc_was_pressed = false;
 }
 
 #ifdef CONFIG_MODEM_SUPPORT_DEBUG
diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index 6bb2546..36e92ff 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -54,6 +54,7 @@
 #define CONFIG_CMD_FS_GENERIC
 
 #define CONFIG_SYS_VSNPRINTF
+#define CONFIG_SYS_CTRLC_IGNORE
 
 #define CONFIG_CMD_GPIO
 #define CONFIG_SANDBOX_GPIO
diff --git a/include/env_callback.h b/include/env_callback.h
index f90a7fa..cdd6b1e 100644
--- a/include/env_callback.h
+++ b/include/env_callback.h
@@ -31,6 +31,12 @@
 #define SPLASHIMAGE_CALLBACK
 #endif
 
+#ifdef CONFIG_SYS_CTRLC_IGNORE
+#define CTRLC_IGNORE "ctrlc_ignore:ctrlc_ignore,"
+#else
+#define CTRLC_IGNORE
+#endif
+
 /*
  * This list of callback bindings is static, but may be overridden by defining
  * a new association in the ".callbacks" environment variable.
@@ -40,6 +46,7 @@
 	"baudrate:baudrate," \
 	"bootfile:bootfile," \
 	"loadaddr:loadaddr," \
+	CTRLC_IGNORE \
 	SILENT_CALLBACK \
 	SPLASHIMAGE_CALLBACK \
 	"stdin:console,stdout:console,stderr:console," \
-- 
2.0.0.526.g5318336



More information about the U-Boot mailing list