NSTask fails to show text message on command prompt
The issue may be unrelated to NSTask itself but come from the command prompt environment. Here is a small sample that repeats the issue:
#import <Foundation/Foundation.h>
int main(void)
{
NSAutoreleasePool *pool = \
[[NSAutoreleasePool alloc] init];
if (!pool)
return 1;
NSTask *task = nil;
NSString *command = \
[NSString stringWithString: @"cmd.exe"];
NSArray *arguments = \
[NSArray arrayWithObjects: \
@"/c", @"dir", nil];
task = [[[NSTask alloc] init] autorelease];
if (!task)
goto ERROR_MAIN;
[task setLaunchPath: command];
[task setArguments: arguments];
[task launch];
/* Block current program. */
[task waitUntilExit];
int status = [task terminationStatus];
[pool drain];
return status;
ERROR_MAIN:
[pool drain];
return 1;
}
On MinGW console of MSYS2, the program successes to show the message emitted by dir. Nevertheless, on command prompt, the same program fails to show the message emitted by dir.
I think this is more an issue for cmd.exe than one in GNUstep.
Looking at the code to create a subtask I see:
// |DETACHED_PROCESS /* We don't set the DETACHED_PROCESS flag as it actually means that the
- child task should get a new Console allocated ... and that means it
- will pop up a console window ... which looks really bad. */
I suspect that the behavior you are looking for is to have the task created as a detached process with its own console attached, so that cmd.exe can display in that console, but if the code did that, then we'd have consoles created for tasks that don't need/want them.
You could try uncommenting the line to set the DETACHED_PROCESS flag and see if that does what you want. If it does, and you thing it's a worthwhile option to have, you could add a windows-specific extension method to control it.