|
| 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 | +} |
0 commit comments