-
Notifications
You must be signed in to change notification settings - Fork 3
/
readgnusmarks.c
403 lines (345 loc) · 9.61 KB
/
readgnusmarks.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* readgnusmarks.c - Helper to convert nnml to Maildir
* Copyright (C) 2009 Werner Koch
*
* This program 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 of the
* License, 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#define PGM "readgnusmarks"
#define PGM_VERSION "0.0"
#define PGM_BUGREPORT "[email protected]"
/* Option flags. */
static int verbose;
static int debug;
static const char *outputdir;
/* Error counter. */
static int any_error;
typedef struct
{
unsigned int in_use:1;
unsigned int seen:1;
unsigned int replied:1;
unsigned int passed:1;
unsigned int flagged:1;
} flags_t;
/* Number of flags we currently support. If one ever needs to process
messages with nnnml file number higher an additional data structure
is required. */
#define MAX_FLAGS 300000
/* An array of flags and a value indicating the allocated numer of
elements. */
static flags_t *flagarray;
static size_t flagarraysize;
/* Print diagnostic message and exit with failure. */
static void
die (const char *format, ...)
{
va_list arg_ptr;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
putc ('\n', stderr);
exit (1);
}
/* Print diagnostic message. */
static void
err (const char *format, ...)
{
va_list arg_ptr;
any_error = 1;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
putc ('\n', stderr);
}
/* Print an info message. */
static void
inf (const char *format, ...)
{
va_list arg_ptr;
if (!verbose)
return;
fflush (stdout);
fprintf (stderr, "%s: ", PGM);
va_start (arg_ptr, format);
vfprintf (stderr, format, arg_ptr);
va_end (arg_ptr);
putc ('\n', stderr);
}
static void
set_mark (int num, int action)
{
if (!action)
return;
if (num < 0 || (size_t)num >= flagarraysize)
die ("nnml file number %d too high; maximum is %lu",
num, (unsigned long)flagarraysize);
flagarray[num].in_use = 1;
switch (action)
{
case 'S': flagarray[num].seen = 1; break;
case 'R': flagarray[num].replied = 1; break;
case 'P': flagarray[num].passed = 1; break;
case 'F': flagarray[num].flagged = 1; break;
}
}
static void
read_marks (const char *fname)
{
FILE *fp;
int c;
int level = 0;
int wait_for_action = 0;
char token[50];
size_t tokenidx = 0;
int action = 0;
int num, n_from, n_to, cons_state;
fp = fopen (fname, "r");
if (!fp)
die ("failed to open `%s': %s", fname, strerror (errno));
while ((c = getc (fp)) != EOF)
{
if (!isascii (c))
die ("non ascii character found in `%s' - can't proceed", fname);
if (isspace (c))
;
else if (level < 0)
die ("garbage at end of `%s' - can't proceed", fname);
else if (c == ')')
;
else if (c == '(')
{
level++;
if (level > 3)
die ("nesting too deep in `%s' - can't proceed", fname);
if (wait_for_action && level == 3)
die ("action missing in `%s' - can't proceed", fname);
wait_for_action = (level == 2);
if (level == 3)
cons_state = n_from = n_to = 0;
tokenidx = 0;
}
else
{
if (tokenidx+1 >= sizeof token)
die ("token too long in `%s' - can't proceed", fname);
token[tokenidx++] = c;
continue;
}
token[tokenidx] = 0;
if (tokenidx)
{
tokenidx = 0;
if (wait_for_action)
{
wait_for_action = 0;
inf ("got action token `%s'", token);
if (!strcmp (token, "read"))
action = 'S';
else if (!strcmp (token, "reply"))
action = 'R';
else if (!strcmp (token, "forward"))
action = 'P';
else if (!strcmp (token, "tick"))
action = 'F';
else if (!strcmp (token, "save")
||!strcmp (token, "dormant")
||!strcmp (token, "killed"))
{
inf ("action '%s' in `%s' - ignored", token, fname);
action = 0;
}
else
{
err ("unknown action '%s' in `%s' - skipped", token, fname);
action = 0;
}
}
else if (level == 2)
{
num = atoi (token);
if (num < 1)
err ("bad number `%s' in `%s' - skipped", token, fname);
else
set_mark (num, action);
}
else if (level == 3)
{
if (*token == '.' && !token[1] && cons_state == 1)
cons_state++;
else
{
num = atoi (token);
if (num < 1)
err ("bad number `%s' in `%s' - skipped", token, fname);
else if (!cons_state)
{
cons_state++;
n_from = num;
}
else if (cons_state == 2)
{
cons_state++;
n_to = num;
if (n_to < n_from)
err ("invalid range in `%s'", fname);
for (num = n_from; num <= n_to; num++)
set_mark (num, action);
}
else
err ("too many numbers in cons `%s' - skipped", fname);
}
}
}
if (c == ')')
level--;
}
if (ferror (fp))
die ("failed to read `%s': %s", fname, strerror (errno));
fclose (fp);
}
static void
process_input (void)
{
char oldname[1024];
char newname[1024+50];
size_t n;
const char *s;
char *endp;
int num;
int counter = 0;
while (fgets (oldname, sizeof oldname, stdin))
{
n = strlen (oldname);
if (n && oldname[n-1] == '\n')
oldname[--n] = 0;
s = oldname;
num = (int)strtol (s, &endp, 10);
if (num < 1 || *endp != '\0')
{
err ("bad file name structure '%s' - skipped", oldname);
continue;
}
if (num < 0 || (size_t)num >= flagarraysize)
{
err ("nnml file number %d in `%s' too high - skipped", num, oldname);
continue;
}
snprintf (newname, sizeof newname-50, "%s/cur/%lu.%d-%d",
outputdir,
(unsigned long)time (NULL), num, ++counter);
strcat (newname, ":2,");
if (flagarray[num].in_use)
{
if (flagarray[num].flagged)
strcat (newname, "F");
if (flagarray[num].passed)
strcat (newname, "P");
if (flagarray[num].replied)
strcat (newname, "R");
if (flagarray[num].seen)
strcat (newname, "S");
}
if ( !strcmp (oldname, newname) )
{
inf ("file `%s' not moved", oldname);
continue;
}
printf ("mv '%s' '%s'\n", oldname, newname);
}
if (ferror (stdin))
die ("error reading from stdin: %s", strerror (errno));
}
static int
show_usage (int ex)
{
fputs ("Usage: " PGM " <MARKSFILE> [OUTDIR]\n"
"Read an nnml .marks file and rename Maildir files from stdin\n\n"
" --verbose enable extra informational output\n"
" --debug enable additional debug output\n"
" --help display this help and exit\n\n"
"This tool is used with the mv-nnml2maildir script.\n"
"Report bugs to " PGM_BUGREPORT ".\n",
ex? stderr:stdout);
exit (ex);
}
int
main (int argc, char **argv)
{
int last_argc = -1;
if (argc)
{
argc--; argv++;
}
while (argc && last_argc != argc )
{
last_argc = argc;
if (!strcmp (*argv, "--"))
{
argc--; argv++;
break;
}
else if (!strcmp (*argv, "--version"))
{
fputs (PGM " " PGM_VERSION "\n", stdout);
exit (0);
}
else if (!strcmp (*argv, "--help"))
{
show_usage (0);
}
else if (!strcmp (*argv, "--verbose"))
{
verbose = 1;
argc--; argv++;
}
else if (!strcmp (*argv, "--debug"))
{
verbose = debug = 1;
argc--; argv++;
}
else if (!strncmp (*argv, "--", 2))
show_usage (1);
}
if (argc < 1 || argc > 2 )
show_usage (1);
if (argc == 2)
outputdir = argv[1];
else
outputdir = ".";
flagarraysize = MAX_FLAGS;
flagarray = calloc (flagarraysize, sizeof *flagarray);
if (!flagarray)
die ("out of core: %s", strerror (errno));
read_marks (*argv);
process_input ();
free (flagarray);
return any_error? 1:0;
}
/*
Local Variables:
compile-command: "gcc -Wall -W -O2 -g -o readgnusmarks readgnusmarks.c"
End:
*/