| 1 | /* | 
|---|
| 2 | * This file is part of the MicroPython project, http://micropython.org/ | 
|---|
| 3 | * | 
|---|
| 4 | * The MIT License (MIT) | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c) 2013, 2014 Damien P. George | 
|---|
| 7 | * Copyright (c) 2014-2017 Paul Sokolovsky | 
|---|
| 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 |  | 
|---|
| 28 | #include <stdint.h> | 
|---|
| 29 | #include <stdbool.h> | 
|---|
| 30 | #include <stdio.h> | 
|---|
| 31 | #include <string.h> | 
|---|
| 32 | #include <stdlib.h> | 
|---|
| 33 | #include <stdarg.h> | 
|---|
| 34 | #include <unistd.h> | 
|---|
| 35 | #include <ctype.h> | 
|---|
| 36 | #include <sys/stat.h> | 
|---|
| 37 | #include <sys/types.h> | 
|---|
| 38 | #include <errno.h> | 
|---|
| 39 | #include <signal.h> | 
|---|
| 40 |  | 
|---|
| 41 | #include "py/compile.h" | 
|---|
| 42 | #include "py/runtime.h" | 
|---|
| 43 | #include "py/builtin.h" | 
|---|
| 44 | #include "py/repl.h" | 
|---|
| 45 | #include "py/gc.h" | 
|---|
| 46 | #include "py/stackctrl.h" | 
|---|
| 47 | #include "py/mphal.h" | 
|---|
| 48 | #include "py/mpthread.h" | 
|---|
| 49 | #include "extmod/misc.h" | 
|---|
| 50 | #include "extmod/vfs.h" | 
|---|
| 51 | #include "extmod/vfs_posix.h" | 
|---|
| 52 | #include "genhdr/mpversion.h" | 
|---|
| 53 | #include "input.h" | 
|---|
| 54 |  | 
|---|
| 55 | // Command line options, with their defaults | 
|---|
| 56 | STATIC bool compile_only = false; | 
|---|
| 57 | STATIC uint emit_opt = MP_EMIT_OPT_NONE; | 
|---|
| 58 |  | 
|---|
| 59 | #if MICROPY_ENABLE_GC | 
|---|
| 60 | // Heap size of GC heap (if enabled) | 
|---|
| 61 | // Make it larger on a 64 bit machine, because pointers are larger. | 
|---|
| 62 | long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4); | 
|---|
| 63 | #endif | 
|---|
| 64 |  | 
|---|
| 65 | STATIC void stderr_print_strn(void *env, const char *str, size_t len) { | 
|---|
| 66 | (void)env; | 
|---|
| 67 | ssize_t ret; | 
|---|
| 68 | MP_HAL_RETRY_SYSCALL(ret, write(STDERR_FILENO, str, len), {}); | 
|---|
| 69 | mp_uos_dupterm_tx_strn(str, len); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | const mp_print_t mp_stderr_print = {NULL, stderr_print_strn}; | 
|---|
| 73 |  | 
|---|
| 74 | #define FORCED_EXIT (0x100) | 
|---|
| 75 | // If exc is SystemExit, return value where FORCED_EXIT bit set, | 
|---|
| 76 | // and lower 8 bits are SystemExit value. For all other exceptions, | 
|---|
| 77 | // return 1. | 
|---|
| 78 | STATIC int handle_uncaught_exception(mp_obj_base_t *exc) { | 
|---|
| 79 | // check for SystemExit | 
|---|
| 80 | if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(exc->type), MP_OBJ_FROM_PTR(&mp_type_SystemExit))) { | 
|---|
| 81 | // None is an exit value of 0; an int is its value; anything else is 1 | 
|---|
| 82 | mp_obj_t exit_val = mp_obj_exception_get_value(MP_OBJ_FROM_PTR(exc)); | 
|---|
| 83 | mp_int_t val = 0; | 
|---|
| 84 | if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) { | 
|---|
| 85 | val = 1; | 
|---|
| 86 | } | 
|---|
| 87 | return FORCED_EXIT | (val & 255); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | // Report all other exceptions | 
|---|
| 91 | mp_obj_print_exception(&mp_stderr_print, MP_OBJ_FROM_PTR(exc)); | 
|---|
| 92 | return 1; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | #define LEX_SRC_STR (1) | 
|---|
| 96 | #define LEX_SRC_VSTR (2) | 
|---|
| 97 | #define LEX_SRC_FILENAME (3) | 
|---|
| 98 | #define LEX_SRC_STDIN (4) | 
|---|
| 99 |  | 
|---|
| 100 | // Returns standard error codes: 0 for success, 1 for all other errors, | 
|---|
| 101 | // except if FORCED_EXIT bit is set then script raised SystemExit and the | 
|---|
| 102 | // value of the exit is in the lower 8 bits of the return value | 
|---|
| 103 | STATIC int execute_from_lexer(int source_kind, const void *source, mp_parse_input_kind_t input_kind, bool is_repl) { | 
|---|
| 104 | mp_hal_set_interrupt_char(CHAR_CTRL_C); | 
|---|
| 105 |  | 
|---|
| 106 | nlr_buf_t nlr; | 
|---|
| 107 | if (nlr_push(&nlr) == 0) { | 
|---|
| 108 | // create lexer based on source kind | 
|---|
| 109 | mp_lexer_t *lex; | 
|---|
| 110 | if (source_kind == LEX_SRC_STR) { | 
|---|
| 111 | const char *line = source; | 
|---|
| 112 | lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false); | 
|---|
| 113 | } else if (source_kind == LEX_SRC_VSTR) { | 
|---|
| 114 | const vstr_t *vstr = source; | 
|---|
| 115 | lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, vstr->buf, vstr->len, false); | 
|---|
| 116 | } else if (source_kind == LEX_SRC_FILENAME) { | 
|---|
| 117 | lex = mp_lexer_new_from_file((const char *)source); | 
|---|
| 118 | } else { // LEX_SRC_STDIN | 
|---|
| 119 | lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, 0, false); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | qstr source_name = lex->source_name; | 
|---|
| 123 |  | 
|---|
| 124 | #if MICROPY_PY___FILE__ | 
|---|
| 125 | if (input_kind == MP_PARSE_FILE_INPUT) { | 
|---|
| 126 | mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); | 
|---|
| 127 | } | 
|---|
| 128 | #endif | 
|---|
| 129 |  | 
|---|
| 130 | mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); | 
|---|
| 131 |  | 
|---|
| 132 | #if defined(MICROPY_UNIX_COVERAGE) | 
|---|
| 133 | // allow to print the parse tree in the coverage build | 
|---|
| 134 | if (mp_verbose_flag >= 3) { | 
|---|
| 135 | printf( "----------------\n"); | 
|---|
| 136 | mp_parse_node_print(&mp_plat_print, parse_tree.root, 0); | 
|---|
| 137 | printf( "----------------\n"); | 
|---|
| 138 | } | 
|---|
| 139 | #endif | 
|---|
| 140 |  | 
|---|
| 141 | mp_obj_t module_fun = mp_compile(&parse_tree, source_name, is_repl); | 
|---|
| 142 |  | 
|---|
| 143 | if (!compile_only) { | 
|---|
| 144 | // execute it | 
|---|
| 145 | mp_call_function_0(module_fun); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | mp_hal_set_interrupt_char(-1); | 
|---|
| 149 | mp_handle_pending(true); | 
|---|
| 150 | nlr_pop(); | 
|---|
| 151 | return 0; | 
|---|
| 152 |  | 
|---|
| 153 | } else { | 
|---|
| 154 | // uncaught exception | 
|---|
| 155 | mp_hal_set_interrupt_char(-1); | 
|---|
| 156 | mp_handle_pending(false); | 
|---|
| 157 | return handle_uncaught_exception(nlr.ret_val); | 
|---|
| 158 | } | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | #if MICROPY_USE_READLINE == 1 | 
|---|
| 162 | #include "lib/mp-readline/readline.h" | 
|---|
| 163 | #else | 
|---|
| 164 | STATIC char *strjoin(const char *s1, int sep_char, const char *s2) { | 
|---|
| 165 | int l1 = strlen(s1); | 
|---|
| 166 | int l2 = strlen(s2); | 
|---|
| 167 | char *s = malloc(l1 + l2 + 2); | 
|---|
| 168 | memcpy(s, s1, l1); | 
|---|
| 169 | if (sep_char != 0) { | 
|---|
| 170 | s[l1] = sep_char; | 
|---|
| 171 | l1 += 1; | 
|---|
| 172 | } | 
|---|
| 173 | memcpy(s + l1, s2, l2); | 
|---|
| 174 | s[l1 + l2] = 0; | 
|---|
| 175 | return s; | 
|---|
| 176 | } | 
|---|
| 177 | #endif | 
|---|
| 178 |  | 
|---|
| 179 | STATIC int do_repl(void) { | 
|---|
| 180 | mp_hal_stdout_tx_str( "MicroPython "MICROPY_GIT_TAG " on "MICROPY_BUILD_DATE "; " | 
|---|
| 181 | MICROPY_PY_SYS_PLATFORM " version\nUse Ctrl-D to exit, Ctrl-E for paste mode\n"); | 
|---|
| 182 |  | 
|---|
| 183 | #if MICROPY_USE_READLINE == 1 | 
|---|
| 184 |  | 
|---|
| 185 | // use MicroPython supplied readline | 
|---|
| 186 |  | 
|---|
| 187 | vstr_t line; | 
|---|
| 188 | vstr_init(&line, 16); | 
|---|
| 189 | for (;;) { | 
|---|
| 190 | mp_hal_stdio_mode_raw(); | 
|---|
| 191 |  | 
|---|
| 192 | input_restart: | 
|---|
| 193 | vstr_reset(&line); | 
|---|
| 194 | int ret = readline(&line, ">>> "); | 
|---|
| 195 | mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT; | 
|---|
| 196 |  | 
|---|
| 197 | if (ret == CHAR_CTRL_C) { | 
|---|
| 198 | // cancel input | 
|---|
| 199 | mp_hal_stdout_tx_str( "\r\n"); | 
|---|
| 200 | goto input_restart; | 
|---|
| 201 | } else if (ret == CHAR_CTRL_D) { | 
|---|
| 202 | // EOF | 
|---|
| 203 | printf( "\n"); | 
|---|
| 204 | mp_hal_stdio_mode_orig(); | 
|---|
| 205 | vstr_clear(&line); | 
|---|
| 206 | return 0; | 
|---|
| 207 | } else if (ret == CHAR_CTRL_E) { | 
|---|
| 208 | // paste mode | 
|---|
| 209 | mp_hal_stdout_tx_str( "\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\n=== "); | 
|---|
| 210 | vstr_reset(&line); | 
|---|
| 211 | for (;;) { | 
|---|
| 212 | char c = mp_hal_stdin_rx_chr(); | 
|---|
| 213 | if (c == CHAR_CTRL_C) { | 
|---|
| 214 | // cancel everything | 
|---|
| 215 | mp_hal_stdout_tx_str( "\n"); | 
|---|
| 216 | goto input_restart; | 
|---|
| 217 | } else if (c == CHAR_CTRL_D) { | 
|---|
| 218 | // end of input | 
|---|
| 219 | mp_hal_stdout_tx_str( "\n"); | 
|---|
| 220 | break; | 
|---|
| 221 | } else { | 
|---|
| 222 | // add char to buffer and echo | 
|---|
| 223 | vstr_add_byte(&line, c); | 
|---|
| 224 | if (c == '\r') { | 
|---|
| 225 | mp_hal_stdout_tx_str( "\n=== "); | 
|---|
| 226 | } else { | 
|---|
| 227 | mp_hal_stdout_tx_strn(&c, 1); | 
|---|
| 228 | } | 
|---|
| 229 | } | 
|---|
| 230 | } | 
|---|
| 231 | parse_input_kind = MP_PARSE_FILE_INPUT; | 
|---|
| 232 | } else if (line.len == 0) { | 
|---|
| 233 | if (ret != 0) { | 
|---|
| 234 | printf( "\n"); | 
|---|
| 235 | } | 
|---|
| 236 | goto input_restart; | 
|---|
| 237 | } else { | 
|---|
| 238 | // got a line with non-zero length, see if it needs continuing | 
|---|
| 239 | while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) { | 
|---|
| 240 | vstr_add_byte(&line, '\n'); | 
|---|
| 241 | ret = readline(&line, "... "); | 
|---|
| 242 | if (ret == CHAR_CTRL_C) { | 
|---|
| 243 | // cancel everything | 
|---|
| 244 | printf( "\n"); | 
|---|
| 245 | goto input_restart; | 
|---|
| 246 | } else if (ret == CHAR_CTRL_D) { | 
|---|
| 247 | // stop entering compound statement | 
|---|
| 248 | break; | 
|---|
| 249 | } | 
|---|
| 250 | } | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | mp_hal_stdio_mode_orig(); | 
|---|
| 254 |  | 
|---|
| 255 | ret = execute_from_lexer(LEX_SRC_VSTR, &line, parse_input_kind, true); | 
|---|
| 256 | if (ret & FORCED_EXIT) { | 
|---|
| 257 | return ret; | 
|---|
| 258 | } | 
|---|
| 259 | } | 
|---|
| 260 |  | 
|---|
| 261 | #else | 
|---|
| 262 |  | 
|---|
| 263 | // use simple readline | 
|---|
| 264 |  | 
|---|
| 265 | for (;;) { | 
|---|
| 266 | char *line = prompt( ">>> "); | 
|---|
| 267 | if (line == NULL) { | 
|---|
| 268 | // EOF | 
|---|
| 269 | return 0; | 
|---|
| 270 | } | 
|---|
| 271 | while (mp_repl_continue_with_input(line)) { | 
|---|
| 272 | char *line2 = prompt( "... "); | 
|---|
| 273 | if (line2 == NULL) { | 
|---|
| 274 | break; | 
|---|
| 275 | } | 
|---|
| 276 | char *line3 = strjoin(line, '\n', line2); | 
|---|
| 277 | free(line); | 
|---|
| 278 | free(line2); | 
|---|
| 279 | line = line3; | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | int ret = execute_from_lexer(LEX_SRC_STR, line, MP_PARSE_SINGLE_INPUT, true); | 
|---|
| 283 | if (ret & FORCED_EXIT) { | 
|---|
| 284 | return ret; | 
|---|
| 285 | } | 
|---|
| 286 | free(line); | 
|---|
| 287 | } | 
|---|
| 288 |  | 
|---|
| 289 | #endif | 
|---|
| 290 | } | 
|---|
| 291 |  | 
|---|
| 292 | STATIC int do_file(const char *file) { | 
|---|
| 293 | return execute_from_lexer(LEX_SRC_FILENAME, file, MP_PARSE_FILE_INPUT, false); | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | STATIC int do_str(const char *str) { | 
|---|
| 297 | return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false); | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | STATIC void print_help(char **argv) { | 
|---|
| 301 | printf( | 
|---|
| 302 | "usage: %s [<opts>] [-X <implopt>] [-c <command> | -m <module> | <filename>]\n" | 
|---|
| 303 | "Options:\n" | 
|---|
| 304 | "-h : print this help message\n" | 
|---|
| 305 | "-i : enable inspection via REPL after running command/module/file\n" | 
|---|
| 306 | #if MICROPY_DEBUG_PRINTERS | 
|---|
| 307 | "-v : verbose (trace various operations); can be multiple\n" | 
|---|
| 308 | #endif | 
|---|
| 309 | "-O[N] : apply bytecode optimizations of level N\n" | 
|---|
| 310 | "\n" | 
|---|
| 311 | "Implementation specific options (-X):\n", argv[0] | 
|---|
| 312 | ); | 
|---|
| 313 | int impl_opts_cnt = 0; | 
|---|
| 314 | printf( | 
|---|
| 315 | "  compile-only                 -- parse and compile only\n" | 
|---|
| 316 | #if MICROPY_EMIT_NATIVE | 
|---|
| 317 | "  emit={bytecode,native,viper} -- set the default code emitter\n" | 
|---|
| 318 | #else | 
|---|
| 319 | "  emit=bytecode                -- set the default code emitter\n" | 
|---|
| 320 | #endif | 
|---|
| 321 | ); | 
|---|
| 322 | impl_opts_cnt++; | 
|---|
| 323 | #if MICROPY_ENABLE_GC | 
|---|
| 324 | printf( | 
|---|
| 325 | "  heapsize=<n>[w][K|M] -- set the heap size for the GC (default %ld)\n" | 
|---|
| 326 | , heap_size); | 
|---|
| 327 | impl_opts_cnt++; | 
|---|
| 328 | #endif | 
|---|
| 329 |  | 
|---|
| 330 | if (impl_opts_cnt == 0) { | 
|---|
| 331 | printf( "  (none)\n"); | 
|---|
| 332 | } | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | STATIC int invalid_args(void) { | 
|---|
| 336 | fprintf(stderr, "Invalid command line arguments. Use -h option for help.\n"); | 
|---|
| 337 | return 1; | 
|---|
| 338 | } | 
|---|
| 339 |  | 
|---|
| 340 | // Process options which set interpreter init options | 
|---|
| 341 | STATIC void pre_process_options(int argc, char **argv) { | 
|---|
| 342 | for (int a = 1; a < argc; a++) { | 
|---|
| 343 | if (argv[a][0] == '-') { | 
|---|
| 344 | if (strcmp(argv[a], "-c") == 0 || strcmp(argv[a], "-m") == 0) { | 
|---|
| 345 | break; // Everything after this is a command/module and arguments for it | 
|---|
| 346 | } | 
|---|
| 347 | if (strcmp(argv[a], "-h") == 0) { | 
|---|
| 348 | print_help(argv); | 
|---|
| 349 | exit(0); | 
|---|
| 350 | } | 
|---|
| 351 | if (strcmp(argv[a], "-X") == 0) { | 
|---|
| 352 | if (a + 1 >= argc) { | 
|---|
| 353 | exit(invalid_args()); | 
|---|
| 354 | } | 
|---|
| 355 | if (0) { | 
|---|
| 356 | } else if (strcmp(argv[a + 1], "compile-only") == 0) { | 
|---|
| 357 | compile_only = true; | 
|---|
| 358 | } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) { | 
|---|
| 359 | emit_opt = MP_EMIT_OPT_BYTECODE; | 
|---|
| 360 | #if MICROPY_EMIT_NATIVE | 
|---|
| 361 | } else if (strcmp(argv[a + 1], "emit=native") == 0) { | 
|---|
| 362 | emit_opt = MP_EMIT_OPT_NATIVE_PYTHON; | 
|---|
| 363 | } else if (strcmp(argv[a + 1], "emit=viper") == 0) { | 
|---|
| 364 | emit_opt = MP_EMIT_OPT_VIPER; | 
|---|
| 365 | #endif | 
|---|
| 366 | #if MICROPY_ENABLE_GC | 
|---|
| 367 | } else if (strncmp(argv[a + 1], "heapsize=", sizeof( "heapsize=") - 1) == 0) { | 
|---|
| 368 | char *end; | 
|---|
| 369 | heap_size = strtol(argv[a + 1] + sizeof( "heapsize=") - 1, &end, 0); | 
|---|
| 370 | // Don't bring unneeded libc dependencies like tolower() | 
|---|
| 371 | // If there's 'w' immediately after number, adjust it for | 
|---|
| 372 | // target word size. Note that it should be *before* size | 
|---|
| 373 | // suffix like K or M, to avoid confusion with kilowords, | 
|---|
| 374 | // etc. the size is still in bytes, just can be adjusted | 
|---|
| 375 | // for word size (taking 32bit as baseline). | 
|---|
| 376 | bool word_adjust = false; | 
|---|
| 377 | if ((*end | 0x20) == 'w') { | 
|---|
| 378 | word_adjust = true; | 
|---|
| 379 | end++; | 
|---|
| 380 | } | 
|---|
| 381 | if ((*end | 0x20) == 'k') { | 
|---|
| 382 | heap_size *= 1024; | 
|---|
| 383 | } else if ((*end | 0x20) == 'm') { | 
|---|
| 384 | heap_size *= 1024 * 1024; | 
|---|
| 385 | } else { | 
|---|
| 386 | // Compensate for ++ below | 
|---|
| 387 | --end; | 
|---|
| 388 | } | 
|---|
| 389 | if (*++end != 0) { | 
|---|
| 390 | goto invalid_arg; | 
|---|
| 391 | } | 
|---|
| 392 | if (word_adjust) { | 
|---|
| 393 | heap_size = heap_size * MP_BYTES_PER_OBJ_WORD / 4; | 
|---|
| 394 | } | 
|---|
| 395 | // If requested size too small, we'll crash anyway | 
|---|
| 396 | if (heap_size < 700) { | 
|---|
| 397 | goto invalid_arg; | 
|---|
| 398 | } | 
|---|
| 399 | #endif | 
|---|
| 400 | } else { | 
|---|
| 401 | invalid_arg: | 
|---|
| 402 | exit(invalid_args()); | 
|---|
| 403 | } | 
|---|
| 404 | a++; | 
|---|
| 405 | } | 
|---|
| 406 | } else { | 
|---|
| 407 | break; // Not an option but a file | 
|---|
| 408 | } | 
|---|
| 409 | } | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | STATIC void set_sys_argv(char *argv[], int argc, int start_arg) { | 
|---|
| 413 | for (int i = start_arg; i < argc; i++) { | 
|---|
| 414 | mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i]))); | 
|---|
| 415 | } | 
|---|
| 416 | } | 
|---|
| 417 |  | 
|---|
| 418 | #ifdef _WIN32 | 
|---|
| 419 | #define PATHLIST_SEP_CHAR ';' | 
|---|
| 420 | #else | 
|---|
| 421 | #define PATHLIST_SEP_CHAR ':' | 
|---|
| 422 | #endif | 
|---|
| 423 |  | 
|---|
| 424 | MP_NOINLINE int main_(int argc, char **argv); | 
|---|
| 425 |  | 
|---|
| 426 | int main(int argc, char **argv) { | 
|---|
| 427 | #if MICROPY_PY_THREAD | 
|---|
| 428 | mp_thread_init(); | 
|---|
| 429 | #endif | 
|---|
| 430 | // We should capture stack top ASAP after start, and it should be | 
|---|
| 431 | // captured guaranteedly before any other stack variables are allocated. | 
|---|
| 432 | // For this, actual main (renamed main_) should not be inlined into | 
|---|
| 433 | // this function. main_() itself may have other functions inlined (with | 
|---|
| 434 | // their own stack variables), that's why we need this main/main_ split. | 
|---|
| 435 | mp_stack_ctrl_init(); | 
|---|
| 436 | return main_(argc, argv); | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 | MP_NOINLINE int main_(int argc, char **argv) { | 
|---|
| 440 | #ifdef SIGPIPE | 
|---|
| 441 | // Do not raise SIGPIPE, instead return EPIPE. Otherwise, e.g. writing | 
|---|
| 442 | // to peer-closed socket will lead to sudden termination of MicroPython | 
|---|
| 443 | // process. SIGPIPE is particularly nasty, because unix shell doesn't | 
|---|
| 444 | // print anything for it, so the above looks like completely sudden and | 
|---|
| 445 | // silent termination for unknown reason. Ignoring SIGPIPE is also what | 
|---|
| 446 | // CPython does. Note that this may lead to problems using MicroPython | 
|---|
| 447 | // scripts as pipe filters, but again, that's what CPython does. So, | 
|---|
| 448 | // scripts which want to follow unix shell pipe semantics (where SIGPIPE | 
|---|
| 449 | // means "pipe was requested to terminate, it's not an error"), should | 
|---|
| 450 | // catch EPIPE themselves. | 
|---|
| 451 | signal(SIGPIPE, SIG_IGN); | 
|---|
| 452 | #endif | 
|---|
| 453 |  | 
|---|
| 454 | mp_stack_set_limit(40000 * (sizeof(void *) / 4)); | 
|---|
| 455 |  | 
|---|
| 456 | pre_process_options(argc, argv); | 
|---|
| 457 |  | 
|---|
| 458 | #if MICROPY_ENABLE_GC | 
|---|
| 459 | char *heap = malloc(heap_size); | 
|---|
| 460 | gc_init(heap, heap + heap_size); | 
|---|
| 461 | #endif | 
|---|
| 462 |  | 
|---|
| 463 | #if MICROPY_ENABLE_PYSTACK | 
|---|
| 464 | static mp_obj_t pystack[1024]; | 
|---|
| 465 | mp_pystack_init(pystack, &pystack[MP_ARRAY_SIZE(pystack)]); | 
|---|
| 466 | #endif | 
|---|
| 467 |  | 
|---|
| 468 | mp_init(); | 
|---|
| 469 |  | 
|---|
| 470 | #if MICROPY_EMIT_NATIVE | 
|---|
| 471 | // Set default emitter options | 
|---|
| 472 | MP_STATE_VM(default_emit_opt) = emit_opt; | 
|---|
| 473 | #else | 
|---|
| 474 | (void)emit_opt; | 
|---|
| 475 | #endif | 
|---|
| 476 |  | 
|---|
| 477 | #if MICROPY_VFS_POSIX | 
|---|
| 478 | { | 
|---|
| 479 | // Mount the host FS at the root of our internal VFS | 
|---|
| 480 | mp_obj_t args[2] = { | 
|---|
| 481 | mp_type_vfs_posix.make_new(&mp_type_vfs_posix, 0, 0, NULL), | 
|---|
| 482 | MP_OBJ_NEW_QSTR(MP_QSTR__slash_), | 
|---|
| 483 | }; | 
|---|
| 484 | mp_vfs_mount(2, args, (mp_map_t *)&mp_const_empty_map); | 
|---|
| 485 | MP_STATE_VM(vfs_cur) = MP_STATE_VM(vfs_mount_table); | 
|---|
| 486 | } | 
|---|
| 487 | #endif | 
|---|
| 488 |  | 
|---|
| 489 | char *home = getenv( "HOME"); | 
|---|
| 490 | char *path = getenv( "MICROPYPATH"); | 
|---|
| 491 | if (path == NULL) { | 
|---|
| 492 | #ifdef MICROPY_PY_SYS_PATH_DEFAULT | 
|---|
| 493 | path = MICROPY_PY_SYS_PATH_DEFAULT; | 
|---|
| 494 | #else | 
|---|
| 495 | path = "~/.micropython/lib:/usr/lib/micropython"; | 
|---|
| 496 | #endif | 
|---|
| 497 | } | 
|---|
| 498 | size_t path_num = 1; // [0] is for current dir (or base dir of the script) | 
|---|
| 499 | if (*path == PATHLIST_SEP_CHAR) { | 
|---|
| 500 | path_num++; | 
|---|
| 501 | } | 
|---|
| 502 | for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) { | 
|---|
| 503 | path_num++; | 
|---|
| 504 | if (p != NULL) { | 
|---|
| 505 | p++; | 
|---|
| 506 | } | 
|---|
| 507 | } | 
|---|
| 508 | mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_path), path_num); | 
|---|
| 509 | mp_obj_t *path_items; | 
|---|
| 510 | mp_obj_list_get(mp_sys_path, &path_num, &path_items); | 
|---|
| 511 | path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_); | 
|---|
| 512 | { | 
|---|
| 513 | char *p = path; | 
|---|
| 514 | for (mp_uint_t i = 1; i < path_num; i++) { | 
|---|
| 515 | char *p1 = strchr(p, PATHLIST_SEP_CHAR); | 
|---|
| 516 | if (p1 == NULL) { | 
|---|
| 517 | p1 = p + strlen(p); | 
|---|
| 518 | } | 
|---|
| 519 | if (p[0] == '~' && p[1] == '/' && home != NULL) { | 
|---|
| 520 | // Expand standalone ~ to $HOME | 
|---|
| 521 | int home_l = strlen(home); | 
|---|
| 522 | vstr_t vstr; | 
|---|
| 523 | vstr_init(&vstr, home_l + (p1 - p - 1) + 1); | 
|---|
| 524 | vstr_add_strn(&vstr, home, home_l); | 
|---|
| 525 | vstr_add_strn(&vstr, p + 1, p1 - p - 1); | 
|---|
| 526 | path_items[i] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); | 
|---|
| 527 | } else { | 
|---|
| 528 | path_items[i] = mp_obj_new_str_via_qstr(p, p1 - p); | 
|---|
| 529 | } | 
|---|
| 530 | p = p1 + 1; | 
|---|
| 531 | } | 
|---|
| 532 | } | 
|---|
| 533 |  | 
|---|
| 534 | mp_obj_list_init(MP_OBJ_TO_PTR(mp_sys_argv), 0); | 
|---|
| 535 |  | 
|---|
| 536 | #if defined(MICROPY_UNIX_COVERAGE) | 
|---|
| 537 | { | 
|---|
| 538 | MP_DECLARE_CONST_FUN_OBJ_0(extra_coverage_obj); | 
|---|
| 539 | MP_DECLARE_CONST_FUN_OBJ_0(extra_cpp_coverage_obj); | 
|---|
| 540 | mp_store_global(MP_QSTR_extra_coverage, MP_OBJ_FROM_PTR(&extra_coverage_obj)); | 
|---|
| 541 | mp_store_global(MP_QSTR_extra_cpp_coverage, MP_OBJ_FROM_PTR(&extra_cpp_coverage_obj)); | 
|---|
| 542 | } | 
|---|
| 543 | #endif | 
|---|
| 544 |  | 
|---|
| 545 | // Here is some example code to create a class and instance of that class. | 
|---|
| 546 | // First is the Python, then the C code. | 
|---|
| 547 | // | 
|---|
| 548 | // class TestClass: | 
|---|
| 549 | //     pass | 
|---|
| 550 | // test_obj = TestClass() | 
|---|
| 551 | // test_obj.attr = 42 | 
|---|
| 552 | // | 
|---|
| 553 | // mp_obj_t test_class_type, test_class_instance; | 
|---|
| 554 | // test_class_type = mp_obj_new_type(qstr_from_str("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0)); | 
|---|
| 555 | // mp_store_name(qstr_from_str("test_obj"), test_class_instance = mp_call_function_0(test_class_type)); | 
|---|
| 556 | // mp_store_attr(test_class_instance, qstr_from_str("attr"), mp_obj_new_int(42)); | 
|---|
| 557 |  | 
|---|
| 558 | /* | 
|---|
| 559 | printf("bytes:\n"); | 
|---|
| 560 | printf("    total %d\n", m_get_total_bytes_allocated()); | 
|---|
| 561 | printf("    cur   %d\n", m_get_current_bytes_allocated()); | 
|---|
| 562 | printf("    peak  %d\n", m_get_peak_bytes_allocated()); | 
|---|
| 563 | */ | 
|---|
| 564 |  | 
|---|
| 565 | const int NOTHING_EXECUTED = -2; | 
|---|
| 566 | int ret = NOTHING_EXECUTED; | 
|---|
| 567 | bool inspect = false; | 
|---|
| 568 | for (int a = 1; a < argc; a++) { | 
|---|
| 569 | if (argv[a][0] == '-') { | 
|---|
| 570 | if (strcmp(argv[a], "-i") == 0) { | 
|---|
| 571 | inspect = true; | 
|---|
| 572 | } else if (strcmp(argv[a], "-c") == 0) { | 
|---|
| 573 | if (a + 1 >= argc) { | 
|---|
| 574 | return invalid_args(); | 
|---|
| 575 | } | 
|---|
| 576 | set_sys_argv(argv, a + 1, a); // The -c becomes first item of sys.argv, as in CPython | 
|---|
| 577 | set_sys_argv(argv, argc, a + 2); // Then what comes after the command | 
|---|
| 578 | ret = do_str(argv[a + 1]); | 
|---|
| 579 | break; | 
|---|
| 580 | } else if (strcmp(argv[a], "-m") == 0) { | 
|---|
| 581 | if (a + 1 >= argc) { | 
|---|
| 582 | return invalid_args(); | 
|---|
| 583 | } | 
|---|
| 584 | mp_obj_t import_args[4]; | 
|---|
| 585 | import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1])); | 
|---|
| 586 | import_args[1] = import_args[2] = mp_const_none; | 
|---|
| 587 | // Ask __import__ to handle imported module specially - set its __name__ | 
|---|
| 588 | // to __main__, and also return this leaf module, not top-level package | 
|---|
| 589 | // containing it. | 
|---|
| 590 | import_args[3] = mp_const_false; | 
|---|
| 591 | // TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m : | 
|---|
| 592 | // "the first element of sys.argv will be the full path to | 
|---|
| 593 | // the module file (while the module file is being located, | 
|---|
| 594 | // the first element will be set to "-m")." | 
|---|
| 595 | set_sys_argv(argv, argc, a + 1); | 
|---|
| 596 |  | 
|---|
| 597 | mp_obj_t mod; | 
|---|
| 598 | nlr_buf_t nlr; | 
|---|
| 599 |  | 
|---|
| 600 | // Allocating subpkg_tried on the stack can lead to compiler warnings about this | 
|---|
| 601 | // variable being clobbered when nlr is implemented using setjmp/longjmp.  Its | 
|---|
| 602 | // value must be preserved across calls to setjmp/longjmp. | 
|---|
| 603 | static bool subpkg_tried; | 
|---|
| 604 | subpkg_tried = false; | 
|---|
| 605 |  | 
|---|
| 606 | reimport: | 
|---|
| 607 | if (nlr_push(&nlr) == 0) { | 
|---|
| 608 | mod = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args); | 
|---|
| 609 | nlr_pop(); | 
|---|
| 610 | } else { | 
|---|
| 611 | // uncaught exception | 
|---|
| 612 | return handle_uncaught_exception(nlr.ret_val) & 0xff; | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 | if (mp_obj_is_package(mod) && !subpkg_tried) { | 
|---|
| 616 | subpkg_tried = true; | 
|---|
| 617 | vstr_t vstr; | 
|---|
| 618 | int len = strlen(argv[a + 1]); | 
|---|
| 619 | vstr_init(&vstr, len + sizeof( ".__main__")); | 
|---|
| 620 | vstr_add_strn(&vstr, argv[a + 1], len); | 
|---|
| 621 | vstr_add_strn(&vstr, ".__main__", sizeof( ".__main__") - 1); | 
|---|
| 622 | import_args[0] = mp_obj_new_str_from_vstr(&mp_type_str, &vstr); | 
|---|
| 623 | goto reimport; | 
|---|
| 624 | } | 
|---|
| 625 |  | 
|---|
| 626 | ret = 0; | 
|---|
| 627 | break; | 
|---|
| 628 | } else if (strcmp(argv[a], "-X") == 0) { | 
|---|
| 629 | a += 1; | 
|---|
| 630 | #if MICROPY_DEBUG_PRINTERS | 
|---|
| 631 | } else if (strcmp(argv[a], "-v") == 0) { | 
|---|
| 632 | mp_verbose_flag++; | 
|---|
| 633 | #endif | 
|---|
| 634 | } else if (strncmp(argv[a], "-O", 2) == 0) { | 
|---|
| 635 | if (unichar_isdigit(argv[a][2])) { | 
|---|
| 636 | MP_STATE_VM(mp_optimise_value) = argv[a][2] & 0xf; | 
|---|
| 637 | } else { | 
|---|
| 638 | MP_STATE_VM(mp_optimise_value) = 0; | 
|---|
| 639 | for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++) {; | 
|---|
| 640 | } | 
|---|
| 641 | } | 
|---|
| 642 | } else { | 
|---|
| 643 | return invalid_args(); | 
|---|
| 644 | } | 
|---|
| 645 | } else { | 
|---|
| 646 | char *pathbuf = malloc(PATH_MAX); | 
|---|
| 647 | char *basedir = realpath(argv[a], pathbuf); | 
|---|
| 648 | if (basedir == NULL) { | 
|---|
| 649 | mp_printf(&mp_stderr_print, "%s: can't open file '%s': [Errno %d] %s\n", argv[0], argv[a], errno, strerror(errno)); | 
|---|
| 650 | // CPython exits with 2 in such case | 
|---|
| 651 | ret = 2; | 
|---|
| 652 | break; | 
|---|
| 653 | } | 
|---|
| 654 |  | 
|---|
| 655 | // Set base dir of the script as first entry in sys.path | 
|---|
| 656 | char *p = strrchr(basedir, '/'); | 
|---|
| 657 | path_items[0] = mp_obj_new_str_via_qstr(basedir, p - basedir); | 
|---|
| 658 | free(pathbuf); | 
|---|
| 659 |  | 
|---|
| 660 | set_sys_argv(argv, argc, a); | 
|---|
| 661 | ret = do_file(argv[a]); | 
|---|
| 662 | break; | 
|---|
| 663 | } | 
|---|
| 664 | } | 
|---|
| 665 |  | 
|---|
| 666 | const char *inspect_env = getenv( "MICROPYINSPECT"); | 
|---|
| 667 | if (inspect_env && inspect_env[0] != '\0') { | 
|---|
| 668 | inspect = true; | 
|---|
| 669 | } | 
|---|
| 670 | if (ret == NOTHING_EXECUTED || inspect) { | 
|---|
| 671 | if (isatty(0) || inspect) { | 
|---|
| 672 | prompt_read_history(); | 
|---|
| 673 | ret = do_repl(); | 
|---|
| 674 | prompt_write_history(); | 
|---|
| 675 | } else { | 
|---|
| 676 | ret = execute_from_lexer(LEX_SRC_STDIN, NULL, MP_PARSE_FILE_INPUT, false); | 
|---|
| 677 | } | 
|---|
| 678 | } | 
|---|
| 679 |  | 
|---|
| 680 | #if MICROPY_PY_SYS_SETTRACE | 
|---|
| 681 | MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL; | 
|---|
| 682 | #endif | 
|---|
| 683 |  | 
|---|
| 684 | #if MICROPY_PY_SYS_ATEXIT | 
|---|
| 685 | // Beware, the sys.settrace callback should be disabled before running sys.atexit. | 
|---|
| 686 | if (mp_obj_is_callable(MP_STATE_VM(sys_exitfunc))) { | 
|---|
| 687 | mp_call_function_0(MP_STATE_VM(sys_exitfunc)); | 
|---|
| 688 | } | 
|---|
| 689 | #endif | 
|---|
| 690 |  | 
|---|
| 691 | #if MICROPY_PY_MICROPYTHON_MEM_INFO | 
|---|
| 692 | if (mp_verbose_flag) { | 
|---|
| 693 | mp_micropython_mem_info(0, NULL); | 
|---|
| 694 | } | 
|---|
| 695 | #endif | 
|---|
| 696 |  | 
|---|
| 697 | #if MICROPY_PY_BLUETOOTH | 
|---|
| 698 | void mp_bluetooth_deinit(void); | 
|---|
| 699 | mp_bluetooth_deinit(); | 
|---|
| 700 | #endif | 
|---|
| 701 |  | 
|---|
| 702 | #if MICROPY_PY_THREAD | 
|---|
| 703 | mp_thread_deinit(); | 
|---|
| 704 | #endif | 
|---|
| 705 |  | 
|---|
| 706 | #if defined(MICROPY_UNIX_COVERAGE) | 
|---|
| 707 | gc_sweep_all(); | 
|---|
| 708 | #endif | 
|---|
| 709 |  | 
|---|
| 710 | mp_deinit(); | 
|---|
| 711 |  | 
|---|
| 712 | #if MICROPY_ENABLE_GC && !defined(NDEBUG) | 
|---|
| 713 | // We don't really need to free memory since we are about to exit the | 
|---|
| 714 | // process, but doing so helps to find memory leaks. | 
|---|
| 715 | free(heap); | 
|---|
| 716 | #endif | 
|---|
| 717 |  | 
|---|
| 718 | // printf("total bytes = %d\n", m_get_total_bytes_allocated()); | 
|---|
| 719 | return ret & 0xff; | 
|---|
| 720 | } | 
|---|
| 721 |  | 
|---|
| 722 | #if !MICROPY_VFS | 
|---|
| 723 | uint mp_import_stat(const char *path) { | 
|---|
| 724 | struct stat st; | 
|---|
| 725 | if (stat(path, &st) == 0) { | 
|---|
| 726 | if (S_ISDIR(st.st_mode)) { | 
|---|
| 727 | return MP_IMPORT_STAT_DIR; | 
|---|
| 728 | } else if (S_ISREG(st.st_mode)) { | 
|---|
| 729 | return MP_IMPORT_STAT_FILE; | 
|---|
| 730 | } | 
|---|
| 731 | } | 
|---|
| 732 | return MP_IMPORT_STAT_NO_EXIST; | 
|---|
| 733 | } | 
|---|
| 734 |  | 
|---|
| 735 | #if MICROPY_PY_IO | 
|---|
| 736 | // Factory function for I/O stream classes, only needed if generic VFS subsystem isn't used. | 
|---|
| 737 | // Note: buffering and encoding are currently ignored. | 
|---|
| 738 | mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kwargs) { | 
|---|
| 739 | enum { ARG_file, ARG_mode }; | 
|---|
| 740 | STATIC const mp_arg_t allowed_args[] = { | 
|---|
| 741 | { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_NONE} }, | 
|---|
| 742 | { MP_QSTR_mode, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_QSTR(MP_QSTR_r)} }, | 
|---|
| 743 | { MP_QSTR_buffering, MP_ARG_INT, {.u_int = -1} }, | 
|---|
| 744 | { MP_QSTR_encoding, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, | 
|---|
| 745 | }; | 
|---|
| 746 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; | 
|---|
| 747 | mp_arg_parse_all(n_args, pos_args, kwargs, MP_ARRAY_SIZE(allowed_args), allowed_args, args); | 
|---|
| 748 | return mp_vfs_posix_file_open(&mp_type_textio, args[ARG_file].u_obj, args[ARG_mode].u_obj); | 
|---|
| 749 | } | 
|---|
| 750 | MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); | 
|---|
| 751 | #endif | 
|---|
| 752 | #endif | 
|---|
| 753 |  | 
|---|
| 754 | void nlr_jump_fail(void *val) { | 
|---|
| 755 | fprintf(stderr, "FATAL: uncaught NLR %p\n", val); | 
|---|
| 756 | exit(1); | 
|---|
| 757 | } | 
|---|
| 758 |  | 
|---|