+}
+
+static void parse_maildir_file(void)
+{
+ char *c;
+ int want = 0;
+
+ reset_buf();
+ while (c = get_line())
+ {
+ // printf("<%s>\n", c);
+ if (!*c)
+ {
+ putchar('\n');
+ return;
+ }
+ else if (*c == ' ' || *c == '\t')
+ {
+ if (want)
+ {
+ while (*c == ' ' || *c == '\t')
+ c++;
+ putchar(' ');
+ fputs(c, stdout);
+ }
+ }
+ else
+ {
+ want = want_header(c);
+ if (want)
+ {
+ putchar('\n');
+ fputs(c, stdout);
+ }
+ }
+ }
+
+ printf("\nX-Warning: Incomplete mail\n\n");
+}
+
+static void parse_maildir(char *dir, char *subdir)
+{
+ char buf[4096];
+ snprintf(buf, sizeof(buf), "%s/%s", dir, subdir);
+
+ DIR *d = opendir(buf);
+ if (!d)
+ return;
+
+ struct dirent *e;
+ while (e = readdir(d))
+ {
+ if (e->d_name[0] == '.')
+ continue;
+ snprintf(buf, sizeof(buf), "%s/%s/%s", dir, subdir, e->d_name);
+ mbox_fd = open(buf, O_RDONLY);
+ if (mbox_fd < 0)
+ continue;
+ parse_maildir_file();
+ close(mbox_fd);
+ }
+
+ closedir(d);
+}
+
+static void usage(void)
+{
+ fprintf(stderr, "Usage: mparse [-f <mailbox>] <header-field> ...\n");
+ exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+ int opt;
+ char *folder = NULL;
+
+ while ((opt = getopt(argc, argv, "f:")) >= 0)
+ switch (opt)
+ {
+ case 'f':
+ folder = optarg;
+ break;
+ default:
+ usage();
+ }
+
+ field_names = argv + optind;
+ num_fields = argc - optind;
+
+ if (!folder)
+ {
+ parse_mbox();
+ return 0;
+ }
+
+ struct stat st;
+ if (stat(folder, &st) < 0)
+ die("Cannot stat %s: %m", folder);
+
+ if (st.st_mode & S_IFDIR)
+ {
+ parse_maildir(folder, "cur");
+ parse_maildir(folder, "new");
+ }
+ else
+ {
+ mbox_fd = open(folder, O_RDONLY);
+ if (mbox_fd < 0)
+ die("Cannot open %s: %m", folder);
+ parse_mbox();
+ close(mbox_fd);
+ }