2001-12-17 14:31:42 +00:00
|
|
|
/** Implementation for NSPipe for GNUStep
|
1997-09-01 21:59:51 +00:00
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
Date: 1997
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1997-09-01 21:59:51 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1997-09-01 21:59:51 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-09 14:00:01 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
1997-09-01 21:59:51 +00:00
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
|
|
|
|
#define EXPOSE_NSPipe_IVARS 1
|
|
|
|
|
2010-02-17 11:47:06 +00:00
|
|
|
#import "Foundation/NSFileHandle.h"
|
|
|
|
#import "GSPrivate.h"
|
1997-12-11 19:09:56 +00:00
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
2003-06-27 14:34:13 +00:00
|
|
|
* <p>The NSPipe provides an encapsulation of the UNIX concept of pipe.<br />
|
2002-08-20 10:22:05 +00:00
|
|
|
* With NSPipe, it is possible to redirect the standard input or
|
|
|
|
* standard output.
|
2003-06-27 14:34:13 +00:00
|
|
|
* </p>
|
|
|
|
* <p>The file handles created by NSPipe are automatically closed when they
|
|
|
|
* are no longer in use (ie when the NSPipe instance is deallocated), so you
|
|
|
|
* don't need to close them explicitly.
|
|
|
|
* </p>
|
2002-08-20 10:22:05 +00:00
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
@implementation NSPipe
|
|
|
|
|
2002-08-20 10:22:05 +00:00
|
|
|
/**
|
|
|
|
* Returns a newly allocated and initialized NSPipe object that has been
|
|
|
|
* sent an autorelease message.
|
|
|
|
*/
|
2000-09-15 10:17:42 +00:00
|
|
|
+ (id) pipe
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2000-09-15 10:17:42 +00:00
|
|
|
return AUTORELEASE([[self alloc] init]);
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
2000-09-15 10:17:42 +00:00
|
|
|
- (void) dealloc
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
RELEASE(_readHandle);
|
|
|
|
RELEASE(_writeHandle);
|
2000-09-15 10:17:42 +00:00
|
|
|
[super dealloc];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
2000-09-15 10:17:42 +00:00
|
|
|
- (id) init
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
|
|
|
self = [super init];
|
2000-09-15 10:17:42 +00:00
|
|
|
if (self != nil)
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2016-03-09 13:16:16 +00:00
|
|
|
#ifndef _WIN32
|
1997-09-01 21:59:51 +00:00
|
|
|
int p[2];
|
|
|
|
|
|
|
|
if (pipe(p) == 0)
|
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
_readHandle = [[NSFileHandle alloc] initWithFileDescriptor: p[0]
|
2003-06-27 14:34:13 +00:00
|
|
|
closeOnDealloc: YES];
|
2010-02-14 10:48:10 +00:00
|
|
|
_writeHandle = [[NSFileHandle alloc] initWithFileDescriptor: p[1]
|
|
|
|
closeOnDealloc: YES];
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
2001-05-09 08:32:52 +00:00
|
|
|
else
|
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
NSLog(@"Failed to create pipe ... %@", [NSError _last]);
|
2001-05-09 08:32:52 +00:00
|
|
|
DESTROY(self);
|
|
|
|
}
|
2000-09-13 19:52:42 +00:00
|
|
|
#else
|
2005-02-23 16:05:09 +00:00
|
|
|
SECURITY_ATTRIBUTES saAttr;
|
2000-09-13 19:52:42 +00:00
|
|
|
HANDLE readh, writeh;
|
|
|
|
|
2005-02-23 16:05:09 +00:00
|
|
|
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
|
2009-09-27 16:07:50 +00:00
|
|
|
saAttr.bInheritHandle = FALSE;
|
2005-02-23 16:05:09 +00:00
|
|
|
saAttr.lpSecurityDescriptor = NULL;
|
|
|
|
|
|
|
|
if (CreatePipe(&readh, &writeh, &saAttr, 0) != 0)
|
2000-09-13 19:52:42 +00:00
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
_readHandle = [[NSFileHandle alloc] initWithNativeHandle: readh
|
2003-06-27 14:34:13 +00:00
|
|
|
closeOnDealloc: YES];
|
2010-02-14 10:48:10 +00:00
|
|
|
_writeHandle = [[NSFileHandle alloc] initWithNativeHandle: writeh
|
|
|
|
closeOnDealloc: YES];
|
2000-09-13 19:52:42 +00:00
|
|
|
}
|
2006-10-09 14:00:01 +00:00
|
|
|
else
|
|
|
|
{
|
2006-10-20 10:56:27 +00:00
|
|
|
NSLog(@"Failed to create pipe ... %@", [NSError _last]);
|
2006-10-09 14:00:01 +00:00
|
|
|
DESTROY(self);
|
|
|
|
}
|
2000-09-13 19:52:42 +00:00
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-06-27 14:34:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the file handle for reading from the pipe.
|
|
|
|
*/
|
2005-07-08 11:48:37 +00:00
|
|
|
- (NSFileHandle*) fileHandleForReading
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
return _readHandle;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
2003-06-27 14:34:13 +00:00
|
|
|
/**
|
|
|
|
* Returns the file handle for writing to the pipe.
|
|
|
|
*/
|
2005-07-08 11:48:37 +00:00
|
|
|
- (NSFileHandle*) fileHandleForWriting
|
1997-09-01 21:59:51 +00:00
|
|
|
{
|
2010-02-14 10:48:10 +00:00
|
|
|
return _writeHandle;
|
1997-09-01 21:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|