summaryrefslogtreecommitdiffstatshomepage
path: root/unix
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2015-05-24 20:00:50 +0100
committerDamien George <damien.p.george@gmail.com>2015-05-27 15:52:35 +0100
commit4a10214be20cb8a51724913903678f4506358752 (patch)
treed8df91460c449cd5d8e2168225d5c9e3f9c94f17 /unix
parent2acfb7c00225c7fbc7b9fe4a4b5da450025dfc9a (diff)
downloadmicropython-4a10214be20cb8a51724913903678f4506358752.tar.gz
micropython-4a10214be20cb8a51724913903678f4506358752.zip
unix: Factor out stdio and ctrl-C code to unix_mphal.c file.
Diffstat (limited to 'unix')
-rw-r--r--unix/Makefile1
-rw-r--r--unix/main.c39
-rw-r--r--unix/unix_mphal.c93
-rw-r--r--unix/unix_mphal.h36
4 files changed, 135 insertions, 34 deletions
diff --git a/unix/Makefile b/unix/Makefile
index 78572dbbae..0d8e35f261 100644
--- a/unix/Makefile
+++ b/unix/Makefile
@@ -90,6 +90,7 @@ endif
SRC_C = \
main.c \
gccollect.c \
+ unix_mphal.c \
input.c \
file.c \
modos.c \
diff --git a/unix/main.c b/unix/main.c
index 50b6c1e0bb..fb6868c88a 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -44,6 +44,7 @@
#include "py/gc.h"
#include "py/stackctrl.h"
#include "genhdr/mpversion.h"
+#include "unix_mphal.h"
#include "input.h"
// Command line options, with their defaults
@@ -57,22 +58,6 @@ mp_uint_t mp_verbose_flag = 0;
long heap_size = 128*1024 * (sizeof(mp_uint_t) / 4);
#endif
-#ifndef _WIN32
-#include <signal.h>
-
-STATIC void sighandler(int signum) {
- if (signum == SIGINT) {
- mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
- MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj);
- // disable our handler so next we really die
- struct sigaction sa;
- sa.sa_handler = SIG_DFL;
- sigemptyset(&sa.sa_mask);
- sigaction(SIGINT, &sa, NULL);
- }
-}
-#endif
-
STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
(void)env;
fwrite(str, len, 1, stderr);
@@ -110,14 +95,7 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
return 1;
}
- #ifndef _WIN32
- // enable signal handler
- struct sigaction sa;
- sa.sa_handler = sighandler;
- sigemptyset(&sa.sa_mask);
- sigaction(SIGINT, &sa, NULL);
- sa.sa_handler = SIG_DFL;
- #endif
+ mp_hal_set_interrupt_char(CHAR_CTRL_C);
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
@@ -144,20 +122,13 @@ STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind,
mp_call_function_0(module_fun);
}
- #ifndef _WIN32
- // disable signal handler
- sigaction(SIGINT, &sa, NULL);
- #endif
-
+ mp_hal_set_interrupt_char(-1);
nlr_pop();
return 0;
} else {
// uncaught exception
- #ifndef _WIN32
- // disable signal handler
- sigaction(SIGINT, &sa, NULL);
- #endif
+ mp_hal_set_interrupt_char(-1);
return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
}
}
@@ -177,7 +148,7 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
}
STATIC int do_repl(void) {
- printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
+ mp_hal_stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
for (;;) {
char *line = prompt(">>> ");
diff --git a/unix/unix_mphal.c b/unix/unix_mphal.c
new file mode 100644
index 0000000000..c70045b4e4
--- /dev/null
+++ b/unix/unix_mphal.c
@@ -0,0 +1,93 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <unistd.h>
+#include <string.h>
+
+#include "py/mpstate.h"
+#include "unix_mphal.h"
+
+#ifndef _WIN32
+#include <signal.h>
+
+STATIC void sighandler(int signum) {
+ if (signum == SIGINT) {
+ mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
+ MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj);
+ // disable our handler so next we really die
+ struct sigaction sa;
+ sa.sa_handler = SIG_DFL;
+ sigemptyset(&sa.sa_mask);
+ sigaction(SIGINT, &sa, NULL);
+ }
+}
+#endif
+
+void mp_hal_set_interrupt_char(char c) {
+ // configure terminal settings to (not) let ctrl-C through
+ if (c == CHAR_CTRL_C) {
+ #ifndef _WIN32
+ // enable signal handler
+ struct sigaction sa;
+ sa.sa_handler = sighandler;
+ sigemptyset(&sa.sa_mask);
+ sigaction(SIGINT, &sa, NULL);
+ #endif
+ } else {
+ #ifndef _WIN32
+ // disable signal handler
+ struct sigaction sa;
+ sa.sa_handler = SIG_DFL;
+ sigemptyset(&sa.sa_mask);
+ sigaction(SIGINT, &sa, NULL);
+ #endif
+ }
+}
+
+int mp_hal_stdin_rx_chr(void) {
+ unsigned char c;
+ int ret = read(0, &c, 1);
+ if (ret == 0) {
+ c = 4; // EOF, ctrl-D
+ } else if (c == '\n') {
+ c = '\r';
+ }
+ return c;
+}
+
+void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
+ int ret = write(1, str, len);
+ (void)ret; // to suppress compiler warning
+}
+
+// cooked is same as uncooked because they terminal does some postprocessing
+void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
+ mp_hal_stdout_tx_strn(str, len);
+}
+
+void mp_hal_stdout_tx_str(const char *str) {
+ mp_hal_stdout_tx_strn(str, strlen(str));
+}
diff --git a/unix/unix_mphal.h b/unix/unix_mphal.h
new file mode 100644
index 0000000000..3d9fee5c3c
--- /dev/null
+++ b/unix/unix_mphal.h
@@ -0,0 +1,36 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef CHAR_CTRL_C
+#define CHAR_CTRL_C (3)
+#endif
+
+void mp_hal_set_interrupt_char(char c);
+
+int mp_hal_stdin_rx_chr(void);
+void mp_hal_stdout_tx_str(const char *str);
+void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
+void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len);