2 * Sherlock Utilities -- URL Handling Tool
4 * (c) 2004 Martin Mares <mj@ucw.cz>
8 #include <ucw/getopt.h>
10 #include <ucw/fastbuf.h>
16 static byte *base_url;
17 static struct url base;
18 static uint opt_split = 0, opt_normalize = 0, opt_forgive = 0;
19 static struct fastbuf *fout;
20 static uint err_count;
23 process_url(byte *url)
25 byte buf1[MAX_URL_SIZE], buf2[MAX_URL_SIZE], buf3[MAX_URL_SIZE], buf4[MAX_URL_SIZE];
29 if ((e = url_deescape(url, buf1)) || (e = url_split(buf1, &ur, buf2)))
31 if ((base_url || opt_normalize) && (e = url_normalize(&ur, &base)))
33 if (opt_normalize && (e = url_canonicalize(&ur)))
38 bprintf(fout, "protocol=%s\n", ur.protocol);
40 bprintf(fout, "user=%s\n", ur.user);
42 bprintf(fout, "pass=%s\n", ur.pass);
44 bprintf(fout, "host=%s\n", ur.host);
46 bprintf(fout, "port=%d\n", ur.port);
48 bprintf(fout, "rest=%s\n", ur.rest);
53 if ((e = url_pack(&ur, buf3)) || (e = url_enescape(buf3, buf4)))
55 bprintf(fout, "%s\n", buf4);
60 msg(L_ERROR, "%s: %s", url, url_error(e));
64 static char *shortopts = CF_SHORT_OPTS "b:fns";
65 static struct option longopts[] =
68 { "base", 1, 0, 'b' },
69 { "forgive", 0, 0, 'f' },
70 { "normalize", 0, 0, 'n' },
71 { "split", 0, 0, 's' },
75 static char *help = "\
76 Usage: ucw-urltool [<options>] <operations> [<URL's>]\n\
80 -b, --base <URL>\tInput URL's are relative to this base\n\
81 -f, --forgive\t\tReturn exit status 0 even if there were errors\n\
84 -s, --split\t\tSplit a given URL to components\n\
85 -n, --normalize\t\tNormalize given URL\n\
101 main(int argc, char **argv)
104 byte *base_url = NULL;
105 byte basebuf1[MAX_URL_SIZE], basebuf2[MAX_URL_SIZE];
108 while ((opt = cf_getopt(argc, argv, shortopts, longopts, NULL)) >= 0)
113 err = url_canon_split(base_url, basebuf1, basebuf2, &base);
115 die("Invalid base URL: %s", url_error(err));
127 usage("Invalid option");
130 fout = bfdopen_shared(1, 4096);
133 struct fastbuf *fin = bfdopen_shared(0, 4096);
134 byte url[MAX_URL_SIZE];
135 while (bgets(fin, url, sizeof(url)))
140 while (optind < argc)
141 process_url(argv[optind++]);
144 return (err_count && !opt_forgive);