url-comp.c
3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2015 Free Software Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/url.h>
#include <mailutils/secret.h>
#include <mailutils/kwd.h>
static const char *
strval (const char *str)
{
return str[0] ? str : NULL;
}
void
usage (FILE *str, int code)
{
fprintf (str, "usage: %s [url=URL] [OPTIONS]\n", mu_program_name);
exit (code);
}
int
main (int argc, char **argv)
{
int i = 1, rc;
mu_url_t url = NULL;
const char *arg;
mu_set_program_name (argv[0]);
if (argc > 1)
{
if (strcmp (argv[1], "help") == 0 ||
strcmp (argv[1], "--help") == 0 ||
strcmp (argv[1], "-h") == 0)
usage (stdout, 0);
if (strncmp (argv[1], "url=", 4) == 0)
{
MU_ASSERT (mu_url_create (&url, argv[1] + 4));
i = 2;
}
}
if (!url)
{
MU_ASSERT (mu_url_create_null (&url));
i = 1;
}
for (; i < argc; i++)
{
if (strncmp (argv[i], "scheme=", 7) == 0)
{
MU_ASSERT (mu_url_set_scheme (url, strval (argv[i] + 7)));
}
else if (strncmp (argv[i], "user=", 5) == 0)
{
MU_ASSERT (mu_url_set_user (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "path=", 5) == 0)
{
MU_ASSERT (mu_url_set_path (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "host=", 5) == 0)
{
MU_ASSERT (mu_url_set_host (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "port=", 5) == 0)
{
MU_ASSERT (mu_url_set_port (url, atoi (argv[i] + 5)));
}
else if (strncmp (argv[i], "service=", 8) == 0)
{
MU_ASSERT (mu_url_set_service (url, strval (argv[i] + 8)));
}
else if (strncmp (argv[i], "auth=", 5) == 0)
{
MU_ASSERT (mu_url_set_auth (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "pass=", 5) == 0)
{
mu_secret_t secret;
arg = strval (argv[i] + 5);
if (arg)
{
MU_ASSERT (mu_secret_create (&secret, arg, strlen (arg)));
}
else
secret = NULL;
MU_ASSERT (mu_url_set_secret (url, secret));
}
else if (strncmp (argv[i], "param=", 6) == 0)
{
arg = strval (argv[i] + 6);
if (arg)
MU_ASSERT (mu_url_add_param (url, 1, (const char **)&arg));
else
MU_ASSERT (mu_url_clear_param (url));
}
else if (strncmp (argv[i], "query=", 6) == 0)
{
arg = strval (argv[i] + 6);
if (arg)
MU_ASSERT (mu_url_add_query (url, 1, (const char **)&arg));
else
MU_ASSERT (mu_url_clear_query (url));
}
else
{
mu_error ("unrecognized argument: %s", argv[i]);
return 1;
}
}
rc = mu_url_sget_name (url, &arg);
if (rc)
{
mu_error ("%s", mu_strerror (rc));
return 1;
}
printf ("%s\n", arg);
return 0;
}