Content-Length: 367075 | pFad | http://github.com/micropython/micropython/commit/6a61e4ecd12c8d780bd2e09e017c2ee0eab3ee3e

6B mpy-cross: Allow reading from stdin and writing to stdout. · micropython/micropython@6a61e4e · GitHub
Skip to content

Commit 6a61e4e

Browse files
arbraunsdpgeorge
authored andcommitted
mpy-cross: Allow reading from stdin and writing to stdout.
Signed-off-by: Armin Brauns <armin.brauns@embedded-solutions.at>
1 parent 625e03d commit 6a61e4e

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

mpy-cross/main.c

+28-10
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ mp_uint_t mp_verbose_flag = 0;
4848
// Make it larger on a 64 bit machine, because pointers are larger.
4949
long heap_size = 1024 * 1024 * (sizeof(mp_uint_t) / 4);
5050

51+
STATIC void stdout_print_strn(void *env, const char *str, size_t len) {
52+
(void)env;
53+
ssize_t dummy = write(STDOUT_FILENO, str, len);
54+
(void)dummy;
55+
}
56+
57+
STATIC const mp_print_t mp_stdout_print = {NULL, stdout_print_strn};
58+
5159
STATIC void stderr_print_strn(void *env, const char *str, size_t len) {
5260
(void)env;
5361
ssize_t dummy = write(STDERR_FILENO, str, len);
@@ -59,7 +67,12 @@ STATIC const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
5967
STATIC int compile_and_save(const char *file, const char *output_file, const char *source_file) {
6068
nlr_buf_t nlr;
6169
if (nlr_push(&nlr) == 0) {
62-
mp_lexer_t *lex = mp_lexer_new_from_file(file);
70+
mp_lexer_t *lex;
71+
if (strcmp(file, "-") == 0) {
72+
lex = mp_lexer_new_from_fd(MP_QSTR__lt_stdin_gt_, STDIN_FILENO, false);
73+
} else {
74+
lex = mp_lexer_new_from_file(file);
75+
}
6376

6477
qstr source_name;
6578
if (source_file == NULL) {
@@ -77,17 +90,22 @@ STATIC int compile_and_save(const char *file, const char *output_file, const cha
7790
cm.context = m_new_obj(mp_module_context_t);
7891
mp_compile_to_raw_code(&parse_tree, source_name, false, &cm);
7992

80-
vstr_t vstr;
81-
vstr_init(&vstr, 16);
82-
if (output_file == NULL) {
83-
vstr_add_str(&vstr, file);
84-
vstr_cut_tail_bytes(&vstr, 2);
85-
vstr_add_str(&vstr, "mpy");
93+
if (output_file != NULL && strcmp(output_file, "-") == 0) {
94+
mp_raw_code_save(&cm, (mp_print_t *)&mp_stdout_print);
8695
} else {
87-
vstr_add_str(&vstr, output_file);
96+
vstr_t vstr;
97+
vstr_init(&vstr, 16);
98+
if (output_file == NULL) {
99+
vstr_add_str(&vstr, file);
100+
vstr_cut_tail_bytes(&vstr, 2);
101+
vstr_add_str(&vstr, "mpy");
102+
} else {
103+
vstr_add_str(&vstr, output_file);
104+
}
105+
106+
mp_raw_code_save_file(&cm, vstr_null_terminated_str(&vstr));
107+
vstr_clear(&vstr);
88108
}
89-
mp_raw_code_save_file(&cm, vstr_null_terminated_str(&vstr));
90-
vstr_clear(&vstr);
91109

92110
nlr_pop();
93111
return 0;

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/micropython/micropython/commit/6a61e4ecd12c8d780bd2e09e017c2ee0eab3ee3e

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy