From 62cecc9496ca7d3a0378565b6e2b824f85f50800 Mon Sep 17 00:00:00 2001
From: Lucas Jenss <lucas@x3ro.de>
Date: Tue, 2 Oct 2018 18:05:15 +0200
Subject: [PATCH] Do not try to continue reading shell commands if input source
 is closed

In RIOT native, sending CTRL+D to a shell started using shell_run would resulted in and
endless prompt loop. I've been unable to trigger such a behaviour
on actual hardware using a UART connection, but calling `pm_off` seemed
like a better alternative than having an `#ifdef BOARD_NATIVE`.

Fixes #9946
---
 sys/include/shell.h | 4 +---
 sys/shell/shell.c   | 6 +++++-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/sys/include/shell.h b/sys/include/shell.h
index c2df96e57f..2c2766936b 100644
--- a/sys/include/shell.h
+++ b/sys/include/shell.h
@@ -69,10 +69,8 @@ typedef struct shell_command_t {
  * @param[in]       commands    ptr to array of command structs
  * @param[in]       line_buf    Buffer that will be used for reading a line
  * @param[in]       len         nr of bytes that fit in line_buf
- *
- * @returns         This function does not return.
  */
-void shell_run(const shell_command_t *commands, char *line_buf, int len) NORETURN;
+void shell_run(const shell_command_t *commands, char *line_buf, int len);
 
 #ifdef __cplusplus
 }
diff --git a/sys/shell/shell.c b/sys/shell/shell.c
index 67b7b830e2..bb4ec74ecc 100644
--- a/sys/shell/shell.c
+++ b/sys/shell/shell.c
@@ -228,7 +228,7 @@ static int readline(char *buf, size_t size)
 
         int c = getchar();
         if (c < 0) {
-            return 1;
+            return EOF;
         }
 
         /* We allow Unix linebreaks (\n), DOS linebreaks (\r\n), and Mac linebreaks (\r). */
@@ -287,6 +287,10 @@ void shell_run(const shell_command_t *shell_commands, char *line_buf, int len)
     while (1) {
         int res = readline(line_buf, len);
 
+        if (res == EOF) {
+            break;
+        }
+
         if (!res) {
             handle_input_line(shell_commands, line_buf);
         }
-- 
GitLab