7 static int uc_len(char *s
)
9 int c
= (unsigned char) s
[0];
25 static int uc_wid(char *s
)
30 static char *msg_dec(char *msg
, long msz
, char *hdr
)
32 char *val
= msg_get(msg
, msz
, hdr
);
37 val_len
= hdrlen(val
, msg
+ msz
- val
) - 1;
38 buf
= malloc(val_len
+ 1);
39 memcpy(buf
, val
, val_len
);
41 ret
= msg_hdrdec(buf
);
46 static int msg_stat(char *msg
, long msz
)
48 char *val
= msg_get(msg
, msz
, "status:");
51 val
+= strlen("status:");
52 while (isspace((unsigned char) val
[0]))
57 static char *fieldformat(char *msg
, long msz
, char *hdr
, int wid
)
61 char *val
= msg_dec(msg
, msz
, hdr
);
68 end
= strchr(val
, '\0');
71 while (val
< end
&& isspace((unsigned char) *val
))
74 while (val
< end
&& (wid
<= 0 || dst_wid
< wid
)) {
77 int c
= (unsigned char) *val
;
78 sbuf_chr(dst
, isblank(c
) || !isprint(c
) ? ' ' : c
);
80 sbuf_mem(dst
, val
, l
);
82 dst_wid
+= uc_wid(val
);
86 while (dst_wid
++ < wid
)
89 return sbuf_done(dst
);
92 static char *segment(char *d
, char *s
, int m
)
94 char *r
= strchr(s
, m
);
95 char *e
= r
? r
+ 1 : strchr(s
, '\0');
102 "usage: neatmail mk [options] mbox\n\n"
104 " -0 fmt \tmessage first line format (e.g., 20from:40subject:)\n"
105 " -1 fmt \tmessage second line format\n"
106 " -f n \tthe first message to list\n";
111 char *ln
[4] = {NULL
};
114 for (i
= 0; argv
[i
] && argv
[i
][0] == '-'; i
++) {
115 if (argv
[i
][1] == 'f') {
116 first
= atoi(argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
]);
119 if (argv
[i
][1] == '0' || argv
[i
][1] == '1') {
120 int idx
= argv
[i
][1] - '0';
121 ln
[idx
] = argv
[i
][2] ? argv
[i
] + 2 : argv
[++i
];
129 mbox
= mbox_open(argv
[i
]);
131 fprintf(stderr
, "neatmail: cannot open <%s>\n", argv
[i
]);
134 for (i
= first
; i
< mbox_len(mbox
); i
++) {
137 mbox_get(mbox
, i
, &msg
, &msz
);
138 printf("%c%04d", msg_stat(msg
, msz
), i
);
139 for (j
= 0; ln
[j
]; j
++) {
141 char *tok
= malloc(strlen(ln
[j
]) + 1);
144 while ((cln
= segment(tok
, cln
, ':')) && tok
[0]) {
148 while (isdigit((unsigned char) *hdr
))
150 val
= fieldformat(msg
, msz
, hdr
, atoi(fmt
));