8000 pyexec: Add named constants for raw repl control characters. · micropython/micropython@249cb20 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 249cb20

Browse files
committed
pyexec: Add named constants for raw repl control characters.
The raw repl language is gradually getting more complex. To structure the code a bit more, introduce some names for the different control characters and the "init command" sequence which is currently only used for starting a paste. Signed-off-by: Angus Gratton <gus@projectgus.com>
1 parent 378126c commit 249cb20

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

shared/runtime/pyexec.c

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,23 @@ STATIC bool repl_display_debugging_info = 0;
5959
#define EXEC_FLAG_SOURCE_IS_FILENAME (1 << 5)
6060
#define EXEC_FLAG_SOURCE_IS_READER (1 << 6)
6161

62-
#define NUM_ESCAPED 8 // This value has to match the value in tools/pyboard.py
62+
#define RAWCODE_PASTE_NUM_ESCAPED 8 // This value has to match the same constant in tools/pyboard.py
63+
64+
// Raw REPL serial protocol control sequences
65+
#define RAW_REPL_CTRL_INIT CHAR_CTRL_A
66+
#define RAW_REPL_CTRL_EXIT_TO_FRIENDLY CHAR_CTRL_B
67+
#define RAW_REPL_CTRL_CLEAR_LINE CHAR_CTRL_C
68+
#define RAW_REPL_CTRL_EOF CHAR_CTRL_D
69+
#define RAW_REPL_CTRL_INIT_CMD CHAR_CTRL_E
70+
// CHAR_CTRL_F is recognised in raw paste mode (as an escape sequence), but not in raw REPL mode
71+
72+
// Sequence ^A ^E (RAW_REPL_CTRL_INIT, RAW_REPL_CTRL_INIT_CMD) can initiate one or more "init commands" based on the next
73+
// character in the sequence:
74+
#define RAW_REPL_INIT_CMD_PASTE_SOURCE 'A'
75+
#define RAW_REPL_INIT_CMD_PASTE_RAWCODE 'B'
76+
77+
#define RAW_REPL_INIT_CMD_RESP_UNSUPPORTED "R\x00"
78+
#define RAW_REPL_INIT_CMD_RESP_OK "R\x01"
6379

6480
// parses, compiles and executes the code in the lexer
6581
// frees the lexer before returning
@@ -234,9 +250,9 @@ STATIC mp_uint_t mp_reader_stdin_readbyte(void *data) {
234250
return MP_READER_EOF;
235251
}
236252
} else if (c == CHAR_CTRL_F) {
237-
// escape sequence, next character is escaped by adding NUM_ESCAPED to it
253+
// escape sequence, next character is escaped by adding RAWCODE_PASTE_NUM_ESCAPED to it
238254
int e = mp_hal_stdin_rx_chr();
239-
c = e - NUM_ESCAPED;
255+
c = e - RAWCODE_PASTE_NUM_ESCAPED;
240256
}
241257

