Skip to content

Commit 4a10214

Browse files
committed
unix: Factor out stdio and ctrl-C code to unix_mphal.c file.
1 parent 2acfb7c commit 4a10214

5 files changed

Lines changed: 136 additions & 34 deletions

File tree

unix/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ endif
9090
SRC_C = \
9191
main.c \
9292
gccollect.c \
93+
unix_mphal.c \
9394
input.c \
9495
file.c \
9596
modos.c \

unix/main.c

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "py/gc.h"
4545
#include "py/stackctrl.h"
4646
#include "genhdr/mpversion.h"
47+
#include "unix_mphal.h"
4748
#include "input.h"
4849

4950
// Command line options, with their defaults
@@ -57,22 +58,6 @@ mp_uint_t mp_verbose_flag = 0;
5758
long heap_size = 128*1024 * (sizeof(mp_uint_t) / 4);
5859
#endif
5960

60-
#ifndef _WIN32
61-
#include <signal.h>
62-
63-
STATIC void sighandler(int signum) {
64-
if (signum == SIGINT) {
65-
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
66-
MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj);
67-
// disable our handler so next we really die
68-
struct sigaction sa;
69-
sa.sa_handler = SIG_DFL;
70-
sigemptyset(&sa.sa_mask);
71-
sigaction(SIGINT, &sa, NULL);
72-
}
73-
}
74-
#endif
75-
7661
STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
7762
(void)env;
7863
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,
11095
return 1;
11196
}
11297

113-
#ifndef _WIN32
114-
// enable signal handler
115-
struct sigaction sa;
116-
sa.sa_handler = sighandler;
117-
sigemptyset(&sa.sa_mask);
118-
sigaction(SIGINT, &sa, NULL);
119-
sa.sa_handler = SIG_DFL;
120-
#endif
98+
mp_hal_set_interrupt_char(CHAR_CTRL_C);
12199

122100
nlr_buf_t nlr;
123101
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,
144122
mp_call_function_0(module_fun);
145123
}
146124

147-
#ifndef _WIN32
148-
// disable signal handler
149-
sigaction(SIGINT, &sa, NULL);
150-
#endif
151-
125+
mp_hal_set_interrupt_char(-1);
152126
nlr_pop();
153127
return 0;
154128

155129
} else {
156130
// uncaught exception
157-
#ifndef _WIN32
158-
// disable signal handler
159-
sigaction(SIGINT, &sa, NULL);
160-
#endif
131+
mp_hal_set_interrupt_char(-1);
161132
return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
162133
}
163134
}
@@ -177,7 +148,7 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
177148
}
178149

179150
STATIC int do_repl(void) {
180-
printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
151+
mp_hal_stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
181152

182153
for (;;) {
183154
char *line = prompt(">>> ");

unix/unix_mphal.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <unistd.h>
28+
#include <string.h>
29+
30+
#include "py/mpstate.h"
31+
#include "unix_mphal.h"
32+
33+
#ifndef _WIN32
34+
#include <signal.h>
35+
36+
STATIC void sighandler(int signum) {
37+
if (signum == SIGINT) {
38+
mp_obj_exception_clear_traceback(MP_STATE_VM(keyboard_interrupt_obj));
39+
MP_STATE_VM(mp_pending_exception) = MP_STATE_VM(keyboard_interrupt_obj);
40+
// disable our handler so next we really die
41+
struct sigaction sa;
42+
sa.sa_handler = SIG_DFL;
43+
sigemptyset(&sa.sa_mask);
44+
sigaction(SIGINT, &sa, NULL);
45+
}
46+
}
47+
#endif
48+
49+
void mp_hal_set_interrupt_char(char c) {
50+
// configure terminal settings to (not) let ctrl-C through
51+
if (c == CHAR_CTRL_C) {
52+
#ifndef _WIN32
53+
// enable signal handler
54+
struct sigaction sa;
55+
sa.sa_handler = sighandler;
56+
sigemptyset(&sa.sa_mask);
57+
sigaction(SIGINT, &sa, NULL);
58+
#endif
59+
} else {
60+
#ifndef _WIN32
61+
// disable signal handler
62+
struct sigaction sa;
63+
sa.sa_handler = SIG_DFL;
64+
sigemptyset(&sa.sa_mask);
65+
sigaction(SIGINT, &sa, NULL);
66+
#endif
67+
}
68+
}
69+
70+
int mp_hal_stdin_rx_chr(void) {
71+
unsigned char c;
72+
int ret = read(0, &c, 1);
73+
if (ret == 0) {
74+
c = 4; // EOF, ctrl-D
75+
} else if (c == '\n') {
76+
c = '\r';
77+
}
78+
return c;
79+
}
80+
81+
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
82+
int ret = write(1, str, len);
83+
(void)ret; // to suppress compiler warning
84+
}
85+
86+
// cooked is same as uncooked because they terminal does some postprocessing
87+
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
88+
mp_hal_stdout_tx_strn(str, len);
89+
}
90+
91+
void mp_hal_stdout_tx_str(const char *str) {
92+
mp_hal_stdout_tx_strn(str, strlen(str));
93+
}

unix/unix_mphal.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef CHAR_CTRL_C
28+
#define CHAR_CTRL_C (3)
29+
#endif
30+
31+
void mp_hal_set_interrupt_char(char c);
32+
33+
int mp_hal_stdin_rx_chr(void);
34+
void mp_hal_stdout_tx_str(const char *str);
35+
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
36+
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len);

windows/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SRC_C = \
3131
unix/main.c \
3232
unix/file.c \
3333
unix/input.c \
34+
unix/unix_mphal.c \
3435
unix/modos.c \
3536
unix/modtime.c \
3637
unix/gccollect.c \

0 commit comments

Comments
 (0)