Skip to content

Commit f6e430f

Browse files
committed
Merge pull request adafruit#600 from stinos/unix-exitcode
unix: Use standard return codes for main
2 parents aeeb448 + 9e040b7 commit f6e430f

1 file changed

Lines changed: 22 additions & 18 deletions

File tree

unix/main.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ void microsocket_init();
6969
void time_init();
7070
void ffi_init();
7171

72-
STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
72+
// returns standard error codes: 0 for success, 1 for all other errors
73+
STATIC int execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) {
7374
if (lex == NULL) {
74-
return;
75+
return 1;
7576
}
7677

7778
if (0) {
@@ -81,7 +82,7 @@ STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
8182
mp_lexer_to_next(lex);
8283
}
8384
mp_lexer_free(lex);
84-
return;
85+
return 0;
8586
}
8687

8788
mp_parse_error_kind_t parse_error_kind;
@@ -91,7 +92,7 @@ STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
9192
// parse error
9293
mp_parse_show_exception(lex, parse_error_kind);
9394
mp_lexer_free(lex);
94-
return;
95+
return 1;
9596
}
9697

9798
qstr source_name = mp_lexer_source_name(lex);
@@ -107,21 +108,23 @@ STATIC void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind
107108

108109
if (module_fun == mp_const_none) {
109110
// compile error
110-
return;
111+
return 1;
111112
}
112113

113114
if (compile_only) {
114-
return;
115+
return 0;
115116
}
116117

117118
// execute it
118119
nlr_buf_t nlr;
119120
if (nlr_push(&nlr) == 0) {
120121
mp_call_function_0(module_fun);
121122
nlr_pop();
123+
return 0;
122124
} else {
123125
// uncaught exception
124126
mp_obj_print_exception((mp_obj_t)nlr.ret_val);
127+
return 1;
125128
}
126129
}
127130

@@ -165,14 +168,14 @@ STATIC void do_repl(void) {
165168
}
166169
}
167170

168-
STATIC void do_file(const char *file) {
171+
STATIC int do_file(const char *file) {
169172
mp_lexer_t *lex = mp_lexer_new_from_file(file);
170-
execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
173+
return execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false);
171174
}
172175

173-
STATIC void do_str(const char *str) {
176+
STATIC int do_str(const char *str) {
174177
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, str, strlen(str), false);
175-
execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
178+
return execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false);
176179
}
177180

178181
int usage(char **argv) {
@@ -321,15 +324,15 @@ int main(int argc, char **argv) {
321324
printf(" peak %d\n", m_get_peak_bytes_allocated());
322325
*/
323326

324-
bool executed = false;
327+
const int NOTHING_EXECUTED = -2;
328+
int ret = NOTHING_EXECUTED;
325329
for (int a = 1; a < argc; a++) {
326330
if (argv[a][0] == '-') {
327331
if (strcmp(argv[a], "-c") == 0) {
328332
if (a + 1 >= argc) {
329333
return usage(argv);
330334
}
331-
do_str(argv[a + 1]);
332-
executed = true;
335+
ret = do_str(argv[a + 1]);
333336
a += 1;
334337
} else if (strcmp(argv[a], "-X") == 0) {
335338
a += 1;
@@ -347,7 +350,8 @@ int main(int argc, char **argv) {
347350
fprintf(stderr, "%s: can't open file '%s': [Errno %d] ", argv[0], argv[1], errno);
348351
perror("");
349352
// CPython exits with 2 in such case
350-
exit(2);
353+
ret = 2;
354+
break;
351355
}
352356

353357
// Set base dir of the script as first entry in sys.path
@@ -358,20 +362,20 @@ int main(int argc, char **argv) {
358362
for (int i = a; i < argc; i++) {
359363
mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
360364
}
361-
do_file(argv[a]);
362-
executed = true;
365+
ret = do_file(argv[a]);
363366
break;
364367
}
365368
}
366369

367-
if (!executed) {
370+
if (ret == NOTHING_EXECUTED) {
368371
do_repl();
372+
ret = 0;
369373
}
370374

371375
mp_deinit();
372376

373377
//printf("total bytes = %d\n", m_get_total_bytes_allocated());
374-
return 0;
378+
return ret;
375379
}
376380

377381
STATIC mp_obj_t mp_sys_exit(uint n_args, const mp_obj_t *args) {

0 commit comments

Comments
 (0)