unix: Factor out stdio and ctrl-C code to unix_mphal.c file.

pull/1290/merge
Damien George 2015-05-24 20:00:50 +01:00
rodzic 2acfb7c002
commit 4a10214be2
5 zmienionych plików z 136 dodań i 34 usunięć

Wyświetl plik

@ -90,6 +90,7 @@ endif
SRC_C = \
main.c \
gccollect.c \
unix_mphal.c \
input.c \
file.c \
modos.c \

Wyświetl plik

@ -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(">>> ");

93
unix/unix_mphal.c 100644
Wyświetl plik

@ -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 100644
Wyświetl plik

@ -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);

Wyświetl plik

@ -31,6 +31,7 @@ SRC_C = \
unix/main.c \
unix/file.c \
unix/input.c \
unix/unix_mphal.c \
unix/modos.c \
unix/modtime.c \
unix/gccollect.c \