Skip to content

Commit b4fc464

Browse files
siddacioustannewt
authored andcommitted
support for doubled file extensions ala issue adafruit#383 (adafruit#415)
Fixes adafruit#383
1 parent 1c12e9a commit b4fc464

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

main.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,21 @@ void reset_mp(void) {
9898

9999
mp_obj_list_init(mp_sys_argv, 0);
100100
}
101+
#define STRING_LIST(...) {__VA_ARGS__, ""}
101102

102-
bool maybe_run(const char* filename, pyexec_result_t* exec_result) {
103-
mp_import_stat_t stat = mp_import_stat(filename);
104-
if (stat != MP_IMPORT_STAT_FILE) {
105-
return false;
103+
bool maybe_run_list(const char ** filenames, pyexec_result_t* exec_result) {
104+
105+
for (int i = 0; filenames[i] != (char*)""; i++) {
106+
mp_import_stat_t stat = mp_import_stat(filenames[i]);
107+
if (stat != MP_IMPORT_STAT_FILE) {
108+
continue;
109+
}
110+
serial_write(filenames[i]);
111+
serial_write(MSG_OUTPUT_SUFFIX);
112+
pyexec_file(filenames[i], exec_result);
113+
return true;
106114
}
107-
serial_write(filename);
108-
serial_write(MSG_OUTPUT_SUFFIX);
109-
pyexec_file(filename, exec_result);
110-
return true;
115+
return false;
111116
}
112117

113118
bool start_mp(safe_mode_t safe_mode) {
@@ -132,10 +137,17 @@ bool start_mp(safe_mode_t safe_mode) {
132137
serial_write(MSG_SAFE_MODE_NO_MAIN);
133138
} else {
134139
new_status_color(MAIN_RUNNING);
135-
found_main = maybe_run("code.txt", &result) ||
136-
maybe_run("code.py", &result) ||
137-
maybe_run("main.py", &result) ||
138-
maybe_run("main.txt", &result);
140+
141+
const char *supported_filenames[] = STRING_LIST("code.txt", "code.py", "main.py", "main.txt");
142+
const char *double_extension_filenames[] = STRING_LIST("code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
143+
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
144+
found_main = maybe_run_list(supported_filenames, &result);
145+
if (!found_main){
146+
found_main = maybe_run_list(double_extension_filenames, &result);
147+
if (found_main) {
148+
serial_write(MSG_DOUBLE_FILE_EXTENSION);
149+
}
150+
}
139151
reset_status_led();
140152

141153
if (result.return_code & PYEXEC_FORCED_EXIT) {
@@ -253,10 +265,8 @@ int __attribute__((used)) main(void) {
253265
#endif
254266

255267
// TODO(tannewt): Re-add support for flashing boot error output.
256-
bool found_boot = maybe_run("settings.txt", NULL) ||
257-
maybe_run("settings.py", NULL) ||
258-
maybe_run("boot.py", NULL) ||
259-
maybe_run("boot.txt", NULL);
268+
static const char *filenames[] = STRING_LIST("settings.txt", "settings.py", "boot.py", "boot.txt");
269+
bool found_boot = maybe_run_list(filenames, NULL);
260270
(void) found_boot;
261271

262272
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE

supervisor/messages/default.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@
9292
#define MSG_SOFT_REBOOT "soft reboot"
9393
#endif
9494

95+
#ifndef MSG_DOUBLE_FILE_EXTENSION
96+
#define MSG_DOUBLE_FILE_EXTENSION "WARNING: Your code filename has two extensions\r\n"
97+
#endif
98+
9599
#endif // MICROPY_SUPERVISOR_MESSAGES_DEFAULT_H

0 commit comments

Comments
 (0)