summaryrefslogtreecommitdiffstatshomepage
path: root/unix/unix_mphal.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-12-28 01:12:59 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-12-28 01:13:21 +0200
commit2c1620ce1fe3bb2d011ccaca701191927f1d49d6 (patch)
treeb2e7a03df7bf98941ac8b24f4ec43339a0157146 /unix/unix_mphal.c
parent3ea03a118880c9ffb6714049c0907a94b39d31a8 (diff)
downloadmicropython-2c1620ce1fe3bb2d011ccaca701191927f1d49d6.tar.gz
micropython-2c1620ce1fe3bb2d011ccaca701191927f1d49d6.zip
unix: Implement uos.dupterm(). Conditional on MICROPY_PY_OS_DUPTERM.
Diffstat (limited to 'unix/unix_mphal.c')
-rw-r--r--unix/unix_mphal.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/unix/unix_mphal.c b/unix/unix_mphal.c
index 8d3321ef66..16e80b7ef8 100644
--- a/unix/unix_mphal.c
+++ b/unix/unix_mphal.c
@@ -31,6 +31,7 @@
#include "py/mpstate.h"
#include "py/mphal.h"
+#include "py/runtime.h"
#ifndef _WIN32
#include <signal.h>
@@ -106,8 +107,40 @@ 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
+ while (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
+ mp_obj_t read_m[3];
+ mp_load_method(MP_STATE_PORT(term_obj), MP_QSTR_read, read_m);
+ read_m[2] = MP_OBJ_NEW_SMALL_INT(1);
+ mp_obj_t res = mp_call_method_n_kw(1, 0, read_m);
+ if (res == mp_const_none) {
+ break;
+ }
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ);
+ if (bufinfo.len == 0) {
+ break;
+ }
+ c = *(byte*)bufinfo.buf;
+ if (c == '\n') {
+ c = '\r';
+ }
+ return c;
+ }
+ #endif
int ret = read(0, &c, 1);
if (ret == 0) {
c = 4; // EOF, ctrl-D
@@ -119,6 +152,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);
(void)ret; // to suppress compiler warning
}