--- /dev/null
+/*
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stddef.h>
+#include <gpxe/uri.h>
+
+/** @file
+ *
+ * Current working URI
+ *
+ * Somewhat analogous to the current working directory in a POSIX
+ * system.
+ */
+
+/** Current working URI */
+struct uri *cwuri = NULL;
+
+/**
+ * Change working URI
+ *
+ * @v uri New working URI
+ */
+void churi ( struct uri *uri ) {
+ if ( cwuri )
+ uri_put ( cwuri );
+ cwuri = uri_get ( uri );
+}
* @v uri URI
*/
static void dump_uri ( struct uri *uri ) {
+ if ( ! uri )
+ return;
if ( uri->scheme )
DBG ( " scheme \"%s\"", uri->scheme );
if ( uri->opaque )
/**
* Get port from URI
*
- * @v uri URI
+ * @v uri URI, or NULL
* @v default_port Default port to use if none specified in URI
* @ret port Port
*/
unsigned int uri_port ( struct uri *uri, unsigned int default_port ) {
- return ( uri->port ? strtoul ( uri->port, NULL, 0 ) : default_port );
+ if ( ( ! uri ) || ( ! uri->port ) )
+ return default_port;
+ return ( strtoul ( uri->port, NULL, 0 ) );
}
/**
*
* @v buf Buffer to fill with URI string
* @v size Size of buffer
- * @v uri URI to write into buffer
+ * @v uri URI to write into buffer, or NULL
* @ret len Length of URI string
*/
int unparse_uri ( char *buf, size_t size, struct uri *uri ) {
dump_uri ( uri );
DBG ( "\n" );
+ /* Special-case NULL URI */
+ if ( ! uri ) {
+ if ( size )
+ buf[0] = '\0';
+ return 0;
+ }
+
/* Special-case opaque URIs */
if ( uri->opaque ) {
return ssnprintf ( ( buf + used ), ( size - used ),
/**
* Resolve base+relative URI
*
- * @v base_uri Base URI
+ * @v base_uri Base URI, or NULL
* @v relative_uri Relative URI
* @ret resolved_uri Resolved URI
*
struct uri *new_uri;
/* If relative URI is absolute, just re-use it */
- if ( uri_is_absolute ( relative_uri ) )
+ if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
return uri_get ( relative_uri );
/* Mangle URI */
ref_put ( &uri->refcnt );
}
+extern struct uri *cwuri;
+
extern struct uri * parse_uri ( const char *uri_string );
extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
extern int unparse_uri ( char *buf, size_t size, struct uri *uri );
const char *relative_path );
extern struct uri * resolve_uri ( struct uri *base_uri,
struct uri *relative_uri );
+extern void churi ( struct uri *uri );
#endif /* _GPXE_URI_H */