mh_sequence.c
3.6 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003, 2005, 2007, 2009, 2010 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 <mh.h>
static char *
private_sequence_name (const char *name)
{
char *p;
char *mbox_dir = mh_expand_name (NULL, mh_current_folder (), 0);
mu_asprintf (&p, "atr-%s-%s", name, mbox_dir);
free (mbox_dir);
return p;
}
const char *
mh_seq_read (const char *name, int flags)
{
const char *value;
if (flags & SEQ_PRIVATE)
{
char *p = private_sequence_name (name);
value = mh_global_context_get (p, NULL);
free (p);
}
else
value = mh_global_sequences_get (name, NULL);
return value;
}
static void
write_sequence (const char *name, char *value, int private)
{
if (private)
{
char *p = private_sequence_name (name);
mh_global_context_set (p, value);
free (p);
}
else
mh_global_sequences_set (name, value);
}
static void
delete_sequence (const char *name, int private)
{
write_sequence (name, NULL, private);
}
void
mh_seq_add (const char *name, mh_msgset_t *mset, int flags)
{
const char *value = mh_seq_read (name, flags);
char *new_value, *p;
const char *buf;
size_t i, len;
delete_sequence (name, !(flags & SEQ_PRIVATE));
if (flags & SEQ_ZERO)
value = NULL;
if (value)
len = strlen (value);
else
len = 0;
len++;
for (i = 0; i < mset->count; i++)
{
buf = mu_umaxtostr (0, mset->list[i]);
len += strlen (buf) + 1;
}
new_value = xmalloc (len + 1);
if (value)
strcpy (new_value, value);
else
new_value[0] = 0;
p = new_value + strlen (new_value);
*p++ = ' ';
for (i = 0; i < mset->count; i++)
{
p += sprintf (p, "%s", mu_umaxtostr (0, mset->list[i]));
*p++ = ' ';
}
*p = 0;
write_sequence (name, new_value, flags & SEQ_PRIVATE);
if (mu_c_strcasecmp (name, "cur") == 0)
current_message = strtoul (new_value, NULL, 0);
free (new_value);
}
static int
cmp_msgnum (const void *a, const void *b)
{
const size_t *as = a;
const size_t *bs = b;
if (*as < *bs)
return -1;
if (*as > *bs)
return 1;
return 0;
}
int
mh_seq_delete (const char *name, mh_msgset_t *mset, int flags)
{
const char *value = mh_seq_read (name, flags);
char *new_val;
char *p;
int argc, i, count;
char **argv;
if (!value)
return 0;
if (mu_argcv_get (value, "", NULL, &argc, &argv))
return 0;
for (i = 0; i < argc; i++)
{
char *p;
size_t num = strtoul (argv[i], &p, 10);
if (*p)
continue;
if (bsearch (&num, mset->list, mset->count, sizeof (mset->list[0]),
cmp_msgnum))
{
free (argv[i]);
argv[i] = NULL;
}
}
new_val = xstrdup (value);
p = new_val;
count = 0;
for (i = 0; i < argc; i++)
{
if (argv[i])
{
strcpy (p, argv[i]);
p += strlen (p);
*p++ = ' ';
count++;
}
}
*p = 0;
write_sequence (name, count > 0 ? new_val : NULL, flags & SEQ_PRIVATE);
mu_argcv_free (argc, argv);
free (new_val);
return 0;
}