From 30b7344eb0a96c9cf608bec55ea38dfcb39ddfaf Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Fri, 1 Jan 2016 16:41:26 +0200
Subject: [PATCH] extmod/moduos_dupterm: Make mp_uos_dupterm_tx_strn() function
 reusable.

Function to actually spool output terminal data to dupterm object.
---
 extmod/misc.h           |  7 +++++++
 extmod/moduos_dupterm.c |  9 +++++++++
 unix/main.c             |  3 ++-
 unix/mpconfigport.h     |  7 -------
 unix/unix_mphal.c       | 14 ++------------
 5 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/extmod/misc.h b/extmod/misc.h
index 032fcba61..39bfd5ecb 100644
--- a/extmod/misc.h
+++ b/extmod/misc.h
@@ -27,6 +27,13 @@
 
 // This file contains cumulative declarations for extmod/ .
 
+#include <stddef.h>
 #include "py/runtime.h"
 
 MP_DECLARE_CONST_FUN_OBJ(mp_uos_dupterm_obj);
+
+#if MICROPY_PY_OS_DUPTERM
+void mp_uos_dupterm_tx_strn(const char *str, size_t len);
+#else
+#define mp_uos_dupterm_tx_strn(s, l)
+#endif
diff --git a/extmod/moduos_dupterm.c b/extmod/moduos_dupterm.c
index d20986279..7fe8524a5 100644
--- a/extmod/moduos_dupterm.c
+++ b/extmod/moduos_dupterm.c
@@ -34,6 +34,15 @@
 
 #if MICROPY_PY_OS_DUPTERM
 
+void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
+    if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
+        mp_obj_t write_m[3];
+        mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m);
+        write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str);
+        mp_call_method_n_kw(1, 0, write_m);
+    }
+}
+
 STATIC mp_obj_t mp_uos_dupterm(mp_uint_t n_args, const mp_obj_t *args) {
     if (n_args == 0) {
         if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) {
diff --git a/unix/main.c b/unix/main.c
index c305c3728..9f787595d 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -45,6 +45,7 @@
 #include "py/gc.h"
 #include "py/stackctrl.h"
 #include "py/mphal.h"
+#include "extmod/misc.h"
 #include "genhdr/mpversion.h"
 #include "input.h"
 
@@ -61,7 +62,7 @@ long heap_size = 1024*1024 * (sizeof(mp_uint_t) / 4);
 STATIC void stderr_print_strn(void *env, const char *str, size_t len) {
     (void)env;
     ssize_t dummy = write(STDERR_FILENO, str, len);
-    mp_hal_dupterm_tx_strn(str, len);
+    mp_uos_dupterm_tx_strn(str, len);
     (void)dummy;
 }
 
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index b5f1a4587..035c62883 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -243,13 +243,6 @@ extern const struct _mp_obj_fun_builtin_t mp_builtin_open_obj;
 
 #define MP_STATE_PORT MP_STATE_VM
 
-#if MICROPY_PY_OS_DUPTERM
-#include <stddef.h>
-void mp_hal_dupterm_tx_strn(const char *str, size_t len);
-#else
-#define mp_hal_dupterm_tx_strn(s, l)
-#endif
-
 #define MICROPY_PORT_ROOT_POINTERS \
     const char *readline_hist[50]; \
     mp_obj_t keyboard_interrupt_obj; \
diff --git a/unix/unix_mphal.c b/unix/unix_mphal.c
index 16e80b7ef..9da868056 100644
--- a/unix/unix_mphal.c
+++ b/unix/unix_mphal.c
@@ -32,6 +32,7 @@
 #include "py/mpstate.h"
 #include "py/mphal.h"
 #include "py/runtime.h"
+#include "extmod/misc.h"
 
 #ifndef _WIN32
 #include <signal.h>
@@ -107,17 +108,6 @@ void mp_hal_stdio_mode_orig(void) {
 
 #endif
 
-#if MICROPY_PY_OS_DUPTERM
-void mp_hal_dupterm_tx_strn(const char *str, size_t len) {
-    if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
-        mp_obj_t write_m[3];
-        mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_write, write_m);
-        write_m[2] = mp_obj_new_bytearray_by_ref(len, (char*)str);
-        mp_call_method_n_kw(1, 0, write_m);
-    }
-}
-#endif
-
 int mp_hal_stdin_rx_chr(void) {
     unsigned char c;
     #if MICROPY_PY_OS_DUPTERM
@@ -152,7 +142,7 @@ int mp_hal_stdin_rx_chr(void) {
 
 void mp_hal_stdout_tx_strn(const char *str, size_t len) {
     int ret = write(1, str, len);
-    mp_hal_dupterm_tx_strn(str, len);
+    mp_uos_dupterm_tx_strn(str, len);
     (void)ret; // to suppress compiler warning
 }
 
-- 
GitLab