Index: src/OFStdIOStream+Private.h ================================================================== --- src/OFStdIOStream+Private.h +++ src/OFStdIOStream+Private.h @@ -17,9 +17,15 @@ #import "OFStdIOStream.h" OF_ASSUME_NONNULL_BEGIN @interface OFStdIOStream () +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) - (instancetype)of_initWithFileDescriptor: (int)fd OF_METHOD_FAMILY(init); +#else +- (instancetype)of_initWithFileDescriptor: (long)handle + closable: (bool)closable + OF_METHOD_FAMILY(init); +#endif @end OF_ASSUME_NONNULL_END Index: src/OFStdIOStream.h ================================================================== --- src/OFStdIOStream.h +++ src/OFStdIOStream.h @@ -29,11 +29,16 @@ #ifdef OF_STDIO_STREAM_WIN32_CONSOLE_H OF_SUBCLASSING_RESTRICTED #endif @interface OFStdIOStream: OFStream { - int _fd; +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) + int _fd; +#else + long _fd; + bool _closable; +#endif bool _atEndOfStream; } - init OF_UNAVAILABLE; Index: src/OFStdIOStream.m ================================================================== --- src/OFStdIOStream.m +++ src/OFStdIOStream.m @@ -47,11 +47,10 @@ # define INVALID_FD 0 # define getpid() ((int)SysBase->ThisTask) # define read(fd, buf, len) Read(fd, buf, len) # define write(fd, buf, len) Write(fd, buf, len) -# define close(fd) Close(fd) extern struct ExecBase *SysBase; #endif #ifndef INVALID_FD @@ -68,10 +67,19 @@ #endif OFStdIOStream *of_stdin = nil; OFStdIOStream *of_stdout = nil; OFStdIOStream *of_stderr = nil; + +#if defined(OF_MORPHOS) && !defined(OF_IXEMUL) +OF_DESTRUCTOR() +{ + [of_stdin dealloc]; + [of_stdout dealloc]; + [of_stderr dealloc]; +} +#endif void of_log(OFConstantString *format, ...) { void *pool = objc_autoreleasePoolPush(); @@ -101,30 +109,76 @@ # if !defined(OF_MORPHOS) || defined(OF_IXEMUL) of_stdin = [[OFStdIOStream alloc] of_initWithFileDescriptor: 0]; of_stdout = [[OFStdIOStream alloc] of_initWithFileDescriptor: 1]; of_stderr = [[OFStdIOStream alloc] of_initWithFileDescriptor: 2]; # else - of_stdin = [[OFStdIOStream alloc] of_initWithFileDescriptor: Input()]; - of_stdout = [[OFStdIOStream alloc] of_initWithFileDescriptor: Output()]; - of_stderr = [[OFStdIOStream alloc] of_initWithFileDescriptor: Output()]; + BPTR input = Input(), output = Output(); + BPTR error = ((struct Process *)SysBase->ThisTask)->pr_CES; + bool inputClosable = false, outputClosable = false, + errorClosable = false; + + if (input == INVALID_FD) { + input = Open("*", MODE_OLDFILE); + inputClosable = true; + } + + if (output == INVALID_FD) { + output = Open("*", MODE_OLDFILE); + outputClosable = true; + } + + if (error == INVALID_FD) { + error = Open("*", MODE_OLDFILE); + errorClosable = true; + } + + of_stdin = [[OFStdIOStream alloc] + of_initWithFileDescriptor: input + closable: inputClosable]; + of_stdout = [[OFStdIOStream alloc] + of_initWithFileDescriptor: output + closable: outputClosable]; + of_stderr = [[OFStdIOStream alloc] + of_initWithFileDescriptor: error + closable: errorClosable]; # endif } #endif - init { OF_INVALID_INIT_METHOD } +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) - (instancetype)of_initWithFileDescriptor: (int)fd { self = [super init]; _fd = fd; return self; } +#else +- (instancetype)of_initWithFileDescriptor: (long)fd + closable: (bool)closable +{ + self = [super init]; + + _fd = fd; + _closable = closable; + + return self; +} +#endif + +- (void)dealloc +{ + [self close]; + + [super dealloc]; +} - (bool)lowlevelIsAtEndOfStream { if (_fd == INVALID_FD) return true; @@ -200,12 +254,17 @@ } #endif - (void)close { +#if !defined(OF_MORPHOS) || defined(OF_IXEMUL) if (_fd != INVALID_FD) close(_fd); +#else + if (_closable && _fd != INVALID_FD) + Close(_fd); +#endif _fd = INVALID_FD; [super close]; } @@ -227,15 +286,10 @@ - (unsigned int)retainCount { return OF_RETAIN_COUNT_MAX; } -- (void)dealloc -{ - OF_DEALLOC_UNSUPPORTED -} - - (int)columns { #if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) struct winsize ws;