242258
if (--reader->window_remain == 0) {
@@ -277,30 +293,33 @@ STATIC void mp_reader_new_stdin(mp_reader_t *reader, mp_reader_stdin_t *reader_s
277293
reader->close = mp_reader_stdin_close;
278294
}
279295

280-
STATIC int do_reader_stdin(int c) {
281-
bool supported_command = c == 'A';
296+
STATIC int handle_raw_repl_init_cmd(int c) {
297+
int exec_flags = EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_READER;
298+
bool supported_command = false;
299+
300+
if (c == RAW_REPL_INIT_CMD_PASTE_SOURCE) {
301+
supported_command = true;
302+
}
282303
#if MICROPY_PERSISTENT_CODE_LOAD
283-
supported_command = (c == 'B') || supported_command;
304+
if (c == RAW_REPL_INIT_CMD_PASTE_RAWCODE) {
305+
exec_flags |= EXEC_FLAG_SOURCE_IS_RAW_CODE;
306+
supported_command = true;
307+
}
284308
#endif
285309

286310
if (!supported_command) {
287311
// Unsupported command.
288-
mp_hal_stdout_tx_strn("R\x00", 2);
312+
mp_hal_stdout_tx_strn(RAW_REPL_INIT_CMD_RESP_UNSUPPORTED, 2);
289313
return 0;
290314
}
291315

292316
// Indicate reception of command.
293-
mp_hal_stdout_tx_strn("R\x01", 2);
317+
mp_hal_stdout_tx_strn(RAW_REPL_INIT_CMD_RESP_OK, 2);
294318

295319
// Entering raw paste mode
296-
// c == 'A' input is source, c == 'B' input is bytecode
297320
mp_reader_t reader;
298321
mp_reader_stdin_t reader_stdin;
299322
mp_reader_new_stdin(&reader, &reader_stdin, MICROPY_REPL_STDIN_BUFFER_MAX);
300-
int exec_flags = EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_READER;
301-
if (c == 'B') {
302-
exec_flags |= EXEC_FLAG_SOURCE_IS_RAW_CODE;
303-
}
304323
return parse_compile_execute(&reader, MP_PARSE_FILE_INPUT, exec_flags);
305324
}
306325

@@ -335,30 +354,30 @@ void pyexec_event_repl_init(void) {
335354
}
336355

337356
STATIC int pyexec_raw_repl_process_char(int c) {
338-
if (c == CHAR_CTRL_A) {
357+
if (c == RAW_REPL_CTRL_INIT) {
339358
// reset raw REPL
340-
if (vstr_len(MP_STATE_VM(repl_line)) == 2 && vstr_str(MP_STATE_VM(repl_line))[0] == CHAR_CTRL_E) {
341-
int ret = do_reader_stdin(vstr_str(MP_STATE_VM(repl_line))[1]);
359+
if (vstr_len(MP_STATE_VM(repl_line)) == 2 && vstr_str(MP_STATE_VM(repl_line))[0] == RAW_REPL_CTRL_INIT_CMD) {
360+
int ret = handle_raw_repl_init_cmd(vstr_str(MP_STATE_VM(repl_line))[1]);
342361
if (ret & PYEXEC_FORCED_EXIT) {
343362
return ret;
344363
}
345364
goto reset;
346365
}
347366
mp_hal_stdout_tx_str("raw REPL; CTRL-B to exit\r\n");
348367
goto reset;
349-
} else if (c == CHAR_CTRL_B) {
368+
} else if (c == RAW_REPL_CTRL_EXIT_TO_FRIENDLY) {
350369
// change to friendly REPL
351370
pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
352371
vstr_reset(MP_STATE_VM(repl_line));
353372
repl.cont_line = false;
354373
repl.paste_mode = false;
355374
pyexec_friendly_repl_process_char(CHAR_CTRL_B);
356375
return 0;
357-
} else if (c == CHAR_CTRL_C) {
376+
} else if (c == RAW_REPL_CTRL_CLEAR_LINE) {
358377
F438 // clear line
359378
vstr_reset(MP_STATE_VM(repl_line));
360379
return 0;
361-
} else if (c == CHAR_CTRL_D) {
380+
} else if (c == RAW_REPL_CTRL_EOF) {
362381
// input finished
363382
} else {
364383
// let through any other raw 8-bit value
@@ -419,7 +438,7 @@ STATIC int pyexec_friendly_repl_process_char(int c) {
419438

420439
if (!repl.cont_line) {
421440

422-
if (ret == CHAR_CTRL_A) {
441+
if (ret == RAW_REPL_CTRL_INIT) {
423442
// change to raw REPL
424443
pyexec_mode_kind = PYEXEC_MODE_RAW_REPL;
425444
mp_hal_stdout_tx_str("\r\n");
@@ -529,10 +548,10 @@ int pyexec_raw_repl(void) {
529548
mp_hal_stdout_tx_str(">");
530549
for (;;) {
531550
int c = mp_hal_stdin_rx_chr();
532-
if (c == CHAR_CTRL_A) {
551+
if (c == RAW_REPL_CTRL_INIT) {
533552
// reset raw REPL
534-
if (vstr_len(&line) == 2 && vstr_str(&line)[0] == CHAR_CTRL_E) {
535-
int ret = do_reader_stdin(vstr_str(&line)[1]);
553+
if (vstr_len(&line) == 2 && vstr_str(&line)[0] == RAW_REPL_CTRL_INIT_CMD) {
554+
int ret = handle_raw_repl_init_cmd(vstr_str(&line)[1]);
536555
if (ret & PYEXEC_FORCED_EXIT) {
537556
return ret;
538557
}
@@ -541,16 +560,16 @@ int pyexec_raw_repl(void) {
541560
continue;
542561
}
543562
goto raw_repl_reset;
544-
} else if (c == CHAR_CTRL_B) {
563+
} else if (c == RAW_REPL_CTRL_EXIT_TO_FRIENDLY) {
545564
// change to friendly REPL
546565
mp_hal_stdout_tx_str("\r\n");
547566
vstr_clear(&line);
548567
pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
549568
return 0;
550-
} else if (c == CHAR_CTRL_C) {
569+
} else if (c == RAW_REPL_CTRL_CLEAR_LINE) {
551570
// clear line
552571
vstr_reset(&line);
553-
} else if (c == CHAR_CTRL_D) {
572+
} else if (c == RAW_REPL_CTRL_EOF) {
554573
// input finished
555574
break;
556575
} else {
@@ -632,7 +651,7 @@ int pyexec_friendly_repl(void) {
632651
int ret = readline(&line, mp_repl_get_ps1());
633652
mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
634653

635-
if (ret == CHAR_CTRL_A) {
654+
if (ret == RAW_REPL_CTRL_INIT) {
636655
// change to raw REPL
637656
mp_hal_stdout_tx_str("\r\n");
638657
vstr_clear(&line);

tools/pyboard.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,10 @@ def raw_paste_write(self, command_bytes):
407407
# escape any characters that need to be escaped. Note this doesn't
408408
# count towards the window size, as unescaping happens before filling
409409
# the window buffer in the device
410-
NUM_ESCAPED = 8 # this values has to match value in pyexec.c
410+
RAWCODE_PASTE_NUM_ESCAPED = 8 # value has to match the same constant in pyexec.c
411411
b = re.sub(
412-
rb"[" + bytes(range(NUM_ESCAPED)) + rb"]",
413-
lambda c: bytes((0x06, c.group()[0] + NUM_ESCAPED)),
412+
rb"[" + bytes(range(RAWCODE_PASTE_NUM_ESCAPED)) + rb"]",
413+
lambda c: bytes((0x06, c.group()[0] + RAWCODE_PASTE_NUM_ESCAPED)),
414414
b,
415415
)
416416

0 commit comments

Comments
 (0)
0