11#include <stdio.h>
22#include <string.h>
33#include <stdlib.h>
4- #include "uae_pragmas.h"
5-
64#include <proto/exec.h>
75#include <proto/dos.h>
86
7+ #include "uae_pragmas.h"
8+
99#define OUTBUFSIZE 4095
1010
11- static const char version [] = "$VER: Host-Shell v1.8 (2025-12-26)" ;
11+ static const char version [] = "$VER: Host-Shell v1.9 (2025-12-26)" ;
1212char outbuf [OUTBUFSIZE + 1 ];
1313
1414int main (int argc , char * argv [])
@@ -49,14 +49,42 @@ int main(int argc, char *argv[])
4949 return 10 ;
5050 }
5151
52+ BOOL esc_pending = FALSE;
53+
5254 while (!brk )
5355 {
5456 // Check for output from host
55- actual = HostShell_Read (handle , (UBYTE * )buffer , sizeof (buffer ) - 1 );
57+ actual = HostShell_Read (handle , (UBYTE * )buffer , sizeof (buffer ) - 2 );
5658 if (actual > 0 )
5759 {
58- buffer [actual ] = 0 ;
59- Write (out , buffer , actual );
60+ int outptr = 0 ;
61+ for (int i = 0 ; i < actual ; i ++ ) {
62+ unsigned char c = (unsigned char )buffer [i ];
63+ if (esc_pending ) {
64+ if (c == 0x5B ) { // '['
65+ outbuf [outptr ++ ] = 0x9B ; // CSI
66+ } else {
67+ outbuf [outptr ++ ] = 0x1B ; // Original ESC
68+ outbuf [outptr ++ ] = c ;
69+ }
70+ esc_pending = FALSE;
71+ } else {
72+ if (c == 0x1B ) {
73+ esc_pending = TRUE;
74+ } else {
75+ outbuf [outptr ++ ] = c ;
76+ }
77+ }
78+
79+ // Safety check for outbuf overflow (should rarely happen given the math)
80+ if (outptr >= OUTBUFSIZE ) {
81+ Write (out , outbuf , outptr );
82+ outptr = 0 ;
83+ }
84+ }
85+ if (outptr > 0 ) {
86+ Write (out , outbuf , outptr );
87+ }
6088 }
6189 else if (actual < 0 ) // Error or closed
6290 {
@@ -67,10 +95,27 @@ int main(int argc, char *argv[])
6795 // Wait up to 20ms (20000 microseconds)
6896 if (WaitForChar (in , 20000 ))
6997 {
70- actual = Read (in , buffer , sizeof (buffer ));
98+ // Read less than buffer size to allow for expansion (max 2x)
99+ actual = Read (in , buffer , 1024 );
71100 if (actual > 0 )
72101 {
73- HostShell_Write (handle , (UBYTE * )buffer , actual );
102+ int outptr = 0 ;
103+ for (int i = 0 ; i < actual ; i ++ ) {
104+ unsigned char c = (unsigned char )buffer [i ];
105+ if (c == 0x9B ) { // Amiga CSI
106+ // Convert to ANSI ESC [
107+ outbuf [outptr ++ ] = 0x1B ;
108+ outbuf [outptr ++ ] = 0x5B ;
109+ } else if (c == 0x08 ) { // Backspace
110+ // Convert BS (0x08) to DEL (0x7F)
111+ outbuf [outptr ++ ] = 0x7F ;
112+ } else {
113+ outbuf [outptr ++ ] = c ;
114+ }
115+ }
116+ if (outptr > 0 ) {
117+ HostShell_Write (handle , (UBYTE * )outbuf , outptr );
118+ }
74119 }
75120 else if (actual == 0 ) // EOF
76121 {
@@ -80,7 +125,10 @@ int main(int argc, char *argv[])
80125
81126 if (SetSignal (0 , 0 ) & SIGBREAKF_CTRL_C )
82127 {
83- brk = TRUE;
128+ SetSignal (0 , SIGBREAKF_CTRL_C ); // Clear the signal
129+ // Send Ctrl-C (ETX) to host
130+ char ctrlc = 0x03 ;
131+ HostShell_Write (handle , (UBYTE * )& ctrlc , 1 );
84132 }
85133 }
86134
0 commit comments