From d4e7e06d7f8bdf3e35cac52cc1f75bdb53981783 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 2 Apr 2014 20:19:32 +0300 Subject: unix: Properly recognize and report when script on cmdline not found. Previosuly just silently exited. --- unix/main.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'unix/main.c') diff --git a/unix/main.c b/unix/main.c index 57eaa1997..b5f7a82f1 100644 --- a/unix/main.c +++ b/unix/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "nlr.h" #include "misc.h" @@ -369,13 +370,19 @@ int main(int argc, char **argv) { return usage(); } } else { - // Set base dir of the script as first entry in sys.path char *basedir = realpath(argv[a], NULL); - if (basedir != NULL) { - char *p = strrchr(basedir, '/'); - path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir)); - free(basedir); + if (basedir == NULL) { + fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno); + perror(""); + // CPython exits with 2 in such case + exit(2); } + + // Set base dir of the script as first entry in sys.path + char *p = strrchr(basedir, '/'); + path_items[0] = MP_OBJ_NEW_QSTR(qstr_from_strn(basedir, p - basedir)); + free(basedir); + for (int i = a; i < argc; i++) { mp_obj_list_append(py_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i]))); } -- cgit v1.2.3 From a55a5469c3186e85a9153139ef4f228a81c9b926 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 2 Apr 2014 20:29:18 +0300 Subject: unix: Support #if-able impl-specific cmdline options. For example, we still build w/o GC enabled, so cannot really set heap size. --- unix/main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'unix/main.c') diff --git a/unix/main.c b/unix/main.c index b5f7a82f1..029a8effa 100644 --- a/unix/main.c +++ b/unix/main.c @@ -218,8 +218,19 @@ int usage(void) { "usage: py [-X ] [-c ] []\n" "\n" "Implementation specific options:\n" +); + int impl_opts_cnt = 0; +#if MICROPY_ENABLE_GC + printf( " heapsize= -- set the heap size for the GC\n" ); + impl_opts_cnt++; +#endif + + if (impl_opts_cnt == 0) { + printf(" (none)\n"); + } + return 1; } -- cgit v1.2.3 From d440dc05135bc93a4e95d07f4d7cd7bf2710408f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 2 Apr 2014 20:31:18 +0300 Subject: unix: Use argv[0] for command name in usage. --- unix/main.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'unix/main.c') diff --git a/unix/main.c b/unix/main.c index 029a8effa..228c4746a 100644 --- a/unix/main.c +++ b/unix/main.c @@ -213,11 +213,11 @@ mp_obj_t test_obj_new(int value) { return o; } -int usage(void) { +int usage(char **argv) { printf( -"usage: py [-X ] [-c ] []\n" +"usage: %s [-X ] [-c ] []\n" "\n" -"Implementation specific options:\n" +"Implementation specific options:\n", argv[0] ); int impl_opts_cnt = 0; #if MICROPY_ENABLE_GC @@ -261,7 +261,7 @@ void pre_process_options(int argc, char **argv) { if (argv[a][0] == '-') { if (strcmp(argv[a], "-X") == 0) { if (a + 1 >= argc) { - exit(usage()); + exit(usage(argv)); } if (0) { #if MICROPY_ENABLE_GC @@ -269,7 +269,7 @@ void pre_process_options(int argc, char **argv) { heap_size = strtol(argv[a + 1] + sizeof("heapsize=") - 1, NULL, 0); #endif } else { - exit(usage()); + exit(usage(argv)); } a++; } @@ -370,7 +370,7 @@ int main(int argc, char **argv) { if (argv[a][0] == '-') { if (strcmp(argv[a], "-c") == 0) { if (a + 1 >= argc) { - return usage(); + return usage(argv); } do_str(argv[a + 1]); executed = true; @@ -378,7 +378,7 @@ int main(int argc, char **argv) { } else if (strcmp(argv[a], "-X") == 0) { a += 1; } else { - return usage(); + return usage(argv); } } else { char *basedir = realpath(argv[a], NULL); -- cgit v1.2.3