View | Details | Raw Unified | Return to bug 30318
Collapse All | Expand All

(-)./lib/libfetch/Makefile (-4 / +4 lines)
Lines 39-48 Link Here
39
	@echo "};" >> ${.TARGET}
39
	@echo "};" >> ${.TARGET}
40
40
41
.for MP in fetchFreeURL fetchGet fetchGetFTP fetchGetFile fetchGetHTTP \
41
.for MP in fetchFreeURL fetchGet fetchGetFTP fetchGetFile fetchGetHTTP \
42
fetchGetURL fetchList fetchListFTP fetchListFile fetchListHTTP fetchListURL \
42
fetchGetURL fetchList fetchStrfURL fetchListFTP fetchListFile fetchListHTTP \
43
fetchMakeURL fetchParseURL fetchPut fetchPutFTP fetchPutFile fetchPutHTTP \
43
fetchListURL fetchMakeURL fetchParseURL fetchPut fetchPutFTP fetchPutFile \
44
fetchPutURL fetchStat fetchStatFTP fetchStatFile fetchStatHTTP fetchStatURL \
44
fetchPutHTTP fetchPutURL fetchStat fetchStatFTP fetchStatFile fetchStatHTTP \
45
fetchXGet fetchXGetFTP fetchXGetFile fetchXGetHTTP fetchXGetURL
45
fetchStatURL fetchXGet fetchXGetFTP fetchXGetFile fetchXGetHTTP fetchXGetURL
46
MLINKS+= fetch.3 ${MP}.3
46
MLINKS+= fetch.3 ${MP}.3
47
.endfor
47
.endfor
48
48
(-)./lib/libfetch/fetch.3 (+69 lines)
Lines 36-41 Link Here
36
.Nm fetchPutURL ,
36
.Nm fetchPutURL ,
37
.Nm fetchStatURL ,
37
.Nm fetchStatURL ,
38
.Nm fetchListURL ,
38
.Nm fetchListURL ,
39
.Nm fetchStrfURL ,
39
.Nm fetchXGet ,
40
.Nm fetchXGet ,
40
.Nm fetchGet ,
41
.Nm fetchGet ,
41
.Nm fetchPut ,
42
.Nm fetchPut ,
Lines 89-94 Link Here
89
.Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags"
90
.Fn fetchStat "struct url *u" "struct url_stat *us" "const char *flags"
90
.Ft struct url_ent *
91
.Ft struct url_ent *
91
.Fn fetchList "struct url *u" "const char *flags"
92
.Fn fetchList "struct url *u" "const char *flags"
93
.Ft size_t
94
.Fn fetchStrfURL "char *buf" "size_t s" "const char *fmt" "struct url *url"
92
.Ft FILE *
95
.Ft FILE *
93
.Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags"
96
.Fn fetchXGetFile "struct url *u" "struct url_stat *us" "const char *flags"
94
.Ft FILE *
97
.Ft FILE *
Lines 262-267 Link Here
262
a
265
a
263
.Vt struct url
266
.Vt struct url
264
rather than a string.
267
rather than a string.
268
.Pp
269
.Fn fetchStrfURL
270
formats the information from the
271
.Fa url
272
into
273
.Fa buf
274
according to the string pointed to by
275
.Fa fmt .
276
No more than 
277
.Fa size
278
characters will be copied to
279
.Fa buf .
280
If the total number of characters including the terminating NUL is
281
less is less than
282
.Fa size ,
283
then the number of characters copied not counting the terminating NUL
284
is returned. Otherwise, zero is returned and the contents of 
285
.Fa buf
286
are indeterminate.
287
.Pp
288
The format string consists of ordinary characters and zero or more 
289
conversion specifications. A conversion specification is a percent sign
290
.Dq Ql %
291
and one character. Conversion specifications are replaced by elements
292
from the url. See the description of
293
.Fn fetchParseURL
294
for the names of the cmoponents.
295
.Pp
296
Not all parts are present in all URLs; those missing will generate the null
297
string. The non-alphabetic characters separate the parts, with the exception
298
of the last 
299
.Dq Ql / ,
300
which is the first character in the file part.
301
.Pp
302
The conversion specifications map to the parts like so:
303
.Bl -tag -width "xxxx"
304
.It Cm %s
305
is replaced by the
306
.Vt scheme
307
from the URL.
308
.It Cm %u
309
is replaced by the
310
.Vt user
311
from the URL.
312
.It Cm %w
313
is replaced by the
314
.Vt pwd
315
from the URL.
316
.It Cm %h
317
is replaced by the
318
.Vt host
319
part of the URL.
320
.It Cm %p
321
is replaced by the
322
.Vt port
323
from the URL.
324
.It %d
325
is replaced by the
326
.Vt document
327
part of the URL, including any initial
328
.Dq Ql /
329
that may be present.
330
.It Cm %%
331
is replaced by 
332
.Ql % .
333
.El
265
.Pp
334
.Pp
266
All of the
335
All of the
267
.Fn fetchXGetXXX ,
336
.Fn fetchXGetXXX ,
(-)./lib/libfetch/fetch.c (+76 lines)
Lines 414-419 Link Here
414
}
414
}
415
415
416
/*
416
/*
417
 * Format a URL into a string.
418
 *
419
 * This function formats the information from urlptr into buf
420
 * according to format. No more than size characters will be copied to
421
 * buf. If the total number of characters including the terminating
422
 * NUL is less is less than size, then the number of characters copied
423
 * not counting the terminating NUL is returned. Otherwise, zero is
424
 * returned and the contents of buf are indeterminate.
425
 *
426
 * The format string consists of ordinary characters and zero or more
427
 * conversion specifications. A conversion specification is a ``%''
428
 * sign and one character. Conversion specifications are replaced by
429
 * elements from the url. The url is parsed into the following parts,
430
 * as defined in RFC1738:
431
 *
432
 *	scheme://user:password@host:port/file
433
 *
434
 * Not all parts are present in all URLs; those missing will generate
435
 * the null string. The non-alphabetic characters separate the parts,
436
 * with the exception of the last ``/'', which is the first character
437
 * in the file part.
438
 *
439
 * The conversion specifications map to the parts like so: %s: scheme,
440
 * %u: user, %w: password, %h: host, %p: port, %f: file.
441
 *
442
 * Any other character after the ``%'' in a conversion specification
443
 * will replace the conversion specification, so that ``%%'' becomes
444
 * ``%''. Using this mechanism for any character other than ``%'' and
445
 * the documented specifications could lead to problems with later
446
 * version of strfurl.
447
 *
448
 * Note for future expansion: capital letters for conversion to imply
449
 * smarts, so that %P will become the default port for the scheme if
450
 * none is provided?
451
 */
