uos_dupterm.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 Paul Sokolovsky
  7. * Copyright (c) 2017 Damien P. George
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <string.h>
  28. #include "py/mpconfig.h"
  29. #include "py/runtime.h"
  30. #include "py/objtuple.h"
  31. #include "py/objarray.h"
  32. #include "py/stream.h"
  33. #include "lib/utils/interrupt_char.h"
  34. #if MICROPY_PY_OS_DUPTERM
  35. void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) {
  36. mp_obj_t term = MP_STATE_VM(dupterm_objs[dupterm_idx]);
  37. MP_STATE_VM(dupterm_objs[dupterm_idx]) = MP_OBJ_NULL;
  38. mp_printf(&mp_plat_print, msg);
  39. if (exc != MP_OBJ_NULL) {
  40. mp_obj_print_exception(&mp_plat_print, exc);
  41. }
  42. nlr_buf_t nlr;
  43. if (nlr_push(&nlr) == 0) {
  44. mp_stream_close(term);
  45. nlr_pop();
  46. } else {
  47. // Ignore any errors during stream closing
  48. }
  49. }
  50. int mp_uos_dupterm_rx_chr(void) {
  51. for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
  52. if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
  53. continue;
  54. }
  55. nlr_buf_t nlr;
  56. if (nlr_push(&nlr) == 0) {
  57. mp_obj_t readinto_m[3];
  58. mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_readinto, readinto_m);
  59. readinto_m[2] = MP_STATE_VM(dupterm_arr_obj);
  60. mp_obj_t res = mp_call_method_n_kw(1, 0, readinto_m);
  61. if (res == mp_const_none) {
  62. nlr_pop();
  63. } else if (res == MP_OBJ_NEW_SMALL_INT(0)) {
  64. nlr_pop();
  65. mp_uos_deactivate(idx, "dupterm: EOF received, deactivating\n", MP_OBJ_NULL);
  66. } else {
  67. mp_buffer_info_t bufinfo;
  68. mp_get_buffer_raise(MP_STATE_VM(dupterm_arr_obj), &bufinfo, MP_BUFFER_READ);
  69. nlr_pop();
  70. if (*(byte*)bufinfo.buf == mp_interrupt_char) {
  71. // Signal keyboard interrupt to be raised as soon as the VM resumes
  72. mp_keyboard_interrupt();
  73. return -2;
  74. }
  75. return *(byte*)bufinfo.buf;
  76. }
  77. } else {
  78. mp_uos_deactivate(idx, "dupterm: Exception in read() method, deactivating: ", nlr.ret_val);
  79. }
  80. }
  81. // No chars available
  82. return -1;
  83. }
  84. void mp_uos_dupterm_tx_strn(const char *str, size_t len) {
  85. for (size_t idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
  86. if (MP_STATE_VM(dupterm_objs[idx]) == MP_OBJ_NULL) {
  87. continue;
  88. }
  89. nlr_buf_t nlr;
  90. if (nlr_push(&nlr) == 0) {
  91. mp_obj_t write_m[3];
  92. mp_load_method(MP_STATE_VM(dupterm_objs[idx]), MP_QSTR_write, write_m);
  93. mp_obj_array_t *arr = MP_OBJ_TO_PTR(MP_STATE_VM(dupterm_arr_obj));
  94. void *org_items = arr->items;
  95. arr->items = (void*)str;
  96. arr->len = len;
  97. write_m[2] = MP_STATE_VM(dupterm_arr_obj);
  98. mp_call_method_n_kw(1, 0, write_m);
  99. arr = MP_OBJ_TO_PTR(MP_STATE_VM(dupterm_arr_obj));
  100. arr->items = org_items;
  101. arr->len = 1;
  102. nlr_pop();
  103. } else {
  104. mp_uos_deactivate(idx, "dupterm: Exception in write() method, deactivating: ", nlr.ret_val);
  105. }
  106. }
  107. }
  108. STATIC mp_obj_t mp_uos_dupterm(size_t n_args, const mp_obj_t *args) {
  109. mp_int_t idx = 0;
  110. if (n_args == 2) {
  111. idx = mp_obj_get_int(args[1]);
  112. }
  113. if (idx < 0 || idx >= MICROPY_PY_OS_DUPTERM) {
  114. mp_raise_ValueError("invalid dupterm index");
  115. }
  116. mp_obj_t previous_obj = MP_STATE_VM(dupterm_objs[idx]);
  117. if (previous_obj == MP_OBJ_NULL) {
  118. previous_obj = mp_const_none;
  119. }
  120. if (args[0] == mp_const_none) {
  121. MP_STATE_VM(dupterm_objs[idx]) = MP_OBJ_NULL;
  122. } else {
  123. MP_STATE_VM(dupterm_objs[idx]) = args[0];
  124. if (MP_STATE_VM(dupterm_arr_obj) == MP_OBJ_NULL) {
  125. MP_STATE_VM(dupterm_arr_obj) = mp_obj_new_bytearray(1, "");
  126. }
  127. }
  128. return previous_obj;
  129. }
  130. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj, 1, 2, mp_uos_dupterm);
  131. #endif