blob: 2a0dc864c4f52390f217cc482f3e6f7b642c7b9d [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>
Paul Sokolovskyd3439d02014-06-02 19:37:55 +030033#include <ctype.h>
xbec93a2212014-03-15 23:24:34 -070034#include <sys/stat.h>
35#include <sys/types.h>
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +030036#include <errno.h>
Damien429d7192013-10-04 19:53:11 +010037
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030038#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010039#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010040#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000041#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010042#include "lexer.h"
Damiena5185f42013-10-20 14:41:27 +010043#include "lexerunix.h"
Damien429d7192013-10-04 19:53:11 +010044#include "parse.h"
Damien0f082672013-12-17 18:33:53 +000045#include "obj.h"
Damien Georgec5966122014-02-15 16:10:44 +000046#include "parsehelper.h"
Damien George1fb03172014-01-03 14:22:03 +000047#include "compile.h"
Damiend99b0522013-12-21 18:17:45 +000048#include "runtime0.h"
49#include "runtime.h"
Paul Sokolovskye5035122014-10-25 21:16:24 +030050#include "builtin.h"
Damien92c06562013-10-22 22:32:27 +010051#include "repl.h"
Paul Sokolovskye7db8172014-02-11 00:44:37 +020052#include "gc.h"
Damien Georged553be52014-04-17 18:03:27 +010053#include "genhdr/py-version.h"
Dave Hylands117c46d2014-05-07 07:15:00 -070054#include "input.h"
Paul Sokolovsky23668692014-06-25 03:03:34 +030055#include "stackctrl.h"
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +020056#include "pfenv.h"
Damien429d7192013-10-04 19:53:11 +010057
Damien Georgef22626e2014-04-10 11:30:35 +010058// Command line options, with their defaults
Damien Georgeb0261342014-09-23 18:10:17 +010059STATIC bool compile_only = false;
60STATIC uint emit_opt = MP_EMIT_OPT_NONE;
61mp_uint_t mp_verbose_flag = 0;
Damien George65cad122014-04-06 11:48:15 +010062
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020063#if MICROPY_ENABLE_GC
Paul Sokolovskye7db8172014-02-11 00:44:37 +020064// Heap size of GC heap (if enabled)
Damien Georgebd17e1b2014-04-04 14:29:00 +010065// Make it larger on a 64 bit machine, because pointers are larger.
Damien George40f3c022014-07-03 13:25:24 +010066long heap_size = 128*1024 * (sizeof(mp_uint_t) / 4);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +020067#endif
Paul Sokolovskye7db8172014-02-11 00:44:37 +020068
Damien Georgec76af322014-10-26 00:42:41 +010069#ifndef _WIN32
70#include <signal.h>
71
Damien George124df6f2014-10-25 18:19:55 +010072STATIC mp_obj_t keyboard_interrupt_obj;
73
74STATIC void sighandler(int signum) {
75 if (signum == SIGINT) {
76 mp_obj_exception_clear_traceback(keyboard_interrupt_obj);
77 mp_pending_exception = keyboard_interrupt_obj;
78 // disable our handler so next we really die
79 struct sigaction sa;
80 sa.sa_handler = SIG_DFL;
81 sigemptyset(&sa.sa_mask);
82 sigaction(SIGINT, &sa, NULL);
83 }
84}
Damien Georgec76af322014-10-26 00:42:41 +010085#endif
Damien George124df6f2014-10-25 18:19:55 +010086
Damien George762d5752014-10-01 23:18:39 +010087#define FORCED_EXIT (0x100)
Paul Sokolovskye5035122014-10-25 21:16:24 +030088// If exc is SystemExit, return value where FORCED_EXIT bit set,
89// and lower 8 bits are SystemExit value. For all other exceptions,
90// return 1.
91STATIC int handle_uncaught_exception(mp_obj_t exc) {
92 // check for SystemExit
93 if (mp_obj_is_subclass_fast(mp_obj_get_type(exc), &mp_type_SystemExit)) {
94 // None is an exit value of 0; an int is its value; anything else is 1
95 mp_obj_t exit_val = mp_obj_exception_get_value(exc);
96 mp_int_t val = 0;
97 if (exit_val != mp_const_none && !mp_obj_get_int_maybe(exit_val, &val)) {
98 val = 1;
99 }
100 return FORCED_EXIT | (val & 255);
101 }
102
103 // Report all other exceptions
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200104 mp_obj_print_exception(printf_wrapper, NULL, exc);
Paul Sokolovskye5035122014-10-25 21:16:24 +0300105 return 1;
106}
107
108// Returns standard error codes: 0 for success, 1 for all other errors,
109// except if FORCED_EXIT bit is set then script raised SystemExit and the
Damien George762d5752014-10-01 23:18:39 +0100110// value of the exit is in the lower 8 bits of the return value
stijn9e040b72014-05-10 19:42:06 +0200111STATIC 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 +0000112 if (lex == NULL) {
stijn9e040b72014-05-10 19:42:06 +0200113 return 1;
Damien George136f6752014-01-07 14:54:15 +0000114 }
115
116 if (0) {
117 // just tokenise
Damien Georgea4c52c52014-12-05 19:35:18 +0000118 while (lex->tok_kind != MP_TOKEN_END) {
119 mp_lexer_show_token(lex);
Damien George136f6752014-01-07 14:54:15 +0000120 mp_lexer_to_next(lex);
121 }
122 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200123 return 0;
Damien George136f6752014-01-07 14:54:15 +0000124 }
125
Damien Georgec5966122014-02-15 16:10:44 +0000126 mp_parse_error_kind_t parse_error_kind;
127 mp_parse_node_t pn = mp_parse(lex, input_kind, &parse_error_kind);
Damien George136f6752014-01-07 14:54:15 +0000128
129 if (pn == MP_PARSE_NODE_NULL) {
130 // parse error
Damien Georgec5966122014-02-15 16:10:44 +0000131 mp_parse_show_exception(lex, parse_error_kind);
Damien George9528cd62014-01-15 21:23:31 +0000132 mp_lexer_free(lex);
stijn9e040b72014-05-10 19:42:06 +0200133 return 1;
Damien George136f6752014-01-07 14:54:15 +0000134 }
135
Damien Georgea4c52c52014-12-05 19:35:18 +0000136 qstr source_name = lex->source_name;
Paul Sokolovskyd0f5e612014-07-25 11:00:15 +0300137 #if MICROPY_PY___FILE__
138 if (input_kind == MP_PARSE_FILE_INPUT) {
139 mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name));
140 }
141 #endif
Damien George9528cd62014-01-15 21:23:31 +0000142 mp_lexer_free(lex);
143
Damien Georgecbd2f742014-01-19 11:48:48 +0000144 /*
145 printf("----------------\n");
146 mp_parse_node_print(pn, 0);
147 printf("----------------\n");
148 */
Damien George136f6752014-01-07 14:54:15 +0000149
Damien George65cad122014-04-06 11:48:15 +0100150 mp_obj_t module_fun = mp_compile(pn, source_name, emit_opt, is_repl);
Damien George136f6752014-01-07 14:54:15 +0000151
Damien George854c8c02014-10-05 22:25:36 +0100152 if (mp_obj_is_exception_instance(module_fun)) {
Damien George136f6752014-01-07 14:54:15 +0000153 // compile error
Paul Sokolovsky46c3ab22014-12-06 14:29:09 +0200154 mp_obj_print_exception(printf_wrapper, NULL, module_fun);
stijn9e040b72014-05-10 19:42:06 +0200155 return 1;
Damien George136f6752014-01-07 14:54:15 +0000156 }
157
Damien Georgef22626e2014-04-10 11:30:35 +0100158 if (compile_only) {
stijn9e040b72014-05-10 19:42:06 +0200159 return 0;
Damien Georgef22626e2014-04-10 11:30:35 +0100160 }
161
Damien Georgec76af322014-10-26 00:42:41 +0100162 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100163 // enable signal handler
164 struct sigaction sa;
165 sa.sa_handler = sighandler;
166 sigemptyset(&sa.sa_mask);
167 sigaction(SIGINT, &sa, NULL);
168 sa.sa_handler = SIG_DFL;
Damien Georgec76af322014-10-26 00:42:41 +0100169 #endif
Damien George124df6f2014-10-25 18:19:55 +0100170
Damien George136f6752014-01-07 14:54:15 +0000171 // execute it
172 nlr_buf_t nlr;
173 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100174 mp_call_function_0(module_fun);
Damien Georgec76af322014-10-26 00:42:41 +0100175 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100176 sigaction(SIGINT, &sa, NULL);
Damien Georgec76af322014-10-26 00:42:41 +0100177 #endif
Damien George136f6752014-01-07 14:54:15 +0000178 nlr_pop();
stijn9e040b72014-05-10 19:42:06 +0200179 return 0;
Damien George136f6752014-01-07 14:54:15 +0000180 } else {
181 // uncaught exception
Damien Georgec76af322014-10-26 00:42:41 +0100182 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100183 sigaction(SIGINT, &sa, NULL);
Damien Georgec76af322014-10-26 00:42:41 +0100184 #endif
Paul Sokolovskye5035122014-10-25 21:16:24 +0300185 return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
Damien George136f6752014-01-07 14:54:15 +0000186 }
187}
188
Paul Sokolovskycd31d822014-04-04 20:25:53 +0300189STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
Damien5ac1b2e2013-10-18 19:58:12 +0100190 int l1 = strlen(s1);
191 int l2 = strlen(s2);
Paul Sokolovsky70193b22014-04-04 17:47:53 +0300192 char *s = malloc(l1 + l2 + 2);
Damien5ac1b2e2013-10-18 19:58:12 +0100193 memcpy(s, s1, l1);
194 if (sep_char != 0) {
195 s[l1] = sep_char;
196 l1 += 1;
197 }
198 memcpy(s + l1, s2, l2);
Damien92c06562013-10-22 22:32:27 +0100199 s[l1 + l2] = 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100200 return s;
201}
202
Damien George762d5752014-10-01 23:18:39 +0100203STATIC int do_repl(void) {
stijnec6fa872014-06-28 21:04:20 +0200204 printf("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_PY_SYS_PLATFORM " version\n");
Damien George6827f9f2014-04-07 13:27:50 +0100205
Damien5ac1b2e2013-10-18 19:58:12 +0100206 for (;;) {
Paul Sokolovskyfa027672014-01-01 18:28:01 +0200207 char *line = prompt(">>> ");
Damien5ac1b2e2013-10-18 19:58:12 +0100208 if (line == NULL) {
209 // EOF
Damien George762d5752014-10-01 23:18:39 +0100210 return 0;
Damien5ac1b2e2013-10-18 19:58:12 +0100211 }
Damien George97790452014-04-08 11:04:29 +0000212 while (mp_repl_continue_with_input(line)) {
213 char *line2 = prompt("... ");
214 if (line2 == NULL) {
215 break;
Damien5ac1b2e2013-10-18 19:58:12 +0100216 }
Damien George97790452014-04-08 11:04:29 +0000217 char *line3 = strjoin(line, '\n', line2);
218 free(line);
219 free(line2);
220 line = line3;
Damien5ac1b2e2013-10-18 19:58:12 +0100221 }
Damienfa2162b2013-10-20 17:42:00 +0100222
Damien Georgeb829b5c2014-01-25 13:51:19 +0000223 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, line, strlen(line), false);
Damien George762d5752014-10-01 23:18:39 +0100224 int ret = execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true);
225 if (ret & FORCED_EXIT) {
226 return ret;
227 }
Paul Sokolovsky2b2cb7b2014-01-24 16:21:26 +0200228 free(line);
Damien5ac1b2e2013-10-18 19:58:12 +0100229 }
230}
231
stijn9e040b72014-05-10 19:42:06 +0200232STATIC int do_file(const char *file) {
Damiend99b0522013-12-21 18:17:45 +0000233 mp_lexer_t *lex = mp_lexer_new_from_file(file);
stijn9e040b72014-05-10 19:42:06 +0200234 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien George136f6752014-01-07 14:54:15 +0000235}
Damien429d7192013-10-04 19:53:11 +0100236
stijn9e040b72014-05-10 19:42:06 +0200237STATIC int do_str(const char *str) {
Damien Georgeb829b5c2014-01-25 13:51:19 +0000238 mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
Damien Georgec92672d2014-10-17 23:51:39 +0100239 return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
Damien5ac1b2e2013-10-18 19:58:12 +0100240}
Damien429d7192013-10-04 19:53:11 +0100241
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300242int usage(char **argv) {
Damien George5e349092014-03-08 19:04:47 +0000243 printf(
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300244"usage: %s [<opts>] [-X <implopt>] [-c <command>] [<filename>]\n"
245"Options:\n"
246"-v : verbose (trace various operations); can be multiple\n"
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300247"-O[N] : apply bytecode optimizations of level N\n"
Damien George5e349092014-03-08 19:04:47 +0000248"\n"
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300249"Implementation specific options:\n", argv[0]
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300250);
251 int impl_opts_cnt = 0;
Damien George65cad122014-04-06 11:48:15 +0100252 printf(
Damien Georgef22626e2014-04-10 11:30:35 +0100253" compile-only -- parse and compile only\n"
Damien George65cad122014-04-06 11:48:15 +0100254" emit={bytecode,native,viper} -- set the default code emitter\n"
255);
256 impl_opts_cnt++;
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300257#if MICROPY_ENABLE_GC
258 printf(
Paul Sokolovsky141df2d2014-06-24 16:58:00 +0300259" heapsize=<n> -- set the heap size for the GC (default %ld)\n"
260, heap_size);
Paul Sokolovskya55a5462014-04-02 20:29:18 +0300261 impl_opts_cnt++;
262#endif
263
264 if (impl_opts_cnt == 0) {
265 printf(" (none)\n");
266 }
267
Damien George136f6752014-01-07 14:54:15 +0000268 return 1;
269}
270
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200271// Process options which set interpreter init options
272void pre_process_options(int argc, char **argv) {
273 for (int a = 1; a < argc; a++) {
274 if (argv[a][0] == '-') {
275 if (strcmp(argv[a], "-X") == 0) {
276 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300277 exit(usage(argv));
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200278 }
Damien George5e349092014-03-08 19:04:47 +0000279 if (0) {
Damien Georgef22626e2014-04-10 11:30:35 +0100280 } else if (strcmp(argv[a + 1], "compile-only") == 0) {
281 compile_only = true;
Damien George65cad122014-04-06 11:48:15 +0100282 } else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
Damien George96f137b2014-05-12 22:35:37 +0100283 emit_opt = MP_EMIT_OPT_BYTECODE;
Damien George65cad122014-04-06 11:48:15 +0100284 } else if (strcmp(argv[a + 1], "emit=native") == 0) {
285 emit_opt = MP_EMIT_OPT_NATIVE_PYTHON;
286 } else if (strcmp(argv[a + 1], "emit=viper") == 0) {
287 emit_opt = MP_EMIT_OPT_VIPER;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200288#if MICROPY_ENABLE_GC
Damien George5e349092014-03-08 19:04:47 +0000289 } else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
Paul Sokolovskyfcff4662014-10-25 04:43:08 +0300290 char *end;
291 heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, &end, 0);
292 // Don't bring unneeded libc dependencies like tolower()
Paul Sokolovsky98d8d592014-11-05 02:08:38 +0200293 // If there's 'w' immediately after number, adjust it for
294 // target word size. Note that it should be *before* size
295 // suffix like K or M, to avoid confusion with kilowords,
296 // etc. the size is still in bytes, just can be adjusted
297 // for word size (taking 32bit as baseline).
298 bool word_adjust = false;
299 if ((*end | 0x20) == 'w') {
300 word_adjust = true;
301 end++;
302 }
Paul Sokolovskyfcff4662014-10-25 04:43:08 +0300303 if ((*end | 0x20) == 'k') {
304 heap_size *= 1024;
305 } else if ((*end | 0x20) == 'm') {
306 heap_size *= 1024 * 1024;
307 }
Paul Sokolovsky98d8d592014-11-05 02:08:38 +0200308 if (word_adjust) {
309 heap_size = heap_size * BYTES_PER_WORD / 4;
310 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200311#endif
Damien George5e349092014-03-08 19:04:47 +0000312 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300313 exit(usage(argv));
Damien George5e349092014-03-08 19:04:47 +0000314 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200315 a++;
316 }
317 }
318 }
319}
320
Paul Sokolovskye5035122014-10-25 21:16:24 +0300321void set_sys_argv(char *argv[], int argc, int start_arg) {
322 for (int i = start_arg; i < argc; i++) {
323 mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
324 }
325}
326
stijndf3ab072014-06-05 12:33:55 +0200327#ifdef _WIN32
328#define PATHLIST_SEP_CHAR ';'
329#else
330#define PATHLIST_SEP_CHAR ':'
331#endif
332
Damien5ac1b2e2013-10-18 19:58:12 +0100333int main(int argc, char **argv) {
Paul Sokolovskycaa73342014-07-01 02:13:42 +0300334 mp_stack_set_limit(32768);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200335
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200336 pre_process_options(argc, argv);
337
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200338#if MICROPY_ENABLE_GC
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200339 char *heap = malloc(heap_size);
340 gc_init(heap, heap + heap_size);
Paul Sokolovskye7db8172014-02-11 00:44:37 +0200341#endif
342
Damien Georged17926d2014-03-30 13:35:08 +0100343 mp_init();
Damien5ac1b2e2013-10-18 19:58:12 +0100344
Damien Georgec76af322014-10-26 00:42:41 +0100345 #ifndef _WIN32
Damien George124df6f2014-10-25 18:19:55 +0100346 // create keyboard interrupt object
347 keyboard_interrupt_obj = mp_obj_new_exception(&mp_type_KeyboardInterrupt);
Damien Georgec76af322014-10-26 00:42:41 +0100348 #endif
Damien George124df6f2014-10-25 18:19:55 +0100349
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200350 char *home = getenv("HOME");
351 char *path = getenv("MICROPYPATH");
352 if (path == NULL) {
353 path = "~/.micropython/lib:/usr/lib/micropython";
354 }
Damien George9c4cbe22014-08-30 14:04:14 +0100355 mp_uint_t path_num = 1; // [0] is for current dir (or base dir of the script)
stijndf3ab072014-06-05 12:33:55 +0200356 for (char *p = path; p != NULL; p = strchr(p, PATHLIST_SEP_CHAR)) {
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200357 path_num++;
358 if (p != NULL) {
359 p++;
360 }
361 }
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300362 mp_obj_list_init(mp_sys_path, path_num);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200363 mp_obj_t *path_items;
Damien Georged17926d2014-03-30 13:35:08 +0100364 mp_obj_list_get(mp_sys_path, &path_num, &path_items);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200365 path_items[0] = MP_OBJ_NEW_QSTR(MP_QSTR_);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200366 char *p = path;
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200367 for (int i = 1; i < path_num; i++) {
stijndf3ab072014-06-05 12:33:55 +0200368 char *p1 = strchr(p, PATHLIST_SEP_CHAR);
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200369 if (p1 == NULL) {
370 p1 = p + strlen(p);
371 }
372 if (p[0] == '~' && p[1] == '/' && home != NULL) {
373 // Expand standalone ~ to $HOME
374 CHECKBUF(buf, PATH_MAX);
375 CHECKBUF_APPEND(buf, home, strlen(home));
376 CHECKBUF_APPEND(buf, p + 1, p1 - p - 1);
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200377 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(buf, CHECKBUF_LEN(buf)));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200378 } else {
Paul Sokolovsky630d8512014-02-05 01:53:44 +0200379 path_items[i] = MP_OBJ_NEW_QSTR(qstr_from_strn(p, p1 - p));
Paul Sokolovsky625d08a2014-02-05 00:59:54 +0200380 }
381 p = p1 + 1;
382 }
383
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300384 mp_obj_list_init(mp_sys_argv, 0);
Paul Sokolovskyfe2690d2014-01-20 00:53:46 +0200385
Damien George062478e2014-01-09 20:57:50 +0000386 // Here is some example code to create a class and instance of that class.
387 // First is the Python, then the C code.
388 //
389 // class TestClass:
390 // pass
391 // test_obj = TestClass()
392 // test_obj.attr = 42
Damien Georgec327c0d2014-05-04 12:24:26 +0100393 //
394 // mp_obj_t test_class_type, test_class_instance;
395 // test_class_type = mp_obj_new_type(QSTR_FROM_STR_STATIC("TestClass"), mp_const_empty_tuple, mp_obj_new_dict(0));
396 // mp_store_name(QSTR_FROM_STR_STATIC("test_obj"), test_class_instance = mp_call_function_0(test_class_type));
397 // mp_store_attr(test_class_instance, QSTR_FROM_STR_STATIC("attr"), mp_obj_new_int(42));
Damien George062478e2014-01-09 20:57:50 +0000398
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000399 /*
400 printf("bytes:\n");
401 printf(" total %d\n", m_get_total_bytes_allocated());
402 printf(" cur %d\n", m_get_current_bytes_allocated());
403 printf(" peak %d\n", m_get_peak_bytes_allocated());
404 */
405
stijn9e040b72014-05-10 19:42:06 +0200406 const int NOTHING_EXECUTED = -2;
407 int ret = NOTHING_EXECUTED;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200408 for (int a = 1; a < argc; a++) {
409 if (argv[a][0] == '-') {
410 if (strcmp(argv[a], "-c") == 0) {
411 if (a + 1 >= argc) {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300412 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000413 }
stijn9e040b72014-05-10 19:42:06 +0200414 ret = do_str(argv[a + 1]);
Damien George762d5752014-10-01 23:18:39 +0100415 if (ret & FORCED_EXIT) {
416 break;
417 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200418 a += 1;
Paul Sokolovskye5035122014-10-25 21:16:24 +0300419 } else if (strcmp(argv[a], "-m") == 0) {
420 if (a + 1 >= argc) {
421 return usage(argv);
422 }
423 mp_obj_t import_args[4];
424 import_args[0] = mp_obj_new_str(argv[a + 1], strlen(argv[a + 1]), false);
425 import_args[1] = import_args[2] = mp_const_none;
426 // Ask __import__ to handle imported module specially - set its __name__
427 // to __main__, and also return this leaf module, not top-level package
428 // containing it.
429 import_args[3] = mp_const_false;
430 // TODO: https://docs.python.org/3/using/cmdline.html#cmdoption-m :
431 // "the first element of sys.argv will be the full path to
432 // the module file (while the module file is being located,
433 // the first element will be set to "-m")."
434 set_sys_argv(argv, argc, a + 1);
435
436 mp_obj_t mod;
437 nlr_buf_t nlr;
438 if (nlr_push(&nlr) == 0) {
439 mod = mp_builtin___import__(MP_ARRAY_SIZE(import_args), import_args);
440 nlr_pop();
441 } else {
442 // uncaught exception
443 return handle_uncaught_exception((mp_obj_t)nlr.ret_val);
444 }
445
446 if (mp_obj_is_package(mod)) {
447 // TODO
448 fprintf(stderr, "%s: -m for packages not yet implemented\n", argv[0]);
449 exit(1);
450 }
451 ret = 0;
452 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200453 } else if (strcmp(argv[a], "-X") == 0) {
454 a += 1;
Paul Sokolovsky6b344d72014-05-05 00:50:05 +0300455 } else if (strcmp(argv[a], "-v") == 0) {
456 mp_verbose_flag++;
Paul Sokolovskyd3439d02014-06-02 19:37:55 +0300457 } else if (strncmp(argv[a], "-O", 2) == 0) {
458 if (isdigit(argv[a][2])) {
459 mp_optimise_value = argv[a][2] & 0xf;
460 } else {
461 mp_optimise_value = 0;
462 for (char *p = argv[a] + 1; *p && *p == 'O'; p++, mp_optimise_value++);
463 }
Damien George136f6752014-01-07 14:54:15 +0000464 } else {
Paul Sokolovskyd440dc02014-04-02 20:31:18 +0300465 return usage(argv);
Damien George136f6752014-01-07 14:54:15 +0000466 }
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200467 } else {
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300468 char *pathbuf = malloc(PATH_MAX);
469 char *basedir = realpath(argv[a], pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300470 if (basedir == NULL) {
Paul Sokolovskydd0dee32014-06-03 01:01:39 +0300471 fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[a], errno);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300472 perror("");
473 // CPython exits with 2 in such case
stijn9e040b72014-05-10 19:42:06 +0200474 ret = 2;
475 break;
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200476 }
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300477
478 // Set base dir of the script as first entry in sys.path
479 char *p = strrchr(basedir, '/');
480 path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir));
Paul Sokolovsky69d0a1c2014-06-22 19:08:32 +0300481 free(pathbuf);
Paul Sokolovskyd4e7e062014-04-02 20:19:32 +0300482
Paul Sokolovskye5035122014-10-25 21:16:24 +0300483 set_sys_argv(argv, argc, a);
stijn9e040b72014-05-10 19:42:06 +0200484 ret = do_file(argv[a]);
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200485 break;
Damien George136f6752014-01-07 14:54:15 +0000486 }
Damien5ac1b2e2013-10-18 19:58:12 +0100487 }
Damien George136f6752014-01-07 14:54:15 +0000488
stijn9e040b72014-05-10 19:42:06 +0200489 if (ret == NOTHING_EXECUTED) {
Damien George762d5752014-10-01 23:18:39 +0100490 ret = do_repl();
Paul Sokolovskya374d9c2014-03-01 12:21:53 +0200491 }
492
Paul Sokolovsky429e3f02014-10-26 23:20:22 +0200493 if (mp_verbose_flag) {
Paul Sokolovsky6e8ff9c2014-12-01 20:41:56 +0200494 extern mp_obj_t mp_micropython_mem_info(mp_uint_t n_args, const mp_obj_t *args);
495 mp_micropython_mem_info(0, NULL);
Paul Sokolovsky429e3f02014-10-26 23:20:22 +0200496 }
497
Damien Georged17926d2014-03-30 13:35:08 +0100498 mp_deinit();
Damien429d7192013-10-04 19:53:11 +0100499
Damien George762d5752014-10-01 23:18:39 +0100500#if MICROPY_ENABLE_GC && !defined(NDEBUG)
501 // We don't really need to free memory since we are about to exit the
502 // process, but doing so helps to find memory leaks.
503 free(heap);
504#endif
505
Damien429d7192013-10-04 19:53:11 +0100506 //printf("total bytes = %d\n", m_get_total_bytes_allocated());
stijn9e040b72014-05-10 19:42:06 +0200507 return ret;
Damien429d7192013-10-04 19:53:11 +0100508}
Damien087d2182013-11-09 20:14:30 +0000509
Damien Georgee09ffa12014-02-05 23:57:48 +0000510uint mp_import_stat(const char *path) {
511 struct stat st;
512 if (stat(path, &st) == 0) {
513 if (S_ISDIR(st.st_mode)) {
514 return MP_IMPORT_STAT_DIR;
515 } else if (S_ISREG(st.st_mode)) {
516 return MP_IMPORT_STAT_FILE;
517 }
518 }
519 return MP_IMPORT_STAT_NO_EXIST;
520}
Paul Sokolovsky44739e22014-02-16 18:11:42 +0200521
522int DEBUG_printf(const char *fmt, ...) {
523 va_list ap;
524 va_start(ap, fmt);
525 int ret = vfprintf(stderr, fmt, ap);
526 va_end(ap);
527 return ret;
528}
Damien George26cf55a2014-04-08 14:08:14 +0000529
530void nlr_jump_fail(void *val) {
531 printf("FATAL: uncaught NLR %p\n", val);
532 exit(1);
533}