blob: 992ea1fec566f012dea007e98681c13ab0823743 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 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
Damien429d7192013-10-04 19:53:11 +010027#include <stdint.h>
xbec93a2212014-03-15 23:24:34 -070028#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +010029#include <stdio.h>
30#include <string.h>
Edd Barrett8146aea2014-01-01 23:14:36 +000031#include <stdlib.h>
Paul Sokolovsky44739e22014-02-16 18:11:42 +020032#include <stdarg.h>
xbec93a2212014-03-15 23:24:34 -070033#include <sys/stat.h>
34#include <sys/types.h>
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +030035#include <errno.h>
Damien429d7192013-10-04 19:53:11 +010036
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030037#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010038#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010039#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000040#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010041#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010042#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010043#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000044#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000045#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000046#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000047#include "runtime0.h"
48#include "runtime.h"
Damien92c06562013-10-22 22:32:27 +010049#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020050#include "gc.h"
Damien Georged553be52014-04-17 18:03:27 +010051#include "genhdr/py-version.h"
Dave Hylands117c46d2014-05-07 07:15:00 -070052#include "input.h"
Damien429d7192013-10-04 19:53:11 +010053
Damien Georgef22626e2014-04-10 11:30:35 +010054// Command line options, with their defaults
55bool compile_only = false;
Damien George65cad122014-04-06 11:48:15 +010056uint emit_opt = MP_EMIT_OPT_NONE;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030057uint mp_verbose_flag;
Damien George65cad122014-04-06 11:48:15 +010058
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020059#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020060// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010061// Make it larger on a 64 bit machine, because pointers are larger.
62long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020063#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020064
65// Stack top at the start of program
Paul Sokolovsky914bcf12014-05-31 02:34:39 +030066char *stack_top;
Paul Sokolovskye7db8172014-02-11 00:44:37 +020067
Paul Sokolovsky9945f332014-02-08 21:10:18 +020068void microsocket_init();
Paul Sokolovskya9459bc2014-02-02 00:57:06 +020069void time_init();
Paul Sokolovsky60a9fac2014-01-29 00:24:00 +020070void ffi_init();
Paul Sokolovskye0e79ae2014-01-08 02:52:20 +020071
stijn9e040b72014-05-10 19:42:06 +020072// returns standard error codes: 0 for success, 1 for all other errors
73STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
Damien George136f6752014-01-07 14:54:15 +000074 if (lex == NULL) {
stijn9e040b72014-05-10 19:42:06 +020075 return 1;
Damien George136f6752014-01-07 14:54:15 +000076 }
77
78 if (0) {
79 // just tokenise
80 while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) {
81 mp_token_show(mp_lexer_cur(lex));
82 mp_lexer_to_next(lex);
83 }
84 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +020085 return 0;
Damien George136f6752014-01-07 14:54:15 +000086 }
87
Damien Georgec5966122014-02-15 16:10:44 +000088 mp_parse_error_kind_t parse_error_kind;
89 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +000090
91 if (pn == MP_PARSE_NODE_NULL) {
92 // parse error
Damien Georgec5966122014-02-15 16:10:44 +000093 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +000094 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +020095 return 1;
Damien George136f6752014-01-07 14:54:15 +000096 }
97
Damien George08335002014-01-18 23:24:36 +000098 qstr source_name = mp_lexer_source_name(lex);
Damien George9528cd62014-01-15 21:23:31 +000099 mp_lexer_free(lex);
100
Damien Georgecbd2f742014-01-19 11:48:48 +0000101 /*
102 printf("----------------\n");
103 mp_parse_node_print(pn, 0);
104 printf("----------------\n");
105 */
Damien George136f6752014-01-07 14:54:15 +0000106
Damien George65cad122014-04-06 11:48:15 +0100107 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000108
109 if (module_fun == mp_const_none) {
110 // compile error
stijn9e040b72014-05-10 19:42:06 +0200111 return 1;
Damien George136f6752014-01-07 14:54:15 +0000112 }
113
Damien Georgef22626e2014-04-10 11:30:35 +0100114 if (compile_only) {
stijn9e040b72014-05-10 19:42:06 +0200115 return 0;
Damien Georgef22626e2014-04-10 11:30:35 +0100116 }
117
Damien George136f6752014-01-07 14:54:15 +0000118 // execute it
119 nlr_buf_t nlr;
120 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100121 mp_call_function_0(module_fun);
Damien George136f6752014-01-07 14:54:15 +0000122 nlr_pop();
stijn9e040b72014-05-10 19:42:06 +0200123 return 0;
Damien George136f6752014-01-07 14:54:15 +0000124 } else {
125 // uncaught exception
Damien George7a4ddd22014-05-24 23:32:19 +0100126 // check for SystemExit
127 mp_obj_t exc = (mp_obj_t)nlr.ret_val;
128 if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
129 exit(mp_obj_get_int(mp_obj_exception_get_value(exc)));
130 }
Damien George136b1492014-01-19 12:38:49 +0000131 mp_obj_print_exception((mp_obj_t)nlr.ret_val);
stijn9e040b72014-05-10 19:42:06 +0200132 return 1;
Damien George136f6752014-01-07 14:54:15 +0000133 }
134}
135
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300136STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100137 int l1 = strlen(s1);
138 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300139 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100140 memcpy(s, s1, l1);
141 if (sep_char != 0) {
142 s[l1] = sep_char;
143 l1 += 1;
144 }
145 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100146 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100147 return s;
148}
149
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300150STATIC void do_repl(void) {
Damien George6ec835d2014-05-03 19:08:02 +0100151 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; UNIX version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100152
Damien5ac1b2e2013-10-18 19:58:12 +0100153 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200154 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100155 if (line == NULL) {
156 // EOF
157 return;
158 }
Damien George97790452014-04-08 11:04:29 +0000159 while (mp_repl_continue_with_input(line)) {
160 char *line2 = prompt("... ");
161 if (line2 == NULL) {
162 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100163 }
Damien George97790452014-04-08 11:04:29 +0000164 char *line3 = strjoin(line, '\n', line2);
165 free(line);
166 free(line2);
167 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100168 }
Damienfa2162b2013-10-20 17:42:00 +0100169
Damien Georgeb829b5c2014-01-25 13:51:19 +0000170 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
Damien George136f6752014-01-07 14:54:15 +0000171 execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200172 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100173 }
174}
175
stijn9e040b72014-05-10 19:42:06 +0200176STATIC int do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000177 mp_lexer_t *lex = mp_lexer_new_from_file(file);
stijn9e040b72014-05-10 19:42:06 +0200178 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien George136f6752014-01-07 14:54:15 +0000179}
Damien429d7192013-10-04 19:53:11 +0100180
stijn9e040b72014-05-10 19:42:06 +0200181STATIC int do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000182 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
stijn9e040b72014-05-10 19:42:06 +0200183 return execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100184}
Damien429d7192013-10-04 19:53:11 +0100185
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300186int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000187 printf(
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300188"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
189"Options:\n"
190"-v : verbose (trace various operations); can be multiple\n"
Damien George5e349092014-03-08 19:04:47 +0000191"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300192"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300193);
194 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100195 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100196" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100197" emit={bytecode,native,viper} -- set the default code emitter\n"
198);
199 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300200#if MICROPY_ENABLE_GC
201 printf(
Damien George5e349092014-03-08 19:04:47 +0000202" heapsize=<n> -- set the heap size for the GC\n"
203);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300204 impl_opts_cnt++;
205#endif
206
207 if (impl_opts_cnt == 0) {
208 printf(" (none)\n");
209 }
210
Damien George136f6752014-01-07 14:54:15 +0000211 return 1;
212}
213
Damien George4d5b28c2014-01-29 18:56:46 +0000214mp_obj_t mem_info(void) {
Paul Sokolovsky914bcf12014-05-31 02:34:39 +0300215 volatile int stack_dummy;
216 printf("mem: total=%d, current=%d, peak=%d\n",
217 m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
Paul Sokolovsky3dfa76c2014-05-31 03:19:15 +0300218 printf("stack: " INT_FMT "\n", stack_top - (char*)&stack_dummy);
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300219#if MICROPY_ENABLE_GC
Paul Sokolovsky59a2f482014-04-17 18:32:36 +0300220 gc_dump_info();
Paul Sokolovskyb76fd842014-04-20 22:10:27 +0300221#endif
Damien George4d5b28c2014-01-29 18:56:46 +0000222 return mp_const_none;
223}
224
225mp_obj_t qstr_info(void) {
226 uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
227 qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
228 printf("qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
229 return mp_const_none;
230}
231
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200232// Process options which set interpreter init options
233void pre_process_options(int argc, char **argv) {
234 for (int a = 1; a < argc; a++) {
235 if (argv[a][0] == '-') {
236 if (strcmp(argv[a], "-X") == 0) {
237 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300238 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200239 }
Damien George5e349092014-03-08 19:04:47 +0000240 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100241 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
242 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100243 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
Damien George96f137b2014-05-12 22:35:37 +0100244 emit_opt = MP_EMIT_OPT_BYTECODE;
Damien George65cad122014-04-06 11:48:15 +0100245 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
246 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
247 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
248 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200249#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000250 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200251 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200252#endif
Damien George5e349092014-03-08 19:04:47 +0000253 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300254 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000255 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200256 a++;
257 }
258 }
259 }
260}
261
Damien5ac1b2e2013-10-18 19:58:12 +0100262int main(int argc, char **argv) {
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200263 volatile int stack_dummy;
Paul Sokolovsky914bcf12014-05-31 02:34:39 +0300264 stack_top = (char*)&stack_dummy;
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200265
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200266 pre_process_options(argc, argv);
267
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200268#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200269 char *heap = malloc(heap_size);
270 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200271#endif
272
Damien5ac1b2e2013-10-18 19:58:12 +0100273 qstr_init();
Damien Georged17926d2014-03-30 13:35:08 +0100274 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100275
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200276 char *home = getenv("HOME");
277 char *path = getenv("MICROPYPATH");
278 if (path == NULL) {
279 path = "~/.micropython/lib:/usr/lib/micropython";
280 }
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200281 uint path_num = 1; // [0] is for current dir (or base dir of the script)
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200282 for (char *p = path; p != NULL; p = strchr(p, ':')) {
283 path_num++;
284 if (p != NULL) {
285 p++;
286 }
287 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300288 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200289 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100290 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200291 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200292 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200293 for (int i = 1; i < path_num; i++) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200294 char *p1 = strchr(p, ':');
295 if (p1 == NULL) {
296 p1 = p + strlen(p);
297 }
298 if (p[0] == '~' && p[1] == '/' && home != NULL) {
299 // Expand standalone ~ to $HOME
300 CHECKBUF(buf, PATH_MAX);
301 CHECKBUF_APPEND(buf, home, strlen(home));
302 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200303 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200304 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200305 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200306 }
307 p = p1 + 1;
308 }
309
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300310 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200311
Damien Georged17926d2014-03-30 13:35:08 +0100312 mp_store_name(qstr_from_str("mem_info"), mp_make_function_n(0, mem_info));
313 mp_store_name(qstr_from_str("qstr_info"), mp_make_function_n(0, qstr_info));
Damien George12eacca2014-01-21 21:54:15 +0000314
Damien George062478e2014-01-09 20:57:50 +0000315 // Here is some example code to create a class and instance of that class.
316 // First is the Python, then the C code.
317 //
318 // class TestClass:
319 // pass
320 // test_obj = TestClass()
321 // test_obj.attr = 42
Damien Georgec327c0d2014-05-04 12:24:26 +0100322 //
323 // mp_obj_t test_class_type, test_class_instance;
324 // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
325 // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
326 // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000327
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000328 /*
329 printf("bytes:\n");
330 printf(" total %d\n", m_get_total_bytes_allocated());
331 printf(" cur %d\n", m_get_current_bytes_allocated());
332 printf(" peak %d\n", m_get_peak_bytes_allocated());
333 */
334
stijn9e040b72014-05-10 19:42:06 +0200335 const int NOTHING_EXECUTED = -2;
336 int ret = NOTHING_EXECUTED;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200337 for (int a = 1; a < argc; a++) {
338 if (argv[a][0] == '-') {
339 if (strcmp(argv[a], "-c") == 0) {
340 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300341 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000342 }
stijn9e040b72014-05-10 19:42:06 +0200343 ret = do_str(argv[a + 1]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200344 a += 1;
345 } else if (strcmp(argv[a], "-X") == 0) {
346 a += 1;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300347 } else if (strcmp(argv[a], "-v") == 0) {
348 mp_verbose_flag++;
Damien Georgecc974462014-05-12 23:14:52 +0100349 } else if (strcmp(argv[a], "-O") == 0) {
350 // optimisation; sets __debug__ to False
351 mp_set_debug(false);
Damien George136f6752014-01-07 14:54:15 +0000352 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300353 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000354 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200355 } else {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200356 char *basedir = realpath(argv[a], NULL);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300357 if (basedir == NULL) {
Paul Sokolovskydd0dee32014-06-03 01:01:39 +0300358 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300359 perror("");
360 // CPython exits with 2 in such case
stijn9e040b72014-05-10 19:42:06 +0200361 ret = 2;
362 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200363 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300364
365 // Set base dir of the script as first entry in sys.path
366 char *p = strrchr(basedir, '/');
367 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
368 free(basedir);
369
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200370 for (int i = a; i < argc; i++) {
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300371 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200372 }
stijn9e040b72014-05-10 19:42:06 +0200373 ret = do_file(argv[a]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200374 break;
Damien George136f6752014-01-07 14:54:15 +0000375 }
Damien5ac1b2e2013-10-18 19:58:12 +0100376 }
Damien George136f6752014-01-07 14:54:15 +0000377
stijn9e040b72014-05-10 19:42:06 +0200378 if (ret == NOTHING_EXECUTED) {
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200379 do_repl();
stijn9e040b72014-05-10 19:42:06 +0200380 ret = 0;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200381 }
382
Damien Georged17926d2014-03-30 13:35:08 +0100383 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100384
385 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
stijn9e040b72014-05-10 19:42:06 +0200386 return ret;
Damien429d7192013-10-04 19:53:11 +0100387}
Damien087d2182013-11-09 20:14:30 +0000388
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300389STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {
390 int rc = 0;
391 if (n_args > 0) {
392 rc = mp_obj_get_int(args[0]);
393 }
Damien George7a4ddd22014-05-24 23:32:19 +0100394 nlr_raise(mp_obj_new_exception_arg1(&mp_type_SystemExit, mp_obj_new_int(rc)));
Paul Sokolovskyd99e9082014-05-10 16:50:45 +0300395}
396MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_sys_exit_obj, 0, 1, mp_sys_exit);
397
Damien Georgee09ffa12014-02-05 23:57:48 +0000398uint mp_import_stat(const char *path) {
399 struct stat st;
400 if (stat(path, &st) == 0) {
401 if (S_ISDIR(st.st_mode)) {
402 return MP_IMPORT_STAT_DIR;
403 } else if (S_ISREG(st.st_mode)) {
404 return MP_IMPORT_STAT_FILE;
405 }
406 }
407 return MP_IMPORT_STAT_NO_EXIST;
408}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200409
410int DEBUG_printf(const char *fmt, ...) {
411 va_list ap;
412 va_start(ap, fmt);
413 int ret = vfprintf(stderr, fmt, ap);
414 va_end(ap);
415 return ret;
416}
Damien George26cf55a2014-04-08 14:08:14 +0000417
418void nlr_jump_fail(void *val) {
419 printf("FATAL: uncaught NLR %p\n", val);
420 exit(1);
421}