[PATCH] git-format-patch-script and mailinfo updates.
[git/gitweb.git] / tools / mailinfo.c
blob4dcc099195817e29c9ec6091f54d26184a44bcfd
1 /*
2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
4 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <ctype.h>
10 static FILE *cmitmsg, *patchfile;
12 static char line[1000];
13 static char date[1000];
14 static char name[1000];
15 static char email[1000];
16 static char subject[1000];
18 static char *sanity_check(char *name, char *email)
20 int len = strlen(name);
21 if (len < 3 || len > 60)
22 return email;
23 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
24 return email;
25 return name;
28 static int handle_from(char *line)
30 char *at = strchr(line, '@');
31 char *dst;
33 if (!at)
34 return 0;
37 * If we already have one email, don't take any confusing lines
39 if (*email && strchr(at+1, '@'))
40 return 0;
42 while (at > line) {
43 char c = at[-1];
44 if (isspace(c) || c == '<')
45 break;
46 at--;
48 dst = email;
49 for (;;) {
50 unsigned char c = *at;
51 if (!c || c == '>' || isspace(c))
52 break;
53 *at++ = ' ';
54 *dst++ = c;
56 *dst++ = 0;
58 at = line + strlen(line);
59 while (at > line) {
60 unsigned char c = *--at;
61 if (isalnum(c))
62 break;
63 *at = 0;
66 at = line;
67 for (;;) {
68 unsigned char c = *at;
69 if (!c)
70 break;
71 if (isalnum(c))
72 break;
73 at++;
76 at = sanity_check(at, email);
78 strcpy(name, at);
79 return 1;
82 static void handle_date(char *line)
84 strcpy(date, line);
87 static void handle_subject(char *line)
89 strcpy(subject, line);
92 static void add_subject_line(char *line)
94 while (isspace(*line))
95 line++;
96 *--line = ' ';
97 strcat(subject, line);
100 static void check_line(char *line, int len)
102 static int cont = -1;
103 if (!memcmp(line, "From:", 5) && isspace(line[5])) {
104 handle_from(line+6);
105 cont = 0;
106 return;
108 if (!memcmp(line, "Date:", 5) && isspace(line[5])) {
109 handle_date(line+6);
110 cont = 0;
111 return;
113 if (!memcmp(line, "Subject:", 8) && isspace(line[8])) {
114 handle_subject(line+9);
115 cont = 1;
116 return;
118 if (isspace(*line)) {
119 switch (cont) {
120 case 0:
121 fprintf(stderr, "I don't do 'Date:' or 'From:' line continuations\n");
122 break;
123 case 1:
124 add_subject_line(line);
125 return;
126 default:
127 break;
130 cont = -1;
133 static char * cleanup_subject(char *subject)
135 for (;;) {
136 char *p;
137 int len, remove;
138 switch (*subject) {
139 case 'r': case 'R':
140 if (!memcmp("e:", subject+1, 2)) {
141 subject +=3;
142 continue;
144 break;
145 case ' ': case '\t': case ':':
146 subject++;
147 continue;
149 case '[':
150 p = strchr(subject, ']');
151 if (!p) {
152 subject++;
153 continue;
155 len = strlen(p);
156 remove = p - subject;
157 if (remove <= len *2) {
158 subject = p+1;
159 continue;
161 break;
163 return subject;
167 static void cleanup_space(char *buf)
169 unsigned char c;
170 while ((c = *buf) != 0) {
171 buf++;
172 if (isspace(c)) {
173 buf[-1] = ' ';
174 c = *buf;
175 while (isspace(c)) {
176 int len = strlen(buf);
177 memmove(buf, buf+1, len);
178 c = *buf;
184 static void handle_rest(void)
186 char *sub = cleanup_subject(subject);
187 cleanup_space(name);
188 cleanup_space(date);
189 cleanup_space(email);
190 cleanup_space(sub);
191 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name, email, sub, date);
192 FILE *out = cmitmsg;
194 do {
195 if (!memcmp("diff -", line, 6) ||
196 !memcmp("---", line, 3) ||
197 !memcmp("Index: ", line, 7))
198 out = patchfile;
200 fputs(line, out);
201 } while (fgets(line, sizeof(line), stdin) != NULL);
203 if (out == cmitmsg) {
204 fprintf(stderr, "No patch found\n");
205 exit(1);
208 fclose(cmitmsg);
209 fclose(patchfile);
212 static int eatspace(char *line)
214 int len = strlen(line);
215 while (len > 0 && isspace(line[len-1]))
216 line[--len] = 0;
217 return len;
220 static void handle_body(void)
222 int has_from = 0;
223 int has_date = 0;
225 /* First lines of body can have From: and Date: */
226 while (fgets(line, sizeof(line), stdin) != NULL) {
227 int len = eatspace(line);
228 if (!len)
229 continue;
230 if (!memcmp("From:", line, 5) && isspace(line[5])) {
231 if (!has_from && handle_from(line+6)) {
232 has_from = 1;
233 continue;
236 if (!memcmp("Date:", line, 5) && isspace(line[5])) {
237 if (!has_date) {
238 handle_date(line+6);
239 has_date = 1;
240 continue;
243 line[len] = '\n';
244 handle_rest();
245 break;
249 static void usage(void)
251 fprintf(stderr, "mailinfo msg-file path-file < email\n");
252 exit(1);
255 int main(int argc, char ** argv)
257 if (argc != 3)
258 usage();
259 cmitmsg = fopen(argv[1], "w");
260 if (!cmitmsg) {
261 perror(argv[1]);
262 exit(1);
264 patchfile = fopen(argv[2], "w");
265 if (!patchfile) {
266 perror(argv[2]);
267 exit(1);
269 while (fgets(line, sizeof(line), stdin) != NULL) {
270 int len = eatspace(line);
271 if (!len) {
272 handle_body();
273 break;
275 check_line(line, len);
277 return 0;