unix: Factor out stdio and ctrl-C code to unix_mphal.c file.
This commit is contained in:
parent
2acfb7c002
commit
4a10214be2
@ -90,6 +90,7 @@ endif
|
|||||||
SRC_C = \
|
SRC_C = \
|
||||||
main.c \
|
main.c \
|
||||||
gccollect.c \
|
gccollect.c \
|
||||||
|
unix_mphal.c \
|
||||||
input.c \
|
input.c \
|
||||||
file.c \
|
file.c \
|
||||||
modos.c \
|
modos.c \
|
||||||
|
39
unix/main.c
39
unix/main.c
@ -44,6 +44,7 @@
|
|||||||
#include "py/gc.h"
|
#include "py/gc.h"
|
||||||
#include "py/stackctrl.h"
|
#include "py/stackctrl.h"
|
||||||
#include "genhdr/mpversion.h"
|
#include "genhdr/mpversion.h"
|
||||||
|
#include "unix_mphal.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
// Command line options, with their defaults
|
// 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);
|
long heap_size = 128*1024 * (sizeof(mp_uint_t) / 4);
|
||||||
#endif
|
#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) {
|
STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
|
||||||
(void)env;
|
(void)env;
|
||||||
fwrite(str, len, 1, stderr);
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
mp_hal_set_interrupt_char(CHAR_CTRL_C);
|
||||||
// enable signal handler
|
|
||||||
struct sigaction sa;
|
|
||||||
sa.sa_handler = sighandler;
|
|
||||||
sigemptyset(&sa.sa_mask);
|
|
||||||
sigaction(SIGINT, &sa, NULL);
|
|
||||||
sa.sa_handler = SIG_DFL;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
nlr_buf_t nlr;
|
nlr_buf_t nlr;
|
||||||
if (nlr_push(&nlr) == 0) {
|
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);
|
mp_call_function_0(module_fun);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
mp_hal_set_interrupt_char(-1);
|
||||||
// disable signal handler
|
|
||||||
sigaction(SIGINT, &sa, NULL);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
nlr_pop();
|
nlr_pop();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// uncaught exception
|
// uncaught exception
|
||||||
#ifndef _WIN32
|
mp_hal_set_interrupt_char(-1);
|
||||||
// disable signal handler
|
|
||||||
sigaction(SIGINT, &sa, NULL);
|
|
||||||
#endif
|
|
||||||
return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
|
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) {
|
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 (;;) {
|
for (;;) {
|
||||||
char *line = prompt(">>> ");
|
char *line = prompt(">>> ");
|
||||||
|
93
unix/unix_mphal.c
Normal file
93
unix/unix_mphal.c
Normal file
@ -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));
|
||||||
|
}
|
36
unix/unix_mphal.h
Normal file
36
unix/unix_mphal.h
Normal file
@ -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);
|
@ -31,6 +31,7 @@ SRC_C = \
|
|||||||
unix/main.c \
|
unix/main.c \
|
||||||
unix/file.c \
|
unix/file.c \
|
||||||
unix/input.c \
|
unix/input.c \
|
||||||
|
unix/unix_mphal.c \
|
||||||
unix/modos.c \
|
unix/modos.c \
|
||||||
unix/modtime.c \
|
unix/modtime.c \
|
||||||
unix/gccollect.c \
|
unix/gccollect.c \
|
||||||
|
Loading…
Reference in New Issue
Block a user