2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
10 static FILE *cmitmsg, *patchfile, *filelist;
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)
23 if (strchr(name, '@') || strchr(name, '<') || strchr(name, '>'))
28 static int handle_from(char *line)
30 char *at = strchr(line, '@');
37 * If we already have one email, don't take any confusing lines
39 if (*email && strchr(at+1, '@'))
44 if (isspace(c) || c == '<')
50 unsigned char c = *at;
51 if (!c || c == '>' || isspace(c))
58 at = line + strlen(line);
60 unsigned char c = *--at;
68 unsigned char c = *at;
76 at = sanity_check(at, email);
82 static void handle_date(char *line)
87 static void handle_subject(char *line)
89 strcpy(subject, line);
92 static void add_subject_line(char *line)
94 while (isspace(*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])) {
108 if (!memcmp(line, "Date:", 5) && isspace(line[5])) {
113 if (!memcmp(line, "Subject:", 8) && isspace(line[8])) {
114 handle_subject(line+9);
118 if (isspace(*line)) {
121 fprintf(stderr, "I don't do 'Date:' or 'From:' line continuations\n");
124 add_subject_line(line);
133 static char * cleanup_subject(char *subject)
140 if (!memcmp("e:", subject+1, 2)) {
145 case ' ': case '\t': case ':':
150 p = strchr(subject, ']');
156 remove = p - subject;
157 if (remove <= len *2) {
167 static void cleanup_space(char *buf)
170 while ((c = *buf) != 0) {
176 int len = strlen(buf);
177 memmove(buf, buf+1, len);
185 * Hacky hacky. This depends not only on -p1, but on
186 * filenames not having some special characters in them,
189 static void show_filename(char *line)
192 char *name = strchr(line, '/');
194 if (!name || !isspace(*line))
199 unsigned char c = name[len];
206 case '\t': case '\n':
209 /* patch tends to special-case these things.. */
215 /* remove ".orig" from the end - common patch behaviour */
216 if (len > 5 && !memcmp(name+len-5, ".orig", 5))
220 fprintf(filelist, "%.*s\n", len, name);
223 static void handle_rest(void)
225 char *sub = cleanup_subject(subject);
228 cleanup_space(email);
230 printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n", name, email, sub, date);
234 /* Track filename information from the patch.. */
235 if (!memcmp("---", line, 3)) {
237 show_filename(line+3);
240 if (!memcmp("+++", line, 3))
241 show_filename(line+3);
244 } while (fgets(line, sizeof(line), stdin) != NULL);
246 if (out == cmitmsg) {
247 fprintf(stderr, "No patch found\n");
255 static int eatspace(char *line)
257 int len = strlen(line);
258 while (len > 0 && isspace(line[len-1]))
263 static void handle_body(void)
267 /* First line of body can be a From: */
268 while (fgets(line, sizeof(line), stdin) != NULL) {
269 int len = eatspace(line);
272 if (!memcmp("From:", line, 5) && isspace(line[5])) {
273 if (!has_from && handle_from(line+6)) {
284 static void usage(void)
286 fprintf(stderr, "mailinfo msg-file path-file filelist-file < email\n");
290 int main(int argc, char ** argv)
294 cmitmsg = fopen(argv[1], "w");
299 patchfile = fopen(argv[2], "w");
304 filelist = fopen(argv[3], "w");
309 while (fgets(line, sizeof(line), stdin) != NULL) {
310 int len = eatspace(line);
315 check_line(line, len);