452
size_t
453
fetchStrfURL(char *const buf, size_t size, const char *format,
454
	     struct url *urlptr)
455
{
456
  char *pt, *ptlim ;
457
  
458
  for (pt = buf, ptlim = pt + size; pt < ptlim && *format; format += 1)
459
    if (*format != '%') *pt++ = *format ;
460
    else {
461
      format += 1 ;
462
      switch (*format) {
463
      case 'd':
464
	pt += snprintf(pt, ptlim - pt, "%s", urlptr->doc) ;
465
	break ;
466
      case 'h':
467
	pt += snprintf(pt, ptlim - pt, "%s", urlptr->host) ;
468
	break ;
469
      case 'p':
470
	pt += snprintf(pt, ptlim - pt, "%d", urlptr->port) ;
471
	break ;
472
      case 's':
473
	pt += snprintf(pt, ptlim - pt, "%s", urlptr->scheme) ;
474
	break ;
475
      case 'u':
476
	pt += snprintf(pt, ptlim - pt, "%s", urlptr->user) ;
477
	break ;
478
      case 'w':
479
	pt += snprintf(pt, ptlim - pt, "%s", urlptr->pwd) ;
480
	break ;
481
      default:
482
	*pt++ = *format ;
483
	break ;
484
      }
485
    }
486
487
  if (pt >= ptlim) return 0 ;
488
  *pt = '\0' ;
489
  return pt - buf ;
490
}
491
492
/*
417
 * Free a URL
493
 * Free a URL
418
 */
494
 */
