Skip to content

interpreters/berry: Fix raw console input#3632

Closed
speccy88 wants to merge 1 commit into
apache:masterfrom
speccy88:codex/fix-berry-console
Closed

interpreters/berry: Fix raw console input#3632
speccy88 wants to merge 1 commit into
apache:masterfrom
speccy88:codex/fix-berry-console

Conversation

@speccy88

Copy link
Copy Markdown
Contributor

Summary

Fix the Berry interactive console on raw NuttX terminals, including USB CDC ACM consoles that do not provide canonical input, CR-to-LF conversion, or driver echo.

The pinned Berry default port uses fgets(). On a raw console, Enter commonly arrives as CR rather than LF, so the REPL and input() wait indefinitely. Routing Berry through the shared NuttX readline implementation also leaks NSH history and tab completion into the interpreter.

This change:

  • adds an opt-in USE_NUTTX_CONSOLE reader to the pinned Berry source;
  • accepts CR, LF, and CRLF line endings;
  • handles a queued LF left by a CRLF command that launched Berry from NSH;
  • provides termios-aware local echo and BS/DEL editing without double echo;
  • treats ETX or an interrupted read as console termination so Ctrl-C returns to NSH;
  • keeps Berry input isolated from NSH readline history, completion, and prompt state; and
  • enables the reader in both Make and CMake builds.

The normal Berry fgets() path remains unchanged when USE_NUTTX_CONSOLE is not defined.

The equivalent source fix is also proposed upstream in berry-lang/berry#539. This local patch is needed for the Berry commit currently pinned by NuttX Apps.

Impact

  • User impact: Berry REPL and input() work on raw serial and USB CDC terminals.
  • Build impact: Berry alone receives one additional compile definition and pinned-source patch.
  • Other applications: unchanged; system/readline is not modified.
  • Compatibility: the existing stdio fallback is preserved.

Verification

Fruit Jam RP2350 hardware, minimal NuttX image over raw USB CDC ACM:

  • CR-only, LF-only, and CRLF input all complete.
  • A CRLF launch no longer leaves an empty line for the first input() call.
  • Piped berry\r\n6*7\r\n preserves the first expression byte and evaluates to 42.
  • CRLF 10+5 evaluates to 15 with one following prompt.
  • A following CR-only 2+3 evaluates to 5.
  • LF-only empty input remains valid.
  • Berry input is not recalled by NSH history, and Tab does not expose NSH completions.
  • Ctrl-C exits Berry and returns to the NSH prompt.

Build and local checks:

  • Exact patched default/be_port.c blob: 7d52aebe055b098febec472f32690c1ef71780e3.
  • Strict standalone Berry build with -Wall -Wextra -std=c99 -pedantic-errors -DUSE_NUTTX_CONSOLE passed.
  • sim:berry Make build passed; berry -e "print(6*7)" returned 42.
  • CMake FetchContent applied both patches and compiled the final be_port.c successfully; the full sim:berry CMake/Ninja path passed before the final hardware-only handoff addition.
  • Pipe and raw-PTY tests covered CR, LF, delayed CRLF, non-LF after CR, empty LF input, BS, DEL, ETX, queued first input, echo, and input() prompt flushing.
  • git diff --check passed.
  • NuttX checkpatch.sh and cmake-format passed.

Use an opt-in NuttX console reader so Berry accepts CR, LF, and CRLF on raw CDC ACM consoles without sharing NSH history or completion state.

Wire the reader into both Make and CMake builds while keeping the default fgets path unchanged.

Signed-off-by: Frederick Blais <fred_blais5@hotmail.com>
@speccy88 speccy88 force-pushed the codex/fix-berry-console branch from c2c9a13 to c5b332e Compare July 11, 2026 22:06
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@speccy88 could you explain more why the process in serial driver framework doesn't work as expect? The basic process(\r, \n, DEL, BACK) should be handled by driver directly:
https://github.com/apache/nuttx/blob/master/drivers/serial/serial.c#L1001-L1125

@linguini1 linguini1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @speccy88 , just a notice of the contribution guide update; if you're using generative tooling to help your development please be sure to indicate the model using the Assisted-by: commit field! Ignore if you're not :)

@speccy88

Copy link
Copy Markdown
Contributor Author

Thanks for questioning the scope of this change. I retraced the reproducer and found that the behavior was real in my downstream Fruit Jam image, but the root cause was in that integration rather than in Berry or stock NuttX.

My downstream NSH console patch configured the shared USB CDC descriptor with c_iflag = 0 and c_lflag = ISIG so readline could own input and echo permanently. Berry then inherited the same raw descriptor, without canonical buffering or CR-to-LF translation, and its normal fgets() input appeared to hang.

I have corrected the terminal lifecycle at the point where I introduced the regression in speccy88/NuttX-RP2350#4: the terminal remains canonical and echo-enabled for applications, while NSH readline temporarily takes byte-at-a-time/no-driver-echo ownership and restores the complete saved mode before launching a command. The Berry-specific reader has been removed downstream.

This PR is therefore not pertinent to NuttX Apps, and I am closing it. Thank you for catching the scope problem before merge.

For transparency, Codex (GPT-5) assisted with diagnosis, implementation, and review. I reviewed the result and remain responsible for the contribution. Future AI-assisted Apache commits from me will include the required Assisted-by: Codex:GPT-5 trailer alongside my own human sign-off.

@speccy88 speccy88 closed this Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants