parse.c
4.51 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 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/>. */
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/mailbox.h>
#include <mailutils/msgset.h>
#include <mailutils/sys/msgset.h>
/* This structure keeps parser state while parsing message set. */
struct parse_msgnum_env
{
const char *s; /* Current position in string */
size_t minval; /* Min. sequence number or UID */
size_t maxval; /* Max. sequence number or UID */
mu_msgset_t msgset; /* Message set being built. */
};
/* Get a single message number/UID from env->s and store it into *PN.
Return 0 on success and error code on error.
Advance env->s to the point past the parsed message number.
*/
static int
get_msgnum (struct parse_msgnum_env *env, size_t *pn)
{
size_t msgnum;
char *p;
errno = 0;
msgnum = strtoul (env->s, &p, 10);
if (msgnum == ULONG_MAX && errno == ERANGE)
return MU_ERR_PARSE;
env->s = p;
if (env->msgset->mbox && env->maxval && msgnum > env->maxval)
msgnum = env->maxval;
*pn = msgnum;
return 0;
}
/* Parse a single message range (A:B). Treat '*' as the largest number/UID
in use. */
static int
parse_msgrange (struct parse_msgnum_env *env)
{
int rc;
struct mu_msgrange msgrange;
if (*env->s == '*')
{
msgrange.msg_beg = env->maxval;
env->s++;
}
else if ((rc = get_msgnum (env, &msgrange.msg_beg)))
return rc;
if (*env->s == ':')
{
if (*++env->s == '*')
{
msgrange.msg_end = env->maxval;
++env->s;
}
else if (*env->s == 0)
return MU_ERR_PARSE;
else if ((rc = get_msgnum (env, &msgrange.msg_end)))
return rc;
}
else
msgrange.msg_end = msgrange.msg_beg;
if (msgrange.msg_end && msgrange.msg_end < msgrange.msg_beg)
{
size_t tmp = msgrange.msg_end;
msgrange.msg_end = msgrange.msg_beg;
msgrange.msg_beg = tmp;
}
if ((env->msgset->flags & MU_MSGSET_UID) && env->msgset->mbox)
{
int rc;
rc = mu_mailbox_translate (env->msgset->mbox,
MU_MAILBOX_UID_TO_MSGNO,
msgrange.msg_beg, &msgrange.msg_beg);
if (rc == MU_ERR_NOENT)
msgrange.msg_beg = env->minval;
else if (rc)
return rc;
rc = mu_mailbox_translate (env->msgset->mbox,
MU_MAILBOX_UID_TO_MSGNO,
msgrange.msg_end, &msgrange.msg_end);
if (rc == MU_ERR_NOENT)
msgrange.msg_end = env->maxval;
else if (rc)
return rc;
}
return mu_msgset_add_range (env->msgset, msgrange.msg_beg, msgrange.msg_end);
}
/* Parse IMAP-style message set specification S.
On success, return 0 and populate MSET with the resulting message ranges.
On error, return error code and point END to the position in the input
string where parsing has failed. */
int
mu_msgset_parse_imap (mu_msgset_t mset, const char *s, char **end)
{
int rc;
struct parse_msgnum_env env;
if (!s || !mset)
return EINVAL;
if (!*s)
return MU_ERR_PARSE;
memset (&env, 0, sizeof (env));
env.s = s;
env.msgset = mset;
env.minval = 1;
if (end)
*end = (char*) s;
if (mset->mbox)
{
size_t lastmsgno; /* Max. sequence number. */
rc = mu_mailbox_messages_count (mset->mbox, &lastmsgno);
if (rc == 0)
{
if (mset->flags & MU_MSGSET_UID)
{
rc = mu_mailbox_translate (mset->mbox, MU_MAILBOX_MSGNO_TO_UID,
lastmsgno, &env.maxval);
if (rc == 0)
rc = mu_mailbox_translate (mset->mbox, MU_MAILBOX_MSGNO_TO_UID,
1, &env.minval);
}
else
env.maxval = lastmsgno;
}
if (rc)
return rc;
}
while ((rc = parse_msgrange (&env)) == 0 && *env.s)
{
if (*env.s != ',' || *++env.s == 0)
{
rc = MU_ERR_PARSE;
break;
}
}
if (end)
*end = (char*) env.s;
return rc;
}