mailsplit: allow feeding mbox from standard input.
When mbox argument is missing, read the mailbox from the standard input. Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
parent
54ba6013b4
commit
8b73edf498
@ -7,7 +7,7 @@ git-mailsplit - Totally braindamaged mbox splitter program.
|
|||||||
|
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
--------
|
--------
|
||||||
'git-mailsplit' [-d<prec>] <mbox> <directory>
|
'git-mailsplit' [-d<prec>] [<mbox>] <directory>
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
-----------
|
-----------
|
||||||
@ -17,7 +17,8 @@ directory so you can process them further from there.
|
|||||||
OPTIONS
|
OPTIONS
|
||||||
-------
|
-------
|
||||||
<mbox>::
|
<mbox>::
|
||||||
Mbox file to split.
|
Mbox file to split. If not given, the mbox is read from
|
||||||
|
the standard input.
|
||||||
|
|
||||||
<directory>::
|
<directory>::
|
||||||
Directory in which to place the individual messages.
|
Directory in which to place the individual messages.
|
||||||
@ -31,6 +32,8 @@ OPTIONS
|
|||||||
Author
|
Author
|
||||||
------
|
------
|
||||||
Written by Linus Torvalds <torvalds@osdl.org>
|
Written by Linus Torvalds <torvalds@osdl.org>
|
||||||
|
and Junio C Hamano <junkio@cox.net>
|
||||||
|
|
||||||
|
|
||||||
Documentation
|
Documentation
|
||||||
--------------
|
--------------
|
||||||
|
173
mailsplit.c
173
mailsplit.c
@ -14,25 +14,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include "cache.h"
|
||||||
|
|
||||||
static int usage(void)
|
static const char git_mailsplit_usage[] =
|
||||||
{
|
"git-mailsplit [-d<prec>] [<mbox>] <directory>";
|
||||||
fprintf(stderr, "git-mailsplit [-d<prec>] <mbox> <directory>\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int linelen(const char *map, unsigned long size)
|
|
||||||
{
|
|
||||||
int len = 0, c;
|
|
||||||
|
|
||||||
do {
|
|
||||||
c = *map;
|
|
||||||
map++;
|
|
||||||
size--;
|
|
||||||
len++;
|
|
||||||
} while (size && c != '\n');
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int is_from_line(const char *line, int len)
|
static int is_from_line(const char *line, int len)
|
||||||
{
|
{
|
||||||
@ -65,88 +50,110 @@ static int is_from_line(const char *line, int len)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_email(const void *map, unsigned long size)
|
/* Could be as small as 64, enough to hold a Unix "From " line. */
|
||||||
{
|
static char buf[4096];
|
||||||
unsigned long offset;
|
|
||||||
|
|
||||||
if (size < 6 || memcmp("From ", map, 5))
|
/* Called with the first line (potentially partial)
|
||||||
|
* already in buf[] -- normally that should begin with
|
||||||
|
* the Unix "From " line. Write it into the specified
|
||||||
|
* file.
|
||||||
|
*/
|
||||||
|
static int split_one(FILE *mbox, const char *name)
|
||||||
|
{
|
||||||
|
FILE *output = NULL;
|
||||||
|
int len = strlen(buf);
|
||||||
|
int fd;
|
||||||
|
int status = 0;
|
||||||
|
|
||||||
|
if (!is_from_line(buf, len))
|
||||||
goto corrupt;
|
goto corrupt;
|
||||||
|
|
||||||
/* Make sure we don't trigger on this first line */
|
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
||||||
map++; size--; offset=1;
|
if (fd < 0)
|
||||||
|
die("cannot open output file %s", name);
|
||||||
|
output = fdopen(fd, "w");
|
||||||
|
|
||||||
/*
|
/* Copy it out, while searching for a line that begins with
|
||||||
* Search for a line beginning with "From ", and
|
* "From " and having something that looks like a date format.
|
||||||
* having something that looks like a date format.
|
|
||||||
*/
|
*/
|
||||||
do {
|
for (;;) {
|
||||||
int len = linelen(map, size);
|
int is_partial = (buf[len-1] != '\n');
|
||||||
if (is_from_line(map, len))
|
|
||||||
return offset;
|
|
||||||
map += len;
|
|
||||||
size -= len;
|
|
||||||
offset += len;
|
|
||||||
} while (size);
|
|
||||||
return offset;
|
|
||||||
|
|
||||||
corrupt:
|
if (fputs(buf, output) == EOF)
|
||||||
|
die("cannot write output");
|
||||||
|
|
||||||
|
if (fgets(buf, sizeof(buf), mbox) == NULL) {
|
||||||
|
if (feof(mbox)) {
|
||||||
|
status = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
die("cannot read mbox");
|
||||||
|
}
|
||||||
|
len = strlen(buf);
|
||||||
|
if (!is_partial && is_from_line(buf, len))
|
||||||
|
break; /* done with one message */
|
||||||
|
}
|
||||||
|
fclose(output);
|
||||||
|
return status;
|
||||||
|
|
||||||
|
corrupt:
|
||||||
|
if (output)
|
||||||
|
fclose(output);
|
||||||
|
unlink(name);
|
||||||
fprintf(stderr, "corrupt mailbox\n");
|
fprintf(stderr, "corrupt mailbox\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
int fd, nr, nr_prec = 4;
|
int i, nr, nr_prec = 4;
|
||||||
struct stat st;
|
FILE *mbox = NULL;
|
||||||
unsigned long size;
|
|
||||||
void *map;
|
|
||||||
|
|
||||||
if (argc == 4 && !strncmp(argv[1], "-d", 2)) {
|
for (i = 1; i < argc; i++) {
|
||||||
nr_prec = strtol(argv[1] + 2, NULL, 10);
|
const char *arg = argv[i];
|
||||||
if (nr_prec < 3 || 10 <= nr_prec)
|
|
||||||
usage();
|
if (arg[0] != '-')
|
||||||
argc--; argv++;
|
break;
|
||||||
|
/* do flags here */
|
||||||
|
if (!strncmp(arg, "-d", 2)) {
|
||||||
|
nr_prec = strtol(arg + 2, NULL, 10);
|
||||||
|
if (nr_prec < 3 || 10 <= nr_prec)
|
||||||
|
usage(git_mailsplit_usage);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (argc != 3)
|
|
||||||
usage();
|
/* Either one remaining arg (dir), or two (mbox and dir) */
|
||||||
fd = open(argv[1], O_RDONLY);
|
switch (argc - i) {
|
||||||
if (fd < 0) {
|
case 1:
|
||||||
perror(argv[1]);
|
mbox = stdin;
|
||||||
exit(1);
|
break;
|
||||||
|
case 2:
|
||||||
|
if ((mbox = fopen(argv[i], "r")) == NULL)
|
||||||
|
die("cannot open mbox %s for reading", argv[i]);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
usage(git_mailsplit_usage);
|
||||||
}
|
}
|
||||||
if (chdir(argv[2]) < 0)
|
if (chdir(argv[argc - 1]) < 0)
|
||||||
usage();
|
usage(git_mailsplit_usage);
|
||||||
if (fstat(fd, &st) < 0) {
|
|
||||||
perror("stat");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
size = st.st_size;
|
|
||||||
map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
||||||
if (map == MAP_FAILED) {
|
|
||||||
perror("mmap");
|
|
||||||
close(fd);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
nr = 0;
|
nr = 0;
|
||||||
do {
|
if (fgets(buf, sizeof(buf), mbox) == NULL)
|
||||||
|
die("cannot read mbox");
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
char name[10];
|
char name[10];
|
||||||
unsigned long len = parse_email(map, size);
|
|
||||||
assert(len <= size);
|
|
||||||
sprintf(name, "%0*d", nr_prec, ++nr);
|
sprintf(name, "%0*d", nr_prec, ++nr);
|
||||||
fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
|
switch (split_one(mbox, name)) {
|
||||||
if (fd < 0) {
|
case 0:
|
||||||
perror(name);
|
break;
|
||||||
|
case 1:
|
||||||
|
printf("%d\n", nr);
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (write(fd, map, len) != len) {
|
}
|
||||||
perror("write");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
map += len;
|
|
||||||
size -= len;
|
|
||||||
} while (size > 0);
|
|
||||||
printf("%d\n", nr);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user