2021-08-21 17:39:54 +02:00
|
|
|
#include "platform.h"
|
|
|
|
#include "linput.h"
|
|
|
|
#include "lua.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include <stdio.h>
|
Initial support for ESP32-C6 and ESP32-H2, plus assorted fixes & improvements (#3646)
* Proof-of-concept multi-type console support via stdio
* Address crashes on linput's use of printf.
On an empty line input, a C3 with UART console would panic while attempting
to output the new Lua prompt. The backtrace shows a xQueueSemaphoreTake
with uxItemSize==0 as the panic cause, deep inside the uart driver, invoked
via vfs_uart and vfs_console layers, from printf.
Similarly, the printf for outputting a backspace/erase sequence would also
trigger a panic.
This workaround (of not mixing fflush() with printf) is likely merely hiding
a deeper issue, but it appears to be consistent. Plus, printf with no args
and a user-supplied format string is a no-no and should be fixed anyway.
* Work around IDF inconsistency with stdout buffering.
* Increase console task stack size.
Seems on Xtensa it ended up not being enough.
* Switch to single-byte console reads.
* Stop cheating and feed Lua from the right context.
* Work around IDF buffering stdout even when told not to, on ACM consoles.
* Initial build support for esp32c6.
Plus fixup of module selection for a variety of targets.
* Update github actions to node 20 versions.
* Update github build to deal with Lua 5.3 being default.
* Address fatal compiler warning.
Newer IDF toolchain is stricter, and we'd apparently failed to build test
the Lua-5.1 path for some time.
* Initial build support for esp32h2.
* Upgrade IDF to v5.1.3
* Fix left-over incorrect type in uzlib.
* Avoid null pointer crashes when debugging startup.
* Workaround for using wifi module on S2 with USB-CDC console.
---------
Co-authored-by: Jade Mattsson <github@frozenlogic.org>
2024-04-26 23:35:22 +02:00
|
|
|
#include <string.h>
|
2021-08-21 17:39:54 +02:00
|
|
|
|
|
|
|
static struct input_state {
|
|
|
|
char *data;
|
|
|
|
int line_pos;
|
|
|
|
size_t len;
|
|
|
|
const char *prompt;
|
|
|
|
char last_nl_char;
|
Initial support for ESP32-C6 and ESP32-H2, plus assorted fixes & improvements (#3646)
* Proof-of-concept multi-type console support via stdio
* Address crashes on linput's use of printf.
On an empty line input, a C3 with UART console would panic while attempting
to output the new Lua prompt. The backtrace shows a xQueueSemaphoreTake
with uxItemSize==0 as the panic cause, deep inside the uart driver, invoked
via vfs_uart and vfs_console layers, from printf.
Similarly, the printf for outputting a backspace/erase sequence would also
trigger a panic.
This workaround (of not mixing fflush() with printf) is likely merely hiding
a deeper issue, but it appears to be consistent. Plus, printf with no args
and a user-supplied format string is a no-no and should be fixed anyway.
* Work around IDF inconsistency with stdout buffering.
* Increase console task stack size.
Seems on Xtensa it ended up not being enough.
* Switch to single-byte console reads.
* Stop cheating and feed Lua from the right context.
* Work around IDF buffering stdout even when told not to, on ACM consoles.
* Initial build support for esp32c6.
Plus fixup of module selection for a variety of targets.
* Update github actions to node 20 versions.
* Update github build to deal with Lua 5.3 being default.
* Address fatal compiler warning.
Newer IDF toolchain is stricter, and we'd apparently failed to build test
the Lua-5.1 path for some time.
* Initial build support for esp32h2.
* Upgrade IDF to v5.1.3
* Fix left-over incorrect type in uzlib.
* Avoid null pointer crashes when debugging startup.
* Workaround for using wifi module on S2 with USB-CDC console.
---------
Co-authored-by: Jade Mattsson <github@frozenlogic.org>
2024-04-26 23:35:22 +02:00
|
|
|
} ins = {
|
|
|
|
.prompt = "? ", // prompt should never be allowed to be null
|
|
|
|
};
|
2021-08-21 17:39:54 +02:00
|
|
|
|
|
|
|
#define NUL '\0'
|
|
|
|
#define BS '\010'
|
|
|
|
#define CR '\r'
|
|
|
|
#define LF '\n'
|
|
|
|
#define DEL 0x7f
|
|
|
|
#define BS_OVER "\010 \010"
|
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
bool input_echo = true;
|
|
|
|
bool run_input = true;
|
2021-08-21 17:39:54 +02:00
|
|
|
|
Initial support for ESP32-C6 and ESP32-H2, plus assorted fixes & improvements (#3646)
* Proof-of-concept multi-type console support via stdio
* Address crashes on linput's use of printf.
On an empty line input, a C3 with UART console would panic while attempting
to output the new Lua prompt. The backtrace shows a xQueueSemaphoreTake
with uxItemSize==0 as the panic cause, deep inside the uart driver, invoked
via vfs_uart and vfs_console layers, from printf.
Similarly, the printf for outputting a backspace/erase sequence would also
trigger a panic.
This workaround (of not mixing fflush() with printf) is likely merely hiding
a deeper issue, but it appears to be consistent. Plus, printf with no args
and a user-supplied format string is a no-no and should be fixed anyway.
* Work around IDF inconsistency with stdout buffering.
* Increase console task stack size.
Seems on Xtensa it ended up not being enough.
* Switch to single-byte console reads.
* Stop cheating and feed Lua from the right context.
* Work around IDF buffering stdout even when told not to, on ACM consoles.
* Initial build support for esp32c6.
Plus fixup of module selection for a variety of targets.
* Update github actions to node 20 versions.
* Update github build to deal with Lua 5.3 being default.
* Address fatal compiler warning.
Newer IDF toolchain is stricter, and we'd apparently failed to build test
the Lua-5.1 path for some time.
* Initial build support for esp32h2.
* Upgrade IDF to v5.1.3
* Fix left-over incorrect type in uzlib.
* Avoid null pointer crashes when debugging startup.
* Workaround for using wifi module on S2 with USB-CDC console.
---------
Co-authored-by: Jade Mattsson <github@frozenlogic.org>
2024-04-26 23:35:22 +02:00
|
|
|
void input_setprompt (const char *prompt) {
|
|
|
|
if (prompt == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: attempted to set a null prompt?!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ins.prompt = prompt;
|
|
|
|
}
|
|
|
|
|
2021-08-21 17:39:54 +02:00
|
|
|
/*
|
|
|
|
** The input state (ins) is private, so input_setup() exposes the necessary
|
|
|
|
** access to public properties and is called in user_init() before the Lua
|
|
|
|
** enviroment is initialised.
|
|
|
|
*/
|
|
|
|
void input_setup(int bufsize, const char *prompt) {
|
|
|
|
// Initialise non-zero elements
|
|
|
|
ins.data = malloc(bufsize);
|
|
|
|
ins.len = bufsize;
|
Initial support for ESP32-C6 and ESP32-H2, plus assorted fixes & improvements (#3646)
* Proof-of-concept multi-type console support via stdio
* Address crashes on linput's use of printf.
On an empty line input, a C3 with UART console would panic while attempting
to output the new Lua prompt. The backtrace shows a xQueueSemaphoreTake
with uxItemSize==0 as the panic cause, deep inside the uart driver, invoked
via vfs_uart and vfs_console layers, from printf.
Similarly, the printf for outputting a backspace/erase sequence would also
trigger a panic.
This workaround (of not mixing fflush() with printf) is likely merely hiding
a deeper issue, but it appears to be consistent. Plus, printf with no args
and a user-supplied format string is a no-no and should be fixed anyway.
* Work around IDF inconsistency with stdout buffering.
* Increase console task stack size.
Seems on Xtensa it ended up not being enough.
* Switch to single-byte console reads.
* Stop cheating and feed Lua from the right context.
* Work around IDF buffering stdout even when told not to, on ACM consoles.
* Initial build support for esp32c6.
Plus fixup of module selection for a variety of targets.
* Update github actions to node 20 versions.
* Update github build to deal with Lua 5.3 being default.
* Address fatal compiler warning.
Newer IDF toolchain is stricter, and we'd apparently failed to build test
the Lua-5.1 path for some time.
* Initial build support for esp32h2.
* Upgrade IDF to v5.1.3
* Fix left-over incorrect type in uzlib.
* Avoid null pointer crashes when debugging startup.
* Workaround for using wifi module on S2 with USB-CDC console.
---------
Co-authored-by: Jade Mattsson <github@frozenlogic.org>
2024-04-26 23:35:22 +02:00
|
|
|
// Call to get the prompt error checking
|
|
|
|
input_setprompt(prompt);
|
2021-08-21 17:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
size_t feed_lua_input(const char *buf, size_t n)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
|
|
|
char ch = buf[i];
|
|
|
|
if (!run_input) // We're no longer interested in the remaining bytes
|
|
|
|
return i;
|
|
|
|
|
|
|
|
/* handle CR & LF characters and aggregate \n\r and \r\n pairs */
|
|
|
|
char tmp_last_nl_char = ins.last_nl_char;
|
|
|
|
/* handle CR & LF characters
|
|
|
|
filters second char of LF&CR (\n\r) or CR&LF (\r\n) sequences */
|
|
|
|
if ((ch == CR && tmp_last_nl_char == LF) || // \n\r sequence -> skip \r
|
|
|
|
(ch == LF && tmp_last_nl_char == CR)) // \r\n sequence -> skip \n
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-21 17:39:54 +02:00
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
/* backspace key */
|
|
|
|
if (ch == DEL || ch == BS) {
|
|
|
|
if (ins.line_pos > 0) {
|
Initial support for ESP32-C6 and ESP32-H2, plus assorted fixes & improvements (#3646)
* Proof-of-concept multi-type console support via stdio
* Address crashes on linput's use of printf.
On an empty line input, a C3 with UART console would panic while attempting
to output the new Lua prompt. The backtrace shows a xQueueSemaphoreTake
with uxItemSize==0 as the panic cause, deep inside the uart driver, invoked
via vfs_uart and vfs_console layers, from printf.
Similarly, the printf for outputting a backspace/erase sequence would also
trigger a panic.
This workaround (of not mixing fflush() with printf) is likely merely hiding
a deeper issue, but it appears to be consistent. Plus, printf with no args
and a user-supplied format string is a no-no and should be fixed anyway.
* Work around IDF inconsistency with stdout buffering.
* Increase console task stack size.
Seems on Xtensa it ended up not being enough.
* Switch to single-byte console reads.
* Stop cheating and feed Lua from the right context.
* Work around IDF buffering stdout even when told not to, on ACM consoles.
* Initial build support for esp32c6.
Plus fixup of module selection for a variety of targets.
* Update github actions to node 20 versions.
* Update github build to deal with Lua 5.3 being default.
* Address fatal compiler warning.
Newer IDF toolchain is stricter, and we'd apparently failed to build test
the Lua-5.1 path for some time.
* Initial build support for esp32h2.
* Upgrade IDF to v5.1.3
* Fix left-over incorrect type in uzlib.
* Avoid null pointer crashes when debugging startup.
* Workaround for using wifi module on S2 with USB-CDC console.
---------
Co-authored-by: Jade Mattsson <github@frozenlogic.org>
2024-04-26 23:35:22 +02:00
|
|
|
if(input_echo) fwrite(BS_OVER, strlen(BS_OVER), 1, stdout);
|
2021-08-23 15:00:54 +02:00
|
|
|
ins.line_pos--;
|
2021-08-21 17:39:54 +02:00
|
|
|
}
|
2021-08-23 15:00:54 +02:00
|
|
|
ins.data[ins.line_pos] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2021-08-21 17:39:54 +02:00
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
/* end of data */
|
|
|
|
if (ch == CR || ch == LF) {
|
|
|
|
ins.last_nl_char = ch;
|
|
|
|
|
|
|
|
if (input_echo) putchar(LF);
|
|
|
|
if (ins.line_pos == 0) {
|
|
|
|
/* Get a empty line, then go to get a new line */
|
Initial support for ESP32-C6 and ESP32-H2, plus assorted fixes & improvements (#3646)
* Proof-of-concept multi-type console support via stdio
* Address crashes on linput's use of printf.
On an empty line input, a C3 with UART console would panic while attempting
to output the new Lua prompt. The backtrace shows a xQueueSemaphoreTake
with uxItemSize==0 as the panic cause, deep inside the uart driver, invoked
via vfs_uart and vfs_console layers, from printf.
Similarly, the printf for outputting a backspace/erase sequence would also
trigger a panic.
This workaround (of not mixing fflush() with printf) is likely merely hiding
a deeper issue, but it appears to be consistent. Plus, printf with no args
and a user-supplied format string is a no-no and should be fixed anyway.
* Work around IDF inconsistency with stdout buffering.
* Increase console task stack size.
Seems on Xtensa it ended up not being enough.
* Switch to single-byte console reads.
* Stop cheating and feed Lua from the right context.
* Work around IDF buffering stdout even when told not to, on ACM consoles.
* Initial build support for esp32c6.
Plus fixup of module selection for a variety of targets.
* Update github actions to node 20 versions.
* Update github build to deal with Lua 5.3 being default.
* Address fatal compiler warning.
Newer IDF toolchain is stricter, and we'd apparently failed to build test
the Lua-5.1 path for some time.
* Initial build support for esp32h2.
* Upgrade IDF to v5.1.3
* Fix left-over incorrect type in uzlib.
* Avoid null pointer crashes when debugging startup.
* Workaround for using wifi module on S2 with USB-CDC console.
---------
Co-authored-by: Jade Mattsson <github@frozenlogic.org>
2024-04-26 23:35:22 +02:00
|
|
|
fwrite(ins.prompt, strlen(ins.prompt), 1, stdout);
|
2021-08-23 15:00:54 +02:00
|
|
|
fflush(stdout);
|
|
|
|
} else {
|
|
|
|
ins.data[ins.line_pos++] = LF;
|
|
|
|
lua_input_string(ins.data, ins.line_pos);
|
2021-08-21 17:39:54 +02:00
|
|
|
ins.line_pos = 0;
|
|
|
|
}
|
2021-08-23 15:00:54 +02:00
|
|
|
continue;
|
2021-08-21 17:39:54 +02:00
|
|
|
}
|
2021-08-23 15:00:54 +02:00
|
|
|
else
|
|
|
|
ins.last_nl_char = NUL;
|
2021-08-21 17:39:54 +02:00
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
if(input_echo) putchar(ch);
|
2021-08-21 17:39:54 +02:00
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
/* it's a large line, discard it */
|
|
|
|
if ( ins.line_pos + 1 >= ins.len ){
|
|
|
|
ins.line_pos = 0;
|
2021-08-21 17:39:54 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 15:00:54 +02:00
|
|
|
ins.data[ins.line_pos++] = ch;
|
2021-08-21 17:39:54 +02:00
|
|
|
}
|
2021-08-23 15:00:54 +02:00
|
|
|
|
|
|
|
return n; // we consumed/buffered all the provided data
|
2021-08-21 17:39:54 +02:00
|
|
|
}
|