url.c
5.64 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <url_mbox.h>
#include <url_unix.h>
#include <url_mdir.h>
#include <url_mmdf.h>
#include <url_pop.h>
#include <url_imap.h>
#include <url_mail.h>
#include <cpystr.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
/* Forward prototypes */
static int get_scheme (const url_t, char *, int);
static int get_user (const url_t, char *, int);
static int get_passwd (const url_t, char *, int);
static int get_host (const url_t, char *, int);
static int get_port (const url_t, long *);
static int get_path (const url_t, char *, int);
static int get_query (const url_t, char *, int);
static int get_id (const url_t, int *id);
/*
Builtin url types.
We are using a simple circular list to hold the builtin type. */
static struct url_builtin
{
const struct url_type *utype;
int is_malloc;
struct url_builtin * next;
} url_builtin [] = {
{ NULL, 0, &url_builtin[1] }, /* Sentinel, head list */
{ &_url_mbox_type, 0, &url_builtin[2] },
{ &_url_unix_type, 0, &url_builtin[3] },
{ &_url_maildir_type, 0, &url_builtin[4] },
{ &_url_mmdf_type, 0, &url_builtin[5] },
{ &_url_pop_type, 0, &url_builtin[6] },
{ &_url_imap_type, 0, &url_builtin[7] },
{ &_url_mailto_type, 0, &url_builtin[0] },
};
/*
FIXME: Proper locking is not done when accessing the list
this code is not thread-safe .. TODO */
int
url_add_type (struct url_type *utype)
{
struct url_builtin *current = malloc (sizeof (*current));
if (current == NULL)
return -1;
utype->id = (int)utype; /* It just has to be uniq */
current->utype = utype;
current->is_malloc = 1;
current->next = url_builtin->next;
url_builtin->next = current;
return 0;
}
int
url_remove_type (const struct url_type *utype)
{
struct url_builtin *current, *previous;
for (previous = url_builtin, current = url_builtin->next;
current != url_builtin;
previous = current, current = current->next)
{
if (current->utype == utype)
{
previous->next = current->next;
if (current->is_malloc)
free (current);
return 0;;
}
}
return -1;
}
int
url_list_type (struct url_type *list, int n)
{
struct url_builtin *current;
int i;
for (i = 0, current = url_builtin->next; current != url_builtin;
current = current->next, i++)
{
if (list)
if (i < n)
list[i] = *(current->utype);
}
return i;
}
int
url_list_mtype (struct url_type **mlist, int *n)
{
struct url_type *utype;
int i;
if ((i = url_list_type (NULL, 0)) <= 0
|| (utype = calloc (i, sizeof (*utype))) == NULL)
{
return -1;
}
*mlist = utype;
return *n = url_list_type (utype, i);
}
int
url_init (url_t * purl, const char *name)
{
int status = -1;
const struct url_type *utype;
struct url_builtin *ub;
/* Sanity checks */
if (name == NULL || *name == '\0')
{
return status;
}
/* Search for a known scheme */
for (ub = url_builtin->next; ub != url_builtin; ub = ub->next)
{
utype = ub->utype;
if (strncasecmp (name, utype->scheme, utype->len) == 0)
{
status = 0;
break;
}
}
/* Found one initialize it */
if (status == 0)
{
status = utype->_init (purl, name);
if (status == 0)
{
url_t url = *purl;
if (url->utype == NULL)
url->utype = utype;
if (url->_get_scheme == NULL)
url->_get_scheme = get_scheme;
if (url->_get_user == NULL)
url->_get_user = get_user;
if (url->_get_passwd == NULL)
url->_get_passwd = get_passwd;
if (url->_get_host == NULL)
url->_get_host = get_host;
if (url->_get_port == NULL)
url->_get_port = get_port;
if (url->_get_path == NULL)
url->_get_path = get_path;
if (url->_get_query == NULL)
url->_get_query = get_query;
if (url->_get_id == NULL)
url->_get_id = get_id;
}
}
return status;
}
void
url_destroy (url_t *purl)
{
if (purl && *purl)
{
const struct url_type *utype = (*purl)->utype;
utype->_destroy(purl);
(*purl) = NULL;
}
}
/* Simple stub functions they all call _cpystr */
static int
get_scheme (const url_t u, char * s, int n)
{
return _cpystr (s, u->scheme, n);
}
static int
get_user (const url_t u, char * s, int n)
{
return _cpystr (s, u->user, n);
}
/* FIXME: We should not store passwd in clear, but rather
have a simple encoding, and decoding mechanism */
static int
get_passwd (const url_t u, char * s, int n)
{
return _cpystr (s, u->passwd, n);
}
static int
get_host (const url_t u, char * s, int n)
{
return _cpystr (s, u->host, n);
}
static int
get_port (const url_t u, long * p)
{
*p = u->port;
return 0;
}
static int
get_path (const url_t u, char * s, int n)
{
return _cpystr(s, u->path, n);
}
static int
get_query (const url_t u, char * s, int n)
{
return _cpystr(s, u->query, n);
}
static int
get_id (const url_t u, int *id)
{
const struct url_type *utype = u->utype;
*id = utype->id;
return 0;
}