2 * Totally braindamaged mbox splitter program.
4 * It just splits a mbox into a list of files: "0001" "0002" ..
5 * so you can process them further from there.
9 #include "string-list.h"
12 static const char git_mailsplit_usage
[] =
13 "git mailsplit [-d<prec>] [-f<n>] [-b] [--keep-cr] -o<directory> [(<mbox>|<Maildir>)...]";
15 static int is_from_line(const char *line
, int len
)
19 if (len
< 20 || memcmp("From ", line
, 5))
22 colon
= line
+ len
- 2;
31 if (!isdigit(colon
[-4]) ||
32 !isdigit(colon
[-2]) ||
33 !isdigit(colon
[-1]) ||
34 !isdigit(colon
[ 1]) ||
39 if (strtol(colon
+3, NULL
, 10) <= 90)
42 /* Ok, close enough */
46 static struct strbuf buf
= STRBUF_INIT
;
50 static int is_gtfrom(const struct strbuf
*buf
)
52 size_t min
= strlen(">From ");
58 ngt
= strspn(buf
->buf
, ">");
59 return ngt
&& starts_with(buf
->buf
+ ngt
, "From ");
62 /* Called with the first line (potentially partial)
63 * already in buf[] -- normally that should begin with
64 * the Unix "From " line. Write it into the specified
67 static int split_one(FILE *mbox
, const char *name
, int allow_bare
)
72 int is_bare
= !is_from_line(buf
.buf
, buf
.len
);
74 if (is_bare
&& !allow_bare
) {
75 fprintf(stderr
, "corrupt mailbox\n");
78 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
80 die_errno("cannot open output file '%s'", name
);
81 output
= xfdopen(fd
, "w");
83 /* Copy it out, while searching for a line that begins with
84 * "From " and having something that looks like a date format.
87 if (!keep_cr
&& buf
.len
> 1 && buf
.buf
[buf
.len
-1] == '\n' &&
88 buf
.buf
[buf
.len
-2] == '\r') {
89 strbuf_setlen(&buf
, buf
.len
-2);
90 strbuf_addch(&buf
, '\n');
93 if (mboxrd
&& is_gtfrom(&buf
))
94 strbuf_remove(&buf
, 0, 1);
96 if (fwrite(buf
.buf
, 1, buf
.len
, output
) != buf
.len
)
97 die_errno("cannot write output");
99 if (strbuf_getwholeline(&buf
, mbox
, '\n')) {
104 die_errno("cannot read mbox");
106 if (!is_bare
&& is_from_line(buf
.buf
, buf
.len
))
107 break; /* done with one message */
113 static int populate_maildir_list(struct string_list
*list
, const char *path
)
118 char *subs
[] = { "cur", "new", NULL
};
122 for (sub
= subs
; *sub
; ++sub
) {
124 name
= xstrfmt("%s/%s", path
, *sub
);
125 if ((dir
= opendir(name
)) == NULL
) {
128 error_errno("cannot opendir %s", name
);
132 while ((dent
= readdir(dir
)) != NULL
) {
133 if (dent
->d_name
[0] == '.')
136 name
= xstrfmt("%s/%s", *sub
, dent
->d_name
);
137 string_list_insert(list
, name
);
150 static int maildir_filename_cmp(const char *a
, const char *b
)
153 if (isdigit(*a
) && isdigit(*b
)) {
155 na
= strtol(a
, (char **)&a
, 10);
156 nb
= strtol(b
, (char **)&b
, 10);
159 /* strtol advanced our pointers */
163 return (unsigned char)*a
- (unsigned char)*b
;
168 return (unsigned char)*a
- (unsigned char)*b
;
171 static int split_maildir(const char *maildir
, const char *dir
,
172 int nr_prec
, int skip
)
178 struct string_list list
= STRING_LIST_INIT_DUP
;
180 list
.cmp
= maildir_filename_cmp
;
182 if (populate_maildir_list(&list
, maildir
) < 0)
185 for (i
= 0; i
< list
.nr
; i
++) {
189 file
= xstrfmt("%s/%s", maildir
, list
.items
[i
].string
);
191 f
= fopen(file
, "r");
193 error_errno("cannot open mail %s", file
);
197 if (strbuf_getwholeline(&buf
, f
, '\n')) {
198 error_errno("cannot read mail %s", file
);
202 name
= xstrfmt("%s/%0*d", dir
, nr_prec
, ++skip
);
203 split_one(f
, name
, 1);
215 string_list_clear(&list
, 1);
219 static int split_mbox(const char *file
, const char *dir
, int allow_bare
,
220 int nr_prec
, int skip
)
225 FILE *f
= !strcmp(file
, "-") ? stdin
: fopen(file
, "r");
229 error_errno("cannot open mbox %s", file
);
237 /* empty stdin is OK */
241 error(_("empty mbox: '%s'"), file
);
245 } while (isspace(peek
));
248 if (strbuf_getwholeline(&buf
, f
, '\n')) {
249 /* empty stdin is OK */
251 error("cannot read mbox %s", file
);
258 char *name
= xstrfmt("%s/%0*d", dir
, nr_prec
, ++skip
);
259 file_done
= split_one(f
, name
, allow_bare
);
271 int cmd_mailsplit(int argc
, const char **argv
, const char *prefix
)
273 int nr
= 0, nr_prec
= 4, num
= 0;
275 const char *dir
= NULL
;
277 static const char *stdin_only
[] = { "-", NULL
};
279 for (argp
= argv
+1; *argp
; argp
++) {
280 const char *arg
= *argp
;
285 if ( arg
[1] == 'd' ) {
286 nr_prec
= strtol(arg
+2, NULL
, 10);
287 if (nr_prec
< 3 || 10 <= nr_prec
)
288 usage(git_mailsplit_usage
);
290 } else if ( arg
[1] == 'f' ) {
291 nr
= strtol(arg
+2, NULL
, 10);
292 } else if ( arg
[1] == 'h' ) {
293 usage(git_mailsplit_usage
);
294 } else if ( arg
[1] == 'b' && !arg
[2] ) {
296 } else if (!strcmp(arg
, "--keep-cr")) {
298 } else if ( arg
[1] == 'o' && arg
[2] ) {
300 } else if (!strcmp(arg
, "--mboxrd")) {
302 } else if ( arg
[1] == '-' && !arg
[2] ) {
303 argp
++; /* -- marks end of options */
306 die("unknown option: %s", arg
);
311 /* Backwards compatibility: if no -o specified, accept
312 <mbox> <dir> or just <dir> */
313 switch (argc
- (argp
-argv
)) {
319 stdin_only
[0] = argp
[0];
324 usage(git_mailsplit_usage
);
327 /* New usage: if no more argument, parse stdin */
333 const char *arg
= *argp
++;
337 if (arg
[0] == '-' && arg
[1] == 0) {
338 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
340 error("cannot split patches from stdin");
348 if (stat(arg
, &argstat
) == -1) {
349 error_errno("cannot stat %s", arg
);
353 if (S_ISDIR(argstat
.st_mode
))
354 ret
= split_maildir(arg
, dir
, nr_prec
, nr
);
356 ret
= split_mbox(arg
, dir
, allow_bare
, nr_prec
, nr
);
359 error("cannot split patches from %s", arg
);