@matheusbsbs / matheus-bagliano / commits / 8b73edf498
Fork 0

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>

Junio C Hamano committed Oct 6, 2005 at 15:55 UTC 8b73edf498adf2895af7ff9c750283cf9325a632
2 files changed +96 -86
Documentation/git-mailsplit.txt
+5 -2
@@ -7,7 +7,7 @@ git-mailsplit - Totally braindamaged mbox splitter program.
7
8 SYNOPSIS
9 --------
10 -'git-mailsplit' [-d<prec>] <mbox> <directory>
10 +'git-mailsplit' [-d<prec>] [<mbox>] <directory>
11
12 DESCRIPTION
13 -----------
@@ -17,7 +17,8 @@ directory so you can process them further from there.
17 OPTIONS
18 -------
19 <mbox>::
20 - Mbox file to split.
20 + Mbox file to split. If not given, the mbox is read from
21 + the standard input.
22
23 <directory>::
24 Directory in which to place the individual messages.
@@ -31,6 +32,8 @@ OPTIONS
32 Author
33 ------
34 Written by Linus Torvalds <torvalds@osdl.org>
35 +and Junio C Hamano <junkio@cox.net>
36 +
37
38 Documentation
39 --------------
mailsplit.c
+91 -84
@@ -14,25 +14,10 @@
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <assert.h>
17 +#include "cache.h"
18
18 -static int usage(void)
19 -{
20 - fprintf(stderr, "git-mailsplit [-d<prec>] <mbox> <directory>\n");
21 - exit(1);
22 -}
23 -
24 -static int linelen(const char *map, unsigned long size)
25 -{
26 - int len = 0, c;
27 -
28 - do {
29 - c = *map;
30 - map++;
31 - size--;
32 - len++;
33 - } while (size && c != '\n');
34 - return len;
35 -}
19 +static const char git_mailsplit_usage[] =
20 +"git-mailsplit [-d<prec>] [<mbox>] <directory>";
21
22 static int is_from_line(const char *line, int len)
23 {
@@ -65,88 +50,110 @@ static int is_from_line(const char *line, int len)
50 return 1;
51 }
52
68 -static int parse_email(const void *map, unsigned long size)
53 +/* Could be as small as 64, enough to hold a Unix "From " line. */
54 +static char buf[4096];
55 +
56 +/* Called with the first line (potentially partial)
57 + * already in buf[] -- normally that should begin with
58 + * the Unix "From " line. Write it into the specified
59 + * file.
60 + */
61 +static int split_one(FILE *mbox, const char *name)
62 {
70 - unsigned long offset;
63 + FILE *output = NULL;
64 + int len = strlen(buf);
65 + int fd;
66 + int status = 0;
67
72 - if (size < 6 || memcmp("From ", map, 5))
68 + if (!is_from_line(buf, len))
69 goto corrupt;
70
75 - /* Make sure we don't trigger on this first line */
76 - map++; size--; offset=1;
71 + fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
72 + if (fd < 0)
73 + die("cannot open output file %s", name);
74 + output = fdopen(fd, "w");
75
78 - /*
79 - * Search for a line beginning with "From ", and
80 - * having something that looks like a date format.
76 + /* Copy it out, while searching for a line that begins with
77 + * "From " and having something that looks like a date format.
78 */
82 - do {
83 - int len = linelen(map, size);
84 - if (is_from_line(map, len))
85 - return offset;
86 - map += len;
87 - size -= len;
88 - offset += len;
89 - } while (size);
90 - return offset;
91 -
92 -corrupt:
79 + for (;;) {
80 + int is_partial = (buf[len-1] != '\n');
81 +
82 + if (fputs(buf, output) == EOF)
83 + die("cannot write output");
84 +
85 + if (fgets(buf, sizeof(buf), mbox) == NULL) {
86 + if (feof(mbox)) {
87 + status = 1;
88 + break;
89 + }
90 + die("cannot read mbox");
91 + }
92 + len = strlen(buf);
93 + if (!is_partial && is_from_line(buf, len))
94 + break; /* done with one message */
95 + }
96 + fclose(output);
97 + return status;
98 +
99 + corrupt:
100 + if (output)
101 + fclose(output);
102 + unlink(name);
103 fprintf(stderr, "corrupt mailbox\n");
104 exit(1);
105 }
106
97 -int main(int argc, char **argv)
107 +int main(int argc, const char **argv)
108 {
99 - int fd, nr, nr_prec = 4;
100 - struct stat st;
101 - unsigned long size;
102 - void *map;
103 -
104 - if (argc == 4 && !strncmp(argv[1], "-d", 2)) {
105 - nr_prec = strtol(argv[1] + 2, NULL, 10);
106 - if (nr_prec < 3 || 10 <= nr_prec)
107 - usage();
108 - argc--; argv++;
109 - }
110 - if (argc != 3)
111 - usage();
112 - fd = open(argv[1], O_RDONLY);
113 - if (fd < 0) {
114 - perror(argv[1]);
115 - exit(1);
116 - }
117 - if (chdir(argv[2]) < 0)
118 - usage();
119 - if (fstat(fd, &st) < 0) {
120 - perror("stat");
121 - exit(1);
109 + int i, nr, nr_prec = 4;
110 + FILE *mbox = NULL;
111 +
112 + for (i = 1; i < argc; i++) {
113 + const char *arg = argv[i];
114 +
115 + if (arg[0] != '-')
116 + break;
117 + /* do flags here */
118 + if (!strncmp(arg, "-d", 2)) {
119 + nr_prec = strtol(arg + 2, NULL, 10);
120 + if (nr_prec < 3 || 10 <= nr_prec)
121 + usage(git_mailsplit_usage);
122 + continue;
123 + }
124 }
123 - size = st.st_size;
124 - map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
125 - if (map == MAP_FAILED) {
126 - perror("mmap");
127 - close(fd);
128 - exit(1);
125 +
126 + /* Either one remaining arg (dir), or two (mbox and dir) */
127 + switch (argc - i) {
128 + case 1:
129 + mbox = stdin;
130 + break;
131 + case 2:
132 + if ((mbox = fopen(argv[i], "r")) == NULL)
133 + die("cannot open mbox %s for reading", argv[i]);
134 + break;
135 + default:
136 + usage(git_mailsplit_usage);
137 }
130 - close(fd);
138 + if (chdir(argv[argc - 1]) < 0)
139 + usage(git_mailsplit_usage);
140 +
141 nr = 0;
132 - do {
142 + if (fgets(buf, sizeof(buf), mbox) == NULL)
143 + die("cannot read mbox");
144 +
145 + for (;;) {
146 char name[10];
134 - unsigned long len = parse_email(map, size);
135 - assert(len <= size);
147 +
148 sprintf(name, "%0*d", nr_prec, ++nr);
137 - fd = open(name, O_WRONLY | O_CREAT | O_EXCL, 0666);
138 - if (fd < 0) {
139 - perror(name);
140 - exit(1);
141 - }
142 - if (write(fd, map, len) != len) {
143 - perror("write");
149 + switch (split_one(mbox, name)) {
150 + case 0:
151 + break;
152 + case 1:
153 + printf("%d\n", nr);
154 + return 0;
155 + default:
156 exit(1);
157 }
146 - close(fd);
147 - map += len;
148 - size -= len;
149 - } while (size > 0);
150 - printf("%d\n", nr);
151 - return 0;
158 + }
159 }