419
void
495
void
(-)./lib/libfetch/fetch.h (+1 lines)
Lines 118-123 Link Here
118
FILE		*fetchPut(struct url *, const char *);
118
FILE		*fetchPut(struct url *, const char *);
119
int		 fetchStat(struct url *, struct url_stat *, const char *);
119
int		 fetchStat(struct url *, struct url_stat *, const char *);
120
struct url_ent	*fetchList(struct url *, const char *);
120
struct url_ent	*fetchList(struct url *, const char *);
121
size_t		 fetchStrfURL(char *, size_t, const char *, struct url *);
121
122
122
/* URL parsing */
123
/* URL parsing */
123
struct url	*fetchMakeURL(const char *, const char *, int,
124
struct url	*fetchMakeURL(const char *, const char *, int,
(-)./usr.bin/url/Makefile (+6 lines)
Line 0 Link Here
1
#	@(#)Makefile	8.1 (Berkeley) 6/6/93
2
3
PROG=	url
4
LDADD=	-lfetch
5
6
.include <bsd.prog.mk>
(-)./usr.bin/url/url.1 (+61 lines)
Line 0 Link Here
1
.\" Copyrithg 2001, Mike W. Meyer <mwm@mired.org>
2
.Dd September 4, 2001
3
.Dt URL 1
4
.Os
5
.Sh NAME
6
.Nm url
7
.Nd parse and reformat uniform resource locators
8
.Sh SYNOPSYS
9
.Nm
10
.Op Fl d | h | m | p | s | u | w | Cm + Ns Ar format
11
.Ar URL
12
.Sh DESCRIPTION
13
The
14
.Nm
15
command reads a URL from the command line, parses it with the
16
.Xr fetchParseURL 3
17
routine, then reformats the components as specified by the option
18
and displays the results on standard out. The meaning of the component
19
names is in
20
.Xr fetchParseURL 3 .
21
.Pp
22
The options are as follows:
23
.Bl -tag -width Ds
24
.It Fl d
25
Display the document part of the url.
26
.It Fl h
27
Display the host from the url.
28
.It Fl m
29
Display the user and host from the url as a valid mail address.
30
.It Fl p
31
Display the port part of the url.
32
.It Fl s
33
Display the scheme from the url.
34
.It Fl u
35
Display the user name part the url.
36
.It Fl w
37
Display the password part of the url.
38
.It Cm + Ns Ar format
39
The format string specifies a format in which to display the components
40
of the URL. The format string can contain any conversion
41
specifications described in the
42
.Xr fetchStrfURL 3
43
manual page, as well as arbitrary text.
44
A newline
45
.Pl Ql \en
46
character is always output after the characters specified by the
47
format sting.
48
.El
49
.Pp
50
If no option is specified,
51
.Nm
52
chooses a format based on the scheme, which may result in the input
53
URL being displayed.
54
.Sh ERRORS
55
.Nm
56
exits 0 on success, >0 if an error occurs.
57
.Sh SEE ALSO
58
.Xr fetchParseURL 3
59
.Xr fetchStrfURL 3
60
.Sh AUTHORS
61
.An Mike Meyer Aq mwm@mired.org
(-)./usr.bin/url/url.c (+86 lines)
Line 0 Link Here
1
/*
2
 * A small shell tool for manipulating URLs as parsed by libfetch.
3
 *
4
 * Copyright 2001, Mike W. Meyer
5
 * <mwm@mired.org>
6
 */
7
8
#include <sys/types.h>
9
#include <sys/param.h>
10
#include <err.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <fetch.h>
14
15
#define OPTIONS "fhmPpsU"
16
17
struct formats {
18
  char *name, *format ;
19
} ;
20
21
static char *
22
searchfmtarray(const struct formats *array, const char *const name) {
23
  for (;array->name != NULL; array += 1)
24
    if (strcmp(array->name, name) == 0) return array->format ;
25
  return NULL ;
26
}
27
28
static struct formats schemes[] = {
29
  {"ftp", "%h%f"},
30
  {"mailto", "%u@%h"}
31
} ;
32
33
34
static struct formats flags[] = {
35
  {"d", "%d"} ,
36
  {"h", "%h"} ,
37
  {"m", "%u@%h"} ,
38
  {"p", "%p"} ,
39
  {"s", "%s"} ,
40
  {"u", "%u"} ,
41
  {"w", "%w"} ,
42
  {NULL, NULL}
43
} ;
44
45
static void
46
usage(char *name) {
47
  fprintf(stderr,
48
	  "usage: %s [-d | -h | -m | -p | -s | -u | -w | +format] URL\n", name)
49
    ;
50
  exit(EXIT_FAILURE) ;
51
}
52
#define URLBUFSIZE	10240
53
54
int
55
main(int argc, char **argv) {
56
  char *fmt ;
57
  char *in, *tmp, buf[URLBUFSIZE] ;
58
  struct url *out ;
59
  struct format *search ;
60
  int res ;
61
62
  fmt = NULL ;
63
  if (argc == 2) {
64
    in = argv[1] ;
65
  } else if (argc == 3) {
66
    in = argv[2] ;
67
    tmp = &argv[1][1] ;
68
    if (argv[1][0] == '+') fmt = tmp ;
69
    else if (argv[1][0] == '-') fmt = searchfmtarray(flags, tmp) ;
70
    if (fmt == NULL) usage(argv[0]) ;
71
  } else usage(argv[0]) ;
72
  
73
  if ((out = fetchParseURL(in)) == NULL)
74
    errx(EXIT_FAILURE, "Failed to parse URL.") ;
75
  else {
76
    if (fmt == NULL) fmt = searchfmtarray(schemes, out->scheme) ;
77
    if (fmt == NULL) puts(in) ;
78
    else {
79
      res = fetchStrfURL(buf, URLBUFSIZE, fmt, out) ;
80
      if (res != 0) puts(buf) ;
81
      else warnx("Output buffer size exceeded.") ;
82
    }
83
    fetchFreeURL(out) ;
84
  }
85
  exit(res == 0 ? EXIT_SUCCESS : EXIT_FAILURE) ;
86
}

Return to bug 30318