Change 14076: First go at fixing Mac::Memory::_open (MacPerl Bug #494481)

[email protected] (Chris Nandor) Fri, 4 Jan 2002 17:09:21 -0500
Newsgroups perl.perl5.changes.mac
Message-ID <p05100312b85bd888ac77@[10.0.1.177]>
Change 14076 by pudge@pudge-mobile on 2002/01/04 20:44:23

	First go at fixing Mac::Memory::_open (MacPerl Bug #494481)

Affected files ...

.... //depot/maint-5.6/macperl/macos/HandleSocket.cp#1 add
.... //depot/maint-5.6/macperl/macos/ext/Mac/Memory/Memory.xs#2 edit
.... //depot/maint-5.6/macperl/macos/ext/Mac/Memory/t/Memory.t#2 edit
.... //depot/maint-5.6/macperl/macos/macperl.sym#3 edit

Differences ...

==== //depot/maint-5.6/macperl/macos/ext/Mac/Memory/Memory.xs#2 (text) ====
Index: perl/macos/ext/Mac/Memory/Memory.xs
--- perl/macos/ext/Mac/Memory/Memory.xs.~1~	Fri Jan  4 14:00:05 2002
+++ perl/macos/ext/Mac/Memory/Memory.xs	Fri Jan  4 14:00:05 2002
@@ -6,6 +6,12 @@
  *    as specified in the README file.
  *
  * $Log: Memory.xs,v $
+ * Revision 1.4  2002/01/04 03:38:19  pudge
+ * Disable Mac::Memory::_open (HandleSocket::OpenHandle) for now
+ *
+ * Revision 1.3  2001/12/19 22:57:11  pudge
+ * Start to make Mac::Memory::_open work.  HandleSocket.cp still needs some lovin ' ... the entire thing is a bit unstable.
+ *
  * Revision 1.2  2000/09/09 22:18:27  neeri
  * Dynamic libraries compile under 5.6
  *
@@ -245,6 +251,10 @@
 
 NOT DEFINED AT THE MOMENT
 
+=cut
+
+#ifdef NOT_YET
+
 SysRet
 _open(hand, mode)
 	Handle	hand
@@ -254,6 +264,8 @@
 	OUTPUT:
 	RETVAL
 	
+#endif
+
 =item dispose
 
 Disposes of the handle.

==== //depot/maint-5.6/macperl/macos/ext/Mac/Memory/t/Memory.t#2 (text) ====
Index: perl/macos/ext/Mac/Memory/t/Memory.t
--- perl/macos/ext/Mac/Memory/t/Memory.t.~1~	Fri Jan  4 14:00:05 2002
+++ perl/macos/ext/Mac/Memory/t/Memory.t	Fri Jan  4 14:00:05 2002
@@ -14,8 +14,27 @@
 END
 
 $h  = new Handle("xyzzy");
-$fh = $h->open("<");
+$r = $h->open("r");
+
+# should be "xyzzy"
+print <$r>, "\n";
+
+$w = $h->open("w");
+print $w "wysiwyg";
+
+# should be "wysiwyg"
+print $h->get, "\n";
 
-$_ = <$fh>;
+truncate $w, 0;
+# should be " (0)"
+printf "%s (%d)\n", $h->get, $h->size;
+# should be blank
+print <$r>, "\n";
+# should be " (0)"
+undef $w;
+printf "%s (%d)\n", $h->get, $h->size;
 
-print;
+truncate $r, 0;
+# should be " (0)"
+printf "%s (%d)\n", $h->get, $h->size;
+__END__

==== //depot/maint-5.6/macperl/macos/macperl.sym#3 (text) ====
Index: perl/macos/macperl.sym
--- perl/macos/macperl.sym.~1~	Fri Jan  4 14:00:05 2002
+++ perl/macos/macperl.sym	Fri Jan  4 14:00:05 2002
@@ -261,3 +261,5 @@
 # more symbols, Fri, Aug 31, 2001 20:21:48
 gMacPerl_Perl5DB
 gMacPerl_ErrorFormat
+# added Wed, Dec 19, 2001 00:07:51
+#OpenHandle

==== //depot/maint-5.6/macperl/macos/HandleSocket.cp#1 (text) ====
Index: perl/macos/HandleSocket.cp
--- perl/macos/HandleSocket.cp.~1~	Fri Jan  4 14:00:05 2002
+++ perl/macos/HandleSocket.cp	Fri Jan  4 14:00:05 2002
@@ -0,0 +1,170 @@
+/*********************************************************************
+Project	:	GUSI				-	Grand Unified Socket Interface
+File		:	HandleSocket.cp-	Handle sockets
+Author	:	Matthias Neeracher <[email protected]>
+Language	:	MPW C++
+
+$Log: HandleSocket.cp,v $
+Revision 1.1  2001/12/19 22:57:11  pudge
+Start to make Mac::Memory::_open work.  HandleSocket.cp still needs some lovin ' ... the entire thing is a bit unstable.
+
+Revision 1.1  1997/04/07 20:46:13  neeri
+Synchronized with MacPerl 5.1.4a1
+
+*********************************************************************/
+
+#define GUSI_SOURCE
+#define GUSI_INTERNAL
+
+#include <GUSISocket.h>
+#include <GUSIDevice.h>
+#include <GUSISocketMixins.h>
+#include <GUSIDescriptor.h>
+#include <GUSIConfig.h>
+
+extern "C" int OpenHandle(Handle h, int oflag);
+
+class HandleSocket : public GUSISocket {
+	friend int OpenHandle(Handle h, int oflag);
+protected:
+	HandleSocket(Handle h, int oflag);
+public:
+	virtual bool		Supports(ConfigOption config);
+	virtual ssize_t	read(const GUSIScatterer & buf);
+	virtual ssize_t	write(const GUSIGatherer & buf);
+	virtual int			ioctl(unsigned int request, va_list argp);
+	virtual long		lseek(long offset, int whence);
+	virtual int			ftruncate(long offset);
+private:
+	Handle	data;
+	long		pos;
+	Boolean	append;
+};
+
+/************************ HandleSocket members ************************/
+
+HandleSocket::HandleSocket(Handle h, int oflag)
+ : GUSISocket(), data(h), pos(0)
+{
+	if (oflag & O_TRUNC)
+		SetHandleSize(data, 0);
+	append	=	(oflag & O_APPEND) != 0; 
+}
+
+ssize_t HandleSocket::read(const GUSIScatterer & scatterer)
+{
+	int	length 	= 	scatterer.Length();
+	int	left		=	GetHandleSize(data)-pos;
+
+	if (length > left)
+		length = left;
+
+	if (length) {
+		HLock(data);
+		memcpy(scatterer.Buffer(), *data+pos, length);
+		pos += length;
+		HUnlock(data);
+	}
+
+	return length;
+}
+
+ssize_t HandleSocket::write(const GUSIGatherer & gatherer)
+{
+	char * 	buffer 	= reinterpret_cast<char *>(gatherer.Buffer());
+	int		buflen	= gatherer.Length();
+	char *	dork;
+
+	if (append)
+		pos = GetHandleSize(data);
+	
+	long size = GetHandleSize(data);
+	if (pos+buflen > size) {
+		SetHandleSize(data, pos+buflen);
+		while (size < pos)
+			(*data)[size++] = 0;
+	}
+	if (buflen) {		
+		HLock(data);
+		memcpy(*data+pos, gatherer.Buffer(), buflen);
+		pos += buflen;
+		HUnlock(data);
+	}
+	
+	return buflen;
+}
+
+int HandleSocket::ioctl(unsigned int request, va_list argp)
+{
+	switch (request) {
+	case FIONREAD:
+		*va_arg(argp, long *) = GetHandleSize(data) - pos;
+		
+		return 0;
+	default :
+		return GUSISocket::ioctl(request, argp);
+	}
+
+	GUSI_ASSERT_CLIENT(false, ("ioctl: illegal request %d\n", request));
+	
+	return GUSISetPosixError(EOPNOTSUPP);
+}
+
+long HandleSocket::lseek(long offset, int whence)
+{	
+	long newPos;
+	
+	switch (whence) {
+	case SEEK_CUR:
+		newPos = pos+offset;
+		break;
+	case SEEK_END:
+		newPos = GetHandleSize(data)+offset;
+		break;
+	case SEEK_SET:
+		newPos = offset;
+		break;
+	default:
+		return GUSISetPosixError(EINVAL);
+	}
+	if (newPos < 0)
+		return GUSISetPosixError(EINVAL);
+	else
+		return pos = newPos;
+}
+
+int HandleSocket::ftruncate(long offset)
+{	
+long size = GetHandleSize(data);
+	if (offset > GetHandleSize(data)) {
+		lseek(offset, SEEK_SET);
+		write(GUSIGatherer(nil, 0));
+	} else
+		SetHandleSize(data, offset);
+
+	return 0;
+}
+
+bool HandleSocket::Supports(ConfigOption config)
+{
+	return config == kSimpleCalls;
+}
+
+int OpenHandle(Handle h, int oflag)
+{	
+	int		fd;
+	GUSISocket * sock = new HandleSocket(h, oflag);
+	GUSIDescriptorTable * table = GUSIDescriptorTable::Instance();
+
+	if (sock)
+		if ((fd = table->InstallSocket(sock)) > -1)
+			return fd;
+		else
+			delete sock;
+
+	if (!errno)
+		return GUSISetPosixError(ENOMEM);
+	else
+		return -1;
+}
+
End of Patch.