Преглед изворни кода

Add concept of "current working URI".

tags/v0.9.3
Michael Brown пре 17 година
родитељ
комит
15dae1e042
3 измењених фајлова са 61 додато и 5 уклоњено
  1. 42
    0
      src/core/cwuri.c
  2. 16
    5
      src/core/uri.c
  3. 3
    0
      src/include/gpxe/uri.h

+ 42
- 0
src/core/cwuri.c Прегледај датотеку

@@ -0,0 +1,42 @@
1
+/*
2
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
4
+ * This program is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU General Public License as
6
+ * published by the Free Software Foundation; either version 2 of the
7
+ * License, or any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful, but
10
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
+ * General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU General Public License
15
+ * along with this program; if not, write to the Free Software
16
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
+ */
18
+
19
+#include <stddef.h>
20
+#include <gpxe/uri.h>
21
+
22
+/** @file
23
+ *
24
+ * Current working URI
25
+ *
26
+ * Somewhat analogous to the current working directory in a POSIX
27
+ * system.
28
+ */
29
+
30
+/** Current working URI */
31
+struct uri *cwuri = NULL;
32
+
33
+/**
34
+ * Change working URI
35
+ *
36
+ * @v uri		New working URI
37
+ */
38
+void churi ( struct uri *uri ) {
39
+	if ( cwuri )
40
+		uri_put ( cwuri );
41
+	cwuri = uri_get ( uri );
42
+}

+ 16
- 5
src/core/uri.c Прегледај датотеку

@@ -35,6 +35,8 @@
35 35
  * @v uri		URI
36 36
  */
37 37
 static void dump_uri ( struct uri *uri ) {
38
+	if ( ! uri )
39
+		return;
38 40
 	if ( uri->scheme )
39 41
 		DBG ( " scheme \"%s\"", uri->scheme );
40 42
 	if ( uri->opaque )
@@ -174,12 +176,14 @@ struct uri * parse_uri ( const char *uri_string ) {
174 176
 /**
175 177
  * Get port from URI
176 178
  *
177
- * @v uri		URI
179
+ * @v uri		URI, or NULL
178 180
  * @v default_port	Default port to use if none specified in URI
179 181
  * @ret port		Port
180 182
  */
181 183
 unsigned int uri_port ( struct uri *uri, unsigned int default_port ) {
182
-	return ( uri->port ? strtoul ( uri->port, NULL, 0 ) : default_port );
184
+	if ( ( ! uri ) || ( ! uri->port ) )
185
+		return default_port;
186
+	return ( strtoul ( uri->port, NULL, 0 ) );
183 187
 }
184 188
 
185 189
 /**
@@ -187,7 +191,7 @@ unsigned int uri_port ( struct uri *uri, unsigned int default_port ) {
187 191
  *
188 192
  * @v buf		Buffer to fill with URI string
189 193
  * @v size		Size of buffer
190
- * @v uri		URI to write into buffer
194
+ * @v uri		URI to write into buffer, or NULL
191 195
  * @ret len		Length of URI string
192 196
  */
193 197
 int unparse_uri ( char *buf, size_t size, struct uri *uri ) {
@@ -197,6 +201,13 @@ int unparse_uri ( char *buf, size_t size, struct uri *uri ) {
197 201
 	dump_uri ( uri );
198 202
 	DBG ( "\n" );
199 203
 
204
+	/* Special-case NULL URI */
205
+	if ( ! uri ) {
206
+		if ( size )
207
+			buf[0] = '\0';
208
+		return 0;
209
+	}
210
+
200 211
 	/* Special-case opaque URIs */
201 212
 	if ( uri->opaque ) {
202 213
 		return ssnprintf ( ( buf + used ), ( size - used ),
@@ -332,7 +343,7 @@ char * resolve_path ( const char *base_path,
332 343
 /**
333 344
  * Resolve base+relative URI
334 345
  *
335
- * @v base_uri		Base URI
346
+ * @v base_uri		Base URI, or NULL
336 347
  * @v relative_uri	Relative URI
337 348
  * @ret resolved_uri	Resolved URI
338 349
  *
@@ -347,7 +358,7 @@ struct uri * resolve_uri ( struct uri *base_uri,
347 358
 	struct uri *new_uri;
348 359
 
349 360
 	/* If relative URI is absolute, just re-use it */
350
-	if ( uri_is_absolute ( relative_uri ) )
361
+	if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
351 362
 		return uri_get ( relative_uri );
352 363
 
353 364
 	/* Mangle URI */

+ 3
- 0
src/include/gpxe/uri.h Прегледај датотеку

@@ -124,6 +124,8 @@ uri_put ( struct uri *uri ) {
124 124
 	ref_put ( &uri->refcnt );
125 125
 }
126 126
 
127
+extern struct uri *cwuri;
128
+
127 129
 extern struct uri * parse_uri ( const char *uri_string );
128 130
 extern unsigned int uri_port ( struct uri *uri, unsigned int default_port );
129 131
 extern int unparse_uri ( char *buf, size_t size, struct uri *uri );
@@ -132,5 +134,6 @@ extern char * resolve_path ( const char *base_path,
132 134
 			     const char *relative_path );
133 135
 extern struct uri * resolve_uri ( struct uri *base_uri,
134 136
 				  struct uri *relative_uri );
137
+extern void churi ( struct uri *uri );
135 138
 
136 139
 #endif /* _GPXE_URI_H */

Loading…
Откажи
Сачувај