*/
#include "h/mh.h"
+#include "sbr/fmt_new.h"
+#include "sbr/dtime.h"
+#include "scansbr.h"
+#include "sbr/m_name.h"
+#include "sbr/getarguments.h"
+#include "sbr/seq_setprev.h"
+#include "sbr/seq_save.h"
+#include "sbr/smatch.h"
#include "sbr/m_convert.h"
#include "sbr/getfolder.h"
#include "sbr/folder_read.h"
#include "sbr/seq_getnum.h"
#include "sbr/error.h"
#include "h/fmt_scan.h"
-#include "h/scansbr.h"
#include "h/tws.h"
#include "h/mts.h"
#include "h/done.h"
#include "h/utils.h"
#include "sbr/m_maildir.h"
#include "sbr/terminal.h"
+#include "sbr/maildir_read_and_sort.h"
#define SCAN_SWITCHES \
X("clear", 0, CLRSW) \
{
bool clearflag = false;
bool hdrflag = false;
- int ontty;
+ int ontty = 0;
int width = -1;
bool revflag = false;
int i, state, msgnum;
char **argp, *nfs, **arguments;
struct msgs_array msgs = { 0, 0, NULL };
struct msgs *mp;
+ charstring_t scanl = NULL;
FILE *in;
if (nmh_init(argv[0], true, true)) { return 1; }
while ((cp = *argp++)) {
if (*cp == '-') {
switch (smatch (++cp, switches)) {
- case AMBIGSW:
+ case AMBIGSW:
ambigsw (cp, switches);
done (1);
- case UNKWNSW:
+ case UNKWNSW:
die("-%s unknown", cp);
- case HELPSW:
+ case HELPSW:
snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
invo_name);
print_help (buf, switches, 1);
print_version(invo_name);
done (0);
- case CLRSW:
+ case CLRSW:
clearflag = true;
continue;
- case NCLRSW:
+ case NCLRSW:
clearflag = false;
continue;
- case FORMSW:
+ case FORMSW:
if (!(form = *argp++) || *form == '-')
die("missing argument to %s", argp[-2]);
format = NULL;
continue;
- case FMTSW:
+ case FMTSW:
if (!(format = *argp++) || *format == '-')
die("missing argument to %s", argp[-2]);
form = NULL;
continue;
- case HEADSW:
+ case HEADSW:
hdrflag = true;
continue;
- case NHEADSW:
+ case NHEADSW:
hdrflag = false;
continue;
- case WIDTHSW:
+ case WIDTHSW:
if (!(cp = *argp++) || *cp == '-')
die("missing argument to %s", argp[-2]);
width = atoi (cp);
*/
nfs = new_fs (form, format, FORMAT);
- /*
- * We are scanning a maildrop file
- */
if (file) {
+ /*
+ * We have a -file argument
+ */
if (msgs.size)
die("\"msgs\" not allowed with -file");
if (folder)
in = stdin;
file = "stdin";
} else {
- if ((in = fopen (file, "r")) == NULL)
- adios (file, "unable to open");
+ /* check if "file" is really a Maildir folder */
+ struct stat st;
+ if (stat (file, &st) == NOTOK)
+ adios (file, "unable to stat");
+ if (!(st.st_mode & S_IFDIR)) {
+ if ((in = fopen (file, "r")) == NULL)
+ adios (file, "unable to open");
+ } else {
+ /*
+ * We are scanning a Maildir folder
+ */
+ struct Maildir_entry *Maildir;
+ int num_maildir_entries;
+ int msgnum = 0;
+ char *fnp;
+ FILE *in;
+ int i;
+
+ maildir_read_and_sort(file, &Maildir, &num_maildir_entries);
+ for (i = 0; i < num_maildir_entries; i++) {
+ msgnum++;
+ fnp = Maildir[i].filename;
+
+ if ((in = fopen (fnp, "r")) == NULL) {
+ admonish (fnp, "unable to open message");
+ continue;
+ }
+
+ switch (state = scan (in, msgnum, 0, nfs, width,
+ 0, 0, hdrflag ? file : NULL,
+ 0L, 1, &scanl)) {
+ case SCNMSG:
+ case SCNERR:
+ break;
+
+ default:
+ die("scan() botch (%d)", state);
+
+ case SCNEOF:
+ inform("message %d: empty", msgnum);
+ break;
+ }
+ if (scanl)
+ charstring_clear(scanl);
+ scan_finished ();
+ hdrflag = false;
+ fclose (in);
+ if (ontty)
+ fflush (stdout);
+ }
+ done (0);
+ }
}
+ /*
+ * We are scanning a maildrop file
+ */
if (hdrflag) {
printf ("FOLDER %s\t%s\n", file, dtimenow (1));
}
scan_detect_mbox_style (in);
for (msgnum = 1; ; ++msgnum) {
- charstring_t scanl = NULL;
-
state = scan (in, msgnum, -1, nfs, width, 0, 0,
hdrflag ? file : NULL, 0L, 1, &scanl);
- charstring_free (scanl);
- if (state != SCNMSG && state != SCNENC)
+ if (scanl)
+ charstring_clear(scanl);
+ if (state != SCNMSG)
break;
}
+ charstring_free (scanl);
scan_finished ();
fclose (in);
done (0);
(revflag ? msgnum >= mp->lowsel : msgnum <= mp->hghsel);
msgnum += (revflag ? -1 : 1)) {
if (is_selected(mp, msgnum)) {
- charstring_t scanl = NULL;
if ((in = fopen (cp = m_name (msgnum), "r")) == NULL) {
admonish (cp, "unable to open message");
switch (state = scan (in, msgnum, 0, nfs, width,
msgnum == mp->curmsg, unseen,
folder, 0L, 1, &scanl)) {
- case SCNMSG:
- case SCNENC:
- case SCNERR:
+ case SCNMSG:
+ case SCNERR:
break;
- default:
+ default:
die("scan() botch (%d)", state);
- case SCNEOF:
+ case SCNEOF:
inform("message %d: empty", msgnum);
break;
}
- charstring_free (scanl);
+ if (scanl)
+ charstring_clear(scanl);
scan_finished ();
hdrflag = false;
fclose (in);
fflush (stdout);
}
}
+ charstring_free (scanl);
ivector_free (seqnum);
folder_free (mp); /* free folder/message structure */