*	open connection to a URL (text only) for CSNOBOL4
*	Phil Budne <phil@ultimate.com>
*	Sept 24, 1997
*	$Id: url.sno,v 1.9 2002/02/07 15:52:03 phil Exp $

-include "http.sno"
-include "ftp.sno"
-include "file.sno"

****************
*	URL.OPEN()
*
*	args;
*	Variable name to associate for I/O
*	URL

*	XXX one table, with data struct?
	URL.TABLE = TABLE()
	URL.FTP.TABLE = TABLE()
	URL.UNIT = TABLE()

	DEFINE("URL.OPEN(URL.NAME,URL)"
+		"URL.HOST,URL.PORT,URL.PROTO,URL.FILE")
+							:(URL.OPEN.END)

* XXX allow /pathname and file://hostname/path?
URL.OPEN
	URL POS(0)
+		((('http' | 'ftp') . URL.PROTO
+			'://'
+			BREAK(':/') . URL.HOST
+			((':' SPAN('0123456789') . URL.PORT) | '') |
+		  'file' . URL.PROTO ':') ('' | '//'))
+			(('/' REM) | '') . URL.FILE	:F(FRETURN)

*	save protocol for close
	URL.TABLE[URL.NAME] = URL.PROTO

*	dispatch!
	:( $('URL.OPEN.' URL.PROTO))

****
URL.OPEN.http
	HTTP.OPEN(URL.NAME,URL.HOST,URL.FILE,URL.PORT)	:S(RETURN)F(FRETURN)

****
URL.OPEN.ftp
*	XXX check for user:password@host
	FTP.OPEN.ANON(.URL.FTP.TABLE[URL.NAME],URL.HOST,URL.PORT)
+							:F(FRETURN)
	FTP.DATA.COMMAND(.URL.FTP.TABLE[URL.NAME],"RETR " URL.FILE,URL.NAME)
+							:S(RETURN)
*	here if data command failed
	FTP.CLOSE(.URL.FTP.TABLE[URL.NAME])		:(FRETURN)
****
URL.OPEN.file
	FILE.OPEN(URL.NAME,URL.FILE)			:S(RETURN)F(FRETURN)

URL.OPEN.END

****************
*	URL.CLOSE
*
*	args;
*	I/O var

	DEFINE("URL.CLOSE(URL.NAME)URL.PROTO")		:(URL.CLOSE.END)
URL.CLOSE
	URL.PROTO = URL.TABLE[URL.NAME]
	IDENT(URL.PROTO)				:S(FRETURN)

*	clear table entry
	URL.TABLE[URL.NAME] =

*	dispatch!
	:( $('URL.CLOSE.' URL.PROTO))

****
URL.CLOSE.http
	HTTP.CLOSE(URL.NAME)				:(RETURN)
****
URL.CLOSE.ftp
	FTP.DATA.CLOSE(URL.NAME)
	FTP.CLOSE(.URL.FTP.TABLE[URL.NAME])		:(RETURN)
****
URL.CLOSE.file
	FILE.CLOSE(URL.NAME)				:(RETURN)

URL.CLOSE.END

****************
*
*	URL.OPEN(.X,'file:/dkinfo.out')			:F(END)
*LOOP	OUTPUT = X					:S(LOOP)
*	URL.CLOSE(.X)
**
*	URL.OPEN(.X,'http://www.tiac.net/users/philb/') :F(END)
*LOOP2	OUTPUT = X					:S(LOOP2)
*	URL.CLOSE(.X)
**
*	URL.OPEN(.X,'ftp://alpha.gnu.ai.mit.edu/README') :F(END)
*LOOP3	OUTPUT = X					:S(LOOP3)
*	URL.CLOSE(.X)
*
*END
